@open-artifact/whiteboard-core 0.0.0-stage → 0.2.0

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 (138) hide show
  1. package/LICENSE +202 -0
  2. package/NOTICE +5 -0
  3. package/README.md +5 -2
  4. package/package.json +38 -4
  5. package/src/arrows/bend-drag.d.ts +61 -0
  6. package/src/arrows/bend-drag.js +118 -0
  7. package/src/arrows/end-snap.d.ts +50 -0
  8. package/src/arrows/end-snap.js +81 -0
  9. package/src/arrows/labels.d.ts +135 -0
  10. package/src/arrows/labels.js +205 -0
  11. package/src/arrows/layout.d.ts +214 -0
  12. package/src/arrows/layout.js +491 -0
  13. package/src/arrows/routing.d.ts +163 -0
  14. package/src/arrows/routing.js +253 -0
  15. package/src/attachments.d.ts +83 -0
  16. package/src/attachments.js +244 -0
  17. package/src/backdrop.d.ts +27 -0
  18. package/src/backdrop.js +65 -0
  19. package/src/board-file.d.ts +91 -0
  20. package/src/board-file.js +136 -0
  21. package/src/check.d.ts +66 -0
  22. package/src/check.js +327 -0
  23. package/src/claims.d.ts +196 -0
  24. package/src/claims.js +367 -0
  25. package/src/clipboard.d.ts +102 -0
  26. package/src/clipboard.js +221 -0
  27. package/src/codec.d.ts +114 -0
  28. package/src/codec.js +218 -0
  29. package/src/comment-anchors.d.ts +251 -0
  30. package/src/comment-anchors.js +515 -0
  31. package/src/derived.d.ts +16 -0
  32. package/src/derived.js +20 -0
  33. package/src/drawn-svg/contrast.d.ts +46 -0
  34. package/src/drawn-svg/contrast.js +192 -0
  35. package/src/drawn-svg/restyle.d.ts +103 -0
  36. package/src/drawn-svg/restyle.js +480 -0
  37. package/src/drawn-svg/svg-geometry.d.ts +105 -0
  38. package/src/drawn-svg/svg-geometry.js +342 -0
  39. package/src/drawn-svg/vetting.d.ts +77 -0
  40. package/src/drawn-svg/vetting.js +213 -0
  41. package/src/drawn-svg/xml.d.ts +122 -0
  42. package/src/drawn-svg/xml.js +481 -0
  43. package/src/edits.d.ts +225 -0
  44. package/src/edits.js +588 -0
  45. package/src/geom-codec.d.ts +26 -0
  46. package/src/geom-codec.js +98 -0
  47. package/src/geometry.d.ts +260 -0
  48. package/src/geometry.js +472 -0
  49. package/src/groups.d.ts +143 -0
  50. package/src/groups.js +339 -0
  51. package/src/history/board-diff.d.ts +74 -0
  52. package/src/history/board-diff.js +260 -0
  53. package/src/history/board-steps.d.ts +61 -0
  54. package/src/history/board-steps.js +115 -0
  55. package/src/history/command-history.d.ts +81 -0
  56. package/src/history/command-history.js +151 -0
  57. package/src/history/content.d.ts +24 -0
  58. package/src/history/content.js +84 -0
  59. package/src/history/table-steps.d.ts +98 -0
  60. package/src/history/table-steps.js +220 -0
  61. package/src/history.d.ts +8 -0
  62. package/src/history.js +8 -0
  63. package/src/hit-test.d.ts +98 -0
  64. package/src/hit-test.js +317 -0
  65. package/src/ideas.d.ts +295 -0
  66. package/src/ideas.js +632 -0
  67. package/src/image-crop.d.ts +68 -0
  68. package/src/image-crop.js +140 -0
  69. package/src/image-source.d.ts +67 -0
  70. package/src/image-source.js +170 -0
  71. package/src/index.d.ts +62 -0
  72. package/src/index.js +68 -0
  73. package/src/ink.d.ts +141 -0
  74. package/src/ink.js +233 -0
  75. package/src/inks.d.ts +37 -0
  76. package/src/inks.js +55 -0
  77. package/src/json.d.ts +2 -0
  78. package/src/json.js +2 -0
  79. package/src/layout.d.ts +173 -0
  80. package/src/layout.js +365 -0
  81. package/src/links.d.ts +43 -0
  82. package/src/links.js +84 -0
  83. package/src/next-item.d.ts +72 -0
  84. package/src/next-item.js +129 -0
  85. package/src/normalize.d.ts +67 -0
  86. package/src/normalize.js +427 -0
  87. package/src/reading-order.d.ts +17 -0
  88. package/src/reading-order.js +178 -0
  89. package/src/schema.d.ts +438 -0
  90. package/src/schema.js +308 -0
  91. package/src/selection.d.ts +238 -0
  92. package/src/selection.js +490 -0
  93. package/src/shadows.d.ts +19 -0
  94. package/src/shadows.js +21 -0
  95. package/src/shape-paths.d.ts +120 -0
  96. package/src/shape-paths.js +197 -0
  97. package/src/snapping.d.ts +84 -0
  98. package/src/snapping.js +108 -0
  99. package/src/sticker-hosts.d.ts +43 -0
  100. package/src/sticker-hosts.js +157 -0
  101. package/src/stickers.d.ts +63 -0
  102. package/src/stickers.js +799 -0
  103. package/src/sync/doc-hash.d.ts +1 -0
  104. package/src/sync/doc-hash.js +1 -0
  105. package/src/sync/engine.d.ts +1 -0
  106. package/src/sync/engine.js +1 -0
  107. package/src/sync/index.d.ts +9 -0
  108. package/src/sync/index.js +9 -0
  109. package/src/sync/live-board.d.ts +66 -0
  110. package/src/sync/live-board.js +157 -0
  111. package/src/sync/transport.d.ts +1 -0
  112. package/src/sync/transport.js +1 -0
  113. package/src/table.d.ts +180 -0
  114. package/src/table.js +576 -0
  115. package/src/text/fit.d.ts +138 -0
  116. package/src/text/fit.js +274 -0
  117. package/src/text/inline-markdown.d.ts +71 -0
  118. package/src/text/inline-markdown.js +179 -0
  119. package/src/text/links.d.ts +19 -0
  120. package/src/text/links.js +26 -0
  121. package/src/text/markup.d.ts +265 -0
  122. package/src/text/markup.js +339 -0
  123. package/src/text/measure.d.ts +74 -0
  124. package/src/text/measure.js +147 -0
  125. package/src/text/note-markdown.d.ts +59 -0
  126. package/src/text/note-markdown.js +71 -0
  127. package/src/text/text-arrows.d.ts +45 -0
  128. package/src/text/text-arrows.js +58 -0
  129. package/src/text-limits.d.ts +14 -0
  130. package/src/text-limits.js +27 -0
  131. package/src/theme.d.ts +114 -0
  132. package/src/theme.js +281 -0
  133. package/src/timer.d.ts +64 -0
  134. package/src/timer.js +128 -0
  135. package/src/version-board.d.ts +28 -0
  136. package/src/version-board.js +74 -0
  137. package/src/view-context.d.ts +94 -0
  138. package/src/view-context.js +161 -0
