@alexsun-top/opencode-chat-room 0.8.2 → 0.8.3
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 +10 -0
- package/README.md +18 -62
- package/README.zh-CN.md +25 -69
- package/dist/cli.js +466 -188
- package/package.json +8 -5
- package/packages/chat-room-channeld/README.md +25 -3
- package/packages/chat-room-channeld/dist/mock-adapter.js +5 -5
- package/packages/chat-room-channeld/src/adapter.ts +5 -0
- package/packages/chat-room-channeld/src/cli.ts +72 -5
- package/packages/chat-room-channeld/src/host.ts +17 -2
- package/packages/chat-room-channeld/src/pg/database.ts +187 -0
- package/packages/chat-room-channeld/src/pg/migrate.ts +150 -0
- package/packages/chat-room-channeld/src/pg/schema.ts +60 -0
- package/packages/chat-room-channeld/src/pg/sql.mjs +175 -0
- package/packages/chat-room-channeld/src/pg/sqlite.ts +100 -0
- package/packages/chat-room-channeld/src/pg/worker.mjs +133 -0
- package/packages/chat-room-channeld/src/protocol.ts +1 -0
- package/packages/chat-room-channeld/src/state.ts +73 -97
- package/packages/chat-room-channeld/src/stats.ts +140 -109
- package/server/assets/base.css +3 -1
- package/server/assets/chat/admin-badge.js +32 -30
- package/server/assets/chat/admin.js +198 -161
- package/server/assets/chat/announcements.js +295 -208
- package/server/assets/chat/attachment-store.js +106 -75
- package/server/assets/chat/attachments.js +686 -441
- package/server/assets/chat/chat-background.js +185 -156
- package/server/assets/chat/clipboard.js +44 -25
- package/server/assets/chat/confirm-dialog.js +36 -33
- package/server/assets/chat/copy-action.js +40 -40
- package/server/assets/chat/create-room-dialog.js +50 -47
- package/server/assets/chat/diagram-viewer.js +293 -234
- package/server/assets/chat/doc-preview.js +507 -441
- package/server/assets/chat/editor.js +423 -335
- package/server/assets/chat/events.js +637 -510
- package/server/assets/chat/files.js +476 -430
- package/server/assets/chat/history-loader.js +79 -73
- package/server/assets/chat/icons.js +9 -11
- package/server/assets/chat/image-zoom.js +239 -176
- package/server/assets/chat/index.js +2 -3
- package/server/assets/chat/install-prompt.js +86 -66
- package/server/assets/chat/integration-account-schema.js +88 -86
- package/server/assets/chat/integration-editor.js +162 -153
- package/server/assets/chat/integration-values.js +109 -100
- package/server/assets/chat/integrations.js +689 -622
- package/server/assets/chat/join-requests.js +408 -358
- package/server/assets/chat/locales/de.js +193 -194
- package/server/assets/chat/locales/es.js +193 -194
- package/server/assets/chat/locales/fr.js +193 -194
- package/server/assets/chat/locales/index.js +1 -2
- package/server/assets/chat/locales/it.js +193 -194
- package/server/assets/chat/locales/ja.js +193 -193
- package/server/assets/chat/locales/ko.js +193 -193
- package/server/assets/chat/markdown-footnotes.js +134 -102
- package/server/assets/chat/markdown.js +547 -403
- package/server/assets/chat/members.js +236 -197
- package/server/assets/chat/mentions.js +157 -133
- package/server/assets/chat/message-cache.js +40 -33
- package/server/assets/chat/message-polling.js +280 -227
- package/server/assets/chat/message-window.js +74 -55
- package/server/assets/chat/messages.js +552 -480
- package/server/assets/chat/office-preview.js +46 -45
- package/server/assets/chat/palette.js +450 -417
- package/server/assets/chat/panels.js +94 -78
- package/server/assets/chat/pwa.js +144 -73
- package/server/assets/chat/questions.js +372 -302
- package/server/assets/chat/recall.js +320 -273
- package/server/assets/chat/receipts.js +149 -121
- package/server/assets/chat/reminders.js +331 -214
- package/server/assets/chat/replies.js +165 -130
- package/server/assets/chat/resource-format.js +59 -62
- package/server/assets/chat/room-list.js +58 -55
- package/server/assets/chat/room-watcher.js +72 -50
- package/server/assets/chat/rooms.js +623 -500
- package/server/assets/chat/search.js +136 -114
- package/server/assets/chat/settings.js +223 -200
- package/server/assets/chat/shiki.js +61 -62
- package/server/assets/chat/shortcuts.js +212 -187
- package/server/assets/chat/side-pane.js +160 -150
- package/server/assets/chat/state.js +849 -735
- package/server/assets/chat/system-events.js +45 -43
- package/server/assets/chat/system-messages.js +57 -56
- package/server/assets/chat/task-form.js +178 -182
- package/server/assets/chat/task-row.js +158 -155
- package/server/assets/chat/tasks.js +653 -587
- package/server/assets/chat/viewport.js +32 -29
- package/server/assets/chat/window-controls.js +32 -32
- package/server/assets/conversation.css +10 -4
- package/server/chat-server.ts +9 -1
- package/server/chat.html +2 -1
- package/server/routes/pages.ts +7 -5
- package/server/sw.js +346 -268
- package/src/web/integration-rules-editor.ts +2 -0
|
@@ -1,389 +1,477 @@
|
|
|
1
1
|
import { createRandomId } from "./state.js";
|
|
2
|
-
|
|
3
2
|
const trustedChipNames = new Map();
|
|
4
3
|
const autoSeparator = "\uE000\uE001";
|
|
5
4
|
function mentionName(node) {
|
|
6
|
-
|
|
7
|
-
|
|
5
|
+
const name = trustedChipNames.get(node.dataset?.chatChip);
|
|
6
|
+
return name !== undefined && node.dataset.mention === name ? name : null;
|
|
8
7
|
}
|
|
9
8
|
function chipLabel(editor, name) {
|
|
10
|
-
|
|
9
|
+
return editor.mentionLabel ? editor.mentionLabel(name) : name;
|
|
11
10
|
}
|
|
12
|
-
|
|
13
11
|
export function editorText(editor, draft = false) {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
12
|
+
if (editor.childNodes.length === 1 && editor.firstChild?.nodeName === "BR")
|
|
13
|
+
return "";
|
|
14
|
+
if (editor.childNodes.length === 1 && editor.firstChild?.nodeName === "DIV" && editor.firstElementChild?.nodeName === "BR")
|
|
15
|
+
return "";
|
|
16
|
+
let value = "";
|
|
17
|
+
const block = (node) => node.nodeType === Node.ELEMENT_NODE && node.tagName === "DIV";
|
|
18
|
+
const walk = (node) => {
|
|
19
|
+
if (node.nodeType === Node.TEXT_NODE)
|
|
20
|
+
value += draft ? encodeDraftText(node.nodeValue) : node.nodeValue;
|
|
21
|
+
else if (node.nodeType === Node.ELEMENT_NODE) {
|
|
22
|
+
const mention = mentionName(node);
|
|
23
|
+
if (node.dataset?.chatAutoSeparator)
|
|
24
|
+
value += draft && node.textContent.startsWith(" ")
|
|
25
|
+
? autoSeparator + encodeDraftText(node.textContent.slice(1)) : draft ? encodeDraftText(node.textContent) : node.textContent;
|
|
26
|
+
else if (mention !== null)
|
|
27
|
+
value += `<at>@${mention}</at>`;
|
|
28
|
+
else if (node.tagName === "BR") {
|
|
29
|
+
if (!node.dataset?.chatTail)
|
|
30
|
+
value += "\n";
|
|
31
|
+
}
|
|
32
|
+
else
|
|
33
|
+
node.childNodes.forEach((child, index, all) => {
|
|
34
|
+
if (index && (block(child) || block(all[index - 1])))
|
|
35
|
+
value += "\n";
|
|
36
|
+
if (!(block(child) && child.childNodes.length === 1 && child.firstChild.nodeName === "BR"))
|
|
37
|
+
walk(child);
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
editor.childNodes.forEach((node, index, all) => {
|
|
42
|
+
if (index && (block(node) || block(all[index - 1])))
|
|
43
|
+
value += "\n";
|
|
44
|
+
if (!(block(node) && node.childNodes.length === 1 && node.firstChild.nodeName === "BR"))
|
|
45
|
+
walk(node);
|
|
46
|
+
});
|
|
47
|
+
return value;
|
|
37
48
|
}
|
|
38
|
-
|
|
39
49
|
export const editorDraftText = (editor) => editorText(editor, true);
|
|
40
50
|
export const autoMentionDraft = (name) => `<at>@${name}</at>${autoSeparator}`;
|
|
41
51
|
const literalMention = /<at>@([^<]+)<\/at>/g;
|
|
42
52
|
function encodeDraftText(text) { return String(text).replace(/\uE000/g, "\uE000\uE000").replace(literalMention, "\uE000$&"); }
|
|
43
53
|
function appendText(editor, text) {
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
54
|
+
String(text).split("\n").forEach((line, index) => {
|
|
55
|
+
if (index)
|
|
56
|
+
editor.append(document.createElement("br"));
|
|
57
|
+
if (line)
|
|
58
|
+
editor.append(document.createTextNode(line));
|
|
59
|
+
});
|
|
48
60
|
}
|
|
49
|
-
|
|
50
61
|
export function setEditorText(editor, value, draft = false) {
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
62
|
+
editor.replaceChildren();
|
|
63
|
+
const pattern = draft ? /\uE000\uE001|\uE000\uE000|\uE000<at>@([^<]+)<\/at>|<at>@([^<]+)<\/at>/g : /<at>@([^<]+)<\/at>/g;
|
|
64
|
+
let offset = 0, match;
|
|
65
|
+
while ((match = pattern.exec(String(value))) !== null) {
|
|
66
|
+
appendText(editor, String(value).slice(offset, match.index));
|
|
67
|
+
if (draft && match[0] === autoSeparator) {
|
|
68
|
+
editor.append(createAutoSeparator());
|
|
69
|
+
offset = pattern.lastIndex;
|
|
70
|
+
continue;
|
|
71
|
+
}
|
|
72
|
+
if (draft && match[0] === "\uE000\uE000") {
|
|
73
|
+
appendText(editor, "\uE000");
|
|
74
|
+
offset = pattern.lastIndex;
|
|
75
|
+
continue;
|
|
76
|
+
}
|
|
77
|
+
if (draft && match[0].startsWith("\uE000")) {
|
|
78
|
+
appendText(editor, match[0].slice(1));
|
|
79
|
+
offset = pattern.lastIndex;
|
|
80
|
+
continue;
|
|
81
|
+
}
|
|
82
|
+
editor.append(createMention(editor, draft ? match[2] : match[1]));
|
|
83
|
+
offset = pattern.lastIndex;
|
|
84
|
+
}
|
|
85
|
+
appendText(editor, String(value).slice(offset));
|
|
86
|
+
editor.dataset.empty = value ? "false" : "true";
|
|
87
|
+
pruneTrustedChipNames();
|
|
64
88
|
}
|
|
65
|
-
|
|
66
89
|
export function editorSelectionOffsets(editor) {
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
const
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
90
|
+
const selection = getSelection();
|
|
91
|
+
if (!selection?.rangeCount || !editor.contains(selection.anchorNode))
|
|
92
|
+
return [editorText(editor).length, editorText(editor).length];
|
|
93
|
+
const selected = selection.getRangeAt(0), collapsed = selected.collapsed;
|
|
94
|
+
const offset = (node, position, isStart) => {
|
|
95
|
+
const element = node.nodeType === Node.ELEMENT_NODE ? node : node.parentElement;
|
|
96
|
+
const chip = element?.closest(".editor-mention");
|
|
97
|
+
const raw = lengthBefore(editor, node, position);
|
|
98
|
+
if (chip && editor.contains(chip) && mentionName(chip) !== null) {
|
|
99
|
+
const start = lengthBefore(editor, chip, 0), end = start + `<at>@${mentionName(chip)}</at>`.length;
|
|
100
|
+
// A caret strictly inside a non-editable chip (e.g. left there by an
|
|
101
|
+
// ESC-cancelled IME composition) is not a text position; snap to the end.
|
|
102
|
+
// A selection edge inside a chip expands to the whole chip so copy and
|
|
103
|
+
// cut keep the mention token instead of falling back to plain text.
|
|
104
|
+
if (raw > start && raw < end)
|
|
105
|
+
return collapsed || !isStart ? end : start;
|
|
106
|
+
}
|
|
107
|
+
return raw;
|
|
108
|
+
};
|
|
109
|
+
return [offset(selected.startContainer, selected.startOffset, true), offset(selected.endContainer, selected.endOffset, false)];
|
|
85
110
|
}
|
|
86
|
-
|
|
87
111
|
function lengthBefore(root, target, position) {
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
112
|
+
const block = (node) => node.nodeType === Node.ELEMENT_NODE && node.tagName === "DIV";
|
|
113
|
+
const length = (node) => node.nodeType === Node.TEXT_NODE ? node.length
|
|
114
|
+
: mentionName(node) !== null ? `<at>@${mentionName(node)}</at>`.length
|
|
115
|
+
: node.tagName === "BR" ? (node.dataset?.chatTail ? 0 : 1) : childrenLength(node);
|
|
116
|
+
const childrenLength = (parent, until = parent.childNodes.length) => {
|
|
117
|
+
let sum = 0;
|
|
118
|
+
for (let index = 0; index < until; index++) {
|
|
119
|
+
const child = parent.childNodes[index];
|
|
120
|
+
if (index && (block(child) || block(parent.childNodes[index - 1])))
|
|
121
|
+
sum++;
|
|
122
|
+
if (!(block(child) && child.childNodes.length === 1 && child.firstChild.nodeName === "BR"))
|
|
123
|
+
sum += length(child);
|
|
124
|
+
}
|
|
125
|
+
return sum;
|
|
126
|
+
};
|
|
127
|
+
const visit = (node) => {
|
|
128
|
+
if (node === target)
|
|
129
|
+
return node.nodeType === Node.TEXT_NODE ? Math.min(position, node.length)
|
|
130
|
+
: childrenLength(node, position);
|
|
131
|
+
let total = 0;
|
|
132
|
+
for (let index = 0; index < node.childNodes.length; index++) {
|
|
133
|
+
const child = node.childNodes[index];
|
|
134
|
+
if (index && (block(child) || block(node.childNodes[index - 1])))
|
|
135
|
+
total++;
|
|
136
|
+
if (child === target || child.contains(target))
|
|
137
|
+
return total + visit(child);
|
|
138
|
+
if (!(block(child) && child.childNodes.length === 1 && child.firstChild.nodeName === "BR"))
|
|
139
|
+
total += length(child);
|
|
140
|
+
}
|
|
141
|
+
return total;
|
|
142
|
+
};
|
|
143
|
+
return visit(root);
|
|
114
144
|
}
|
|
115
|
-
|
|
116
145
|
export function setCaret(editor, offset) {
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
146
|
+
const selection = getSelection(), range = document.createRange();
|
|
147
|
+
// Chromium starts IME composition poorly at an element boundary next to a
|
|
148
|
+
// non-editable chip; keep the caret inside a real text node instead.
|
|
149
|
+
const [container, index] = textPoint(editorPoint(editor, offset));
|
|
150
|
+
range.setStart(container, index);
|
|
151
|
+
range.collapse(true);
|
|
152
|
+
selection.removeAllRanges();
|
|
153
|
+
selection.addRange(range);
|
|
123
154
|
}
|
|
124
|
-
|
|
125
155
|
function textPoint([container, offset]) {
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
156
|
+
if (container.nodeType !== Node.ELEMENT_NODE)
|
|
157
|
+
return [container, offset];
|
|
158
|
+
const child = container.childNodes[offset], previous = container.childNodes[offset - 1];
|
|
159
|
+
if (child?.nodeType === Node.TEXT_NODE && child.length === 0) {
|
|
160
|
+
const next = child.nextSibling;
|
|
161
|
+
if (next?.nodeType === Node.TEXT_NODE) {
|
|
162
|
+
child.remove();
|
|
163
|
+
return [next, 0];
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
if (child?.nodeType === Node.TEXT_NODE)
|
|
167
|
+
return [child, 0];
|
|
168
|
+
if (previous?.nodeType === Node.TEXT_NODE)
|
|
169
|
+
return [previous, previous.length];
|
|
170
|
+
const text = document.createTextNode("");
|
|
171
|
+
container.insertBefore(text, child || null);
|
|
172
|
+
return [text, 0];
|
|
137
173
|
}
|
|
138
|
-
|
|
139
174
|
export function setEditorSelection(editor, start, end = start) {
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
175
|
+
const range = document.createRange(), selection = getSelection();
|
|
176
|
+
const first = editorPoint(editor, start), last = editorPoint(editor, end);
|
|
177
|
+
range.setStart(first[0], first[1]);
|
|
178
|
+
range.setEnd(last[0], last[1]);
|
|
179
|
+
selection.removeAllRanges();
|
|
180
|
+
selection.addRange(range);
|
|
144
181
|
}
|
|
145
|
-
|
|
146
182
|
function editorPoint(editor, offset) {
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
if (!editor.lastChild.dataset?.chatTail) {
|
|
155
|
-
const tail = document.createElement("br");
|
|
156
|
-
tail.dataset.chatTail = "true";
|
|
157
|
-
editor.append(tail);
|
|
183
|
+
const atEnd = offset === editorText(editor).length;
|
|
184
|
+
if (atEnd && editor.lastChild?.nodeType === Node.TEXT_NODE && editor.lastChild.nodeValue === "")
|
|
185
|
+
editor.lastChild.remove();
|
|
186
|
+
const last = editor.lastElementChild;
|
|
187
|
+
if (atEnd && editor.lastChild === last && last?.tagName === "DIV" && last.firstChild?.nodeName === "BR") {
|
|
188
|
+
const text = last.appendChild(document.createTextNode(""));
|
|
189
|
+
return [text, 0];
|
|
158
190
|
}
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
191
|
+
if (atEnd && editor.lastChild?.nodeName === "BR") {
|
|
192
|
+
if (!editor.lastChild.dataset?.chatTail) {
|
|
193
|
+
const tail = document.createElement("br");
|
|
194
|
+
tail.dataset.chatTail = "true";
|
|
195
|
+
editor.append(tail);
|
|
196
|
+
}
|
|
197
|
+
return [editor, editor.childNodes.length - 1];
|
|
198
|
+
}
|
|
199
|
+
const block = (node) => node.nodeType === Node.ELEMENT_NODE && node.tagName === "DIV";
|
|
200
|
+
let left = offset, point = null;
|
|
201
|
+
const visit = (node) => {
|
|
202
|
+
if (point)
|
|
203
|
+
return;
|
|
204
|
+
if (node.nodeType === Node.TEXT_NODE) {
|
|
205
|
+
if (left <= node.length)
|
|
206
|
+
point = [node, left];
|
|
207
|
+
else
|
|
208
|
+
left -= node.length;
|
|
209
|
+
}
|
|
210
|
+
else if (node.nodeType === Node.ELEMENT_NODE && mentionName(node) !== null) {
|
|
211
|
+
const index = [...node.parentNode.childNodes].indexOf(node), length = `<at>@${mentionName(node)}</at>`.length;
|
|
212
|
+
if (left <= length)
|
|
213
|
+
point = [node.parentNode, index + (left ? 1 : 0)];
|
|
214
|
+
else
|
|
215
|
+
left -= length;
|
|
216
|
+
}
|
|
217
|
+
else if (node.tagName === "BR") {
|
|
218
|
+
const index = [...node.parentNode.childNodes].indexOf(node);
|
|
219
|
+
if (left <= 1)
|
|
220
|
+
point = [node.parentNode, index + (left ? 1 : 0)];
|
|
221
|
+
else
|
|
222
|
+
left--;
|
|
223
|
+
}
|
|
224
|
+
else
|
|
225
|
+
node.childNodes.forEach((child, index, all) => {
|
|
226
|
+
if (index && (block(child) || block(all[index - 1]))) {
|
|
227
|
+
if (left === 0) {
|
|
228
|
+
point = [node, index];
|
|
229
|
+
return;
|
|
230
|
+
}
|
|
231
|
+
left--;
|
|
232
|
+
}
|
|
233
|
+
if (!(block(child) && child.childNodes.length === 1 && child.firstChild.nodeName === "BR"))
|
|
234
|
+
visit(child);
|
|
235
|
+
});
|
|
236
|
+
};
|
|
237
|
+
visit(editor);
|
|
238
|
+
if (point)
|
|
239
|
+
return point;
|
|
240
|
+
const node = editor.appendChild(document.createTextNode(""));
|
|
241
|
+
return [node, 0];
|
|
184
242
|
}
|
|
185
|
-
|
|
186
243
|
export function replaceEditorRange(editor, start, end, value, parseMentions = true) {
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
244
|
+
const emptyBefore = !editorText(editor);
|
|
245
|
+
setEditorSelection(editor, start, end);
|
|
246
|
+
if (!parseMentions && document.execCommand("insertText", false, String(value))) {
|
|
247
|
+
if (emptyBefore && !String(value).endsWith("\n"))
|
|
248
|
+
trimTrailingBreak(editor);
|
|
249
|
+
editor.dataset.empty = editorText(editor) ? "false" : "true";
|
|
250
|
+
return;
|
|
251
|
+
}
|
|
252
|
+
const marker = `chat-insert-${createRandomId()}`;
|
|
253
|
+
const source = String(value), generatedSpace = parseMentions && source.endsWith(" "), content = generatedSpace ? source.slice(0, -1) : source;
|
|
254
|
+
const token = parseMentions ? /<at>@([^<]+)<\/at>/g : null;
|
|
255
|
+
let html = "", cursor = 0, match;
|
|
256
|
+
while (token && (match = token.exec(content))) {
|
|
257
|
+
html += textHTML(content.slice(cursor, match.index));
|
|
258
|
+
const name = escapeHTML(match[1]), label = escapeHTML(chipLabel(editor, match[1]));
|
|
259
|
+
const chipID = createRandomId();
|
|
260
|
+
trustedChipNames.set(chipID, match[1]);
|
|
261
|
+
html += `<span class="editor-mention" data-mention="${name}" data-chat-chip="${chipID}" data-insert-marker="${marker}" contenteditable="false" role="img" aria-label="Mention ${label}">@${label}</span>`;
|
|
262
|
+
cursor = token.lastIndex;
|
|
263
|
+
}
|
|
264
|
+
html += textHTML(content.slice(cursor));
|
|
265
|
+
if (generatedSpace)
|
|
266
|
+
html += `<span data-chat-auto-separator="true"> </span>`;
|
|
267
|
+
if (document.execCommand("insertHTML", false, html)) {
|
|
268
|
+
editor.querySelectorAll(`[data-insert-marker="${marker}"]`).forEach((chip) => chip.removeAttribute("data-insert-marker"));
|
|
269
|
+
if (emptyBefore && !String(value).endsWith("\n"))
|
|
270
|
+
trimTrailingBreak(editor);
|
|
271
|
+
setCaret(editor, start + source.length);
|
|
272
|
+
pruneTrustedChipNames();
|
|
273
|
+
}
|
|
274
|
+
else {
|
|
275
|
+
const current = editorText(editor), next = current.slice(0, start) + value + current.slice(end);
|
|
276
|
+
if (parseMentions)
|
|
277
|
+
setEditorText(editor, next);
|
|
278
|
+
else
|
|
279
|
+
editor.replaceChildren(document.createTextNode(next));
|
|
280
|
+
if (generatedSpace)
|
|
281
|
+
wrapAutoSeparator(editor, start + source.length - 1);
|
|
282
|
+
setCaret(editor, start + value.length);
|
|
283
|
+
pruneTrustedChipNames();
|
|
284
|
+
}
|
|
191
285
|
editor.dataset.empty = editorText(editor) ? "false" : "true";
|
|
192
|
-
return;
|
|
193
|
-
}
|
|
194
|
-
const marker = `chat-insert-${createRandomId()}`;
|
|
195
|
-
const source = String(value), generatedSpace = parseMentions && source.endsWith(" "), content = generatedSpace ? source.slice(0, -1) : source;
|
|
196
|
-
const token = parseMentions ? /<at>@([^<]+)<\/at>/g : null;
|
|
197
|
-
let html = "", cursor = 0, match;
|
|
198
|
-
while (token && (match = token.exec(content))) {
|
|
199
|
-
html += textHTML(content.slice(cursor, match.index));
|
|
200
|
-
const name = escapeHTML(match[1]), label = escapeHTML(chipLabel(editor, match[1]));
|
|
201
|
-
const chipID = createRandomId(); trustedChipNames.set(chipID, match[1]);
|
|
202
|
-
html += `<span class="editor-mention" data-mention="${name}" data-chat-chip="${chipID}" data-insert-marker="${marker}" contenteditable="false" role="img" aria-label="Mention ${label}">@${label}</span>`;
|
|
203
|
-
cursor = token.lastIndex;
|
|
204
|
-
}
|
|
205
|
-
html += textHTML(content.slice(cursor));
|
|
206
|
-
if (generatedSpace) html += `<span data-chat-auto-separator="true"> </span>`;
|
|
207
|
-
if (document.execCommand("insertHTML", false, html)) {
|
|
208
|
-
editor.querySelectorAll(`[data-insert-marker="${marker}"]`).forEach((chip) => chip.removeAttribute("data-insert-marker"));
|
|
209
|
-
if (emptyBefore && !String(value).endsWith("\n")) trimTrailingBreak(editor);
|
|
210
|
-
setCaret(editor, start + source.length);
|
|
211
|
-
pruneTrustedChipNames();
|
|
212
|
-
} else {
|
|
213
|
-
const current = editorText(editor), next = current.slice(0, start) + value + current.slice(end);
|
|
214
|
-
if (parseMentions) setEditorText(editor, next); else editor.replaceChildren(document.createTextNode(next));
|
|
215
|
-
if (generatedSpace) wrapAutoSeparator(editor, start + source.length - 1);
|
|
216
|
-
setCaret(editor, start + value.length);
|
|
217
|
-
pruneTrustedChipNames();
|
|
218
|
-
}
|
|
219
|
-
editor.dataset.empty = editorText(editor) ? "false" : "true";
|
|
220
286
|
}
|
|
221
|
-
|
|
222
287
|
function trimTrailingBreak(editor) {
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
288
|
+
let last = editor.lastChild;
|
|
289
|
+
if (last?.nodeName === "BR" && last.dataset?.chatTail)
|
|
290
|
+
last = last.previousSibling;
|
|
291
|
+
if (last?.nodeName === "BR")
|
|
292
|
+
last.remove();
|
|
226
293
|
}
|
|
227
|
-
|
|
228
294
|
function sanitizeEditor(editor) {
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
295
|
+
for (const node of editor.querySelectorAll("*")) {
|
|
296
|
+
if (node.tagName === "BR" || node.classList.contains("editor-mention") || node.hasAttribute("data-chat-auto-separator"))
|
|
297
|
+
continue;
|
|
298
|
+
const selection = getSelection(), range = selection?.rangeCount ? selection.getRangeAt(0) : null;
|
|
299
|
+
const [start, end] = editorSelectionOffsets(editor), text = editorText(editor);
|
|
300
|
+
let atEnd = false;
|
|
301
|
+
if (range?.collapsed && editor.contains(range.endContainer)) {
|
|
302
|
+
const tail = document.createRange();
|
|
303
|
+
tail.setStart(range.endContainer, range.endOffset);
|
|
304
|
+
tail.setEnd(editor, editor.childNodes.length);
|
|
305
|
+
atEnd = tail.toString().length === 0;
|
|
306
|
+
}
|
|
307
|
+
setEditorText(editor, text);
|
|
308
|
+
setEditorSelection(editor, atEnd ? text.length : start, atEnd ? text.length : end);
|
|
309
|
+
return;
|
|
239
310
|
}
|
|
240
|
-
setEditorText(editor, text);
|
|
241
|
-
setEditorSelection(editor, atEnd ? text.length : start, atEnd ? text.length : end);
|
|
242
|
-
return;
|
|
243
|
-
}
|
|
244
311
|
}
|
|
245
|
-
|
|
246
312
|
function knownMentions(editor, text) {
|
|
247
|
-
|
|
248
|
-
|
|
313
|
+
// A copied mention only comes back if that name is still in the room.
|
|
314
|
+
return text.replace(/<at>@([^<]+)<\/at>/g, (token, name) => editor.isKnownMention && !editor.isKnownMention(name) ? `@${name}` : token);
|
|
249
315
|
}
|
|
250
316
|
function escapeHTML(text) { return String(text).replace(/[&<>"']/g, (char) => ({ "&": "&", "<": "<", ">": ">", '"': """, "'": "'" })[char]); }
|
|
251
317
|
function textHTML(text) { return escapeHTML(text).replace(/\r\n|\r|\n/g, "<br>"); }
|
|
252
|
-
|
|
253
318
|
function createMention(editor, name) {
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
319
|
+
const chip = document.createElement("span"), label = chipLabel(editor, name);
|
|
320
|
+
chip.className = "editor-mention";
|
|
321
|
+
chip.dataset.mention = name;
|
|
322
|
+
chip.dataset.chatChip = createRandomId();
|
|
323
|
+
trustedChipNames.set(chip.dataset.chatChip, name);
|
|
324
|
+
chip.contentEditable = "false";
|
|
325
|
+
chip.setAttribute("role", "img");
|
|
326
|
+
chip.setAttribute("aria-label", `Mention ${label}`);
|
|
327
|
+
chip.textContent = `@${label}`;
|
|
328
|
+
return chip;
|
|
260
329
|
}
|
|
261
330
|
function createAutoSeparator() {
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
331
|
+
const separator = document.createElement("span");
|
|
332
|
+
separator.dataset.chatAutoSeparator = "true";
|
|
333
|
+
separator.textContent = " ";
|
|
334
|
+
return separator;
|
|
265
335
|
}
|
|
266
336
|
function wrapAutoSeparator(editor, offset) {
|
|
267
|
-
|
|
268
|
-
|
|
337
|
+
const start = editorPoint(editor, offset), end = editorPoint(editor, offset + 1), range = document.createRange();
|
|
338
|
+
range.setStart(start[0], start[1]);
|
|
339
|
+
range.setEnd(end[0], end[1]);
|
|
340
|
+
range.deleteContents();
|
|
341
|
+
range.insertNode(createAutoSeparator());
|
|
269
342
|
}
|
|
270
|
-
|
|
271
343
|
function deleteMentionAtCaret(editor, event) {
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
344
|
+
if (event.isComposing)
|
|
345
|
+
return;
|
|
346
|
+
const backward = event.inputType === "deleteContentBackward" || (event.key === "Backspace" && event.keyCode !== 229);
|
|
347
|
+
if (!backward)
|
|
348
|
+
return;
|
|
349
|
+
const [caret, end] = editorSelectionOffsets(editor);
|
|
350
|
+
if (caret !== end)
|
|
351
|
+
return;
|
|
352
|
+
const chips = [...editor.querySelectorAll(".editor-mention")];
|
|
353
|
+
const separator = [...editor.querySelectorAll("[data-chat-auto-separator]")].find((node) => {
|
|
354
|
+
const start = lengthBefore(editor, node, 0);
|
|
355
|
+
return caret === start + 1 && node.previousElementSibling?.classList.contains("editor-mention");
|
|
356
|
+
});
|
|
357
|
+
const mention = separator?.previousElementSibling || chips.find((node) => {
|
|
358
|
+
const start = lengthBefore(editor, node, 0);
|
|
359
|
+
return caret > start && caret <= start + `<at>@${mentionName(node)}</at>`.length;
|
|
360
|
+
});
|
|
361
|
+
if (!mention)
|
|
362
|
+
return;
|
|
363
|
+
const start = lengthBefore(editor, mention, 0);
|
|
364
|
+
const autoSeparator = mention.nextElementSibling?.dataset.chatAutoSeparator ? mention.nextElementSibling : null;
|
|
365
|
+
event.preventDefault();
|
|
366
|
+
mention.remove();
|
|
367
|
+
autoSeparator?.remove();
|
|
368
|
+
setCaret(editor, start);
|
|
369
|
+
editor.dispatchEvent(new InputEvent("input", { bubbles: true, inputType: "deleteContentBackward" }));
|
|
291
370
|
}
|
|
292
|
-
|
|
293
371
|
function pruneTrustedChipNames() {
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
372
|
+
const live = new Set([...document.querySelectorAll("[data-chat-chip]")].map((node) => node.dataset.chatChip));
|
|
373
|
+
const stale = [...trustedChipNames.keys()].filter((id) => !live.has(id));
|
|
374
|
+
// shortcut: retain 2,048 detached IDs for undo history; raise if longer histories are required.
|
|
375
|
+
stale.slice(0, Math.max(0, stale.length - 2048)).forEach((id) => trustedChipNames.delete(id));
|
|
298
376
|
}
|
|
299
|
-
|
|
300
377
|
export function installEditor(editor) {
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
const
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
378
|
+
Object.defineProperty(editor, "value", {
|
|
379
|
+
get: () => editorText(editor), set: (value) => setEditorText(editor, value),
|
|
380
|
+
});
|
|
381
|
+
Object.defineProperty(editor, "draftValue", {
|
|
382
|
+
get: () => editorDraftText(editor), set: (value) => setEditorText(editor, value, true),
|
|
383
|
+
});
|
|
384
|
+
Object.defineProperties(editor, {
|
|
385
|
+
selectionStart: { get: () => editorSelectionOffsets(editor)[0] },
|
|
386
|
+
selectionEnd: { get: () => editorSelectionOffsets(editor)[1] },
|
|
387
|
+
});
|
|
388
|
+
editor.getSelectionOffsets = () => editorSelectionOffsets(editor);
|
|
389
|
+
editor.setCaret = (offset) => setCaret(editor, offset);
|
|
390
|
+
editor.moveCaretToLineEdge = (end) => {
|
|
391
|
+
const [start, finish] = editorSelectionOffsets(editor);
|
|
392
|
+
const text = editorText(editor);
|
|
393
|
+
const caret = end ? finish : start;
|
|
394
|
+
const next = text.indexOf("\n", caret);
|
|
395
|
+
// A native Home next to a non-editable chip can land inside the chip, where
|
|
396
|
+
// the caret is invisible. Re-seat all line-edge moves in a real text node.
|
|
397
|
+
const lineStart = end ? caret : text.lastIndexOf("\n", caret - 1) + 1;
|
|
398
|
+
const target = end ? (next === -1 ? text.length : next) : lineStart;
|
|
399
|
+
// A caret before a leading mention chip can render invisibly in Chromium.
|
|
400
|
+
// Home starts at the first editable character after that chip instead.
|
|
401
|
+
const leading = !end ? text.slice(lineStart).match(/^(?:<at>@[^<]+<\/at>)+/) : null;
|
|
402
|
+
setCaret(editor, leading ? lineStart + leading[0].length : target);
|
|
403
|
+
};
|
|
404
|
+
editor.setSelectionRange = (start, end = start) => setEditorSelection(editor, start, end);
|
|
405
|
+
editor.replaceTextRange = (start, end, value) => replaceEditorRange(editor, start, end, value);
|
|
406
|
+
editor.addEventListener("input", pruneTrustedChipNames);
|
|
407
|
+
editor.addEventListener("keydown", (event) => deleteMentionAtCaret(editor, event));
|
|
408
|
+
editor.addEventListener("beforeinput", (event) => deleteMentionAtCaret(editor, event));
|
|
409
|
+
editor.addEventListener("pointerdown", (event) => {
|
|
410
|
+
const chip = event.target.closest?.(".editor-mention");
|
|
411
|
+
if (!chip || !editor.contains(chip))
|
|
412
|
+
return;
|
|
413
|
+
event.preventDefault();
|
|
414
|
+
editor.focus({ preventScroll: true });
|
|
415
|
+
// A tap on a chip is usually followed by Backspace/Delete. Place the caret
|
|
416
|
+
// after it so a narrow or rounded chip cannot trap the caret before it.
|
|
417
|
+
setCaret(editor, lengthBefore(editor, chip, 0) + `<at>@${mentionName(chip)}</at>`.length);
|
|
418
|
+
});
|
|
419
|
+
const copySelection = (event) => {
|
|
420
|
+
const [start, end] = editorSelectionOffsets(editor);
|
|
421
|
+
if (start === end)
|
|
422
|
+
return "";
|
|
423
|
+
const text = editorText(editor).slice(start, end);
|
|
424
|
+
if (!text)
|
|
425
|
+
return "";
|
|
426
|
+
// Copy mentions in their token form so a paste rebuilds the same chips.
|
|
427
|
+
event.preventDefault();
|
|
428
|
+
event.clipboardData?.setData("text/plain", text);
|
|
429
|
+
if (/<at>@[^<]+<\/at>/.test(text))
|
|
430
|
+
event.clipboardData?.setData("text/html", `<div data-chatroom-mentions>${escapeHTML(text)}</div>`);
|
|
431
|
+
return text;
|
|
432
|
+
};
|
|
433
|
+
editor.addEventListener("copy", (event) => { copySelection(event); });
|
|
434
|
+
editor.addEventListener("cut", (event) => {
|
|
435
|
+
const [start, end] = editorSelectionOffsets(editor);
|
|
436
|
+
if (!copySelection(event))
|
|
437
|
+
return;
|
|
438
|
+
// The native cut would overwrite the token clipboard; delete the range here.
|
|
439
|
+
replaceEditorRange(editor, start, end, "", false);
|
|
440
|
+
sanitizeEditor(editor);
|
|
441
|
+
editor.dispatchEvent(new InputEvent("input", { bubbles: true, inputType: "deleteByCut" }));
|
|
442
|
+
});
|
|
443
|
+
editor.addEventListener("paste", (event) => {
|
|
444
|
+
if (event.defaultPrevented || event.clipboardData?.files.length)
|
|
445
|
+
return;
|
|
446
|
+
event.preventDefault();
|
|
447
|
+
const [start, end] = editorSelectionOffsets(editor);
|
|
448
|
+
const text = event.clipboardData?.getData("text/plain") || "";
|
|
449
|
+
// Rebuild chips only for copies this app marked; external text stays literal
|
|
450
|
+
// and keeps the native insert path, which handles newlines correctly.
|
|
451
|
+
const marked = (event.clipboardData?.getData("text/html") || "").includes("data-chatroom-mentions");
|
|
452
|
+
const value = marked ? knownMentions(editor, text) : text;
|
|
453
|
+
replaceEditorRange(editor, start, end, value, marked && /<at>@[^<]+<\/at>/.test(value));
|
|
454
|
+
sanitizeEditor(editor);
|
|
455
|
+
editor.dispatchEvent(new InputEvent("input", { bubbles: true, inputType: "insertFromPaste" }));
|
|
456
|
+
});
|
|
457
|
+
editor.addEventListener("drop", (event) => {
|
|
458
|
+
if (event.defaultPrevented || event.dataTransfer?.files.length)
|
|
459
|
+
return;
|
|
460
|
+
const html = event.dataTransfer?.getData("text/html") || "";
|
|
461
|
+
const text = event.dataTransfer?.getData("text/plain") || new DOMParser().parseFromString(html, "text/html").body.textContent || "";
|
|
462
|
+
if (!text)
|
|
463
|
+
return;
|
|
464
|
+
event.preventDefault();
|
|
465
|
+
const range = document.caretRangeFromPoint?.(event.clientX, event.clientY), selection = getSelection();
|
|
466
|
+
if (range && editor.contains(range.startContainer)) {
|
|
467
|
+
selection.removeAllRanges();
|
|
468
|
+
selection.addRange(range);
|
|
469
|
+
}
|
|
470
|
+
const [start, end] = editorSelectionOffsets(editor);
|
|
471
|
+
const marked = html.includes("data-chatroom-mentions");
|
|
472
|
+
const value = marked ? knownMentions(editor, text) : text;
|
|
473
|
+
replaceEditorRange(editor, start, end, value, marked && /<at>@[^<]+<\/at>/.test(value));
|
|
474
|
+
sanitizeEditor(editor);
|
|
475
|
+
editor.dispatchEvent(new InputEvent("input", { bubbles: true, inputType: "insertFromDrop" }));
|
|
476
|
+
});
|
|
389
477
|
}
|