Added support for search within the template as suggested in #581. I decided to go with a client side search based on [Ninja keys](https://github.com/ssleptsov/ninja-keys), but using [deepdub's fork](https://github.com/deepdub-ai/ninja-keys) as basis since it supports fuzzy search. Had to do a bunch of changes to their code to make it work without using node to install everything. Also changed to use some colors defined in our side and using both pages' titles and descriptions for search. Also had to increase the template max width to better accomodate the new item in navigation bar. Missing implementations: - [ ] One thing I'd love to do (but currently have no idea how) would be to change the text next to the search button depending on the platform. For example, if the user is accessing the site on a mac they should use ⌘k instead of ctrl k. - [x] Test how this looks like (and how it is supposed to work) on devices with smaller screens - [x] Support for offline mode Some screenshots: --- ## Dark version    --- ## Light version    --------- Signed-off-by: George Araujo <george.gcac@gmail.com> Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
39 lines
10 KiB
JavaScript
39 lines
10 KiB
JavaScript
/**
|
|
* Minified by jsDelivr using Terser v5.19.2.
|
|
* Original file: /npm/@deepdub/ninja-keys@1.2.11/dist/ninja-keys.js
|
|
*
|
|
* Do NOT use SRI with dynamically generated files! More information: https://www.jsdelivr.com/using-sri-with-dynamic-files
|
|
*/
|
|
var __decorate=this&&this.__decorate||function(e,t,i,s){var o,a=arguments.length,n=a<3?t:null===s?s=Object.getOwnPropertyDescriptor(t,i):s;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)n=Reflect.decorate(e,t,i,s);else for(var r=e.length-1;r>=0;r--)(o=e[r])&&(n=(a<3?o(n):a>3?o(t,i,n):o(t,i))||n);return a>3&&n&&Object.defineProperty(t,i,n),n};import{LitElement,html}from"./lit/index.min.js";import{customElement,property,state}from"./lit/decorators.min.js";import{repeat}from"./lit/directives/repeat.min.js";import{live}from"./lit/directives/live.min.js";import{createRef,ref}from"./lit-html/directives/ref.min.js";import{classMap}from"./lit/directives/class-map.min.js";import hotkeys from"./hotkeys-js/hotkeys.esm.min.js";import"./ninja-header.min.js";import"./ninja-action.min.js";import{footerHtml}from"./ninja-footer.min.js";import{baseStyles}from"./base-styles.min.js";import{commandScore}from"./command-score.min.js";let NinjaKeys=class extends LitElement{constructor(){super(...arguments),this._ignorePrefixesSplit=null,this.placeholder="Type a command or search...",this.disableHotkeys=!1,this.hideBreadcrumbs=!1,this.ignorePrefixes="",this.openHotkey="cmd+k,ctrl+k",this.navigationUpHotkey="up,shift+tab",this.navigationDownHotkey="down,tab",this.closeHotkey="esc",this.goBackHotkey="backspace",this.selectHotkey="enter",this.hotKeysJoinedView=!1,this.noAutoLoadMdIcons=!1,this.numRecentActions=0,this.data=[],this.visible=!1,this._bump=!0,this._actionMatches=[],this._search="",this._headerRef=createRef(),this._wrapperRef=createRef()}open(e={}){var t,i,s;this._bump=!0,this.visible=!0,this._headerRef.value.focusSearch(),this._actionMatches.length>0&&(this._selected=this._actionMatches[0]),this.setParent(e.parent),null===(t=this._headerRef.value)||void 0===t||t.setSearch(null!==(i=e.search)&&void 0!==i?i:""),this._search=null!==(s=e.search)&&void 0!==s?s:"",setTimeout((()=>{var e,t;null===(t=null===(e=this._wrapperRef.value)||void 0===e?void 0:e.querySelector(".actions-list"))||void 0===t||t.scrollTo({top:0})}),0)}close(){this._bump=!1,this.visible=!1}setParent(e){this._currentRoot=e||void 0,this._selected=void 0,this._search="",this._headerRef.value.setSearch("")}get breadcrumbs(){var e;const t=[];let i=null===(e=this._selected)||void 0===e?void 0:e.parent;if(i)for(t.push(i);i;){const e=[].find((e=>e.id===i));(null==e?void 0:e.parent)&&t.push(e.parent),i=e?e.parent:void 0}return t.reverse()}connectedCallback(){super.connectedCallback(),this.noAutoLoadMdIcons||document.fonts.load("24px Material Icons","apps").then((()=>{})),this._registerInternalHotkeys()}disconnectedCallback(){super.disconnectedCallback(),this._unregisterInternalHotkeys()}_registerInternalHotkeys(){this.openHotkey&&hotkeys(this.openHotkey,(e=>{e.preventDefault(),this.visible?this.close():this.open()})),this.selectHotkey&&hotkeys(this.selectHotkey,(e=>{this.visible&&(e.preventDefault(),this._actionSelected(this._actionMatches[this._selectedIndex]))})),this.goBackHotkey&&hotkeys(this.goBackHotkey,(e=>{this.visible&&(this._search||(e.preventDefault(),this._goBack()))})),this.navigationDownHotkey&&hotkeys(this.navigationDownHotkey,(e=>{this.visible&&(e.preventDefault(),this._selectedIndex>=this._actionMatches.length-1?this._selected=this._actionMatches[0]:this._selected=this._actionMatches[this._selectedIndex+1])})),this.navigationUpHotkey&&hotkeys(this.navigationUpHotkey,(e=>{this.visible&&(e.preventDefault(),0===this._selectedIndex?this._selected=this._actionMatches[this._actionMatches.length-1]:this._selected=this._actionMatches[this._selectedIndex-1])})),this.closeHotkey&&hotkeys(this.closeHotkey,(()=>{this.visible&&this.close()}))}_unregisterInternalHotkeys(){this.openHotkey&&hotkeys.unbind(this.openHotkey),this.selectHotkey&&hotkeys.unbind(this.selectHotkey),this.goBackHotkey&&hotkeys.unbind(this.goBackHotkey),this.navigationDownHotkey&&hotkeys.unbind(this.navigationDownHotkey),this.navigationUpHotkey&&hotkeys.unbind(this.navigationUpHotkey),this.closeHotkey&&hotkeys.unbind(this.closeHotkey)}_actionFocused(e,t){this._selected=e,t.target.ensureInView()}_onTransitionEnd(){this._bump=!1}_goBack(){const e=this.breadcrumbs.length>1?this.breadcrumbs[this.breadcrumbs.length-2]:void 0;this.setParent(e)}render(){var e,t,i,s;const o={bump:this._bump,"modal-content":!0},a={visible:this.visible,modal:!0,isLoadingItems:!1};let n=this._search;null!==(e=this._ignorePrefixesSplit)&&void 0!==e||(this._ignorePrefixesSplit=""!==this.ignorePrefixes?this.ignorePrefixes.split(","):[]),null===(t=this._ignorePrefixesSplit)||void 0===t||t.some((e=>!!n.startsWith(e)&&(n=n.substring(e.length),!0))),n=n.trim();const r={},c=[];(this._currentRoot?null!==(s=null===(i=this.data.find((e=>e.id===this._currentRoot)))||void 0===i?void 0:i.children)&&void 0!==s?s:[]:this.data).forEach(((e,t)=>{var i;if("loading"===e)return void(a.isLoadingItems=!0);if("function"==typeof e){const s=this.data.find((e=>e.id===this._currentRoot));return null===(i=s.children)||void 0===i||i.splice(t,1,"loading"),a.isLoadingItems=!0,void e().then((e=>{var i;null===(i=s.children)||void 0===i||i.splice(t,1,...e),this.render()}))}const s=commandScore(e.title+" "+e.description,n);(!this._currentRoot&&n||e.parent===this._currentRoot)&&(r[e.title]=s.indices,s.score>0&&c.push({score:s.score,item:e}))}));const h=(n?c.sort(((e,t)=>e.score===t.score?e.item.title.localeCompare(t.item.title):t.score-e.score)):c).map((e=>e.item)).reduce(((e,t)=>e.set(t.section,[...e.get(t.section)||[],t])),new Map);this._actionMatches=[...h.values()].flat(),this._actionMatches.length>0&&-1===this._selectedIndex&&(this._selected=this._actionMatches[0]),0===this._actionMatches.length&&(this._selected=void 0);const d=!this._currentRoot&&0!==this.numRecentActions&&!n,l=e=>html` ${repeat(e,(e=>e.id),((e,t)=>{var i;const s=d?0===t?html`<div class="title">Recently Used</div>`:this.numRecentActions===t?html`<div class="title separator">Other Commands</div>`:"":"";return html`${s}<ninja-action
|
|
exportparts="ninja-action,ninja-selected,ninja-icon"
|
|
.selected=${live(e.id===(null===(i=this._selected)||void 0===i?void 0:i.id))}
|
|
.hotKeysJoinedView=${this.hotKeysJoinedView}
|
|
@mousemove=${t=>this._actionFocused(e,t)}
|
|
@actionsSelected=${e=>this._actionSelected(e.detail)}
|
|
.action=${e}
|
|
.matchIndices=${r[e.title]}
|
|
></ninja-action>`}))}`,p=[];return h.forEach(((e,t)=>{const i=t?html`<div class="group-header">${t}</div>`:void 0;p.push(html`${i}${l(e)}`)})),html`
|
|
<div @click=${this._overlayClick} class=${classMap(a)} ${ref(this._wrapperRef)}>
|
|
<div class=${classMap(o)} @animationend=${this._onTransitionEnd}>
|
|
<ninja-header
|
|
exportparts="ninja-input,ninja-input-wrapper"
|
|
${ref(this._headerRef)}
|
|
.placeholder=${this.placeholder}
|
|
.hideBreadcrumbs=${this.hideBreadcrumbs}
|
|
.breadcrumbs=${this.breadcrumbs}
|
|
@change=${this._handleInput}
|
|
@setParent=${e=>this.setParent(e.detail.parent)}
|
|
@close=${this.close}
|
|
>
|
|
</ninja-header>
|
|
<div class="modal-body">
|
|
<div class="loading-indicator">
|
|
<span class="bar1"></span>
|
|
<span class="bar2"></span>
|
|
</div>
|
|
<div class="actions-list" part="actions-list">${p}</div>
|
|
</div>
|
|
<slot name="footer"> ${footerHtml} </slot>
|
|
</div>
|
|
</div>
|
|
`}get _selectedIndex(){return this._selected?this._actionMatches.indexOf(this._selected):-1}_actionSelected(e){var t;if(this.dispatchEvent(new CustomEvent("selected",{detail:{search:this._search,action:e},bubbles:!0,composed:!0})),e){if(e.children&&(null===(t=e.children)||void 0===t?void 0:t.length)>0&&(this._currentRoot=e.id,this._search=""),this._headerRef.value.setSearch(""),this._headerRef.value.focusSearch(),e.handler){const t=e.handler(e);(null==t?void 0:t.keepOpen)||this.close()}this._bump=!0}}async _handleInput(e){this._search=e.detail.search,await this.updateComplete,this._selected=this._actionMatches[0],this.dispatchEvent(new CustomEvent("change",{detail:{search:this._search,actions:this._actionMatches},bubbles:!0,composed:!0}))}_overlayClick(e){var t;(null===(t=e.target)||void 0===t?void 0:t.classList.contains("modal"))&&this.close()}};NinjaKeys.styles=[baseStyles],__decorate([property({type:String})],NinjaKeys.prototype,"placeholder",void 0),__decorate([property({type:Boolean})],NinjaKeys.prototype,"disableHotkeys",void 0),__decorate([property({type:Boolean})],NinjaKeys.prototype,"hideBreadcrumbs",void 0),__decorate([property({type:String})],NinjaKeys.prototype,"ignorePrefixes",void 0),__decorate([property()],NinjaKeys.prototype,"openHotkey",void 0),__decorate([property()],NinjaKeys.prototype,"navigationUpHotkey",void 0),__decorate([property()],NinjaKeys.prototype,"navigationDownHotkey",void 0),__decorate([property()],NinjaKeys.prototype,"closeHotkey",void 0),__decorate([property()],NinjaKeys.prototype,"goBackHotkey",void 0),__decorate([property()],NinjaKeys.prototype,"selectHotkey",void 0),__decorate([property({type:Boolean})],NinjaKeys.prototype,"hotKeysJoinedView",void 0),__decorate([property({type:Boolean})],NinjaKeys.prototype,"noAutoLoadMdIcons",void 0),__decorate([property({type:Number})],NinjaKeys.prototype,"numRecentActions",void 0),__decorate([property({type:Array,hasChanged:()=>!0})],NinjaKeys.prototype,"data",void 0),__decorate([state()],NinjaKeys.prototype,"visible",void 0),__decorate([state()],NinjaKeys.prototype,"_bump",void 0),__decorate([state()],NinjaKeys.prototype,"_actionMatches",void 0),__decorate([state()],NinjaKeys.prototype,"_search",void 0),__decorate([state()],NinjaKeys.prototype,"_currentRoot",void 0),__decorate([state()],NinjaKeys.prototype,"breadcrumbs",null),__decorate([state()],NinjaKeys.prototype,"_selected",void 0),NinjaKeys=__decorate([customElement("ninja-keys")],NinjaKeys);export{NinjaKeys}; |