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