From f3b0f010e64da7a20db5888e89b3e39c01453524 Mon Sep 17 00:00:00 2001 From: Voltra Date: Sun, 19 May 2024 21:14:22 +0000 Subject: [PATCH 1/3] Add recursion, change icon name resolution --- src/Forms/IconPicker.php | 41 +++++++++++++++++++++++++++++++++++----- 1 file changed, 36 insertions(+), 5 deletions(-) diff --git a/src/Forms/IconPicker.php b/src/Forms/IconPicker.php index 1c3414e..eef6027 100644 --- a/src/Forms/IconPicker.php +++ b/src/Forms/IconPicker.php @@ -14,6 +14,7 @@ use Illuminate\Support\Facades\App; use Illuminate\Support\Facades\File; use Illuminate\View\View; +use Symfony\Component\Finder\SplFileInfo; class IconPicker extends Select { @@ -244,21 +245,51 @@ function () { foreach ($sets as $set) { $prefix = $set['prefix']; foreach ($set['paths'] as $path) { - foreach (File::files($path) as $file) { - $filename = $prefix . '-' . $file->getFilenameWithoutExtension(); + // To include icons from sub-folders, we use File::allFiles instead of File::files + // See https://github.com/blade-ui-kit/blade-icons/blob/ce60487deeb7bcbccd5e69188dc91b4c29622aff/src/IconsManifest.php#L40 + foreach (File::allFiles($path) as $file) { + // Simply ignore files that aren't SVGs + if ($file->getExtension() !== 'svg') { + continue; + } - if ($allowedIcons && !in_array($filename, $allowedIcons)) { + $iconName = $this->getIconName($file, parentPath: $path, prefix: $prefix); + + if ($allowedIcons && !in_array($iconName, $allowedIcons)) { continue; } - if ($disallowedIcons && in_array($filename, $disallowedIcons)) { + if ($disallowedIcons && in_array($iconName, $disallowedIcons)) { continue; } - $icons[] = $filename; + $icons[] = $iconName; } } } return collect($icons); } + + /** + * @see https://github.com/blade-ui-kit/blade-icons and its IconsManifest.php + * @see https://github.com/blade-ui-kit/blade-icons/blob/ce60487deeb7bcbccd5e69188dc91b4c29622aff/src/IconsManifest.php#L78 + */ + private function getIconName(SplFileInfo $file, string $parentPath, string $prefix): string { + // BladeIcons uses a simple (and view-compliant) naming convention for icon names + // `xtra-icon` is the `icon.svg` from the `xtra` icon set + // `xtra-dir.icon` is the `icon.svg` from the `dir/` folder from the `xtra` icon set + // `xtra-sub.dir.icon` is the `icon.svg` from the `sub/dir/` folder from the `xtra` icon set + // + // As such, we: + // - get the string after the parent directory's path + // - replace every directory separator by a dot + // - add the prefix at the beginning, followed by a dash + + $iconName = str($file->getFilenameWithoutExtension()) + ->after($parentPath . DIRECTORY_SEPARATOR) + ->replace(DIRECTORY_SEPARATOR, '.') + ->toString(); + + return "$prefix-$iconName"; + } } From d1e562bd68ceea645f6c2038c8bf7d74c957912a Mon Sep 17 00:00:00 2001 From: Voltra Date: Mon, 20 May 2024 20:11:27 +0000 Subject: [PATCH 2/3] Ensure extension is always stripped --- src/Forms/IconPicker.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Forms/IconPicker.php b/src/Forms/IconPicker.php index eef6027..2942753 100644 --- a/src/Forms/IconPicker.php +++ b/src/Forms/IconPicker.php @@ -288,6 +288,7 @@ private function getIconName(SplFileInfo $file, string $parentPath, string $pref $iconName = str($file->getFilenameWithoutExtension()) ->after($parentPath . DIRECTORY_SEPARATOR) ->replace(DIRECTORY_SEPARATOR, '.') + ->basename('.svg') ->toString(); return "$prefix-$iconName"; From 3dd1e80eba7b122ef759cd8142f671f0f69e4f07 Mon Sep 17 00:00:00 2001 From: Voltra Date: Tue, 21 May 2024 10:34:34 +0000 Subject: [PATCH 3/3] Fix icon name resolution algorithm --- src/Forms/IconPicker.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Forms/IconPicker.php b/src/Forms/IconPicker.php index 2942753..6d163b1 100644 --- a/src/Forms/IconPicker.php +++ b/src/Forms/IconPicker.php @@ -285,7 +285,7 @@ private function getIconName(SplFileInfo $file, string $parentPath, string $pref // - replace every directory separator by a dot // - add the prefix at the beginning, followed by a dash - $iconName = str($file->getFilenameWithoutExtension()) + $iconName = str($file->getPathname()) ->after($parentPath . DIRECTORY_SEPARATOR) ->replace(DIRECTORY_SEPARATOR, '.') ->basename('.svg')