@alexsun-top/opencode-chat-room 0.8.2 → 0.8.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 +12 -0
- package/README.md +18 -62
- package/README.zh-CN.md +25 -69
- package/dist/cli.js +466 -188
- package/package.json +11 -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 +201 -161
- package/server/assets/chat/announcements.js +302 -208
- package/server/assets/chat/attachment-store.js +116 -76
- package/server/assets/chat/attachments.js +701 -441
- package/server/assets/chat/chat-background.js +179 -156
- package/server/assets/chat/clipboard.js +45 -25
- package/server/assets/chat/confirm-dialog.js +38 -35
- package/server/assets/chat/copy-action.js +40 -41
- package/server/assets/chat/create-room-dialog.js +50 -47
- package/server/assets/chat/diagram-viewer.js +293 -236
- package/server/assets/chat/doc-preview.js +516 -443
- package/server/assets/chat/editor.js +446 -336
- package/server/assets/chat/events.js +650 -510
- package/server/assets/chat/files.js +505 -431
- package/server/assets/chat/history-loader.js +81 -73
- package/server/assets/chat/icons.js +9 -11
- package/server/assets/chat/image-zoom.js +246 -179
- package/server/assets/chat/index.js +2 -3
- package/server/assets/chat/install-prompt.js +90 -69
- package/server/assets/chat/integration-account-schema.js +91 -87
- package/server/assets/chat/integration-editor.js +162 -155
- package/server/assets/chat/integration-values.js +116 -101
- package/server/assets/chat/integrations.js +750 -626
- package/server/assets/chat/join-requests.js +415 -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 -103
- package/server/assets/chat/markdown.js +557 -404
- package/server/assets/chat/members.js +259 -197
- package/server/assets/chat/mentions.js +181 -133
- package/server/assets/chat/message-cache.js +40 -33
- package/server/assets/chat/message-polling.js +299 -227
- package/server/assets/chat/message-window.js +78 -55
- package/server/assets/chat/messages.js +562 -480
- package/server/assets/chat/office-preview.js +46 -45
- package/server/assets/chat/palette.js +464 -417
- package/server/assets/chat/panels.js +94 -79
- package/server/assets/chat/pwa.js +147 -75
- package/server/assets/chat/questions.js +374 -302
- package/server/assets/chat/recall.js +338 -273
- package/server/assets/chat/receipts.js +151 -121
- package/server/assets/chat/reminders.js +345 -214
- package/server/assets/chat/replies.js +181 -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 +76 -50
- package/server/assets/chat/rooms.js +631 -500
- package/server/assets/chat/search.js +138 -114
- package/server/assets/chat/settings.js +225 -201
- package/server/assets/chat/shiki.js +62 -63
- package/server/assets/chat/shortcuts.js +212 -189
- package/server/assets/chat/side-pane.js +161 -150
- package/server/assets/chat/state.js +860 -737
- package/server/assets/chat/system-events.js +46 -44
- package/server/assets/chat/system-messages.js +57 -56
- package/server/assets/chat/task-form.js +179 -183
- package/server/assets/chat/task-row.js +158 -155
- package/server/assets/chat/tasks.js +665 -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 +3 -1
- package/server/routes/pages.ts +7 -5
- package/server/sw.js +346 -268
- package/src/web/integration-rules-editor.ts +2 -0
|
@@ -1,217 +1,311 @@
|
|
|
1
1
|
import { LS } from "./state.js";
|
|
2
|
-
|
|
3
2
|
export function initAnnouncements(app) {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
if (saved?.name === snapshot.name && saved.content === snapshot.content) drafts.delete(key(id, name));
|
|
24
|
-
}
|
|
25
|
-
function status(text) { $("#announcementStatus").textContent = text; }
|
|
26
|
-
function formStatus(text) { $("#announcementDialogStatus").textContent = text; }
|
|
27
|
-
function count() { $("#announcementCount").textContent = app.t("announcementCount", { count: Array.from($("#announcementContent").value).length }); }
|
|
28
|
-
const clearPane = () => {
|
|
29
|
-
surface.classList.remove("is-pane");
|
|
30
|
-
if (chatView.dataset.sidePane !== "announcements") return;
|
|
31
|
-
delete chatView.dataset.sidePane;
|
|
32
|
-
chatView.classList.remove("has-pane");
|
|
33
|
-
};
|
|
34
|
-
const showSurface = () => {
|
|
35
|
-
if (app.panelPlacement("announcements") === "pane") {
|
|
36
|
-
app.closeOtherPanelSurfaces?.("announcements");
|
|
37
|
-
surface.classList.add("is-pane");
|
|
38
|
-
chatView.classList.add("has-pane");
|
|
39
|
-
chatView.dataset.sidePane = "announcements";
|
|
40
|
-
if (!surface.open) surface.show();
|
|
41
|
-
} else {
|
|
42
|
-
clearPane();
|
|
43
|
-
if (!surface.open) surface.showModal();
|
|
3
|
+
const $ = (selector) => {
|
|
4
|
+
const node = app.$(selector);
|
|
5
|
+
if (!node)
|
|
6
|
+
throw new Error(`Missing ${selector}`);
|
|
7
|
+
return node;
|
|
8
|
+
};
|
|
9
|
+
const drafts = new Map();
|
|
10
|
+
const surface = $("#announcementsDialog"), trigger = $("#btnAnnouncements"), list = $("#announcementList");
|
|
11
|
+
const dialog = $("#announcementDialog"), heading = $("#announcementDialogHeading");
|
|
12
|
+
const panelHeading = $("#announcementHeading"), chatView = $("#chatView");
|
|
13
|
+
let items = [], selected = "", generation = 0, roomGeneration = 0, editorRevision = 0, saveToken = 0;
|
|
14
|
+
let opener = null, pendingReopen = false, restoreSurfaceFocus = true;
|
|
15
|
+
const key = (id, name = selected) => `${id}\0${name ? `item\0${name}` : "new"}`;
|
|
16
|
+
const roomId = () => app.state.room?.id;
|
|
17
|
+
const currentRoom = (id, request) => roomId() === id && roomGeneration === request;
|
|
18
|
+
const draft = (id, name = selected) => drafts.get(key(id, name));
|
|
19
|
+
function storeDraft(id = roomId(), name = selected) {
|
|
20
|
+
if (id)
|
|
21
|
+
drafts.set(key(id, name), { name: name || $("#announcementName").value, content: $("#announcementContent").value });
|
|
44
22
|
}
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
if (!surface.open || (app.panelPlacement("announcements") === "pane") === surface.classList.contains("is-pane")) return;
|
|
48
|
-
pendingReopen = true;
|
|
49
|
-
surface.close();
|
|
50
|
-
showSurface();
|
|
51
|
-
panelHeading.focus({ preventScroll: true });
|
|
52
|
-
};
|
|
53
|
-
const closeSurface = (focus = true) => {
|
|
54
|
-
restoreSurfaceFocus = focus;
|
|
55
|
-
if (surface.open) surface.close();
|
|
56
|
-
};
|
|
57
|
-
function openSurface() {
|
|
58
|
-
const id = roomId();
|
|
59
|
-
if (!id) return;
|
|
60
|
-
if (app.panelPlacement("announcements") === "pane") app.closeDocPreview?.(false);
|
|
61
|
-
if (surface.open) { panelHeading.focus(); return; }
|
|
62
|
-
app.closePanels?.();
|
|
63
|
-
const active = document.activeElement;
|
|
64
|
-
opener = active instanceof HTMLElement && active !== document.body && !active.closest("dialog") && !$("#roomOptions")?.contains(active)
|
|
65
|
-
? active : $("#btnRoomOptions");
|
|
66
|
-
showSurface();
|
|
67
|
-
panelHeading.focus();
|
|
68
|
-
void load(id);
|
|
69
|
-
}
|
|
70
|
-
function render() {
|
|
71
|
-
list.textContent = "";
|
|
72
|
-
for (const entry of items) {
|
|
73
|
-
const li = document.createElement("li"), button = document.createElement("button");
|
|
74
|
-
button.type = "button"; button.textContent = entry.name; button.setAttribute("aria-haspopup", "dialog");
|
|
75
|
-
if (entry.name === selected) button.setAttribute("aria-current", "true");
|
|
76
|
-
button.addEventListener("click", (event) => { event.stopPropagation(); openEditor(entry); });
|
|
77
|
-
li.append(button); list.append(li);
|
|
23
|
+
function editorSnapshot() {
|
|
24
|
+
return { selected, revision: editorRevision, name: $("#announcementName").value, content: $("#announcementContent").value };
|
|
78
25
|
}
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
const
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
if (!id) return;
|
|
104
|
-
const request = ++generation, context = roomGeneration; status(app.t("announcementLoading"));
|
|
105
|
-
try {
|
|
106
|
-
const result = await app.api(`/rooms/${encodeURIComponent(id)}/announcements`);
|
|
107
|
-
if (roomId() !== id || context !== roomGeneration || generation !== request) return;
|
|
108
|
-
items = Array.isArray(result.announcements) ? result.announcements : [];
|
|
109
|
-
status("");
|
|
110
|
-
const chosen = items.find((item) => item.name === selected);
|
|
111
|
-
if (!chosen) {
|
|
112
|
-
const stale = selected && draft(id, selected);
|
|
113
|
-
if (stale) {
|
|
114
|
-
drafts.set(key(id, ""), { ...stale, name: $("#announcementName").value.trim() || stale.name });
|
|
115
|
-
drafts.delete(key(id, selected));
|
|
26
|
+
const editorMatches = (snapshot) => selected === snapshot.selected && editorRevision === snapshot.revision;
|
|
27
|
+
function clearDraftIfUnchanged(id, name, snapshot) {
|
|
28
|
+
const saved = draft(id, name);
|
|
29
|
+
if (saved?.name === snapshot.name && saved.content === snapshot.content)
|
|
30
|
+
drafts.delete(key(id, name));
|
|
31
|
+
}
|
|
32
|
+
function status(text) { $("#announcementStatus").textContent = text; }
|
|
33
|
+
function formStatus(text) { $("#announcementDialogStatus").textContent = text; }
|
|
34
|
+
function count() { $("#announcementCount").textContent = app.t("announcementCount", { count: Array.from($("#announcementContent").value).length }); }
|
|
35
|
+
const clearPane = () => {
|
|
36
|
+
surface.classList.remove("is-pane");
|
|
37
|
+
if (chatView.dataset.sidePane !== "announcements")
|
|
38
|
+
return;
|
|
39
|
+
delete chatView.dataset.sidePane;
|
|
40
|
+
chatView.classList.remove("has-pane");
|
|
41
|
+
};
|
|
42
|
+
const showSurface = () => {
|
|
43
|
+
if (app.panelPlacement("announcements") === "pane") {
|
|
44
|
+
app.closeOtherPanelSurfaces?.("announcements");
|
|
45
|
+
surface.classList.add("is-pane");
|
|
46
|
+
chatView.classList.add("has-pane");
|
|
47
|
+
chatView.dataset.sidePane = "announcements";
|
|
48
|
+
if (!surface.open)
|
|
49
|
+
surface.show();
|
|
116
50
|
}
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
51
|
+
else {
|
|
52
|
+
clearPane();
|
|
53
|
+
if (!surface.open)
|
|
54
|
+
surface.showModal();
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
const syncMode = () => {
|
|
58
|
+
if (!surface.open || (app.panelPlacement("announcements") === "pane") === surface.classList.contains("is-pane"))
|
|
59
|
+
return;
|
|
60
|
+
pendingReopen = true;
|
|
61
|
+
surface.close();
|
|
62
|
+
showSurface();
|
|
63
|
+
panelHeading.focus({ preventScroll: true });
|
|
64
|
+
};
|
|
65
|
+
const closeSurface = (focus = true) => {
|
|
66
|
+
restoreSurfaceFocus = focus;
|
|
67
|
+
if (surface.open)
|
|
68
|
+
surface.close();
|
|
69
|
+
};
|
|
70
|
+
function openSurface() {
|
|
71
|
+
const id = roomId();
|
|
72
|
+
if (!id)
|
|
73
|
+
return;
|
|
74
|
+
if (app.panelPlacement("announcements") === "pane")
|
|
75
|
+
app.closeDocPreview?.(false);
|
|
76
|
+
if (surface.open) {
|
|
77
|
+
panelHeading.focus();
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
app.closePanels?.();
|
|
81
|
+
const active = document.activeElement;
|
|
82
|
+
opener = active instanceof HTMLElement && active !== document.body && !active.closest("dialog") && !$("#roomOptions")?.contains(active)
|
|
83
|
+
? active : $("#btnRoomOptions");
|
|
84
|
+
showSurface();
|
|
85
|
+
panelHeading.focus();
|
|
86
|
+
void load(id);
|
|
87
|
+
}
|
|
88
|
+
function render() {
|
|
89
|
+
list.textContent = "";
|
|
90
|
+
for (const entry of items) {
|
|
91
|
+
const li = document.createElement("li"), button = document.createElement("button");
|
|
92
|
+
button.type = "button";
|
|
93
|
+
button.textContent = entry.name;
|
|
94
|
+
button.setAttribute("aria-haspopup", "dialog");
|
|
95
|
+
if (entry.name === selected)
|
|
96
|
+
button.setAttribute("aria-current", "true");
|
|
97
|
+
button.addEventListener("click", (event) => { event.stopPropagation(); openEditor(entry); });
|
|
98
|
+
li.append(button);
|
|
99
|
+
list.append(li);
|
|
100
|
+
}
|
|
101
|
+
if (!items.length)
|
|
102
|
+
status(app.t("announcementEmpty"));
|
|
103
|
+
}
|
|
104
|
+
function select(entry) {
|
|
105
|
+
const id = roomId();
|
|
106
|
+
if (!id)
|
|
107
|
+
return;
|
|
108
|
+
const next = entry?.name || "";
|
|
109
|
+
if (next !== selected)
|
|
110
|
+
editorRevision++;
|
|
111
|
+
selected = next;
|
|
112
|
+
const name = $("#announcementName"), content = $("#announcementContent");
|
|
113
|
+
name.value = selected;
|
|
114
|
+
name.readOnly = Boolean(selected);
|
|
115
|
+
const saved = draft(id);
|
|
116
|
+
name.value = selected || saved?.name || "";
|
|
117
|
+
content.value = saved?.content ?? entry?.content ?? "";
|
|
118
|
+
$("#btnDeleteAnnouncement").hidden = !selected;
|
|
119
|
+
render();
|
|
120
|
+
count();
|
|
124
121
|
}
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
const id = roomId(), name = $("#announcementName").value.trim(), content = $("#announcementContent").value;
|
|
135
|
-
if (!id || !name || Array.from(name).length > 64 || Array.from(content).length > 5000) return formStatus(app.t("announcementInvalid"));
|
|
136
|
-
if (!selected && items.some((item) => item.name === name)) return formStatus(app.t("announcementDuplicate"));
|
|
137
|
-
const context = roomGeneration, captured = editorSnapshot(), draftName = selected, button = $("#btnSaveAnnouncement"), token = ++saveToken;
|
|
138
|
-
storeDraft(id, draftName); button.disabled = true;
|
|
139
|
-
try {
|
|
140
|
-
const result = await app.api(`/rooms/${encodeURIComponent(id)}/announcements/${encodeURIComponent(name)}`, {
|
|
141
|
-
method: "PUT", body: JSON.stringify({ sessionID: LS.sessionID, content }),
|
|
142
|
-
});
|
|
143
|
-
if (currentRoom(id, context)) {
|
|
144
|
-
generation++; items = [...items.filter((item) => item.name !== name), result.announcement]; render();
|
|
145
|
-
clearDraftIfUnchanged(id, draftName, captured);
|
|
146
|
-
if (editorMatches(captured)) { select(result.announcement); status(app.t("announcementSaved")); formStatus(""); dialog.close(); }
|
|
147
|
-
else if (!selected) {
|
|
148
|
-
// Keep editing the newer draft under the name the create just reserved.
|
|
149
|
-
drafts.delete(key(id, draftName));
|
|
150
|
-
selected = result.announcement.name;
|
|
151
|
-
heading.textContent = selected; delete heading.dataset.i18n;
|
|
152
|
-
$("#announcementName").value = selected; $("#announcementName").readOnly = true;
|
|
153
|
-
$("#btnDeleteAnnouncement").hidden = false;
|
|
154
|
-
drafts.set(key(id, selected), { name: selected, content: $("#announcementContent").value });
|
|
155
|
-
render();
|
|
122
|
+
function openEditor(entry) {
|
|
123
|
+
select(entry);
|
|
124
|
+
if (entry) {
|
|
125
|
+
delete heading.dataset.i18n;
|
|
126
|
+
heading.textContent = entry.name;
|
|
127
|
+
}
|
|
128
|
+
else {
|
|
129
|
+
heading.dataset.i18n = "announcementNew";
|
|
130
|
+
heading.textContent = app.t("announcementNew");
|
|
156
131
|
}
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
132
|
+
formStatus("");
|
|
133
|
+
if (!dialog.open)
|
|
134
|
+
dialog.showModal();
|
|
135
|
+
$(entry ? "#announcementContent" : "#announcementName").focus();
|
|
160
136
|
}
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
137
|
+
async function load(id = roomId()) {
|
|
138
|
+
if (!id)
|
|
139
|
+
return;
|
|
140
|
+
const request = ++generation, context = roomGeneration;
|
|
141
|
+
status(app.t("announcementLoading"));
|
|
142
|
+
try {
|
|
143
|
+
const result = await app.api(`/rooms/${encodeURIComponent(id)}/announcements`);
|
|
144
|
+
if (roomId() !== id || context !== roomGeneration || generation !== request)
|
|
145
|
+
return;
|
|
146
|
+
items = Array.isArray(result.announcements) ? result.announcements : [];
|
|
147
|
+
status("");
|
|
148
|
+
const chosen = items.find((item) => item.name === selected);
|
|
149
|
+
if (!chosen) {
|
|
150
|
+
const stale = selected && draft(id, selected);
|
|
151
|
+
if (stale) {
|
|
152
|
+
drafts.set(key(id, ""), { ...stale, name: $("#announcementName").value.trim() || stale.name });
|
|
153
|
+
drafts.delete(key(id, selected));
|
|
154
|
+
}
|
|
155
|
+
select(null);
|
|
156
|
+
}
|
|
157
|
+
else
|
|
158
|
+
select(chosen);
|
|
159
|
+
render();
|
|
160
|
+
}
|
|
161
|
+
catch (error) {
|
|
162
|
+
if (roomId() !== id || context !== roomGeneration || generation !== request)
|
|
163
|
+
return;
|
|
164
|
+
status(app.t("announcementLoadFailed"));
|
|
165
|
+
if (error.status === 401)
|
|
166
|
+
app.openChatSettings();
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
$("#announcementContent").addEventListener("input", () => {
|
|
170
|
+
editorRevision++;
|
|
171
|
+
count();
|
|
172
|
+
storeDraft();
|
|
173
|
+
});
|
|
174
|
+
$("#announcementName").addEventListener("input", () => {
|
|
175
|
+
editorRevision++;
|
|
176
|
+
storeDraft();
|
|
177
|
+
});
|
|
178
|
+
$("#announcementForm").addEventListener("submit", async (event) => {
|
|
179
|
+
event.preventDefault();
|
|
180
|
+
const id = roomId(), name = $("#announcementName").value.trim(), content = $("#announcementContent").value;
|
|
181
|
+
if (!id || !name || Array.from(name).length > 64 || Array.from(content).length > 5000)
|
|
182
|
+
return formStatus(app.t("announcementInvalid"));
|
|
183
|
+
if (!selected && items.some((item) => item.name === name))
|
|
184
|
+
return formStatus(app.t("announcementDuplicate"));
|
|
185
|
+
const context = roomGeneration, captured = editorSnapshot(), draftName = selected, button = $("#btnSaveAnnouncement"), token = ++saveToken;
|
|
186
|
+
storeDraft(id, draftName);
|
|
187
|
+
button.disabled = true;
|
|
188
|
+
try {
|
|
189
|
+
const result = await app.api(`/rooms/${encodeURIComponent(id)}/announcements/${encodeURIComponent(name)}`, {
|
|
190
|
+
method: "PUT", body: JSON.stringify({ sessionID: LS.sessionID, content }),
|
|
191
|
+
});
|
|
192
|
+
if (currentRoom(id, context)) {
|
|
193
|
+
generation++;
|
|
194
|
+
items = [...items.filter((item) => item.name !== name), result.announcement];
|
|
195
|
+
render();
|
|
196
|
+
clearDraftIfUnchanged(id, draftName, captured);
|
|
197
|
+
if (editorMatches(captured)) {
|
|
198
|
+
select(result.announcement);
|
|
199
|
+
status(app.t("announcementSaved"));
|
|
200
|
+
formStatus("");
|
|
201
|
+
dialog.close();
|
|
202
|
+
}
|
|
203
|
+
else if (!selected) {
|
|
204
|
+
// Keep editing the newer draft under the name the create just reserved.
|
|
205
|
+
drafts.delete(key(id, draftName));
|
|
206
|
+
selected = result.announcement.name;
|
|
207
|
+
heading.textContent = selected;
|
|
208
|
+
delete heading.dataset.i18n;
|
|
209
|
+
$("#announcementName").value = selected;
|
|
210
|
+
$("#announcementName").readOnly = true;
|
|
211
|
+
$("#btnDeleteAnnouncement").hidden = false;
|
|
212
|
+
drafts.set(key(id, selected), { name: selected, content: $("#announcementContent").value });
|
|
213
|
+
render();
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
catch (error) {
|
|
218
|
+
const failure = error;
|
|
219
|
+
if (currentRoom(id, context) && editorMatches(captured))
|
|
220
|
+
formStatus(failure.status === 403 ? failure.message : app.t("announcementLoadFailed"));
|
|
221
|
+
}
|
|
222
|
+
finally {
|
|
223
|
+
if (token === saveToken)
|
|
224
|
+
button.disabled = false;
|
|
225
|
+
}
|
|
226
|
+
});
|
|
227
|
+
$("#btnNewAnnouncement").addEventListener("click", () => openEditor(null));
|
|
228
|
+
$("#btnDeleteAnnouncement").addEventListener("click", async () => {
|
|
229
|
+
const id = roomId(), name = selected, button = $("#btnDeleteAnnouncement");
|
|
230
|
+
if (!id || !name || button.disabled)
|
|
231
|
+
return;
|
|
232
|
+
button.disabled = true;
|
|
233
|
+
const context = roomGeneration, captured = editorSnapshot();
|
|
234
|
+
try {
|
|
235
|
+
if (!(await app.confirmAction(app.t("announcementDeleteConfirm", { name }))))
|
|
236
|
+
return;
|
|
237
|
+
await app.api(`/rooms/${encodeURIComponent(id)}/announcements/${encodeURIComponent(name)}`, { method: "DELETE", body: JSON.stringify({ sessionID: LS.sessionID }) });
|
|
238
|
+
if (currentRoom(id, context)) {
|
|
239
|
+
generation++;
|
|
240
|
+
items = items.filter((item) => item.name !== name);
|
|
241
|
+
render();
|
|
242
|
+
clearDraftIfUnchanged(id, name, captured);
|
|
243
|
+
if (editorMatches(captured)) {
|
|
244
|
+
select(null);
|
|
245
|
+
status(app.t("announcementDeleted"));
|
|
246
|
+
formStatus("");
|
|
247
|
+
dialog.close();
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
catch (error) {
|
|
252
|
+
if (currentRoom(id, context) && editorMatches(captured))
|
|
253
|
+
formStatus(error.message);
|
|
254
|
+
}
|
|
255
|
+
finally {
|
|
256
|
+
button.disabled = false;
|
|
257
|
+
}
|
|
258
|
+
});
|
|
259
|
+
$("#btnRefreshAnnouncements").addEventListener("click", () => void load());
|
|
260
|
+
$("#btnCloseAnnouncement").addEventListener("click", () => dialog.close());
|
|
261
|
+
$("#btnCancelAnnouncement").addEventListener("click", () => dialog.close());
|
|
262
|
+
trigger.addEventListener("click", () => {
|
|
263
|
+
// The entry lives in the Options menu, so choosing it closes that menu first.
|
|
264
|
+
app.closePanels();
|
|
265
|
+
openSurface();
|
|
266
|
+
});
|
|
267
|
+
$("#btnCloseAnnouncements").addEventListener("click", () => closeSurface());
|
|
268
|
+
surface.addEventListener("cancel", (event) => { event.preventDefault(); closeSurface(); });
|
|
269
|
+
surface.addEventListener("close", () => {
|
|
270
|
+
if (pendingReopen) {
|
|
271
|
+
pendingReopen = false;
|
|
272
|
+
return;
|
|
273
|
+
}
|
|
274
|
+
clearPane();
|
|
275
|
+
const focus = restoreSurfaceFocus;
|
|
276
|
+
restoreSurfaceFocus = true;
|
|
277
|
+
const target = opener;
|
|
278
|
+
opener = null;
|
|
279
|
+
if (focus && target?.isConnected)
|
|
280
|
+
target.focus({ preventScroll: true });
|
|
281
|
+
});
|
|
282
|
+
document.addEventListener("keydown", (event) => {
|
|
283
|
+
if (event.key !== "Escape" || event.altKey || !surface.open || !surface.classList.contains("is-pane"))
|
|
284
|
+
return;
|
|
285
|
+
if (!surface.contains(document.activeElement))
|
|
286
|
+
return;
|
|
287
|
+
event.preventDefault();
|
|
288
|
+
closeSurface();
|
|
289
|
+
});
|
|
290
|
+
app.registerPanelSurface?.("announcements", { element: surface, close: (restore = false) => closeSurface(restore) });
|
|
291
|
+
app.watchPanel?.("announcements", syncMode);
|
|
292
|
+
const closePanels = app.closePanels;
|
|
293
|
+
app.closePanels = () => { closePanels(); if (dialog.open)
|
|
294
|
+
dialog.close(); };
|
|
295
|
+
document.addEventListener("chat-room-activated", (event) => {
|
|
296
|
+
const id = event.detail;
|
|
297
|
+
if (dialog.open)
|
|
298
|
+
dialog.close();
|
|
299
|
+
if (surface.open && !surface.classList.contains("is-pane"))
|
|
300
|
+
closeSurface(false);
|
|
301
|
+
formStatus("");
|
|
302
|
+
roomGeneration++;
|
|
303
|
+
generation++;
|
|
304
|
+
saveToken++;
|
|
305
|
+
$("#btnSaveAnnouncement").disabled = false;
|
|
306
|
+
selected = "";
|
|
307
|
+
items = [];
|
|
308
|
+
select(null);
|
|
309
|
+
void load(id);
|
|
310
|
+
});
|
|
217
311
|
}
|