@dou-ui/flow 0.1.0-alpha.3 → 0.1.0-alpha.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (42) hide show
  1. package/dist/comments.js +177 -0
  2. package/dist/comments.js.map +1 -0
  3. package/dist/components/flow-comments.js +6 -0
  4. package/dist/components/flow-comments.js.map +1 -0
  5. package/dist/components/flow-comments.vue_vue_type_script_setup_true_lang.js +184 -0
  6. package/dist/components/flow-comments.vue_vue_type_script_setup_true_lang.js.map +1 -0
  7. package/dist/context.js.map +1 -1
  8. package/dist/core/history.js +26 -0
  9. package/dist/core/history.js.map +1 -1
  10. package/dist/core/model.js.map +1 -1
  11. package/dist/core/node-types.js +8 -1
  12. package/dist/core/node-types.js.map +1 -1
  13. package/dist/core/transaction.js +5 -4
  14. package/dist/core/transaction.js.map +1 -1
  15. package/dist/document.js +1 -1
  16. package/dist/export/render.js +78 -0
  17. package/dist/export/render.js.map +1 -0
  18. package/dist/flow-editor.js.map +1 -1
  19. package/dist/flow-editor.vue_vue_type_script_setup_true_lang.js +220 -15
  20. package/dist/flow-editor.vue_vue_type_script_setup_true_lang.js.map +1 -1
  21. package/dist/index.js +4 -3
  22. package/dist/index.js.map +1 -1
  23. package/dist/kit.js +1 -1
  24. package/dist/locale.js +6 -0
  25. package/dist/locale.js.map +1 -1
  26. package/dist/style.css +1 -1
  27. package/dist/style.js +1 -1
  28. package/dist/types/comments.d.ts +55 -0
  29. package/dist/types/components/flow-comments.vue.d.ts +28 -0
  30. package/dist/types/context.d.ts +31 -1
  31. package/dist/types/core/history.d.ts +10 -0
  32. package/dist/types/core/menu.d.ts +5 -0
  33. package/dist/types/core/model.d.ts +7 -0
  34. package/dist/types/core/node-types.d.ts +2 -0
  35. package/dist/types/core/transaction.d.ts +2 -1
  36. package/dist/types/export/render.d.ts +27 -0
  37. package/dist/types/flow-editor.vue.d.ts +10 -0
  38. package/dist/types/index.d.ts +30 -1
  39. package/dist/types/locale.d.ts +4 -0
  40. package/dist/whiteboard/connector.js +1 -1
  41. package/dist/whiteboard/kit.js +1 -1
  42. package/package.json +3 -3
@@ -1,8 +1,12 @@
1
1
  import { flowEditorKey } from "./context.js";
2
2
  import { boundsOf, clampPan, intersects, intersectsTurned, rectFrom, snapBox } from "./core/geometry.js";
3
- import { labelOf, nodeBounds, portsOf, resizeOf } from "./core/node-types.js";
3
+ import { labelOf, nodeBounds, portsOf, resizeOf, warnUnknownNodeType } from "./core/node-types.js";
4
4
  import node_view_default from "./components/node-view.js";
5
5
  import flow_menu_default from "./components/flow-menu.js";
6
+ import { format, useFlowMessages } from "./locale.js";
7
+ import flow_comments_default from "./components/flow-comments.js";
8
+ import { resolveFlowExtensions } from "./core/extension.js";
9
+ import { useFlowComments } from "./comments.js";
6
10
  import flow_quick_add_default from "./components/flow-quick-add.js";
7
11
  import flow_toolbar_default from "./components/flow-toolbar.js";
8
12
  import { drawTools } from "./export/context.js";
@@ -13,8 +17,6 @@ import { canJoin, displacedEdges, joinRefusal, primaryType, typesMatch } from ".
13
17
  import { FlowTransaction } from "./core/transaction.js";
14
18
  import { FlowHistory } from "./core/history.js";
15
19
  import { flowOrder } from "./core/order.js";
16
- import { resolveFlowExtensions } from "./core/extension.js";
17
- import { format, useFlowMessages } from "./locale.js";
18
20
  import { flowKit } from "./kit.js";
19
21
  import { readClipboard, writeClipboard } from "./core/clipboard.js";
