@fragiola/flow-svelte 0.0.0-stage → 0.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.
Files changed (137) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +13 -2
  3. package/dist/components/background.svelte +150 -0
  4. package/dist/components/background.svelte.d.ts +25 -0
  5. package/dist/components/connection-line.svelte +120 -0
  6. package/dist/components/connection-line.svelte.d.ts +25 -0
  7. package/dist/components/controls.svelte +54 -0
  8. package/dist/components/controls.svelte.d.ts +17 -0
  9. package/dist/components/edge-label.svelte +126 -0
  10. package/dist/components/edge-label.svelte.d.ts +25 -0
  11. package/dist/components/edge-path.svelte +116 -0
  12. package/dist/components/edge-path.svelte.d.ts +27 -0
  13. package/dist/components/edge-toolbar.svelte +137 -0
  14. package/dist/components/edge-toolbar.svelte.d.ts +39 -0
  15. package/dist/components/edge.svelte +90 -0
  16. package/dist/components/edge.svelte.d.ts +41 -0
  17. package/dist/components/edges.svelte +242 -0
  18. package/dist/components/edges.svelte.d.ts +49 -0
  19. package/dist/components/fit-view.svelte +84 -0
  20. package/dist/components/fit-view.svelte.d.ts +32 -0
  21. package/dist/components/handle.svelte +96 -0
  22. package/dist/components/handle.svelte.d.ts +17 -0
  23. package/dist/components/node-toolbar.svelte +153 -0
  24. package/dist/components/node-toolbar.svelte.d.ts +42 -0
  25. package/dist/components/node.svelte +73 -0
  26. package/dist/components/node.svelte.d.ts +42 -0
  27. package/dist/components/nodes-selection.svelte +102 -0
  28. package/dist/components/nodes-selection.svelte.d.ts +26 -0
  29. package/dist/components/nodes.svelte +108 -0
  30. package/dist/components/nodes.svelte.d.ts +45 -0
  31. package/dist/components/reconnect-anchor.svelte +119 -0
  32. package/dist/components/reconnect-anchor.svelte.d.ts +26 -0
  33. package/dist/components/root.svelte +698 -0
  34. package/dist/components/root.svelte.d.ts +106 -0
  35. package/dist/components/selection-box.svelte +91 -0
  36. package/dist/components/selection-box.svelte.d.ts +20 -0
  37. package/dist/components/toggle-interactive.svelte +98 -0
  38. package/dist/components/toggle-interactive.svelte.d.ts +25 -0
  39. package/dist/components/viewport.svelte +108 -0
  40. package/dist/components/viewport.svelte.d.ts +16 -0
  41. package/dist/components/zoom-in.svelte +65 -0
  42. package/dist/components/zoom-in.svelte.d.ts +18 -0
  43. package/dist/components/zoom-out.svelte +65 -0
  44. package/dist/components/zoom-out.svelte.d.ts +18 -0
  45. package/dist/connection/index.d.ts +1 -0
  46. package/dist/connection/index.js +3 -0
  47. package/dist/context.d.ts +71 -0
  48. package/dist/context.js +81 -0
  49. package/dist/drag/index.d.ts +1 -0
  50. package/dist/drag/index.js +3 -0
  51. package/dist/index.d.ts +45 -0
  52. package/dist/index.js +25 -0
  53. package/dist/keyboard/index.d.ts +1 -0
  54. package/dist/keyboard/index.js +3 -0
  55. package/dist/minimap/index.d.ts +4 -0
  56. package/dist/minimap/index.js +7 -0
  57. package/dist/minimap/mini-map-context.d.ts +7 -0
  58. package/dist/minimap/mini-map-context.js +13 -0
  59. package/dist/minimap/mini-map-mask.svelte +65 -0
  60. package/dist/minimap/mini-map-mask.svelte.d.ts +19 -0
  61. package/dist/minimap/mini-map-node.svelte +88 -0
  62. package/dist/minimap/mini-map-node.svelte.d.ts +17 -0
  63. package/dist/minimap/mini-map.svelte +188 -0
  64. package/dist/minimap/mini-map.svelte.d.ts +39 -0
  65. package/dist/parts.d.ts +21 -0
  66. package/dist/parts.js +24 -0
  67. package/dist/reconnection/index.d.ts +1 -0
  68. package/dist/reconnection/index.js +3 -0
  69. package/dist/resize/index.d.ts +3 -0
  70. package/dist/resize/index.js +5 -0
  71. package/dist/resize/node-resizer.svelte +134 -0
  72. package/dist/resize/node-resizer.svelte.d.ts +36 -0
  73. package/dist/resize/resize-handle.svelte +192 -0
  74. package/dist/resize/resize-handle.svelte.d.ts +44 -0
  75. package/dist/runes/create-flow-ref.svelte.d.ts +32 -0
  76. package/dist/runes/create-flow-ref.svelte.js +32 -0
  77. package/dist/runes/internal.svelte.d.ts +31 -0
  78. package/dist/runes/internal.svelte.js +63 -0
  79. package/dist/runes/use-connection.svelte.d.ts +8 -0
  80. package/dist/runes/use-connection.svelte.js +18 -0
  81. package/dist/runes/use-control-button.svelte.d.ts +16 -0
  82. package/dist/runes/use-control-button.svelte.js +41 -0
  83. package/dist/runes/use-edge.svelte.d.ts +48 -0
  84. package/dist/runes/use-edge.svelte.js +98 -0
  85. package/dist/runes/use-edges.svelte.d.ts +4 -0
  86. package/dist/runes/use-edges.svelte.js +7 -0
  87. package/dist/runes/use-flow-ref.svelte.d.ts +18 -0
  88. package/dist/runes/use-flow-ref.svelte.js +19 -0
  89. package/dist/runes/use-flow-store.svelte.d.ts +13 -0
  90. package/dist/runes/use-flow-store.svelte.js +18 -0
  91. package/dist/runes/use-flow.svelte.d.ts +22 -0
  92. package/dist/runes/use-flow.svelte.js +18 -0
  93. package/dist/runes/use-handle.svelte.d.ts +56 -0
  94. package/dist/runes/use-handle.svelte.js +94 -0
  95. package/dist/runes/use-internal-node.svelte.d.ts +10 -0
  96. package/dist/runes/use-internal-node.svelte.js +16 -0
  97. package/dist/runes/use-key-press.svelte.d.ts +16 -0
  98. package/dist/runes/use-key-press.svelte.js +45 -0
  99. package/dist/runes/use-node-connections.svelte.d.ts +18 -0
  100. package/dist/runes/use-node-connections.svelte.js +30 -0
  101. package/dist/runes/use-node-id.svelte.d.ts +5 -0
  102. package/dist/runes/use-node-id.svelte.js +8 -0
  103. package/dist/runes/use-node.svelte.d.ts +47 -0
  104. package/dist/runes/use-node.svelte.js +122 -0
  105. package/dist/runes/use-nodes-data.svelte.d.ts +10 -0
  106. package/dist/runes/use-nodes-data.svelte.js +20 -0
  107. package/dist/runes/use-nodes.svelte.d.ts +4 -0
  108. package/dist/runes/use-nodes.svelte.js +7 -0
  109. package/dist/runes/use-on-viewport-change.svelte.d.ts +17 -0
  110. package/dist/runes/use-on-viewport-change.svelte.js +28 -0
  111. package/dist/runes/use-selection.svelte.d.ts +12 -0
  112. package/dist/runes/use-selection.svelte.js +19 -0
  113. package/dist/runes/use-viewport.svelte.d.ts +7 -0
  114. package/dist/runes/use-viewport.svelte.js +10 -0
  115. package/dist/selection/index.d.ts +1 -0
  116. package/dist/selection/index.js +3 -0
  117. package/dist/subflows/index.d.ts +1 -0
  118. package/dist/subflows/index.js +3 -0
  119. package/dist/typed.d.ts +32 -0
  120. package/dist/typed.js +15 -0
  121. package/dist/utils/engine-facade.d.ts +11 -0
  122. package/dist/utils/engine-facade.js +64 -0
  123. package/dist/utils/flow-store.svelte.d.ts +39 -0
  124. package/dist/utils/flow-store.svelte.js +133 -0
  125. package/dist/utils/merge-props.d.ts +8 -0
  126. package/dist/utils/merge-props.js +33 -0
  127. package/dist/utils/plain.svelte.d.ts +12 -0
  128. package/dist/utils/plain.svelte.js +44 -0
  129. package/dist/utils/portal-content.svelte +8 -0
  130. package/dist/utils/portal-content.svelte.d.ts +7 -0
  131. package/dist/utils/portal.svelte +34 -0
  132. package/dist/utils/portal.svelte.d.ts +8 -0
  133. package/dist/utils/render.d.ts +93 -0
  134. package/dist/utils/render.js +87 -0
  135. package/dist/utils/shallow-equal.d.ts +5 -0
  136. package/dist/utils/shallow-equal.js +32 -0
  137. package/package.json +105 -3
