undercity 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/AGENTS.md +26 -0
- package/README.md +58 -0
- package/actions/AGENTS.md +41 -0
- package/actions/_shared/container.js +16 -0
- package/actions/auth/ask-login/action.json +15 -0
- package/actions/auth/ask-signup/action.json +14 -0
- package/actions/display/clear/action.json +18 -0
- package/actions/display/clear/action.test.js +32 -0
- package/actions/display/markdown/action.json +12 -0
- package/actions/display/markdown/action.test.js +32 -0
- package/actions/display/rawHtml/action.json +24 -0
- package/actions/display/rawHtml/action.test.js +32 -0
- package/actions/display/safeHtml/action.json +24 -0
- package/actions/display/safeHtml/action.test.js +32 -0
- package/actions/display/text/action.js +9 -0
- package/actions/display/text/action.json +12 -0
- package/actions/display/text/action.test.js +40 -0
- package/actions/display/value/action.json +24 -0
- package/actions/display/value/action.test.js +32 -0
- package/actions/dom/addClass/action.json +23 -0
- package/actions/dom/addClass/action.test.js +32 -0
- package/actions/dom/focus/action.json +17 -0
- package/actions/dom/focus/action.test.js +32 -0
- package/actions/dom/hide/action.json +18 -0
- package/actions/dom/hide/action.test.js +32 -0
- package/actions/dom/removeClass/action.json +22 -0
- package/actions/dom/removeClass/action.test.js +32 -0
- package/actions/dom/scroll/action.json +29 -0
- package/actions/dom/scroll/action.test.js +32 -0
- package/actions/dom/setAttr/action.json +29 -0
- package/actions/dom/setAttr/action.test.js +32 -0
- package/actions/dom/setHtml/action.json +22 -0
- package/actions/dom/setHtml/action.test.js +32 -0
- package/actions/dom/setStyle/action.json +29 -0
- package/actions/dom/setStyle/action.test.js +32 -0
- package/actions/dom/setText/action.json +24 -0
- package/actions/dom/setText/action.test.js +32 -0
- package/actions/dom/show/action.json +18 -0
- package/actions/dom/show/action.test.js +32 -0
- package/actions/dom/toggle/action.json +17 -0
- package/actions/dom/toggle/action.test.js +32 -0
- package/actions/dom/toggleClass/action.json +22 -0
- package/actions/dom/toggleClass/action.test.js +32 -0
- package/actions/event/emit/action.json +24 -0
- package/actions/event/emit/action.test.js +32 -0
- package/actions/event/on/action.json +23 -0
- package/actions/event/on/action.test.js +32 -0
- package/actions/event/waitFor/action.json +28 -0
- package/actions/event/waitFor/action.test.js +32 -0
- package/actions/forms/bindField/action.js +27 -0
- package/actions/forms/bindField/action.json +24 -0
- package/actions/forms/bindField/action.test.js +20 -0
- package/actions/forms/check/action.json +22 -0
- package/actions/forms/check/action.test.js +32 -0
- package/actions/forms/clearErrors/action.json +11 -0
- package/actions/forms/clearErrors/action.test.js +35 -0
- package/actions/forms/clearField/action.json +17 -0
- package/actions/forms/clearField/action.test.js +32 -0
- package/actions/forms/getField/action.json +24 -0
- package/actions/forms/getField/action.test.js +32 -0
- package/actions/forms/getRange/action.json +22 -0
- package/actions/forms/getRange/action.test.js +32 -0
- package/actions/forms/getSelect/action.json +22 -0
- package/actions/forms/getSelect/action.test.js +32 -0
- package/actions/forms/index.js +140 -0
- package/actions/forms/serialize/action.json +24 -0
- package/actions/forms/serialize/action.test.js +32 -0
- package/actions/forms/setCheck/action.json +23 -0
- package/actions/forms/setCheck/action.test.js +32 -0
- package/actions/forms/setError/action.json +22 -0
- package/actions/forms/setError/action.test.js +32 -0
- package/actions/forms/setField/action.js +14 -0
- package/actions/forms/setField/action.json +24 -0
- package/actions/forms/setField/action.test.js +32 -0
- package/actions/forms/submit/action.json +18 -0
- package/actions/forms/submit/action.test.js +32 -0
- package/actions/forms/validate/action.json +24 -0
- package/actions/forms/validate/action.test.js +32 -0
- package/actions/http/delete/action.json +22 -0
- package/actions/http/delete/action.test.js +32 -0
- package/actions/http/get/action.json +24 -0
- package/actions/http/get/action.test.js +32 -0
- package/actions/http/post/action.json +30 -0
- package/actions/http/post/action.test.js +32 -0
- package/actions/http/put/action.json +27 -0
- package/actions/http/put/action.test.js +32 -0
- package/actions/http/upload/action.json +35 -0
- package/actions/http/upload/action.test.js +32 -0
- package/actions/index.js +306 -0
- package/actions/index.json +5 -0
- package/actions/input/askChoice/action.json +29 -0
- package/actions/input/askChoice/action.test.js +32 -0
- package/actions/input/askConfirm/action.json +24 -0
- package/actions/input/askConfirm/action.test.js +32 -0
- package/actions/input/askDate/action.json +24 -0
- package/actions/input/askDate/action.test.js +32 -0
- package/actions/input/askEmail/action.json +24 -0
- package/actions/input/askEmail/action.test.js +32 -0
- package/actions/input/askNumber/action.json +35 -0
- package/actions/input/askNumber/action.test.js +32 -0
- package/actions/input/askPassword/action.json +24 -0
- package/actions/input/askPassword/action.test.js +32 -0
- package/actions/input/askText/action.json +36 -0
- package/actions/input/askText/action.test.js +32 -0
- package/actions/logic/delay/action.json +18 -0
- package/actions/logic/delay/action.test.js +32 -0
- package/actions/logic/if/action.json +28 -0
- package/actions/logic/if/action.test.js +32 -0
- package/actions/logic/log/action.json +18 -0
- package/actions/logic/log/action.test.js +32 -0
- package/actions/logic/random/action.json +36 -0
- package/actions/logic/random/action.test.js +32 -0
- package/actions/logic/transform/action.json +24 -0
- package/actions/logic/transform/action.test.js +32 -0
- package/actions/media/askAudioUpload/action.json +30 -0
- package/actions/media/askAudioUpload/action.test.js +32 -0
- package/actions/media/askFileUpload/action.json +36 -0
- package/actions/media/askFileUpload/action.test.js +32 -0
- package/actions/media/askImageUpload/action.json +37 -0
- package/actions/media/askImageUpload/action.test.js +32 -0
- package/actions/media/askVideoUpload/action.js +74 -0
- package/actions/media/askVideoUpload/action.json +44 -0
- package/actions/media/askVideoUpload/action.test.js +51 -0
- package/actions/media/captureWebcam/action.json +24 -0
- package/actions/media/captureWebcam/action.test.js +32 -0
- package/actions/nav/back/action.json +11 -0
- package/actions/nav/back/action.test.js +35 -0
- package/actions/nav/goto/action.json +18 -0
- package/actions/nav/goto/action.test.js +32 -0
- package/actions/nav/redirect/action.json +28 -0
- package/actions/nav/redirect/action.test.js +32 -0
- package/actions/nav/reload/action.json +11 -0
- package/actions/nav/reload/action.test.js +35 -0
- package/actions/nav/reset/action.json +11 -0
- package/actions/nav/reset/action.test.js +35 -0
- package/actions/render/alert/action.js +12 -0
- package/actions/render/alert/action.json +36 -0
- package/actions/render/alert/action.test.js +32 -0
- package/actions/render/button/action.js +15 -0
- package/actions/render/button/action.json +44 -0
- package/actions/render/button/action.test.js +37 -0
- package/actions/render/clear/action.js +7 -0
- package/actions/render/clear/action.json +11 -0
- package/actions/render/clear/action.test.js +35 -0
- package/actions/render/divider/action.js +8 -0
- package/actions/render/divider/action.json +11 -0
- package/actions/render/divider/action.test.js +35 -0
- package/actions/render/field/action.js +17 -0
- package/actions/render/field/action.json +59 -0
- package/actions/render/field/action.test.js +57 -0
- package/actions/render/link/action.js +20 -0
- package/actions/render/link/action.json +30 -0
- package/actions/render/link/action.test.js +32 -0
- package/actions/render/markdown/action.json +18 -0
- package/actions/render/markdown/action.test.js +32 -0
- package/actions/render/paragraph/action.js +9 -0
- package/actions/render/paragraph/action.json +32 -0
- package/actions/render/paragraph/action.test.js +32 -0
- package/actions/render/section/action.js +10 -0
- package/actions/render/section/action.json +18 -0
- package/actions/render/section/action.test.js +32 -0
- package/actions/render/subtitle/action.js +9 -0
- package/actions/render/subtitle/action.json +18 -0
- package/actions/render/subtitle/action.test.js +32 -0
- package/actions/render/title/action.js +9 -0
- package/actions/render/title/action.json +30 -0
- package/actions/render/title/action.test.js +44 -0
- package/actions/session/clear/action.json +11 -0
- package/actions/session/clear/action.test.js +35 -0
- package/actions/session/load/action.json +22 -0
- package/actions/session/load/action.test.js +32 -0
- package/actions/session/local/action.json +22 -0
- package/actions/session/local/action.test.js +32 -0
- package/actions/session/save/action.json +22 -0
- package/actions/session/save/action.test.js +32 -0
- package/actions/ui/accordion/action.json +23 -0
- package/actions/ui/accordion/action.test.js +32 -0
- package/actions/ui/badge/action.json +22 -0
- package/actions/ui/badge/action.test.js +32 -0
- package/actions/ui/collapse/action.json +23 -0
- package/actions/ui/collapse/action.test.js +32 -0
- package/actions/ui/hideModal/action.json +17 -0
- package/actions/ui/hideModal/action.test.js +32 -0
- package/actions/ui/loading/action.json +18 -0
- package/actions/ui/loading/action.test.js +32 -0
- package/actions/ui/modal/action.json +18 -0
- package/actions/ui/modal/action.test.js +32 -0
- package/actions/ui/progress/action.json +24 -0
- package/actions/ui/progress/action.test.js +32 -0
- package/actions/ui/toast/action.json +29 -0
- package/actions/ui/toast/action.test.js +32 -0
- package/actions/ui/tooltip/action.json +17 -0
- package/actions/ui/tooltip/action.test.js +32 -0
- package/actions/user/carry/action.json +25 -0
- package/actions/user/carry/action.test.js +32 -0
- package/actions/user/check/action.json +24 -0
- package/actions/user/check/action.test.js +32 -0
- package/actions/user/clear/action.json +11 -0
- package/actions/user/clear/action.test.js +35 -0
- package/actions/user/delete/action.json +17 -0
- package/actions/user/delete/action.test.js +32 -0
- package/actions/user/dump/action.json +11 -0
- package/actions/user/dump/action.test.js +35 -0
- package/actions/user/get/action.json +24 -0
- package/actions/user/get/action.test.js +32 -0
- package/actions/user/merge/action.json +18 -0
- package/actions/user/merge/action.test.js +32 -0
- package/actions/user/set/action.json +24 -0
- package/actions/user/set/action.test.js +32 -0
- package/generator/base/css/bootstrap.min.css +6 -0
- package/generator/base/icons/app-indicator.svg +4 -0
- package/generator/base/icons/backpack.svg +4 -0
- package/generator/base/icons/broadcast.svg +3 -0
- package/generator/base/icons/bullseye.svg +6 -0
- package/generator/base/icons/chat-dots.svg +4 -0
- package/generator/base/icons/check-circle.svg +4 -0
- package/generator/base/icons/clipboard-check.svg +5 -0
- package/generator/base/icons/clipboard.svg +4 -0
- package/generator/base/icons/copy.svg +3 -0
- package/generator/base/icons/cursor.svg +3 -0
- package/generator/base/icons/diamond.svg +3 -0
- package/generator/base/icons/exclamation-triangle.svg +4 -0
- package/generator/base/icons/film.svg +3 -0
- package/generator/base/icons/floppy.svg +4 -0
- package/generator/base/icons/gear-wide-connected.svg +3 -0
- package/generator/base/icons/gear.svg +4 -0
- package/generator/base/icons/globe.svg +3 -0
- package/generator/base/icons/image.svg +4 -0
- package/generator/base/icons/layout-text-window.svg +4 -0
- package/generator/base/icons/lightning-charge.svg +3 -0
- package/generator/base/icons/magic.svg +3 -0
- package/generator/base/icons/pencil-square.svg +4 -0
- package/generator/base/icons/record-circle.svg +4 -0
- package/generator/base/icons/robot.svg +4 -0
- package/generator/base/icons/shield-check.svg +4 -0
- package/generator/base/icons/shield-lock.svg +4 -0
- package/generator/base/icons/signpost.svg +3 -0
- package/generator/base/icons/stars.svg +3 -0
- package/generator/base/icons/type.svg +3 -0
- package/generator/base/js/bootstrap.bundle.min.js +7 -0
- package/package.json +14 -0
- package/packages/undercity-http-server/index.js +249 -0
- package/packages/undercity-http-server/package.json +10 -0
- package/packages/undercity-parser/index.js +323 -0
- package/packages/undercity-parser/lexer.js +128 -0
- package/packages/undercity-parser/package.json +11 -0
- package/plugins/forms.js +397 -0
- package/plugins/index.js +83 -0
- package/plugins/multipage.js +165 -0
- package/plugins/wizard.js +239 -0
- package/projects/asd/project.json +1031 -0
- package/projects/test-1/project.json +335 -0
- package/projects/test-a/project.json +456 -0
- package/public/icons/arrows-angle-expand.svg +3 -0
- package/public/icons/arrows-fullscreen.svg +3 -0
- package/public/icons/bezier2.svg +3 -0
- package/public/icons/bootstrap/app-indicator.svg +4 -0
- package/public/icons/bootstrap/arrow-clockwise.svg +4 -0
- package/public/icons/bootstrap/arrow-counterclockwise.svg +4 -0
- package/public/icons/bootstrap/arrow-left.svg +3 -0
- package/public/icons/bootstrap/arrows-angle-expand.svg +3 -0
- package/public/icons/bootstrap/arrows-fullscreen.svg +3 -0
- package/public/icons/bootstrap/backpack.svg +4 -0
- package/public/icons/bootstrap/bezier2.svg +3 -0
- package/public/icons/bootstrap/bookmark-check.svg +4 -0
- package/public/icons/bootstrap/bookmark-plus.svg +4 -0
- package/public/icons/bootstrap/box-arrow-right.svg +4 -0
- package/public/icons/bootstrap/box-arrow-up.svg +4 -0
- package/public/icons/bootstrap/broadcast.svg +3 -0
- package/public/icons/bootstrap/bullseye.svg +6 -0
- package/public/icons/bootstrap/chat-dots.svg +4 -0
- package/public/icons/bootstrap/check-circle.svg +4 -0
- package/public/icons/bootstrap/check2.svg +3 -0
- package/public/icons/bootstrap/clipboard-check.svg +5 -0
- package/public/icons/bootstrap/clipboard.svg +4 -0
- package/public/icons/bootstrap/clock-history.svg +5 -0
- package/public/icons/bootstrap/command.svg +3 -0
- package/public/icons/bootstrap/copy.svg +3 -0
- package/public/icons/bootstrap/cursor.svg +3 -0
- package/public/icons/bootstrap/diagram-3.svg +3 -0
- package/public/icons/bootstrap/diamond.svg +3 -0
- package/public/icons/bootstrap/exclamation-triangle.svg +4 -0
- package/public/icons/bootstrap/eye.svg +4 -0
- package/public/icons/bootstrap/file-earmark-plus.svg +4 -0
- package/public/icons/bootstrap/film.svg +3 -0
- package/public/icons/bootstrap/floppy.svg +4 -0
- package/public/icons/bootstrap/folder2-open.svg +3 -0
- package/public/icons/bootstrap/gear-wide-connected.svg +3 -0
- package/public/icons/bootstrap/gear.svg +4 -0
- package/public/icons/bootstrap/globe.svg +3 -0
- package/public/icons/bootstrap/grid-3x3-gap.svg +3 -0
- package/public/icons/bootstrap/house-door.svg +3 -0
- package/public/icons/bootstrap/image.svg +4 -0
- package/public/icons/bootstrap/layout-text-window.svg +4 -0
- package/public/icons/bootstrap/lightning-charge.svg +3 -0
- package/public/icons/bootstrap/magic.svg +3 -0
- package/public/icons/bootstrap/pencil-square.svg +4 -0
- package/public/icons/bootstrap/pencil.svg +3 -0
- package/public/icons/bootstrap/play.svg +3 -0
- package/public/icons/bootstrap/plus-circle.svg +4 -0
- package/public/icons/bootstrap/plus-lg.svg +3 -0
- package/public/icons/bootstrap/record-circle.svg +4 -0
- package/public/icons/bootstrap/robot.svg +4 -0
- package/public/icons/bootstrap/save.svg +3 -0
- package/public/icons/bootstrap/scissors.svg +3 -0
- package/public/icons/bootstrap/shield-check.svg +4 -0
- package/public/icons/bootstrap/shield-lock.svg +4 -0
- package/public/icons/bootstrap/signpost.svg +3 -0
- package/public/icons/bootstrap/stars.svg +3 -0
- package/public/icons/bootstrap/stop-circle.svg +4 -0
- package/public/icons/bootstrap/terminal.svg +4 -0
- package/public/icons/bootstrap/trash3.svg +3 -0
- package/public/icons/bootstrap/type.svg +3 -0
- package/public/icons/bootstrap/x-circle.svg +4 -0
- package/public/icons/bootstrap/x-lg.svg +3 -0
- package/public/icons/bootstrap/zoom-in.svg +5 -0
- package/public/icons/bootstrap/zoom-out.svg +5 -0
- package/public/icons/bullseye.svg +6 -0
- package/public/icons/check2.svg +3 -0
- package/public/icons/cursor.svg +3 -0
- package/public/icons/diamond.svg +3 -0
- package/public/icons/eye.svg +4 -0
- package/public/icons/file-earmark-plus.svg +4 -0
- package/public/icons/floppy.svg +4 -0
- package/public/icons/gear.svg +4 -0
- package/public/icons/lightning-charge.svg +3 -0
- package/public/icons/pencil.svg +3 -0
- package/public/icons/play.svg +3 -0
- package/public/icons/plus-circle.svg +4 -0
- package/public/icons/record-circle.svg +4 -0
- package/public/icons/robot.svg +4 -0
- package/public/icons/save.svg +3 -0
- package/public/icons/stop-circle.svg +4 -0
- package/public/icons/terminal.svg +4 -0
- package/public/icons/trash3.svg +3 -0
- package/public/icons/x-circle.svg +4 -0
- package/public/icons/zoom-in.svg +5 -0
- package/public/icons/zoom-out.svg +5 -0
- package/public/index.html +424 -0
- package/public/testbench.html +899 -0
- package/scripts/extract-actions.js +128 -0
- package/server.js +11 -0
- package/src/emitter.js +48 -0
- package/src/generator/css.js +135 -0
- package/src/generator/index.js +122 -0
- package/src/generator/md-renderer-src.js +77 -0
- package/src/generator/page.js +300 -0
- package/src/generator/runtime.js +1632 -0
- package/src/generator/templates.js +508 -0
- package/src/ide/action-library.js +856 -0
- package/src/ide/af-icons.js +127 -0
- package/src/ide/app.js +1375 -0
- package/src/ide/command-line/commands.js +242 -0
- package/src/ide/command-line/index.js +329 -0
- package/src/ide/command-line/parser.js +21 -0
- package/src/ide/css/ide.css +1501 -0
- package/src/ide/graph.js +282 -0
- package/src/ide/history.js +46 -0
- package/src/ide/map-builder.js +583 -0
- package/src/ide/project-api.js +39 -0
- package/src/ide/savant-chat.js +513 -0
- package/src/ide/savant.js +1287 -0
- package/src/ide/thing-library.js +89 -0
- package/src/ide/undercity-map.js +978 -0
- package/src/lib/icons.js +72 -0
- package/src/lib/scope.js +88 -0
- package/src/lib/signal.js +155 -0
- package/src/lib/state-machine.js +113 -0
- package/src/server/index.js +96 -0
- package/src/server/routes/actions.js +144 -0
- package/src/server/routes/ai.js +176 -0
- package/src/server/routes/generate.js +54 -0
- package/src/server/routes/projects.js +106 -0
- package/src/server/routes/reset.js +30 -0
- package/src/server/routes/submit.js +30 -0
- package/src/server/routes/templates.js +139 -0
- package/src/server/routes/things.js +33 -0
- package/templates/auth-flow.json +335 -0
- package/templates/blank.json +39 -0
- package/things/auth-server/thing.json +17 -0
- package/things/persona-live/thing.json +20 -0
- package/things/workflow/thing.json +15 -0
package/src/ide/graph.js
ADDED
|
@@ -0,0 +1,282 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* graph.js — Flow graph data model.
|
|
3
|
+
*
|
|
4
|
+
* Built on Signal + Emitter from the framework.
|
|
5
|
+
* Three node types mirror the MUD metaphor:
|
|
6
|
+
*
|
|
7
|
+
* room ● — A location the user inhabits. Has enter/exit payload.
|
|
8
|
+
* diamond ◆ — A logic joint. Routes the user based on inventory.
|
|
9
|
+
* terminal ◎ — End state (success / failure / info). No outgoing edges.
|
|
10
|
+
*
|
|
11
|
+
* Connections are directed edges. On a diamond, each edge carries a condition.
|
|
12
|
+
* When a room has multiple outgoing edges the user chooses the next path.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import { Signal, Emitter } from '/src/lib/signal.js';
|
|
16
|
+
|
|
17
|
+
function _uuid() {
|
|
18
|
+
if (typeof crypto !== 'undefined' && typeof crypto.randomUUID === 'function') {
|
|
19
|
+
return crypto.randomUUID();
|
|
20
|
+
}
|
|
21
|
+
// Fallback for non-secure contexts (LAN HTTP)
|
|
22
|
+
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, c => {
|
|
23
|
+
const r = (typeof crypto !== 'undefined' && crypto.getRandomValues)
|
|
24
|
+
? (crypto.getRandomValues(new Uint8Array(1))[0] & 15)
|
|
25
|
+
: (Math.random() * 16 | 0);
|
|
26
|
+
return (c === 'x' ? r : (r & 0x3 | 0x8)).toString(16);
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export const NodeType = Object.freeze({
|
|
31
|
+
ROOM: 'room',
|
|
32
|
+
DIAMOND: 'diamond',
|
|
33
|
+
TERMINAL: 'terminal',
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
// ── GraphNode ─────────────────────────────────────────────────────────────────
|
|
37
|
+
export class GraphNode {
|
|
38
|
+
/** @type {string} */ id;
|
|
39
|
+
/** @type {'room'|'diamond'|'terminal'} */ type;
|
|
40
|
+
/** @type {Signal<string>} */ label;
|
|
41
|
+
/** @type {Signal<number>} */ x;
|
|
42
|
+
/** @type {Signal<number>} */ y;
|
|
43
|
+
/** @type {Signal<object>} */ payload; // { onEnter, onExit, onBack, onReset, onUnload }
|
|
44
|
+
/** @type {Signal<object[]>} */ routes; // diamond: [{ condition, target, label }]
|
|
45
|
+
/** @type {string|null} */ template;
|
|
46
|
+
/** @type {object} */ meta;
|
|
47
|
+
/** @type {Signal<object[]>} */ things; // [{id, type, config, events}]
|
|
48
|
+
|
|
49
|
+
constructor(data = {}) {
|
|
50
|
+
this.id = data.id ?? _uuid().split('-')[0];
|
|
51
|
+
// Normalize legacy type name: 'room' was renamed to 'room'
|
|
52
|
+
const rawType = data.type ?? NodeType.ROOM;
|
|
53
|
+
this.type = rawType === 'room' ? NodeType.ROOM : rawType;
|
|
54
|
+
this.label = new Signal(data.label ?? (this.type === NodeType.DIAMOND ? 'Logic Check' : 'Room'));
|
|
55
|
+
this.x = new Signal(data.x ?? 200);
|
|
56
|
+
this.y = new Signal(data.y ?? 200);
|
|
57
|
+
this.payload = new Signal(data.payload ?? {
|
|
58
|
+
onEnter: [], onExit: [], onBack: [], onReset: [], onUnload: [],
|
|
59
|
+
});
|
|
60
|
+
this.routes = new Signal(data.routes ?? []);
|
|
61
|
+
this.template = data.template ?? null;
|
|
62
|
+
this.meta = { ...(data.meta ?? {}) };
|
|
63
|
+
this.things = new Signal(data.things ?? []);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** Ensure all five lifecycle buckets exist; preserve any custom event keys. */
|
|
67
|
+
ensurePayload() {
|
|
68
|
+
const p = this.payload.peek();
|
|
69
|
+
const out = {
|
|
70
|
+
...p,
|
|
71
|
+
onEnter: p.onEnter ?? [],
|
|
72
|
+
onExit: p.onExit ?? [],
|
|
73
|
+
onBack: p.onBack ?? [],
|
|
74
|
+
onReset: p.onReset ?? [],
|
|
75
|
+
onUnload: p.onUnload ?? [],
|
|
76
|
+
};
|
|
77
|
+
this.payload.value = out;
|
|
78
|
+
return out;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
addStep(event, step) {
|
|
82
|
+
const p = this.ensurePayload();
|
|
83
|
+
p[event] = [...(p[event] ?? []), step];
|
|
84
|
+
this.payload.value = { ...p };
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
insertStep(event, index, step) {
|
|
88
|
+
const p = this.ensurePayload();
|
|
89
|
+
const steps = [...(p[event] ?? [])];
|
|
90
|
+
steps.splice(index, 0, step);
|
|
91
|
+
p[event] = steps;
|
|
92
|
+
this.payload.value = { ...p };
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
removeStep(event, index) {
|
|
96
|
+
const p = this.ensurePayload();
|
|
97
|
+
p[event] = p[event].filter((_, i) => i !== index);
|
|
98
|
+
this.payload.value = { ...p };
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
updateStep(event, index, step) {
|
|
102
|
+
const p = this.ensurePayload();
|
|
103
|
+
p[event] = p[event].map((s, i) => i === index ? { ...s, ...step } : s);
|
|
104
|
+
this.payload.value = { ...p };
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
moveStep(event, from, to) {
|
|
108
|
+
const p = this.ensurePayload();
|
|
109
|
+
const steps = [...p[event]];
|
|
110
|
+
const [item] = steps.splice(from, 1);
|
|
111
|
+
steps.splice(to, 0, item);
|
|
112
|
+
p[event] = steps;
|
|
113
|
+
this.payload.value = { ...p };
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// ── Things ───────────────────────────────────────────────────────────────────
|
|
117
|
+
|
|
118
|
+
addThing(thingDef) {
|
|
119
|
+
// thingDef: { id?, type, config?, events? }
|
|
120
|
+
const id = thingDef.id ?? _uuid().split('-')[0];
|
|
121
|
+
const t = { id, type: thingDef.type, config: thingDef.config ?? {}, events: thingDef.events ?? {} };
|
|
122
|
+
this.things.value = [...this.things.peek(), t];
|
|
123
|
+
return t;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
removeThing(id) {
|
|
127
|
+
this.things.value = this.things.peek().filter(t => t.id !== id);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
updateThing(id, patch) {
|
|
131
|
+
this.things.value = this.things.peek().map(t => t.id === id ? { ...t, ...patch } : t);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
toJSON() {
|
|
135
|
+
return {
|
|
136
|
+
id: this.id,
|
|
137
|
+
type: this.type,
|
|
138
|
+
label: this.label.peek(),
|
|
139
|
+
x: this.x.peek(),
|
|
140
|
+
y: this.y.peek(),
|
|
141
|
+
payload: this.payload.peek(),
|
|
142
|
+
routes: this.routes.peek(),
|
|
143
|
+
template: this.template,
|
|
144
|
+
meta: this.meta,
|
|
145
|
+
things: this.things.peek(),
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
// ── GraphEdge ─────────────────────────────────────────────────────────────────
|
|
151
|
+
export class GraphEdge {
|
|
152
|
+
/** @type {string} */ id;
|
|
153
|
+
/** @type {string} */ fromId;
|
|
154
|
+
/** @type {string} */ toId;
|
|
155
|
+
/** @type {Signal<string>} */ label;
|
|
156
|
+
/** @type {Signal<string>} */ condition; // optional JS expression
|
|
157
|
+
|
|
158
|
+
constructor(data = {}) {
|
|
159
|
+
this.id = data.id ?? _uuid().split('-')[0];
|
|
160
|
+
this.fromId = data.fromId;
|
|
161
|
+
this.toId = data.toId;
|
|
162
|
+
this.label = new Signal(data.label ?? '');
|
|
163
|
+
this.condition = new Signal(data.condition ?? '');
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
toJSON() {
|
|
167
|
+
return {
|
|
168
|
+
id: this.id,
|
|
169
|
+
fromId: this.fromId,
|
|
170
|
+
toId: this.toId,
|
|
171
|
+
label: this.label.peek(),
|
|
172
|
+
condition: this.condition.peek(),
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
// ── Graph ─────────────────────────────────────────────────────────────────────
|
|
178
|
+
export class Graph extends Emitter {
|
|
179
|
+
/** @type {Map<string, GraphNode>} */ nodes = new Map();
|
|
180
|
+
/** @type {Map<string, GraphEdge>} */ edges = new Map();
|
|
181
|
+
|
|
182
|
+
// ── Nodes ──────────────────────────────────────────────────────────────────
|
|
183
|
+
addNode(data = {}) {
|
|
184
|
+
const node = new GraphNode(data);
|
|
185
|
+
this.nodes.set(node.id, node);
|
|
186
|
+
this.emit('nodeAdded', node);
|
|
187
|
+
return node;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
updateNode(id, changes) {
|
|
191
|
+
const node = this.nodes.get(id);
|
|
192
|
+
if (!node) return;
|
|
193
|
+
if (changes.label !== undefined) node.label.value = changes.label;
|
|
194
|
+
if (changes.x !== undefined) node.x.value = changes.x;
|
|
195
|
+
if (changes.y !== undefined) node.y.value = changes.y;
|
|
196
|
+
if (changes.payload !== undefined) node.payload.value = changes.payload;
|
|
197
|
+
if (changes.routes !== undefined) node.routes.value = changes.routes;
|
|
198
|
+
if (changes.template !== undefined) node.template = changes.template;
|
|
199
|
+
if (changes.meta !== undefined) Object.assign(node.meta, changes.meta);
|
|
200
|
+
this.emit('nodeUpdated', node);
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
removeNode(id) {
|
|
204
|
+
const node = this.nodes.get(id);
|
|
205
|
+
if (!node) return;
|
|
206
|
+
// Remove all incident edges and emit edgeRemoved so listeners can clean up visuals
|
|
207
|
+
for (const [eid, edge] of [...this.edges]) {
|
|
208
|
+
if (edge.fromId === id || edge.toId === id) {
|
|
209
|
+
this.edges.delete(eid);
|
|
210
|
+
this.emit('edgeRemoved', { id: eid });
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
this.nodes.delete(id);
|
|
214
|
+
this.emit('nodeRemoved', { id });
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
setEntry(id) {
|
|
218
|
+
for (const node of this.nodes.values()) {
|
|
219
|
+
node.meta.isEntry = (node.id === id);
|
|
220
|
+
}
|
|
221
|
+
this.emit('entryChanged', { id });
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
// ── Edges ──────────────────────────────────────────────────────────────────
|
|
225
|
+
addEdge(fromId, toId, data = {}) {
|
|
226
|
+
// Prevent duplicate
|
|
227
|
+
for (const e of this.edges.values()) {
|
|
228
|
+
if (e.fromId === fromId && e.toId === toId) return e;
|
|
229
|
+
}
|
|
230
|
+
const edge = new GraphEdge({ fromId, toId, ...data });
|
|
231
|
+
this.edges.set(edge.id, edge);
|
|
232
|
+
this.emit('edgeAdded', edge);
|
|
233
|
+
return edge;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
removeEdge(id) {
|
|
237
|
+
const edge = this.edges.get(id);
|
|
238
|
+
if (!edge) return;
|
|
239
|
+
this.edges.delete(id);
|
|
240
|
+
this.emit('edgeRemoved', { id });
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
// ── Queries ────────────────────────────────────────────────────────────────
|
|
244
|
+
outEdges(nodeId) {
|
|
245
|
+
return [...this.edges.values()].filter(e => e.fromId === nodeId);
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
inEdges(nodeId) {
|
|
249
|
+
return [...this.edges.values()].filter(e => e.toId === nodeId);
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
getEntry() {
|
|
253
|
+
for (const node of this.nodes.values()) {
|
|
254
|
+
if (node.meta.isEntry) return node;
|
|
255
|
+
}
|
|
256
|
+
return this.nodes.values().next().value ?? null;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
// ── Serialisation ──────────────────────────────────────────────────────────
|
|
260
|
+
toJSON() {
|
|
261
|
+
return {
|
|
262
|
+
nodes: [...this.nodes.values()].map(n => n.toJSON()),
|
|
263
|
+
edges: [...this.edges.values()].map(e => e.toJSON()),
|
|
264
|
+
};
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
/** Load from plain JSON (project.graph). Replaces existing data. */
|
|
268
|
+
fromJSON(data) {
|
|
269
|
+
this.nodes.clear();
|
|
270
|
+
this.edges.clear();
|
|
271
|
+
|
|
272
|
+
for (const n of (data.nodes ?? [])) {
|
|
273
|
+
this.nodes.set(n.id, new GraphNode(n));
|
|
274
|
+
}
|
|
275
|
+
for (const e of (data.edges ?? [])) {
|
|
276
|
+
this.edges.set(e.id, new GraphEdge(e));
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
this.emit('graphLoaded', this);
|
|
280
|
+
return this;
|
|
281
|
+
}
|
|
282
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* history.js — Snapshot-based undo/redo for the Undercity IDE.
|
|
3
|
+
*
|
|
4
|
+
* Usage:
|
|
5
|
+
* history.record(graph.toJSON()) // call BEFORE mutating
|
|
6
|
+
* const prev = history.undo(graph.toJSON()) // returns state to restore
|
|
7
|
+
* const next = history.redo(graph.toJSON()) // returns state to restore
|
|
8
|
+
*/
|
|
9
|
+
export class CommandHistory {
|
|
10
|
+
#undo = [];
|
|
11
|
+
#redo = [];
|
|
12
|
+
static MAX = 80;
|
|
13
|
+
|
|
14
|
+
/** Save the current state before a mutation so it can be undone. */
|
|
15
|
+
record(snapshot) {
|
|
16
|
+
this.#undo.push(snapshot);
|
|
17
|
+
if (this.#undo.length > CommandHistory.MAX) this.#undo.shift();
|
|
18
|
+
this.#redo.length = 0; // new action clears redo stack
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Undo: returns snapshot to restore, or null if nothing to undo.
|
|
23
|
+
* @param {object} current - current graph JSON (pushed to redo stack)
|
|
24
|
+
*/
|
|
25
|
+
undo(current) {
|
|
26
|
+
if (!this.#undo.length) return null;
|
|
27
|
+
this.#redo.push(current);
|
|
28
|
+
return this.#undo.pop();
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Redo: returns snapshot to restore, or null if nothing to redo.
|
|
33
|
+
* @param {object} current - current graph JSON (pushed to undo stack)
|
|
34
|
+
*/
|
|
35
|
+
redo(current) {
|
|
36
|
+
if (!this.#redo.length) return null;
|
|
37
|
+
this.#undo.push(current);
|
|
38
|
+
return this.#redo.pop();
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
get canUndo() { return this.#undo.length > 0; }
|
|
42
|
+
get canRedo() { return this.#redo.length > 0; }
|
|
43
|
+
|
|
44
|
+
/** Reset history (e.g. when a new project is opened). */
|
|
45
|
+
clear() { this.#undo.length = 0; this.#redo.length = 0; }
|
|
46
|
+
}
|