-
Notifications
You must be signed in to change notification settings - Fork 2
/
auto-complete-demo.tsx
153 lines (142 loc) · 4.15 KB
/
auto-complete-demo.tsx
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
import { o } from '../jsx/jsx.js'
import type { attrs } from '../jsx/types'
import { existsSync, readdirSync, readFileSync } from 'fs'
import { join } from 'path'
import { Update } from '../components/update.js'
import { distance } from 'fastest-levenshtein'
import SourceCode from '../components/source-code.js'
import { getContextSearchParams, Context } from '../context.js'
import { Routes } from '../routes.js'
import { Locale, Title } from '../components/locale.js'
const wordSet = new Set<string>()
const dictDir = '/usr/share/dict'
if (existsSync(dictDir)) {
loadWordsFromDir()
}
if (wordSet.size === 0) {
loadWordsFromPackage()
}
function loadWordsFromDir() {
readdirSync(dictDir).forEach(file => {
file = join(dictDir, file)
readFileSync(file)
.toString()
.split('\n')
.forEach(line => {
line = line.trim()
if (line) {
wordSet.add(line)
}
})
})
}
function loadWordsFromPackage() {
readFileSync('package.json')
.toString()
.match(/"(.*?)"/g)
?.map(s => s.slice(1, -1)) // remove double quotes
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
.filter(s => !(+s.at(-1)! + 1)) // skip package versions
.filter(s => !s.includes('.com')) // skip email address
.forEach(s => wordSet.add(s))
}
const words = Array.from(wordSet)
const TopN = 20
let content = (
<div id="auto-complete-demo">
<h1>Auto-complete Demo</h1>
<p>
This page demo how to implement server-filtered auto-complete input field.
</p>
<p>
The server holds a list of {words.length.toLocaleString()} dictionary
words.
</p>
<p>
When the user input some text, the server filter the list by running:{' '}
<code>{`words.filter(word => word.includes(input))`}</code>
</p>
<p>
To avoid freezing the UI. When there exists more than {TopN} matches, the
server will only send top {TopN} matches (with lowest{' '}
<a href="https://www.npmjs.com/package/fastest-levenshtein">
edit distance
</a>
) to the browser.
</p>
<p>
It all happens on the server so the client does not need to download
additional libraries.
</p>
<fieldset style="display: inline-block">
<legend>Demo</legend>
<p>
Total number of matches:{' '}
<span id="num-matches">
(Input at least one character to get auto-complete list)
</span>
</p>
<label for="input-package">Try to input an English word: </label>
<input
id="input-package"
list="package-list"
oninput="emit('/auto-complete?input='+this.value)"
placeholder="e.g. apple"
/>
<datalist id="package-list"></datalist>
</fieldset>
<SourceCode page="auto-complete-demo.tsx" />
</div>
)
function renderUpdate(input: string) {
const allMatches = words.filter(word => word.includes(input))
const topMatches = allMatches
.map(word => [word, distance(word, input)] as const)
.sort((a, b) => a[1] - b[1])
.slice(0, TopN)
return (
<Update
to="/auto-complete"
message={[
'batch',
[
[
'update-in',
'#auto-complete-demo #package-list',
[topMatches.map(([word]) => <option value={word} />)],
],
[
'update-in',
'#auto-complete-demo #num-matches',
allMatches.length.toLocaleString(),
],
],
]}
/>
)
}
export function AutoCompleteDemo(_attrs: attrs, context: Context) {
if (context.type === 'ws') {
const input = getContextSearchParams(context)?.get('input')
if (input) {
return renderUpdate(input)
}
}
return content
}
let t = <Locale en="Auto Complete" zh_hk="自動輸入框" zh_cn="自动输入框" />
let routes = {
'/auto-complete': {
menuText: t,
title: <Title t={t} />,
description: (
<Locale
en="Server-driven auto-complete input box demo"
zh_hk="伺服器驅動的自動完成輸入框範例"
zh_cn="服务器驱动的自动完成输入框示例"
/>
),
node: <AutoCompleteDemo />,
},
} satisfies Routes
export default { routes }