BlueGit

timfa questward

Files

questward / client / src / editor / dialogueStepModal.js

dialogueStepModal.js

dialogueStepModal.js 25.2 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 = kind === "triggers"
441 ? "Wire next steps on the canvas. Here you edit button text and conditions."
442 : "Wire next steps on the canvas. Here you edit labels and conditions.";
443 subBody.append(hint);
444
445 if (kind === "triggers")
446 {
447 if (!Array.isArray(step.exit.triggers))
448 { step.exit.triggers = []; }
449 const head = sectionHeader("Triggers", () =>
450 {
451 step.exit.triggers.push({
452 trigger: `choice-${step.exit.triggers.length + 1}`,
453 text: "Choice",
454 nextStepId: null,
455 when: emptyConditionSet(),
456 effects: []
457 });
458 onChange();
459 renderTriggers();
460 render();
461 });
462 subBody.append(head);
463 const list = document.createElement("div");
464 function renderTriggers ()
465 {
466 list.replaceChildren();
467 step.exit.triggers.forEach((trig, trigIndex) =>
468 {
469 const tBox = document.createElement("div");
470 tBox.className = "content-editor-section";
471 tBox.append(sectionHeader(`Trigger ${trigIndex + 1}`, null, () =>
472 {
473 step.exit.triggers.splice(trigIndex, 1);
474 onChange();
475 renderTriggers();
476 render();
477 }));
478 tBox.append(
479 field("trigger", textInput(trig.trigger, (v) =>
480 {
481 trig.trigger = v;
482 onChange();
483 })),
484 field("Button", textInput(trig.text ?? "", (v) =>
485 {
486 trig.text = v;
487 onChange();
488 render();
489 }))
490 );
491 tBox.append(renderConditionSet(trig.when, (next, { remount = true } = {}) =>
492 {
493 trig.when = next;
494 onChange();
495 if (remount)
496 { renderTriggers(); }
497 }, conditionCtx()));
498 list.append(tBox);
499 });
500 }
501 renderTriggers();
502 subBody.append(list);
503 return;
504 }
505
506 if (!Array.isArray(step.exit.branches))
507 { step.exit.branches = []; }
508 const head = sectionHeader("Branches", () =>
509 {
510 step.exit.branches.push({
511 label: "",
512 when: emptyConditionSet(),
513 nextStepId: null,
514 effects: []
515 });
516 onChange();
517 renderBranches();
518 render();
519 });
520 subBody.append(head);
521 const list = document.createElement("div");
522 function renderBranches ()
523 {
524 list.replaceChildren();
525 step.exit.branches.forEach((branch, branchIndex) =>
526 {
527 const bBox = document.createElement("div");
528 bBox.className = "content-editor-section";
529 bBox.append(sectionHeader(`Branch ${branchIndex + 1}`, null, () =>
530 {
531 step.exit.branches.splice(branchIndex, 1);
532 onChange();
533 renderBranches();
534 render();
535 }));
536 bBox.append(field("label", textInput(branch.label ?? "", (v) =>
537 {
538 branch.label = v;
539 onChange();
540 })));
541 bBox.append(renderConditionSet(branch.when, (next, { remount = true } = {}) =>
542 {
543 branch.when = next;
544 onChange();
545 if (remount)
546 { renderBranches(); }
547 }, conditionCtx()));
548 list.append(bBox);
549 });
550 }
551 renderBranches();
552 subBody.append(list);
553 });
554 }
555
556 function confirmDeleteStep (step)
557 {
558 const draft = getDraft();
559 if (!draft)
560 { return; }
561 const linked = stepHasEdges(draft, step);
562 if (!linked)
563 {
564 if (!window.confirm(`Delete dialogue step "${step.label || step.stepId}"?`))
565 { return; }
566 deleteStep(step);
567 return;
568 }
569 openSubModal("Delete step?", (subBody, close) =>
570 {
571 subBody.classList.add("dialogue-step-delete-warn");
572 const warn = document.createElement("p");
573 warn.className = "dialogue-step-delete-warn-text";
574 warn.textContent = `“${step.label || step.stepId}” has incoming or outgoing connections. Deleting it will remove those links.`;
575 const actions = document.createElement("div");
576 actions.className = "dialogue-step-delete-actions";
577 const cancel = document.createElement("button");
578 cancel.type = "button";
579 cancel.textContent = "Cancel";
580 cancel.addEventListener("click", close);
581 const confirm = document.createElement("button");
582 confirm.type = "button";
583 confirm.className = "dialogue-step-delete-confirm";
584 confirm.textContent = "Delete step";
585 confirm.addEventListener("click", () =>
586 {
587 close();
588 deleteStep(step);
589 });
590 actions.append(cancel, confirm);
591 subBody.append(warn, actions);
592 });
593 }
594
595 function deleteStep (step)
596 {
597 const draft = getDraft();
598 if (!draft)
599 { return; }
600 const index = draft.steps.indexOf(step);
601 if (index < 0)
602 { return; }
603 const removedId = step.stepId;
604 draft.steps.splice(index, 1);
605 scrubDialogueStepLinks(draft, removedId);
606 if (draft.entryStepId === removedId)
607 { draft.entryStepId = draft.steps[0]?.stepId ?? ""; }
608 if (draft.steps.length === 0)
609 {
610 draft.steps.push({
611 stepId: "greeting",
612 label: "Greeting",
613 lines: [{ speaker: "npc", text: "…", effects: [] }],
614 itemRewards: [],
615 itemTakes: [],
616 questRewards: [],
617 exit: { kind: "end" },
618 editorX: 80,
619 editorY: 80
620 });
621 draft.entryStepId = "greeting";
622 }
623 show(false);
624 onChange();
625 }
626
627 function setExitKind (step, kind)
628 {
629 if (kind === "triggers")
630 {
631 step.exit = {
632 kind: "triggers",
633 triggers: step.exit?.triggers?.length
634 ? step.exit.triggers
635 : [{
636 trigger: "continue",
637 text: "Continue",
638 nextStepId: null,
639 when: emptyConditionSet(),
640 effects: []
641 }]
642 };
643 }
644 else if (kind === "branches")
645 {
646 step.exit = {
647 kind: "branches",
648 branches: step.exit?.branches?.length
649 ? step.exit.branches
650 : [{
651 label: "",
652 when: emptyConditionSet(),
653 nextStepId: null,
654 effects: []
655 }]
656 };
657 }
658 else if (kind === "continue")
659 {
660 const prevNext = step.exit?.kind === "continue"
661 ? step.exit.nextStepId
662 : null;
663 step.exit = { kind: "continue", nextStepId: prevNext ?? null };
664 }
665 else
666 { step.exit = { kind: "end" }; }
667 }
668
669 function render ()
670 {
671 const step = currentStep();
672 body.replaceChildren();
673 if (!step)
674 {
675 show(false);
676 return;
677 }
678 title.textContent = step.label || step.stepId || "Step";
679
680 body.append(
681 field("stepId", textInput(step.stepId, (v) =>
682 {
683 const draft = getDraft();
684 const oldId = step.stepId;
685 step.stepId = v;
686 if (draft?.entryStepId === oldId)
687 { draft.entryStepId = v; }
688 scrubRename(draft, oldId, v);
689 openStepId = v;
690 onChange();
691 })),
692 field("Label", textInput(step.label ?? "", (v) =>
693 {
694 step.label = v;
695 title.textContent = v || step.stepId || "Step";
696 onChange();
697 }))
698 );
699
700 const linesHead = sectionHeader("Lines", () =>
701 {
702 step.lines.push({ speaker: "npc", text: "…", effects: [] });
703 onChange();
704 render();
705 });
706 body.append(linesHead);
707 step.lines.forEach((line, lineIndex) =>
708 {
709 const row = document.createElement("div");
710 row.className = "content-editor-row";
711 const speaker = document.createElement("select");
712 for (const s of ["npc", "player"])
713 {
714 const opt = document.createElement("option");
715 opt.value = s;
716 opt.textContent = s;
717 if (line.speaker === s)
718 { opt.selected = true; }
719 speaker.append(opt);
720 }
721 speaker.addEventListener("change", () =>
722 {
723 line.speaker = speaker.value;
724 onChange();
725 });
726 const text = document.createElement("input");
727 text.type = "text";
728 text.value = line.text ?? "";
729 text.style.flex = "1";
730 text.addEventListener("input", () =>
731 {
732 line.text = text.value;
733 onChange();
734 });
735 row.append(speaker, text, removeButton(() =>
736 {
737 if (!window.confirm("Delete this dialogue line?"))
738 { return; }
739 step.lines.splice(lineIndex, 1);
740 if (step.lines.length === 0)
741 { step.lines.push({ speaker: "npc", text: "…", effects: [] }); }
742 onChange();
743 render();
744 }));
745 body.append(row);
746 });
747
748 const actions = document.createElement("div");
749 actions.className = "dialogue-step-actions";
750
751 const rewardCount = (step.itemRewards?.length ?? 0) + (step.itemTakes?.length ?? 0);
752 const rewardsBtn = document.createElement("button");
753 rewardsBtn.type = "button";
754 rewardsBtn.textContent = rewardCount > 0 ? `Rewards (${rewardCount})` : "Rewards";
755 rewardsBtn.addEventListener("click", () => openRewardsUi(step));
756
757 const questsBtn = document.createElement("button");
758 questsBtn.type = "button";
759 questsBtn.textContent = (step.questRewards?.length ?? 0) > 0
760 ? `Quests (${step.questRewards.length})`
761 : "Quests";
762 questsBtn.addEventListener("click", () => openQuestsUi(step));
763
764 const deleteBtn = document.createElement("button");
765 deleteBtn.type = "button";
766 deleteBtn.className = "content-editor-remove";
767 deleteBtn.textContent = "Delete Step";
768 deleteBtn.addEventListener("click", () => confirmDeleteStep(step));
769
770 actions.append(rewardsBtn, questsBtn, deleteBtn);
771 body.append(actions);
772
773 const exitKind = document.createElement("select");
774 for (const k of [
775 { value: "end", label: "End" },
776 { value: "branches", label: "Branches" },
777 { value: "triggers", label: "Triggers" },
778 { value: "continue", label: "Continue" }
779 ])
780 {
781 const opt = document.createElement("option");
782 opt.value = k.value;
783 opt.textContent = k.label;
784 if ((step.exit?.kind ?? "end") === k.value)
785 { opt.selected = true; }
786 exitKind.append(opt);
787 }
788 exitKind.addEventListener("change", () =>
789 {
790 setExitKind(step, exitKind.value);
791 onChange();
792 render();
793 });
794 body.append(field("Exit", exitKind));
795
796 const configure = document.createElement("button");
797 configure.type = "button";
798 configure.textContent = "Configure";
799 const canConfigure = step.exit?.kind === "branches" || step.exit?.kind === "triggers";
800 configure.disabled = !canConfigure;
801 configure.addEventListener("click", () => openExitConfigure(step));
802 body.append(configure);
803
804 if (step.exit?.kind === "continue")
805 {
806 const note = document.createElement("p");
807 note.className = "content-editor-hint";
808 note.textContent = "Continue uses a single outgoing port on the canvas.";
809 body.append(note);
810 }
811 }
812
813 function scrubRename (draft, oldId, newId)
814 {
815 if (!draft || !oldId || oldId === newId)
816 { return; }
817 for (const other of draft.steps)
818 {
819 const exit = other.exit;
820 if (!exit)
821 { continue; }
822 if (exit.kind === "continue" && exit.nextStepId === oldId)
823 { exit.nextStepId = newId; }
824 if (exit.kind === "triggers")
825 {
826 for (const trig of exit.triggers ?? [])
827 {
828 if (trig.nextStepId === oldId)
829 { trig.nextStepId = newId; }
830 }
831 }
832 if (exit.kind === "branches")
833 {
834 for (const branch of exit.branches ?? [])
835 {
836 if (branch.nextStepId === oldId)
837 { branch.nextStepId = newId; }
838 }
839 }
840 }
841 }
842
843 closeBtn.addEventListener("click", () => show(false));
844 backdrop.addEventListener("click", () => show(false));
845
846 return {
847 open (stepId)
848 {
849 openStepId = stepId;
850 show(true);
851 render();
852 },
853 close: () => show(false),
854 isOpen: () => !!openStepId && !root.hidden,
855 refresh ()
856 {
857 if (openStepId)
858 { render(); }
859 },
860 el: root
861 };
862}
863
864function field (label, input)
865{
866 const wrap = document.createElement("label");
867 wrap.append(document.createTextNode(label), input);
868 return wrap;
869}
870
871function textInput (value, onChange)
872{
873 const input = document.createElement("input");
874 input.type = "text";
875 input.value = value ?? "";
876 input.addEventListener("input", () => onChange(input.value));
877 return input;
878}
879
880function removeButton (onClick)
881{
882 const remove = document.createElement("button");
883 remove.type = "button";
884 remove.className = "content-editor-remove";
885 remove.textContent = "×";
886 remove.title = "Remove";
887 remove.addEventListener("click", onClick);
888 return remove;
889}
890
891function sectionHeader (title, onAdd, onRemove)
892{
893 const head = document.createElement("div");
894 head.className = "content-editor-section-header";
895 const strong = document.createElement("strong");
896 strong.textContent = title;
897 head.append(strong);
898 const actions = document.createElement("div");
899 actions.className = "content-editor-row";
900 if (onAdd)
901 {
902 const add = document.createElement("button");
903 add.type = "button";
904 add.textContent = "+";
905 add.title = "Add";
906 add.addEventListener("click", onAdd);
907 actions.append(add);
908 }
909 if (onRemove)
910 { actions.append(removeButton(onRemove)); }
911 head.append(actions);
912 return head;
913}