From 8e94efce789d39c76e4b0ef52eba232b10f09e30 Mon Sep 17 00:00:00 2001 From: Vincent Vanwaelscappel Date: Wed, 19 Jan 2022 16:49:30 +0100 Subject: [PATCH] wip #4804 @0.5 --- app/Jobs/SearchIndex.php | 8 +++++--- resources/js/search.js | 19 ++++++++++++++++--- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/app/Jobs/SearchIndex.php b/app/Jobs/SearchIndex.php index 54dff2b..af51fbd 100644 --- a/app/Jobs/SearchIndex.php +++ b/app/Jobs/SearchIndex.php @@ -12,9 +12,11 @@ use Cubist\Minisearch\Index; class SearchIndex extends Index { + protected $varname = 'minisearchodl'; + public function __construct() { - $this->output = storage_path('search.js'); + $this->output = public_path('js/search.index.js'); } public function handle() @@ -72,11 +74,11 @@ class SearchIndex extends Index // } /** @var Asset[] $pdfs */ - $pdfs=Asset::getPDFInResources(); + $pdfs = Asset::getPDFInResources(); foreach ($pdfs as $pdf) { $document = new PDF($pdf->getFirstMediaInField('file_upload')->getPath()); $document->setId($pdf->id); - $document->setUrl('asset_'.$document->getId()); + $document->setUrl('asset_' . $document->getId()); $document->setTitle($pdf->title); $document->setKeywords($pdf->keywords); $document->setThumb($pdf->getThumbURL()); diff --git a/resources/js/search.js b/resources/js/search.js index 1d4a06f..fb6effd 100644 --- a/resources/js/search.js +++ b/resources/js/search.js @@ -8,7 +8,7 @@ export default (searchData = []) => ({ miniSearch: {}, // Holds the MiniSearch instance setup: { // MiniSearch options: https://lucaong.github.io/minisearch/#search-options - fields: ['title', 'text'], // fields to index for full-text search + fields: ['title', 'text','keywords'], // fields to index for full-text search storeFields: ['id', 'title', 'text', 'type', 'url', 'thumb'], // fields to return with search results searchOptions: { prefix: true, // Allow partial matches @@ -16,9 +16,11 @@ export default (searchData = []) => ({ }, query: '', // The search query - init() { + async init() { + this.miniSearch = new MiniSearch(this.setup); - this.miniSearch.addAllAsync(searchData); // Load asynchronously for better performance + await loadScript('search.index.js'); + this.miniSearch.addAllAsync(minisearchodl); }, get results() { @@ -41,3 +43,14 @@ export default (searchData = []) => ({ }, }); + +const loadScript = src => { + return new Promise((resolve, reject) => { + const script = document.createElement('script') + script.type = 'text/javascript' + script.onload = resolve + script.onerror = reject + script.src = src + document.head.append(script) + }) +} -- 2.39.5