@@ -248,7 +248,7 @@ new #[Title('Todos')] class extends Component
|
||||
$ids = $this->orderedTodoIds;
|
||||
$currentIndex = array_search($todoId, $ids, true);
|
||||
|
||||
if ($currentIndex === false || $ids === []) {
|
||||
if ($currentIndex === false) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -258,38 +258,55 @@ new #[Title('Todos')] class extends Component
|
||||
return;
|
||||
}
|
||||
|
||||
$this->reorderTodo($todoId, $ids, $target);
|
||||
$this->reorderTodo($todoId, $target);
|
||||
}
|
||||
|
||||
public function handleSort(int $todoId, int $position): void
|
||||
{
|
||||
$this->todoForUser($todoId);
|
||||
|
||||
$ids = $this->orderedTodoIds;
|
||||
$currentIndex = array_search($todoId, $ids, true);
|
||||
|
||||
if ($currentIndex === false || $ids === []) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->reorderTodo($todoId, $ids, $position);
|
||||
$this->reorderTodo($todoId, $position);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<int, int> $ids
|
||||
* Move a todo to the given index of the currently visible order. The
|
||||
* reordered todos are written back into the slots they already occupy in
|
||||
* the user's full priority order, so reordering under an active filter
|
||||
* permutes the visible todos without renumbering them from zero and
|
||||
* colliding with the todos the filter hides.
|
||||
*/
|
||||
private function reorderTodo(int $todoId, array $ids, int $newIndex): void
|
||||
private function reorderTodo(int $todoId, int $newIndex): void
|
||||
{
|
||||
unset($ids[array_search($todoId, $ids, true)]);
|
||||
$ids = array_values($ids);
|
||||
$visibleIds = $this->orderedTodoIds;
|
||||
$currentIndex = array_search($todoId, $visibleIds, true);
|
||||
|
||||
$newIndex = max(0, min($newIndex, count($ids)));
|
||||
if ($currentIndex === false) {
|
||||
return;
|
||||
}
|
||||
|
||||
array_splice($ids, $newIndex, 0, $todoId);
|
||||
$newIndex = max(0, min($newIndex, count($visibleIds) - 1));
|
||||
|
||||
if ($newIndex === $currentIndex) {
|
||||
return;
|
||||
}
|
||||
|
||||
$visibleOrder = $visibleIds;
|
||||
unset($visibleOrder[$currentIndex]);
|
||||
array_splice($visibleOrder, $newIndex, 0, $todoId);
|
||||
|
||||
$ids = Todo::query()->forUser(auth()->id())->priority()->pluck('id')->all();
|
||||
|
||||
$slots = array_keys(array_intersect($ids, $visibleIds));
|
||||
|
||||
foreach ($slots as $visibleSlot => $slot) {
|
||||
$ids[$slot] = $visibleOrder[$visibleSlot];
|
||||
}
|
||||
|
||||
foreach ($ids as $position => $id) {
|
||||
Todo::query()->forUser(auth()->id())->whereKey($id)->update(['position' => $position]);
|
||||
}
|
||||
|
||||
unset($this->todos, $this->orderedTodoIds);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -407,7 +424,7 @@ new #[Title('Todos')] class extends Component
|
||||
@forelse ($this->todos as $todo)
|
||||
<flux:table.row :key="$todo->id" wire:sort:item="{{ $todo->id }}" data-test="todo-row">
|
||||
<flux:table.cell class="py-0 text-zinc-300 dark:text-zinc-600">
|
||||
<div wire:sort:handle class="inline-flex cursor-grab" :title="__('Reorder')">
|
||||
<div wire:sort:handle class="inline-flex cursor-grab" title="{{ __('Reorder') }}">
|
||||
<flux:icon.chevrons-up-down variant="micro" />
|
||||
</div>
|
||||
</flux:table.cell>
|
||||
|
||||
Reference in New Issue
Block a user