@alexsun-top/opencode-chat-room 0.1.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/README.md +121 -0
- package/README.zh-CN.md +121 -0
- package/cli.ts +5 -0
- package/index.ts +9 -0
- package/package.json +47 -0
- package/plugin.ts +487 -0
- package/server/chat-server.ts +554 -0
- package/server/chat.html +1856 -0
- package/src/center-client.ts +530 -0
- package/src/room-service.ts +169 -0
- package/src/store.ts +237 -0
package/server/chat.html
ADDED
|
@@ -0,0 +1,1856 @@
|
|
|
1
|
+
<!doctype html>
|
|
2
|
+
<html lang="zh-CN">
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="utf-8">
|
|
5
|
+
<meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover">
|
|
6
|
+
<meta name="color-scheme" content="light dark">
|
|
7
|
+
<title>闲叙 · 轻量聊天室</title>
|
|
8
|
+
<style>
|
|
9
|
+
* { box-sizing: border-box; }
|
|
10
|
+
[hidden] { display: none !important; }
|
|
11
|
+
|
|
12
|
+
:root {
|
|
13
|
+
--font-sans: "PingFang SC","Hiragino Sans GB","Noto Sans SC","Noto Sans CJK SC","Source Han Sans SC","Microsoft YaHei",system-ui,-apple-system,sans-serif;
|
|
14
|
+
--font-serif: "Songti SC","STSong","Noto Serif SC","Noto Serif CJK SC","Source Han Serif SC","SimSun",serif;
|
|
15
|
+
--font-mono: ui-monospace,"SF Mono","Cascadia Mono","JetBrains Mono",Consolas,monospace;
|
|
16
|
+
--bg: #f4f1ea;
|
|
17
|
+
--surface: #fdfcf8;
|
|
18
|
+
--surface-2: #ece9df;
|
|
19
|
+
--ink: #2a2721;
|
|
20
|
+
--ink-2: #777162;
|
|
21
|
+
--ink-3: #847c69;
|
|
22
|
+
--line: #e2ddd0;
|
|
23
|
+
--accent: #b85a35;
|
|
24
|
+
--accent-ink: #fdf6ef;
|
|
25
|
+
--accent-soft: #f6e3d6;
|
|
26
|
+
--danger: #b23a2e;
|
|
27
|
+
--danger-soft: #f9e7e2;
|
|
28
|
+
--mention-blue: #2e6cc4;
|
|
29
|
+
--shadow: 0 1px 2px rgb(48 38 25 / .05), 0 12px 32px rgb(48 38 25 / .08);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
@media (prefers-color-scheme: dark) {
|
|
33
|
+
:root {
|
|
34
|
+
--bg: #17150f;
|
|
35
|
+
--surface: #211e18;
|
|
36
|
+
--surface-2: #2c2820;
|
|
37
|
+
--ink: #eae5d8;
|
|
38
|
+
--ink-2: #a19a88;
|
|
39
|
+
--ink-3: #8a8372;
|
|
40
|
+
--line: #322e25;
|
|
41
|
+
--accent: #dd8761;
|
|
42
|
+
--accent-ink: #241208;
|
|
43
|
+
--accent-soft: #3b2418;
|
|
44
|
+
--danger: #e27a6c;
|
|
45
|
+
--danger-soft: #3a201b;
|
|
46
|
+
--mention-blue: #86a9ee;
|
|
47
|
+
--shadow: 0 1px 2px rgb(0 0 0 / .4), 0 14px 36px rgb(0 0 0 / .45);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
html, body { height: 100%; }
|
|
52
|
+
body {
|
|
53
|
+
margin: 0;
|
|
54
|
+
font-family: var(--font-sans);
|
|
55
|
+
font-size: 15px;
|
|
56
|
+
line-height: 1.6;
|
|
57
|
+
color: var(--ink);
|
|
58
|
+
background:
|
|
59
|
+
radial-gradient(1100px 520px at 50% -8%, color-mix(in srgb, var(--accent) 7%, transparent), transparent 65%),
|
|
60
|
+
var(--bg);
|
|
61
|
+
-webkit-tap-highlight-color: transparent;
|
|
62
|
+
}
|
|
63
|
+
::selection { background: var(--accent-soft); }
|
|
64
|
+
|
|
65
|
+
#app {
|
|
66
|
+
max-width: 760px;
|
|
67
|
+
height: 100vh;
|
|
68
|
+
height: 100dvh;
|
|
69
|
+
margin: 0 auto;
|
|
70
|
+
display: flex;
|
|
71
|
+
flex-direction: column;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/* ── 视图 ── */
|
|
75
|
+
.view { flex: 1; min-height: 0; }
|
|
76
|
+
#joinView {
|
|
77
|
+
display: flex;
|
|
78
|
+
overflow-y: auto;
|
|
79
|
+
padding: 40px 20px;
|
|
80
|
+
animation: rise .45s cubic-bezier(.2,.7,.3,1) both;
|
|
81
|
+
}
|
|
82
|
+
#chatView { display: flex; flex-direction: column; animation: view-in .3s ease both; }
|
|
83
|
+
|
|
84
|
+
@keyframes rise { from { opacity: 0; transform: translateY(12px); } }
|
|
85
|
+
@keyframes view-in { from { opacity: 0; } }
|
|
86
|
+
@keyframes msg-in { from { opacity: 0; transform: translateY(5px); } }
|
|
87
|
+
@keyframes pulse { 50% { opacity: .35; } }
|
|
88
|
+
|
|
89
|
+
@media (prefers-reduced-motion: reduce) {
|
|
90
|
+
*, *::before, *::after { animation: none !important; transition: none !important; }
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/* ── 加入 / 创建 ── */
|
|
94
|
+
.join-card {
|
|
95
|
+
width: 100%;
|
|
96
|
+
max-width: 424px;
|
|
97
|
+
margin: auto;
|
|
98
|
+
padding: 36px 32px 30px;
|
|
99
|
+
background: var(--surface);
|
|
100
|
+
border: 1px solid var(--line);
|
|
101
|
+
border-radius: 18px;
|
|
102
|
+
box-shadow: var(--shadow);
|
|
103
|
+
}
|
|
104
|
+
.wordmark {
|
|
105
|
+
margin: 0;
|
|
106
|
+
font-family: var(--font-serif);
|
|
107
|
+
font-size: 30px;
|
|
108
|
+
letter-spacing: .14em;
|
|
109
|
+
color: var(--ink);
|
|
110
|
+
}
|
|
111
|
+
.tagline { margin: 8px 0 26px; font-size: 13.5px; color: var(--ink-2); }
|
|
112
|
+
|
|
113
|
+
.field { display: block; margin-bottom: 14px; }
|
|
114
|
+
.field-label {
|
|
115
|
+
display: block;
|
|
116
|
+
margin-bottom: 6px;
|
|
117
|
+
font-size: 12.5px;
|
|
118
|
+
font-weight: 600;
|
|
119
|
+
letter-spacing: .02em;
|
|
120
|
+
color: var(--ink-2);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
.input {
|
|
124
|
+
width: 100%;
|
|
125
|
+
font: inherit;
|
|
126
|
+
font-size: 16px; /* 防止 iOS 聚焦时缩放 */
|
|
127
|
+
color: var(--ink);
|
|
128
|
+
background: var(--surface);
|
|
129
|
+
border: 1px solid var(--line);
|
|
130
|
+
border-radius: 10px;
|
|
131
|
+
padding: 11px 13px;
|
|
132
|
+
transition: border-color .15s, box-shadow .15s;
|
|
133
|
+
}
|
|
134
|
+
.input::placeholder { color: var(--ink-3); }
|
|
135
|
+
.input:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 20%, transparent); }
|
|
136
|
+
|
|
137
|
+
.btn {
|
|
138
|
+
appearance: none;
|
|
139
|
+
border: none;
|
|
140
|
+
cursor: pointer;
|
|
141
|
+
font: inherit;
|
|
142
|
+
font-weight: 600;
|
|
143
|
+
border-radius: 10px;
|
|
144
|
+
padding: 11px 18px;
|
|
145
|
+
transition: filter .15s, transform .1s, border-color .15s, color .15s;
|
|
146
|
+
}
|
|
147
|
+
.btn:active { transform: translateY(1px); }
|
|
148
|
+
.btn:disabled { opacity: .5; cursor: default; }
|
|
149
|
+
.btn.primary { background: var(--accent); color: var(--accent-ink); }
|
|
150
|
+
.btn.primary:not(:disabled):hover { filter: brightness(1.07); }
|
|
151
|
+
.btn.ghost { background: transparent; color: var(--ink-2); border: 1px solid var(--line); }
|
|
152
|
+
.btn.ghost:not(:disabled):hover { color: var(--ink); border-color: var(--ink-3); }
|
|
153
|
+
.btn.small { padding: 8px 14px; font-size: 13.5px; }
|
|
154
|
+
.btn:focus-visible, .chip:focus-visible, .room-row:focus-visible, .input:focus-visible,
|
|
155
|
+
.jump:focus-visible, .copy-id:focus-visible {
|
|
156
|
+
outline: 2px solid var(--accent);
|
|
157
|
+
outline-offset: 2px;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
#joinForm .btn.primary, #createForm .btn.ghost { width: 100%; margin-top: 4px; }
|
|
161
|
+
|
|
162
|
+
.or { display: flex; align-items: center; gap: 12px; margin: 18px 0; font-size: 12.5px; color: var(--ink-3); }
|
|
163
|
+
.or::before, .or::after { content: ""; flex: 1; height: 1px; background: var(--line); }
|
|
164
|
+
|
|
165
|
+
.alert {
|
|
166
|
+
margin-top: 16px;
|
|
167
|
+
padding: 10px 13px;
|
|
168
|
+
border-radius: 10px;
|
|
169
|
+
background: var(--danger-soft);
|
|
170
|
+
color: var(--danger);
|
|
171
|
+
font-size: 13.5px;
|
|
172
|
+
word-break: break-word;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
.hint { margin: 8px 0 0; font-size: 12.5px; color: var(--ink-3); }
|
|
176
|
+
|
|
177
|
+
.token-box { margin-top: 18px; border: 1px solid var(--line); border-radius: 12px; background: var(--surface); }
|
|
178
|
+
.token-box summary {
|
|
179
|
+
cursor: pointer;
|
|
180
|
+
padding: 11px 14px;
|
|
181
|
+
font-size: 13.5px;
|
|
182
|
+
font-weight: 600;
|
|
183
|
+
color: var(--ink-2);
|
|
184
|
+
list-style: none;
|
|
185
|
+
}
|
|
186
|
+
.token-box summary::-webkit-details-marker { display: none; }
|
|
187
|
+
.token-box summary::marker { content: none; }
|
|
188
|
+
.token-box summary::after { content: "▾"; margin-left: 6px; font-size: 11px; color: var(--ink-3); }
|
|
189
|
+
.token-box[open] summary::after { content: "▴"; }
|
|
190
|
+
.token-body { padding: 0 14px 14px; }
|
|
191
|
+
.token-body .btn { margin-top: 10px; }
|
|
192
|
+
|
|
193
|
+
.room-list { margin-top: 22px; }
|
|
194
|
+
.room-list h2 { margin: 0 0 8px; font-size: 13px; font-weight: 600; letter-spacing: .06em; color: var(--ink-2); }
|
|
195
|
+
#roomList { list-style: none; margin: 0; padding: 0; max-height: 240px; overflow-y: auto; }
|
|
196
|
+
.room-row {
|
|
197
|
+
position: relative;
|
|
198
|
+
display: flex;
|
|
199
|
+
flex-direction: column;
|
|
200
|
+
gap: 2px;
|
|
201
|
+
width: 100%;
|
|
202
|
+
padding: 9px 44px 9px 11px;
|
|
203
|
+
font: inherit;
|
|
204
|
+
text-align: left;
|
|
205
|
+
color: var(--ink);
|
|
206
|
+
background: none;
|
|
207
|
+
border: 1px solid transparent;
|
|
208
|
+
border-radius: 10px;
|
|
209
|
+
cursor: pointer;
|
|
210
|
+
transition: background .12s, border-color .12s;
|
|
211
|
+
}
|
|
212
|
+
.room-row:hover { background: var(--surface-2); border-color: var(--line); }
|
|
213
|
+
.r-name { font-weight: 600; font-size: 14px; }
|
|
214
|
+
.r-meta { font-size: 12px; color: var(--ink-3); font-variant-numeric: tabular-nums; }
|
|
215
|
+
.r-go {
|
|
216
|
+
position: absolute;
|
|
217
|
+
right: 12px;
|
|
218
|
+
top: 50%;
|
|
219
|
+
transform: translateY(-50%);
|
|
220
|
+
font-size: 12px;
|
|
221
|
+
font-weight: 600;
|
|
222
|
+
color: var(--accent);
|
|
223
|
+
opacity: 0;
|
|
224
|
+
transition: opacity .12s;
|
|
225
|
+
}
|
|
226
|
+
.room-row:hover .r-go, .room-row:focus-visible .r-go { opacity: 1; }
|
|
227
|
+
|
|
228
|
+
/* ── 聊天 ── */
|
|
229
|
+
.chat-head {
|
|
230
|
+
display: flex;
|
|
231
|
+
flex-wrap: wrap;
|
|
232
|
+
align-items: center;
|
|
233
|
+
justify-content: space-between;
|
|
234
|
+
gap: 8px 14px;
|
|
235
|
+
padding: 14px 20px;
|
|
236
|
+
border-bottom: 1px solid var(--line);
|
|
237
|
+
background: var(--surface);
|
|
238
|
+
}
|
|
239
|
+
.chat-head-main h1 { margin: 0; font-family: var(--font-serif); font-size: 19px; letter-spacing: .02em; }
|
|
240
|
+
.room-meta { margin: 2px 0 0; font-size: 12px; color: var(--ink-3); }
|
|
241
|
+
.mono { font-family: var(--font-mono); }
|
|
242
|
+
.copy-id {
|
|
243
|
+
appearance: none;
|
|
244
|
+
font: inherit;
|
|
245
|
+
font-size: 12px;
|
|
246
|
+
color: var(--ink-3);
|
|
247
|
+
background: none;
|
|
248
|
+
border: none;
|
|
249
|
+
border-bottom: 1px dashed var(--ink-3);
|
|
250
|
+
padding: 0 0 1px;
|
|
251
|
+
cursor: pointer;
|
|
252
|
+
transition: color .12s, border-color .12s;
|
|
253
|
+
}
|
|
254
|
+
.copy-id:hover { color: var(--accent); border-color: var(--accent); }
|
|
255
|
+
.copy-id .copy-ok { display: none; }
|
|
256
|
+
.copy-id.copied .mono { display: none; }
|
|
257
|
+
.copy-id.copied .copy-ok { display: inline; color: var(--accent); }
|
|
258
|
+
|
|
259
|
+
.chat-head-actions { display: flex; align-items: center; gap: 8px; }
|
|
260
|
+
.chip {
|
|
261
|
+
appearance: none;
|
|
262
|
+
font: inherit;
|
|
263
|
+
font-size: 13px;
|
|
264
|
+
font-weight: 600;
|
|
265
|
+
color: var(--ink-2);
|
|
266
|
+
background: var(--surface);
|
|
267
|
+
border: 1px solid var(--line);
|
|
268
|
+
border-radius: 999px;
|
|
269
|
+
padding: 7px 13px;
|
|
270
|
+
cursor: pointer;
|
|
271
|
+
transition: color .12s, border-color .12s, background .12s;
|
|
272
|
+
}
|
|
273
|
+
.chip:hover { color: var(--ink); border-color: var(--ink-3); }
|
|
274
|
+
.chip.danger { color: var(--danger); }
|
|
275
|
+
.chip.danger:hover { border-color: var(--danger); background: var(--danger-soft); }
|
|
276
|
+
|
|
277
|
+
.anchor { position: relative; }
|
|
278
|
+
.panel {
|
|
279
|
+
position: absolute;
|
|
280
|
+
top: calc(100% + 8px);
|
|
281
|
+
right: 0;
|
|
282
|
+
z-index: 20;
|
|
283
|
+
width: min(320px, calc(100vw - 32px));
|
|
284
|
+
padding: 16px;
|
|
285
|
+
background: var(--surface);
|
|
286
|
+
border: 1px solid var(--line);
|
|
287
|
+
border-radius: 14px;
|
|
288
|
+
box-shadow: var(--shadow);
|
|
289
|
+
animation: rise .18s ease both;
|
|
290
|
+
}
|
|
291
|
+
.panel h3 { margin: 0 0 10px; font-size: 14px; font-weight: 600; }
|
|
292
|
+
.panel .btn { margin-top: 10px; }
|
|
293
|
+
#memberList { list-style: none; margin: 0; padding: 0; max-height: 260px; overflow-y: auto; }
|
|
294
|
+
#memberList li {
|
|
295
|
+
display: flex;
|
|
296
|
+
justify-content: space-between;
|
|
297
|
+
align-items: baseline;
|
|
298
|
+
gap: 10px;
|
|
299
|
+
padding: 7px 0;
|
|
300
|
+
border-bottom: 1px solid var(--line);
|
|
301
|
+
font-size: 14px;
|
|
302
|
+
}
|
|
303
|
+
#memberList li:last-child { border-bottom: none; }
|
|
304
|
+
.m-time { font-size: 11.5px; color: var(--ink-3); }
|
|
305
|
+
|
|
306
|
+
.conn-bar {
|
|
307
|
+
display: flex;
|
|
308
|
+
align-items: center;
|
|
309
|
+
justify-content: center;
|
|
310
|
+
gap: 8px;
|
|
311
|
+
padding: 6px;
|
|
312
|
+
font-size: 12.5px;
|
|
313
|
+
color: var(--ink-2);
|
|
314
|
+
background: var(--surface-2);
|
|
315
|
+
border-bottom: 1px solid var(--line);
|
|
316
|
+
}
|
|
317
|
+
.pulse { width: 7px; height: 7px; border-radius: 50%; background: var(--accent); animation: pulse 1.1s ease-in-out infinite; }
|
|
318
|
+
|
|
319
|
+
.msg-scroll {
|
|
320
|
+
flex: 1;
|
|
321
|
+
min-height: 0;
|
|
322
|
+
position: relative;
|
|
323
|
+
display: flex;
|
|
324
|
+
flex-direction: column;
|
|
325
|
+
}
|
|
326
|
+
.thread {
|
|
327
|
+
flex: 1;
|
|
328
|
+
min-height: 0;
|
|
329
|
+
overflow-y: auto;
|
|
330
|
+
overscroll-behavior: contain;
|
|
331
|
+
padding: 22px 22px 6px;
|
|
332
|
+
}
|
|
333
|
+
#msgList { list-style: none; margin: 0; padding: 0; }
|
|
334
|
+
|
|
335
|
+
.thread, #roomList, #memberList {
|
|
336
|
+
scrollbar-width: thin;
|
|
337
|
+
scrollbar-color: color-mix(in srgb, var(--ink-3) 40%, transparent) transparent;
|
|
338
|
+
}
|
|
339
|
+
.thread::-webkit-scrollbar, #roomList::-webkit-scrollbar, #memberList::-webkit-scrollbar { width: 8px; }
|
|
340
|
+
.thread::-webkit-scrollbar-thumb, #roomList::-webkit-scrollbar-thumb, #memberList::-webkit-scrollbar-thumb {
|
|
341
|
+
background: color-mix(in srgb, var(--ink-3) 40%, transparent);
|
|
342
|
+
border-radius: 4px;
|
|
343
|
+
}
|
|
344
|
+
.thread::-webkit-scrollbar-track { background: transparent; }
|
|
345
|
+
|
|
346
|
+
.jump {
|
|
347
|
+
position: absolute;
|
|
348
|
+
right: 20px;
|
|
349
|
+
bottom: 16px;
|
|
350
|
+
z-index: 5;
|
|
351
|
+
display: flex;
|
|
352
|
+
align-items: center;
|
|
353
|
+
gap: 6px;
|
|
354
|
+
padding: 9px 14px;
|
|
355
|
+
font: inherit;
|
|
356
|
+
font-size: 13px;
|
|
357
|
+
font-weight: 600;
|
|
358
|
+
color: var(--accent-ink);
|
|
359
|
+
background: var(--accent);
|
|
360
|
+
border: none;
|
|
361
|
+
border-radius: 999px;
|
|
362
|
+
cursor: pointer;
|
|
363
|
+
box-shadow: var(--shadow);
|
|
364
|
+
transition: filter .12s, transform .1s;
|
|
365
|
+
}
|
|
366
|
+
.jump:hover { filter: brightness(1.07); }
|
|
367
|
+
.jump:active { transform: translateY(1px); }
|
|
368
|
+
.jump .badge {
|
|
369
|
+
display: inline-flex;
|
|
370
|
+
align-items: center;
|
|
371
|
+
justify-content: center;
|
|
372
|
+
min-width: 18px;
|
|
373
|
+
height: 18px;
|
|
374
|
+
padding: 0 5px;
|
|
375
|
+
font-size: 11px;
|
|
376
|
+
font-weight: 700;
|
|
377
|
+
color: var(--accent);
|
|
378
|
+
background: var(--accent-ink);
|
|
379
|
+
border-radius: 999px;
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
.msg { display: flex; flex-direction: column; margin-bottom: 14px; }
|
|
383
|
+
.msg.in, .sys.in, .day-sep.in { animation: msg-in .25s ease both; }
|
|
384
|
+
.msg.own { align-items: flex-end; }
|
|
385
|
+
.msg.repeat { margin-bottom: 8px; }
|
|
386
|
+
.msg-meta { display: flex; align-items: baseline; gap: 8px; margin-bottom: 3px; }
|
|
387
|
+
.msg-name { font-size: 13px; font-weight: 600; color: var(--ink); }
|
|
388
|
+
.msg.own .msg-name { color: var(--accent); }
|
|
389
|
+
.msg.repeat .msg-name { display: none; }
|
|
390
|
+
.msg-time { font-size: 11.5px; color: var(--ink-3); }
|
|
391
|
+
.msg-text {
|
|
392
|
+
display: inline-block;
|
|
393
|
+
max-width: min(86%, 540px);
|
|
394
|
+
padding: 8px 12px;
|
|
395
|
+
border-radius: 14px;
|
|
396
|
+
background: var(--surface-2);
|
|
397
|
+
border-bottom-left-radius: 4px;
|
|
398
|
+
font-size: 15px;
|
|
399
|
+
line-height: 1.65;
|
|
400
|
+
white-space: pre-line; /* Markdown 依赖换行:保留 \n,折叠连续空格 */
|
|
401
|
+
overflow-wrap: anywhere;
|
|
402
|
+
}
|
|
403
|
+
.msg.own .msg-text { background: var(--accent-soft); border-bottom-left-radius: 14px; border-bottom-right-radius: 4px; }
|
|
404
|
+
|
|
405
|
+
/* ── 消息内 Markdown 元素 ── */
|
|
406
|
+
.msg-text p, .msg-text pre, .msg-text blockquote, .msg-text ul, .msg-text ol { margin: 0; }
|
|
407
|
+
.msg-text > :not(:first-child) { margin-top: 8px; }
|
|
408
|
+
.msg-text ul, .msg-text ol { padding-left: 22px; }
|
|
409
|
+
.msg-text li { margin: 2px 0; }
|
|
410
|
+
.msg-text blockquote {
|
|
411
|
+
padding: 1px 0 1px 12px;
|
|
412
|
+
border-left: 3px solid var(--ink-3);
|
|
413
|
+
color: var(--ink-2);
|
|
414
|
+
}
|
|
415
|
+
.msg-text code {
|
|
416
|
+
font-family: var(--font-mono);
|
|
417
|
+
font-size: .9em;
|
|
418
|
+
background: color-mix(in srgb, var(--ink) 8%, transparent);
|
|
419
|
+
border: 1px solid color-mix(in srgb, var(--ink) 14%, transparent);
|
|
420
|
+
border-radius: 5px;
|
|
421
|
+
padding: 0 4px;
|
|
422
|
+
}
|
|
423
|
+
.msg-text pre {
|
|
424
|
+
overflow-x: auto; /* 长代码行横向滚动,不折行 */
|
|
425
|
+
padding: 10px 12px;
|
|
426
|
+
border: 1px solid var(--line);
|
|
427
|
+
border-radius: 8px;
|
|
428
|
+
background: color-mix(in srgb, var(--ink) 5%, transparent);
|
|
429
|
+
font-size: 13.5px;
|
|
430
|
+
line-height: 1.55;
|
|
431
|
+
}
|
|
432
|
+
.msg-text pre code {
|
|
433
|
+
display: block;
|
|
434
|
+
background: none;
|
|
435
|
+
border: none;
|
|
436
|
+
padding: 0;
|
|
437
|
+
font-size: inherit;
|
|
438
|
+
white-space: pre;
|
|
439
|
+
}
|
|
440
|
+
.msg-text a {
|
|
441
|
+
color: var(--mention-blue);
|
|
442
|
+
text-decoration: underline;
|
|
443
|
+
text-underline-offset: 2px;
|
|
444
|
+
}
|
|
445
|
+
.msg-text a:hover { text-decoration: none; }
|
|
446
|
+
|
|
447
|
+
/* ── @提及 ── */
|
|
448
|
+
/* 默认:灰调(成员已离开 / 改名 / 从未加入);.member:蓝色高亮(当前房间成员) */
|
|
449
|
+
.mention-chip {
|
|
450
|
+
display: inline-block;
|
|
451
|
+
margin: 0 1px;
|
|
452
|
+
padding: 2px 7px;
|
|
453
|
+
max-width: 100%;
|
|
454
|
+
overflow: hidden;
|
|
455
|
+
text-overflow: ellipsis;
|
|
456
|
+
white-space: nowrap;
|
|
457
|
+
/* 对齐:middle 相对父元素 x-height 中点居中——CJK 与 Latin 混排时
|
|
458
|
+
都比基线微调(-0.15em 对中文仍偏上)更稳;line-height:1 让药丸
|
|
459
|
+
高度只由自身字号+内边距决定,不撑高行盒 */
|
|
460
|
+
vertical-align: middle;
|
|
461
|
+
line-height: 1;
|
|
462
|
+
font-size: .92em;
|
|
463
|
+
font-weight: 600;
|
|
464
|
+
color: var(--ink-2);
|
|
465
|
+
background: color-mix(in srgb, var(--ink-2) 12%, transparent);
|
|
466
|
+
border: 1px solid color-mix(in srgb, var(--ink-2) 26%, transparent);
|
|
467
|
+
border-radius: 999px;
|
|
468
|
+
}
|
|
469
|
+
.mention-chip.member {
|
|
470
|
+
color: var(--mention-blue);
|
|
471
|
+
background: color-mix(in srgb, var(--mention-blue) 14%, transparent);
|
|
472
|
+
border: 1px solid color-mix(in srgb, var(--mention-blue) 32%, transparent);
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
.mention-picker {
|
|
476
|
+
position: absolute;
|
|
477
|
+
left: 16px;
|
|
478
|
+
right: 16px;
|
|
479
|
+
bottom: calc(100% + 8px);
|
|
480
|
+
z-index: 30;
|
|
481
|
+
max-height: 224px;
|
|
482
|
+
overflow-y: auto;
|
|
483
|
+
padding: 6px;
|
|
484
|
+
background: var(--surface);
|
|
485
|
+
border: 1px solid var(--line);
|
|
486
|
+
border-radius: 12px;
|
|
487
|
+
box-shadow: var(--shadow);
|
|
488
|
+
animation: rise .15s ease both;
|
|
489
|
+
}
|
|
490
|
+
.mention-picker ul { list-style: none; margin: 0; padding: 0; position: relative; }
|
|
491
|
+
.mention-opt {
|
|
492
|
+
padding: 7px 10px;
|
|
493
|
+
border-radius: 8px;
|
|
494
|
+
font-size: 14px;
|
|
495
|
+
color: var(--ink);
|
|
496
|
+
cursor: pointer;
|
|
497
|
+
white-space: nowrap;
|
|
498
|
+
overflow: hidden;
|
|
499
|
+
text-overflow: ellipsis;
|
|
500
|
+
}
|
|
501
|
+
.mention-opt:hover { background: var(--surface-2); }
|
|
502
|
+
.mention-opt.active { background: var(--accent-soft); color: var(--accent); }
|
|
503
|
+
.mention-at { font-weight: 600; color: var(--accent); margin-right: 4px; }
|
|
504
|
+
.mention-hit { background: color-mix(in srgb, var(--accent) 25%, transparent); border-radius: 3px; }
|
|
505
|
+
.mention-empty { margin: 0; padding: 12px 8px; font-size: 13px; color: var(--ink-3); text-align: center; }
|
|
506
|
+
.mention-hint {
|
|
507
|
+
margin: 6px 0 2px;
|
|
508
|
+
padding: 7px 8px 0;
|
|
509
|
+
border-top: 1px solid var(--line);
|
|
510
|
+
font-size: 11.5px;
|
|
511
|
+
color: var(--ink-3);
|
|
512
|
+
text-align: center;
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
.sys {
|
|
516
|
+
display: flex;
|
|
517
|
+
align-items: center;
|
|
518
|
+
gap: 12px;
|
|
519
|
+
margin: 20px 0;
|
|
520
|
+
font-size: 12.5px;
|
|
521
|
+
color: var(--ink-2);
|
|
522
|
+
}
|
|
523
|
+
.sys::before, .sys::after { content: ""; flex: 1; height: 1px; background: var(--line); }
|
|
524
|
+
.sys span { flex-shrink: 0; }
|
|
525
|
+
|
|
526
|
+
.day-sep {
|
|
527
|
+
display: flex;
|
|
528
|
+
align-items: center;
|
|
529
|
+
gap: 12px;
|
|
530
|
+
margin: 26px 0 16px;
|
|
531
|
+
font-size: 12px;
|
|
532
|
+
font-weight: 600;
|
|
533
|
+
letter-spacing: .08em;
|
|
534
|
+
color: var(--ink-2);
|
|
535
|
+
}
|
|
536
|
+
.day-sep::before, .day-sep::after { content: ""; flex: 1; height: 1px; background: var(--line); }
|
|
537
|
+
|
|
538
|
+
.gap { padding: 6px 0 2px; text-align: center; font-size: 12px; color: var(--ink-3); }
|
|
539
|
+
|
|
540
|
+
.chat-empty { padding: 56px 0 40px; text-align: center; font-size: 13px; color: var(--ink-3); }
|
|
541
|
+
.chat-empty::before {
|
|
542
|
+
content: "";
|
|
543
|
+
display: block;
|
|
544
|
+
width: 56px;
|
|
545
|
+
height: 56px;
|
|
546
|
+
margin: 0 auto 14px;
|
|
547
|
+
border: 1.5px dashed var(--line);
|
|
548
|
+
border-radius: 50%;
|
|
549
|
+
background: radial-gradient(circle at 50% 50%, var(--ink-3) 0 3px, transparent 4px);
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
.composer {
|
|
553
|
+
position: relative;
|
|
554
|
+
display: flex;
|
|
555
|
+
flex-direction: column;
|
|
556
|
+
gap: 8px;
|
|
557
|
+
padding: 10px 16px calc(10px + env(safe-area-inset-bottom));
|
|
558
|
+
border-top: 1px solid var(--line);
|
|
559
|
+
background: var(--surface);
|
|
560
|
+
}
|
|
561
|
+
.composer-row { display: flex; gap: 10px; align-items: flex-end; }
|
|
562
|
+
.composer .input { flex: 1; }
|
|
563
|
+
.composer .input:disabled { opacity: .5; }
|
|
564
|
+
#msgInput {
|
|
565
|
+
min-height: 46px;
|
|
566
|
+
max-height: 132px;
|
|
567
|
+
padding: 12px 13px;
|
|
568
|
+
line-height: 1.5;
|
|
569
|
+
resize: none;
|
|
570
|
+
overflow-y: auto;
|
|
571
|
+
}
|
|
572
|
+
#btnSend { padding: 12px 22px; }
|
|
573
|
+
.send-error { margin: 0; font-size: 12.5px; color: var(--danger); }
|
|
574
|
+
|
|
575
|
+
@media (max-width: 560px) {
|
|
576
|
+
#joinView { padding: 20px 14px; }
|
|
577
|
+
.join-card { padding: 28px 22px 24px; }
|
|
578
|
+
.chat-head { padding: 12px 16px; }
|
|
579
|
+
.thread { padding: 16px 16px 4px; }
|
|
580
|
+
.jump { right: 12px; bottom: 10px; }
|
|
581
|
+
}
|
|
582
|
+
</style>
|
|
583
|
+
</head>
|
|
584
|
+
<body>
|
|
585
|
+
|
|
586
|
+
<main id="app">
|
|
587
|
+
|
|
588
|
+
<!-- ── 加入 / 创建 ── -->
|
|
589
|
+
<section id="joinView" class="view">
|
|
590
|
+
<div class="join-card">
|
|
591
|
+
<p class="wordmark">闲叙</p>
|
|
592
|
+
<p class="tagline">一个轻量的聊天房间。输入房间 ID 加入,或直接新建一个。</p>
|
|
593
|
+
|
|
594
|
+
<form id="joinForm" novalidate>
|
|
595
|
+
<label class="field">
|
|
596
|
+
<span class="field-label">房间 ID</span>
|
|
597
|
+
<input id="inRoomId" class="input" placeholder="粘贴或输入房间 ID" autocomplete="off" spellcheck="false">
|
|
598
|
+
</label>
|
|
599
|
+
<label class="field">
|
|
600
|
+
<span class="field-label">你的昵称</span>
|
|
601
|
+
<input id="inName" class="input" placeholder="怎么称呼你" autocomplete="nickname" maxlength="24">
|
|
602
|
+
</label>
|
|
603
|
+
<button class="btn primary" type="submit" id="btnJoin">加入房间</button>
|
|
604
|
+
</form>
|
|
605
|
+
|
|
606
|
+
<div class="or"><span>或</span></div>
|
|
607
|
+
|
|
608
|
+
<form id="createForm" novalidate>
|
|
609
|
+
<label class="field">
|
|
610
|
+
<span class="field-label">新房间名称</span>
|
|
611
|
+
<input id="inRoomName" class="input" placeholder="例如:周末电影夜" maxlength="40">
|
|
612
|
+
</label>
|
|
613
|
+
<button class="btn ghost" type="submit" id="btnCreate">创建新房间</button>
|
|
614
|
+
</form>
|
|
615
|
+
|
|
616
|
+
<div id="joinError" class="alert" role="alert" hidden></div>
|
|
617
|
+
|
|
618
|
+
<details id="tokenBox" class="token-box">
|
|
619
|
+
<summary>访问令牌(可选)</summary>
|
|
620
|
+
<div class="token-body">
|
|
621
|
+
<label class="field">
|
|
622
|
+
<span class="field-label">Bearer Token</span>
|
|
623
|
+
<input id="inToken" class="input" type="password" placeholder="服务器要求时填写" autocomplete="off" spellcheck="false">
|
|
624
|
+
</label>
|
|
625
|
+
<button class="btn ghost small" type="button" data-token-save="inToken">保存</button>
|
|
626
|
+
<p class="hint">若服务器设置了访问令牌(401),所有请求都会带上它。</p>
|
|
627
|
+
</div>
|
|
628
|
+
</details>
|
|
629
|
+
|
|
630
|
+
<section class="room-list">
|
|
631
|
+
<h2>已有房间</h2>
|
|
632
|
+
<ul id="roomList"></ul>
|
|
633
|
+
<p id="roomListEmpty" class="hint" hidden>还没有房间,创建一个吧。</p>
|
|
634
|
+
</section>
|
|
635
|
+
</div>
|
|
636
|
+
</section>
|
|
637
|
+
|
|
638
|
+
<!-- ── 聊天 ── -->
|
|
639
|
+
<section id="chatView" class="view" hidden>
|
|
640
|
+
<header class="chat-head">
|
|
641
|
+
<div class="chat-head-main">
|
|
642
|
+
<h1 id="roomName"></h1>
|
|
643
|
+
<p class="room-meta">
|
|
644
|
+
<button type="button" id="btnCopyId" class="copy-id" aria-label="复制房间 ID" title="点击复制完整房间 ID">
|
|
645
|
+
<span class="mono" id="roomIdShort"></span><span class="copy-ok">已复制</span>
|
|
646
|
+
</button>
|
|
647
|
+
</p>
|
|
648
|
+
</div>
|
|
649
|
+
<div class="chat-head-actions">
|
|
650
|
+
<div class="anchor">
|
|
651
|
+
<button class="chip" type="button" id="btnMembers" aria-haspopup="true" aria-expanded="false">成员</button>
|
|
652
|
+
<div class="panel" id="memberPanel" hidden>
|
|
653
|
+
<h3>成员</h3>
|
|
654
|
+
<ul id="memberList"></ul>
|
|
655
|
+
<p id="memberEmpty" class="hint" hidden>暂无成员</p>
|
|
656
|
+
</div>
|
|
657
|
+
</div>
|
|
658
|
+
<div class="anchor">
|
|
659
|
+
<button class="chip" type="button" id="btnSettings" aria-haspopup="true" aria-expanded="false">设置</button>
|
|
660
|
+
<div class="panel" id="chatSettings" hidden>
|
|
661
|
+
<h3>设置</h3>
|
|
662
|
+
<label class="field">
|
|
663
|
+
<span class="field-label">访问令牌(可选)</span>
|
|
664
|
+
<input id="inTokenChat" class="input" type="password" placeholder="服务器要求时填写" autocomplete="off" spellcheck="false">
|
|
665
|
+
</label>
|
|
666
|
+
<button class="btn ghost small" type="button" data-token-save="inTokenChat">保存</button>
|
|
667
|
+
<p id="tokenHint" class="hint" hidden>服务器要求访问令牌(401)。</p>
|
|
668
|
+
</div>
|
|
669
|
+
</div>
|
|
670
|
+
<button class="chip danger" type="button" id="btnLeave">离开</button>
|
|
671
|
+
</div>
|
|
672
|
+
</header>
|
|
673
|
+
|
|
674
|
+
<div id="connBar" class="conn-bar" hidden aria-live="polite">
|
|
675
|
+
<span class="pulse"></span>
|
|
676
|
+
<span>连接中断,正在重试…</span>
|
|
677
|
+
</div>
|
|
678
|
+
|
|
679
|
+
<div id="msgScroll" class="msg-scroll">
|
|
680
|
+
<div id="thread" class="thread">
|
|
681
|
+
<ul id="msgList" aria-label="消息列表"></ul>
|
|
682
|
+
<p id="msgEmpty" class="chat-empty">还没有消息,说点什么吧。</p>
|
|
683
|
+
</div>
|
|
684
|
+
<button type="button" id="btnJump" class="jump" aria-label="回到最新消息" hidden>
|
|
685
|
+
<span aria-hidden="true">↓</span><span class="badge" id="jumpBadge" hidden></span>
|
|
686
|
+
</button>
|
|
687
|
+
</div>
|
|
688
|
+
|
|
689
|
+
<footer class="composer">
|
|
690
|
+
<p id="sendError" class="send-error" role="alert" hidden></p>
|
|
691
|
+
<div id="mentionPicker" class="mention-picker" role="listbox" aria-label="选择要提及的成员" hidden></div>
|
|
692
|
+
<div class="composer-row">
|
|
693
|
+
<textarea id="msgInput" class="input" rows="1" placeholder="输入消息,Enter 发送,Shift+Enter 换行" autocomplete="off" aria-autocomplete="list" aria-controls="mentionPicker" aria-expanded="false" disabled></textarea>
|
|
694
|
+
<button class="btn primary" type="button" id="btnSend" disabled>发送</button>
|
|
695
|
+
</div>
|
|
696
|
+
</footer>
|
|
697
|
+
</section>
|
|
698
|
+
|
|
699
|
+
</main>
|
|
700
|
+
|
|
701
|
+
<script>
|
|
702
|
+
(() => {
|
|
703
|
+
"use strict";
|
|
704
|
+
const $ = (s) => document.querySelector(s);
|
|
705
|
+
|
|
706
|
+
/* ---------- 本地存储(隐私模式下 localStorage 可能抛错,回退到内存) ---------- */
|
|
707
|
+
const memStore = {};
|
|
708
|
+
function lsGet(k) {
|
|
709
|
+
try { return localStorage.getItem(k); }
|
|
710
|
+
catch { return k in memStore ? memStore[k] : null; }
|
|
711
|
+
}
|
|
712
|
+
function lsSet(k, v) {
|
|
713
|
+
try { if (v == null) localStorage.removeItem(k); else localStorage.setItem(k, v); }
|
|
714
|
+
catch { if (v == null) delete memStore[k]; else memStore[k] = v; }
|
|
715
|
+
}
|
|
716
|
+
const LS = {
|
|
717
|
+
get sessionID() {
|
|
718
|
+
// ponytail: sessionID 必须 tab 级独立(sessionStorage)——localStorage
|
|
719
|
+
// 会被同一浏览器的多个 tab 共享,一个 tab 的 /read 推进会吃掉另一个
|
|
720
|
+
// tab 未读窗口的消息(orc-1 #1)
|
|
721
|
+
let v = null;
|
|
722
|
+
try { v = sessionStorage.getItem("chat.sessionID"); } catch { /* ignore */ }
|
|
723
|
+
if (!v) {
|
|
724
|
+
v = crypto.randomUUID();
|
|
725
|
+
try { sessionStorage.setItem("chat.sessionID", v); } catch { /* ignore */ }
|
|
726
|
+
}
|
|
727
|
+
return v;
|
|
728
|
+
},
|
|
729
|
+
get roomId() { return lsGet("chat.roomId") || ""; },
|
|
730
|
+
set roomId(v) { lsSet("chat.roomId", v || null); },
|
|
731
|
+
get name() { return lsGet("chat.name") || ""; },
|
|
732
|
+
set name(v) { lsSet("chat.name", v || null); },
|
|
733
|
+
get token() { return lsGet("chat.token") || ""; },
|
|
734
|
+
set token(v) { lsSet("chat.token", v || null); },
|
|
735
|
+
};
|
|
736
|
+
|
|
737
|
+
/* ---------- 请求 ---------- */
|
|
738
|
+
async function api(path, opts = {}) {
|
|
739
|
+
const headers = {};
|
|
740
|
+
if (opts.body) headers["Content-Type"] = "application/json";
|
|
741
|
+
if (LS.token) headers.Authorization = `Bearer ${LS.token}`;
|
|
742
|
+
let res;
|
|
743
|
+
try {
|
|
744
|
+
res = await fetch(path, { ...opts, headers });
|
|
745
|
+
} catch {
|
|
746
|
+
const e = new Error("网络连接失败");
|
|
747
|
+
e.network = true;
|
|
748
|
+
throw e;
|
|
749
|
+
}
|
|
750
|
+
const data = await res.json().catch(() => ({}));
|
|
751
|
+
if (!res.ok) {
|
|
752
|
+
const e = new Error(data.error || `请求失败(HTTP ${res.status})`);
|
|
753
|
+
e.status = res.status;
|
|
754
|
+
throw e;
|
|
755
|
+
}
|
|
756
|
+
return data;
|
|
757
|
+
}
|
|
758
|
+
|
|
759
|
+
/* ---------- 状态 ---------- */
|
|
760
|
+
const state = { room: null, myName: "", rendered: 0, firstMsgId: null, polling: false, unread: 0, copyT: 0 };
|
|
761
|
+
// orc-2 #3——渲染失败的消息 id;对账时据此强制重建重试
|
|
762
|
+
const failedRenderIds = new Set();
|
|
763
|
+
let lastFocus = null;
|
|
764
|
+
let pollDelay = 2000;
|
|
765
|
+
|
|
766
|
+
/* ---------- 工具 ---------- */
|
|
767
|
+
function fmtTime(ts) {
|
|
768
|
+
const d = new Date(ts);
|
|
769
|
+
const hm = d.toLocaleTimeString("zh-CN", { hour: "2-digit", minute: "2-digit" });
|
|
770
|
+
const sameDay = d.toDateString() === new Date().toDateString();
|
|
771
|
+
return sameDay ? `今天 ${hm}` : `${d.getMonth() + 1}月${d.getDate()}日 ${hm}`;
|
|
772
|
+
}
|
|
773
|
+
|
|
774
|
+
function timeHM(ts) {
|
|
775
|
+
return new Date(ts).toLocaleTimeString("zh-CN", { hour: "2-digit", minute: "2-digit" });
|
|
776
|
+
}
|
|
777
|
+
|
|
778
|
+
function dayText(ts) {
|
|
779
|
+
const d = new Date(ts);
|
|
780
|
+
const now = new Date();
|
|
781
|
+
const today = new Date(now.getFullYear(), now.getMonth(), now.getDate());
|
|
782
|
+
const that = new Date(d.getFullYear(), d.getMonth(), d.getDate());
|
|
783
|
+
const diff = Math.round((today - that) / 86400000);
|
|
784
|
+
if (diff === 0) return "今天";
|
|
785
|
+
if (diff === 1) return "昨天";
|
|
786
|
+
return `${d.getMonth() + 1}月${d.getDate()}日`;
|
|
787
|
+
}
|
|
788
|
+
|
|
789
|
+
function fmtSys(text) {
|
|
790
|
+
return String(text)
|
|
791
|
+
.replace(/^(.*?)\s+joined the room$/, "$1 加入了房间")
|
|
792
|
+
.replace(/^(.*?)\s+left the room$/, "$1 离开了房间");
|
|
793
|
+
}
|
|
794
|
+
|
|
795
|
+
function flash(btn, msg) {
|
|
796
|
+
const old = btn.textContent;
|
|
797
|
+
btn.textContent = msg;
|
|
798
|
+
setTimeout(() => { btn.textContent = old; }, 1200);
|
|
799
|
+
}
|
|
800
|
+
|
|
801
|
+
/* ---------- 发送框 ---------- */
|
|
802
|
+
function autoGrow(el) {
|
|
803
|
+
el.style.height = "auto";
|
|
804
|
+
el.style.height = Math.min(el.scrollHeight, 132) + "px";
|
|
805
|
+
}
|
|
806
|
+
function updateSendState() {
|
|
807
|
+
$("#btnSend").disabled = !$("#msgInput").value.trim();
|
|
808
|
+
}
|
|
809
|
+
|
|
810
|
+
/* ---------- 回到底部与未读 ---------- */
|
|
811
|
+
function jumpToBottom() {
|
|
812
|
+
state.unread = 0;
|
|
813
|
+
$("#thread").scrollTop = $("#thread").scrollHeight;
|
|
814
|
+
updateBadge();
|
|
815
|
+
updateJump();
|
|
816
|
+
}
|
|
817
|
+
function updateJump() {
|
|
818
|
+
const sc = $("#thread");
|
|
819
|
+
const away = sc.scrollHeight - sc.scrollTop - sc.clientHeight > 160;
|
|
820
|
+
$("#btnJump").hidden = !(away || state.unread > 0);
|
|
821
|
+
}
|
|
822
|
+
function updateBadge() {
|
|
823
|
+
const b = $("#jumpBadge");
|
|
824
|
+
b.textContent = state.unread > 99 ? "99+" : state.unread;
|
|
825
|
+
b.hidden = state.unread === 0;
|
|
826
|
+
updateTitle();
|
|
827
|
+
}
|
|
828
|
+
function updateTitle() {
|
|
829
|
+
const base = state.room ? `${state.room.name} · 闲叙` : "闲叙 · 轻量聊天室";
|
|
830
|
+
document.title = state.unread > 0 ? `(${state.unread}) ${base}` : base;
|
|
831
|
+
}
|
|
832
|
+
|
|
833
|
+
/* ---------- 长会话截断 ---------- */
|
|
834
|
+
function trimList() {
|
|
835
|
+
const list = $("#msgList");
|
|
836
|
+
const MAX = 400, KEEP = 300;
|
|
837
|
+
if (list.children.length <= MAX) return;
|
|
838
|
+
while (list.children.length > KEEP) list.removeChild(list.firstChild);
|
|
839
|
+
if (list.firstElementChild && !list.firstElementChild.classList.contains("gap")) {
|
|
840
|
+
const gap = document.createElement("li");
|
|
841
|
+
gap.className = "gap";
|
|
842
|
+
gap.textContent = "更早的消息已省略";
|
|
843
|
+
list.insertBefore(gap, list.firstChild);
|
|
844
|
+
}
|
|
845
|
+
}
|
|
846
|
+
|
|
847
|
+
/* ---------- 复制房间 ID ---------- */
|
|
848
|
+
function copyRoomId() {
|
|
849
|
+
const done = () => {
|
|
850
|
+
const b = $("#btnCopyId");
|
|
851
|
+
b.classList.add("copied");
|
|
852
|
+
clearTimeout(state.copyT);
|
|
853
|
+
state.copyT = setTimeout(() => b.classList.remove("copied"), 1400);
|
|
854
|
+
};
|
|
855
|
+
const id = state.room.id;
|
|
856
|
+
if (navigator.clipboard && navigator.clipboard.writeText) {
|
|
857
|
+
navigator.clipboard.writeText(id).then(done).catch(() => fallbackCopy(id, done));
|
|
858
|
+
} else fallbackCopy(id, done);
|
|
859
|
+
}
|
|
860
|
+
function fallbackCopy(text, done) {
|
|
861
|
+
const ta = document.createElement("textarea");
|
|
862
|
+
ta.value = text;
|
|
863
|
+
ta.style.position = "fixed";
|
|
864
|
+
ta.style.opacity = "0";
|
|
865
|
+
document.body.appendChild(ta);
|
|
866
|
+
ta.select();
|
|
867
|
+
try { document.execCommand("copy"); done(); } catch { /* 忽略 */ }
|
|
868
|
+
ta.remove();
|
|
869
|
+
}
|
|
870
|
+
|
|
871
|
+
/* ---------- 视图切换 ---------- */
|
|
872
|
+
function showJoin() {
|
|
873
|
+
$("#chatView").hidden = true;
|
|
874
|
+
$("#joinView").hidden = false;
|
|
875
|
+
updateTitle();
|
|
876
|
+
refreshRoomList();
|
|
877
|
+
$("#inRoomId").focus();
|
|
878
|
+
}
|
|
879
|
+
function showChat() {
|
|
880
|
+
$("#joinView").hidden = true;
|
|
881
|
+
$("#chatView").hidden = false;
|
|
882
|
+
}
|
|
883
|
+
|
|
884
|
+
/* ---------- 加入 / 创建 ---------- */
|
|
885
|
+
function showJoinError(msg) {
|
|
886
|
+
const el = $("#joinError");
|
|
887
|
+
el.textContent = msg;
|
|
888
|
+
el.hidden = false;
|
|
889
|
+
}
|
|
890
|
+
function hideJoinError() { $("#joinError").hidden = true; }
|
|
891
|
+
function setJoinBusy(b) {
|
|
892
|
+
$("#btnJoin").disabled = b;
|
|
893
|
+
$("#btnCreate").disabled = b;
|
|
894
|
+
}
|
|
895
|
+
|
|
896
|
+
function joinRoom() {
|
|
897
|
+
const roomId = $("#inRoomId").value.trim();
|
|
898
|
+
const name = $("#inName").value.trim();
|
|
899
|
+
if (!roomId) return showJoinError("请输入房间 ID");
|
|
900
|
+
if (!name) return showJoinError("请输入你的昵称");
|
|
901
|
+
doJoin(roomId, name);
|
|
902
|
+
}
|
|
903
|
+
|
|
904
|
+
async function doJoin(roomId, name) {
|
|
905
|
+
setJoinBusy(true);
|
|
906
|
+
hideJoinError();
|
|
907
|
+
try {
|
|
908
|
+
const room = await api(`/rooms/${encodeURIComponent(roomId)}/join`, {
|
|
909
|
+
method: "POST",
|
|
910
|
+
body: JSON.stringify({ sessionID: LS.sessionID, participantId: name, participantName: name }),
|
|
911
|
+
});
|
|
912
|
+
enterRoom(room, name);
|
|
913
|
+
} catch (e) {
|
|
914
|
+
if (e.status === 401) { openTokenBox(); showJoinError("服务器要求访问令牌,请填写后重试"); }
|
|
915
|
+
else if (e.network) showJoinError("无法连接服务器,请检查网络后重试");
|
|
916
|
+
else showJoinError(`加入失败:${e.message}`);
|
|
917
|
+
} finally {
|
|
918
|
+
setJoinBusy(false);
|
|
919
|
+
}
|
|
920
|
+
}
|
|
921
|
+
|
|
922
|
+
async function createRoom() {
|
|
923
|
+
const roomName = $("#inRoomName").value.trim();
|
|
924
|
+
const displayName = $("#inName").value.trim() || "访客";
|
|
925
|
+
if (!roomName) return showJoinError("请输入房间名称");
|
|
926
|
+
setJoinBusy(true);
|
|
927
|
+
hideJoinError();
|
|
928
|
+
try {
|
|
929
|
+
const room = await api("/rooms", {
|
|
930
|
+
method: "POST",
|
|
931
|
+
body: JSON.stringify({ name: roomName, ownerId: displayName, sessionID: LS.sessionID }),
|
|
932
|
+
});
|
|
933
|
+
enterRoom(room, displayName);
|
|
934
|
+
} catch (e) {
|
|
935
|
+
if (e.status === 401) { openTokenBox(); showJoinError("服务器要求访问令牌,请填写后重试"); }
|
|
936
|
+
else if (e.network) showJoinError("无法连接服务器,请检查网络后重试");
|
|
937
|
+
else showJoinError(`创建失败:${e.message}`);
|
|
938
|
+
} finally {
|
|
939
|
+
setJoinBusy(false);
|
|
940
|
+
}
|
|
941
|
+
}
|
|
942
|
+
|
|
943
|
+
function enterRoom(room, myName) {
|
|
944
|
+
state.room = room;
|
|
945
|
+
state.myName = myName;
|
|
946
|
+
state.rendered = 0;
|
|
947
|
+
state.firstMsgId = null;
|
|
948
|
+
state.unread = 0;
|
|
949
|
+
LS.roomId = room.id;
|
|
950
|
+
LS.name = myName;
|
|
951
|
+
showChat();
|
|
952
|
+
renderHead();
|
|
953
|
+
appendMessages(false);
|
|
954
|
+
renderMembers();
|
|
955
|
+
setOnline(true);
|
|
956
|
+
hideSendError();
|
|
957
|
+
startPolling();
|
|
958
|
+
$("#msgInput").disabled = false;
|
|
959
|
+
updateSendState();
|
|
960
|
+
updateJump();
|
|
961
|
+
$("#msgInput").focus();
|
|
962
|
+
}
|
|
963
|
+
|
|
964
|
+
function renderHead() {
|
|
965
|
+
$("#roomName").textContent = state.room.name;
|
|
966
|
+
$("#roomIdShort").textContent = state.room.id.slice(0, 8);
|
|
967
|
+
$("#btnCopyId").title = `复制完整房间 ID:${state.room.id}`;
|
|
968
|
+
updateTitle();
|
|
969
|
+
}
|
|
970
|
+
|
|
971
|
+
/* ---------- 消息 ---------- */
|
|
972
|
+
function renderMessage(m, live) {
|
|
973
|
+
const li = document.createElement("li");
|
|
974
|
+
li.dataset.sender = m.senderId;
|
|
975
|
+
li.dataset.ts = m.createdAt;
|
|
976
|
+
li.dataset.msgId = m.id;
|
|
977
|
+
if (m.senderId === "system") {
|
|
978
|
+
li.className = "sys";
|
|
979
|
+
const span = document.createElement("span");
|
|
980
|
+
span.textContent = fmtSys(m.text);
|
|
981
|
+
li.appendChild(span);
|
|
982
|
+
} else {
|
|
983
|
+
const own = m.senderId === state.myName;
|
|
984
|
+
li.className = "msg" + (own ? " own" : "");
|
|
985
|
+
const meta = document.createElement("div");
|
|
986
|
+
meta.className = "msg-meta";
|
|
987
|
+
const nm = document.createElement("span");
|
|
988
|
+
nm.className = "msg-name";
|
|
989
|
+
nm.textContent = own ? "我" : m.senderId;
|
|
990
|
+
const tm = document.createElement("span");
|
|
991
|
+
tm.className = "msg-time";
|
|
992
|
+
tm.textContent = timeHM(m.createdAt);
|
|
993
|
+
meta.append(nm, tm);
|
|
994
|
+
const tx = document.createElement("div");
|
|
995
|
+
tx.className = "msg-text";
|
|
996
|
+
renderTextWithMentions(tx, m.text);
|
|
997
|
+
li.append(meta, tx);
|
|
998
|
+
}
|
|
999
|
+
if (live) li.classList.add("in");
|
|
1000
|
+
return li;
|
|
1001
|
+
}
|
|
1002
|
+
|
|
1003
|
+
function appendMessages(live) {
|
|
1004
|
+
const scroll = $("#thread");
|
|
1005
|
+
const list = $("#msgList");
|
|
1006
|
+
const msgs = state.room.messages;
|
|
1007
|
+
// ponytail: the server trims history to 500 — when the head changes,
|
|
1008
|
+
// index-based tracking would stall forever (length stays pinned at 500),
|
|
1009
|
+
// so rebuild the list once from scratch
|
|
1010
|
+
let rebuild = false;
|
|
1011
|
+
let anchorId = null;
|
|
1012
|
+
if (msgs.length && msgs[0].id !== state.firstMsgId) {
|
|
1013
|
+
// remember the first visible message to restore the scroll position
|
|
1014
|
+
const firstVisible = Array.from(list.children).find((el) =>
|
|
1015
|
+
el.classList.contains("msg"),
|
|
1016
|
+
);
|
|
1017
|
+
if (firstVisible) anchorId = firstVisible.dataset.msgId || null;
|
|
1018
|
+
list.textContent = "";
|
|
1019
|
+
state.rendered = 0;
|
|
1020
|
+
rebuild = true;
|
|
1021
|
+
}
|
|
1022
|
+
if (rebuild) live = false;
|
|
1023
|
+
const nearBottom = scroll.scrollHeight - scroll.scrollTop - scroll.clientHeight < 80;
|
|
1024
|
+
let appended = 0;
|
|
1025
|
+
let skipped = 0; // orc-3 #1——渲染失败计数(此前未声明 → ReferenceError)
|
|
1026
|
+
let last = list.lastElementChild;
|
|
1027
|
+
// ponytail: 防御性去重——发送回显(send.then 全量替换+渲染)与长轮询
|
|
1028
|
+
// echo(inbox excludeSelf=0)可能把同一条消息带进渲染区间,按 id
|
|
1029
|
+
// 跳过已渲染节点,确保任何并发路径下都不产生重复消息节点
|
|
1030
|
+
const renderedIds = new Set(
|
|
1031
|
+
Array.from(list.children)
|
|
1032
|
+
.map((el) => el.dataset.msgId)
|
|
1033
|
+
.filter(Boolean),
|
|
1034
|
+
);
|
|
1035
|
+
for (let i = state.rendered; i < msgs.length; i++) {
|
|
1036
|
+
const m = msgs[i];
|
|
1037
|
+
if (renderedIds.has(m.id)) continue;
|
|
1038
|
+
// ponytail: 单条消息渲染容错——一条坏消息(渲染抛异常)绝不能卡死
|
|
1039
|
+
// 整批渲染(否则后续消息全部不可见 = "Agent 消息丢失");跳过它并
|
|
1040
|
+
// 继续渲染其余,统计 skipped 供调用方决定水位推进
|
|
1041
|
+
try {
|
|
1042
|
+
const day = new Date(m.createdAt).toDateString();
|
|
1043
|
+
if (!last || last.dataset.day !== day) {
|
|
1044
|
+
const sep = document.createElement("li");
|
|
1045
|
+
sep.className = "day-sep";
|
|
1046
|
+
sep.dataset.day = day;
|
|
1047
|
+
sep.textContent = dayText(m.createdAt);
|
|
1048
|
+
list.appendChild(sep);
|
|
1049
|
+
last = sep;
|
|
1050
|
+
}
|
|
1051
|
+
const li = renderMessage(m, live);
|
|
1052
|
+
li.dataset.day = day;
|
|
1053
|
+
// 同一人 8 分钟内连发:省略重复名字,间距收紧
|
|
1054
|
+
if (last.classList.contains("msg") && last.dataset.sender === m.senderId
|
|
1055
|
+
&& m.createdAt - +last.dataset.ts < 8 * 60 * 1000) {
|
|
1056
|
+
li.classList.add("repeat");
|
|
1057
|
+
}
|
|
1058
|
+
list.appendChild(li);
|
|
1059
|
+
last = li;
|
|
1060
|
+
} catch (err) {
|
|
1061
|
+
console.error("[chat] 单条消息渲染失败,跳过:", m.id, err);
|
|
1062
|
+
skipped++;
|
|
1063
|
+
failedRenderIds.add(m.id); // orc-2 #3——对账时据此强制重建重试
|
|
1064
|
+
}
|
|
1065
|
+
renderedIds.add(m.id);
|
|
1066
|
+
appended++;
|
|
1067
|
+
}
|
|
1068
|
+
state.rendered = msgs.length;
|
|
1069
|
+
state.firstMsgId = msgs.length ? msgs[0].id : state.firstMsgId;
|
|
1070
|
+
$("#msgEmpty").hidden = msgs.length > 0;
|
|
1071
|
+
// 初次进入直接到底;增量到达时只有本来就在底部才跟随;重建时恢复锚点
|
|
1072
|
+
if (rebuild && anchorId) {
|
|
1073
|
+
const anchor = list.querySelector(`[data-msg-id="${CSS.escape(anchorId)}"]`);
|
|
1074
|
+
if (anchor) {
|
|
1075
|
+
const top = anchor.getBoundingClientRect().top - scroll.getBoundingClientRect().top;
|
|
1076
|
+
scroll.scrollTop += top - 40;
|
|
1077
|
+
} else {
|
|
1078
|
+
scroll.scrollTop = scroll.scrollHeight;
|
|
1079
|
+
}
|
|
1080
|
+
} else if (msgs.length && (live ? nearBottom : true)) {
|
|
1081
|
+
scroll.scrollTop = scroll.scrollHeight;
|
|
1082
|
+
}
|
|
1083
|
+
// 未读计数已统一在 poll 层处理(rebuild 场景 live=false 也能计数)
|
|
1084
|
+
updateJump();
|
|
1085
|
+
trimList();
|
|
1086
|
+
return { skipped };
|
|
1087
|
+
}
|
|
1088
|
+
|
|
1089
|
+
function renderMembers() {
|
|
1090
|
+
const parts = state.room.participants || [];
|
|
1091
|
+
$("#btnMembers").textContent = `成员 ${parts.length}`;
|
|
1092
|
+
const list = $("#memberList");
|
|
1093
|
+
list.textContent = "";
|
|
1094
|
+
for (const p of parts) {
|
|
1095
|
+
const li = document.createElement("li");
|
|
1096
|
+
const nm = document.createElement("span");
|
|
1097
|
+
nm.textContent = p.name;
|
|
1098
|
+
const tm = document.createElement("span");
|
|
1099
|
+
tm.className = "m-time";
|
|
1100
|
+
tm.textContent = fmtTime(p.joinedAt);
|
|
1101
|
+
li.append(nm, tm);
|
|
1102
|
+
list.appendChild(li);
|
|
1103
|
+
}
|
|
1104
|
+
$("#memberEmpty").hidden = parts.length > 0;
|
|
1105
|
+
if (mention.active) updateMentionList(mention.query); // 成员刷新时同步提及候选
|
|
1106
|
+
}
|
|
1107
|
+
|
|
1108
|
+
/* ---------- 轮询(失败时指数退避,恢复后回到 2s) ---------- */
|
|
1109
|
+
// ponytail: 串行轮询——poll 完成(含长轮询挂起)后才等待间隔再下一轮,
|
|
1110
|
+
// 绝不多个轮询并发叠加;generation 计数器保证 leave+rejoin 后旧循环
|
|
1111
|
+
// 立即终止(ora-1 #1:否则 25s 在途的旧 poll 会让两条循环永久并存)
|
|
1112
|
+
function startPolling() {
|
|
1113
|
+
stopPolling();
|
|
1114
|
+
state.pollGen = (state.pollGen ?? 0) + 1;
|
|
1115
|
+
state.polling = true;
|
|
1116
|
+
pollDelay = 2000;
|
|
1117
|
+
void pollLoop(state.pollGen);
|
|
1118
|
+
}
|
|
1119
|
+
async function pollLoop(gen) {
|
|
1120
|
+
while (state.polling && state.room && gen === state.pollGen) {
|
|
1121
|
+
await poll().catch(() => {});
|
|
1122
|
+
if (!state.polling || gen !== state.pollGen) break;
|
|
1123
|
+
await new Promise((r) => setTimeout(r, pollDelay));
|
|
1124
|
+
}
|
|
1125
|
+
}
|
|
1126
|
+
function stopPolling() {
|
|
1127
|
+
state.polling = false;
|
|
1128
|
+
}
|
|
1129
|
+
|
|
1130
|
+
async function poll() {
|
|
1131
|
+
if (!state.room) return;
|
|
1132
|
+
const roomId = state.room.id; // 请求发起时的房间(ora-2 M1:防过期响应串房)
|
|
1133
|
+
try {
|
|
1134
|
+
// ponytail: 25s 长轮询(串行 pollLoop 保证同一时刻只有一个请求)
|
|
1135
|
+
const q = new URLSearchParams({
|
|
1136
|
+
sessionID: LS.sessionID,
|
|
1137
|
+
roomId,
|
|
1138
|
+
excludeSelf: "0",
|
|
1139
|
+
timeout: "25000",
|
|
1140
|
+
});
|
|
1141
|
+
const data = await api(`/inbox?${q}`);
|
|
1142
|
+
// 长轮询挂起期间离开/换房:丢弃过期响应,绝不污染新房间
|
|
1143
|
+
if (!state.room || state.room.id !== roomId) return;
|
|
1144
|
+
if (!data.member) {
|
|
1145
|
+
leaveRoom(true);
|
|
1146
|
+
showJoinError("房间不存在或已被删除");
|
|
1147
|
+
return;
|
|
1148
|
+
}
|
|
1149
|
+
const items = Array.isArray(data.items) ? data.items : [];
|
|
1150
|
+
if (items.length) {
|
|
1151
|
+
const msgs = state.room.messages;
|
|
1152
|
+
const known = new Set(msgs.map((m) => m.id));
|
|
1153
|
+
let maxTs = 0;
|
|
1154
|
+
let sawSystem = false;
|
|
1155
|
+
let newCount = 0;
|
|
1156
|
+
for (const it of items) {
|
|
1157
|
+
maxTs = Math.max(maxTs, it.createdAt);
|
|
1158
|
+
if (it.senderId === "system") sawSystem = true;
|
|
1159
|
+
if (known.has(it.id)) continue;
|
|
1160
|
+
msgs.push({
|
|
1161
|
+
id: it.id,
|
|
1162
|
+
roomId: state.room.id,
|
|
1163
|
+
senderId: it.senderId,
|
|
1164
|
+
text: it.text,
|
|
1165
|
+
createdAt: it.createdAt,
|
|
1166
|
+
});
|
|
1167
|
+
newCount++;
|
|
1168
|
+
}
|
|
1169
|
+
// ponytail: mirror the server's 500-message trim locally so the
|
|
1170
|
+
// rebuild-on-head-change path fires like before
|
|
1171
|
+
if (msgs.length > 500) msgs.splice(0, msgs.length - 500);
|
|
1172
|
+
// ponytail: 渲染成功才推进水位——渲染失败(含单条跳过)时消息不得
|
|
1173
|
+
// 标记已读,否则消息"已读"但未显示(不刷新就永久丢);跳过即强制
|
|
1174
|
+
// 对账重试
|
|
1175
|
+
let rendered = true;
|
|
1176
|
+
try {
|
|
1177
|
+
const { skipped } = appendMessages(true);
|
|
1178
|
+
if (skipped > 0) {
|
|
1179
|
+
rendered = false;
|
|
1180
|
+
state.lastReconcileAt = 0; // 强制下一次 poll 全量对账
|
|
1181
|
+
}
|
|
1182
|
+
} catch (err) {
|
|
1183
|
+
rendered = false;
|
|
1184
|
+
state.lastReconcileAt = 0;
|
|
1185
|
+
console.error("[chat] 渲染失败,水位不推进,立即对账:", err);
|
|
1186
|
+
}
|
|
1187
|
+
if (sawSystem) refreshRoomMeta(); // 成员进出事件后同步成员列表
|
|
1188
|
+
// ponytail: ora-1 #3 — 未读计数在 poll 层统一统计(rebuild 场景
|
|
1189
|
+
// appendMessages 以 live=false 渲染,原计数逻辑会漏掉角标)
|
|
1190
|
+
if (newCount > 0) {
|
|
1191
|
+
const sc = $("#thread");
|
|
1192
|
+
const nearBottom = sc.scrollHeight - sc.scrollTop - sc.clientHeight < 80;
|
|
1193
|
+
if (!nearBottom || document.hidden) {
|
|
1194
|
+
state.unread += newCount;
|
|
1195
|
+
updateBadge();
|
|
1196
|
+
}
|
|
1197
|
+
}
|
|
1198
|
+
// advance our own watermark only after successful render
|
|
1199
|
+
if (rendered && maxTs > 0) {
|
|
1200
|
+
void api("/read", {
|
|
1201
|
+
method: "POST",
|
|
1202
|
+
body: JSON.stringify({ roomId: state.room.id, sessionID: LS.sessionID, ts: maxTs }),
|
|
1203
|
+
}).catch(() => {});
|
|
1204
|
+
}
|
|
1205
|
+
}
|
|
1206
|
+
// ponytail: 周期性全量对账(每 2 分钟)——拉房间全量按 msgId 补漏,
|
|
1207
|
+
// 兜底水位竞态/渲染跳过等任何口子,保证 Agent 消息永不丢
|
|
1208
|
+
if (!state.lastReconcileAt || Date.now() - state.lastReconcileAt > 120000) {
|
|
1209
|
+
state.lastReconcileAt = Date.now();
|
|
1210
|
+
void reconcileFull();
|
|
1211
|
+
}
|
|
1212
|
+
setOnline(true);
|
|
1213
|
+
pollDelay = 2000;
|
|
1214
|
+
} catch (e) {
|
|
1215
|
+
if (!state.room || state.room.id !== roomId) return; // 过期错误不处理
|
|
1216
|
+
if (e.status === 401) {
|
|
1217
|
+
setOnline(false);
|
|
1218
|
+
if ($("#chatSettings").hidden) openChatSettings(); // 只弹一次,避免抢焦点
|
|
1219
|
+
pollDelay = Math.min(pollDelay * 2, 30000); // 配置问题——退避
|
|
1220
|
+
} else if (e.status === 404) {
|
|
1221
|
+
leaveRoom(true);
|
|
1222
|
+
showJoinError("房间不存在或已被删除");
|
|
1223
|
+
return;
|
|
1224
|
+
} else {
|
|
1225
|
+
setOnline(false); // 网络/服务器重启等暂时性错误——固定 2s 快速重试
|
|
1226
|
+
pollDelay = 2000;
|
|
1227
|
+
}
|
|
1228
|
+
}
|
|
1229
|
+
}
|
|
1230
|
+
|
|
1231
|
+
// ponytail: 全量对账——拉房间全量(500 cap 内)按 msgId 补缺失并重建渲染,
|
|
1232
|
+
// 确保任何情况下前端与服务器一致(多 tab 各自独立 sessionID,互不影响)
|
|
1233
|
+
async function reconcileFull() {
|
|
1234
|
+
if (!state.room) return;
|
|
1235
|
+
const roomId = state.room.id;
|
|
1236
|
+
try {
|
|
1237
|
+
const room = await api(`/rooms/${encodeURIComponent(roomId)}`);
|
|
1238
|
+
if (!state.room || state.room.id !== roomId) return;
|
|
1239
|
+
const msgs = state.room.messages;
|
|
1240
|
+
const known = new Set(msgs.map((m) => m.id));
|
|
1241
|
+
const missing = (room.messages || []).filter((m) => !known.has(m.id));
|
|
1242
|
+
// orc-2 #3——渲染失败的消息在数组里(missing 为空),但需要重建重试
|
|
1243
|
+
if (missing.length === 0 && failedRenderIds.size === 0) return; // 一致
|
|
1244
|
+
for (const m of missing) {
|
|
1245
|
+
let i = msgs.length;
|
|
1246
|
+
while (i > 0 && msgs[i - 1].createdAt > m.createdAt) i--;
|
|
1247
|
+
msgs.splice(i, 0, m);
|
|
1248
|
+
}
|
|
1249
|
+
if (msgs.length > 500) msgs.splice(0, msgs.length - 500);
|
|
1250
|
+
const list = $("#msgList");
|
|
1251
|
+
list.textContent = ""; // 缺失可能插入已渲染区间——强制重建
|
|
1252
|
+
state.rendered = 0;
|
|
1253
|
+
failedRenderIds.clear(); // 重建会重新渲染全部——失败会重新登记
|
|
1254
|
+
const { skipped } = appendMessages(false);
|
|
1255
|
+
// orc-3 #5——渲染失败时不得推进水位(与 poll 同规则):失败消息留在
|
|
1256
|
+
// 未读,下次对账重试;否则水位越过它且 inbox 不再返回,只剩整页刷新
|
|
1257
|
+
const maxTs = msgs.reduce((mx, m) => Math.max(mx, m.createdAt), 0);
|
|
1258
|
+
if (maxTs > 0 && skipped === 0) {
|
|
1259
|
+
void api("/read", {
|
|
1260
|
+
method: "POST",
|
|
1261
|
+
body: JSON.stringify({ roomId, sessionID: LS.sessionID, ts: maxTs }),
|
|
1262
|
+
}).catch(() => {});
|
|
1263
|
+
}
|
|
1264
|
+
} catch { /* 网络错误——下次对账 */ }
|
|
1265
|
+
}
|
|
1266
|
+
|
|
1267
|
+
async function refreshRoomMeta() {
|
|
1268
|
+
const roomId = state.room?.id;
|
|
1269
|
+
if (!roomId) return;
|
|
1270
|
+
try {
|
|
1271
|
+
const room = await api(`/rooms/${encodeURIComponent(roomId)}`);
|
|
1272
|
+
// orc-2 #4——await 期间可能已切换房间:丢弃过期响应
|
|
1273
|
+
if (state.room && state.room.id === roomId) {
|
|
1274
|
+
state.room.participants = room.participants;
|
|
1275
|
+
renderMembers();
|
|
1276
|
+
}
|
|
1277
|
+
} catch { /* 下次 system 事件再试 */ }
|
|
1278
|
+
}
|
|
1279
|
+
|
|
1280
|
+
function setOnline(ok) { $("#connBar").hidden = ok; }
|
|
1281
|
+
|
|
1282
|
+
/* ---------- 发送 ---------- */
|
|
1283
|
+
function send() {
|
|
1284
|
+
const input = $("#msgInput");
|
|
1285
|
+
const text = input.value.trim();
|
|
1286
|
+
if (!text || !state.room) return;
|
|
1287
|
+
closeMention();
|
|
1288
|
+
input.value = "";
|
|
1289
|
+
autoGrow(input);
|
|
1290
|
+
updateSendState();
|
|
1291
|
+
hideSendError();
|
|
1292
|
+
api(`/rooms/${encodeURIComponent(state.room.id)}/messages`, {
|
|
1293
|
+
method: "POST",
|
|
1294
|
+
body: JSON.stringify({ sessionID: LS.sessionID, text }),
|
|
1295
|
+
})
|
|
1296
|
+
.then((room) => {
|
|
1297
|
+
state.room = room;
|
|
1298
|
+
appendMessages(true);
|
|
1299
|
+
$("#thread").scrollTop = $("#thread").scrollHeight; // 发送后回到底部
|
|
1300
|
+
renderMembers();
|
|
1301
|
+
state.unread = 0;
|
|
1302
|
+
updateBadge();
|
|
1303
|
+
updateJump();
|
|
1304
|
+
})
|
|
1305
|
+
.catch((e) => {
|
|
1306
|
+
input.value = text; // 失败时把消息还回来,提示重试
|
|
1307
|
+
autoGrow(input);
|
|
1308
|
+
updateSendState();
|
|
1309
|
+
if (e.status === 401) { openChatSettings(); showSendError("发送失败:需要访问令牌"); }
|
|
1310
|
+
else showSendError(e.network ? "发送失败:网络连接中断,消息已保留,可稍后重试" : `发送失败:${e.message}`);
|
|
1311
|
+
});
|
|
1312
|
+
}
|
|
1313
|
+
function showSendError(msg) {
|
|
1314
|
+
const el = $("#sendError");
|
|
1315
|
+
el.textContent = msg;
|
|
1316
|
+
el.hidden = false;
|
|
1317
|
+
}
|
|
1318
|
+
function hideSendError() { $("#sendError").hidden = true; }
|
|
1319
|
+
|
|
1320
|
+
/* ---------- 离开 ---------- */
|
|
1321
|
+
function leaveRoom(silent) {
|
|
1322
|
+
stopPolling();
|
|
1323
|
+
closeMention();
|
|
1324
|
+
const roomId = state.room ? state.room.id : "";
|
|
1325
|
+
state.room = null;
|
|
1326
|
+
state.myName = "";
|
|
1327
|
+
state.rendered = 0;
|
|
1328
|
+
state.firstMsgId = null;
|
|
1329
|
+
state.unread = 0;
|
|
1330
|
+
LS.roomId = "";
|
|
1331
|
+
LS.name = "";
|
|
1332
|
+
$("#msgList").textContent = "";
|
|
1333
|
+
$("#msgInput").disabled = true;
|
|
1334
|
+
$("#btnSend").disabled = true;
|
|
1335
|
+
updateBadge();
|
|
1336
|
+
updateJump();
|
|
1337
|
+
if (!silent && roomId) {
|
|
1338
|
+
api(`/rooms/${encodeURIComponent(roomId)}/leave`, {
|
|
1339
|
+
method: "POST",
|
|
1340
|
+
body: JSON.stringify({ sessionID: LS.sessionID }),
|
|
1341
|
+
}).catch(() => {});
|
|
1342
|
+
}
|
|
1343
|
+
showJoin();
|
|
1344
|
+
}
|
|
1345
|
+
|
|
1346
|
+
/* ---------- 已有房间列表 ---------- */
|
|
1347
|
+
async function refreshRoomList() {
|
|
1348
|
+
const list = $("#roomList");
|
|
1349
|
+
const empty = $("#roomListEmpty");
|
|
1350
|
+
empty.textContent = "还没有房间,创建一个吧。";
|
|
1351
|
+
try {
|
|
1352
|
+
const rooms = await api("/rooms");
|
|
1353
|
+
list.textContent = "";
|
|
1354
|
+
empty.hidden = rooms.length > 0;
|
|
1355
|
+
for (const r of rooms.slice(0, 20)) {
|
|
1356
|
+
const li = document.createElement("li");
|
|
1357
|
+
const btn = document.createElement("button");
|
|
1358
|
+
btn.type = "button";
|
|
1359
|
+
btn.className = "room-row";
|
|
1360
|
+
btn.title = "点击直接加入";
|
|
1361
|
+
const nm = document.createElement("span");
|
|
1362
|
+
nm.className = "r-name";
|
|
1363
|
+
nm.textContent = r.name;
|
|
1364
|
+
const meta = document.createElement("span");
|
|
1365
|
+
meta.className = "r-meta";
|
|
1366
|
+
meta.textContent = `${r.participants.length} 人 · ${r.messages.length} 条消息`;
|
|
1367
|
+
const go = document.createElement("span");
|
|
1368
|
+
go.className = "r-go";
|
|
1369
|
+
go.textContent = "加入 →";
|
|
1370
|
+
btn.append(nm, meta, go);
|
|
1371
|
+
btn.addEventListener("click", () => {
|
|
1372
|
+
$("#inRoomId").value = r.id;
|
|
1373
|
+
doJoin(r.id, $("#inName").value.trim() || "访客"); // 一键加入,昵称未填则用“访客”
|
|
1374
|
+
});
|
|
1375
|
+
li.appendChild(btn);
|
|
1376
|
+
list.appendChild(li);
|
|
1377
|
+
}
|
|
1378
|
+
} catch (e) {
|
|
1379
|
+
if (e.status === 401) openTokenBox();
|
|
1380
|
+
else {
|
|
1381
|
+
// 网络失败:加入/创建仍可用,列表给个说明
|
|
1382
|
+
empty.hidden = false;
|
|
1383
|
+
empty.textContent = "无法加载房间列表,可直接输入房间 ID 加入。";
|
|
1384
|
+
}
|
|
1385
|
+
}
|
|
1386
|
+
}
|
|
1387
|
+
|
|
1388
|
+
/* ---------- 面板与令牌 ---------- */
|
|
1389
|
+
function closePanels() {
|
|
1390
|
+
let any = false;
|
|
1391
|
+
document.querySelectorAll(".panel").forEach((p) => { if (!p.hidden) any = true; p.hidden = true; });
|
|
1392
|
+
return any;
|
|
1393
|
+
}
|
|
1394
|
+
function togglePanel(sel) {
|
|
1395
|
+
const p = document.querySelector(sel);
|
|
1396
|
+
const wasHidden = p.hidden;
|
|
1397
|
+
closePanels();
|
|
1398
|
+
p.hidden = !wasHidden;
|
|
1399
|
+
const btn = document.querySelector(sel === "#memberPanel" ? "#btnMembers" : "#btnSettings");
|
|
1400
|
+
btn.setAttribute("aria-expanded", String(!wasHidden));
|
|
1401
|
+
if (!wasHidden) {
|
|
1402
|
+
lastFocus = document.activeElement;
|
|
1403
|
+
const h = p.querySelector("h3");
|
|
1404
|
+
h.tabIndex = -1;
|
|
1405
|
+
h.focus();
|
|
1406
|
+
}
|
|
1407
|
+
}
|
|
1408
|
+
function openTokenBox() { $("#tokenBox").open = true; }
|
|
1409
|
+
function openChatSettings() {
|
|
1410
|
+
closePanels();
|
|
1411
|
+
$("#chatSettings").hidden = false;
|
|
1412
|
+
$("#btnSettings").setAttribute("aria-expanded", "true");
|
|
1413
|
+
lastFocus = $("#btnSettings");
|
|
1414
|
+
$("#tokenHint").hidden = false;
|
|
1415
|
+
$("#inTokenChat").focus();
|
|
1416
|
+
}
|
|
1417
|
+
|
|
1418
|
+
/* ---------- @提及 ---------- */
|
|
1419
|
+
const mention = { active: false, start: 0, query: "", items: [], index: 0 };
|
|
1420
|
+
const MENTION_RE = /<at>@([^<]+)<\/at>/g;
|
|
1421
|
+
const MENTION_HINT = document.createElement("p");
|
|
1422
|
+
MENTION_HINT.className = "mention-hint";
|
|
1423
|
+
MENTION_HINT.textContent = "↑↓ 选择 · Enter 确认 · Esc 关闭";
|
|
1424
|
+
|
|
1425
|
+
// Markdown 最小子集渲染:粗体 / 斜体 / 行内代码 / 链接 / 围栏代码 / 列表 / 引用。
|
|
1426
|
+
// 全部用 DOM API + textContent 构建,不用 innerHTML;任何未闭合或畸形的标记
|
|
1427
|
+
// 都退化为字面文本,不会破坏布局或注入 HTML。
|
|
1428
|
+
const MD_FENCE = /^```/;
|
|
1429
|
+
const MD_QUOTE = /^>\s?/;
|
|
1430
|
+
const MD_UL = /^\s*[-*+]\s+(.*)$/;
|
|
1431
|
+
const MD_OL = /^\s*\d+\.\s+(.*)$/;
|
|
1432
|
+
const MD_INLINE = /(`[^`\n]+`)|(\*\*[^*\n]+\*\*)|(\*[^*\n]+\*)|\[([^\]\n]+)\]\(([^)\n]*)\)/g;
|
|
1433
|
+
|
|
1434
|
+
// 行内渲染:先按 <at>@名字</at> 拆提及(提及优先,提及内部永不做 Markdown),
|
|
1435
|
+
// 纯文本段再做行内格式化。提及是否高亮蓝色取决于当前房间成员名单。
|
|
1436
|
+
function renderInline(root, text) {
|
|
1437
|
+
const parts = String(text).split(MENTION_RE);
|
|
1438
|
+
const names = mentionNames();
|
|
1439
|
+
for (let i = 0; i < parts.length; i++) {
|
|
1440
|
+
if (i % 2 === 1) {
|
|
1441
|
+
const chip = document.createElement("span");
|
|
1442
|
+
chip.className = "mention-chip";
|
|
1443
|
+
if (names.has(parts[i])) chip.classList.add("member");
|
|
1444
|
+
chip.textContent = "@" + parts[i];
|
|
1445
|
+
root.appendChild(chip);
|
|
1446
|
+
} else if (parts[i]) {
|
|
1447
|
+
formatInline(root, parts[i]);
|
|
1448
|
+
}
|
|
1449
|
+
}
|
|
1450
|
+
}
|
|
1451
|
+
|
|
1452
|
+
function formatInline(root, text) {
|
|
1453
|
+
MD_INLINE.lastIndex = 0;
|
|
1454
|
+
let last = 0, m;
|
|
1455
|
+
while ((m = MD_INLINE.exec(text))) {
|
|
1456
|
+
if (m.index > last) root.appendChild(document.createTextNode(text.slice(last, m.index)));
|
|
1457
|
+
if (m[1]) { // 行内代码
|
|
1458
|
+
const code = document.createElement("code");
|
|
1459
|
+
code.textContent = m[1].slice(1, -1);
|
|
1460
|
+
root.appendChild(code);
|
|
1461
|
+
} else if (m[2]) { // 粗体
|
|
1462
|
+
const strong = document.createElement("strong");
|
|
1463
|
+
strong.textContent = m[2].slice(2, -2);
|
|
1464
|
+
root.appendChild(strong);
|
|
1465
|
+
} else if (m[3]) { // 斜体
|
|
1466
|
+
const em = document.createElement("em");
|
|
1467
|
+
em.textContent = m[3].slice(1, -1);
|
|
1468
|
+
root.appendChild(em);
|
|
1469
|
+
} else { // 链接:href 仅允许 http/https,否则按纯文本显示
|
|
1470
|
+
const url = m[5];
|
|
1471
|
+
if (/^https?:\/\//i.test(url)) {
|
|
1472
|
+
const a = document.createElement("a");
|
|
1473
|
+
a.href = url;
|
|
1474
|
+
a.textContent = m[4];
|
|
1475
|
+
a.rel = "noopener noreferrer";
|
|
1476
|
+
a.target = "_blank";
|
|
1477
|
+
root.appendChild(a);
|
|
1478
|
+
} else {
|
|
1479
|
+
root.appendChild(document.createTextNode(m[0]));
|
|
1480
|
+
}
|
|
1481
|
+
}
|
|
1482
|
+
last = m.index + m[0].length;
|
|
1483
|
+
}
|
|
1484
|
+
if (last < text.length) root.appendChild(document.createTextNode(text.slice(last)));
|
|
1485
|
+
}
|
|
1486
|
+
|
|
1487
|
+
function renderList(root, tag, re, lines, i, first) {
|
|
1488
|
+
const list = document.createElement(tag);
|
|
1489
|
+
const item = (content) => {
|
|
1490
|
+
const li = document.createElement("li");
|
|
1491
|
+
renderInline(li, content);
|
|
1492
|
+
list.appendChild(li);
|
|
1493
|
+
};
|
|
1494
|
+
item(first);
|
|
1495
|
+
i++;
|
|
1496
|
+
while (i < lines.length) {
|
|
1497
|
+
const m = re.exec(lines[i]);
|
|
1498
|
+
if (!m) break; // 换个列表符号就结束本列表,交给外层循环重新识别
|
|
1499
|
+
item(m[1]);
|
|
1500
|
+
i++;
|
|
1501
|
+
}
|
|
1502
|
+
root.appendChild(list);
|
|
1503
|
+
return i;
|
|
1504
|
+
}
|
|
1505
|
+
|
|
1506
|
+
// 块级解析:逐行扫描产出 p / pre / ul / ol / blockquote,行内内容交给 renderInline
|
|
1507
|
+
function renderMarkdown(root, text) {
|
|
1508
|
+
const lines = String(text).split("\n");
|
|
1509
|
+
let i = 0;
|
|
1510
|
+
while (i < lines.length) {
|
|
1511
|
+
const line = lines[i];
|
|
1512
|
+
|
|
1513
|
+
if (MD_FENCE.test(line)) { // 围栏代码块(``` 后可跟语言,不展示);未闭合则到消息末尾
|
|
1514
|
+
const buf = [];
|
|
1515
|
+
i++;
|
|
1516
|
+
while (i < lines.length && !MD_FENCE.test(lines[i])) { buf.push(lines[i]); i++; }
|
|
1517
|
+
i++; // 跳过闭合围栏行
|
|
1518
|
+
const pre = document.createElement("pre");
|
|
1519
|
+
const code = document.createElement("code");
|
|
1520
|
+
code.textContent = buf.join("\n");
|
|
1521
|
+
pre.appendChild(code);
|
|
1522
|
+
root.appendChild(pre);
|
|
1523
|
+
continue;
|
|
1524
|
+
}
|
|
1525
|
+
|
|
1526
|
+
let m = MD_UL.exec(line);
|
|
1527
|
+
if (m) { i = renderList(root, "ul", MD_UL, lines, i, m[1]); continue; }
|
|
1528
|
+
m = MD_OL.exec(line);
|
|
1529
|
+
if (m) { i = renderList(root, "ol", MD_OL, lines, i, m[1]); continue; }
|
|
1530
|
+
|
|
1531
|
+
if (MD_QUOTE.test(line)) { // 引用:连续 > 行合并为一个块
|
|
1532
|
+
const buf = [];
|
|
1533
|
+
while (i < lines.length) {
|
|
1534
|
+
const q = MD_QUOTE.exec(lines[i]);
|
|
1535
|
+
if (!q) break;
|
|
1536
|
+
buf.push(lines[i].slice(q[0].length));
|
|
1537
|
+
i++;
|
|
1538
|
+
}
|
|
1539
|
+
const bq = document.createElement("blockquote");
|
|
1540
|
+
renderInline(bq, buf.join("\n"));
|
|
1541
|
+
root.appendChild(bq);
|
|
1542
|
+
continue;
|
|
1543
|
+
}
|
|
1544
|
+
|
|
1545
|
+
if (!line.trim()) { i++; continue; } // 空行:段落分隔
|
|
1546
|
+
|
|
1547
|
+
// 段落:连续的非空、非块起始行;换行保留(.msg-text 为 pre-line)
|
|
1548
|
+
const buf = [line];
|
|
1549
|
+
i++;
|
|
1550
|
+
while (i < lines.length && lines[i].trim()
|
|
1551
|
+
&& !MD_FENCE.test(lines[i]) && !MD_QUOTE.test(lines[i])
|
|
1552
|
+
&& !MD_UL.test(lines[i]) && !MD_OL.test(lines[i])) {
|
|
1553
|
+
buf.push(lines[i]);
|
|
1554
|
+
i++;
|
|
1555
|
+
}
|
|
1556
|
+
const p = document.createElement("p");
|
|
1557
|
+
renderInline(p, buf.join("\n"));
|
|
1558
|
+
root.appendChild(p);
|
|
1559
|
+
}
|
|
1560
|
+
}
|
|
1561
|
+
|
|
1562
|
+
// 消息正文入口:<at>@名字</at> 负载对 Markdown 语法是惰性的(不含 *、`、[ ] 等,
|
|
1563
|
+
// 且总是以 < 开头,不会误判为块起始),所以块结构按原文解析、提及在行内拆分渲染,
|
|
1564
|
+
// 两者互不污染:提及永远保持为 chip,Markdown 永不进入 chip 内部。
|
|
1565
|
+
function renderTextWithMentions(root, text) {
|
|
1566
|
+
renderMarkdown(root, text);
|
|
1567
|
+
}
|
|
1568
|
+
|
|
1569
|
+
// 光标处的 @ 查询:@ 前必须是行首/空白/标点(不在单词中间)才算有效位置
|
|
1570
|
+
// SKIP_CHAR: 跳过 @ 后的查询文本(中文+字母+数字);ASCII_WORD: 英文
|
|
1571
|
+
// mid-word 保护("email@x" 不触发),中文前输入 @ 正常触发("你好@小明")
|
|
1572
|
+
const SKIP_CHAR = /[\p{L}\p{N}_]/u;
|
|
1573
|
+
const ASCII_WORD = /[A-Za-z0-9_]/;
|
|
1574
|
+
function currentMention(input) {
|
|
1575
|
+
const pos = input.selectionStart;
|
|
1576
|
+
const val = input.value;
|
|
1577
|
+
let s = pos;
|
|
1578
|
+
while (s > 0 && SKIP_CHAR.test(val[s - 1])) s--;
|
|
1579
|
+
if (s > 0 && val[s - 1] === "@" && (s - 1 === 0 || !ASCII_WORD.test(val[s - 2]))) {
|
|
1580
|
+
return { start: s - 1, query: val.slice(s, pos) };
|
|
1581
|
+
}
|
|
1582
|
+
return null;
|
|
1583
|
+
}
|
|
1584
|
+
|
|
1585
|
+
function mentionNames() {
|
|
1586
|
+
const names = new Set();
|
|
1587
|
+
for (const p of (state.room && state.room.participants) || []) {
|
|
1588
|
+
// 不包含自己:@ 自己没有意义
|
|
1589
|
+
if (p.name !== state.myName) names.add(p.name);
|
|
1590
|
+
}
|
|
1591
|
+
return names;
|
|
1592
|
+
}
|
|
1593
|
+
|
|
1594
|
+
// 前缀优先、其次包含;不区分大小写
|
|
1595
|
+
function filterMention(q) {
|
|
1596
|
+
const ql = q.toLowerCase();
|
|
1597
|
+
const prefix = [], sub = [];
|
|
1598
|
+
for (const n of mentionNames()) {
|
|
1599
|
+
if (!n) continue;
|
|
1600
|
+
const nl = n.toLowerCase();
|
|
1601
|
+
if (nl.startsWith(ql)) prefix.push(n);
|
|
1602
|
+
else if (nl.includes(ql)) sub.push(n);
|
|
1603
|
+
}
|
|
1604
|
+
return prefix.concat(sub);
|
|
1605
|
+
}
|
|
1606
|
+
|
|
1607
|
+
function highlightMention() {
|
|
1608
|
+
const input = $("#msgInput");
|
|
1609
|
+
const picker = $("#mentionPicker");
|
|
1610
|
+
const opts = picker.querySelectorAll(".mention-opt");
|
|
1611
|
+
if (!mention.items.length) { input.removeAttribute("aria-activedescendant"); return; }
|
|
1612
|
+
opts.forEach((el, i) => {
|
|
1613
|
+
el.classList.toggle("active", i === mention.index);
|
|
1614
|
+
el.setAttribute("aria-selected", i === mention.index ? "true" : "false");
|
|
1615
|
+
});
|
|
1616
|
+
const el = opts[mention.index];
|
|
1617
|
+
input.setAttribute("aria-activedescendant", el.id);
|
|
1618
|
+
const ul = picker.querySelector("ul");
|
|
1619
|
+
if (el.offsetTop < ul.scrollTop) ul.scrollTop = el.offsetTop;
|
|
1620
|
+
else if (el.offsetTop + el.offsetHeight > ul.scrollTop + ul.clientHeight)
|
|
1621
|
+
ul.scrollTop = el.offsetTop + el.offsetHeight - ul.clientHeight;
|
|
1622
|
+
}
|
|
1623
|
+
|
|
1624
|
+
function updateMentionList(query) {
|
|
1625
|
+
mention.query = query;
|
|
1626
|
+
mention.items = filterMention(query);
|
|
1627
|
+
mention.index = mention.items.length ? 0 : -1;
|
|
1628
|
+
const picker = $("#mentionPicker");
|
|
1629
|
+
picker.textContent = "";
|
|
1630
|
+
const ul = document.createElement("ul");
|
|
1631
|
+
mention.items.forEach((name, i) => {
|
|
1632
|
+
const li = document.createElement("li");
|
|
1633
|
+
li.className = "mention-opt";
|
|
1634
|
+
li.id = `mention-opt-${i}`;
|
|
1635
|
+
li.setAttribute("role", "option");
|
|
1636
|
+
li.dataset.name = name;
|
|
1637
|
+
const at = document.createElement("span");
|
|
1638
|
+
at.className = "mention-at";
|
|
1639
|
+
at.textContent = "@";
|
|
1640
|
+
li.appendChild(at);
|
|
1641
|
+
// 命中部分高亮
|
|
1642
|
+
const nl = name.toLowerCase();
|
|
1643
|
+
const ql = mention.query.toLowerCase();
|
|
1644
|
+
const idx = ql ? nl.indexOf(ql) : -1;
|
|
1645
|
+
if (idx < 0) li.appendChild(document.createTextNode(name));
|
|
1646
|
+
else {
|
|
1647
|
+
if (idx > 0) li.appendChild(document.createTextNode(name.slice(0, idx)));
|
|
1648
|
+
const hit = document.createElement("span");
|
|
1649
|
+
hit.className = "mention-hit";
|
|
1650
|
+
hit.textContent = name.slice(idx, idx + mention.query.length);
|
|
1651
|
+
li.appendChild(hit);
|
|
1652
|
+
li.appendChild(document.createTextNode(name.slice(idx + mention.query.length)));
|
|
1653
|
+
}
|
|
1654
|
+
ul.appendChild(li);
|
|
1655
|
+
});
|
|
1656
|
+
picker.appendChild(ul);
|
|
1657
|
+
if (!mention.items.length) {
|
|
1658
|
+
const empty = document.createElement("p");
|
|
1659
|
+
empty.className = "mention-empty";
|
|
1660
|
+
empty.textContent = "没有匹配的成员";
|
|
1661
|
+
picker.appendChild(empty);
|
|
1662
|
+
}
|
|
1663
|
+
picker.appendChild(MENTION_HINT);
|
|
1664
|
+
highlightMention();
|
|
1665
|
+
}
|
|
1666
|
+
|
|
1667
|
+
function openMention(start, query) {
|
|
1668
|
+
mention.active = true;
|
|
1669
|
+
mention.start = start;
|
|
1670
|
+
updateMentionList(query);
|
|
1671
|
+
$("#mentionPicker").hidden = false;
|
|
1672
|
+
$("#msgInput").setAttribute("aria-expanded", "true");
|
|
1673
|
+
}
|
|
1674
|
+
|
|
1675
|
+
function closeMention() {
|
|
1676
|
+
if (!mention.active) return;
|
|
1677
|
+
mention.active = false;
|
|
1678
|
+
mention.items = [];
|
|
1679
|
+
mention.index = 0;
|
|
1680
|
+
$("#mentionPicker").hidden = true;
|
|
1681
|
+
const input = $("#msgInput");
|
|
1682
|
+
input.removeAttribute("aria-expanded");
|
|
1683
|
+
input.removeAttribute("aria-activedescendant");
|
|
1684
|
+
}
|
|
1685
|
+
|
|
1686
|
+
function recomputeMention() {
|
|
1687
|
+
const input = $("#msgInput");
|
|
1688
|
+
const m = currentMention(input);
|
|
1689
|
+
if (m) {
|
|
1690
|
+
// 命中:弹框未开则打开,已开则更新过滤
|
|
1691
|
+
if (mention.active) updateMentionList(m.query);
|
|
1692
|
+
else openMention(m.start, m.query);
|
|
1693
|
+
} else if (mention.active) {
|
|
1694
|
+
// 弹框已开但边界扫描未命中:只要 @ 还在输入框里就保持弹框
|
|
1695
|
+
// (仅更新过滤),避免 keydown 打开后被 input 事件链立刻误关;
|
|
1696
|
+
// @ 被删除/移动后才关闭
|
|
1697
|
+
const val = input.value;
|
|
1698
|
+
if (val.includes("@")) {
|
|
1699
|
+
const at = val.lastIndexOf("@");
|
|
1700
|
+
// ponytail: orc-1 #6 — 同步更新 mention.start,避免第二个 @ 时
|
|
1701
|
+
// 插入替换到旧位置
|
|
1702
|
+
mention.start = at;
|
|
1703
|
+
updateMentionList(val.slice(at + 1, input.selectionStart));
|
|
1704
|
+
} else {
|
|
1705
|
+
closeMention();
|
|
1706
|
+
}
|
|
1707
|
+
}
|
|
1708
|
+
}
|
|
1709
|
+
|
|
1710
|
+
function moveMention(d) {
|
|
1711
|
+
const n = mention.items.length;
|
|
1712
|
+
if (!n) return;
|
|
1713
|
+
mention.index = (mention.index + d + n) % n;
|
|
1714
|
+
highlightMention();
|
|
1715
|
+
}
|
|
1716
|
+
|
|
1717
|
+
// 把光标处的 @查询 替换为 <at>@名字</at>,光标放到末尾
|
|
1718
|
+
function insertMention(name) {
|
|
1719
|
+
const input = $("#msgInput");
|
|
1720
|
+
const end = mention.start + 1 + mention.query.length;
|
|
1721
|
+
if (input.value.slice(mention.start, end) !== "@" + mention.query) { closeMention(); return; } // 内容变了就放弃
|
|
1722
|
+
input.setRangeText(`<at>@${name}</at>`, mention.start, end, "end");
|
|
1723
|
+
closeMention();
|
|
1724
|
+
autoGrow(input);
|
|
1725
|
+
updateSendState();
|
|
1726
|
+
input.focus({ preventScroll: true });
|
|
1727
|
+
}
|
|
1728
|
+
|
|
1729
|
+
/* ---------- 事件 ---------- */
|
|
1730
|
+
function wireEvents() {
|
|
1731
|
+
$("#joinForm").addEventListener("submit", (e) => { e.preventDefault(); joinRoom(); });
|
|
1732
|
+
$("#createForm").addEventListener("submit", (e) => { e.preventDefault(); createRoom(); });
|
|
1733
|
+
$("#btnSend").addEventListener("click", send);
|
|
1734
|
+
$("#btnJump").addEventListener("click", jumpToBottom);
|
|
1735
|
+
$("#btnCopyId").addEventListener("click", copyRoomId);
|
|
1736
|
+
$("#msgInput").addEventListener("keydown", (e) => {
|
|
1737
|
+
// ponytail: @ 弹框由 input 事件统一驱动(recomputeMention)——之前
|
|
1738
|
+
// 为绕过 scroll 误关打的多条补丁(keydown 即弹、doc-capture)已随
|
|
1739
|
+
// 根因修复移除;此处只处理弹框开启后的键盘导航
|
|
1740
|
+
if (mention.active && !e.isComposing) {
|
|
1741
|
+
if (e.key === "ArrowDown") { e.preventDefault(); moveMention(1); return; }
|
|
1742
|
+
if (e.key === "ArrowUp") { e.preventDefault(); moveMention(-1); return; }
|
|
1743
|
+
if (e.key === "Escape") { e.preventDefault(); closeMention(); return; }
|
|
1744
|
+
if (e.key === "Enter" || e.key === "Tab") {
|
|
1745
|
+
if (mention.items.length) {
|
|
1746
|
+
e.preventDefault();
|
|
1747
|
+
insertMention(mention.items[mention.index]);
|
|
1748
|
+
return;
|
|
1749
|
+
}
|
|
1750
|
+
closeMention();
|
|
1751
|
+
if (e.key === "Tab") return; // 无匹配时 Tab 照常离开
|
|
1752
|
+
// Enter 无匹配:落到下面的发送逻辑
|
|
1753
|
+
}
|
|
1754
|
+
}
|
|
1755
|
+
// Enter 发送(Ctrl+Enter 也发送,Shift+Enter 换行);isComposing: 中文输入法组词回车不误发
|
|
1756
|
+
if (e.key === "Enter" && !e.isComposing && (e.ctrlKey || !e.shiftKey)) {
|
|
1757
|
+
e.preventDefault();
|
|
1758
|
+
send();
|
|
1759
|
+
}
|
|
1760
|
+
});
|
|
1761
|
+
$("#msgInput").addEventListener("input", (e) => {
|
|
1762
|
+
autoGrow($("#msgInput"));
|
|
1763
|
+
updateSendState();
|
|
1764
|
+
// ponytail: 不因 isComposing 跳过——中文输入法下 @ 上屏的 input 事件
|
|
1765
|
+
// 可能带 composing 标志,跳过会导致弹框永不打开;组词产生的候选
|
|
1766
|
+
// 字符不含 @,recomputeMention 天然无害
|
|
1767
|
+
recomputeMention();
|
|
1768
|
+
});
|
|
1769
|
+
$("#msgInput").addEventListener("keyup", (e) => {
|
|
1770
|
+
// 左右方向键 / Home / End 移动了光标后重新定位(上下键已被面板占用)
|
|
1771
|
+
if (mention.active && !e.isComposing && /^(ArrowLeft|ArrowRight|Home|End)/.test(e.key)) recomputeMention();
|
|
1772
|
+
});
|
|
1773
|
+
$("#msgInput").addEventListener("click", recomputeMention);
|
|
1774
|
+
$("#msgInput").addEventListener("blur", closeMention);
|
|
1775
|
+
$("#mentionPicker").addEventListener("mousedown", (e) => {
|
|
1776
|
+
const opt = e.target.closest(".mention-opt");
|
|
1777
|
+
if (!opt) return;
|
|
1778
|
+
e.preventDefault(); // 阻止 textarea 失焦,blur 就不会抢先关面板
|
|
1779
|
+
insertMention(opt.dataset.name);
|
|
1780
|
+
});
|
|
1781
|
+
["#inRoomId", "#inName", "#inRoomName"].forEach((s) => {
|
|
1782
|
+
$(s).addEventListener("input", hideJoinError);
|
|
1783
|
+
});
|
|
1784
|
+
$("#thread").addEventListener("scroll", () => {
|
|
1785
|
+
// ponytail: 不在此处 closeMention——弹框打开引起的布局/滚动位置
|
|
1786
|
+
// 调整会触发 scroll 事件,无条件关闭会让弹框"打开即消失";
|
|
1787
|
+
// 弹框只由用户明确操作(Esc/点击外部/失焦/插入/发送)关闭
|
|
1788
|
+
const sc = $("#thread");
|
|
1789
|
+
if (sc.scrollHeight - sc.scrollTop - sc.clientHeight < 80) {
|
|
1790
|
+
state.unread = 0;
|
|
1791
|
+
updateBadge();
|
|
1792
|
+
}
|
|
1793
|
+
updateJump();
|
|
1794
|
+
});
|
|
1795
|
+
$("#btnLeave").addEventListener("click", () => leaveRoom(false));
|
|
1796
|
+
$("#btnMembers").addEventListener("click", () => togglePanel("#memberPanel"));
|
|
1797
|
+
$("#btnSettings").addEventListener("click", () => togglePanel("#chatSettings"));
|
|
1798
|
+
|
|
1799
|
+
document.addEventListener("click", (e) => {
|
|
1800
|
+
const save = e.target.closest("[data-token-save]");
|
|
1801
|
+
if (save) {
|
|
1802
|
+
const input = document.getElementById(save.dataset.tokenSave);
|
|
1803
|
+
LS.token = input.value.trim();
|
|
1804
|
+
input.value = LS.token;
|
|
1805
|
+
$("#inToken").value = LS.token;
|
|
1806
|
+
$("#inTokenChat").value = LS.token;
|
|
1807
|
+
flash(save, "已保存");
|
|
1808
|
+
return;
|
|
1809
|
+
}
|
|
1810
|
+
if (mention.active && !e.target.closest("#mentionPicker")) closeMention();
|
|
1811
|
+
if (!e.target.closest(".anchor")) closePanels();
|
|
1812
|
+
});
|
|
1813
|
+
// ponytail: 不用 pagehide 自动 leave——它无法区分关闭/后台/bfcache,
|
|
1814
|
+
// 刷新竞态会把新 join 的条目删掉(orc-1 #2);死条目由 join 清理 +
|
|
1815
|
+
// claim 窗口限制影响,可接受
|
|
1816
|
+
document.addEventListener("keydown", (e) => {
|
|
1817
|
+
if (e.key === "Escape" && (closePanels() || mention.active)) {
|
|
1818
|
+
e.preventDefault();
|
|
1819
|
+
closeMention();
|
|
1820
|
+
if (lastFocus) lastFocus.focus();
|
|
1821
|
+
}
|
|
1822
|
+
});
|
|
1823
|
+
}
|
|
1824
|
+
|
|
1825
|
+
/* ---------- 启动 ---------- */
|
|
1826
|
+
async function autoJoin() {
|
|
1827
|
+
const roomId = LS.roomId;
|
|
1828
|
+
const name = LS.name;
|
|
1829
|
+
if (!roomId || !name) return;
|
|
1830
|
+
$("#inRoomId").value = roomId;
|
|
1831
|
+
$("#inName").value = name;
|
|
1832
|
+
try {
|
|
1833
|
+
const room = await api(`/rooms/${encodeURIComponent(roomId)}/join`, {
|
|
1834
|
+
method: "POST",
|
|
1835
|
+
body: JSON.stringify({ sessionID: LS.sessionID, participantId: name, participantName: name }),
|
|
1836
|
+
});
|
|
1837
|
+
enterRoom(room, name);
|
|
1838
|
+
} catch (e) {
|
|
1839
|
+
LS.roomId = "";
|
|
1840
|
+
LS.name = "";
|
|
1841
|
+
if (e.status === 401) openTokenBox();
|
|
1842
|
+
else if (e.status === 404) showJoinError("上次的房间已不存在");
|
|
1843
|
+
else if (e.network) showJoinError("无法连接服务器,请检查网络后重试");
|
|
1844
|
+
}
|
|
1845
|
+
}
|
|
1846
|
+
|
|
1847
|
+
$("#inToken").value = LS.token;
|
|
1848
|
+
$("#inTokenChat").value = LS.token;
|
|
1849
|
+
wireEvents();
|
|
1850
|
+
refreshRoomList();
|
|
1851
|
+
autoJoin();
|
|
1852
|
+
$("#inRoomId").focus();
|
|
1853
|
+
})();
|
|
1854
|
+
</script>
|
|
1855
|
+
</body>
|
|
1856
|
+
</html>
|