-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
Add autocomplete functionality for the search input
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Setono\SyliusMeilisearchPlugin\Meilisearch\Autocomplete\Configuration; | ||
|
||
final class Configuration | ||
{ | ||
/** @var list<Source> */ | ||
public array $sources = []; | ||
|
||
public function __construct( | ||
public readonly string $host, | ||
public readonly string $searchKey, | ||
|
||
/** This is the javascript selector for the HTML element that will contain the autocomplete */ | ||
public readonly string $container, | ||
|
||
/** This is the placeholder text that will be displayed in the input field */ | ||
public readonly string $placeholder, | ||
) { | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Setono\SyliusMeilisearchPlugin\Meilisearch\Autocomplete\Configuration; | ||
|
||
final class Source | ||
{ | ||
public function __construct( | ||
public readonly string $id, | ||
public readonly string $index, | ||
/** The attribute that holds the URL on any given item/document */ | ||
public readonly ?string $urlAttribute = null, | ||
) { | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?xml version="1.0" encoding="UTF-8" ?> | ||
<container xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://symfony.com/schema/dic/services" | ||
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> | ||
<services> | ||
<service id="Setono\SyliusMeilisearchPlugin\Twig\AutocompleteRuntime"> | ||
<argument type="service" id="setono_sylius_meilisearch.resolver.index_name"/> | ||
<argument type="service" id="translator"/> | ||
<argument>%setono_sylius_meilisearch.server.host%</argument> | ||
<argument>%setono_sylius_meilisearch.server.search_key%</argument> | ||
<argument>%setono_sylius_meilisearch.autocomplete.container%</argument> | ||
<argument>%setono_sylius_meilisearch.autocomplete.placeholder%</argument> | ||
<argument type="collection" /> <!-- Gets set in the extension --> | ||
|
||
<tag name="twig.runtime"/> | ||
</service> | ||
</services> | ||
</container> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?xml version="1.0" encoding="UTF-8" ?> | ||
<container xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://symfony.com/schema/dic/services" | ||
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> | ||
<services> | ||
<service id="Setono\SyliusMeilisearchPlugin\Twig\AutocompleteExtension"> | ||
<argument>%setono_sylius_meilisearch.autocomplete.enabled%</argument> | ||
|
||
<tag name="twig.extension"/> | ||
</service> | ||
|
||
<service id="Setono\SyliusMeilisearchPlugin\Twig\SearchExtension"> | ||
<argument>%setono_sylius_meilisearch.search.enabled%</argument> | ||
|
||
<tag name="twig.extension"/> | ||
</service> | ||
</services> | ||
</container> |
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
{% if ssm_autocomplete_enabled() %} | ||
{# @var configuration \Setono\SyliusMeilisearchPlugin\Meilisearch\Autocomplete\Configuration\Configuration #} | ||
{% set configuration = ssm_autocomplete_configuration() %} | ||
|
||
{# todo should be saved on the individual sources #} | ||
{% set templates = { | ||
item: include('@SetonoSyliusMeilisearchPlugin/autocomplete/templates/item.html.twig'), | ||
} %} | ||
<script src="{{ asset('bundles/setonosyliusmeilisearchplugin/js/algolia.autocomplete.js') }}"></script> | ||
<script src="{{ asset('bundles/setonosyliusmeilisearchplugin/js/meilisearch.autocomplete.js') }}"></script> | ||
<script> | ||
const { autocomplete } = window['@algolia/autocomplete-js']; | ||
const { meilisearchAutocompleteClient, getMeilisearchResults } = window['@meilisearch/autocomplete-client']; | ||
const searchClient = meilisearchAutocompleteClient({ | ||
url: '{{ configuration.host }}', | ||
apiKey: '{{ configuration.searchKey }}' | ||
}); | ||
autocomplete({ | ||
{% if app.debug -%} | ||
debug: true, | ||
{% endif -%} | ||
container: '{{ configuration.container }}', | ||
placeholder: '{{ configuration.placeholder }}', | ||
{% if ssm_search_enabled() -%} | ||
onSubmit({ event, state }) { | ||
location.href = '{{ path('setono_sylius_meilisearch_shop_search') }}?q=' + state.query; | ||
}, | ||
initialState: { | ||
query: new URL(window.location).searchParams.get('q'), | ||
}, | ||
{% endif -%} | ||
getSources({ query }) { | ||
return [ | ||
{% for source in configuration.sources %} | ||
{ | ||
sourceId: '{{ source.id }}', | ||
getItems() { | ||
return getMeilisearchResults({ | ||
searchClient, | ||
queries: [ | ||
{ | ||
indexName: '{{ source.index }}', | ||
query, | ||
}, | ||
], | ||
}) | ||
}, | ||
templates: { | ||
item({ item, components, html }) { | ||
return html`{{ templates.item|raw }}`; | ||
}, | ||
}, | ||
{% if source.urlAttribute -%} | ||
onSelect({ item }) { | ||
location.href = item.{{ source.urlAttribute }}; | ||
}, | ||
getItemUrl({ item }) { | ||
return item.{{ source.urlAttribute }}; | ||
}, | ||
{% endif -%} | ||
}, | ||
{% endfor %} | ||
] | ||
}, | ||
}); | ||
</script> | ||
{% endif %} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{%- if ssm_autocomplete_enabled() -%} | ||
<link href="{{ asset('bundles/setonosyliusmeilisearchplugin/css/algolia.autocomplete.css') }}" rel="stylesheet"> | ||
<style> | ||
.aa-Panel { | ||
z-index: 9999; | ||
} | ||
</style> | ||
{%- endif -%} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<div class="aa-ItemWrapper"> | ||
<div class="aa-ItemContent"> | ||
<div class="aa-ItemIcon aa-ItemIcon--alignTop"> | ||
<img src="${item.image}" alt="${item.name}" width="100"/> | ||
</div> | ||
<div class="aa-ItemContentBody"> | ||
<div class="aa-ItemContentTitle"> | ||
${components.Highlight({ | ||
hit: item, | ||
attribute: 'name', | ||
})} | ||
</div> | ||
</div> | ||
</div> | ||
</div> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,7 @@ | ||
{{ render(path('setono_sylius_meilisearch_shop_search_widget')) }} | ||
{% if ssm_autocomplete_enabled() %} | ||
<div class="ui center aligned column"> | ||
<div id="autocomplete"></div> | ||
</div> | ||
{% elseif ssm_search_enabled() %} | ||
{{ render(path('setono_sylius_meilisearch_shop_search_widget')) }} | ||
{% endif %} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Setono\SyliusMeilisearchPlugin\Twig; | ||
|
||
use Twig\Extension\AbstractExtension; | ||
use Twig\TwigFunction; | ||
|
||
final class AutocompleteExtension extends AbstractExtension | ||
{ | ||
public function __construct(private readonly bool $enabled) | ||
{ | ||
} | ||
|
||
/** | ||
* @return list<TwigFunction> | ||
*/ | ||
public function getFunctions(): array | ||
{ | ||
return [ | ||
new TwigFunction('ssm_autocomplete_configuration', [AutocompleteRuntime::class, 'configuration']), | ||
Check failure on line 22 in src/Twig/AutocompleteExtension.php GitHub Actions / Static Code Analysis (PHP8.1 | Deps: highest | SF~5.4.0)InvalidArgument
Check failure on line 22 in src/Twig/AutocompleteExtension.php GitHub Actions / Static Code Analysis (PHP8.1 | Deps: highest | SF~6.4.0)InvalidArgument
Check failure on line 22 in src/Twig/AutocompleteExtension.php GitHub Actions / Static Code Analysis (PHP8.2 | Deps: highest | SF~5.4.0)InvalidArgument
Check failure on line 22 in src/Twig/AutocompleteExtension.php GitHub Actions / Static Code Analysis (PHP8.2 | Deps: highest | SF~6.4.0)InvalidArgument
|
||
new TwigFunction('ssm_autocomplete_enabled', $this->isEnabled(...)), | ||
]; | ||
} | ||
|
||
public function isEnabled(): bool | ||
{ | ||
return $this->enabled; | ||
} | ||
} |