-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: migrate to autocomplete.js v1 [BREAKING CHANGES] (#135)
Co-authored-by: Sylvain Bellone <[email protected]>
- Loading branch information
Showing
23 changed files
with
526 additions
and
484 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# Contributing | ||
|
||
## Scripts | ||
|
||
- `yarn dev`: run dev environment | ||
- `yarn release`: build & publish the library | ||
|
||
## Development | ||
|
||
From this folder: | ||
|
||
```sh | ||
yarn dev | ||
``` | ||
|
||
Or from the root of the repository: | ||
|
||
```sh | ||
yarn dev:frontend | ||
``` | ||
|
||
This runs a `webpack-dev-server` on port 9100. | ||
Meant to be used in conjunction with the [test website](../public/). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,66 +1,51 @@ | ||
import { AutocompleteWrapper } from './AutocompleteWrapper'; | ||
import { Options } from './options'; | ||
import type { Options } from './types'; | ||
|
||
const defaultOptions: Omit< | ||
Options, | ||
'appId' | 'apiKey' | 'indexName' | 'siteId' | 'branch' | ||
'appId' | 'apiKey' | 'selector' | 'siteId' | 'branch' | ||
> = { | ||
analytics: true, | ||
autocomplete: { | ||
hitsPerPage: 5, | ||
inputSelector: 'input[type=search]', | ||
}, | ||
color: '#3c4fe0', | ||
hitsPerPage: 5, | ||
debug: false, | ||
silenceWarnings: false, | ||
poweredBy: true, | ||
placeholder: 'Search...', | ||
openOnFocus: false, | ||
}; | ||
|
||
class AlgoliasearchNetlify { | ||
static instances: AlgoliasearchNetlify[]; | ||
|
||
search: AutocompleteWrapper; | ||
|
||
constructor(_options: Options) { | ||
AlgoliasearchNetlify.instances.push(this); | ||
|
||
// Temporary | ||
const splitIndexName = ( | ||
indexName: string | ||
): { siteId: string; branch: string } => { | ||
const regexp = /^netlify_([0-9a-f-]+)_(.*)_all$/; | ||
const [, siteId, branch] = indexName.match(regexp)!; | ||
return { siteId, branch }; | ||
}; | ||
|
||
// eslint-disable-next-line no-warning-comments | ||
// TODO: add validation | ||
const options = { | ||
...defaultOptions, | ||
..._options, | ||
...(_options.indexName && splitIndexName(_options.indexName)), // Temporary | ||
autocomplete: { | ||
...defaultOptions.autocomplete, | ||
..._options.autocomplete, | ||
}, | ||
}; | ||
|
||
this.search = new AutocompleteWrapper(options); | ||
|
||
// Wait for DOM initialization, then render | ||
const render = this.render.bind(this, options); | ||
if (['complete', 'interactive'].includes(document.readyState)) { | ||
render(); | ||
} else { | ||
document.addEventListener('DOMContentLoaded', render); | ||
} | ||
const mandatory: Array<keyof Options> = [ | ||
'appId', | ||
'apiKey', | ||
'selector', | ||
'siteId', | ||
'branch', | ||
]; | ||
|
||
const instances: AutocompleteWrapper[] = []; | ||
|
||
function algoliasearchNetlify(_options: Options) { | ||
const options = { | ||
...defaultOptions, | ||
..._options, | ||
}; | ||
for (const key of mandatory) { | ||
if (options[key]) continue; | ||
|
||
throw new Error(`[algoliasearch-netlify] Missing mandatory key: ${key}`); | ||
} | ||
|
||
render(options: Options) { | ||
this.search.render(options); | ||
const autocomplete = new AutocompleteWrapper(options); | ||
instances.push(autocomplete); | ||
|
||
// Wait for DOM initialization, then render | ||
const render = () => { | ||
autocomplete.render(); | ||
}; | ||
if (['complete', 'interactive'].includes(document.readyState)) { | ||
render(); | ||
} else { | ||
document.addEventListener('DOMContentLoaded', render); | ||
} | ||
} | ||
|
||
AlgoliasearchNetlify.instances = []; | ||
|
||
export { AlgoliasearchNetlify }; | ||
export { algoliasearchNetlify }; |
Oops, something went wrong.