GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — stacki/src/lib/insertRank.ts at main · flowtricks/stacki Expand file tree/ Copy pathinsertRank.tsMore file actions 180 lines (170 loc) · 6.49 KB / Copy pathinsertRank.tsFile metadata and controls180 lines (170 loc) · 6.49 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 import { assert } from '../../shared/core/assert'; import { LIMITS } from '../../shared/core/limits'; export interface SearchItem { readonly name?: string; readonly label?: string; readonly search?: string; readonly sub?: string; readonly folder?: string; readonly cat?: string; } // Searching the insert palette. // // What is being searched is a list of things with two names: what the thing is // called, and where it came from — `Input` in the `Form` folder, `Picture` from // `astro:assets`. So the query is words, and a word can land on either. // // form — still being typed, so it is a beginning: Form, // FormattedDate, <form>, and everything in the Form folder // form + a space — now a whole word, and FormattedDate is not it // form input — two words that have to land on different things: the Input // in the Form folder // // The space is the whole trick. A word you have finished typing is a word you // have finished saying, and matching it as a prefix after that is the palette // pretending not to have heard you. /** The words in a name, including the pieces of a camelCase one. */ function wordsOf(text: unknown): readonly string[] { const raw = String(text || '').toLowerCase(); if (!raw) { return []; } const out = new Set<string>(); // The whole thing, so `astro:assets` and `<form>` can be said as they read. out.add(raw); for (const part of raw.split(/[^a-z0-9]+/i)) { if (part) { out.add(part); } } // FormattedDate → formatted, date. Written on the original text, because the // capitals are what say where the pieces are. for (const part of String(text || '').split(/[^A-Za-z0-9]+/)) { for (const piece of part.split(/(?<=[a-z0-9])(?=[A-Z])/)) { if (piece) { out.add(piece.toLowerCase()); } } } return [...out]; } // What a token can land on, best first: an item's own name beats the folder it // sits in, because that is what it is called. const FIELDS = [ { of: (item: SearchItem) => item.search || item.label || item.name, base: 0 }, { of: (item: SearchItem) => item.label || item.name, base: 0 }, // `sub` in the palette, `folder` on a component record straight off the // scan — the components panel searches the same list without dressing it up // first. A folder is a path (`Form/Fields/Advanced`), and its words are its // segments, so any one of them can be typed. { of: (item: SearchItem) => item.sub || item.folder, base: 4 }, ]; /** A word the person has finished typing (there is a space after it), or one * they are still typing. */ type TokenState = 'finished' | 'typing'; /** * How well one token lands on one item, or undefined for not at all. * A finished token has to match a word rather than begin one. */ function scoreToken(item: SearchItem, token: string, state: TokenState): number | undefined { let best: number | undefined; for (const field of FIELDS) { const text = String(field.of(item) || '').toLowerCase(); if (!text) { continue; } const words = wordsOf(field.of(item)); let score: number | undefined; if (text === token || words.includes(token)) { score = field.base; } else if (state === 'finished') { score = undefined; } else if (text.startsWith(token)) { score = field.base + 1; } else if (words.some((word) => word.startsWith(token))) { score = field.base + 2; } else if (text.includes(token)) { score = field.base + 3; } if (score !== undefined && (best === undefined || score < best)) { best = score; } } return best; } // What an element is called in words, for the tags whose names don't say it. // A person looking for a paragraph types `paragraph`, not `p`; the tag still // lists below any component that matched, because the category sort decides // that, not the words. const TAG_WORDS: Readonly<Record<string, string>> = { a: 'link', blockquote: 'quote', br: 'line break', em: 'italic emphasis', h1: 'heading', h2: 'heading', h3: 'heading', h4: 'heading', h5: 'heading', h6: 'heading', hr: 'divider line', iframe: 'embed', img: 'image', li: 'list item', nav: 'navigation', ol: 'list ordered', p: 'paragraph', slot: 'children content', strong: 'bold', ul: 'list unordered', }; /** The search text for an element: its tag, then the words it goes by. */ export function tagSearchText(tag: string): string { assert(tag.length > 0, 'An element search text needs a tag'); const words = Object.hasOwn(TAG_WORDS, tag) ? TAG_WORDS[tag] : undefined; return words === undefined ? tag : `${tag} ${words}`; } // Components first, then elements, then everything else — the order the tabs // are in, and the order of how much of the project a thing belongs to. A // project's own components are its vocabulary; `<form>` is always there. Apps // come last: they open a tool rather than insert into the page. const CATEGORY: Readonly<Record<string, number>> = { components: 0, elements: 1, other: 2, apps: 3 }; /** * The items that match, best first. An empty query keeps the list as it is, * which is the palette's own order. * * Every word must land somewhere. `form input` is not "anything with form or * input in it" — it is the thing called Input in the folder called Form, and a * list that answered with forty rows would be answering a different question. */ export function rankInsertItems<T extends SearchItem>(items: readonly T[] | undefined, query: unknown): readonly T[] { assert((items?.length ?? 0) <= LIMITS.scanEntriesMax, 'Palette item count exceeds limit'); const text = String(query || '').toLowerCase(); const tokens = text.split(/\s+/).filter(Boolean); if (!tokens.length) { return [...(items || [])]; } // A trailing space means the last word is finished too. const finished = /\s$/.test(text); const scored = []; for (const [index, item] of (items || []).entries()) { let total = 0; let matched = true; for (const [at, token] of tokens.entries()) { const state = finished || at < tokens.length - 1 ? 'finished' : 'typing'; const score = scoreToken(item, token, state); if (score === undefined) { matched = false; break; } total += score; } if (matched) { scored.push({ item, total, index }); } } scored.sort( (left, right) => (CATEGORY[left.item.cat ?? ''] ?? 4) - (CATEGORY[right.item.cat ?? ''] ?? 4) || left.total - right.total || left.index - right.index, ); return scored.map((entry) => entry.item); } |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |