| 1 | import { normalizeDialogueTree, normalizeQuestDef } from "/shared/quests/normalize.js"; |
| 2 | import { |
| 3 | applyDialogueTextReplacements, |
| 4 | dialogueReplacementCatalog |
| 5 | } from "/shared/speech/dialogueText.js"; |
| 6 | import { initDialogueSimulate } from "./dialogueSimulate.js"; |
| 7 | import { |
| 8 | blankDialogueStep, |
| 9 | createDialogueCanvas |
| 10 | } from "./dialogueCanvas.js"; |
| 11 | import { initDialogueStepModal } from "./dialogueStepModal.js"; |
| 12 | |
| 13 | export function initDialoguesModal ({ |
| 14 | socket, |
| 15 | MessageType, |
| 16 | describeWorldTile = null, |
| 17 | centerEditorOnTile = null, |
| 18 | getDimensionOptions = null |
| 19 | }) |
| 20 | { |
| 21 | const backdrop = document.getElementById("dialogues-backdrop"); |
| 22 | const modal = document.getElementById("dialogues-modal"); |
| 23 | const listEl = document.getElementById("dialogues-list"); |
| 24 | const editorEl = document.getElementById("dialogues-editor"); |
| 25 | const logEl = document.getElementById("dialogues-log"); |
| 26 | const saveBtn = document.getElementById("dialogues-save"); |
| 27 | const deleteBtn = document.getElementById("dialogues-delete"); |
| 28 | const simulateBtn = document.getElementById("dialogues-simulate"); |
| 29 | const tokensBackdrop = document.getElementById("dialogue-tokens-backdrop"); |
| 30 | const tokensModal = document.getElementById("dialogue-tokens-modal"); |
| 31 | const tokensList = document.getElementById("dialogue-tokens-list"); |
| 32 | const tokensPreviewInput = document.getElementById("dialogue-tokens-preview-input"); |
| 33 | const tokensPreviewMale = document.getElementById("dialogue-tokens-preview-male"); |
| 34 | const tokensPreviewFemale = document.getElementById("dialogue-tokens-preview-female"); |
| 35 | let open = false; |
| 36 | let tokensHelpOpen = false; |
| 37 | /** @type {any[]} */ |
| 38 | let dialogues = []; |
| 39 | /** @type {ReturnType<typeof normalizeQuestDef>[]} */ |
| 40 | let questCatalog = []; |
| 41 | /** @type {any|null} */ |
| 42 | let draft = null; |
| 43 | let selectedId = null; |
| 44 | /** @type {object[]} */ |
| 45 | let changelog = []; |
| 46 | |
| 47 | const simulate = initDialogueSimulate({ |
| 48 | getTree: () => (draft ? normalizeDialogueTree(draft) : null), |
| 49 | getQuestTitle (questId) |
| 50 | { |
| 51 | const quest = questCatalog.find((q) => q.questId === questId); |
| 52 | return quest?.title || questId; |
| 53 | } |
| 54 | }); |
| 55 | |
| 56 | function conditionCtx () |
| 57 | { |
| 58 | return { |
| 59 | quests: questCatalog, |
| 60 | dialogues, |
| 61 | describeWorldTile, |
| 62 | centerEditorOnTile, |
| 63 | getDimensionOptions |
| 64 | }; |
| 65 | } |
| 66 | |
| 67 | const stepModal = initDialogueStepModal({ |
| 68 | getDraft: () => draft, |
| 69 | getQuestCatalog: () => questCatalog, |
| 70 | getDialogues: () => dialogues, |
| 71 | conditionCtx, |
| 72 | onChange () |
| 73 | { |
| 74 | canvas?.refresh(); |
| 75 | }, |
| 76 | host: modal |
| 77 | }); |
| 78 | |
| 79 | const canvas = createDialogueCanvas({ |
| 80 | getDraft: () => draft, |
| 81 | onOpenStep (stepId) |
| 82 | { |
| 83 | stepModal.open(stepId); |
| 84 | }, |
| 85 | onCreateStep (worldX, worldY) |
| 86 | { |
| 87 | if (!draft) |
| 88 | { return; } |
| 89 | const stepId = uniqueStepId(draft); |
| 90 | draft.steps.push(blankDialogueStep(stepId, worldX, worldY)); |
| 91 | canvas.refresh(); |
| 92 | stepModal.open(stepId); |
| 93 | }, |
| 94 | onChange () |
| 95 | { |
| 96 | stepModal.refresh(); |
| 97 | } |
| 98 | }); |
| 99 | |
| 100 | function uniqueStepId (tree) |
| 101 | { |
| 102 | const used = new Set((tree.steps ?? []).map((s) => s.stepId)); |
| 103 | let n = (tree.steps?.length ?? 0) + 1; |
| 104 | let id = `step-${n}`; |
| 105 | while (used.has(id)) |
| 106 | { |
| 107 | n += 1; |
| 108 | id = `step-${n}`; |
| 109 | } |
| 110 | return id; |
| 111 | } |
| 112 | |
| 113 | function show (visible) |
| 114 | { |
| 115 | open = visible; |
| 116 | backdrop.hidden = !visible; |
| 117 | modal.hidden = !visible; |
| 118 | if (!visible) |
| 119 | { |
| 120 | draft = null; |
| 121 | selectedId = null; |
| 122 | stepModal.close(); |
| 123 | showTokensHelp(false); |
| 124 | simulate.close(); |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | function showTokensHelp (visible) |
| 129 | { |
| 130 | tokensHelpOpen = visible; |
| 131 | if (tokensBackdrop) |
| 132 | { tokensBackdrop.hidden = !visible; } |
| 133 | if (tokensModal) |
| 134 | { tokensModal.hidden = !visible; } |
| 135 | if (visible) |
| 136 | { |
| 137 | renderTokensHelp(); |
| 138 | tokensPreviewInput?.focus(); |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | function renderTokensHelp () |
| 143 | { |
| 144 | if (tokensList) |
| 145 | { |
| 146 | tokensList.replaceChildren(); |
| 147 | for (const entry of dialogueReplacementCatalog()) |
| 148 | { |
| 149 | const li = document.createElement("li"); |
| 150 | const code = document.createElement("code"); |
| 151 | code.textContent = entry.token; |
| 152 | li.append(code, document.createTextNode(` — ${entry.meaning}`)); |
| 153 | tokensList.append(li); |
| 154 | } |
| 155 | } |
| 156 | updateTokensPreview(); |
| 157 | } |
| 158 | |
| 159 | function updateTokensPreview () |
| 160 | { |
| 161 | const sample = tokensPreviewInput?.value |
| 162 | || "Hello [Mr] [playername]. May [he] bring [his] [man]’s courage?"; |
| 163 | if (tokensPreviewInput && !tokensPreviewInput.value) |
| 164 | { tokensPreviewInput.placeholder = sample; } |
| 165 | const text = tokensPreviewInput?.value || sample; |
| 166 | const male = applyDialogueTextReplacements(text, { |
| 167 | firstName: "Ada", |
| 168 | lastName: "Worker", |
| 169 | sex: "male" |
| 170 | }); |
| 171 | const female = applyDialogueTextReplacements(text, { |
| 172 | firstName: "Ada", |
| 173 | lastName: "Worker", |
| 174 | sex: "female" |
| 175 | }); |
| 176 | if (tokensPreviewMale) |
| 177 | { tokensPreviewMale.textContent = male; } |
| 178 | if (tokensPreviewFemale) |
| 179 | { tokensPreviewFemale.textContent = female; } |
| 180 | } |
| 181 | |
| 182 | function selectDialogue (dialogueId) |
| 183 | { |
| 184 | const found = dialogues.find((d) => d.dialogueId === dialogueId); |
| 185 | if (!found) |
| 186 | { |
| 187 | draft = null; |
| 188 | selectedId = null; |
| 189 | stepModal.close(); |
| 190 | render(); |
| 191 | return; |
| 192 | } |
| 193 | selectedId = found.dialogueId; |
| 194 | draft = structuredClone(found); |
| 195 | stepModal.close(); |
| 196 | canvas.clearWiring(); |
| 197 | render(); |
| 198 | } |
| 199 | |
| 200 | function renderList () |
| 201 | { |
| 202 | listEl.replaceChildren(); |
| 203 | for (const tree of dialogues) |
| 204 | { |
| 205 | const btn = document.createElement("button"); |
| 206 | btn.type = "button"; |
| 207 | const unique = tree.unique ? " · unique" : ""; |
| 208 | const assigned = tree.assignedSpawnerCount != null |
| 209 | ? ` (${tree.assignedSpawnerCount})` |
| 210 | : ""; |
| 211 | btn.textContent = `${tree.label || tree.dialogueId}${unique}${assigned}`; |
| 212 | if (tree.dialogueId === selectedId) |
| 213 | { btn.classList.add("active"); } |
| 214 | btn.addEventListener("click", () => selectDialogue(tree.dialogueId)); |
| 215 | listEl.append(btn); |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | function renderEditor () |
| 220 | { |
| 221 | editorEl.replaceChildren(); |
| 222 | editorEl.classList.add("dialogues-editor-canvas-host"); |
| 223 | saveBtn.disabled = !draft; |
| 224 | deleteBtn.disabled = !draft; |
| 225 | if (simulateBtn) |
| 226 | { simulateBtn.disabled = !draft; } |
| 227 | if (!draft) |
| 228 | { |
| 229 | const hint = document.createElement("p"); |
| 230 | hint.className = "content-editor-hint"; |
| 231 | hint.textContent = "Select a dialogue or create a new one."; |
| 232 | editorEl.append(hint); |
| 233 | return; |
| 234 | } |
| 235 | |
| 236 | const meta = document.createElement("div"); |
| 237 | meta.className = "dialogue-editor-meta"; |
| 238 | meta.append( |
| 239 | field("Dialogue id", textInput(draft.dialogueId, (v) => { draft.dialogueId = v; })), |
| 240 | field("Label", textInput(draft.label, (v) => { draft.label = v; })) |
| 241 | ); |
| 242 | |
| 243 | const uniqueLabel = document.createElement("label"); |
| 244 | uniqueLabel.className = "dialogue-editor-unique"; |
| 245 | const uniqueBox = document.createElement("input"); |
| 246 | uniqueBox.type = "checkbox"; |
| 247 | uniqueBox.checked = !!draft.unique; |
| 248 | uniqueBox.addEventListener("change", () => { draft.unique = uniqueBox.checked; }); |
| 249 | uniqueLabel.append(uniqueBox, document.createTextNode(" Unique (at most one spawner)")); |
| 250 | meta.append(uniqueLabel); |
| 251 | |
| 252 | const assigned = document.createElement("p"); |
| 253 | assigned.className = "content-editor-hint"; |
| 254 | assigned.textContent = `Assigned spawners: ${draft.assignedSpawnerCount ?? 0}`; |
| 255 | meta.append(assigned); |
| 256 | |
| 257 | meta.append(field("Entry step id", textInput(draft.entryStepId ?? "", (v) => |
| 258 | { |
| 259 | draft.entryStepId = v; |
| 260 | canvas.refresh(); |
| 261 | }))); |
| 262 | |
| 263 | const canvasHint = document.createElement("p"); |
| 264 | canvasHint.className = "content-editor-hint dialogue-canvas-help"; |
| 265 | canvasHint.textContent = "Drag empty space to pan · wheel to zoom · drag cards to move · click a card to edit · right-click empty canvas to create a step · click an output port then an input to wire."; |
| 266 | meta.append(canvasHint); |
| 267 | |
| 268 | editorEl.append(meta, canvas.el); |
| 269 | canvas.refresh(); |
| 270 | } |
| 271 | |
| 272 | function render () |
| 273 | { |
| 274 | renderList(); |
| 275 | renderEditor(); |
| 276 | renderLog(); |
| 277 | } |
| 278 | |
| 279 | function renderLog () |
| 280 | { |
| 281 | if (!logEl) |
| 282 | { return; } |
| 283 | logEl.innerHTML = ""; |
| 284 | const scopeId = draft?.dialogueId ?? selectedId; |
| 285 | const entries = scopeId |
| 286 | ? changelog.filter((entry) => entry.dialogueId === scopeId) |
| 287 | : changelog; |
| 288 | if (!entries.length) |
| 289 | { |
| 290 | logEl.innerHTML = scopeId |
| 291 | ? "<p class=\"hint\">No edits logged for this dialogue yet.</p>" |
| 292 | : "<p class=\"hint\">No dialogue edits logged yet.</p>"; |
| 293 | return; |
| 294 | } |
| 295 | for (const entry of entries) |
| 296 | { |
| 297 | const item = document.createElement("article"); |
| 298 | const who = entry.editorDisplayName || entry.editor_admin_id; |
| 299 | const scope = scopeId ? "" : ` · ${entry.dialogueId}`; |
| 300 | item.innerHTML = `<header>${who}${scope} · ${new Date(entry.edited_at).toLocaleString()}</header>`; |
| 301 | const list = document.createElement("ul"); |
| 302 | for (const bullet of entry.diffEntries ?? []) |
| 303 | { |
| 304 | const li = document.createElement("li"); |
| 305 | li.textContent = bullet; |
| 306 | list.append(li); |
| 307 | } |
| 308 | item.append(list); |
| 309 | logEl.append(item); |
| 310 | } |
| 311 | logEl.scrollTop = logEl.scrollHeight; |
| 312 | } |
| 313 | |
| 314 | document.getElementById("open-dialogues").addEventListener("click", () => |
| 315 | { |
| 316 | socket.send({ type: MessageType.editorLoadDialogues }); |
| 317 | socket.send({ type: MessageType.editorLoadQuests }); |
| 318 | show(true); |
| 319 | render(); |
| 320 | }); |
| 321 | document.getElementById("dialogues-close").addEventListener("click", () => show(false)); |
| 322 | backdrop.addEventListener("click", () => show(false)); |
| 323 | document.getElementById("dialogues-tokens-help")?.addEventListener("click", () => |
| 324 | { |
| 325 | showTokensHelp(true); |
| 326 | }); |
| 327 | document.getElementById("dialogue-tokens-close")?.addEventListener("click", () => showTokensHelp(false)); |
| 328 | tokensBackdrop?.addEventListener("click", () => showTokensHelp(false)); |
| 329 | tokensPreviewInput?.addEventListener("input", () => updateTokensPreview()); |
| 330 | simulateBtn?.addEventListener("click", () => |
| 331 | { |
| 332 | if (!draft) |
| 333 | { return; } |
| 334 | simulate.start(); |
| 335 | }); |
| 336 | document.getElementById("dialogues-new").addEventListener("click", () => |
| 337 | { |
| 338 | const base = normalizeDialogueTree({ |
| 339 | dialogueId: `dialogue-${dialogues.length + 1}`, |
| 340 | label: "New dialogue", |
| 341 | unique: false |
| 342 | }); |
| 343 | if (base.steps[0]) |
| 344 | { |
| 345 | base.steps[0].editorX = 80; |
| 346 | base.steps[0].editorY = 80; |
| 347 | } |
| 348 | draft = { ...structuredClone(base), assignedSpawnerCount: 0 }; |
| 349 | selectedId = null; |
| 350 | stepModal.close(); |
| 351 | render(); |
| 352 | }); |
| 353 | saveBtn.addEventListener("click", () => |
| 354 | { |
| 355 | if (!draft) |
| 356 | { return; } |
| 357 | socket.send({ type: MessageType.editorSaveDialogue, dialogue: draft }); |
| 358 | }); |
| 359 | deleteBtn.addEventListener("click", () => |
| 360 | { |
| 361 | if (!draft?.dialogueId) |
| 362 | { return; } |
| 363 | if (!window.confirm(`Delete dialogue "${draft.dialogueId}"?`)) |
| 364 | { return; } |
| 365 | socket.send({ type: MessageType.editorDeleteDialogue, dialogueId: draft.dialogueId }); |
| 366 | }); |
| 367 | |
| 368 | return { |
| 369 | isOpen: () => open || tokensHelpOpen || simulate.isOpen() || stepModal.isOpen(), |
| 370 | getDialogues: () => dialogues, |
| 371 | handleMessage (message) |
| 372 | { |
| 373 | if (message.type === MessageType.editorQuests) |
| 374 | { |
| 375 | questCatalog = (message.quests ?? []).map((q) => normalizeQuestDef(q)); |
| 376 | if (open) |
| 377 | { render(); } |
| 378 | return; |
| 379 | } |
| 380 | if (message.type !== MessageType.editorDialogues) |
| 381 | { return; } |
| 382 | dialogues = (message.dialogues ?? []).map((d) => |
| 383 | { |
| 384 | const tree = normalizeDialogueTree(d); |
| 385 | return { |
| 386 | ...tree, |
| 387 | assignedSpawnerCount: d.assignedSpawnerCount ?? 0 |
| 388 | }; |
| 389 | }); |
| 390 | changelog = message.changelog ?? []; |
| 391 | if (draft) |
| 392 | { |
| 393 | const still = dialogues.find((d) => d.dialogueId === draft.dialogueId); |
| 394 | if (still) |
| 395 | { |
| 396 | selectedId = still.dialogueId; |
| 397 | draft = structuredClone(still); |
| 398 | } |
| 399 | else if (selectedId) |
| 400 | { |
| 401 | draft = null; |
| 402 | selectedId = null; |
| 403 | } |
| 404 | } |
| 405 | if (open) |
| 406 | { render(); } |
| 407 | } |
| 408 | }; |
| 409 | } |
| 410 | |
| 411 | function field (label, input) |
| 412 | { |
| 413 | const wrap = document.createElement("label"); |
| 414 | wrap.append(document.createTextNode(label), input); |
| 415 | return wrap; |
| 416 | } |
| 417 | |
| 418 | function textInput (value, onChange) |
| 419 | { |
| 420 | const input = document.createElement("input"); |
| 421 | input.type = "text"; |
| 422 | input.value = value ?? ""; |
| 423 | input.addEventListener("input", () => onChange(input.value)); |
| 424 | return input; |
| 425 | } |