| 1 | /** |
| 2 | * Step panel for the quest graph editor. |
| 3 | * Wires stay on the canvas; this panel edits text, exit kind, objectives, and branches. |
| 4 | */ |
| 5 | import { emptyConditionSet, EffectKind } from "/shared/quests/conditions.js"; |
| 6 | import { relinkImplicitQuestExits } from "/shared/quests/normalize.js"; |
| 7 | import { blankQuestStep, branchShadowsLater } from "/shared/quests/questGraph.js"; |
| 8 | import { itemDefinition, listAllItemPickerEntries } from "/shared/definitions/items.js"; |
| 9 | import { ALL_SKILL_IDS, SKILL_DEFINITIONS } from "/shared/definitions/skills.js"; |
| 10 | import { renderConditionSet } from "./conditionSetEditor.js"; |
| 11 | import { scrubDialogueStepLinks, stepHasEdges } from "./dialogueCanvas.js"; |
| 12 | |
| 13 | /** |
| 14 | * @param {{ |
| 15 | * getDraft: () => object | null, |
| 16 | * conditionCtx: () => object, |
| 17 | * onChange: () => void, |
| 18 | * host?: HTMLElement | null |
| 19 | * }} options |
| 20 | */ |
| 21 | export function initQuestStepModal ({ |
| 22 | getDraft, |
| 23 | conditionCtx, |
| 24 | onChange, |
| 25 | host = null |
| 26 | }) |
| 27 | { |
| 28 | const root = document.createElement("div"); |
| 29 | root.className = "dialogue-step-overlay"; |
| 30 | root.hidden = true; |
| 31 | |
| 32 | const backdrop = document.createElement("div"); |
| 33 | backdrop.className = "dialogue-step-backdrop"; |
| 34 | |
| 35 | const panel = document.createElement("div"); |
| 36 | panel.className = "dialogue-step-panel quest-step-panel"; |
| 37 | |
| 38 | const header = document.createElement("header"); |
| 39 | const title = document.createElement("h3"); |
| 40 | title.textContent = "Step"; |
| 41 | const closeBtn = document.createElement("button"); |
| 42 | closeBtn.type = "button"; |
| 43 | closeBtn.textContent = "Close"; |
| 44 | header.append(title, closeBtn); |
| 45 | |
| 46 | const body = document.createElement("div"); |
| 47 | body.className = "dialogue-step-body"; |
| 48 | |
| 49 | root.append(backdrop, panel); |
| 50 | panel.append(header, body); |
| 51 | (host || document.body).append(root); |
| 52 | |
| 53 | /** @type {string | null} */ |
| 54 | let openStepId = null; |
| 55 | |
| 56 | function show (visible) |
| 57 | { |
| 58 | root.hidden = !visible; |
| 59 | if (!visible) |
| 60 | { |
| 61 | openStepId = null; |
| 62 | body.replaceChildren(); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | function currentStep () |
| 67 | { |
| 68 | const draft = getDraft(); |
| 69 | if (!draft || !openStepId) |
| 70 | { return null; } |
| 71 | return draft.steps?.find((step) => step.stepId === openStepId) ?? null; |
| 72 | } |
| 73 | |
| 74 | function uniqueStepId (draft, preferred) |
| 75 | { |
| 76 | const used = new Set((draft.steps ?? []).map((step) => step.stepId)); |
| 77 | if (!used.has(preferred)) |
| 78 | { return preferred; } |
| 79 | let n = (draft.steps?.length ?? 0) + 1; |
| 80 | let id = `step-${n}`; |
| 81 | while (used.has(id)) |
| 82 | { |
| 83 | n += 1; |
| 84 | id = `step-${n}`; |
| 85 | } |
| 86 | return id; |
| 87 | } |
| 88 | |
| 89 | function renameStep (step, nextId) |
| 90 | { |
| 91 | const draft = getDraft(); |
| 92 | const previous = step.stepId; |
| 93 | if (!draft || !nextId || nextId === previous) |
| 94 | { return; } |
| 95 | if (draft.steps.some((other) => other !== step && other.stepId === nextId)) |
| 96 | { return; } |
| 97 | for (const other of draft.steps) |
| 98 | { |
| 99 | const exit = other.exit; |
| 100 | if (!exit) |
| 101 | { continue; } |
| 102 | if (exit.kind === "continue" && exit.nextStepId === previous) |
| 103 | { exit.nextStepId = nextId; } |
| 104 | for (const branch of exit.branches ?? []) |
| 105 | { |
| 106 | if (branch.nextStepId === previous) |
| 107 | { branch.nextStepId = nextId; } |
| 108 | } |
| 109 | } |
| 110 | step.stepId = nextId; |
| 111 | openStepId = nextId; |
| 112 | onChange(); |
| 113 | } |
| 114 | |
| 115 | function setExitKind (step, kind) |
| 116 | { |
| 117 | const carried = step.exit?.kind === "continue" |
| 118 | ? (step.exit.nextStepId ?? null) |
| 119 | : (step.exit?.kind === "branches" |
| 120 | ? (step.exit.branches?.find((branch) => branch.nextStepId)?.nextStepId ?? null) |
| 121 | : null); |
| 122 | if (kind === "branches") |
| 123 | { |
| 124 | const existing = step.exit?.kind === "branches" ? (step.exit.branches ?? []) : []; |
| 125 | const branches = existing.length |
| 126 | ? existing |
| 127 | : [{ |
| 128 | label: "", |
| 129 | text: "", |
| 130 | when: emptyConditionSet(), |
| 131 | nextStepId: carried, |
| 132 | effects: [] |
| 133 | }]; |
| 134 | step.exit = { kind: "branches", branches }; |
| 135 | return; |
| 136 | } |
| 137 | if (kind === "continue") |
| 138 | { |
| 139 | const prev = step.exit?.kind === "continue" |
| 140 | ? (step.exit.nextStepId ?? null) |
| 141 | : carried; |
| 142 | step.exit = { kind: "continue", nextStepId: prev }; |
| 143 | return; |
| 144 | } |
| 145 | step.exit = { kind: "complete" }; |
| 146 | } |
| 147 | |
| 148 | function deleteStep (step) |
| 149 | { |
| 150 | const draft = getDraft(); |
| 151 | if (!draft) |
| 152 | { return; } |
| 153 | const removedId = step.stepId; |
| 154 | const index = draft.steps.indexOf(step); |
| 155 | if (index < 0) |
| 156 | { return; } |
| 157 | draft.steps.splice(index, 1); |
| 158 | scrubDialogueStepLinks(draft, removedId); |
| 159 | if (draft.steps.length === 0) |
| 160 | { draft.steps.push(blankQuestStep(uniqueStepId(draft, "step-1"), 80, 80)); } |
| 161 | else |
| 162 | { relinkImplicitQuestExits(draft.steps); } |
| 163 | show(false); |
| 164 | onChange(); |
| 165 | } |
| 166 | |
| 167 | function confirmDelete (step) |
| 168 | { |
| 169 | const draft = getDraft(); |
| 170 | const linked = stepHasEdges(draft, step); |
| 171 | const message = linked |
| 172 | ? `Delete step "${step.title || step.stepId}"? Incoming and outgoing wires to it will be cleared.` |
| 173 | : `Delete step "${step.title || step.stepId}"?`; |
| 174 | if (!window.confirm(message)) |
| 175 | { return; } |
| 176 | deleteStep(step); |
| 177 | } |
| 178 | |
| 179 | function renderObjectives (step) |
| 180 | { |
| 181 | const head = sectionHeader("Objectives", () => |
| 182 | { |
| 183 | step.objectives.push({ |
| 184 | objectiveId: `obj-${step.objectives.length + 1}`, |
| 185 | text: "New objective", |
| 186 | when: emptyConditionSet() |
| 187 | }); |
| 188 | onChange(); |
| 189 | render(); |
| 190 | }); |
| 191 | body.append(head); |
| 192 | const note = document.createElement("p"); |
| 193 | note.className = "content-editor-hint"; |
| 194 | note.textContent = "All objectives must be done before this step advances."; |
| 195 | body.append(note); |
| 196 | |
| 197 | step.objectives.forEach((obj, objIndex) => |
| 198 | { |
| 199 | const objBox = document.createElement("div"); |
| 200 | objBox.className = "content-editor-section"; |
| 201 | objBox.append( |
| 202 | sectionHeader(`Objective ${objIndex + 1}`, null, () => |
| 203 | { |
| 204 | if (!window.confirm(`Delete objective "${obj.text || obj.objectiveId || objIndex + 1}"?`)) |
| 205 | { return; } |
| 206 | step.objectives.splice(objIndex, 1); |
| 207 | if (step.objectives.length === 0) |
| 208 | { |
| 209 | step.objectives.push({ |
| 210 | objectiveId: "obj-1", |
| 211 | text: "Complete this step", |
| 212 | when: emptyConditionSet() |
| 213 | }); |
| 214 | } |
| 215 | onChange(); |
| 216 | render(); |
| 217 | }), |
| 218 | field("Text", textInput(obj.text, (value) => |
| 219 | { |
| 220 | obj.text = value; |
| 221 | onChange(); |
| 222 | })) |
| 223 | ); |
| 224 | objBox.append(renderConditionSet(obj.when, (next, { remount = true } = {}) => |
| 225 | { |
| 226 | obj.when = next; |
| 227 | onChange(); |
| 228 | if (remount) |
| 229 | { render(); } |
| 230 | }, conditionCtx())); |
| 231 | body.append(objBox); |
| 232 | }); |
| 233 | } |
| 234 | |
| 235 | function renderBranches (step) |
| 236 | { |
| 237 | if (!Array.isArray(step.exit.branches)) |
| 238 | { step.exit.branches = []; } |
| 239 | const branches = step.exit.branches; |
| 240 | const head = sectionHeader("Branches", () => |
| 241 | { |
| 242 | branches.push({ |
| 243 | label: "", |
| 244 | text: "", |
| 245 | when: emptyConditionSet(), |
| 246 | nextStepId: null, |
| 247 | effects: [] |
| 248 | }); |
| 249 | onChange(); |
| 250 | render(); |
| 251 | }); |
| 252 | body.append(head); |
| 253 | const note = document.createElement("p"); |
| 254 | note.className = "content-editor-hint"; |
| 255 | note.textContent = "First matching branch wins. The top of this list is checked first. Wire each branch from its port on the canvas. An empty next step completes the quest."; |
| 256 | body.append(note); |
| 257 | |
| 258 | branches.forEach((branch, index) => |
| 259 | { |
| 260 | const box = document.createElement("div"); |
| 261 | box.className = "content-editor-section"; |
| 262 | const titleText = document.createElement("span"); |
| 263 | titleText.append( |
| 264 | Object.assign(document.createElement("span"), { |
| 265 | className: "quest-branch-order", |
| 266 | textContent: `${index + 1}. ` |
| 267 | }), |
| 268 | document.createTextNode(branch.label?.trim() || branch.text?.trim() || "Branch") |
| 269 | ); |
| 270 | const headerRow = document.createElement("div"); |
| 271 | headerRow.className = "content-editor-section-header"; |
| 272 | const strong = document.createElement("strong"); |
| 273 | strong.append(titleText); |
| 274 | headerRow.append(strong); |
| 275 | const actions = document.createElement("div"); |
| 276 | actions.className = "content-editor-row"; |
| 277 | actions.append( |
| 278 | smallButton("Up", index === 0, () => moveBranch(branches, index, -1)), |
| 279 | smallButton("Down", index === branches.length - 1, () => moveBranch(branches, index, 1)), |
| 280 | removeButton(() => |
| 281 | { |
| 282 | if (!window.confirm(`Delete branch ${index + 1}?`)) |
| 283 | { return; } |
| 284 | branches.splice(index, 1); |
| 285 | onChange(); |
| 286 | render(); |
| 287 | }) |
| 288 | ); |
| 289 | headerRow.append(actions); |
| 290 | box.append(headerRow); |
| 291 | |
| 292 | if (branchShadowsLater(branches, index)) |
| 293 | { |
| 294 | const warn = document.createElement("p"); |
| 295 | warn.className = "quest-branch-shadow-warn"; |
| 296 | warn.textContent = "This condition is empty, so it always matches. Branches below it never run."; |
| 297 | box.append(warn); |
| 298 | } |
| 299 | |
| 300 | box.append( |
| 301 | field("Label", textInput(branch.label ?? "", (value) => |
| 302 | { |
| 303 | branch.label = value; |
| 304 | onChange(); |
| 305 | })), |
| 306 | field("Log text", textArea(branch.text ?? "", (value) => |
| 307 | { |
| 308 | branch.text = value; |
| 309 | onChange(); |
| 310 | }, 2)) |
| 311 | ); |
| 312 | box.append(renderConditionSet(branch.when, (next, { remount = true } = {}) => |
| 313 | { |
| 314 | branch.when = next; |
| 315 | onChange(); |
| 316 | if (remount) |
| 317 | { render(); } |
| 318 | }, conditionCtx())); |
| 319 | box.append(renderEffects(branch)); |
| 320 | body.append(box); |
| 321 | }); |
| 322 | |
| 323 | function moveBranch (list, index, delta) |
| 324 | { |
| 325 | const target = index + delta; |
| 326 | if (target < 0 || target >= list.length) |
| 327 | { return; } |
| 328 | const [row] = list.splice(index, 1); |
| 329 | list.splice(target, 0, row); |
| 330 | onChange(); |
| 331 | render(); |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | function renderEffects (branch) |
| 336 | { |
| 337 | if (!Array.isArray(branch.effects)) |
| 338 | { branch.effects = []; } |
| 339 | const wrap = document.createElement("div"); |
| 340 | wrap.append(sectionHeader("Effects", () => |
| 341 | { |
| 342 | branch.effects.push(blankEffect(EffectKind.giveXp)); |
| 343 | onChange(); |
| 344 | render(); |
| 345 | })); |
| 346 | if (!branch.effects.length) |
| 347 | { |
| 348 | const empty = document.createElement("p"); |
| 349 | empty.className = "content-editor-hint"; |
| 350 | empty.textContent = "No effects. They run when this branch is taken."; |
| 351 | wrap.append(empty); |
| 352 | return wrap; |
| 353 | } |
| 354 | branch.effects.forEach((effect, effectIndex) => |
| 355 | { |
| 356 | const row = document.createElement("div"); |
| 357 | row.className = "content-editor-section"; |
| 358 | const kind = document.createElement("select"); |
| 359 | for (const value of Object.values(EffectKind)) |
| 360 | { |
| 361 | const opt = document.createElement("option"); |
| 362 | opt.value = value; |
| 363 | opt.textContent = effectKindLabel(value); |
| 364 | if (effect.kind === value) |
| 365 | { opt.selected = true; } |
| 366 | kind.append(opt); |
| 367 | } |
| 368 | kind.addEventListener("change", () => |
| 369 | { |
| 370 | branch.effects[effectIndex] = blankEffect(kind.value); |
| 371 | onChange(); |
| 372 | render(); |
| 373 | }); |
| 374 | row.append(field("Effect", kind)); |
| 375 | if (effect.kind === EffectKind.giveXp) |
| 376 | { row.append(xpFields(effect)); } |
| 377 | else if (effect.kind === EffectKind.giveItem || effect.kind === EffectKind.takeItem) |
| 378 | { row.append(itemFields(effect)); } |
| 379 | else if (effect.kind === EffectKind.startQuest) |
| 380 | { row.append(questFields(effect)); } |
| 381 | row.append(removeButton(() => |
| 382 | { |
| 383 | branch.effects.splice(effectIndex, 1); |
| 384 | onChange(); |
| 385 | render(); |
| 386 | })); |
| 387 | wrap.append(row); |
| 388 | }); |
| 389 | return wrap; |
| 390 | |
| 391 | function xpFields (effect) |
| 392 | { |
| 393 | const line = document.createElement("div"); |
| 394 | line.className = "content-editor-row"; |
| 395 | const skill = document.createElement("select"); |
| 396 | for (const skillId of ALL_SKILL_IDS) |
| 397 | { |
| 398 | const opt = document.createElement("option"); |
| 399 | opt.value = skillId; |
| 400 | opt.textContent = SKILL_DEFINITIONS[skillId]?.displayName ?? skillId; |
| 401 | if (effect.skillId === skillId) |
| 402 | { opt.selected = true; } |
| 403 | skill.append(opt); |
| 404 | } |
| 405 | skill.addEventListener("change", () => |
| 406 | { |
| 407 | effect.skillId = skill.value; |
| 408 | onChange(); |
| 409 | }); |
| 410 | const amount = document.createElement("input"); |
| 411 | amount.type = "number"; |
| 412 | amount.min = "1"; |
| 413 | amount.step = "1"; |
| 414 | amount.value = String(Math.max(1, effect.amount | 0 || 1)); |
| 415 | amount.addEventListener("input", () => |
| 416 | { |
| 417 | effect.amount = Math.max(1, Number(amount.value) || 1) | 0; |
| 418 | onChange(); |
| 419 | }); |
| 420 | line.append(skill, amount); |
| 421 | return line; |
| 422 | } |
| 423 | |
| 424 | function itemFields (effect) |
| 425 | { |
| 426 | const line = document.createElement("div"); |
| 427 | line.className = "content-editor-row"; |
| 428 | const select = document.createElement("select"); |
| 429 | for (const item of listAllItemPickerEntries()) |
| 430 | { |
| 431 | const opt = document.createElement("option"); |
| 432 | opt.value = String(item.itemId); |
| 433 | opt.textContent = item.displayName; |
| 434 | if (Number(effect.itemId) === item.itemId) |
| 435 | { opt.selected = true; } |
| 436 | select.append(opt); |
| 437 | } |
| 438 | if (effect.itemId && ![...select.options].some((opt) => opt.value === String(effect.itemId))) |
| 439 | { |
| 440 | const missing = document.createElement("option"); |
| 441 | missing.value = String(effect.itemId); |
| 442 | missing.textContent = itemDefinition(effect.itemId)?.displayName ?? `Item ${effect.itemId}`; |
| 443 | missing.selected = true; |
| 444 | select.append(missing); |
| 445 | } |
| 446 | select.addEventListener("change", () => |
| 447 | { |
| 448 | effect.itemId = Number(select.value) | 0; |
| 449 | onChange(); |
| 450 | }); |
| 451 | const qty = document.createElement("input"); |
| 452 | qty.type = "number"; |
| 453 | qty.min = "1"; |
| 454 | qty.step = "1"; |
| 455 | qty.value = String(Math.max(1, effect.quantity | 0 || 1)); |
| 456 | qty.addEventListener("input", () => |
| 457 | { |
| 458 | effect.quantity = Math.max(1, Number(qty.value) || 1) | 0; |
| 459 | onChange(); |
| 460 | }); |
| 461 | line.append(select, qty); |
| 462 | if (effect.kind === EffectKind.giveItem) |
| 463 | { |
| 464 | const bankLabel = document.createElement("label"); |
| 465 | bankLabel.className = "quest-reward-bank"; |
| 466 | const bankBox = document.createElement("input"); |
| 467 | bankBox.type = "checkbox"; |
| 468 | bankBox.checked = !!effect.toBank; |
| 469 | bankBox.addEventListener("change", () => |
| 470 | { |
| 471 | effect.toBank = bankBox.checked; |
| 472 | onChange(); |
| 473 | }); |
| 474 | bankLabel.append(bankBox, document.createTextNode(" Bank")); |
| 475 | line.append(bankLabel); |
| 476 | } |
| 477 | return line; |
| 478 | } |
| 479 | |
| 480 | function questFields (effect) |
| 481 | { |
| 482 | const select = document.createElement("select"); |
| 483 | const quests = conditionCtx().quests ?? []; |
| 484 | const none = document.createElement("option"); |
| 485 | none.value = ""; |
| 486 | none.textContent = "Choose a quest"; |
| 487 | select.append(none); |
| 488 | for (const quest of quests) |
| 489 | { |
| 490 | const opt = document.createElement("option"); |
| 491 | opt.value = quest.questId; |
| 492 | opt.textContent = quest.title || quest.questId; |
| 493 | if (effect.questId === quest.questId) |
| 494 | { opt.selected = true; } |
| 495 | select.append(opt); |
| 496 | } |
| 497 | if (effect.questId && ![...select.options].some((opt) => opt.value === effect.questId)) |
| 498 | { |
| 499 | const missing = document.createElement("option"); |
| 500 | missing.value = effect.questId; |
| 501 | missing.textContent = effect.questId; |
| 502 | missing.selected = true; |
| 503 | select.append(missing); |
| 504 | } |
| 505 | select.addEventListener("change", () => |
| 506 | { |
| 507 | effect.questId = select.value; |
| 508 | onChange(); |
| 509 | }); |
| 510 | return field("Quest", select); |
| 511 | } |
| 512 | } |
| 513 | |
| 514 | function render () |
| 515 | { |
| 516 | const step = currentStep(); |
| 517 | body.replaceChildren(); |
| 518 | if (!step) |
| 519 | { |
| 520 | show(false); |
| 521 | return; |
| 522 | } |
| 523 | title.textContent = step.title || step.stepId || "Step"; |
| 524 | const stepIdInput = document.createElement("input"); |
| 525 | stepIdInput.type = "text"; |
| 526 | stepIdInput.value = step.stepId ?? ""; |
| 527 | stepIdInput.addEventListener("change", () => |
| 528 | { |
| 529 | const next = stepIdInput.value.trim(); |
| 530 | const previous = step.stepId; |
| 531 | renameStep(step, next); |
| 532 | if (step.stepId === previous && next !== previous) |
| 533 | { stepIdInput.value = previous; } |
| 534 | }); |
| 535 | body.append( |
| 536 | field("Step id", stepIdInput), |
| 537 | field("Title", textInput(step.title, (value) => |
| 538 | { |
| 539 | step.title = value; |
| 540 | title.textContent = value || step.stepId; |
| 541 | onChange(); |
| 542 | })), |
| 543 | field("Description", textArea(step.description, (value) => |
| 544 | { |
| 545 | step.description = value; |
| 546 | onChange(); |
| 547 | }, 2)), |
| 548 | field("Completion journal", textArea(step.completionJournalText, (value) => |
| 549 | { |
| 550 | step.completionJournalText = value; |
| 551 | onChange(); |
| 552 | }, 2)) |
| 553 | ); |
| 554 | |
| 555 | const exitKind = document.createElement("select"); |
| 556 | for (const option of [ |
| 557 | { value: "continue", label: "Continue" }, |
| 558 | { value: "branches", label: "Branches" }, |
| 559 | { value: "complete", label: "Complete" } |
| 560 | ]) |
| 561 | { |
| 562 | const opt = document.createElement("option"); |
| 563 | opt.value = option.value; |
| 564 | opt.textContent = option.label; |
| 565 | if ((step.exit?.kind ?? "complete") === option.value) |
| 566 | { opt.selected = true; } |
| 567 | exitKind.append(opt); |
| 568 | } |
| 569 | exitKind.addEventListener("change", () => |
| 570 | { |
| 571 | setExitKind(step, exitKind.value); |
| 572 | onChange(); |
| 573 | render(); |
| 574 | }); |
| 575 | body.append(field("Exit", exitKind)); |
| 576 | |
| 577 | const exitNote = document.createElement("p"); |
| 578 | exitNote.className = "content-editor-hint"; |
| 579 | if (step.exit?.kind === "branches") |
| 580 | { exitNote.textContent = "Each branch has its own output port. A port with no wire completes the quest."; } |
| 581 | else if (step.exit?.kind === "continue") |
| 582 | { exitNote.textContent = "One output port. Wire it on the canvas. No wire completes the quest."; } |
| 583 | else |
| 584 | { exitNote.textContent = "Quest end. This card has no output port. Objectives still have to be finished first."; } |
| 585 | body.append(exitNote); |
| 586 | |
| 587 | if (step.exit?.kind === "branches") |
| 588 | { renderBranches(step); } |
| 589 | else |
| 590 | { renderObjectives(step); } |
| 591 | |
| 592 | const remove = document.createElement("button"); |
| 593 | remove.type = "button"; |
| 594 | remove.className = "content-editor-remove"; |
| 595 | remove.textContent = "Delete step"; |
| 596 | remove.addEventListener("click", () => confirmDelete(step)); |
| 597 | body.append(remove); |
| 598 | } |
| 599 | |
| 600 | closeBtn.addEventListener("click", () => show(false)); |
| 601 | backdrop.addEventListener("click", () => show(false)); |
| 602 | |
| 603 | return { |
| 604 | open (stepId) |
| 605 | { |
| 606 | openStepId = stepId; |
| 607 | show(true); |
| 608 | render(); |
| 609 | }, |
| 610 | close: () => show(false), |
| 611 | isOpen: () => !!openStepId && !root.hidden, |
| 612 | refresh () |
| 613 | { |
| 614 | if (openStepId) |
| 615 | { render(); } |
| 616 | } |
| 617 | }; |
| 618 | } |
| 619 | |
| 620 | function blankEffect (kind) |
| 621 | { |
| 622 | const item = listAllItemPickerEntries()[0]; |
| 623 | if (kind === EffectKind.giveItem) |
| 624 | { return { kind, itemId: item?.itemId ?? 1, quantity: 1, toBank: false }; } |
| 625 | if (kind === EffectKind.takeItem) |
| 626 | { return { kind, itemId: item?.itemId ?? 1, quantity: 1 }; } |
| 627 | if (kind === EffectKind.startQuest) |
| 628 | { return { kind, questId: "" }; } |
| 629 | return { kind: EffectKind.giveXp, skillId: ALL_SKILL_IDS[0], amount: 10 }; |
| 630 | } |
| 631 | |
| 632 | function effectKindLabel (kind) |
| 633 | { |
| 634 | if (kind === EffectKind.giveXp) |
| 635 | { return "Give XP"; } |
| 636 | if (kind === EffectKind.giveItem) |
| 637 | { return "Give item"; } |
| 638 | if (kind === EffectKind.takeItem) |
| 639 | { return "Take item"; } |
| 640 | if (kind === EffectKind.startQuest) |
| 641 | { return "Start quest"; } |
| 642 | return kind; |
| 643 | } |
| 644 | |
| 645 | function smallButton (label, disabled, onClick) |
| 646 | { |
| 647 | const button = document.createElement("button"); |
| 648 | button.type = "button"; |
| 649 | button.textContent = label; |
| 650 | button.disabled = disabled; |
| 651 | button.addEventListener("click", onClick); |
| 652 | return button; |
| 653 | } |
| 654 | |
| 655 | function removeButton (onClick) |
| 656 | { |
| 657 | const remove = document.createElement("button"); |
| 658 | remove.type = "button"; |
| 659 | remove.className = "content-editor-remove"; |
| 660 | remove.textContent = "×"; |
| 661 | remove.title = "Remove"; |
| 662 | remove.addEventListener("click", onClick); |
| 663 | return remove; |
| 664 | } |
| 665 | |
| 666 | function field (label, input) |
| 667 | { |
| 668 | const wrap = document.createElement("label"); |
| 669 | wrap.append(document.createTextNode(label), input); |
| 670 | return wrap; |
| 671 | } |
| 672 | |
| 673 | function textInput (value, onChange, eventName = "input") |
| 674 | { |
| 675 | const input = document.createElement("input"); |
| 676 | input.type = "text"; |
| 677 | input.value = value ?? ""; |
| 678 | input.addEventListener(eventName, () => onChange(input.value)); |
| 679 | return input; |
| 680 | } |
| 681 | |
| 682 | function textArea (value, onChange, rows = 3) |
| 683 | { |
| 684 | const input = document.createElement("textarea"); |
| 685 | input.rows = rows; |
| 686 | input.value = value ?? ""; |
| 687 | input.addEventListener("input", () => onChange(input.value)); |
| 688 | return input; |
| 689 | } |
| 690 | |
| 691 | function sectionHeader (titleText, onAdd, onRemove) |
| 692 | { |
| 693 | const head = document.createElement("div"); |
| 694 | head.className = "content-editor-section-header"; |
| 695 | const strong = document.createElement("strong"); |
| 696 | strong.textContent = titleText; |
| 697 | head.append(strong); |
| 698 | const actions = document.createElement("div"); |
| 699 | actions.className = "content-editor-row"; |
| 700 | if (onAdd) |
| 701 | { |
| 702 | const add = document.createElement("button"); |
| 703 | add.type = "button"; |
| 704 | add.textContent = "+"; |
| 705 | add.title = "Add"; |
| 706 | add.addEventListener("click", onAdd); |
| 707 | actions.append(add); |
| 708 | } |
| 709 | if (onRemove) |
| 710 | { actions.append(removeButton(onRemove)); } |
| 711 | head.append(actions); |
| 712 | return head; |
| 713 | } |