@dou-ui/flow 0.1.0-alpha.6 → 0.1.0-alpha.8

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.
@@ -1,6 +1,6 @@
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, warnUnknownNodeType } from "./core/node-types.js";
3
+ import { labelOf, nodeBounds, portSide, 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
6
  import { format, useFlowMessages } from "./locale.js";
@@ -95,6 +95,7 @@ var FIT_PADDING = 40;
95
95
  * that; what still doesn't fit starts at the top left, to be panned to.
96
96
  */
97
97
  var PANEL_GAP = 16;
98
+ var TIDY_NEAR = 24;
98
99
  var FIELDS = "input, textarea, [contenteditable]:not([contenteditable=\"false\"]), [data-dou-nodrag]";
99
100
  var MAGNET = 24;
100
101
  var FIND_INSET = 12;
@@ -884,7 +885,7 @@ var flow_editor_vue_vue_type_script_setup_true_lang_default = /*@__PURE__*/ defi
884
885
  zoom
885
886
  }, animate);
886
887
  }
887
- /** Room between ranks (along the flow) and between nodes in a rank when tidying. */
888
+ /** How near a note on a board is to a node, in px, to move along with it when tidying. */
888
889
  const TIDY_GAP = {
889
890
  horizontal: {
890
891
  rank: 80,
@@ -896,6 +897,50 @@ var flow_editor_vue_vue_type_script_setup_true_lang_default = /*@__PURE__*/ defi
896
897
  }
897
898
  };
898
899
  /**
900
+ * Which way tidying runs: the flow's `direction`, unless most edges between
901
+ * nodes with `ports: 'sides'` leave their nodes by the other
902
+ * sides, e.g. a whiteboard drawn top to bottom. Laying that out the other
903
+ * way would undo how it was drawn. Fixed ports say nothing about it.
904
+ */
905
+ function tidiesVertically(blocks, blockOf) {
906
+ const vertical = props.direction === "vertical";
907
+ let along = 0;
908
+ let across = 0;
909
+ for (const edge of doc.value.edges) {
910
+ const from = blockOf(edge.source.node);
911
+ const to = blockOf(edge.target.node);
912
+ if (!from || !to || from === to || !blocks.has(from) || !blocks.has(to)) continue;
913
+ const port = portSpec(edge.source);
914
+ const node = nodeById.value.get(edge.source.node);
915
+ if (!port || !node || specOf(node).ports !== "sides" || specOf(node).rotation?.(node)) continue;
916
+ const side = portSide(port);
917
+ if ((side === "top" || side === "bottom") === vertical) along++;
918
+ else across++;
919
+ }
920
+ return across > along ? !vertical : vertical;
921
+ }
922
+ const OPPOSITE = {
923
+ top: "bottom",
924
+ bottom: "top",
925
+ left: "right",
926
+ right: "left"
927
+ };
928
+ /**
929
+ * The port an edge end moves to when tidying, on a node whose ports are
930
+ * `ports: 'sides'`: on the side facing the other end along the flow.
931
+ */
932
+ function anchorFor(end, kind, side) {
933
+ const node = nodeById.value.get(end.node);
934
+ if (!node) return void 0;
935
+ const spec = specOf(node);
936
+ if (spec.ports !== "sides" || spec.rotation?.(node)) return void 0;
937
+ const port = portsOf(spec, node).find((p) => p.kind === kind && portSide(p) === side);
938
+ return port && port.id !== end.port ? {
939
+ ...end,
940
+ port: port.id
941
+ } : void 0;
942
+ }
943
+ /**
899
944
  * Lays the given nodes, or the selected ones when two or more are selected,
900
945
  * or all, out in ranks along their edges in the flow's `direction`, as one
901
946
  * undo step. The group keeps its top left corner; locked nodes stay put.
@@ -910,10 +955,23 @@ var flow_editor_vue_vue_type_script_setup_true_lang_default = /*@__PURE__*/ defi
910
955
  return at;
911
956
  };
912
957
  const blocks = new Set([...chosen].flatMap((id) => blockOf(id) ?? []));
