@yft-design/engine 0.0.0-stage → 0.0.2

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 (255) hide show
  1. package/README.md +39 -2
  2. package/assets/images/greyfloral.png +0 -0
  3. package/assets/images/honey_im_subtle.png +0 -0
  4. package/assets/images/nasty_fabric.png +0 -0
  5. package/assets/images/retina_wood.png +0 -0
  6. package/assets/images/rotate.png +0 -0
  7. package/dist/EngineCanvas.d.ts +500 -0
  8. package/dist/EngineCanvas.js +927 -0
  9. package/dist/core/canvasPerf.d.ts +23 -0
  10. package/dist/core/canvasPerf.js +170 -0
  11. package/dist/core/color.d.ts +52 -0
  12. package/dist/core/color.js +107 -0
  13. package/dist/core/distanceField.d.ts +15 -0
  14. package/dist/core/distanceField.js +92 -0
  15. package/dist/core/events.d.ts +34 -0
  16. package/dist/core/events.js +52 -0
  17. package/dist/core/gradient.d.ts +97 -0
  18. package/dist/core/gradient.js +175 -0
  19. package/dist/core/imageFilters.d.ts +150 -0
  20. package/dist/core/imageFilters.js +330 -0
  21. package/dist/core/mat2d.d.ts +151 -0
  22. package/dist/core/mat2d.js +241 -0
  23. package/dist/core/pathData.d.ts +53 -0
  24. package/dist/core/pathData.js +926 -0
  25. package/dist/core/pattern.d.ts +38 -0
  26. package/dist/core/pattern.js +40 -0
  27. package/dist/core/point.d.ts +59 -0
  28. package/dist/core/point.js +119 -0
  29. package/dist/core/psdEffects.d.ts +126 -0
  30. package/dist/core/psdEffects.js +158 -0
  31. package/dist/core/roundedRect.d.ts +62 -0
  32. package/dist/core/roundedRect.js +163 -0
  33. package/dist/core/shadow.d.ts +51 -0
  34. package/dist/core/shadow.js +47 -0
  35. package/dist/core/snap.d.ts +232 -0
  36. package/dist/core/snap.js +793 -0
  37. package/dist/core/stroke.d.ts +41 -0
  38. package/dist/core/stroke.js +44 -0
  39. package/dist/core/triangulate.d.ts +24 -0
  40. package/dist/core/triangulate.js +118 -0
  41. package/dist/debug/canvasInspection.d.ts +92 -0
  42. package/dist/debug/canvasInspection.js +113 -0
  43. package/dist/debug/pixelDiff.d.ts +22 -0
  44. package/dist/debug/pixelDiff.js +64 -0
  45. package/dist/debug/seed.d.ts +473 -0
  46. package/dist/debug/seed.js +3803 -0
  47. package/dist/decoders/gif2sprite.d.ts +9 -0
  48. package/dist/decoders/gif2sprite.js +84 -0
  49. package/dist/decoders/webp2sprite.d.ts +22 -0
  50. package/dist/decoders/webp2sprite.js +133 -0
  51. package/dist/engineFlag.d.ts +72 -0
  52. package/dist/engineFlag.js +86 -0
  53. package/dist/export/SceneExporter.d.ts +92 -0
  54. package/dist/export/SceneExporter.js +280 -0
  55. package/dist/export/glExport.d.ts +66 -0
  56. package/dist/export/glExport.js +79 -0
  57. package/dist/export/svgExport.d.ts +277 -0
  58. package/dist/export/svgExport.js +1790 -0
  59. package/dist/index.d.ts +52 -0
  60. package/dist/index.js +48 -0
  61. package/dist/interact/animationPlayback.d.ts +125 -0
  62. package/dist/interact/animationPlayback.js +344 -0
  63. package/dist/interact/canvasInteractions.d.ts +50 -0
  64. package/dist/interact/canvasInteractions.js +150 -0
  65. package/dist/interact/cropSession.d.ts +123 -0
  66. package/dist/interact/cropSession.js +526 -0
  67. package/dist/interact/dblClick.d.ts +51 -0
  68. package/dist/interact/dblClick.js +106 -0
  69. package/dist/interact/expandSession.d.ts +107 -0
  70. package/dist/interact/expandSession.js +281 -0
  71. package/dist/interact/hotkeyActions.d.ts +16 -0
  72. package/dist/interact/hotkeyActions.js +41 -0
  73. package/dist/interact/pathSession.d.ts +141 -0
  74. package/dist/interact/pathSession.js +480 -0
  75. package/dist/interact/pointer.d.ts +106 -0
  76. package/dist/interact/pointer.js +1594 -0
  77. package/dist/interact/pointerTablePuzzle.d.ts +100 -0
  78. package/dist/interact/pointerTablePuzzle.js +358 -0
  79. package/dist/interact/pointerTransform.d.ts +86 -0
  80. package/dist/interact/pointerTransform.js +440 -0
  81. package/dist/interact/pointerTypes.d.ts +577 -0
  82. package/dist/interact/pointerTypes.js +96 -0
  83. package/dist/interact/snapGuide.d.ts +127 -0
  84. package/dist/interact/snapGuide.js +376 -0
  85. package/dist/interact/tableCellEditing.d.ts +61 -0
  86. package/dist/interact/tableCellEditing.js +198 -0
  87. package/dist/interact/tableInteraction.d.ts +55 -0
  88. package/dist/interact/tableInteraction.js +212 -0
  89. package/dist/interact/textEditing.d.ts +77 -0
  90. package/dist/interact/textEditing.js +410 -0
  91. package/dist/interact/warpFacade.d.ts +36 -0
  92. package/dist/interact/warpFacade.js +51 -0
  93. package/dist/interact/warpSession.d.ts +191 -0
  94. package/dist/interact/warpSession.js +582 -0
  95. package/dist/interact/wheelScroll.d.ts +62 -0
  96. package/dist/interact/wheelScroll.js +72 -0
  97. package/dist/model/alphaHit.d.ts +59 -0
  98. package/dist/model/alphaHit.js +262 -0
  99. package/dist/model/animation.d.ts +228 -0
  100. package/dist/model/animation.js +510 -0
  101. package/dist/model/arcLayout.d.ts +54 -0
  102. package/dist/model/arcLayout.js +307 -0
  103. package/dist/model/arcTextTransform.d.ts +7 -0
  104. package/dist/model/arcTextTransform.js +87 -0
  105. package/dist/model/compatEvents.d.ts +135 -0
  106. package/dist/model/compatEvents.js +73 -0
  107. package/dist/model/compatObject.d.ts +716 -0
  108. package/dist/model/compatObject.js +1579 -0
  109. package/dist/model/cropEdit.d.ts +118 -0
  110. package/dist/model/cropEdit.js +497 -0
  111. package/dist/model/document.d.ts +294 -0
  112. package/dist/model/document.js +572 -0
  113. package/dist/model/expandEdit.d.ts +93 -0
  114. package/dist/model/expandEdit.js +161 -0
  115. package/dist/model/fabricPrimitives.d.ts +156 -0
  116. package/dist/model/fabricPrimitives.js +9 -0
  117. package/dist/model/filler.d.ts +53 -0
  118. package/dist/model/filler.js +1 -0
  119. package/dist/model/groupAssemble.d.ts +7 -0
  120. package/dist/model/groupAssemble.js +98 -0
  121. package/dist/model/groupChildMove.d.ts +46 -0
  122. package/dist/model/groupChildMove.js +145 -0
  123. package/dist/model/groupResize.d.ts +78 -0
  124. package/dist/model/groupResize.js +108 -0
  125. package/dist/model/handles.d.ts +40 -0
  126. package/dist/model/handles.js +29 -0
  127. package/dist/model/imageMaskBake.d.ts +75 -0
  128. package/dist/model/imageMaskBake.js +136 -0
  129. package/dist/model/lineConstruct.d.ts +13 -0
  130. package/dist/model/lineConstruct.js +79 -0
  131. package/dist/model/maskCropEdit.d.ts +64 -0
  132. package/dist/model/maskCropEdit.js +90 -0
  133. package/dist/model/pathBackground.d.ts +51 -0
  134. package/dist/model/pathBackground.js +196 -0
  135. package/dist/model/pathConstruct.d.ts +27 -0
  136. package/dist/model/pathConstruct.js +55 -0
  137. package/dist/model/pathEdit.d.ts +114 -0
  138. package/dist/model/pathEdit.js +378 -0
  139. package/dist/model/puzzleEdit.d.ts +136 -0
  140. package/dist/model/puzzleEdit.js +742 -0
  141. package/dist/model/puzzleLayout.d.ts +91 -0
  142. package/dist/model/puzzleLayout.js +143 -0
  143. package/dist/model/scene.d.ts +1004 -0
  144. package/dist/model/scene.js +2841 -0
  145. package/dist/model/schema.d.ts +290 -0
  146. package/dist/model/schema.js +13 -0
  147. package/dist/model/shapePath.d.ts +14 -0
  148. package/dist/model/shapePath.js +136 -0
  149. package/dist/model/tableEdit.d.ts +196 -0
  150. package/dist/model/tableEdit.js +751 -0
  151. package/dist/model/tableFacade.d.ts +6 -0
  152. package/dist/model/tableFacade.js +808 -0
  153. package/dist/model/tableLayout.d.ts +85 -0
  154. package/dist/model/tableLayout.js +193 -0
  155. package/dist/model/textLayout.d.ts +357 -0
  156. package/dist/model/textLayout.js +822 -0
  157. package/dist/render/GLBlend.d.ts +40 -0
  158. package/dist/render/GLBlend.js +270 -0
  159. package/dist/render/GLContext.d.ts +25 -0
  160. package/dist/render/GLContext.js +48 -0
  161. package/dist/render/GLIsolate.d.ts +46 -0
  162. package/dist/render/GLIsolate.js +188 -0
  163. package/dist/render/GLTextureCache.d.ts +86 -0
  164. package/dist/render/GLTextureCache.js +187 -0
  165. package/dist/render/GlyphAtlas.d.ts +151 -0
  166. package/dist/render/GlyphAtlas.js +404 -0
  167. package/dist/render/MeshBatch.d.ts +123 -0
  168. package/dist/render/MeshBatch.js +416 -0
  169. package/dist/render/QuadBatch.d.ts +138 -0
  170. package/dist/render/QuadBatch.js +490 -0
  171. package/dist/render/SceneRenderer.d.ts +186 -0
  172. package/dist/render/SceneRenderer.js +700 -0
  173. package/dist/render/SourceManager.d.ts +121 -0
  174. package/dist/render/SourceManager.js +505 -0
  175. package/dist/render/TextRenderer.d.ts +140 -0
  176. package/dist/render/TextRenderer.js +795 -0
  177. package/dist/render/arcText.d.ts +40 -0
  178. package/dist/render/arcText.js +187 -0
  179. package/dist/render/batchOrder.d.ts +105 -0
  180. package/dist/render/batchOrder.js +168 -0
  181. package/dist/render/blendMode.d.ts +35 -0
  182. package/dist/render/blendMode.js +142 -0
  183. package/dist/render/clipMaskPainter.d.ts +17 -0
  184. package/dist/render/clipMaskPainter.js +121 -0
  185. package/dist/render/fontInvalidation.d.ts +25 -0
  186. package/dist/render/fontInvalidation.js +77 -0
  187. package/dist/render/fontInvalidationController.d.ts +76 -0
  188. package/dist/render/fontInvalidationController.js +114 -0
  189. package/dist/render/frameOverlay.d.ts +62 -0
  190. package/dist/render/frameOverlay.js +124 -0
  191. package/dist/render/framePipeline.d.ts +99 -0
  192. package/dist/render/framePipeline.js +95 -0
  193. package/dist/render/gpuFilterPasses.d.ts +25 -0
  194. package/dist/render/gpuFilterPasses.js +883 -0
  195. package/dist/render/gpuFx.d.ts +136 -0
  196. package/dist/render/gpuFx.js +614 -0
  197. package/dist/render/gpuImageFilters.d.ts +59 -0
  198. package/dist/render/gpuImageFilters.js +289 -0
  199. package/dist/render/gpuShadow.d.ts +52 -0
  200. package/dist/render/gpuShadow.js +289 -0
  201. package/dist/render/gradientRamp.d.ts +45 -0
  202. package/dist/render/gradientRamp.js +74 -0
  203. package/dist/render/imageEffects.d.ts +123 -0
  204. package/dist/render/imageEffects.js +390 -0
  205. package/dist/render/maskRender.d.ts +92 -0
  206. package/dist/render/maskRender.js +224 -0
  207. package/dist/render/progressiveLoad.d.ts +45 -0
  208. package/dist/render/progressiveLoad.js +86 -0
  209. package/dist/render/psdEffectPainter.d.ts +34 -0
  210. package/dist/render/psdEffectPainter.js +309 -0
  211. package/dist/render/puzzleClamp.d.ts +28 -0
  212. package/dist/render/puzzleClamp.js +30 -0
  213. package/dist/render/retainedFrame.d.ts +85 -0
  214. package/dist/render/retainedFrame.js +193 -0
  215. package/dist/render/ruler.d.ts +90 -0
  216. package/dist/render/ruler.js +287 -0
  217. package/dist/render/scrollText.d.ts +65 -0
  218. package/dist/render/scrollText.js +261 -0
  219. package/dist/render/selectionOverlay.d.ts +318 -0
  220. package/dist/render/selectionOverlay.js +921 -0
  221. package/dist/render/shaders.d.ts +38 -0
  222. package/dist/render/shaders.js +314 -0
  223. package/dist/render/shadowBake.d.ts +43 -0
  224. package/dist/render/shadowBake.js +202 -0
  225. package/dist/render/shadowPainter.d.ts +13 -0
  226. package/dist/render/shadowPainter.js +80 -0
  227. package/dist/render/sharedGpuPainters.d.ts +25 -0
  228. package/dist/render/sharedGpuPainters.js +40 -0
  229. package/dist/render/sourceHydration.d.ts +12 -0
  230. package/dist/render/sourceHydration.js +48 -0
  231. package/dist/render/strokeBake.d.ts +85 -0
  232. package/dist/render/strokeBake.js +515 -0
  233. package/dist/render/strokePainter.d.ts +28 -0
  234. package/dist/render/strokePainter.js +119 -0
  235. package/dist/render/strokeSdf.d.ts +35 -0
  236. package/dist/render/strokeSdf.js +74 -0
  237. package/dist/render/svgRaster.d.ts +46 -0
  238. package/dist/render/svgRaster.js +71 -0
  239. package/dist/render/tableOverlay.d.ts +38 -0
  240. package/dist/render/tableOverlay.js +233 -0
  241. package/dist/render/textHighlight.d.ts +117 -0
  242. package/dist/render/textHighlight.js +380 -0
  243. package/dist/render/textureUpload.d.ts +18 -0
  244. package/dist/render/textureUpload.js +26 -0
  245. package/dist/render/verticalText.d.ts +38 -0
  246. package/dist/render/verticalText.js +63 -0
  247. package/dist/render/warpText.d.ts +58 -0
  248. package/dist/render/warpText.js +380 -0
  249. package/dist/render/workspaceBackdrop.d.ts +35 -0
  250. package/dist/render/workspaceBackdrop.js +71 -0
  251. package/dist/render/workspaceChecker.d.ts +54 -0
  252. package/dist/render/workspaceChecker.js +170 -0
  253. package/dist/render/workspaceGlow.d.ts +51 -0
  254. package/dist/render/workspaceGlow.js +171 -0
  255. package/package.json +44 -3
