Skip to content

Commit

Permalink
fix: support nested directories
Browse files Browse the repository at this point in the history
fix: support nested directories
  • Loading branch information
lukas-frey authored May 22, 2024
2 parents e2aa51c + 3dd1e80 commit 22220c4
Showing 1 changed file with 37 additions and 5 deletions.
42 changes: 37 additions & 5 deletions src/Forms/IconPicker.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -244,21 +245,52 @@ 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->getPathname())
->after($parentPath . DIRECTORY_SEPARATOR)
->replace(DIRECTORY_SEPARATOR, '.')
->basename('.svg')
->toString();

return "$prefix-$iconName";
}
}

0 comments on commit 22220c4

Please sign in to comment.