chore: upgrade cmdk to 1.0.0 (#6401)

Also include the command score into our own repo for some tweaks.

Might fix https://github.com/toeverything/AFFiNE/issues/6322
This commit is contained in:
pengx17 committed 2024-03-30 08:00:21 +00:00
1 parent 822bbb54a4
commit f41d587d65
7 files changed
+304 -232

No files matched your search

@@ -1,202 +0,0 @@
diff --git a/dist/command-score.d.ts b/dist/command-score.d.ts
new file mode 100644
index 0000000000000000000000000000000000000000..949ceeb1266241f7bf294b6df4422fd904e163c1
--- /dev/null
+++ b/dist/command-score.d.ts
@@ -0,0 +1,3 @@
+declare function commandScore(string: string, abbreviation: string): number;
+
+export { commandScore };
diff --git a/dist/command-score.js b/dist/command-score.js
new file mode 100644
index 0000000000000000000000000000000000000000..0d88276d3d39315e68322c54aceb3cf9769ad732
--- /dev/null
+++ b/dist/command-score.js
@@ -0,0 +1 @@
+var p=Object.defineProperty;var a=Object.getOwnPropertyDescriptor;var H=Object.getOwnPropertyNames;var J=Object.prototype.hasOwnProperty;var k=(_,E)=>{for(var f in E)p(_,f,{get:E[f],enumerable:!0})},m=(_,E,f,C)=>{if(E&&typeof E=="object"||typeof E=="function")for(let c of H(E))!J.call(_,c)&&c!==f&&p(_,c,{get:()=>E[c],enumerable:!(C=a(E,c))||C.enumerable});return _};var B=_=>m(p({},"__esModule",{value:!0}),_);var Z={};k(Z,{commandScore:()=>V});module.exports=B(Z);var D=1,K=.9,W=.8,$=.17,u=.1,G=.999,y=.9999;var F=.99,j=/[\\\/_+.#"@\[\(\{&]/,q=/[\\\/_+.#"@\[\(\{&]/g,Q=/[\s-]/,Y=/[\s-]/g;function L(_,E,f,C,c,P,O){if(P===E.length)return c===_.length?D:F;var T=`${c},${P}`;if(O[T]!==void 0)return O[T];for(var U=C.charAt(P),A=f.indexOf(U,c),S=0,h,N,R,M;A>=0;)h=L(_,E,f,C,A+1,P+1,O),h>S&&(A===c?h*=D:j.test(_.charAt(A-1))?(h*=W,R=_.slice(c,A-1).match(q),R&&c>0&&(h*=Math.pow(G,R.length))):Q.test(_.charAt(A-1))?(h*=K,M=_.slice(c,A-1).match(Y),M&&c>0&&(h*=Math.pow(G,M.length))):(h*=$,c>0&&(h*=Math.pow(G,A-c))),_.charAt(A)!==E.charAt(P)&&(h*=y)),(h<u&&f.charAt(A-1)===C.charAt(P+1)||C.charAt(P+1)===C.charAt(P)&&f.charAt(A-1)!==C.charAt(P))&&(N=L(_,E,f,C,A+1,P+2,O),N*u>h&&(h=N*u)),h>S&&(S=h),A=f.indexOf(U,A+1);return O[T]=S,S}function X(_){return _.toLowerCase().replace(Y," ")}function V(_,E){return L(_,E,X(_),X(E),0,0,{})}0&&(module.exports={commandScore});
diff --git a/dist/command-score.mjs b/dist/command-score.mjs
new file mode 100644
index 0000000000000000000000000000000000000000..bb680ba9ce704741e08454e4884bcbb1697c7f2b
--- /dev/null
+++ b/dist/command-score.mjs
@@ -0,0 +1 @@
+var U=1,Y=.9,a=.8,H=.17,p=.1,u=.999,J=.9999;var k=.99,m=/[\\\/_+.#"@\[\(\{&]/,B=/[\\\/_+.#"@\[\(\{&]/g,K=/[\s-]/,X=/[\s-]/g;function G(c,f,P,C,h,A,O){if(A===f.length)return h===c.length?U:k;var T=`${h},${A}`;if(O[T]!==void 0)return O[T];for(var L=C.charAt(A),E=P.indexOf(L,h),S=0,_,N,R,M;E>=0;)_=G(c,f,P,C,E+1,A+1,O),_>S&&(E===h?_*=U:m.test(c.charAt(E-1))?(_*=a,R=c.slice(h,E-1).match(B),R&&h>0&&(_*=Math.pow(u,R.length))):K.test(c.charAt(E-1))?(_*=Y,M=c.slice(h,E-1).match(X),M&&h>0&&(_*=Math.pow(u,M.length))):(_*=H,h>0&&(_*=Math.pow(u,E-h))),c.charAt(E)!==f.charAt(A)&&(_*=J)),(_<p&&P.charAt(E-1)===C.charAt(A+1)||C.charAt(A+1)===C.charAt(A)&&P.charAt(E-1)!==C.charAt(A))&&(N=G(c,f,P,C,E+1,A+2,O),N*p>_&&(_=N*p)),_>S&&(S=_),E=P.indexOf(L,E+1);return O[T]=S,S}function D(c){return c.toLowerCase().replace(X," ")}function W(c,f){return G(c,f,D(c),D(f),0,0,{})}export{W as commandScore};
diff --git a/dist/index.d.ts b/dist/index.d.ts
index faf9d6c5ad2de8af1abf49c9d70903bd1e911408..53eed433f6571e6a4ee34395a24d8cedefca3a33 100644
--- a/dist/index.d.ts
+++ b/dist/index.d.ts
@@ -1,5 +1,6 @@
import * as RadixDialog from '@radix-ui/react-dialog';
import * as React from 'react';
+export { commandScore } from './command-score.js';
declare type Children = {
children?: React.ReactNode;
@@ -30,6 +31,10 @@ declare const Command: React.ForwardRefExoticComponent<Children & DivProps & {
* By default, uses the `command-score` library.
*/
filter?: (value: string, search: string) => number;
+ /**
+ * Optional default item value when it is initially rendered.
+ */
+ defaultValue?: string;
/**
* Optional controlled state of the selected command menu item.
*/
@@ -42,6 +47,10 @@ declare const Command: React.ForwardRefExoticComponent<Children & DivProps & {
* Optionally set to `true` to turn on looping around when using the arrow keys.
*/
loop?: boolean;
+ /**
+ * Set to `false` to disable ctrl+n/j/p/k shortcuts. Defaults to `true`.
+ */
+ vimBindings?: boolean;
} & React.RefAttributes<HTMLDivElement>>;
/**
* Command menu item. Becomes active on pointer enter or through keyboard navigation.
@@ -58,7 +67,13 @@ declare const Item: React.ForwardRefExoticComponent<Children & Omit<DivProps, "d
* If no value is provided, it will be inferred from `children` or the rendered `textContent`. If your `textContent` changes between renders, you _must_ provide a stable, unique `value`.
*/
value?: string;
+ /** Whether this item is forcibly rendered regardless of filtering. */
+ forceMount?: boolean;
} & React.RefAttributes<HTMLDivElement>>;
+declare type Group = {
+ id: string;
+ forceMount?: boolean;
+};
/**
* Group command menu items together with a heading.
* Grouped items are always shown together.
@@ -68,6 +83,8 @@ declare const Group: React.ForwardRefExoticComponent<Children & Omit<DivProps, "
heading?: React.ReactNode;
/** If no heading is provided, you must provide a value that is unique for this group. */
value?: string;
+ /** Whether this group is forcibly rendered regardless of filtering. */
+ forceMount?: boolean;
} & React.RefAttributes<HTMLDivElement>>;
/**
* A visual and semantic separator between items or groups.
@@ -115,6 +132,10 @@ declare const Dialog: React.ForwardRefExoticComponent<RadixDialog.DialogProps &
* By default, uses the `command-score` library.
*/
filter?: (value: string, search: string) => number;
+ /**
+ * Optional default item value when it is initially rendered.
+ */
+ defaultValue?: string;
/**
* Optional controlled state of the selected command menu item.
*/
@@ -127,7 +148,15 @@ declare const Dialog: React.ForwardRefExoticComponent<RadixDialog.DialogProps &
* Optionally set to `true` to turn on looping around when using the arrow keys.
*/
loop?: boolean;
+ /**
+ * Set to `false` to disable ctrl+n/j/p/k shortcuts. Defaults to `true`.
+ */
+ vimBindings?: boolean;
} & {
+ /** Provide a className to the Dialog overlay. */
+ overlayClassName?: string;
+ /** Provide a className to the Dialog content. */
+ contentClassName?: string;
/** Provide a custom element the Dialog should portal into. */
container?: HTMLElement;
} & React.RefAttributes<HTMLDivElement>>;
@@ -138,7 +167,7 @@ declare const Empty: React.ForwardRefExoticComponent<Children & DivProps & React
/**
* You should conditionally render this with `progress` while loading asynchronous items.
*/
-declare const Loading: React.ForwardRefExoticComponent<Children & {
+declare const Loading: React.ForwardRefExoticComponent<Children & DivProps & {
/** Estimated progress of loading asynchronous options. */
progress?: number;
} & React.RefAttributes<HTMLDivElement>>;
@@ -158,6 +187,10 @@ declare const pkg: React.ForwardRefExoticComponent<Children & DivProps & {
* By default, uses the `command-score` library.
*/
filter?: (value: string, search: string) => number;
+ /**
+ * Optional default item value when it is initially rendered.
+ */
+ defaultValue?: string;
/**
* Optional controlled state of the selected command menu item.
*/
@@ -170,6 +203,10 @@ declare const pkg: React.ForwardRefExoticComponent<Children & DivProps & {
* Optionally set to `true` to turn on looping around when using the arrow keys.
*/
loop?: boolean;
+ /**
+ * Set to `false` to disable ctrl+n/j/p/k shortcuts. Defaults to `true`.
+ */
+ vimBindings?: boolean;
} & React.RefAttributes<HTMLDivElement>> & {
List: React.ForwardRefExoticComponent<Children & DivProps & React.RefAttributes<HTMLDivElement>>;
Item: React.ForwardRefExoticComponent<Children & Omit<DivProps, "disabled" | "onSelect" | "value"> & {
@@ -182,6 +219,8 @@ declare const pkg: React.ForwardRefExoticComponent<Children & DivProps & {
* If no value is provided, it will be inferred from `children` or the rendered `textContent`. If your `textContent` changes between renders, you _must_ provide a stable, unique `value`.
*/
value?: string;
+ /** Whether this item is forcibly rendered regardless of filtering. */
+ forceMount?: boolean;
} & React.RefAttributes<HTMLDivElement>>;
Input: React.ForwardRefExoticComponent<Omit<React.InputHTMLAttributes<HTMLInputElement>, "value" | "onChange" | "type"> & {
/**
@@ -198,6 +237,8 @@ declare const pkg: React.ForwardRefExoticComponent<Children & DivProps & {
heading?: React.ReactNode;
/** If no heading is provided, you must provide a value that is unique for this group. */
value?: string;
+ /** Whether this group is forcibly rendered regardless of filtering. */
+ forceMount?: boolean;
} & React.RefAttributes<HTMLDivElement>>;
Separator: React.ForwardRefExoticComponent<DivProps & {
/** Whether this separator should always be rendered. Useful if you disable automatic filtering. */
@@ -219,6 +260,10 @@ declare const pkg: React.ForwardRefExoticComponent<Children & DivProps & {
* By default, uses the `command-score` library.
*/
filter?: (value: string, search: string) => number;
+ /**
+ * Optional default item value when it is initially rendered.
+ */
+ defaultValue?: string;
/**
* Optional controlled state of the selected command menu item.
*/
@@ -231,12 +276,20 @@ declare const pkg: React.ForwardRefExoticComponent<Children & DivProps & {
* Optionally set to `true` to turn on looping around when using the arrow keys.
*/
loop?: boolean;
+ /**
+ * Set to `false` to disable ctrl+n/j/p/k shortcuts. Defaults to `true`.
+ */
+ vimBindings?: boolean;
} & {
+ /** Provide a className to the Dialog overlay. */
+ overlayClassName?: string;
+ /** Provide a className to the Dialog content. */
+ contentClassName?: string;
/** Provide a custom element the Dialog should portal into. */
container?: HTMLElement;
} & React.RefAttributes<HTMLDivElement>>;
Empty: React.ForwardRefExoticComponent<Children & DivProps & React.RefAttributes<HTMLDivElement>>;
- Loading: React.ForwardRefExoticComponent<Children & {
+ Loading: React.ForwardRefExoticComponent<Children & DivProps & {
/** Estimated progress of loading asynchronous options. */
progress?: number;
} & React.RefAttributes<HTMLDivElement>>;
diff --git a/dist/index.js b/dist/index.js
index 64801d481bdc4872c6ace390225105ab21508c0c..038dbe22b7c0eecb6babe424d1425131fac4d242 100644
--- a/dist/index.js
+++ b/dist/index.js
@@ -1 +1 @@
-var Se=Object.create;var F=Object.defineProperty;var Ce=Object.getOwnPropertyDescriptor;var ye=Object.getOwnPropertyNames;var xe=Object.getPrototypeOf,Te=Object.prototype.hasOwnProperty;var Le=(r,o)=>{for(var n in o)F(r,n,{get:o[n],enumerable:!0})},re=(r,o,n,a)=>{if(o&&typeof o=="object"||typeof o=="function")for(let l of ye(o))!Te.call(r,l)&&l!==n&&F(r,l,{get:()=>o[l],enumerable:!(a=Ce(o,l))||a.enumerable});return r};var z=(r,o,n)=>(n=r!=null?Se(xe(r)):{},re(o||!r||!r.__esModule?F(n,"default",{value:r,enumerable:!0}):n,r)),we=r=>re(F({},"__esModule",{value:!0}),r);var _e={};Le(_e,{Command:()=>Me,CommandDialog:()=>pe,CommandEmpty:()=>ge,CommandGroup:()=>ue,CommandInput:()=>fe,CommandItem:()=>le,CommandList:()=>me,CommandLoading:()=>ve,CommandRoot:()=>J,CommandSeparator:()=>de,useCommandState:()=>y});module.exports=we(_e);var C=z(require("@radix-ui/react-dialog")),t=z(require("react")),oe=z(require("command-score")),De='[cmdk-list-sizer=""]',M='[cmdk-group=""]',U='[cmdk-group-items=""]',Ie='[cmdk-group-heading=""]',ae='[cmdk-item=""]',ne=`${ae}:not([aria-disabled="true"])`,B="cmdk-item-select",S="data-value",Pe=(r,o)=>(0,oe.default)(r,o),se=t.createContext(void 0),k=()=>t.useContext(se),ie=t.createContext(void 0),W=()=>t.useContext(ie),ce=t.createContext(void 0),J=t.forwardRef((r,o)=>{let n=t.useRef(null),a=T(()=>({search:"",value:"",filtered:{count:0,items:new Map,groups:new Set}})),l=T(()=>new Set),u=T(()=>new Map),p=T(()=>new Map),f=T(()=>new Set),d=Re(r),{label:v,children:E,value:R,onValueChange:w,filter:O,shouldFilter:he,...D}=r,K=t.useId(),g=t.useId(),A=t.useId(),x=Oe();L(()=>{if(R!==void 0){let e=R.trim().toLowerCase();a.current.value=e,x(6,X),h.emit()}},[R]);let h=t.useMemo(()=>({subscribe:e=>(f.current.add(e),()=>f.current.delete(e)),snapshot:()=>a.current,setState:(e,c,i)=>{var s,m,b;if(!Object.is(a.current[e],c)){if(a.current[e]=c,e==="search")q(),V(),x(1,j);else if(e==="value")if(((s=d.current)==null?void 0:s.value)!==void 0){(b=(m=d.current).onValueChange)==null||b.call(m,c);return}else i||x(5,X);h.emit()}},emit:()=>{f.current.forEach(e=>e())}}),[]),G=t.useMemo(()=>({value:(e,c)=>{c!==p.current.get(e)&&(p.current.set(e,c),a.current.filtered.items.set(e,Q(c)),x(2,()=>{V(),h.emit()}))},item:(e,c)=>(l.current.add(e),c&&(u.current.has(c)?u.current.get(c).add(e):u.current.set(c,new Set([e]))),x(3,()=>{q(),V(),a.current.value||j(),h.emit()}),()=>{p.current.delete(e),l.current.delete(e),a.current.filtered.items.delete(e),x(4,()=>{q(),j(),h.emit()})}),group:e=>(u.current.has(e)||u.current.set(e,new Set),()=>{p.current.delete(e),u.current.delete(e)}),filter:()=>d.current.shouldFilter,label:v||r["aria-label"],listId:K,inputId:A,labelId:g}),[]);function Q(e){var i;let c=((i=d.current)==null?void 0:i.filter)??Pe;return e?c(e,a.current.search):0}function V(){if(!n.current||!a.current.search||d.current.shouldFilter===!1)return;let e=a.current.filtered.items,c=[];a.current.filtered.groups.forEach(s=>{let m=u.current.get(s),b=0;m.forEach(P=>{let Ee=e.get(P);b=Math.max(Ee,b)}),c.push([s,b])});let i=n.current.querySelector(De);I().sort((s,m)=>{let b=s.getAttribute(S),P=m.getAttribute(S);return(e.get(P)??0)-(e.get(b)??0)}).forEach(s=>{let m=s.closest(U);m?m.appendChild(s.parentElement===m?s:s.closest(`${U} > *`)):i.appendChild(s.parentElement===i?s:s.closest(`${U} > *`))}),c.sort((s,m)=>m[1]-s[1]).forEach(s=>{let m=n.current.querySelector(`${M}[${S}="${s[0]}"]`);m==null||m.parentElement.appendChild(m)})}function j(){let e=I().find(i=>!i.ariaDisabled),c=e==null?void 0:e.getAttribute(S);h.setState("value",c||void 0)}function q(){if(!a.current.search||d.current.shouldFilter===!1){a.current.filtered.count=l.current.size;return}a.current.filtered.groups=new Set;let e=0;for(let c of l.current){let i=p.current.get(c),s=Q(i);a.current.filtered.items.set(c,s),s>0&&e++}for(let[c,i]of u.current)for(let s of i)if(a.current.filtered.items.get(s)>0){a.current.filtered.groups.add(c);break}a.current.filtered.count=e}function X(){var c,i,s;let e=_();e&&(((c=e.parentElement)==null?void 0:c.firstChild)===e&&((s=(i=e.closest(M))==null?void 0:i.querySelector(Ie))==null||s.scrollIntoView({block:"nearest"})),e.scrollIntoView({block:"nearest"}))}function _(){return n.current.querySelector(`${ae}[aria-selected="true"]`)}function I(){return Array.from(n.current.querySelectorAll(ne))}function $(e){let i=I()[e];i&&h.setState("value",i.getAttribute(S))}function N(e){var b;let c=_(),i=I(),s=i.findIndex(P=>P===c),m=i[s+e];(b=d.current)!=null&&b.loop&&(m=s+e<0?i[i.length-1]:s+e===i.length?i[0]:i[s+e]),m&&h.setState("value",m.getAttribute(S))}function Y(e){let c=_(),i=c==null?void 0:c.closest(M),s;for(;i&&!s;)i=e>0?ke(i,M):He(i,M),s=i==null?void 0:i.querySelector(ne);s?h.setState("value",s.getAttribute(S)):N(e)}let Z=()=>$(I().length-1),ee=e=>{e.preventDefault(),e.metaKey?Z():e.altKey?Y(1):N(1)},te=e=>{e.preventDefault(),e.metaKey?$(0):e.altKey?Y(-1):N(-1)};return t.createElement("div",{ref:H([n,o]),...D,"cmdk-root":"",onKeyDown:e=>{var c;if((c Line truncated
+var De=Object.create;var G=Object.defineProperty;var xe=Object.getOwnPropertyDescriptor;var Ae=Object.getOwnPropertyNames;var we=Object.getPrototypeOf,_e=Object.prototype.hasOwnProperty;var Oe=(t,o)=>{for(var n in o)G(t,n,{get:o[n],enumerable:!0})},ie=(t,o,n,a)=>{if(o&&typeof o=="object"||typeof o=="function")for(let c of Ae(o))!_e.call(t,c)&&c!==n&&G(t,c,{get:()=>o[c],enumerable:!(a=xe(o,c))||a.enumerable});return t};var le=(t,o,n)=>(n=t!=null?De(we(t)):{},ie(o||!t||!t.__esModule?G(n,"default",{value:t,enumerable:!0}):n,t)),ke=t=>ie(G({},"__esModule",{value:!0}),t);var ze={};Oe(ze,{Command:()=>Be,CommandDialog:()=>be,CommandEmpty:()=>Te,CommandGroup:()=>he,CommandInput:()=>Ce,CommandItem:()=>ge,CommandList:()=>Se,CommandLoading:()=>ye,CommandRoot:()=>te,CommandSeparator:()=>Ee,commandScore:()=>W,useCommandState:()=>P});module.exports=ke(ze);var M=le(require("@radix-ui/react-dialog")),r=le(require("react"));var ue=1,Ie=.9,He=.8,Ne=.17,X=.1,Y=.999,Ge=.9999;var Ke=.99,Ve=/[\\\/_+.#"@\[\(\{&]/,Fe=/[\\\/_+.#"@\[\(\{&]/g,$e=/[\s-]/,fe=/[\s-]/g;function J(t,o,n,a,c,i,f){if(i===o.length)return c===t.length?ue:Ke;var v=`${c},${i}`;if(f[v]!==void 0)return f[v];for(var d=a.charAt(i),p=n.indexOf(d,c),R=0,m,S,T,E;p>=0;)m=J(t,o,n,a,p+1,i+1,f),m>R&&(p===c?m*=ue:Ve.test(t.charAt(p-1))?(m*=He,T=t.slice(c,p-1).match(Fe),T&&c>0&&(m*=Math.pow(Y,T.length))):$e.test(t.charAt(p-1))?(m*=Ie,E=t.slice(c,p-1).match(fe),E&&c>0&&(m*=Math.pow(Y,E.length))):(m*=Ne,c>0&&(m*=Math.pow(Y,p-c))),t.charAt(p)!==o.charAt(i)&&(m*=Ge)),(m<X&&n.charAt(p-1)===a.charAt(i+1)||a.charAt(i+1)===a.charAt(i)&&n.charAt(p-1)!==a.charAt(i))&&(S=J(t,o,n,a,p+1,i+2,f),S*X>m&&(m=S*X)),m>R&&(R=m),p=n.indexOf(d,p+1);return f[v]=R,R}function de(t){return t.toLowerCase().replace(fe," ")}function W(t,o){return J(t,o,de(t),de(o),0,0,{})}var je='[cmdk-list-sizer=""]',k='[cmdk-group=""]',z='[cmdk-group-items=""]',Ue='[cmdk-group-heading=""]',Z='[cmdk-item=""]',me=`${Z}:not([aria-disabled="true"])`,Q="cmdk-item-select",D="data-value",qe=(t,o)=>W(t,o),pe=r.createContext(void 0),I=()=>r.useContext(pe),Re=r.createContext(void 0),ee=()=>r.useContext(Re),ve=r.createContext(void 0),te=r.forwardRef((t,o)=>{let n=r.useRef(null),a=x(()=>{var e;return{search:"",value:t.value??((e=t.defaultValue)==null?void 0:e.toLowerCase())??"",filtered:{count:0,items:new Map,groups:new Set}}}),c=x(()=>new Set),i=x(()=>new Map),f=x(()=>new Map),v=x(()=>new Set),d=Pe(t),{label:p,children:R,value:m,onValueChange:S,filter:T,shouldFilter:E,vimBindings:K=!0,...w}=t,V=r.useId(),N=r.useId(),h=r.useId(),y=Je();A(()=>{if(m!==void 0){let e=m.trim().toLowerCase();a.current.value=e,y(6,ne),b.emit()}},[m]);let b=r.useMemo(()=>({subscribe:e=>(v.current.add(e),()=>v.current.delete(e)),snapshot:()=>a.current,setState:(e,u,s)=>{var l,g,C;if(!Object.is(a.current[e],u)){if(a.current[e]=u,e==="search")U(),$(),y(1,j);else if(e==="value")if(((l=d.current)==null?void 0:l.value)!==void 0){let L=u??"";(C=(g=d.current).onValueChange)==null||C.call(g,L);return}else s||y(5,ne);b.emit()}},emit:()=>{v.current.forEach(e=>e())}}),[]),F=r.useMemo(()=>({value:(e,u)=>{u!==f.current.get(e)&&(f.current.set(e,u),a.current.filtered.items.set(e,re(u)),y(2,()=>{$(),b.emit()}))},item:(e,u)=>(c.current.add(e),u&&(i.current.has(u)?i.current.get(u).add(e):i.current.set(u,new Set([e]))),y(3,()=>{U(),$(),a.current.value||j(),b.emit()}),()=>{f.current.delete(e),c.current.delete(e),a.current.filtered.items.delete(e);let s=_();y(4,()=>{U(),(s==null?void 0:s.getAttribute("id"))===e&&j(),b.emit()})}),group:e=>(i.current.has(e)||i.current.set(e,new Set),()=>{f.current.delete(e),i.current.delete(e)}),filter:()=>d.current.shouldFilter,label:p||t["aria-label"],commandRef:n,listId:V,inputId:h,labelId:N}),[]);function re(e){var s;let u=((s=d.current)==null?void 0:s.filter)??qe;return e?u(e,a.current.search):0}function $(){if(!n.current||!a.current.search||d.current.shouldFilter===!1)return;let e=a.current.filtered.items,u=[];a.current.filtered.groups.forEach(l=>{let g=i.current.get(l),C=0;g.forEach(L=>{let Le=e.get(L);C=Math.max(Le,C)}),u.push([l,C])});let s=n.current.querySelector(je);O().sort((l,g)=>{let C=l.getAttribute("id"),L=g.getAttribute("id");return(e.get(L)??0)-(e.get(C)??0)}).forEach(l=>{let g=l.closest(z);g?g.appendChild(l.parentElement===g?l:l.closest(`${z} > *`)):s.appendChild(l.parentElement===s?l:l.closest(`${z} > *`))}),u.sort((l,g)=>g[1]-l[1]).forEach(l=>{let g=n.current.querySelector(`${k}[${D}="${l[0]}"]`);g==null||g.parentElement.appendChild(g)})}function j(){let e=O().find(s=>!s.ariaDisabled),u=e==null?void 0:e.getAttribute(D);b.setState("value",u||void 0)}function U(){if(!a.current.search||d.current.shouldFilter===!1){a.current.filtered.count=c.current.size;return}a.current.filtered.groups=new Set;let e=0;for(let u of c.current){let s=f.current.get(u),l=re(s);a.current.filtered.items.set(u,l),l>0&&e++}for(let[u,s]of i.current)for(let l of s)if(a.current.filtered.items.get(l)>0){a.current.filtered.groups.add(u);break}a.current.fi Line truncated
diff --git a/dist/index.mjs b/dist/index.mjs
index 798a436cba7ea1506303d72e5708be4b464797e7..0cc5cf3a06d6450b21e1e51c01d39a0486d11f18 100644
--- a/dist/index.mjs
+++ b/dist/index.mjs
@@ -1 +1 @@
-import*as C from"@radix-ui/react-dialog";import*as t from"react";import le from"command-score";var ue='[cmdk-list-sizer=""]',M='[cmdk-group=""]',N='[cmdk-group-items=""]',de='[cmdk-group-heading=""]',ee='[cmdk-item=""]',Z=`${ee}:not([aria-disabled="true"])`,z="cmdk-item-select",S="data-value",fe=(n,a)=>le(n,a),te=t.createContext(void 0),k=()=>t.useContext(te),re=t.createContext(void 0),U=()=>t.useContext(re),ne=t.createContext(void 0),oe=t.forwardRef((n,a)=>{let r=t.useRef(null),o=x(()=>({search:"",value:"",filtered:{count:0,items:new Map,groups:new Set}})),u=x(()=>new Set),l=x(()=>new Map),p=x(()=>new Map),f=x(()=>new Set),d=ae(n),{label:v,children:E,value:R,onValueChange:w,filter:O,shouldFilter:ie,...D}=n,F=t.useId(),g=t.useId(),A=t.useId(),y=ye();L(()=>{if(R!==void 0){let e=R.trim().toLowerCase();o.current.value=e,y(6,W),h.emit()}},[R]);let h=t.useMemo(()=>({subscribe:e=>(f.current.add(e),()=>f.current.delete(e)),snapshot:()=>o.current,setState:(e,c,i)=>{var s,m,b;if(!Object.is(o.current[e],c)){if(o.current[e]=c,e==="search")j(),G(),y(1,V);else if(e==="value")if(((s=d.current)==null?void 0:s.value)!==void 0){(b=(m=d.current).onValueChange)==null||b.call(m,c);return}else i||y(5,W);h.emit()}},emit:()=>{f.current.forEach(e=>e())}}),[]),K=t.useMemo(()=>({value:(e,c)=>{c!==p.current.get(e)&&(p.current.set(e,c),o.current.filtered.items.set(e,B(c)),y(2,()=>{G(),h.emit()}))},item:(e,c)=>(u.current.add(e),c&&(l.current.has(c)?l.current.get(c).add(e):l.current.set(c,new Set([e]))),y(3,()=>{j(),G(),o.current.value||V(),h.emit()}),()=>{p.current.delete(e),u.current.delete(e),o.current.filtered.items.delete(e),y(4,()=>{j(),V(),h.emit()})}),group:e=>(l.current.has(e)||l.current.set(e,new Set),()=>{p.current.delete(e),l.current.delete(e)}),filter:()=>d.current.shouldFilter,label:v||n["aria-label"],listId:F,inputId:A,labelId:g}),[]);function B(e){var i;let c=((i=d.current)==null?void 0:i.filter)??fe;return e?c(e,o.current.search):0}function G(){if(!r.current||!o.current.search||d.current.shouldFilter===!1)return;let e=o.current.filtered.items,c=[];o.current.filtered.groups.forEach(s=>{let m=l.current.get(s),b=0;m.forEach(P=>{let ce=e.get(P);b=Math.max(ce,b)}),c.push([s,b])});let i=r.current.querySelector(ue);I().sort((s,m)=>{let b=s.getAttribute(S),P=m.getAttribute(S);return(e.get(P)??0)-(e.get(b)??0)}).forEach(s=>{let m=s.closest(N);m?m.appendChild(s.parentElement===m?s:s.closest(`${N} > *`)):i.appendChild(s.parentElement===i?s:s.closest(`${N} > *`))}),c.sort((s,m)=>m[1]-s[1]).forEach(s=>{let m=r.current.querySelector(`${M}[${S}="${s[0]}"]`);m==null||m.parentElement.appendChild(m)})}function V(){let e=I().find(i=>!i.ariaDisabled),c=e==null?void 0:e.getAttribute(S);h.setState("value",c||void 0)}function j(){if(!o.current.search||d.current.shouldFilter===!1){o.current.filtered.count=u.current.size;return}o.current.filtered.groups=new Set;let e=0;for(let c of u.current){let i=p.current.get(c),s=B(i);o.current.filtered.items.set(c,s),s>0&&e++}for(let[c,i]of l.current)for(let s of i)if(o.current.filtered.items.get(s)>0){o.current.filtered.groups.add(c);break}o.current.filtered.count=e}function W(){var c,i,s;let e=_();e&&(((c=e.parentElement)==null?void 0:c.firstChild)===e&&((s=(i=e.closest(M))==null?void 0:i.querySelector(de))==null||s.scrollIntoView({block:"nearest"})),e.scrollIntoView({block:"nearest"}))}function _(){return r.current.querySelector(`${ee}[aria-selected="true"]`)}function I(){return Array.from(r.current.querySelectorAll(Z))}function q(e){let i=I()[e];i&&h.setState("value",i.getAttribute(S))}function $(e){var b;let c=_(),i=I(),s=i.findIndex(P=>P===c),m=i[s+e];(b=d.current)!=null&&b.loop&&(m=s+e<0?i[i.length-1]:s+e===i.length?i[0]:i[s+e]),m&&h.setState("value",m.getAttribute(S))}function J(e){let c=_(),i=c==null?void 0:c.closest(M),s;for(;i&&!s;)i=e>0?Se(i,M):Ce(i,M),s=i==null?void 0:i.querySelector(Z);s?h.setState("value",s.getAttribute(S)):$(e)}let Q=()=>q(I().length-1),X=e=>{e.preventDefault(),e.metaKey?Q():e.altKey?J(1):$(1)},Y=e=>{e.preventDefault(),e.metaKey?q(0):e.altKey?J(-1):$(-1)};return t.createElement("div",{ref:H([r,a]),...D,"cmdk-root":"",onKeyDown:e=>{var c;if((c=D.onKeyDown)==null||c.call(D,e),!e.defaultPrevented)switch(e.key){case"n":case"j":{e.ctrlKey&&X(e);break}case"ArrowDown":{X(e);break}case"p":case"k":{e.ctrlKey&&Y(e);break}case"ArrowUp":{Y(e);break}case"Home":{e.preventDefault(),q(0);break}case"End":{e.preventDefault(),Q();break}case"Enter":{e.preventDefault();let i=_();if(i){let s=new Event(z);i.dispatchEvent(s)}}}}},t.createElement("label",{"cmdk-label":"",htmlFor:K.inputId,id:K.labelId,style:xe},v),t.createElement(re.Provider,{value:h},t.createElement(te.Provider,{value:K},E)))}),me=t.forwardRef((n,a)=>{let r=t.useId(),o=t.useRef(null),u=t.useContext(ne),l=k(),p=ae(n);L(()=>l.item(r,u),[]);let f=se(r,o,[n.value,n.children,o]),d=U(),v=T(g=>g.value&&g.value===f.current),E=T(g=>l.filter()===!1?!0:g.search?g.filtered.items.get(r)>0:!0);t.useEffect(()=>{let g=o.current;if(!(!g||n.disab Line truncated
+import*as P from"@radix-ui/react-dialog";import*as t from"react";var ae=1,ge=.9,he=.8,Ee=.17,B=.1,X=.999,Ce=.9999;var Se=.99,be=/[\\\/_+.#"@\[\(\{&]/,Te=/[\\\/_+.#"@\[\(\{&]/g,ye=/[\s-]/,se=/[\s-]/g;function Y(r,a,n,o,c,i,f){if(i===a.length)return c===r.length?ae:Se;var v=`${c},${i}`;if(f[v]!==void 0)return f[v];for(var d=o.charAt(i),p=n.indexOf(d,c),R=0,m,S,T,E;p>=0;)m=Y(r,a,n,o,p+1,i+1,f),m>R&&(p===c?m*=ae:be.test(r.charAt(p-1))?(m*=he,T=r.slice(c,p-1).match(Te),T&&c>0&&(m*=Math.pow(X,T.length))):ye.test(r.charAt(p-1))?(m*=ge,E=r.slice(c,p-1).match(se),E&&c>0&&(m*=Math.pow(X,E.length))):(m*=Ee,c>0&&(m*=Math.pow(X,p-c))),r.charAt(p)!==a.charAt(i)&&(m*=Ce)),(m<B&&n.charAt(p-1)===o.charAt(i+1)||o.charAt(i+1)===o.charAt(i)&&n.charAt(p-1)!==o.charAt(i))&&(S=Y(r,a,n,o,p+1,i+2,f),S*B>m&&(m=S*B)),m>R&&(R=m),p=n.indexOf(d,p+1);return f[v]=R,R}function ce(r){return r.toLowerCase().replace(se," ")}function ie(r,a){return Y(r,a,ce(r),ce(a),0,0,{})}var Pe='[cmdk-list-sizer=""]',k='[cmdk-group=""]',J='[cmdk-group-items=""]',Me='[cmdk-group-heading=""]',z='[cmdk-item=""]',le=`${z}:not([aria-disabled="true"])`,W="cmdk-item-select",L="data-value",Le=(r,a)=>ie(r,a),ue=t.createContext(void 0),I=()=>t.useContext(ue),de=t.createContext(void 0),Q=()=>t.useContext(de),fe=t.createContext(void 0),me=t.forwardRef((r,a)=>{let n=t.useRef(null),o=x(()=>{var e;return{search:"",value:r.value??((e=r.defaultValue)==null?void 0:e.toLowerCase())??"",filtered:{count:0,items:new Map,groups:new Set}}}),c=x(()=>new Set),i=x(()=>new Map),f=x(()=>new Map),v=x(()=>new Set),d=pe(r),{label:p,children:R,value:m,onValueChange:S,filter:T,shouldFilter:E,vimBindings:G=!0,...w}=r,K=t.useId(),N=t.useId(),h=t.useId(),y=Ge();A(()=>{if(m!==void 0){let e=m.trim().toLowerCase();o.current.value=e,y(6,ee),b.emit()}},[m]);let b=t.useMemo(()=>({subscribe:e=>(v.current.add(e),()=>v.current.delete(e)),snapshot:()=>o.current,setState:(e,u,s)=>{var l,g,C;if(!Object.is(o.current[e],u)){if(o.current[e]=u,e==="search")j(),F(),y(1,$);else if(e==="value")if(((l=d.current)==null?void 0:l.value)!==void 0){let M=u??"";(C=(g=d.current).onValueChange)==null||C.call(g,M);return}else s||y(5,ee);b.emit()}},emit:()=>{v.current.forEach(e=>e())}}),[]),V=t.useMemo(()=>({value:(e,u)=>{u!==f.current.get(e)&&(f.current.set(e,u),o.current.filtered.items.set(e,Z(u)),y(2,()=>{F(),b.emit()}))},item:(e,u)=>(c.current.add(e),u&&(i.current.has(u)?i.current.get(u).add(e):i.current.set(u,new Set([e]))),y(3,()=>{j(),F(),o.current.value||$(),b.emit()}),()=>{f.current.delete(e),c.current.delete(e),o.current.filtered.items.delete(e);let s=_();y(4,()=>{j(),(s==null?void 0:s.getAttribute("id"))===e&&$(),b.emit()})}),group:e=>(i.current.has(e)||i.current.set(e,new Set),()=>{f.current.delete(e),i.current.delete(e)}),filter:()=>d.current.shouldFilter,label:p||r["aria-label"],commandRef:n,listId:K,inputId:h,labelId:N}),[]);function Z(e){var s;let u=((s=d.current)==null?void 0:s.filter)??Le;return e?u(e,o.current.search):0}function F(){if(!n.current||!o.current.search||d.current.shouldFilter===!1)return;let e=o.current.filtered.items,u=[];o.current.filtered.groups.forEach(l=>{let g=i.current.get(l),C=0;g.forEach(M=>{let ve=e.get(M);C=Math.max(ve,C)}),u.push([l,C])});let s=n.current.querySelector(Pe);O().sort((l,g)=>{let C=l.getAttribute("id"),M=g.getAttribute("id");return(e.get(M)??0)-(e.get(C)??0)}).forEach(l=>{let g=l.closest(J);g?g.appendChild(l.parentElement===g?l:l.closest(`${J} > *`)):s.appendChild(l.parentElement===s?l:l.closest(`${J} > *`))}),u.sort((l,g)=>g[1]-l[1]).forEach(l=>{let g=n.current.querySelector(`${k}[${L}="${l[0]}"]`);g==null||g.parentElement.appendChild(g)})}function $(){let e=O().find(s=>!s.ariaDisabled),u=e==null?void 0:e.getAttribute(L);b.setState("value",u||void 0)}function j(){if(!o.current.search||d.current.shouldFilter===!1){o.current.filtered.count=c.current.size;return}o.current.filtered.groups=new Set;let e=0;for(let u of c.current){let s=f.current.get(u),l=Z(s);o.current.filtered.items.set(u,l),l>0&&e++}for(let[u,s]of i.current)for(let l of s)if(o.current.filtered.items.get(l)>0){o.current.filtered.groups.add(u);break}o.current.filtered.count=e}function ee(){var u,s,l;let e=_();e&&(((u=e.parentElement)==null?void 0:u.firstChild)===e&&((l=(s=e.closest(k))==null?void 0:s.querySelector(Me))==null||l.scrollIntoView({block:"nearest"})),e.scrollIntoView({block:"nearest"}))}function _(){var e;return(e=n.current)==null?void 0:e.querySelector(`${z}[aria-selected="true"]`)}function O(){return Array.from(n.current.querySelectorAll(le))}function U(e){let s=O()[e];s&&b.setState("value",s.getAttribute(L))}function q(e){var C;let u=_(),s=O(),l=s.findIndex(M=>M===u),g=s[l+e];(C=d.current)!=null&&C.loop&&(g=l+e<0?s[s.length-1]:l+e===s.length?s[0]:s[l+e]),g&&b.setState("value",g.getAttribute(L))}function te(e){let u=_(),s=u==null?void 0:u.closest(k),l;for(;s&&!l;)s=e>0?He(s,k):Ne(s,k),l=s==null?void 0:s.querySelector(le);l?b.setState("value",l.getAttribute(L)):q(e)}let re=()=>U(O().length- Line truncated
+1 -1
View File
@@ -51,7 +51,7 @@
"async-call-rpc": "^6.4.0",
"bytes": "^3.1.2",
"clsx": "^2.1.0",
"cmdk": "patch:cmdk@npm%3A0.2.0#~/.yarn/patches/cmdk-npm-0.2.0-302237a911.patch",
"cmdk": "^1.0.0",
"css-spring": "^4.1.0",
"dayjs": "^1.11.10",
"foxact": "^0.2.31",
@@ -0,0 +1,99 @@
import { describe, expect, it } from 'vitest';
import { commandScore } from '../command-score';
describe('commandScore', function () {
it('should match exact strings exactly', function () {
expect(commandScore('hello', 'hello')).to.equal(1);
});
it('should prefer case-sensitive matches', function () {
expect(commandScore('Hello', 'Hello')).to.be.greaterThan(
commandScore('Hello', 'hello')
);
});
it('should mark down prefixes', function () {
expect(commandScore('hello', 'hello')).to.be.greaterThan(
commandScore('hello', 'he')
);
});
it('should score all prefixes the same', function () {
expect(commandScore('help', 'he')).to.equal(commandScore('hello', 'he'));
});
it('should mark down word jumps', function () {
expect(commandScore('hello world', 'hello')).to.be.greaterThan(
commandScore('hello world', 'hewo')
);
});
it('should score similar word jumps the same', function () {
expect(commandScore('hello world', 'hewo')).to.equal(
commandScore('hey world', 'hewo')
);
});
it('should penalize long word jumps', function () {
expect(commandScore('hello world', 'hewo')).to.be.greaterThan(
commandScore('hello kind world', 'hewo')
);
});
it('should match missing characters', function () {
expect(commandScore('hello', 'hl')).to.be.greaterThan(0);
});
it('should penalize more for more missing characters', function () {
expect(commandScore('hello', 'hllo')).to.be.greaterThan(
commandScore('hello', 'hlo')
);
});
it('should penalize more for missing characters than case', function () {
expect(commandScore('go to Inbox', 'in')).to.be.greaterThan(
commandScore('go to Unversity/Societies/CUE/info@cue.org.uk', 'in')
);
});
it('should match transpotisions', function () {
expect(commandScore('hello', 'hle')).to.be.greaterThan(0);
});
it('should not match with a trailing letter', function () {
expect(commandScore('ss', 'sss')).to.equal(0.1);
});
it('should match long jumps', function () {
expect(commandScore('go to @QuickFix', 'fix')).to.be.greaterThan(0);
expect(commandScore('go to Quick Fix', 'fix')).to.be.greaterThan(
commandScore('go to @QuickFix', 'fix')
);
});
it('should work well with the presence of an m-dash', function () {
expect(commandScore('no go — Windows', 'windows')).to.be.greaterThan(0);
});
it('should be robust to duplicated letters', function () {
expect(commandScore('talent', 'tall')).to.be.equal(0.099);
});
it('should not allow letter insertion', function () {
expect(commandScore('talent', 'tadlent')).to.be.equal(0);
});
it('should match - with " " characters', function () {
expect(commandScore('Auto-Advance', 'Auto Advance')).to.be.equal(0.9999);
});
it('should score long strings quickly', function () {
expect(
commandScore(
'go to this is a really long label that is really longthis is a really long label that is really longthis is a really long label that is really longthis is a really long label that is really long',
'this is a'
)
).to.be.equal(0.891);
});
});
@@ -0,0 +1,195 @@
// The scores are arranged so that a continuous match of characters will
// result in a total score of 1.
//
// The best case, this character is a match, and either this is the start
// of the string, or the previous character was also a match.
const SCORE_CONTINUE_MATCH = 1,
// A new match at the start of a word scores better than a new match
// elsewhere as it's more likely that the user will type the starts
// of fragments.
// NOTE: We score word jumps between spaces slightly higher than slashes, brackets
// hyphens, etc.
SCORE_SPACE_WORD_JUMP = 0.9,
SCORE_NON_SPACE_WORD_JUMP = 0.8,
// Any other match isn't ideal, but we include it for completeness.
SCORE_CHARACTER_JUMP = 0.17,
// If the user transposed two letters, it should be significantly penalized.
//
// i.e. "ouch" is more likely than "curtain" when "uc" is typed.
SCORE_TRANSPOSITION = 0.1,
// The goodness of a match should decay slightly with each missing
// character.
//
// i.e. "bad" is more likely than "bard" when "bd" is typed.
//
// This will not change the order of suggestions based on SCORE_* until
// 100 characters are inserted between matches.
PENALTY_SKIPPED = 0.999,
// The goodness of an exact-case match should be higher than a
// case-insensitive match by a small amount.
//
// i.e. "HTML" is more likely than "haml" when "HM" is typed.
//
// This will not change the order of suggestions based on SCORE_* until
// 1000 characters are inserted between matches.
PENALTY_CASE_MISMATCH = 0.9999,
// If the word has more characters than the user typed, it should
// be penalised slightly.
//
// i.e. "html" is more likely than "html5" if I type "html".
//
// However, it may well be the case that there's a sensible secondary
// ordering (like alphabetical) that it makes sense to rely on when
// there are many prefix matches, so we don't make the penalty increase
// with the number of tokens.
PENALTY_NOT_COMPLETE = 0.99;
const IS_GAP_REGEXP = /[\\/_+.#"@[({&]/,
COUNT_GAPS_REGEXP = /[\\/_+.#"@[({&]/g,
IS_SPACE_REGEXP = /[\s-]/,
COUNT_SPACE_REGEXP = /[\s-]/g;
const MAX_RECUR = 1500;
function commandScoreInner(
string: string,
abbreviation: string,
lowerString: string,
lowerAbbreviation: string,
stringIndex: number,
abbreviationIndex: number,
memoizedResults: Record<string, number>,
recur: number = 0
) {
recur += 1;
if (abbreviationIndex === abbreviation.length) {
if (stringIndex === string.length) {
return SCORE_CONTINUE_MATCH;
}
return PENALTY_NOT_COMPLETE;
}
const memoizeKey = `${stringIndex},${abbreviationIndex}`;
if (memoizedResults[memoizeKey] !== undefined) {
return memoizedResults[memoizeKey];
}
const abbreviationChar = lowerAbbreviation.charAt(abbreviationIndex);
let index = lowerString.indexOf(abbreviationChar, stringIndex);
let highScore = 0;
let score, transposedScore, wordBreaks, spaceBreaks;
while (index >= 0) {
score = commandScoreInner(
string,
abbreviation,
lowerString,
lowerAbbreviation,
index + 1,
abbreviationIndex + 1,
memoizedResults,
recur
);
if (score > highScore) {
if (index === stringIndex) {
score *= SCORE_CONTINUE_MATCH;
} else if (IS_GAP_REGEXP.test(string.charAt(index - 1))) {
score *= SCORE_NON_SPACE_WORD_JUMP;
wordBreaks = string
.slice(stringIndex, index - 1)
.match(COUNT_GAPS_REGEXP);
if (wordBreaks && stringIndex > 0) {
score *= Math.pow(PENALTY_SKIPPED, wordBreaks.length);
}
} else if (IS_SPACE_REGEXP.test(string.charAt(index - 1))) {
score *= SCORE_SPACE_WORD_JUMP;
spaceBreaks = string
.slice(stringIndex, index - 1)
.match(COUNT_SPACE_REGEXP);
if (spaceBreaks && stringIndex > 0) {
score *= Math.pow(PENALTY_SKIPPED, spaceBreaks.length);
}
} else {
score *= SCORE_CHARACTER_JUMP;
if (stringIndex > 0) {
score *= Math.pow(PENALTY_SKIPPED, index - stringIndex);
}
}
if (string.charAt(index) !== abbreviation.charAt(abbreviationIndex)) {
score *= PENALTY_CASE_MISMATCH;
}
}
if (
(score < SCORE_TRANSPOSITION &&
lowerString.charAt(index - 1) ===
lowerAbbreviation.charAt(abbreviationIndex + 1)) ||
(lowerAbbreviation.charAt(abbreviationIndex + 1) ===
lowerAbbreviation.charAt(abbreviationIndex) && // allow duplicate letters. Ref #7428
lowerString.charAt(index - 1) !==
lowerAbbreviation.charAt(abbreviationIndex))
) {
transposedScore = commandScoreInner(
string,
abbreviation,
lowerString,
lowerAbbreviation,
index + 1,
abbreviationIndex + 2,
memoizedResults,
recur
);
if (transposedScore * SCORE_TRANSPOSITION > score) {
score = transposedScore * SCORE_TRANSPOSITION;
}
}
if (score > highScore) {
highScore = score;
}
index = lowerString.indexOf(abbreviationChar, index + 1);
if (recur > MAX_RECUR || score > 0.85) {
break;
}
}
memoizedResults[memoizeKey] = highScore;
return highScore;
}
function formatInput(string: string) {
// convert all valid space characters to space so they match each other
return string.toLowerCase().replace(COUNT_SPACE_REGEXP, ' ');
}
export function commandScore(
string: string,
abbreviation: string,
aliases?: string[]
): number {
/* NOTE:
* in the original, we used to do the lower-casing on each recursive call, but this meant that toLowerCase()
* was the dominating cost in the algorithm, passing both is a little ugly, but considerably faster.
*/
string =
aliases && aliases.length > 0
? `${string + ' ' + aliases.join(' ')}`
: string;
const memoizedResults = {};
const result = commandScoreInner(
string,
abbreviation,
formatInput(string),
formatInput(abbreviation),
0,
0,
memoizedResults
);
return result;
}
@@ -1,7 +1,7 @@
import type { CommandCategory } from '@toeverything/infra';
import { commandScore } from 'cmdk';
import { groupBy } from 'lodash-es';
import { commandScore } from './command-score';
import type { CMDKCommand } from './types';
import { highlightTextFragments } from './use-highlight';
+1 -1
View File
@@ -26,7 +26,7 @@ const insertInputText = async (page: Page, text: string) => {
const keyboardDownAndSelect = async (page: Page, label: string) => {
await page.keyboard.press('ArrowDown');
const selectedEl = page.locator(
'[cmdk-item][data-selected] [data-testid="cmdk-label"]'
'[cmdk-item][data-selected="true"] [data-testid="cmdk-label"]'
);
if (
!(await selectedEl.isVisible()) ||
+7 -27
View File
@@ -371,7 +371,7 @@ __metadata:
async-call-rpc: "npm:^6.4.0"
bytes: "npm:^3.1.2"
clsx: "npm:^2.1.0"
cmdk: "patch:cmdk@npm%3A0.2.0#~/.yarn/patches/cmdk-npm-0.2.0-302237a911.patch"
cmdk: "npm:^1.0.0"
css-spring: "npm:^4.1.0"
dayjs: "npm:^1.11.10"
express: "npm:^4.18.2"
@@ -18054,29 +18054,16 @@ __metadata:
languageName: node
linkType: hard
"cmdk@npm:0.2.0":
version: 0.2.0
resolution: "cmdk@npm:0.2.0"
"cmdk@npm:^1.0.0":
version: 1.0.0
resolution: "cmdk@npm:1.0.0"
dependencies:
"@radix-ui/react-dialog": "npm:1.0.0"
command-score: "npm:0.1.2"
"@radix-ui/react-dialog": "npm:1.0.5"
"@radix-ui/react-primitive": "npm:1.0.3"
peerDependencies:
react: ^18.0.0
react-dom: ^18.0.0
checksum: 10/e178e3d3276e0b5fd158c9c99716c0405427871f48fa97c15c4be2de24be4a478cf0205ffa04244628dbe103dd8573a1bd1aa68f04f8b60633d4ffc04e5eee62
languageName: node
linkType: hard
"cmdk@patch:cmdk@npm%3A0.2.0#~/.yarn/patches/cmdk-npm-0.2.0-302237a911.patch":
version: 0.2.0
resolution: "cmdk@patch:cmdk@npm%3A0.2.0#~/.yarn/patches/cmdk-npm-0.2.0-302237a911.patch::version=0.2.0&hash=640d85"
dependencies:
"@radix-ui/react-dialog": "npm:1.0.0"
command-score: "npm:0.1.2"
peerDependencies:
react: ^18.0.0
react-dom: ^18.0.0
checksum: 10/758bacb7761a72c6fa03a1b20ea2514ff14ad6b3d00cc1d8bc6781a216b0a719f991eacded9f923ddcf1b58b8efb304209b268c17bd7d6f5671aa3352934b754
checksum: 10/7a0675783d9b12828c30b044993d1ecf0e9230984c04f7a1714025804d34294b2b0f8958f30b26fe3b5be276b3cd874dbe1d0bc27cd25d15daa06adfcd3feb85
languageName: node
linkType: hard
@@ -18218,13 +18205,6 @@ __metadata:
languageName: node
linkType: hard
"command-score@npm:0.1.2":
version: 0.1.2
resolution: "command-score@npm:0.1.2"
checksum: 10/84f6a69e6b215d3fc8c9ed402d109587f511e4cc84cd5da10a7857b50fb1638953e32dcce8ed8f3549b0bfe499e82601fb7fb6891c9c71b48933d4bb8bac238a
languageName: node
linkType: hard
"commander@npm:11.1.0":
version: 11.1.0
resolution: "commander@npm:11.1.0"