20
22
  import { createId, emptyDocument, emptySelection } from "./core/model.js";
@@ -99,6 +101,13 @@ var FIND_INSET = 12;
99
101
  var DRAW_MARGIN = 200;
100
102
  /** The part of the canvas that is drawn: the view and a margin around it. */
101
103
  var TINY = .2;
104
+ var EDGE_LABEL_HALF = 10;
105
+ /** How far above an edge's label its marker sits, in screen px; a finger's target adds 8 (style.css). */
106
+ var EDGE_MARKER_LIFT = 14;
107
+ /** A marker's size, and how far apart two markers' middles keep, so a finger's 40px targets don't overlap. */
108
+ var MARKER_SIZE = 24;
109
+ var MARKER_GAP = 32;
110
+ /** A marker's middle, in px within the editor: a node's sits off its top right corner (style.css). */
102
111
  var flow_editor_vue_vue_type_script_setup_true_lang_default = /*@__PURE__*/ defineComponent({
103
112
  name: "DouFlowEditor",
104
113
  __name: "flow-editor",
@@ -141,7 +150,14 @@ var flow_editor_vue_vue_type_script_setup_true_lang_default = /*@__PURE__*/ defi
141
150
  default: true
142
151
  },
143
152
  ariaLabel: {},
144
- messages: {}
153
+ messages: {},
154
+ comments: { default: void 0 },
155
+ author: { default: void 0 },
156
+ activeComment: { default: void 0 },
157
+ commentable: {
158
+ type: Boolean,
159
+ default: true
160
+ }
145
161
  }, {
146
162
  "modelValue": { default: emptyDocument },
147
163
  "modelModifiers": {},
@@ -157,6 +173,9 @@ var flow_editor_vue_vue_type_script_setup_true_lang_default = /*@__PURE__*/ defi
157
173
  emits: /*@__PURE__*/ mergeModels([
158
174
  "transaction",
159
175
  "transactionCancel",
176
+ "update:comments",
177
+ "update:activeComment",
178
+ "commentAction",
160
179
  "nodeClick",
161
180
  "nodeDblclick",
162
181
  "nodeContextmenu",
@@ -195,7 +214,20 @@ var flow_editor_vue_vue_type_script_setup_true_lang_default = /*@__PURE__*/ defi
195
214
  /** The axis that levels two ports on this side. */
196
215
  const level = (side) => side === "left" || side === "right" ? "y" : "x";
197
216
  const same = (a, b) => a.length === b.length && a.every((id, i) => id === b[i]);
198
- const resolved = computed(() => resolveFlowExtensions(props.extensions ?? flowKit()));
217
+ const { comments, extension: commentsExtension, anchors: getCommentAnchors, anchorOf: commentAnchorOf } = useFlowComments({
218
+ props,
219
+ emit,
220
+ doc,
221
+ selection,
222
+ messages,
223
+ transact,
224
+ carry: (on, patch) => !customHistory && history.carry(on, patch),
225
+ shownOn: (on) => commentSpots.value.find((spot) => spot.key === ("node" in on ? `n:${on.node}` : `e:${on.edge}`))?.threads
226
+ });
227
+ const resolved = computed(() => {
228
+ const own = props.extensions ?? flowKit();
229
+ return resolveFlowExtensions(comments.enabled.value ? [...own, commentsExtension] : own);
230
+ });
199
231
  const types = computed(() => {
200
232
  const map = new Map(resolved.value.nodeTypes);
201
233
  for (const spec of props.nodeTypes ?? []) map.set(spec.type, spec);
@@ -213,6 +245,9 @@ var flow_editor_vue_vue_type_script_setup_true_lang_default = /*@__PURE__*/ defi
213
245
  /** The type of edges without one: the prop, else the extensions', else bezier. */
214
246
  const defaultEdgeType = computed(() => props.edgeType ?? resolved.value.edgeType ?? "bezier");
215
247
  const edgeTypeOf = (type) => edgeTypes.value.get(type ?? defaultEdgeType.value) ?? edgeTypes.value.get(defaultEdgeType.value) ?? bezierEdge;
248
+ if (process.env.NODE_ENV !== "production") watch(() => [doc.value.nodes, types.value], ([nodes, map]) => {
249
+ for (const node of nodes) if (node.type && !map.has(node.type)) warnUnknownNodeType(node.type);
250
+ }, { immediate: true });
216
251
  const specOf = (node) => types.value.get(node.type ?? "default") ?? types.value.get("default") ?? defaultNode;
217
252
  /** Each node's ports by id, worked out once per change rather than per edge. */
218
253
  const portsByNode = computed(() => {
@@ -818,7 +853,7 @@ var flow_editor_vue_vue_type_script_setup_true_lang_default = /*@__PURE__*/ defi
818
853
  left: FIT_PADDING,
819
854
  right: FIT_PADDING
820
855
  };
821
- for (const panel of root.value.querySelectorAll(".dou-flow-editor__panels > :not(.dou-flow-toolbar)")) {
856
+ for (const panel of root.value.querySelectorAll(".dou-flow-editor__panels > :not(.dou-flow-toolbar, .dou-flow-editor__comments, .dou-flow-editor__comment-card)")) {
822
857
  const rect = panel.getBoundingClientRect();
823
858
  if (!rect.height) continue;
824
859
  const upper = rect.top + rect.height / 2 < box.top + box.height / 2;
@@ -881,6 +916,7 @@ var flow_editor_vue_vue_type_script_setup_true_lang_default = /*@__PURE__*/ defi
881
916
  const vertical = props.direction === "vertical";
882
917
  const gap = TIDY_GAP[vertical ? "vertical" : "horizontal"];
883
918
  const across = vertical ? ["top", "bottom"] : ["left", "right"];
919
+ const anchors = /* @__PURE__ */ new Map();
884
920
  const layout = layoutGraph(nodes.map((node) => {
885
921
  const m = metrics.get(node.id);
886
922
  const { width, height } = rectOf(node);
@@ -889,6 +925,8 @@ var flow_editor_vue_vue_type_script_setup_true_lang_default = /*@__PURE__*/ defi
889
925
  offset = vertical ? port.x : port.y;
890
926
  break;
891
927
  }
928
+ offset = Math.round(offset);
929
+ anchors.set(node.id, offset);
892
930
  return {
893
931
  id: node.id,
894
932
  width,
@@ -923,9 +961,13 @@ var flow_editor_vue_vue_type_script_setup_true_lang_default = /*@__PURE__*/ defi
923
961
  for (const node of nodes) {
924
962
  const placed = layout.nodes.get(node.id);
925
963
  if (!placed || node.locked) continue;
926
- const to = {
927
- x: origin.x + snap(placed.x),
964
+ const offset = anchors.get(node.id) ?? 0;
965
+ const to = vertical ? {
966
+ x: origin.x + snap(placed.x + offset) - offset,
928
967
  y: origin.y + snap(placed.y)
968
+ } : {
969
+ x: origin.x + snap(placed.x),
970
+ y: origin.y + snap(placed.y + offset) - offset
929
971
  };
930
972
  if (to.x !== node.x || to.y !== node.y) moved.set(node.id, to);
931
973
  }
@@ -1025,7 +1067,11 @@ var flow_editor_vue_vue_type_script_setup_true_lang_default = /*@__PURE__*/ defi
1025
1067
  /** Keeps a node in view, e.g. when it gets keyboard focus. */
1026
1068
  function reveal(id) {
1027
1069
  const rect = getNodeRect(id);
1028
- if (!rect || !root.value) return;
1070
+ if (rect) revealRect(rect);
1071
+ }
1072
+ /** Pans the least that brings `rect`, in canvas coordinates, into view. */
1073
+ function revealRect(rect) {
1074
+ if (!root.value) return;
1029
1075
  const { x, y, zoom } = viewport.value;
1030
1076
  const pad = 16;
1031
1077
  const left = rect.x * zoom + x;
@@ -1989,6 +2035,7 @@ var flow_editor_vue_vue_type_script_setup_true_lang_default = /*@__PURE__*/ defi
1989
2035
  x,
1990
2036
  y,
1991
2037
  locked: void 0,
2038
+ comments: void 0,
1992
2039
  parent: copied ?? containerAt(centre, containers)
1993
2040
  };
1994
2041
  });
@@ -2005,6 +2052,7 @@ var flow_editor_vue_vue_type_script_setup_true_lang_default = /*@__PURE__*/ defi
2005
2052
  y: p.y + shift.y
2006
2053
  })),
2007
2054
  id: createId("e"),
2055
+ comments: void 0,
2008
2056
  source: {
2009
2057
  ...edge.source,
2010
2058
  node: source
@@ -2256,14 +2304,18 @@ var flow_editor_vue_vue_type_script_setup_true_lang_default = /*@__PURE__*/ defi
2256
2304
  window.removeEventListener("pointerdown", onMenuPress, true);
2257
2305
  window.removeEventListener("click", onMenuClick);
2258
2306
  });
2307
+ /** The toolbar's groups with something in; while read-only, only what is offered then too, such as comments. */
2308
+ function offeredItems(lists, readonly) {
2309
+ return lists.map((list) => readonly ? list.filter((item) => item.showWhenReadonly) : list).filter((list) => list.length);
2310
+ }
2259
2311
  const toolbarItems = computed(() => {
2260
- if (props.readonly || gesture.value || keyLink.value || quickAdd.value) return [];
2312
+ if (gesture.value || keyLink.value || quickAdd.value) return [];
2261
2313
  const nodes = selection.value.nodes.flatMap((id) => nodeById.value.get(id) ?? []);
2262
- if (nodes.length) return resolved.value.toolbars.map((fn) => fn(context, nodes)).filter((list) => list.length);
2314
+ if (nodes.length) return offeredItems(resolved.value.toolbars.map((fn) => fn(context, nodes)), props.readonly);
2263
2315
  const ids = new Set(selection.value.edges);
2264
2316
  const edges = doc.value.edges.filter((edge) => ids.has(edge.id));
2265
2317
  if (!edges.length) return [];
2266
- return resolved.value.edgeToolbars.map((fn) => fn(context, edges)).filter((list) => list.length);
2318
+ return offeredItems(resolved.value.edgeToolbars.map((fn) => fn(context, edges)), props.readonly);
2267
2319
  });
2268
2320
  /** Where the toolbar floats: above the selected nodes, in screen px within the editor. */
2269
2321
  const toolbarAnchor = computed(() => {
@@ -2294,9 +2346,12 @@ var flow_editor_vue_vue_type_script_setup_true_lang_default = /*@__PURE__*/ defi
2294
2346
  height: bounds.height + reach * 2
2295
2347
  };
2296
2348
  const avoid = nodes.length ? void 0 : shownNodes.value.filter((node) => !hiddenIn.value.has(node.id) && !isContainer(node)).map((node) => rectOf(node)).filter((rect) => intersects(rect, band)).map(screen);
2349
+ const keys = /* @__PURE__ */ new Set([...nodes.map((id) => `n:${id}`), ...edges.map((id) => `e:${id}`)]);
2350
+ const marked = commentMarkers.value.filter((marker) => keys.has(marker.key));
2351
+ const top = Math.min(bounds.y * zoom + y, ...marked.map(markerTop));
2297
2352
  return {
2298
2353
  x: (bounds.x + bounds.width / 2) * zoom + x,
2299
- top: bounds.y * zoom + y,
2354
+ top,
2300
2355
  bottom: (bounds.y + bounds.height) * zoom + y,
2301
2356
  avoid
2302
2357
  };
@@ -3419,6 +3474,143 @@ var flow_editor_vue_vue_type_script_setup_true_lang_default = /*@__PURE__*/ defi
3419
3474
  ...PNG_SOURCE
3420
3475
  });
3421
3476
  }
3477
+ /** Half an edge label's height, in canvas px. */
3478
+ const markerCenter = (marker) => "node" in marker.anchor ? {
3479
+ x: marker.x + 6 + MARKER_SIZE / 2,
3480
+ y: marker.y - 6 - MARKER_SIZE / 2
3481
+ } : {
3482
+ x: marker.x,
3483
+ y: marker.y
3484
+ };
3485
+ /** The top of a marker with a finger's target around it, in px within the editor. */
3486
+ const markerTop = (marker) => markerCenter(marker).y - MARKER_SIZE / 2 - 8 - (marker.lifted ? 8 : 0);
3487
+ /** Where a node or edge shows its comments: a collapsed container hiding it shows them instead. */
3488
+ function commentSpotOf(on) {
3489
+ const hidden = hiddenIn.value;
3490
+ if ("node" in on) {
3491
+ if (!nodeById.value.has(on.node)) return null;
3492
+ return { node: hidden.get(on.node) ?? on.node };
3493
+ }
3494
+ if (edgeRouteById.value.has(on.edge)) return on;
3495
+ const edge = doc.value.edges.find((e) => e.id === on.edge);
3496
+ const outer = edge && (hidden.get(edge.source.node) ?? hidden.get(edge.target.node));
3497
+ return outer ? { node: outer } : null;
3498
+ }
3499
+ const edgeRouteById = computed(() => new Map(edgeRoutes.value.map((item) => [item.edge.id, item])));
3500
+ /**
3501
+ * The markers, in canvas px: one at the top right of each node and on each
3502
+ * edge's middle with open threads. Worked out as the document changes; the
3503
+ * view only moves them, below.
3504
+ */
3505
+ const commentSpots = computed(() => {
3506
+ if (!comments.enabled.value) return [];
3507
+ const { nodes, edges } = comments.openOn.value;
3508
+ const spots = /* @__PURE__ */ new Map();
3509
+ const add = (on, threads, extra = {}) => {
3510
+ const spot = commentSpotOf(on);
3511
+ if (!spot) return;
3512
+ const key = "node" in spot ? `n:${spot.node}` : `e:${spot.edge}`;
3513
+ const had = spots.get(key);
3514
+ if (had) {
3515
+ for (const id of threads) if (!had.threads.includes(id)) had.threads.push(id);
3516
+ if (extra.pending) had.pending = true;
3517
+ return;
3518
+ }
3519
+ let at;
3520
+ let lift = 0;
3521
+ if ("node" in spot) {
3522
+ const node = nodeById.value.get(spot.node);
3523
+ const box = nodeBounds(specOf(node), node, rectOf(node));
3524
+ at = {
3525
+ x: box.x + box.width,
3526
+ y: box.y
3527
+ };
3528
+ } else {
3529
+ const item = edgeRouteById.value.get(spot.edge);
3530
+ at = item.route.label;
3531
+ if (item.edge.label) {
3532
+ at = {
3533
+ x: at.x,
3534
+ y: at.y - EDGE_LABEL_HALF
3535
+ };
3536
+ lift = EDGE_MARKER_LIFT;
3537
+ }
3538
+ }
3539
+ spots.set(key, {
3540
+ key,
3541
+ anchor: spot,
3542
+ threads: [...threads],
3543
+ shown: [],
3544
+ pending: !!extra.pending,
3545
+ ...at,
3546
+ lift
3547
+ });
3548
+ };
3549
+ for (const [id, threads] of nodes) add({ node: id }, threads);
3550
+ for (const [id, threads] of edges) add({ edge: id }, threads);
3551
+ const starting = comments.pending.value;
3552
+ if (starting) add(starting, [], { pending: true });
3553
+ const list = [...spots.values()];
3554
+ const active = comments.active.value;
3555
+ const where = active ? commentAnchorOf(active) : null;
3556
+ for (const spot of list) spot.shown = spot.threads;
3557
+ if (active && where) {
3558
+ const spot = commentSpotOf(where);
3559
+ const key = spot && ("node" in spot ? `n:${spot.node}` : `e:${spot.edge}`);
3560
+ const there = key && spots.get(key);
3561
+ if (there && !there.threads.includes(active)) there.shown = [...there.threads, active];
3562
+ else if (!there) {
3563
+ add(where, []);
3564
+ const added = key && spots.get(key);
3565
+ if (added) {
3566
+ added.shown = [active];
3567
+ list.push(added);
3568
+ }
3569
+ }
3570
+ }
3571
+ return list;
3572
+ });
3573
+ /**
3574
+ * The markers in px within the editor: only this follows the view as it
3575
+ * pans and zooms. An edge's marker that would sit on another one moves
3576
+ * aside, so each can be tapped.
3577
+ */
3578
+ const commentMarkers = computed(() => {
3579
+ const { x: vx, y: vy, zoom } = viewport.value;
3580
+ const markers = commentSpots.value.map(({ x, y, lift, ...spot }) => ({
3581
+ ...spot,
3582
+ x: x * zoom + vx,
3583
+ y: y * zoom + vy - lift,
3584
+ lifted: lift > 0
3585
+ }));
3586
+ const placed = markers.filter((marker) => "node" in marker.anchor).map(markerCenter);
3587
+ for (const marker of markers) {
3588
+ if ("node" in marker.anchor) continue;
3589
+ for (let tries = 0; tries < 8; tries++) {
3590
+ const other = placed.find((p) => Math.abs(p.x - marker.x) < MARKER_GAP && Math.abs(p.y - marker.y) < MARKER_GAP);
3591
+ if (!other) break;
3592
+ marker.x = other.x + (marker.x < other.x ? -32 : MARKER_GAP);
3593
+ }
3594
+ placed.push(markerCenter(marker));
3595
+ }
3596
+ return markers;
3597
+ });
3598
+ watch(() => props.activeComment, (id) => {
3599
+ const at = id && !comments.openedHere(id) ? commentAnchorOf(id) : null;
3600
+ const on = at && commentSpotOf(at);
3601
+ if (!on) return;
3602
+ if ("node" in on) return reveal(on.node);
3603
+ const label = edgeRouteById.value.get(on.edge)?.route.label;
3604
+ if (label) revealRect({
3605
+ x: label.x - 16,
3606
+ y: label.y - 16,
3607
+ width: 32,
3608
+ height: 32
3609
+ });
3610
+ });
3611
+ function restoreCommentFocus(on) {
3612
+ ((on && "node" in on ? root.value?.querySelector(`[data-node-id="${CSS.escape(on.node)}"]`) : null) ?? root.value)?.focus({ preventScroll: true });
3613
+ }
3422
3614
  __expose({
3423
3615
  setViewport,
3424
3616
  openSearch,
@@ -3447,6 +3639,8 @@ var flow_editor_vue_vue_type_script_setup_true_lang_default = /*@__PURE__*/ defi
3447
3639
  return [...staleEdges.value];
3448
3640
  },
3449
3641
  removeStaleEdges,
3642
+ comment: (on) => comments.start(on),
3643
+ getCommentAnchors,
3450
3644
  insertNode,
3451
3645
  startInsertDrag,
3452
3646
  editLabel,
@@ -3675,8 +3869,19 @@ var flow_editor_vue_vue_type_script_setup_true_lang_default = /*@__PURE__*/ defi
3675
3869
  (openBlock(true), createElementBlock(Fragment, null, renderList(resolved.value.overlays, (overlay, i) => {
3676
3870
  return openBlock(), createBlock(resolveDynamicComponent(overlay), { key: i });
3677
3871
  }), 128)),
3678
- toolbarItems.value.length && toolbarAnchor.value ? (openBlock(), createBlock(flow_toolbar_default, {
3872
+ unref(comments).enabled.value ? (openBlock(), createBlock(flow_comments_default, {
3679
3873
  key: 0,
3874
+ comments: unref(comments),
3875
+ markers: commentMarkers.value,
3876
+ messages: unref(messages),
3877
+ restore: restoreCommentFocus
3878
+ }, null, 8, [
3879
+ "comments",
3880
+ "markers",
3881
+ "messages"
3882
+ ])) : createCommentVNode("", true),
3883
+ toolbarItems.value.length && toolbarAnchor.value ? (openBlock(), createBlock(flow_toolbar_default, {
3884
+ key: 1,
3680
3885
  groups: toolbarItems.value,
3681
3886
  anchor: toolbarAnchor.value,
3682
3887
  size: size.value
@@ -3686,7 +3891,7 @@ var flow_editor_vue_vue_type_script_setup_true_lang_default = /*@__PURE__*/ defi
3686
3891
  "size"
3687
3892
  ])) : createCommentVNode("", true),
3688
3893
  findOpen.value ? (openBlock(), createElementBlock("div", {
3689
- key: 1,
3894
+ key: 2,
3690
3895
  ref_key: "findBox",
3691
3896
  ref: findBox,
3692
3897
  class: "dou-flow-editor__find",