@g9tm/lynxcontrols-canvas 2.5.9 → 2.5.11

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.
@@ -0,0 +1,814 @@
1
+ import { observable } from "./observable.js";
2
+ import { createFreeDraw, createImage, createLinear, createShape, createText, deleteElements, duplicate, editText, erasedBy, expandSelection, group, hitTest, inBox, lockElements, moveElements, movePoint, movingSet, reorder, resizeElements, rotateElements, styleElements, ungroup } from "./scene/edit.js";
3
+ import { DEFAULT_STYLE, FONT_FAMILY, bump, isLinear } from "./scene/elements.js";
4
+ import { absoluteCoords, commonBounds, elementBounds } from "./scene/geometry.js";
5
+ import { toItem } from "./scene/shapes.js";
6
+ function _define_property(obj, key, value) {
7
+ if (key in obj) Object.defineProperty(obj, key, {
8
+ value: value,
9
+ enumerable: true,
10
+ configurable: true,
11
+ writable: true
12
+ });
13
+ else obj[key] = value;
14
+ return obj;
15
+ }
16
+ const BACKGROUND = '#ffffff';
17
+ class BoardController {
18
+ subscribe(listener) {
19
+ this.listeners.add(listener);
20
+ return ()=>this.listeners.delete(listener);
21
+ }
22
+ get scene() {
23
+ return this.options.scene;
24
+ }
25
+ destroy() {
26
+ this.off();
27
+ }
28
+ attach() {
29
+ this.attached = true;
30
+ this.items.clear();
31
+ this.order = [];
32
+ const message = {
33
+ reset: true,
34
+ ...this.diff(this.scene.all(), true),
35
+ files: this.files,
36
+ selection: this.selectionMessage(),
37
+ peers: this.peerMessage()
38
+ };
39
+ if (this.fitted) message.view = {
40
+ scrollX: this.view.scrollX,
41
+ scrollY: this.view.scrollY,
42
+ zoom: this.view.zoom
43
+ };
44
+ this.options.send(message);
45
+ }
46
+ detach() {
47
+ this.attached = false;
48
+ }
49
+ loaded() {
50
+ this.state.empty = 0 === this.scene.visible().length;
51
+ if (this.attached) this.attach();
52
+ this.updateHistory();
53
+ this.fitted = this.view.width > 0 && this.view.height > 0;
54
+ if (this.fitted) this.fit(false);
55
+ }
56
+ send(message) {
57
+ if (!this.attached) return;
58
+ if (this.pendingDone.length) {
59
+ var _message_done;
60
+ message.done = [
61
+ ...null != (_message_done = message.done) ? _message_done : [],
62
+ ...this.pendingDone
63
+ ];
64
+ this.pendingDone = [];
65
+ }
66
+ this.options.send(message);
67
+ }
68
+ diff(elements, all = false) {
69
+ const upsert = [];
70
+ const remove = [];
71
+ let orderChanged = all;
72
+ for (const element of elements){
73
+ const known = this.items.get(element.id);
74
+ if (element.isDeleted) {
75
+ if (known) {
76
+ this.items.delete(element.id);
77
+ remove.push(element.id);
78
+ orderChanged = true;
79
+ }
80
+ continue;
81
+ }
82
+ if (known && known.v === element.version && !all) continue;
83
+ const item = toItem(element, {
84
+ background: BACKGROUND
85
+ });
86
+ if (!known) orderChanged = true;
87
+ this.items.set(element.id, {
88
+ v: element.version,
89
+ item
90
+ });
91
+ upsert.push(item);
92
+ }
93
+ const result = {};
94
+ if (upsert.length) result.items = upsert;
95
+ if (remove.length) result.remove = remove;
96
+ if (orderChanged || all) {
97
+ const order = this.scene.visible().map((element)=>element.id);
98
+ if (all || order.join() !== this.order.join()) {
99
+ this.order = order;
100
+ result.order = order;
101
+ }
102
+ }
103
+ return result;
104
+ }
105
+ sceneChanged(change) {
106
+ var _this_state_text;
107
+ const message = this.diff(change.changed);
108
+ if (change.orderChanged && !message.order) {
109
+ const order = this.scene.visible().map((element)=>element.id);
110
+ if (order.join() !== this.order.join()) {
111
+ this.order = order;
112
+ message.order = order;
113
+ }
114
+ }
115
+ const selected = this.state.selected.filter((id)=>this.scene.get(id) && !this.scene.get(id).isDeleted);
116
+ if (selected.length !== this.state.selected.length) this.state.selected = selected;
117
+ if (selected.some((id)=>change.changed.some((element)=>element.id === id))) message.selection = this.selectionMessage();
118
+ if (!change.local) message.peers = this.peerMessage();
119
+ this.state.empty = 0 === this.scene.visible().length;
120
+ this.updateHistory();
121
+ this.send(message);
122
+ if (change.local && (null == (_this_state_text = this.state.text) ? void 0 : _this_state_text.id) && change.changed.some((element)=>{
123
+ var _this_state_text;
124
+ return element.id === (null == (_this_state_text = this.state.text) ? void 0 : _this_state_text.id) && element.isDeleted;
125
+ })) this.state.text = null;
126
+ }
127
+ updateHistory() {
128
+ if (this.state.canUndo !== this.scene.canUndo) this.state.canUndo = this.scene.canUndo;
129
+ if (this.state.canRedo !== this.scene.canRedo) this.state.canRedo = this.scene.canRedo;
130
+ }
131
+ select(ids) {
132
+ var _this_options_pointer, _this_options;
133
+ this.state.selected = [
134
+ ...ids
135
+ ];
136
+ const first = ids.length ? this.scene.get(ids[0]) : void 0;
137
+ if (first) this.adoptStyle(first);
138
+ this.send({
139
+ selection: this.selectionMessage()
140
+ });
141
+ null == (_this_options_pointer = (_this_options = this.options).pointer) || _this_options_pointer.call(_this_options, this.lastPointer[0], this.lastPointer[1], false, this.state.selected, false);
142
+ }
143
+ adoptStyle(element) {
144
+ const style = {
145
+ ...this.state.style
146
+ };
147
+ if ('image' !== element.type) style.strokeColor = element.strokeColor;
148
+ if ('text' !== element.type && 'arrow' !== element.type && 'line' !== element.type && 'freedraw' !== element.type && 'image' !== element.type) {
149
+ style.backgroundColor = element.backgroundColor;
150
+ style.fillStyle = element.fillStyle;
151
+ }
152
+ style.strokeWidth = element.strokeWidth;
153
+ style.strokeStyle = element.strokeStyle;
154
+ style.roughness = element.roughness;
155
+ style.opacity = element.opacity;
156
+ if ('rectangle' === element.type || 'diamond' === element.type || isLinear(element)) style.roundness = element.roundness ? 'round' : 'sharp';
157
+ if ('text' === element.type) {
158
+ var _element_fontSize, _element_fontFamily, _element_textAlign;
159
+ style.fontSize = null != (_element_fontSize = element.fontSize) ? _element_fontSize : style.fontSize;
160
+ style.fontFamily = null != (_element_fontFamily = element.fontFamily) ? _element_fontFamily : style.fontFamily;
161
+ style.textAlign = null != (_element_textAlign = element.textAlign) ? _element_textAlign : style.textAlign;
162
+ }
163
+ if ('arrow' === element.type) {
164
+ var _element_startArrowhead, _element_endArrowhead;
165
+ style.startArrowhead = null != (_element_startArrowhead = element.startArrowhead) ? _element_startArrowhead : null;
166
+ style.endArrowhead = null != (_element_endArrowhead = element.endArrowhead) ? _element_endArrowhead : null;
167
+ }
168
+ this.state.style = style;
169
+ }
170
+ selectedElements() {
171
+ return this.state.selected.map((id)=>this.scene.get(id)).filter((element)=>Boolean(element && !element.isDeleted));
172
+ }
173
+ selectionMessage() {
174
+ const elements = this.selectedElements().filter((element)=>!('text' === element.type && element.containerId && this.state.selected.includes(element.containerId)));
175
+ if (!elements.length) return null;
176
+ const boxes = elements.map((element)=>{
177
+ const [x1, y1, x2, y2] = absoluteCoords(element);
178
+ return {
179
+ b: [
180
+ x1,
181
+ y1,
182
+ x2,
183
+ y2
184
+ ],
185
+ a: element.angle || 0
186
+ };
187
+ });
188
+ const locked = elements.some((element)=>element.locked) || !this.state.editable;
189
+ const ids = movingSet(this.scene, elements.map((element)=>element.id));
190
+ if (1 === elements.length) {
191
+ var _ref;
192
+ var _element_points;
193
+ const element = elements[0];
194
+ if (isLinear(element) && (null != (_ref = null == (_element_points = element.points) ? void 0 : _element_points.length) ? _ref : 0) >= 2) {
195
+ var _element_points1;
196
+ const [, , , , cx, cy] = absoluteCoords(element);
197
+ const angle = element.angle || 0;
198
+ const points = (null != (_element_points1 = element.points) ? _element_points1 : []).map(([px, py])=>{
199
+ const x = px + element.x;
200
+ const y = py + element.y;
201
+ const cos = Math.cos(angle);
202
+ const sin = Math.sin(angle);
203
+ return [
204
+ (x - cx) * cos - (y - cy) * sin + cx,
205
+ (x - cx) * sin + (y - cy) * cos + cy
206
+ ];
207
+ });
208
+ return {
209
+ ids,
210
+ boxes: [],
211
+ frame: boxes[0],
212
+ handles: locked ? 'none' : 2 === points.length ? 'none' : 'corners',
213
+ ...locked ? {} : {
214
+ points
215
+ }
216
+ };
217
+ }
218
+ return {
219
+ ids,
220
+ boxes,
221
+ frame: boxes[0],
222
+ handles: locked ? 'none' : 'text' === element.type || 'freedraw' === element.type || 'image' === element.type ? 'corners' : 'all'
223
+ };
224
+ }
225
+ const bounds = commonBounds(elements);
226
+ return {
227
+ ids,
228
+ boxes,
229
+ frame: {
230
+ b: bounds,
231
+ a: 0
232
+ },
233
+ handles: locked ? 'none' : 'corners'
234
+ };
235
+ }
236
+ setPeers(peers) {
237
+ this.peers = peers;
238
+ this.state.peers = [
239
+ ...new Set(peers.filter((peer)=>!peer.gone).map((peer)=>peer.userId))
240
+ ];
241
+ this.send({
242
+ peers: this.peerMessage()
243
+ });
244
+ }
245
+ refreshPeers() {
246
+ this.send({
247
+ peers: this.peerMessage()
248
+ });
249
+ }
250
+ peerMessage() {
251
+ return this.peers.map((peer)=>{
252
+ const who = this.options.identify(peer.userId);
253
+ const boxes = peer.selectedIds.map((id)=>this.scene.get(id)).filter((element)=>Boolean(element && !element.isDeleted)).map((element)=>{
254
+ const [x1, y1, x2, y2] = absoluteCoords(element);
255
+ return {
256
+ b: [
257
+ x1,
258
+ y1,
259
+ x2,
260
+ y2
261
+ ],
262
+ a: element.angle || 0
263
+ };
264
+ });
265
+ return {
266
+ id: peer.peerKey,
267
+ name: who.name,
268
+ color: who.color,
269
+ x: peer.gone ? null : peer.x,
270
+ y: peer.gone ? null : peer.y,
271
+ down: peer.down,
272
+ boxes
273
+ };
274
+ });
275
+ }
276
+ addFiles(urls) {
277
+ this.files = {
278
+ ...this.files,
279
+ ...urls
280
+ };
281
+ this.send({
282
+ files: urls
283
+ });
284
+ }
285
+ onView(view) {
286
+ const resized = view.width !== this.view.width || view.height !== this.view.height;
287
+ this.view = view;
288
+ if (Math.abs(this.state.zoom - view.zoom) > 1e-4) this.state.zoom = view.zoom;
289
+ if (view.width > 0 && view.height > 0 && (!this.fitted || resized && !this.cameraTouched)) {
290
+ this.fitted = true;
291
+ this.fit(false);
292
+ } else if (!resized && this.fitted && !this.isRequested(view)) this.cameraTouched = true;
293
+ }
294
+ isRequested(view) {
295
+ const r = this.requested;
296
+ return !!r && Math.abs(r.zoom - view.zoom) < 1e-6 && Math.abs(r.scrollX - view.scrollX) < 1e-3 && Math.abs(r.scrollY - view.scrollY) < 1e-3;
297
+ }
298
+ get camera() {
299
+ return this.view;
300
+ }
301
+ setView(view) {
302
+ this.requested = view;
303
+ this.view = {
304
+ ...this.view,
305
+ ...view
306
+ };
307
+ this.state.zoom = view.zoom;
308
+ this.send({
309
+ view
310
+ });
311
+ }
312
+ fit(enlarge = true) {
313
+ const bounds = commonBounds(this.scene.visible());
314
+ const { width, height } = this.view;
315
+ if (!width || !height) return;
316
+ if (!bounds) return void this.setView({
317
+ zoom: 1,
318
+ scrollX: width / 2,
319
+ scrollY: height / 2
320
+ });
321
+ const padding = 40;
322
+ const bars = 64;
323
+ const fitZoom = Math.min((width - 2 * padding) / Math.max(1, bounds[2] - bounds[0]), (height - 2 * bars) / Math.max(1, bounds[3] - bounds[1]));
324
+ const zoom = Math.max(0.1, Math.min(enlarge ? 3 : 1, fitZoom));
325
+ const cx = (bounds[0] + bounds[2]) / 2;
326
+ const cy = (bounds[1] + bounds[3]) / 2;
327
+ this.setView({
328
+ zoom,
329
+ scrollX: width / 2 / zoom - cx,
330
+ scrollY: height / 2 / zoom - cy
331
+ });
332
+ }
333
+ zoomBy(factor) {
334
+ this.cameraTouched = true;
335
+ const { width, height, zoom, scrollX, scrollY } = this.view;
336
+ const next = Math.max(0.1, Math.min(30, 0 === factor ? 1 : zoom * factor));
337
+ const cx = width / 2 / zoom - scrollX;
338
+ const cy = height / 2 / zoom - scrollY;
339
+ this.setView({
340
+ zoom: next,
341
+ scrollX: width / 2 / next - cx,
342
+ scrollY: height / 2 / next - cy
343
+ });
344
+ }
345
+ centre() {
346
+ const { width, height, zoom, scrollX, scrollY } = this.view;
347
+ return [
348
+ width / 2 / zoom - scrollX,
349
+ height / 2 / zoom - scrollY
350
+ ];
351
+ }
352
+ setTool(tool) {
353
+ if (this.state.text) this.finishText();
354
+ this.state.tool = tool;
355
+ if ('selection' !== tool && this.state.selected.length && 'hand' !== tool) this.select([]);
356
+ }
357
+ setStyle(patch) {
358
+ this.state.style = {
359
+ ...this.state.style,
360
+ ...patch
361
+ };
362
+ if (!this.state.selected.length || !this.state.editable) return;
363
+ const ids = this.state.selected;
364
+ const { roundness, ...rest } = patch;
365
+ const merged = new Map();
366
+ if (Object.keys(rest).length) for (const element of styleElements(this.scene, ids, rest))merged.set(element.id, element);
367
+ if (void 0 !== roundness) for (const id of ids){
368
+ var _merged_get;
369
+ const element = null != (_merged_get = merged.get(id)) ? _merged_get : this.scene.get(id);
370
+ if (!element || element.isDeleted || element.locked || ![
371
+ 'rectangle',
372
+ 'diamond',
373
+ 'line',
374
+ 'arrow',
375
+ 'image'
376
+ ].includes(element.type)) continue;
377
+ const value = 'round' === roundness ? 'rectangle' === element.type || 'image' === element.type ? {
378
+ type: 3
379
+ } : {
380
+ type: 2
381
+ } : null;
382
+ merged.set(id, merged.has(id) ? {
383
+ ...element,
384
+ roundness: value
385
+ } : bump(element, {
386
+ roundness: value
387
+ }));
388
+ }
389
+ this.commit([
390
+ ...merged.values()
391
+ ]);
392
+ }
393
+ commit(edits, history = true) {
394
+ if (!edits.length || !this.state.editable) return;
395
+ this.scene.commit(edits, history);
396
+ }
397
+ undo() {
398
+ if (!this.state.editable) return;
399
+ if (this.state.text) this.finishText();
400
+ this.scene.undo();
401
+ this.refreshSelection();
402
+ }
403
+ redo() {
404
+ if (!this.state.editable) return;
405
+ this.scene.redo();
406
+ this.refreshSelection();
407
+ }
408
+ refreshSelection() {
409
+ const selected = this.state.selected.filter((id)=>this.scene.get(id) && !this.scene.get(id).isDeleted);
410
+ this.state.selected = selected;
411
+ this.send({
412
+ selection: this.selectionMessage()
413
+ });
414
+ }
415
+ deleteSelection() {
416
+ if (!this.state.selected.length) return;
417
+ this.commit(deleteElements(this.scene, this.state.selected));
418
+ this.select([]);
419
+ }
420
+ duplicateSelection() {
421
+ if (!this.state.selected.length) return;
422
+ const copy = duplicate(this.scene, this.state.selected);
423
+ this.commit(copy.elements);
424
+ this.select(copy.selected);
425
+ }
426
+ selectAll() {
427
+ this.setTool('selection');
428
+ this.select(this.scene.visible().filter((element)=>!('text' === element.type && element.containerId) && !element.locked).map((element)=>element.id));
429
+ }
430
+ reorder(move) {
431
+ if (!this.state.selected.length) return;
432
+ this.commit(reorder(this.scene, movingSet(this.scene, this.state.selected), move));
433
+ }
434
+ group() {
435
+ this.commit(group(this.scene, this.state.selected));
436
+ this.refreshSelection();
437
+ }
438
+ ungroup() {
439
+ this.commit(ungroup(this.scene, this.state.selected));
440
+ this.refreshSelection();
441
+ }
442
+ setLocked(locked) {
443
+ this.commit(lockElements(this.scene, this.state.selected, locked));
444
+ this.refreshSelection();
445
+ }
446
+ clear() {
447
+ this.commit(deleteElements(this.scene, this.scene.visible().map((element)=>element.id)));
448
+ this.select([]);
449
+ }
450
+ insertImage(fileId, width, height) {
451
+ const max = 360 / Math.max(this.view.zoom, 0.1);
452
+ const scale = Math.min(1, max / Math.max(width, height, 1));
453
+ const w = Math.max(20, width * scale);
454
+ const h = Math.max(20, height * scale);
455
+ const [cx, cy] = this.centre();
456
+ const element = {
457
+ ...createImage(this.scene, fileId, cx - w / 2, cy - h / 2, w, h, this.state.style),
458
+ status: 'saved'
459
+ };
460
+ this.commit([
461
+ element
462
+ ]);
463
+ this.state.tool = 'selection';
464
+ this.select([
465
+ element.id
466
+ ]);
467
+ }
468
+ beginText(target, at) {
469
+ var _this_scene_get, _ref;
470
+ if (!this.state.editable) return;
471
+ if (this.state.text) this.finishText();
472
+ let id = null;
473
+ let containerId = null;
474
+ if ((null == target ? void 0 : target.type) === 'text') id = target.id;
475
+ else if (target && ('rectangle' === target.type || 'ellipse' === target.type || 'diamond' === target.type)) {
476
+ var _ref1;
477
+ var _target_boundElements_find, _target_boundElements;
478
+ containerId = target.id;
479
+ id = null != (_ref1 = null == (_target_boundElements = target.boundElements) ? void 0 : null == (_target_boundElements_find = _target_boundElements.find((bound)=>'text' === bound.type)) ? void 0 : _target_boundElements_find.id) ? _ref1 : null;
480
+ }
481
+ const element = id ? this.scene.get(id) : void 0;
482
+ this.textBefore = new Map();
483
+ if (element) {
484
+ this.textBefore.set(element.id, element);
485
+ this.scene.editing.add(element.id);
486
+ this.adoptStyle(element);
487
+ }
488
+ if (containerId) this.textBefore.set(containerId, null != (_this_scene_get = this.scene.get(containerId)) ? _this_scene_get : null);
489
+ this.state.text = {
490
+ id: element ? element.id : null,
491
+ x: at[0],
492
+ y: at[1],
493
+ containerId,
494
+ value: null != (_ref = null == element ? void 0 : element.text) ? _ref : ''
495
+ };
496
+ this.select(element ? [
497
+ element.id
498
+ ] : containerId ? [
499
+ containerId
500
+ ] : []);
501
+ }
502
+ typeText(value) {
503
+ var _this_scene_get;
504
+ var _this_textBefore, _this_textBefore1;
505
+ const edit = this.state.text;
506
+ if (!edit || !this.state.editable) return;
507
+ this.state.text = {
508
+ ...edit,
509
+ value
510
+ };
511
+ if (edit.id && this.scene.get(edit.id) && !this.scene.get(edit.id).isDeleted) {
512
+ if (!value.trim()) return;
513
+ this.commit(editText(this.scene, edit.id, value), false);
514
+ return;
515
+ }
516
+ if (!value.trim()) return;
517
+ const container = edit.containerId ? this.scene.get(edit.containerId) : void 0;
518
+ const lineHeight = 1.25 * this.state.style.fontSize;
519
+ const created = createText(this.scene, edit.x, edit.y - lineHeight / 2, value, this.state.style, container && !container.isDeleted ? container : void 0);
520
+ for (const element of created)if (!(null == (_this_textBefore = this.textBefore) ? void 0 : _this_textBefore.has(element.id))) null == (_this_textBefore1 = this.textBefore) || _this_textBefore1.set(element.id, 'text' === element.type ? null : null != (_this_scene_get = this.scene.get(element.id)) ? _this_scene_get : null);
521
+ this.commit(created, false);
522
+ this.scene.editing.add(created[0].id);
523
+ this.state.text = {
524
+ ...edit,
525
+ value,
526
+ id: created[0].id
527
+ };
528
+ this.select([
529
+ created[0].id
530
+ ]);
531
+ }
532
+ finishText() {
533
+ const edit = this.state.text;
534
+ if (!edit) return;
535
+ this.state.text = null;
536
+ if (edit.id) this.scene.editing.delete(edit.id);
537
+ const element = edit.id ? this.scene.get(edit.id) : void 0;
538
+ if (element && !element.isDeleted) if (edit.value.trim()) {
539
+ if (element.text !== edit.value) this.commit(editText(this.scene, element.id, edit.value), false);
540
+ } else this.commit(deleteElements(this.scene, [
541
+ element.id
542
+ ]), false);
543
+ if (this.textBefore) this.scene.record(this.textBefore);
544
+ this.textBefore = null;
545
+ this.updateHistory();
546
+ if (!this.state.toolLocked && 'text' === this.state.tool) this.state.tool = 'selection';
547
+ this.refreshSelection();
548
+ }
549
+ threshold() {
550
+ return 10 / Math.max(this.view.zoom, 0.1);
551
+ }
552
+ afterDrawing() {
553
+ if (!this.state.toolLocked) this.state.tool = 'selection';
554
+ }
555
+ onDown(event) {
556
+ if (this.state.text) this.finishText();
557
+ const hit = hitTest(this.scene, [
558
+ event.x,
559
+ event.y
560
+ ], this.threshold(), new Set(this.state.selected));
561
+ let grab = false;
562
+ if (hit) {
563
+ const ids = expandSelection(this.scene, [
564
+ hit.id
565
+ ]);
566
+ if (event.shift) {
567
+ const already = this.state.selected.includes(hit.id);
568
+ this.select(already ? this.state.selected.filter((id)=>!ids.includes(id)) : [
569
+ ...new Set([
570
+ ...this.state.selected,
571
+ ...ids
572
+ ])
573
+ ]);
574
+ this.shiftHandled.add(event.g);
575
+ } else if (!this.state.selected.includes(hit.id)) this.select(ids);
576
+ grab = this.state.editable && !hit.locked && this.state.selected.includes(hit.id);
577
+ } else if (!event.shift && this.state.selected.length) this.select([]);
578
+ this.options.send({
579
+ gesture: {
580
+ id: event.g,
581
+ grab
582
+ },
583
+ ...this.attached ? {
584
+ selection: this.selectionMessage()
585
+ } : {}
586
+ });
587
+ }
588
+ onTap(event) {
589
+ const point = [
590
+ event.x,
591
+ event.y
592
+ ];
593
+ const hit = hitTest(this.scene, point, this.threshold());
594
+ if ('text' === event.tool) {
595
+ this.beginText(hit && ('text' === hit.type || 'rectangle' === hit.type || 'ellipse' === hit.type || 'diamond' === hit.type) ? hit : null, point);
596
+ this.done(event.g);
597
+ return;
598
+ }
599
+ if ('eraser' === event.tool) {
600
+ if (hit) this.commit(deleteElements(this.scene, [
601
+ hit.id
602
+ ]));
603
+ this.done(event.g);
604
+ return;
605
+ }
606
+ if ('selection' !== event.tool) return void this.done(event.g);
607
+ if (2 === event.count && this.state.editable) {
608
+ var _hit_boundElements;
609
+ const target = hit ? 'text' === hit.type ? hit : (null == (_hit_boundElements = hit.boundElements) ? void 0 : _hit_boundElements.some((bound)=>'text' === bound.type)) || 'rectangle' === hit.type || 'ellipse' === hit.type || 'diamond' === hit.type ? hit : null : null;
610
+ if (target || !hit) this.beginText(target, point);
611
+ this.done(event.g);
612
+ return;
613
+ }
614
+ if (this.state.text) this.finishText();
615
+ const handled = this.shiftHandled.delete(event.g);
616
+ if (hit && event.shift && handled) ;
617
+ else if (hit) {
618
+ const ids = expandSelection(this.scene, [
619
+ hit.id
620
+ ]);
621
+ if (event.shift) {
622
+ const already = this.state.selected.includes(hit.id);
623
+ this.select(already ? this.state.selected.filter((id)=>!ids.includes(id)) : [
624
+ ...new Set([
625
+ ...this.state.selected,
626
+ ...ids
627
+ ])
628
+ ]);
629
+ } else this.select(ids);
630
+ } else if (!event.shift) this.select([]);
631
+ this.done(event.g);
632
+ }
633
+ done(g) {
634
+ this.pendingDone.push(g);
635
+ this.send({});
636
+ }
637
+ onMove(event) {
638
+ if (event.alt) {
639
+ const copy = duplicate(this.scene, this.state.selected, 0);
640
+ this.commit(copy.elements);
641
+ this.commit(moveElements(this.scene, copy.selected, event.dx, event.dy));
642
+ this.select(copy.selected);
643
+ } else {
644
+ this.commit(moveElements(this.scene, this.state.selected, event.dx, event.dy));
645
+ this.pendingDone.push(event.g);
646
+ this.send({
647
+ selection: this.selectionMessage()
648
+ });
649
+ return;
650
+ }
651
+ this.done(event.g);
652
+ }
653
+ onTransform(event) {
654
+ const elements = this.selectedElements();
655
+ if ('rotation' === event.handle) {
656
+ const pivot = [
657
+ (event.from[0] + event.from[2]) / 2,
658
+ (event.from[1] + event.from[3]) / 2
659
+ ];
660
+ this.commit(rotateElements(this.scene, this.state.selected, event.angle, pivot));
661
+ } else {
662
+ const angle = 1 === elements.length ? elements[0].angle || 0 : 0;
663
+ this.commit(resizeElements(this.scene, this.state.selected, event.from, event.to, angle));
664
+ }
665
+ this.pendingDone.push(event.g);
666
+ this.send({
667
+ selection: this.selectionMessage()
668
+ });
669
+ }
670
+ onBox(event) {
671
+ const ids = inBox(this.scene, event.b);
672
+ this.select(event.shift ? [
673
+ ...new Set([
674
+ ...this.state.selected,
675
+ ...ids
676
+ ])
677
+ ] : ids);
678
+ this.done(event.g);
679
+ }
680
+ onShape(event) {
681
+ const element = createShape(this.scene, event.tool, event.b, this.state.style);
682
+ this.commit([
683
+ element
684
+ ]);
685
+ this.afterDrawing();
686
+ this.pendingDone.push(event.g);
687
+ this.select([
688
+ element.id
689
+ ]);
690
+ }
691
+ onLine(event) {
692
+ const created = createLinear(this.scene, event.tool, event.points, this.state.style, this.view.zoom);
693
+ this.commit(created);
694
+ this.afterDrawing();
695
+ this.pendingDone.push(event.g);
696
+ this.select([
697
+ created[0].id
698
+ ]);
699
+ }
700
+ onStroke(event) {
701
+ const element = createFreeDraw(this.scene, event.points, event.pressures, this.state.style);
702
+ this.commit([
703
+ element
704
+ ]);
705
+ this.done(event.g);
706
+ }
707
+ onErase(event) {
708
+ const ids = erasedBy(this.scene, event.points, this.threshold());
709
+ if (ids.length) this.commit(deleteElements(this.scene, ids));
710
+ this.done(event.g);
711
+ }
712
+ onPoint(event) {
713
+ const id = this.state.selected[0];
714
+ if (id) this.commit(movePoint(this.scene, id, event.index, event.x, event.y, this.view.zoom));
715
+ this.pendingDone.push(event.g);
716
+ this.send({
717
+ selection: this.selectionMessage()
718
+ });
719
+ }
720
+ onCursor(x, y, down, gone) {
721
+ var _this_options_pointer, _this_options;
722
+ this.lastPointer = [
723
+ x,
724
+ y
725
+ ];
726
+ null == (_this_options_pointer = (_this_options = this.options).pointer) || _this_options_pointer.call(_this_options, x, y, down, this.state.selected, gone);
727
+ }
728
+ setEditable(editable) {
729
+ this.state.editable = editable;
730
+ if (!editable && this.state.text) this.finishText();
731
+ this.send({
732
+ selection: this.selectionMessage()
733
+ });
734
+ }
735
+ setStatus(status) {
736
+ this.state.status = status;
737
+ }
738
+ selectionBounds() {
739
+ const elements = this.selectedElements();
740
+ return elements.length ? commonBounds(elements) : null;
741
+ }
742
+ boundsOf(id) {
743
+ const element = this.scene.get(id);
744
+ return element ? elementBounds(element) : null;
745
+ }
746
+ constructor(options){
747
+ _define_property(this, "options", void 0);
748
+ _define_property(this, "state", void 0);
749
+ _define_property(this, "listeners", void 0);
750
+ _define_property(this, "items", void 0);
751
+ _define_property(this, "order", void 0);
752
+ _define_property(this, "files", void 0);
753
+ _define_property(this, "peers", void 0);
754
+ _define_property(this, "view", void 0);
755
+ _define_property(this, "attached", void 0);
756
+ _define_property(this, "fitted", void 0);
757
+ _define_property(this, "textBefore", void 0);
758
+ _define_property(this, "off", void 0);
759
+ _define_property(this, "pendingDone", void 0);
760
+ _define_property(this, "shiftHandled", void 0);
761
+ _define_property(this, "requested", void 0);
762
+ _define_property(this, "cameraTouched", void 0);
763
+ _define_property(this, "lastPointer", void 0);
764
+ this.options = options;
765
+ this.listeners = new Set();
766
+ this.items = new Map();
767
+ this.order = [];
768
+ this.files = {};
769
+ this.peers = [];
770
+ this.view = {
771
+ scrollX: 0,
772
+ scrollY: 0,
773
+ zoom: 1,
774
+ width: 0,
775
+ height: 0
776
+ };
777
+ this.attached = false;
778
+ this.fitted = false;
779
+ this.textBefore = null;
780
+ this.pendingDone = [];
781
+ this.shiftHandled = new Set();
782
+ this.requested = null;
783
+ this.cameraTouched = false;
784
+ this.lastPointer = [
785
+ 0,
786
+ 0
787
+ ];
788
+ this.state = observable(this.listeners, {
789
+ tool: 'selection',
790
+ toolLocked: false,
791
+ style: {
792
+ ...DEFAULT_STYLE
793
+ },
794
+ selected: [],
795
+ canUndo: false,
796
+ canRedo: false,
797
+ status: 'connecting',
798
+ peers: [],
799
+ text: null,
800
+ zoom: 1,
801
+ editable: false,
802
+ empty: true
803
+ });
804
+ this.off = options.scene.on((change)=>this.sceneChanged(change));
805
+ }
806
+ }
807
+ _define_property(BoardController, "FONTS", [
808
+ FONT_FAMILY.Excalifont,
809
+ FONT_FAMILY.Nunito,
810
+ FONT_FAMILY["Comic Shanns"]
811
+ ]);
812
+ export { BoardController };
813
+
814
+ //# sourceMappingURL=board-controller.js.map