@fragiola/flow-svelte 0.0.0-stage → 0.1.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.
Files changed (138) hide show
  1. package/CHANGELOG.md +17 -0
  2. package/LICENSE +21 -0
  3. package/README.md +20 -2
  4. package/dist/components/background.svelte +150 -0
  5. package/dist/components/background.svelte.d.ts +25 -0
  6. package/dist/components/connection-line.svelte +120 -0
  7. package/dist/components/connection-line.svelte.d.ts +25 -0
  8. package/dist/components/controls.svelte +54 -0
  9. package/dist/components/controls.svelte.d.ts +17 -0
  10. package/dist/components/edge-label.svelte +126 -0
  11. package/dist/components/edge-label.svelte.d.ts +25 -0
  12. package/dist/components/edge-path.svelte +116 -0
  13. package/dist/components/edge-path.svelte.d.ts +27 -0
  14. package/dist/components/edge-toolbar.svelte +137 -0
  15. package/dist/components/edge-toolbar.svelte.d.ts +39 -0
  16. package/dist/components/edge.svelte +90 -0
  17. package/dist/components/edge.svelte.d.ts +41 -0
  18. package/dist/components/edges.svelte +242 -0
  19. package/dist/components/edges.svelte.d.ts +49 -0
  20. package/dist/components/fit-view.svelte +84 -0
  21. package/dist/components/fit-view.svelte.d.ts +32 -0
  22. package/dist/components/handle.svelte +96 -0
  23. package/dist/components/handle.svelte.d.ts +17 -0
  24. package/dist/components/node-toolbar.svelte +153 -0
  25. package/dist/components/node-toolbar.svelte.d.ts +42 -0
  26. package/dist/components/node.svelte +73 -0
  27. package/dist/components/node.svelte.d.ts +42 -0
  28. package/dist/components/nodes-selection.svelte +102 -0
  29. package/dist/components/nodes-selection.svelte.d.ts +26 -0
  30. package/dist/components/nodes.svelte +108 -0
  31. package/dist/components/nodes.svelte.d.ts +45 -0
  32. package/dist/components/reconnect-anchor.svelte +119 -0
  33. package/dist/components/reconnect-anchor.svelte.d.ts +26 -0
  34. package/dist/components/root.svelte +698 -0
  35. package/dist/components/root.svelte.d.ts +106 -0
  36. package/dist/components/selection-box.svelte +91 -0
  37. package/dist/components/selection-box.svelte.d.ts +20 -0
  38. package/dist/components/toggle-interactive.svelte +98 -0
  39. package/dist/components/toggle-interactive.svelte.d.ts +25 -0
  40. package/dist/components/viewport.svelte +108 -0
  41. package/dist/components/viewport.svelte.d.ts +16 -0
  42. package/dist/components/zoom-in.svelte +65 -0
  43. package/dist/components/zoom-in.svelte.d.ts +18 -0
  44. package/dist/components/zoom-out.svelte +65 -0
  45. package/dist/components/zoom-out.svelte.d.ts +18 -0
  46. package/dist/connection/index.d.ts +1 -0
  47. package/dist/connection/index.js +3 -0
  48. package/dist/context.d.ts +71 -0
  49. package/dist/context.js +81 -0
  50. package/dist/drag/index.d.ts +1 -0
  51. package/dist/drag/index.js +3 -0
  52. package/dist/index.d.ts +45 -0
  53. package/dist/index.js +25 -0
  54. package/dist/keyboard/index.d.ts +1 -0
  55. package/dist/keyboard/index.js +3 -0
  56. package/dist/minimap/index.d.ts +4 -0
  57. package/dist/minimap/index.js +7 -0
  58. package/dist/minimap/mini-map-context.d.ts +7 -0
  59. package/dist/minimap/mini-map-context.js +13 -0
  60. package/dist/minimap/mini-map-mask.svelte +65 -0
  61. package/dist/minimap/mini-map-mask.svelte.d.ts +19 -0
  62. package/dist/minimap/mini-map-node.svelte +88 -0
  63. package/dist/minimap/mini-map-node.svelte.d.ts +17 -0
  64. package/dist/minimap/mini-map.svelte +188 -0
  65. package/dist/minimap/mini-map.svelte.d.ts +39 -0
  66. package/dist/parts.d.ts +21 -0
  67. package/dist/parts.js +24 -0
  68. package/dist/reconnection/index.d.ts +1 -0
  69. package/dist/reconnection/index.js +3 -0
  70. package/dist/resize/index.d.ts +3 -0
  71. package/dist/resize/index.js +5 -0
  72. package/dist/resize/node-resizer.svelte +134 -0
  73. package/dist/resize/node-resizer.svelte.d.ts +36 -0
  74. package/dist/resize/resize-handle.svelte +192 -0
  75. package/dist/resize/resize-handle.svelte.d.ts +44 -0
  76. package/dist/runes/create-flow-ref.svelte.d.ts +32 -0
  77. package/dist/runes/create-flow-ref.svelte.js +32 -0
  78. package/dist/runes/internal.svelte.d.ts +31 -0
  79. package/dist/runes/internal.svelte.js +63 -0
  80. package/dist/runes/use-connection.svelte.d.ts +8 -0
  81. package/dist/runes/use-connection.svelte.js +18 -0
  82. package/dist/runes/use-control-button.svelte.d.ts +16 -0
  83. package/dist/runes/use-control-button.svelte.js +41 -0
  84. package/dist/runes/use-edge.svelte.d.ts +48 -0
  85. package/dist/runes/use-edge.svelte.js +98 -0
  86. package/dist/runes/use-edges.svelte.d.ts +4 -0
  87. package/dist/runes/use-edges.svelte.js +7 -0
  88. package/dist/runes/use-flow-ref.svelte.d.ts +18 -0
  89. package/dist/runes/use-flow-ref.svelte.js +19 -0
  90. package/dist/runes/use-flow-store.svelte.d.ts +13 -0
  91. package/dist/runes/use-flow-store.svelte.js +18 -0
  92. package/dist/runes/use-flow.svelte.d.ts +22 -0
  93. package/dist/runes/use-flow.svelte.js +18 -0
  94. package/dist/runes/use-handle.svelte.d.ts +56 -0
  95. package/dist/runes/use-handle.svelte.js +94 -0
  96. package/dist/runes/use-internal-node.svelte.d.ts +10 -0
  97. package/dist/runes/use-internal-node.svelte.js +16 -0
  98. package/dist/runes/use-key-press.svelte.d.ts +16 -0
  99. package/dist/runes/use-key-press.svelte.js +45 -0
  100. package/dist/runes/use-node-connections.svelte.d.ts +18 -0
  101. package/dist/runes/use-node-connections.svelte.js +30 -0
  102. package/dist/runes/use-node-id.svelte.d.ts +5 -0
  103. package/dist/runes/use-node-id.svelte.js +8 -0
  104. package/dist/runes/use-node.svelte.d.ts +47 -0
  105. package/dist/runes/use-node.svelte.js +122 -0
  106. package/dist/runes/use-nodes-data.svelte.d.ts +10 -0
  107. package/dist/runes/use-nodes-data.svelte.js +20 -0
  108. package/dist/runes/use-nodes.svelte.d.ts +4 -0
  109. package/dist/runes/use-nodes.svelte.js +7 -0
  110. package/dist/runes/use-on-viewport-change.svelte.d.ts +17 -0
  111. package/dist/runes/use-on-viewport-change.svelte.js +28 -0
  112. package/dist/runes/use-selection.svelte.d.ts +12 -0
  113. package/dist/runes/use-selection.svelte.js +19 -0
  114. package/dist/runes/use-viewport.svelte.d.ts +7 -0
  115. package/dist/runes/use-viewport.svelte.js +10 -0
  116. package/dist/selection/index.d.ts +1 -0
  117. package/dist/selection/index.js +3 -0
  118. package/dist/subflows/index.d.ts +1 -0
  119. package/dist/subflows/index.js +3 -0
  120. package/dist/typed.d.ts +32 -0
  121. package/dist/typed.js +15 -0
  122. package/dist/utils/engine-facade.d.ts +11 -0
  123. package/dist/utils/engine-facade.js +64 -0
  124. package/dist/utils/flow-store.svelte.d.ts +39 -0
  125. package/dist/utils/flow-store.svelte.js +133 -0
  126. package/dist/utils/merge-props.d.ts +8 -0
  127. package/dist/utils/merge-props.js +33 -0
  128. package/dist/utils/plain.svelte.d.ts +12 -0
  129. package/dist/utils/plain.svelte.js +44 -0
  130. package/dist/utils/portal-content.svelte +8 -0
  131. package/dist/utils/portal-content.svelte.d.ts +7 -0
  132. package/dist/utils/portal.svelte +34 -0
  133. package/dist/utils/portal.svelte.d.ts +8 -0
  134. package/dist/utils/render.d.ts +93 -0
  135. package/dist/utils/render.js +87 -0
  136. package/dist/utils/shallow-equal.d.ts +5 -0
  137. package/dist/utils/shallow-equal.js +32 -0
  138. package/package.json +106 -3
