@johnoscott/isometric-engine 0.0.0-stage → 0.9.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/EDITOR-GUIDE.md +317 -0
- package/LICENSE +21 -0
- package/LICENSES/ajv.txt +22 -0
- package/LICENSES/lucide-static.txt +43 -0
- package/LICENSES/yaml.txt +13 -0
- package/README.md +62 -2
- package/assets/app.css +265 -0
- package/assets/default-theme.yaml +99 -0
- package/assets/icons.svg +1 -0
- package/assets/marks.css +35 -0
- package/assets/skin.css +181 -0
- package/iso-editor.d.ts +4 -0
- package/iso-editor.js +550 -0
- package/iso-render.d.ts +3 -0
- package/iso-render.js +388 -0
- package/iso-systems.d.ts +4 -0
- package/iso-systems.js +389 -0
- package/js/iso-core/compile-schema.js +22 -0
- package/js/iso-core/depth.js +55 -0
- package/js/iso-core/errors.js +65 -0
- package/js/iso-core/glyphs.js +100 -0
- package/js/iso-core/index.js +55 -0
- package/js/iso-core/kernel.js +158 -0
- package/js/iso-core/metrics.json +1 -0
- package/js/iso-core/package.json +6 -0
- package/js/iso-core/parse.js +79 -0
- package/js/iso-core/schema-errors.js +170 -0
- package/js/iso-core/shims/crypto.js +125 -0
- package/js/iso-core/shims/path.js +49 -0
- package/js/iso-core/svg-dom.js +144 -0
- package/js/iso-core/theme.js +48 -0
- package/js/iso-core/vdom.js +48 -0
- package/js/iso-editor/adapter.js +0 -0
- package/js/iso-editor/clipboard.js +152 -0
- package/js/iso-editor/commands.js +173 -0
- package/js/iso-editor/complete.js +55 -0
- package/js/iso-editor/doc.js +182 -0
- package/js/iso-editor/index.js +105 -0
- package/js/iso-editor/model.js +210 -0
- package/js/iso-editor/outline.js +111 -0
- package/js/iso-editor/package.json +6 -0
- package/js/iso-editor/path.js +19 -0
- package/js/iso-editor/providers.js +96 -0
- package/js/iso-editor/schema-nav.js +117 -0
- package/js/iso-editor/splice.js +441 -0
- package/js/iso-editor/storage-keys.js +6 -0
- package/js/iso-editor/ui/actions.js +116 -0
- package/js/iso-editor/ui/canvas-layer.js +0 -0
- package/js/iso-editor/ui/canvas.js +1205 -0
- package/js/iso-editor/ui/clip.js +133 -0
- package/js/iso-editor/ui/dialog.js +26 -0
- package/js/iso-editor/ui/flat-canvas.js +54 -0
- package/js/iso-editor/ui/icons.js +15 -0
- package/js/iso-editor/ui/keys.js +26 -0
- package/js/iso-editor/ui/later.js +28 -0
- package/js/iso-editor/ui/layer.js +11 -0
- package/js/iso-editor/ui/menu.js +105 -0
- package/js/iso-editor/ui/monaco-entry.js +2 -0
- package/js/iso-editor/ui/mount-kit.js +0 -0
- package/js/iso-editor/ui/mount.js +767 -0
- package/js/iso-editor/ui/pane.js +0 -0
- package/js/iso-editor/ui/part.js +0 -0
- package/js/iso-editor/ui/plain-icons.js +34 -0
- package/js/iso-editor/ui/props.js +314 -0
- package/js/iso-editor/ui/scope-css.js +81 -0
- package/js/iso-editor/ui/setup.js +145 -0
- package/js/iso-editor/ui/text.js +323 -0
- package/js/iso-editor/ui/tree.js +728 -0
- package/js/iso-editor/validate.js +33 -0
- package/js/iso-editor/window/protocol.js +8 -0
- package/js/iso-objects/assemble.js +43 -0
- package/js/iso-objects/assets.js +88 -0
- package/js/iso-objects/board.js +162 -0
- package/js/iso-objects/compile.js +60 -0
- package/js/iso-objects/css.js +154 -0
- package/js/iso-objects/flat.js +696 -0
- package/js/iso-objects/focus.js +16 -0
- package/js/iso-objects/geometry.js +76 -0
- package/js/iso-objects/index.js +93 -0
- package/js/iso-objects/kinds.js +12 -0
- package/js/iso-objects/labels.js +325 -0
- package/js/iso-objects/package.json +6 -0
- package/js/iso-objects/place.js +347 -0
- package/js/iso-objects/route.js +461 -0
- package/js/iso-objects/scene.js +350 -0
- package/js/iso-objects/steps.js +246 -0
- package/js/iso-objects/types.js +0 -0
- package/js/iso-systems/catalog.js +42 -0
- package/js/iso-systems/compile.js +34 -0
- package/js/iso-systems/css.js +8 -0
- package/js/iso-systems/index.js +16 -0
- package/js/iso-systems/nodes.js +124 -0
- package/js/iso-systems/package.json +6 -0
- package/js/iso-systems/schema.json +311 -0
- package/js/iso-systems/validate.js +35 -0
- package/js/iso-viewer/bundle.js +20 -0
- package/js/iso-viewer/card-motion.js +228 -0
- package/js/iso-viewer/dom.js +14 -0
- package/js/iso-viewer/dual-view.js +212 -0
- package/js/iso-viewer/flat-view.js +124 -0
- package/js/iso-viewer/history.js +990 -0
- package/js/iso-viewer/index.js +98 -0
- package/js/iso-viewer/morph.js +192 -0
- package/js/iso-viewer/overlay.js +199 -0
- package/js/iso-viewer/package.json +6 -0
- package/js/iso-viewer/page.js +138 -0
- package/js/iso-viewer/player.js +409 -0
- package/js/iso-viewer/still.js +81 -0
- package/js/iso-viewer/timeline-contract.js +14 -0
- package/js/iso-viewer/timeline-engine.js +475 -0
- package/js/iso-viewer/timeline-export.js +36 -0
- package/js/iso-viewer/timeline-scale.js +116 -0
- package/js/iso-viewer/timeline-scope.js +58 -0
- package/js/iso-viewer/timeline-search.js +216 -0
- package/js/iso-viewer/timeline-time.js +55 -0
- package/js/iso-viewer/view-css.js +68 -0
- package/js/iso-viewer/viewport.js +177 -0
- package/package.json +502 -4
- package/src/iso-core/compile-schema.ts +23 -0
- package/src/iso-core/depth.ts +56 -0
- package/src/iso-core/errors.ts +73 -0
- package/src/iso-core/glyphs.ts +108 -0
- package/src/iso-core/index.ts +27 -0
- package/src/iso-core/kernel.ts +163 -0
- package/src/iso-core/metrics.json +1 -0
- package/src/iso-core/package.json +6 -0
- package/src/iso-core/parse.ts +91 -0
- package/src/iso-core/schema-errors.ts +163 -0
- package/src/iso-core/shims/crypto.ts +37 -0
- package/src/iso-core/shims/path.ts +24 -0
- package/src/iso-core/svg-dom.ts +103 -0
- package/src/iso-core/theme.ts +64 -0
- package/src/iso-core/vdom.ts +41 -0
- package/src/iso-editor/adapter.ts +128 -0
- package/src/iso-editor/clipboard.ts +163 -0
- package/src/iso-editor/commands.ts +191 -0
- package/src/iso-editor/complete.ts +73 -0
- package/src/iso-editor/doc.ts +207 -0
- package/src/iso-editor/index.ts +67 -0
- package/src/iso-editor/model.ts +234 -0
- package/src/iso-editor/outline.ts +126 -0
- package/src/iso-editor/package.json +6 -0
- package/src/iso-editor/path.ts +18 -0
- package/src/iso-editor/providers.ts +113 -0
- package/src/iso-editor/schema-nav.ts +129 -0
- package/src/iso-editor/splice.ts +472 -0
- package/src/iso-editor/storage-keys.ts +6 -0
- package/src/iso-editor/ui/actions.ts +119 -0
- package/src/iso-editor/ui/app.css +230 -0
- package/src/iso-editor/ui/canvas-layer.ts +56 -0
- package/src/iso-editor/ui/canvas.ts +1071 -0
- package/src/iso-editor/ui/clip.ts +97 -0
- package/src/iso-editor/ui/dialog.ts +25 -0
- package/src/iso-editor/ui/flat-canvas.ts +51 -0
- package/src/iso-editor/ui/icons.ts +21 -0
- package/src/iso-editor/ui/keys.ts +30 -0
- package/src/iso-editor/ui/later.ts +22 -0
- package/src/iso-editor/ui/layer.ts +7 -0
- package/src/iso-editor/ui/marks.css +35 -0
- package/src/iso-editor/ui/menu.ts +67 -0
- package/src/iso-editor/ui/monaco-entry.ts +6 -0
- package/src/iso-editor/ui/mount-kit.ts +35 -0
- package/src/iso-editor/ui/mount.ts +684 -0
- package/src/iso-editor/ui/pane.ts +45 -0
- package/src/iso-editor/ui/part.ts +45 -0
- package/src/iso-editor/ui/plain-icons.ts +83 -0
- package/src/iso-editor/ui/props.ts +347 -0
- package/src/iso-editor/ui/scope-css.ts +65 -0
- package/src/iso-editor/ui/setup.ts +144 -0
- package/src/iso-editor/ui/skin.css +181 -0
- package/src/iso-editor/ui/text.ts +262 -0
- package/src/iso-editor/ui/tree.ts +657 -0
- package/src/iso-editor/validate.ts +62 -0
- package/src/iso-editor/window/protocol.ts +33 -0
- package/src/iso-objects/assemble.ts +57 -0
- package/src/iso-objects/assets.ts +122 -0
- package/src/iso-objects/board.ts +169 -0
- package/src/iso-objects/compile.ts +76 -0
- package/src/iso-objects/css.ts +163 -0
- package/src/iso-objects/flat.ts +653 -0
- package/src/iso-objects/focus.ts +14 -0
- package/src/iso-objects/geometry.ts +86 -0
- package/src/iso-objects/index.ts +33 -0
- package/src/iso-objects/kinds.ts +56 -0
- package/src/iso-objects/labels.ts +310 -0
- package/src/iso-objects/package.json +6 -0
- package/src/iso-objects/place.ts +318 -0
- package/src/iso-objects/route.ts +444 -0
- package/src/iso-objects/scene.ts +376 -0
- package/src/iso-objects/steps.ts +234 -0
- package/src/iso-objects/types.ts +68 -0
- package/src/iso-systems/catalog.ts +31 -0
- package/src/iso-systems/compile.ts +39 -0
- package/src/iso-systems/css.ts +9 -0
- package/src/iso-systems/index.ts +12 -0
- package/src/iso-systems/nodes.ts +126 -0
- package/src/iso-systems/package.json +6 -0
- package/src/iso-systems/schema.json +311 -0
- package/src/iso-systems/validate.ts +34 -0
- package/src/iso-viewer/bundle.ts +15 -0
- package/src/iso-viewer/card-motion.ts +237 -0
- package/src/iso-viewer/dom.ts +13 -0
- package/src/iso-viewer/dual-view.ts +212 -0
- package/src/iso-viewer/flat-view.ts +127 -0
- package/src/iso-viewer/history.ts +781 -0
- package/src/iso-viewer/index.ts +40 -0
- package/src/iso-viewer/morph.ts +191 -0
- package/src/iso-viewer/overlay.ts +212 -0
- package/src/iso-viewer/package.json +6 -0
- package/src/iso-viewer/page.ts +150 -0
- package/src/iso-viewer/player.ts +321 -0
- package/src/iso-viewer/still.ts +74 -0
- package/src/iso-viewer/timeline-contract.ts +342 -0
- package/src/iso-viewer/timeline-engine.ts +420 -0
- package/src/iso-viewer/timeline-export.ts +31 -0
- package/src/iso-viewer/timeline-scale.ts +129 -0
- package/src/iso-viewer/timeline-scope.ts +56 -0
- package/src/iso-viewer/timeline-search.ts +198 -0
- package/src/iso-viewer/timeline-time.ts +54 -0
- package/src/iso-viewer/view-css.ts +59 -0
- package/src/iso-viewer/viewport.ts +142 -0
- package/types/iso-core/compile-schema.d.ts +11 -0
- package/types/iso-core/depth.d.ts +6 -0
- package/types/iso-core/errors.d.ts +24 -0
- package/types/iso-core/glyphs.d.ts +19 -0
- package/types/iso-core/index.d.ts +17 -0
- package/types/iso-core/kernel.d.ts +130 -0
- package/types/iso-core/metrics.json +1 -0
- package/types/iso-core/parse.d.ts +18 -0
- package/types/iso-core/schema-errors.d.ts +14 -0
- package/types/iso-core/shims/crypto.d.ts +9 -0
- package/types/iso-core/svg-dom.d.ts +15 -0
- package/types/iso-core/theme.d.ts +38 -0
- package/types/iso-core/vdom.d.ts +26 -0
- package/types/iso-editor/adapter.d.ts +145 -0
- package/types/iso-editor/clipboard.d.ts +23 -0
- package/types/iso-editor/commands.d.ts +32 -0
- package/types/iso-editor/complete.d.ts +25 -0
- package/types/iso-editor/doc.d.ts +50 -0
- package/types/iso-editor/index.d.ts +57 -0
- package/types/iso-editor/model.d.ts +96 -0
- package/types/iso-editor/outline.d.ts +28 -0
- package/types/iso-editor/path.d.ts +7 -0
- package/types/iso-editor/providers.d.ts +43 -0
- package/types/iso-editor/schema-nav.d.ts +34 -0
- package/types/iso-editor/splice.d.ts +53 -0
- package/types/iso-editor/storage-keys.d.ts +5 -0
- package/types/iso-editor/ui/actions.d.ts +23 -0
- package/types/iso-editor/ui/canvas-layer.d.ts +65 -0
- package/types/iso-editor/ui/canvas.d.ts +279 -0
- package/types/iso-editor/ui/clip.d.ts +14 -0
- package/types/iso-editor/ui/dialog.d.ts +7 -0
- package/types/iso-editor/ui/flat-canvas.d.ts +2 -0
- package/types/iso-editor/ui/icons.d.ts +5 -0
- package/types/iso-editor/ui/keys.d.ts +7 -0
- package/types/iso-editor/ui/later.d.ts +12 -0
- package/types/iso-editor/ui/layer.d.ts +2 -0
- package/types/iso-editor/ui/menu.d.ts +21 -0
- package/types/iso-editor/ui/mount-kit.d.ts +31 -0
- package/types/iso-editor/ui/mount.d.ts +146 -0
- package/types/iso-editor/ui/pane.d.ts +60 -0
- package/types/iso-editor/ui/part.d.ts +40 -0
- package/types/iso-editor/ui/plain-icons.d.ts +10 -0
- package/types/iso-editor/ui/props.d.ts +57 -0
- package/types/iso-editor/ui/scope-css.d.ts +5 -0
- package/types/iso-editor/ui/setup.d.ts +40 -0
- package/types/iso-editor/ui/text.d.ts +19 -0
- package/types/iso-editor/ui/tree.d.ts +100 -0
- package/types/iso-editor/validate.d.ts +36 -0
- package/types/iso-editor/window/protocol.d.ts +60 -0
- package/types/iso-objects/assemble.d.ts +35 -0
- package/types/iso-objects/assets.d.ts +49 -0
- package/types/iso-objects/board.d.ts +36 -0
- package/types/iso-objects/compile.d.ts +35 -0
- package/types/iso-objects/css.d.ts +16 -0
- package/types/iso-objects/flat.d.ts +130 -0
- package/types/iso-objects/focus.d.ts +1 -0
- package/types/iso-objects/geometry.d.ts +59 -0
- package/types/iso-objects/index.d.ts +26 -0
- package/types/iso-objects/kinds.d.ts +53 -0
- package/types/iso-objects/labels.d.ts +92 -0
- package/types/iso-objects/place.d.ts +25 -0
- package/types/iso-objects/route.d.ts +67 -0
- package/types/iso-objects/scene.d.ts +63 -0
- package/types/iso-objects/steps.d.ts +90 -0
- package/types/iso-objects/types.d.ts +140 -0
- package/types/iso-systems/catalog.d.ts +4 -0
- package/types/iso-systems/compile.d.ts +6 -0
- package/types/iso-systems/css.d.ts +3 -0
- package/types/iso-systems/index.d.ts +5 -0
- package/types/iso-systems/nodes.d.ts +3 -0
- package/types/iso-viewer/card-motion.d.ts +56 -0
- package/types/iso-viewer/dom.d.ts +1 -0
- package/types/iso-viewer/dual-view.d.ts +44 -0
- package/types/iso-viewer/flat-view.d.ts +25 -0
- package/types/iso-viewer/history.d.ts +91 -0
- package/types/iso-viewer/index.d.ts +31 -0
- package/types/iso-viewer/morph.d.ts +28 -0
- package/types/iso-viewer/overlay.d.ts +135 -0
- package/types/iso-viewer/page.d.ts +13 -0
- package/types/iso-viewer/still.d.ts +26 -0
- package/types/iso-viewer/timeline-contract.d.ts +345 -0
- package/types/iso-viewer/timeline-engine.d.ts +20 -0
- package/types/iso-viewer/timeline-export.d.ts +5 -0
- package/types/iso-viewer/timeline-scale.d.ts +44 -0
- package/types/iso-viewer/timeline-scope.d.ts +33 -0
- package/types/iso-viewer/timeline-search.d.ts +52 -0
- package/types/iso-viewer/timeline-time.d.ts +8 -0
- package/types/iso-viewer/view-css.d.ts +7 -0
- package/types/iso-viewer/viewport.d.ts +50 -0
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
// The drawing format's JSON Schema validation (src/iso-systems/schema.json), in plain words tied to YAML lines; the
|
|
2
|
+
// shared machinery is in src/iso-core/schema-errors.ts, and this registers the format's own words with it.
|
|
3
|
+
|
|
4
|
+
import { compileSchema } from '../iso-core/compile-schema.ts';
|
|
5
|
+
import schema from './schema.json' with { type: 'json' };
|
|
6
|
+
import type { Diag } from '../iso-core/errors.ts';
|
|
7
|
+
import type { Parsed } from '../iso-core/parse.ts';
|
|
8
|
+
import { prune, registerConst, registerNoun, registerPattern, registerRefKeys } from '../iso-core/schema-errors.ts';
|
|
9
|
+
export { schemaValidator } from '../iso-core/schema-errors.ts';
|
|
10
|
+
|
|
11
|
+
const check = compileSchema(schema);
|
|
12
|
+
const placementKeys = Object.keys((schema.$defs.placement as { properties: object }).properties);
|
|
13
|
+
registerRefKeys('#/$defs/placement', placementKeys);
|
|
14
|
+
registerConst('/isodiag', got => `"isodiag" must be 1 (the format version), got ${got}`);
|
|
15
|
+
const what: Record<string, string> = {
|
|
16
|
+
scene: 'a scene', region: 'a region', node: 'a node', route: 'a route', note: 'a note', step: 'a step',
|
|
17
|
+
};
|
|
18
|
+
registerNoun(path => {
|
|
19
|
+
const m = /\/(scenes|regions|nodes|routes|notes|steps)\/\d+$/.exec(path);
|
|
20
|
+
return m ? what[m[1].replace(/s$/, '')] ?? 'this item' : undefined;
|
|
21
|
+
});
|
|
22
|
+
registerPattern('^[A-Za-z][A-Za-z0-9_-]*(', data => ({
|
|
23
|
+
message: `"${data}" is not a node reference`,
|
|
24
|
+
hint: 'write an id, optionally with a side: worker, worker.e (sides: n, e, s, w, top)',
|
|
25
|
+
}));
|
|
26
|
+
|
|
27
|
+
export function validateSchema(parsed: Parsed): Diag[] {
|
|
28
|
+
if (parsed.data == null || typeof parsed.data !== 'object' || Array.isArray(parsed.data)) {
|
|
29
|
+
return [{ severity: 'error', message: 'the file must be a YAML map with "isodiag: 1" and "scenes:"', line: 1, col: 1, path: '' }];
|
|
30
|
+
}
|
|
31
|
+
if (check(parsed.data)) return [];
|
|
32
|
+
return prune(check.errors ?? [], parsed);
|
|
33
|
+
}
|
|
34
|
+
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
// Bundles the player to one minified script string, to inline into pages. Node only (uses esbuild, a dev dependency).
|
|
2
|
+
|
|
3
|
+
import { buildSync } from 'esbuild';
|
|
4
|
+
import { fileURLToPath } from 'node:url';
|
|
5
|
+
|
|
6
|
+
let cached: string | undefined;
|
|
7
|
+
export function playerScript(): string {
|
|
8
|
+
if (cached) return cached;
|
|
9
|
+
const r = buildSync({
|
|
10
|
+
entryPoints: [fileURLToPath(new URL('./player.ts', import.meta.url))],
|
|
11
|
+
bundle: true, minify: true, format: 'iife', target: 'es2020', write: false, legalComments: 'none',
|
|
12
|
+
});
|
|
13
|
+
cached = r.outputFiles[0].text;
|
|
14
|
+
return cached;
|
|
15
|
+
}
|
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
// Motion for the overlay's cards, with effects to choose from. The overlay draws each card still; this moves the drawn
|
|
2
|
+
// cards and never changes what is drawn: an effect ends with the card exactly as the still drawing.
|
|
3
|
+
// - appear / disappear: scale (from and to nothing), spin (expand or shrink while turning about the card's own
|
|
4
|
+
// vertical axis, in 3D), dissolve (a fade with a slight blur), inflate (a spring past full size; disappear deflates);
|
|
5
|
+
// - idle while showing: none, jiggle, pirouette (a windup, a hop, one full 3D turn, a landing), glow (the amber edge on
|
|
6
|
+
// a slow period), breathe (a slow scale in and out); a card open ten minutes or more (still) keeps still.
|
|
7
|
+
// Spins are real 3D: the card's corners turned about its vertical axis and projected again each frame (the canvas's
|
|
8
|
+
// spin()); the tail stays on what the card is anchored to. Everything else is the Web Animations API on the card's
|
|
9
|
+
// group, its origin at the tail's tip, so a card grows out of its anchor. Idle effects are staggered by the card's key,
|
|
10
|
+
// on the document's clock, so a card redrawn (its text changed) keeps its phase. Off: nothing moves, at all.
|
|
11
|
+
// The cards' class and attribute names are the domain's (CardNames): this module names none.
|
|
12
|
+
|
|
13
|
+
export type Appear = 'scale' | 'spin' | 'dissolve' | 'inflate';
|
|
14
|
+
export type Idle = 'none' | 'jiggle' | 'pirouette' | 'glow' | 'breathe';
|
|
15
|
+
export interface MotionSettings { enabled: boolean; appear: Appear; idle: Idle }
|
|
16
|
+
export const APPEARS: [Appear, string][] = [['scale', 'Scale'], ['spin', 'Spin'], ['dissolve', 'Dissolve'], ['inflate', 'Inflate']];
|
|
17
|
+
export const IDLES: [Idle, string][] = [['none', 'None'], ['jiggle', 'Jiggle'], ['pirouette', 'Pirouette'], ['glow', 'Glow'], ['breathe', 'Breathe']];
|
|
18
|
+
|
|
19
|
+
/** the domain's cards: their class prefix, as the overlay draws them (PREFIX-float, a card that has gone PREFIX-leaving,
|
|
20
|
+
* in the PREFIX-leaving-layer), and the attributes a card's key is read from: key, else alt (removed when it goes) */
|
|
21
|
+
export interface CardNames { prefix: string; key: string; alt: string }
|
|
22
|
+
|
|
23
|
+
export interface MotionHost {
|
|
24
|
+
/** turn a card about its vertical axis (radians), lifted (cells): its faces drawn again */
|
|
25
|
+
spin(card: SVGGElement, theta: number, lift: number): void;
|
|
26
|
+
/** the card as drawn still again */
|
|
27
|
+
restore(card: SVGGElement): void;
|
|
28
|
+
/** where a card that has gone may finish going (outside the state layer, which is reconciled) */
|
|
29
|
+
leavingLayer(): SVGGElement | undefined;
|
|
30
|
+
/** the amber of the cards' edge */
|
|
31
|
+
amber(): string;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** a stable offset in [0, 1) from an id: cards don't move in unison */
|
|
35
|
+
function phaseOf(id: string): number {
|
|
36
|
+
let h = 2166136261;
|
|
37
|
+
for (let i = 0; i < id.length; i++) { h ^= id.charCodeAt(i); h = Math.imul(h, 16777619); }
|
|
38
|
+
return ((h >>> 0) % 1000) / 1000;
|
|
39
|
+
}
|
|
40
|
+
const ease = (t: number) => (t < 0 ? 0 : t > 1 ? 1 : 1 - Math.pow(1 - t, 3));
|
|
41
|
+
const easeIn = (t: number) => (t < 0 ? 0 : t > 1 ? 1 : t * t * t);
|
|
42
|
+
const TAU = Math.PI * 2;
|
|
43
|
+
|
|
44
|
+
/** the timings, in ms (divided by speed, for the tests) */
|
|
45
|
+
const T = { appear: { scale: 420, spin: 760, dissolve: 520, inflate: 640 }, disappear: { scale: 320, spin: 600, dissolve: 420, inflate: 420 },
|
|
46
|
+
period: { jiggle: 4200, pirouette: 6400, glow: 3200, breathe: 4400 } };
|
|
47
|
+
/** pirouette: windup, then hop and one turn, then landing (ms into its period) */
|
|
48
|
+
const PIR = { windup: 220, turn: 980, land: 260 };
|
|
49
|
+
|
|
50
|
+
interface Job { card: SVGGElement; start: number; dur: number; frame(t: number): { theta: number; lift: number; scale: number }; done?(): void; repeat?: number; offset?: number }
|
|
51
|
+
|
|
52
|
+
export function cardMotion(host: MotionHost, initial: MotionSettings, { prefix, key, alt }: CardNames) {
|
|
53
|
+
let s = { ...initial };
|
|
54
|
+
let speed = 1;
|
|
55
|
+
const jobs = new Map<SVGGElement, Job>();
|
|
56
|
+
const turned = new WeakSet<SVGGElement>();
|
|
57
|
+
let raf = 0, frames = 0;
|
|
58
|
+
const idleAnims = new WeakMap<SVGGElement, Animation>();
|
|
59
|
+
/** each turning card's last frame (for the tests: the model the drawing must equal) */
|
|
60
|
+
const last = new WeakMap<SVGGElement, { theta: number; lift: number; scale: number }>();
|
|
61
|
+
|
|
62
|
+
// ---------- the spin loop (rAF): only while a job wants it ----------
|
|
63
|
+
function tick(now: number) {
|
|
64
|
+
raf = 0;
|
|
65
|
+
for (const [card, j] of jobs) {
|
|
66
|
+
if (!card.isConnected) { jobs.delete(card); continue; }
|
|
67
|
+
let t: number;
|
|
68
|
+
if (j.repeat) {
|
|
69
|
+
// a repeating idle job (pirouette): where it is in its period, on the document's clock
|
|
70
|
+
const p = ((now + (j.offset ?? 0)) % j.repeat);
|
|
71
|
+
t = p / j.dur;
|
|
72
|
+
if (t > 1) { if (turned.has(card)) { host.restore(card); turned.delete(card); card.style.removeProperty('transform'); } continue; }
|
|
73
|
+
} else {
|
|
74
|
+
t = (now - j.start) / j.dur;
|
|
75
|
+
if (t >= 1) { finish(card, j); continue; }
|
|
76
|
+
}
|
|
77
|
+
const f = j.frame(Math.max(0, t));
|
|
78
|
+
host.spin(card, f.theta, f.lift); turned.add(card); last.set(card, f);
|
|
79
|
+
if (f.scale !== 1) card.style.transform = `scale(${f.scale.toFixed(4)})`; else card.style.removeProperty('transform');
|
|
80
|
+
frames++;
|
|
81
|
+
}
|
|
82
|
+
if (jobs.size) raf = requestAnimationFrame(tick);
|
|
83
|
+
}
|
|
84
|
+
function finish(card: SVGGElement, j: Job) {
|
|
85
|
+
jobs.delete(card);
|
|
86
|
+
if (turned.has(card)) { host.restore(card); turned.delete(card); }
|
|
87
|
+
card.style.removeProperty('transform');
|
|
88
|
+
j.done?.();
|
|
89
|
+
}
|
|
90
|
+
function run(j: Job) {
|
|
91
|
+
const old = jobs.get(j.card);
|
|
92
|
+
if (old) finish(j.card, old);
|
|
93
|
+
jobs.set(j.card, j);
|
|
94
|
+
if (!raf) raf = requestAnimationFrame(tick);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// ---------- appear and disappear ----------
|
|
98
|
+
/** at most this many appear or disappear effects at once (Monkey at 10×): past it, a card comes or goes at once */
|
|
99
|
+
const MAX_EFFECTS = 8;
|
|
100
|
+
let effects = 0;
|
|
101
|
+
const counted = <T>(p: Promise<T>) => { effects++; return p.finally(() => { effects--; }); };
|
|
102
|
+
function appear(card: SVGGElement): Promise<void> {
|
|
103
|
+
if (!s.enabled || effects >= MAX_EFFECTS) return Promise.resolve();
|
|
104
|
+
return counted(appearNow(card));
|
|
105
|
+
}
|
|
106
|
+
function disappear(card: SVGGElement): Promise<void> {
|
|
107
|
+
if (!s.enabled || effects >= MAX_EFFECTS) return Promise.resolve();
|
|
108
|
+
return counted(disappearNow(card));
|
|
109
|
+
}
|
|
110
|
+
function appearNow(card: SVGGElement): Promise<void> {
|
|
111
|
+
const d = T.appear[s.appear] / speed;
|
|
112
|
+
if (s.appear === 'spin') {
|
|
113
|
+
return new Promise(res => run({ card, start: performance.now(), dur: d, frame: t => ({ theta: (1 - ease(t)) * TAU, lift: 0, scale: Math.max(0.001, ease(t)) }), done: res }));
|
|
114
|
+
}
|
|
115
|
+
const kf: Keyframe[] = s.appear === 'scale' ? [{ transform: 'scale(0)' }, { transform: 'scale(1)' }]
|
|
116
|
+
: s.appear === 'dissolve' ? [{ opacity: 0, filter: 'blur(4px)' }, { opacity: 1, filter: 'blur(0px)' }]
|
|
117
|
+
: [{ transform: 'scale(0)', easing: 'cubic-bezier(.3,0,.4,1)' }, { transform: 'scale(1.16)', offset: 0.55, easing: 'ease-in-out' }, { transform: 'scale(.95)', offset: 0.78, easing: 'ease-in-out' }, { transform: 'scale(1)' }];
|
|
118
|
+
const a = card.animate(kf, { duration: d, easing: s.appear === 'inflate' ? 'linear' : 'ease-out' });
|
|
119
|
+
return a.finished.then(() => undefined, () => undefined);
|
|
120
|
+
}
|
|
121
|
+
function disappearNow(card: SVGGElement): Promise<void> {
|
|
122
|
+
const d = T.disappear[s.appear] / speed;
|
|
123
|
+
if (s.appear === 'spin') {
|
|
124
|
+
return new Promise(res => run({ card, start: performance.now(), dur: d, frame: t => ({ theta: easeIn(t) * TAU, lift: 0, scale: Math.max(0.001, 1 - easeIn(t)) }), done: () => { card.style.transform = 'scale(0)'; res(); } }));
|
|
125
|
+
}
|
|
126
|
+
const kf: Keyframe[] = s.appear === 'scale' ? [{ transform: 'scale(1)' }, { transform: 'scale(0)' }]
|
|
127
|
+
: s.appear === 'dissolve' ? [{ opacity: 1, filter: 'blur(0px)' }, { opacity: 0, filter: 'blur(4px)' }]
|
|
128
|
+
: [{ transform: 'scale(1)' }, { transform: 'scale(1.08)', offset: 0.3 }, { transform: 'scale(0)' }];
|
|
129
|
+
const a = card.animate(kf, { duration: d, easing: 'ease-in', fill: 'forwards' });
|
|
130
|
+
return a.finished.then(() => undefined, () => undefined);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// ---------- idle ----------
|
|
134
|
+
function stopIdle(card: SVGGElement) {
|
|
135
|
+
idleAnims.get(card)?.cancel(); idleAnims.delete(card);
|
|
136
|
+
const j = jobs.get(card);
|
|
137
|
+
if (j?.repeat) finish(card, j);
|
|
138
|
+
}
|
|
139
|
+
/** start the idle effect, at the card's own phase (period: a shorter one for Preview) */
|
|
140
|
+
function startIdle(card: SVGGElement, period?: number) {
|
|
141
|
+
stopIdle(card);
|
|
142
|
+
if (!s.enabled || s.idle === 'none' || card.classList.contains('still')) return;
|
|
143
|
+
const id = card.getAttribute(key) ?? card.getAttribute(alt) ?? '';
|
|
144
|
+
const P = (period ?? T.period[s.idle]) / speed, off = phaseOf(id) * P;
|
|
145
|
+
if (s.idle === 'pirouette') {
|
|
146
|
+
const dur = (PIR.windup + PIR.turn + PIR.land) / speed;
|
|
147
|
+
const w = PIR.windup / (PIR.windup + PIR.turn + PIR.land), l = (PIR.windup + PIR.turn) / (PIR.windup + PIR.turn + PIR.land);
|
|
148
|
+
run({ card, start: 0, dur, repeat: P, offset: off, frame: t => {
|
|
149
|
+
if (t < w) return { theta: -0.35 * Math.sin((t / w) * Math.PI / 2), lift: 0, scale: 1 };
|
|
150
|
+
if (t < l) { const u = (t - w) / (l - w); return { theta: -0.35 * (1 - u) + ease(u) * TAU, lift: 0.9 * Math.sin(u * Math.PI), scale: 1 }; }
|
|
151
|
+
const u = (t - l) / (1 - l); return { theta: 0, lift: 0, scale: 1 - 0.04 * Math.sin(u * Math.PI) };
|
|
152
|
+
} });
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
const float = card.querySelector<SVGGElement>(`.${prefix}-float`) ?? card;
|
|
156
|
+
const amber = host.amber();
|
|
157
|
+
const kf: Keyframe[] = s.idle === 'jiggle'
|
|
158
|
+
? [{ transform: 'scale(1)', offset: 0 }, { transform: 'scale(1.07)', offset: 0.04 }, { transform: 'scale(.97)', offset: 0.08 }, { transform: 'scale(1.02)', offset: 0.11 }, { transform: 'scale(1)', offset: 0.14 }, { transform: 'scale(1)', offset: 1 }]
|
|
159
|
+
: s.idle === 'glow' ? [{ filter: `drop-shadow(0 0 0px ${amber})` }, { filter: `drop-shadow(0 0 7px ${amber})` }, { filter: `drop-shadow(0 0 0px ${amber})` }]
|
|
160
|
+
: [{ transform: 'scale(1)' }, { transform: 'scale(1.035)' }, { transform: 'scale(1)' }];
|
|
161
|
+
const a = float.animate(kf, { duration: P, iterations: Infinity, easing: s.idle === 'jiggle' ? 'linear' : 'ease-in-out' });
|
|
162
|
+
// on the document's clock: the same card (or its redrawing) is always at the same point of its cycle
|
|
163
|
+
a.startTime = (document.timeline.currentTime as number ?? 0) - (((document.timeline.currentTime as number ?? 0) + off) % P);
|
|
164
|
+
idleAnims.set(card, a);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
// ---------- after each repaint of the state layer ----------
|
|
168
|
+
/** prev and cur: the cards by key before and after; gone: the keys whose cards went, and whether each expired */
|
|
169
|
+
function update(prev: Map<string, SVGGElement>, cur: Map<string, SVGGElement>, gone: Map<string, boolean>, animateOut: boolean, still = false) {
|
|
170
|
+
// still (O8b: a scrub, a step back): what appears or goes does so at once
|
|
171
|
+
if (still) animateOut = false;
|
|
172
|
+
for (const [from, card] of cur) {
|
|
173
|
+
if (!prev.has(from) && !still) void appear(card);
|
|
174
|
+
if (!idleAnims.has(card) && !jobs.get(card)?.repeat) startIdle(card);
|
|
175
|
+
}
|
|
176
|
+
if (!s.enabled || !animateOut) return;
|
|
177
|
+
const layer = host.leavingLayer();
|
|
178
|
+
if (!layer) return;
|
|
179
|
+
for (const [from, expired] of gone) {
|
|
180
|
+
const old = prev.get(from);
|
|
181
|
+
if (!old || cur.has(from)) continue;
|
|
182
|
+
// the drawn card, kept a moment where it was, then gone by the chosen effect
|
|
183
|
+
stopIdle(old);
|
|
184
|
+
old.setAttribute('class', `${prefix}-leaving${expired ? ' expired' : ''}`);
|
|
185
|
+
old.removeAttribute(alt);
|
|
186
|
+
layer.append(old);
|
|
187
|
+
void disappear(old).then(() => old.remove());
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/** stop everything now: off, or the layer cleared */
|
|
192
|
+
function stopAll(root: ParentNode | undefined) {
|
|
193
|
+
for (const [card, j] of [...jobs]) finish(card, j);
|
|
194
|
+
if (raf) cancelAnimationFrame(raf); raf = 0;
|
|
195
|
+
for (const a of document.getAnimations()) {
|
|
196
|
+
const t = (a.effect as KeyframeEffect | null)?.target as Element | null;
|
|
197
|
+
if (t?.closest?.(`.live-layer, .${prefix}-leaving-layer`)) a.cancel();
|
|
198
|
+
}
|
|
199
|
+
host.leavingLayer()?.replaceChildren();
|
|
200
|
+
void root;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
return {
|
|
204
|
+
get settings() { return { ...s }; },
|
|
205
|
+
set(next: Partial<MotionSettings>, cards: Iterable<SVGGElement>) {
|
|
206
|
+
const was = s; s = { ...s, ...next };
|
|
207
|
+
if (!s.enabled || was.idle !== s.idle || was.enabled !== s.enabled) {
|
|
208
|
+
stopAll(undefined);
|
|
209
|
+
if (s.enabled) for (const c of cards) startIdle(c);
|
|
210
|
+
}
|
|
211
|
+
},
|
|
212
|
+
update, appear, disappear, startIdle, stopIdle, stopAll,
|
|
213
|
+
/** Preview: on each card, appear, then a few idle cycles, then disappear, then back as it was (still) and idling */
|
|
214
|
+
async preview(cards: SVGGElement[]) {
|
|
215
|
+
if (!s.enabled) return;
|
|
216
|
+
await Promise.all(cards.map(async c => {
|
|
217
|
+
stopIdle(c);
|
|
218
|
+
await appear(c);
|
|
219
|
+
if (s.idle !== 'none' && !c.classList.contains('still')) {
|
|
220
|
+
const P = s.idle === 'pirouette' ? 1800 : 1400;
|
|
221
|
+
startIdle(c, P);
|
|
222
|
+
await new Promise(r => setTimeout(r, (P * 2.2) / speed));
|
|
223
|
+
stopIdle(c);
|
|
224
|
+
} else await new Promise(r => setTimeout(r, 700 / speed));
|
|
225
|
+
await disappear(c);
|
|
226
|
+
for (const a of c.getAnimations()) a.cancel();
|
|
227
|
+
c.style.removeProperty('transform');
|
|
228
|
+
startIdle(c);
|
|
229
|
+
}));
|
|
230
|
+
},
|
|
231
|
+
/** for the tests */
|
|
232
|
+
get stats() { return { jobs: jobs.size, raf: !!raf, frames, effects }; },
|
|
233
|
+
setSpeed(n: number) { speed = n; },
|
|
234
|
+
lastFrame: (card: SVGGElement) => last.get(card),
|
|
235
|
+
};
|
|
236
|
+
}
|
|
237
|
+
export type CardMotion = ReturnType<typeof cardMotion>;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
// h(): the DOM, built as text only (never HTML): an element with its attributes, listeners and children.
|
|
2
|
+
|
|
3
|
+
export function h<K extends keyof HTMLElementTagNameMap>(tag: K, attrs: Record<string, unknown> | null = null, ...kids: (Node | string | null | undefined | false)[]): HTMLElementTagNameMap[K] {
|
|
4
|
+
const el = document.createElement(tag);
|
|
5
|
+
for (const [k, v] of Object.entries(attrs ?? {})) {
|
|
6
|
+
if (v === undefined || v === null || v === false) continue;
|
|
7
|
+
if (k === 'class') el.className = String(v);
|
|
8
|
+
else if (k === 'on') for (const [ev, fn] of Object.entries(v as Record<string, EventListener>)) el.addEventListener(ev, fn);
|
|
9
|
+
else el.setAttribute(k, v === true ? '' : String(v));
|
|
10
|
+
}
|
|
11
|
+
for (const kid of kids) if (kid !== undefined && kid !== null && kid !== false) el.append(kid instanceof Node ? kid : document.createTextNode(String(kid)));
|
|
12
|
+
return el;
|
|
13
|
+
}
|
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
// One drawing in two views (0.6.0): the engine's 3D scene and its 2D layout, with an animated morph between them both
|
|
2
|
+
// ways, and the 3D | 2D switch. Both views carry the same data-id on things and data-a / data-b on connectors, so a
|
|
3
|
+
// selection (and its highlighted connectors) holds in either, and through the switch.
|
|
4
|
+
//
|
|
5
|
+
// The morph (about 600 ms): the connectors fade out; each thing (matched by data-id) moves and resizes from where it
|
|
6
|
+
// is in one view to where it is in the other, its 3D top face becoming (or coming from) the 2D card while the rest of
|
|
7
|
+
// it cross-fades; the new view's connectors draw in. It runs on a layer of its own over both views; when it ends, or
|
|
8
|
+
// is interrupted by another switch, the layer goes and the final view is shown exactly. With prefers-reduced-motion the
|
|
9
|
+
// switch is instant, unless the host says `motion: 'always'` ('never': always instant).
|
|
10
|
+
|
|
11
|
+
import { parseEnd } from '../iso-objects/route.ts';
|
|
12
|
+
import type { CompiledScene } from '../iso-objects/compile.ts';
|
|
13
|
+
import { boardOf } from '../iso-objects/board.ts';
|
|
14
|
+
import { layoutFlat, type Board, type FlatLayout, type FlatOptions } from '../iso-objects/flat.ts';
|
|
15
|
+
import { flatSvg, measureIn, type FlatStyle } from './flat-view.ts';
|
|
16
|
+
import { buildLayer, clamp, ease, markConnectors, type ViewMode } from './morph.ts';
|
|
17
|
+
|
|
18
|
+
export type { ViewMode } from './morph.ts';
|
|
19
|
+
export interface DualViewOptions {
|
|
20
|
+
mode?: ViewMode;
|
|
21
|
+
/** the board the 2D view draws; default: boardOf(scene) */
|
|
22
|
+
board?: Board;
|
|
23
|
+
/** link kinds' styles in 2D */
|
|
24
|
+
styles?: Record<string, FlatStyle>;
|
|
25
|
+
flat?: Partial<FlatOptions>;
|
|
26
|
+
/** the svg's label */
|
|
27
|
+
title?: string;
|
|
28
|
+
onSelect?(id: string | undefined): void;
|
|
29
|
+
/** the morph's length, ms */
|
|
30
|
+
duration?: number;
|
|
31
|
+
/** whether a switch animates (0.7.3): 'auto' honours prefers-reduced-motion (the default), 'always' morphs
|
|
32
|
+
* regardless (a host's deliberate choice for a switch the user asked for), 'never' always swaps at once */
|
|
33
|
+
motion?: 'auto' | 'always' | 'never';
|
|
34
|
+
}
|
|
35
|
+
export interface DualView {
|
|
36
|
+
readonly mode: ViewMode;
|
|
37
|
+
readonly layout: FlatLayout;
|
|
38
|
+
readonly root: HTMLElement;
|
|
39
|
+
/** switch view, animated (instant with reduced motion); resolves when the view is shown */
|
|
40
|
+
morph(to: ViewMode): Promise<void>;
|
|
41
|
+
select(id: string | undefined): void;
|
|
42
|
+
readonly selected: string | undefined;
|
|
43
|
+
/** the svg of a view */
|
|
44
|
+
svgOf(mode: ViewMode): SVGSVGElement;
|
|
45
|
+
destroy(): void;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
let seq = 0;
|
|
49
|
+
|
|
50
|
+
/** the scene in both views, in `host`: the 3D drawing as the engine rendered it, the 2D one laid out from its board */
|
|
51
|
+
export function dualView(host: HTMLElement, scene: CompiledScene, opts: DualViewOptions = {}): DualView {
|
|
52
|
+
const uid = `dv${++seq}`;
|
|
53
|
+
const root = document.createElement('div');
|
|
54
|
+
root.className = 'dual-view';
|
|
55
|
+
root.style.position = 'relative';
|
|
56
|
+
host.appendChild(root);
|
|
57
|
+
const wrap3 = document.createElement('div'), wrap2 = document.createElement('div');
|
|
58
|
+
wrap3.className = 'dual-3d'; wrap2.className = 'dual-2d';
|
|
59
|
+
root.append(wrap3, wrap2);
|
|
60
|
+
wrap3.innerHTML = scene.rendered.svg;
|
|
61
|
+
const svg3 = wrap3.querySelector('svg') as SVGSVGElement;
|
|
62
|
+
svg3.setAttribute('data-view', '3d');
|
|
63
|
+
const board = opts.board ?? boardOf(scene);
|
|
64
|
+
markConnectors(svg3, scene, board);
|
|
65
|
+
const routeIds = new Set(scene.routes.map(r => r.id));
|
|
66
|
+
// 3D things are reachable by keyboard, as the 2D cards are
|
|
67
|
+
for (const g of svg3.querySelectorAll('g[data-id]')) {
|
|
68
|
+
const id = g.getAttribute('data-id')!;
|
|
69
|
+
if (routeIds.has(id) || g.hasAttribute('tabindex') || !g.querySelector('polygon')) continue;
|
|
70
|
+
g.setAttribute('tabindex', '0'); g.setAttribute('role', 'button'); g.setAttribute('aria-label', id);
|
|
71
|
+
}
|
|
72
|
+
const measure = measureIn(wrap2);
|
|
73
|
+
const layout = layoutFlat(board, measure, opts.flat);
|
|
74
|
+
measure.dispose();
|
|
75
|
+
wrap2.innerHTML = flatSvg(layout, { id: uid, title: opts.title, styles: opts.styles });
|
|
76
|
+
const svg2 = wrap2.querySelector('svg') as SVGSVGElement;
|
|
77
|
+
for (const s of [svg3, svg2]) { s.style.display = 'block'; s.style.width = '100%'; s.style.height = 'auto'; }
|
|
78
|
+
if (opts.title) svg3.setAttribute('aria-label', opts.title);
|
|
79
|
+
|
|
80
|
+
let mode: ViewMode = opts.mode ?? '3d', sel: string | undefined;
|
|
81
|
+
const wrapOf = (m: ViewMode) => (m === '3d' ? wrap3 : wrap2);
|
|
82
|
+
const svgOf = (m: ViewMode) => (m === '3d' ? svg3 : svg2);
|
|
83
|
+
const show = (m: ViewMode) => {
|
|
84
|
+
wrap3.hidden = m !== '3d'; wrap2.hidden = m !== '2d';
|
|
85
|
+
for (const w of [wrap3, wrap2]) w.style.cssText = '';
|
|
86
|
+
root.style.height = '';
|
|
87
|
+
root.dataset.mode = m;
|
|
88
|
+
};
|
|
89
|
+
show(mode);
|
|
90
|
+
|
|
91
|
+
const paint = () => {
|
|
92
|
+
for (const s of [svg3, svg2]) {
|
|
93
|
+
for (const e of s.querySelectorAll('[data-id]')) e.classList.toggle('sel', !!sel && e.getAttribute('data-id') === sel);
|
|
94
|
+
for (const e of s.querySelectorAll('[data-a]')) {
|
|
95
|
+
const on = !!sel && (e.getAttribute('data-a') === sel || e.getAttribute('data-b') === sel);
|
|
96
|
+
e.classList.toggle('hot', on); e.classList.toggle('dim', !!sel && !on);
|
|
97
|
+
const lab = s.querySelector(`[data-for="${CSS.escape(e.getAttribute('data-id') ?? '')}"]`);
|
|
98
|
+
lab?.classList.toggle('dim', !!sel && !on);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
};
|
|
102
|
+
const select = (id: string | undefined) => { sel = id; paint(); };
|
|
103
|
+
const onClick = (e: Event) => {
|
|
104
|
+
const g = (e.target as Element).closest?.('[data-id]');
|
|
105
|
+
if (!g || !root.contains(g) || g.hasAttribute('data-a')) return;
|
|
106
|
+
const id = g.getAttribute('data-id')!;
|
|
107
|
+
select(id); opts.onSelect?.(id);
|
|
108
|
+
};
|
|
109
|
+
const onKey = (e: KeyboardEvent) => { if ((e.key === 'Enter' || e.key === ' ') && (e.target as Element).matches?.('[data-id][tabindex]')) { e.preventDefault(); onClick(e); } };
|
|
110
|
+
root.addEventListener('click', onClick);
|
|
111
|
+
root.addEventListener('keydown', onKey);
|
|
112
|
+
|
|
113
|
+
// ---- the morph ----
|
|
114
|
+
let run: { frame: number; layer: SVGSVGElement; to: ViewMode; done: () => void } | undefined;
|
|
115
|
+
const finish = () => {
|
|
116
|
+
if (!run) return;
|
|
117
|
+
cancelAnimationFrame(run.frame);
|
|
118
|
+
run.layer.remove();
|
|
119
|
+
const r = run; run = undefined;
|
|
120
|
+
mode = r.to; show(mode); paint();
|
|
121
|
+
r.done();
|
|
122
|
+
};
|
|
123
|
+
const focusSelected = (had: boolean) => {
|
|
124
|
+
if (!had || !sel) return;
|
|
125
|
+
(svgOf(mode).querySelector(`[data-id="${CSS.escape(sel)}"][tabindex]`) as SVGElement | null)?.focus({ preventScroll: true });
|
|
126
|
+
};
|
|
127
|
+
const morph = (to: ViewMode): Promise<void> => {
|
|
128
|
+
const hadFocus = root.contains(document.activeElement);
|
|
129
|
+
if (run) finish();
|
|
130
|
+
if (to === mode) { focusSelected(hadFocus); return Promise.resolve(); }
|
|
131
|
+
const motion = opts.motion ?? 'auto';
|
|
132
|
+
const reduced = motion === 'never' || (motion === 'auto' && typeof matchMedia === 'function' && matchMedia('(prefers-reduced-motion: reduce)').matches);
|
|
133
|
+
if (reduced || !(opts.duration ?? 600)) { mode = to; show(mode); paint(); focusSelected(hadFocus); return Promise.resolve(); }
|
|
134
|
+
return new Promise<void>(resolve => {
|
|
135
|
+
const from = mode;
|
|
136
|
+
// both views laid out, stacked; the layer draws everything while it runs
|
|
137
|
+
const h0 = root.getBoundingClientRect().height;
|
|
138
|
+
wrap3.hidden = false; wrap2.hidden = false;
|
|
139
|
+
for (const w of [wrap3, wrap2]) w.style.cssText = 'position:absolute;left:0;top:0;right:0;visibility:hidden';
|
|
140
|
+
const h1 = wrapOf(to).getBoundingClientRect().height;
|
|
141
|
+
root.style.height = `${h0}px`;
|
|
142
|
+
const layer = buildLayer(root, svgOf(from), svgOf(to), from);
|
|
143
|
+
root.appendChild(layer.svg);
|
|
144
|
+
const T = opts.duration ?? 600, t0 = performance.now();
|
|
145
|
+
// each frame is this run's: a frame left over from a run already finished does nothing
|
|
146
|
+
const me = { frame: 0, layer: layer.svg, to, done: () => { focusSelected(hadFocus); resolve(); } };
|
|
147
|
+
const step = (now: number) => {
|
|
148
|
+
if (run !== me) return;
|
|
149
|
+
const t = Math.min(1, (now - t0) / T);
|
|
150
|
+
layer.at(t);
|
|
151
|
+
root.style.height = `${h0 + (h1 - h0) * ease(clamp((t - 0.2) / 0.6))}px`;
|
|
152
|
+
if (t < 1) me.frame = requestAnimationFrame(step);
|
|
153
|
+
else finish();
|
|
154
|
+
};
|
|
155
|
+
run = me;
|
|
156
|
+
me.frame = requestAnimationFrame(step);
|
|
157
|
+
});
|
|
158
|
+
};
|
|
159
|
+
|
|
160
|
+
return {
|
|
161
|
+
get mode() { return run ? run.to : mode; },
|
|
162
|
+
layout, root,
|
|
163
|
+
morph, select,
|
|
164
|
+
get selected() { return sel; },
|
|
165
|
+
svgOf,
|
|
166
|
+
destroy() { if (run) finish(); root.removeEventListener('click', onClick); root.removeEventListener('keydown', onKey); root.remove(); },
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
// ---- the switch ----
|
|
171
|
+
|
|
172
|
+
/** the view a page last showed (localStorage), if any */
|
|
173
|
+
export function storedMode(key = 'iso-view'): ViewMode | undefined {
|
|
174
|
+
try { const v = localStorage.getItem(key); return v === '2d' || v === '3d' ? v : undefined; } catch { return undefined; }
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/** the 3D | 2D switch: two buttons (aria-pressed), the choice remembered in localStorage; it switches every view given */
|
|
178
|
+
export function viewToggle(host: HTMLElement, views: DualView | DualView[], opts: { storageKey?: string; labels?: [string, string] } = {}): HTMLElement {
|
|
179
|
+
const key = opts.storageKey ?? 'iso-view', list = Array.isArray(views) ? views : [views];
|
|
180
|
+
const box = document.createElement('div');
|
|
181
|
+
box.className = 'view-switch';
|
|
182
|
+
box.setAttribute('role', 'group');
|
|
183
|
+
box.setAttribute('aria-label', 'Drawing view');
|
|
184
|
+
const btns = (['3d', '2d'] as ViewMode[]).map((m, i) => {
|
|
185
|
+
const b = document.createElement('button');
|
|
186
|
+
b.type = 'button'; b.dataset.mode = m; b.textContent = (opts.labels ?? ['3D', '2D'])[i];
|
|
187
|
+
b.addEventListener('click', () => {
|
|
188
|
+
try { localStorage.setItem(key, m); } catch { /* storage may be off */ }
|
|
189
|
+
paint(m);
|
|
190
|
+
for (const v of list) void v.morph(m);
|
|
191
|
+
});
|
|
192
|
+
box.appendChild(b);
|
|
193
|
+
return b;
|
|
194
|
+
});
|
|
195
|
+
const paint = (m: ViewMode) => btns.forEach(b => b.setAttribute('aria-pressed', String(b.dataset.mode === m)));
|
|
196
|
+
paint(list[0]?.mode ?? '3d');
|
|
197
|
+
host.appendChild(box);
|
|
198
|
+
return box;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/** the switch's and the views' own CSS (unscoped) */
|
|
202
|
+
export const DUAL_VIEW_CSS = `
|
|
203
|
+
.view-switch{display:inline-flex;border:1px solid var(--line);border-radius:8px;overflow:hidden;background:var(--surface);font:600 12px/1 system-ui,sans-serif;box-shadow:0 1px 4px rgba(0,0,0,.12)}
|
|
204
|
+
.view-switch button{font:inherit;border:0;background:none;color:var(--ink);padding:6px 10px;cursor:pointer}
|
|
205
|
+
.view-switch button+button{border-left:1px solid var(--line)}
|
|
206
|
+
.view-switch button[aria-pressed=true]{background:var(--accent,#2f6fd6);color:#fff}
|
|
207
|
+
.view-switch button:focus-visible{outline:2px solid var(--accent,#2f6fd6);outline-offset:-2px}
|
|
208
|
+
.dual-view [data-id][tabindex]:focus-visible{outline:none}
|
|
209
|
+
.dual-view .blk.sel polygon{stroke:var(--accent,#2f6fd6);stroke-width:2.4}
|
|
210
|
+
.dual-view [data-a].dim{opacity:.3}
|
|
211
|
+
.dual-view [data-a].hot path{stroke-width:3.4}
|
|
212
|
+
`;
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
// The 2D view drawn (0.6.0): a flat layout (src/iso-objects/flat.ts) as SVG, its stylesheet, and the measure a page
|
|
2
|
+
// lays it out with (its own rendered text). The SVG is a string, so Node makes stills of it as a page shows it.
|
|
3
|
+
//
|
|
4
|
+
// What it carries, as the 3D drawing does: data-id on every card and panel, data-id, data-a and data-b on every
|
|
5
|
+
// connector (and its label: data-for), so selection and highlighting work in either view. Colours are the theme's:
|
|
6
|
+
// a card's or panel's role (its hue and saturation, the theme's face lightness), a link kind's colour token.
|
|
7
|
+
|
|
8
|
+
import { tag, esc } from '../iso-core/kernel.ts';
|
|
9
|
+
import type { FlatLayout, FlatRect, Measure, FlatFont } from '../iso-objects/flat.ts';
|
|
10
|
+
|
|
11
|
+
/** how a link kind is drawn: its ends, its dash, its colour (a theme token, e.g. "send", or any CSS colour) */
|
|
12
|
+
export interface FlatStyle { start?: 'arrow' | 'open' | 'diamond' | 'none'; end?: 'arrow' | 'open' | 'diamond' | 'none'; dash?: string; color?: string; width?: number }
|
|
13
|
+
|
|
14
|
+
const MARKS = {
|
|
15
|
+
arrow: { d: 'M0,0 L10,5 L0,10 z', refX: 9 },
|
|
16
|
+
open: { d: 'M0,0 L10,5 L0,10 L3,5 z', refX: 9 },
|
|
17
|
+
diamond: { d: 'M0,5 L5,1.5 L10,5 L5,8.5 z', refX: 1 },
|
|
18
|
+
} as const;
|
|
19
|
+
const f = (v: number) => (Math.round(v * 10) / 10).toString();
|
|
20
|
+
const kindOf = (k: string | undefined) => (k ?? 'link').replace(/[^\w-]/g, '_');
|
|
21
|
+
|
|
22
|
+
/** the 2D drawing: panels, connectors, cards, then the connectors' labels. `id` prefixes its marker ids. */
|
|
23
|
+
export function flatSvg(L: FlatLayout, opts: { id: string; title?: string; styles?: Record<string, FlatStyle> }): string {
|
|
24
|
+
const { id } = opts, styles = opts.styles ?? {}, o = L.options;
|
|
25
|
+
const b = L.bounds;
|
|
26
|
+
const used = new Map<string, Set<string>>();
|
|
27
|
+
const mark = (kind: string, end: 'start' | 'end') => {
|
|
28
|
+
const st = styles[kind] ?? {}, shape = end === 'start' ? (st.start ?? 'none') : (st.end ?? 'arrow');
|
|
29
|
+
if (shape === 'none') return undefined;
|
|
30
|
+
(used.get(kind) ?? used.set(kind, new Set()).get(kind)!).add(shape);
|
|
31
|
+
return `url(#${id}-mk-${kind}-${shape})`;
|
|
32
|
+
};
|
|
33
|
+
let panels = '', conns = '', cards = '', labels = '';
|
|
34
|
+
for (const p of [...L.panels].sort((x, y) => x.depth - y.depth)) {
|
|
35
|
+
const r = p.rect;
|
|
36
|
+
const title = p.label ? tag('text', { class: 'pl', x: p.titleAt[0], y: p.titleAt[1] }, esc(p.label) + (p.sub ? tag('tspan', { class: 'pls', dx: 10 }, esc(p.sub)) : '')) : '';
|
|
37
|
+
panels += tag('g', { class: `panel k-${p.role ?? 'grey'}`, 'data-id': p.id },
|
|
38
|
+
tag('rect', { class: 'face', x: f(r.l), y: f(r.t), width: f(r.r - r.l), height: f(r.b - r.t), rx: o.panelRx }) + title);
|
|
39
|
+
}
|
|
40
|
+
for (const l of L.links) {
|
|
41
|
+
if (!l.points.length) continue;
|
|
42
|
+
const kind = kindOf(l.kind);
|
|
43
|
+
conns += tag('path', { class: `conn k-${kind}`, d: 'M' + l.points.map(p => `${f(p[0])},${f(p[1])}`).join(' L'), 'data-id': l.id, 'data-a': l.a, 'data-b': l.b, 'marker-start': mark(kind, 'start'), 'marker-end': mark(kind, 'end') });
|
|
44
|
+
if (l.at) labels += tag('text', { class: `cl k-${kind}`, x: l.at.x, y: l.at.y, 'text-anchor': l.at.anchor === 'start' ? undefined : l.at.anchor, 'data-for': l.id }, esc(l.at.text));
|
|
45
|
+
}
|
|
46
|
+
for (const c of L.cards) {
|
|
47
|
+
const r = c.rect;
|
|
48
|
+
cards += tag('g', { class: `card k-${c.role ?? 'grey'}${c.ghost ? ' ghost' : ''}`, 'data-id': c.id, tabindex: 0, role: 'button', 'aria-label': c.sub ? `${c.label}: ${c.sub}` : c.label },
|
|
49
|
+
tag('rect', { class: 'face', x: f(r.l), y: f(r.t), width: o.W, height: o.H, rx: o.cardRx })
|
|
50
|
+
+ tag('text', { class: 'lt', x: f(r.l + 14), y: f(r.t + 24) }, esc(c.label))
|
|
51
|
+
+ (c.sub ? tag('text', { class: 'st', x: f(r.l + 14), y: f(r.t + 42) }, esc(c.sub)) : ''));
|
|
52
|
+
}
|
|
53
|
+
let defs = '';
|
|
54
|
+
for (const [kind, shapes] of used) for (const s of shapes) {
|
|
55
|
+
const m = MARKS[s as keyof typeof MARKS];
|
|
56
|
+
defs += tag('marker', { id: `${id}-mk-${kind}-${s}`, viewBox: '0 0 10 10', refX: m.refX, refY: 5, markerWidth: 5.4, markerHeight: 5.4, orient: 'auto-start-reverse' },
|
|
57
|
+
tag('path', { d: m.d, class: `mk k-${kind}` }));
|
|
58
|
+
}
|
|
59
|
+
return tag('svg', { xmlns: 'http://www.w3.org/2000/svg', class: 'flat', viewBox: `${f(b.l)} ${f(b.t)} ${f(b.r - b.l)} ${f(b.b - b.t)}`, role: 'img', 'aria-label': opts.title ?? 'drawing, flat view', 'data-view': '2d' },
|
|
60
|
+
(defs ? tag('defs', {}, defs) : '') + tag('g', { class: 'panels' }, panels) + tag('g', { class: 'conns' }, conns) + tag('g', { class: 'cards' }, cards) + tag('g', { class: 'labels' }, labels));
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** the 2D view's stylesheet: its fonts (the ones flat.ts's FLAT_FONTS measures), its faces in the theme's role colours,
|
|
64
|
+
* and each link kind's colour and dash. Unscoped: a page scopes it as it scopes the drawing's (view-css.ts). */
|
|
65
|
+
export function flatCss(styles: Record<string, FlatStyle> = {}): string {
|
|
66
|
+
// a theme token by name ("send"), or a colour as CSS writes it ("#2a64d6", "hsl(…)", "var(--x)")
|
|
67
|
+
const col = (c: string | undefined) => (!c ? 'var(--ink)' : c.startsWith('#') || c.includes('(') ? c : `var(--${c})`);
|
|
68
|
+
let css = `
|
|
69
|
+
.flat{font-family:inherit}
|
|
70
|
+
.flat .card{cursor:pointer;outline:none}
|
|
71
|
+
.flat .card .face{fill:hsl(var(--h) var(--s) var(--l-top));stroke:hsl(var(--h) var(--s) var(--l-edge) / .55);stroke-width:1}
|
|
72
|
+
.flat .card:hover .face,.flat .card:focus-visible .face{fill:hsl(var(--h) var(--s) calc(var(--l-top) + 6%))}
|
|
73
|
+
.flat .card.ghost{opacity:.4}
|
|
74
|
+
.flat .card .lt{font-size:13px;font-weight:600;fill:var(--ink)}
|
|
75
|
+
.flat .card .st{font-size:10px;fill:var(--ink);opacity:.8}
|
|
76
|
+
.flat .panel .face{fill:color-mix(in srgb,hsl(var(--h) var(--s) var(--l-top)) 22%,var(--surface));stroke:color-mix(in srgb,hsl(var(--h) var(--s) var(--l-top)) 40%,var(--line));stroke-width:1}
|
|
77
|
+
.flat .pl{font-size:12px;font-weight:700;letter-spacing:.02em;fill:var(--ink)}
|
|
78
|
+
.flat .pls{font-size:10.5px;font-weight:400;letter-spacing:0;fill:var(--muted)}
|
|
79
|
+
.flat .conn{fill:none;stroke:var(--ink);stroke-width:1.5}
|
|
80
|
+
.flat .mk{fill:var(--ink)}
|
|
81
|
+
.flat .cl{font-size:10.5px;font-weight:600;fill:var(--ink);paint-order:stroke;stroke:var(--surface);stroke-width:4px;stroke-linejoin:round}
|
|
82
|
+
.flat .card.sel .face{stroke:var(--accent,#2f6fd6);stroke-width:2.4}
|
|
83
|
+
.flat .panel.sel .face{stroke:var(--accent,#2f6fd6);stroke-width:2}
|
|
84
|
+
.flat .conn.hot{stroke-width:3}
|
|
85
|
+
.flat .conn.dim,.flat .cl.dim{opacity:.3}
|
|
86
|
+
`;
|
|
87
|
+
for (const [kind, st] of Object.entries(styles)) {
|
|
88
|
+
const k = kindOf(kind), c = col(st.color);
|
|
89
|
+
css += `.flat .conn.k-${k}{stroke:${c}${st.dash ? `;stroke-dasharray:${st.dash}` : ''}${st.width ? `;stroke-width:${st.width}` : ''}} .flat .mk.k-${k}{fill:${c}} .flat .cl.k-${k}{fill:${c}}\n`;
|
|
90
|
+
}
|
|
91
|
+
return css;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** what each measured font's text is drawn as (class, and the element it sits in) */
|
|
95
|
+
const FONT_CLASS: Record<FlatFont, [string, string]> = { name: ['card', 'lt'], sub: ['card', 'st'], title: ['panel', 'pl'], titleSub: ['panel', 'pls'], label: ['', 'cl'] };
|
|
96
|
+
|
|
97
|
+
/** a measure from a page's own rendered text: a hidden 2D svg in `host` (so the view's stylesheet applies), each text
|
|
98
|
+
* measured by getComputedTextLength, remembered */
|
|
99
|
+
export function measureIn(host: Element): Measure & { dispose(): void } {
|
|
100
|
+
const NS = 'http://www.w3.org/2000/svg';
|
|
101
|
+
const svg = document.createElementNS(NS, 'svg');
|
|
102
|
+
svg.setAttribute('class', 'flat');
|
|
103
|
+
svg.setAttribute('aria-hidden', 'true');
|
|
104
|
+
svg.style.cssText = 'position:absolute;left:-10000px;top:0;width:10px;height:10px;visibility:hidden';
|
|
105
|
+
host.appendChild(svg);
|
|
106
|
+
const els = {} as Record<FlatFont, SVGTextElement>;
|
|
107
|
+
for (const [font, [g, cls]] of Object.entries(FONT_CLASS) as [FlatFont, [string, string]][]) {
|
|
108
|
+
const wrap = document.createElementNS(NS, 'g');
|
|
109
|
+
if (g) wrap.setAttribute('class', g);
|
|
110
|
+
const t = document.createElementNS(NS, 'text');
|
|
111
|
+
t.setAttribute('class', cls);
|
|
112
|
+
wrap.appendChild(t); svg.appendChild(wrap);
|
|
113
|
+
els[font] = t;
|
|
114
|
+
}
|
|
115
|
+
const memo = new Map<string, number>();
|
|
116
|
+
const m = ((text: string, font: FlatFont) => {
|
|
117
|
+
const key = font + '\u0000' + text;
|
|
118
|
+
let w = memo.get(key);
|
|
119
|
+
if (w === undefined) { els[font].textContent = text; w = els[font].getComputedTextLength(); memo.set(key, w); }
|
|
120
|
+
return w;
|
|
121
|
+
}) as Measure & { dispose(): void };
|
|
122
|
+
m.dispose = () => svg.remove();
|
|
123
|
+
return m;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/** the rect of a card or panel in a layout */
|
|
127
|
+
export const rectOf = (L: FlatLayout, id: string): FlatRect | undefined => L.cards.find(c => c.id === id)?.rect ?? L.panels.find(p => p.id === id)?.rect;
|