@tdh-keyboard/core 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,269 @@
1
+ //#region src/canvas.d.ts
2
+ interface CanvasDrawerOptions {
3
+ onDrawEnd?: () => void;
4
+ clearDelay?: number;
5
+ }
6
+ declare class CanvasDrawer {
7
+ private canvas;
8
+ private ctx;
9
+ private lastX;
10
+ private lastY;
11
+ private strokeData;
12
+ private clearTimerId;
13
+ private isDrawing;
14
+ private options;
15
+ constructor(canvas: HTMLCanvasElement, options?: CanvasDrawerOptions);
16
+ private setupCanvas;
17
+ drawGrid(): void;
18
+ clearCanvas(): void;
19
+ startDrawing(offsetX: number, offsetY: number): void;
20
+ draw(offsetX: number, offsetY: number): void;
21
+ endStroke(): void;
22
+ getStrokeData(): ReadonlyArray<number>;
23
+ resetClearTimer(): void;
24
+ startClearTimer(): void;
25
+ private getEventCoordinates;
26
+ private handleStart;
27
+ private handleMove;
28
+ private handleEnd;
29
+ private attachEvents;
30
+ private detachEvents;
31
+ destroy(): void;
32
+ getCanvas(): HTMLCanvasElement;
33
+ getContext(): CanvasRenderingContext2D;
34
+ }
35
+ //#endregion
36
+ //#region src/handwriting.d.ts
37
+ interface RecognizerInitOptions {
38
+ /**
39
+ * 初始化进度回调函数
40
+ * @param progress 进度范围为0到1,表示初始化的完成度
41
+ * @returns void
42
+ */
43
+ onProgress?: (progress: number) => void;
44
+ }
45
+ /**
46
+ * 手写识别器接口
47
+ * 用于实现手写汉字识别功能
48
+ */
49
+ interface HandwritingRecognizer {
50
+ /**
51
+ * 初始化手写识别服务
52
+ * @returns 返回是否初始化成功
53
+ */
54
+ initialize(options?: RecognizerInitOptions): Promise<boolean>;
55
+ /**
56
+ * 识别手写笔迹
57
+ * @param strokeData 笔迹数据,格式为 x y c x y c ...,其中x和y是坐标,c表示是否为笔画的最后一点(1表示是,0表示否)
58
+ * @returns 识别结果列表
59
+ */
60
+ recognize(strokeData: number[]): Promise<string[]>;
61
+ /**
62
+ * 关闭手写识别服务
63
+ */
64
+ close(): Promise<void>;
65
+ }
66
+ //#endregion
67
+ //#region src/pinyin-engine.d.ts
68
+ interface Candidate {
69
+ text: string;
70
+ comment: string;
71
+ }
72
+ interface PinyinState {
73
+ /** 已提交(最终)文本;若无提交则为 null。 */
74
+ committed: string | null;
75
+ /** 选区前的预编辑文本。 */
76
+ preeditHead: string;
77
+ /** 当前被选中的预编辑部分。 */
78
+ preeditBody: string;
79
+ /** 选区后的预编辑文本。 */
80
+ preeditTail: string;
81
+ /** 预编辑中的光标位置。 */
82
+ cursorPos: number;
83
+ /** 当前页的候选项列表。 */
84
+ candidates: Candidate[];
85
+ /** 当前页码(从 0 开始)。 */
86
+ pageNo: number;
87
+ /** 是否为候选的最后一页。 */
88
+ isLastPage: boolean;
89
+ /** 高亮候选的索引。 */
90
+ highlightedIndex: number;
91
+ /** 候选选择键的标签数组。 */
92
+ selectLabels: string[];
93
+ }
94
+ /**
95
+ * 拼音引擎通用接口
96
+ * 所有拼音引擎实现必须遵循此接口
97
+ */
98
+ interface PinyinEngine {
99
+ /**
100
+ * 初始化引擎,加载必要资源
101
+ */
102
+ initialize(): Promise<void>;
103
+ /**
104
+ * 处理完整的拼音输入串,返回所有候选词列表(跨所有页)。
105
+ * 引擎内部负责增量 vs 重置的优化,调用方只传完整拼音。
106
+ * @param pinyin 完整的拼音字符串
107
+ * @returns 候选词状态,包括所有页的候选词集合和分页信息
108
+ */
109
+ processInput(pinyin: string): Promise<PinyinState | null>;
110
+ /**
111
+ * 按全局索引选择候选词,返回已提交的文本。
112
+ * @param index 候选词在全量列表中的全局索引(从0开始)
113
+ * @returns 候选词状态,包括所有页的候选词集合和分页信息
114
+ */
115
+ pickCandidate(index: number): Promise<PinyinState>;
116
+ /**
117
+ * 清除引擎当前的预编辑输入状态
118
+ */
119
+ clearInput(): Promise<void>;
120
+ /**
121
+ * 切换简体/繁体输出(可选,引擎不支持时忽略)
122
+ * @param simplified true 为简体,false 为繁体
123
+ */
124
+ setSimplified?(simplified: boolean): Promise<void>;
125
+ /**
126
+ * 销毁引擎,释放所有持有的资源
127
+ */
128
+ destroy(): Promise<void>;
129
+ }
130
+ //#endregion
131
+ //#region src/config.d.ts
132
+ /**
133
+ * 键盘配置类型
134
+ */
135
+ interface KeyboardConfig {
136
+ /**
137
+ * 默认的键盘模式
138
+ */
139
+ defaultMode?: 'en' | 'zh' | 'hand' | 'num' | 'symbol';
140
+ /**
141
+ * 是否启用手写输入
142
+ */
143
+ enableHandwriting?: boolean;
144
+ /**
145
+ * 键盘定位模式
146
+ */
147
+ position?: 'static' | 'float' | 'bottom';
148
+ /**
149
+ * 浮动模式下键盘与输入框的距离
150
+ * @default 10
151
+ */
152
+ floatMarginTop?: number;
153
+ /**
154
+ * 当没有input获得焦点时是否禁用键盘
155
+ */
156
+ disableWhenNoFocus?: boolean;
157
+ /**
158
+ * 是否启用手动打开模式
159
+ * 启用后不再根据焦点自动显示,需要通过实例方法手动调用 open / close / destroy。
160
+ */
161
+ manual?: boolean;
162
+ /**
163
+ * 数字键盘的行配置
164
+ */
165
+ numKeys?: string[][];
166
+ /**
167
+ * RIME WASM 文件及数据文件的 URL 或路径前缀。
168
+ * 用于默认 RIME 引擎加载(当未通过 registerPinyinEngine 注册自定义引擎时)。
169
+ * @default '/rime'
170
+ */
171
+ wasmDir?: string;
172
+ }
173
+ /**
174
+ * 获取全局键盘配置
175
+ */
176
+ declare function getKeyboardConfig(): KeyboardConfig;
177
+ /**
178
+ * 设置全局键盘配置
179
+ */
180
+ declare function setKeyboardConfig(config: KeyboardConfig): void;
181
+ /**
182
+ * 注册手写识别服务
183
+ * @param recognizer 手写识别服务实现
184
+ */
185
+ declare function registerHandwritingRecognizer(recognizer: HandwritingRecognizer): void;
186
+ /**
187
+ * 获取手写识别服务实例
188
+ * @returns 手写识别服务实例
189
+ */
190
+ declare function getHandwritingRecognizer(): HandwritingRecognizer | null;
191
+ /**
192
+ * 注册拼音引擎。
193
+ * 注册后,CandidateBar 将使用此引擎而非默认的 RIME 引擎。
194
+ * 适用于自定义引擎或 Worker 中运行的引擎。
195
+ * @param engine 拼音引擎实现
196
+ */
197
+ declare function registerPinyinEngine(engine: PinyinEngine): void;
198
+ /**
199
+ * 获取已注册的拼音引擎实例
200
+ * @returns 拼音引擎实例,未注册时返回 null
201
+ */
202
+ declare function getPinyinEngine(): PinyinEngine | null;
203
+ //#endregion
204
+ //#region src/dom.d.ts
205
+ interface KeyboardPosition {
206
+ top: string;
207
+ left: string;
208
+ }
209
+ /**
210
+ * 计算键盘的显示位置
211
+ * @param inputElement 输入框元素
212
+ * @param keyboardElement 键盘元素
213
+ * @param positionMode 位置模式:'static'(静态,不计算位置)、'float'(浮动,根据输入框位置计算)、'bottom'(固定在底部)
214
+ * @returns 返回键盘的 top 和 left 位置,如果不需要计算则返回 null
215
+ */
216
+ declare function calculateKeyboardPosition(inputElement: HTMLElement | null, keyboardElement: HTMLElement | null, positionMode: 'static' | 'float' | 'bottom', floatMarginTop?: number): KeyboardPosition | null;
217
+ //#endregion
218
+ //#region src/input.d.ts
219
+ type TextInputElement = HTMLInputElement | HTMLTextAreaElement;
220
+ /**
221
+ * 判断元素是否为可输入元素(支持文本输入的 input 或 textarea)
222
+ * @param el 要检测的元素
223
+ * @returns 如果是可输入元素则返回 true
224
+ */
225
+ declare function isInputElement(el?: Element | null): el is TextInputElement;
226
+ /**
227
+ * 获取当前激活的输入元素
228
+ * @throws {Error} 如果当前没有激活的输入框
229
+ */
230
+ declare function getInputElement(): TextInputElement;
231
+ /**
232
+ * 向输入框插入文本
233
+ * @param inputElement 输入框元素
234
+ * @param text 要插入的文本
235
+ */
236
+ declare function writeToInputElement(inputElement: TextInputElement, text: string): void;
237
+ /**
238
+ * 向输入框删除文本
239
+ * @param inputElement 输入框元素
240
+ */
241
+ declare function delToInputElement(inputElement: TextInputElement): void;
242
+ /**
243
+ * 设置输入框光标位置
244
+ * @param inputElement 输入框元素
245
+ * @param index 光标位置索引
246
+ */
247
+ declare function moveCursor(inputElement: TextInputElement, index: number): void;
248
+ //#endregion
249
+ //#region src/repeat.d.ts
250
+ interface KeyRepeatOptions {
251
+ /** Long-press delay before repeating starts (ms). */
252
+ delay?: number;
253
+ /** Repeat interval after the initial delay (ms). */
254
+ interval?: number;
255
+ }
256
+ interface KeyRepeater {
257
+ start: (action: () => void) => void;
258
+ stop: () => void;
259
+ }
260
+ /**
261
+ * Create a small, framework-agnostic key repeater for long-press.
262
+ *
263
+ * - Calls `action()` immediately on start.
264
+ * - After `delay`, repeats with `interval` until `stop()` is called.
265
+ */
266
+ declare function createKeyRepeater(options?: KeyRepeatOptions): KeyRepeater;
267
+ //#endregion
268
+ export { Candidate, CanvasDrawer, CanvasDrawerOptions, HandwritingRecognizer, KeyRepeatOptions, KeyRepeater, KeyboardConfig, KeyboardPosition, PinyinEngine, PinyinState, RecognizerInitOptions, TextInputElement, calculateKeyboardPosition, createKeyRepeater, delToInputElement, getHandwritingRecognizer, getInputElement, getKeyboardConfig, getPinyinEngine, isInputElement, moveCursor, registerHandwritingRecognizer, registerPinyinEngine, setKeyboardConfig, writeToInputElement };
269
+ //# sourceMappingURL=index.d.cts.map
@@ -0,0 +1,269 @@
1
+ //#region src/canvas.d.ts
2
+ interface CanvasDrawerOptions {
3
+ onDrawEnd?: () => void;
4
+ clearDelay?: number;
5
+ }
6
+ declare class CanvasDrawer {
7
+ private canvas;
8
+ private ctx;
9
+ private lastX;
10
+ private lastY;
11
+ private strokeData;
12
+ private clearTimerId;
13
+ private isDrawing;
14
+ private options;
15
+ constructor(canvas: HTMLCanvasElement, options?: CanvasDrawerOptions);
16
+ private setupCanvas;
17
+ drawGrid(): void;
18
+ clearCanvas(): void;
19
+ startDrawing(offsetX: number, offsetY: number): void;
20
+ draw(offsetX: number, offsetY: number): void;
21
+ endStroke(): void;
22
+ getStrokeData(): ReadonlyArray<number>;
23
+ resetClearTimer(): void;
24
+ startClearTimer(): void;
25
+ private getEventCoordinates;
26
+ private handleStart;
27
+ private handleMove;
28
+ private handleEnd;
29
+ private attachEvents;
30
+ private detachEvents;
31
+ destroy(): void;
32
+ getCanvas(): HTMLCanvasElement;
33
+ getContext(): CanvasRenderingContext2D;
34
+ }
35
+ //#endregion
36
+ //#region src/handwriting.d.ts
37
+ interface RecognizerInitOptions {
38
+ /**
39
+ * 初始化进度回调函数
40
+ * @param progress 进度范围为0到1,表示初始化的完成度
41
+ * @returns void
42
+ */
43
+ onProgress?: (progress: number) => void;
44
+ }
45
+ /**
46
+ * 手写识别器接口
47
+ * 用于实现手写汉字识别功能
48
+ */
49
+ interface HandwritingRecognizer {
50
+ /**
51
+ * 初始化手写识别服务
52
+ * @returns 返回是否初始化成功
53
+ */
54
+ initialize(options?: RecognizerInitOptions): Promise<boolean>;
55
+ /**
56
+ * 识别手写笔迹
57
+ * @param strokeData 笔迹数据,格式为 x y c x y c ...,其中x和y是坐标,c表示是否为笔画的最后一点(1表示是,0表示否)
58
+ * @returns 识别结果列表
59
+ */
60
+ recognize(strokeData: number[]): Promise<string[]>;
61
+ /**
62
+ * 关闭手写识别服务
63
+ */
64
+ close(): Promise<void>;
65
+ }
66
+ //#endregion
67
+ //#region src/pinyin-engine.d.ts
68
+ interface Candidate {
69
+ text: string;
70
+ comment: string;
71
+ }
72
+ interface PinyinState {
73
+ /** 已提交(最终)文本;若无提交则为 null。 */
74
+ committed: string | null;
75
+ /** 选区前的预编辑文本。 */
76
+ preeditHead: string;
77
+ /** 当前被选中的预编辑部分。 */
78
+ preeditBody: string;
79
+ /** 选区后的预编辑文本。 */
80
+ preeditTail: string;
81
+ /** 预编辑中的光标位置。 */
82
+ cursorPos: number;
83
+ /** 当前页的候选项列表。 */
84
+ candidates: Candidate[];
85
+ /** 当前页码(从 0 开始)。 */
86
+ pageNo: number;
87
+ /** 是否为候选的最后一页。 */
88
+ isLastPage: boolean;
89
+ /** 高亮候选的索引。 */
90
+ highlightedIndex: number;
91
+ /** 候选选择键的标签数组。 */
92
+ selectLabels: string[];
93
+ }
94
+ /**
95
+ * 拼音引擎通用接口
96
+ * 所有拼音引擎实现必须遵循此接口
97
+ */
98
+ interface PinyinEngine {
99
+ /**
100
+ * 初始化引擎,加载必要资源
101
+ */
102
+ initialize(): Promise<void>;
103
+ /**
104
+ * 处理完整的拼音输入串,返回所有候选词列表(跨所有页)。
105
+ * 引擎内部负责增量 vs 重置的优化,调用方只传完整拼音。
106
+ * @param pinyin 完整的拼音字符串
107
+ * @returns 候选词状态,包括所有页的候选词集合和分页信息
108
+ */
109
+ processInput(pinyin: string): Promise<PinyinState | null>;
110
+ /**
111
+ * 按全局索引选择候选词,返回已提交的文本。
112
+ * @param index 候选词在全量列表中的全局索引(从0开始)
113
+ * @returns 候选词状态,包括所有页的候选词集合和分页信息
114
+ */
115
+ pickCandidate(index: number): Promise<PinyinState>;
116
+ /**
117
+ * 清除引擎当前的预编辑输入状态
118
+ */
119
+ clearInput(): Promise<void>;
120
+ /**
121
+ * 切换简体/繁体输出(可选,引擎不支持时忽略)
122
+ * @param simplified true 为简体,false 为繁体
123
+ */
124
+ setSimplified?(simplified: boolean): Promise<void>;
125
+ /**
126
+ * 销毁引擎,释放所有持有的资源
127
+ */
128
+ destroy(): Promise<void>;
129
+ }
130
+ //#endregion
131
+ //#region src/config.d.ts
132
+ /**
133
+ * 键盘配置类型
134
+ */
135
+ interface KeyboardConfig {
136
+ /**
137
+ * 默认的键盘模式
138
+ */
139
+ defaultMode?: 'en' | 'zh' | 'hand' | 'num' | 'symbol';
140
+ /**
141
+ * 是否启用手写输入
142
+ */
143
+ enableHandwriting?: boolean;
144
+ /**
145
+ * 键盘定位模式
146
+ */
147
+ position?: 'static' | 'float' | 'bottom';
148
+ /**
149
+ * 浮动模式下键盘与输入框的距离
150
+ * @default 10
151
+ */
152
+ floatMarginTop?: number;
153
+ /**
154
+ * 当没有input获得焦点时是否禁用键盘
155
+ */
156
+ disableWhenNoFocus?: boolean;
157
+ /**
158
+ * 是否启用手动打开模式
159
+ * 启用后不再根据焦点自动显示,需要通过实例方法手动调用 open / close / destroy。
160
+ */
161
+ manual?: boolean;
162
+ /**
163
+ * 数字键盘的行配置
164
+ */
165
+ numKeys?: string[][];
166
+ /**
167
+ * RIME WASM 文件及数据文件的 URL 或路径前缀。
168
+ * 用于默认 RIME 引擎加载(当未通过 registerPinyinEngine 注册自定义引擎时)。
169
+ * @default '/rime'
170
+ */
171
+ wasmDir?: string;
172
+ }
173
+ /**
174
+ * 获取全局键盘配置
175
+ */
176
+ declare function getKeyboardConfig(): KeyboardConfig;
177
+ /**
178
+ * 设置全局键盘配置
179
+ */
180
+ declare function setKeyboardConfig(config: KeyboardConfig): void;
181
+ /**
182
+ * 注册手写识别服务
183
+ * @param recognizer 手写识别服务实现
184
+ */
185
+ declare function registerHandwritingRecognizer(recognizer: HandwritingRecognizer): void;
186
+ /**
187
+ * 获取手写识别服务实例
188
+ * @returns 手写识别服务实例
189
+ */
190
+ declare function getHandwritingRecognizer(): HandwritingRecognizer | null;
191
+ /**
192
+ * 注册拼音引擎。
193
+ * 注册后,CandidateBar 将使用此引擎而非默认的 RIME 引擎。
194
+ * 适用于自定义引擎或 Worker 中运行的引擎。
195
+ * @param engine 拼音引擎实现
196
+ */
197
+ declare function registerPinyinEngine(engine: PinyinEngine): void;
198
+ /**
199
+ * 获取已注册的拼音引擎实例
200
+ * @returns 拼音引擎实例,未注册时返回 null
201
+ */
202
+ declare function getPinyinEngine(): PinyinEngine | null;
203
+ //#endregion
204
+ //#region src/dom.d.ts
205
+ interface KeyboardPosition {
206
+ top: string;
207
+ left: string;
208
+ }
209
+ /**
210
+ * 计算键盘的显示位置
211
+ * @param inputElement 输入框元素
212
+ * @param keyboardElement 键盘元素
213
+ * @param positionMode 位置模式:'static'(静态,不计算位置)、'float'(浮动,根据输入框位置计算)、'bottom'(固定在底部)
214
+ * @returns 返回键盘的 top 和 left 位置,如果不需要计算则返回 null
215
+ */
216
+ declare function calculateKeyboardPosition(inputElement: HTMLElement | null, keyboardElement: HTMLElement | null, positionMode: 'static' | 'float' | 'bottom', floatMarginTop?: number): KeyboardPosition | null;
217
+ //#endregion
218
+ //#region src/input.d.ts
219
+ type TextInputElement = HTMLInputElement | HTMLTextAreaElement;
220
+ /**
221
+ * 判断元素是否为可输入元素(支持文本输入的 input 或 textarea)
222
+ * @param el 要检测的元素
223
+ * @returns 如果是可输入元素则返回 true
224
+ */
225
+ declare function isInputElement(el?: Element | null): el is TextInputElement;
226
+ /**
227
+ * 获取当前激活的输入元素
228
+ * @throws {Error} 如果当前没有激活的输入框
229
+ */
230
+ declare function getInputElement(): TextInputElement;
231
+ /**
232
+ * 向输入框插入文本
233
+ * @param inputElement 输入框元素
234
+ * @param text 要插入的文本
235
+ */
236
+ declare function writeToInputElement(inputElement: TextInputElement, text: string): void;
237
+ /**
238
+ * 向输入框删除文本
239
+ * @param inputElement 输入框元素
240
+ */
241
+ declare function delToInputElement(inputElement: TextInputElement): void;
242
+ /**
243
+ * 设置输入框光标位置
244
+ * @param inputElement 输入框元素
245
+ * @param index 光标位置索引
246
+ */
247
+ declare function moveCursor(inputElement: TextInputElement, index: number): void;
248
+ //#endregion
249
+ //#region src/repeat.d.ts
250
+ interface KeyRepeatOptions {
251
+ /** Long-press delay before repeating starts (ms). */
252
+ delay?: number;
253
+ /** Repeat interval after the initial delay (ms). */
254
+ interval?: number;
255
+ }
256
+ interface KeyRepeater {
257
+ start: (action: () => void) => void;
258
+ stop: () => void;
259
+ }
260
+ /**
261
+ * Create a small, framework-agnostic key repeater for long-press.
262
+ *
263
+ * - Calls `action()` immediately on start.
264
+ * - After `delay`, repeats with `interval` until `stop()` is called.
265
+ */
266
+ declare function createKeyRepeater(options?: KeyRepeatOptions): KeyRepeater;
267
+ //#endregion
268
+ export { Candidate, CanvasDrawer, CanvasDrawerOptions, HandwritingRecognizer, KeyRepeatOptions, KeyRepeater, KeyboardConfig, KeyboardPosition, PinyinEngine, PinyinState, RecognizerInitOptions, TextInputElement, calculateKeyboardPosition, createKeyRepeater, delToInputElement, getHandwritingRecognizer, getInputElement, getKeyboardConfig, getPinyinEngine, isInputElement, moveCursor, registerHandwritingRecognizer, registerPinyinEngine, setKeyboardConfig, writeToInputElement };
269
+ //# sourceMappingURL=index.d.mts.map