@@ -0,0 +1,45 @@
1
+ import { watchKeyPress, } from "@fragiola/flow";
2
+ import { findFlowContext } from "../context.js";
3
+ import { read } from "./internal.svelte.js";
4
+ /**
5
+ * Whether a key, or a combo of keys, is held, as `current`, which notifies its readers when that
6
+ * changes. `keys` is a combo or several (any of them): key names joined by `+`, held at once
7
+ * (`"Shift"`, `"Meta+s"`, `["Meta+z", "Control+z"]`); a name is a `KeyboardEvent.key` or `.code`,
8
+ * without case (`watchKeyPress`). The keys are read on `target`, by default the window of the
9
+ * enclosing `Flow.Root`'s element (never the global one): false before the root is mounted, or
10
+ * outside a root without a `target`. Pass getters to follow props; it listens again only when
11
+ * the keys (by value) or the target change. Call it in a component's script.
12
+ */
13
+ export function useKeyPress(keys, options = {}) {
14
+ const context = findFlowContext();
15
+ const target = $derived.by(() => {
16
+ const given = read(options).target;
17
+ return given === undefined
18
+ ? (context?.rootElement?.ownerDocument.defaultView ?? null)
19
+ : given;
20
+ });
21
+ // an equal list of keys keeps the listener (and what it holds)
22
+ const combos = $derived.by(() => {
23
+ const given = read(keys);
24
+ return JSON.stringify(typeof given === "string" ? [given] : given);
25
+ });
26
+ let pressed = $state(false);
27
+ $effect(() => {
28
+ const on = target;
29
+ const list = JSON.parse(combos);
30
+ if (!on)
31
+ return;
32
+ const stop = watchKeyPress(on, list, (value) => {
33
+ pressed = value;
34
+ });
35
+ return () => {
36
+ stop();
37
+ pressed = false;
38
+ };
39
+ });
40
+ return {
41
+ get current() {
42
+ return pressed;
43
+ },
44
+ };
45
+ }
@@ -0,0 +1,18 @@
1
+ import { type HandleType, type NodeConnection } from "@fragiola/flow";
2
+ import { type FlowValue, type MaybeGetter } from "./internal.svelte.js";
3
+ export interface UseNodeConnectionsOptions {
4
+ /** the node; default the enclosing `Flow.Node`'s */
5
+ id?: string | undefined;
6
+ /** only the connections at this handle of it */
7
+ handleId?: string | undefined;
8
+ /** only those that start (`"source"`) or end (`"target"`) at it */
9
+ type?: HandleType | undefined;
10
+ }
11
+ /**
12
+ * The connections at a node (`node-connections-by`) as `current`: each edge that starts or ends
13
+ * at it, as its `edgeId`, `source`, `sourceHandle`, `target` and `targetHandle`, in the document's
14
+ * order. Pass the options, or a getter of them, to follow a prop. It notifies its readers only
15
+ * when they change (an edge added, removed or reconnected there), not when a node moves or an
16
+ * edge's data changes. Outside a `Flow.Node` it needs an `id` (it throws without one).
17
+ */
18
+ export declare function useNodeConnections(options?: MaybeGetter<UseNodeConnectionsOptions>): FlowValue<readonly NodeConnection[]>;
@@ -0,0 +1,30 @@
1
+ import { sameFlowValue, } from "@fragiola/flow";
2
+ import { findNodeContext, useFlowContext } from "../context.js";
3
+ import { flowValue, read, } from "./internal.svelte.js";
4
+ /**
5
+ * The connections at a node (`node-connections-by`) as `current`: each edge that starts or ends
6
+ * at it, as its `edgeId`, `source`, `sourceHandle`, `target` and `targetHandle`, in the document's
7
+ * order. Pass the options, or a getter of them, to follow a prop. It notifies its readers only
8
+ * when they change (an edge added, removed or reconnected there), not when a node moves or an
9
+ * edge's data changes. Outside a `Flow.Node` it needs an `id` (it throws without one).
10
+ */
11
+ export function useNodeConnections(options = {}) {
12
+ const context = useFlowContext("useNodeConnections");
13
+ const enclosing = findNodeContext();
14
+ const nodeIdOf = () => {
15
+ const nodeId = read(options).id ?? enclosing?.nodeId;
16
+ if (nodeId === undefined) {
17
+ throw new Error("useNodeConnections must be called inside Flow.Node (or be given an id)");
18
+ }
19
+ return nodeId;
20
+ };
21
+ nodeIdOf();
22
+ return flowValue(context, () => {
23
+ const { handleId, type } = read(options);
24
+ return context.model.get("node-connections-by", {
25
+ nodeId: nodeIdOf(),
26
+ handleId,
27
+ handleType: type,
28
+ });
29
+ }, sameFlowValue);
30
+ }
@@ -0,0 +1,5 @@
1
+ /**
2
+ * The id of the node the component renders inside (`Flow.Node`), or null outside one: a custom
3
+ * node's piece reads its own node without being given its id. Call it in a component's script.
4
+ */
5
+ export declare function useNodeId(): string | null;
@@ -0,0 +1,8 @@
1
+ import { findNodeContext } from "../context.js";
2
+ /**
3
+ * The id of the node the component renders inside (`Flow.Node`), or null outside one: a custom
4
+ * node's piece reads its own node without being given its id. Call it in a component's script.
5
+ */
6
+ export function useNodeId() {
7
+ return findNodeContext()?.nodeId ?? null;
8
+ }
@@ -0,0 +1,47 @@
1
+ import { type AnyTypes, type FlowNode, type FlowTypes, type PartAttributes } from "@fragiola/flow";
2
+ import { type Attachment } from "svelte/attachments";
3
+ import { type DataAttributes } from "../utils/render.js";
4
+ import { type MaybeGetter } from "./internal.svelte.js";
5
+ /** What a node shows: its state on screen. */
6
+ export interface NodeState<T extends FlowTypes = AnyTypes> {
7
+ readonly nodeId: string;
8
+ /** the node, as the model holds it */
9
+ readonly node: FlowNode<T>;
10
+ /** its top-left corner in absolute flow coordinates (its parents' positions added) */
11
+ readonly x: number;
12
+ readonly y: number;
13
+ /** `data-selected` (`selection()`) */
14
+ readonly selected: boolean;
15
+ /** `data-dragging` (`drag()`) */
16
+ readonly dragging: boolean;
17
+ /** `data-resizing` (`resize()`) */
18
+ readonly resizing: boolean;
19
+ /** its size is known (measured or given): `data-measured` */
20
+ readonly measured: boolean;
21
+ /** it takes DOM focus (`keyboard()`): `tabindex="0"` */
22
+ readonly focusable: boolean;
23
+ /** the attributes its element carries (`data-flow-part`, `data-flow-id`, the state) */
24
+ readonly attributes: PartAttributes;
25
+ }
26
+ /** What goes on a node's element (`{...props}`). */
27
+ export type NodeElementProps = {
28
+ tabindex: number | undefined;
29
+ /** absolute, at its position, with the document's size when given */
30
+ style: string;
31
+ /** the attachment that registers the element with the engine (measured) */
32
+ readonly [attachment: symbol]: Attachment<Element>;
33
+ } & DataAttributes;
34
+ export interface UseNodeResult<T extends FlowTypes = AnyTypes> {
35
+ readonly state: NodeState<T>;
36
+ /**
37
+ * what goes on the node's element (`{...props}`): its attributes, `tabindex`, its structural
38
+ * style and the attachment that registers it with the engine
39
+ */
40
+ readonly props: NodeElementProps;
41
+ }
42
+ /**
43
+ * A node's state and the props of its element, for a node rendered by hand (`Flow.Node` uses it).
44
+ * Pass the node, or a getter of it (`useNode(() => node)`) to follow a prop; spread `props` on the
45
+ * element (the engine resolves presses from its attributes).
46
+ */
47
+ export declare function useNode<T extends FlowTypes = AnyTypes>(node: MaybeGetter<FlowNode<T>>): UseNodeResult<T>;
@@ -0,0 +1,122 @@
1
+ import { flag, partAttributes, } from "@fragiola/flow";
2
+ import { createAttachmentKey } from "svelte/attachments";
3
+ import { useFlowContext } from "../context.js";
4
+ import { px, styleString } from "../utils/render.js";
5
+ import { flowValue, read, sameValue, } from "./internal.svelte.js";
6
+ /**
7
+ * A node's state and the props of its element, for a node rendered by hand (`Flow.Node` uses it).
8
+ * Pass the node, or a getter of it (`useNode(() => node)`) to follow a prop; spread `props` on the
9
+ * element (the engine resolves presses from its attributes).
10
+ */
11
+ export function useNode(node) {
12
+ const context = useFlowContext("useNode");
13
+ const current = () => read(node);
14
+ const view = flowValue(context, () => {
15
+ const value = current();
16
+ const nodeId = value.id;
17
+ const { engine, model, geometry } = context;
18
+ if (!engine) {
19
+ // before the engine: the static geometry's box (the node's origin applied) and z-index
20
+ const rect = geometry?.nodeRects[nodeId];
21
+ const at = rect
22
+ ? undefined
23
+ : model.get("absolute-position-by", { nodeId });
24
+ return {
25
+ attributes: {
26
+ ...partAttributes("node", nodeId),
27
+ "data-selected": flag(value.selected === true),
28
+ },
29
+ x: rect?.x ?? at?.x ?? value.position.x,
30
+ y: rect?.y ?? at?.y ?? value.position.y,
31
+ zIndex: geometry?.nodeZIndexes[nodeId],
32
+ focusable: false,
33
+ };
34
+ }
35
+ const rect = engine.get("node-rect-by", { nodeId });
36
+ return {
37
+ attributes: engine.adapter.attributes("node", { nodeId }),
38
+ x: rect?.x ?? value.position.x,
39
+ y: rect?.y ?? value.position.y,
40
+ zIndex: engine.get("node-z-index-by", { nodeId }),
41
+ focusable: engine.is("node-focusable", { nodeId }),
42
+ };
43
+ }, sameValue, { entity: () => ({ kind: "node", id: current().id }) });
44
+ // registered with the engine once both are there, again for another engine or node id only:
45
+ // the id is derived, so that a new node object (each drag step) does not register it again
46
+ const nodeId = $derived(current().id);
47
+ const key = createAttachmentKey();
48
+ const attachment = (element) => {
49
+ const id = nodeId;
50
+ const engine = context.engine;
51
+ if (!engine)
52
+ return;
53
+ // at once, as the element is in place: a node the app adds is registered before its
54
+ // `await tick()` and before its content's effects. One that throws (its measurement)
55
+ // leaves the flush and the other nodes alone: the error is reported, not thrown into
56
+ // the render
57
+ try {
58
+ engine.adapter.registerNode(id, element);
59
+ }
60
+ catch (error) {
61
+ report(error);
62
+ }
63
+ return () => engine.adapter.registerNode(id, null);
64
+ };
65
+ const state = $derived.by(() => {
66
+ const { attributes, x, y, focusable } = view.current;
67
+ const value = current();
68
+ return {
69
+ nodeId: value.id,
70
+ node: value,
71
+ x,
72
+ y,
73
+ selected: attributes["data-selected"] !== undefined,
74
+ dragging: attributes["data-dragging"] !== undefined,
75
+ resizing: attributes["data-resizing"] !== undefined,
76
+ measured: attributes["data-measured"] !== undefined,
77
+ focusable,
78
+ attributes,
79
+ };
80
+ });
81
+ const props = $derived.by(() => {
82
+ const { attributes, x, y, zIndex, focusable } = view.current;
83
+ const value = current();
84
+ return {
85
+ ...attributes,
86
+ tabindex: focusable ? 0 : undefined,
87
+ style: styleString(nodeStyle(x, y, zIndex, value)) ?? "",
88
+ [key]: attachment,
89
+ };
90
+ });
91
+ return {
92
+ get state() {
93
+ return state;
94
+ },
95
+ get props() {
96
+ return props;
97
+ },
98
+ };
99
+ }
100
+ /** A node's structural style: absolute, at its position, its `z-index`, with the document's size. */
101
+ function nodeStyle(x, y, zIndex, node) {
102
+ return {
103
+ position: "absolute",
104
+ top: "0",
105
+ left: "0",
106
+ transform: `translate(${px(x)}, ${px(y)})`,
107
+ ...(zIndex === undefined ? {} : { "z-index": String(zIndex) }),
108
+ ...(node.width === undefined ? {} : { width: px(node.width) }),
109
+ ...(node.height === undefined ? {} : { height: px(node.height) }),
110
+ };
111
+ }
112
+ /**
113
+ * An error the render must not stop for, reported as an uncaught one (the window's `error` event,
114
+ * `reportError`), as a listener's would be.
115
+ */
116
+ function report(error) {
117
+ if (typeof globalThis.reportError === "function") {
118
+ globalThis.reportError(error);
119
+ return;
120
+ }
121
+ void Promise.reject(error);
122
+ }
@@ -0,0 +1,10 @@
1
+ import { type AnyTypes, type FlowTypes, type NodeData } from "@fragiola/flow";
2
+ import { type FlowValue, type MaybeGetter } from "./internal.svelte.js";
3
+ /**
4
+ * A node's id, type and data (`nodes-data-by`) as `current`, or null while it does not exist;
5
+ * given several ids, each existing one's in the order given. Pass the id, or a getter of it
6
+ * (`useNodesData(() => id)`) to follow a prop. It notifies its readers only when one of them
7
+ * changes (its type or its data, compared by value), not when a node moves or another changes.
8
+ */
9
+ export declare function useNodesData<T extends FlowTypes = AnyTypes>(nodeId: MaybeGetter<string>): FlowValue<NodeData<T> | null>;
10
+ export declare function useNodesData<T extends FlowTypes = AnyTypes>(nodeIds: MaybeGetter<readonly string[]>): FlowValue<readonly NodeData<T>[]>;
@@ -0,0 +1,20 @@
1
+ import { sameFlowValue, } from "@fragiola/flow";
2
+ import { useFlowContext } from "../context.js";
3
+ import { plain } from "../utils/plain.svelte.js";
4
+ import { flowValue, read, } from "./internal.svelte.js";
5
+ export function useNodesData(ids) {
6
+ const context = useFlowContext("useNodesData");
7
+ const list = flowValue(context, () => {
8
+ const given = read(ids);
9
+ return context.model.get("nodes-data-by", {
10
+ nodeIds: typeof given === "string" ? [given] : plain(given),
11
+ });
12
+ }, sameFlowValue);
13
+ return {
14
+ get current() {
15
+ return typeof read(ids) === "string"
16
+ ? (list.current[0] ?? null)
17
+ : list.current;
18
+ },
19
+ };
20
+ }
@@ -0,0 +1,4 @@
1
+ import type { AnyTypes, FlowNode, FlowTypes } from "@fragiola/flow";
2
+ import { type FlowValue } from "./internal.svelte.js";
3
+ /** Every node of the enclosing `Flow.Root`, in drawing order, as `current`, which follows every change of one. */
4
+ export declare function useNodes<T extends FlowTypes = AnyTypes>(): FlowValue<readonly FlowNode<T>[]>;
@@ -0,0 +1,7 @@
1
+ import { useFlowContext } from "../context.js";
2
+ import { flowValue } from "./internal.svelte.js";
3
+ /** Every node of the enclosing `Flow.Root`, in drawing order, as `current`, which follows every change of one. */
4
+ export function useNodes() {
5
+ const context = useFlowContext("useNodes");
6
+ return flowValue(context, () => context.model.get("all-nodes"), Object.is);
7
+ }
@@ -0,0 +1,17 @@
1
+ import type { Viewport } from "@fragiola/flow";
2
+ import { type MaybeGetter } from "./internal.svelte.js";
3
+ export interface UseOnViewportChangeOptions {
4
+ /** the viewport starts changing (`viewport-change-start`): a pan, a zoom, an animation */
5
+ onstart?: ((viewport: Viewport, gesture: boolean) => void) | undefined;
6
+ /** each change of the viewport (`viewport-change`) */
7
+ onchange?: ((viewport: Viewport, gesture: boolean) => void) | undefined;
8
+ /** the viewport stopped changing (`viewport-change-end`) */
9
+ onend?: ((viewport: Viewport, gesture: boolean) => void) | undefined;
10
+ }
11
+ /**
12
+ * Calls back on the viewport's changes of the enclosing `Flow.Root` (the engine's events), without
13
+ * updating anything: `gesture` is true for a pan, the wheel, a pinch or auto-pan, false for an
14
+ * action. Pass the callbacks, or a getter of them (props): they are read at each event, so the
15
+ * latest are called. Call it in a component's script, inside the root.
16
+ */
17
+ export declare function useOnViewportChange(options: MaybeGetter<UseOnViewportChangeOptions>): void;
@@ -0,0 +1,28 @@
1
+ import { useFlowContext } from "../context.js";
2
+ import { read } from "./internal.svelte.js";
3
+ /**
4
+ * Calls back on the viewport's changes of the enclosing `Flow.Root` (the engine's events), without
5
+ * updating anything: `gesture` is true for a pan, the wheel, a pinch or auto-pan, false for an
6
+ * action. Pass the callbacks, or a getter of them (props): they are read at each event, so the
7
+ * latest are called. Call it in a component's script, inside the root.
8
+ */
9
+ export function useOnViewportChange(options) {
10
+ const context = useFlowContext("useOnViewportChange");
11
+ $effect(() => {
12
+ const engine = context.engine;
13
+ if (!engine)
14
+ return;
15
+ return engine.subscribe((event) => {
16
+ const { onstart, onchange, onend } = read(options);
17
+ if (event.type === "viewport-change-start") {
18
+ onstart?.(event.viewport, event.gesture);
19
+ }
20
+ else if (event.type === "viewport-change") {
21
+ onchange?.(event.viewport, event.gesture);
22
+ }
23
+ else if (event.type === "viewport-change-end") {
24
+ onend?.(event.viewport, event.gesture);
25
+ }
26
+ });
27
+ });
28
+ }
@@ -0,0 +1,12 @@
1
+ import type { AnyTypes, FlowEdge, FlowNode, FlowTypes } from "@fragiola/flow";
2
+ import { type FlowValue } from "./internal.svelte.js";
3
+ /** What is selected. */
4
+ export interface FlowSelection<T extends FlowTypes = AnyTypes> {
5
+ readonly nodes: readonly FlowNode<T>[];
6
+ readonly edges: readonly FlowEdge<T>[];
7
+ }
8
+ /**
9
+ * The selected nodes and edges of the enclosing `Flow.Root` (`selection()`; none without it) as
10
+ * `current`, which follows them: a panel showing the selection, a Delete button.
11
+ */
12
+ export declare function useSelection<T extends FlowTypes = AnyTypes>(): FlowValue<FlowSelection<T>>;
@@ -0,0 +1,19 @@
1
+ import { useFlowContext } from "../context.js";
2
+ import { flowValue } from "./internal.svelte.js";
3
+ const NONE = Object.freeze([]);
4
+ /**
5
+ * The selected nodes and edges of the enclosing `Flow.Root` (`selection()`; none without it) as
6
+ * `current`, which follows them: a panel showing the selection, a Delete button.
7
+ */
8
+ export function useSelection() {
9
+ const context = useFlowContext("useSelection");
10
+ return flowValue(context, () => ({
11
+ nodes: (context.model.get("selected-nodes") ??
12
+ NONE),
13
+ edges: (context.model.get("selected-edges") ??
14
+ NONE),
15
+ }), (a, b) => a.nodes.length === b.nodes.length &&
16
+ a.edges.length === b.edges.length &&
17
+ a.nodes.every((node, i) => node === b.nodes[i]) &&
18
+ a.edges.every((edge, i) => edge === b.edges[i]));
19
+ }
@@ -0,0 +1,7 @@
1
+ import type { Viewport } from "@fragiola/flow";
2
+ import { type FlowValue } from "./internal.svelte.js";
3
+ /**
4
+ * The viewport of the enclosing `Flow.Root` (`{ x, y, zoom }`) as `current`, which follows it: a
5
+ * zoom level readout, a control's state. Change it with `engine.run("set-viewport")`.
6
+ */
7
+ export declare function useViewport(): FlowValue<Viewport>;
@@ -0,0 +1,10 @@
1
+ import { useFlowContext } from "../context.js";
2
+ import { flowValue } from "./internal.svelte.js";
3
+ /**
4
+ * The viewport of the enclosing `Flow.Root` (`{ x, y, zoom }`) as `current`, which follows it: a
5
+ * zoom level readout, a control's state. Change it with `engine.run("set-viewport")`.
6
+ */
7
+ export function useViewport() {
8
+ const context = useFlowContext("useViewport");
9
+ return flowValue(context, () => context.publicEngine.get("viewport"));
10
+ }
@@ -0,0 +1 @@
1
+ export * from "@fragiola/flow/selection";
@@ -0,0 +1,3 @@
1
+ // The entry of @fragiola/flow-svelte/selection: the core's selection feature (`selection()`), so that a Svelte
2
+ // app installs and imports one package. A flow that never imports it does not ship it.
3
+ export * from "@fragiola/flow/selection";
@@ -0,0 +1 @@
1
+ export * from "@fragiola/flow/subflows";
@@ -0,0 +1,3 @@
1
+ // The entry of @fragiola/flow-svelte/subflows: the core's subflows feature (`subflows()`), so that a Svelte
2
+ // app installs and imports one package. A flow that never imports it does not ship it.
3
+ export * from "@fragiola/flow/subflows";
@@ -0,0 +1,32 @@
1
+ import type { AnyTypes, FlowFeatures, FlowTypes } from "@fragiola/flow";
2
+ import type { Component } from "svelte";
3
+ import type { EdgeProps } from "./components/edge.svelte";
4
+ import type { EdgesProps } from "./components/edges.svelte";
5
+ import type { NodeProps } from "./components/node.svelte";
6
+ import type { NodesProps } from "./components/nodes.svelte";
7
+ import type { RootProps } from "./components/root.svelte";
8
+ import * as parts from "./parts.js";
9
+ /**
10
+ * The `Flow` primitives typed by the registry `T` and the features `F`: the root's model, lists,
11
+ * features and callbacks, and the nodes and edges the snippets give and the parts take. The others
12
+ * are the same as `Flow`'s.
13
+ */
14
+ export type TypedFlow<T extends FlowTypes = AnyTypes, F extends FlowFeatures = readonly []> = Omit<typeof parts, "Root" | "Nodes" | "Node" | "Edges" | "Edge"> & {
15
+ Root: Component<RootProps<T, F>, Record<string, never>, "nodes" | "edges" | "viewport" | "ref">;
16
+ Nodes: Component<NodesProps<T>, Record<string, never>, "ref">;
17
+ Node: Component<NodeProps<T>, Record<string, never>, "ref">;
18
+ Edges: Component<EdgesProps<T>, Record<string, never>, "ref">;
19
+ Edge: Component<EdgeProps<T>, Record<string, never>, "ref">;
20
+ };
21
+ /**
22
+ * The `Flow` primitives typed by your registry and features: what React writes as type arguments
23
+ * (`<Flow.Nodes<Types>>`), which a Svelte template cannot pass. The same components, typed:
24
+ *
25
+ * ```svelte
26
+ * <script lang="ts">
27
+ * const Flow = typedFlow<Types, typeof features>();
28
+ * </script>
29
+ * <!-- {#snippet children(node)}: `node.data` is the registry's -->
30
+ * ```
31
+ */
32
+ export declare function typedFlow<T extends FlowTypes = AnyTypes, F extends FlowFeatures = readonly []>(): TypedFlow<T, F>;
package/dist/typed.js ADDED
@@ -0,0 +1,15 @@
1
+ import * as parts from "./parts.js";
2
+ /**
3
+ * The `Flow` primitives typed by your registry and features: what React writes as type arguments
4
+ * (`<Flow.Nodes<Types>>`), which a Svelte template cannot pass. The same components, typed:
5
+ *
6
+ * ```svelte
7
+ * <script lang="ts">
8
+ * const Flow = typedFlow<Types, typeof features>();
9
+ * </script>
10
+ * <!-- {#snippet children(node)}: `node.data` is the registry's -->
11
+ * ```
12
+ */
13
+ export function typedFlow() {
14
+ return parts;
15
+ }
@@ -0,0 +1,11 @@
1
+ import type { FlowEngine, Viewport } from "@fragiola/flow";
2
+ /** The facade, and how the root points it at its current engine. */
3
+ export interface EngineFacade {
4
+ readonly engine: FlowEngine;
5
+ /** the root's current engine (null: not mounted) */
6
+ readonly current: FlowEngine | null;
7
+ /** points the facade at `engine` (null: the root unmounted) */
8
+ set(engine: FlowEngine | null): void;
9
+ }
10
+ /** A facade whose reads before the mount answer `viewport` with `initial`. */
11
+ export declare function createEngineFacade(initial: Viewport): EngineFacade;
@@ -0,0 +1,64 @@
1
+ const NOT_MOUNTED = Object.freeze({
2
+ ok: false,
3
+ error: Object.freeze({
4
+ code: "refused",
5
+ message: "the flow is not mounted",
6
+ }),
7
+ });
8
+ /** A facade whose reads before the mount answer `viewport` with `initial`. */
9
+ export function createEngineFacade(initial) {
10
+ let current = null;
11
+ const listeners = new Set();
12
+ let unsubscribe = () => { };
13
+ const empty = {
14
+ viewport: Object.freeze({ ...initial }),
15
+ "root-size": Object.freeze({ width: 0, height: 0 }),
16
+ "handles-by": Object.freeze([]),
17
+ "dragging-node-ids": Object.freeze([]),
18
+ "selection-box": null,
19
+ connection: null,
20
+ };
21
+ const forward = (verb, fallback) => (key, payload) => current
22
+ ? current[verb](key, payload)
23
+ : fallback(key, payload);
24
+ const engine = Object.freeze({
25
+ run: forward("run", () => NOT_MOUNTED),
26
+ can: forward("can", () => false),
27
+ check: forward("check", () => NOT_MOUNTED),
28
+ get: forward("get", (key) => Object.hasOwn(empty, key) ? empty[key] : undefined),
29
+ is: forward("is", () => false),
30
+ // listens across engines: a listener added before the mount hears the mounted engine
31
+ subscribe(listener) {
32
+ const wrapper = (event) => listener(event);
33
+ listeners.add(wrapper);
34
+ return () => {
35
+ listeners.delete(wrapper);
36
+ };
37
+ },
38
+ // the root owns its engine and destroys it when it unmounts
39
+ destroy() { },
40
+ get adapter() {
41
+ if (!current)
42
+ throw new Error("the flow is not mounted");
43
+ return current.adapter;
44
+ },
45
+ });
46
+ return {
47
+ engine,
48
+ get current() {
49
+ return current;
50
+ },
51
+ set(next) {
52
+ if (next === current)
53
+ return;
54
+ unsubscribe();
55
+ current = next;
56
+ unsubscribe = next
57
+ ? next.subscribe((event) => {
58
+ for (const listener of [...listeners])
59
+ listener(event);
60
+ })
61
+ : () => { };
62
+ },
63
+ };
64
+ }
@@ -0,0 +1,39 @@
1
+ import type { AnyFlowEngine, AnyFlowModel } from "../context.js";
2
+ /**
3
+ * @internal what a reader depends on:
4
+ * - `view`: anything, the viewport included (the layers' lists, the background, toolbars);
5
+ * - `entity`: one node's or edge's own view (its place, path, attributes, `z-index`), which a
6
+ * viewport step leaves alone, so a pan or a zoom never reads it again. A viewport step does
7
+ * change the nodes' `z-index` while there are several render levels (`node-z-index-by`, the
8
+ * levels follow the drawn edges): then it counts as an entity change too.
9
+ */
10
+ export type FlowValueScope = "view" | "entity";
11
+ /**
12
+ * @internal the node or the edge an `entity` reader shows: a change that is only a measurement
13
+ * (nodes mounted, a node resized) tells it only when the measurement changed it (its node's box or
14
+ * measured state, its edge's path), not every node and edge on screen
15
+ */
16
+ export interface FlowEntity {
17
+ readonly kind: "node" | "edge";
18
+ readonly id: string;
19
+ }
20
+ /** @internal the counters of one root, read by its `flowValue` deriveds */
21
+ export interface FlowStore {
22
+ /** every change: a commit of the model, every engine notification (the context's `revision`) */
23
+ readonly view: number;
24
+ /**
25
+ * reads the counters a reader of `scope` depends on (in a derived): `view`, or `entity` and,
26
+ * for a reader of one node or edge, its measurements (every measurement without a record)
27
+ */
28
+ track(scope: FlowValueScope, entity?: FlowEntity): void;
29
+ /** a reader of a node or an edge: its record is kept until released */
30
+ acquire(entity: FlowEntity): () => void;
31
+ /** a commit of the model */
32
+ commit(): void;
33
+ /** the engine, once created: its notifications are told from then on; returns the remover */
34
+ attach(engine: AnyFlowEngine): () => void;
35
+ /** @internal the records kept (tests) */
36
+ readonly records: number;
37
+ }
38
+ /** @internal the store of a root (its model a getter: another `model` prop is followed) */
39
+ export declare function createFlowStore(model: () => AnyFlowModel): FlowStore;