913
- const nodes = doc.value.nodes.filter((node) => blocks.has(node.id));
958
+ const board = [...blocks].some((id) => {
959
+ const node = nodeById.value.get(id);
960
+ return node && specOf(node).ports === "sides";
961
+ });
962
+ const linked = /* @__PURE__ */ new Set();
963
+ if (board) for (const edge of doc.value.edges) {
964
+ const a = blockOf(edge.source.node);
965
+ const b = blockOf(edge.target.node);
966
+ if (!a || !b || a === b || !blocks.has(a) || !blocks.has(b)) continue;
967
+ linked.add(a);
968
+ linked.add(b);
969
+ }
970
+ const loose = board ? doc.value.nodes.filter((n) => blocks.has(n.id) && !linked.has(n.id)) : [];
971
+ const nodes = doc.value.nodes.filter((node) => blocks.has(node.id) && (!board || linked.has(node.id)));
914
972
  const bounds = boundsOf(nodes.map(rectOf));
915
973
  if (!bounds || nodes.length < 2) return;
916
- const vertical = props.direction === "vertical";
974
+ const vertical = tidiesVertically(blocks, blockOf);
917
975
  const gap = TIDY_GAP[vertical ? "vertical" : "horizontal"];
918
976
  const across = vertical ? ["top", "bottom"] : ["left", "right"];
919
977
  const anchors = /* @__PURE__ */ new Map();
@@ -941,7 +999,7 @@ var flow_editor_vue_vue_type_script_setup_true_lang_default = /*@__PURE__*/ defi
941
999
  to
942
1000
  }] : [];
943
1001
  }), {
944
- direction: props.direction,
1002
+ direction: vertical ? "vertical" : "horizontal",
945
1003
  rankGap: gap.rank,
946
1004
  laneGap: gap.row,
947
1005
  equalWidth: false,
@@ -971,6 +1029,32 @@ var flow_editor_vue_vue_type_script_setup_true_lang_default = /*@__PURE__*/ defi
971
1029
  };
972
1030
  if (to.x !== node.x || to.y !== node.y) moved.set(node.id, to);
973
1031
  }
1032
+ for (const note of loose) {
1033
+ if (note.locked) continue;
1034
+ const rect = rectOf(note);
1035
+ let host;
1036
+ let best = Infinity;
1037
+ for (const node of nodes) {
1038
+ const other = rectOf(node);
1039
+ const apart = Math.max(other.x - (rect.x + rect.width), rect.x - (other.x + other.width), other.y - (rect.y + rect.height), rect.y - (other.y + other.height), 0);
1040
+ if (apart > TIDY_NEAR) continue;
1041
+ const score = apart * 1e6 + Math.hypot(other.x + other.width / 2 - (rect.x + rect.width / 2), other.y + other.height / 2 - (rect.y + rect.height / 2));
1042
+ if (score < best) {
1043
+ best = score;
1044
+ host = node;
1045
+ }
1046
+ }
1047
+ const to = host && moved.get(host.id);
1048
+ if (!host || !to) continue;
1049
+ from.set(note.id, {
1050
+ x: note.x,
1051
+ y: note.y
1052
+ });
1053
+ moved.set(note.id, {
1054
+ x: note.x + to.x - host.x,
1055
+ y: note.y + to.y - host.y
1056
+ });
1057
+ }
974
1058
  const through = new Map(layout.edges.map((e) => [`${e.from}\u0000${e.to}`, e.points]));
975
1059
  const locked = new Set(nodes.filter((node) => node.locked).map((node) => node.id));
976
1060
  const routes = /* @__PURE__ */ new Map();
@@ -994,10 +1078,53 @@ var flow_editor_vue_vue_type_script_setup_true_lang_default = /*@__PURE__*/ defi
994
1078
  routes.set(edge.id, next);
995
1079
  });
996
1080
  }
