@dou-ui/flow 0.1.0-alpha.2 → 0.1.0-alpha.3
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/dist/components/default-node2.js.map +1 -1
- package/dist/components/port.js +7 -0
- package/dist/components/port.js.map +1 -0
- package/dist/context.js.map +1 -1
- package/dist/core/edge-types.js +107 -3
- package/dist/core/edge-types.js.map +1 -1
- package/dist/core/model.js.map +1 -1
- package/dist/core/transaction.js +107 -11
- package/dist/core/transaction.js.map +1 -1
- package/dist/flow-editor.js.map +1 -1
- package/dist/flow-editor.vue_vue_type_script_setup_true_lang.js +56 -8
- package/dist/flow-editor.vue_vue_type_script_setup_true_lang.js.map +1 -1
- package/dist/index.js +1 -2
- package/dist/index.js.map +1 -1
- package/dist/locale.js +9 -9
- package/dist/locale.js.map +1 -1
- package/dist/style.css +1 -1
- package/dist/types/components/default-node.d.ts +1 -1
- package/dist/types/components/port.d.ts +7 -0
- package/dist/types/context.d.ts +6 -0
- package/dist/types/core/model.d.ts +6 -0
- package/dist/types/core/transaction.d.ts +21 -0
- package/dist/types/flow-editor.vue.d.ts +1 -0
- package/dist/types/index.d.ts +5 -7
- package/dist/types/locale.d.ts +3 -3
- package/dist/whiteboard/board-node.js.map +1 -1
- package/dist/whiteboard/board-node.vue_vue_type_script_setup_true_lang.js +3 -3
- package/dist/whiteboard/board-node.vue_vue_type_script_setup_true_lang.js.map +1 -1
- package/package.json +3 -3
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"default-node2.js","names":["DefaultNode"],"sources":["../../src/components/default-node.ts"],"sourcesContent":["import { defineNodeType } from '../core/node-types'\nimport { drawCard } from '../export/draw'\nimport type { Component } from 'vue'\nimport type { FlowPortSpec } from '../core/model'\nimport DefaultNode from './default-node.vue'\n\nconst px = (value: string, fallback: number) => Number.parseFloat(value) || fallback\n\nexport interface DefaultNodeData {\n [key: string]: unknown\n /** The title. */\n label?: string\n description?: string\n icon?: Component\n status?: '
|
|
1
|
+
{"version":3,"file":"default-node2.js","names":["DefaultNode"],"sources":["../../src/components/default-node.ts"],"sourcesContent":["import { defineNodeType } from '../core/node-types'\nimport { drawCard } from '../export/draw'\nimport type { Component } from 'vue'\nimport type { FlowPortSpec } from '../core/model'\nimport DefaultNode from './default-node.vue'\n\nconst px = (value: string, fallback: number) => Number.parseFloat(value) || fallback\n\nexport interface DefaultNodeData {\n [key: string]: unknown\n /** The title. */\n label?: string\n description?: string\n icon?: Component\n status?: 'pending' | 'running' | 'done' | 'error'\n inputs?: Omit<FlowPortSpec, 'kind'>[]\n outputs?: Omit<FlowPortSpec, 'kind'>[]\n}\n\n/**\n * A titled card: an optional icon, a status dot and a description, then a\n * row per port, inputs on the left and outputs on the right. A single\n * unnamed input and output sit on the title row.\n */\nexport const defaultNode = defineNodeType<DefaultNodeData>({\n type: 'default',\n component: DefaultNode,\n ports: (node) => [\n ...(node.data?.inputs ?? []).map((port) => ({ ...port, kind: 'input' as const })),\n ...(node.data?.outputs ?? []).map((port) => ({ ...port, kind: 'output' as const })),\n ],\n defaults: () => ({ label: '' }),\n draw(node, draw) {\n const data = node.data ?? {}\n const t = draw.messages\n const parts = [drawCard(draw)]\n const head = draw.style('dou-flow-default-node__head')\n const headHeight = px(head.minHeight, 44)\n const inputs = data.inputs ?? []\n const outputs = data.outputs ?? []\n const inline =\n inputs.length <= 1 && outputs.length <= 1 && ![...inputs, ...outputs].some((p) => p.label)\n let left = 12\n let right = draw.width - 12\n if (data.icon) {\n const box = draw.style('dou-flow-default-node__icon')\n const y = (headHeight - 24) / 2\n parts.push(\n `<rect x=\"${left}\" y=\"${y}\" width=\"24\" height=\"24\" rx=\"${px(box.borderTopLeftRadius, 6)}\" fill=\"${draw.color(box.backgroundColor)}\"/>`,\n draw.icon(\n data.icon,\n { x: left + 4, y: y + 4, width: 16, height: 16 },\n draw.color(box.color),\n ),\n )\n left += 32\n }\n if (data.status) {\n const dot = draw.style(`dou-flow-default-node__status is-${data.status}`)\n parts.push(\n `<circle cx=\"${right - 4}\" cy=\"${headHeight / 2}\" r=\"4\" fill=\"${draw.color(dot.backgroundColor)}\"/>`,\n )\n right -= 16\n }\n const title = draw.style('dou-flow-default-node__title')\n parts.push(\n draw.text(\n data.label ?? '',\n { x: left, y: 0, width: Math.max(0, right - left), height: headHeight },\n { weight: title.fontWeight, valign: 'middle', nowrap: true, lineHeight: headHeight },\n ),\n )\n let y = headHeight\n if (data.description) {\n const text = draw.style('dou-flow-default-node__description')\n const lineHeight = px(text.lineHeight, 20)\n const width = draw.width - 24\n const lines = draw.text(\n data.description,\n { x: 12, y: y - 4, width, height: 1e6 },\n {\n size: px(text.fontSize, 12),\n lineHeight,\n color: draw.color(text.color),\n },\n )\n parts.push(lines)\n y += (lines.match(/<tspan/g)?.length ?? 1) * lineHeight + 12 - 4\n }\n if (!inline) {\n const rows = draw.style('dou-flow-default-node__ports')\n const row = draw.style('dou-flow-default-node__row')\n parts.push(\n `<rect x=\"1\" y=\"${y}\" width=\"${draw.width - 2}\" height=\"1\" fill=\"${draw.color(rows.borderTopColor)}\"/>`,\n )\n y += 1 + px(rows.paddingTop, 3)\n const height = px(row.height, 28)\n const size = px(row.fontSize, 12)\n const color = draw.color(row.color)\n const half = (draw.width - 24) / 2\n for (let i = 0; i < Math.max(inputs.length, outputs.length); i++) {\n const input = inputs[i]\n const output = outputs[i]\n const box = (x: number) => ({ x, y: y + i * height, width: half - 4, height })\n const options = { size, color, valign: 'middle' as const, nowrap: true, lineHeight: height }\n if (input) parts.push(draw.text(input.label ?? t.input, box(12), options))\n if (output)\n parts.push(\n draw.text(output.label ?? t.output, box(draw.width / 2 + 4), {\n ...options,\n align: 'end',\n }),\n )\n }\n }\n for (const id of draw.ports.keys()) parts.push(draw.port(id))\n return parts.join('')\n },\n})\n"],"mappings":";;;;AAMA,IAAM,MAAM,OAAe,aAAqB,OAAO,WAAW,KAAK,KAAK;;;;;;AAkB5E,IAAa,cAAc,eAAgC;CACzD,MAAM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,MAAM,QAAQ,OAAO;IACrB,MAAM,SAAS,QAAQ;IACvB,MAAM,OAAO,OAAe;KAAE;KAAG,GAAG,IAAI,IAAI;KAAQ,OAAO,OAAO;KAAG;IAAO;IAC5E,MAAM,UAAU;KAAE;KAAM;KAAO,QAAQ;KAAmB,QAAQ;KAAM,YAAY;IAAO;IAC3F,IAAI,OAAO,MAAM,KAAK,KAAK,KAAK,MAAM,SAAS,EAAE,OAAO,IAAI,EAAE,GAAG,OAAO,CAAC;IACzE,IAAI,QACF,MAAM,KACJ,KAAK,KAAK,OAAO,SAAS,EAAE,QAAQ,IAAI,KAAK,QAAQ,IAAI,CAAC,GAAG;KAC3D,GAAG;KACH,OAAO;IACT,CAAC,CACH;GACJ;EACF;EACA,KAAK,MAAM,MAAM,KAAK,MAAM,KAAK,GAAG,MAAM,KAAK,KAAK,KAAK,EAAE,CAAC;EAC5D,OAAO,MAAM,KAAK,EAAE;CACtB;AACF,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"port.js","names":["FlowPort"],"sources":["../../src/components/port.ts"],"sourcesContent":["import { withInstall } from 'dou-ui'\nimport FlowPort from './flow-port.vue'\n\n// 单独成模块:白板(@dou-ui/flow/whiteboard)从这里取包装过的组件,不用引入整个编辑器入口\nexport const DouFlowPort = withInstall(FlowPort)\n"],"mappings":";;;AAIA,IAAa,cAAc,YAAYA,iBAAQ"}
|
package/dist/context.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"context.js","names":[],"sources":["../src/context.ts"],"sourcesContent":["import type { ContextMenuItem } from 'dou-ui'\nimport type { FlowExportOptions, FlowImageOptions } from './export/draw'\nimport type { FlowTree } from './core/groups'\nimport { inject, type ComputedRef, type InjectionKey, type Ref, type ShallowRef } from 'vue'\nimport type { FlowMessages, FlowMessagesOverride } from './locale'\nimport type {\n FlowData,\n FlowDocument,\n FlowEndpoint,\n FlowNode,\n FlowPortSpec,\n FlowSelection,\n FlowViewport,\n Point,\n PortSide,\n Rect,\n} from './core/model'\nimport type { FlowConnection, PortTypes, PortTypeSpec } from './core/connect'\nimport type { EdgeTypeSpec } from './core/edge-types'\nimport type { FlowExtension } from './core/extension'\nimport type { FlowMenuTarget } from './core/menu'\nimport type { NodeTypeSpec } from './core/node-types'\nimport type { FlowTransaction, FlowTransactionMeta } from './core/transaction'\n\nexport type FlowPanelPlacement = 'top-left' | 'top-right' | 'bottom-left' | 'bottom-right'\n\nexport interface FlowControlsProps {\n /** Corner of the editor to sit in. */\n placement?: FlowPanelPlacement\n /** Show undo and redo. */\n history?: boolean\n /** Show the tidy button, which lays the nodes out along their edges. */\n tidy?: boolean\n}\n\nexport interface FlowMinimapProps {\n /** Corner of the editor to sit in. */\n placement?: FlowPanelPlacement\n /** Size of the map in px. */\n width?: number\n height?: number\n}\n\n/** What the find panel looks for: text in the nodes' names. */\nexport interface FlowSearchQuery {\n search: string\n /** Tell capital letters from small ones. */\n caseSensitive?: boolean\n}\n\nexport interface FlowEditorProps {\n /**\n * What the editor is made of: node types, port types, rules, keys and\n * layers. `flowKit()` by default.\n */\n extensions?: (FlowExtension | FlowExtension[])[]\n /** Node types besides the extensions'; they win over an extension's type of the same name. */\n nodeTypes?: NodeTypeSpec<any>[]\n /** Edge types besides the extensions'; they win over an extension's type of the same name. */\n edgeTypes?: EdgeTypeSpec<any>[]\n /** The type of edges without `type`, new ones included. `bezier` by default. */\n edgeType?: string\n /**\n * Below this zoom nodes are drawn as plain boxes with their names, which\n * keeps a large flow quick; see the `simple` slot. `0` never does. `0.4` by default.\n */\n simpleZoom?: number\n /** Grid step for snapping and arrow keys, in px. `0` turns snapping to the grid off. */\n grid?: number\n /** Line nodes up with their neighbours while dragging. */\n guides?: boolean\n minZoom?: number\n maxZoom?: number\n /**\n * Which way the flow runs: `horizontal` left to right, `vertical` top to\n * bottom. Tidying lays nodes out this way.\n */\n direction?: 'horizontal' | 'vertical'\n /** Fit all nodes into view once they are measured. */\n fitView?: boolean\n /**\n * The furthest `fit-view` zooms out, so text stays readable; a larger flow\n * starts at its top left and is panned to. `0` always fits everything.\n */\n fitMinZoom?: number\n /** View, select and pan, but not change anything. */\n readonly?: boolean\n /** The built-in find panel (`Ctrl/⌘+F`). Off, the keys are left to the page. On by default. */\n searchable?: boolean\n /** Scrolling pans the canvas; with Ctrl / ⌘ it zooms. Off, only Ctrl / ⌘ + scroll zooms. */\n panOnScroll?: boolean\n /** Looks and connection rules of port types, by the name used in `dataType`. */\n portTypes?: PortTypes\n /** Refuse edges that would close a cycle. */\n acyclic?: boolean\n /**\n * The last check before an edge is made. Return `false` to refuse it, or a\n * sentence saying why. Called for every port while an edge is dragged.\n */\n canConnect?: (connection: FlowConnection, doc: FlowDocument) => boolean | string\n /** Accessible name of the canvas. */\n ariaLabel?: string\n /** Texts to change; only the ones given are replaced. */\n messages?: FlowMessagesOverride\n}\n\nexport interface FlowEditorEmits {\n /** Every change to the document, with its steps; `update:modelValue` follows. */\n transaction: [tr: FlowTransaction]\n /**\n * An extension's `transaction` hook cancelled a change; `tr.cancelReason`\n * says why, and the editor shows it and reads it out.\n */\n transactionCancel: [tr: FlowTransaction]\n nodeClick: [node: FlowNode, event: MouseEvent]\n nodeDblclick: [node: FlowNode, event: MouseEvent]\n nodeContextmenu: [node: FlowNode, event: MouseEvent]\n edgeClick: [edge: FlowDocument['edges'][number], event: MouseEvent]\n edgeContextmenu: [edge: FlowDocument['edges'][number], event: MouseEvent]\n /** Clicking the empty canvas; `point` is in canvas coordinates. */\n canvasClick: [point: Point, event: MouseEvent]\n /**\n * Right click or long press on the empty canvas. The context menu events\n * come before the editor's own menu; `event.preventDefault()` keeps it closed.\n */\n canvasContextmenu: [point: Point, event: MouseEvent]\n /** An edge was drawn, or moved to another port. */\n connect: [edge: FlowDocument['edges'][number]]\n /**\n * A new edge was dropped on empty canvas; `point` is where, in canvas\n * coordinates. The editor then offers the node types that can take the\n * edge; `event.preventDefault()` handles it yourself instead.\n */\n connectToCanvas: [from: Required<FlowEndpoint>, point: Point, event: MouseEvent]\n}\n\nexport interface FlowEditorExpose {\n /** Applies changes as one transaction, one undo step. */\n transact: (fn: (tr: FlowTransaction) => void, meta?: FlowTransactionMeta) => FlowTransaction\n undo: () => boolean\n redo: () => boolean\n readonly canUndo: boolean\n readonly canRedo: boolean\n /** Shows all nodes, or the given ones. */\n fit: (ids?: string[]) => void\n zoomTo: (zoom: number) => void\n zoomIn: () => void\n zoomOut: () => void\n /** Centers a node in view without changing the zoom. */\n centerNode: (id: string) => void\n /** Opens the find panel (`Ctrl/⌘+F`), with a query put in; `false` when `searchable` is off. */\n openSearch: (query?: Partial<FlowSearchQuery>) => boolean\n /** Closes the find panel; `false` when it was not open. */\n closeSearch: () => boolean\n /** The ids of the nodes whose names contain the search, in reading order. */\n findNodes: (query: FlowSearchQuery) => string[]\n /**\n * Moves the view like the user's pans do: no further than having the nodes\n * just leave it. `v-model:viewport` goes anywhere.\n */\n setViewport: (viewport: FlowViewport, animate?: boolean) => void\n /** Converts a point on screen (client coordinates) to canvas coordinates. */\n toCanvas: (point: Point) => Point\n /** A node's measured box in canvas coordinates. */\n getNodeRect: (id: string) => Rect | null\n /**\n * The flow as an SVG document, as it looks now: every node and edge, what\n * is in view, or the selected ones (it throws when nothing is selected). Colors are written as `rgb(…)`, `rgba(…)` or `#rrggbb`, so\n * vector editors read them; nodes are drawn by their type's `draw`.\n */\n exportSVG: (options?: FlowExportOptions) => string\n /** The flow as a PNG image, with images embedded where their address can be read. */\n exportPNG: (options?: FlowImageOptions) => Promise<Blob>\n /**\n * Lays nodes out in columns along their edges, as one undo step: the given\n * ones, the selection when two or more nodes are selected, or all.\n */\n tidy: (ids?: string[]) => void\n /**\n * Ids of edges whose ports no longer fit: a port is gone, or the types of\n * its two ends don't match any more. They are drawn red and dashed.\n */\n readonly staleEdges: string[]\n /** Removes the stale edges as one undo step; returns how many. */\n removeStaleEdges: () => number\n /**\n * Adds a node of a type with its defaults and selects it, as one undo\n * step; returns its id. See `FlowInsertOptions` for where it goes.\n */\n insertNode: (type: string, options?: FlowInsertOptions) => string | null\n /**\n * Starts dragging a new node of a type from `event`, a `pointerdown` on\n * any element on the page; letting go over the canvas adds it there, and\n * Escape cancels. The click that ends a drag is stopped. Returns `false`\n * when nothing was started.\n */\n startInsertDrag: (type: string, event: PointerEvent) => boolean\n /** Node types with a `palette` entry, in the order they were given. */\n readonly paletteTypes: NodeTypeSpec[]\n /** Selects an edge and opens the text field on its label. */\n editLabel: (id: string) => void\n /**\n * Puts nodes, the selected ones by default, into a new container of\n * `type` (`'group'` by default) drawn around them, as one undo step;\n * returns its id, or `null` when nothing was grouped.\n */\n groupNodes: (ids?: string[], type?: string) => string | null\n /** Removes a container and keeps the nodes inside, as one undo step. */\n ungroupNode: (id: string) => boolean\n /**\n * Takes nodes, the selected ones by default, out of their groups and puts\n * them just below, as one undo step; returns how many moved.\n */\n leaveGroup: (ids?: string[]) => number\n}\n\nexport interface FlowEditorSlots {\n /** Panels over the canvas, e.g. `DouFlowControls`. */\n default?: () => unknown\n /** What follows the pointer while a new node is dragged in. */\n ghost?: (props: { spec: NodeTypeSpec; over: boolean }) => unknown\n /** Replaces the list offered when an edge is dropped on the empty canvas. */\n quickAdd?: (props: {\n types: NodeTypeSpec[]\n choose: (type: string) => void\n close: () => void\n }) => unknown\n /** A node's content while zoomed out past `simple-zoom`; its name by default. */\n simple?: (props: { node: FlowNode }) => unknown\n}\n\nexport interface FlowInsertOptions {\n /**\n * Where the node goes, in canvas coordinates: its centre, or with `from`,\n * the port that takes the edge. Without it the node goes after the one\n * selected node, connected to it when it can be, or else in the middle\n * of the view; either way clear of other nodes.\n */\n at?: Point\n /** Connect the new node to this port, through the first port that can take it. */\n from?: Required<FlowEndpoint>\n /** Move focus to the new node, so keys such as undo act on it. `true` by default. */\n focus?: boolean\n /** Merged over the type's `defaults()`. */\n data?: FlowData\n /** The node's size instead of the type's `size`. */\n size?: { width?: number; height?: number }\n}\n\n/** Where a port's centre is, from its node's top left corner. */\nexport interface PortAnchor extends Point {\n side: PortSide\n}\n\nexport interface NodeMetrics {\n width: number\n height: number\n ports: Map<string, PortAnchor>\n}\n\n/** What `previewNode` can show changed: the box, and the data. */\nexport type FlowNodePreview = Partial<Pick<FlowNode, 'x' | 'y' | 'width' | 'height' | 'data'>>\n\n/** How a port shows while an edge is being drawn. */\nexport type PortState = 'active' | 'valid' | 'candidate' | 'refused'\n\nexport interface FlowEditorContext {\n doc: Ref<FlowDocument>\n selection: Ref<FlowSelection>\n viewport: Ref<FlowViewport>\n /** See `FlowEditorExpose.setViewport`; writing `viewport` goes anywhere. */\n setViewport: FlowEditorExpose['setViewport']\n readonly: ComputedRef<boolean>\n messages: ComputedRef<FlowMessages>\n minZoom: ComputedRef<number>\n maxZoom: ComputedRef<number>\n transact: FlowEditorExpose['transact']\n undo: () => boolean\n redo: () => boolean\n canUndo: Ref<boolean>\n canRedo: Ref<boolean>\n fit: FlowEditorExpose['fit']\n zoomTo: FlowEditorExpose['zoomTo']\n zoomIn: () => void\n zoomOut: () => void\n /** The registered type of a port, if any. */\n portTypeOf: (port: FlowPortSpec) => PortTypeSpec | undefined\n /** A port type's color as CSS. */\n colorOf: (spec: PortTypeSpec | undefined) => string | undefined\n getNodeRect: FlowEditorExpose['getNodeRect']\n exportSVG: FlowEditorExpose['exportSVG']\n exportPNG: FlowEditorExpose['exportPNG']\n tidy: FlowEditorExpose['tidy']\n centerNode: FlowEditorExpose['centerNode']\n /** Says `text` to screen readers. */\n announce: (text: string) => void\n /** The editor's size on screen, in px. */\n size: Ref<{ width: number; height: number }>\n selectedNodes: ComputedRef<Set<string>>\n insertNode: FlowEditorExpose['insertNode']\n /** Node types with a `palette` entry, in the order they were given. */\n paletteTypes: ComputedRef<NodeTypeSpec[]>\n startInsertDrag: FlowEditorExpose['startInsertDrag']\n editLabel: FlowEditorExpose['editLabel']\n groupNodes: FlowEditorExpose['groupNodes']\n ungroupNode: FlowEditorExpose['ungroupNode']\n leaveGroup: FlowEditorExpose['leaveGroup']\n /** Which node is inside which, from the nodes' `parent`. */\n tree: ComputedRef<FlowTree>\n /** Whether the node's type can hold other nodes. */\n isContainer: (node: FlowNode) => boolean\n /** How far the node is drawn turned about its centre, from its type's `rotation`. */\n nodeRotation: (id: string) => number\n /**\n * While nodes are moved or resized, or an edge is bent: the document\n * changes on every frame until the pointer is let go.\n */\n gesturing: ComputedRef<boolean>\n /**\n * Shows a node as if `patch` were applied, without changing the document:\n * its frame, ports, edges and `getNodeRect` all follow. For handles that\n * change the document once, when let go; `null` shows it as it is again,\n * and must be passed when the drag ends or is cancelled. `patch.data`\n * replaces the node's data, it is not merged. Only this editor shows it:\n * peers see the change once it is committed. A preview is dropped when\n * its node leaves the document.\n */\n previewNode: (id: string, patch: FlowNodePreview | null) => void\n /** The edge being drawn, as an SVG path in canvas coordinates. */\n connectingPath: ComputedRef<string | null>\n /** The editor's element. */\n root: Readonly<ShallowRef<HTMLElement | null>>\n /**\n * Opens the context menu for a target at a point on screen; `false` when\n * no extension has items for it. Pass the button that opens it as `from`:\n * the menu then opens below the button instead of at the point, or above\n * it when there is no room, and pressing the button again closes it.\n */\n openMenu: (target: FlowMenuTarget, client: Point, from?: Element) => boolean\n /** The items the context menu has for a target, as `openMenu` shows them. */\n menuItems: (target: FlowMenuTarget) => ContextMenuItem[]\n /**\n * Opens the editor's menu with these items, e.g. the choices behind a\n * toolbar button; `from` works as in `openMenu`.\n */\n showMenu: (\n items: ContextMenuItem[],\n client: Point,\n from?: Element,\n options?: { focusChecked?: boolean },\n ) => boolean\n /** The element that opened the menu, while it is open, e.g. to mark a button expanded. */\n menuFrom: Readonly<ShallowRef<Element | null>>\n toCanvas: (point: Point) => Point\n /** Copies the selected nodes to the clipboard; `false` when none are selected. */\n copy: () => boolean\n cut: () => boolean\n /** Pastes what was copied, at `at` (canvas coordinates) or next to the original. */\n paste: (at?: Point) => Promise<boolean>\n /** Adds a copy of the selected nodes next to them. */\n duplicate: () => void\n /** Deletes the selected nodes and edges, except locked nodes. */\n deleteSelection: () => void\n selectAll: () => void\n}\n\nexport const flowEditorKey: InjectionKey<FlowEditorContext> = Symbol('dou-flow-editor')\n\n/** The surrounding flow editor; for controls and panels placed in its default slot. */\nexport function useFlowEditor() {\n const context = inject(flowEditorKey, null)\n if (!context)\n throw new Error('[@dou-ui/flow] useFlowEditor() must be called inside <DouFlowEditor>')\n return context\n}\n\nexport interface FlowNodeContext {\n node: ComputedRef<FlowNode>\n ports: ComputedRef<FlowPortSpec[]>\n /** Port ids with at least one edge. */\n connected: ComputedRef<Set<string>>\n /** While an edge is being drawn, how each port shows; `null` otherwise. */\n portStates: ComputedRef<Map<string, PortState> | null>\n registerPort: (id: string, el: HTMLElement, auto?: boolean) => void\n unregisterPort: (id: string, auto?: boolean) => void\n}\n\nexport const flowNodeKey: InjectionKey<FlowNodeContext> = Symbol('dou-flow-node')\n"],"mappings":";;AA8WA,IAAa,gBAAiD,OAAO,iBAAiB;;AAGtF,SAAgB,gBAAgB;CAC9B,MAAM,UAAU,OAAO,eAAe,IAAI;CAC1C,IAAI,CAAC,SACH,MAAM,IAAI,MAAM,sEAAsE;CACxF,OAAO;AACT;AAaA,IAAa,cAA6C,OAAO,eAAe"}
|
|
1
|
+
{"version":3,"file":"context.js","names":[],"sources":["../src/context.ts"],"sourcesContent":["import type { ContextMenuItem } from 'dou-ui'\nimport type { FlowExportOptions, FlowImageOptions } from './export/draw'\nimport type { FlowTree } from './core/groups'\nimport { inject, type ComputedRef, type InjectionKey, type Ref, type ShallowRef } from 'vue'\nimport type { FlowMessages, FlowMessagesOverride } from './locale'\nimport type {\n FlowData,\n FlowDocument,\n FlowEndpoint,\n FlowNode,\n FlowPortSpec,\n FlowSelection,\n FlowViewport,\n Point,\n PortSide,\n Rect,\n} from './core/model'\nimport type { FlowConnection, PortTypes, PortTypeSpec } from './core/connect'\nimport type { EdgeTypeSpec } from './core/edge-types'\nimport type { FlowExtension } from './core/extension'\nimport type { FlowMenuTarget } from './core/menu'\nimport type { NodeTypeSpec } from './core/node-types'\nimport type { FlowTransaction, FlowTransactionMeta } from './core/transaction'\n\nexport type FlowPanelPlacement = 'top-left' | 'top-right' | 'bottom-left' | 'bottom-right'\n\nexport interface FlowControlsProps {\n /** Corner of the editor to sit in. */\n placement?: FlowPanelPlacement\n /** Show undo and redo. */\n history?: boolean\n /** Show the tidy button, which lays the nodes out along their edges. */\n tidy?: boolean\n}\n\nexport interface FlowMinimapProps {\n /** Corner of the editor to sit in. */\n placement?: FlowPanelPlacement\n /** Size of the map in px. */\n width?: number\n height?: number\n}\n\n/** What the find panel looks for: text in the nodes' names. */\nexport interface FlowSearchQuery {\n search: string\n /** Tell capital letters from small ones. */\n caseSensitive?: boolean\n}\n\nexport interface FlowEditorProps {\n /**\n * What the editor is made of: node types, port types, rules, keys and\n * layers. `flowKit()` by default.\n */\n extensions?: (FlowExtension | FlowExtension[])[]\n /** Node types besides the extensions'; they win over an extension's type of the same name. */\n nodeTypes?: NodeTypeSpec<any>[]\n /** Edge types besides the extensions'; they win over an extension's type of the same name. */\n edgeTypes?: EdgeTypeSpec<any>[]\n /** The type of edges without `type`, new ones included. `bezier` by default. */\n edgeType?: string\n /**\n * Below this zoom nodes are drawn as plain boxes with their names, which\n * keeps a large flow quick; see the `simple` slot. `0` never does. `0.4` by default.\n */\n simpleZoom?: number\n /** Grid step for snapping and arrow keys, in px. `0` turns snapping to the grid off. */\n grid?: number\n /** Line nodes up with their neighbours while dragging. */\n guides?: boolean\n minZoom?: number\n maxZoom?: number\n /**\n * Which way the flow runs: `horizontal` left to right, `vertical` top to\n * bottom. Tidying lays nodes out this way.\n */\n direction?: 'horizontal' | 'vertical'\n /** Fit all nodes into view once they are measured. */\n fitView?: boolean\n /**\n * The furthest `fit-view` zooms out, so text stays readable; a larger flow\n * starts at its top left and is panned to. `0` always fits everything.\n */\n fitMinZoom?: number\n /** View, select and pan, but not change anything. */\n readonly?: boolean\n /** The built-in find panel (`Ctrl/⌘+F`). Off, the keys are left to the page. On by default. */\n searchable?: boolean\n /** Scrolling pans the canvas; with Ctrl / ⌘ it zooms. Off, only Ctrl / ⌘ + scroll zooms. */\n panOnScroll?: boolean\n /** Looks and connection rules of port types, by the name used in `dataType`. */\n portTypes?: PortTypes\n /** Refuse edges that would close a cycle. */\n acyclic?: boolean\n /**\n * The last check before an edge is made. Return `false` to refuse it, or a\n * sentence saying why. Called for every port while an edge is dragged.\n */\n canConnect?: (connection: FlowConnection, doc: FlowDocument) => boolean | string\n /**\n * The hairline frame and rounded corners. Off, the canvas takes no\n * border at all, for a panel or page region that already has an edge.\n * On by default.\n */\n bordered?: boolean\n /** Accessible name of the canvas. */\n ariaLabel?: string\n /** Texts to change; only the ones given are replaced. */\n messages?: FlowMessagesOverride\n}\n\nexport interface FlowEditorEmits {\n /** Every change to the document, with its steps; `update:modelValue` follows. */\n transaction: [tr: FlowTransaction]\n /**\n * An extension's `transaction` hook cancelled a change; `tr.cancelReason`\n * says why, and the editor shows it and reads it out.\n */\n transactionCancel: [tr: FlowTransaction]\n nodeClick: [node: FlowNode, event: MouseEvent]\n nodeDblclick: [node: FlowNode, event: MouseEvent]\n nodeContextmenu: [node: FlowNode, event: MouseEvent]\n edgeClick: [edge: FlowDocument['edges'][number], event: MouseEvent]\n edgeContextmenu: [edge: FlowDocument['edges'][number], event: MouseEvent]\n /** Clicking the empty canvas; `point` is in canvas coordinates. */\n canvasClick: [point: Point, event: MouseEvent]\n /**\n * Right click or long press on the empty canvas. The context menu events\n * come before the editor's own menu; `event.preventDefault()` keeps it closed.\n */\n canvasContextmenu: [point: Point, event: MouseEvent]\n /** An edge was drawn, or moved to another port. */\n connect: [edge: FlowDocument['edges'][number]]\n /**\n * A new edge was dropped on empty canvas; `point` is where, in canvas\n * coordinates. The editor then offers the node types that can take the\n * edge; `event.preventDefault()` handles it yourself instead.\n */\n connectToCanvas: [from: Required<FlowEndpoint>, point: Point, event: MouseEvent]\n}\n\nexport interface FlowEditorExpose {\n /** Applies changes as one transaction, one undo step. */\n transact: (fn: (tr: FlowTransaction) => void, meta?: FlowTransactionMeta) => FlowTransaction\n undo: () => boolean\n redo: () => boolean\n readonly canUndo: boolean\n readonly canRedo: boolean\n /** Shows all nodes, or the given ones. */\n fit: (ids?: string[]) => void\n zoomTo: (zoom: number) => void\n zoomIn: () => void\n zoomOut: () => void\n /** Centers a node in view without changing the zoom. */\n centerNode: (id: string) => void\n /** Opens the find panel (`Ctrl/⌘+F`), with a query put in; `false` when `searchable` is off. */\n openSearch: (query?: Partial<FlowSearchQuery>) => boolean\n /** Closes the find panel; `false` when it was not open. */\n closeSearch: () => boolean\n /** The ids of the nodes whose names contain the search, in reading order. */\n findNodes: (query: FlowSearchQuery) => string[]\n /**\n * Moves the view like the user's pans do: no further than having the nodes\n * just leave it. `v-model:viewport` goes anywhere.\n */\n setViewport: (viewport: FlowViewport, animate?: boolean) => void\n /** Converts a point on screen (client coordinates) to canvas coordinates. */\n toCanvas: (point: Point) => Point\n /** A node's measured box in canvas coordinates. */\n getNodeRect: (id: string) => Rect | null\n /**\n * The flow as an SVG document, as it looks now: every node and edge, what\n * is in view, or the selected ones (it throws when nothing is selected). Colors are written as `rgb(…)`, `rgba(…)` or `#rrggbb`, so\n * vector editors read them; nodes are drawn by their type's `draw`.\n */\n exportSVG: (options?: FlowExportOptions) => string\n /** The flow as a PNG image, with images embedded where their address can be read. */\n exportPNG: (options?: FlowImageOptions) => Promise<Blob>\n /**\n * Lays nodes out in columns along their edges, as one undo step: the given\n * ones, the selection when two or more nodes are selected, or all.\n */\n tidy: (ids?: string[]) => void\n /**\n * Ids of edges whose ports no longer fit: a port is gone, or the types of\n * its two ends don't match any more. They are drawn red and dashed.\n */\n readonly staleEdges: string[]\n /** Removes the stale edges as one undo step; returns how many. */\n removeStaleEdges: () => number\n /**\n * Adds a node of a type with its defaults and selects it, as one undo\n * step; returns its id. See `FlowInsertOptions` for where it goes.\n */\n insertNode: (type: string, options?: FlowInsertOptions) => string | null\n /**\n * Starts dragging a new node of a type from `event`, a `pointerdown` on\n * any element on the page; letting go over the canvas adds it there, and\n * Escape cancels. The click that ends a drag is stopped. Returns `false`\n * when nothing was started.\n */\n startInsertDrag: (type: string, event: PointerEvent) => boolean\n /** Node types with a `palette` entry, in the order they were given. */\n readonly paletteTypes: NodeTypeSpec[]\n /** Selects an edge and opens the text field on its label. */\n editLabel: (id: string) => void\n /**\n * Puts nodes, the selected ones by default, into a new container of\n * `type` (`'group'` by default) drawn around them, as one undo step;\n * returns its id, or `null` when nothing was grouped.\n */\n groupNodes: (ids?: string[], type?: string) => string | null\n /** Removes a container and keeps the nodes inside, as one undo step. */\n ungroupNode: (id: string) => boolean\n /**\n * Takes nodes, the selected ones by default, out of their groups and puts\n * them just below, as one undo step; returns how many moved.\n */\n leaveGroup: (ids?: string[]) => number\n}\n\nexport interface FlowEditorSlots {\n /** Panels over the canvas, e.g. `DouFlowControls`. */\n default?: () => unknown\n /** What follows the pointer while a new node is dragged in. */\n ghost?: (props: { spec: NodeTypeSpec; over: boolean }) => unknown\n /** Replaces the list offered when an edge is dropped on the empty canvas. */\n quickAdd?: (props: {\n types: NodeTypeSpec[]\n choose: (type: string) => void\n close: () => void\n }) => unknown\n /** A node's content while zoomed out past `simple-zoom`; its name by default. */\n simple?: (props: { node: FlowNode }) => unknown\n}\n\nexport interface FlowInsertOptions {\n /**\n * Where the node goes, in canvas coordinates: its centre, or with `from`,\n * the port that takes the edge. Without it the node goes after the one\n * selected node, connected to it when it can be, or else in the middle\n * of the view; either way clear of other nodes.\n */\n at?: Point\n /** Connect the new node to this port, through the first port that can take it. */\n from?: Required<FlowEndpoint>\n /** Move focus to the new node, so keys such as undo act on it. `true` by default. */\n focus?: boolean\n /** Merged over the type's `defaults()`. */\n data?: FlowData\n /** The node's size instead of the type's `size`. */\n size?: { width?: number; height?: number }\n}\n\n/** Where a port's centre is, from its node's top left corner. */\nexport interface PortAnchor extends Point {\n side: PortSide\n}\n\nexport interface NodeMetrics {\n width: number\n height: number\n ports: Map<string, PortAnchor>\n}\n\n/** What `previewNode` can show changed: the box, and the data. */\nexport type FlowNodePreview = Partial<Pick<FlowNode, 'x' | 'y' | 'width' | 'height' | 'data'>>\n\n/** How a port shows while an edge is being drawn. */\nexport type PortState = 'active' | 'valid' | 'candidate' | 'refused'\n\nexport interface FlowEditorContext {\n doc: Ref<FlowDocument>\n selection: Ref<FlowSelection>\n viewport: Ref<FlowViewport>\n /** See `FlowEditorExpose.setViewport`; writing `viewport` goes anywhere. */\n setViewport: FlowEditorExpose['setViewport']\n readonly: ComputedRef<boolean>\n messages: ComputedRef<FlowMessages>\n minZoom: ComputedRef<number>\n maxZoom: ComputedRef<number>\n transact: FlowEditorExpose['transact']\n undo: () => boolean\n redo: () => boolean\n canUndo: Ref<boolean>\n canRedo: Ref<boolean>\n fit: FlowEditorExpose['fit']\n zoomTo: FlowEditorExpose['zoomTo']\n zoomIn: () => void\n zoomOut: () => void\n /** The registered type of a port, if any. */\n portTypeOf: (port: FlowPortSpec) => PortTypeSpec | undefined\n /** A port type's color as CSS. */\n colorOf: (spec: PortTypeSpec | undefined) => string | undefined\n getNodeRect: FlowEditorExpose['getNodeRect']\n exportSVG: FlowEditorExpose['exportSVG']\n exportPNG: FlowEditorExpose['exportPNG']\n tidy: FlowEditorExpose['tidy']\n centerNode: FlowEditorExpose['centerNode']\n /** Says `text` to screen readers. */\n announce: (text: string) => void\n /** The editor's size on screen, in px. */\n size: Ref<{ width: number; height: number }>\n selectedNodes: ComputedRef<Set<string>>\n insertNode: FlowEditorExpose['insertNode']\n /** Node types with a `palette` entry, in the order they were given. */\n paletteTypes: ComputedRef<NodeTypeSpec[]>\n startInsertDrag: FlowEditorExpose['startInsertDrag']\n editLabel: FlowEditorExpose['editLabel']\n groupNodes: FlowEditorExpose['groupNodes']\n ungroupNode: FlowEditorExpose['ungroupNode']\n leaveGroup: FlowEditorExpose['leaveGroup']\n /** Which node is inside which, from the nodes' `parent`. */\n tree: ComputedRef<FlowTree>\n /** Whether the node's type can hold other nodes. */\n isContainer: (node: FlowNode) => boolean\n /** How far the node is drawn turned about its centre, from its type's `rotation`. */\n nodeRotation: (id: string) => number\n /**\n * While nodes are moved or resized, or an edge is bent: the document\n * changes on every frame until the pointer is let go.\n */\n gesturing: ComputedRef<boolean>\n /**\n * Shows a node as if `patch` were applied, without changing the document:\n * its frame, ports, edges and `getNodeRect` all follow. For handles that\n * change the document once, when let go; `null` shows it as it is again,\n * and must be passed when the drag ends or is cancelled. `patch.data`\n * replaces the node's data, it is not merged. Only this editor shows it:\n * peers see the change once it is committed. A preview is dropped when\n * its node leaves the document.\n */\n previewNode: (id: string, patch: FlowNodePreview | null) => void\n /** The edge being drawn, as an SVG path in canvas coordinates. */\n connectingPath: ComputedRef<string | null>\n /** The editor's element. */\n root: Readonly<ShallowRef<HTMLElement | null>>\n /**\n * Opens the context menu for a target at a point on screen; `false` when\n * no extension has items for it. Pass the button that opens it as `from`:\n * the menu then opens below the button instead of at the point, or above\n * it when there is no room, and pressing the button again closes it.\n */\n openMenu: (target: FlowMenuTarget, client: Point, from?: Element) => boolean\n /** The items the context menu has for a target, as `openMenu` shows them. */\n menuItems: (target: FlowMenuTarget) => ContextMenuItem[]\n /**\n * Opens the editor's menu with these items, e.g. the choices behind a\n * toolbar button; `from` works as in `openMenu`.\n */\n showMenu: (\n items: ContextMenuItem[],\n client: Point,\n from?: Element,\n options?: { focusChecked?: boolean },\n ) => boolean\n /** The element that opened the menu, while it is open, e.g. to mark a button expanded. */\n menuFrom: Readonly<ShallowRef<Element | null>>\n toCanvas: (point: Point) => Point\n /** Copies the selected nodes to the clipboard; `false` when none are selected. */\n copy: () => boolean\n cut: () => boolean\n /** Pastes what was copied, at `at` (canvas coordinates) or next to the original. */\n paste: (at?: Point) => Promise<boolean>\n /** Adds a copy of the selected nodes next to them. */\n duplicate: () => void\n /** Deletes the selected nodes and edges, except locked nodes. */\n deleteSelection: () => void\n selectAll: () => void\n}\n\nexport const flowEditorKey: InjectionKey<FlowEditorContext> = Symbol('dou-flow-editor')\n\n/** The surrounding flow editor; for controls and panels placed in its default slot. */\nexport function useFlowEditor() {\n const context = inject(flowEditorKey, null)\n if (!context)\n throw new Error('[@dou-ui/flow] useFlowEditor() must be called inside <DouFlowEditor>')\n return context\n}\n\nexport interface FlowNodeContext {\n node: ComputedRef<FlowNode>\n ports: ComputedRef<FlowPortSpec[]>\n /** Port ids with at least one edge. */\n connected: ComputedRef<Set<string>>\n /** While an edge is being drawn, how each port shows; `null` otherwise. */\n portStates: ComputedRef<Map<string, PortState> | null>\n registerPort: (id: string, el: HTMLElement, auto?: boolean) => void\n unregisterPort: (id: string, auto?: boolean) => void\n}\n\nexport const flowNodeKey: InjectionKey<FlowNodeContext> = Symbol('dou-flow-node')\n"],"mappings":";;AAoXA,IAAa,gBAAiD,OAAO,iBAAiB;;AAGtF,SAAgB,gBAAgB;CAC9B,MAAM,UAAU,OAAO,eAAe,IAAI;CAC1C,IAAI,CAAC,SACH,MAAM,IAAI,MAAM,sEAAsE;CACxF,OAAO;AACT;AAaA,IAAa,cAA6C,OAAO,eAAe"}
|
package/dist/core/edge-types.js
CHANGED
|
@@ -54,10 +54,66 @@ function bezierPath(from, fromSide, to, toSide, min = 40) {
|
|
|
54
54
|
}, min);
|
|
55
55
|
return `M${fmt(from)}C${fmt(c1)} ${fmt(c2)} ${fmt(to)}`;
|
|
56
56
|
}
|
|
57
|
+
/** The point halfway along a run of points: a middle one, or between the two middle ones. */
|
|
58
|
+
function middle(points) {
|
|
59
|
+
const a = points[Math.floor((points.length - 1) / 2)];
|
|
60
|
+
const b = points[Math.ceil((points.length - 1) / 2)];
|
|
61
|
+
return {
|
|
62
|
+
x: (a.x + b.x) / 2,
|
|
63
|
+
y: (a.y + b.y) / 2
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* The side a run between two points leaves the first by and the side it
|
|
68
|
+
* enters the second by, along the axis the flow runs in: the one the
|
|
69
|
+
* source's port faces.
|
|
70
|
+
*/
|
|
71
|
+
function sides(a, b, vertical) {
|
|
72
|
+
if (vertical) return b.y >= a.y ? ["bottom", "top"] : ["top", "bottom"];
|
|
73
|
+
return b.x >= a.x ? ["right", "left"] : ["left", "right"];
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* A curve through the points an edge runs through, as tidying lays them:
|
|
77
|
+
* straight along a rank, curving across the gaps between ranks.
|
|
78
|
+
*/
|
|
79
|
+
function bezierThrough(source, target, points) {
|
|
80
|
+
const vertical = isVertical(source.side);
|
|
81
|
+
const all = [
|
|
82
|
+
source,
|
|
83
|
+
...points.map((p) => ({
|
|
84
|
+
...p,
|
|
85
|
+
side: source.side
|
|
86
|
+
})),
|
|
87
|
+
target
|
|
88
|
+
];
|
|
89
|
+
let path = `M${fmt(source)}`;
|
|
90
|
+
for (let i = 1; i < all.length; i++) {
|
|
91
|
+
const a = all[i - 1];
|
|
92
|
+
const b = all[i];
|
|
93
|
+
if ((vertical ? Math.abs(a.x - b.x) < .5 : Math.abs(a.y - b.y) < .5) && i > 1 && i < all.length - 1) {
|
|
94
|
+
path += `L${fmt(b)}`;
|
|
95
|
+
continue;
|
|
96
|
+
}
|
|
97
|
+
const [out, into] = sides(a, b, vertical);
|
|
98
|
+
const [c1, c2] = bezierControls({
|
|
99
|
+
...a,
|
|
100
|
+
side: i === 1 ? source.side : out
|
|
101
|
+
}, {
|
|
102
|
+
...b,
|
|
103
|
+
side: i === all.length - 1 ? target.side : into
|
|
104
|
+
}, 0);
|
|
105
|
+
path += `C${fmt(c1)} ${fmt(c2)} ${fmt(b)}`;
|
|
106
|
+
}
|
|
107
|
+
return {
|
|
108
|
+
path,
|
|
109
|
+
label: middle(points)
|
|
110
|
+
};
|
|
111
|
+
}
|
|
57
112
|
/** The default: a smooth curve out of and into the ports' sides. */
|
|
58
113
|
var bezierEdge = defineEdgeType({
|
|
59
114
|
type: "bezier",
|
|
60
|
-
route(source, target) {
|
|
115
|
+
route(source, target, edge) {
|
|
116
|
+
if (edge?.points?.length) return bezierThrough(source, target, edge.points);
|
|
61
117
|
const [c1, c2] = bezierControls(source, target);
|
|
62
118
|
return {
|
|
63
119
|
path: `M${fmt(source)}C${fmt(c1)} ${fmt(c2)} ${fmt(target)}`,
|
|
@@ -70,13 +126,20 @@ var bezierEdge = defineEdgeType({
|
|
|
70
126
|
});
|
|
71
127
|
var straightEdge = defineEdgeType({
|
|
72
128
|
type: "straight",
|
|
73
|
-
route: (source, target) =>
|
|
129
|
+
route: (source, target, edge) => edge?.points?.length ? {
|
|
130
|
+
path: `M${[
|
|
131
|
+
source,
|
|
132
|
+
...edge.points,
|
|
133
|
+
target
|
|
134
|
+
].map(fmt).join("L")}`,
|
|
135
|
+
label: middle(edge.points)
|
|
136
|
+
} : {
|
|
74
137
|
path: `M${fmt(source)}L${fmt(target)}`,
|
|
75
138
|
label: {
|
|
76
139
|
x: (source.x + target.x) / 2,
|
|
77
140
|
y: (source.y + target.y) / 2
|
|
78
141
|
}
|
|
79
|
-
}
|
|
142
|
+
}
|
|
80
143
|
});
|
|
81
144
|
/** How far a step edge runs out of a port before its first turn. */
|
|
82
145
|
var STUB = 20;
|
|
@@ -130,6 +193,7 @@ var flipSide = {
|
|
|
130
193
|
var stepEdge = defineEdgeType({
|
|
131
194
|
type: "step",
|
|
132
195
|
route(source, target, edge) {
|
|
196
|
+
if (edge?.points?.length) return stepThrough(source, target, edge.points);
|
|
133
197
|
const turned = isVertical(source.side) && isVertical(target.side);
|
|
134
198
|
const s = turned ? {
|
|
135
199
|
...flip(source),
|
|
@@ -235,6 +299,46 @@ var stepEdge = defineEdgeType({
|
|
|
235
299
|
};
|
|
236
300
|
}
|
|
237
301
|
});
|
|
302
|
+
/**
|
|
303
|
+
* Right angles through the points an edge runs through: along each rank,
|
|
304
|
+
* across each gap between ranks halfway through it.
|
|
305
|
+
*/
|
|
306
|
+
function stepThrough(source, target, points) {
|
|
307
|
+
const vertical = isVertical(source.side);
|
|
308
|
+
const all = [
|
|
309
|
+
source,
|
|
310
|
+
...points,
|
|
311
|
+
target
|
|
312
|
+
];
|
|
313
|
+
const out = [source];
|
|
314
|
+
for (let i = 1; i < all.length; i++) {
|
|
315
|
+
const a = all[i - 1];
|
|
316
|
+
const b = all[i];
|
|
317
|
+
if (vertical) {
|
|
318
|
+
const y = (a.y + b.y) / 2;
|
|
319
|
+
out.push({
|
|
320
|
+
x: a.x,
|
|
321
|
+
y
|
|
322
|
+
}, {
|
|
323
|
+
x: b.x,
|
|
324
|
+
y
|
|
325
|
+
}, b);
|
|
326
|
+
} else {
|
|
327
|
+
const x = (a.x + b.x) / 2;
|
|
328
|
+
out.push({
|
|
329
|
+
x,
|
|
330
|
+
y: a.y
|
|
331
|
+
}, {
|
|
332
|
+
x,
|
|
333
|
+
y: b.y
|
|
334
|
+
}, b);
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
return {
|
|
338
|
+
path: rounded(out),
|
|
339
|
+
label: middle(points)
|
|
340
|
+
};
|
|
341
|
+
}
|
|
238
342
|
export { bezierEdge, bezierPath, defineEdgeType, stepEdge, straightEdge };
|
|
239
343
|
|
|
240
344
|
//# sourceMappingURL=edge-types.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"edge-types.js","names":[],"sources":["../../src/core/edge-types.ts"],"sourcesContent":["import type { CSSProperties } from 'vue'\nimport type { FlowData, FlowEdge, Point, PortSide } from './model'\n\n/** Where an edge leaves or enters a node: a point and the side it faces. */\nexport interface EdgeAnchor extends Point {\n side: PortSide\n}\n\nexport interface EdgeRoute {\n /** The SVG path, in canvas coordinates. */\n path: string\n /** Where the label sits. */\n label: Point\n /**\n * A handle shown on the selected edge that moves part of it, e.g. the\n * middle of a step edge. `axis` is the way it moves; `move` turns the\n * pointer's canvas point into a patch for `edge.data`.\n */\n handle?: { at: Point; axis: 'x' | 'y'; move: (point: Point) => FlowData }\n /**\n * Marks drawn with the edge's stroke but never dashed, e.g. arrowheads:\n * an SVG path in canvas coordinates.\n */\n marks?: string\n}\n\nexport interface EdgeTypeSpec<Data extends FlowData = FlowData> {\n type: string\n /** The edge's path between two anchors. `edge` is absent while one is being drawn. */\n route: (source: EdgeAnchor, target: EdgeAnchor, edge?: FlowEdge<Data>) => EdgeRoute\n /** The edge's own look, e.g. `strokeDasharray`, over what its port type gives it. */\n style?: (edge: FlowEdge<Data>) => CSSProperties | undefined\n}\n\n/** Declares an edge type, with `edge.data` typed. */\nexport function defineEdgeType<Data extends FlowData = FlowData>(\n spec: EdgeTypeSpec<Data>,\n): EdgeTypeSpec<Data> {\n return spec\n}\n\nconst direction: Record<PortSide, Point> = {\n left: { x: -1, y: 0 },\n right: { x: 1, y: 0 },\n top: { x: 0, y: -1 },\n bottom: { x: 0, y: 1 },\n}\n\nconst round = (value: number) => Math.round(value * 100) / 100\nconst fmt = (p: Point) => `${round(p.x)} ${round(p.y)}`\nconst isVertical = (side: PortSide) => side === 'top' || side === 'bottom'\n\n/**\n * A cubic curve leaving `from` and entering `to` straight out of their\n * sides. The handles are equally long, half the distance along the axis\n * they point in (at least `min`), so a fan of edges from one port is\n * mirror-symmetric and a straight run stays a straight line.\n */\nfunction bezierControls(from: EdgeAnchor, to: EdgeAnchor, min = 40) {\n const a = direction[from.side]\n const b = direction[to.side]\n const span = (dir: Point) => Math.abs(dir.x ? to.x - from.x : to.y - from.y)\n const lenA = Math.max(min, span(a) / 2)\n const lenB = Math.max(min, span(b) / 2)\n return [\n { x: from.x + a.x * lenA, y: from.y + a.y * lenA },\n { x: to.x + b.x * lenB, y: to.y + b.y * lenB },\n ] as const\n}\n\nexport function bezierPath(from: Point, fromSide: PortSide, to: Point, toSide: PortSide, min = 40) {\n const [c1, c2] = bezierControls({ ...from, side: fromSide }, { ...to, side: toSide }, min)\n return `M${fmt(from)}C${fmt(c1)} ${fmt(c2)} ${fmt(to)}`\n}\n\n/** The default: a smooth curve out of and into the ports' sides. */\nexport const bezierEdge = defineEdgeType({\n type: 'bezier',\n route(source, target) {\n const [c1, c2] = bezierControls(source, target)\n return {\n path: `M${fmt(source)}C${fmt(c1)} ${fmt(c2)} ${fmt(target)}`,\n // The curve at its middle.\n label: {\n x: (source.x + 3 * c1.x + 3 * c2.x + target.x) / 8,\n y: (source.y + 3 * c1.y + 3 * c2.y + target.y) / 8,\n },\n }\n },\n})\n\nexport const straightEdge = defineEdgeType({\n type: 'straight',\n route: (source, target) => ({\n path: `M${fmt(source)}L${fmt(target)}`,\n label: { x: (source.x + target.x) / 2, y: (source.y + target.y) / 2 },\n }),\n})\n\n/** How far a step edge runs out of a port before its first turn. */\nconst STUB = 20\nconst RADIUS = 6\n\n/** A polyline with its corners rounded. */\nfunction rounded(points: Point[], radius = RADIUS) {\n // Drop repeated points and points in the middle of a straight run.\n const pts: Point[] = []\n for (const p of points) {\n const last = pts[pts.length - 1]\n if (last && Math.abs(last.x - p.x) < 0.01 && Math.abs(last.y - p.y) < 0.01) continue\n const before = pts[pts.length - 2]\n if (\n before &&\n last &&\n ((Math.abs(before.x - last.x) < 0.01 && Math.abs(last.x - p.x) < 0.01) ||\n (Math.abs(before.y - last.y) < 0.01 && Math.abs(last.y - p.y) < 0.01))\n )\n pts.pop()\n pts.push(p)\n }\n let d = `M${fmt(pts[0]!)}`\n for (let i = 1; i < pts.length - 1; i++) {\n const [a, b, c] = [pts[i - 1]!, pts[i]!, pts[i + 1]!]\n const lenIn = Math.hypot(b.x - a.x, b.y - a.y)\n const lenOut = Math.hypot(c.x - b.x, c.y - b.y)\n const r = Math.min(radius, lenIn / 2, lenOut / 2)\n const p1 = { x: b.x - ((b.x - a.x) / lenIn) * r, y: b.y - ((b.y - a.y) / lenIn) * r }\n const p2 = { x: b.x + ((c.x - b.x) / lenOut) * r, y: b.y + ((c.y - b.y) / lenOut) * r }\n d += `L${fmt(p1)}Q${fmt(b)} ${fmt(p2)}`\n }\n return d + `L${fmt(pts[pts.length - 1]!)}`\n}\n\nconst flip = (p: Point): Point => ({ x: p.y, y: p.x })\nconst flipSide: Record<PortSide, PortSide> = {\n left: 'top',\n right: 'bottom',\n top: 'left',\n bottom: 'right',\n}\n\ninterface StepData extends FlowData {\n /** Where the middle segment sits between the two ends, from 0 to 1. */\n bend?: number\n}\n\n/**\n * Right angles with rounded corners. Between ports that face each other the\n * middle segment can be dragged; its place is kept in `edge.data.bend`.\n */\nexport const stepEdge = defineEdgeType<StepData>({\n type: 'step',\n route(source, target, edge) {\n // Work as if the ports faced left and right, then turn the result back.\n const turned = isVertical(source.side) && isVertical(target.side)\n const s = turned ? { ...flip(source), side: flipSide[source.side] } : source\n const t = turned ? { ...flip(target), side: flipSide[target.side] } : target\n const back = (p: Point) => (turned ? flip(p) : p)\n const ds = direction[s.side]\n const dt = direction[t.side]\n const s1 = { x: s.x + ds.x * STUB, y: s.y + ds.y * STUB }\n const t1 = { x: t.x + dt.x * STUB, y: t.y + dt.y * STUB }\n let points: Point[]\n let label: Point\n let handle: EdgeRoute['handle']\n\n if (ds.x && dt.x && ds.x === -dt.x && (t.x - s.x) * ds.x > 0) {\n // Facing each other: across, along, across.\n const bend = Math.min(0.95, Math.max(0.05, edge?.data?.bend ?? 0.5))\n const mid = s.x + (t.x - s.x) * bend\n points = [s, { x: mid, y: s.y }, { x: mid, y: t.y }, t]\n label = { x: mid, y: (s.y + t.y) / 2 }\n // No handle when the ends line up: the middle segment has no length.\n if (t.x !== s.x && Math.abs(t.y - s.y) >= 1) {\n const span = t.x - s.x\n const at = back(label)\n handle = {\n at,\n axis: turned ? 'y' : 'x',\n move: (point) => {\n const v = turned ? point.y : point.x\n return { bend: Math.min(0.95, Math.max(0.05, (v - s.x) / span)) }\n },\n }\n }\n } else if (ds.x && dt.x) {\n // Facing away, or the same way: out, around through the middle, in.\n const gap = Math.abs(s.y - t.y)\n const my = gap >= STUB * 2 ? (s.y + t.y) / 2 : Math.max(s.y, t.y) + STUB * 2\n const x1 = ds.x === dt.x ? (ds.x > 0 ? Math.max(s1.x, t1.x) : Math.min(s1.x, t1.x)) : s1.x\n const x2 = ds.x === dt.x ? x1 : t1.x\n points = [s, { x: x1, y: s.y }, { x: x1, y: my }, { x: x2, y: my }, { x: x2, y: t.y }, t]\n label = { x: (x1 + x2) / 2, y: my }\n } else {\n // One across, one along: a single corner when the ends allow it.\n const corner = ds.x ? { x: t1.x, y: s1.y } : { x: s1.x, y: t1.y }\n points = [s, s1, corner, t1, t]\n label = corner\n }\n return { path: rounded(points.map(back)), label: back(label), handle }\n },\n})\n"],"mappings":";;AAmCA,SAAgB,eACd,MACoB;CACpB,OAAO;AACT;AAEA,IAAM,YAAqC;CACzC,MAAM;EAAE,GAAG;EAAI,GAAG;CAAE;CACpB,OAAO;EAAE,GAAG;EAAG,GAAG;CAAE;CACpB,KAAK;EAAE,GAAG;EAAG,GAAG;CAAG;CACnB,QAAQ;EAAE,GAAG;EAAG,GAAG;CAAE;AACvB;AAEA,IAAM,SAAS,UAAkB,KAAK,MAAM,QAAQ,GAAG,IAAI;AAC3D,IAAM,OAAO,MAAa,GAAG,MAAM,EAAE,CAAC,EAAE,GAAG,MAAM,EAAE,CAAC;AACpD,IAAM,cAAc,SAAmB,SAAS,SAAS,SAAS;;;;;;;AAQlE,SAAS,eAAe,MAAkB,IAAgB,MAAM,IAAI;CAClE,MAAM,IAAI,UAAU,KAAK;CACzB,MAAM,IAAI,UAAU,GAAG;CACvB,MAAM,QAAQ,QAAe,KAAK,IAAI,IAAI,IAAI,GAAG,IAAI,KAAK,IAAI,GAAG,IAAI,KAAK,CAAC;CAC3E,MAAM,OAAO,KAAK,IAAI,KAAK,KAAK,CAAC,IAAI,CAAC;CACtC,MAAM,OAAO,KAAK,IAAI,KAAK,KAAK,CAAC,IAAI,CAAC;CACtC,OAAO,CACL;EAAE,GAAG,KAAK,IAAI,EAAE,IAAI;EAAM,GAAG,KAAK,IAAI,EAAE,IAAI;CAAK,GACjD;EAAE,GAAG,GAAG,IAAI,EAAE,IAAI;EAAM,GAAG,GAAG,IAAI,EAAE,IAAI;CAAK,CAC/C;AACF;AAEA,SAAgB,WAAW,MAAa,UAAo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U;MACf,MAAM,IAAI,SAAS,MAAM,IAAI,MAAM;MACnC,OAAO,EAAE,MAAM,KAAK,IAAI,KAAM,KAAK,IAAI,MAAO,IAAI,EAAE,KAAK,IAAI,CAAC,EAAE;KAClE;IACF;GACF;EACF,OAAO,IAAI,GAAG,KAAK,GAAG,GAAG;GAGvB,MAAM,KADM,KAAK,IAAI,EAAE,IAAI,EAAE,CAClB,KAAO,MAAY,EAAE,IAAI,EAAE,KAAK,IAAI,KAAK,IAAI,EAAE,GAAG,EAAE,CAAC,IAAI;GACpE,MAAM,KAAK,GAAG,MAAM,GAAG,IAAK,GAAG,IAAI,IAAI,KAAK,IAAI,GAAG,GAAG,GAAG,CAAC,IAAI,KAAK,IAAI,GAAG,GAAG,GAAG,CAAC,IAAK,GAAG;GACzF,MAAM,KAAK,GAAG,MAAM,GAAG,IAAI,KAAK,GAAG;GACnC,SAAS;IAAC;IAAG;KAAE,GAAG;KAAI,GAAG,EAAE;IAAE;IAAG;KAAE,GAAG;KAAI,GAAG;IAAG;IAAG;KAAE,GAAG;KAAI,GAAG;IAAG;IAAG;KAAE,GAAG;KAAI,GAAG,EAAE;IAAE;IAAG;GAAC;GACxF,QAAQ;IAAE,IAAI,KAAK,MAAM;IAAG,GAAG;GAAG;EACpC,OAAO;GAEL,MAAM,SAAS,GAAG,IAAI;IAAE,GAAG,GAAG;IAAG,GAAG,GAAG;GAAE,IAAI;IAAE,GAAG,GAAG;IAAG,GAAG,GAAG;GAAE;GAChE,SAAS;IAAC;IAAG;IAAI;IAAQ;IAAI;GAAC;GAC9B,QAAQ;EACV;EACA,OAAO;GAAE,MAAM,QAAQ,OAAO,IAAI,IAAI,CAAC;GAAG,OAAO,KAAK,KAAK;GAAG;EAAO;CACvE;AACF,CAAC"}
|
|
1
|
+
{"version":3,"file":"edge-types.js","names":[],"sources":["../../src/core/edge-types.ts"],"sourcesContent":["import type { CSSProperties } from 'vue'\nimport type { FlowData, FlowEdge, Point, PortSide } from './model'\n\n/** Where an edge leaves or enters a node: a point and the side it faces. */\nexport interface EdgeAnchor extends Point {\n side: PortSide\n}\n\nexport interface EdgeRoute {\n /** The SVG path, in canvas coordinates. */\n path: string\n /** Where the label sits. */\n label: Point\n /**\n * A handle shown on the selected edge that moves part of it, e.g. the\n * middle of a step edge. `axis` is the way it moves; `move` turns the\n * pointer's canvas point into a patch for `edge.data`.\n */\n handle?: { at: Point; axis: 'x' | 'y'; move: (point: Point) => FlowData }\n /**\n * Marks drawn with the edge's stroke but never dashed, e.g. arrowheads:\n * an SVG path in canvas coordinates.\n */\n marks?: string\n}\n\nexport interface EdgeTypeSpec<Data extends FlowData = FlowData> {\n type: string\n /** The edge's path between two anchors. `edge` is absent while one is being drawn. */\n route: (source: EdgeAnchor, target: EdgeAnchor, edge?: FlowEdge<Data>) => EdgeRoute\n /** The edge's own look, e.g. `strokeDasharray`, over what its port type gives it. */\n style?: (edge: FlowEdge<Data>) => CSSProperties | undefined\n}\n\n/** Declares an edge type, with `edge.data` typed. */\nexport function defineEdgeType<Data extends FlowData = FlowData>(\n spec: EdgeTypeSpec<Data>,\n): EdgeTypeSpec<Data> {\n return spec\n}\n\nconst direction: Record<PortSide, Point> = {\n left: { x: -1, y: 0 },\n right: { x: 1, y: 0 },\n top: { x: 0, y: -1 },\n bottom: { x: 0, y: 1 },\n}\n\nconst round = (value: number) => Math.round(value * 100) / 100\nconst fmt = (p: Point) => `${round(p.x)} ${round(p.y)}`\nconst isVertical = (side: PortSide) => side === 'top' || side === 'bottom'\n\n/**\n * A cubic curve leaving `from` and entering `to` straight out of their\n * sides. The handles are equally long, half the distance along the axis\n * they point in (at least `min`), so a fan of edges from one port is\n * mirror-symmetric and a straight run stays a straight line.\n */\nfunction bezierControls(from: EdgeAnchor, to: EdgeAnchor, min = 40) {\n const a = direction[from.side]\n const b = direction[to.side]\n const span = (dir: Point) => Math.abs(dir.x ? to.x - from.x : to.y - from.y)\n const lenA = Math.max(min, span(a) / 2)\n const lenB = Math.max(min, span(b) / 2)\n return [\n { x: from.x + a.x * lenA, y: from.y + a.y * lenA },\n { x: to.x + b.x * lenB, y: to.y + b.y * lenB },\n ] as const\n}\n\nexport function bezierPath(from: Point, fromSide: PortSide, to: Point, toSide: PortSide, min = 40) {\n const [c1, c2] = bezierControls({ ...from, side: fromSide }, { ...to, side: toSide }, min)\n return `M${fmt(from)}C${fmt(c1)} ${fmt(c2)} ${fmt(to)}`\n}\n\n/** The point halfway along a run of points: a middle one, or between the two middle ones. */\nfunction middle(points: Point[]): Point {\n const a = points[Math.floor((points.length - 1) / 2)]!\n const b = points[Math.ceil((points.length - 1) / 2)]!\n return { x: (a.x + b.x) / 2, y: (a.y + b.y) / 2 }\n}\n\n/**\n * The side a run between two points leaves the first by and the side it\n * enters the second by, along the axis the flow runs in: the one the\n * source's port faces.\n */\nfunction sides(a: Point, b: Point, vertical: boolean): [PortSide, PortSide] {\n if (vertical) return b.y >= a.y ? ['bottom', 'top'] : ['top', 'bottom']\n return b.x >= a.x ? ['right', 'left'] : ['left', 'right']\n}\n\n/**\n * A curve through the points an edge runs through, as tidying lays them:\n * straight along a rank, curving across the gaps between ranks.\n */\nfunction bezierThrough(source: EdgeAnchor, target: EdgeAnchor, points: Point[]): EdgeRoute {\n const vertical = isVertical(source.side)\n const all: EdgeAnchor[] = [source, ...points.map((p) => ({ ...p, side: source.side })), target]\n let path = `M${fmt(source)}`\n for (let i = 1; i < all.length; i++) {\n const a = all[i - 1]!\n const b = all[i]!\n const level = vertical ? Math.abs(a.x - b.x) < 0.5 : Math.abs(a.y - b.y) < 0.5\n if (level && i > 1 && i < all.length - 1) {\n path += `L${fmt(b)}`\n continue\n }\n const [out, into] = sides(a, b, vertical)\n const [c1, c2] = bezierControls(\n { ...a, side: i === 1 ? source.side : out },\n { ...b, side: i === all.length - 1 ? target.side : into },\n 0,\n )\n path += `C${fmt(c1)} ${fmt(c2)} ${fmt(b)}`\n }\n return { path, label: middle(points) }\n}\n\n/** The default: a smooth curve out of and into the ports' sides. */\nexport const bezierEdge = defineEdgeType({\n type: 'bezier',\n route(source, target, edge) {\n if (edge?.points?.length) return bezierThrough(source, target, edge.points)\n const [c1, c2] = bezierControls(source, target)\n return {\n path: `M${fmt(source)}C${fmt(c1)} ${fmt(c2)} ${fmt(target)}`,\n // The curve at its middle.\n label: {\n x: (source.x + 3 * c1.x + 3 * c2.x + target.x) / 8,\n y: (source.y + 3 * c1.y + 3 * c2.y + target.y) / 8,\n },\n }\n },\n})\n\nexport const straightEdge = defineEdgeType({\n type: 'straight',\n route: (source, target, edge) =>\n edge?.points?.length\n ? {\n path: `M${[source, ...edge.points, target].map(fmt).join('L')}`,\n label: middle(edge.points),\n }\n : {\n path: `M${fmt(source)}L${fmt(target)}`,\n label: { x: (source.x + target.x) / 2, y: (source.y + target.y) / 2 },\n },\n})\n\n/** How far a step edge runs out of a port before its first turn. */\nconst STUB = 20\nconst RADIUS = 6\n\n/** A polyline with its corners rounded. */\nfunction rounded(points: Point[], radius = RADIUS) {\n // Drop repeated points and points in the middle of a straight run.\n const pts: Point[] = []\n for (const p of points) {\n const last = pts[pts.length - 1]\n if (last && Math.abs(last.x - p.x) < 0.01 && Math.abs(last.y - p.y) < 0.01) continue\n const before = pts[pts.length - 2]\n if (\n before &&\n last &&\n ((Math.abs(before.x - last.x) < 0.01 && Math.abs(last.x - p.x) < 0.01) ||\n (Math.abs(before.y - last.y) < 0.01 && Math.abs(last.y - p.y) < 0.01))\n )\n pts.pop()\n pts.push(p)\n }\n let d = `M${fmt(pts[0]!)}`\n for (let i = 1; i < pts.length - 1; i++) {\n const [a, b, c] = [pts[i - 1]!, pts[i]!, pts[i + 1]!]\n const lenIn = Math.hypot(b.x - a.x, b.y - a.y)\n const lenOut = Math.hypot(c.x - b.x, c.y - b.y)\n const r = Math.min(radius, lenIn / 2, lenOut / 2)\n const p1 = { x: b.x - ((b.x - a.x) / lenIn) * r, y: b.y - ((b.y - a.y) / lenIn) * r }\n const p2 = { x: b.x + ((c.x - b.x) / lenOut) * r, y: b.y + ((c.y - b.y) / lenOut) * r }\n d += `L${fmt(p1)}Q${fmt(b)} ${fmt(p2)}`\n }\n return d + `L${fmt(pts[pts.length - 1]!)}`\n}\n\nconst flip = (p: Point): Point => ({ x: p.y, y: p.x })\nconst flipSide: Record<PortSide, PortSide> = {\n left: 'top',\n right: 'bottom',\n top: 'left',\n bottom: 'right',\n}\n\ninterface StepData extends FlowData {\n /** Where the middle segment sits between the two ends, from 0 to 1. */\n bend?: number\n}\n\n/**\n * Right angles with rounded corners. Between ports that face each other the\n * middle segment can be dragged; its place is kept in `edge.data.bend`.\n */\nexport const stepEdge = defineEdgeType<StepData>({\n type: 'step',\n route(source, target, edge) {\n if (edge?.points?.length) return stepThrough(source, target, edge.points)\n // Work as if the ports faced left and right, then turn the result back.\n const turned = isVertical(source.side) && isVertical(target.side)\n const s = turned ? { ...flip(source), side: flipSide[source.side] } : source\n const t = turned ? { ...flip(target), side: flipSide[target.side] } : target\n const back = (p: Point) => (turned ? flip(p) : p)\n const ds = direction[s.side]\n const dt = direction[t.side]\n const s1 = { x: s.x + ds.x * STUB, y: s.y + ds.y * STUB }\n const t1 = { x: t.x + dt.x * STUB, y: t.y + dt.y * STUB }\n let points: Point[]\n let label: Point\n let handle: EdgeRoute['handle']\n\n if (ds.x && dt.x && ds.x === -dt.x && (t.x - s.x) * ds.x > 0) {\n // Facing each other: across, along, across.\n const bend = Math.min(0.95, Math.max(0.05, edge?.data?.bend ?? 0.5))\n const mid = s.x + (t.x - s.x) * bend\n points = [s, { x: mid, y: s.y }, { x: mid, y: t.y }, t]\n label = { x: mid, y: (s.y + t.y) / 2 }\n // No handle when the ends line up: the middle segment has no length.\n if (t.x !== s.x && Math.abs(t.y - s.y) >= 1) {\n const span = t.x - s.x\n const at = back(label)\n handle = {\n at,\n axis: turned ? 'y' : 'x',\n move: (point) => {\n const v = turned ? point.y : point.x\n return { bend: Math.min(0.95, Math.max(0.05, (v - s.x) / span)) }\n },\n }\n }\n } else if (ds.x && dt.x) {\n // Facing away, or the same way: out, around through the middle, in.\n const gap = Math.abs(s.y - t.y)\n const my = gap >= STUB * 2 ? (s.y + t.y) / 2 : Math.max(s.y, t.y) + STUB * 2\n const x1 = ds.x === dt.x ? (ds.x > 0 ? Math.max(s1.x, t1.x) : Math.min(s1.x, t1.x)) : s1.x\n const x2 = ds.x === dt.x ? x1 : t1.x\n points = [s, { x: x1, y: s.y }, { x: x1, y: my }, { x: x2, y: my }, { x: x2, y: t.y }, t]\n label = { x: (x1 + x2) / 2, y: my }\n } else {\n // One across, one along: a single corner when the ends allow it.\n const corner = ds.x ? { x: t1.x, y: s1.y } : { x: s1.x, y: t1.y }\n points = [s, s1, corner, t1, t]\n label = corner\n }\n return { path: rounded(points.map(back)), label: back(label), handle }\n },\n})\n\n/**\n * Right angles through the points an edge runs through: along each rank,\n * across each gap between ranks halfway through it.\n */\nfunction stepThrough(source: EdgeAnchor, target: EdgeAnchor, points: Point[]): EdgeRoute {\n const vertical = isVertical(source.side)\n const all = [source, ...points, target]\n const out: Point[] = [source]\n for (let i = 1; i < all.length; i++) {\n const a = all[i - 1]!\n const b = all[i]!\n if (vertical) {\n const y = (a.y + b.y) / 2\n out.push({ x: a.x, y }, { x: b.x, y }, b)\n } else {\n const x = (a.x + b.x) / 2\n out.push({ x, y: a.y }, { x, y: b.y }, b)\n }\n }\n return { path: rounded(out), label: middle(points) }\n}\n"],"mappings":";;AAmCA,SAAgB,eACd,MACoB;CACpB,OAAO;AACT;AAEA,IAAM,YAAqC;CACzC,MAAM;EAAE,GAAG;EAAI,GAAG;CAAE;CACpB,OAAO;EAAE,GAAG;EAAG,GAAG;CAAE;CACpB,KAAK;EAAE,GAAG;EAAG,GAAG;CAAG;CACnB,QAAQ;EAAE,GAAG;EAAG,GAAG;CAAE;AACvB;AAEA,IAAM,SAAS,UAAkB,KAAK,MAAM,QAAQ,GAAG,IAAI;AAC3D,IAAM,OAAO,MAAa,GAAG,MAAM,EAAE,CAAC,EAAE,GAAG,MAAM,EAA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AAE,KAAK;GACxB,IAAI,KAAK;IAAE,GAAG,EAAE;IAAG;GAAE,GAAG;IAAE,GAAG,EAAE;IAAG;GAAE,GAAG,CAAC;EAC1C,OAAO;GACL,MAAM,KAAK,EAAE,IAAI,EAAE,KAAK;GACxB,IAAI,KAAK;IAAE;IAAG,GAAG,EAAE;GAAE,GAAG;IAAE;IAAG,GAAG,EAAE;GAAE,GAAG,CAAC;EAC1C;CACF;CACA,OAAO;EAAE,MAAM,QAAQ,GAAG;EAAG,OAAO,OAAO,MAAM;CAAE;AACrD"}
|
package/dist/core/model.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"model.js","names":[],"sources":["../../src/core/model.ts"],"sourcesContent":["/*\n * The document a flow editor edits: plain, serialisable data, bound with\n * `v-model`. Positions are canvas coordinates in px, from the top left of a\n * node. Ports are not stored; a node type declares them, and edges refer to\n * them by id.\n */\n\nexport type FlowData = Record<string, unknown>\n\nexport interface FlowNode<Data extends FlowData = FlowData> {\n id: string\n /** A registered node type; `'default'` when left out. */\n type?: string\n x: number\n y: number\n /** Only stored for nodes that can be resized; otherwise the content decides. */\n width?: number\n height?: number\n data?: Data\n /** A locked node cannot be moved or deleted. */\n locked?: boolean\n /**\n * The container node, e.g. a group, this node sits in. Its `x` and `y`\n * stay canvas coordinates; moving the container moves it along.\n */\n parent?: string\n /**\n * Stacking order among nodes that aren't containers: higher is drawn on\n * top. Nodes without it count as `0`; equal ones keep the document order.\n */\n z?: number\n}\n\nexport interface FlowEndpoint {\n node: string\n /** Leave out to attach to the node itself. */\n port?: string\n}\n\nexport interface FlowEdge<Data extends FlowData = FlowData> {\n id: string\n source: FlowEndpoint\n target: FlowEndpoint\n /** The edge type: `bezier`, `step`, `straight` or one added by an extension. The editor's `edge-type` when left out. */\n type?: string\n /** Text shown on the edge. */\n label?: string\n /** Dashes run along the edge in its direction, e.g. while data flows through it. */\n animated?: boolean\n data?: Data\n}\n\nexport interface FlowDocument {\n /** The version of the format; see `migrateDocument`. */\n version: 1\n nodes: FlowNode[]\n edges: FlowEdge[]\n}\n\nexport type PortSide = 'left' | 'right' | 'top' | 'bottom'\n\nexport interface FlowPortSpec {\n id: string\n /** Inputs receive edges, outputs start them. */\n kind: 'input' | 'output'\n /** Where the port sits; inputs default to the left, outputs to the right. */\n side?: PortSide\n label?: string\n /** What the port carries. Two ports connect when they share a type, or either is `'any'`. */\n dataType?: string | string[]\n /**\n * The most edges the port takes. Inputs take one by default, outputs any\n * number. A new edge on a full port replaces its oldest edge.\n */\n max?: number\n}\n\n/** Where the canvas is shown: `x` and `y` are the screen offset of the canvas origin. */\nexport interface FlowViewport {\n x: number\n y: number\n zoom: number\n}\n\nexport interface FlowSelection {\n nodes: string[]\n edges: string[]\n}\n\nexport interface Point {\n x: number\n y: number\n}\n\nexport interface Rect {\n x: number\n y: number\n width: number\n height: number\n}\n\nexport const emptyDocument = (): FlowDocument => ({ version: 1, nodes: [], edges: [] })\nexport const emptySelection = (): FlowSelection => ({ nodes: [], edges: [] })\n\nlet counter = 0\n\n/** A short id, unique within this page and very likely across clients. */\nexport function createId(prefix = 'n') {\n counter = (counter + 1) % 1296\n const time = Date.now().toString(36).slice(-5)\n const random = Math.random().toString(36).slice(2, 6)\n return `${prefix}${time}${counter.toString(36).padStart(2, '0')}${random}`\n}\n"],"mappings":";
|
|
1
|
+
{"version":3,"file":"model.js","names":[],"sources":["../../src/core/model.ts"],"sourcesContent":["/*\n * The document a flow editor edits: plain, serialisable data, bound with\n * `v-model`. Positions are canvas coordinates in px, from the top left of a\n * node. Ports are not stored; a node type declares them, and edges refer to\n * them by id.\n */\n\nexport type FlowData = Record<string, unknown>\n\nexport interface FlowNode<Data extends FlowData = FlowData> {\n id: string\n /** A registered node type; `'default'` when left out. */\n type?: string\n x: number\n y: number\n /** Only stored for nodes that can be resized; otherwise the content decides. */\n width?: number\n height?: number\n data?: Data\n /** A locked node cannot be moved or deleted. */\n locked?: boolean\n /**\n * The container node, e.g. a group, this node sits in. Its `x` and `y`\n * stay canvas coordinates; moving the container moves it along.\n */\n parent?: string\n /**\n * Stacking order among nodes that aren't containers: higher is drawn on\n * top. Nodes without it count as `0`; equal ones keep the document order.\n */\n z?: number\n}\n\nexport interface FlowEndpoint {\n node: string\n /** Leave out to attach to the node itself. */\n port?: string\n}\n\nexport interface FlowEdge<Data extends FlowData = FlowData> {\n id: string\n source: FlowEndpoint\n target: FlowEndpoint\n /** The edge type: `bezier`, `step`, `straight` or one added by an extension. The editor's `edge-type` when left out. */\n type?: string\n /** Text shown on the edge. */\n label?: string\n /** Dashes run along the edge in its direction, e.g. while data flows through it. */\n animated?: boolean\n /**\n * Points on the canvas the edge runs through on its way, in order. Tidying\n * sets them on an edge that spans several ranks, so it passes between\n * the nodes in its way; moving either end clears them.\n */\n points?: Point[]\n data?: Data\n}\n\nexport interface FlowDocument {\n /** The version of the format; see `migrateDocument`. */\n version: 1\n nodes: FlowNode[]\n edges: FlowEdge[]\n}\n\nexport type PortSide = 'left' | 'right' | 'top' | 'bottom'\n\nexport interface FlowPortSpec {\n id: string\n /** Inputs receive edges, outputs start them. */\n kind: 'input' | 'output'\n /** Where the port sits; inputs default to the left, outputs to the right. */\n side?: PortSide\n label?: string\n /** What the port carries. Two ports connect when they share a type, or either is `'any'`. */\n dataType?: string | string[]\n /**\n * The most edges the port takes. Inputs take one by default, outputs any\n * number. A new edge on a full port replaces its oldest edge.\n */\n max?: number\n}\n\n/** Where the canvas is shown: `x` and `y` are the screen offset of the canvas origin. */\nexport interface FlowViewport {\n x: number\n y: number\n zoom: number\n}\n\nexport interface FlowSelection {\n nodes: string[]\n edges: string[]\n}\n\nexport interface Point {\n x: number\n y: number\n}\n\nexport interface Rect {\n x: number\n y: number\n width: number\n height: number\n}\n\nexport const emptyDocument = (): FlowDocument => ({ version: 1, nodes: [], edges: [] })\nexport const emptySelection = (): FlowSelection => ({ nodes: [], edges: [] })\n\nlet counter = 0\n\n/** A short id, unique within this page and very likely across clients. */\nexport function createId(prefix = 'n') {\n counter = (counter + 1) % 1296\n const time = Date.now().toString(36).slice(-5)\n const random = Math.random().toString(36).slice(2, 6)\n return `${prefix}${time}${counter.toString(36).padStart(2, '0')}${random}`\n}\n"],"mappings":";AA2GA,IAAa,uBAAqC;CAAE,SAAS;CAAG,OAAO,CAAC;CAAG,OAAO,CAAC;AAAE;AACrF,IAAa,wBAAuC;CAAE,OAAO,CAAC;CAAG,OAAO,CAAC;AAAE;AAE3E,IAAI,UAAU;;AAGd,SAAgB,SAAS,SAAS,KAAK;CACrC,WAAW,UAAU,KAAK;CAC1B,MAAM,OAAO,KAAK,IAAI,CAAC,CAAC,SAAS,EAAE,CAAC,CAAC,MAAM,EAAE;CAC7C,MAAM,SAAS,KAAK,OAAO,CAAC,CAAC,SAAS,EAAE,CAAC,CAAC,MAAM,GAAG,CAAC;CACpD,OAAO,GAAG,SAAS,OAAO,QAAQ,SAAS,EAAE,CAAC,CAAC,SAAS,GAAG,GAAG,IAAI;AACpE"}
|
package/dist/core/transaction.js
CHANGED
|
@@ -69,15 +69,59 @@ var FlowTransaction = class {
|
|
|
69
69
|
get cancelReason() {
|
|
70
70
|
return this.stopped?.reason;
|
|
71
71
|
}
|
|
72
|
+
index = {};
|
|
73
|
+
incident;
|
|
74
|
+
was;
|
|
75
|
+
nodeIndex(id) {
|
|
76
|
+
this.index.nodes ??= new Map(this.nodes.map((node, i) => [node.id, i]));
|
|
77
|
+
return this.index.nodes.get(id) ?? -1;
|
|
78
|
+
}
|
|
79
|
+
edgeIndex(id) {
|
|
80
|
+
this.index.edges ??= new Map(this.edges.map((edge, i) => [edge.id, i]));
|
|
81
|
+
return this.index.edges.get(id) ?? -1;
|
|
82
|
+
}
|
|
83
|
+
/** The document as it was before this transaction. */
|
|
84
|
+
get original() {
|
|
85
|
+
this.was ??= {
|
|
86
|
+
nodes: new Map(this.before.nodes.map((node) => [node.id, node])),
|
|
87
|
+
edges: new Map(this.before.edges.map((edge) => [edge.id, edge]))
|
|
88
|
+
};
|
|
89
|
+
return this.was;
|
|
90
|
+
}
|
|
72
91
|
getNode(id) {
|
|
73
|
-
return this.nodes.
|
|
92
|
+
return this.nodes[this.nodeIndex(id)];
|
|
74
93
|
}
|
|
75
94
|
getEdge(id) {
|
|
76
|
-
return this.edges.
|
|
95
|
+
return this.edges[this.edgeIndex(id)];
|
|
77
96
|
}
|
|
78
97
|
/** Edges that start or end at a node. */
|
|
79
98
|
edgesOf(id) {
|
|
80
|
-
|
|
99
|
+
if (!this.incident) {
|
|
100
|
+
this.incident = /* @__PURE__ */ new Map();
|
|
101
|
+
for (const edge of this.edges) this.attach(edge);
|
|
102
|
+
}
|
|
103
|
+
return (this.incident.get(id) ?? []).flatMap((e) => this.getEdge(e) ?? []);
|
|
104
|
+
}
|
|
105
|
+
attach(edge) {
|
|
106
|
+
if (!this.incident) return;
|
|
107
|
+
for (const end of /* @__PURE__ */ new Set([edge.source.node, edge.target.node])) {
|
|
108
|
+
const list = this.incident.get(end);
|
|
109
|
+
if (list) list.push(edge.id);
|
|
110
|
+
else this.incident.set(end, [edge.id]);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
detach(edge) {
|
|
114
|
+
if (!this.incident) return;
|
|
115
|
+
for (const end of /* @__PURE__ */ new Set([edge.source.node, edge.target.node])) {
|
|
116
|
+
const list = this.incident.get(end);
|
|
117
|
+
const at = list?.indexOf(edge.id) ?? -1;
|
|
118
|
+
if (at >= 0) list.splice(at, 1);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
/** Renumbers an index from `from` on, after an insert or removal there. */
|
|
122
|
+
reindex(map, list, from) {
|
|
123
|
+
if (!map) return;
|
|
124
|
+
for (let i = from; i < list.length; i++) map.set(list[i].id, i);
|
|
81
125
|
}
|
|
82
126
|
setMeta(meta) {
|
|
83
127
|
Object.assign(this.meta, meta);
|
|
@@ -106,19 +150,56 @@ var FlowTransaction = class {
|
|
|
106
150
|
id
|
|
107
151
|
});
|
|
108
152
|
}
|
|
153
|
+
/**
|
|
154
|
+
* Changes a node. Moving it moves the points of an edge at it along when
|
|
155
|
+
* the edge's other end has moved just as far in this transaction, and
|
|
156
|
+
* drops them otherwise, as they no longer fit; so does resizing it, as
|
|
157
|
+
* its ports move.
|
|
158
|
+
*/
|
|
109
159
|
updateNode(id, patch) {
|
|
110
|
-
|
|
160
|
+
const node = this.getNode(id);
|
|
161
|
+
const changed = (key) => node !== void 0 && key in patch && patch[key] !== node[key];
|
|
162
|
+
const moved = changed("x") || changed("y");
|
|
163
|
+
const resized = changed("width") || changed("height");
|
|
164
|
+
this.step({
|
|
111
165
|
type: "updateNode",
|
|
112
166
|
id,
|
|
113
167
|
patch
|
|
114
168
|
});
|
|
169
|
+
if (moved || resized) this.carryPoints(id, resized);
|
|
170
|
+
return this;
|
|
115
171
|
}
|
|
116
172
|
moveNode(id, x, y) {
|
|
173
|
+
const node = this.getNode(id);
|
|
174
|
+
if (node && node.x === x && node.y === y) return this;
|
|
117
175
|
return this.updateNode(id, {
|
|
118
176
|
x,
|
|
119
177
|
y
|
|
120
178
|
});
|
|
121
179
|
}
|
|
180
|
+
/** Moves the points of the edges at a moved node along, or drops them. */
|
|
181
|
+
carryPoints(id, drop) {
|
|
182
|
+
const shift = this.shiftOf(id);
|
|
183
|
+
for (const edge of this.edgesOf(id)) {
|
|
184
|
+
const before = this.original.edges.get(edge.id);
|
|
185
|
+
const other = this.shiftOf(edge.source.node === id ? edge.target.node : edge.source.node);
|
|
186
|
+
const together = !drop && shift && other && other.x === shift.x && other.y === shift.y;
|
|
187
|
+
if (before?.points && together) this.updateEdge(edge.id, { points: before.points.map((p) => ({
|
|
188
|
+
x: p.x + shift.x,
|
|
189
|
+
y: p.y + shift.y
|
|
190
|
+
})) });
|
|
191
|
+
else if (edge.points) this.updateEdge(edge.id, { points: void 0 });
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
/** How far a node has moved in this transaction so far. */
|
|
195
|
+
shiftOf(id) {
|
|
196
|
+
const now = this.getNode(id);
|
|
197
|
+
const was = this.original.nodes.get(id);
|
|
198
|
+
return now && was ? {
|
|
199
|
+
x: now.x - was.x,
|
|
200
|
+
y: now.y - was.y
|
|
201
|
+
} : void 0;
|
|
202
|
+
}
|
|
122
203
|
/** Merges `patch` into the node's `data`. */
|
|
123
204
|
updateData(id, patch) {
|
|
124
205
|
const node = this.getNode(id);
|
|
@@ -164,15 +245,19 @@ var FlowTransaction = class {
|
|
|
164
245
|
const list = this.writable("nodes");
|
|
165
246
|
const index = clampIndex(step.index, list.length);
|
|
166
247
|
list.splice(index, 0, step.node);
|
|
248
|
+
this.reindex(this.index.nodes, list, index);
|
|
167
249
|
return {
|
|
168
250
|
type: "removeNode",
|
|
169
251
|
id: step.node.id
|
|
170
252
|
};
|
|
171
253
|
}
|
|
172
254
|
case "removeNode": {
|
|
173
|
-
const index = this.
|
|
255
|
+
const index = this.nodeIndex(step.id);
|
|
174
256
|
if (index < 0) return null;
|
|
175
|
-
const
|
|
257
|
+
const list = this.writable("nodes");
|
|
258
|
+
const [node] = list.splice(index, 1);
|
|
259
|
+
this.index.nodes?.delete(step.id);
|
|
260
|
+
this.reindex(this.index.nodes, list, index);
|
|
176
261
|
return {
|
|
177
262
|
type: "addNode",
|
|
178
263
|
node,
|
|
@@ -180,7 +265,7 @@ var FlowTransaction = class {
|
|
|
180
265
|
};
|
|
181
266
|
}
|
|
182
267
|
case "updateNode": {
|
|
183
|
-
const index = this.
|
|
268
|
+
const index = this.nodeIndex(step.id);
|
|
184
269
|
if (index < 0) return null;
|
|
185
270
|
const node = this.nodes[index];
|
|
186
271
|
if (Object.keys(step.patch).every((key) => sameValue(node[key], step.patch[key]))) return null;
|
|
@@ -195,16 +280,23 @@ var FlowTransaction = class {
|
|
|
195
280
|
case "addEdge": {
|
|
196
281
|
if (this.getEdge(step.edge.id)) return null;
|
|
197
282
|
const list = this.writable("edges");
|
|
198
|
-
|
|
283
|
+
const index = clampIndex(step.index, list.length);
|
|
284
|
+
list.splice(index, 0, step.edge);
|
|
285
|
+
this.reindex(this.index.edges, list, index);
|
|
286
|
+
this.attach(step.edge);
|
|
199
287
|
return {
|
|
200
288
|
type: "removeEdge",
|
|
201
289
|
id: step.edge.id
|
|
202
290
|
};
|
|
203
291
|
}
|
|
204
292
|
case "removeEdge": {
|
|
205
|
-
const index = this.
|
|
293
|
+
const index = this.edgeIndex(step.id);
|
|
206
294
|
if (index < 0) return null;
|
|
207
|
-
const
|
|
295
|
+
const list = this.writable("edges");
|
|
296
|
+
const [edge] = list.splice(index, 1);
|
|
297
|
+
this.index.edges?.delete(step.id);
|
|
298
|
+
this.reindex(this.index.edges, list, index);
|
|
299
|
+
this.detach(edge);
|
|
208
300
|
return {
|
|
209
301
|
type: "addEdge",
|
|
210
302
|
edge,
|
|
@@ -212,12 +304,16 @@ var FlowTransaction = class {
|
|
|
212
304
|
};
|
|
213
305
|
}
|
|
214
306
|
case "updateEdge": {
|
|
215
|
-
const index = this.
|
|
307
|
+
const index = this.edgeIndex(step.id);
|
|
216
308
|
if (index < 0) return null;
|
|
217
309
|
const edge = this.edges[index];
|
|
218
310
|
if (Object.keys(step.patch).every((key) => sameValue(edge[key], step.patch[key]))) return null;
|
|
219
311
|
const list = this.writable("edges");
|
|
220
312
|
list[index] = patched(edge, step.patch);
|
|
313
|
+
if ("source" in step.patch || "target" in step.patch) {
|
|
314
|
+
this.detach(edge);
|
|
315
|
+
this.attach(list[index]);
|
|
316
|
+
}
|
|
221
317
|
return {
|
|
222
318
|
type: "updateEdge",
|
|
223
319
|
id: step.id,
|