@daasboard/editor 0.0.0-stage → 0.1.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 (83) hide show
  1. package/LICENSE +5 -0
  2. package/README.md +6 -2
  3. package/dist/camera/CameraInput.d.ts +106 -0
  4. package/dist/camera/CameraInput.js +398 -0
  5. package/dist/clipboard/ObjectClipboard.d.ts +40 -0
  6. package/dist/clipboard/ObjectClipboard.js +243 -0
  7. package/dist/clipboard/PasteCommitError.d.ts +4 -0
  8. package/dist/clipboard/PasteCommitError.js +7 -0
  9. package/dist/clipboard/carrier.d.ts +5 -0
  10. package/dist/clipboard/carrier.js +59 -0
  11. package/dist/clipboard/objects.d.ts +21 -0
  12. package/dist/clipboard/objects.js +362 -0
  13. package/dist/clipboard/placement.d.ts +11 -0
  14. package/dist/clipboard/placement.js +21 -0
  15. package/dist/clipboard/systemImages.d.ts +1 -0
  16. package/dist/clipboard/systemImages.js +24 -0
  17. package/dist/index.d.ts +23 -0
  18. package/dist/index.js +22 -0
  19. package/dist/input/TouchInput.d.ts +159 -0
  20. package/dist/input/TouchInput.js +656 -0
  21. package/dist/input/visiblePart.d.ts +8 -0
  22. package/dist/input/visiblePart.js +15 -0
  23. package/dist/interaction/InteractionLayer.d.ts +83 -0
  24. package/dist/interaction/InteractionLayer.js +520 -0
  25. package/dist/props/PropertyController.d.ts +130 -0
  26. package/dist/props/PropertyController.js +1448 -0
  27. package/dist/props/PropertyRegistry.d.ts +38 -0
  28. package/dist/props/PropertyRegistry.js +21 -0
  29. package/dist/props/builtinProperties.d.ts +23 -0
  30. package/dist/props/builtinProperties.js +350 -0
  31. package/dist/props/defaults.d.ts +6 -0
  32. package/dist/props/defaults.js +29 -0
  33. package/dist/selection/SelectionModel.d.ts +25 -0
  34. package/dist/selection/SelectionModel.js +79 -0
  35. package/dist/text/CanvasEditorView.d.ts +99 -0
  36. package/dist/text/CanvasEditorView.js +308 -0
  37. package/dist/text/InputSink.d.ts +157 -0
  38. package/dist/text/InputSink.js +841 -0
  39. package/dist/text/TextEditSession.d.ts +173 -0
  40. package/dist/text/TextEditSession.js +1328 -0
  41. package/dist/text/clipboard.d.ts +11 -0
  42. package/dist/text/clipboard.js +489 -0
  43. package/dist/text/commands.d.ts +48 -0
  44. package/dist/text/commands.js +329 -0
  45. package/dist/text/contextMirror.d.ts +51 -0
  46. package/dist/text/contextMirror.js +292 -0
  47. package/dist/text/flat.d.ts +35 -0
  48. package/dist/text/flat.js +556 -0
  49. package/dist/text/graphemes.d.ts +16 -0
  50. package/dist/text/graphemes.js +219 -0
  51. package/dist/text/index.d.ts +13 -0
  52. package/dist/text/index.js +13 -0
  53. package/dist/text/inputReducer.d.ts +24 -0
  54. package/dist/text/inputReducer.js +44 -0
  55. package/dist/text/keymap.d.ts +17 -0
  56. package/dist/text/keymap.js +148 -0
  57. package/dist/text/schema.d.ts +12 -0
  58. package/dist/text/schema.js +44 -0
  59. package/dist/text/sessionFocus.d.ts +20 -0
  60. package/dist/text/sessionFocus.js +51 -0
  61. package/dist/text/types.d.ts +114 -0
  62. package/dist/text/types.js +1 -0
  63. package/dist/tools/CreateTool.d.ts +78 -0
  64. package/dist/tools/CreateTool.js +258 -0
  65. package/dist/tools/SelectTool.d.ts +155 -0
  66. package/dist/tools/SelectTool.js +808 -0
  67. package/dist/tools/ToolManager.d.ts +63 -0
  68. package/dist/tools/ToolManager.js +146 -0
  69. package/dist/tools/connectorEndpoints.d.ts +17 -0
  70. package/dist/tools/connectorEndpoints.js +53 -0
  71. package/dist/tools/frameRotation.d.ts +8 -0
  72. package/dist/tools/frameRotation.js +66 -0
  73. package/dist/tools/gradientHandles.d.ts +67 -0
  74. package/dist/tools/gradientHandles.js +110 -0
  75. package/dist/tools/presetPaths.d.ts +5 -0
  76. package/dist/tools/presetPaths.js +49 -0
  77. package/dist/tools/snapResize.d.ts +7 -0
  78. package/dist/tools/snapResize.js +65 -0
  79. package/dist/tools/snapping.d.ts +30 -0
  80. package/dist/tools/snapping.js +280 -0
  81. package/dist/tools/transform.d.ts +52 -0
  82. package/dist/tools/transform.js +277 -0
  83. package/package.json +36 -3