package/LICENSE ADDED
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright [yyyy] [name of copyright owner]
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
package/NOTICE ADDED
@@ -0,0 +1,5 @@
1
+ Open Artifact
2
+ Copyright 2026 Jim Liu 宝玉
3
+
4
+ This product is licensed under the Apache License, Version 2.0 (see LICENSE).
5
+ It was formerly published as baoyu-design.
package/README.md CHANGED
@@ -1,3 +1,6 @@
1
- # Temporary Holding Version
1
+ # @open-artifact/whiteboard-core
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ The Whiteboard artifact model: element schema and normalization, the document codec, geometry, arrows, hand-drawn ink, history, view context and the live sync engine.
4
+
5
+ Part of [Open Artifact](https://github.com/JimLiu/open-artifact): slide decks, design canvases and whiteboards an agent makes
6
+ and you view, edit and comment on in one local page. Source: [`packages/whiteboard-core`](https://github.com/JimLiu/open-artifact/tree/main/packages/whiteboard-core).
package/package.json CHANGED
@@ -1,6 +1,40 @@
1
1
  {
2
2
  "name": "@open-artifact/whiteboard-core",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.2.0",
4
+ "description": "The Whiteboard artifact model: element schema and normalization, the document codec, geometry, arrows, hand-drawn ink, history, view context and the live sync engine.",
5
+ "type": "module",
6
+ "exports": {
7
+ ".": {
8
+ "types": "./src/index.d.ts",
9
+ "default": "./src/index.js"
10
+ },
11
+ "./package.json": "./package.json"
12
+ },
13
+ "files": [
14
+ "src",
15
+ "LICENSE",
16
+ "NOTICE"
17
+ ],
18
+ "dependencies": {
19
+ "@open-artifact/live-store": "0.2.0",
20
+ "@open-artifact/protocol": "0.2.0",
21
+ "@open-artifact/wasm-wb-geom": "0.2.0"
22
+ },
23
+ "license": "Apache-2.0",
24
+ "author": {
25
+ "name": "Jim Liu 宝玉",
26
+ "url": "https://github.com/JimLiu"
27
+ },
28
+ "homepage": "https://github.com/JimLiu/open-artifact#readme",
29
+ "repository": {
30
+ "type": "git",
31
+ "url": "git+https://github.com/JimLiu/open-artifact.git",
32
+ "directory": "packages/whiteboard-core"
33
+ },
34
+ "bugs": {
35
+ "url": "https://github.com/JimLiu/open-artifact/issues"
36
+ },
37
+ "publishConfig": {
38
+ "access": "public"
39
+ }
40
+ }
@@ -0,0 +1,61 @@
1
+ /**
2
+ * Dragging one leg of an elbow arrow sideways to bend its route.
3
+ *
4
+ * A selected elbow arrow shows a handle on each leg that can move. Dragging
5
+ * it slides the leg across its own direction (neighboring legs stretch) and
6
+ * stores the new route as manual `bends`. A drag that lands back on the
7
+ * automatic route, or on the straight line between the ends, clears the
8
+ * bends instead, so an arrow straightens by being dragged back into line.
9
+ *
10
+ * The leg handle and clearance math runs in WebAssembly
11
+ * (`crates/open-artifact-wb-geom/src/bend_drag.rs`). The drag itself stays here: it
12
+ * combines board lookups and arrow layouts that already run there.
13
+ */
14
+ import { type ArrowBends, type ArrowElement } from '../schema.ts';
15
+ import type { ArrowLabelBox } from './labels.ts';
16
+ import { type ArrowGeometry, type BoardIndex, type ElementLayoutBoxResolver } from './layout.ts';
17
+ import { type RoutePoint } from './routing.ts';
18
+ /** How close (screen pixels) a dragged leg counts as back on the automatic route. */
19
+ export declare const ARROW_LEG_GRAB_PX = 4;
20
+ /** Extra room a fitted manual route must keep from the end boxes to count as routable. */
21
+ export declare const BEND_FIT_TOLERANCE = 4;
22
+ /** Handle reach from an arrow end, screen pixels: a leg handle this close to an end moves along the leg. */
23
+ export declare const ARROW_LEG_HANDLE_REACH: {
24
+ readonly fine: 24;
25
+ readonly coarse: 40;
26
+ };
27
+ /**
28
+ * The shortest end leg each end of `route` must keep: room for the arrow
29
+ * head (bounded by how long the end leg already is, on this route or the
30
+ * automatic one), or the plain minimum for an end without a head.
31
+ */
32
+ export declare function arrowEndClearances(arrow: ArrowElement, route: readonly RoutePoint[], board: BoardIndex, layoutBox: ElementLayoutBoxResolver): [number, number];
33
+ /**
34
+ * What dragging leg `leg` of `route` to the point `x, y` does to the
35
+ * arrow's manual route:
36
+ * - `'same'`: the leg cannot move there (nothing changes);
37
+ * - bends: the new manual route;
38
+ * - `null`: the drag lands on the automatic or straight route (clear the bends);
39
+ * - `undefined`: no valid route (keep the last one).
40
+ *
41
+ * `tolerance` is how close (board units) counts as landing on a route.
42
+ */
43
+ export declare function bendsForDraggedSegment(arrow: ArrowElement, route: readonly RoutePoint[], leg: number, x: number, y: number, tolerance: number, board: BoardIndex, layoutBox: ElementLayoutBoxResolver): ArrowBends | null | undefined | 'same';
44
+ /** A handle that drags one leg of an arrow. */
45
+ export interface ArrowLegHandle {
46
+ /** Index of the leg (it runs from route point `leg` to `leg + 1`). */
47
+ leg: number;
48
+ /** Where the handle sits, board units. */
49
+ x: number;
50
+ y: number;
51
+ /** Whether the leg runs horizontally (the handle drags up and down). */
52
+ level: boolean;
53
+ }
54
+ /**
55
+ * The leg handles of a selected arrow drawn as `line`: one at the middle of
56
+ * each leg that can move, slid along the leg off the label and away from the
57
+ * end handles. A straight arrow gets no handle too close to its ends.
58
+ *
59
+ * `zoom` is the camera zoom; `coarse` is a touch pointer (wider reach).
60
+ */
61
+ export declare function arrowLegHandles(arrow: ArrowElement, line: ArrowGeometry, board: BoardIndex, layoutBox: ElementLayoutBoxResolver, label: ArrowLabelBox | undefined, zoom: number, coarse: boolean): ArrowLegHandle[];
@@ -0,0 +1,118 @@
1
+ /**
2
+ * Dragging one leg of an elbow arrow sideways to bend its route.
3
+ *
4
+ * A selected elbow arrow shows a handle on each leg that can move. Dragging
5
+ * it slides the leg across its own direction (neighboring legs stretch) and
6
+ * stores the new route as manual `bends`. A drag that lands back on the
7
+ * automatic route, or on the straight line between the ends, clears the
8
+ * bends instead, so an arrow straightens by being dragged back into line.
9
+ *
10
+ * The leg handle and clearance math runs in WebAssembly
11
+ * (`crates/open-artifact-wb-geom/src/bend_drag.rs`). The drag itself stays here: it
12
+ * combines board lookups and arrow layouts that already run there.
13
+ */
14
+ import { wbGeom } from '@open-artifact/wasm-wb-geom';
15
+ import { encodePoints } from "../geom-codec.js";
16
+ import { distanceToSegmentSq } from "../hit-test.js";
17
+ import { MAX_ARROW_BENDS, arrowHeads } from "../schema.js";
18
+ import { NO_ARROW_SPREAD, arrowEndAsBox, arrowEndCenter, arrowGeometry, bendOrigins, insetArrowEndsToOutline, resolveArrowEndGeometry, routeElbowArrow, routePlanForArrow, } from "./layout.js";
19
+ import { ARROW_POINT_SNAP_DISTANCE, bendsFromRoute, moveRouteSegment, routeFromBendsOrUndefined, } from "./routing.js";
20
+ /** How close (screen pixels) a dragged leg counts as back on the automatic route. */
21
+ export const ARROW_LEG_GRAB_PX = 4;
22
+ /** Extra room a fitted manual route must keep from the end boxes to count as routable. */
23
+ export const BEND_FIT_TOLERANCE = 4;
24
+ /** Handle reach from an arrow end, screen pixels: a leg handle this close to an end moves along the leg. */
25
+ export const ARROW_LEG_HANDLE_REACH = { fine: 24, coarse: 40 };
26
+ /** The arrow without a manual route (no `bends` and no `jog`). */
27
+ function withoutRoute(arrow) {
28
+ const { bends: _bends, jog: _jog, ...rest } = arrow;
29
+ return rest;
30
+ }
31
+ /**
32
+ * The shortest end leg each end of `route` must keep: room for the arrow
33
+ * head (bounded by how long the end leg already is, on this route or the
34
+ * automatic one), or the plain minimum for an end without a head.
35
+ */
36
+ export function arrowEndClearances(arrow, route, board, layoutBox) {
37
+ const auto = arrowGeometry(withoutRoute(arrow), board, layoutBox)?.elbow ?? [];
38
+ const heads = arrowHeads(arrow);
39
+ const values = wbGeom().arrow_end_clearances(encodePoints(route), encodePoints(auto), heads[0] !== 'none', heads[1] !== 'none');
40
+ return [values[0], values[1]];
41
+ }
42
+ /**
43
+ * What dragging leg `leg` of `route` to the point `x, y` does to the
44
+ * arrow's manual route:
45
+ * - `'same'`: the leg cannot move there (nothing changes);
46
+ * - bends: the new manual route;
47
+ * - `null`: the drag lands on the automatic or straight route (clear the bends);
48
+ * - `undefined`: no valid route (keep the last one).
49
+ *
50
+ * `tolerance` is how close (board units) counts as landing on a route.
51
+ */
52
+ export function bendsForDraggedSegment(arrow, route, leg, x, y, tolerance, board, layoutBox) {
53
+ const legStart = route[leg];
54
+ const legEnd = route[leg + 1];
55
+ const fromEnd = resolveArrowEndGeometry(arrow.from, board, layoutBox);
56
+ const toEnd = resolveArrowEndGeometry(arrow.to, board, layoutBox);
57
+ if (!legStart || !legEnd || !fromEnd || !toEnd)
58
+ return undefined;
59
+ const horizontal = Math.abs(legEnd[0] - legStart[0]) >= Math.abs(legEnd[1] - legStart[1]);
60
+ const dragged = moveRouteSegment(route, leg, horizontal ? y : x, ['w' in fromEnd, 'w' in toEnd], arrowEndClearances(arrow, route, board, layoutBox));
61
+ if (dragged.length === route.length && dragged.every((point, index) => point[0] === route[index]?.[0] && point[1] === route[index]?.[1]))
62
+ return 'same';
63
+ const unbent = withoutRoute(arrow);
64
+ const straight = arrowGeometry(unbent, board, layoutBox);
65
+ // A drag that keeps every corner on the straight line between the ends straightens the arrow.
66
+ const straightResult = straight?.elbow?.length === 2 && dragged.every((point) => distanceToSegmentSq(point[0], point[1], straight.x1, straight.y1, straight.x2, straight.y2) < ARROW_POINT_SNAP_DISTANCE ** 2)
67
+ ? null
68
+ : undefined;
69
+ const fit = bendsFromRoute(dragged);
70
+ if (fit === undefined)
71
+ return straightResult;
72
+ // One bend back at the middle of a straight arrow: straight again.
73
+ if (straight?.elbow?.length === 2 && fit.at.length === 1 && Math.abs((fit.at[0] ?? NaN) - (fit.first === 'h' ? straight.x1 + straight.x2 : straight.y1 + straight.y2) / 2) < tolerance)
74
+ return null;
75
+ if (fit.at.length > MAX_ARROW_BENDS)
76
+ return undefined;
77
+ const origins = bendOrigins(fit.first, fit.at.length, arrowEndCenter(fromEnd), arrowEndCenter(toEnd));
78
+ const bends = [fit.first, ...fit.at.map((at, index) => at - origins[index])];
79
+ const candidate = { ...unbent, bends };
80
+ const candidateLine = arrowGeometry(candidate, board, layoutBox);
81
+ if (!candidateLine?.bent)
82
+ return straightResult;
83
+ // Landing on the automatic route clears the bends.
84
+ const matchesStraight = (elbow) => straight?.elbow?.length === elbow?.length &&
85
+ (straight?.elbow ?? []).every((point, index) => Math.abs(point[0] - (elbow?.[index]?.[0] ?? NaN)) < tolerance && Math.abs(point[1] - (elbow?.[index]?.[1] ?? NaN)) < tolerance);
86
+ const autoExits = routeElbowArrow(unbent, fromEnd, toEnd, routePlanForArrow(unbent, board, layoutBox))?.exits ?? NO_ARROW_SPREAD.exits;
87
+ const autoRoute = routeElbowArrow(candidate, fromEnd, toEnd, { exits: autoExits, stagger: 0 });
88
+ if (matchesStraight(candidateLine.elbow) ||
89
+ (autoRoute?.bent && autoRoute.pts.length === candidateLine.elbow?.length && matchesStraight(insetArrowEndsToOutline(autoRoute.pts, arrow, board, autoRoute.sides, autoRoute.exits, layoutBox))))
90
+ return null;
91
+ // Keep the new bends when they route (with or without spreading); when the
92
+ // old route did not route either, keep them anyway rather than getting stuck.
93
+ const exitOptions = [NO_ARROW_SPREAD.exits, routePlanForArrow(candidate, board, layoutBox).exits];
94
+ const routable = (bendsFit) => bendsFit === undefined ||
95
+ exitOptions.some((exits) => routeFromBendsOrUndefined(arrowEndAsBox(fromEnd), 'w' in fromEnd ? arrow.fromSide : undefined, arrowEndAsBox(toEnd), 'w' in toEnd ? arrow.toSide : undefined, bendsFit.first, bendsFit.at, BEND_FIT_TOLERANCE, exits) !== undefined);
96
+ return routable(fit) || !routable(bendsFromRoute(route)) ? bends : straightResult;
97
+ }
98
+ /**
99
+ * The leg handles of a selected arrow drawn as `line`: one at the middle of
100
+ * each leg that can move, slid along the leg off the label and away from the
101
+ * end handles. A straight arrow gets no handle too close to its ends.
102
+ *
103
+ * `zoom` is the camera zoom; `coarse` is a touch pointer (wider reach).
104
+ */
105
+ export function arrowLegHandles(arrow, line, board, layoutBox, label, zoom, coarse) {
106
+ const elbow = line.elbow;
107
+ if (!elbow)
108
+ return [];
109
+ const clearances = arrowEndClearances(arrow, elbow, board, layoutBox);
110
+ const reach = (coarse ? ARROW_LEG_HANDLE_REACH.coarse : ARROW_LEG_HANDLE_REACH.fine) / zoom;
111
+ const labelValues = label ? Float64Array.of(1, label.onLine ? 1 : 0, label.x, label.y, label.w, label.h) : new Float64Array(6);
112
+ const lineValues = Float64Array.of(line.x1, line.y1, line.x2, line.y2, line.bent ? 1 : 0);
113
+ const values = wbGeom().arrow_leg_handles(encodePoints(elbow), typeof arrow.from === 'string', typeof arrow.to === 'string', clearances[0], clearances[1], labelValues, reach, zoom, lineValues);
114
+ const handles = [];
115
+ for (let index = 0; index + 3 < values.length; index += 4)
116
+ handles.push({ leg: values[index], x: values[index + 1], y: values[index + 2], level: values[index + 3] === 1 });
117
+ return handles;
118
+ }
@@ -0,0 +1,50 @@
1
+ /**
2
+ * Snapping a dragged arrow end: lining it up with the other end, and
3
+ * finding the element it attaches to (the nearest one within reach,
4
+ * measured to the real outline of ellipses, hollow shapes and irregular
5
+ * shapes, honoring rotation).
6
+ *
7
+ * The math runs in WebAssembly (`crates/open-artifact-wb-geom/src/end_snap.rs`):
8
+ * {@link findArrowEndTarget} sends all candidates (with their layout
9
+ * boxes, resolved here) in one call.
10
+ */
11
+ import { type ArrowElement, type ArrowEnd, type ArrowSide, type BoardElement, type ElementId, type ShapeKind } from '../schema.ts';
12
+ import type { ArrowEndPoint, ElementLayoutBoxResolver } from './layout.ts';
13
+ import type { RouteBox } from './routing.ts';
14
+ /** How close (in screen pixels) a dragged end snaps, per pointer type. */
15
+ export declare const ARROW_END_SNAP_PX: {
16
+ readonly mouse: 20;
17
+ readonly touch: 28;
18
+ };
19
+ /** Shapes whose distance is measured to their box from outside and to their outline from inside. */
20
+ export declare const BOX_DISTANCE_SHAPE_KINDS: ReadonlySet<ShapeKind>;
21
+ /** The other end of an arrow and its side. */
22
+ export declare function arrowOppositeEnd(arrow: Pick<ArrowElement, 'from' | 'to' | 'fromSide' | 'toSide'>, end: 'from' | 'to'): [ArrowEnd, ArrowSide | undefined];
23
+ /** Distance from a point to a box (0 inside). */
24
+ export declare function pointToBoxDistance(box: RouteBox, x: number, y: number): number;
25
+ /**
26
+ * A dragged end at `x, y` lined up with the other end `from` when it is
27
+ * within `snapPx` of level (or plumb) and the drag runs along that axis
28
+ * (out of `side`, when the other end has one).
29
+ */
30
+ export declare function alignArrowEndPoint(from: ArrowEndPoint, side: ArrowSide | undefined, x: number, y: number, snapPx: number): ArrowEndPoint;
31
+ /**
32
+ * Distance from a point (in the element's unrotated frame) to where an
33
+ * arrow end would attach: the box for most elements, the real outline of
34
+ * ellipses and hollow shapes (a point inside a hollow shape is as far as the
35
+ * nearest edge). 0 means the point is on (or, for filled boxes, in) the
36
+ * element.
37
+ */
38
+ export declare function arrowEndDistanceToElement(element: BoardElement, bounds: RouteBox, x: number, y: number, reach: number): number;
39
+ /** The element an arrow end attaches to and the side facing the pointer. */
40
+ export interface ArrowEndTarget {
41
+ end: ElementId;
42
+ side: ArrowSide;
43
+ }
44
+ /**
45
+ * The element nearest to `x, y` within `reach` that an arrow end can attach
46
+ * to (`elements` in hit order, topmost first; stickers are skipped; ties go
47
+ * to non-containers). A point exactly on a filled element is not a target:
48
+ * the caller handles direct hits.
49
+ */
50
+ export declare function findArrowEndTarget(elements: Iterable<BoardElement>, x: number, y: number, reach: number, layoutBox: ElementLayoutBoxResolver): ArrowEndTarget | undefined;
@@ -0,0 +1,81 @@
1
+ /**
2
+ * Snapping a dragged arrow end: lining it up with the other end, and
3
+ * finding the element it attaches to (the nearest one within reach,
4
+ * measured to the real outline of ellipses, hollow shapes and irregular
5
+ * shapes, honoring rotation).
6
+ *
7
+ * The math runs in WebAssembly (`crates/open-artifact-wb-geom/src/end_snap.rs`):
8
+ * {@link findArrowEndTarget} sends all candidates (with their layout
9
+ * boxes, resolved here) in one call.
10
+ */
11
+ import { wbGeom } from '@open-artifact/wasm-wb-geom';
12
+ import { SIDE_ORDER, kindCode } from "../geom-codec.js";
13
+ import { elementTurn } from "../schema.js";
14
+ /** How close (in screen pixels) a dragged end snaps, per pointer type. */
15
+ export const ARROW_END_SNAP_PX = { mouse: 20, touch: 28 };
16
+ /** Shapes whose distance is measured to their box from outside and to their outline from inside. */
17
+ export const BOX_DISTANCE_SHAPE_KINDS = new Set(['cloud', 'parallelogram', 'person']);
18
+ /** The other end of an arrow and its side. */
19
+ export function arrowOppositeEnd(arrow, end) {
20
+ return end === 'from' ? [arrow.to, arrow.toSide] : [arrow.from, arrow.fromSide];
21
+ }
22
+ /** Numbers per element sent to {@link findArrowEndTarget}. */
23
+ const ELEMENT_STRIDE = 13;
24
+ /** Writes an element as `[type, kind, hollow, x, y, w, h, turn, hasBounds, bx, by, bw, bh]`. */
25
+ function encodeElement(element, bounds, out, at) {
26
+ const shape = element.type === 'shape';
27
+ out[at] = element.type === 'container' ? 1 : shape ? 2 : 0;
28
+ out[at + 1] = shape ? kindCode(element.kind) : 0;
29
+ out[at + 2] = shape && element.hollow === true ? 1 : 0;
30
+ // Elements without a box field (drawings have no w, h) send NaN, as the original arithmetic on undefined gives.
31
+ const placed = element;
32
+ out[at + 3] = placed.x ?? NaN;
33
+ out[at + 4] = placed.y ?? NaN;
34
+ out[at + 5] = placed.w ?? NaN;
35
+ out[at + 6] = placed.h ?? NaN;
36
+ out[at + 7] = elementTurn(element);
37
+ if (bounds)
38
+ out.set([1, bounds.x, bounds.y, bounds.w, bounds.h], at + 8);
39
+ }
40
+ function encodeBox(box) {
41
+ return Float64Array.of(box.x, box.y, box.w, box.h);
42
+ }
43
+ /** Distance from a point to a box (0 inside). */
44
+ export function pointToBoxDistance(box, x, y) {
45
+ return wbGeom().point_to_box_distance(encodeBox(box), x, y);
46
+ }
47
+ /**
48
+ * A dragged end at `x, y` lined up with the other end `from` when it is
49
+ * within `snapPx` of level (or plumb) and the drag runs along that axis
50
+ * (out of `side`, when the other end has one).
51
+ */
52
+ export function alignArrowEndPoint(from, side, x, y, snapPx) {
53
+ const index = side === undefined ? 255 : SIDE_ORDER.indexOf(side);
54
+ const values = wbGeom().align_arrow_end_point(from.x, from.y, index < 0 ? 254 : index, x, y, snapPx);
55
+ return { x: values[0], y: values[1] };
56
+ }
57
+ /**
58
+ * Distance from a point (in the element's unrotated frame) to where an
59
+ * arrow end would attach: the box for most elements, the real outline of
60
+ * ellipses and hollow shapes (a point inside a hollow shape is as far as the
61
+ * nearest edge). 0 means the point is on (or, for filled boxes, in) the
62
+ * element.
63
+ */
64
+ export function arrowEndDistanceToElement(element, bounds, x, y, reach) {
65
+ const values = new Float64Array(ELEMENT_STRIDE);
66
+ encodeElement(element, undefined, values, 0);
67
+ return wbGeom().arrow_end_distance_to_element(values, encodeBox(bounds), x, y, reach);
68
+ }
69
+ /**
70
+ * The element nearest to `x, y` within `reach` that an arrow end can attach
71
+ * to (`elements` in hit order, topmost first; stickers are skipped; ties go
72
+ * to non-containers). A point exactly on a filled element is not a target:
73
+ * the caller handles direct hits.
74
+ */
75
+ export function findArrowEndTarget(elements, x, y, reach, layoutBox) {
76
+ const list = [...elements];
77
+ const values = new Float64Array(list.length * ELEMENT_STRIDE);
78
+ list.forEach((element, index) => encodeElement(element, element.type === 'sticker' ? undefined : layoutBox(element), values, index * ELEMENT_STRIDE));
79
+ const found = wbGeom().find_arrow_end_target(values, x, y, reach);
80
+ return found.length === 0 ? undefined : { end: list[found[0]].id, side: SIDE_ORDER[found[1]] };
81
+ }