BlueGit

timfa questward

Files

questward / client / src / editor / dialogueStepModal.js

dialogueStepModal.js

dialogueStepModal.js 25.3 KiB Download Raw
1/**
2 * Step detail modal for the dialogue canvas editor.
3 */
4import { emptyConditionSet } from "/shared/quests/conditions.js";
5import {
6 dialogueTakeIsUnguaranteed,
7 unguaranteedDialogueTakes
8} from "/shared/quests/dialoguePathTakes.js";
9import { itemDefinition, listAllItemPickerEntries } from "/shared/definitions/items.js";
10import { renderConditionSet } from "./conditionSetEditor.js";
11import { scrubDialogueStepLinks, stepHasEdges } from "./dialogueCanvas.js";
12
13/**
14 * @param {{
15 * getDraft: () => object | null,
16 * getQuestCatalog: () => object[],
17 * getDialogues: () => object[],
18 * conditionCtx: () => object,
19 * onChange: () => void,
20 * host?: HTMLElement | null
21 * }} options
22 */
23export function initDialogueStepModal ({
24 getDraft,
25 getQuestCatalog,
26 getDialogues,
27 conditionCtx,
28 onChange,
29 host = null
30})
31{
32 const root = document.createElement("div");
33 root.className = "dialogue-step-overlay";
34 root.hidden = true;
35
36 const backdrop = document.createElement("div");
37 backdrop.className = "dialogue-step-backdrop";
38
39 const panel = document.createElement("div");
40 panel.className = "dialogue-step-panel";
41
42 const header = document.createElement("header");
43 const title = document.createElement("h3");
44 title.textContent = "Step";
45 const closeBtn = document.createElement("button");
46 closeBtn.type = "button";
47 closeBtn.textContent = "Close";
48 header.append(title, closeBtn);
49
50 const body = document.createElement("div");
51 body.className = "dialogue-step-body";
52
53 root.append(backdrop, panel);
54 panel.append(header, body);
55 (host || document.body).append(root);
56
57 /** @type {string | null} */
58 let openStepId = null;
59 /** @type {null | {
60 * step: object,
61 * field: "itemRewards" | "itemTakes",
62 * resultsEl: HTMLElement,
63 * inputEl: HTMLInputElement
64 * }} */
65 let itemPickContext = null;
66
67 function show (visible)
68 {
69 root.hidden = !visible;
70 if (!visible)
71 {
72 openStepId = null;
73 itemPickContext = null;
74 body.replaceChildren();
75 }
76 }
77
78 function currentStep ()
79 {
80 const draft = getDraft();
81 if (!draft || !openStepId)
82 { return null; }
83 return draft.steps.find((s) => s.stepId === openStepId) ?? null;
84 }
85
86 function closeInlineItemSearch ()
87 {
88 if (itemPickContext?.resultsEl)
89 { itemPickContext.resultsEl.replaceChildren(); }
90 itemPickContext = null;
91 }
92
93 function openInlineItemSearch (step, field, inputEl, resultsEl)
94 {
95 itemPickContext = { step, field, inputEl, resultsEl };
96 inputEl.value = "";
97 renderInlineItemSearch("");
98 inputEl.focus();
99 }
100
101 function renderInlineItemSearch (query)
102 {
103 if (!itemPickContext)
104 { return; }
105 const { step, field, resultsEl } = itemPickContext;
106 resultsEl.replaceChildren();
107 const q = String(query ?? "").trim().toLowerCase();
108 const taken = new Set((step[field] ?? []).map((row) => row.itemId));
109 const matches = listAllItemPickerEntries().filter((item) =>
110 {
111 if (taken.has(item.itemId))
112 { return false; }
113 if (!q)
114 { return true; }
115 return item.displayName.toLowerCase().includes(q)
116 || item.key.toLowerCase().includes(q)
117 || String(item.itemId).includes(q);
118 }).slice(0, 40);
119 if (!matches.length)
120 {
121 resultsEl.textContent = "No matching items.";
122 return;
123 }
124 for (const item of matches)
125 {
126 const row = document.createElement("button");
127 row.type = "button";
128 row.className = "shop-item-search-row";
129 const name = document.createElement("span");
130 name.className = "shop-item-search-row-name";
131 name.textContent = item.displayName;
132 const key = document.createElement("span");
133 key.className = "shop-item-search-row-key";
134 key.textContent = `${item.key} · #${item.itemId}`;
135 row.append(name, key);
136 row.addEventListener("click", () =>
137 {
138 if (!Array.isArray(step[field]))
139 { step[field] = []; }
140 step[field].push({ itemId: item.itemId, quantity: 1 });
141 closeInlineItemSearch();
142 onChange();
143 render();
144 });
145 resultsEl.append(row);
146 }
147 }
148
149 function refreshTakeHighlights ()
150 {
151 const draft = getDraft();
152 if (!draft)
153 { return; }
154 const warnings = unguaranteedDialogueTakes(draft);
155 for (const row of body.querySelectorAll("[data-take-step-id][data-take-item-id]"))
156 {
157 const stepId = row.getAttribute("data-take-step-id");
158 const itemId = Number(row.getAttribute("data-take-item-id"));
159 const bad = dialogueTakeIsUnguaranteed(warnings, stepId, itemId);
160 row.classList.toggle("dialogue-item-take-unguaranteed", bad);
161 row.title = bad
162 ? "Not guaranteed by every branch/trigger path to this step."
163 : "";
164 }
165 }
166
167 function openSubModal (titleText, build)
168 {
169 const sub = document.createElement("div");
170 sub.className = "dialogue-step-submodal";
171 const subPanel = document.createElement("div");
172 subPanel.className = "dialogue-step-subpanel";
173 const subHead = document.createElement("header");
174 const h = document.createElement("h4");
175 h.textContent = titleText;
176 const done = document.createElement("button");
177 done.type = "button";
178 done.textContent = "Done";
179 subHead.append(h, done);
180 const subBody = document.createElement("div");
181 subBody.className = "dialogue-step-subbody";
182 subPanel.append(subHead, subBody);
183 sub.append(subPanel);
184 panel.append(sub);
185 const close = () => sub.remove();
186 done.addEventListener("click", close);
187 sub.addEventListener("click", (event) =>
188 {
189 if (event.target === sub)
190 { close(); }
191 });
192 build(subBody, close);
193 return close;
194 }
195
196 function openRewardsUi (step)
197 {
198 openSubModal("Rewards & takes", (subBody) =>
199 {
200 if (!Array.isArray(step.itemRewards))
201 { step.itemRewards = []; }
202 if (!Array.isArray(step.itemTakes))
203 { step.itemTakes = []; }
204
205 const rewardsHead = sectionHeader("Item rewards", () =>
206 {
207 searchWrap.hidden = false;
208 openInlineItemSearch(step, "itemRewards", searchInput, searchResults);
209 });
210 subBody.append(rewardsHead);
211 const rewardsHint = document.createElement("p");
212 rewardsHint.className = "content-editor-hint";
213 rewardsHint.textContent = "Granted after all lines, before triggers/branches.";
214 subBody.append(rewardsHint);
215
216 const searchWrap = document.createElement("div");
217 searchWrap.className = "dialogue-reward-item-search";
218 searchWrap.hidden = true;
219 const searchInput = document.createElement("input");
220 searchInput.type = "search";
221 searchInput.placeholder = "Search items…";
222 searchInput.autocomplete = "off";
223 searchInput.addEventListener("input", () => renderInlineItemSearch(searchInput.value));
224 const searchResults = document.createElement("div");
225 searchResults.className = "dialogue-reward-item-results";
226 searchWrap.append(searchInput, searchResults);
227
228 const rewardsList = document.createElement("div");
229 function renderRewards ()
230 {
231 rewardsList.replaceChildren();
232 if (!step.itemRewards.length)
233 {
234 const empty = document.createElement("p");
235 empty.className = "content-editor-hint";
236 empty.textContent = "No item rewards.";
237 rewardsList.append(empty);
238 }
239 step.itemRewards.forEach((reward, rewardIndex) =>
240 {
241 const def = itemDefinition(reward.itemId);
242 const row = document.createElement("div");
243 row.className = "spawner-drops-row";
244 const name = document.createElement("span");
245 name.className = "spawner-sells-name";
246 name.textContent = def?.displayName ?? `Item ${reward.itemId}`;
247 const qty = document.createElement("input");
248 qty.type = "number";
249 qty.min = "1";
250 qty.step = "1";
251 qty.value = String(Math.max(1, reward.quantity | 0 || 1));
252 qty.title = "Quantity";
253 qty.addEventListener("input", () =>
254 {
255 step.itemRewards[rewardIndex].quantity = Math.max(1, Number(qty.value) || 1) | 0;
256 onChange();
257 });
258 row.append(name, qty, removeButton(() =>
259 {
260 step.itemRewards.splice(rewardIndex, 1);
261 onChange();
262 renderRewards();
263 render();
264 }));
265 rewardsList.append(row);
266 });
267 }
268 renderRewards();
269 subBody.append(rewardsList, searchWrap);
270
271 const takesHead = sectionHeader("Item takes", () =>
272 {
273 takeSearchWrap.hidden = false;
274 openInlineItemSearch(step, "itemTakes", takeSearchInput, takeSearchResults);
275 });
276 subBody.append(takesHead);
277 const takesHint = document.createElement("p");
278 takesHint.className = "content-editor-hint";
279 takesHint.textContent = "Removed from the bag after all lines (soft). Yellow = not guaranteed on every path.";
280 subBody.append(takesHint);
281
282 const takeSearchWrap = document.createElement("div");
283 takeSearchWrap.className = "dialogue-reward-item-search";
284 takeSearchWrap.hidden = true;
285 const takeSearchInput = document.createElement("input");
286 takeSearchInput.type = "search";
287 takeSearchInput.placeholder = "Search items…";
288 takeSearchInput.autocomplete = "off";
289 takeSearchInput.addEventListener("input", () => renderInlineItemSearch(takeSearchInput.value));
290 const takeSearchResults = document.createElement("div");
291 takeSearchResults.className = "dialogue-reward-item-results";
292 takeSearchWrap.append(takeSearchInput, takeSearchResults);
293
294 const takesList = document.createElement("div");
295 const draft = getDraft();
296 const takeWarnings = draft ? unguaranteedDialogueTakes(draft) : new Map();
297 function renderTakes ()
298 {
299 takesList.replaceChildren();
300 if (!step.itemTakes.length)
301 {
302 const empty = document.createElement("p");
303 empty.className = "content-editor-hint";
304 empty.textContent = "No item takes.";
305 takesList.append(empty);
306 }
307 step.itemTakes.forEach((take, takeIndex) =>
308 {
309 const def = itemDefinition(take.itemId);
310 const row = document.createElement("div");
311 row.className = "spawner-drops-row";
312 row.setAttribute("data-take-step-id", step.stepId);
313 row.setAttribute("data-take-item-id", String(take.itemId));
314 if (dialogueTakeIsUnguaranteed(takeWarnings, step.stepId, take.itemId))
315 {
316 row.classList.add("dialogue-item-take-unguaranteed");
317 row.title = "Not guaranteed by every branch/trigger path to this step.";
318 }
319 const name = document.createElement("span");
320 name.className = "spawner-sells-name";
321 name.textContent = def?.displayName ?? `Item ${take.itemId}`;
322 const qty = document.createElement("input");
323 qty.type = "number";
324 qty.min = "1";
325 qty.step = "1";
326 qty.value = String(Math.max(1, take.quantity | 0 || 1));
327 qty.title = "Quantity";
328 qty.addEventListener("input", () =>
329 {
330 step.itemTakes[takeIndex].quantity = Math.max(1, Number(qty.value) || 1) | 0;
331 onChange();
332 refreshTakeHighlights();
333 });
334 row.append(name, qty, removeButton(() =>
335 {
336 step.itemTakes.splice(takeIndex, 1);
337 onChange();
338 renderTakes();
339 render();
340 }));
341 takesList.append(row);
342 });
343 }
344 renderTakes();
345 subBody.append(takesList, takeSearchWrap);
346 });
347 }
348
349 function openQuestsUi (step)
350 {
351 openSubModal("Quest rewards", (subBody) =>
352 {
353 if (!Array.isArray(step.questRewards))
354 { step.questRewards = []; }
355 const questCatalog = getQuestCatalog();
356 const head = sectionHeader("Quest rewards", () =>
357 {
358 const available = questCatalog.find((q) => !step.questRewards.includes(q.questId));
359 if (!available)
360 { return; }
361 step.questRewards.push(available.questId);
362 onChange();
363 renderQuests();
364 render();
365 });
366 subBody.append(head);
367 const hint = document.createElement("p");
368 hint.className = "content-editor-hint";
369 hint.textContent = "Started after all lines (with item rewards), before triggers/branches.";
370 subBody.append(hint);
371 const list = document.createElement("div");
372 function renderQuests ()
373 {
374 list.replaceChildren();
375 if (!step.questRewards.length)
376 {
377 const empty = document.createElement("p");
378 empty.className = "content-editor-hint";
379 empty.textContent = "No quest rewards.";
380 list.append(empty);
381 }
382 step.questRewards.forEach((questId, rewardIndex) =>
383 {
384 const row = document.createElement("div");
385 row.className = "spawner-drops-row";
386 const select = document.createElement("select");
387 const taken = new Set(step.questRewards);
388 const catalog = questCatalog.length
389 ? questCatalog
390 : [{ questId, title: questId }];
391 for (const quest of catalog)
392 {
393 if (taken.has(quest.questId) && quest.questId !== questId)
394 { continue; }
395 const opt = document.createElement("option");
396 opt.value = quest.questId;
397 opt.textContent = quest.title
398 ? `${quest.title} (${quest.questId})`
399 : quest.questId;
400 select.append(opt);
401 }
402 if (![...select.options].some((o) => o.value === questId))
403 {
404 const missing = document.createElement("option");
405 missing.value = questId;
406 missing.textContent = `${questId} (missing)`;
407 select.append(missing);
408 }
409 select.value = questId;
410 select.addEventListener("change", () =>
411 {
412 step.questRewards[rewardIndex] = select.value;
413 onChange();
414 render();
415 });
416 row.append(select, removeButton(() =>
417 {
418 step.questRewards.splice(rewardIndex, 1);
419 onChange();
420 renderQuests();
421 render();
422 }));
423 list.append(row);
424 });
425 }
426 renderQuests();
427 subBody.append(list);
428 });
429 }
430
431 function openExitConfigure (step)
432 {
433 const kind = step.exit?.kind;
434 if (kind !== "branches" && kind !== "triggers")
435 { return; }
436 openSubModal(kind === "triggers" ? "Configure triggers" : "Configure branches", (subBody) =>
437 {
438 const hint = document.createElement("p");
439 hint.className = "content-editor-hint";
440 hint.textContent = "Wire next steps on the canvas. Here you edit labels, text, conditions, and effects.";
441 subBody.append(hint);
442
443 if (kind === "triggers")
444 {
445 if (!Array.isArray(step.exit.triggers))
446 { step.exit.triggers = []; }
447 const head = sectionHeader("Triggers", () =>
448 {
449 step.exit.triggers.push({
450 trigger: `choice-${step.exit.triggers.length + 1}`,
451 label: `choice-${step.exit.triggers.length + 1}`,
452 text: "Choice",
453 nextStepId: null,
454 when: emptyConditionSet(),
455 effects: []
456 });
457 onChange();
458 renderTriggers();
459 render();
460 });
461 subBody.append(head);
462 const list = document.createElement("div");
463 function renderTriggers ()
464 {
465 list.replaceChildren();
466 step.exit.triggers.forEach((trig, trigIndex) =>
467 {
468 const tBox = document.createElement("div");
469 tBox.className = "content-editor-section";
470 tBox.append(sectionHeader(`Trigger ${trigIndex + 1}`, null, () =>
471 {
472 step.exit.triggers.splice(trigIndex, 1);
473 onChange();
474 renderTriggers();
475 render();
476 }));
477 tBox.append(
478 field("trigger", textInput(trig.trigger, (v) =>
479 {
480 trig.trigger = v;
481 onChange();
482 })),
483 field("label", textInput(trig.label ?? "", (v) =>
484 {
485 trig.label = v;
486 onChange();
487 })),
488 field("text (button)", textInput(trig.text, (v) =>
489 {
490 trig.text = v;
491 onChange();
492 }))
493 );
494 tBox.append(renderConditionSet(trig.when, (next, { remount = true } = {}) =>
495 {
496 trig.when = next;
497 onChange();
498 if (remount)
499 { renderTriggers(); }
500 }, conditionCtx()));
501 list.append(tBox);
502 });
503 }
504 renderTriggers();
505 subBody.append(list);
506 return;
507 }
508
509 if (!Array.isArray(step.exit.branches))
510 { step.exit.branches = []; }
511 const head = sectionHeader("Branches", () =>
512 {
513 step.exit.branches.push({
514 label: "",
515 when: emptyConditionSet(),
516 nextStepId: null,
517 effects: []
518 });
519 onChange();
520 renderBranches();
521 render();
522 });
523 subBody.append(head);
524 const list = document.createElement("div");
525 function renderBranches ()
526 {
527 list.replaceChildren();
528 step.exit.branches.forEach((branch, branchIndex) =>
529 {
530 const bBox = document.createElement("div");
531 bBox.className = "content-editor-section";
532 bBox.append(sectionHeader(`Branch ${branchIndex + 1}`, null, () =>
533 {
534 step.exit.branches.splice(branchIndex, 1);
535 onChange();
536 renderBranches();
537 render();
538 }));
539 bBox.append(field("label", textInput(branch.label ?? "", (v) =>
540 {
541 branch.label = v;
542 onChange();
543 })));
544 bBox.append(renderConditionSet(branch.when, (next, { remount = true } = {}) =>
545 {
546 branch.when = next;
547 onChange();
548 if (remount)
549 { renderBranches(); }
550 }, conditionCtx()));
551 list.append(bBox);
552 });
553 }
554 renderBranches();
555 subBody.append(list);
556 });
557 }
558
559 function confirmDeleteStep (step)
560 {
561 const draft = getDraft();
562 if (!draft)
563 { return; }
564 const linked = stepHasEdges(draft, step);
565 if (!linked)
566 {
567 if (!window.confirm(`Delete dialogue step "${step.label || step.stepId}"?`))
568 { return; }
569 deleteStep(step);
570 return;
571 }
572 openSubModal("Delete step?", (subBody, close) =>
573 {
574 subBody.classList.add("dialogue-step-delete-warn");
575 const warn = document.createElement("p");
576 warn.className = "dialogue-step-delete-warn-text";
577 warn.textContent = `“${step.label || step.stepId}” has incoming or outgoing connections. Deleting it will remove those links.`;
578 const actions = document.createElement("div");
579 actions.className = "dialogue-step-delete-actions";
580 const cancel = document.createElement("button");
581 cancel.type = "button";
582 cancel.textContent = "Cancel";
583 cancel.addEventListener("click", close);
584 const confirm = document.createElement("button");
585 confirm.type = "button";
586 confirm.className = "dialogue-step-delete-confirm";
587 confirm.textContent = "Delete step";
588 confirm.addEventListener("click", () =>
589 {
590 close();
591 deleteStep(step);
592 });
593 actions.append(cancel, confirm);
594 subBody.append(warn, actions);
595 });
596 }
597
598 function deleteStep (step)
599 {
600 const draft = getDraft();
601 if (!draft)
602 { return; }
603 const index = draft.steps.indexOf(step);
604 if (index < 0)
605 { return; }
606 const removedId = step.stepId;
607 draft.steps.splice(index, 1);
608 scrubDialogueStepLinks(draft, removedId);
609 if (draft.entryStepId === removedId)
610 { draft.entryStepId = draft.steps[0]?.stepId ?? ""; }
611 if (draft.steps.length === 0)
612 {
613 draft.steps.push({
614 stepId: "greeting",
615 label: "Greeting",
616 lines: [{ speaker: "npc", text: "…", effects: [] }],
617 itemRewards: [],
618 itemTakes: [],
619 questRewards: [],
620 exit: { kind: "end" },
621 editorX: 80,
622 editorY: 80
623 });
624 draft.entryStepId = "greeting";
625 }
626 show(false);
627 onChange();
628 }
629
630 function setExitKind (step, kind)
631 {
632 if (kind === "triggers")
633 {
634 step.exit = {
635 kind: "triggers",
636 triggers: step.exit?.triggers?.length
637 ? step.exit.triggers
638 : [{
639 trigger: "continue",
640 label: "continue",
641 text: "Continue",
642 nextStepId: null,
643 when: emptyConditionSet(),
644 effects: []
645 }]
646 };
647 }
648 else if (kind === "branches")
649 {
650 step.exit = {
651 kind: "branches",
652 branches: step.exit?.branches?.length
653 ? step.exit.branches
654 : [{
655 label: "",
656 when: emptyConditionSet(),
657 nextStepId: null,
658 effects: []
659 }]
660 };
661 }
662 else if (kind === "continue")
663 {
664 const prevNext = step.exit?.kind === "continue"
665 ? step.exit.nextStepId
666 : null;
667 step.exit = { kind: "continue", nextStepId: prevNext ?? null };
668 }
669 else
670 { step.exit = { kind: "end" }; }
671 }
672
673 function render ()
674 {
675 const step = currentStep();
676 body.replaceChildren();
677 if (!step)
678 {
679 show(false);
680 return;
681 }
682 title.textContent = step.label || step.stepId || "Step";
683
684 body.append(
685 field("stepId", textInput(step.stepId, (v) =>
686 {
687 const draft = getDraft();
688 const oldId = step.stepId;
689 step.stepId = v;
690 if (draft?.entryStepId === oldId)
691 { draft.entryStepId = v; }
692 scrubRename(draft, oldId, v);
693 openStepId = v;
694 onChange();
695 })),
696 field("Label", textInput(step.label ?? "", (v) =>
697 {
698 step.label = v;
699 title.textContent = v || step.stepId || "Step";
700 onChange();
701 }))
702 );
703
704 const linesHead = sectionHeader("Lines", () =>
705 {
706 step.lines.push({ speaker: "npc", text: "…", effects: [] });
707 onChange();
708 render();
709 });
710 body.append(linesHead);
711 step.lines.forEach((line, lineIndex) =>
712 {
713 const row = document.createElement("div");
714 row.className = "content-editor-row";
715 const speaker = document.createElement("select");
716 for (const s of ["npc", "player"])
717 {
718 const opt = document.createElement("option");
719 opt.value = s;
720 opt.textContent = s;
721 if (line.speaker === s)
722 { opt.selected = true; }
723 speaker.append(opt);
724 }
725 speaker.addEventListener("change", () =>
726 {
727 line.speaker = speaker.value;
728 onChange();
729 });
730 const text = document.createElement("input");
731 text.type = "text";
732 text.value = line.text ?? "";
733 text.style.flex = "1";
734 text.addEventListener("input", () =>
735 {
736 line.text = text.value;
737 onChange();
738 });
739 row.append(speaker, text, removeButton(() =>
740 {
741 if (!window.confirm("Delete this dialogue line?"))
742 { return; }
743 step.lines.splice(lineIndex, 1);
744 if (step.lines.length === 0)
745 { step.lines.push({ speaker: "npc", text: "…", effects: [] }); }
746 onChange();
747 render();
748 }));
749 body.append(row);
750 });
751
752 const actions = document.createElement("div");
753 actions.className = "dialogue-step-actions";
754
755 const rewardCount = (step.itemRewards?.length ?? 0) + (step.itemTakes?.length ?? 0);
756 const rewardsBtn = document.createElement("button");
757 rewardsBtn.type = "button";
758 rewardsBtn.textContent = rewardCount > 0 ? `Rewards (${rewardCount})` : "Rewards";
759 rewardsBtn.addEventListener("click", () => openRewardsUi(step));
760
761 const questsBtn = document.createElement("button");
762 questsBtn.type = "button";
763 questsBtn.textContent = (step.questRewards?.length ?? 0) > 0
764 ? `Quests (${step.questRewards.length})`
765 : "Quests";
766 questsBtn.addEventListener("click", () => openQuestsUi(step));
767
768 const deleteBtn = document.createElement("button");
769 deleteBtn.type = "button";
770 deleteBtn.className = "content-editor-remove";
771 deleteBtn.textContent = "Delete Step";
772 deleteBtn.addEventListener("click", () => confirmDeleteStep(step));
773
774 actions.append(rewardsBtn, questsBtn, deleteBtn);
775 body.append(actions);
776
777 const exitKind = document.createElement("select");
778 for (const k of [
779 { value: "end", label: "End" },
780 { value: "branches", label: "Branches" },
781 { value: "triggers", label: "Triggers" },
782 { value: "continue", label: "Continue" }
783 ])
784 {
785 const opt = document.createElement("option");
786 opt.value = k.value;
787 opt.textContent = k.label;
788 if ((step.exit?.kind ?? "end") === k.value)
789 { opt.selected = true; }
790 exitKind.append(opt);
791 }
792 exitKind.addEventListener("change", () =>
793 {
794 setExitKind(step, exitKind.value);
795 onChange();
796 render();
797 });
798 body.append(field("Exit", exitKind));
799
800 const configure = document.createElement("button");
801 configure.type = "button";
802 configure.textContent = "Configure";
803 const canConfigure = step.exit?.kind === "branches" || step.exit?.kind === "triggers";
804 configure.disabled = !canConfigure;
805 configure.addEventListener("click", () => openExitConfigure(step));
806 body.append(configure);
807
808 if (step.exit?.kind === "continue")
809 {
810 const note = document.createElement("p");
811 note.className = "content-editor-hint";
812 note.textContent = "Continue uses a single outgoing port on the canvas.";
813 body.append(note);
814 }
815 }
816
817 function scrubRename (draft, oldId, newId)
818 {
819 if (!draft || !oldId || oldId === newId)
820 { return; }
821 for (const other of draft.steps)
822 {
823 const exit = other.exit;
824 if (!exit)
825 { continue; }
826 if (exit.kind === "continue" && exit.nextStepId === oldId)
827 { exit.nextStepId = newId; }
828 if (exit.kind === "triggers")
829 {
830 for (const trig of exit.triggers ?? [])
831 {
832 if (trig.nextStepId === oldId)
833 { trig.nextStepId = newId; }
834 }
835 }
836 if (exit.kind === "branches")
837 {
838 for (const branch of exit.branches ?? [])
839 {
840 if (branch.nextStepId === oldId)
841 { branch.nextStepId = newId; }
842 }
843 }
844 }
845 }
846
847 closeBtn.addEventListener("click", () => show(false));
848 backdrop.addEventListener("click", () => show(false));
849
850 return {
851 open (stepId)
852 {
853 openStepId = stepId;
854 show(true);
855 render();
856 },
857 close: () => show(false),
858 isOpen: () => !!openStepId && !root.hidden,
859 refresh ()
860 {
861 if (openStepId)
862 { render(); }
863 },
864 el: root
865 };
866}
867
868function field (label, input)
869{
870 const wrap = document.createElement("label");
871 wrap.append(document.createTextNode(label), input);
872 return wrap;
873}
874
875function textInput (value, onChange)
876{
877 const input = document.createElement("input");
878 input.type = "text";
879 input.value = value ?? "";
880 input.addEventListener("input", () => onChange(input.value));
881 return input;
882}
883
884function removeButton (onClick)
885{
886 const remove = document.createElement("button");
887 remove.type = "button";
888 remove.className = "content-editor-remove";
889 remove.textContent = "×";
890 remove.title = "Remove";
891 remove.addEventListener("click", onClick);
892 return remove;
893}
894
895function sectionHeader (title, onAdd, onRemove)
896{
897 const head = document.createElement("div");
898 head.className = "content-editor-section-header";
899 const strong = document.createElement("strong");
900 strong.textContent = title;
901 head.append(strong);
902 const actions = document.createElement("div");
903 actions.className = "content-editor-row";
904 if (onAdd)
905 {
906 const add = document.createElement("button");
907 add.type = "button";
908 add.textContent = "+";
909 add.title = "Add";
910 add.addEventListener("click", onAdd);
911 actions.append(add);
912 }
913 if (onRemove)
914 { actions.append(removeButton(onRemove)); }
915 head.append(actions);
916 return head;
917}