From d9ff159cf3643a2b06b8423da83beef4c26c3ea9 Mon Sep 17 00:00:00 2001 From: Jesse Leite Date: Tue, 5 Nov 2024 07:36:53 -0500 Subject: [PATCH] wip --- src/CP/Navigation/NavBuilder.php | 34 +++++++----- .../Navigation/NavPreferencesNormalizer.php | 4 +- src/CP/Navigation/NavTransformer.php | 55 ++++++++++++++----- 3 files changed, 64 insertions(+), 29 deletions(-) diff --git a/src/CP/Navigation/NavBuilder.php b/src/CP/Navigation/NavBuilder.php index 4219e56c2e..67d4ebbcc0 100644 --- a/src/CP/Navigation/NavBuilder.php +++ b/src/CP/Navigation/NavBuilder.php @@ -251,8 +251,8 @@ protected function applyPreferenceOverrides($preferences = null) ->each(fn ($overrides) => $this->createPendingItemsForSection($overrides)) ->each(fn ($overrides) => $this->applyPreferenceOverridesForSection($overrides)); - if ($navPreferencesConfig['reorder']) { - $this->setSectionOrder($sections); + if ($reorder = $navPreferencesConfig['reorder']) { + $this->setSectionOrder($sections, $reorder); } return $this; @@ -317,8 +317,8 @@ protected function applyPreferenceOverridesForSection($sectionNav) ->filter() ->each(fn ($item) => $item->isChild(false)); - if ($sectionNav['reorder']) { - $this->setSectionItemOrder($section, $sectionNav['items']); + if ($reorder = $sectionNav['reorder']) { + $this->setSectionItemOrder($section, $sectionNav['items'], $reorder); } } @@ -442,23 +442,31 @@ protected function renameSection($sectionKey) /** * Set section order. * - * @param array $sections + * @param array $reorder */ - protected function setSectionOrder($sections) + protected function setSectionOrder($sections, $reorder) { - // Get conconfigured core sections... + // Get unconfigured core sections... $unconfiguredCoreSections = $this->sections; // Get unconfigured sections... $unconfiguredRegisteredSections = collect($this->items)->map->section()->filter()->unique(); - - // Merge unconfigured sections onto the end of the list and map their order... - $this->sectionsOrder = collect($sections) - ->pluck('display') - ->merge($unconfiguredRegisteredSections) + ray($sections, $this->sections, $unconfiguredRegisteredSections); + // Get merged unique list of sections... + $order = collect() ->merge($unconfiguredCoreSections) + ->merge($unconfiguredRegisteredSections) ->unique() - ->values() + ->values(); + + ray($reorder)->purple(); + $reorder = collect($reorder) + ->map(fn ($sectionKey) => $sections->pluck('display')->flip()->get($sectionKey)); + + ray($reorder)->purple(); + + // exit; + return $order ->mapWithKeys(fn ($section, $index) => [$section => $index + 1]) ->all(); } diff --git a/src/CP/Navigation/NavPreferencesNormalizer.php b/src/CP/Navigation/NavPreferencesNormalizer.php index 5d12eab177..7be6c20720 100644 --- a/src/CP/Navigation/NavPreferencesNormalizer.php +++ b/src/CP/Navigation/NavPreferencesNormalizer.php @@ -59,9 +59,9 @@ public function __construct($navPreferences) */ public static function fromPreferences($navPreferences) { - return (new static($navPreferences)) + return ray()->pass((new static($navPreferences)) ->normalize() - ->get(); + ->get()); } /** diff --git a/src/CP/Navigation/NavTransformer.php b/src/CP/Navigation/NavTransformer.php index 2f1f8001eb..d5c9a21c52 100644 --- a/src/CP/Navigation/NavTransformer.php +++ b/src/CP/Navigation/NavTransformer.php @@ -3,6 +3,7 @@ namespace Statamic\CP\Navigation; use Facades\Statamic\CP\Navigation\NavItemIdHasher; +use Illuminate\Support\Collection; use Statamic\Facades\CP\Nav; use Statamic\Support\Arr; use Statamic\Support\Str; @@ -30,6 +31,8 @@ public function __construct(array $submitted) */ public static function fromVue(array $submitted) { + ray($submitted)->orange(); + return (new static($submitted)) ->transform() ->minify() @@ -256,10 +259,12 @@ protected function transformItemUrl($url) */ protected function getReorderedItems($originalList, $newList): bool|array { - $itemsAreReordered = collect($originalList) + $comparableLists = collect($originalList) ->intersect($newList) ->values() - ->zip($newList) + ->zip($newList); + + $itemsAreReordered = $comparableLists ->reject(fn ($pair) => is_null($pair->first())) ->reject(fn ($pair) => $pair->first() === $pair->last()) ->isNotEmpty(); @@ -269,33 +274,55 @@ protected function getReorderedItems($originalList, $newList): bool|array } return collect($newList) - ->take($this->calculateMinimumItemsForReorder($originalList, $newList)) + ->take($this->calculateMinimumItemsForReorder($comparableLists->map->first(), $comparableLists->map->last())) ->all(); } /** * Calculate minimum number of items needed for reorder config. - * - * @param array $originalList - * @param array $newList */ - protected function calculateMinimumItemsForReorder($originalList, $newList): int + protected function calculateMinimumItemsForReorder(Collection $originalList, Collection $newList): int + { + $continueFiltering = true; + + $newList = $this->rejectNewItemsFromEndOfNewList($originalList, $newList); + + $redundantTailItems = $originalList + ->filter() + ->values() + ->reverse() + ->zip($newList->reverse()) + ->filter(function ($pair) use (&$continueFiltering) { + if ($continueFiltering && $pair->first() === $pair->last()) { + return true; + } + + return $continueFiltering = false; + }); + + return max(1, $newList->count() - $redundantTailItems->count()); + } + + /** + * Reject new items from the tail end end of our new list by checking to see if they existed in the old list. + */ + protected function rejectNewItemsFromEndOfNewList(Collection $originalList, Collection $newList): Collection { $continueRejecting = true; - $minimumItemsCount = collect($originalList) + ray($originalList, $newList)->purple(); + + return $newList ->reverse() - ->zip(collect($newList)->reverse()) - ->reject(function ($pair) use (&$continueRejecting) { - if ($continueRejecting && $pair->first() === $pair->last()) { + ->reject(function ($item) use ($originalList, &$continueRejecting) { + if ($continueRejecting && ! $originalList->contains($item)) { return true; } return $continueRejecting = false; }) - ->count(); - - return max(1, $minimumItemsCount - 1); + ->reverse() + ->values(); } /**