@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.
- package/LICENSE +5 -0
- package/README.md +6 -2
- package/dist/camera/CameraInput.d.ts +106 -0
- package/dist/camera/CameraInput.js +398 -0
- package/dist/clipboard/ObjectClipboard.d.ts +40 -0
- package/dist/clipboard/ObjectClipboard.js +243 -0
- package/dist/clipboard/PasteCommitError.d.ts +4 -0
- package/dist/clipboard/PasteCommitError.js +7 -0
- package/dist/clipboard/carrier.d.ts +5 -0
- package/dist/clipboard/carrier.js +59 -0
- package/dist/clipboard/objects.d.ts +21 -0
- package/dist/clipboard/objects.js +362 -0
- package/dist/clipboard/placement.d.ts +11 -0
- package/dist/clipboard/placement.js +21 -0
- package/dist/clipboard/systemImages.d.ts +1 -0
- package/dist/clipboard/systemImages.js +24 -0
- package/dist/index.d.ts +23 -0
- package/dist/index.js +22 -0
- package/dist/input/TouchInput.d.ts +159 -0
- package/dist/input/TouchInput.js +656 -0
- package/dist/input/visiblePart.d.ts +8 -0
- package/dist/input/visiblePart.js +15 -0
- package/dist/interaction/InteractionLayer.d.ts +83 -0
- package/dist/interaction/InteractionLayer.js +520 -0
- package/dist/props/PropertyController.d.ts +130 -0
- package/dist/props/PropertyController.js +1448 -0
- package/dist/props/PropertyRegistry.d.ts +38 -0
- package/dist/props/PropertyRegistry.js +21 -0
- package/dist/props/builtinProperties.d.ts +23 -0
- package/dist/props/builtinProperties.js +350 -0
- package/dist/props/defaults.d.ts +6 -0
- package/dist/props/defaults.js +29 -0
- package/dist/selection/SelectionModel.d.ts +25 -0
- package/dist/selection/SelectionModel.js +79 -0
- package/dist/text/CanvasEditorView.d.ts +99 -0
- package/dist/text/CanvasEditorView.js +308 -0
- package/dist/text/InputSink.d.ts +157 -0
- package/dist/text/InputSink.js +841 -0
- package/dist/text/TextEditSession.d.ts +173 -0
- package/dist/text/TextEditSession.js +1328 -0
- package/dist/text/clipboard.d.ts +11 -0
- package/dist/text/clipboard.js +489 -0
- package/dist/text/commands.d.ts +48 -0
- package/dist/text/commands.js +329 -0
- package/dist/text/contextMirror.d.ts +51 -0
- package/dist/text/contextMirror.js +292 -0
- package/dist/text/flat.d.ts +35 -0
- package/dist/text/flat.js +556 -0
- package/dist/text/graphemes.d.ts +16 -0
- package/dist/text/graphemes.js +219 -0
- package/dist/text/index.d.ts +13 -0
- package/dist/text/index.js +13 -0
- package/dist/text/inputReducer.d.ts +24 -0
- package/dist/text/inputReducer.js +44 -0
- package/dist/text/keymap.d.ts +17 -0
- package/dist/text/keymap.js +148 -0
- package/dist/text/schema.d.ts +12 -0
- package/dist/text/schema.js +44 -0
- package/dist/text/sessionFocus.d.ts +20 -0
- package/dist/text/sessionFocus.js +51 -0
- package/dist/text/types.d.ts +114 -0
- package/dist/text/types.js +1 -0
- package/dist/tools/CreateTool.d.ts +78 -0
- package/dist/tools/CreateTool.js +258 -0
- package/dist/tools/SelectTool.d.ts +155 -0
- package/dist/tools/SelectTool.js +808 -0
- package/dist/tools/ToolManager.d.ts +63 -0
- package/dist/tools/ToolManager.js +146 -0
- package/dist/tools/connectorEndpoints.d.ts +17 -0
- package/dist/tools/connectorEndpoints.js +53 -0
- package/dist/tools/frameRotation.d.ts +8 -0
- package/dist/tools/frameRotation.js +66 -0
- package/dist/tools/gradientHandles.d.ts +67 -0
- package/dist/tools/gradientHandles.js +110 -0
- package/dist/tools/presetPaths.d.ts +5 -0
- package/dist/tools/presetPaths.js +49 -0
- package/dist/tools/snapResize.d.ts +7 -0
- package/dist/tools/snapResize.js +65 -0
- package/dist/tools/snapping.d.ts +30 -0
- package/dist/tools/snapping.js +280 -0
- package/dist/tools/transform.d.ts +52 -0
- package/dist/tools/transform.js +277 -0
- package/package.json +36 -3
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { type DrawContext, type ImagePaintSource, type InPlaceSet, type Point, type Rect, type Renderer, type SceneStore, type TextLayoutCache } from "@daasboard/render-core";
|
|
2
|
+
import type { RemotePresence } from "@daasboard/doc";
|
|
3
|
+
import type { PropertyController } from "../props/PropertyController.js";
|
|
4
|
+
import type { SelectionModel } from "../selection/SelectionModel.js";
|
|
5
|
+
import type { TextSessionView } from "../text/types.js";
|
|
6
|
+
import type { CreationPreview } from "../tools/CreateTool.js";
|
|
7
|
+
import type { GradientHandleState } from "../tools/SelectTool.js";
|
|
8
|
+
import type { SnapGuide } from "../tools/snapping.js";
|
|
9
|
+
import { type HandleKind, type HandleProfile } from "../tools/transform.js";
|
|
10
|
+
export declare const SELECTION_KNOB_PX = 12;
|
|
11
|
+
export declare const SELECTION_KNOB_GAP_PX = 4;
|
|
12
|
+
export declare const SELECTION_REACH_PX = 22;
|
|
13
|
+
export declare const SELECTION_INNER_REACH_PX = 10;
|
|
14
|
+
export interface SelectionHandleHit {
|
|
15
|
+
end: "anchor" | "head" | "caret";
|
|
16
|
+
at: Point;
|
|
17
|
+
}
|
|
18
|
+
export interface InteractionDrawOptions {
|
|
19
|
+
zoom: number;
|
|
20
|
+
viewport?: Rect;
|
|
21
|
+
handleSizePx?: number;
|
|
22
|
+
lineWidthPx?: number;
|
|
23
|
+
now?: number;
|
|
24
|
+
previewsInPlace?: boolean;
|
|
25
|
+
textHandles?: number;
|
|
26
|
+
}
|
|
27
|
+
interface PresenceSource {
|
|
28
|
+
peers(): readonly RemotePresence[];
|
|
29
|
+
caretObjectId?(connectionId: string): string | null;
|
|
30
|
+
}
|
|
31
|
+
export interface ToolChromeState {
|
|
32
|
+
marquee: Rect | null;
|
|
33
|
+
handleProfile?: HandleProfile;
|
|
34
|
+
handle?: HandleKind | null;
|
|
35
|
+
creation?: CreationPreview | null;
|
|
36
|
+
guides?: readonly SnapGuide[];
|
|
37
|
+
sizeLabel?: {
|
|
38
|
+
width: number;
|
|
39
|
+
height: number;
|
|
40
|
+
at: Point;
|
|
41
|
+
} | null;
|
|
42
|
+
gradient?: GradientHandleState | null;
|
|
43
|
+
}
|
|
44
|
+
export declare class InteractionLayer {
|
|
45
|
+
private readonly store;
|
|
46
|
+
private readonly selection;
|
|
47
|
+
private readonly ctrl;
|
|
48
|
+
private readonly texts;
|
|
49
|
+
private readonly scene;
|
|
50
|
+
private session;
|
|
51
|
+
private images;
|
|
52
|
+
private readonly sizeTextId;
|
|
53
|
+
private sizeText;
|
|
54
|
+
private sizeTextVersion;
|
|
55
|
+
private presence;
|
|
56
|
+
private presenceRenderer;
|
|
57
|
+
private readonly presenceLabels;
|
|
58
|
+
private readonly presenceText;
|
|
59
|
+
private handlesShown;
|
|
60
|
+
constructor(store: SceneStore, selection: SelectionModel, ctrl: PropertyController, texts: TextLayoutCache);
|
|
61
|
+
setImages(images: ImagePaintSource | undefined): void;
|
|
62
|
+
setTextSession(s: TextSessionView | null): void;
|
|
63
|
+
textSession(): TextSessionView | null;
|
|
64
|
+
private selectionHandles;
|
|
65
|
+
selectionHandleAt(p: Point, css: number): SelectionHandleHit | null;
|
|
66
|
+
setPresence(source: PresenceSource | null, renderer: Renderer): void;
|
|
67
|
+
previewIds(): Set<string>;
|
|
68
|
+
private localPreviewIds;
|
|
69
|
+
inPlace(): InPlaceSet | null;
|
|
70
|
+
private remoteGhosts;
|
|
71
|
+
private presenceColor;
|
|
72
|
+
private drawPresenceLabel;
|
|
73
|
+
private drawPresence;
|
|
74
|
+
private addDescendants;
|
|
75
|
+
private isUnder;
|
|
76
|
+
textEditIds(): Set<string>;
|
|
77
|
+
private effectiveScene;
|
|
78
|
+
private effectiveSelected;
|
|
79
|
+
selectionBounds(exclude?: string): Rect | null;
|
|
80
|
+
private drawSizeLabel;
|
|
81
|
+
draw(ctx: DrawContext, tool: ToolChromeState, opts: InteractionDrawOptions): void;
|
|
82
|
+
}
|
|
83
|
+
export {};
|
|
@@ -0,0 +1,520 @@
|
|
|
1
|
+
import { caseContent, caseOffset, paintObject, rectIntersects, renderBounds, textOrigin } from "@daasboard/render-core";
|
|
2
|
+
import { EditorState } from "prosemirror-state";
|
|
3
|
+
import { CanvasEditorView, selectionRects } from "../text/CanvasEditorView.js";
|
|
4
|
+
import { contentToPm, offsetToPmPos } from "../text/flat.js";
|
|
5
|
+
import { endpointPoints, endpointsDraggable } from "../tools/connectorEndpoints.js";
|
|
6
|
+
import { GRADIENT_HANDLE_PX, GRADIENT_STOP_PX } from "../tools/gradientHandles.js";
|
|
7
|
+
import { handleFrame, handlePositions, handlesSuppressed, isLocked, isTransformable, pointerProfile, RESIZE_HANDLES, rotatePoint, transformBounds } from "../tools/transform.js";
|
|
8
|
+
export const SELECTION_KNOB_PX = 12;
|
|
9
|
+
export const SELECTION_KNOB_GAP_PX = 4;
|
|
10
|
+
export const SELECTION_REACH_PX = 22;
|
|
11
|
+
export const SELECTION_INNER_REACH_PX = 10;
|
|
12
|
+
const ACCENT = [0.16, 0.45, 0.95, 1];
|
|
13
|
+
const ACCENT_FILL = [0.16, 0.45, 0.95, 0.12];
|
|
14
|
+
const WHITE = [1, 1, 1, 1];
|
|
15
|
+
const CARET_PX = 2;
|
|
16
|
+
const PREEDIT_LINE_PX = 1;
|
|
17
|
+
const GUIDE = [0.84, 0.18, 0.52, 1];
|
|
18
|
+
const LABEL_GAP_PX = 8;
|
|
19
|
+
const LABEL_PADDING_PX = 5;
|
|
20
|
+
let nextLayerId = 0;
|
|
21
|
+
export class InteractionLayer {
|
|
22
|
+
store;
|
|
23
|
+
selection;
|
|
24
|
+
ctrl;
|
|
25
|
+
texts;
|
|
26
|
+
scene;
|
|
27
|
+
session = null;
|
|
28
|
+
images;
|
|
29
|
+
sizeTextId = `\0interaction:size:${nextLayerId++}`;
|
|
30
|
+
sizeText = "";
|
|
31
|
+
sizeTextVersion = 0;
|
|
32
|
+
presence = null;
|
|
33
|
+
presenceRenderer = null;
|
|
34
|
+
presenceLabels = new Map();
|
|
35
|
+
presenceText = new Map();
|
|
36
|
+
handlesShown = false;
|
|
37
|
+
constructor(store, selection, ctrl, texts) {
|
|
38
|
+
this.store = store;
|
|
39
|
+
this.selection = selection;
|
|
40
|
+
this.ctrl = ctrl;
|
|
41
|
+
this.texts = texts;
|
|
42
|
+
this.scene = store.scene;
|
|
43
|
+
}
|
|
44
|
+
setImages(images) { this.images = images; }
|
|
45
|
+
setTextSession(s) {
|
|
46
|
+
if (this.session !== s) {
|
|
47
|
+
this.session?.drawnAs?.(null);
|
|
48
|
+
this.handlesShown = false;
|
|
49
|
+
}
|
|
50
|
+
this.session = s;
|
|
51
|
+
s?.drawnAs?.((o) => this.ctrl.effective(o));
|
|
52
|
+
}
|
|
53
|
+
textSession() {
|
|
54
|
+
return this.session;
|
|
55
|
+
}
|
|
56
|
+
selectionHandles(css) {
|
|
57
|
+
const ends = this.session?.selectionEnds?.() ?? [];
|
|
58
|
+
const d = (SELECTION_KNOB_GAP_PX + SELECTION_KNOB_PX / 2) * css;
|
|
59
|
+
return ends.map(({ end, rect: r }, i) => ({ end, x: r.x, top: r.y, bottom: r.y + r.h, knob: { x: r.x, y: i === 0 && ends.length > 1 ? r.y - d : r.y + r.h + d } }));
|
|
60
|
+
}
|
|
61
|
+
selectionHandleAt(p, css) {
|
|
62
|
+
const s = this.session;
|
|
63
|
+
if (!s || !this.handlesShown)
|
|
64
|
+
return null;
|
|
65
|
+
const o = this.ctrl.effective(s.liveObject());
|
|
66
|
+
const c = { x: o.x + o.w / 2, y: o.y + o.h / 2 };
|
|
67
|
+
const q = o.rot ? rotatePoint(p, c, -o.rot) : p;
|
|
68
|
+
const inside = q.x >= o.x && q.x <= o.x + o.w && q.y >= o.y && q.y <= o.y + o.h;
|
|
69
|
+
let reach = (inside ? SELECTION_INNER_REACH_PX : SELECTION_REACH_PX) * css;
|
|
70
|
+
let hit = null;
|
|
71
|
+
for (const h of this.selectionHandles(css)) {
|
|
72
|
+
const d = Math.hypot(q.x - h.knob.x, q.y - h.knob.y);
|
|
73
|
+
if (d > reach)
|
|
74
|
+
continue;
|
|
75
|
+
reach = d;
|
|
76
|
+
const at = { x: h.x, y: (h.top + h.bottom) / 2 };
|
|
77
|
+
hit = { end: h.end, at: o.rot ? rotatePoint(at, c, o.rot) : at };
|
|
78
|
+
}
|
|
79
|
+
return hit;
|
|
80
|
+
}
|
|
81
|
+
setPresence(source, renderer) {
|
|
82
|
+
this.presence = source;
|
|
83
|
+
this.presenceRenderer = renderer;
|
|
84
|
+
for (const label of this.presenceLabels.values())
|
|
85
|
+
this.texts.invalidate(label.id);
|
|
86
|
+
this.presenceLabels.clear();
|
|
87
|
+
this.presenceText.clear();
|
|
88
|
+
}
|
|
89
|
+
previewIds() {
|
|
90
|
+
const ids = this.localPreviewIds();
|
|
91
|
+
for (const id of this.remoteGhosts(this.presence?.peers() ?? [], ids).keys())
|
|
92
|
+
ids.add(id);
|
|
93
|
+
return ids;
|
|
94
|
+
}
|
|
95
|
+
localPreviewIds() {
|
|
96
|
+
const ids = new Set();
|
|
97
|
+
for (const id of this.ctrl.previewOverlay().keys()) {
|
|
98
|
+
ids.add(id);
|
|
99
|
+
const o = this.scene.objects.get(id);
|
|
100
|
+
if (o && this.ctrl.subtreeDelta(o))
|
|
101
|
+
this.addDescendants(id, ids);
|
|
102
|
+
}
|
|
103
|
+
for (const id of [...ids])
|
|
104
|
+
for (const c of this.store.attachedConnectors(id))
|
|
105
|
+
ids.add(c);
|
|
106
|
+
return ids;
|
|
107
|
+
}
|
|
108
|
+
inPlace() {
|
|
109
|
+
const ids = this.localPreviewIds();
|
|
110
|
+
if (ids.size === 0)
|
|
111
|
+
return null;
|
|
112
|
+
const objects = new Map();
|
|
113
|
+
for (const id of ids) {
|
|
114
|
+
const o = this.scene.objects.get(id);
|
|
115
|
+
if (o)
|
|
116
|
+
objects.set(id, this.ctrl.effective(o));
|
|
117
|
+
}
|
|
118
|
+
const base = this.scene;
|
|
119
|
+
const scene = { objects: { get: (id) => objects.get(id) ?? base.objects.get(id) }, order: base.order };
|
|
120
|
+
const skip = new Set(this.remoteGhosts(this.presence?.peers() ?? [], ids).keys());
|
|
121
|
+
return { objects, scene, skip, skipText: this.textEditIds() };
|
|
122
|
+
}
|
|
123
|
+
remoteGhosts(peers, local) {
|
|
124
|
+
const blocked = new Set(local);
|
|
125
|
+
if (this.session)
|
|
126
|
+
blocked.add(this.session.objectId);
|
|
127
|
+
for (const id of [...blocked]) {
|
|
128
|
+
for (let p = this.scene.objects.get(id)?.parent; p; p = this.scene.objects.get(p)?.parent)
|
|
129
|
+
blocked.add(p);
|
|
130
|
+
}
|
|
131
|
+
const ghosts = new Map();
|
|
132
|
+
for (const peer of [...peers].sort((a, b) => a.connectionId.localeCompare(b.connectionId))) {
|
|
133
|
+
for (const ghost of peer.drag?.objects ?? []) {
|
|
134
|
+
const base = this.scene.objects.get(ghost.id);
|
|
135
|
+
if (!base || base.hidden || blocked.has(base.id) || ghosts.has(base.id))
|
|
136
|
+
continue;
|
|
137
|
+
if (this.session && this.isUnder(base, this.session.objectId))
|
|
138
|
+
continue;
|
|
139
|
+
ghosts.set(base.id, { object: { ...base, ...ghost }, peer });
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
return ghosts;
|
|
143
|
+
}
|
|
144
|
+
presenceColor(peer) {
|
|
145
|
+
const color = peer.user.color;
|
|
146
|
+
return /^#[0-9a-f]{6}$/i.test(color)
|
|
147
|
+
? [parseInt(color.slice(1, 3), 16) / 255, parseInt(color.slice(3, 5), 16) / 255, parseInt(color.slice(5, 7), 16) / 255, 1]
|
|
148
|
+
: ACCENT;
|
|
149
|
+
}
|
|
150
|
+
drawPresenceLabel(ctx, peer, at, zoom) {
|
|
151
|
+
const text = peer.user.name;
|
|
152
|
+
let label = this.presenceLabels.get(peer.connectionId);
|
|
153
|
+
if (!label) {
|
|
154
|
+
label = { id: `${this.sizeTextId}:peer:${peer.connectionId}`, text, version: 0 };
|
|
155
|
+
this.presenceLabels.set(peer.connectionId, label);
|
|
156
|
+
}
|
|
157
|
+
else if (label.text !== text) {
|
|
158
|
+
label.text = text;
|
|
159
|
+
label.version++;
|
|
160
|
+
}
|
|
161
|
+
const layout = this.texts.get({
|
|
162
|
+
id: label.id, kind: "text", parent: null, x: 0, y: 0, w: 2048, h: 20, rot: 0, version: 0,
|
|
163
|
+
textVersion: label.version, text: { paragraphs: [{ runs: [{ text }] }] },
|
|
164
|
+
style: { fontFamily: "Inter", fontSize: 12, color: "#ffffff", lineHeight: 1.2, paddingLeft: 0, paddingRight: 0 },
|
|
165
|
+
});
|
|
166
|
+
if (!layout)
|
|
167
|
+
return;
|
|
168
|
+
ctx.save();
|
|
169
|
+
ctx.translate(at.x, at.y);
|
|
170
|
+
ctx.scale(1 / zoom, 1 / zoom);
|
|
171
|
+
ctx.rect({ x: 0, y: 0, w: layout.width + 8, h: layout.height + 4 }, { fill: this.presenceColor(peer) }, 3);
|
|
172
|
+
ctx.text(layout, 4, 2);
|
|
173
|
+
ctx.restore();
|
|
174
|
+
}
|
|
175
|
+
drawPresence(ctx, peers, ghosts, zoom, viewport) {
|
|
176
|
+
const visibleLabels = new Set();
|
|
177
|
+
const visibleText = new Set();
|
|
178
|
+
const ownedGhosts = new Map();
|
|
179
|
+
for (const { object, peer } of ghosts.values()) {
|
|
180
|
+
let owned = ownedGhosts.get(peer);
|
|
181
|
+
if (!owned) {
|
|
182
|
+
owned = [];
|
|
183
|
+
ownedGhosts.set(peer, owned);
|
|
184
|
+
}
|
|
185
|
+
owned.push(object);
|
|
186
|
+
}
|
|
187
|
+
for (const peer of peers) {
|
|
188
|
+
const color = this.presenceColor(peer);
|
|
189
|
+
const fill = [color[0], color[1], color[2], 0.18];
|
|
190
|
+
let labelAt = null;
|
|
191
|
+
const outline = (object) => {
|
|
192
|
+
ctx.save();
|
|
193
|
+
if (object.rot)
|
|
194
|
+
ctx.rotate(object.rot, object.x + object.w / 2, object.y + object.h / 2);
|
|
195
|
+
ctx.rect({ x: object.x, y: object.y, w: object.w, h: object.h }, { stroke: { color, width: 1.5 / zoom } });
|
|
196
|
+
ctx.restore();
|
|
197
|
+
labelAt ??= { x: object.x, y: object.y - 22 / zoom };
|
|
198
|
+
};
|
|
199
|
+
for (const id of peer.selection) {
|
|
200
|
+
const base = this.scene.objects.get(id);
|
|
201
|
+
if (base && !base.hidden)
|
|
202
|
+
outline(this.ctrl.effective(ghosts.get(id)?.object ?? base));
|
|
203
|
+
}
|
|
204
|
+
for (const object of ownedGhosts.get(peer) ?? [])
|
|
205
|
+
outline(object);
|
|
206
|
+
const caretId = this.presence?.caretObjectId ? this.presence.caretObjectId(peer.connectionId) : peer.caret?.objectId;
|
|
207
|
+
const caretBase = caretId ? this.scene.objects.get(caretId) : null;
|
|
208
|
+
const caretObject = caretBase && !caretBase.hidden && caretBase.text
|
|
209
|
+
? this.ctrl.effective(this.session?.objectId === caretBase.id ? this.session.liveObject() : caretBase) : null;
|
|
210
|
+
const caretVisible = caretObject && (!viewport || rectIntersects(renderBounds(caretObject), viewport));
|
|
211
|
+
const caret = caretVisible ? peer.caret : null;
|
|
212
|
+
const renderer = this.presenceRenderer;
|
|
213
|
+
if (caret && renderer && caretObject) {
|
|
214
|
+
const object = caretObject;
|
|
215
|
+
const objectCase = object.style.textCase;
|
|
216
|
+
let cached = this.presenceText.get(object.id);
|
|
217
|
+
if (!cached || cached.text !== object.text || cached.objectCase !== objectCase) {
|
|
218
|
+
cached = { text: object.text, objectCase, doc: contentToPm(caseContent(object.text, objectCase)) };
|
|
219
|
+
this.presenceText.set(object.id, cached);
|
|
220
|
+
}
|
|
221
|
+
visibleText.add(object.id);
|
|
222
|
+
const layout = this.texts.get(object);
|
|
223
|
+
const view = new CanvasEditorView({ state: EditorState.create({ doc: cached.doc }), layout: {
|
|
224
|
+
renderer, layout: () => layout, origin: () => textOrigin(object, layout?.height ?? 0),
|
|
225
|
+
} });
|
|
226
|
+
const anchor = offsetToPmPos(cached.doc, caseOffset(object.text, objectCase, caret.anchor));
|
|
227
|
+
const head = offsetToPmPos(cached.doc, caseOffset(object.text, objectCase, caret.head));
|
|
228
|
+
const rect = view.coordsAtPos(head);
|
|
229
|
+
ctx.save();
|
|
230
|
+
if (object.rot)
|
|
231
|
+
ctx.rotate(object.rot, object.x + object.w / 2, object.y + object.h / 2);
|
|
232
|
+
for (const range of selectionRects(view, anchor, head))
|
|
233
|
+
ctx.rect(range, { fill });
|
|
234
|
+
if (rect.h > 0)
|
|
235
|
+
ctx.rect({ x: rect.x - 1 / zoom, y: rect.y, w: 2 / zoom, h: rect.h }, { fill: color });
|
|
236
|
+
this.drawPresenceLabel(ctx, peer, { x: rect.x, y: rect.y - 22 / zoom }, zoom);
|
|
237
|
+
visibleLabels.add(peer.connectionId);
|
|
238
|
+
ctx.restore();
|
|
239
|
+
}
|
|
240
|
+
if (peer.cursor) {
|
|
241
|
+
const { x, y } = peer.cursor;
|
|
242
|
+
ctx.path([{ t: "M", x, y }, { t: "L", x: x + 4 / zoom, y: y + 14 / zoom }, { t: "L", x: x + 7 / zoom, y: y + 9 / zoom }, { t: "L", x: x + 13 / zoom, y: y + 8 / zoom }, { t: "Z" }], { fill: color, stroke: { color: WHITE, width: 1 / zoom } });
|
|
243
|
+
labelAt = { x: x + 11 / zoom, y: y + 12 / zoom };
|
|
244
|
+
}
|
|
245
|
+
if (labelAt) {
|
|
246
|
+
this.drawPresenceLabel(ctx, peer, labelAt, zoom);
|
|
247
|
+
visibleLabels.add(peer.connectionId);
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
for (const [id, label] of this.presenceLabels)
|
|
251
|
+
if (!visibleLabels.has(id)) {
|
|
252
|
+
this.texts.invalidate(label.id);
|
|
253
|
+
this.presenceLabels.delete(id);
|
|
254
|
+
}
|
|
255
|
+
for (const id of this.presenceText.keys())
|
|
256
|
+
if (!visibleText.has(id))
|
|
257
|
+
this.presenceText.delete(id);
|
|
258
|
+
}
|
|
259
|
+
addDescendants(id, out) {
|
|
260
|
+
const order = this.scene.order;
|
|
261
|
+
for (let i = this.store.rankOf(id) + 1; i < order.length; i++) {
|
|
262
|
+
const cid = order[i];
|
|
263
|
+
const o = this.scene.objects.get(cid);
|
|
264
|
+
if (!o || !this.isUnder(o, id))
|
|
265
|
+
break;
|
|
266
|
+
out.add(cid);
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
isUnder(o, ancestor) {
|
|
270
|
+
for (let p = o.parent; p; p = this.scene.objects.get(p)?.parent ?? null)
|
|
271
|
+
if (p === ancestor)
|
|
272
|
+
return true;
|
|
273
|
+
return false;
|
|
274
|
+
}
|
|
275
|
+
textEditIds() {
|
|
276
|
+
return this.session ? new Set([this.session.objectId]) : new Set();
|
|
277
|
+
}
|
|
278
|
+
effectiveScene() {
|
|
279
|
+
const ctrl = this.ctrl;
|
|
280
|
+
const base = this.scene;
|
|
281
|
+
const objects = {
|
|
282
|
+
get: (id) => {
|
|
283
|
+
const o = base.objects.get(id);
|
|
284
|
+
return o ? ctrl.effective(o) : undefined;
|
|
285
|
+
},
|
|
286
|
+
};
|
|
287
|
+
return { objects, order: base.order };
|
|
288
|
+
}
|
|
289
|
+
effectiveSelected(exclude) {
|
|
290
|
+
const out = [];
|
|
291
|
+
for (const id of this.selection.ids) {
|
|
292
|
+
if (id === exclude)
|
|
293
|
+
continue;
|
|
294
|
+
const o = this.scene.objects.get(id);
|
|
295
|
+
if (o && !o.hidden)
|
|
296
|
+
out.push(this.ctrl.effective(o));
|
|
297
|
+
}
|
|
298
|
+
return out;
|
|
299
|
+
}
|
|
300
|
+
selectionBounds(exclude) {
|
|
301
|
+
return transformBounds(this.effectiveSelected(exclude));
|
|
302
|
+
}
|
|
303
|
+
drawSizeLabel(ctx, label, zoom) {
|
|
304
|
+
const format = (n) => String(Math.round(n * 100) / 100);
|
|
305
|
+
const text = `${format(label.width)} × ${format(label.height)}`;
|
|
306
|
+
if (text !== this.sizeText) {
|
|
307
|
+
this.sizeText = text;
|
|
308
|
+
this.sizeTextVersion++;
|
|
309
|
+
}
|
|
310
|
+
const layout = this.texts.get({
|
|
311
|
+
id: this.sizeTextId, kind: "text", parent: null, x: 0, y: 0, w: 2048, h: 20, rot: 0, version: 0,
|
|
312
|
+
textVersion: this.sizeTextVersion,
|
|
313
|
+
text: { paragraphs: [{ runs: [{ text }] }] },
|
|
314
|
+
style: { fontFamily: "Inter", fontSize: 12, color: "#ffffff", lineHeight: 1.2, paddingLeft: 0, paddingRight: 0 },
|
|
315
|
+
});
|
|
316
|
+
if (!layout)
|
|
317
|
+
return;
|
|
318
|
+
const pad = LABEL_PADDING_PX;
|
|
319
|
+
const height = layout.height + pad * 2;
|
|
320
|
+
ctx.save();
|
|
321
|
+
ctx.translate(label.at.x + LABEL_GAP_PX / zoom, label.at.y);
|
|
322
|
+
ctx.scale(1 / zoom, 1 / zoom);
|
|
323
|
+
ctx.rect({ x: 0, y: -height / 2, w: layout.width + pad * 2, h: height }, { fill: ACCENT }, 3);
|
|
324
|
+
ctx.text(layout, pad, -height / 2 + pad);
|
|
325
|
+
ctx.restore();
|
|
326
|
+
}
|
|
327
|
+
draw(ctx, tool, opts) {
|
|
328
|
+
const z = opts.zoom;
|
|
329
|
+
const lw = (opts.lineWidthPx ?? 1) / z;
|
|
330
|
+
const profile = tool.handleProfile ?? pointerProfile({ tolerance: 4 / z, zoom: z }).handles;
|
|
331
|
+
const hs = (opts.handleSizePx ?? profile.sizePx) / z;
|
|
332
|
+
const handleShape = profile.shape;
|
|
333
|
+
const session = this.session;
|
|
334
|
+
const editing = session?.objectId ?? null;
|
|
335
|
+
const handlePaint = { fill: WHITE, stroke: { color: ACCENT, width: lw } };
|
|
336
|
+
const css = session && opts.textHandles ? opts.textHandles / z : 0;
|
|
337
|
+
this.handlesShown = css > 0;
|
|
338
|
+
const previewIds = this.localPreviewIds();
|
|
339
|
+
const peers = this.presence?.peers() ?? [];
|
|
340
|
+
const remoteGhosts = this.remoteGhosts(peers, previewIds);
|
|
341
|
+
if (remoteGhosts.size) {
|
|
342
|
+
const objects = { get: (id) => remoteGhosts.get(id)?.object ?? this.scene.objects.get(id) };
|
|
343
|
+
const scene = { objects, order: this.scene.order };
|
|
344
|
+
for (const { object } of [...remoteGhosts.values()].sort((a, b) => this.store.rankOf(a.object.id) - this.store.rankOf(b.object.id))) {
|
|
345
|
+
paintObject(ctx, object, scene, this.texts, { zoom: z, ...(this.images ? { images: this.images } : {}) });
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
if (!opts.previewsInPlace && previewIds.size) {
|
|
349
|
+
const eff = this.effectiveScene();
|
|
350
|
+
const ordered = [...previewIds].sort((a, b) => this.store.rankOf(a) - this.store.rankOf(b));
|
|
351
|
+
for (const id of ordered) {
|
|
352
|
+
const o = this.scene.objects.get(id);
|
|
353
|
+
if (o && !o.hidden)
|
|
354
|
+
paintObject(ctx, this.ctrl.effective(o), eff, this.texts, id === editing ? { zoom: z, skipText: true, ...(this.images ? { images: this.images } : {}) } : { zoom: z, ...(this.images ? { images: this.images } : {}) });
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
if (session) {
|
|
358
|
+
const live = this.ctrl.effective(session.liveObject());
|
|
359
|
+
const eff = this.effectiveScene();
|
|
360
|
+
paintObject(ctx, live, eff, this.texts, { zoom: z, textOnly: true });
|
|
361
|
+
ctx.save();
|
|
362
|
+
if (live.rot)
|
|
363
|
+
ctx.rotate(live.rot, live.x + live.w / 2, live.y + live.h / 2);
|
|
364
|
+
for (const r of session.selectionRects())
|
|
365
|
+
ctx.rect(r, { fill: ACCENT_FILL });
|
|
366
|
+
const ul = PREEDIT_LINE_PX / z;
|
|
367
|
+
for (const r of session.preeditRects())
|
|
368
|
+
ctx.line(r.x, r.y + r.h - ul / 2, r.x + r.w, r.y + r.h - ul / 2, { color: ACCENT, width: ul });
|
|
369
|
+
const caret = session.caretVisible(opts.now ?? performance.now()) ? session.caretRect() : null;
|
|
370
|
+
if (caret) {
|
|
371
|
+
const cw = CARET_PX / z;
|
|
372
|
+
ctx.rect({ x: caret.x - cw / 2, y: caret.y, w: cw, h: caret.h }, { fill: ACCENT });
|
|
373
|
+
}
|
|
374
|
+
ctx.rect({ x: live.x, y: live.y, w: live.w, h: live.h }, { stroke: { color: ACCENT, width: lw } });
|
|
375
|
+
const r = (SELECTION_KNOB_PX / 2) * css;
|
|
376
|
+
if (css)
|
|
377
|
+
for (const h of this.selectionHandles(css)) {
|
|
378
|
+
ctx.line(h.x, Math.min(h.top, h.knob.y), h.x, Math.max(h.bottom, h.knob.y), { color: ACCENT, width: 2 * css });
|
|
379
|
+
ctx.ellipse({ x: h.knob.x - r, y: h.knob.y - r, w: 2 * r, h: 2 * r }, { fill: ACCENT });
|
|
380
|
+
}
|
|
381
|
+
ctx.restore();
|
|
382
|
+
}
|
|
383
|
+
const selected = this.effectiveSelected(editing ?? undefined);
|
|
384
|
+
let connectorsSelected = false;
|
|
385
|
+
for (const e of selected) {
|
|
386
|
+
if (e.kind === "connector") {
|
|
387
|
+
connectorsSelected = true;
|
|
388
|
+
continue;
|
|
389
|
+
}
|
|
390
|
+
if (e.kind === "group")
|
|
391
|
+
continue;
|
|
392
|
+
ctx.save();
|
|
393
|
+
if (e.rot)
|
|
394
|
+
ctx.rotate(e.rot, e.x + e.w / 2, e.y + e.h / 2);
|
|
395
|
+
ctx.rect({ x: e.x, y: e.y, w: e.w, h: e.h }, { stroke: { color: ACCENT, width: lw } });
|
|
396
|
+
ctx.restore();
|
|
397
|
+
}
|
|
398
|
+
if (connectorsSelected) {
|
|
399
|
+
const eff = this.effectiveScene();
|
|
400
|
+
const markPaint = { stroke: { color: ACCENT, width: lw } };
|
|
401
|
+
for (const e of selected) {
|
|
402
|
+
const points = endpointPoints(e, eff);
|
|
403
|
+
if (!points)
|
|
404
|
+
continue;
|
|
405
|
+
const paint = this.selection.size === 1 && endpointsDraggable(this.scene.objects.get(e.id), this.scene) ? handlePaint : markPaint;
|
|
406
|
+
for (const at of points) {
|
|
407
|
+
const box = { x: at.x - hs / 2, y: at.y - hs / 2, w: hs, h: hs };
|
|
408
|
+
if (handleShape === "circle")
|
|
409
|
+
ctx.ellipse(box, paint);
|
|
410
|
+
else
|
|
411
|
+
ctx.rect(box, paint);
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
const gradientId = tool.gradient?.target.id;
|
|
416
|
+
const transformable = selected.filter(o => isTransformable(o) && !isLocked(o, this.scene) && o.id !== gradientId);
|
|
417
|
+
const frame = handleFrame(transformable);
|
|
418
|
+
if (frame) {
|
|
419
|
+
const b = frame.box;
|
|
420
|
+
if (transformable.length > 1)
|
|
421
|
+
ctx.rect(b, { stroke: { color: ACCENT, width: lw, dash: [4 / z, 3 / z] } });
|
|
422
|
+
if (!handlesSuppressed(b, z, profile)) {
|
|
423
|
+
ctx.save();
|
|
424
|
+
if (frame.rot)
|
|
425
|
+
ctx.rotate(frame.rot, b.x + b.w / 2, b.y + b.h / 2);
|
|
426
|
+
const handles = handlePositions(b, z, 0, profile);
|
|
427
|
+
for (const kind of RESIZE_HANDLES) {
|
|
428
|
+
const c = handles[kind];
|
|
429
|
+
const box = { x: c.x - hs / 2, y: c.y - hs / 2, w: hs, h: hs };
|
|
430
|
+
if (handleShape === "circle")
|
|
431
|
+
ctx.ellipse(box, handlePaint);
|
|
432
|
+
else
|
|
433
|
+
ctx.rect(box, handlePaint);
|
|
434
|
+
}
|
|
435
|
+
const r = handles.rotate;
|
|
436
|
+
ctx.line(handles.n.x, handles.n.y - hs / 2, r.x, r.y + hs / 2, { color: ACCENT, width: lw });
|
|
437
|
+
ctx.ellipse({ x: r.x - hs / 2, y: r.y - hs / 2, w: hs, h: hs }, handlePaint);
|
|
438
|
+
ctx.restore();
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
if (tool.gradient)
|
|
442
|
+
drawGradientHandles(ctx, tool.gradient, z);
|
|
443
|
+
if (tool.marquee)
|
|
444
|
+
ctx.rect(tool.marquee, { fill: ACCENT_FILL, stroke: { color: ACCENT, width: lw } });
|
|
445
|
+
const creation = tool.creation;
|
|
446
|
+
if (creation) {
|
|
447
|
+
const dashed = { stroke: { color: ACCENT, width: lw, dash: [4 / z, 3 / z] } };
|
|
448
|
+
if (creation.rect) {
|
|
449
|
+
if (creation.kind === "ellipse")
|
|
450
|
+
ctx.ellipse(creation.rect, dashed);
|
|
451
|
+
else
|
|
452
|
+
ctx.rect(creation.rect, dashed);
|
|
453
|
+
}
|
|
454
|
+
if (creation.line) {
|
|
455
|
+
const { from, to } = creation.line;
|
|
456
|
+
ctx.line(from.x, from.y, to.x, to.y, { color: ACCENT, width: lw, dash: [4 / z, 3 / z] });
|
|
457
|
+
for (const p of [from, to])
|
|
458
|
+
ctx.ellipse({ x: p.x - hs / 2, y: p.y - hs / 2, w: hs, h: hs }, handlePaint);
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
for (const guide of tool.guides ?? []) {
|
|
462
|
+
const { from, to } = guide;
|
|
463
|
+
const stroke = { color: GUIDE, width: lw };
|
|
464
|
+
ctx.line(from.x, from.y, to.x, to.y, stroke);
|
|
465
|
+
if (guide.kind === "spacing") {
|
|
466
|
+
const tick = 3 / z;
|
|
467
|
+
for (const p of [from, to]) {
|
|
468
|
+
if (guide.axis === "x")
|
|
469
|
+
ctx.line(p.x, p.y - tick, p.x, p.y + tick, stroke);
|
|
470
|
+
else
|
|
471
|
+
ctx.line(p.x - tick, p.y, p.x + tick, p.y, stroke);
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
if (tool.sizeLabel)
|
|
476
|
+
this.drawSizeLabel(ctx, tool.sizeLabel, z);
|
|
477
|
+
else
|
|
478
|
+
this.texts.invalidate(this.sizeTextId);
|
|
479
|
+
this.drawPresence(ctx, peers, remoteGhosts, z, opts.viewport);
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
function drawGradientHandles(ctx, g, z) {
|
|
483
|
+
const halo = { color: WHITE, width: 3 / z };
|
|
484
|
+
const line = { color: ACCENT, width: 1 / z };
|
|
485
|
+
const disc = (at, px, inner, ring) => {
|
|
486
|
+
const d = px / z;
|
|
487
|
+
ctx.ellipse({ x: at.x - d / 2, y: at.y - d / 2, w: d, h: d }, { fill: WHITE, stroke: { color: ACCENT, width: ring / z } });
|
|
488
|
+
const i = d - 4 / z;
|
|
489
|
+
if (i > 0)
|
|
490
|
+
ctx.ellipse({ x: at.x - i / 2, y: at.y - i / 2, w: i, h: i }, { fill: inner });
|
|
491
|
+
};
|
|
492
|
+
if (g.line) {
|
|
493
|
+
const { from, to } = g.line;
|
|
494
|
+
ctx.line(from.x, from.y, to.x, to.y, halo);
|
|
495
|
+
ctx.line(from.x, from.y, to.x, to.y, line);
|
|
496
|
+
}
|
|
497
|
+
if (g.ellipse) {
|
|
498
|
+
const f = g.frame;
|
|
499
|
+
ctx.save();
|
|
500
|
+
if (f.rot)
|
|
501
|
+
ctx.rotate(f.rot, f.x + f.w / 2, f.y + f.h / 2);
|
|
502
|
+
ctx.ellipse(g.ellipse, { stroke: halo });
|
|
503
|
+
ctx.ellipse(g.ellipse, { stroke: line });
|
|
504
|
+
ctx.restore();
|
|
505
|
+
const { center, radius } = g.handles;
|
|
506
|
+
if (center && radius) {
|
|
507
|
+
ctx.line(center.x, center.y, radius.x, radius.y, halo);
|
|
508
|
+
ctx.line(center.x, center.y, radius.x, radius.y, line);
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
for (const s of g.stops)
|
|
512
|
+
disc(s.at, GRADIENT_STOP_PX, s.color, 1);
|
|
513
|
+
for (const kind of g.type === "linear" ? ["p0", "p1"] : ["center", "radius"]) {
|
|
514
|
+
const at = g.handles[kind];
|
|
515
|
+
if (!at)
|
|
516
|
+
continue;
|
|
517
|
+
const active = g.active === kind;
|
|
518
|
+
disc(at, GRADIENT_HANDLE_PX + (active ? 2 : 0), kind === "p0" || kind === "center" ? g.ends[0] : g.ends[1], active ? 2 : 1);
|
|
519
|
+
}
|
|
520
|
+
}
|