997
- const rerouted = doc.value.edges.some((edge) => routes.has(edge.id) && JSON.stringify(routes.get(edge.id)) !== JSON.stringify(edge.points));
1081
+ const rankOf = new Map(layout.ranks.flatMap((rank, i) => rank.map((id) => [id, i])));
1082
+ const centre = (id) => {
1083
+ const rect = rectOf(nodeById.value.get(id));
1084
+ const block = blockOf(id);
1085
+ const to = moved.get(block);
1086
+ const was = from.get(block);
1087
+ const dx = to ? to.x - was.x : 0;
1088
+ const dy = to ? to.y - was.y : 0;
1089
+ return {
1090
+ x: rect.x + dx + rect.width / 2,
1091
+ y: rect.y + dy + rect.height / 2
1092
+ };
1093
+ };
1094
+ const ends = /* @__PURE__ */ new Map();
1095
+ const joined = /* @__PURE__ */ new Set();
1096
+ for (const edge of doc.value.edges) {
1097
+ const a = blockOf(edge.source.node);
1098
+ const b = blockOf(edge.target.node);
1099
+ if (a && b) joined.add(`${a}\u0000${b}`);
1100
+ }
1101
+ const middle = (vertical ? origin.x + layout.width / 2 : origin.y + layout.height / 2) - .5;
1102
+ for (const edge of doc.value.edges) {
1103
+ const a = blockOf(edge.source.node);
1104
+ const b = blockOf(edge.target.node);
1105
+ if (!a || !b || a === b || !blocks.has(a) || !blocks.has(b)) continue;
1106
+ const s = centre(edge.source.node);
1107
+ const t = centre(edge.target.node);
1108
+ const along = !locked.has(a) && !locked.has(b) ? (rankOf.get(b) ?? 0) - (rankOf.get(a) ?? 0) : Math.round(vertical ? t.y - s.y : t.x - s.x);
1109
+ const aside = vertical ? t.x - s.x : t.y - s.y;
1110
+ const out = along ? vertical ? along > 0 ? "bottom" : "top" : along > 0 ? "right" : "left" : vertical ? aside >= 0 ? "right" : "left" : aside >= 0 ? "bottom" : "top";
1111
+ const round = along < 0 && (along < -1 || joined.has(`${b}\u0000${a}`));
1112
+ const outer = vertical ? (s.x + t.x) / 2 >= middle ? "right" : "left" : (s.y + t.y) / 2 >= middle ? "bottom" : "top";
1113
+ const source = anchorFor(edge.source, "output", round ? outer : out);
1114
+ const target = anchorFor(edge.target, "input", round ? outer : OPPOSITE[out]);
1115
+ if (source || target) ends.set(edge.id, {
1116
+ source: source ?? edge.source,
1117
+ target: target ?? edge.target
1118
+ });
1119
+ }
1120
+ const rerouted = ends.size > 0 || doc.value.edges.some((edge) => routes.has(edge.id) && JSON.stringify(routes.get(edge.id)) !== JSON.stringify(edge.points));
998
1121
  if (!moved.size && !rerouted) return;
999
- const count = moved.size;
1000
- for (const { id } of nodes) {
1122
+ const edgesBefore = new Map(doc.value.edges.map((e) => [e.id, e]));
1123
+ const count = (/* @__PURE__ */ new Set([...[...moved.keys()].filter((id) => !linked.size || linked.has(id)), ...[...ends].flatMap(([id, end]) => {
1124
+ const was = edgesBefore.get(id);
1125
+ return [...was?.source.port !== end.source.port ? [end.source.node] : [], ...was?.target.port !== end.target.port ? [end.target.node] : []];
1126
+ })])).size;
1127
+ for (const { id } of [...nodes, ...loose]) {
1001
1128
  const to = moved.get(id);
1002
1129
  if (!to) continue;
1003
1130
  const block = from.get(id);
@@ -1016,13 +1143,17 @@ var flow_editor_vue_vue_type_script_setup_true_lang_default = /*@__PURE__*/ defi
1016
1143
  }
1017
1144
  transact((tr) => {
1018
1145
  for (const [id, to] of moved) tr.moveNode(id, to.x, to.y);
1146
+ for (const [id, end] of ends) tr.updateEdge(id, end);
1019
1147
  for (const [id, points] of routes) if (JSON.stringify(points) !== JSON.stringify(tr.getEdge(id)?.points)) tr.updateEdge(id, { points });
1020
1148
  });
1021
1149
  announce(format(messages.value.tidied, { count }));
1022
1150
  animateTidy(from, moved);
1023
- const after = boundsOf(nodes.map((node) => ({
1151
+ const after = boundsOf([...nodes, ...loose].map((node) => ({
1024
1152
  ...rectOf(node),
1025
- ...moved.get(node.id) ?? from.get(node.id)
1153
+ ...moved.get(node.id) ?? {
1154
+ x: node.x,
1155
+ y: node.y
1156
+ }
1026
1157
  })));
1027
1158
  const { x, y, zoom } = viewport.value;
1028
1159
  const { width, height } = size.value;