@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,63 @@
1
+ import type { SceneStore, Style } from "@daasboard/render-core";
2
+ import type { PropertyController } from "../props/PropertyController.js";
3
+ import type { SelectionModel } from "../selection/SelectionModel.js";
4
+ import { CreateTool, type CreateKind, type CreatePress, type CreateToolName, type CreateToolOptions, type CreateToolState, type CreationPreview } from "./CreateTool.js";
5
+ import { SelectTool, type PointerInfo, type SelectPress, type SelectToolOptions, type SelectToolState } from "./SelectTool.js";
6
+ import type { ToolCursor } from "./transform.js";
7
+ export type ToolName = "select" | "hand" | CreateToolName;
8
+ export declare const TOOL_NAMES: readonly ToolName[];
9
+ declare class HandTool {
10
+ pointerDown(_p: PointerInfo): void;
11
+ pointerMove(_p: PointerInfo): void;
12
+ pointerUp(_p: PointerInfo): void;
13
+ hover(_p: PointerInfo): void;
14
+ cancel(): void;
15
+ key(_key: string, _mods: {
16
+ shift: boolean;
17
+ ctrl: boolean;
18
+ }): boolean;
19
+ cursor(): ToolCursor;
20
+ state(): SelectToolState;
21
+ }
22
+ export interface ToolPress {
23
+ readonly tool: ToolName;
24
+ readonly select?: SelectPress;
25
+ readonly create?: CreatePress;
26
+ }
27
+ export interface ToolManagerOptions extends SelectToolOptions, CreateToolOptions {
28
+ onCreated?: (id: string, kind: CreateKind) => void;
29
+ }
30
+ export interface ToolManagerState extends Omit<SelectToolState, "mode"> {
31
+ tool: ToolName;
32
+ mode: SelectToolState["mode"] | CreateToolState["mode"];
33
+ creation: CreationPreview | null;
34
+ }
35
+ export declare class ToolManager {
36
+ readonly select: SelectTool;
37
+ private readonly hand;
38
+ private readonly creators;
39
+ private active_;
40
+ private listeners;
41
+ constructor(store: SceneStore, selection: SelectionModel, ctrl: PropertyController, opts?: ToolManagerOptions);
42
+ get active(): ToolName;
43
+ tool(): SelectTool | CreateTool | HandTool;
44
+ creator(name: CreateToolName): CreateTool;
45
+ setCreationStyle(kind: CreateKind, style: Style | undefined): void;
46
+ creationStyle(kind: CreateKind): Style | undefined;
47
+ setTool(name: ToolName): void;
48
+ onToolChange(l: (tool: ToolName) => void): () => void;
49
+ setTextEditing(id: string | null): void;
50
+ capturePress(p: PointerInfo): ToolPress;
51
+ pointerDown(p: PointerInfo, press?: ToolPress): boolean;
52
+ pointerMove(p: PointerInfo): void;
53
+ pointerUp(p: PointerInfo): void;
54
+ doubleClick(p: PointerInfo): void;
55
+ cancel(): void;
56
+ key(key: string, mods: {
57
+ shift: boolean;
58
+ ctrl: boolean;
59
+ }): boolean;
60
+ cursor(): ToolCursor;
61
+ state(): ToolManagerState;
62
+ }
63
+ export {};
@@ -0,0 +1,146 @@
1
+ import { CreateTool, CREATE_TOOL_NAMES } from "./CreateTool.js";
2
+ import { SelectTool } from "./SelectTool.js";
3
+ export const TOOL_NAMES = ["select", "hand", ...CREATE_TOOL_NAMES];
4
+ class HandTool {
5
+ pointerDown(_p) { }
6
+ pointerMove(_p) { }
7
+ pointerUp(_p) { }
8
+ hover(_p) { }
9
+ cancel() { }
10
+ key(_key, _mods) { return false; }
11
+ cursor() { return "grab"; }
12
+ state() {
13
+ return { mode: "idle", marquee: null, hover: null, handle: null, guides: [], sizeLabel: null };
14
+ }
15
+ }
16
+ export class ToolManager {
17
+ select;
18
+ hand = new HandTool();
19
+ creators = new Map();
20
+ active_ = "select";
21
+ listeners = new Set();
22
+ constructor(store, selection, ctrl, opts = {}) {
23
+ const selectOpts = {};
24
+ if (opts.reparentOnDrop !== undefined)
25
+ selectOpts.reparentOnDrop = opts.reparentOnDrop;
26
+ if (opts.onPresenceDrag)
27
+ selectOpts.onPresenceDrag = opts.onPresenceDrag;
28
+ this.select = new SelectTool(store, selection, ctrl, selectOpts);
29
+ const parentToContainer = opts.parentToContainer ?? opts.reparentOnDrop;
30
+ const createOpts = {};
31
+ if (parentToContainer !== undefined)
32
+ createOpts.parentToContainer = parentToContainer;
33
+ for (const name of CREATE_TOOL_NAMES) {
34
+ const tool = new CreateTool(name, store, selection, ctrl, createOpts);
35
+ tool.onCreated = (id, k) => opts.onCreated?.(id, k);
36
+ this.creators.set(name, tool);
37
+ }
38
+ }
39
+ get active() {
40
+ return this.active_;
41
+ }
42
+ tool() {
43
+ return this.active_ === "select" ? this.select : this.active_ === "hand" ? this.hand : this.creators.get(this.active_);
44
+ }
45
+ creator(name) {
46
+ return this.creators.get(name);
47
+ }
48
+ setCreationStyle(kind, style) {
49
+ for (const tool of this.creators.values())
50
+ if (tool.kind === kind)
51
+ tool.setCreationStyle(style);
52
+ }
53
+ creationStyle(kind) {
54
+ for (const tool of this.creators.values())
55
+ if (tool.kind === kind)
56
+ return tool.creationStyle();
57
+ return undefined;
58
+ }
59
+ setTool(name) {
60
+ if (name === this.active_)
61
+ return;
62
+ this.tool().cancel();
63
+ this.active_ = name;
64
+ for (const l of this.listeners)
65
+ l(name);
66
+ }
67
+ onToolChange(l) {
68
+ this.listeners.add(l);
69
+ return () => this.listeners.delete(l);
70
+ }
71
+ setTextEditing(id) {
72
+ this.select.setTextEditing(id);
73
+ }
74
+ capturePress(p) {
75
+ if (this.active_ === "select")
76
+ return { tool: "select", select: this.select.capturePress(p) };
77
+ const tool = this.tool();
78
+ return tool instanceof CreateTool ? { tool: this.active_, create: tool.capturePress(p) } : { tool: this.active_ };
79
+ }
80
+ pointerDown(p, press) {
81
+ if (press && press.tool !== this.active_)
82
+ return false;
83
+ const tool = this.tool();
84
+ if (this.active_ === "select" && press?.select) {
85
+ if (!this.select.pointerDown(p, press.select))
86
+ return false;
87
+ }
88
+ else if (tool instanceof CreateTool && press?.create) {
89
+ if (!tool.pointerDown(p, press.create))
90
+ return false;
91
+ }
92
+ else
93
+ tool.pointerDown(p);
94
+ this.select.setPointerProfile(p);
95
+ return true;
96
+ }
97
+ pointerMove(p) {
98
+ this.select.setPointerProfile(p);
99
+ this.tool().pointerMove(p);
100
+ }
101
+ pointerUp(p) {
102
+ this.select.setPointerProfile(p);
103
+ const tool = this.tool();
104
+ if (tool instanceof CreateTool) {
105
+ const created = tool.pointerUp(p);
106
+ if (created && !p.shift)
107
+ this.setTool("select");
108
+ }
109
+ else
110
+ tool.pointerUp(p);
111
+ }
112
+ doubleClick(p) {
113
+ this.select.setPointerProfile(p);
114
+ if (this.active_ === "select")
115
+ this.select.doubleClick(p);
116
+ }
117
+ cancel() {
118
+ this.tool().cancel();
119
+ }
120
+ key(key, mods) {
121
+ const tool = this.tool();
122
+ if (tool.key(key, mods))
123
+ return true;
124
+ if (tool === this.select)
125
+ return false;
126
+ if (key === "Escape") {
127
+ this.setTool("select");
128
+ return true;
129
+ }
130
+ return this.select.key(key, mods);
131
+ }
132
+ cursor() {
133
+ return this.tool().cursor();
134
+ }
135
+ state() {
136
+ const tool = this.tool();
137
+ const select = this.select.state();
138
+ if (tool === this.hand)
139
+ return { ...this.hand.state(), ...(select.handleProfile ? { handleProfile: select.handleProfile } : {}), tool: this.active_, creation: null };
140
+ if (tool instanceof CreateTool) {
141
+ const s = tool.state();
142
+ return { ...select, mode: s.mode, tool: this.active_, creation: s.preview, guides: s.guides, sizeLabel: null, gradient: null };
143
+ }
144
+ return { ...select, tool: this.active_, creation: null };
145
+ }
146
+ }
@@ -0,0 +1,17 @@
1
+ import { type Point, type Scene, type SceneObject } from "@daasboard/render-core";
2
+ import { type BoxFrame } from "./gradientHandles.js";
3
+ import { type HandleProfile } from "./transform.js";
4
+ export type ConnectorEnd = "start" | "end";
5
+ export interface ConnectorEndpointRef {
6
+ id: string;
7
+ end: ConnectorEnd;
8
+ }
9
+ export interface EndpointMarker extends ConnectorEndpointRef {
10
+ at: Point;
11
+ on: BoxFrame | null;
12
+ }
13
+ export declare const endpointPointKey: (end: ConnectorEnd) => "startAt" | "endAt";
14
+ export declare function endpointsDraggable(o: SceneObject | undefined, scene: Scene): o is SceneObject;
15
+ export declare function endpointPoints(o: SceneObject, scene: Scene): [Point, Point] | null;
16
+ export declare function endpointMarkers(o: SceneObject, scene: Scene): [EndpointMarker, EndpointMarker] | null;
17
+ export declare function endpointAt(markers: Iterable<EndpointMarker>, p: Point, zoom: number, tolerance: number, profile?: HandleProfile): EndpointMarker | null;
@@ -0,0 +1,53 @@
1
+ import { attachmentPoint, connectorPolyline } from "@daasboard/render-core";
2
+ import { frameOf, worldToBox } from "./gradientHandles.js";
3
+ import { HANDLE_SIZE_PX, isLocked } from "./transform.js";
4
+ export const endpointPointKey = (end) => (end === "start" ? "startAt" : "endAt");
5
+ export function endpointsDraggable(o, scene) {
6
+ return !!o && o.kind === "connector" && !!o.conn && !o.hidden && !isLocked(o, scene);
7
+ }
8
+ export function endpointPoints(o, scene) {
9
+ if (o.kind !== "connector" || !o.conn)
10
+ return null;
11
+ const pts = connectorPolyline(o.conn, scene, o);
12
+ const first = pts[0];
13
+ const last = pts[pts.length - 1];
14
+ return [{ x: first[0], y: first[1] }, { x: last[0], y: last[1] }];
15
+ }
16
+ export function endpointMarkers(o, scene) {
17
+ const points = endpointPoints(o, scene);
18
+ if (!points)
19
+ return null;
20
+ const on = (end) => {
21
+ const ep = o.conn[end];
22
+ const target = ep?.ref ? scene.objects.get(ep.ref) : undefined;
23
+ return ep && target && !target.hidden && target.kind !== "connector" && attachmentPoint(target, ep) ? frameOf(target) : null;
24
+ };
25
+ return [
26
+ { id: o.id, end: "start", at: points[0], on: on("start") },
27
+ { id: o.id, end: "end", at: points[1], on: on("end") },
28
+ ];
29
+ }
30
+ function inFrame(f, p) {
31
+ const [x, y] = worldToBox(f, p);
32
+ const roundoff = f.rot ? 8 * Number.EPSILON * Math.max(1, Math.abs(f.x), Math.abs(f.y), f.w, f.h, Math.abs(p.x), Math.abs(p.y)) : 0;
33
+ return x >= -roundoff && x <= f.w + roundoff && y >= -roundoff && y <= f.h + roundoff;
34
+ }
35
+ export function endpointAt(markers, p, zoom, tolerance, profile) {
36
+ let best = null;
37
+ let bestD = Infinity;
38
+ for (const m of markers) {
39
+ const circle = profile?.shape === "circle";
40
+ const reach = circle && m.on && inFrame(m.on, p) ? ((10 / 12) * profile.sizePx) / zoom
41
+ : profile ? profile.reachPx / zoom : HANDLE_SIZE_PX / 2 / zoom + tolerance;
42
+ const dx = p.x - m.at.x;
43
+ const dy = p.y - m.at.y;
44
+ const d = dx * dx + dy * dy;
45
+ if (!(circle ? d <= reach * reach : Math.abs(dx) <= reach && Math.abs(dy) <= reach))
46
+ continue;
47
+ if (d < bestD) {
48
+ bestD = d;
49
+ best = m;
50
+ }
51
+ }
52
+ return best;
53
+ }
@@ -0,0 +1,8 @@
1
+ import { type Point, type SceneObject } from "@daasboard/render-core";
2
+ export type RotationPatch = Partial<Record<"x" | "y" | "rot", number>> & {
3
+ "conn.startAt"?: [number, number];
4
+ "conn.endAt"?: [number, number];
5
+ "conn.points"?: [number, number][];
6
+ };
7
+ export declare function captureRotationIds(roots: readonly SceneObject[], lookup: (id: string) => SceneObject | undefined, order: () => readonly string[]): string[];
8
+ export declare function rotationPatches(objects: readonly SceneObject[], centre: Point, delta: number, lookup: (id: string) => SceneObject | undefined, translation?: Point): Map<string, RotationPatch>;
@@ -0,0 +1,66 @@
1
+ import { attachmentPoint } from "@daasboard/render-core";
2
+ import { normalizeAngle, rotatePoint, whole } from "./transform.js";
3
+ const CONTAINERS = new Set(["frame", "section", "group"]);
4
+ export function captureRotationIds(roots, lookup, order) {
5
+ const ids = new Set(roots.map((o) => o.id));
6
+ const containers = new Set(roots.filter((o) => CONTAINERS.has(o.kind)).map((o) => o.id));
7
+ if (containers.size) {
8
+ for (const id of order()) {
9
+ const o = lookup(id);
10
+ for (let p = o?.parent; p; p = lookup(p)?.parent ?? null) {
11
+ if (containers.has(p)) {
12
+ ids.add(id);
13
+ break;
14
+ }
15
+ }
16
+ }
17
+ }
18
+ return [...ids];
19
+ }
20
+ export function rotationPatches(objects, centre, delta, lookup, translation = { x: 0, y: 0 }) {
21
+ const out = new Map();
22
+ if (!Number.isFinite(delta) || !Number.isFinite(centre.x) || !Number.isFinite(centre.y))
23
+ return out;
24
+ const ids = new Set(objects.map((o) => o.id));
25
+ const hasTransformedAncestor = (o) => {
26
+ for (let p = o.parent; p; p = lookup(p)?.parent ?? null)
27
+ if (ids.has(p))
28
+ return true;
29
+ return false;
30
+ };
31
+ const point = (p) => {
32
+ const next = rotatePoint({ x: p[0], y: p[1] }, centre, delta);
33
+ return [next.x + translation.x, next.y + translation.y];
34
+ };
35
+ for (const o of objects) {
36
+ if (o.kind === "connector") {
37
+ if (!o.conn)
38
+ continue;
39
+ const patch = {};
40
+ for (const end of ["start", "end"]) {
41
+ const ep = o.conn[end];
42
+ const target = ep.ref ? lookup(ep.ref) : undefined;
43
+ if (!target || target.kind === "connector" || !attachmentPoint(target, ep))
44
+ patch[`conn.${end}At`] = point(o.conn[`${end}At`]);
45
+ }
46
+ if (o.conn.points)
47
+ patch["conn.points"] = o.conn.points.map(point);
48
+ if (Object.keys(patch).length)
49
+ out.set(o.id, patch);
50
+ continue;
51
+ }
52
+ const c = rotatePoint({ x: o.x + o.w / 2, y: o.y + o.h / 2 }, centre, delta);
53
+ const x = c.x - o.w / 2 + translation.x;
54
+ const y = c.y - o.h / 2 + translation.y;
55
+ const nextX = Math.abs(x - o.x) < 1e-9 ? o.x : whole(x);
56
+ const nextY = Math.abs(y - o.y) < 1e-9 ? o.y : whole(y);
57
+ const patch = { rot: normalizeAngle(o.rot + delta) };
58
+ const carried = hasTransformedAncestor(o);
59
+ if (carried || nextX !== o.x)
60
+ patch.x = nextX;
61
+ if (carried || nextY !== o.y)
62
+ patch.y = nextY;
63
+ out.set(o.id, patch);
64
+ }
65
+ return out;
66
+ }
@@ -0,0 +1,67 @@
1
+ import { type Color, type LinearGradient, type Point, type RadialGradient, type SceneObject } from "@daasboard/render-core";
2
+ export type Vec = [number, number];
3
+ export type GradientHandleKind = "p0" | "p1" | "center" | "radius" | "stop";
4
+ export declare const GRADIENT_HANDLE_PX = 12;
5
+ export declare const GRADIENT_STOP_PX = 8;
6
+ export declare const GRADIENT_SNAP_DEG = 15;
7
+ export declare const MIN_GRADIENT_RADIUS = 0.01;
8
+ export interface GradientHandleTarget {
9
+ id: string;
10
+ layer?: number;
11
+ }
12
+ export type GradientHandleEdit = {
13
+ op: "axis";
14
+ p0: Vec;
15
+ p1: Vec;
16
+ } | {
17
+ op: "radial";
18
+ cx?: number;
19
+ cy?: number;
20
+ r?: number;
21
+ };
22
+ export interface BoxFrame {
23
+ x: number;
24
+ y: number;
25
+ w: number;
26
+ h: number;
27
+ rot: number;
28
+ }
29
+ export type DrawableGradient = LinearGradient | RadialGradient;
30
+ export interface GradientHandleGeometry {
31
+ target: GradientHandleTarget;
32
+ type: "linear" | "radial";
33
+ frame: BoxFrame;
34
+ handles: Partial<Record<Exclude<GradientHandleKind, "stop">, Point>>;
35
+ line: {
36
+ from: Point;
37
+ to: Point;
38
+ } | null;
39
+ ellipse: {
40
+ x: number;
41
+ y: number;
42
+ w: number;
43
+ h: number;
44
+ } | null;
45
+ stops: {
46
+ at: Point;
47
+ color: Color;
48
+ }[];
49
+ ends: [Color, Color];
50
+ }
51
+ export declare function frameOf(o: Pick<SceneObject, "x" | "y" | "w" | "h" | "rot">): BoxFrame;
52
+ export declare function boxToWorld(f: BoxFrame, p: Vec): Point;
53
+ export declare function worldToBox(f: BoxFrame, p: Point): Vec;
54
+ export declare function gradientAt(o: SceneObject, layer?: number): DrawableGradient | null;
55
+ export declare function linearPoints(g: Pick<LinearGradient, "start" | "end">, w: number, h: number): {
56
+ p0: Vec;
57
+ p1: Vec;
58
+ };
59
+ export declare function radialParams(g: RadialGradient): {
60
+ cx: number;
61
+ cy: number;
62
+ r: number;
63
+ };
64
+ export declare function radiusPoint(g: RadialGradient, w: number, h: number, phi: number): Vec;
65
+ export declare function gradientHandleGeometry(o: SceneObject, target: GradientHandleTarget, phi?: number): GradientHandleGeometry | null;
66
+ export declare function gradientHandleAt(geo: GradientHandleGeometry, p: Point, zoom: number, tolerance: number): GradientHandleKind | null;
67
+ export declare function snapLinear(fixed: Vec, moving: Vec, stepDeg?: number): Vec;
@@ -0,0 +1,110 @@
1
+ import { parseColor } from "@daasboard/render-core";
2
+ import { rotatePoint } from "./transform.js";
3
+ export const GRADIENT_HANDLE_PX = 12;
4
+ export const GRADIENT_STOP_PX = 8;
5
+ export const GRADIENT_SNAP_DEG = 15;
6
+ export const MIN_GRADIENT_RADIUS = 0.01;
7
+ const finite = (v) => typeof v === "number" && Number.isFinite(v);
8
+ const isPoint = (v) => Array.isArray(v) && v.length === 2 && finite(v[0]) && finite(v[1]);
9
+ const RAD = Math.PI / 180;
10
+ export function frameOf(o) {
11
+ return { x: o.x, y: o.y, w: o.w, h: o.h, rot: o.rot || 0 };
12
+ }
13
+ export function boxToWorld(f, p) {
14
+ const world = { x: f.x + p[0], y: f.y + p[1] };
15
+ return f.rot ? rotatePoint(world, { x: f.x + f.w / 2, y: f.y + f.h / 2 }, f.rot) : world;
16
+ }
17
+ export function worldToBox(f, p) {
18
+ const local = f.rot ? rotatePoint(p, { x: f.x + f.w / 2, y: f.y + f.h / 2 }, -f.rot) : p;
19
+ return [local.x - f.x, local.y - f.y];
20
+ }
21
+ export function gradientAt(o, layer) {
22
+ const fill = o.style.fill;
23
+ let paint;
24
+ if (layer === undefined)
25
+ paint = Array.isArray(fill) ? (fill.length === 1 ? fill[0] : undefined) : fill;
26
+ else if (Number.isInteger(layer) && layer >= 0)
27
+ paint = (Array.isArray(fill) ? fill : fill === undefined || fill === null ? [] : [fill])[layer];
28
+ if (!paint || typeof paint !== "object" || !Array.isArray(paint.stops) || !paint.stops.some((s) => finite(s?.at) && typeof s.color === "string"))
29
+ return null;
30
+ if (paint.type === "linear")
31
+ return isPoint(paint.start) && isPoint(paint.end) ? paint : null;
32
+ if (paint.type === "radial") {
33
+ const { cx = 0.5, cy = 0.5, r = 0.5 } = paint;
34
+ return finite(cx) && finite(cy) && finite(r) && r > 0 ? paint : null;
35
+ }
36
+ return null;
37
+ }
38
+ export function linearPoints(g, w, h) {
39
+ const a = [g.end[0] - g.start[0], g.end[1] - g.start[1]];
40
+ const aa = a[0] * a[0] + a[1] * a[1];
41
+ const p0 = [w * g.start[0], h * g.start[1]];
42
+ if (!(aa > 0))
43
+ return { p0, p1: [p0[0], p0[1]] };
44
+ const gx = a[0] / (w * aa);
45
+ const gy = a[1] / (h * aa);
46
+ const gg = gx * gx + gy * gy;
47
+ return { p0, p1: [p0[0] + gx / gg, p0[1] + gy / gg] };
48
+ }
49
+ export function radialParams(g) {
50
+ return { cx: g.cx ?? 0.5, cy: g.cy ?? 0.5, r: g.r ?? 0.5 };
51
+ }
52
+ export function radiusPoint(g, w, h, phi) {
53
+ const { cx, cy, r } = radialParams(g);
54
+ return [w * (cx + r * Math.cos(phi)), h * (cy + r * Math.sin(phi))];
55
+ }
56
+ const sorted = (g) => g.stops.filter((s) => finite(s?.at) && typeof s.color === "string").sort((a, b) => a.at - b.at);
57
+ export function gradientHandleGeometry(o, target, phi = 0) {
58
+ const g = gradientAt(o, target.layer);
59
+ if (!g || !(o.w > 0) || !(o.h > 0) || !finite(o.x) || !finite(o.y) || !finite(o.w) || !finite(o.h))
60
+ return null;
61
+ const frame = frameOf(o);
62
+ const stops = sorted(g);
63
+ const ends = [parseColor(stops[0].color), parseColor(stops[stops.length - 1].color)];
64
+ if (g.type === "linear") {
65
+ const { p0, p1 } = linearPoints(g, o.w, o.h);
66
+ const from = boxToWorld(frame, p0);
67
+ const to = boxToWorld(frame, p1);
68
+ const dots = g.stops
69
+ .filter((s) => finite(s?.at) && typeof s.color === "string")
70
+ .map((s) => {
71
+ const t = Math.min(1, Math.max(0, s.at));
72
+ return { at: boxToWorld(frame, [p0[0] + t * (p1[0] - p0[0]), p0[1] + t * (p1[1] - p0[1])]), color: parseColor(s.color) };
73
+ });
74
+ return { target, type: "linear", frame, handles: { p0: from, p1: to }, line: { from, to }, ellipse: null, stops: dots, ends };
75
+ }
76
+ const { cx, cy, r } = radialParams(g);
77
+ const center = boxToWorld(frame, [cx * o.w, cy * o.h]);
78
+ const radius = boxToWorld(frame, radiusPoint(g, o.w, o.h, phi));
79
+ const ellipse = { x: o.x + (cx - r) * o.w, y: o.y + (cy - r) * o.h, w: 2 * r * o.w, h: 2 * r * o.h };
80
+ return { target, type: "radial", frame, handles: { center, radius }, line: null, ellipse, stops: [], ends };
81
+ }
82
+ export function gradientHandleAt(geo, p, zoom, tolerance) {
83
+ const reach = GRADIENT_HANDLE_PX / 2 / zoom + tolerance;
84
+ let best = null;
85
+ let bestD = Infinity;
86
+ const kinds = (geo.type === "linear" ? ["p1", "p0"] : ["radius", "center"]);
87
+ for (const kind of kinds) {
88
+ const c = geo.handles[kind];
89
+ if (!c)
90
+ continue;
91
+ const d = (p.x - c.x) ** 2 + (p.y - c.y) ** 2;
92
+ if (d <= reach * reach && d < bestD) {
93
+ bestD = d;
94
+ best = kind;
95
+ }
96
+ }
97
+ if (best)
98
+ return best;
99
+ const dot = GRADIENT_STOP_PX / 2 / zoom + tolerance;
100
+ return geo.stops.some((s) => (p.x - s.at.x) ** 2 + (p.y - s.at.y) ** 2 <= dot * dot) ? "stop" : null;
101
+ }
102
+ export function snapLinear(fixed, moving, stepDeg = GRADIENT_SNAP_DEG) {
103
+ const dx = moving[0] - fixed[0];
104
+ const dy = moving[1] - fixed[1];
105
+ const len = Math.hypot(dx, dy);
106
+ if (!(len > 0))
107
+ return moving;
108
+ const deg = Math.round(Math.atan2(dx, -dy) / RAD / stepDeg) * stepDeg;
109
+ return [fixed[0] + len * Math.sin(deg * RAD), fixed[1] - len * Math.cos(deg * RAD)];
110
+ }
@@ -0,0 +1,5 @@
1
+ import type { PathCmd } from "@daasboard/render-core";
2
+ export declare const PRESET_NAMES: readonly ["star", "diamond", "arrow-left", "arrow-right", "arrow-double", "speech-bubble", "database"];
3
+ export type PresetName = typeof PRESET_NAMES[number];
4
+ export declare const PRESET_PATHS: Readonly<Record<PresetName, readonly PathCmd[]>>;
5
+ export declare function isPresetName(name: string): name is PresetName;
@@ -0,0 +1,49 @@
1
+ export const PRESET_NAMES = ["star", "diamond", "arrow-left", "arrow-right", "arrow-double", "speech-bubble", "database"];
2
+ function polygon(points) {
3
+ return [...points.map(([x, y], i) => ({ t: i === 0 ? "M" : "L", x, y })), { t: "Z" }];
4
+ }
5
+ function star() {
6
+ const inner = Math.sin(Math.PI / 10) / Math.sin(3 * Math.PI / 10);
7
+ const points = Array.from({ length: 10 }, (_, i) => {
8
+ const angle = -Math.PI / 2 + i * Math.PI / 5;
9
+ const radius = i % 2 === 0 ? 1 : inner;
10
+ return [Math.cos(angle) * radius, Math.sin(angle) * radius];
11
+ });
12
+ const xs = points.map(([x]) => x), ys = points.map(([, y]) => y);
13
+ const left = Math.min(...xs), top = Math.min(...ys);
14
+ const width = Math.max(...xs) - left, height = Math.max(...ys) - top;
15
+ return polygon(points.map(([x, y]) => [(x - left) / width, (y - top) / height]));
16
+ }
17
+ const rightArrow = [[0, 0.3], [0.6, 0.3], [0.6, 0], [1, 0.5], [0.6, 1], [0.6, 0.7], [0, 0.7]];
18
+ const outlines = {
19
+ star: star(),
20
+ diamond: polygon([[0.5, 0], [1, 0.5], [0.5, 1], [0, 0.5]]),
21
+ "arrow-left": polygon(rightArrow.map(([x, y]) => [1 - x, y])),
22
+ "arrow-right": polygon(rightArrow),
23
+ "arrow-double": polygon([[0, 0.5], [0.3, 0], [0.3, 0.3], [0.7, 0.3], [0.7, 0], [1, 0.5], [0.7, 1], [0.7, 0.7], [0.3, 0.7], [0.3, 1]]),
24
+ "speech-bubble": [
25
+ { t: "M", x: 0.1, y: 0 }, { t: "L", x: 0.9, y: 0 },
26
+ { t: "Q", cx: 1, cy: 0, x: 1, y: 0.1 }, { t: "L", x: 1, y: 0.65 },
27
+ { t: "Q", cx: 1, cy: 0.75, x: 0.9, y: 0.75 }, { t: "L", x: 0.45, y: 0.75 },
28
+ { t: "L", x: 0.2, y: 1 }, { t: "L", x: 0.2, y: 0.75 }, { t: "L", x: 0.1, y: 0.75 },
29
+ { t: "Q", cx: 0, cy: 0.75, x: 0, y: 0.65 }, { t: "L", x: 0, y: 0.1 },
30
+ { t: "Q", cx: 0, cy: 0, x: 0.1, y: 0 }, { t: "Z" },
31
+ ],
32
+ database: (() => {
33
+ const k = 4 * (Math.SQRT2 - 1) / 3;
34
+ const rx = 0.5, ry = 0.16;
35
+ return [
36
+ { t: "M", x: 0, y: ry },
37
+ { t: "C", c1x: 0, c1y: ry * (1 - k), c2x: rx * (1 - k), c2y: 0, x: rx, y: 0 },
38
+ { t: "C", c1x: rx * (1 + k), c1y: 0, c2x: 1, c2y: ry * (1 - k), x: 1, y: ry },
39
+ { t: "L", x: 1, y: 1 - ry },
40
+ { t: "C", c1x: 1, c1y: 1 - ry * (1 - k), c2x: rx * (1 + k), c2y: 1, x: rx, y: 1 },
41
+ { t: "C", c1x: rx * (1 - k), c1y: 1, c2x: 0, c2y: 1 - ry * (1 - k), x: 0, y: 1 - ry },
42
+ { t: "Z" },
43
+ ];
44
+ })(),
45
+ };
46
+ export const PRESET_PATHS = Object.freeze(Object.fromEntries(PRESET_NAMES.map(name => [name, Object.freeze(outlines[name].map(cmd => Object.freeze(cmd)))])));
47
+ export function isPresetName(name) {
48
+ return Object.prototype.hasOwnProperty.call(PRESET_PATHS, name);
49
+ }
@@ -0,0 +1,7 @@
1
+ import { type Point, type Rect, type SceneStore } from "@daasboard/render-core";
2
+ import { type SnapGuide } from "./snapping.js";
3
+ import { type HandleFrame, type ResizeHandle, type ResizeMods } from "./transform.js";
4
+ export declare function snapResize(store: SceneStore, frame: HandleFrame, handle: ResizeHandle, pointer: Point, mods: ResizeMods, zoom: number, exclude: ReadonlySet<string>): {
5
+ box: Rect;
6
+ guides: SnapGuide[];
7
+ };
@@ -0,0 +1,65 @@
1
+ import { rotatedBounds } from "@daasboard/render-core";
2
+ import { snapBox, SNAP_THRESHOLD_PX } from "./snapping.js";
3
+ import { handlePositions, MIN_SIZE, resizeBounds, rotatePoint } from "./transform.js";
4
+ const EPS = 1e-7;
5
+ const priority = { edge: 0, center: 1, spacing: 2 };
6
+ export function snapResize(store, frame, handle, pointer, mods, zoom, exclude) {
7
+ const base = resizeBounds(frame.box, handle, pointer, mods);
8
+ const centre = { x: frame.box.x + frame.box.w / 2, y: frame.box.y + frame.box.h / 2 };
9
+ const pagePoint = (p) => frame.rot ? rotatePoint(p, centre, frame.rot) : p;
10
+ const localPoint = (p) => frame.rot ? rotatePoint(p, centre, -frame.rot) : p;
11
+ const active = (box) => pagePoint(handlePositions(box, zoom)[handle]);
12
+ const from = active(base);
13
+ const pageCentre = pagePoint({ x: base.x + base.w / 2, y: base.y + base.h / 2 });
14
+ const extent = rotatedBounds({ x: pageCentre.x - base.w / 2, y: pageCentre.y - base.h / 2, w: base.w, h: base.h }, frame.rot);
15
+ const hx = handle.includes("e") ? 1 : handle.includes("w") ? -1 : 0;
16
+ const hy = handle.includes("s") ? 1 : handle.includes("n") ? -1 : 0;
17
+ const constrained = !hx || !hy || mods.shift;
18
+ let direction = null;
19
+ if (constrained) {
20
+ const local = {
21
+ x: hx * (hy ? Math.max(MIN_SIZE, frame.box.w) : 1),
22
+ y: hy * (hx ? Math.max(MIN_SIZE, frame.box.h) : 1),
23
+ };
24
+ const rotated = rotatePoint(local, { x: 0, y: 0 }, frame.rot);
25
+ const length = Math.hypot(rotated.x, rotated.y);
26
+ direction = { x: rotated.x / length, y: rotated.y / length };
27
+ }
28
+ const snapped = snapBox(store, extent, zoom, exclude, {
29
+ x: direction && Math.abs(direction.x) < EPS ? [] : [from.x],
30
+ y: direction && Math.abs(direction.y) < EPS ? [] : [from.y],
31
+ spacing: !frame.rot && !mods.alt,
32
+ });
33
+ if (!snapped.guides.length)
34
+ return { box: base, guides: [] };
35
+ const resizeAt = (p) => resizeBounds(frame.box, handle, localPoint(p), mods);
36
+ const satisfied = (box, guides) => {
37
+ const p = active(box);
38
+ return guides.filter(g => Math.abs(p[g.axis] - g.at) < EPS);
39
+ };
40
+ if (!direction) {
41
+ const box = resizeAt({ x: from.x + snapped.dx, y: from.y + snapped.dy });
42
+ const guides = satisfied(box, snapped.guides);
43
+ return guides.length ? { box, guides } : { box: base, guides: [] };
44
+ }
45
+ const threshold = SNAP_THRESHOLD_PX / (Number.isFinite(zoom) && zoom > 0 ? zoom : 1);
46
+ const choices = [];
47
+ for (const axis of ["x", "y"]) {
48
+ const guide = snapped.guides.find(g => g.axis === axis);
49
+ if (!guide || Math.abs(direction[axis]) < EPS)
50
+ continue;
51
+ const travel = (guide.at - from[axis]) / direction[axis];
52
+ if (Math.abs(travel) > threshold + EPS)
53
+ continue;
54
+ const box = resizeAt({ x: from.x + travel * direction.x, y: from.y + travel * direction.y });
55
+ if (!satisfied(box, [guide]).length)
56
+ continue;
57
+ choices.push({ travel, kind: guide.kind, box, axis });
58
+ }
59
+ choices.sort((a, b) => {
60
+ const d = Math.abs(a.travel) - Math.abs(b.travel);
61
+ return Math.abs(d) > EPS ? d : priority[a.kind] - priority[b.kind] || (a.axis === "x" ? -1 : 1);
62
+ });
63
+ const best = choices[0];
64
+ return best ? { box: best.box, guides: satisfied(best.box, snapped.guides) } : { box: base, guides: [] };
65
+ }