chatluna-plugin-onebot-sandbox 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.
- package/LICENSE +21 -0
- package/README.md +20 -0
- package/client/app.tsx +306 -0
- package/client/chat/chat-pane.tsx +303 -0
- package/client/chat/composer.css +635 -0
- package/client/chat/composer.tsx +646 -0
- package/client/chat/details-panel.tsx +196 -0
- package/client/chat/emoji-picker.tsx +70 -0
- package/client/chat/extension-mark.tsx +16 -0
- package/client/chat/forward.tsx +257 -0
- package/client/chat/image-preview.tsx +93 -0
- package/client/chat/member-menu.tsx +64 -0
- package/client/chat/message-list.tsx +617 -0
- package/client/chat/messages.css +1018 -0
- package/client/chat/notifications.tsx +83 -0
- package/client/chat/reactions.tsx +52 -0
- package/client/chat/search-panel.tsx +231 -0
- package/client/chat/sidebar.tsx +424 -0
- package/client/chat/webqq-view.tsx +71 -0
- package/client/environment/avatar-picker.tsx +92 -0
- package/client/environment/create-popover.tsx +228 -0
- package/client/environment/directory-model.ts +63 -0
- package/client/environment/entity-dialog.css +383 -0
- package/client/environment/entity-dialog.tsx +442 -0
- package/client/environment/page.css +14 -0
- package/client/environment/page.tsx +280 -0
- package/client/i18n.ts +19 -0
- package/client/index.ts +17 -0
- package/client/locales/en.yml +617 -0
- package/client/locales/zh.yml +617 -0
- package/client/mcp/capability-catalog.tsx +277 -0
- package/client/mcp/credential-manager.tsx +257 -0
- package/client/onebot-debug/page.tsx +329 -0
- package/client/records/record-layout.tsx +429 -0
- package/client/records/records.css +259 -0
- package/client/settings/about.tsx +43 -0
- package/client/settings/draft.ts +47 -0
- package/client/settings/localize.ts +76 -0
- package/client/settings/page.tsx +210 -0
- package/client/settings/settings.css +47 -0
- package/client/shared/avatar.tsx +35 -0
- package/client/shared/confirm-dialog.tsx +44 -0
- package/client/shared/controls.css +17 -0
- package/client/shared/discard-confirm.tsx +45 -0
- package/client/shared/floating-panel.ts +40 -0
- package/client/shared/format-duration.ts +22 -0
- package/client/shared/format-time.ts +49 -0
- package/client/shared/glass.css +150 -0
- package/client/shared/implementation.ts +12 -0
- package/client/shared/overlay-scrollbar.css +45 -0
- package/client/shared/overlay-scrollbar.tsx +123 -0
- package/client/shared/use-element-width.ts +19 -0
- package/client/shared/use-retained.ts +12 -0
- package/client/test-call/page.tsx +260 -0
- package/client/test-space/agent-visuals.tsx +121 -0
- package/client/test-space/observe-overlay.tsx +42 -0
- package/client/test-space/overview.tsx +267 -0
- package/client/test-space/spaces.css +201 -0
- package/client/test-space/thumbnail-capture.ts +63 -0
- package/client/test-space/thumbnail-scale.ts +21 -0
- package/client/test-space/use-space-list.ts +35 -0
- package/client/test-space/zoom.ts +197 -0
- package/client/webqq/composer-draft-host.ts +578 -0
- package/client/webqq/composer-draft.ts +81 -0
- package/client/webqq/conversation-tree.ts +155 -0
- package/client/webqq/emoji-catalog.ts +78 -0
- package/client/webqq/fork-boundary.ts +28 -0
- package/client/webqq/forward-cluster.ts +20 -0
- package/client/webqq/forward-dialog-stack.ts +74 -0
- package/client/webqq/forward-preview.ts +40 -0
- package/client/webqq/friend-menu.ts +17 -0
- package/client/webqq/group-display.ts +18 -0
- package/client/webqq/group-menu.ts +25 -0
- package/client/webqq/mention.ts +4 -0
- package/client/webqq/message-capabilities.ts +37 -0
- package/client/webqq/message-cluster.ts +68 -0
- package/client/webqq/message-presentation.ts +197 -0
- package/client/webqq/message-reveal.ts +31 -0
- package/client/webqq/message-search-date.ts +40 -0
- package/client/webqq/message-selection.ts +85 -0
- package/client/webqq/notification-requests.ts +20 -0
- package/client/webqq/participant-presentation.ts +128 -0
- package/client/webqq/profile-card.ts +199 -0
- package/client/webqq/relationship-directory.ts +62 -0
- package/client/webqq/thinking-panel.ts +7 -0
- package/client/webqq/user-stack.ts +53 -0
- package/client/workspace/agent-overlay-effect.ts +238 -0
- package/client/workspace/context.tsx +117 -0
- package/client/workspace/overlay-host.tsx +299 -0
- package/client/workspace/profile-card.css +164 -0
- package/client/workspace/state.ts +28 -0
- package/client/workspace/store.ts +388 -0
- package/client/workspace/use-test-spaces.ts +21 -0
- package/dist/.vite/manifest.json +21 -0
- package/dist/app-63rfTh6w.js +85 -0
- package/dist/client.css +4 -0
- package/dist/client.js +1 -0
- package/lib/account-profile.d.ts +17 -0
- package/lib/account-profile.d.ts.map +1 -0
- package/lib/base-dir.d.ts +4 -0
- package/lib/base-dir.d.ts.map +1 -0
- package/lib/bot.d.ts +185 -0
- package/lib/bot.d.ts.map +1 -0
- package/lib/builtin-avatar-options.d.ts +11 -0
- package/lib/builtin-avatar-options.d.ts.map +1 -0
- package/lib/builtin-avatars.d.ts +6 -0
- package/lib/builtin-avatars.d.ts.map +1 -0
- package/lib/channel-assignee.d.ts +52 -0
- package/lib/channel-assignee.d.ts.map +1 -0
- package/lib/chatluna/conversation-lines.d.ts +46 -0
- package/lib/chatluna/conversation-lines.d.ts.map +1 -0
- package/lib/chatluna/runtime.d.ts +26 -0
- package/lib/chatluna/runtime.d.ts.map +1 -0
- package/lib/chatluna/state.d.ts +67 -0
- package/lib/chatluna/state.d.ts.map +1 -0
- package/lib/chatluna/thinking.d.ts +10 -0
- package/lib/chatluna/thinking.d.ts.map +1 -0
- package/lib/chatluna/tools.d.ts +36 -0
- package/lib/chatluna/tools.d.ts.map +1 -0
- package/lib/chatluna/wakeup.d.ts +143 -0
- package/lib/chatluna/wakeup.d.ts.map +1 -0
- package/lib/console-contract.d.ts +131 -0
- package/lib/console-contract.d.ts.map +1 -0
- package/lib/console.d.ts +31 -0
- package/lib/console.d.ts.map +1 -0
- package/lib/control-service.d.ts +425 -0
- package/lib/control-service.d.ts.map +1 -0
- package/lib/conversation-resolution.d.ts +157 -0
- package/lib/conversation-resolution.d.ts.map +1 -0
- package/lib/error-detail.d.ts +3 -0
- package/lib/error-detail.d.ts.map +1 -0
- package/lib/inbound-delivery.d.ts +156 -0
- package/lib/inbound-delivery.d.ts.map +1 -0
- package/lib/index.d.ts +76 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +12834 -0
- package/lib/index.js.map +1 -0
- package/lib/lifecycle.d.ts +8 -0
- package/lib/lifecycle.d.ts.map +1 -0
- package/lib/mcp/arguments.d.ts +71 -0
- package/lib/mcp/arguments.d.ts.map +1 -0
- package/lib/mcp/call-records.d.ts +32 -0
- package/lib/mcp/call-records.d.ts.map +1 -0
- package/lib/mcp/http-api.d.ts +147 -0
- package/lib/mcp/http-api.d.ts.map +1 -0
- package/lib/mcp/page-cursor.d.ts +37 -0
- package/lib/mcp/page-cursor.d.ts.map +1 -0
- package/lib/mcp/server.d.ts +60 -0
- package/lib/mcp/server.d.ts.map +1 -0
- package/lib/mcp/service.d.ts +219 -0
- package/lib/mcp/service.d.ts.map +1 -0
- package/lib/mcp/tool-registry.d.ts +113 -0
- package/lib/mcp/tool-registry.d.ts.map +1 -0
- package/lib/mcp/types.d.ts +144 -0
- package/lib/mcp/types.d.ts.map +1 -0
- package/lib/media-storage.d.ts +33 -0
- package/lib/media-storage.d.ts.map +1 -0
- package/lib/message-capabilities.d.ts +78 -0
- package/lib/message-capabilities.d.ts.map +1 -0
- package/lib/onebot-debug.d.ts +115 -0
- package/lib/onebot-debug.d.ts.map +1 -0
- package/lib/onebot-message.d.ts +23 -0
- package/lib/onebot-message.d.ts.map +1 -0
- package/lib/onebot-profiles.d.ts +45 -0
- package/lib/onebot-profiles.d.ts.map +1 -0
- package/lib/persistence.d.ts +148 -0
- package/lib/persistence.d.ts.map +1 -0
- package/lib/random.d.ts +5 -0
- package/lib/random.d.ts.map +1 -0
- package/lib/record-store.d.ts +80 -0
- package/lib/record-store.d.ts.map +1 -0
- package/lib/relationship-actions.d.ts +157 -0
- package/lib/relationship-actions.d.ts.map +1 -0
- package/lib/scope-directory.d.ts +105 -0
- package/lib/scope-directory.d.ts.map +1 -0
- package/lib/settings.d.ts +33 -0
- package/lib/settings.d.ts.map +1 -0
- package/lib/test-spaces.d.ts +69 -0
- package/lib/test-spaces.d.ts.map +1 -0
- package/lib/types.d.ts +739 -0
- package/lib/types.d.ts.map +1 -0
- package/lib/webui.d.ts +26 -0
- package/lib/webui.d.ts.map +1 -0
- package/package.json +112 -0
|
@@ -0,0 +1,578 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createEmptyComposerDraft,
|
|
3
|
+
deleteComposerBackward,
|
|
4
|
+
detectMentionTrigger,
|
|
5
|
+
filterMentionCandidates,
|
|
6
|
+
insertComposerMention,
|
|
7
|
+
isComposerDraftEmpty,
|
|
8
|
+
normalizeComposerTokens,
|
|
9
|
+
replaceComposerTextRange,
|
|
10
|
+
serializeComposerDraft,
|
|
11
|
+
type ComposerDraft,
|
|
12
|
+
type ComposerDraftToken,
|
|
13
|
+
type MentionCandidate,
|
|
14
|
+
} from './composer-draft'
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* 草稿与 contenteditable 之间的双向转换、光标读写、输入法状态与提及候选菜单。
|
|
18
|
+
*
|
|
19
|
+
* 纯 token 计算在 `composer-draft.ts`;这里处理「怎么把它和 contenteditable 对上」:从子节点
|
|
20
|
+
* 遍历回读出草稿、把草稿渲染成节点、把光标放到第几个 token 的第几个偏移、输入法未上屏时不许
|
|
21
|
+
* 读回、input 事件早于 Selection 更新时要等一拍。它们的失效形态都是静默的——光标跳到开头、
|
|
22
|
+
* 中文输入法上屏后前一个字符消失、退格删掉半个提及——不报错,所以收在纯模块里逐条断言。
|
|
23
|
+
*
|
|
24
|
+
* 候选菜单与草稿同处一个模块:每次输入都要重算菜单、方向键要被路由给菜单、选中候选项又要写回
|
|
25
|
+
* 草稿并移动光标,拆开会让两者之间的 interface 宽到没有意义。
|
|
26
|
+
*
|
|
27
|
+
* 状态变化经 `notify` 回调通知视图。判定进这个模块,带副作用与有时序的宿主操作按最小结构接口
|
|
28
|
+
* 注入:不注入 DOM 类型,视图负责把 Text 节点、提及芯片与 Selection 映射成下面这几个朴素结构。
|
|
29
|
+
*/
|
|
30
|
+
|
|
31
|
+
/** 编辑器一个子节点的结构读数。视图从 DOM 映射出来,映射本身是机械动作。 */
|
|
32
|
+
export type ComposerHostNodeReading =
|
|
33
|
+
| { readonly kind: 'text', readonly text: string }
|
|
34
|
+
| { readonly kind: 'mention', readonly id: string, readonly name: string }
|
|
35
|
+
/** 浏览器在 contenteditable 里换行时插入的 `<br>`。 */
|
|
36
|
+
| { readonly kind: 'line-break' }
|
|
37
|
+
/** 浏览器编辑时套上的容器元素,正文在它的子节点里。 */
|
|
38
|
+
| { readonly kind: 'container', readonly children: readonly ComposerHostNodeReading[] }
|
|
39
|
+
|
|
40
|
+
/** 要渲染进编辑器的节点。与 token 一一对应,视图按它造 Text 节点与提及芯片。 */
|
|
41
|
+
export type ComposerHostNodePlan =
|
|
42
|
+
| { readonly kind: 'text', readonly text: string }
|
|
43
|
+
| { readonly kind: 'mention', readonly id: string, readonly name: string }
|
|
44
|
+
|
|
45
|
+
/** 光标在宿主里的落点读数。 */
|
|
46
|
+
export type ComposerHostCaretReading =
|
|
47
|
+
/** 落在编辑器元素本身上,偏移是子节点序号。 */
|
|
48
|
+
| { readonly kind: 'editor', readonly childOffset: number }
|
|
49
|
+
/** 落在第 `childIndex` 个子节点里,含落进提及芯片内部的文字。 */
|
|
50
|
+
| { readonly kind: 'child', readonly childIndex: number, readonly offset: number }
|
|
51
|
+
|
|
52
|
+
/** 光标要写到哪里。 */
|
|
53
|
+
export type ComposerHostCaretTarget =
|
|
54
|
+
| { readonly kind: 'child', readonly childIndex: number, readonly offset: number }
|
|
55
|
+
/** 目标 token 不存在:落到编辑器末尾。 */
|
|
56
|
+
| { readonly kind: 'editor-end' }
|
|
57
|
+
|
|
58
|
+
export interface ComposerDraftCaret {
|
|
59
|
+
readonly tokenIndex: number
|
|
60
|
+
readonly offset: number
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* 空文本 token 的光标锚点。
|
|
65
|
+
*
|
|
66
|
+
* 提及芯片不可编辑,两侧必须各有一个真实文本节点,用户才点得出光标;空文本节点在宿主里没有
|
|
67
|
+
* 可点面积,因此填一个零宽字符。回读时它不算正文,由 `normalizeComposerTokens` 去掉。
|
|
68
|
+
*/
|
|
69
|
+
export const COMPOSER_CARET_ANCHOR = ''
|
|
70
|
+
|
|
71
|
+
/** 从子节点结构读数回读出草稿 token。容器被摊平,换行成为正文里的换行字符。 */
|
|
72
|
+
export function readComposerDraftTokens(
|
|
73
|
+
readings: readonly ComposerHostNodeReading[],
|
|
74
|
+
): ComposerDraftToken[] {
|
|
75
|
+
const tokens: ComposerDraftToken[] = []
|
|
76
|
+
const walk = (reading: ComposerHostNodeReading) => {
|
|
77
|
+
if (reading.kind === 'text') {
|
|
78
|
+
tokens.push({ type: 'text', text: reading.text })
|
|
79
|
+
return
|
|
80
|
+
}
|
|
81
|
+
if (reading.kind === 'mention') {
|
|
82
|
+
tokens.push({ type: 'mention', id: reading.id, name: reading.name })
|
|
83
|
+
return
|
|
84
|
+
}
|
|
85
|
+
if (reading.kind === 'line-break') {
|
|
86
|
+
tokens.push({ type: 'text', text: '\n' })
|
|
87
|
+
return
|
|
88
|
+
}
|
|
89
|
+
reading.children.forEach(walk)
|
|
90
|
+
}
|
|
91
|
+
readings.forEach(walk)
|
|
92
|
+
return normalizeComposerTokens(tokens)
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/** 把草稿渲染成节点计划。 */
|
|
96
|
+
export function planComposerHostNodes(
|
|
97
|
+
tokens: readonly ComposerDraftToken[],
|
|
98
|
+
): ComposerHostNodePlan[] {
|
|
99
|
+
const plan = tokens.map((token): ComposerHostNodePlan => token.type === 'text'
|
|
100
|
+
? { kind: 'text', text: token.text || COMPOSER_CARET_ANCHOR }
|
|
101
|
+
: { kind: 'mention', id: token.id, name: token.name })
|
|
102
|
+
// 浏览器在空 contenteditable 里常插入 `<br>`;至少留一个文本节点,光标与 `:empty` 判定才成立。
|
|
103
|
+
return plan.length ? plan : [{ kind: 'text', text: '' }]
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* 把光标夹到一个可落脚的位置。
|
|
108
|
+
*
|
|
109
|
+
* 提及 token 上没有可落脚的偏移(芯片不可编辑),统一挪到后一个文本 token 起点;
|
|
110
|
+
* `normalizeComposerTokens` 保证每个提及后面都有一个文本 token。
|
|
111
|
+
*/
|
|
112
|
+
function clampDraftCaret(
|
|
113
|
+
tokens: readonly ComposerDraftToken[],
|
|
114
|
+
tokenIndex: number,
|
|
115
|
+
offset: number,
|
|
116
|
+
): ComposerDraftCaret {
|
|
117
|
+
const lastIndex = Math.max(0, tokens.length - 1)
|
|
118
|
+
const index = Math.min(Math.max(tokenIndex, 0), lastIndex)
|
|
119
|
+
const token = tokens[index]
|
|
120
|
+
if (token?.type === 'text') {
|
|
121
|
+
return { tokenIndex: index, offset: Math.min(Math.max(offset, 0), token.text.length) }
|
|
122
|
+
}
|
|
123
|
+
return { tokenIndex: Math.min(index + 1, lastIndex), offset: 0 }
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* 读回光标落在第几个 token 的第几个偏移。
|
|
128
|
+
*
|
|
129
|
+
* 落在编辑器元素上有两种含义:下一个子节点是文本时归到它的起点;是提及芯片时停在**前一个**
|
|
130
|
+
* 文本 token 末尾——用户此时看到的光标就贴在芯片左侧,接着退格删的应当是这个提及。
|
|
131
|
+
*
|
|
132
|
+
* 子节点序号按「一个 token 一个节点」对应。浏览器换行插入的 `<br>` 会在回读时并进相邻文本
|
|
133
|
+
* token,节点数从此多于 token 数,落在换行之后的光标因此夹到最后一个 token 上——这是治理前
|
|
134
|
+
* 就有的口径,本轮逐字保留,不在这里顺手改判。
|
|
135
|
+
*/
|
|
136
|
+
export function resolveComposerCaretFromReading(
|
|
137
|
+
tokens: readonly ComposerDraftToken[],
|
|
138
|
+
reading: ComposerHostCaretReading,
|
|
139
|
+
): ComposerDraftCaret {
|
|
140
|
+
if (reading.kind === 'editor') {
|
|
141
|
+
const next = tokens[reading.childOffset]
|
|
142
|
+
if (!next) return clampDraftCaret(tokens, tokens.length - 1, Number.MAX_SAFE_INTEGER)
|
|
143
|
+
if (next.type === 'mention') {
|
|
144
|
+
return clampDraftCaret(tokens, reading.childOffset - 1, Number.MAX_SAFE_INTEGER)
|
|
145
|
+
}
|
|
146
|
+
return clampDraftCaret(tokens, reading.childOffset, 0)
|
|
147
|
+
}
|
|
148
|
+
const token = tokens[reading.childIndex]
|
|
149
|
+
return clampDraftCaret(tokens, reading.childIndex, token?.type === 'text' ? reading.offset : 0)
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* 把「第几个 token 的第几个偏移」换成宿主的落点。
|
|
154
|
+
*
|
|
155
|
+
* 偏移只收下界不收上界:空文本 token 在宿主里是一个零宽字符节点,长度是 1 而不是 0,
|
|
156
|
+
* 真实节点长度只有宿主知道,上限由视图夹紧。
|
|
157
|
+
*/
|
|
158
|
+
export function resolveComposerCaretTarget(
|
|
159
|
+
tokens: readonly ComposerDraftToken[],
|
|
160
|
+
tokenIndex: number,
|
|
161
|
+
offset: number,
|
|
162
|
+
): ComposerHostCaretTarget {
|
|
163
|
+
const token = tokens[tokenIndex]
|
|
164
|
+
if (!token) return { kind: 'editor-end' }
|
|
165
|
+
if (token.type === 'text') {
|
|
166
|
+
return { kind: 'child', childIndex: tokenIndex, offset: Math.max(offset, 0) }
|
|
167
|
+
}
|
|
168
|
+
// 提及芯片不可编辑:光标写到它后面那个文本节点起点。
|
|
169
|
+
return { kind: 'child', childIndex: tokenIndex + 1, offset: 0 }
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* 把正文偏移换成宿主那个文本节点里的偏移。
|
|
174
|
+
*
|
|
175
|
+
* 零宽锚点是渲染空文本 token 时补进去的占位符,回读时不算正文。用户在空草稿里打过字之后节点里
|
|
176
|
+
* 仍留着它(那次输入不重渲染),草稿 token 因此比真实节点短一位;直接把正文偏移当节点偏移写回去
|
|
177
|
+
* 会让光标停在最后一个字之前,表现为右键「回复」之后接着打的字插到草稿中间。
|
|
178
|
+
*
|
|
179
|
+
* 上界也在这里收:真实节点文本只有视图读得到,走到末尾就停下。
|
|
180
|
+
*/
|
|
181
|
+
export function resolveComposerHostTextOffset(text: string, offset: number): number {
|
|
182
|
+
let remaining = Math.max(offset, 0)
|
|
183
|
+
let index = 0
|
|
184
|
+
while (index < text.length && remaining > 0) {
|
|
185
|
+
if (text[index] !== COMPOSER_CARET_ANCHOR) remaining -= 1
|
|
186
|
+
index += 1
|
|
187
|
+
}
|
|
188
|
+
// 落点后面紧跟锚点时也要跨过去:停在锚点前会让下一个字插到这个不算正文的字符之前。
|
|
189
|
+
while (index < text.length && text[index] === COMPOSER_CARET_ANCHOR) index += 1
|
|
190
|
+
return index
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/** 候选菜单开着时它盯住的那个 `@片段`:在第几个 token、从第几位开始、已经打进去了什么。 */
|
|
194
|
+
export interface ComposerMentionMenuState {
|
|
195
|
+
readonly tokenIndex: number
|
|
196
|
+
readonly start: number
|
|
197
|
+
readonly query: string
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* 菜单该不该开。
|
|
202
|
+
*
|
|
203
|
+
* 四条关闭条件各有各的理由:一个候选都没有时(私聊)开个空菜单只是挡住输入区;组字期间宿主里
|
|
204
|
+
* 是未上屏的拼音,据它过滤等于用拼音去搜人;光标不在文本 token 上时没有可检测的片段;
|
|
205
|
+
* 片段里一出现空白就说明用户已经在写正文而不是在挑人。
|
|
206
|
+
*/
|
|
207
|
+
export function resolveComposerMentionMenu(input: {
|
|
208
|
+
readonly tokens: readonly ComposerDraftToken[]
|
|
209
|
+
readonly tokenIndex: number
|
|
210
|
+
readonly offset: number
|
|
211
|
+
readonly composing: boolean
|
|
212
|
+
readonly hasCandidates: boolean
|
|
213
|
+
}): ComposerMentionMenuState | undefined {
|
|
214
|
+
if (!input.hasCandidates || input.composing) return
|
|
215
|
+
const token = input.tokens[input.tokenIndex]
|
|
216
|
+
if (token?.type !== 'text') return
|
|
217
|
+
const trigger = detectMentionTrigger(token.text, input.offset)
|
|
218
|
+
if (!trigger) return
|
|
219
|
+
return { tokenIndex: input.tokenIndex, start: trigger.start, query: trigger.query }
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
export type ComposerKeyAction =
|
|
223
|
+
/** 不消费,交给宿主的默认行为:普通字符、换行、移动光标、删一个字符。 */
|
|
224
|
+
| { readonly kind: 'none' }
|
|
225
|
+
/** 发送中或没有会话:吞掉这次按键,输入区整体不接受编辑。 */
|
|
226
|
+
| { readonly kind: 'blocked' }
|
|
227
|
+
| { readonly kind: 'move-candidate', readonly delta: 1 | -1 }
|
|
228
|
+
| { readonly kind: 'select-candidate' }
|
|
229
|
+
| { readonly kind: 'close-menu' }
|
|
230
|
+
/** 光标正贴在提及右边,这次退格删掉整块提及。 */
|
|
231
|
+
| { readonly kind: 'delete-mention' }
|
|
232
|
+
/** 交回调用方去发送。发送编排不反过来问菜单开没开。 */
|
|
233
|
+
| { readonly kind: 'submit' }
|
|
234
|
+
|
|
235
|
+
/**
|
|
236
|
+
* 光标是不是正贴在一个提及的右边。
|
|
237
|
+
*
|
|
238
|
+
* 只认「当前是文本 token 且偏移为 0,前一个 token 是提及」这一种形状——那正是用户看到光标
|
|
239
|
+
* 紧贴芯片右侧的位置。`normalizeComposerTokens` 保证提及永远不在第 0 位,因此前一个 token 一定
|
|
240
|
+
* 存在;读回光标也永远不会停在提及 token 上(会被挪到它后面那个文本 token 的起点),
|
|
241
|
+
* 所以「光标在提及内部」这种情形已经被归一成这同一种形状。
|
|
242
|
+
*
|
|
243
|
+
* **偏移 1 不算边界。** 选中候选后提及右边会自动补一个不换行空格,光标停在空格之后(偏移 1)。
|
|
244
|
+
* 那个位置上的退格只删掉那个空格,提及留着——这是治理前的行为,本轮保留。
|
|
245
|
+
*/
|
|
246
|
+
export function isComposerMentionBoundary(
|
|
247
|
+
tokens: readonly ComposerDraftToken[],
|
|
248
|
+
tokenIndex: number,
|
|
249
|
+
offset: number,
|
|
250
|
+
): boolean {
|
|
251
|
+
if (offset !== 0) return false
|
|
252
|
+
if (tokens[tokenIndex]?.type !== 'text') return false
|
|
253
|
+
return tokens[tokenIndex - 1]?.type === 'mention'
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
/**
|
|
257
|
+
* 退格落在提及边界上:整块删掉那个提及,光标停在它原来的位置。
|
|
258
|
+
*
|
|
259
|
+
* token 算术复用 `deleteComposerBackward`,光标不复用它给的落点:它把光标放到合并后那个文本
|
|
260
|
+
* token 的**末尾**,提及后面还有正文时那是行尾而不是提及原来的位置,表现为「删掉一个提及,
|
|
261
|
+
* 光标却跳到了行尾」。删掉提及之后前后两段文本会合并成一个 token,提及原来的位置就是
|
|
262
|
+
* 前一段文本的长度。
|
|
263
|
+
*/
|
|
264
|
+
export function deleteComposerMentionAtBoundary(
|
|
265
|
+
tokens: readonly ComposerDraftToken[],
|
|
266
|
+
tokenIndex: number,
|
|
267
|
+
): ComposerDraft {
|
|
268
|
+
const before = tokens[tokenIndex - 2]
|
|
269
|
+
return {
|
|
270
|
+
tokens: deleteComposerBackward(tokens, tokenIndex, 0).tokens,
|
|
271
|
+
tokenIndex: Math.max(0, tokenIndex - 2),
|
|
272
|
+
offset: before?.type === 'text' ? before.text.length : 0,
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
/**
|
|
277
|
+
* 这一次按键是什么意思。
|
|
278
|
+
*
|
|
279
|
+
* 「Enter 到底是发送还是选中候选」由这一个函数回答,调用方只按返回值决定要不要 `preventDefault`。
|
|
280
|
+
*
|
|
281
|
+
* 退格只在提及边界上被消费,让「整块删掉提及」这条规则由本模块说了算,两个引擎口径一致;
|
|
282
|
+
* 其余位置的退格照旧交给宿主,删一个字符就是删一个字符。菜单开着时不可能同时落在提及边界上
|
|
283
|
+
* (菜单要求光标前面有 `@片段`,边界要求偏移为 0),两个分支互不相交。
|
|
284
|
+
*/
|
|
285
|
+
export function routeComposerKey(input: {
|
|
286
|
+
readonly key: string
|
|
287
|
+
readonly shiftKey: boolean
|
|
288
|
+
readonly disabled: boolean
|
|
289
|
+
readonly composing: boolean
|
|
290
|
+
readonly menuOpen: boolean
|
|
291
|
+
readonly mentionBoundary: boolean
|
|
292
|
+
}): ComposerKeyAction {
|
|
293
|
+
if (input.disabled) return { kind: 'blocked' }
|
|
294
|
+
|
|
295
|
+
if (input.menuOpen) {
|
|
296
|
+
if (input.key === 'ArrowDown') return { kind: 'move-candidate', delta: 1 }
|
|
297
|
+
if (input.key === 'ArrowUp') return { kind: 'move-candidate', delta: -1 }
|
|
298
|
+
if (input.key === 'Enter' || input.key === 'Tab') return { kind: 'select-candidate' }
|
|
299
|
+
if (input.key === 'Escape') return { kind: 'close-menu' }
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
// 组字期间的 Enter 是上屏确认;shift+Enter 是换行。两者都不是发送。
|
|
303
|
+
if (input.key === 'Enter' && !input.shiftKey && !input.composing) return { kind: 'submit' }
|
|
304
|
+
// 组字期间的退格由输入法处理未上屏的拼音,不该动草稿。
|
|
305
|
+
if (input.key === 'Backspace' && input.mentionBoundary && !input.composing) return { kind: 'delete-mention' }
|
|
306
|
+
return { kind: 'none' }
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
/** 宿主操作。带副作用、有时序,全部按最小结构接口注入。 */
|
|
310
|
+
|
|
311
|
+
/** 草稿宿主的最小响应式:值写入即通知订阅方,派生值按读取时现算。React 侧以订阅触发重渲染。 */
|
|
312
|
+
interface Cell<T> { value: T }
|
|
313
|
+
function createCells(notify: () => void) {
|
|
314
|
+
const ref = <T>(initial: T): Cell<T> => {
|
|
315
|
+
let current = initial
|
|
316
|
+
return {
|
|
317
|
+
get value() { return current },
|
|
318
|
+
set value(next: T) {
|
|
319
|
+
if (Object.is(next, current)) return
|
|
320
|
+
current = next
|
|
321
|
+
notify()
|
|
322
|
+
},
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
const computed = <T>(read: () => T): { readonly value: T } => ({ get value() { return read() } })
|
|
326
|
+
return { ref, computed }
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
export interface ComposerDraftHostAdapter {
|
|
330
|
+
/** 回读编辑器子节点;宿主还没挂上时返回 undefined。 */
|
|
331
|
+
readNodes(): readonly ComposerHostNodeReading[] | undefined
|
|
332
|
+
/** 按计划整体替换编辑器子节点。 */
|
|
333
|
+
renderNodes(plan: readonly ComposerHostNodePlan[]): void
|
|
334
|
+
/** 读光标落点;光标不在编辑器里时返回 undefined。 */
|
|
335
|
+
readCaret(): ComposerHostCaretReading | undefined
|
|
336
|
+
/** 把光标写到目标落点。 */
|
|
337
|
+
writeCaret(target: ComposerHostCaretTarget): void
|
|
338
|
+
/** 等下一拍。contenteditable 的 input 事件有时早于 Selection 更新。 */
|
|
339
|
+
nextTick(): Promise<void>
|
|
340
|
+
/** 聚焦输入控件。 */
|
|
341
|
+
focus(): void
|
|
342
|
+
/**
|
|
343
|
+
* 当前可提及的候选。
|
|
344
|
+
*
|
|
345
|
+
* 候选的来源与「谁能被提及」由上层模型决定;宿主只管菜单什么时候开、按已输入的片段怎么过滤。
|
|
346
|
+
*/
|
|
347
|
+
readMentionCandidates(): readonly MentionCandidate[]
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
export function createComposerDraftHost(adapter: ComposerDraftHostAdapter, notify: () => void = () => {}) {
|
|
351
|
+
const { ref, computed } = createCells(notify)
|
|
352
|
+
const draft = ref<ComposerDraft>(createEmptyComposerDraft())
|
|
353
|
+
/** 输入法组字中。 */
|
|
354
|
+
const composing = ref(false)
|
|
355
|
+
/**
|
|
356
|
+
* 占位文案与发送按钮的可用性都看这一项。
|
|
357
|
+
*
|
|
358
|
+
* 组字期间草稿仍是空的(不读回),但宿主里已经有未上屏的拼音;此时若按「草稿为空」显示占位
|
|
359
|
+
* 文案,它会和拼音叠在一起。因此组字中一律不算空。
|
|
360
|
+
*/
|
|
361
|
+
const isEmpty = computed(() => !composing.value && isComposerDraftEmpty(draft.value.tokens))
|
|
362
|
+
const mentionMenu = ref<ComposerMentionMenuState | undefined>(undefined)
|
|
363
|
+
/**
|
|
364
|
+
* 高亮项的原始序号与夹紧后的序号分开。
|
|
365
|
+
*
|
|
366
|
+
* 候选表会随上层模型变短(有人退群、切换会话),夹紧写成 computed 而不是一个 watch:
|
|
367
|
+
* 高亮项因此永远指向一个真实存在的候选,不需要靠某次副作用去修正它。
|
|
368
|
+
*/
|
|
369
|
+
const rawMentionIndex = ref(0)
|
|
370
|
+
/** 菜单开着的条件里带上「候选列表非空」:私聊没有可提及的人,菜单一次都不该出现。 */
|
|
371
|
+
const mentionMenuOpen = computed(() => !!mentionMenu.value && adapter.readMentionCandidates().length > 0)
|
|
372
|
+
const mentionCandidates = computed(() => mentionMenu.value
|
|
373
|
+
? filterMentionCandidates(adapter.readMentionCandidates(), mentionMenu.value.query)
|
|
374
|
+
: [])
|
|
375
|
+
const mentionMenuIndex = computed(() => Math.min(
|
|
376
|
+
Math.max(rawMentionIndex.value, 0),
|
|
377
|
+
Math.max(0, mentionCandidates.value.length - 1),
|
|
378
|
+
))
|
|
379
|
+
/** 程序化替换节点期间不读回:宿主若把这次替换报成 input,读回会拿到半份内容。 */
|
|
380
|
+
let suppressRead = false
|
|
381
|
+
|
|
382
|
+
function closeMentionMenu() {
|
|
383
|
+
mentionMenu.value = undefined
|
|
384
|
+
rawMentionIndex.value = 0
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
function updateMentionMenu() {
|
|
388
|
+
const current = draft.value
|
|
389
|
+
const next = resolveComposerMentionMenu({
|
|
390
|
+
tokens: current.tokens,
|
|
391
|
+
tokenIndex: current.tokenIndex,
|
|
392
|
+
offset: current.offset,
|
|
393
|
+
composing: composing.value,
|
|
394
|
+
hasCandidates: adapter.readMentionCandidates().length > 0,
|
|
395
|
+
})
|
|
396
|
+
if (!next) {
|
|
397
|
+
closeMentionMenu()
|
|
398
|
+
return
|
|
399
|
+
}
|
|
400
|
+
mentionMenu.value = next
|
|
401
|
+
rawMentionIndex.value = 0
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
function render() {
|
|
405
|
+
suppressRead = true
|
|
406
|
+
adapter.renderNodes(planComposerHostNodes(draft.value.tokens))
|
|
407
|
+
suppressRead = false
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
function apply(next: ComposerDraft, options: { focus?: boolean } = {}) {
|
|
411
|
+
draft.value = {
|
|
412
|
+
tokens: normalizeComposerTokens(next.tokens),
|
|
413
|
+
tokenIndex: next.tokenIndex,
|
|
414
|
+
offset: next.offset,
|
|
415
|
+
}
|
|
416
|
+
render()
|
|
417
|
+
if (options.focus === false) return
|
|
418
|
+
// 等节点替换生效后再聚焦写光标;同一拍里写会落在已经被替换掉的旧节点上。
|
|
419
|
+
void adapter.nextTick().then(focus)
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
/**
|
|
423
|
+
* 把焦点交回输入框,光标落到草稿记着的位置。
|
|
424
|
+
*
|
|
425
|
+
* 光标必须显式写。右键菜单打开期间焦点先落在菜单容器上,浏览器为 contenteditable 记住的选区
|
|
426
|
+
* 到这时已经作废,只 focus 会让光标落在正文最前面,接着输入的字插到草稿前面。
|
|
427
|
+
*/
|
|
428
|
+
function focus() {
|
|
429
|
+
adapter.focus()
|
|
430
|
+
const current = draft.value
|
|
431
|
+
adapter.writeCaret(resolveComposerCaretTarget(current.tokens, current.tokenIndex, current.offset))
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
/** 清空草稿与候选菜单。切换会话与切换发送者时用,不抢焦点——用户可能正在别处操作。 */
|
|
435
|
+
function reset() {
|
|
436
|
+
apply(createEmptyComposerDraft(), { focus: false })
|
|
437
|
+
closeMentionMenu()
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
function readCaret(tokens: readonly ComposerDraftToken[]): ComposerDraftCaret | undefined {
|
|
441
|
+
const reading = adapter.readCaret()
|
|
442
|
+
if (!reading) return
|
|
443
|
+
return resolveComposerCaretFromReading(tokens, reading)
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
/** 按宿主当前 Selection 对齐草稿光标。指针与方向键之后用。 */
|
|
447
|
+
function syncCaretFromHost() {
|
|
448
|
+
const caret = readCaret(draft.value.tokens)
|
|
449
|
+
if (!caret) return
|
|
450
|
+
draft.value = { ...draft.value, ...caret }
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
/**
|
|
454
|
+
* 宿主报出一次输入。
|
|
455
|
+
*
|
|
456
|
+
* 两条时序都在这里:组字期间一个字都不读回;读回前先等一拍,否则读到的是旧光标,
|
|
457
|
+
* 表现为「单独输入 @ 之后候选菜单不弹」。
|
|
458
|
+
*/
|
|
459
|
+
async function handleInput() {
|
|
460
|
+
if (suppressRead) return
|
|
461
|
+
// 组字期间菜单仍要关掉:留着它会拿未上屏的拼音去过滤候选。
|
|
462
|
+
if (composing.value) {
|
|
463
|
+
closeMentionMenu()
|
|
464
|
+
return
|
|
465
|
+
}
|
|
466
|
+
await adapter.nextTick()
|
|
467
|
+
const readings = adapter.readNodes()
|
|
468
|
+
if (!readings) return
|
|
469
|
+
const tokens = readComposerDraftTokens(readings)
|
|
470
|
+
const caret = readCaret(tokens)
|
|
471
|
+
draft.value = {
|
|
472
|
+
tokens,
|
|
473
|
+
tokenIndex: caret?.tokenIndex ?? draft.value.tokenIndex,
|
|
474
|
+
offset: caret?.offset ?? draft.value.offset,
|
|
475
|
+
}
|
|
476
|
+
updateMentionMenu()
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
function startComposition() {
|
|
480
|
+
composing.value = true
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
/** 上屏。此刻宿主里的正文才是最终结果,立刻读回一次。 */
|
|
484
|
+
async function endComposition() {
|
|
485
|
+
composing.value = false
|
|
486
|
+
await handleInput()
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
/** 从别处(消息右键「提及某人」)插入一个提及。 */
|
|
490
|
+
function insertMention(mention: { id: string, name: string }) {
|
|
491
|
+
syncCaretFromHost()
|
|
492
|
+
const current = draft.value
|
|
493
|
+
const token = current.tokens[current.tokenIndex]
|
|
494
|
+
const offset = token?.type === 'text' ? current.offset : 0
|
|
495
|
+
apply(insertComposerMention(current.tokens, current.tokenIndex, offset, mention))
|
|
496
|
+
closeMentionMenu()
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
/** 选中一个候选:`@片段` 整段换成提及,光标越过自动补入的分隔空格停在提及之后。 */
|
|
500
|
+
function selectMentionCandidate(candidate: MentionCandidate) {
|
|
501
|
+
const menu = mentionMenu.value
|
|
502
|
+
if (!menu) return
|
|
503
|
+
const token = draft.value.tokens[menu.tokenIndex]
|
|
504
|
+
const end = token?.type === 'text' ? draft.value.offset : menu.start
|
|
505
|
+
apply(replaceComposerTextRange(
|
|
506
|
+
draft.value.tokens,
|
|
507
|
+
menu.tokenIndex,
|
|
508
|
+
menu.start,
|
|
509
|
+
Math.max(menu.start, end),
|
|
510
|
+
{ id: candidate.id, name: candidate.name },
|
|
511
|
+
))
|
|
512
|
+
closeMentionMenu()
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
function moveMentionSelection(delta: number) {
|
|
516
|
+
const total = mentionCandidates.value.length
|
|
517
|
+
if (!total) return
|
|
518
|
+
rawMentionIndex.value = (mentionMenuIndex.value + delta + total) % total
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
/**
|
|
522
|
+
* 路由一次按键,并把菜单相关的动作就地执行掉。
|
|
523
|
+
*
|
|
524
|
+
* 返回的动作同时回答了「这次按键有没有被候选菜单消费」:调用方只在 `none` 时放任宿主默认行为,
|
|
525
|
+
* 只在 `submit` 时走发送。发送编排因此不必反过来问菜单开没开。
|
|
526
|
+
*/
|
|
527
|
+
function routeKey(input: { key: string, shiftKey: boolean, disabled: boolean }): ComposerKeyAction {
|
|
528
|
+
const current = draft.value
|
|
529
|
+
const action = routeComposerKey({
|
|
530
|
+
key: input.key,
|
|
531
|
+
shiftKey: input.shiftKey,
|
|
532
|
+
disabled: input.disabled,
|
|
533
|
+
composing: composing.value,
|
|
534
|
+
menuOpen: mentionMenuOpen.value,
|
|
535
|
+
mentionBoundary: isComposerMentionBoundary(current.tokens, current.tokenIndex, current.offset),
|
|
536
|
+
})
|
|
537
|
+
if (action.kind === 'move-candidate') moveMentionSelection(action.delta)
|
|
538
|
+
if (action.kind === 'close-menu') closeMentionMenu()
|
|
539
|
+
if (action.kind === 'select-candidate') {
|
|
540
|
+
const candidate = mentionCandidates.value[mentionMenuIndex.value]
|
|
541
|
+
if (candidate) selectMentionCandidate(candidate)
|
|
542
|
+
}
|
|
543
|
+
if (action.kind === 'delete-mention') {
|
|
544
|
+
apply(deleteComposerMentionAtBoundary(current.tokens, current.tokenIndex))
|
|
545
|
+
}
|
|
546
|
+
return action
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
function serialize() {
|
|
550
|
+
return serializeComposerDraft(draft.value.tokens)
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
return {
|
|
554
|
+
draft,
|
|
555
|
+
composing,
|
|
556
|
+
isEmpty,
|
|
557
|
+
mentionMenuOpen,
|
|
558
|
+
mentionCandidates,
|
|
559
|
+
mentionMenuIndex,
|
|
560
|
+
render,
|
|
561
|
+
apply,
|
|
562
|
+
reset,
|
|
563
|
+
syncCaretFromHost,
|
|
564
|
+
handleInput,
|
|
565
|
+
startComposition,
|
|
566
|
+
endComposition,
|
|
567
|
+
insertMention,
|
|
568
|
+
selectMentionCandidate,
|
|
569
|
+
closeMentionMenu,
|
|
570
|
+
setMentionSelection: (index: number) => {
|
|
571
|
+
rawMentionIndex.value = index
|
|
572
|
+
},
|
|
573
|
+
routeKey,
|
|
574
|
+
serialize,
|
|
575
|
+
focus,
|
|
576
|
+
}
|
|
577
|
+
}
|
|
578
|
+
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
export interface ComposerMentionToken { type: 'mention', id: string, name: string }
|
|
2
|
+
export interface ComposerTextToken { type: 'text', text: string }
|
|
3
|
+
export type ComposerDraftToken = ComposerTextToken | ComposerMentionToken
|
|
4
|
+
export interface ComposerDraft { tokens: ComposerDraftToken[], tokenIndex: number, offset: number }
|
|
5
|
+
export interface MentionCandidate { id: string, name: string, avatar?: string, kind: 'user' | 'bot', keywords?: string[] }
|
|
6
|
+
|
|
7
|
+
export function createEmptyComposerDraft(): ComposerDraft { return { tokens: [{ type: 'text', text: '' }], tokenIndex: 0, offset: 0 } }
|
|
8
|
+
export function draftText(tokens: readonly ComposerDraftToken[]): string { return tokens.map((token) => token.type === 'text' ? token.text : `@${token.name}`).join('') }
|
|
9
|
+
export function isComposerDraftEmpty(tokens: readonly ComposerDraftToken[]): boolean { return !tokens.some((token) => token.type === 'mention' || !!token.text.trim()) }
|
|
10
|
+
export function serializeComposerDraft(tokens: readonly ComposerDraftToken[]): string { return tokens.map((token) => token.type === 'mention' ? `<at id="${token.id}"/>` : token.text.replace(/ /g, ' ')).join('').trim() }
|
|
11
|
+
|
|
12
|
+
export function normalizeComposerTokens(tokens: readonly ComposerDraftToken[]): ComposerDraftToken[] {
|
|
13
|
+
const normalized: ComposerDraftToken[] = []
|
|
14
|
+
for (const token of tokens) {
|
|
15
|
+
if (token.type === 'mention') normalized.push({ ...token })
|
|
16
|
+
else {
|
|
17
|
+
const text = token.text.replace(//g, '')
|
|
18
|
+
const previous = normalized.at(-1)
|
|
19
|
+
if (previous?.type === 'text') previous.text += text
|
|
20
|
+
else normalized.push({ type: 'text', text })
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
if (!normalized.length) return [{ type: 'text', text: '' }]
|
|
24
|
+
const withCarets: ComposerDraftToken[] = []
|
|
25
|
+
normalized.forEach((token, index) => { withCarets.push(token); if (token.type === 'mention' && (!normalized[index + 1] || normalized[index + 1].type === 'mention')) withCarets.push({ type: 'text', text: '' }) })
|
|
26
|
+
if (withCarets[0]?.type === 'mention') withCarets.unshift({ type: 'text', text: '' })
|
|
27
|
+
return withCarets
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function clampTextCaret(tokens: readonly ComposerDraftToken[], tokenIndex: number, offset: number): ComposerDraft {
|
|
31
|
+
let index = Math.min(Math.max(tokenIndex, 0), Math.max(0, tokens.length - 1))
|
|
32
|
+
if (tokens[index]?.type !== 'text') index = tokens.findIndex((token) => token.type === 'text')
|
|
33
|
+
if (index < 0) return createEmptyComposerDraft()
|
|
34
|
+
const token = tokens[index]
|
|
35
|
+
const text = token?.type === 'text' ? token.text : ''
|
|
36
|
+
return { tokens: [...tokens], tokenIndex: index, offset: Math.min(Math.max(offset, 0), text.length) }
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function insertComposerMention(tokens: readonly ComposerDraftToken[], tokenIndex: number, offset: number, mention: Omit<ComposerMentionToken, 'type'>): ComposerDraft { return replaceComposerTextRange(tokens, tokenIndex, offset, offset, mention) }
|
|
40
|
+
export function replaceComposerTextRange(tokens: readonly ComposerDraftToken[], tokenIndex: number, start: number, end: number, mention: Omit<ComposerMentionToken, 'type'>): ComposerDraft {
|
|
41
|
+
const source = tokens[tokenIndex]?.type === 'text' ? tokens[tokenIndex].text : ''
|
|
42
|
+
const from = Math.max(0, Math.min(start, end, source.length)), to = Math.max(from, Math.min(Math.max(start, end), source.length))
|
|
43
|
+
let before = source.slice(0, from)
|
|
44
|
+
const rawAfter = source.slice(to)
|
|
45
|
+
if (before && !/\s$/.test(before)) before += ' '
|
|
46
|
+
const after = rawAfter ? (rawAfter.startsWith(' ') ? rawAfter : ` ${rawAfter}`) : ' '
|
|
47
|
+
const next = normalizeComposerTokens([...tokens.slice(0, tokenIndex), ...(before ? [{ type: 'text' as const, text: before }] : []), { type: 'mention' as const, id: mention.id, name: mention.name }, { type: 'text' as const, text: after }, ...tokens.slice(tokenIndex + 1)])
|
|
48
|
+
const mentionIndex = next.findIndex((token, index) => index >= tokenIndex && token.type === 'mention' && token.id === mention.id)
|
|
49
|
+
const caretIndex = mentionIndex + 1
|
|
50
|
+
const caretToken = next[caretIndex]
|
|
51
|
+
// 光标越过自动补入的后置空格,继续输入才能得到“@用户 内容”,而不是“@用户内容 ”。
|
|
52
|
+
const caretOffset = caretToken?.type === 'text' && /^[ ]/.test(caretToken.text) ? 1 : 0
|
|
53
|
+
return clampTextCaret(next, caretIndex, caretOffset)
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export function deleteComposerBackward(tokens: readonly ComposerDraftToken[], tokenIndex: number, offset: number): ComposerDraft {
|
|
57
|
+
const caret = clampTextCaret(normalizeComposerTokens(tokens), tokenIndex, offset), current = caret.tokens[caret.tokenIndex]
|
|
58
|
+
const previous = caret.tokens[caret.tokenIndex - 1]
|
|
59
|
+
if (current?.type === 'text' && caret.offset === 1 && current.text.startsWith(' ') && previous?.type === 'mention') {
|
|
60
|
+
return clampTextCaret(normalizeComposerTokens([
|
|
61
|
+
...caret.tokens.slice(0, caret.tokenIndex - 1),
|
|
62
|
+
{ type: 'text', text: current.text.slice(1) },
|
|
63
|
+
...caret.tokens.slice(caret.tokenIndex + 1),
|
|
64
|
+
]), caret.tokenIndex - 1, Number.MAX_SAFE_INTEGER)
|
|
65
|
+
}
|
|
66
|
+
if (current?.type === 'text' && caret.offset > 0) return clampTextCaret(normalizeComposerTokens([...caret.tokens.slice(0, caret.tokenIndex), { type: 'text', text: current.text.slice(0, caret.offset - 1) + current.text.slice(caret.offset) }, ...caret.tokens.slice(caret.tokenIndex + 1)]), caret.tokenIndex, caret.offset - 1)
|
|
67
|
+
if (caret.tokenIndex <= 0) return caret
|
|
68
|
+
if (previous?.type === 'mention') return clampTextCaret(normalizeComposerTokens([...caret.tokens.slice(0, caret.tokenIndex - 1), ...caret.tokens.slice(caret.tokenIndex)]), caret.tokenIndex - 1, Number.MAX_SAFE_INTEGER)
|
|
69
|
+
return caret
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export function detectMentionTrigger(text: string, caretOffset: number): { query: string, start: number } | null {
|
|
73
|
+
const prefix = text.slice(0, caretOffset)
|
|
74
|
+
const match = /(^|[^\s@])?@([^\s@]*)$/.exec(prefix)
|
|
75
|
+
return match ? { query: match[2], start: caretOffset - match[2].length - 1 } : null
|
|
76
|
+
}
|
|
77
|
+
function candidateSearchTexts(candidate: MentionCandidate) { return [candidate.name, candidate.id, ...(candidate.keywords ?? [])] }
|
|
78
|
+
export function filterMentionCandidates(candidates: readonly MentionCandidate[], query: string): MentionCandidate[] {
|
|
79
|
+
const normalized = query.trim().toLocaleLowerCase()
|
|
80
|
+
return [...candidates].filter((candidate) => !normalized || candidateSearchTexts(candidate).some((text) => text.toLocaleLowerCase().includes(normalized))).sort((a, b) => { const ap = candidateSearchTexts(a).some((text) => text.toLocaleLowerCase().startsWith(normalized)), bp = candidateSearchTexts(b).some((text) => text.toLocaleLowerCase().startsWith(normalized)); return Number(bp) - Number(ap) || a.name.localeCompare(b.name, 'zh-Hans') || a.id.localeCompare(b.id) })
|
|
81
|
+
}
|