@alexsun-top/opencode-chat-room 0.7.2 → 0.7.4
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 +11 -4
- package/README.zh-CN.md +11 -4
- package/cli.ts +26 -6
- package/dist/cli.js +7804 -0
- package/package.json +19 -6
- package/plugin.ts +7 -4
- package/server/admin-auth.ts +1 -1
- package/server/admin-routes.ts +2 -2
- package/server/admin-state.ts +2 -1
- package/server/assets/chat/admin.js +43 -3
- package/server/assets/chat/announcements.js +5 -3
- package/server/assets/chat/confirm-dialog.js +12 -2
- package/server/assets/chat/create-room-dialog.js +5 -3
- package/server/assets/chat/events.js +32 -7
- package/server/assets/chat/files.js +368 -0
- package/server/assets/chat/index.js +6 -0
- package/server/assets/chat/locales/de.js +17 -1
- package/server/assets/chat/locales/es.js +17 -1
- package/server/assets/chat/locales/fr.js +17 -1
- package/server/assets/chat/locales/it.js +17 -1
- package/server/assets/chat/locales/ja.js +17 -1
- package/server/assets/chat/locales/ko.js +17 -1
- package/server/assets/chat/markdown.js +10 -3
- package/server/assets/chat/message-polling.js +61 -6
- package/server/assets/chat/message-window.js +28 -0
- package/server/assets/chat/messages.js +29 -4
- package/server/assets/chat/palette.js +384 -0
- package/server/assets/chat/rooms.js +8 -3
- package/server/assets/chat/search.js +137 -0
- package/server/assets/chat/settings.js +11 -5
- package/server/assets/chat/state.js +34 -2
- package/server/assets/code-highlight.css +2 -5
- package/server/assets/edit-room.css +77 -0
- package/server/assets/files.css +237 -0
- package/server/assets/palette.css +277 -0
- package/server/assets/responsive.css +3 -2
- package/server/assets/room-create.css +42 -43
- package/server/assets/settings.css +6 -3
- package/server/chat-server.ts +46 -29
- package/server/chat.html +129 -33
- package/server/http-server.ts +182 -0
- package/server/join-requests.ts +5 -4
- package/server/manifest.webmanifest +1 -1
- package/server/member-routes.ts +3 -3
- package/server/message-recall-routes.ts +1 -1
- package/server/newrooms.ts +4 -4
- package/server/resource-routes.ts +19 -4
- package/server/room-join.ts +8 -3
- package/server/room-payload.ts +1 -1
- package/server/sw.js +3 -0
- package/server/wake.ts +1 -1
- package/skills/chatroom/SKILL.md +1 -1
- package/src/README.md +4 -2
- package/src/config/file-config.ts +294 -0
- package/src/rooms/resource-read.ts +2 -1
- package/src/rooms/resource-upload.ts +6 -3
- package/src/rooms/resources.ts +7 -2
- package/src/runtime/v2-plugin.ts +2 -2
- package/src/runtime/watch.ts +13 -8
- package/src/state/receiver-state.ts +3 -5
- package/src/state/sqlite-state.ts +11 -7
- package/src/state/sqlite.ts +80 -0
- package/src/state/store.ts +1 -0
- package/src/tui/app.mjs +417 -0
- package/src/tui/config.mjs +194 -0
- package/src/tui/http.mjs +103 -0
- package/src/tui/main.mjs +176 -0
- package/src/tui/messages.mjs +67 -0
- package/src/tui/palette-app.mjs +265 -0
- package/src/tui/palette.mjs +101 -0
- package/src/tui/session.mjs +40 -0
- package/src/tui/ui-content.mjs +426 -0
- package/src/tui/ui-theme.mjs +60 -0
- package/src/tui/ui-view.mjs +452 -0
- package/src/tui/ui.mjs +262 -0
- package/src/tests/announcement-board.ts +0 -186
|
@@ -0,0 +1,384 @@
|
|
|
1
|
+
// Alt+Space opens one keyboard-first surface: message search plus common room commands.
|
|
2
|
+
const ICONS = {
|
|
3
|
+
refresh: '<svg aria-hidden="true" viewBox="0 0 24 24"><path d="M20 7v5h-5M4 17v-5h5"/><path d="M5.6 9a7 7 0 0 1 11.8-2L20 12M4 12l2.6 5a7 7 0 0 0 11.8-2"/></svg>',
|
|
4
|
+
plus: '<svg aria-hidden="true" viewBox="0 0 24 24"><path d="M12 5v14M5 12h14"/></svg>',
|
|
5
|
+
gear: '<svg aria-hidden="true" viewBox="0 0 24 24"><path d="M4 7h9M17 7h3M4 17h3M11 17h9"/><circle cx="15" cy="7" r="2"/><circle cx="9" cy="17" r="2"/></svg>',
|
|
6
|
+
members: '<svg aria-hidden="true" viewBox="0 0 24 24"><circle cx="9" cy="8" r="3"/><path d="M3.5 19a5.5 5.5 0 0 1 11 0M16 5.5a3 3 0 0 1 0 5.8M17 14a4.5 4.5 0 0 1 3.5 4.4V19h-3"/></svg>',
|
|
7
|
+
questions: '<svg aria-hidden="true" viewBox="0 0 24 24"><path d="M9.5 9a2.5 2.5 0 1 1 4.6 1.4c-.7.9-2.1 1.3-2.1 2.8M12 17.5h.01"/><circle cx="12" cy="12" r="9"/></svg>',
|
|
8
|
+
announcements: '<svg aria-hidden="true" viewBox="0 0 24 24"><path d="M5 4h14v16H5zM8 8h8M8 12h8M8 16h5"/></svg>',
|
|
9
|
+
reminders: '<svg aria-hidden="true" viewBox="0 0 24 24"><path d="M18 8a6 6 0 1 0-12 0c0 7-3 7-3 9h18c0-2-3-2-3-9"/><path d="M10 21a2 2 0 0 0 4 0"/></svg>',
|
|
10
|
+
leave: '<svg aria-hidden="true" viewBox="0 0 24 24"><path d="M10 4H5v16h5M15 8l4 4-4 4M19 12H9"/></svg>',
|
|
11
|
+
latest: '<svg aria-hidden="true" viewBox="0 0 24 24"><path d="M12 4v14M7 13l5 5 5-5M5 21h14"/></svg>',
|
|
12
|
+
command: '<svg aria-hidden="true" viewBox="0 0 24 24"><path d="M5 12h14M13 6l6 6-6 6"/></svg>',
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
export function initPalette(app) {
|
|
16
|
+
const dialog = app.$("#paletteDialog");
|
|
17
|
+
if (!dialog) return;
|
|
18
|
+
const input = app.$("#paletteInput"), list = app.$("#paletteList"), status = app.$("#paletteStatus");
|
|
19
|
+
const scopeButton = app.$("#paletteScope"), scopeLabel = app.$("#paletteScopeLabel"), modeLabel = app.$("#paletteMode");
|
|
20
|
+
let scope = "room", items = [], selected = -1, sequence = 0, searchTimer = 0, suppressComposerFocus = false, hitTimer = 0;
|
|
21
|
+
const t = (key, values) => app.t(key, values);
|
|
22
|
+
const room = () => app.state.room;
|
|
23
|
+
|
|
24
|
+
// The eyebrow names the real key on this platform, matching the shortcut sheet.
|
|
25
|
+
const combo = app.$("#paletteCombo");
|
|
26
|
+
if (combo && /Mac|iPhone|iPad|iPod/.test(navigator.platform || navigator.userAgent)) combo.textContent = "Option + Space";
|
|
27
|
+
|
|
28
|
+
const commands = () => {
|
|
29
|
+
const available = [
|
|
30
|
+
{ id: "refresh", label: t("refreshRooms"), icon: "refresh", keywords: "reload rooms", run: () => void app.refreshRoomList?.() },
|
|
31
|
+
{ id: "create", label: t("createRoom"), icon: "plus", keywords: "new room", focusComposer: false,
|
|
32
|
+
run: () => room() ? app.openCreateRoomDialog?.() : app.$("#inRoomName")?.focus({ preventScroll: true }) },
|
|
33
|
+
{ id: "settings", label: t("settings"), icon: "gear", keywords: "preferences theme language", focusComposer: false, run: () => app.openSettings?.() },
|
|
34
|
+
];
|
|
35
|
+
if (room()) available.push(
|
|
36
|
+
{ id: "members", label: t("members"), icon: "members", keywords: "people roster", focusComposer: false, run: () => app.$("#btnMembers")?.click() },
|
|
37
|
+
{ id: "questions", label: t("questions"), icon: "questions", keywords: "answers", focusComposer: false, run: () => app.$("#btnQuestions")?.click() },
|
|
38
|
+
{ id: "announcements", label: t("announcements"), icon: "announcements", keywords: "notice board", focusComposer: false, run: () => app.$("#btnAnnouncements")?.click() },
|
|
39
|
+
{ id: "reminders", label: t("reminders"), icon: "reminders", keywords: "schedule alarm", focusComposer: false, run: () => app.$("#btnReminders")?.click() },
|
|
40
|
+
{ id: "leave", label: t("paletteLeaveRoom"), icon: "leave", keywords: "exit quit", focusComposer: false, run: () => app.leaveRoom?.(false) },
|
|
41
|
+
{ id: "latest", label: t("paletteJumpLatest"), icon: "latest", keywords: "bottom newest", run: () => app.jumpToBottom?.() },
|
|
42
|
+
);
|
|
43
|
+
return available;
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
const scopeText = () => t(scope === "all" ? "paletteScopeAll" : "paletteScopeRoom");
|
|
47
|
+
const syncScope = () => {
|
|
48
|
+
scopeLabel.textContent = scopeText();
|
|
49
|
+
scopeButton.dataset.scope = scope;
|
|
50
|
+
scopeButton.title = t("paletteScopeHint");
|
|
51
|
+
scopeButton.setAttribute("aria-label", `${scopeText()}. ${t("paletteScopeHint")}`);
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
const appendSnippet = (node, text, needle) => {
|
|
55
|
+
const flat = String(text ?? "").replace(/\s+/g, " ").trim();
|
|
56
|
+
if (!flat) { node.textContent = ""; return; }
|
|
57
|
+
const lower = flat.toLowerCase(), match = needle ? lower.indexOf(needle.toLowerCase()) : -1;
|
|
58
|
+
let start = 0, end = flat.length;
|
|
59
|
+
if (flat.length > 180) {
|
|
60
|
+
start = match > 60 ? match - 60 : 0;
|
|
61
|
+
end = Math.min(flat.length, start + 180);
|
|
62
|
+
}
|
|
63
|
+
const body = flat.slice(start, end);
|
|
64
|
+
if (start > 0) node.append("…");
|
|
65
|
+
if (match >= start && match < end) {
|
|
66
|
+
const offset = match - start;
|
|
67
|
+
node.append(body.slice(0, offset));
|
|
68
|
+
const mark = document.createElement("mark");
|
|
69
|
+
mark.textContent = body.slice(offset, offset + needle.length);
|
|
70
|
+
node.append(mark, body.slice(offset + needle.length));
|
|
71
|
+
} else node.append(body);
|
|
72
|
+
if (end < flat.length) node.append("…");
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
const syncSelection = () => {
|
|
76
|
+
const rows = [...list.querySelectorAll(".palette-row")];
|
|
77
|
+
rows.forEach((row, index) => row.setAttribute("aria-selected", String(index === selected)));
|
|
78
|
+
input.setAttribute("aria-expanded", String(items.length > 0));
|
|
79
|
+
if (items.length && selected >= 0) input.setAttribute("aria-activedescendant", `palette-option-${selected}`);
|
|
80
|
+
else input.removeAttribute("aria-activedescendant");
|
|
81
|
+
rows[selected]?.scrollIntoView({ block: "nearest" });
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
const render = (next, emptyKey) => {
|
|
85
|
+
items = next;
|
|
86
|
+
selected = next.length ? 0 : -1;
|
|
87
|
+
list.textContent = "";
|
|
88
|
+
next.forEach((item, index) => {
|
|
89
|
+
const row = document.createElement("li");
|
|
90
|
+
row.id = `palette-option-${index}`;
|
|
91
|
+
row.className = `palette-row ${item.kind === "command" ? "palette-command" : "palette-result"}`;
|
|
92
|
+
row.setAttribute("role", "option");
|
|
93
|
+
row.style.setProperty("--palette-i", String(Math.min(index, 9)));
|
|
94
|
+
if (item.kind === "command") {
|
|
95
|
+
const icon = document.createElement("span");
|
|
96
|
+
icon.className = "palette-icon";
|
|
97
|
+
icon.innerHTML = ICONS[item.icon] || ICONS.command;
|
|
98
|
+
const label = document.createElement("span");
|
|
99
|
+
label.className = "palette-label";
|
|
100
|
+
label.textContent = item.label;
|
|
101
|
+
const enter = document.createElement("kbd");
|
|
102
|
+
enter.className = "palette-enter";
|
|
103
|
+
enter.setAttribute("aria-hidden", "true");
|
|
104
|
+
enter.textContent = "↵";
|
|
105
|
+
row.append(icon, label, enter);
|
|
106
|
+
} else {
|
|
107
|
+
const top = document.createElement("span");
|
|
108
|
+
top.className = "palette-result-top";
|
|
109
|
+
const roomName = document.createElement("span");
|
|
110
|
+
roomName.className = "palette-room";
|
|
111
|
+
roomName.textContent = item.roomName || app.state.roomNames.get(item.roomId) || "";
|
|
112
|
+
const sender = document.createElement("span");
|
|
113
|
+
sender.className = "palette-sender";
|
|
114
|
+
sender.textContent = item.sender || "";
|
|
115
|
+
const time = document.createElement("time");
|
|
116
|
+
time.className = "palette-time";
|
|
117
|
+
if (item.createdAt) time.dateTime = new Date(item.createdAt).toISOString();
|
|
118
|
+
time.textContent = item.createdAt ? app.fmtTime(item.createdAt) : "";
|
|
119
|
+
top.append(roomName, sender, time);
|
|
120
|
+
const snippet = document.createElement("span");
|
|
121
|
+
snippet.className = "palette-snippet";
|
|
122
|
+
appendSnippet(snippet, item.text, input.value.trim());
|
|
123
|
+
row.append(top, snippet);
|
|
124
|
+
}
|
|
125
|
+
row.addEventListener("click", () => { selected = index; syncSelection(); activate(item); });
|
|
126
|
+
list.append(row);
|
|
127
|
+
});
|
|
128
|
+
if (!next.length && emptyKey) {
|
|
129
|
+
const empty = document.createElement("li");
|
|
130
|
+
empty.className = "palette-empty";
|
|
131
|
+
empty.setAttribute("role", "presentation");
|
|
132
|
+
empty.textContent = t(emptyKey);
|
|
133
|
+
list.append(empty);
|
|
134
|
+
}
|
|
135
|
+
syncSelection();
|
|
136
|
+
};
|
|
137
|
+
|
|
138
|
+
const showCommands = (fragment = "") => {
|
|
139
|
+
clearTimeout(searchTimer);
|
|
140
|
+
sequence++;
|
|
141
|
+
const needle = fragment.trim().toLowerCase();
|
|
142
|
+
const available = commands()
|
|
143
|
+
.filter((command) => !needle || `${command.label} ${command.keywords || ""}`.toLowerCase().includes(needle))
|
|
144
|
+
.map((command) => ({ kind: "command", ...command }));
|
|
145
|
+
dialog.dataset.mode = "commands";
|
|
146
|
+
dialog.dataset.state = "ready";
|
|
147
|
+
modeLabel.textContent = t("paletteCommandMode");
|
|
148
|
+
list.setAttribute("aria-busy", "false");
|
|
149
|
+
render(available, "paletteNoCommands");
|
|
150
|
+
status.textContent = needle
|
|
151
|
+
? available.length ? t("paletteResults", { count: available.length }) : t("paletteNoCommands")
|
|
152
|
+
: t("paletteCommandsHint");
|
|
153
|
+
};
|
|
154
|
+
|
|
155
|
+
const senderName = (source, message) => {
|
|
156
|
+
if (source.kind === "direct") return source.participants?.find((person) => person.id === message.senderId)?.name || message.senderId;
|
|
157
|
+
return message.senderId;
|
|
158
|
+
};
|
|
159
|
+
|
|
160
|
+
const localSearch = (text, searchScope) => {
|
|
161
|
+
const needle = text.toLowerCase();
|
|
162
|
+
const entries = searchScope === "all"
|
|
163
|
+
? [...app.state.rooms.values()]
|
|
164
|
+
: [app.state.rooms.get(app.state.room?.id)].filter(Boolean);
|
|
165
|
+
const hits = [];
|
|
166
|
+
for (const entry of entries) {
|
|
167
|
+
const source = entry.room;
|
|
168
|
+
if (!source?.messages) continue;
|
|
169
|
+
for (const message of source.messages) {
|
|
170
|
+
if (message.recalledAt || message.pending || message.senderId === "system") continue;
|
|
171
|
+
const body = String(message.text ?? "");
|
|
172
|
+
if (!body.toLowerCase().includes(needle)) continue;
|
|
173
|
+
const hit = {
|
|
174
|
+
roomId: source.id,
|
|
175
|
+
roomName: source.name || app.state.roomNames.get(source.id) || "",
|
|
176
|
+
messageId: message.id,
|
|
177
|
+
sender: senderName(source, message),
|
|
178
|
+
createdAt: message.createdAt,
|
|
179
|
+
text: body,
|
|
180
|
+
};
|
|
181
|
+
hits.push({ kind: "hit", raw: hit, ...hit });
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
hits.sort((a, b) => b.createdAt - a.createdAt);
|
|
185
|
+
return hits.slice(0, 80);
|
|
186
|
+
};
|
|
187
|
+
|
|
188
|
+
const normalizeHits = (raw) => raw.map((item) => {
|
|
189
|
+
if (!item || typeof item !== "object") return null;
|
|
190
|
+
const message = item.message || {};
|
|
191
|
+
const roomId = String(item.roomId ?? item.roomID ?? item.room?.id ?? "");
|
|
192
|
+
const messageId = String(item.messageId ?? item.messageID ?? message.id ?? item.id ?? "");
|
|
193
|
+
if (!messageId) return null;
|
|
194
|
+
return {
|
|
195
|
+
kind: "hit",
|
|
196
|
+
raw: item,
|
|
197
|
+
roomId,
|
|
198
|
+
messageId,
|
|
199
|
+
roomName: String(item.roomName ?? item.room?.name ?? app.state.roomNames.get(roomId) ?? app.state.rooms.get(roomId)?.room?.name ?? ""),
|
|
200
|
+
sender: String(item.senderName ?? item.sender ?? item.senderId ?? message.senderId ?? ""),
|
|
201
|
+
createdAt: Number(item.createdAt ?? message.createdAt ?? item.ts ?? 0) || 0,
|
|
202
|
+
text: String(item.text ?? item.snippet ?? item.preview ?? message.text ?? ""),
|
|
203
|
+
};
|
|
204
|
+
}).filter(Boolean);
|
|
205
|
+
|
|
206
|
+
const failSearch = () => {
|
|
207
|
+
dialog.dataset.state = "ready";
|
|
208
|
+
list.setAttribute("aria-busy", "false");
|
|
209
|
+
render([], null);
|
|
210
|
+
status.textContent = t("paletteSearchFailed");
|
|
211
|
+
};
|
|
212
|
+
|
|
213
|
+
const executeSearch = async (text, token) => {
|
|
214
|
+
let result;
|
|
215
|
+
try {
|
|
216
|
+
result = typeof app.searchMessages === "function" ? await app.searchMessages(text, scope) : localSearch(text, scope);
|
|
217
|
+
} catch {
|
|
218
|
+
if (token === sequence) failSearch();
|
|
219
|
+
return;
|
|
220
|
+
}
|
|
221
|
+
if (token !== sequence) return;
|
|
222
|
+
const raw = Array.isArray(result) ? result
|
|
223
|
+
: Array.isArray(result?.hits) ? result.hits
|
|
224
|
+
: Array.isArray(result?.results) ? result.results
|
|
225
|
+
: Array.isArray(result?.items) ? result.items : [];
|
|
226
|
+
const hits = normalizeHits(raw).slice(0, 80);
|
|
227
|
+
dialog.dataset.state = "ready";
|
|
228
|
+
list.setAttribute("aria-busy", "false");
|
|
229
|
+
render(hits, "paletteNoResults");
|
|
230
|
+
status.textContent = hits.length ? t("paletteResults", { count: hits.length }) : t("paletteNoResults");
|
|
231
|
+
};
|
|
232
|
+
|
|
233
|
+
const scheduleSearch = () => {
|
|
234
|
+
clearTimeout(searchTimer);
|
|
235
|
+
const token = ++sequence;
|
|
236
|
+
const value = input.value;
|
|
237
|
+
if (!value.trim()) { showCommands(""); return; }
|
|
238
|
+
if (value.startsWith(">")) { showCommands(value.slice(1)); return; }
|
|
239
|
+
dialog.dataset.mode = "search";
|
|
240
|
+
dialog.dataset.state = "searching";
|
|
241
|
+
modeLabel.textContent = t("paletteSearchMode");
|
|
242
|
+
list.setAttribute("aria-busy", "true");
|
|
243
|
+
render([], null);
|
|
244
|
+
status.textContent = t("paletteSearching");
|
|
245
|
+
const text = value.trim();
|
|
246
|
+
searchTimer = setTimeout(() => { void executeSearch(text, token); }, 120);
|
|
247
|
+
};
|
|
248
|
+
|
|
249
|
+
const highlightHit = (item) => {
|
|
250
|
+
if (!item?.messageId) return;
|
|
251
|
+
clearTimeout(hitTimer);
|
|
252
|
+
document.querySelectorAll("#msgList .palette-hit").forEach((row) => row.classList.remove("palette-hit"));
|
|
253
|
+
let attempts = 0;
|
|
254
|
+
const reveal = () => {
|
|
255
|
+
const row = document.querySelector(`#msgList [data-msg-id="${CSS.escape(item.messageId)}"]`);
|
|
256
|
+
if (row) {
|
|
257
|
+
row.scrollIntoView({ block: "center", behavior: matchMedia("(prefers-reduced-motion: reduce)").matches ? "auto" : "smooth" });
|
|
258
|
+
row.classList.add("palette-hit");
|
|
259
|
+
hitTimer = setTimeout(() => row.classList.remove("palette-hit"), 4000);
|
|
260
|
+
return;
|
|
261
|
+
}
|
|
262
|
+
if (++attempts < 50) setTimeout(reveal, 50);
|
|
263
|
+
};
|
|
264
|
+
reveal();
|
|
265
|
+
};
|
|
266
|
+
|
|
267
|
+
const openLocalHit = (item) => {
|
|
268
|
+
if (item.roomId && app.state.room?.id !== item.roomId) app.activateRoom?.(item.roomId);
|
|
269
|
+
};
|
|
270
|
+
|
|
271
|
+
const activate = (item) => {
|
|
272
|
+
if (!item) return;
|
|
273
|
+
if (item.kind === "command") {
|
|
274
|
+
closePalette({ focusComposer: item.focusComposer !== false });
|
|
275
|
+
try { item.run(); } catch { /* The command owns its own error surface. */ }
|
|
276
|
+
return;
|
|
277
|
+
}
|
|
278
|
+
closePalette();
|
|
279
|
+
const raw = item.raw || item;
|
|
280
|
+
if (typeof app.openSearchHit === "function") {
|
|
281
|
+
Promise.resolve().then(() => app.openSearchHit(raw)).catch(() => openLocalHit(item)).finally(() => highlightHit(item));
|
|
282
|
+
} else { openLocalHit(item); highlightHit(item); }
|
|
283
|
+
};
|
|
284
|
+
|
|
285
|
+
const moveSelection = (step) => {
|
|
286
|
+
if (!items.length) return;
|
|
287
|
+
selected = selected < 0 ? 0 : (selected + step + items.length) % items.length;
|
|
288
|
+
syncSelection();
|
|
289
|
+
};
|
|
290
|
+
|
|
291
|
+
const toggleScope = () => {
|
|
292
|
+
scope = scope === "room" ? "all" : "room";
|
|
293
|
+
syncScope();
|
|
294
|
+
const value = input.value;
|
|
295
|
+
if (!value.trim()) showCommands("");
|
|
296
|
+
else if (value.startsWith(">")) showCommands(value.slice(1));
|
|
297
|
+
else scheduleSearch();
|
|
298
|
+
};
|
|
299
|
+
|
|
300
|
+
const closePalette = (options = {}) => {
|
|
301
|
+
if (!dialog.open) return;
|
|
302
|
+
suppressComposerFocus = options.focusComposer === false;
|
|
303
|
+
dialog.close();
|
|
304
|
+
};
|
|
305
|
+
|
|
306
|
+
const openPalette = () => {
|
|
307
|
+
if (dialog.open) { input.focus(); return; }
|
|
308
|
+
app.closePanels?.();
|
|
309
|
+
app.closeMention?.();
|
|
310
|
+
input.value = "";
|
|
311
|
+
scope = "room";
|
|
312
|
+
syncScope();
|
|
313
|
+
showCommands("");
|
|
314
|
+
dialog.showModal();
|
|
315
|
+
input.focus();
|
|
316
|
+
};
|
|
317
|
+
app.openPalette = openPalette;
|
|
318
|
+
|
|
319
|
+
input.addEventListener("input", scheduleSearch);
|
|
320
|
+
scopeButton.addEventListener("click", () => { toggleScope(); input.focus(); });
|
|
321
|
+
app.$("#btnClosePalette")?.addEventListener("click", () => closePalette());
|
|
322
|
+
dialog.addEventListener("click", (event) => { if (event.target === dialog) closePalette(); });
|
|
323
|
+
dialog.addEventListener("cancel", (event) => { event.preventDefault(); closePalette(); });
|
|
324
|
+
dialog.addEventListener("close", () => {
|
|
325
|
+
clearTimeout(searchTimer);
|
|
326
|
+
sequence++;
|
|
327
|
+
list.setAttribute("aria-busy", "false");
|
|
328
|
+
const suppressed = suppressComposerFocus;
|
|
329
|
+
suppressComposerFocus = false;
|
|
330
|
+
if (suppressed) return;
|
|
331
|
+
requestAnimationFrame(() => {
|
|
332
|
+
// A stacked dialog keeps its own focus; otherwise the composer owns the page.
|
|
333
|
+
if (document.querySelector("dialog[open]")) return;
|
|
334
|
+
const composer = app.$("#msgInput");
|
|
335
|
+
if (app.state.room && !app.$("#chatView")?.hidden && composer?.contentEditable === "true") composer.focus({ preventScroll: true });
|
|
336
|
+
else if (!app.state.room) app.$("#inRoomId")?.focus({ preventScroll: true });
|
|
337
|
+
});
|
|
338
|
+
});
|
|
339
|
+
|
|
340
|
+
dialog.addEventListener("keydown", (event) => {
|
|
341
|
+
if (event.isComposing) return;
|
|
342
|
+
if (event.key === "Tab") {
|
|
343
|
+
event.preventDefault();
|
|
344
|
+
toggleScope();
|
|
345
|
+
return;
|
|
346
|
+
}
|
|
347
|
+
if (event.key === "ArrowDown" || event.key === "ArrowUp") {
|
|
348
|
+
event.preventDefault();
|
|
349
|
+
moveSelection(event.key === "ArrowDown" ? 1 : -1);
|
|
350
|
+
return;
|
|
351
|
+
}
|
|
352
|
+
if (event.key === "Home" || event.key === "End") {
|
|
353
|
+
if (!items.length) return;
|
|
354
|
+
event.preventDefault();
|
|
355
|
+
selected = event.key === "Home" ? 0 : items.length - 1;
|
|
356
|
+
syncSelection();
|
|
357
|
+
return;
|
|
358
|
+
}
|
|
359
|
+
if (event.key === "Enter") {
|
|
360
|
+
if (!items.length || selected < 0) return;
|
|
361
|
+
event.preventDefault();
|
|
362
|
+
activate(items[selected]);
|
|
363
|
+
}
|
|
364
|
+
});
|
|
365
|
+
|
|
366
|
+
document.addEventListener("keydown", (event) => {
|
|
367
|
+
if (event.defaultPrevented || event.isComposing) return;
|
|
368
|
+
if (!event.altKey || event.ctrlKey || event.metaKey || event.shiftKey) return;
|
|
369
|
+
if (event.code !== "Space" && event.key !== " ") return;
|
|
370
|
+
event.preventDefault();
|
|
371
|
+
event.stopPropagation();
|
|
372
|
+
openPalette();
|
|
373
|
+
}, true);
|
|
374
|
+
|
|
375
|
+
const applyLocale = app.applyLocale;
|
|
376
|
+
app.applyLocale = () => {
|
|
377
|
+
applyLocale();
|
|
378
|
+
syncScope();
|
|
379
|
+
if (!dialog.open) return;
|
|
380
|
+
const value = input.value;
|
|
381
|
+
if (!value.trim() || value.startsWith(">")) showCommands(value.startsWith(">") ? value.slice(1) : "");
|
|
382
|
+
else scheduleSearch();
|
|
383
|
+
};
|
|
384
|
+
}
|
|
@@ -135,6 +135,7 @@ export function initRooms(app) {
|
|
|
135
135
|
app.rendered = 0;
|
|
136
136
|
app.firstMessageId = null;
|
|
137
137
|
app.state.pendingAction = null;
|
|
138
|
+
app.state.focusMessageId = null;
|
|
138
139
|
LS.roomId = roomId;
|
|
139
140
|
app.showChat();
|
|
140
141
|
app.renderHead();
|
|
@@ -249,9 +250,12 @@ export function initRooms(app) {
|
|
|
249
250
|
$("#btnCreate").disabled = busy;
|
|
250
251
|
};
|
|
251
252
|
|
|
252
|
-
app.leaveRoom = (silent = false) => {
|
|
253
|
-
const
|
|
254
|
-
if (
|
|
253
|
+
app.leaveRoom = async (silent = false) => {
|
|
254
|
+
const room = app.state.room;
|
|
255
|
+
if (!room) return;
|
|
256
|
+
if (!silent && !(await app.confirmAction(app.t("leaveRoomConfirm", { name: room.name })))) return;
|
|
257
|
+
app.closePanels?.();
|
|
258
|
+
void app.removeJoinedRoom(room.id, { leaveServer: !silent });
|
|
255
259
|
};
|
|
256
260
|
app.removeJoinedRoom = async (roomId, { leaveServer = false, reason = false, reconcile = false } = {}) => {
|
|
257
261
|
if (!reconcile) invalidateRoomList();
|
|
@@ -269,6 +273,7 @@ export function initRooms(app) {
|
|
|
269
273
|
app.state.unread = 0;
|
|
270
274
|
app.rendered = 0;
|
|
271
275
|
app.firstMessageId = null;
|
|
276
|
+
app.state.focusMessageId = null;
|
|
272
277
|
$("#msgList").textContent = "";
|
|
273
278
|
$("#msgInput").contentEditable = "false";
|
|
274
279
|
$("#msgInput").setAttribute("aria-disabled", "true"); $("#msgInput").tabIndex = -1;
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
import { LOADED_MESSAGE_LIMIT } from "./state.js";
|
|
2
|
+
import { flashMessageRow } from "./message-window.js";
|
|
3
|
+
|
|
4
|
+
// Palette search: local scan first, then lazy history pages until the room's
|
|
5
|
+
// loaded window fills up. There is no server-side search endpoint.
|
|
6
|
+
export const SEARCH_RESULT_LIMIT = 100;
|
|
7
|
+
export const SEARCH_MESSAGE_LIMIT = LOADED_MESSAGE_LIMIT;
|
|
8
|
+
const PAGE_LIMIT = 100;
|
|
9
|
+
const MAX_PAGES_PER_ROOM = Math.ceil(SEARCH_MESSAGE_LIMIT / PAGE_LIMIT);
|
|
10
|
+
|
|
11
|
+
export function initSearch(app) {
|
|
12
|
+
app.searchMessages = (query, scope = "room") => searchMessages(app, query, scope);
|
|
13
|
+
app.openSearchHit = (hit) => openSearchHit(app, hit);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export async function searchMessages(app, query, scope = "room") {
|
|
17
|
+
const needle = String(query ?? "").trim().toLocaleLowerCase();
|
|
18
|
+
if (!needle) return [];
|
|
19
|
+
const activeId = app.state.room?.id;
|
|
20
|
+
const roomIds = [...app.state.rooms.keys()]
|
|
21
|
+
.filter((roomId) => scope === "all" || roomId === activeId)
|
|
22
|
+
.sort((left, right) => {
|
|
23
|
+
if (left === activeId) return -1;
|
|
24
|
+
if (right === activeId) return 1;
|
|
25
|
+
return latestAt(app, right) - latestAt(app, left);
|
|
26
|
+
});
|
|
27
|
+
const hits = [];
|
|
28
|
+
for (const roomId of roomIds) {
|
|
29
|
+
await collectRoomHits(app, roomId, needle, hits);
|
|
30
|
+
// Keep the page responsive while several rooms are scanned.
|
|
31
|
+
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
32
|
+
}
|
|
33
|
+
return hits
|
|
34
|
+
.sort((left, right) => right.createdAt - left.createdAt || (right.seq ?? 0) - (left.seq ?? 0))
|
|
35
|
+
.slice(0, SEARCH_RESULT_LIMIT);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export async function openSearchHit(app, hit) {
|
|
39
|
+
const roomId = hit?.roomId, messageId = hit?.messageId;
|
|
40
|
+
if (!roomId || !messageId) return false;
|
|
41
|
+
const entry = app.state.rooms.get(roomId);
|
|
42
|
+
if (!entry?.room) return false;
|
|
43
|
+
if (!entry.room.messages.some((message) => message.id === messageId)) {
|
|
44
|
+
const seq = Number(hit.seq);
|
|
45
|
+
// beforeSeq is exclusive: seq + 1 puts the target at the end of the page.
|
|
46
|
+
if (!Number.isSafeInteger(seq)) return false;
|
|
47
|
+
try {
|
|
48
|
+
const page = await app.api(`/rooms/${encodeURIComponent(roomId)}/history?beforeSeq=${seq + 1}&limit=${PAGE_LIMIT}`);
|
|
49
|
+
if (app.state.rooms.get(roomId) !== entry) return false;
|
|
50
|
+
const messages = Array.isArray(page.messages) ? page.messages : [];
|
|
51
|
+
mergeHistory(entry, roomId, messages);
|
|
52
|
+
if (page.hasMore) {
|
|
53
|
+
// Continue upward pagination from the oldest message now loaded.
|
|
54
|
+
const oldest = entry.room.messages.find((message) => Number.isSafeInteger(message.seq))?.seq;
|
|
55
|
+
if (Number.isSafeInteger(oldest)) entry.historyBeforeSeq = oldest;
|
|
56
|
+
} else {
|
|
57
|
+
entry.historyComplete = true;
|
|
58
|
+
entry.historyBeforeSeq = null;
|
|
59
|
+
}
|
|
60
|
+
app.cacheRoomSoon?.(roomId);
|
|
61
|
+
if (!entry.room.messages.some((message) => message.id === messageId)) return false;
|
|
62
|
+
} catch { return false; }
|
|
63
|
+
}
|
|
64
|
+
if (app.state.room?.id !== roomId) app.activateRoom(roomId);
|
|
65
|
+
if (app.state.room?.id !== roomId) return false;
|
|
66
|
+
app.state.room = app.state.rooms.get(roomId)?.room || app.state.room;
|
|
67
|
+
app.state.focusMessageId = messageId;
|
|
68
|
+
app.state.followBottom = false;
|
|
69
|
+
app.state.readAnchorGuard = null;
|
|
70
|
+
app.renderMessages(false);
|
|
71
|
+
const row = document.querySelector(`[data-msg-id="${CSS.escape(messageId)}"]`);
|
|
72
|
+
if (!row) return false;
|
|
73
|
+
flashMessageRow(app, row);
|
|
74
|
+
return true;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
async function collectRoomHits(app, roomId, needle, hits) {
|
|
78
|
+
const entry = app.state.rooms.get(roomId);
|
|
79
|
+
if (!entry?.room) return;
|
|
80
|
+
const seen = new Set();
|
|
81
|
+
const scan = (messages) => {
|
|
82
|
+
for (const message of messages) {
|
|
83
|
+
if (seen.has(message.id)) continue;
|
|
84
|
+
seen.add(message.id);
|
|
85
|
+
if (message.recalledAt || message.pending || message.senderId === "system") continue;
|
|
86
|
+
const text = typeof message.text === "string" ? message.text : "";
|
|
87
|
+
if (!text || !text.toLocaleLowerCase().includes(needle)) continue;
|
|
88
|
+
hits.push({
|
|
89
|
+
roomId,
|
|
90
|
+
roomName: entry.room.name || roomId,
|
|
91
|
+
messageId: message.id,
|
|
92
|
+
seq: message.seq,
|
|
93
|
+
sender: message.senderId,
|
|
94
|
+
createdAt: message.createdAt,
|
|
95
|
+
text,
|
|
96
|
+
snippet: snippetFor(text, needle),
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
};
|
|
100
|
+
scan(entry.room.messages);
|
|
101
|
+
let pages = 0;
|
|
102
|
+
while (!entry.historyComplete && Number.isSafeInteger(entry.historyBeforeSeq)
|
|
103
|
+
&& entry.room.messages.length < SEARCH_MESSAGE_LIMIT && pages++ < MAX_PAGES_PER_ROOM) {
|
|
104
|
+
try {
|
|
105
|
+
const page = await app.api(`/rooms/${encodeURIComponent(roomId)}/history?beforeSeq=${entry.historyBeforeSeq}&limit=${PAGE_LIMIT}`);
|
|
106
|
+
if (app.state.rooms.get(roomId) !== entry) return;
|
|
107
|
+
const messages = Array.isArray(page.messages) ? page.messages : [];
|
|
108
|
+
scan(messages);
|
|
109
|
+
mergeHistory(entry, roomId, messages);
|
|
110
|
+
const next = typeof page.nextBeforeSeq === "number" ? page.nextBeforeSeq : null;
|
|
111
|
+
entry.historyComplete = !page.hasMore;
|
|
112
|
+
if (entry.historyComplete) entry.historyBeforeSeq = null;
|
|
113
|
+
else if (next === null || next >= entry.historyBeforeSeq) break;
|
|
114
|
+
else entry.historyBeforeSeq = next;
|
|
115
|
+
} catch { break; }
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
function mergeHistory(entry, roomId, messages) {
|
|
120
|
+
const merged = new Map(entry.room.messages.map((message) => [message.id, message]));
|
|
121
|
+
for (const message of messages) if (!merged.has(message.id)) merged.set(message.id, { ...message, roomId });
|
|
122
|
+
entry.room.messages = [...merged.values()]
|
|
123
|
+
.sort((left, right) => left.createdAt - right.createdAt || (left.seq ?? 0) - (right.seq ?? 0))
|
|
124
|
+
.slice(-LOADED_MESSAGE_LIMIT);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
function latestAt(app, roomId) {
|
|
128
|
+
return app.state.rooms.get(roomId)?.room.messages.at(-1)?.createdAt ?? 0;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
function snippetFor(text, needle) {
|
|
132
|
+
const flat = text.replace(/\s+/g, " ").trim();
|
|
133
|
+
const index = flat.toLocaleLowerCase().indexOf(needle);
|
|
134
|
+
if (index < 0) return flat.slice(0, 140);
|
|
135
|
+
const start = Math.max(0, index - 40), end = Math.min(flat.length, index + needle.length + 60);
|
|
136
|
+
return `${start > 0 ? "…" : ""}${flat.slice(start, end)}${end < flat.length ? "…" : ""}`;
|
|
137
|
+
}
|
|
@@ -7,7 +7,7 @@ export function initSettings(app) {
|
|
|
7
7
|
if (!dialog) return;
|
|
8
8
|
const tokenInput = $("#inTokenChat"), tokenHint = $("#tokenHint");
|
|
9
9
|
const contentInput = $("#inContentTransition");
|
|
10
|
-
const pageInput = $("#inPageStep");
|
|
10
|
+
const pageInput = $("#inPageStep"), pageValue = $("#pageStepValue");
|
|
11
11
|
const tabs = [...dialog.querySelectorAll('[role="tab"]')];
|
|
12
12
|
const scheme = matchMedia("(prefers-color-scheme: dark)");
|
|
13
13
|
let opener = null;
|
|
@@ -61,6 +61,7 @@ export function initSettings(app) {
|
|
|
61
61
|
syncThemeInputs();
|
|
62
62
|
if (contentInput) contentInput.checked = app.LS.contentTransition;
|
|
63
63
|
if (pageInput) pageInput.value = String(app.LS.pageStep);
|
|
64
|
+
if (pageValue) pageValue.textContent = `${app.LS.pageStep}%`;
|
|
64
65
|
tokenInput.value = app.LS.token;
|
|
65
66
|
if (tokenHint) tokenHint.hidden = !token;
|
|
66
67
|
if (dialog.open) {
|
|
@@ -88,21 +89,26 @@ export function initSettings(app) {
|
|
|
88
89
|
$("#btnCloseSettings")?.addEventListener("click", close);
|
|
89
90
|
dialog.addEventListener("click", (event) => { if (event.target === dialog) close(); });
|
|
90
91
|
dialog.addEventListener("cancel", () => opener?.setAttribute("aria-expanded", "false"));
|
|
92
|
+
pageInput?.addEventListener("input", () => {
|
|
93
|
+
app.LS.pageStep = pageInput.value;
|
|
94
|
+
pageInput.value = String(app.LS.pageStep);
|
|
95
|
+
if (pageValue) pageValue.textContent = `${app.LS.pageStep}%`;
|
|
96
|
+
});
|
|
91
97
|
dialog.addEventListener("change", (event) => {
|
|
92
98
|
if (event.target instanceof HTMLInputElement && event.target.name === "theme") app.setTheme(event.target.value);
|
|
93
99
|
if (event.target === contentInput) app.LS.contentTransition = contentInput.checked;
|
|
94
100
|
if (event.target === pageInput) {
|
|
95
101
|
app.LS.pageStep = pageInput.value;
|
|
96
102
|
pageInput.value = String(app.LS.pageStep);
|
|
103
|
+
if (pageValue) pageValue.textContent = `${app.LS.pageStep}%`;
|
|
97
104
|
}
|
|
98
105
|
});
|
|
99
106
|
dialog.addEventListener("close", () => {
|
|
100
107
|
const target = opener;
|
|
101
108
|
opener = null;
|
|
102
|
-
if (target?.isConnected)
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
}
|
|
109
|
+
if (target?.isConnected) target.setAttribute("aria-expanded", "false");
|
|
110
|
+
if (app.focusChatComposer?.({ force: true })) return;
|
|
111
|
+
if (target?.isConnected) target.focus({ preventScroll: true });
|
|
106
112
|
});
|
|
107
113
|
// The palette follows the system through its media query; only the browser chrome needs a nudge.
|
|
108
114
|
scheme.addEventListener("change", () => { if (app.LS.theme === "system") syncThemeColor(); });
|