/** * Step panel for the quest graph editor. * Wires stay on the canvas; this panel edits text, exit kind, objectives, and branches. */ import { emptyConditionSet, EffectKind } from "/shared/quests/conditions.js"; import { relinkImplicitQuestExits } from "/shared/quests/normalize.js"; import { blankQuestStep, branchShadowsLater } from "/shared/quests/questGraph.js"; import { itemDefinition, listAllItemPickerEntries } from "/shared/definitions/items.js"; import { ALL_SKILL_IDS, SKILL_DEFINITIONS } from "/shared/definitions/skills.js"; import { renderConditionSet } from "./conditionSetEditor.js"; import { scrubDialogueStepLinks, stepHasEdges } from "./dialogueCanvas.js"; /** * @param {{ * getDraft: () => object | null, * conditionCtx: () => object, * onChange: () => void, * host?: HTMLElement | null * }} options */ export function initQuestStepModal ({ getDraft, conditionCtx, onChange, host = null }) { const root = document.createElement("div"); root.className = "dialogue-step-overlay"; root.hidden = true; const backdrop = document.createElement("div"); backdrop.className = "dialogue-step-backdrop"; const panel = document.createElement("div"); panel.className = "dialogue-step-panel quest-step-panel"; const header = document.createElement("header"); const title = document.createElement("h3"); title.textContent = "Step"; const closeBtn = document.createElement("button"); closeBtn.type = "button"; closeBtn.textContent = "Close"; header.append(title, closeBtn); const body = document.createElement("div"); body.className = "dialogue-step-body"; root.append(backdrop, panel); panel.append(header, body); (host || document.body).append(root); /** @type {string | null} */ let openStepId = null; function show (visible) { root.hidden = !visible; if (!visible) { openStepId = null; body.replaceChildren(); } } function currentStep () { const draft = getDraft(); if (!draft || !openStepId) { return null; } return draft.steps?.find((step) => step.stepId === openStepId) ?? null; } function uniqueStepId (draft, preferred) { const used = new Set((draft.steps ?? []).map((step) => step.stepId)); if (!used.has(preferred)) { return preferred; } let n = (draft.steps?.length ?? 0) + 1; let id = `step-${n}`; while (used.has(id)) { n += 1; id = `step-${n}`; } return id; } function renameStep (step, nextId) { const draft = getDraft(); const previous = step.stepId; if (!draft || !nextId || nextId === previous) { return; } if (draft.steps.some((other) => other !== step && other.stepId === nextId)) { return; } for (const other of draft.steps) { const exit = other.exit; if (!exit) { continue; } if (exit.kind === "continue" && exit.nextStepId === previous) { exit.nextStepId = nextId; } for (const branch of exit.branches ?? []) { if (branch.nextStepId === previous) { branch.nextStepId = nextId; } } } step.stepId = nextId; openStepId = nextId; onChange(); } function setExitKind (step, kind) { const carried = step.exit?.kind === "continue" ? (step.exit.nextStepId ?? null) : (step.exit?.kind === "branches" ? (step.exit.branches?.find((branch) => branch.nextStepId)?.nextStepId ?? null) : null); if (kind === "branches") { const existing = step.exit?.kind === "branches" ? (step.exit.branches ?? []) : []; const branches = existing.length ? existing : [{ label: "", text: "", when: emptyConditionSet(), nextStepId: carried, effects: [] }]; step.exit = { kind: "branches", branches }; return; } if (kind === "continue") { const prev = step.exit?.kind === "continue" ? (step.exit.nextStepId ?? null) : carried; step.exit = { kind: "continue", nextStepId: prev }; return; } step.exit = { kind: "complete" }; } function deleteStep (step) { const draft = getDraft(); if (!draft) { return; } const removedId = step.stepId; const index = draft.steps.indexOf(step); if (index < 0) { return; } draft.steps.splice(index, 1); scrubDialogueStepLinks(draft, removedId); if (draft.steps.length === 0) { draft.steps.push(blankQuestStep(uniqueStepId(draft, "step-1"), 80, 80)); } else { relinkImplicitQuestExits(draft.steps); } show(false); onChange(); } function confirmDelete (step) { const draft = getDraft(); const linked = stepHasEdges(draft, step); const message = linked ? `Delete step "${step.title || step.stepId}"? Incoming and outgoing wires to it will be cleared.` : `Delete step "${step.title || step.stepId}"?`; if (!window.confirm(message)) { return; } deleteStep(step); } function renderObjectives (step) { const head = sectionHeader("Objectives", () => { step.objectives.push({ objectiveId: `obj-${step.objectives.length + 1}`, text: "New objective", when: emptyConditionSet() }); onChange(); render(); }); body.append(head); const note = document.createElement("p"); note.className = "content-editor-hint"; note.textContent = "All objectives must be done before this step advances."; body.append(note); step.objectives.forEach((obj, objIndex) => { const objBox = document.createElement("div"); objBox.className = "content-editor-section"; objBox.append( sectionHeader(`Objective ${objIndex + 1}`, null, () => { if (!window.confirm(`Delete objective "${obj.text || obj.objectiveId || objIndex + 1}"?`)) { return; } step.objectives.splice(objIndex, 1); if (step.objectives.length === 0) { step.objectives.push({ objectiveId: "obj-1", text: "Complete this step", when: emptyConditionSet() }); } onChange(); render(); }), field("Text", textInput(obj.text, (value) => { obj.text = value; onChange(); })) ); objBox.append(renderConditionSet(obj.when, (next, { remount = true } = {}) => { obj.when = next; onChange(); if (remount) { render(); } }, conditionCtx())); body.append(objBox); }); } function renderBranches (step) { if (!Array.isArray(step.exit.branches)) { step.exit.branches = []; } const branches = step.exit.branches; const head = sectionHeader("Branches", () => { branches.push({ label: "", text: "", when: emptyConditionSet(), nextStepId: null, effects: [] }); onChange(); render(); }); body.append(head); const note = document.createElement("p"); note.className = "content-editor-hint"; 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."; body.append(note); branches.forEach((branch, index) => { const box = document.createElement("div"); box.className = "content-editor-section"; const titleText = document.createElement("span"); titleText.append( Object.assign(document.createElement("span"), { className: "quest-branch-order", textContent: `${index + 1}. ` }), document.createTextNode(branch.label?.trim() || branch.text?.trim() || "Branch") ); const headerRow = document.createElement("div"); headerRow.className = "content-editor-section-header"; const strong = document.createElement("strong"); strong.append(titleText); headerRow.append(strong); const actions = document.createElement("div"); actions.className = "content-editor-row"; actions.append( smallButton("Up", index === 0, () => moveBranch(branches, index, -1)), smallButton("Down", index === branches.length - 1, () => moveBranch(branches, index, 1)), removeButton(() => { if (!window.confirm(`Delete branch ${index + 1}?`)) { return; } branches.splice(index, 1); onChange(); render(); }) ); headerRow.append(actions); box.append(headerRow); if (branchShadowsLater(branches, index)) { const warn = document.createElement("p"); warn.className = "quest-branch-shadow-warn"; warn.textContent = "This condition is empty, so it always matches. Branches below it never run."; box.append(warn); } box.append( field("Label", textInput(branch.label ?? "", (value) => { branch.label = value; onChange(); })), field("Log text", textArea(branch.text ?? "", (value) => { branch.text = value; onChange(); }, 2)) ); box.append(renderConditionSet(branch.when, (next, { remount = true } = {}) => { branch.when = next; onChange(); if (remount) { render(); } }, conditionCtx())); box.append(renderEffects(branch)); body.append(box); }); function moveBranch (list, index, delta) { const target = index + delta; if (target < 0 || target >= list.length) { return; } const [row] = list.splice(index, 1); list.splice(target, 0, row); onChange(); render(); } } function renderEffects (branch) { if (!Array.isArray(branch.effects)) { branch.effects = []; } const wrap = document.createElement("div"); wrap.append(sectionHeader("Effects", () => { branch.effects.push(blankEffect(EffectKind.giveXp)); onChange(); render(); })); if (!branch.effects.length) { const empty = document.createElement("p"); empty.className = "content-editor-hint"; empty.textContent = "No effects. They run when this branch is taken."; wrap.append(empty); return wrap; } branch.effects.forEach((effect, effectIndex) => { const row = document.createElement("div"); row.className = "content-editor-section"; const kind = document.createElement("select"); for (const value of Object.values(EffectKind)) { const opt = document.createElement("option"); opt.value = value; opt.textContent = effectKindLabel(value); if (effect.kind === value) { opt.selected = true; } kind.append(opt); } kind.addEventListener("change", () => { branch.effects[effectIndex] = blankEffect(kind.value); onChange(); render(); }); row.append(field("Effect", kind)); if (effect.kind === EffectKind.giveXp) { row.append(xpFields(effect)); } else if (effect.kind === EffectKind.giveItem || effect.kind === EffectKind.takeItem) { row.append(itemFields(effect)); } else if (effect.kind === EffectKind.startQuest) { row.append(questFields(effect)); } row.append(removeButton(() => { branch.effects.splice(effectIndex, 1); onChange(); render(); })); wrap.append(row); }); return wrap; function xpFields (effect) { const line = document.createElement("div"); line.className = "content-editor-row"; const skill = document.createElement("select"); for (const skillId of ALL_SKILL_IDS) { const opt = document.createElement("option"); opt.value = skillId; opt.textContent = SKILL_DEFINITIONS[skillId]?.displayName ?? skillId; if (effect.skillId === skillId) { opt.selected = true; } skill.append(opt); } skill.addEventListener("change", () => { effect.skillId = skill.value; onChange(); }); const amount = document.createElement("input"); amount.type = "number"; amount.min = "1"; amount.step = "1"; amount.value = String(Math.max(1, effect.amount | 0 || 1)); amount.addEventListener("input", () => { effect.amount = Math.max(1, Number(amount.value) || 1) | 0; onChange(); }); line.append(skill, amount); return line; } function itemFields (effect) { const line = document.createElement("div"); line.className = "content-editor-row"; const select = document.createElement("select"); for (const item of listAllItemPickerEntries()) { const opt = document.createElement("option"); opt.value = String(item.itemId); opt.textContent = item.displayName; if (Number(effect.itemId) === item.itemId) { opt.selected = true; } select.append(opt); } if (effect.itemId && ![...select.options].some((opt) => opt.value === String(effect.itemId))) { const missing = document.createElement("option"); missing.value = String(effect.itemId); missing.textContent = itemDefinition(effect.itemId)?.displayName ?? `Item ${effect.itemId}`; missing.selected = true; select.append(missing); } select.addEventListener("change", () => { effect.itemId = Number(select.value) | 0; onChange(); }); const qty = document.createElement("input"); qty.type = "number"; qty.min = "1"; qty.step = "1"; qty.value = String(Math.max(1, effect.quantity | 0 || 1)); qty.addEventListener("input", () => { effect.quantity = Math.max(1, Number(qty.value) || 1) | 0; onChange(); }); line.append(select, qty); if (effect.kind === EffectKind.giveItem) { const bankLabel = document.createElement("label"); bankLabel.className = "quest-reward-bank"; const bankBox = document.createElement("input"); bankBox.type = "checkbox"; bankBox.checked = !!effect.toBank; bankBox.addEventListener("change", () => { effect.toBank = bankBox.checked; onChange(); }); bankLabel.append(bankBox, document.createTextNode(" Bank")); line.append(bankLabel); } return line; } function questFields (effect) { const select = document.createElement("select"); const quests = conditionCtx().quests ?? []; const none = document.createElement("option"); none.value = ""; none.textContent = "Choose a quest"; select.append(none); for (const quest of quests) { const opt = document.createElement("option"); opt.value = quest.questId; opt.textContent = quest.title || quest.questId; if (effect.questId === quest.questId) { opt.selected = true; } select.append(opt); } if (effect.questId && ![...select.options].some((opt) => opt.value === effect.questId)) { const missing = document.createElement("option"); missing.value = effect.questId; missing.textContent = effect.questId; missing.selected = true; select.append(missing); } select.addEventListener("change", () => { effect.questId = select.value; onChange(); }); return field("Quest", select); } } function render () { const step = currentStep(); body.replaceChildren(); if (!step) { show(false); return; } title.textContent = step.title || step.stepId || "Step"; const stepIdInput = document.createElement("input"); stepIdInput.type = "text"; stepIdInput.value = step.stepId ?? ""; stepIdInput.addEventListener("change", () => { const next = stepIdInput.value.trim(); const previous = step.stepId; renameStep(step, next); if (step.stepId === previous && next !== previous) { stepIdInput.value = previous; } }); body.append( field("Step id", stepIdInput), field("Title", textInput(step.title, (value) => { step.title = value; title.textContent = value || step.stepId; onChange(); })), field("Description", textArea(step.description, (value) => { step.description = value; onChange(); }, 2)), field("Completion journal", textArea(step.completionJournalText, (value) => { step.completionJournalText = value; onChange(); }, 2)) ); const exitKind = document.createElement("select"); for (const option of [ { value: "continue", label: "Continue" }, { value: "branches", label: "Branches" }, { value: "complete", label: "Complete" } ]) { const opt = document.createElement("option"); opt.value = option.value; opt.textContent = option.label; if ((step.exit?.kind ?? "complete") === option.value) { opt.selected = true; } exitKind.append(opt); } exitKind.addEventListener("change", () => { setExitKind(step, exitKind.value); onChange(); render(); }); body.append(field("Exit", exitKind)); const exitNote = document.createElement("p"); exitNote.className = "content-editor-hint"; if (step.exit?.kind === "branches") { exitNote.textContent = "Each branch has its own output port. A port with no wire completes the quest."; } else if (step.exit?.kind === "continue") { exitNote.textContent = "One output port. Wire it on the canvas. No wire completes the quest."; } else { exitNote.textContent = "Quest end. This card has no output port. Objectives still have to be finished first."; } body.append(exitNote); if (step.exit?.kind === "branches") { renderBranches(step); } else { renderObjectives(step); } const remove = document.createElement("button"); remove.type = "button"; remove.className = "content-editor-remove"; remove.textContent = "Delete step"; remove.addEventListener("click", () => confirmDelete(step)); body.append(remove); } closeBtn.addEventListener("click", () => show(false)); backdrop.addEventListener("click", () => show(false)); return { open (stepId) { openStepId = stepId; show(true); render(); }, close: () => show(false), isOpen: () => !!openStepId && !root.hidden, refresh () { if (openStepId) { render(); } } }; } function blankEffect (kind) { const item = listAllItemPickerEntries()[0]; if (kind === EffectKind.giveItem) { return { kind, itemId: item?.itemId ?? 1, quantity: 1, toBank: false }; } if (kind === EffectKind.takeItem) { return { kind, itemId: item?.itemId ?? 1, quantity: 1 }; } if (kind === EffectKind.startQuest) { return { kind, questId: "" }; } return { kind: EffectKind.giveXp, skillId: ALL_SKILL_IDS[0], amount: 10 }; } function effectKindLabel (kind) { if (kind === EffectKind.giveXp) { return "Give XP"; } if (kind === EffectKind.giveItem) { return "Give item"; } if (kind === EffectKind.takeItem) { return "Take item"; } if (kind === EffectKind.startQuest) { return "Start quest"; } return kind; } function smallButton (label, disabled, onClick) { const button = document.createElement("button"); button.type = "button"; button.textContent = label; button.disabled = disabled; button.addEventListener("click", onClick); return button; } function removeButton (onClick) { const remove = document.createElement("button"); remove.type = "button"; remove.className = "content-editor-remove"; remove.textContent = "×"; remove.title = "Remove"; remove.addEventListener("click", onClick); return remove; } function field (label, input) { const wrap = document.createElement("label"); wrap.append(document.createTextNode(label), input); return wrap; } function textInput (value, onChange, eventName = "input") { const input = document.createElement("input"); input.type = "text"; input.value = value ?? ""; input.addEventListener(eventName, () => onChange(input.value)); return input; } function textArea (value, onChange, rows = 3) { const input = document.createElement("textarea"); input.rows = rows; input.value = value ?? ""; input.addEventListener("input", () => onChange(input.value)); return input; } function sectionHeader (titleText, onAdd, onRemove) { const head = document.createElement("div"); head.className = "content-editor-section-header"; const strong = document.createElement("strong"); strong.textContent = titleText; head.append(strong); const actions = document.createElement("div"); actions.className = "content-editor-row"; if (onAdd) { const add = document.createElement("button"); add.type = "button"; add.textContent = "+"; add.title = "Add"; add.addEventListener("click", onAdd); actions.append(add); } if (onRemove) { actions.append(removeButton(onRemove)); } head.append(actions); return head; }