@@ -0,0 +1,63 @@
1
+ // The reactive helpers only the primitives call.
2
+ import { untrack } from "svelte";
3
+ import { shallowEqual } from "../utils/shallow-equal.js";
4
+ /** @internal two values the same for re-rendering: records compared by their entries */
5
+ export function sameValue(a, b) {
6
+ if (Object.is(a, b))
7
+ return true;
8
+ if (typeof a === "object" &&
9
+ typeof b === "object" &&
10
+ a !== null &&
11
+ b !== null &&
12
+ !Array.isArray(a) &&
13
+ !Array.isArray(b)) {
14
+ return shallowEqual(a, b);
15
+ }
16
+ if (Array.isArray(a) && Array.isArray(b)) {
17
+ return (a.length === b.length &&
18
+ a.every((value, i) => Object.is(value, b[i])));
19
+ }
20
+ return false;
21
+ }
22
+ /**
23
+ * @internal a value read from the model and the engine (the React adapter's `useFlowValue`), as a
24
+ * `$derived` that notifies its readers only when it changes (`isEqual`; records by their entries
25
+ * by default). It is read again when the root's store tells its scope (`view`: every commit and
26
+ * view change; `entity`: not a viewport-only one, and a measurement only when it changed its node
27
+ * or edge), when the engine is created, and when a reactive value the reader uses changes (a
28
+ * prop); a value equal to the last one keeps the last object, so nothing re-renders. Called while
29
+ * a component initializes (an `entity` reader keeps its record in the store while mounted).
30
+ */
31
+ export function flowValue(context, read, isEqual = sameValue, options = {}) {
32
+ const scope = options.scope ?? (options.entity ? "entity" : "view");
33
+ const { entity } = options;
34
+ if (entity) {
35
+ // its record, kept while it is mounted (before the deriveds read it: a pre effect runs as
36
+ // the component initializes); another id acquires another one
37
+ $effect.pre(() => {
38
+ const of = entity();
39
+ const store = context.store;
40
+ return untrack(() => store.acquire({ kind: of.kind, id: of.id }));
41
+ });
42
+ }
43
+ let last;
44
+ const value = $derived.by(() => {
45
+ context.store.track(scope, entity?.());
46
+ context.engine;
47
+ const next = read();
48
+ if (last !== undefined && isEqual(last.value, next)) {
49
+ return last.value;
50
+ }
51
+ last = { value: next };
52
+ return next;
53
+ });
54
+ return {
55
+ get current() {
56
+ return value;
57
+ },
58
+ };
59
+ }
60
+ /** @internal the value behind a `MaybeGetter` */
61
+ export function read(value) {
62
+ return typeof value === "function" ? value() : value;
63
+ }
@@ -0,0 +1,8 @@
1
+ import type { Connection } from "@fragiola/flow";
2
+ import { type FlowValue } from "./internal.svelte.js";
3
+ /**
4
+ * The connection being drawn from a handle (`connection()`), or null, as `current`, which follows
5
+ * it: where it starts and ends, the handle it would end at and whether the drop would be accepted
6
+ * (`status`).
7
+ */
8
+ export declare function useConnection(): FlowValue<Connection | null>;
@@ -0,0 +1,18 @@
1
+ import { useFlowContext } from "../context.js";
2
+ import { flowValue } from "./internal.svelte.js";
3
+ /**
4
+ * The connection being drawn from a handle (`connection()`), or null, as `current`, which follows
5
+ * it: where it starts and ends, the handle it would end at and whether the drop would be accepted
6
+ * (`status`).
7
+ */
8
+ export function useConnection() {
9
+ const context = useFlowContext("useConnection");
10
+ return flowValue(context, () => context.engine?.get("connection") ?? null, (a, b) => a === b ||
11
+ (a !== null &&
12
+ b !== null &&
13
+ a.path === b.path &&
14
+ a.status === b.status &&
15
+ a.to === b.to &&
16
+ a.from === b.from &&
17
+ a.toSide === b.toSide));
18
+ }
@@ -0,0 +1,16 @@
1
+ import type { EngineActionKey } from "@fragiola/flow";
2
+ /** What a control button shows. */
3
+ export interface ControlButtonState {
4
+ /** the engine refuses its action now (a zoom at its limit, `fit-view` before a size) */
5
+ readonly disabled: boolean;
6
+ }
7
+ /** @internal a control button's state and own props: it runs `action` when clicked */
8
+ export declare function useControlButton(part: string, action: EngineActionKey, disabledProp: () => boolean | null | undefined, fields?: () => object): {
9
+ readonly state: ControlButtonState;
10
+ readonly props: Record<string, unknown>;
11
+ };
12
+ /**
13
+ * @internal a control's payload: the fields its props give (an absent one is the engine's
14
+ * default), without a `$state` proxy
15
+ */
16
+ export declare function givenPayload<P extends object>(fields: P): Partial<P>;
@@ -0,0 +1,41 @@
1
+ import { useFlowContext } from "../context.js";
2
+ import { plain } from "../utils/plain.svelte.js";
3
+ import { flowValue } from "./internal.svelte.js";
4
+ /** @internal a control button's state and own props: it runs `action` when clicked */
5
+ export function useControlButton(part, action, disabledProp, fields = () => ({})) {
6
+ const context = useFlowContext(part);
7
+ // the fields its props give (an absent one is the engine's default)
8
+ const payload = () => givenPayload(fields());
9
+ const allowed = flowValue(context, () => context.engine ? context.engine.can(action, payload()) : false);
10
+ const disabled = $derived(disabledProp() === true || !allowed.current);
11
+ const onclick = () => {
12
+ context.engine?.run(action, payload());
13
+ };
14
+ return {
15
+ get state() {
16
+ return { disabled };
17
+ },
18
+ get props() {
19
+ return {
20
+ type: "button",
21
+ "data-flow-part": partName(part),
22
+ "data-disabled": disabled ? "" : undefined,
23
+ disabled,
24
+ onclick,
25
+ };
26
+ },
27
+ };
28
+ }
29
+ /**
30
+ * @internal a control's payload: the fields its props give (an absent one is the engine's
31
+ * default), without a `$state` proxy
32
+ */
33
+ export function givenPayload(fields) {
34
+ return Object.fromEntries(Object.entries(fields)
35
+ .filter(([, value]) => value !== undefined)
36
+ .map(([name, value]) => [name, plain(value)]));
37
+ }
38
+ /** `ZoomIn` → `zoom-in`. */
39
+ function partName(part) {
40
+ return part.replace(/[A-Z]/g, (letter, at) => `${at === 0 ? "" : "-"}${letter.toLowerCase()}`);
41
+ }
@@ -0,0 +1,48 @@
1
+ import { type AnyTypes, type EdgePath, type FlowEdge, type FlowTypes, type PartAttributes } from "@fragiola/flow";
2
+ import type { FlowContextValue } from "../context.js";
3
+ import type { DataAttributes } from "../utils/render.js";
4
+ import { type FlowValue, type MaybeGetter } from "./internal.svelte.js";
5
+ /** What an edge shows: its state on screen. */
6
+ export interface EdgeState<T extends FlowTypes = AnyTypes> {
7
+ readonly edgeId: string;
8
+ /** the edge, as the model holds it */
9
+ readonly edge: FlowEdge<T>;
10
+ /**
11
+ * its path, label point and ends (`edge-path-by`), once both its nodes have a size; an app
12
+ * type gets the bezier path, and its ends to draw its own
13
+ */
14
+ readonly path: EdgePath | undefined;
15
+ /** `data-selected` (`selection()`) */
16
+ readonly selected: boolean;
17
+ /** one of its ends is being moved (`reconnection()`) */
18
+ readonly reconnecting: boolean;
19
+ /** it takes DOM focus (`keyboard()`): `tabindex="0"` */
20
+ readonly focusable: boolean;
21
+ readonly attributes: PartAttributes;
22
+ }
23
+ /** What goes on an edge's element (an SVG group, `{...props}`). */
24
+ export type EdgeElementProps = {
25
+ tabindex: number | undefined;
26
+ } & DataAttributes;
27
+ export interface UseEdgeResult<T extends FlowTypes = AnyTypes> {
28
+ readonly state: EdgeState<T>;
29
+ /** what goes on the edge's element (an SVG group): its attributes and `tabindex` */
30
+ readonly props: EdgeElementProps;
31
+ }
32
+ /** Two edge paths that draw the same (`edge-path-by` builds a new one at every read). */
33
+ export declare function sameEdgePath(a: EdgePath | undefined, b: EdgePath | undefined): boolean;
34
+ /**
35
+ * An edge's state and the props of its element, for an edge drawn by hand (`Flow.Edge` uses it).
36
+ * Pass the edge, or a getter of it (`useEdge(() => edge)`) to follow a prop.
37
+ */
38
+ export declare function useEdge<T extends FlowTypes = AnyTypes>(edge: MaybeGetter<FlowEdge<T>>): UseEdgeResult<T>;
39
+ /**
40
+ * @internal an edge's path, notifying its readers when it changes (`Flow.EdgePath`, …): the
41
+ * engine's, or the static geometry's before the engine
42
+ */
43
+ export declare function edgePathValue(context: FlowContextValue, edgeId: () => string): FlowValue<EdgePath | undefined>;
44
+ /**
45
+ * @internal the attributes of an edge part before the engine (as a node's): its part, its id,
46
+ * `data-selected` and `data-animated` from the document
47
+ */
48
+ export declare function staticAttributes(part: "edge" | "edge-path" | "edge-label", edgeId: string, edge: FlowEdge | undefined): PartAttributes;
@@ -0,0 +1,98 @@
1
+ import { flag, partAttributes, } from "@fragiola/flow";
2
+ import { useFlowContext } from "../context.js";
3
+ import { shallowEqual } from "../utils/shallow-equal.js";
4
+ import { flowValue, read, } from "./internal.svelte.js";
5
+ /** Two edge paths that draw the same (`edge-path-by` builds a new one at every read). */
6
+ export function sameEdgePath(a, b) {
7
+ if (a === b)
8
+ return true;
9
+ if (!a || !b)
10
+ return false;
11
+ return (a.path === b.path &&
12
+ a.sourceSide === b.sourceSide &&
13
+ a.targetSide === b.targetSide &&
14
+ a.label.x === b.label.x &&
15
+ a.label.y === b.label.y &&
16
+ a.source.x === b.source.x &&
17
+ a.source.y === b.source.y &&
18
+ a.target.x === b.target.x &&
19
+ a.target.y === b.target.y);
20
+ }
21
+ /**
22
+ * An edge's state and the props of its element, for an edge drawn by hand (`Flow.Edge` uses it).
23
+ * Pass the edge, or a getter of it (`useEdge(() => edge)`) to follow a prop.
24
+ */
25
+ export function useEdge(edge) {
26
+ const context = useFlowContext("useEdge");
27
+ const current = () => read(edge);
28
+ const view = flowValue(context, () => {
29
+ const edgeId = current().id;
30
+ const engine = context.engine;
31
+ return engine
32
+ ? {
33
+ attributes: engine.adapter.attributes("edge", { edgeId }),
34
+ path: engine.get("edge-path-by", { edgeId }),
35
+ reconnecting: engine.is("edge-reconnecting", { edgeId }),
36
+ focusable: engine.is("edge-focusable", { edgeId }),
37
+ }
38
+ : {
39
+ // before the engine: the static geometry's path, once both nodes have a size
40
+ attributes: staticAttributes("edge", edgeId, current()),
41
+ path: context.geometry?.edgePaths[edgeId],
42
+ reconnecting: false,
43
+ focusable: false,
44
+ };
45
+ }, (a, b) => a.reconnecting === b.reconnecting &&
46
+ a.focusable === b.focusable &&
47
+ sameEdgePath(a.path, b.path) &&
48
+ shallowEqual(a.attributes, b.attributes), { entity: () => ({ kind: "edge", id: current().id }) });
49
+ const state = $derived.by(() => {
50
+ const { attributes, path, reconnecting, focusable } = view.current;
51
+ const value = current();
52
+ return {
53
+ edgeId: value.id,
54
+ edge: value,
55
+ path,
56
+ selected: attributes["data-selected"] !== undefined,
57
+ reconnecting,
58
+ focusable,
59
+ attributes,
60
+ };
61
+ });
62
+ const props = $derived.by(() => ({
63
+ ...view.current.attributes,
64
+ tabindex: view.current.focusable ? 0 : undefined,
65
+ }));
66
+ return {
67
+ get state() {
68
+ return state;
69
+ },
70
+ get props() {
71
+ return props;
72
+ },
73
+ };
74
+ }
75
+ /**
76
+ * @internal an edge's path, notifying its readers when it changes (`Flow.EdgePath`, …): the
77
+ * engine's, or the static geometry's before the engine
78
+ */
79
+ export function edgePathValue(context, edgeId) {
80
+ return flowValue(context, () => {
81
+ const id = edgeId();
82
+ const { engine, geometry } = context;
83
+ return engine
84
+ ? engine.get("edge-path-by", { edgeId: id })
85
+ : geometry?.edgePaths[id];
86
+ }, sameEdgePath, { entity: () => ({ kind: "edge", id: edgeId() }) });
87
+ }
88
+ /**
89
+ * @internal the attributes of an edge part before the engine (as a node's): its part, its id,
90
+ * `data-selected` and `data-animated` from the document
91
+ */
92
+ export function staticAttributes(part, edgeId, edge) {
93
+ return {
94
+ ...partAttributes(part, edgeId),
95
+ "data-selected": flag(edge?.selected === true),
96
+ "data-animated": flag(edge?.animated === true),
97
+ };
98
+ }
@@ -0,0 +1,4 @@
1
+ import type { AnyTypes, FlowEdge, FlowTypes } from "@fragiola/flow";
2
+ import { type FlowValue } from "./internal.svelte.js";
3
+ /** Every edge of the enclosing `Flow.Root`, in drawing order, as `current`, which follows every change of one. */
4
+ export declare function useEdges<T extends FlowTypes = AnyTypes>(): FlowValue<readonly FlowEdge<T>[]>;
@@ -0,0 +1,7 @@
1
+ import { useFlowContext } from "../context.js";
2
+ import { flowValue } from "./internal.svelte.js";
3
+ /** Every edge of the enclosing `Flow.Root`, in drawing order, as `current`, which follows every change of one. */
4
+ export function useEdges() {
5
+ const context = useFlowContext("useEdges");
6
+ return flowValue(context, () => context.model.get("all-edges"), Object.is);
7
+ }
@@ -0,0 +1,18 @@
1
+ import type { AnyTypes, FlowFeatures, FlowTypes } from "@fragiola/flow";
2
+ import { type FlowRef } from "./create-flow-ref.svelte.js";
3
+ /**
4
+ * A flow ref for this component: pass it as a `Flow.Root`'s `flowRef`, and read `flow.current` in
5
+ * the template (a toolbar next to the flow, which updates when the root mounts) or in handlers.
6
+ *
7
+ * ```svelte
8
+ * <script lang="ts">
9
+ * const flow = useFlowRef<Types>();
10
+ * </script>
11
+ *
12
+ * <button type="button" disabled={!flow.current} onclick={() => flow.current?.engine.run("fit-view")}>
13
+ * Fit
14
+ * </button>
15
+ * <Flow.Root flowRef={flow} defaultNodes={nodes}>…</Flow.Root>
16
+ * ```
17
+ */
18
+ export declare function useFlowRef<T extends FlowTypes = AnyTypes, F extends FlowFeatures = readonly []>(): FlowRef<T, F>;
@@ -0,0 +1,19 @@
1
+ import { createFlowRef } from "./create-flow-ref.svelte.js";
2
+ /**
3
+ * A flow ref for this component: pass it as a `Flow.Root`'s `flowRef`, and read `flow.current` in
4
+ * the template (a toolbar next to the flow, which updates when the root mounts) or in handlers.
5
+ *
6
+ * ```svelte
7
+ * <script lang="ts">
8
+ * const flow = useFlowRef<Types>();
9
+ * </script>
10
+ *
11
+ * <button type="button" disabled={!flow.current} onclick={() => flow.current?.engine.run("fit-view")}>
12
+ * Fit
13
+ * </button>
14
+ * <Flow.Root flowRef={flow} defaultNodes={nodes}>…</Flow.Root>
15
+ * ```
16
+ */
17
+ export function useFlowRef() {
18
+ return createFlowRef();
19
+ }
@@ -0,0 +1,13 @@
1
+ import type { AnyTypes, FlowFeatures, FlowTypes } from "@fragiola/flow";
2
+ import { type FlowValue } from "./internal.svelte.js";
3
+ import type { UseFlowResult } from "./use-flow.svelte.js";
4
+ /**
5
+ * A value selected from the model and the engine of the enclosing `Flow.Root`, as `current`,
6
+ * which notifies its readers only when it changes (`equal`, default `Object.is`): `selector` runs
7
+ * again on every change of the model and every view change of the engine (a measurement, the
8
+ * viewport, a gesture), and when a reactive value it reads changes. Pass the registry and the
9
+ * features as the type arguments after the selected value's
10
+ * (`useFlowStore<number, Types, typeof features>(…)`). Give a selector that builds an object or an
11
+ * array an `equal` that compares it (`sameFlowValue`), or its readers update on every change.
12
+ */
13
+ export declare function useFlowStore<S, T extends FlowTypes = AnyTypes, F extends FlowFeatures = readonly []>(selector: (flow: UseFlowResult<T, F>) => S, equal?: (a: S, b: S) => boolean): FlowValue<S>;
@@ -0,0 +1,18 @@
1
+ import { typedEngine, typedModel, useFlowContext } from "../context.js";
2
+ import { flowValue } from "./internal.svelte.js";
3
+ /**
4
+ * A value selected from the model and the engine of the enclosing `Flow.Root`, as `current`,
5
+ * which notifies its readers only when it changes (`equal`, default `Object.is`): `selector` runs
6
+ * again on every change of the model and every view change of the engine (a measurement, the
7
+ * viewport, a gesture), and when a reactive value it reads changes. Pass the registry and the
8
+ * features as the type arguments after the selected value's
9
+ * (`useFlowStore<number, Types, typeof features>(…)`). Give a selector that builds an object or an
10
+ * array an `equal` that compares it (`sameFlowValue`), or its readers update on every change.
11
+ */
12
+ export function useFlowStore(selector, equal = Object.is) {
13
+ const context = useFlowContext("useFlowStore");
14
+ return flowValue(context, () => selector({
15
+ model: typedModel(context.model),
16
+ engine: typedEngine(context.publicEngine),
17
+ }), equal);
18
+ }
@@ -0,0 +1,22 @@
1
+ import type { AnyTypes, FlowEngine, FlowFeatures, FlowModel, FlowTypes } from "@fragiola/flow";
2
+ export interface UseFlowResult<T extends FlowTypes = AnyTypes, F extends FlowFeatures = readonly []> {
3
+ /**
4
+ * the model of the enclosing `Flow.Root`: the flow's document and its rules. Change it with
5
+ * `model.run("node.move", { nodeId, position })`, read it with `model.get` / `model.is`
6
+ */
7
+ readonly model: FlowModel<T, F>;
8
+ /**
9
+ * the engine of the flow on screen: screen actions (`engine.run("fit-view")`), screen facts
10
+ * (`engine.get("viewport")`) and its events (`engine.subscribe`). It is there from the first
11
+ * render; before the root is mounted (and on the server) an action is refused and a read is
12
+ * empty
13
+ */
14
+ readonly engine: FlowEngine<T, F>;
15
+ }
16
+ /**
17
+ * The model and the engine of the enclosing `Flow.Root` (getters: another `model` given to the
18
+ * root is followed). Pass the registry and the features as the type arguments
19
+ * (`useFlow<Types, typeof features>()`): a child cannot infer them through context. Call it in a
20
+ * component's script, inside the root.
21
+ */
22
+ export declare function useFlow<T extends FlowTypes = AnyTypes, F extends FlowFeatures = readonly []>(): UseFlowResult<T, F>;
@@ -0,0 +1,18 @@
1
+ import { typedEngine, typedModel, useFlowContext } from "../context.js";
2
+ /**
3
+ * The model and the engine of the enclosing `Flow.Root` (getters: another `model` given to the
4
+ * root is followed). Pass the registry and the features as the type arguments
5
+ * (`useFlow<Types, typeof features>()`): a child cannot infer them through context. Call it in a
6
+ * component's script, inside the root.
7
+ */
8
+ export function useFlow() {
9
+ const context = useFlowContext("useFlow");
10
+ return {
11
+ get model() {
12
+ return typedModel(context.model);
13
+ },
14
+ get engine() {
15
+ return typedEngine(context.publicEngine);
16
+ },
17
+ };
18
+ }
@@ -0,0 +1,56 @@
1
+ import { type HandleSide, type HandleType, 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
+ export interface UseHandleOptions {
6
+ /** whether edges start (`source`) or end (`target`) here */
7
+ type: HandleType;
8
+ /** the side of the node it sits on: the direction its edges leave or enter by */
9
+ side: HandleSide;
10
+ /** its id among the node's handles of its type (none: the node's default handle) */
11
+ handleId?: string | undefined;
12
+ /** whether a connection may start or end here; default true */
13
+ connectable?: boolean | undefined;
14
+ /** whether a connection may start here (a drag, a tap, a key); default true */
15
+ connectableStart?: boolean | undefined;
16
+ /** whether a connection may end here (be its target); default true */
17
+ connectableEnd?: boolean | undefined;
18
+ /** its node; default the enclosing `Flow.Node`'s */
19
+ nodeId?: string | undefined;
20
+ }
21
+ /** What a handle shows. */
22
+ export interface HandleState {
23
+ readonly nodeId: string;
24
+ readonly handleId: string | undefined;
25
+ readonly type: HandleType;
26
+ readonly side: HandleSide;
27
+ /** a connection may start or end here (its flag and its node's): `data-connectable` */
28
+ readonly connectable: boolean;
29
+ /** a connection may start here: `data-connectable-start` */
30
+ readonly connectableStart: boolean;
31
+ /** a connection may end here: `data-connectable-end` */
32
+ readonly connectableEnd: boolean;
33
+ /** the connection under way starts here (`connection()`): `data-connecting` */
34
+ readonly connecting: boolean;
35
+ /** the connection under way would end here and be accepted: `data-valid` */
36
+ readonly valid: boolean;
37
+ /** the connection under way would end here and be refused: `data-invalid` */
38
+ readonly invalid: boolean;
39
+ readonly attributes: PartAttributes;
40
+ }
41
+ /** What goes on a handle's element (`{...props}`). */
42
+ export type HandleElementProps = {
43
+ /** the attachment that registers the element with the engine (D8) */
44
+ readonly [attachment: symbol]: Attachment<Element>;
45
+ } & DataAttributes;
46
+ export interface UseHandleResult {
47
+ readonly state: HandleState;
48
+ /** what goes on the handle's element: its attributes and the attachment that registers it */
49
+ readonly props: HandleElementProps;
50
+ }
51
+ /**
52
+ * A handle's state and the props of its element, for a handle rendered by hand (`Flow.Handle`
53
+ * uses it): registered with the engine, which measures its centre relative to its node. Pass the
54
+ * options, or a getter of them to follow props (`useHandle(() => ({ type, side }))`).
55
+ */
56
+ export declare function useHandle(options: MaybeGetter<UseHandleOptions>): UseHandleResult;
@@ -0,0 +1,94 @@
1
+ import { partAttributes, } from "@fragiola/flow";
2
+ import { createAttachmentKey } from "svelte/attachments";
3
+ import { useFlowContext, usePartNodeId } from "../context.js";
4
+ import { flowValue, read, sameValue, } from "./internal.svelte.js";
5
+ /** A registration's identity: a change registers the handle again. */
6
+ function registrationKey(registration) {
7
+ return JSON.stringify([
8
+ registration.nodeId,
9
+ registration.handleId ?? null,
10
+ registration.type,
11
+ registration.side,
12
+ registration.connectable ?? null,
13
+ registration.connectableStart ?? null,
14
+ registration.connectableEnd ?? null,
15
+ ]);
16
+ }
17
+ /**
18
+ * A handle's state and the props of its element, for a handle rendered by hand (`Flow.Handle`
19
+ * uses it): registered with the engine, which measures its centre relative to its node. Pass the
20
+ * options, or a getter of them to follow props (`useHandle(() => ({ type, side }))`).
21
+ */
22
+ export function useHandle(options) {
23
+ const context = useFlowContext("useHandle");
24
+ const nodeIdOf = usePartNodeId("Handle", () => read(options).nodeId);
25
+ // the last object while its fields stay: a new one registers the handle again
26
+ let last;
27
+ const registration = $derived.by(() => {
28
+ const { type, side, handleId, connectable, connectableStart, connectableEnd, } = read(options);
29
+ const next = {
30
+ nodeId: nodeIdOf(),
31
+ handleId,
32
+ type,
33
+ side,
34
+ connectable,
35
+ connectableStart,
36
+ connectableEnd,
37
+ };
38
+ if (last !== undefined &&
39
+ registrationKey(last) === registrationKey(next)) {
40
+ return last;
41
+ }
42
+ last = next;
43
+ return next;
44
+ });
45
+ const attributes = flowValue(context, () => {
46
+ const value = registration;
47
+ const engine = context.engine;
48
+ return engine
49
+ ? engine.adapter.attributes("handle", value)
50
+ : {
51
+ ...partAttributes("handle", value.handleId),
52
+ "data-handle-type": value.type,
53
+ "data-side": value.side,
54
+ };
55
+ }, sameValue, { scope: "entity" });
56
+ const key = createAttachmentKey();
57
+ const attachment = (element) => {
58
+ const engine = context.engine;
59
+ const value = registration;
60
+ if (!engine)
61
+ return;
62
+ engine.adapter.registerHandle(value, element);
63
+ return () => engine.adapter.registerHandle(value, null);
64
+ };
65
+ const state = $derived.by(() => {
66
+ const value = registration;
67
+ const current = attributes.current;
68
+ return {
69
+ nodeId: value.nodeId,
70
+ handleId: value.handleId,
71
+ type: value.type,
72
+ side: value.side,
73
+ connectable: current["data-connectable"] !== undefined,
74
+ connectableStart: current["data-connectable-start"] !== undefined,
75
+ connectableEnd: current["data-connectable-end"] !== undefined,
76
+ connecting: current["data-connecting"] !== undefined,
77
+ valid: current["data-valid"] !== undefined,
78
+ invalid: current["data-invalid"] !== undefined,
79
+ attributes: current,
80
+ };
81
+ });
82
+ const props = $derived.by(() => ({
83
+ ...attributes.current,
84
+ [key]: attachment,
85
+ }));
86
+ return {
87
+ get state() {
88
+ return state;
89
+ },
90
+ get props() {
91
+ return props;
92
+ },
93
+ };
94
+ }
@@ -0,0 +1,10 @@
1
+ import { type AnyTypes, type FlowTypes, type InternalNode } from "@fragiola/flow";
2
+ import { type FlowValue, type MaybeGetter } from "./internal.svelte.js";
3
+ /**
4
+ * A node as the engine knows it on screen (`internal-node-by`) as `current`: the document's node,
5
+ * its absolute position and box (`nodeOrigin` applied), whether its size is known, its z-index and
6
+ * its handles with their centres; undefined while it does not exist or before the root is
7
+ * mounted. Pass the id, or a getter of it, to follow a prop. It notifies its readers only when
8
+ * one of them changes (compared by value): a pan does not.
9
+ */
10
+ export declare function useInternalNode<T extends FlowTypes = AnyTypes>(nodeId: MaybeGetter<string>): FlowValue<InternalNode<T> | undefined>;
@@ -0,0 +1,16 @@
1
+ import { sameFlowValue, } from "@fragiola/flow";
2
+ import { useFlowContext } from "../context.js";
3
+ import { flowValue, read, } from "./internal.svelte.js";
4
+ /**
5
+ * A node as the engine knows it on screen (`internal-node-by`) as `current`: the document's node,
6
+ * its absolute position and box (`nodeOrigin` applied), whether its size is known, its z-index and
7
+ * its handles with their centres; undefined while it does not exist or before the root is
8
+ * mounted. Pass the id, or a getter of it, to follow a prop. It notifies its readers only when
9
+ * one of them changes (compared by value): a pan does not.
10
+ */
11
+ export function useInternalNode(nodeId) {
12
+ const context = useFlowContext("useInternalNode");
13
+ return flowValue(context, () => context.engine?.get("internal-node-by", {
14
+ nodeId: read(nodeId),
15
+ }), sameFlowValue);
16
+ }
@@ -0,0 +1,16 @@
1
+ import { type KeyPressKeys, type KeyPressTarget } from "@fragiola/flow";
2
+ import { type FlowValue, type MaybeGetter } from "./internal.svelte.js";
3
+ export interface UseKeyPressOptions {
4
+ /** where the keys are read: a window, a document or an element; default the root's window */
5
+ target?: KeyPressTarget | null | undefined;
6
+ }
7
+ /**
8
+ * Whether a key, or a combo of keys, is held, as `current`, which notifies its readers when that
9
+ * changes. `keys` is a combo or several (any of them): key names joined by `+`, held at once
10
+ * (`"Shift"`, `"Meta+s"`, `["Meta+z", "Control+z"]`); a name is a `KeyboardEvent.key` or `.code`,
11
+ * without case (`watchKeyPress`). The keys are read on `target`, by default the window of the
12
+ * enclosing `Flow.Root`'s element (never the global one): false before the root is mounted, or
13
+ * outside a root without a `target`. Pass getters to follow props; it listens again only when
14
+ * the keys (by value) or the target change. Call it in a component's script.
15
+ */
16
+ export declare function useKeyPress(keys: MaybeGetter<KeyPressKeys>, options?: MaybeGetter<UseKeyPressOptions>): FlowValue<boolean>;