@@ -0,0 +1,30 @@
1
+ import { type Point, type Rect, type SceneStore } from "@daasboard/render-core";
2
+ export declare const SNAP_THRESHOLD_PX = 6;
3
+ export declare const SNAP_NEIGHBOURHOOD_PX = 240;
4
+ export declare const SNAP_CANDIDATE_LIMIT = 128;
5
+ export type SnapAxis = "x" | "y";
6
+ export type SnapKind = "edge" | "center" | "spacing";
7
+ export interface SnapGuide {
8
+ axis: SnapAxis;
9
+ kind: SnapKind;
10
+ from: Point;
11
+ to: Point;
12
+ at: number;
13
+ }
14
+ export interface SnapCandidate {
15
+ id: string;
16
+ box: Rect;
17
+ }
18
+ export interface SnapOptions {
19
+ x?: readonly number[];
20
+ y?: readonly number[];
21
+ spacing?: boolean;
22
+ }
23
+ export interface SnapResult {
24
+ dx: number;
25
+ dy: number;
26
+ guides: SnapGuide[];
27
+ }
28
+ export declare function snapCandidates(store: SceneStore, box: Rect, zoom: number, exclude?: ReadonlySet<string>, options?: SnapOptions): SnapCandidate[];
29
+ export declare function snapToCandidates(box: Rect, candidates: readonly SnapCandidate[], zoom: number, options?: SnapOptions): SnapResult;
30
+ export declare function snapBox(store: SceneStore, box: Rect, zoom: number, exclude?: ReadonlySet<string>, options?: SnapOptions): SnapResult;
@@ -0,0 +1,280 @@
1
+ export const SNAP_THRESHOLD_PX = 6;
2
+ export const SNAP_NEIGHBOURHOOD_PX = 240;
3
+ export const SNAP_CANDIDATE_LIMIT = 128;
4
+ const POOL_LIMIT = SNAP_CANDIDATE_LIMIT / 4;
5
+ const EMPTY_IDS = new Set();
6
+ const EPS = 1e-9;
7
+ const distanceRank = (delta) => Math.round(Math.abs(delta) / EPS);
8
+ const priority = { edge: 0, center: 1, spacing: 2 };
9
+ const scale = (zoom) => Number.isFinite(zoom) && zoom > 0 ? zoom : 1;
10
+ const min = (box, axis) => box[axis];
11
+ const size = (box, axis) => axis === "x" ? box.w : box.h;
12
+ const max = (box, axis) => min(box, axis) + size(box, axis);
13
+ const cross = (axis) => axis === "x" ? "y" : "x";
14
+ const point = (axis, along, across) => axis === "x" ? { x: along, y: across } : { x: across, y: along };
15
+ const valid = (box) => Number.isFinite(box.x) && Number.isFinite(box.y) && Number.isFinite(box.w) && Number.isFinite(box.h) && box.w >= 0 && box.h >= 0;
16
+ function compareAlignment(a, b) {
17
+ if (a.distance !== b.distance)
18
+ return a.distance - b.distance;
19
+ if (a.priority !== b.priority)
20
+ return a.priority - b.priority;
21
+ const ak = a.key ??= `${a.id}:${a.target}:${a.source}`;
22
+ const bk = b.key ??= `${b.id}:${b.target}:${b.source}`;
23
+ return ak < bk ? -1 : ak > bk ? 1 : 0;
24
+ }
25
+ function alignmentPool(store, axis, sources, box, radius, threshold, accept) {
26
+ const pool = [], byId = new Map();
27
+ const left = box.x - radius, right = box.x + box.w + radius;
28
+ const top = box.y - radius, bottom = box.y + box.h + radius;
29
+ store.alignmentIndex.nearestMany(axis, sources, threshold + EPS, (feature, source) => {
30
+ const distance = distanceRank(feature.value - source);
31
+ if (pool.length === POOL_LIMIT && distance > pool[pool.length - 1].distance)
32
+ return false;
33
+ const b = feature.box;
34
+ if (b.x > right || b.x + b.w < left || b.y > bottom || b.y + b.h < top || !accept(feature.id))
35
+ return true;
36
+ const match = { id: feature.id, distance, priority: feature.kind === "edge" ? 0 : 1, target: feature.value, source, key: undefined };
37
+ const previous = byId.get(match.id);
38
+ if (previous) {
39
+ if (compareAlignment(match, previous) >= 0)
40
+ return true;
41
+ pool.splice(pool.indexOf(previous), 1);
42
+ }
43
+ else if (pool.length === POOL_LIMIT && compareAlignment(match, pool[pool.length - 1]) >= 0)
44
+ return true;
45
+ let lo = 0, hi = pool.length;
46
+ while (lo < hi) {
47
+ const mid = (lo + hi) >>> 1;
48
+ if (compareAlignment(pool[mid], match) < 0)
49
+ lo = mid + 1;
50
+ else
51
+ hi = mid;
52
+ }
53
+ pool.splice(lo, 0, match);
54
+ byId.set(match.id, match);
55
+ if (pool.length > POOL_LIMIT)
56
+ byId.delete(pool.pop().id);
57
+ return true;
58
+ });
59
+ return pool.map(match => match.id);
60
+ }
61
+ export function snapCandidates(store, box, zoom, exclude = EMPTY_IDS, options = {}) {
62
+ if (!valid(box))
63
+ return [];
64
+ const accepted = new Map();
65
+ const accept = (id) => {
66
+ const known = accepted.get(id);
67
+ if (known !== undefined)
68
+ return known;
69
+ if (exclude.has(id)) {
70
+ accepted.set(id, false);
71
+ return false;
72
+ }
73
+ const parent = store.scene.objects.get(id)?.parent ?? null;
74
+ if (!parent) {
75
+ accepted.set(id, true);
76
+ return true;
77
+ }
78
+ const path = [id];
79
+ let keep = true;
80
+ for (let cur = parent; cur;) {
81
+ if (exclude.has(cur)) {
82
+ keep = false;
83
+ break;
84
+ }
85
+ const cached = accepted.get(cur);
86
+ if (cached !== undefined) {
87
+ keep = cached;
88
+ break;
89
+ }
90
+ path.push(cur);
91
+ cur = store.scene.objects.get(cur)?.parent ?? null;
92
+ }
93
+ for (const cur of path)
94
+ accepted.set(cur, keep);
95
+ return keep;
96
+ };
97
+ const ids = new Set();
98
+ const radius = SNAP_NEIGHBOURHOOD_PX / scale(zoom), threshold = SNAP_THRESHOLD_PX / scale(zoom);
99
+ for (const axis of ["x", "y"]) {
100
+ const lo = min(box, axis), hi = max(box, axis);
101
+ const sources = options[axis] ?? [lo, (lo + hi) / 2, hi];
102
+ if (!sources.length)
103
+ continue;
104
+ for (const id of alignmentPool(store, axis, sources, box, radius, threshold, accept))
105
+ ids.add(id);
106
+ if (options.spacing === false)
107
+ continue;
108
+ for (const id of store.shapeIndex.nearbyInRow(box, axis, radius, threshold, POOL_LIMIT, accept))
109
+ ids.add(id);
110
+ }
111
+ return [...ids].sort().map(id => ({ id, box: store.shapeIndex.boundsOf(id) }));
112
+ }
113
+ const zeroDistance = (match) => match?.distance === 0;
114
+ const matchKey = (match) => match.key ??= match.kind === "spacing"
115
+ ? match.makeKey() : `${match.candidate.id}:${match.target}:${match.source}`;
116
+ function matchGuides(match, box, axis) {
117
+ if (!match)
118
+ return [];
119
+ if (match.kind === "spacing")
120
+ return match.guides();
121
+ const orth = cross(axis), target = match.target, b = match.candidate.box;
122
+ return [{ axis, kind: match.kind, at: target,
123
+ from: point(axis, target, Math.min(min(box, orth), min(b, orth))),
124
+ to: point(axis, target, Math.max(max(box, orth), max(b, orth))),
125
+ }];
126
+ }
127
+ function better(delta, kind, key, best) {
128
+ if (!best)
129
+ return true;
130
+ const distance = distanceRank(delta) - best.distance;
131
+ if (distance !== 0)
132
+ return distance < 0;
133
+ if (priority[kind] !== priority[best.kind])
134
+ return priority[kind] < priority[best.kind];
135
+ return key() < matchKey(best);
136
+ }
137
+ function neighbouringPairs(row, box, axis) {
138
+ if (row.length < 2)
139
+ return [];
140
+ const orth = cross(axis);
141
+ const low = (c) => Math.max(min(c.box, orth), min(box, orth));
142
+ const high = (c) => Math.min(max(c.box, orth), max(box, orth));
143
+ const coords = [...new Set(row.flatMap((c) => [low(c), high(c)]))].sort((a, b) => a - b);
144
+ const indices = new Map(coords.map((v, i) => [v, i]));
145
+ const n = coords.length - 1;
146
+ const ends = [...row].sort((a, b) => max(a.box, axis) - max(b.box, axis) || (a.id < b.id ? -1 : a.id > b.id ? 1 : 0));
147
+ const tree = new Int32Array(n * 4 + 4).fill(-1), lazy = new Int32Array(n * 4 + 4).fill(-1);
148
+ const push = (node) => {
149
+ if (lazy[node] < 0)
150
+ return;
151
+ tree[node * 2] = tree[node * 2 + 1] = lazy[node * 2] = lazy[node * 2 + 1] = lazy[node];
152
+ lazy[node] = -1;
153
+ };
154
+ const update = (node, l, r, from, to, rank) => {
155
+ if (from <= l && r <= to) {
156
+ tree[node] = lazy[node] = rank;
157
+ return;
158
+ }
159
+ push(node);
160
+ const mid = (l + r) >> 1;
161
+ if (from <= mid)
162
+ update(node * 2, l, mid, from, to, rank);
163
+ if (to > mid)
164
+ update(node * 2 + 1, mid + 1, r, from, to, rank);
165
+ tree[node] = Math.max(tree[node * 2], tree[node * 2 + 1]);
166
+ };
167
+ const query = (node, l, r, from, to) => {
168
+ if (from <= l && r <= to)
169
+ return tree[node];
170
+ push(node);
171
+ const mid = (l + r) >> 1;
172
+ return Math.max(from <= mid ? query(node * 2, l, mid, from, to) : -1, to > mid ? query(node * 2 + 1, mid + 1, r, from, to) : -1);
173
+ };
174
+ const out = [];
175
+ let end = 0;
176
+ for (const current of row) {
177
+ while (end < ends.length && max(ends[end].box, axis) < min(current.box, axis)) {
178
+ const previous = ends[end];
179
+ update(1, 0, n - 1, indices.get(low(previous)), indices.get(high(previous)) - 1, end++);
180
+ }
181
+ const rank = query(1, 0, n - 1, indices.get(low(current)), indices.get(high(current)) - 1);
182
+ if (rank >= 0)
183
+ out.push([ends[rank], current]);
184
+ }
185
+ return out;
186
+ }
187
+ function snapAxis(box, candidates, axis, zoom, options) {
188
+ const lo = min(box, axis), hi = max(box, axis), orth = cross(axis);
189
+ const supplied = options[axis];
190
+ const sources = supplied ?? [lo, (lo + hi) / 2, hi];
191
+ if (!sources.length)
192
+ return null;
193
+ const threshold = SNAP_THRESHOLD_PX / scale(zoom);
194
+ let best = null;
195
+ const offer = (source, target, kind, key, guides) => {
196
+ const delta = target - source;
197
+ if (!Number.isFinite(delta) || Math.abs(delta) > threshold + EPS || !better(delta, kind, key, best))
198
+ return;
199
+ best = { delta, distance: distanceRank(delta), kind, key: undefined, makeKey: key, guides };
200
+ };
201
+ const offerAlignment = (source, target, kind, candidate) => {
202
+ const delta = target - source;
203
+ if (!Number.isFinite(delta) || Math.abs(delta) > threshold + EPS)
204
+ return;
205
+ const distance = distanceRank(delta);
206
+ let key;
207
+ if (best) {
208
+ if (distance > best.distance || (distance === best.distance && priority[kind] > priority[best.kind]))
209
+ return;
210
+ if (distance === best.distance && priority[kind] === priority[best.kind]) {
211
+ key = `${candidate.id}:${target}:${source}`;
212
+ if (key >= matchKey(best))
213
+ return;
214
+ }
215
+ }
216
+ best = { delta, distance, kind, key, candidate, target, source };
217
+ };
218
+ for (const candidate of candidates) {
219
+ const b = candidate.box;
220
+ if (!valid(b))
221
+ continue;
222
+ const start = min(b, axis), end = max(b, axis);
223
+ for (const source of sources) {
224
+ offerAlignment(source, start, "edge", candidate);
225
+ offerAlignment(source, end, "edge", candidate);
226
+ offerAlignment(source, (start + end) / 2, "center", candidate);
227
+ }
228
+ }
229
+ if (options.spacing === false || zeroDistance(best))
230
+ return best;
231
+ const row = candidates.filter((c) => valid(c.box) && Math.max(min(c.box, orth), min(box, orth)) < Math.min(max(c.box, orth), max(box, orth)))
232
+ .sort((a, b) => min(a.box, axis) - min(b.box, axis) || max(a.box, axis) - max(b.box, axis) || (a.id < b.id ? -1 : a.id > b.id ? 1 : 0));
233
+ const has = (value) => supplied === undefined || sources.some((source) => Math.abs(source - value) < EPS);
234
+ const gapGuides = (at, a, b, gaps) => {
235
+ const across = (Math.max(min(box, orth), min(a.box, orth), min(b.box, orth)) + Math.min(max(box, orth), max(a.box, orth), max(b.box, orth))) / 2;
236
+ return gaps.map(([from, to]) => ({ axis, kind: "spacing", at, from: point(axis, from, across), to: point(axis, to, across) }));
237
+ };
238
+ for (const [a, b] of neighbouringPairs(row, box, axis)) {
239
+ const a0 = min(a.box, axis), a1 = max(a.box, axis), b0 = min(b.box, axis), b1 = max(b.box, axis);
240
+ const gap = b0 - a1;
241
+ if (gap <= 0 || min(a.box, orth) >= max(b.box, orth) || min(b.box, orth) >= max(a.box, orth))
242
+ continue;
243
+ if (lo >= b1 - threshold && has(lo)) {
244
+ const at = b1 + gap;
245
+ offer(lo, at, "spacing", () => `${a.id}:${b.id}:after`, () => gapGuides(at, a, b, [[a1, b0], [b1, at]]));
246
+ }
247
+ if (hi <= a0 + threshold && has(hi)) {
248
+ const at = a0 - gap;
249
+ offer(hi, at, "spacing", () => `${a.id}:${b.id}:before`, () => gapGuides(at, a, b, [[at, a0], [a1, b0]]));
250
+ }
251
+ if (a1 > lo || b0 < hi)
252
+ continue;
253
+ if (supplied === undefined) {
254
+ const at = (a1 + b0 - size(box, axis)) / 2;
255
+ offer(lo, at, "spacing", () => `${a.id}:${b.id}:between`, () => gapGuides(at, a, b, [[a1, at], [at + size(box, axis), b0]]));
256
+ }
257
+ else {
258
+ if (has(lo)) {
259
+ const at = a1 + b0 - hi;
260
+ if (at < hi)
261
+ offer(lo, at, "spacing", () => `${a.id}:${b.id}:between-min`, () => gapGuides(at, a, b, [[a1, at], [hi, b0]]));
262
+ }
263
+ if (has(hi)) {
264
+ const at = a1 + b0 - lo;
265
+ if (at > lo)
266
+ offer(hi, at, "spacing", () => `${a.id}:${b.id}:between-max`, () => gapGuides(at, a, b, [[a1, lo], [at, b0]]));
267
+ }
268
+ }
269
+ }
270
+ return best;
271
+ }
272
+ export function snapToCandidates(box, candidates, zoom, options = {}) {
273
+ if (!valid(box))
274
+ return { dx: 0, dy: 0, guides: [] };
275
+ const x = snapAxis(box, candidates, "x", zoom, options), y = snapAxis(box, candidates, "y", zoom, options);
276
+ return { dx: x?.delta ?? 0, dy: y?.delta ?? 0, guides: [...matchGuides(x, box, "x"), ...matchGuides(y, box, "y")] };
277
+ }
278
+ export function snapBox(store, box, zoom, exclude = EMPTY_IDS, options = {}) {
279
+ return snapToCandidates(box, snapCandidates(store, box, zoom, exclude, options), zoom, options);
280
+ }
@@ -0,0 +1,52 @@
1
+ import { type Point, type Rect, type Scene, type SceneObject, type SceneStore } from "@daasboard/render-core";
2
+ import type { PointerInfo } from "./SelectTool.js";
3
+ export type HandleKind = "nw" | "n" | "ne" | "e" | "se" | "s" | "sw" | "w" | "rotate";
4
+ export type ResizeHandle = Exclude<HandleKind, "rotate">;
5
+ export declare const RESIZE_HANDLES: readonly ResizeHandle[];
6
+ export declare const MIN_SIZE = 1;
7
+ export declare const HANDLE_SIZE_PX = 8;
8
+ export declare const ROTATE_HANDLE_GAP_PX = 20;
9
+ export declare const ROTATE_SNAP_DEG = 15;
10
+ export declare const HANDLE_MIN_SIDE_PX: number;
11
+ export interface HandleProfile {
12
+ sizePx: number;
13
+ reachPx: number;
14
+ minSidePx: number;
15
+ rotateGapPx: number;
16
+ shape: "square" | "circle";
17
+ }
18
+ export declare function pointerProfile(p: Pick<PointerInfo, "pointer" | "tolerance" | "zoom">): {
19
+ hitTolerances: readonly number[];
20
+ dragThreshold: number;
21
+ handles: HandleProfile;
22
+ };
23
+ export declare const isContainer: (o: SceneObject) => boolean;
24
+ export declare function pointerHit(store: SceneStore, p: PointerInfo, accepts?: (o: SceneObject) => boolean): SceneObject | undefined;
25
+ export type ToolCursor = "default" | "move" | "crosshair" | "nwse-resize" | "nesw-resize" | "ns-resize" | "ew-resize" | "grab" | "grabbing";
26
+ export declare const isTransformable: (o: SceneObject) => boolean;
27
+ export declare function isLocked(o: SceneObject, scene: Scene): boolean;
28
+ export declare function transformBounds(objects: Iterable<SceneObject>): Rect | null;
29
+ export interface HandleFrame {
30
+ box: Rect;
31
+ rot: number;
32
+ }
33
+ export declare function handleFrame(objects: Iterable<SceneObject>): HandleFrame | null;
34
+ export declare function handlesSuppressed(b: Rect, zoom: number, profile?: HandleProfile): boolean;
35
+ export declare function handlePositions(b: Rect, zoom: number, rot?: number, profile?: HandleProfile): Record<HandleKind, Point>;
36
+ export declare function handleAt(b: Rect, p: Point, zoom: number, tolerance: number, rot?: number, profile?: HandleProfile): HandleKind | null;
37
+ export declare function cursorFor(handle: HandleKind, rot?: number): ToolCursor;
38
+ export interface ResizeMods {
39
+ shift: boolean;
40
+ aspectEdges?: boolean;
41
+ alt: boolean;
42
+ }
43
+ export declare function resizeBounds(b: Rect, handle: ResizeHandle, pointer: Point, mods: ResizeMods): Rect;
44
+ export declare function mapBoxInto(box: Rect, from: Rect, to: Rect, rot?: number, round?: boolean | {
45
+ x: boolean;
46
+ y: boolean;
47
+ }): Rect;
48
+ export declare const whole: (v: number) => number;
49
+ export declare function normalizeAngle(a: number): number;
50
+ export declare function snapAngle(a: number, stepDeg?: number): number;
51
+ export declare function rotatePoint(p: Point, centre: Point, angle: number): Point;
52
+ export declare function nearestSide(o: SceneObject, p: Point): "t" | "r" | "b" | "l";
@@ -0,0 +1,277 @@
1
+ import { hitTest, rectUnion, rotatedBounds } from "@daasboard/render-core";
2
+ export const RESIZE_HANDLES = ["nw", "n", "ne", "e", "se", "s", "sw", "w"];
3
+ export const MIN_SIZE = 1;
4
+ export const HANDLE_SIZE_PX = 8;
5
+ export const ROTATE_HANDLE_GAP_PX = 20;
6
+ export const ROTATE_SNAP_DEG = 15;
7
+ export const HANDLE_MIN_SIDE_PX = 2 * (HANDLE_SIZE_PX / 2 + 4);
8
+ export function pointerProfile(p) {
9
+ const css = p.tolerance / 4;
10
+ const cssToDevice = css * p.zoom;
11
+ const touch = p.pointer === "touch";
12
+ const tolerance = p.pointer === "pen" ? 6 * css : p.tolerance;
13
+ const reachPx = touch ? 22 * cssToDevice : HANDLE_SIZE_PX / 2 + tolerance * p.zoom;
14
+ return {
15
+ hitTolerances: touch ? [p.tolerance, 16 * css] : [tolerance],
16
+ dragThreshold: touch ? 10 * css : p.pointer === "pen" ? tolerance : Math.max(p.tolerance, 0.5),
17
+ handles: {
18
+ sizePx: touch ? 12 * cssToDevice : HANDLE_SIZE_PX,
19
+ reachPx,
20
+ minSidePx: touch ? 44 * cssToDevice : p.pointer === "pen" ? 2 * reachPx : HANDLE_MIN_SIDE_PX,
21
+ rotateGapPx: touch ? 40 * cssToDevice : ROTATE_HANDLE_GAP_PX,
22
+ shape: touch ? "circle" : "square",
23
+ },
24
+ };
25
+ }
26
+ const CONTAINER_KINDS = new Set(["frame", "section"]);
27
+ export const isContainer = (o) => CONTAINER_KINDS.has(o.kind);
28
+ export function pointerHit(store, p, accepts) {
29
+ const tolerances = pointerProfile(p).hitTolerances;
30
+ for (let i = 0; i < tolerances.length; i++) {
31
+ const hits = hitTest(store, p.x, p.y, { tolerance: tolerances[i], first: !accepts });
32
+ const hit = accepts ? hits.find(accepts) : hits[0];
33
+ if (!hit)
34
+ continue;
35
+ const broad = tolerances[i + 1];
36
+ return broad !== undefined && isContainer(hit) ? frontOf(store, p, broad, hit, accepts) ?? hit : hit;
37
+ }
38
+ return undefined;
39
+ }
40
+ function frontOf(store, p, tolerance, container, accepts) {
41
+ const rank = store.rankOf(container.id);
42
+ for (const o of hitTest(store, p.x, p.y, { tolerance })) {
43
+ if (store.rankOf(o.id) <= rank)
44
+ return undefined;
45
+ if (!isContainer(o) && (!accepts || accepts(o)))
46
+ return o;
47
+ }
48
+ return undefined;
49
+ }
50
+ export const isTransformable = (o) => !o.hidden && o.kind !== "connector" && o.kind !== "group";
51
+ export function isLocked(o, scene) {
52
+ for (let cur = o; cur; cur = cur.parent ? scene.objects.get(cur.parent) : undefined)
53
+ if (cur.locked)
54
+ return true;
55
+ return false;
56
+ }
57
+ export function transformBounds(objects) {
58
+ let r = null;
59
+ for (const o of objects) {
60
+ if (!isTransformable(o))
61
+ continue;
62
+ const b = rotatedBounds({ x: o.x, y: o.y, w: o.w, h: o.h }, o.rot);
63
+ r = r ? rectUnion(r, b) : b;
64
+ }
65
+ return r;
66
+ }
67
+ export function handleFrame(objects) {
68
+ let only = null;
69
+ let n = 0;
70
+ for (const o of objects) {
71
+ if (!isTransformable(o))
72
+ continue;
73
+ n++;
74
+ only = o;
75
+ }
76
+ if (n === 1 && only.rot)
77
+ return { box: { x: only.x, y: only.y, w: only.w, h: only.h }, rot: only.rot };
78
+ const b = transformBounds(objects);
79
+ return b ? { box: b, rot: 0 } : null;
80
+ }
81
+ export function handlesSuppressed(b, zoom, profile) {
82
+ return Math.min(b.w, b.h) * zoom < (profile?.minSidePx ?? HANDLE_MIN_SIDE_PX);
83
+ }
84
+ export function handlePositions(b, zoom, rot = 0, profile) {
85
+ const cx = b.x + b.w / 2;
86
+ const cy = b.y + b.h / 2;
87
+ const right = b.x + b.w;
88
+ const bottom = b.y + b.h;
89
+ const local = {
90
+ nw: { x: b.x, y: b.y },
91
+ n: { x: cx, y: b.y },
92
+ ne: { x: right, y: b.y },
93
+ e: { x: right, y: cy },
94
+ se: { x: right, y: bottom },
95
+ s: { x: cx, y: bottom },
96
+ sw: { x: b.x, y: bottom },
97
+ w: { x: b.x, y: cy },
98
+ rotate: { x: cx, y: b.y - (profile?.rotateGapPx ?? ROTATE_HANDLE_GAP_PX) / zoom },
99
+ };
100
+ if (!rot)
101
+ return local;
102
+ const centre = { x: cx, y: cy };
103
+ const out = {};
104
+ for (const kind of Object.keys(local))
105
+ out[kind] = rotatePoint(local[kind], centre, rot);
106
+ return out;
107
+ }
108
+ export function handleAt(b, p, zoom, tolerance, rot = 0, profile) {
109
+ if (handlesSuppressed(b, zoom, profile))
110
+ return null;
111
+ const positions = handlePositions(b, zoom, 0, profile);
112
+ const lp = rot ? rotatePoint(p, { x: b.x + b.w / 2, y: b.y + b.h / 2 }, -rot) : p;
113
+ const edgeRoundoff = rot ? 8 * Number.EPSILON * Math.max(1, Math.abs(b.x), Math.abs(b.y), b.w, b.h, Math.abs(p.x), Math.abs(p.y)) : 0;
114
+ const inBox = lp.x >= b.x - edgeRoundoff && lp.x <= b.x + b.w + edgeRoundoff && lp.y >= b.y - edgeRoundoff && lp.y <= b.y + b.h + edgeRoundoff;
115
+ const reach = profile?.shape === "circle" && inBox ? (10 / 12) * profile.sizePx / zoom
116
+ : profile ? profile.reachPx / zoom : HANDLE_SIZE_PX / 2 / zoom + tolerance;
117
+ let best = null;
118
+ let bestD = Infinity;
119
+ for (const kind of Object.keys(positions)) {
120
+ const c = positions[kind];
121
+ const dx = lp.x - c.x;
122
+ const dy = lp.y - c.y;
123
+ const inside = kind === "rotate" || profile?.shape === "circle" ? dx * dx + dy * dy <= reach * reach : Math.abs(dx) <= reach && Math.abs(dy) <= reach;
124
+ if (!inside)
125
+ continue;
126
+ const d = dx * dx + dy * dy;
127
+ if (d < bestD) {
128
+ bestD = d;
129
+ best = kind;
130
+ }
131
+ }
132
+ return best;
133
+ }
134
+ const RESIZE_CURSORS = ["ew-resize", "nwse-resize", "ns-resize", "nesw-resize", "ew-resize", "nwse-resize", "ns-resize", "nesw-resize"];
135
+ const HANDLE_OCTANT = { e: 0, se: 1, s: 2, sw: 3, w: 4, nw: 5, n: 6, ne: 7 };
136
+ export function cursorFor(handle, rot = 0) {
137
+ if (handle === "rotate")
138
+ return "grab";
139
+ const steps = Math.round(rot / (Math.PI / 4));
140
+ return RESIZE_CURSORS[(((HANDLE_OCTANT[handle] + steps) % 8) + 8) % 8];
141
+ }
142
+ export function resizeBounds(b, handle, pointer, mods) {
143
+ const hx = handle.includes("e") ? 1 : handle.includes("w") ? -1 : 0;
144
+ const hy = handle.includes("s") ? 1 : handle.includes("n") ? -1 : 0;
145
+ const cx = b.x + b.w / 2;
146
+ const cy = b.y + b.h / 2;
147
+ if (mods.shift && mods.aspectEdges && (!hx || !hy)) {
148
+ const bw = Math.max(MIN_SIZE, b.w), bh = Math.max(MIN_SIZE, b.h);
149
+ const anchor = hx ? (mods.alt ? cx : hx > 0 ? b.x : b.x + b.w)
150
+ : (mods.alt ? cy : hy > 0 ? b.y : b.y + b.h);
151
+ const distance = hx ? hx * (pointer.x - anchor) : hy * (pointer.y - anchor);
152
+ const scale = Math.max(MIN_SIZE / bw, MIN_SIZE / bh, distance / ((hx ? bw : bh) * (mods.alt ? .5 : 1)));
153
+ const w = bw * scale, h = bh * scale;
154
+ return { x: !hx || mods.alt ? cx - w / 2 : hx > 0 ? anchor : anchor - w,
155
+ y: !hy || mods.alt ? cy - h / 2 : hy > 0 ? anchor : anchor - h, w, h };
156
+ }
157
+ if (mods.shift && hx && hy) {
158
+ const bw = Math.max(MIN_SIZE, b.w);
159
+ const bh = Math.max(MIN_SIZE, b.h);
160
+ const ax = mods.alt ? cx : hx > 0 ? b.x : b.x + b.w;
161
+ const ay = mods.alt ? cy : hy > 0 ? b.y : b.y + b.h;
162
+ const dx = hx * bw * (mods.alt ? 0.5 : 1);
163
+ const dy = hy * bh * (mods.alt ? 0.5 : 1);
164
+ const minScale = Math.max(MIN_SIZE / bw, MIN_SIZE / bh);
165
+ const s = Math.max(minScale, ((pointer.x - ax) * dx + (pointer.y - ay) * dy) / (dx * dx + dy * dy));
166
+ const w = bw * s;
167
+ const h = bh * s;
168
+ if (mods.alt)
169
+ return { x: cx - w / 2, y: cy - h / 2, w, h };
170
+ return { x: hx > 0 ? ax : ax - w, y: hy > 0 ? ay : ay - h, w, h };
171
+ }
172
+ let x0 = b.x;
173
+ let x1 = b.x + b.w;
174
+ let y0 = b.y;
175
+ let y1 = b.y + b.h;
176
+ if (mods.alt) {
177
+ if (hx) {
178
+ const half = Math.max(MIN_SIZE / 2, Math.abs(pointer.x - cx));
179
+ x0 = cx - half;
180
+ x1 = cx + half;
181
+ }
182
+ if (hy) {
183
+ const half = Math.max(MIN_SIZE / 2, Math.abs(pointer.y - cy));
184
+ y0 = cy - half;
185
+ y1 = cy + half;
186
+ }
187
+ }
188
+ else {
189
+ if (hx > 0)
190
+ x1 = Math.max(x0 + MIN_SIZE, pointer.x);
191
+ else if (hx < 0)
192
+ x0 = Math.min(x1 - MIN_SIZE, pointer.x);
193
+ if (hy > 0)
194
+ y1 = Math.max(y0 + MIN_SIZE, pointer.y);
195
+ else if (hy < 0)
196
+ y0 = Math.min(y1 - MIN_SIZE, pointer.y);
197
+ }
198
+ return { x: x0, y: y0, w: x1 - x0, h: y1 - y0 };
199
+ }
200
+ const finiteRect = (r) => Number.isFinite(r.x) && Number.isFinite(r.y) && Number.isFinite(r.w) && Number.isFinite(r.h);
201
+ export function mapBoxInto(box, from, to, rot = 0, round = true) {
202
+ if (!finiteRect(from) || !finiteRect(to))
203
+ return { ...box };
204
+ const sx = from.w > 0 ? to.w / from.w : 1;
205
+ const sy = from.h > 0 ? to.h / from.h : 1;
206
+ const cx = to.x + (box.x + box.w / 2 - from.x) * sx;
207
+ const cy = to.y + (box.y + box.h / 2 - from.y) * sy;
208
+ let w;
209
+ let h;
210
+ if (rot) {
211
+ const c = Math.abs(Math.cos(rot));
212
+ const s = Math.abs(Math.sin(rot));
213
+ const ew = (box.w * c + box.h * s) * sx;
214
+ const eh = (box.w * s + box.h * c) * sy;
215
+ const det = c * c - s * s;
216
+ w = (ew * c - eh * s) / det;
217
+ h = (eh * c - ew * s) / det;
218
+ if (Math.abs(det) < 0.2 || w < MIN_SIZE || h < MIN_SIZE) {
219
+ const k = (ew + eh) / (box.w * c + box.h * s + box.w * s + box.h * c);
220
+ w = box.w * k;
221
+ h = box.h * k;
222
+ }
223
+ }
224
+ else {
225
+ w = box.w * sx;
226
+ h = box.h * sy;
227
+ }
228
+ const axes = typeof round === "boolean" ? { x: round, y: round } : round;
229
+ const finishX = axes.x ? whole : (v) => v;
230
+ const finishY = axes.y ? whole : (v) => v;
231
+ const coupled = !!rot && (!axes.x || !axes.y);
232
+ w = Math.max(MIN_SIZE, coupled ? w : finishX(w));
233
+ h = Math.max(MIN_SIZE, coupled ? h : finishY(h));
234
+ return { x: finishX(cx - w / 2), y: finishY(cy - h / 2), w, h };
235
+ }
236
+ export const whole = (v) => Math.round(v) || 0;
237
+ export function normalizeAngle(a) {
238
+ const twoPi = 2 * Math.PI;
239
+ let r = a % twoPi;
240
+ if (r <= -Math.PI)
241
+ r += twoPi;
242
+ else if (r > Math.PI)
243
+ r -= twoPi;
244
+ return r;
245
+ }
246
+ export function snapAngle(a, stepDeg = ROTATE_SNAP_DEG) {
247
+ const step = (stepDeg * Math.PI) / 180;
248
+ return Math.round(a / step) * step || 0;
249
+ }
250
+ export function rotatePoint(p, centre, angle) {
251
+ const c = Math.cos(angle);
252
+ const s = Math.sin(angle);
253
+ const dx = p.x - centre.x;
254
+ const dy = p.y - centre.y;
255
+ return { x: centre.x + dx * c - dy * s, y: centre.y + dx * s + dy * c };
256
+ }
257
+ export function nearestSide(o, p) {
258
+ let lx = p.x;
259
+ let ly = p.y;
260
+ if (o.rot) {
261
+ const local = rotatePoint(p, { x: o.x + o.w / 2, y: o.y + o.h / 2 }, -o.rot);
262
+ lx = local.x;
263
+ ly = local.y;
264
+ }
265
+ const dl = Math.abs(lx - o.x);
266
+ const dr = Math.abs(o.x + o.w - lx);
267
+ const dt = Math.abs(ly - o.y);
268
+ const db = Math.abs(o.y + o.h - ly);
269
+ const min = Math.min(dl, dr, dt, db);
270
+ if (min === dl)
271
+ return "l";
272
+ if (min === dr)
273
+ return "r";
274
+ if (min === dt)
275
+ return "t";
276
+ return "b";
277
+ }
package/package.json CHANGED
@@ -1,6 +1,39 @@
1
1
  {
2
2
  "name": "@daasboard/editor",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "0.1.0",
4
+ "type": "module",
5
+ "main": "./dist/index.js",
6
+ "types": "./dist/index.d.ts",
7
+ "dependencies": {
8
+ "@daasboard/doc": "0.1.0",
9
+ "@daasboard/render-core": "0.1.0",
10
+ "prosemirror-commands": "^1.7.0",
11
+ "prosemirror-keymap": "^1.2.2",
12
+ "prosemirror-inputrules": "^1.5.0",
13
+ "prosemirror-model": "^1.25.0",
14
+ "prosemirror-state": "^1.4.3",
15
+ "prosemirror-transform": "^1.10.0"
16
+ },
17
+ "license": "UNLICENSED",
18
+ "description": "Headless editing and property controllers for daasboard.",
19
+ "exports": {
20
+ ".": {
21
+ "types": "./dist/index.d.ts",
22
+ "import": "./dist/index.js",
23
+ "default": "./dist/index.js"
24
+ }
25
+ },
26
+ "files": [
27
+ "dist",
28
+ "README.md",
29
+ "LICENSE"
30
+ ],
31
+ "sideEffects": false,
32
+ "publishConfig": {
33
+ "access": "public"
34
+ },
35
+ "engines": {
36
+ "node": ">=22.3.0"
37
+ },
38
+ "daasboardCommit": "ec08eb540f53c278ff9aa3d7c23a51407c429133"
6
39
  }