@@ -0,0 +1,793 @@
1
+ /**
2
+ * 纯对齐吸附算法(P5-2)——`src/app/fabricSnapAlgorithm.ts` 的逐行同源移植
3
+ * (该模块本身从 Excalidraw snapping.ts 移植,纯数学零 Fabric 依赖)。
4
+ *
5
+ * 引擎自持副本而非 import 应用模块:引擎目录自包含(对拍 fabricSnapAlgorithm.spec
6
+ * 以「双实现同输入同输出」锁死漂移,任一侧改动立即红)。设计要点与上游一致:
7
+ * - 点用 `{ x, y }`,bounds 用 `{ left, right, top, bottom }`,range 用 `[start, end]`。
8
+ * - 「最近 + 并列」语义:`<=` 保留与当前最小偏移并列的匹配,`<` 发现更近时清空。
9
+ * - 两遍计算(two-pass):第一遍在 snapDistance 阈值内求最近 snapOffset;
10
+ * 第二遍把 snapOffset 应用到选区后,以 minOffset=0 收集「恰好对齐」的匹配用于画线。
11
+ * - 间距(gap)吸附实现等距分布:center(居中于间距)/ side(等距贴近某一侧)。
12
+ *
13
+ * 调用方(interact/snapGuide)负责把 snapDistance 按 zoom 折算到画布坐标后传入。
14
+ */
15
+ // -----------------------------------------------------------------------------
16
+ // 常量与工具
17
+ // -----------------------------------------------------------------------------
18
+ /** 默认吸附距离(画布坐标,调用方按 zoom 折算) */
19
+ export const SNAP_DISTANCE = 8;
20
+ /** 取 6 位小数,消除浮点抖动 */
21
+ export const round = (x) => Math.round(x * 10 ** 6) / 10 ** 6;
22
+ export const areRoughlyEqual = (a, b, precision = 0.01) => Math.abs(a - b) <= precision;
23
+ /** 两区间是否重叠(含端点相等) */
24
+ export const rangesOverlap = ([a0, a1], [b0, b1]) => {
25
+ if (a0 <= b0)
26
+ return a1 >= b0;
27
+ if (a0 >= b0)
28
+ return b1 >= a0;
29
+ return false;
30
+ };
31
+ /** 两区间的交集,无交集返回 null */
32
+ export const rangeIntersection = ([a0, a1], [b0, b1]) => {
33
+ const start = Math.max(a0, b0);
34
+ const end = Math.min(a1, b1);
35
+ return start <= end ? [start, end] : null;
36
+ };
37
+ /**
38
+ * 「最近 + 并列」核心:当 |offset| 在 minOffset 内时压入候选;
39
+ * 严格更近(<)则先清空再压入,相等(<=)则并列保留。
40
+ */
41
+ const pushNearest = (nearest, offset, minOffset, axis, snap) => {
42
+ const abs = Math.abs(offset);
43
+ if (abs <= minOffset[axis]) {
44
+ if (abs < minOffset[axis])
45
+ nearest.length = 0;
46
+ nearest.push(snap);
47
+ minOffset[axis] = abs;
48
+ }
49
+ };
50
+ export const buildAxisSortedPoints = (points) => {
51
+ const byX = points.map((p, i) => ({ p, i }));
52
+ byX.sort((a, b) => a.p.x - b.p.x);
53
+ const byY = points.map((p, i) => ({ p, i }));
54
+ byY.sort((a, b) => a.p.y - b.p.y);
55
+ return { byX, byY };
56
+ };
57
+ const lowerBoundBy = (arr, key, v) => {
58
+ let lo = 0;
59
+ let hi = arr.length;
60
+ while (lo < hi) {
61
+ const mid = (lo + hi) >> 1;
62
+ if (arr[mid].p[key] < v)
63
+ lo = mid + 1;
64
+ else
65
+ hi = mid;
66
+ }
67
+ return lo;
68
+ };
69
+ /**
70
+ * 窗口化点扫(getPointSnaps 的等价重排):X 吸附只判 |Δx|≤阈值、Y 只判
71
+ * |Δy|≤阈值——条件按轴可分离,轴序二分取窗 [p±window] 后仅迭代窗内候选。
72
+ * 窗界取本遍的初始阈值(minOffset 会被 pushNearest 逐候选收缩,窗必须按
73
+ * 初始值划,否则会漏掉后来并列的候选)。第二遍 window=0 即精确相等匹配。
74
+ * 等价性口径:窗口内先按严格更近/并列收集「最小档」(档内通常 1-3 个),
75
+ * 档按原数组下标排序后经 pushNearest 入序——终集与 [0](等距并列 ±d 的
76
+ * snapOffset 符号)与全量双重循环逐位一致(对拍 spec 锁定)。窗内不整档
77
+ * 排序:重场景窗口可含数百候选,逐选区点 sort 会吃掉窗口化的收益。
78
+ */
79
+ export const getPointSnapsWindowed = (selectionPoints, axis, windowX, windowY, minOffset, nearestSnapsX, nearestSnapsY) => {
80
+ // 最小档收集(axis 序);len/abs 复位后重用作入序缓冲
81
+ const tier = [];
82
+ const scanAxis = (sorted, key, window, points, nearest, minOff) => {
83
+ const axisKey = key;
84
+ for (const thisPoint of points) {
85
+ const lo = lowerBoundBy(sorted, key, thisPoint[key] - window);
86
+ const hi = thisPoint[key] + window;
87
+ let best = minOff[axisKey];
88
+ tier.length = 0;
89
+ for (let k = lo; k < sorted.length; k++) {
90
+ const e = sorted[k];
91
+ if (e.p[key] > hi)
92
+ break;
93
+ const offset = e.p[key] - thisPoint[key];
94
+ const abs = Math.abs(offset);
95
+ if (abs <= best) {
96
+ if (abs < best) {
97
+ best = abs;
98
+ tier.length = 0;
99
+ }
100
+ tier.push({ p: e.p, i: e.i, offset });
101
+ }
102
+ }
103
+ tier.sort((a, b) => a.i - b.i);
104
+ // 不预缩 minOff:pushNearest 的「严格更近即清空」负责跨选区点收缩
105
+ //(预设会把前点的较大档残留进列表头,[0] → snapOffset 取错——对拍实证)
106
+ for (const e of tier) {
107
+ pushNearest(nearest, e.offset, minOff, axisKey, {
108
+ type: 'point',
109
+ points: [thisPoint, e.p],
110
+ offset: e.offset
111
+ });
112
+ }
113
+ }
114
+ };
115
+ scanAxis(axis.byX, 'x', windowX, selectionPoints, nearestSnapsX, minOffset);
116
+ scanAxis(axis.byY, 'y', windowY, selectionPoints, nearestSnapsY, minOffset);
117
+ };
118
+ // -----------------------------------------------------------------------------
119
+ // 参考点
120
+ // -----------------------------------------------------------------------------
121
+ /** 将点取整到 6 位小数(消除浮点抖动,保证两遍算法第二遍的精确匹配) */
122
+ const roundPoint = (p) => ({ x: round(p.x), y: round(p.y) });
123
+ /** 将包围盒取整到 6 位小数 */
124
+ const roundBounds = (b) => ({ left: round(b.left), top: round(b.top), right: round(b.right), bottom: round(b.bottom) });
125
+ /** 从缓存坐标({ tl, tr, br, bl, c })取出 5 个吸附关键点(已取整) */
126
+ export const coordsToPoints = (coords) => [coords.tl, coords.tr, coords.br, coords.bl, coords.c].map(roundPoint);
127
+ /** 铺平所有参考元素的关键点 */
128
+ export const getReferenceSnapPoints = (coordsList) => coordsList.flatMap(coordsToPoints);
129
+ // -----------------------------------------------------------------------------
130
+ // 参考间距 getVisibleGaps
131
+ // -----------------------------------------------------------------------------
132
+ /**
133
+ * 计算所有参考元素两两之间的水平/垂直间距。
134
+ * 水平间距:两元素左右排列,Y 投影有重叠。
135
+ * 垂直间距:两元素上下排列,X 投影有重叠。
136
+ */
137
+ export const getVisibleGaps = (referenceBounds) => {
138
+ const horizontalGaps = [];
139
+ const verticalGaps = [];
140
+ // 取整到 6 位小数,消除浮点抖动
141
+ const bounds = referenceBounds.map(roundBounds);
142
+ // 水平间距:按 left 排序,start 在 end 左边
143
+ const horizontallySorted = [...bounds].sort((a, b) => a.left - b.left);
144
+ for (let i = 0; i < horizontallySorted.length; i++) {
145
+ const start = horizontallySorted[i];
146
+ for (let j = i + 1; j < horizontallySorted.length; j++) {
147
+ const end = horizontallySorted[j];
148
+ if (start.right < end.left && rangesOverlap([start.top, start.bottom], [end.top, end.bottom])) {
149
+ horizontalGaps.push({
150
+ startBounds: start,
151
+ endBounds: end,
152
+ startSide: [
153
+ { x: start.right, y: start.top },
154
+ { x: start.right, y: start.bottom }
155
+ ],
156
+ endSide: [
157
+ { x: end.left, y: end.top },
158
+ { x: end.left, y: end.bottom }
159
+ ],
160
+ length: end.left - start.right,
161
+ overlap: rangeIntersection([start.top, start.bottom], [end.top, end.bottom])
162
+ });
163
+ }
164
+ }
165
+ }
166
+ // 垂直间距:按 top 排序,start 在 end 上边
167
+ const verticallySorted = [...bounds].sort((a, b) => a.top - b.top);
168
+ for (let i = 0; i < verticallySorted.length; i++) {
169
+ const start = verticallySorted[i];
170
+ for (let j = i + 1; j < verticallySorted.length; j++) {
171
+ const end = verticallySorted[j];
172
+ if (start.bottom < end.top && rangesOverlap([start.left, start.right], [end.left, end.right])) {
173
+ verticalGaps.push({
174
+ startBounds: start,
175
+ endBounds: end,
176
+ startSide: [
177
+ { x: start.left, y: start.bottom },
178
+ { x: start.right, y: start.bottom }
179
+ ],
180
+ endSide: [
181
+ { x: end.left, y: end.top },
182
+ { x: end.right, y: end.top }
183
+ ],
184
+ length: end.top - start.bottom,
185
+ overlap: rangeIntersection([start.left, start.right], [end.left, end.right])
186
+ });
187
+ }
188
+ }
189
+ }
190
+ return { horizontalGaps, verticalGaps };
191
+ };
192
+ /** 手势期一次性构建;内部会 roundBounds(与 getVisibleGaps 入口同口径) */
193
+ export const buildGapIndex = (referenceBounds) => {
194
+ const withIdx = referenceBounds.map(roundBounds).map((bounds, idx) => ({ bounds, idx }));
195
+ const byKey = (key) => [...withIdx].sort((a, b) => a.bounds[key] - b.bounds[key]);
196
+ const bySum = (a, b) => [...withIdx].sort((x, y) => x.bounds[a] + x.bounds[b] - y.bounds[a] - y.bounds[b]);
197
+ return {
198
+ byLeft: byKey('left'),
199
+ byRight: byKey('right'),
200
+ byTop: byKey('top'),
201
+ byBottom: byKey('bottom'),
202
+ byLeftPlusRight: bySum('left', 'right'),
203
+ byTopPlusBottom: bySum('top', 'bottom')
204
+ };
205
+ };
206
+ /** 窗口半径的安全余量:round 偏移后再过闸(|round(x)| ≤ d ⟹ |x| ≤ d + 1e-6) */
207
+ const WINDOW_EPSILON = 1e-6;
208
+ /** 外层前缀/后缀剪枝的余量(远大于 WINDOW_EPSILON 即安全,亚像素级不影响剪枝率) */
209
+ const PRUNE_SLACK = 1e-3;
210
+ /**
211
+ * 索引版 gap 吸附:枚举可能命中 center/side 三种等距位置的对,再走与 getGapSnaps
212
+ * 相同的收编逻辑。selectionBounds 须已取整(调用方 snapDraggedElements 与旧路径同口径)。
213
+ */
214
+ export const getGapSnapsFromIndex = (selectionBounds, index, minOffset, nearestSnapsX, nearestSnapsY) => {
215
+ const { left: minX, top: minY, right: maxX, bottom: maxY } = selectionBounds;
216
+ const centerX = (minX + maxX) / 2;
217
+ const centerY = (minY + maxY) / 2;
218
+ const dx = minOffset.x;
219
+ const dy = minOffset.y;
220
+ const selW = maxX - minX;
221
+ const selH = maxY - minY;
222
+ // 水平 gap 的 Gap 对象(字段构造与 getVisibleGaps 逐字段一致,供 createGapSnapLines 消费)
223
+ const makeGapH = (s, e, overlap, length) => ({
224
+ startBounds: s,
225
+ endBounds: e,
226
+ startSide: [
227
+ { x: s.right, y: s.top },
228
+ { x: s.right, y: s.bottom }
229
+ ],
230
+ endSide: [
231
+ { x: e.left, y: e.top },
232
+ { x: e.left, y: e.bottom }
233
+ ],
234
+ length,
235
+ overlap
236
+ });
237
+ const makeGapV = (s, e, overlap, length) => ({
238
+ startBounds: s,
239
+ endBounds: e,
240
+ startSide: [
241
+ { x: s.left, y: s.bottom },
242
+ { x: s.right, y: s.bottom }
243
+ ],
244
+ endSide: [
245
+ { x: e.left, y: e.top },
246
+ { x: e.right, y: e.top }
247
+ ],
248
+ length,
249
+ overlap
250
+ });
251
+ // 单对收编:与 getGapSnaps 循环体逐行同口径(y/x 投影与选区相交 → center → side_right/left,互斥 continue)
252
+ const admitH = (s, e) => {
253
+ if (!(s.right < e.left && rangesOverlap([s.top, s.bottom], [e.top, e.bottom])))
254
+ return;
255
+ const overlap = rangeIntersection([s.top, s.bottom], [e.top, e.bottom]);
256
+ if (!rangesOverlap([minY, maxY], overlap))
257
+ return;
258
+ const length = e.left - s.right;
259
+ const centerOffset = round(s.right + length / 2 - centerX);
260
+ if (length > selW && Math.abs(centerOffset) <= minOffset.x) {
261
+ pushNearest(nearestSnapsX, centerOffset, minOffset, 'x', { type: 'gap', direction: 'center_horizontal', gap: makeGapH(s, e, overlap, length), offset: centerOffset });
262
+ return;
263
+ }
264
+ const sideOffsetRight = round(length - (minX - e.right));
265
+ if (Math.abs(sideOffsetRight) <= minOffset.x) {
266
+ pushNearest(nearestSnapsX, sideOffsetRight, minOffset, 'x', { type: 'gap', direction: 'side_right', gap: makeGapH(s, e, overlap, length), offset: sideOffsetRight });
267
+ return;
268
+ }
269
+ const sideOffsetLeft = round(s.left - maxX - length);
270
+ if (Math.abs(sideOffsetLeft) <= minOffset.x) {
271
+ pushNearest(nearestSnapsX, sideOffsetLeft, minOffset, 'x', { type: 'gap', direction: 'side_left', gap: makeGapH(s, e, overlap, length), offset: sideOffsetLeft });
272
+ }
273
+ };
274
+ const admitV = (s, e) => {
275
+ if (!(s.bottom < e.top && rangesOverlap([s.left, s.right], [e.left, e.right])))
276
+ return;
277
+ const overlap = rangeIntersection([s.left, s.right], [e.left, e.right]);
278
+ if (!rangesOverlap([minX, maxX], overlap))
279
+ return;
280
+ const length = e.top - s.bottom;
281
+ const centerOffset = round(s.bottom + length / 2 - centerY);
282
+ if (length > selH && Math.abs(centerOffset) <= minOffset.y) {
283
+ pushNearest(nearestSnapsY, centerOffset, minOffset, 'y', { type: 'gap', direction: 'center_vertical', gap: makeGapV(s, e, overlap, length), offset: centerOffset });
284
+ return;
285
+ }
286
+ const sideOffsetTop = round(s.top - maxY - length);
287
+ if (Math.abs(sideOffsetTop) <= minOffset.y) {
288
+ pushNearest(nearestSnapsY, sideOffsetTop, minOffset, 'y', { type: 'gap', direction: 'side_top', gap: makeGapV(s, e, overlap, length), offset: sideOffsetTop });
289
+ return;
290
+ }
291
+ // 与旧实现一致:round 施加在距离上,offset 不再 round
292
+ const distanceToEndElementY = round(minY - e.bottom);
293
+ const sideOffsetBottom = length - distanceToEndElementY;
294
+ if (Math.abs(sideOffsetBottom) <= minOffset.y) {
295
+ pushNearest(nearestSnapsY, sideOffsetBottom, minOffset, 'y', { type: 'gap', direction: 'side_bottom', gap: makeGapV(s, e, overlap, length), offset: sideOffsetBottom });
296
+ }
297
+ };
298
+ // 枚举收编:每地标一个两指针归并——外层按键单调迭代,窗口随之单调滑动,指针全程 O(N)。
299
+ // 收编集合与顺序无关(pushNearest 收缩门下终集 = 全局最小 |offset| 的全部候选),
300
+ // 枚举顺序仍确定(排序数组 + 单调指针)。同一对跨地标窗口的重复枚举罕见且无害。
301
+ const byLeft = index.byLeft;
302
+ const byRight = index.byRight;
303
+ const byTop = index.byTop;
304
+ const byBottom = index.byBottom;
305
+ const wX = dx + WINDOW_EPSILON;
306
+ const wY = dy + WINDOW_EPSILON;
307
+ // —— 水平间距地标(窗口半径 = dx + ε)——
308
+ // center_horizontal:gapMid = (s.right + e.left)/2 ≈ centerX → e.left ≈ 2·centerX − s.right ± 2wX
309
+ {
310
+ let p = byLeft.length;
311
+ for (const s of byRight) {
312
+ if (s.bounds.right >= centerX + dx + PRUNE_SLACK)
313
+ break;
314
+ const mid = 2 * centerX - s.bounds.right;
315
+ const hi = mid + 2 * wX;
316
+ const lo = mid - 2 * wX;
317
+ while (p > 0 && byLeft[p - 1].bounds.left > hi)
318
+ p--;
319
+ for (let i = p - 1; i >= 0 && byLeft[i].bounds.left >= lo; i--)
320
+ admitH(s.bounds, byLeft[i].bounds);
321
+ }
322
+ }
323
+ // side_right:offset = length − (minX − e.right) ≈ 0 → s.right ≈ e.left + e.right − minX ± wX
324
+ {
325
+ let q = 0;
326
+ for (const e of index.byLeftPlusRight) {
327
+ if (e.bounds.right >= minX + dx + PRUNE_SLACK)
328
+ continue;
329
+ const key = e.bounds.left + e.bounds.right - minX;
330
+ const lo = key - wX;
331
+ const hi = key + wX;
332
+ while (q < byRight.length && byRight[q].bounds.right < lo)
333
+ q++;
334
+ for (let i = q; i < byRight.length && byRight[i].bounds.right <= hi; i++)
335
+ admitH(byRight[i].bounds, e.bounds);
336
+ }
337
+ }
338
+ // side_left:offset = (s.left − maxX) − length ≈ 0 → e.left ≈ s.left + s.right − maxX ± wX
339
+ {
340
+ let q = 0;
341
+ for (const s of index.byLeftPlusRight) {
342
+ if (s.bounds.left <= maxX - dx - PRUNE_SLACK)
343
+ continue;
344
+ const key = s.bounds.left + s.bounds.right - maxX;
345
+ const lo = key - wX;
346
+ const hi = key + wX;
347
+ while (q < byLeft.length && byLeft[q].bounds.left < lo)
348
+ q++;
349
+ for (let i = q; i < byLeft.length && byLeft[i].bounds.left <= hi; i++)
350
+ admitH(s.bounds, byLeft[i].bounds);
351
+ }
352
+ }
353
+ // —— 垂直间距地标(镜像;side_bottom 的 key 含 round(minY − e.bottom) 怪癖,与收编式一致)——
354
+ // center_vertical:gapMid = (s.bottom + e.top)/2 ≈ centerY → e.top ≈ 2·centerY − s.bottom ± 2wY
355
+ {
356
+ let p = byTop.length;
357
+ for (const s of byBottom) {
358
+ if (s.bounds.bottom >= centerY + dy + PRUNE_SLACK)
359
+ break;
360
+ const mid = 2 * centerY - s.bounds.bottom;
361
+ const hi = mid + 2 * wY;
362
+ const lo = mid - 2 * wY;
363
+ while (p > 0 && byTop[p - 1].bounds.top > hi)
364
+ p--;
365
+ for (let i = p - 1; i >= 0 && byTop[i].bounds.top >= lo; i--)
366
+ admitV(s.bounds, byTop[i].bounds);
367
+ }
368
+ }
369
+ // side_top:offset = (s.top − maxY) − length ≈ 0 → e.top ≈ s.top + s.bottom − maxY ± wY
370
+ {
371
+ let q = 0;
372
+ for (const s of index.byTopPlusBottom) {
373
+ if (s.bounds.top <= maxY - dy - PRUNE_SLACK)
374
+ continue;
375
+ const key = s.bounds.top + s.bounds.bottom - maxY;
376
+ const lo = key - wY;
377
+ const hi = key + wY;
378
+ while (q < byTop.length && byTop[q].bounds.top < lo)
379
+ q++;
380
+ for (let i = q; i < byTop.length && byTop[i].bounds.top <= hi; i++)
381
+ admitV(s.bounds, byTop[i].bounds);
382
+ }
383
+ }
384
+ // side_bottom:offset = length − round(minY − e.bottom) ≈ 0 → s.bottom ≈ e.top − round(minY − e.bottom) ± wY。
385
+ // key 含 round(minY − e.bottom):沿 byTopPlusBottom 序有 ≤1e-6 的抖动(非严格单调),
386
+ // 指针推进条件留 POINTER_SLACK 余量——滞后无害(多扫的候选被收编精确闸拒绝)
387
+ {
388
+ const POINTER_SLACK = 1e-5;
389
+ let q = 0;
390
+ for (const e of index.byTopPlusBottom) {
391
+ if (e.bounds.bottom >= minY + dy + PRUNE_SLACK)
392
+ continue;
393
+ const key = e.bounds.top - round(minY - e.bounds.bottom);
394
+ const lo = key - wY;
395
+ const hi = key + wY;
396
+ while (q < byBottom.length && byBottom[q].bounds.bottom < lo - POINTER_SLACK)
397
+ q++;
398
+ for (let i = q; i < byBottom.length && byBottom[i].bounds.bottom <= hi; i++)
399
+ admitV(byBottom[i].bounds, e.bounds);
400
+ }
401
+ }
402
+ };
403
+ // -----------------------------------------------------------------------------
404
+ // 点对齐 getPointSnaps
405
+ // -----------------------------------------------------------------------------
406
+ /**
407
+ * 双重循环:选区每个关键点 × 每个参考点,分别按 X / Y 计算偏移,收集最近(含并列)匹配。
408
+ * 就地修改 nearestSnapsX / nearestSnapsY 与 minOffset。
409
+ */
410
+ export const getPointSnaps = (selectionPoints, referencePoints, minOffset, nearestSnapsX, nearestSnapsY) => {
411
+ for (const thisPoint of selectionPoints) {
412
+ for (const otherPoint of referencePoints) {
413
+ const offsetX = otherPoint.x - thisPoint.x;
414
+ const offsetY = otherPoint.y - thisPoint.y;
415
+ if (Math.abs(offsetX) <= minOffset.x) {
416
+ pushNearest(nearestSnapsX, offsetX, minOffset, 'x', {
417
+ type: 'point',
418
+ points: [thisPoint, otherPoint],
419
+ offset: offsetX
420
+ });
421
+ }
422
+ if (Math.abs(offsetY) <= minOffset.y) {
423
+ pushNearest(nearestSnapsY, offsetY, minOffset, 'y', {
424
+ type: 'point',
425
+ points: [thisPoint, otherPoint],
426
+ offset: offsetY
427
+ });
428
+ }
429
+ }
430
+ }
431
+ };
432
+ // -----------------------------------------------------------------------------
433
+ // 间距对齐 getGapSnaps
434
+ // -----------------------------------------------------------------------------
435
+ /**
436
+ * 对每个参考间距,判断选区是否落入三种等距位置之一,收集最近(含并列)匹配。
437
+ * 三种位置对同一间距**互斥**:取首个落入阈值的即 continue,故每条间距最多贡献一个 snap。
438
+ * X 轴:center_horizontal(仅当间距比选区宽)/ side_right / side_left
439
+ * Y 轴:center_vertical(仅当间距比选区高)/ side_top / side_bottom
440
+ */
441
+ export const getGapSnaps = (selectionBounds, gaps, minOffset, nearestSnapsX, nearestSnapsY) => {
442
+ const { left: minX, top: minY, right: maxX, bottom: maxY } = selectionBounds;
443
+ const centerX = (minX + maxX) / 2;
444
+ const centerY = (minY + maxY) / 2;
445
+ for (const gap of gaps.horizontalGaps) {
446
+ if (!rangesOverlap([minY, maxY], gap.overlap))
447
+ continue;
448
+ // 中心对齐:仅当间距比选区更宽时才有意义
449
+ const gapMidX = gap.startSide[0].x + gap.length / 2;
450
+ const centerOffset = round(gapMidX - centerX);
451
+ const gapIsLargerThanSelection = gap.length > maxX - minX;
452
+ if (gapIsLargerThanSelection && Math.abs(centerOffset) <= minOffset.x) {
453
+ if (Math.abs(centerOffset) < minOffset.x)
454
+ nearestSnapsX.length = 0;
455
+ minOffset.x = Math.abs(centerOffset);
456
+ nearestSnapsX.push({ type: 'gap', direction: 'center_horizontal', gap, offset: centerOffset });
457
+ continue;
458
+ }
459
+ // 等距贴近 end 元素(右侧):使 (start→end) 与 (end→选区) 等距
460
+ const distanceToEndElementX = minX - gap.endBounds.right;
461
+ const sideOffsetRight = round(gap.length - distanceToEndElementX);
462
+ if (Math.abs(sideOffsetRight) <= minOffset.x) {
463
+ if (Math.abs(sideOffsetRight) < minOffset.x)
464
+ nearestSnapsX.length = 0;
465
+ minOffset.x = Math.abs(sideOffsetRight);
466
+ nearestSnapsX.push({ type: 'gap', direction: 'side_right', gap, offset: sideOffsetRight });
467
+ continue;
468
+ }
469
+ // 等距贴近 start 元素(左侧)
470
+ const distanceToStartElementX = gap.startBounds.left - maxX;
471
+ const sideOffsetLeft = round(distanceToStartElementX - gap.length);
472
+ if (Math.abs(sideOffsetLeft) <= minOffset.x) {
473
+ if (Math.abs(sideOffsetLeft) < minOffset.x)
474
+ nearestSnapsX.length = 0;
475
+ minOffset.x = Math.abs(sideOffsetLeft);
476
+ nearestSnapsX.push({ type: 'gap', direction: 'side_left', gap, offset: sideOffsetLeft });
477
+ continue;
478
+ }
479
+ }
480
+ for (const gap of gaps.verticalGaps) {
481
+ if (!rangesOverlap([minX, maxX], gap.overlap))
482
+ continue;
483
+ const gapMidY = gap.startSide[0].y + gap.length / 2;
484
+ const centerOffset = round(gapMidY - centerY);
485
+ const gapIsLargerThanSelection = gap.length > maxY - minY;
486
+ if (gapIsLargerThanSelection && Math.abs(centerOffset) <= minOffset.y) {
487
+ if (Math.abs(centerOffset) < minOffset.y)
488
+ nearestSnapsY.length = 0;
489
+ minOffset.y = Math.abs(centerOffset);
490
+ nearestSnapsY.push({ type: 'gap', direction: 'center_vertical', gap, offset: centerOffset });
491
+ continue;
492
+ }
493
+ // 等距贴近 start 元素(上方)
494
+ const distanceToStartElementY = gap.startBounds.top - maxY;
495
+ const sideOffsetTop = round(distanceToStartElementY - gap.length);
496
+ if (Math.abs(sideOffsetTop) <= minOffset.y) {
497
+ if (Math.abs(sideOffsetTop) < minOffset.y)
498
+ nearestSnapsY.length = 0;
499
+ minOffset.y = Math.abs(sideOffsetTop);
500
+ nearestSnapsY.push({ type: 'gap', direction: 'side_top', gap, offset: sideOffsetTop });
501
+ continue;
502
+ }
503
+ // 等距贴近 end 元素(下方)
504
+ const distanceToEndElementY = round(minY - gap.endBounds.bottom);
505
+ const sideOffsetBottom = gap.length - distanceToEndElementY;
506
+ if (Math.abs(sideOffsetBottom) <= minOffset.y) {
507
+ if (Math.abs(sideOffsetBottom) < minOffset.y)
508
+ nearestSnapsY.length = 0;
509
+ minOffset.y = Math.abs(sideOffsetBottom);
510
+ nearestSnapsY.push({ type: 'gap', direction: 'side_bottom', gap, offset: sideOffsetBottom });
511
+ continue;
512
+ }
513
+ }
514
+ };
515
+ // -----------------------------------------------------------------------------
516
+ // 渲染线生成
517
+ // -----------------------------------------------------------------------------
518
+ const dedupePoints = (points) => {
519
+ const map = new Map();
520
+ for (const point of points) {
521
+ const key = `${round(point.x)},${round(point.y)}`;
522
+ if (!map.has(key))
523
+ map.set(key, point);
524
+ }
525
+ return Array.from(map.values());
526
+ };
527
+ /**
528
+ * 把点对齐命中按坐标分组、投影、去重、排序,合成可画的点对齐线。
529
+ * snapsX → 竖线(x=key,按 y 排序);snapsY → 横线(y=key,按 x 排序)。
530
+ */
531
+ export const createPointSnapLines = (nearestSnapsX, nearestSnapsY) => {
532
+ const snapsX = {};
533
+ const snapsY = {};
534
+ for (const snap of nearestSnapsX) {
535
+ if (snap.type !== 'point')
536
+ continue;
537
+ const key = String(round(snap.points[0].x));
538
+ if (!snapsX[key])
539
+ snapsX[key] = [];
540
+ snapsX[key].push(...snap.points.map((p) => ({ x: round(p.x), y: round(p.y) })));
541
+ }
542
+ for (const snap of nearestSnapsY) {
543
+ if (snap.type !== 'point')
544
+ continue;
545
+ const key = String(round(snap.points[0].y));
546
+ if (!snapsY[key])
547
+ snapsY[key] = [];
548
+ snapsY[key].push(...snap.points.map((p) => ({ x: round(p.x), y: round(p.y) })));
549
+ }
550
+ const fromX = Object.entries(snapsX).map(([key, points]) => ({
551
+ type: 'points',
552
+ points: dedupePoints(points.map((p) => ({ x: Number(key), y: p.y })).sort((a, b) => a.y - b.y))
553
+ }));
554
+ const fromY = Object.entries(snapsY).map(([key, points]) => ({
555
+ type: 'points',
556
+ points: dedupePoints(points.map((p) => ({ x: p.x, y: Number(key) })).sort((a, b) => a.x - b.x))
557
+ }));
558
+ return [...fromX, ...fromY];
559
+ };
560
+ const dedupeGapSnapLines = (gapSnapLines) => {
561
+ const map = new Map();
562
+ for (const line of gapSnapLines) {
563
+ const key = [line.points[0], line.points[1]].flatMap((p) => [round(p.x), round(p.y)]).join(',');
564
+ if (!map.has(key))
565
+ map.set(key, line);
566
+ }
567
+ return Array.from(map.values());
568
+ };
569
+ /**
570
+ * 按 6 种 gap direction 生成间距线段(每条间距贡献两段:选区左/右或上/下各一段)。
571
+ * selectionBounds 应为「吸附后」的选区包围盒。
572
+ */
573
+ export const createGapSnapLines = (selectionBounds, gapSnaps) => {
574
+ const { left: minX, top: minY, right: maxX, bottom: maxY } = selectionBounds;
575
+ const gapSnapLines = [];
576
+ for (const gapSnap of gapSnaps) {
577
+ const start = gapSnap.gap.startBounds;
578
+ const end = gapSnap.gap.endBounds;
579
+ const verticalIntersection = rangeIntersection([minY, maxY], gapSnap.gap.overlap);
580
+ const horizontalGapIntersection = rangeIntersection([minX, maxX], gapSnap.gap.overlap);
581
+ switch (gapSnap.direction) {
582
+ case 'center_horizontal': {
583
+ if (verticalIntersection) {
584
+ const gapLineY = (verticalIntersection[0] + verticalIntersection[1]) / 2;
585
+ gapSnapLines.push({
586
+ type: 'gap',
587
+ direction: 'horizontal',
588
+ points: [
589
+ { x: gapSnap.gap.startSide[0].x, y: gapLineY },
590
+ { x: minX, y: gapLineY }
591
+ ]
592
+ }, {
593
+ type: 'gap',
594
+ direction: 'horizontal',
595
+ points: [
596
+ { x: maxX, y: gapLineY },
597
+ { x: gapSnap.gap.endSide[0].x, y: gapLineY }
598
+ ]
599
+ });
600
+ }
601
+ break;
602
+ }
603
+ case 'center_vertical': {
604
+ if (horizontalGapIntersection) {
605
+ const gapLineX = (horizontalGapIntersection[0] + horizontalGapIntersection[1]) / 2;
606
+ gapSnapLines.push({
607
+ type: 'gap',
608
+ direction: 'vertical',
609
+ points: [
610
+ { x: gapLineX, y: gapSnap.gap.startSide[0].y },
611
+ { x: gapLineX, y: minY }
612
+ ]
613
+ }, {
614
+ type: 'gap',
615
+ direction: 'vertical',
616
+ points: [
617
+ { x: gapLineX, y: maxY },
618
+ { x: gapLineX, y: gapSnap.gap.endSide[0].y }
619
+ ]
620
+ });
621
+ }
622
+ break;
623
+ }
624
+ case 'side_right': {
625
+ if (verticalIntersection) {
626
+ const gapLineY = (verticalIntersection[0] + verticalIntersection[1]) / 2;
627
+ gapSnapLines.push({
628
+ type: 'gap',
629
+ direction: 'horizontal',
630
+ points: [
631
+ { x: start.right, y: gapLineY },
632
+ { x: end.left, y: gapLineY }
633
+ ]
634
+ }, {
635
+ type: 'gap',
636
+ direction: 'horizontal',
637
+ points: [
638
+ { x: end.right, y: gapLineY },
639
+ { x: minX, y: gapLineY }
640
+ ]
641
+ });
642
+ }
643
+ break;
644
+ }
645
+ case 'side_left': {
646
+ if (verticalIntersection) {
647
+ const gapLineY = (verticalIntersection[0] + verticalIntersection[1]) / 2;
648
+ gapSnapLines.push({
649
+ type: 'gap',
650
+ direction: 'horizontal',
651
+ points: [
652
+ { x: maxX, y: gapLineY },
653
+ { x: start.left, y: gapLineY }
654
+ ]
655
+ }, {
656
+ type: 'gap',
657
+ direction: 'horizontal',
658
+ points: [
659
+ { x: start.right, y: gapLineY },
660
+ { x: end.left, y: gapLineY }
661
+ ]
662
+ });
663
+ }
664
+ break;
665
+ }
666
+ case 'side_top': {
667
+ if (horizontalGapIntersection) {
668
+ const gapLineX = (horizontalGapIntersection[0] + horizontalGapIntersection[1]) / 2;
669
+ gapSnapLines.push({
670
+ type: 'gap',
671
+ direction: 'vertical',
672
+ points: [
673
+ { x: gapLineX, y: maxY },
674
+ { x: gapLineX, y: start.top }
675
+ ]
676
+ }, {
677
+ type: 'gap',
678
+ direction: 'vertical',
679
+ points: [
680
+ { x: gapLineX, y: start.bottom },
681
+ { x: gapLineX, y: end.top }
682
+ ]
683
+ });
684
+ }
685
+ break;
686
+ }
687
+ case 'side_bottom': {
688
+ if (horizontalGapIntersection) {
689
+ const gapLineX = (horizontalGapIntersection[0] + horizontalGapIntersection[1]) / 2;
690
+ gapSnapLines.push({
691
+ type: 'gap',
692
+ direction: 'vertical',
693
+ points: [
694
+ { x: gapLineX, y: start.bottom },
695
+ { x: gapLineX, y: end.top }
696
+ ]
697
+ }, {
698
+ type: 'gap',
699
+ direction: 'vertical',
700
+ points: [
701
+ { x: gapLineX, y: end.bottom },
702
+ { x: gapLineX, y: minY }
703
+ ]
704
+ });
705
+ }
706
+ break;
707
+ }
708
+ }
709
+ }
710
+ return dedupeGapSnapLines(gapSnapLines.map((line) => ({
711
+ ...line,
712
+ points: [
713
+ { x: round(line.points[0].x), y: round(line.points[0].y) },
714
+ { x: round(line.points[1].x), y: round(line.points[1].y) }
715
+ ]
716
+ })));
717
+ };
718
+ /**
719
+ * 拖拽吸附入口(两遍):
720
+ * 1. 在 snapDistance 阈值内求最近 snapOffset(决策移动量);
721
+ * 2. 把 snapOffset 应用到选区后,以 minOffset=0 收集「恰好对齐」的匹配生成辅助线。
722
+ * 返回的 snapOffset 为「加到选区中心」的增量,由调用方移动对象。
723
+ * gaps 收联合类型:GapIndex(按需枚举)/ 物化结果(oracle 路径),按入参形态分发。
724
+ */
725
+ export const snapDraggedElements = (selectionPoints, selectionBounds, referencePoints, gaps, snapDistance, axisIndex) => {
726
+ const runGapScan = (bounds, nearestSnapsX, nearestSnapsY) => {
727
+ if ('byLeft' in gaps)
728
+ getGapSnapsFromIndex(bounds, gaps, minOffset, nearestSnapsX, nearestSnapsY);
729
+ else
730
+ getGapSnaps(bounds, gaps, minOffset, nearestSnapsX, nearestSnapsY);
731
+ };
732
+ // 第一遍:求最近 snapOffset(点/间距均已取整,保证偏移是 1e-6 的整数倍)。
733
+ // axisIndex(批次 222):轴序索引在场走窗口扫描(等价重排),oracle/规格
734
+ // 对拍路径不传索引保持全量双重循环原样
735
+ const selBounds = roundBounds(selectionBounds);
736
+ const minOffset = { x: snapDistance, y: snapDistance };
737
+ const nearestSnapsX = [];
738
+ const nearestSnapsY = [];
739
+ if (axisIndex)
740
+ getPointSnapsWindowed(selectionPoints, axisIndex, snapDistance, snapDistance, minOffset, nearestSnapsX, nearestSnapsY);
741
+ else
742
+ getPointSnaps(selectionPoints, referencePoints, minOffset, nearestSnapsX, nearestSnapsY);
743
+ runGapScan(selBounds, nearestSnapsX, nearestSnapsY);
744
+ const snapOffset = {
745
+ x: round(nearestSnapsX[0]?.offset ?? 0),
746
+ y: round(nearestSnapsY[0]?.offset ?? 0)
747
+ };
748
+ // 第二遍:用吸附后位置、minOffset=0 收集精确对齐,生成辅助线。
749
+ // 偏移后的点/包围盒再次取整,抵消浮点加法残留,使第二遍的精确匹配(<=0)成立。
750
+ const offsetSelectionPoints = selectionPoints.map((p) => roundPoint({ x: p.x + snapOffset.x, y: p.y + snapOffset.y }));
751
+ const offsetSelectionBounds = roundBounds({
752
+ left: selBounds.left + snapOffset.x,
753
+ top: selBounds.top + snapOffset.y,
754
+ right: selBounds.right + snapOffset.x,
755
+ bottom: selBounds.bottom + snapOffset.y
756
+ });
757
+ minOffset.x = 0;
758
+ minOffset.y = 0;
759
+ nearestSnapsX.length = 0;
760
+ nearestSnapsY.length = 0;
761
+ if (axisIndex)
762
+ getPointSnapsWindowed(offsetSelectionPoints, axisIndex, 0, 0, minOffset, nearestSnapsX, nearestSnapsY);
763
+ else
764
+ getPointSnaps(offsetSelectionPoints, referencePoints, minOffset, nearestSnapsX, nearestSnapsY);
765
+ runGapScan(offsetSelectionBounds, nearestSnapsX, nearestSnapsY);
766
+ const pointSnapLines = createPointSnapLines(nearestSnapsX, nearestSnapsY);
767
+ const gapSnaps = [...nearestSnapsX, ...nearestSnapsY].filter((s) => s.type === 'gap');
768
+ const gapSnapLines = createGapSnapLines(offsetSelectionBounds, gapSnaps);
769
+ return { snapOffset, pointSnapLines, gapSnapLines };
770
+ };
771
+ /**
772
+ * 缩放吸附入口(point-only)。
773
+ * 本编辑器缩放路径**不磁吸**(只画辅助线),故用单阈值遍得到的最近(含并列)匹配直接生成辅助线;
774
+ * snapOffset 仍一并返回,供将来若启用「磁吸式 resize」时使用。
775
+ */
776
+ export const snapResizingElements = (cornerPoints, referencePoints, snapDistance) => {
777
+ const minOffset = { x: snapDistance, y: snapDistance };
778
+ const nearestSnapsX = [];
779
+ const nearestSnapsY = [];
780
+ getPointSnaps(cornerPoints, referencePoints, minOffset, nearestSnapsX, nearestSnapsY);
781
+ const snapOffset = {
782
+ x: round(nearestSnapsX[0]?.offset ?? 0),
783
+ y: round(nearestSnapsY[0]?.offset ?? 0)
784
+ };
785
+ minOffset.x = 0;
786
+ minOffset.y = 0;
787
+ nearestSnapsX.length = 0;
788
+ nearestSnapsY.length = 0;
789
+ const offsetCornerPoints = cornerPoints.map((p) => roundPoint({ x: p.x + snapOffset.x, y: p.y + snapOffset.y }));
790
+ getPointSnaps(offsetCornerPoints, referencePoints, minOffset, nearestSnapsX, nearestSnapsY);
791
+ const pointSnapLines = createPointSnapLines(nearestSnapsX, nearestSnapsY);
792
+ return { snapOffset, pointSnapLines };
793
+ };