| 1 | import { normalizeQuestDef, normalizeDialogueTree } from "/shared/quests/normalize.js"; |
| 2 | import { EffectKind, normalizeEffects } from "/shared/quests/conditions.js"; |
| 3 | import { |
| 4 | blankQuestStep, |
| 5 | layoutQuestSteps, |
| 6 | prepareQuestExitsForSave, |
| 7 | questCanvasPorts |
| 8 | } from "/shared/quests/questGraph.js"; |
| 9 | import { createDialogueCanvas } from "./dialogueCanvas.js"; |
| 10 | import { initQuestStepModal } from "./questStepModal.js"; |
| 11 | import { itemDefinition, listAllItemPickerEntries } from "/shared/definitions/items.js"; |
| 12 | import { ALL_SKILL_IDS, SKILL_DEFINITIONS } from "/shared/definitions/skills.js"; |
| 13 | |
| 14 | export function initQuestsModal ({ |
| 15 | socket, |
| 16 | MessageType, |
| 17 | describeWorldTile = null, |
| 18 | centerEditorOnTile = null, |
| 19 | getDimensionOptions = null, |
| 20 | getSpawnerOptions = null |
| 21 | }) |
| 22 | { |
| 23 | const backdrop = document.getElementById("quests-backdrop"); |
| 24 | const modal = document.getElementById("quests-modal"); |
| 25 | const listEl = document.getElementById("quests-list"); |
| 26 | const editorEl = document.getElementById("quests-editor"); |
| 27 | const logEl = document.getElementById("quests-log"); |
| 28 | const saveBtn = document.getElementById("quests-save"); |
| 29 | const deleteBtn = document.getElementById("quests-delete"); |
| 30 | let open = false; |
| 31 | /** @type {ReturnType<typeof normalizeQuestDef>[]} */ |
| 32 | let quests = []; |
| 33 | /** @type {ReturnType<typeof normalizeDialogueTree>[]} */ |
| 34 | let dialogues = []; |
| 35 | /** @type {{ spawnerId: string, mobTypeId?: string, dimensionId?: string, tileX?: number, tileY?: number }[]} */ |
| 36 | let serverSpawners = []; |
| 37 | /** @type {ReturnType<typeof normalizeQuestDef>|null} */ |
| 38 | let draft = null; |
| 39 | let selectedId = null; |
| 40 | /** @type {object[]} */ |
| 41 | let changelog = []; |
| 42 | /** JSON snapshot of the last loaded quest. null = never saved (new). */ |
| 43 | let draftBaseline = null; |
| 44 | |
| 45 | function conditionCtx () |
| 46 | { |
| 47 | return { |
| 48 | quests, |
| 49 | dialogues, |
| 50 | describeWorldTile, |
| 51 | centerEditorOnTile, |
| 52 | getDimensionOptions, |
| 53 | getSpawnerOptions: (mobTypeId) => |
| 54 | { |
| 55 | if (typeof getSpawnerOptions === "function") |
| 56 | { return getSpawnerOptions(mobTypeId) ?? []; } |
| 57 | return serverSpawners.filter((row) => !mobTypeId || String(row.mobTypeId) === String(mobTypeId)); |
| 58 | } |
| 59 | }; |
| 60 | } |
| 61 | |
| 62 | const stepModal = initQuestStepModal({ |
| 63 | getDraft: () => draft, |
| 64 | conditionCtx, |
| 65 | onChange () |
| 66 | { |
| 67 | noteChange(); |
| 68 | canvas?.refresh(); |
| 69 | }, |
| 70 | host: modal |
| 71 | }); |
| 72 | |
| 73 | const canvas = createDialogueCanvas({ |
| 74 | getDraft: () => draft, |
| 75 | getPorts: questCanvasPorts, |
| 76 | cardLabel: (step) => step.title || step.stepId, |
| 77 | cardMeta (step, isEntry) |
| 78 | { |
| 79 | const kind = step.exit?.kind === "complete" ? "quest end" : (step.exit?.kind ?? "continue"); |
| 80 | return isEntry ? `start · ${kind}` : kind; |
| 81 | }, |
| 82 | isEntryStep: (tree, step) => tree?.steps?.[0] === step, |
| 83 | stepPosition (step) |
| 84 | { |
| 85 | const laid = layoutQuestSteps(draft?.steps ?? []); |
| 86 | return laid.get(step.stepId) ?? null; |
| 87 | }, |
| 88 | cardFlags: (step) => ({ "is-quest-end": step.exit?.kind === "complete" }), |
| 89 | createStepLabel: "Create new step", |
| 90 | onOpenStep (stepId) |
| 91 | { |
| 92 | stepModal.open(stepId); |
| 93 | }, |
| 94 | onCreateStep (worldX, worldY) |
| 95 | { |
| 96 | if (!draft) |
| 97 | { return; } |
| 98 | const stepId = uniqueStepId(draft); |
| 99 | draft.steps.push(blankQuestStep(stepId, worldX, worldY)); |
| 100 | noteChange(); |
| 101 | canvas.refresh(); |
| 102 | stepModal.open(stepId); |
| 103 | }, |
| 104 | onChange () |
| 105 | { |
| 106 | noteChange(); |
| 107 | stepModal.refresh(); |
| 108 | } |
| 109 | }); |
| 110 | |
| 111 | function uniqueStepId (tree) |
| 112 | { |
| 113 | const used = new Set((tree.steps ?? []).map((step) => step.stepId)); |
| 114 | let n = (tree.steps?.length ?? 0) + 1; |
| 115 | let id = `step-${n}`; |
| 116 | while (used.has(id)) |
| 117 | { |
| 118 | n += 1; |
| 119 | id = `step-${n}`; |
| 120 | } |
| 121 | return id; |
| 122 | } |
| 123 | |
| 124 | function persistableSnapshot (quest) |
| 125 | { |
| 126 | if (!quest) |
| 127 | { return null; } |
| 128 | return JSON.stringify(quest); |
| 129 | } |
| 130 | |
| 131 | function captureBaseline () |
| 132 | { |
| 133 | draftBaseline = draft ? persistableSnapshot(draft) : null; |
| 134 | syncSaveButton(); |
| 135 | } |
| 136 | |
| 137 | function isDraftDirty () |
| 138 | { |
| 139 | if (!draft) |
| 140 | { return false; } |
| 141 | if (draftBaseline == null) |
| 142 | { return true; } |
| 143 | return persistableSnapshot(draft) !== draftBaseline; |
| 144 | } |
| 145 | |
| 146 | function noteChange () |
| 147 | { |
| 148 | syncSaveButton(); |
| 149 | } |
| 150 | |
| 151 | function syncSaveButton () |
| 152 | { |
| 153 | const dirty = isDraftDirty(); |
| 154 | saveBtn.disabled = !draft || !dirty; |
| 155 | saveBtn.classList.toggle("save-dirty", !!draft && dirty); |
| 156 | } |
| 157 | |
| 158 | function confirmDiscard () |
| 159 | { |
| 160 | if (!isDraftDirty()) |
| 161 | { return true; } |
| 162 | return window.confirm("Discard unsaved quest changes?"); |
| 163 | } |
| 164 | |
| 165 | /** @type {null | { resultsEl: HTMLElement, inputEl: HTMLInputElement }} */ |
| 166 | let itemPickContext = null; |
| 167 | |
| 168 | function show (visible) |
| 169 | { |
| 170 | if (!visible && !confirmDiscard()) |
| 171 | { return; } |
| 172 | open = visible; |
| 173 | backdrop.hidden = !visible; |
| 174 | modal.hidden = !visible; |
| 175 | if (!visible) |
| 176 | { |
| 177 | draft = null; |
| 178 | selectedId = null; |
| 179 | draftBaseline = null; |
| 180 | stepModal.close(); |
| 181 | canvas.clearWiring(); |
| 182 | closeInlineItemSearch(); |
| 183 | syncSaveButton(); |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | function closeInlineItemSearch () |
| 188 | { |
| 189 | if (itemPickContext?.resultsEl) |
| 190 | { itemPickContext.resultsEl.replaceChildren(); } |
| 191 | itemPickContext = null; |
| 192 | } |
| 193 | |
| 194 | function openInlineItemSearch (inputEl, resultsEl) |
| 195 | { |
| 196 | itemPickContext = { inputEl, resultsEl }; |
| 197 | inputEl.value = ""; |
| 198 | renderInlineItemSearch(""); |
| 199 | inputEl.focus(); |
| 200 | } |
| 201 | |
| 202 | function renderInlineItemSearch (query) |
| 203 | { |
| 204 | if (!itemPickContext || !draft) |
| 205 | { return; } |
| 206 | const { resultsEl } = itemPickContext; |
| 207 | resultsEl.replaceChildren(); |
| 208 | const q = String(query ?? "").trim().toLowerCase(); |
| 209 | const taken = new Set( |
| 210 | (draft.rewards ?? []) |
| 211 | .filter((row) => row.kind === EffectKind.giveItem) |
| 212 | .map((row) => row.itemId) |
| 213 | ); |
| 214 | const matches = listAllItemPickerEntries().filter((item) => |
| 215 | { |
| 216 | if (taken.has(item.itemId)) |
| 217 | { return false; } |
| 218 | if (!q) |
| 219 | { return true; } |
| 220 | return item.displayName.toLowerCase().includes(q) |
| 221 | || item.key.toLowerCase().includes(q) |
| 222 | || String(item.itemId).includes(q); |
| 223 | }).slice(0, 40); |
| 224 | if (!matches.length) |
| 225 | { |
| 226 | resultsEl.textContent = "No matching items."; |
| 227 | return; |
| 228 | } |
| 229 | for (const item of matches) |
| 230 | { |
| 231 | const row = document.createElement("button"); |
| 232 | row.type = "button"; |
| 233 | row.className = "shop-item-search-row"; |
| 234 | const name = document.createElement("span"); |
| 235 | name.className = "shop-item-search-row-name"; |
| 236 | name.textContent = item.displayName; |
| 237 | const key = document.createElement("span"); |
| 238 | key.className = "shop-item-search-row-key"; |
| 239 | key.textContent = `${item.key} · #${item.itemId}`; |
| 240 | row.append(name, key); |
| 241 | row.addEventListener("click", () => |
| 242 | { |
| 243 | if (!Array.isArray(draft.rewards)) |
| 244 | { draft.rewards = []; } |
| 245 | draft.rewards.push({ |
| 246 | kind: EffectKind.giveItem, |
| 247 | itemId: item.itemId, |
| 248 | quantity: 1, |
| 249 | toBank: false |
| 250 | }); |
| 251 | draft.rewards = normalizeEffects(draft.rewards); |
| 252 | closeInlineItemSearch(); |
| 253 | renderEditor(); |
| 254 | }); |
| 255 | resultsEl.append(row); |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | function selectQuest (questId) |
| 260 | { |
| 261 | if (questId === selectedId && draft) |
| 262 | { return; } |
| 263 | if (!confirmDiscard()) |
| 264 | { return; } |
| 265 | const found = quests.find((q) => q.questId === questId); |
| 266 | if (!found) |
| 267 | { |
| 268 | draft = null; |
| 269 | selectedId = null; |
| 270 | draftBaseline = null; |
| 271 | stepModal.close(); |
| 272 | render(); |
| 273 | return; |
| 274 | } |
| 275 | selectedId = found.questId; |
| 276 | draft = structuredClone(found); |
| 277 | captureBaseline(); |
| 278 | stepModal.close(); |
| 279 | canvas.clearWiring(); |
| 280 | render(); |
| 281 | } |
| 282 | |
| 283 | function renderList () |
| 284 | { |
| 285 | listEl.replaceChildren(); |
| 286 | for (const quest of quests) |
| 287 | { |
| 288 | const btn = document.createElement("button"); |
| 289 | btn.type = "button"; |
| 290 | btn.textContent = quest.title || quest.questId; |
| 291 | if (quest.questId === selectedId) |
| 292 | { btn.classList.add("active"); } |
| 293 | btn.addEventListener("click", () => selectQuest(quest.questId)); |
| 294 | listEl.append(btn); |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | function renderEditor () |
| 299 | { |
| 300 | editorEl.replaceChildren(); |
| 301 | editorEl.classList.toggle("dialogues-editor-canvas-host", !!draft); |
| 302 | syncSaveButton(); |
| 303 | deleteBtn.disabled = !draft; |
| 304 | if (!draft) |
| 305 | { |
| 306 | const hint = document.createElement("p"); |
| 307 | hint.className = "content-editor-hint"; |
| 308 | hint.textContent = "Select a quest or create a new one."; |
| 309 | editorEl.append(hint); |
| 310 | return; |
| 311 | } |
| 312 | |
| 313 | if (!Array.isArray(draft.rewards)) |
| 314 | { draft.rewards = []; } |
| 315 | |
| 316 | const meta = document.createElement("div"); |
| 317 | meta.className = "dialogue-editor-meta"; |
| 318 | meta.append( |
| 319 | field("Quest id", textInput(draft.questId, (value) => |
| 320 | { |
| 321 | draft.questId = value; |
| 322 | noteChange(); |
| 323 | })), |
| 324 | field("Title", textInput(draft.title, (value) => |
| 325 | { |
| 326 | draft.title = value; |
| 327 | noteChange(); |
| 328 | })), |
| 329 | field("Description", textArea(draft.description, (value) => |
| 330 | { |
| 331 | draft.description = value; |
| 332 | noteChange(); |
| 333 | }, 3)) |
| 334 | ); |
| 335 | const canvasHint = document.createElement("p"); |
| 336 | canvasHint.className = "content-editor-hint dialogue-canvas-help"; |
| 337 | 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. The first card is the start. A quest-end card has no output."; |
| 338 | meta.append(canvasHint); |
| 339 | const rewards = document.createElement("details"); |
| 340 | rewards.className = "quest-rewards-drawer"; |
| 341 | rewards.open = true; |
| 342 | const summary = document.createElement("summary"); |
| 343 | summary.textContent = "Completion rewards"; |
| 344 | rewards.append(summary); |
| 345 | editorEl.append(meta, rewards); |
| 346 | renderRewardsSection(rewards); |
| 347 | editorEl.append(canvas.el); |
| 348 | canvas.refresh(); |
| 349 | } |
| 350 | |
| 351 | function renderRewardsSection (parent = editorEl) |
| 352 | { |
| 353 | const box = document.createElement("div"); |
| 354 | box.className = "content-editor-section"; |
| 355 | |
| 356 | const hint = document.createElement("p"); |
| 357 | hint.className = "content-editor-hint"; |
| 358 | hint.textContent = "Granted when the final step completes. Bank-checked items (or overflow) go to the bank with a Questward letter."; |
| 359 | box.append(hint); |
| 360 | |
| 361 | const xpHead = sectionHeader("Skill XP", () => |
| 362 | { |
| 363 | const skillId = ALL_SKILL_IDS.find((id) => |
| 364 | !(draft.rewards ?? []).some((r) => r.kind === EffectKind.giveXp && r.skillId === id) |
| 365 | ) ?? ALL_SKILL_IDS[0]; |
| 366 | draft.rewards.push({ kind: EffectKind.giveXp, skillId, amount: 15 }); |
| 367 | draft.rewards = normalizeEffects(draft.rewards); |
| 368 | renderEditor(); |
| 369 | }); |
| 370 | box.append(xpHead); |
| 371 | |
| 372 | const xpRows = (draft.rewards ?? []).filter((r) => r.kind === EffectKind.giveXp); |
| 373 | if (!xpRows.length) |
| 374 | { |
| 375 | const empty = document.createElement("p"); |
| 376 | empty.className = "content-editor-hint"; |
| 377 | empty.textContent = "No skill XP rewards."; |
| 378 | box.append(empty); |
| 379 | } |
| 380 | xpRows.forEach((reward) => |
| 381 | { |
| 382 | const rewardIndex = draft.rewards.indexOf(reward); |
| 383 | const row = document.createElement("div"); |
| 384 | row.className = "content-editor-row"; |
| 385 | const skill = document.createElement("select"); |
| 386 | for (const skillId of ALL_SKILL_IDS) |
| 387 | { |
| 388 | const opt = document.createElement("option"); |
| 389 | opt.value = skillId; |
| 390 | opt.textContent = SKILL_DEFINITIONS[skillId]?.displayName ?? skillId; |
| 391 | if (reward.skillId === skillId) |
| 392 | { opt.selected = true; } |
| 393 | skill.append(opt); |
| 394 | } |
| 395 | skill.addEventListener("change", () => |
| 396 | { |
| 397 | draft.rewards[rewardIndex].skillId = skill.value; |
| 398 | noteChange(); |
| 399 | }); |
| 400 | const amount = document.createElement("input"); |
| 401 | amount.type = "number"; |
| 402 | amount.min = "1"; |
| 403 | amount.step = "1"; |
| 404 | amount.value = String(Math.max(1, reward.amount | 0 || 1)); |
| 405 | amount.title = "XP amount"; |
| 406 | amount.addEventListener("input", () => |
| 407 | { |
| 408 | draft.rewards[rewardIndex].amount = Math.max(1, Number(amount.value) || 1) | 0; |
| 409 | noteChange(); |
| 410 | }); |
| 411 | row.append(skill, amount, removeButton(() => |
| 412 | { |
| 413 | draft.rewards.splice(rewardIndex, 1); |
| 414 | renderEditor(); |
| 415 | })); |
| 416 | box.append(row); |
| 417 | }); |
| 418 | |
| 419 | const searchWrap = document.createElement("div"); |
| 420 | searchWrap.className = "dialogue-reward-item-search"; |
| 421 | searchWrap.hidden = true; |
| 422 | const searchInput = document.createElement("input"); |
| 423 | searchInput.type = "search"; |
| 424 | searchInput.placeholder = "Search items…"; |
| 425 | searchInput.autocomplete = "off"; |
| 426 | searchInput.addEventListener("input", () => renderInlineItemSearch(searchInput.value)); |
| 427 | const searchResults = document.createElement("div"); |
| 428 | searchResults.className = "dialogue-reward-item-results"; |
| 429 | searchWrap.append(searchInput, searchResults); |
| 430 | |
| 431 | const itemHead = sectionHeader("Items", () => |
| 432 | { |
| 433 | searchWrap.hidden = false; |
| 434 | openInlineItemSearch(searchInput, searchResults); |
| 435 | }); |
| 436 | box.append(itemHead); |
| 437 | |
| 438 | const itemRows = (draft.rewards ?? []).filter((r) => r.kind === EffectKind.giveItem); |
| 439 | if (!itemRows.length) |
| 440 | { |
| 441 | const empty = document.createElement("p"); |
| 442 | empty.className = "content-editor-hint"; |
| 443 | empty.textContent = "No item rewards."; |
| 444 | box.append(empty); |
| 445 | } |
| 446 | itemRows.forEach((reward) => |
| 447 | { |
| 448 | const rewardIndex = draft.rewards.indexOf(reward); |
| 449 | const def = itemDefinition(reward.itemId); |
| 450 | const row = document.createElement("div"); |
| 451 | row.className = "spawner-drops-row"; |
| 452 | const name = document.createElement("span"); |
| 453 | name.className = "spawner-sells-name"; |
| 454 | name.textContent = def?.displayName ?? `Item ${reward.itemId}`; |
| 455 | const qty = document.createElement("input"); |
| 456 | qty.type = "number"; |
| 457 | qty.min = "1"; |
| 458 | qty.step = "1"; |
| 459 | qty.value = String(Math.max(1, reward.quantity | 0 || 1)); |
| 460 | qty.title = "Quantity"; |
| 461 | qty.addEventListener("input", () => |
| 462 | { |
| 463 | draft.rewards[rewardIndex].quantity = Math.max(1, Number(qty.value) || 1) | 0; |
| 464 | noteChange(); |
| 465 | }); |
| 466 | const bankLabel = document.createElement("label"); |
| 467 | bankLabel.className = "quest-reward-bank"; |
| 468 | const bankBox = document.createElement("input"); |
| 469 | bankBox.type = "checkbox"; |
| 470 | bankBox.checked = !!reward.toBank; |
| 471 | bankBox.addEventListener("change", () => |
| 472 | { |
| 473 | draft.rewards[rewardIndex].toBank = bankBox.checked; |
| 474 | noteChange(); |
| 475 | }); |
| 476 | bankLabel.append(bankBox, document.createTextNode(" Bank")); |
| 477 | row.append(name, qty, bankLabel, removeButton(() => |
| 478 | { |
| 479 | draft.rewards.splice(rewardIndex, 1); |
| 480 | renderEditor(); |
| 481 | })); |
| 482 | box.append(row); |
| 483 | }); |
| 484 | box.append(searchWrap); |
| 485 | parent.append(box); |
| 486 | } |
| 487 | |
| 488 | function render () |
| 489 | { |
| 490 | renderList(); |
| 491 | renderEditor(); |
| 492 | renderLog(); |
| 493 | } |
| 494 | |
| 495 | function renderLog () |
| 496 | { |
| 497 | if (!logEl) |
| 498 | { return; } |
| 499 | logEl.innerHTML = ""; |
| 500 | const scopeId = draft?.questId ?? selectedId; |
| 501 | const entries = scopeId |
| 502 | ? changelog.filter((entry) => entry.questId === scopeId) |
| 503 | : changelog; |
| 504 | if (!entries.length) |
| 505 | { |
| 506 | logEl.innerHTML = scopeId |
| 507 | ? "<p class=\"hint\">No edits logged for this quest yet.</p>" |
| 508 | : "<p class=\"hint\">No quest edits logged yet.</p>"; |
| 509 | return; |
| 510 | } |
| 511 | for (const entry of entries) |
| 512 | { |
| 513 | const item = document.createElement("article"); |
| 514 | const who = entry.editorDisplayName || entry.editor_admin_id; |
| 515 | const scope = scopeId ? "" : ` · ${entry.questId}`; |
| 516 | item.innerHTML = `<header>${who}${scope} · ${new Date(entry.edited_at).toLocaleString()}</header>`; |
| 517 | const list = document.createElement("ul"); |
| 518 | for (const bullet of entry.diffEntries ?? []) |
| 519 | { |
| 520 | const li = document.createElement("li"); |
| 521 | li.textContent = bullet; |
| 522 | list.append(li); |
| 523 | } |
| 524 | item.append(list); |
| 525 | logEl.append(item); |
| 526 | } |
| 527 | logEl.scrollTop = logEl.scrollHeight; |
| 528 | } |
| 529 | |
| 530 | document.getElementById("open-quests").addEventListener("click", () => |
| 531 | { |
| 532 | socket.send({ type: MessageType.editorLoadQuests }); |
| 533 | socket.send({ type: MessageType.editorLoadDialogues }); |
| 534 | show(true); |
| 535 | render(); |
| 536 | }); |
| 537 | document.getElementById("quests-close").addEventListener("click", () => show(false)); |
| 538 | backdrop.addEventListener("click", () => show(false)); |
| 539 | document.getElementById("quests-new").addEventListener("click", () => |
| 540 | { |
| 541 | if (!confirmDiscard()) |
| 542 | { return; } |
| 543 | const base = normalizeQuestDef({ |
| 544 | questId: `quest-${quests.length + 1}`, |
| 545 | title: "New quest", |
| 546 | description: "" |
| 547 | }); |
| 548 | draft = structuredClone(base); |
| 549 | selectedId = null; |
| 550 | draftBaseline = null; |
| 551 | stepModal.close(); |
| 552 | canvas.clearWiring(); |
| 553 | render(); |
| 554 | }); |
| 555 | saveBtn.addEventListener("click", () => |
| 556 | { |
| 557 | if (!draft || !isDraftDirty()) |
| 558 | { return; } |
| 559 | draft.rewards = normalizeEffects(draft.rewards); |
| 560 | prepareQuestExitsForSave(draft.steps); |
| 561 | socket.send({ type: MessageType.editorSaveQuest, quest: draft }); |
| 562 | }); |
| 563 | deleteBtn.addEventListener("click", () => |
| 564 | { |
| 565 | if (!draft?.questId) |
| 566 | { return; } |
| 567 | if (!window.confirm(`Delete quest "${draft.questId}"?`)) |
| 568 | { return; } |
| 569 | socket.send({ type: MessageType.editorDeleteQuest, questId: draft.questId }); |
| 570 | }); |
| 571 | |
| 572 | return { |
| 573 | isOpen: () => open, |
| 574 | getQuests: () => quests, |
| 575 | getServerSpawners: () => serverSpawners.slice(), |
| 576 | handleMessage (message) |
| 577 | { |
| 578 | if (message.type === MessageType.editorDialogues) |
| 579 | { |
| 580 | dialogues = (message.dialogues ?? []).map((d) => normalizeDialogueTree(d)); |
| 581 | if (open) |
| 582 | { render(); } |
| 583 | return; |
| 584 | } |
| 585 | if (message.type !== MessageType.editorQuests) |
| 586 | { return; } |
| 587 | quests = (message.quests ?? []).map((q) => normalizeQuestDef(q)); |
| 588 | serverSpawners = Array.isArray(message.spawners) ? message.spawners.slice() : []; |
| 589 | changelog = message.changelog ?? []; |
| 590 | if (draft) |
| 591 | { |
| 592 | const still = quests.find((q) => q.questId === draft.questId); |
| 593 | if (still) |
| 594 | { |
| 595 | selectedId = still.questId; |
| 596 | draft = structuredClone(still); |
| 597 | captureBaseline(); |
| 598 | stepModal.refresh(); |
| 599 | } |
| 600 | else if (selectedId) |
| 601 | { |
| 602 | draft = null; |
| 603 | selectedId = null; |
| 604 | draftBaseline = null; |
| 605 | stepModal.close(); |
| 606 | } |
| 607 | } |
| 608 | if (open) |
| 609 | { render(); } |
| 610 | } |
| 611 | }; |
| 612 | } |
| 613 | |
| 614 | function removeButton (onClick) |
| 615 | { |
| 616 | const remove = document.createElement("button"); |
| 617 | remove.type = "button"; |
| 618 | remove.className = "content-editor-remove"; |
| 619 | remove.textContent = "×"; |
| 620 | remove.title = "Remove"; |
| 621 | remove.addEventListener("click", onClick); |
| 622 | return remove; |
| 623 | } |
| 624 | |
| 625 | function field (label, input) |
| 626 | { |
| 627 | const wrap = document.createElement("label"); |
| 628 | wrap.append(document.createTextNode(label), input); |
| 629 | return wrap; |
| 630 | } |
| 631 | |
| 632 | function textInput (value, onChange) |
| 633 | { |
| 634 | const input = document.createElement("input"); |
| 635 | input.type = "text"; |
| 636 | input.value = value ?? ""; |
| 637 | input.addEventListener("input", () => onChange(input.value)); |
| 638 | return input; |
| 639 | } |
| 640 | |
| 641 | function textArea (value, onChange, rows = 3) |
| 642 | { |
| 643 | const input = document.createElement("textarea"); |
| 644 | input.rows = rows; |
| 645 | input.value = value ?? ""; |
| 646 | input.addEventListener("input", () => onChange(input.value)); |
| 647 | return input; |
| 648 | } |
| 649 | |
| 650 | function sectionHeader (title, onAdd, onRemove) |
| 651 | { |
| 652 | const head = document.createElement("div"); |
| 653 | head.className = "content-editor-section-header"; |
| 654 | const strong = document.createElement("strong"); |
| 655 | strong.textContent = title; |
| 656 | head.append(strong); |
| 657 | const actions = document.createElement("div"); |
| 658 | actions.className = "content-editor-row"; |
| 659 | if (onAdd) |
| 660 | { |
| 661 | const add = document.createElement("button"); |
| 662 | add.type = "button"; |
| 663 | add.textContent = "+"; |
| 664 | add.title = "Add"; |
| 665 | add.addEventListener("click", onAdd); |
| 666 | actions.append(add); |
| 667 | } |
| 668 | if (onRemove) |
| 669 | { actions.append(removeButton(onRemove)); } |
| 670 | head.append(actions); |
| 671 | return head; |
| 672 | } |