@dou-ui/flow 0.1.0-alpha.1 → 0.1.0-alpha.11
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/collaboration/index.js +76 -5
- package/dist/collaboration/index.js.map +1 -1
- package/dist/comments.js +245 -0
- package/dist/comments.js.map +1 -0
- package/dist/components/default-node2.js.map +1 -1
- package/dist/components/flow-comments.js +6 -0
- package/dist/components/flow-comments.js.map +1 -0
- package/dist/components/flow-comments.vue_vue_type_script_setup_true_lang.js +205 -0
- package/dist/components/flow-comments.vue_vue_type_script_setup_true_lang.js.map +1 -0
- 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/extension.js.map +1 -1
- package/dist/core/history.js +26 -0
- package/dist/core/history.js.map +1 -1
- package/dist/core/model.js.map +1 -1
- package/dist/core/node-types.js +35 -1
- package/dist/core/node-types.js.map +1 -1
- package/dist/core/transaction.js +112 -15
- package/dist/core/transaction.js.map +1 -1
- package/dist/document.js +1 -1
- package/dist/export/render.js +78 -0
- package/dist/export/render.js.map +1 -0
- package/dist/flow-editor.js.map +1 -1
- package/dist/flow-editor.vue_vue_type_script_setup_true_lang.js +454 -36
- package/dist/flow-editor.vue_vue_type_script_setup_true_lang.js.map +1 -1
- package/dist/index.js +6 -6
- package/dist/index.js.map +1 -1
- package/dist/kit.js +1 -1
- package/dist/locale.js +24 -9
- package/dist/locale.js.map +1 -1
- package/dist/style.css +1 -1
- package/dist/style.js +2 -1
- package/dist/types/collaboration/index.d.ts +32 -4
- package/dist/types/comments.d.ts +73 -0
- package/dist/types/components/default-node.d.ts +1 -1
- package/dist/types/components/flow-comments.vue.d.ts +35 -0
- package/dist/types/components/port.d.ts +7 -0
- package/dist/types/context.d.ts +54 -1
- package/dist/types/core/extension.d.ts +2 -1
- package/dist/types/core/history.d.ts +10 -0
- package/dist/types/core/menu.d.ts +5 -0
- package/dist/types/core/model.d.ts +19 -0
- package/dist/types/core/node-types.d.ts +16 -2
- package/dist/types/core/transaction.d.ts +23 -1
- package/dist/types/export/render.d.ts +27 -0
- package/dist/types/flow-editor.vue.d.ts +11 -0
- package/dist/types/index.d.ts +36 -9
- package/dist/types/locale.d.ts +13 -3
- package/dist/types/whiteboard/image.d.ts +8 -0
- package/dist/types/whiteboard/index.d.ts +1 -1
- package/dist/types/whiteboard/kit.d.ts +7 -1
- package/dist/types/whiteboard/model.d.ts +2 -11
- package/dist/types/whiteboard/state.d.ts +5 -1
- 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/dist/whiteboard/connector.js +5 -10
- package/dist/whiteboard/connector.js.map +1 -1
- package/dist/whiteboard/image.js +17 -6
- package/dist/whiteboard/image.js.map +1 -1
- package/dist/whiteboard/kit.js +64 -8
- package/dist/whiteboard/kit.js.map +1 -1
- package/dist/whiteboard/model.js +4 -31
- package/dist/whiteboard/model.js.map +1 -1
- package/dist/whiteboard/state.js +6 -1
- package/dist/whiteboard/state.js.map +1 -1
- package/dist/whiteboard/whiteboard-toolbar.js.map +1 -1
- package/dist/whiteboard/whiteboard-toolbar.vue_vue_type_script_setup_true_lang.js +14 -7
- package/dist/whiteboard/whiteboard-toolbar.vue_vue_type_script_setup_true_lang.js.map +1 -1
- package/package.json +3 -3
package/dist/core/history.js
CHANGED
|
@@ -69,6 +69,32 @@ var FlowHistory = class {
|
|
|
69
69
|
selection: entry.selectionAfter
|
|
70
70
|
};
|
|
71
71
|
}
|
|
72
|
+
/**
|
|
73
|
+
* Gives a change kept out of undo to a node or edge as undo and redo
|
|
74
|
+
* bring it back, too: comments started on a new node stay on it when its
|
|
75
|
+
* adding is undone and redone.
|
|
76
|
+
*/
|
|
77
|
+
carry(target, patch) {
|
|
78
|
+
const merge = (item) => {
|
|
79
|
+
const next = {
|
|
80
|
+
...item,
|
|
81
|
+
...patch
|
|
82
|
+
};
|
|
83
|
+
for (const [key, value] of Object.entries(patch)) if (value === void 0) delete next[key];
|
|
84
|
+
return next;
|
|
85
|
+
};
|
|
86
|
+
const fix = (steps) => steps.map((step) => "node" in target && step.type === "addNode" && step.node.id === target.node ? {
|
|
87
|
+
...step,
|
|
88
|
+
node: merge(step.node)
|
|
89
|
+
} : "edge" in target && step.type === "addEdge" && step.edge.id === target.edge ? {
|
|
90
|
+
...step,
|
|
91
|
+
edge: merge(step.edge)
|
|
92
|
+
} : step);
|
|
93
|
+
for (const entry of [...this.done, ...this.undone]) {
|
|
94
|
+
entry.steps = fix(entry.steps);
|
|
95
|
+
entry.inverse = fix(entry.inverse);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
72
98
|
clear() {
|
|
73
99
|
this.done = [];
|
|
74
100
|
this.undone = [];
|
package/dist/core/history.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"history.js","names":[],"sources":["../../src/core/history.ts"],"sourcesContent":["import type { FlowDocument, FlowSelection } from './model'\nimport {\n applyFlowSteps,\n compactFlowSteps,\n type FlowStep,\n type FlowTransaction,\n} from './transaction'\n\ninterface Entry {\n steps: FlowStep[]\n inverse: FlowStep[]\n group?: string\n time: number\n selectionBefore: FlowSelection\n selectionAfter: FlowSelection\n}\n\n/** Transactions in one group merge when they come within this many ms. */\nconst GROUP_WINDOW = 1000\n\nexport class FlowHistory {\n private done: Entry[] = []\n private undone: Entry[] = []\n\n constructor(private depth = 100) {}\n\n get canUndo() {\n return this.done.length > 0\n }\n\n get canRedo() {\n return this.undone.length > 0\n }\n\n record(\n tr: FlowTransaction,\n selectionBefore: FlowSelection,\n selectionAfter: FlowSelection,\n now = Date.now(),\n ) {\n if (!tr.changed || tr.meta.history === false) return\n this.undone = []\n const last = this.done[this.done.length - 1]\n const group = tr.meta.group\n if (last && group && last.group === group && now - last.time < GROUP_WINDOW) {\n last.steps = compactFlowSteps([...last.steps, ...tr.steps])\n last.inverse = compactFlowSteps([...tr.inverse, ...last.inverse])\n last.time = now\n last.selectionAfter = selectionAfter\n return\n }\n this.done.push({\n steps: tr.steps.slice(),\n inverse: tr.inverse.slice(),\n group,\n time: now,\n selectionBefore,\n selectionAfter,\n })\n if (this.done.length > this.depth) this.done.shift()\n }\n\n /** Ends the current group, so the next change starts a new undo step. */\n close() {\n const last = this.done[this.done.length - 1]\n if (last) last.group = undefined\n }\n\n /** Forgets the last entry if it belongs to `group`, e.g. after a cancelled drag. */\n drop(group: string) {\n const last = this.done[this.done.length - 1]\n if (last?.group === group) this.done.pop()\n }\n\n undo(doc: FlowDocument) {\n const entry = this.done.pop()\n if (!entry) return null\n const tr = applyFlowSteps(doc, entry.inverse)\n this.undone.push(entry)\n return { tr, selection: entry.selectionBefore }\n }\n\n redo(doc: FlowDocument) {\n const entry = this.undone.pop()\n if (!entry) return null\n const tr = applyFlowSteps(doc, entry.steps)\n this.done.push({ ...entry, group: undefined })\n return { tr, selection: entry.selectionAfter }\n }\n\n clear() {\n this.done = []\n this.undone = []\n }\n}\n"],"mappings":";;;AAkBA,IAAM,eAAe;AAErB,IAAa,cAAb,MAAyB;CAIH;CAHpB,OAAwB,CAAC;CACzB,SAA0B,CAAC;CAE3B,YAAY,QAAgB,KAAK;EAAb,KAAA,QAAA;CAAc;CAElC,IAAI,UAAU;EACZ,OAAO,KAAK,KAAK,SAAS;CAC5B;CAEA,IAAI,UAAU;EACZ,OAAO,KAAK,OAAO,SAAS;CAC9B;CAEA,OACE,IACA,iBACA,gBACA,MAAM,KAAK,IAAI,GACf;EACA,IAAI,CAAC,GAAG,WAAW,GAAG,KAAK,YAAY,OAAO;EAC9C,KAAK,SAAS,CAAC;EACf,MAAM,OAAO,KAAK,KAAK,KAAK,KAAK,SAAS;EAC1C,MAAM,QAAQ,GAAG,KAAK;EACtB,IAAI,QAAQ,SAAS,KAAK,UAAU,SAA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owDocument, FlowSelection } from './model'\nimport {\n applyFlowSteps,\n compactFlowSteps,\n type FlowStep,\n type FlowTransaction,\n} from './transaction'\n\ninterface Entry {\n steps: FlowStep[]\n inverse: FlowStep[]\n group?: string\n time: number\n selectionBefore: FlowSelection\n selectionAfter: FlowSelection\n}\n\n/** Transactions in one group merge when they come within this many ms. */\nconst GROUP_WINDOW = 1000\n\nexport class FlowHistory {\n private done: Entry[] = []\n private undone: Entry[] = []\n\n constructor(private depth = 100) {}\n\n get canUndo() {\n return this.done.length > 0\n }\n\n get canRedo() {\n return this.undone.length > 0\n }\n\n record(\n tr: FlowTransaction,\n selectionBefore: FlowSelection,\n selectionAfter: FlowSelection,\n now = Date.now(),\n ) {\n if (!tr.changed || tr.meta.history === false) return\n this.undone = []\n const last = this.done[this.done.length - 1]\n const group = tr.meta.group\n if (last && group && last.group === group && now - last.time < GROUP_WINDOW) {\n last.steps = compactFlowSteps([...last.steps, ...tr.steps])\n last.inverse = compactFlowSteps([...tr.inverse, ...last.inverse])\n last.time = now\n last.selectionAfter = selectionAfter\n return\n }\n this.done.push({\n steps: tr.steps.slice(),\n inverse: tr.inverse.slice(),\n group,\n time: now,\n selectionBefore,\n selectionAfter,\n })\n if (this.done.length > this.depth) this.done.shift()\n }\n\n /** Ends the current group, so the next change starts a new undo step. */\n close() {\n const last = this.done[this.done.length - 1]\n if (last) last.group = undefined\n }\n\n /** Forgets the last entry if it belongs to `group`, e.g. after a cancelled drag. */\n drop(group: string) {\n const last = this.done[this.done.length - 1]\n if (last?.group === group) this.done.pop()\n }\n\n undo(doc: FlowDocument) {\n const entry = this.done.pop()\n if (!entry) return null\n const tr = applyFlowSteps(doc, entry.inverse)\n this.undone.push(entry)\n return { tr, selection: entry.selectionBefore }\n }\n\n redo(doc: FlowDocument) {\n const entry = this.undone.pop()\n if (!entry) return null\n const tr = applyFlowSteps(doc, entry.steps)\n this.done.push({ ...entry, group: undefined })\n return { tr, selection: entry.selectionAfter }\n }\n\n /**\n * Gives a change kept out of undo to a node or edge as undo and redo\n * bring it back, too: comments started on a new node stay on it when its\n * adding is undone and redone.\n */\n carry(target: { node: string } | { edge: string }, patch: Record<string, unknown>) {\n const merge = <T extends object>(item: T): T => {\n const next: Record<string, unknown> = { ...item, ...patch }\n for (const [key, value] of Object.entries(patch)) if (value === undefined) delete next[key]\n return next as T\n }\n const fix = (steps: FlowStep[]) =>\n steps.map((step) =>\n 'node' in target && step.type === 'addNode' && step.node.id === target.node\n ? { ...step, node: merge(step.node) }\n : 'edge' in target && step.type === 'addEdge' && step.edge.id === target.edge\n ? { ...step, edge: merge(step.edge) }\n : step,\n )\n for (const entry of [...this.done, ...this.undone]) {\n entry.steps = fix(entry.steps)\n entry.inverse = fix(entry.inverse)\n }\n }\n\n clear() {\n this.done = []\n this.undone = []\n }\n}\n"],"mappings":";;;AAkBA,IAAM,eAAe;AAErB,IAAa,cAAb,MAAyB;CAIH;CAHpB,OAAwB,CAAC;CACzB,SAA0B,CAAC;CAE3B,YAAY,QAAgB,KAAK;EAAb,KAAA,QAAA;CAAc;CAElC,IAAI,UAAU;EACZ,OAAO,KAAK,KAAK,SAAS;CAC5B;CAEA,IAAI,UAAU;EACZ,OAAO,KAAK,OAAO,SAAS;CAC9B;CAEA,OACE,IACA,iBACA,gBACA,MAAM,KAAK,IAAI,GACf;EACA,IAAI,CAAC,GAAG,WAAW,GAAG,KAAK,YAAY,OAAO;EAC9C,KAAK,SAAS,CAAC;EACf,MAAM,OAAO,KAAK,KAAK,KAAK,KAAK,SAAS;EAC1C,MAAM,QAAQ,GAAG,KAAK;EACtB,IAAI,QAAQ,SAAS,KAAK,UAAU,SAAS,MAAM,KAAK,OAAO,cAAc;GAC3E,KAAK,QAAQ,iBAAiB,CAAC,GAAG,KAAK,OAAO,GAAG,GAAG,KAAK,CAAC;GAC1D,KAAK,UAAU,iBAAiB,CAAC,GAAG,GAAG,SAAS,GAAG,KAAK,OAAO,CAAC;GAChE,KAAK,OAAO;GACZ,KAAK,iBAAiB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AAI;EAAE,IAClC,UAAU,UAAU,KAAK,SAAS,aAAa,KAAK,KAAK,OAAO,OAAO,OACrE;GAAE,GAAG;GAAM,MAAM,MAAM,KAAK,IAAI;EAAE,IAClC,IACR;EACF,KAAK,MAAM,SAAS,CAAC,GAAG,KAAK,MAAM,GAAG,KAAK,MAAM,GAAG;GAClD,MAAM,QAAQ,IAAI,MAAM,KAAK;GAC7B,MAAM,UAAU,IAAI,MAAM,OAAO;EACnC;CACF;CAEA,QAAQ;EACN,KAAK,OAAO,CAAC;EACb,KAAK,SAAS,CAAC;CACjB;AACF"}
|
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 * The ids of the comment threads about this node, see the editor's\n * `comments`. Kept when the node is deleted and brought back by undo.\n */\n comments?: string[]\n /**\n * Where on the node each pinned thread is, by thread id: `x` and `y` are\n * fractions of the node's width and height from its top left, 0 to 1, so\n * a pin moves and scales with the node. The ids are in `comments` too.\n */\n commentPins?: Record<string, Point>\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 /** The ids of the comment threads about this edge, see the editor's `comments`. */\n comments?: string[]\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":";AAwHA,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/node-types.js
CHANGED
|
@@ -6,9 +6,36 @@ function defineNodeType(spec) {
|
|
|
6
6
|
return spec;
|
|
7
7
|
}
|
|
8
8
|
var portSide = (port) => port.side ?? (port.kind === "input" ? "left" : "right");
|
|
9
|
+
/**
|
|
10
|
+
* The ports of `ports: 'sides'`: an output and an input in the middle of
|
|
11
|
+
* each side. Edges go from an output (`right`) to an input (`left-in`).
|
|
12
|
+
* `right` and `left-in` come first, so a node added after another joins it
|
|
13
|
+
* left to right.
|
|
14
|
+
*/
|
|
15
|
+
var sidePorts = [...[
|
|
16
|
+
"right",
|
|
17
|
+
"bottom",
|
|
18
|
+
"left",
|
|
19
|
+
"top"
|
|
20
|
+
].map((side) => ({
|
|
21
|
+
id: side,
|
|
22
|
+
kind: "output",
|
|
23
|
+
side
|
|
24
|
+
})), ...[
|
|
25
|
+
"left",
|
|
26
|
+
"top",
|
|
27
|
+
"right",
|
|
28
|
+
"bottom"
|
|
29
|
+
].map((side) => ({
|
|
30
|
+
id: `${side}-in`,
|
|
31
|
+
kind: "input",
|
|
32
|
+
side,
|
|
33
|
+
max: Number.POSITIVE_INFINITY
|
|
34
|
+
}))];
|
|
9
35
|
function portsOf(spec, node) {
|
|
10
36
|
const ports = spec?.ports;
|
|
11
37
|
if (!ports) return [];
|
|
38
|
+
if (ports === "sides") return sidePorts;
|
|
12
39
|
return typeof ports === "function" ? ports(node) : ports;
|
|
13
40
|
}
|
|
14
41
|
/** Whether a node can be resized, and its smallest size; `null` when it can't. */
|
|
@@ -27,6 +54,13 @@ function labelOf(spec, node, messages) {
|
|
|
27
54
|
function nodeBounds(spec, node, rect) {
|
|
28
55
|
return spec.bounds?.(node, rect) ?? turnedBox(rect, spec.rotation?.(node) ?? 0);
|
|
29
56
|
}
|
|
30
|
-
|
|
57
|
+
var unknownWarned = /* @__PURE__ */ new Set();
|
|
58
|
+
/** Warns once per page about a node type no one registered. Call only while developing. */
|
|
59
|
+
function warnUnknownNodeType(type) {
|
|
60
|
+
if (unknownWarned.has(type)) return;
|
|
61
|
+
unknownWarned.add(type);
|
|
62
|
+
console.warn(`[@dou-ui/flow] node type "${type}" is not registered, so it is drawn as a default node: pass it in nodeTypes (or an extension)`);
|
|
63
|
+
}
|
|
64
|
+
export { defineNodeType, labelOf, nodeBounds, portSide, portsOf, resizeOf, sidePorts, warnUnknownNodeType };
|
|
31
65
|
|
|
32
66
|
//# sourceMappingURL=node-types.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"node-types.js","names":[],"sources":["../../src/core/node-types.ts"],"sourcesContent":["import type { FlowDrawContext } from '../export/context'\nimport type { Component } from 'vue'\nimport type { FlowMessages } from '../locale'\nimport { turnedBox } from './geometry'\nimport type { FlowData, FlowNode, FlowPortSpec, PortSide, Rect } from './model'\n\n/** What a node type's component receives. */\nexport interface NodeViewProps<Data extends FlowData = FlowData> {\n node: FlowNode<Data>\n data: Data\n ports: FlowPortSpec[]\n selected: boolean\n readonly: boolean\n /** Merges `patch` into the node's data, as one undo step. */\n updateData: (patch: Partial<Data>) => void\n}\n\nexport interface NodeTypeSpec<Data extends FlowData = FlowData> {\n type: string\n /**\n * Renders the node's content. The editor draws the frame, selection and\n * focus ring around it. Place ports with `<DouFlowPort>`; ports the\n * component does not place are spread along their sides.\n */\n component?: Component\n
|
|
1
|
+
{"version":3,"file":"node-types.js","names":[],"sources":["../../src/core/node-types.ts"],"sourcesContent":["import type { FlowDrawContext } from '../export/context'\nimport type { Component } from 'vue'\nimport type { FlowMessages } from '../locale'\nimport { turnedBox } from './geometry'\nimport type { FlowData, FlowNode, FlowPortSpec, PortSide, Rect } from './model'\n\n/** What a node type's component receives. */\nexport interface NodeViewProps<Data extends FlowData = FlowData> {\n node: FlowNode<Data>\n data: Data\n ports: FlowPortSpec[]\n selected: boolean\n readonly: boolean\n /** Merges `patch` into the node's data, as one undo step. */\n updateData: (patch: Partial<Data>) => void\n}\n\nexport interface NodeTypeSpec<Data extends FlowData = FlowData> {\n type: string\n /**\n * Renders the node's content. The editor draws the frame, selection and\n * focus ring around it. Place ports with `<DouFlowPort>`; ports the\n * component does not place are spread along their sides.\n */\n component?: Component\n /**\n * The node's ports, fixed or worked out from its data. `'sides'`: an\n * output and an input in the middle of each side, all alike (see\n * `sidePorts`); tidying lays such nodes out the way their edges are\n * drawn and moves the edges' ends onto the sides that face each other.\n */\n ports?: FlowPortSpec[] | 'sides' | ((node: FlowNode<Data>) => FlowPortSpec[])\n /** Data for a new node of this type. */\n defaults?: () => Data\n /**\n * The node's accessible name; `data.label` by default. `messages` are the\n * editor's current strings, for names that follow the locale.\n */\n label?: (node: FlowNode<Data>, messages: FlowMessages) => string\n /**\n * How the type is offered by `paletteTypes` and in the list that opens\n * when an edge is dropped on the empty canvas. Types without it aren't offered.\n */\n palette?: NodePaletteSpec\n /**\n * Nodes dropped inside become its children (`parent`) and move with it;\n * with `data.collapsed` they are hidden and their edges end on it.\n * Containers are drawn under edges and other nodes.\n */\n container?: boolean\n /**\n * A handle on the selected node changes its stored `width` and `height`.\n * A function decides per node, e.g. not while it is turned.\n */\n resizable?: NodeResize | ((node: FlowNode<Data>) => NodeResize)\n /** The `width` and `height` a new node starts with. */\n size?: { width: number; height: number }\n /**\n * `false` leaves out the card the editor draws around the component (its\n * border, background and shadow), for nodes that draw their own outline,\n * e.g. a circle. Selection then shows as a thin outline around the box.\n */\n frame?: boolean\n /**\n * How far the node is drawn turned about its centre, in degrees clockwise;\n * `0` by default. The component turns what it draws, and `draw` turns its\n * export; the editor follows: the selection outline, picking with a box,\n * what is drawn in view, the toolbar and other people's outlines. For\n * nodes with `frame: false`. A turned node has no editor resize handle.\n */\n rotation?: (node: FlowNode<Data>) => number\n /**\n * The box the node covers on screen when it draws outside its own `rect`.\n * The toolbar over the selection floats above it. By default the box\n * around `rect` turned by `rotation`.\n */\n bounds?: (node: FlowNode<Data>, rect: Rect) => Rect\n /**\n * Draws the node as SVG markup for `exportSVG` and `exportPNG`, in its\n * own coordinates. Without it the node is drawn as a card with its name.\n */\n draw?: (node: FlowNode<Data>, draw: FlowDrawContext) => string\n}\n\nexport interface NodePaletteSpec {\n label: string\n icon?: Component\n /** Types with the same group are listed together under its name. */\n group?: string\n description?: string\n /** Other words the search should find the type by. */\n keywords?: string[]\n}\n\n/** Declares a node type, with the data type checked against its component. */\nexport function defineNodeType<Data extends FlowData = FlowData>(\n spec: NodeTypeSpec<Data>,\n): NodeTypeSpec<Data> {\n if (process.env.NODE_ENV !== 'production' && spec.rotation && spec.frame !== false)\n console.warn(\n `[@dou-ui/flow] the node type \"${spec.type}\" has rotation but not frame: false: the card the editor draws around it does not turn`,\n )\n return spec\n}\n\nexport const portSide = (port: FlowPortSpec): PortSide =>\n port.side ?? (port.kind === 'input' ? 'left' : 'right')\n\n/**\n * The ports of `ports: 'sides'`: an output and an input in the middle of\n * each side. Edges go from an output (`right`) to an input (`left-in`).\n * `right` and `left-in` come first, so a node added after another joins it\n * left to right.\n */\nexport const sidePorts: readonly FlowPortSpec[] = [\n ...(['right', 'bottom', 'left', 'top'] as const).map((side) => ({\n id: side,\n kind: 'output' as const,\n side,\n })),\n ...(['left', 'top', 'right', 'bottom'] as const).map((side) => ({\n id: `${side}-in`,\n kind: 'input' as const,\n side,\n max: Number.POSITIVE_INFINITY,\n })),\n]\n\nexport function portsOf(spec: NodeTypeSpec | undefined, node: FlowNode): FlowPortSpec[] {\n const ports = spec?.ports\n if (!ports) return []\n if (ports === 'sides') return sidePorts as FlowPortSpec[]\n return typeof ports === 'function' ? ports(node) : ports\n}\n\nexport type NodeResize = boolean | { minWidth?: number; minHeight?: number }\n\n/** Whether a node can be resized, and its smallest size; `null` when it can't. */\nexport function resizeOf(\n spec: NodeTypeSpec | undefined,\n node: FlowNode,\n): { minWidth?: number; minHeight?: number } | null {\n // the editor's handle grows a node along the page, not along a turned node's sides\n if (spec?.rotation?.(node)) return null\n const value = typeof spec?.resizable === 'function' ? spec.resizable(node) : spec?.resizable\n if (!value) return null\n return value === true ? {} : value\n}\n\nexport function labelOf(\n spec: NodeTypeSpec | undefined,\n node: FlowNode,\n messages: FlowMessages,\n): string {\n if (spec?.label) return spec.label(node, messages)\n const label = node.data?.label\n return typeof label === 'string' ? label : node.id\n}\n\n/** The box `node` covers on screen, drawn at `rect`. */\nexport function nodeBounds(spec: NodeTypeSpec, node: FlowNode, rect: Rect): Rect {\n return spec.bounds?.(node, rect) ?? turnedBox(rect, spec.rotation?.(node) ?? 0)\n}\n\nconst unknownWarned = new Set<string>()\n/** Warns once per page about a node type no one registered. Call only while developing. */\nexport function warnUnknownNodeType(type: string) {\n if (unknownWarned.has(type)) return\n unknownWarned.add(type)\n console.warn(\n `[@dou-ui/flow] node type \"${type}\" is not registered, so it is drawn as a default node: pass it in nodeTypes (or an extension)`,\n )\n}\n"],"mappings":";;;AA+FA,SAAgB,eACd,MACoB;CACpB,IAAA,QAAA,IAAA,aAA6B,gBAAgB,KAAK,YAAY,KAAK,UAAU,OAC3E,QAAQ,KACN,iCAAiC,KAAK,KAAK,uFAC7C;CACF,OAAO;AACT;AAEA,IAAa,YAAY,SACvB,KAAK,SAAS,KAAK,SAAS,UAAU,SAAS;;;;;;;AAQjD,IAAa,YAAqC,CAChD,GAAI;CAAC;CAAS;CAAU;CAAQ;AAAK,CAAC,CAAW,KAAK,UAAU;CAC9D,IAAI;CACJ,MAAM;CACN;AACF,EAAE,GACF,GAAI;CAAC;CAAQ;CAAO;CAAS;AAAQ,CAAC,CAAW,KAAK,UAAU;CAC9D,IAAI,GAAG,KAAK;CACZ,MAAM;CACN;CACA,KAAK,OAAO;AACd,EAAE,CACJ;AAEA,SAAgB,QAAQ,MAAgC,MAAgC;CACtF,MAAM,QAAQ,MAAM;CACpB,IAAI,CAAC,OAAO,OAAO,CAAC;CACpB,IAAI,UAAU,SAAS,OAAO;CAC9B,OAAO,OAAO,UAAU,aAAa,MAAM,IAAI,IAAI;AACrD;;AAKA,SAAgB,SACd,MACA,MACkD;CAElD,IAAI,MAAM,WAAW,IAAI,GAAG,OAAO;CACnC,MAAM,QAAQ,OAAO,MAAM,cAAc,aAAa,KAAK,UAAU,IAAI,IAAI,MAAM;CACnF,IAAI,CAAC,OAAO,OAAO;CACnB,OAAO,UAAU,OAAO,CAAC,IAAI;AAC/B;AAEA,SAAgB,QACd,MACA,MACA,UACQ;CACR,IAAI,MAAM,OAAO,OAAO,KAAK,MAAM,MAAM,QAAQ;CACjD,MAAM,QAAQ,KAAK,MAAM;CACzB,OAAO,OAAO,UAAU,WAAW,QAAQ,KAAK;AAClD;;AAGA,SAAgB,WAAW,MAAoB,MAAgB,MAAkB;CAC/E,OAAO,KAAK,SAAS,MAAM,IAAI,KAAK,UAAU,MAAM,KAAK,WAAW,IAAI,KAAK,CAAC;AAChF;AAEA,IAAM,gCAAgB,IAAI,IAAY;;AAEtC,SAAgB,oBAAoB,MAAc;CAChD,IAAI,cAAc,IAAI,IAAI,GAAG;CAC7B,cAAc,IAAI,IAAI;CACtB,QAAQ,KACN,6BAA6B,KAAK,8FACpC;AACF"}
|
package/dist/core/transaction.js
CHANGED
|
@@ -54,11 +54,12 @@ var FlowTransaction = class {
|
|
|
54
54
|
* editor fires `transactionCancel` and says why. For an extension's `transaction`
|
|
55
55
|
* hook, e.g. to keep people from changing what they may not; hooks after
|
|
56
56
|
* it don't run. A change kept out of the history (`meta.history: false`),
|
|
57
|
-
* such as undo, redo
|
|
57
|
+
* such as undo, redo, another person's or a comment's (`meta.origin`
|
|
58
|
+
* tells which), can't be cancelled and goes ahead.
|
|
58
59
|
*/
|
|
59
60
|
cancel(reason) {
|
|
60
61
|
if (this.meta.history !== false) this.stopped = { reason };
|
|
61
|
-
else warnUncancellable();
|
|
62
|
+
else warnUncancellable(this.meta.origin);
|
|
62
63
|
return this;
|
|
63
64
|
}
|
|
64
65
|
/** Whether `cancel` stopped it. */
|
|
@@ -69,15 +70,59 @@ var FlowTransaction = class {
|
|
|
69
70
|
get cancelReason() {
|
|
70
71
|
return this.stopped?.reason;
|
|
71
72
|
}
|
|
73
|
+
index = {};
|
|
74
|
+
incident;
|
|
75
|
+
was;
|
|
76
|
+
nodeIndex(id) {
|
|
77
|
+
this.index.nodes ??= new Map(this.nodes.map((node, i) => [node.id, i]));
|
|
78
|
+
return this.index.nodes.get(id) ?? -1;
|
|
79
|
+
}
|
|
80
|
+
edgeIndex(id) {
|
|
81
|
+
this.index.edges ??= new Map(this.edges.map((edge, i) => [edge.id, i]));
|
|
82
|
+
return this.index.edges.get(id) ?? -1;
|
|
83
|
+
}
|
|
84
|
+
/** The document as it was before this transaction. */
|
|
85
|
+
get original() {
|
|
86
|
+
this.was ??= {
|
|
87
|
+
nodes: new Map(this.before.nodes.map((node) => [node.id, node])),
|
|
88
|
+
edges: new Map(this.before.edges.map((edge) => [edge.id, edge]))
|
|
89
|
+
};
|
|
90
|
+
return this.was;
|
|
91
|
+
}
|
|
72
92
|
getNode(id) {
|
|
73
|
-
return this.nodes.
|
|
93
|
+
return this.nodes[this.nodeIndex(id)];
|
|
74
94
|
}
|
|
75
95
|
getEdge(id) {
|
|
76
|
-
return this.edges.
|
|
96
|
+
return this.edges[this.edgeIndex(id)];
|
|
77
97
|
}
|
|
78
98
|
/** Edges that start or end at a node. */
|
|
79
99
|
edgesOf(id) {
|
|
80
|
-
|
|
100
|
+
if (!this.incident) {
|
|
101
|
+
this.incident = /* @__PURE__ */ new Map();
|
|
102
|
+
for (const edge of this.edges) this.attach(edge);
|
|
103
|
+
}
|
|
104
|
+
return (this.incident.get(id) ?? []).flatMap((e) => this.getEdge(e) ?? []);
|
|
105
|
+
}
|
|
106
|
+
attach(edge) {
|
|
107
|
+
if (!this.incident) return;
|
|
108
|
+
for (const end of /* @__PURE__ */ new Set([edge.source.node, edge.target.node])) {
|
|
109
|
+
const list = this.incident.get(end);
|
|
110
|
+
if (list) list.push(edge.id);
|
|
111
|
+
else this.incident.set(end, [edge.id]);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
detach(edge) {
|
|
115
|
+
if (!this.incident) return;
|
|
116
|
+
for (const end of /* @__PURE__ */ new Set([edge.source.node, edge.target.node])) {
|
|
117
|
+
const list = this.incident.get(end);
|
|
118
|
+
const at = list?.indexOf(edge.id) ?? -1;
|
|
119
|
+
if (at >= 0) list.splice(at, 1);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
/** Renumbers an index from `from` on, after an insert or removal there. */
|
|
123
|
+
reindex(map, list, from) {
|
|
124
|
+
if (!map) return;
|
|
125
|
+
for (let i = from; i < list.length; i++) map.set(list[i].id, i);
|
|
81
126
|
}
|
|
82
127
|
setMeta(meta) {
|
|
83
128
|
Object.assign(this.meta, meta);
|
|
@@ -106,19 +151,56 @@ var FlowTransaction = class {
|
|
|
106
151
|
id
|
|
107
152
|
});
|
|
108
153
|
}
|
|
154
|
+
/**
|
|
155
|
+
* Changes a node. Moving it moves the points of an edge at it along when
|
|
156
|
+
* the edge's other end has moved just as far in this transaction, and
|
|
157
|
+
* drops them otherwise, as they no longer fit; so does resizing it, as
|
|
158
|
+
* its ports move.
|
|
159
|
+
*/
|
|
109
160
|
updateNode(id, patch) {
|
|
110
|
-
|
|
161
|
+
const node = this.getNode(id);
|
|
162
|
+
const changed = (key) => node !== void 0 && key in patch && patch[key] !== node[key];
|
|
163
|
+
const moved = changed("x") || changed("y");
|
|
164
|
+
const resized = changed("width") || changed("height");
|
|
165
|
+
this.step({
|
|
111
166
|
type: "updateNode",
|
|
112
167
|
id,
|
|
113
168
|
patch
|
|
114
169
|
});
|
|
170
|
+
if (moved || resized) this.carryPoints(id, resized);
|
|
171
|
+
return this;
|
|
115
172
|
}
|
|
116
173
|
moveNode(id, x, y) {
|
|
174
|
+
const node = this.getNode(id);
|
|
175
|
+
if (node && node.x === x && node.y === y) return this;
|
|
117
176
|
return this.updateNode(id, {
|
|
118
177
|
x,
|
|
119
178
|
y
|
|
120
179
|
});
|
|
121
180
|
}
|
|
181
|
+
/** Moves the points of the edges at a moved node along, or drops them. */
|
|
182
|
+
carryPoints(id, drop) {
|
|
183
|
+
const shift = this.shiftOf(id);
|
|
184
|
+
for (const edge of this.edgesOf(id)) {
|
|
185
|
+
const before = this.original.edges.get(edge.id);
|
|
186
|
+
const other = this.shiftOf(edge.source.node === id ? edge.target.node : edge.source.node);
|
|
187
|
+
const together = !drop && shift && other && other.x === shift.x && other.y === shift.y;
|
|
188
|
+
if (before?.points && together) this.updateEdge(edge.id, { points: before.points.map((p) => ({
|
|
189
|
+
x: p.x + shift.x,
|
|
190
|
+
y: p.y + shift.y
|
|
191
|
+
})) });
|
|
192
|
+
else if (edge.points) this.updateEdge(edge.id, { points: void 0 });
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
/** How far a node has moved in this transaction so far. */
|
|
196
|
+
shiftOf(id) {
|
|
197
|
+
const now = this.getNode(id);
|
|
198
|
+
const was = this.original.nodes.get(id);
|
|
199
|
+
return now && was ? {
|
|
200
|
+
x: now.x - was.x,
|
|
201
|
+
y: now.y - was.y
|
|
202
|
+
} : void 0;
|
|
203
|
+
}
|
|
122
204
|
/** Merges `patch` into the node's `data`. */
|
|
123
205
|
updateData(id, patch) {
|
|
124
206
|
const node = this.getNode(id);
|
|
@@ -164,15 +246,19 @@ var FlowTransaction = class {
|
|
|
164
246
|
const list = this.writable("nodes");
|
|
165
247
|
const index = clampIndex(step.index, list.length);
|
|
166
248
|
list.splice(index, 0, step.node);
|
|
249
|
+
this.reindex(this.index.nodes, list, index);
|
|
167
250
|
return {
|
|
168
251
|
type: "removeNode",
|
|
169
252
|
id: step.node.id
|
|
170
253
|
};
|
|
171
254
|
}
|
|
172
255
|
case "removeNode": {
|
|
173
|
-
const index = this.
|
|
256
|
+
const index = this.nodeIndex(step.id);
|
|
174
257
|
if (index < 0) return null;
|
|
175
|
-
const
|
|
258
|
+
const list = this.writable("nodes");
|
|
259
|
+
const [node] = list.splice(index, 1);
|
|
260
|
+
this.index.nodes?.delete(step.id);
|
|
261
|
+
this.reindex(this.index.nodes, list, index);
|
|
176
262
|
return {
|
|
177
263
|
type: "addNode",
|
|
178
264
|
node,
|
|
@@ -180,7 +266,7 @@ var FlowTransaction = class {
|
|
|
180
266
|
};
|
|
181
267
|
}
|
|
182
268
|
case "updateNode": {
|
|
183
|
-
const index = this.
|
|
269
|
+
const index = this.nodeIndex(step.id);
|
|
184
270
|
if (index < 0) return null;
|
|
185
271
|
const node = this.nodes[index];
|
|
186
272
|
if (Object.keys(step.patch).every((key) => sameValue(node[key], step.patch[key]))) return null;
|
|
@@ -195,16 +281,23 @@ var FlowTransaction = class {
|
|
|
195
281
|
case "addEdge": {
|
|
196
282
|
if (this.getEdge(step.edge.id)) return null;
|
|
197
283
|
const list = this.writable("edges");
|
|
198
|
-
|
|
284
|
+
const index = clampIndex(step.index, list.length);
|
|
285
|
+
list.splice(index, 0, step.edge);
|
|
286
|
+
this.reindex(this.index.edges, list, index);
|
|
287
|
+
this.attach(step.edge);
|
|
199
288
|
return {
|
|
200
289
|
type: "removeEdge",
|
|
201
290
|
id: step.edge.id
|
|
202
291
|
};
|
|
203
292
|
}
|
|
204
293
|
case "removeEdge": {
|
|
205
|
-
const index = this.
|
|
294
|
+
const index = this.edgeIndex(step.id);
|
|
206
295
|
if (index < 0) return null;
|
|
207
|
-
const
|
|
296
|
+
const list = this.writable("edges");
|
|
297
|
+
const [edge] = list.splice(index, 1);
|
|
298
|
+
this.index.edges?.delete(step.id);
|
|
299
|
+
this.reindex(this.index.edges, list, index);
|
|
300
|
+
this.detach(edge);
|
|
208
301
|
return {
|
|
209
302
|
type: "addEdge",
|
|
210
303
|
edge,
|
|
@@ -212,12 +305,16 @@ var FlowTransaction = class {
|
|
|
212
305
|
};
|
|
213
306
|
}
|
|
214
307
|
case "updateEdge": {
|
|
215
|
-
const index = this.
|
|
308
|
+
const index = this.edgeIndex(step.id);
|
|
216
309
|
if (index < 0) return null;
|
|
217
310
|
const edge = this.edges[index];
|
|
218
311
|
if (Object.keys(step.patch).every((key) => sameValue(edge[key], step.patch[key]))) return null;
|
|
219
312
|
const list = this.writable("edges");
|
|
220
313
|
list[index] = patched(edge, step.patch);
|
|
314
|
+
if ("source" in step.patch || "target" in step.patch) {
|
|
315
|
+
this.detach(edge);
|
|
316
|
+
this.attach(list[index]);
|
|
317
|
+
}
|
|
221
318
|
return {
|
|
222
319
|
type: "updateEdge",
|
|
223
320
|
id: step.id,
|
|
@@ -278,10 +375,10 @@ function compactFlowSteps(steps) {
|
|
|
278
375
|
}
|
|
279
376
|
var warnedUncancellable = false;
|
|
280
377
|
/** Says once that cancelling a change kept out of the history does nothing. */
|
|
281
|
-
function warnUncancellable() {
|
|
378
|
+
function warnUncancellable(origin) {
|
|
282
379
|
if (process.env.NODE_ENV === "production" || warnedUncancellable) return;
|
|
283
380
|
warnedUncancellable = true;
|
|
284
|
-
console.warn(
|
|
381
|
+
console.warn(`[@dou-ui/flow] a change kept out of the history can't be cancelled; it goes ahead (meta.origin: ${JSON.stringify(origin ?? null)}). Undo, redo, other people's changes and the marks of comments are such changes: skip them in the hook by meta.origin`);
|
|
285
382
|
}
|
|
286
383
|
export { FlowTransaction, applyFlowSteps, compactFlowSteps };
|
|
287
384
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"transaction.js","names":[],"sources":["../../src/core/transaction.ts"],"sourcesContent":["import type { FlowDocument, FlowEdge, FlowNode } from './model'\n\n/*\n * Every change to a document is a list of steps. Applying a step also\n * gives the step that undoes it, so history, collaboration and the\n * `transaction` event all work on the same small vocabulary.\n */\n\ntype Patch<T> = { [K in keyof T]?: T[K] | undefined }\n\nexport type FlowStep =\n | { type: 'addNode'; node: FlowNode; index?: number }\n | { type: 'removeNode'; id: string }\n | { type: 'updateNode'; id: string; patch: Patch<FlowNode> }\n | { type: 'addEdge'; edge: FlowEdge; index?: number }\n | { type: 'removeEdge'; id: string }\n | { type: 'updateEdge'; id: string; patch: Patch<FlowEdge> }\n\nexport interface FlowTransactionMeta {\n /** Keep out of the undo history, e.g. changes from another client. */\n history?: boolean\n /**\n * Consecutive transactions in the same group within a second undo as\n * one, e.g. the frames of a drag or repeated arrow key nudges.\n */\n group?: string\n /** Who made the change, passed on to the `transaction` event. */\n origin?: string\n}\n\nfunction patched<T extends object>(item: T, patch: Patch<T>): T {\n const next = { ...item } as Record<string, unknown>\n for (const key of Object.keys(patch)) {\n const value = (patch as Record<string, unknown>)[key]\n if (value === undefined) delete next[key]\n else next[key] = value\n }\n return next as T\n}\n\n/** The values `patch` would replace, so applying them restores `item`. */\nfunction previous<T extends object>(item: T, patch: Patch<T>): Patch<T> {\n const old: Record<string, unknown> = {}\n for (const key of Object.keys(patch)) old[key] = (item as Record<string, unknown>)[key]\n return old as Patch<T>\n}\n\nconst sameValue = (a: unknown, b: unknown) => a === b\n\n/**\n * A batch of changes to a document. Steps apply as they are added, so\n * later calls see earlier ones; `doc` is the result.\n */\nexport class FlowTransaction {\n readonly steps: FlowStep[] = []\n /** The steps that undo this transaction, in the order to apply them. */\n readonly inverse: FlowStep[] = []\n meta: FlowTransactionMeta = {}\n private nodes: FlowNode[]\n private edges: FlowEdge[]\n private copied = { nodes: false, edges: false }\n\n constructor(readonly before: FlowDocument) {\n this.nodes = before.nodes\n this.edges = before.edges\n }\n\n get doc(): FlowDocument {\n if (!this.steps.length) return this.before\n return { ...this.before, nodes: this.nodes, edges: this.edges }\n }\n\n /** Whether the change makes any change: it has steps and wasn't cancelled. */\n get changed() {\n return this.steps.length > 0 && !this.stopped\n }\n\n private stopped: { reason?: string } | null = null\n\n /**\n * Stops the change: none of it is applied or kept for undo, and the\n * editor fires `transactionCancel` and says why. For an extension's `transaction`\n * hook, e.g. to keep people from changing what they may not; hooks after\n * it don't run. A change kept out of the history (`meta.history: false`),\n * such as undo, redo or another person's, can't be cancelled and goes ahead.\n */\n cancel(reason?: string) {\n if (this.meta.history !== false) this.stopped = { reason }\n else warnUncancellable()\n return this\n }\n\n /** Whether `cancel` stopped it. */\n get cancelled() {\n return this.stopped !== null\n }\n\n /** What `cancel` was given, to tell people why. */\n get cancelReason() {\n return this.stopped?.reason\n }\n\n getNode(id: string) {\n return this.nodes.find((node) => node.id === id)\n }\n\n getEdge(id: string) {\n return this.edges.find((edge) => edge.id === id)\n }\n\n /** Edges that start or end at a node. */\n edgesOf(id: string) {\n return this.edges.filter((edge) => edge.source.node === id || edge.target.node === id)\n }\n\n setMeta(meta: FlowTransactionMeta) {\n Object.assign(this.meta, meta)\n return this\n }\n\n step(step: FlowStep) {\n const inverse = this.apply(step)\n if (inverse) {\n this.steps.push(step)\n this.inverse.unshift(inverse)\n }\n return this\n }\n\n addNode(node: FlowNode, index?: number) {\n return this.step({ type: 'addNode', node, index })\n }\n\n /** Removes a node and every edge attached to it. */\n removeNode(id: string) {\n for (const edge of this.edgesOf(id)) this.removeEdge(edge.id)\n return this.step({ type: 'removeNode', id })\n }\n\n updateNode(id: string, patch: Patch<FlowNode>) {\n return this.step({ type: 'updateNode', id, patch })\n }\n\n moveNode(id: string, x: number, y: number) {\n return this.updateNode(id, { x, y })\n }\n\n /** Merges `patch` into the node's `data`. */\n updateData(id: string, patch: Record<string, unknown>) {\n const node = this.getNode(id)\n if (!node) return this\n return this.updateNode(id, { data: { ...node.data, ...patch } })\n }\n\n addEdge(edge: FlowEdge, index?: number) {\n return this.step({ type: 'addEdge', edge, index })\n }\n\n removeEdge(id: string) {\n return this.step({ type: 'removeEdge', id })\n }\n\n updateEdge(id: string, patch: Patch<FlowEdge>) {\n return this.step({ type: 'updateEdge', id, patch })\n }\n\n private writable(kind: 'nodes' | 'edges') {\n if (!this.copied[kind]) {\n this.copied[kind] = true\n if (kind === 'nodes') this.nodes = this.nodes.slice()\n else this.edges = this.edges.slice()\n }\n return kind === 'nodes' ? this.nodes : this.edges\n }\n\n /** Applies a step and returns its inverse, or `null` when it changes nothing. */\n private apply(step: FlowStep): FlowStep | null {\n switch (step.type) {\n case 'addNode': {\n if (this.getNode(step.node.id)) return null\n const list = this.writable('nodes') as FlowNode[]\n const index = clampIndex(step.index, list.length)\n list.splice(index, 0, step.node)\n return { type: 'removeNode', id: step.node.id }\n }\n case 'removeNode': {\n const index = this.nodes.findIndex((node) => node.id === step.id)\n if (index < 0) return null\n const list = this.writable('nodes') as FlowNode[]\n const [node] = list.splice(index, 1)\n return { type: 'addNode', node: node!, index }\n }\n case 'updateNode': {\n const index = this.nodes.findIndex((node) => node.id === step.id)\n if (index < 0) return null\n const node = this.nodes[index]!\n const keys = Object.keys(step.patch) as (keyof FlowNode)[]\n if (keys.every((key) => sameValue(node[key], step.patch[key]))) return null\n const list = this.writable('nodes') as FlowNode[]\n list[index] = patched(node, step.patch)\n return { type: 'updateNode', id: step.id, patch: previous(node, step.patch) }\n }\n case 'addEdge': {\n if (this.getEdge(step.edge.id)) return null\n const list = this.writable('edges') as FlowEdge[]\n list.splice(clampIndex(step.index, list.length), 0, step.edge)\n return { type: 'removeEdge', id: step.edge.id }\n }\n case 'removeEdge': {\n const index = this.edges.findIndex((edge) => edge.id === step.id)\n if (index < 0) return null\n const list = this.writable('edges') as FlowEdge[]\n const [edge] = list.splice(index, 1)\n return { type: 'addEdge', edge: edge!, index }\n }\n case 'updateEdge': {\n const index = this.edges.findIndex((edge) => edge.id === step.id)\n if (index < 0) return null\n const edge = this.edges[index]!\n const keys = Object.keys(step.patch) as (keyof FlowEdge)[]\n if (keys.every((key) => sameValue(edge[key], step.patch[key]))) return null\n const list = this.writable('edges') as FlowEdge[]\n list[index] = patched(edge, step.patch)\n return { type: 'updateEdge', id: step.id, patch: previous(edge, step.patch) }\n }\n }\n }\n}\n\nfunction clampIndex(index: number | undefined, length: number) {\n if (index == null) return length\n return Math.max(0, Math.min(length, index))\n}\n\n/** Applies steps to a document; steps that no longer apply are skipped. */\nexport function applyFlowSteps(doc: FlowDocument, steps: readonly FlowStep[]) {\n const tr = new FlowTransaction(doc)\n for (const step of steps) tr.step(step)\n return tr\n}\n\nconst targetOf = (step: FlowStep) => {\n switch (step.type) {\n case 'addNode':\n return `n:${step.node.id}`\n case 'addEdge':\n return `e:${step.edge.id}`\n case 'removeNode':\n case 'updateNode':\n return `n:${step.id}`\n case 'removeEdge':\n case 'updateEdge':\n return `e:${step.id}`\n }\n}\n\n/**\n * Folds updates of the same node or edge into one, as long as nothing\n * adds or removes it in between. Updates of different targets commute, so\n * the result applies the same as the original list.\n */\nexport function compactFlowSteps(steps: readonly FlowStep[]): FlowStep[] {\n const out: (FlowStep | null)[] = []\n const lastUpdate = new Map<string, number>()\n for (const step of steps) {\n const target = targetOf(step)\n if (step.type === 'updateNode' || step.type === 'updateEdge') {\n const at = lastUpdate.get(target)\n if (at != null) {\n const prev = out[at] as typeof step\n out[at] = { ...prev, patch: { ...prev.patch, ...step.patch } } as FlowStep\n continue\n }\n lastUpdate.set(target, out.length)\n } else {\n lastUpdate.delete(target)\n }\n out.push(step)\n }\n return out.filter((step): step is FlowStep => step != null)\n}\n\nlet warnedUncancellable = false\n/** Says once that cancelling a change kept out of the history does nothing. */\nfunction warnUncancellable() {\n // the app's bundler replaces this, as for Vue's own warnings\n if (process.env.NODE_ENV === 'production' || warnedUncancellable) return\n warnedUncancellable = true\n console.warn(\n \"[@dou-ui/flow] a change kept out of the history (undo, redo or another person's) can't be cancelled; it goes ahead\",\n )\n}\n"],"mappings":";AA8BA,SAAS,QAA0B,MAAS,OAAoB;CAC9D,MAAM,OAAO,EAAE,GAAG,KAAK;CACvB,KAAK,MAAM,OAAO,OAAO,KAAK,KAAK,GAAG;EACpC,MAAM,QAAS,MAAkC;EACjD,IAAI,UAAU,KAAA,GAAW,OAAO,KAAK;OAChC,KAAK,OAAO;CACnB;CACA,OAAO;AACT;;AAGA,SAAS,SAA2B,MAAS,OAA2B;CACtE,MAAM,MAA+B,CAAC;CACtC,KAAK,MAAM,OAAO,OAAO,KAAK,KAAK,GAAG,IAAI,OAAQ,KAAiC;CACnF,OAAO;AACT;AAEA,IAAM,aAAa,GAAY,MAAe,MAAM;;;;;AAMpD,IAAa,kBAAb,MAA6B;CASN;CARrB,QAA6B,CAAC;;CAE9B,UAA+B,CAAC;CAChC,OAA4B,CAAC;CAC7B;CACA;CACA,SAAiB;EAAE,OAAO;EAAO,OAAO;CAAM;CAE9C,YAAY,QAA+B;EAAtB,KAAA,SAAA;EACnB,KAAK,QAAQ,OAAO;EACpB,KAAK,QAAQ,OAAO;CACtB;CAEA,IAAI,MAAoB;EACtB,IAAI,CAAC,KAAK,MAAM,QAAQ,OAAO,KAAK;EACpC,OAAO;GAAE,GAAG,KAAK;GAAQ,OAAO,KAAK;GAAO,OAAO,KAAK;EAAM;CAChE;;CAGA,IAAI,UAAU;EACZ,OAAO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BAAgB,qBAAqB;CAClE,sBAAsB;CACtB,QAAQ,KACN,oHACF;AACF"}
|
|
1
|
+
{"version":3,"file":"transaction.js","names":[],"sources":["../../src/core/transaction.ts"],"sourcesContent":["import type { FlowDocument, FlowEdge, FlowNode } from './model'\n\n/*\n * Every change to a document is a list of steps. Applying a step also\n * gives the step that undoes it, so history, collaboration and the\n * `transaction` event all work on the same small vocabulary.\n */\n\ntype Patch<T> = { [K in keyof T]?: T[K] | undefined }\n\nexport type FlowStep =\n | { type: 'addNode'; node: FlowNode; index?: number }\n | { type: 'removeNode'; id: string }\n | { type: 'updateNode'; id: string; patch: Patch<FlowNode> }\n | { type: 'addEdge'; edge: FlowEdge; index?: number }\n | { type: 'removeEdge'; id: string }\n | { type: 'updateEdge'; id: string; patch: Patch<FlowEdge> }\n\nexport interface FlowTransactionMeta {\n /** Keep out of the undo history, e.g. changes from another client. */\n history?: boolean\n /**\n * Consecutive transactions in the same group within a second undo as\n * one, e.g. the frames of a drag or repeated arrow key nudges.\n */\n group?: string\n /** Who made the change, passed on to the `transaction` event. */\n origin?: string\n}\n\nfunction patched<T extends object>(item: T, patch: Patch<T>): T {\n const next = { ...item } as Record<string, unknown>\n for (const key of Object.keys(patch)) {\n const value = (patch as Record<string, unknown>)[key]\n if (value === undefined) delete next[key]\n else next[key] = value\n }\n return next as T\n}\n\n/** The values `patch` would replace, so applying them restores `item`. */\nfunction previous<T extends object>(item: T, patch: Patch<T>): Patch<T> {\n const old: Record<string, unknown> = {}\n for (const key of Object.keys(patch)) old[key] = (item as Record<string, unknown>)[key]\n return old as Patch<T>\n}\n\nconst sameValue = (a: unknown, b: unknown) => a === b\n\n/**\n * A batch of changes to a document. Steps apply as they are added, so\n * later calls see earlier ones; `doc` is the result.\n */\nexport class FlowTransaction {\n readonly steps: FlowStep[] = []\n /** The steps that undo this transaction, in the order to apply them. */\n readonly inverse: FlowStep[] = []\n meta: FlowTransactionMeta = {}\n private nodes: FlowNode[]\n private edges: FlowEdge[]\n private copied = { nodes: false, edges: false }\n\n constructor(readonly before: FlowDocument) {\n this.nodes = before.nodes\n this.edges = before.edges\n }\n\n get doc(): FlowDocument {\n if (!this.steps.length) return this.before\n return { ...this.before, nodes: this.nodes, edges: this.edges }\n }\n\n /** Whether the change makes any change: it has steps and wasn't cancelled. */\n get changed() {\n return this.steps.length > 0 && !this.stopped\n }\n\n private stopped: { reason?: string } | null = null\n\n /**\n * Stops the change: none of it is applied or kept for undo, and the\n * editor fires `transactionCancel` and says why. For an extension's `transaction`\n * hook, e.g. to keep people from changing what they may not; hooks after\n * it don't run. A change kept out of the history (`meta.history: false`),\n * such as undo, redo, another person's or a comment's (`meta.origin`\n * tells which), can't be cancelled and goes ahead.\n */\n cancel(reason?: string) {\n if (this.meta.history !== false) this.stopped = { reason }\n else warnUncancellable(this.meta.origin)\n return this\n }\n\n /** Whether `cancel` stopped it. */\n get cancelled() {\n return this.stopped !== null\n }\n\n /** What `cancel` was given, to tell people why. */\n get cancelReason() {\n return this.stopped?.reason\n }\n\n // Indexes built when first needed and kept up to date after, so moving\n // or removing many nodes of a large document doesn't search it each time.\n private index: { nodes?: Map<string, number>; edges?: Map<string, number> } = {}\n private incident?: Map<string, string[]>\n private was?: { nodes: Map<string, FlowNode>; edges: Map<string, FlowEdge> }\n\n private nodeIndex(id: string) {\n this.index.nodes ??= new Map(this.nodes.map((node, i) => [node.id, i]))\n return this.index.nodes.get(id) ?? -1\n }\n\n private edgeIndex(id: string) {\n this.index.edges ??= new Map(this.edges.map((edge, i) => [edge.id, i]))\n return this.index.edges.get(id) ?? -1\n }\n\n /** The document as it was before this transaction. */\n private get original() {\n this.was ??= {\n nodes: new Map(this.before.nodes.map((node) => [node.id, node])),\n edges: new Map(this.before.edges.map((edge) => [edge.id, edge])),\n }\n return this.was\n }\n\n getNode(id: string) {\n return this.nodes[this.nodeIndex(id)]\n }\n\n getEdge(id: string) {\n return this.edges[this.edgeIndex(id)]\n }\n\n /** Edges that start or end at a node. */\n edgesOf(id: string) {\n if (!this.incident) {\n this.incident = new Map()\n for (const edge of this.edges) this.attach(edge)\n }\n return (this.incident.get(id) ?? []).flatMap((e) => this.getEdge(e) ?? [])\n }\n\n private attach(edge: FlowEdge) {\n if (!this.incident) return\n for (const end of new Set([edge.source.node, edge.target.node])) {\n const list = this.incident.get(end)\n if (list) list.push(edge.id)\n else this.incident.set(end, [edge.id])\n }\n }\n\n private detach(edge: FlowEdge) {\n if (!this.incident) return\n for (const end of new Set([edge.source.node, edge.target.node])) {\n const list = this.incident.get(end)\n const at = list?.indexOf(edge.id) ?? -1\n if (at >= 0) list!.splice(at, 1)\n }\n }\n\n /** Renumbers an index from `from` on, after an insert or removal there. */\n private reindex(map: Map<string, number> | undefined, list: { id: string }[], from: number) {\n if (!map) return\n for (let i = from; i < list.length; i++) map.set(list[i]!.id, i)\n }\n\n setMeta(meta: FlowTransactionMeta) {\n Object.assign(this.meta, meta)\n return this\n }\n\n step(step: FlowStep) {\n const inverse = this.apply(step)\n if (inverse) {\n this.steps.push(step)\n this.inverse.unshift(inverse)\n }\n return this\n }\n\n addNode(node: FlowNode, index?: number) {\n return this.step({ type: 'addNode', node, index })\n }\n\n /** Removes a node and every edge attached to it. */\n removeNode(id: string) {\n for (const edge of this.edgesOf(id)) this.removeEdge(edge.id)\n return this.step({ type: 'removeNode', id })\n }\n\n /**\n * Changes a node. Moving it moves the points of an edge at it along when\n * the edge's other end has moved just as far in this transaction, and\n * drops them otherwise, as they no longer fit; so does resizing it, as\n * its ports move.\n */\n updateNode(id: string, patch: Patch<FlowNode>) {\n const node = this.getNode(id)\n const changed = (key: 'x' | 'y' | 'width' | 'height') =>\n node !== undefined && key in patch && patch[key] !== node[key]\n const moved = changed('x') || changed('y')\n const resized = changed('width') || changed('height')\n this.step({ type: 'updateNode', id, patch })\n if (moved || resized) this.carryPoints(id, resized)\n return this\n }\n\n moveNode(id: string, x: number, y: number) {\n const node = this.getNode(id)\n if (node && node.x === x && node.y === y) return this\n return this.updateNode(id, { x, y })\n }\n\n /** Moves the points of the edges at a moved node along, or drops them. */\n private carryPoints(id: string, drop: boolean) {\n const shift = this.shiftOf(id)\n for (const edge of this.edgesOf(id)) {\n const before = this.original.edges.get(edge.id)\n const other = this.shiftOf(edge.source.node === id ? edge.target.node : edge.source.node)\n const together = !drop && shift && other && other.x === shift.x && other.y === shift.y\n if (before?.points && together)\n this.updateEdge(edge.id, {\n points: before.points.map((p) => ({ x: p.x + shift!.x, y: p.y + shift!.y })),\n })\n else if (edge.points) this.updateEdge(edge.id, { points: undefined })\n }\n }\n\n /** How far a node has moved in this transaction so far. */\n private shiftOf(id: string) {\n const now = this.getNode(id)\n const was = this.original.nodes.get(id)\n return now && was ? { x: now.x - was.x, y: now.y - was.y } : undefined\n }\n\n /** Merges `patch` into the node's `data`. */\n updateData(id: string, patch: Record<string, unknown>) {\n const node = this.getNode(id)\n if (!node) return this\n return this.updateNode(id, { data: { ...node.data, ...patch } })\n }\n\n addEdge(edge: FlowEdge, index?: number) {\n return this.step({ type: 'addEdge', edge, index })\n }\n\n removeEdge(id: string) {\n return this.step({ type: 'removeEdge', id })\n }\n\n updateEdge(id: string, patch: Patch<FlowEdge>) {\n return this.step({ type: 'updateEdge', id, patch })\n }\n\n private writable(kind: 'nodes' | 'edges') {\n if (!this.copied[kind]) {\n this.copied[kind] = true\n if (kind === 'nodes') this.nodes = this.nodes.slice()\n else this.edges = this.edges.slice()\n }\n return kind === 'nodes' ? this.nodes : this.edges\n }\n\n /** Applies a step and returns its inverse, or `null` when it changes nothing. */\n private apply(step: FlowStep): FlowStep | null {\n switch (step.type) {\n case 'addNode': {\n if (this.getNode(step.node.id)) return null\n const list = this.writable('nodes') as FlowNode[]\n const index = clampIndex(step.index, list.length)\n list.splice(index, 0, step.node)\n this.reindex(this.index.nodes, list, index)\n return { type: 'removeNode', id: step.node.id }\n }\n case 'removeNode': {\n const index = this.nodeIndex(step.id)\n if (index < 0) return null\n const list = this.writable('nodes') as FlowNode[]\n const [node] = list.splice(index, 1)\n this.index.nodes?.delete(step.id)\n this.reindex(this.index.nodes, list, index)\n return { type: 'addNode', node: node!, index }\n }\n case 'updateNode': {\n const index = this.nodeIndex(step.id)\n if (index < 0) return null\n const node = this.nodes[index]!\n const keys = Object.keys(step.patch) as (keyof FlowNode)[]\n if (keys.every((key) => sameValue(node[key], step.patch[key]))) return null\n const list = this.writable('nodes') as FlowNode[]\n list[index] = patched(node, step.patch)\n return { type: 'updateNode', id: step.id, patch: previous(node, step.patch) }\n }\n case 'addEdge': {\n if (this.getEdge(step.edge.id)) return null\n const list = this.writable('edges') as FlowEdge[]\n const index = clampIndex(step.index, list.length)\n list.splice(index, 0, step.edge)\n this.reindex(this.index.edges, list, index)\n this.attach(step.edge)\n return { type: 'removeEdge', id: step.edge.id }\n }\n case 'removeEdge': {\n const index = this.edgeIndex(step.id)\n if (index < 0) return null\n const list = this.writable('edges') as FlowEdge[]\n const [edge] = list.splice(index, 1)\n this.index.edges?.delete(step.id)\n this.reindex(this.index.edges, list, index)\n this.detach(edge!)\n return { type: 'addEdge', edge: edge!, index }\n }\n case 'updateEdge': {\n const index = this.edgeIndex(step.id)\n if (index < 0) return null\n const edge = this.edges[index]!\n const keys = Object.keys(step.patch) as (keyof FlowEdge)[]\n if (keys.every((key) => sameValue(edge[key], step.patch[key]))) return null\n const list = this.writable('edges') as FlowEdge[]\n list[index] = patched(edge, step.patch)\n if ('source' in step.patch || 'target' in step.patch) {\n this.detach(edge)\n this.attach(list[index]!)\n }\n return { type: 'updateEdge', id: step.id, patch: previous(edge, step.patch) }\n }\n }\n }\n}\n\nfunction clampIndex(index: number | undefined, length: number) {\n if (index == null) return length\n return Math.max(0, Math.min(length, index))\n}\n\n/** Applies steps to a document; steps that no longer apply are skipped. */\nexport function applyFlowSteps(doc: FlowDocument, steps: readonly FlowStep[]) {\n const tr = new FlowTransaction(doc)\n for (const step of steps) tr.step(step)\n return tr\n}\n\nconst targetOf = (step: FlowStep) => {\n switch (step.type) {\n case 'addNode':\n return `n:${step.node.id}`\n case 'addEdge':\n return `e:${step.edge.id}`\n case 'removeNode':\n case 'updateNode':\n return `n:${step.id}`\n case 'removeEdge':\n case 'updateEdge':\n return `e:${step.id}`\n }\n}\n\n/**\n * Folds updates of the same node or edge into one, as long as nothing\n * adds or removes it in between. Updates of different targets commute, so\n * the result applies the same as the original list.\n */\nexport function compactFlowSteps(steps: readonly FlowStep[]): FlowStep[] {\n const out: (FlowStep | null)[] = []\n const lastUpdate = new Map<string, number>()\n for (const step of steps) {\n const target = targetOf(step)\n if (step.type === 'updateNode' || step.type === 'updateEdge') {\n const at = lastUpdate.get(target)\n if (at != null) {\n const prev = out[at] as typeof step\n out[at] = { ...prev, patch: { ...prev.patch, ...step.patch } } as FlowStep\n continue\n }\n lastUpdate.set(target, out.length)\n } else {\n lastUpdate.delete(target)\n }\n out.push(step)\n }\n return out.filter((step): step is FlowStep => step != null)\n}\n\nlet warnedUncancellable = false\n/** Says once that cancelling a change kept out of the history does nothing. */\nfunction warnUncancellable(origin: unknown) {\n // the app's bundler replaces this, as for Vue's own warnings\n if (process.env.NODE_ENV === 'production' || warnedUncancellable) return\n warnedUncancellable = true\n console.warn(\n `[@dou-ui/flow] a change kept out of the history can't be cancelled; it goes ahead (meta.origin: ${JSON.stringify(origin ?? null)}). Undo, redo, other people's changes and the marks of comments are such changes: skip them in the hook by meta.origin`,\n )\n}\n"],"mappings":";AA8BA,SAAS,QAA0B,MAAS,OAAoB;CAC9D,MAAM,OAAO,EAAE,GAAG,KAAK;CACvB,KAAK,MAAM,OAAO,OAAO,KAAK,KAAK,GAAG;EACpC,MAAM,QAAS,MAAkC;EACjD,IAAI,UAAU,KAAA,GAAW,OAAO,KAAK;OAChC,KAAK,OAAO;CACnB;CACA,OAAO;AACT;;AAGA,SAAS,SAA2B,MAAS,OAA2B;CACtE,MAAM,MAA+B,CAAC;CACtC,KAAK,MAAM,OAAO,OAAO,KAAK,KAAK,GAAG,IAAI,OAAQ,KAAiC;CACnF,OAAO;AACT;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;AACF"}
|
package/dist/document.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { portsOf } from "./core/node-types.js";
|
|
2
|
-
import { defaultNode } from "./components/default-node2.js";
|
|
3
2
|
import { resolveFlowExtensions } from "./core/extension.js";
|
|
3
|
+
import { defaultNode } from "./components/default-node2.js";
|
|
4
4
|
import { flowKit } from "./kit.js";
|
|
5
5
|
// src/document.ts
|
|
6
6
|
var isNumber = (value) => typeof value === "number" && Number.isFinite(value);
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import flow_editor_default from "../flow-editor.js";
|
|
2
|
+
import { installedAppContext } from "dou-ui";
|
|
3
|
+
import { h, nextTick, render } from "vue";
|
|
4
|
+
// src/export/render.ts
|
|
5
|
+
var frame = () => new Promise((resolve) => typeof requestAnimationFrame === "undefined" ? setTimeout(resolve, 16) : requestAnimationFrame(() => resolve()));
|
|
6
|
+
/**
|
|
7
|
+
* Draws the document in an editor of its own, on the page but not seen, and
|
|
8
|
+
* hands the editor to `use` once every node has been laid out and measured;
|
|
9
|
+
* the editor is removed after. `name` is the function called, for errors.
|
|
10
|
+
*/
|
|
11
|
+
async function withEditor(name, document, options, use) {
|
|
12
|
+
if (typeof window === "undefined") throw new Error(`[@dou-ui/flow] ${name} needs a browser: it lays out the nodes on the page`);
|
|
13
|
+
const host = window.document.createElement("div");
|
|
14
|
+
host.setAttribute("aria-hidden", "true");
|
|
15
|
+
host.inert = true;
|
|
16
|
+
host.style.cssText = "position:fixed;top:0;left:0;width:800px;height:600px;overflow:hidden;visibility:hidden;pointer-events:none;contain:strict;z-index:-1";
|
|
17
|
+
if (options.colorMode) host.setAttribute("data-dou-theme", options.colorMode);
|
|
18
|
+
window.document.body.append(host);
|
|
19
|
+
const vnode = h(flow_editor_default, {
|
|
20
|
+
modelValue: document,
|
|
21
|
+
extensions: options.extensions,
|
|
22
|
+
nodeTypes: options.nodeTypes,
|
|
23
|
+
edgeTypes: options.edgeTypes,
|
|
24
|
+
edgeType: options.edgeType,
|
|
25
|
+
portTypes: options.portTypes,
|
|
26
|
+
direction: options.direction,
|
|
27
|
+
messages: options.messages,
|
|
28
|
+
readonly: true,
|
|
29
|
+
fitView: false,
|
|
30
|
+
searchable: false
|
|
31
|
+
});
|
|
32
|
+
vnode.appContext = options.appContext === void 0 ? installedAppContext() : options.appContext;
|
|
33
|
+
try {
|
|
34
|
+
render(vnode, host);
|
|
35
|
+
const root = host.firstElementChild;
|
|
36
|
+
if (root && !getComputedStyle(root).getPropertyValue("--dou-flow-editor-node-bg")) throw new Error(`[@dou-ui/flow] ${name} needs the flow styles, which load with <DouFlowEditor> only: import '@dou-ui/flow/style'`);
|
|
37
|
+
await nextTick();
|
|
38
|
+
await frame();
|
|
39
|
+
if (window.document.fonts?.ready) await window.document.fonts.ready;
|
|
40
|
+
await frame();
|
|
41
|
+
await nextTick();
|
|
42
|
+
const editor = vnode.component?.exposed;
|
|
43
|
+
if (!editor) throw new Error("[@dou-ui/flow] the flow could not be drawn");
|
|
44
|
+
return await use(editor);
|
|
45
|
+
} finally {
|
|
46
|
+
render(null, host);
|
|
47
|
+
host.remove();
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* The document as an SVG document, drawn as `exportSVG` draws it, without an
|
|
52
|
+
* editor on the page: for thumbnails and previews. Give it the same node,
|
|
53
|
+
* edge and port types as the editor that shows the document.
|
|
54
|
+
*/
|
|
55
|
+
function renderFlowSVG(document, options = {}) {
|
|
56
|
+
return withEditor("renderFlowSVG", document, options, (editor) => editor.exportSVG({
|
|
57
|
+
padding: options.padding,
|
|
58
|
+
background: options.background
|
|
59
|
+
}));
|
|
60
|
+
}
|
|
61
|
+
/** The document as a PNG image, as `exportPNG` draws it, without an editor on the page. */
|
|
62
|
+
function renderFlowPNG(document, options = {}) {
|
|
63
|
+
return withEditor("renderFlowPNG", document, options, async (editor) => {
|
|
64
|
+
try {
|
|
65
|
+
return await editor.exportPNG({
|
|
66
|
+
padding: options.padding,
|
|
67
|
+
background: options.background,
|
|
68
|
+
pixelRatio: options.pixelRatio
|
|
69
|
+
});
|
|
70
|
+
} catch (error) {
|
|
71
|
+
if (error instanceof RangeError && error.message.includes("exportSVG()")) throw new RangeError(error.message.replace(/; [^;]*exportSVG\(\)$/, "; use renderFlowSVG() instead"));
|
|
72
|
+
throw error;
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
export { renderFlowPNG, renderFlowSVG };
|
|
77
|
+
|
|
78
|
+
//# sourceMappingURL=render.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"render.js","names":["FlowEditor"],"sources":["../../src/export/render.ts"],"sourcesContent":["import { type ResolvedColorMode, installedAppContext } from 'dou-ui'\nimport { type AppContext, h, nextTick, render } from 'vue'\nimport FlowEditor from '../flow-editor.vue'\nimport type { FlowEditorExpose, FlowEditorProps } from '../context'\nimport type { FlowDocument } from '../core/model'\nimport type { FlowExportOptions, FlowImageOptions } from './draw'\n\nexport interface RenderFlowOptions\n extends\n Pick<\n FlowEditorProps,\n 'extensions' | 'nodeTypes' | 'edgeTypes' | 'edgeType' | 'portTypes' | 'direction' | 'messages'\n >,\n Omit<FlowExportOptions, 'range'> {\n /** Light or dark colors; the page's own color mode by default. */\n colorMode?: ResolvedColorMode\n /**\n * The app context node components render in, for its locale, plugins and\n * provides, e.g. `getCurrentInstance()!.appContext`. Defaults to the app\n * `app.use(DouUI)` was called on.\n */\n appContext?: AppContext | null\n}\n\nexport interface RenderFlowImageOptions extends RenderFlowOptions {\n /** Pixels per CSS pixel. Defaults to `2`. */\n pixelRatio?: number\n}\n\nconst frame = () =>\n new Promise<void>((resolve) =>\n typeof requestAnimationFrame === 'undefined'\n ? setTimeout(resolve, 16)\n : requestAnimationFrame(() => resolve()),\n )\n\n/**\n * Draws the document in an editor of its own, on the page but not seen, and\n * hands the editor to `use` once every node has been laid out and measured;\n * the editor is removed after. `name` is the function called, for errors.\n */\nasync function withEditor<T>(\n name: string,\n document: FlowDocument,\n options: RenderFlowOptions,\n use: (editor: FlowEditorExpose) => T | Promise<T>,\n): Promise<T> {\n if (typeof window === 'undefined')\n throw new Error(`[@dou-ui/flow] ${name} needs a browser: it lays out the nodes on the page`)\n const host = window.document.createElement('div')\n host.setAttribute('aria-hidden', 'true')\n host.inert = true\n // Laid out, so nodes measure, but not seen and never in the way.\n host.style.cssText =\n 'position:fixed;top:0;left:0;width:800px;height:600px;overflow:hidden;visibility:hidden;pointer-events:none;contain:strict;z-index:-1'\n if (options.colorMode) host.setAttribute('data-dou-theme', options.colorMode)\n window.document.body.append(host)\n const vnode = h(FlowEditor, {\n modelValue: document,\n extensions: options.extensions,\n nodeTypes: options.nodeTypes,\n edgeTypes: options.edgeTypes,\n edgeType: options.edgeType,\n portTypes: options.portTypes,\n direction: options.direction,\n messages: options.messages,\n readonly: true,\n fitView: false,\n searchable: false,\n })\n vnode.appContext = options.appContext === undefined ? installedAppContext() : options.appContext\n try {\n render(vnode, host)\n // Without the flow's styles nodes lay out wrong, and the picture with them.\n const root = host.firstElementChild\n if (root && !getComputedStyle(root).getPropertyValue('--dou-flow-editor-node-bg'))\n throw new Error(\n `[@dou-ui/flow] ${name} needs the flow styles, which load with <DouFlowEditor> only: import '@dou-ui/flow/style'`,\n )\n // Nodes measure after they mount, and edges follow their ports; text needs its font.\n await nextTick()\n await frame()\n if (window.document.fonts?.ready) await window.document.fonts.ready\n await frame()\n await nextTick()\n const editor = vnode.component?.exposed as FlowEditorExpose | undefined\n if (!editor) throw new Error('[@dou-ui/flow] the flow could not be drawn')\n return await use(editor)\n } finally {\n render(null, host)\n host.remove()\n }\n}\n\n/**\n * The document as an SVG document, drawn as `exportSVG` draws it, without an\n * editor on the page: for thumbnails and previews. Give it the same node,\n * edge and port types as the editor that shows the document.\n */\nexport function renderFlowSVG(document: FlowDocument, options: RenderFlowOptions = {}) {\n return withEditor('renderFlowSVG', document, options, (editor) =>\n editor.exportSVG({ padding: options.padding, background: options.background }),\n )\n}\n\n/** The document as a PNG image, as `exportPNG` draws it, without an editor on the page. */\nexport function renderFlowPNG(document: FlowDocument, options: RenderFlowImageOptions = {}) {\n return withEditor('renderFlowPNG', document, options, async (editor) => {\n try {\n return await editor.exportPNG({\n padding: options.padding,\n background: options.background,\n pixelRatio: options.pixelRatio,\n } satisfies FlowImageOptions)\n } catch (error) {\n // the editor's advice is about its own ranges; here only the SVG helps\n if (error instanceof RangeError && error.message.includes('exportSVG()'))\n throw new RangeError(\n error.message.replace(/; [^;]*exportSVG\\(\\)$/, '; use renderFlowSVG() instead'),\n )\n throw error\n }\n })\n}\n"],"mappings":";;;;AA6BA,IAAM,cACJ,IAAI,SAAe,YACjB,OAAO,0BAA0B,cAC7B,WAAW,SAAS,EAAE,IACtB,4BAA4B,QAAQ,CAAC,CAC3C;;;;;;AAOF,eAAe,WACb,MACA,UACA,SACA,KACY;CACZ,IAAI,OAAO,WAAW,aACpB,MAAM,IAAI,MAAM,kBAAkB,KAAK,oDAAoD;CAC7F,MAAM,OAAO,OAAO,SAAS,cAAc,KAAK;CAChD,KAAK,aAAa,eAAe,MAAM;CACvC,KAAK,QAAQ;CAEb,KAAK,MAAM,UACT;CACF,IAAI,QAAQ,WAAW,KAAK,aAAa,kBAAkB,QAAQ,SAAS;CAC5E,OAAO,SAAS,KAAK,OAAO,IAAI;CAChC,MAAM,QAAQ,EAAEA,qBAAY;EAC1B,YAAY;EACZ,YAAY,QAAQ;EACpB,WAAW,QAAQ;EACnB,WAAW,QAAQ;EACnB,UAAU,QAAQ;EAClB,WAAW,QAAQ;EACnB,WAAW,QAAQ;EACnB,UAAU,QAAQ;EAClB,UAAU;EACV,SAAS;EACT,YAAY;CACd,CAAC;CACD,MAAM,aAAa,QAAQ,eAAe,KAAA,IAAY,oBAAoB,IAAI,QAAQ;CACtF,IAAI;EACF,OAAO,OAAO,IAAI;EAElB,MAAM,OAAO,KAAK;EAClB,IAAI,QAAQ,CAAC,iBAAiB,IAAI,CAAC,CA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