Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.x] Improved fieldtype search using keywords #11053

Merged
merged 12 commits into from
Nov 4, 2024
11 changes: 6 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
"codemirror": "^5.58.2",
"cookies-js": "^1.2.2",
"floating-vue": "^1.0.0-beta.19",
"fuse.js": "^3.4.6",
"fuse.js": "^7.0.0",
"highlight.js": "^11.7.0",
"imask": "^6.6.0-alpha.0",
"laravel-echo": "^1.16.0",
Expand Down
2 changes: 1 addition & 1 deletion resources/js/components/data-list/DataList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ export default {
keys: this.searchableColumns,
});

return fuse.search(this.searchQuery);
return fuse.search(this.searchQuery).map(result => result.item);
},

sortRows(rows) {
Expand Down
11 changes: 7 additions & 4 deletions resources/js/components/fields/FieldtypeSelector.vue
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export default {
if (!this.fieldtypesLoaded) return [];

let options = this.fieldtypes.map(fieldtype => {
return {text: fieldtype.title, value: fieldtype.handle, categories: fieldtype.categories, icon: fieldtype.icon};
return {text: fieldtype.title, value: fieldtype.handle, categories: fieldtype.categories, keywords: fieldtype.keywords, icon: fieldtype.icon};
});

if (this.allowDate) options.unshift({text: __('Publish Date'), value: 'date', categories: ['system'], isMeta: true, icon: 'date'});
Expand Down Expand Up @@ -131,11 +131,14 @@ export default {
const fuse = new Fuse(options, {
findAllMatches: true,
threshold: 0.1,
minMatchCharLength: 2,
keys: ['text'],
keys: [
{name: 'text', weight: 1},
{name: 'categories', weight: 0.1},
{name: 'keywords', weight: 0.4},
],
});

options = fuse.search(this.search);
options = fuse.search(this.search).map(result => result.item);
}

return options;
Expand Down
1 change: 1 addition & 0 deletions src/Dictionaries/Countries.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class Countries extends BasicDictionary
{
protected string $valueKey = 'iso3';
protected array $searchable = ['name', 'iso3'];
protected array $keywords = ['countries', 'country'];
private array $regions;
private array $subregions;

Expand Down
1 change: 1 addition & 0 deletions src/Dictionaries/Currencies.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
class Currencies extends BasicDictionary
{
protected string $valueKey = 'code';
protected array $keywords = ['currencies', 'currency', 'money', 'dollar'];

protected function getItemLabel(array $item): string
{
Expand Down
6 changes: 6 additions & 0 deletions src/Dictionaries/Dictionary.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ abstract class Dictionary

protected array $fields = [];
protected array $config = [];
protected array $keywords = [];

abstract public function options(?string $search = null): array;

Expand Down Expand Up @@ -75,4 +76,9 @@ public function optionItems(?string $search = null): array
->map(fn ($label, $value) => new Item($value, $label, $this->get($value)->extra()))
->all();
}

public function keywords(): array
{
return $this->keywords;
}
}
2 changes: 2 additions & 0 deletions src/Dictionaries/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

class File extends BasicDictionary
{
protected array $keywords = ['files', 'file', 'json', 'csv', 'yaml', 'yml'];

protected function fieldItems()
{
return [
Expand Down
1 change: 1 addition & 0 deletions src/Dictionaries/Timezones.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
class Timezones extends BasicDictionary
{
protected string $valueKey = 'name';
protected array $keywords = ['timezone', 'tz', 'zone', 'time', 'date'];

protected function getItemLabel(array $item): string
{
Expand Down
7 changes: 7 additions & 0 deletions src/Fields/Fieldtype.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ abstract class Fieldtype implements Arrayable
protected $selectableInForms = false;
protected $relationship = false;
protected $categories = [];
protected $keywords = [];
protected $rules = [];
protected $extraRules = [];
protected $defaultValue;
Expand Down Expand Up @@ -113,6 +114,11 @@ public function categories(): array
return $this->categories;
}

public function keywords(): array
{
return $this->keywords;
}

public function filter()
{
return new FieldtypeFilter($this);
Expand Down Expand Up @@ -167,6 +173,7 @@ public function toArray(): array
'validatable' => $this->validatable(),
'defaultable' => $this->defaultable(),
'categories' => $this->categories(),
'keywords' => $this->keywords(),
'icon' => $this->icon(),
'config' => $this->configFields()->toPublishArray(),
];
Expand Down
1 change: 1 addition & 0 deletions src/Fieldtypes/Assets/Assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
class Assets extends Fieldtype
{
protected $categories = ['media', 'relationship'];
protected $keywords = ['file', 'files', 'image', 'images', 'video', 'videos', 'audio', 'upload'];
protected $selectableInForms = true;

protected function configFieldItems(): array
Expand Down
1 change: 1 addition & 0 deletions src/Fieldtypes/Bard.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class Bard extends Replicator
];

protected $categories = ['text', 'structured'];
protected $keywords = ['rich', 'richtext', 'rich text', 'editor', 'wysiwg', 'builder', 'page builder', 'gutenberg', 'content'];
protected $rules = [];

protected function configFieldItems(): array
Expand Down
1 change: 1 addition & 0 deletions src/Fieldtypes/Collections.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
class Collections extends Relationship
{
protected $categories = ['relationship'];
protected $keywords = ['entries'];
protected $canEdit = false;
protected $canCreate = false;
protected $canSearch = false;
Expand Down
1 change: 1 addition & 0 deletions src/Fieldtypes/Color.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
class Color extends Fieldtype
{
protected $categories = ['special'];
protected $keywords = ['rgb', 'hex', 'colour'];

protected function configFieldItems(): array
{
Expand Down
1 change: 1 addition & 0 deletions src/Fieldtypes/Date.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
class Date extends Fieldtype
{
protected $categories = ['special'];
protected $keywords = ['datetime', 'time'];

const DEFAULT_DATE_FORMAT = 'Y-m-d';
const DEFAULT_DATETIME_FORMAT = 'Y-m-d H:i';
Expand Down
11 changes: 11 additions & 0 deletions src/Fieldtypes/Dictionary.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,4 +170,15 @@ public function addGqlTypes()
{
GraphQL::addType($this->dictionary()->getGqlType());
}

public function keywords(): array
{
return \Statamic\Facades\Dictionary::all()
->flatMap(fn ($dictionary) => [
str($dictionary->handle())->replace('_', ' ')->toString(),
...$dictionary->keywords(),
])
->merge(['select', 'option', 'choice', 'dropdown', 'list'])
->unique()->values()->all();
}
}
1 change: 1 addition & 0 deletions src/Fieldtypes/Entries.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class Entries extends Relationship
use QueriesFilters;

protected $categories = ['relationship'];
protected $keywords = ['entry'];
protected $canEdit = true;
protected $canCreate = true;
protected $canSearch = true;
Expand Down
1 change: 1 addition & 0 deletions src/Fieldtypes/Markdown.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
class Markdown extends Fieldtype
{
protected $categories = ['text'];
protected $keywords = ['md', 'content', 'html'];

use Concerns\ResolvesStatamicUrls;

Expand Down
1 change: 1 addition & 0 deletions src/Fieldtypes/Replicator.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
class Replicator extends Fieldtype
{
protected $categories = ['structured'];
protected $keywords = ['builder', 'page builder', 'content'];
protected $rules = ['array'];

protected function configFieldItems(): array
Expand Down
1 change: 1 addition & 0 deletions src/Fieldtypes/Select.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class Select extends Fieldtype
use HasSelectOptions;

protected $categories = ['controls'];
protected $keywords = ['select', 'option', 'choice', 'dropdown', 'list'];
protected $selectableInForms = true;
protected $indexComponent = 'tags';

Expand Down
1 change: 1 addition & 0 deletions src/Fieldtypes/Toggle.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
class Toggle extends Fieldtype
{
protected $categories = ['controls'];
protected $keywords = ['checkbox', 'bool', 'boolean'];
protected $selectableInForms = true;
protected $defaultValue = false;

Expand Down
1 change: 1 addition & 0 deletions src/Fieldtypes/Yaml.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
class Yaml extends Fieldtype
{
protected $categories = ['special'];
protected $keywords = ['yml'];

protected function configFieldItems(): array
{
Expand Down
1 change: 1 addition & 0 deletions tests/Fields/FieldtypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ public function converts_to_an_array()
'validatable' => true,
'defaultable' => true,
'categories' => [],
'keywords' => [],
'icon' => 'test',
'config' => [],
], $fieldtype->toArray());
Expand Down
Loading