Line data Source code
1 : import type {
2 : Size2D,
3 : CompactRect as Rect,
4 : Point2D as Vec2
5 : } from '@comfyorg/shared-frontend-utils/geometry'
6 : import { BakeRasterCommand, snapshotRaster } from '#/commands/bakeContent'
7 : import { AddNodeCommand, ReorderCommand } from '#/commands/structure'
8 : import type { Compositor, CompositeInput } from '#/compositor'
9 : import type { ContentStore } from '#/content'
10 : import type { DocGuide, Document } from '#/document'
11 : import { findNode } from '#/document'
12 : import { History } from '#/history'
13 : import { DefaultContentStore } from '#/impl/contentStore'
14 : import { defaultMode, resolveMode } from '#/mode'
15 : import type {
16 : ChannelData,
17 : GroupData,
18 : RasterData,
19 : SceneNode,
20 : Transform
21 : } from '#/node'
22 : import { getNodeKind } from '#/nodeKind'
23 : import {
24 : bakeMaskInto,
25 : bakePlaced,
26 : drawPlacedInto,
27 : isIdentityPlacement,
28 : placedBounds
29 : } from '#/render/bake'
30 : import { placeBitmap } from '#/render/place'
31 : import type { PlacedEntry } from '#/render/renderStack'
32 : import { renderDocument } from '#/render/renderStack'
33 : import type { Tool, ToolContext } from '#/tool'
34 : import { getTool } from '#/tool'
35 : import { addTransformBox } from '#/tools/overlayBox'
36 : import type { HandleId } from '#/tools/transformMath'
37 : import {
38 : angleTo,
39 : applyMove,
40 : applyResize,
41 : applyRotate,
42 : hitHandle,
43 : insideBox
44 : } from '#/tools/transformMath'
45 : import { isTransformTool } from '#/tools/transformTool'
46 : import { SetSelectionCommand, snapshotSelection } from '#/commands/selection'
47 : import { generateId } from '#/id'
48 :
49 : import { fullSelectionCanvas } from './selectionOps'
50 : import type { GrayMask, SelectionOp } from './selectionMath'
51 : import {
52 : combineMasks,
53 : emptyMask,
54 : maskBoundary,
55 : maskBounds,
56 : maskFromCanvas,
57 : maskToCanvas
58 : } from './selectionMath'
59 : import { OverlayList } from './overlayList'
60 :
61 : interface FloatingItem {
62 : contentId: string
63 : transform: Transform
64 : name?: string
65 : url?: string
66 : }
67 :
68 : type FloatSession =
69 : | { mode: 'idle' }
70 : | { mode: 'move'; start: Vec2; before: Transform }
71 : | { mode: 'resize'; handle: HandleId; before: Transform }
72 : | { mode: 'rotate'; before: Transform; grab: number }
73 :
74 : export interface EditorOptions {
75 : compositor: Compositor
76 : content?: ContentStore
77 : onChange?: () => void
78 : }
79 :
80 2 : function emptyDocument(width: number, height: number): Document {
81 : const root: GroupData = {
82 : kind: 'group',
83 : id: 'root',
84 : name: 'root',
85 : visible: true,
86 : opacity: 1,
87 : mode: defaultMode('normal'),
88 : transform: { x: 0, y: 0, w: width, h: height, rotation: 0 },
89 : locks: { content: false, position: false, visibility: false },
90 : children: [],
91 : passThrough: false
92 : }
93 2 : return { version: 2, width, height, root, channels: [] }
94 : }
95 :
96 : export interface Editor {
97 : readonly history: History
98 : readonly content: ContentStore
99 : readonly overlay: OverlayList
100 : document(): Document
101 : setTool(id: string): void
102 : activeToolId(): string
103 : transformApply(): boolean
104 : guides(): DocGuide[]
105 : activeNodeId(): string | null
106 : setActiveNode(id: string | null): void
107 : selectedNodeIds(): string[]
108 : setSelectedNodes(ids: string[]): void
109 : pointerDown(e: PointerEvent, pt: Vec2): void
110 : pointerMove(e: PointerEvent, pt: Vec2): void
111 : pointerUp(e: PointerEvent, pt: Vec2): void
112 : cursorAt(pt: Vec2): string
113 : addNode(node: SceneNode, index?: number, parentId?: string): void
114 : moveNode(id: string, dir: 1 | -1): boolean
115 : moveNodeTo(id: string, parentId: string | undefined, toIndex: number): boolean
116 : setZoom(z: number): void
117 : render(region?: Rect | null): void
118 : takePresentDamage(): { full: boolean; rect: Rect | null }
119 : buildOverlay(): void
120 : invalidate(): void
121 : undo(): void
122 : redo(): void
123 : floating(): FloatingItem | null
124 : startFloating(
125 : contentId: string,
126 : width: number,
127 : height: number,
128 : name?: string
129 : ): void
130 : anchorFloating(target?: 'active' | 'new'): void
131 : cancelFloating(): void
132 : selectionBounds(): Rect | null
133 : selectAll(): boolean
134 : selectNone(): boolean
135 : }
136 :
137 2 : export function createEditor(opts: EditorOptions): Editor {
138 2 : const compositor = opts.compositor
139 2 : const content = opts.content ?? new DefaultContentStore()
140 2 : const history = new History()
141 2 : const notify = opts.onChange ?? (() => {})
142 2 : const overlay = new OverlayList(() => notify())
143 :
144 2 : const doc = emptyDocument(1024, 1024)
145 2 : let toolId = 'select'
146 2 : let tool: Tool | null = null
147 2 : let selectedIds: string[] = []
148 2 : let zoomLevel = 1
149 2 : const snapGridSize = 0
150 :
151 2 : const placedCache = new Map<string, PlacedEntry>()
152 2 : let presentFull = true
153 2 : let presentRect: Rect | null = null
154 2 : let floating: FloatingItem | null = null
155 2 : let floatSession: FloatSession = { mode: 'idle' }
156 :
157 18 : function floatingInputs(): CompositeInput[] {
158 18 : if (!floating) return []
159 0 : const entry = content.get(floating.contentId)
160 0 : if (!entry) return []
161 0 : const canvas = placeBitmap(
162 : entry.canvas,
163 : floating.transform,
164 : doc.width,
165 : doc.height
166 : )
167 0 : if (!canvas) return []
168 0 : return [
169 : {
170 : texture: {
171 : source: canvas,
172 : rect: { x: 0, y: 0, w: doc.width, h: doc.height },
173 : linear: false
174 : },
175 : opacity: 1,
176 : mode: resolveMode(defaultMode('normal'))
177 : }
178 : ]
179 : }
180 18 : function render(region?: Rect | null): void {
181 18 : renderDocument(
182 : doc,
183 : { content, compositor, devicePixelRatio: 1, placedCache },
184 : floatingInputs(),
185 : region
186 : )
187 : }
188 22 : function selectionChannel(): ChannelData | null {
189 22 : if (!doc.selectionId) return null
190 22 : return (
191 : doc.channels.find(
192 : (ch) => ch.id === doc.selectionId && ch.role === 'selection'
193 : ) ?? null
194 : )
195 : }
196 :
197 2 : let selOutlineCache: { key: string; outlines: Vec2[][] } | null = null
198 0 : function selectionOutlines(sel: ChannelData): Vec2[][] {
199 0 : if (selOutlineCache?.key === sel.contentId) return selOutlineCache.outlines
200 0 : const entry = content.get(sel.contentId)
201 0 : const mask = entry ? maskFromCanvas(entry.canvas) : null
202 0 : const outlines = mask ? maskBoundary(mask) : []
203 0 : selOutlineCache = { key: sel.contentId, outlines }
204 0 : return outlines
205 : }
206 :
207 22 : function buildOverlay(): void {
208 22 : overlay.clear()
209 22 : for (const g of doc.guides ?? []) {
210 0 : if (g.axis === 'x') {
211 0 : overlay.add({
212 : type: 'line',
213 : a: { x: g.pos, y: 0 },
214 : b: { x: g.pos, y: doc.height }
215 : })
216 : } else {
217 0 : overlay.add({
218 : type: 'line',
219 : a: { x: 0, y: g.pos },
220 : b: { x: doc.width, y: g.pos }
221 : })
222 : }
223 : }
224 22 : const sel = selectionChannel()
225 22 : if (sel?.bounds) {
226 0 : const outlines = selectionOutlines(sel)
227 0 : if (outlines.length) {
228 0 : for (const points of outlines)
229 0 : overlay.add({ type: 'polyline', points, closed: true, ants: true })
230 : } else {
231 0 : overlay.add({ type: 'rect', rect: sel.bounds, ants: true })
232 : }
233 : }
234 22 : if (floating) {
235 0 : addTransformBox(overlay, floating.transform)
236 0 : return
237 : }
238 22 : tool?.drawOverlay(overlay)
239 : }
240 9 : function refresh(): void {
241 9 : presentFull = true
242 9 : presentRect = null
243 9 : render()
244 9 : buildOverlay()
245 9 : notify()
246 : }
247 44 : function liveSelectedIds(): string[] {
248 44 : return selectedIds.filter((id) => findNode(doc.root, id))
249 : }
250 11 : function activeNodeIdOf(): string | null {
251 11 : for (let i = selectedIds.length - 1; i >= 0; i--) {
252 9 : if (findNode(doc.root, selectedIds[i])) return selectedIds[i]
253 : }
254 2 : return null
255 : }
256 2 : function setSelected(ids: string[]): void {
257 2 : const seen = new Set<string>()
258 2 : const next: string[] = []
259 2 : for (const id of ids) {
260 2 : if (seen.has(id) || !findNode(doc.root, id)) continue
261 2 : seen.add(id)
262 2 : next.push(id)
263 : }
264 2 : if (
265 : next.length === selectedIds.length &&
266 : next.every((id, i) => id === selectedIds[i])
267 : )
268 2 : return
269 0 : selectedIds = next
270 0 : buildOverlay()
271 0 : notify()
272 : }
273 2 : function setActive(id: string | null): void {
274 2 : setSelected(id ? [id] : [])
275 : }
276 13 : function collectGarbage(): void {
277 13 : const live = new Set<string>()
278 13 : for (const id of getNodeKind(doc.root.kind).contentIds(doc.root))
279 16 : live.add(id)
280 13 : for (const ch of doc.channels) live.add(ch.contentId)
281 13 : for (const id of history.contentRefs()) live.add(id)
282 13 : if (floating) live.add(floating.contentId)
283 13 : content.collectGarbage(live)
284 : }
285 2 : history.onChange(collectGarbage)
286 :
287 2 : const ctx: ToolContext = {
288 : document: () => doc,
289 : history,
290 : compositor,
291 : content,
292 : overlay,
293 : activeNodeId: activeNodeIdOf,
294 : setActiveNode: setActive,
295 : selectedNodeIds: liveSelectedIds,
296 : setSelectedNodes: setSelected,
297 : selection: {
298 : combineShape: (label, mask, op) => {
299 0 : combineSelectionMask(label, mask, op)
300 : },
301 : currentMask: currentSelectionMask,
302 : none: () => {
303 0 : commitSelection('Select None', null, null)
304 : }
305 : },
306 : floatSelection: () => false,
307 : compositePixels: () => {
308 0 : render()
309 0 : const img = compositor.readback()
310 0 : if (img.width !== doc.width || img.height !== doc.height) return null
311 0 : return img
312 : },
313 : zoom: () => zoomLevel,
314 : snapGrid: () => snapGridSize,
315 : requestRender: refresh
316 : }
317 :
318 4 : function makeTool(): void {
319 4 : tool?.onDeactivate?.()
320 4 : tool = getTool(toolId).create(ctx)
321 4 : tool.onActivate?.()
322 : }
323 2 : makeTool()
324 :
325 0 : function activeLocation(): {
326 : parent: GroupData
327 : node: SceneNode
328 : index: number
329 : } | null {
330 0 : const id = activeNodeIdOf()
331 0 : if (!id) return null
332 0 : return findNode(doc.root, id)
333 : }
334 :
335 0 : function activeRaster(): RasterData | null {
336 0 : const loc = activeLocation()
337 0 : return loc && loc.node.kind === 'raster' ? loc.node : null
338 : }
339 :
340 0 : function currentSelectionMask(): GrayMask | null {
341 0 : const sel = selectionChannel()
342 0 : if (!sel) return null
343 0 : const entry = content.get(sel.contentId)
344 0 : if (!entry) return null
345 0 : return maskFromCanvas(entry.canvas)
346 : }
347 :
348 0 : function combineSelectionMask(
349 : label: string,
350 : shapeMask: GrayMask,
351 : op: SelectionOp
352 : ): boolean {
353 0 : let result = shapeMask
354 0 : if (op !== 'replace') {
355 : const base = currentSelectionMask() ?? emptyMask(doc.width, doc.height)
356 0 : result = combineMasks(base, shapeMask, op)
357 : }
358 0 : const bounds = maskBounds(result)
359 0 : if (!bounds) return commitSelection(label, null, null)
360 0 : const canvas = maskToCanvas(result)
361 0 : if (!canvas) return false
362 0 : return commitSelection(label, canvas, bounds)
363 : }
364 :
365 0 : function commitSelection(
366 : label: string,
367 : canvas: HTMLCanvasElement | null,
368 : bounds: Rect | null
369 : ): boolean {
370 0 : const before = snapshotSelection(doc)
371 0 : doc.channels = doc.channels.filter((ch) => ch.role !== 'selection')
372 0 : if (canvas && bounds) {
373 0 : const channel: ChannelData = {
374 : id: generateId('sel'),
375 : role: 'selection',
376 : contentId: content.register(canvas),
377 : enabled: true,
378 : bounds
379 : }
380 0 : doc.channels.push(channel)
381 0 : doc.selectionId = channel.id
382 : } else {
383 0 : doc.selectionId = undefined
384 0 : if (!before.channel) return false
385 : }
386 0 : history.push(
387 : new SetSelectionCommand(
388 : label,
389 : doc,
390 : before,
391 : snapshotSelection(doc),
392 : content
393 : )
394 : )
395 0 : refresh()
396 0 : return true
397 : }
398 :
399 6 : function addNodeInternal(
400 : node: SceneNode,
401 : index?: number,
402 : parent?: GroupData
403 : ): void {
404 6 : const into = parent ?? doc.root
405 6 : const at = index ?? into.children.length
406 6 : into.children.splice(at, 0, node)
407 6 : history.push(new AddNodeCommand(`Add ${node.name}`, into, node, at))
408 6 : selectedIds = [node.id]
409 6 : refresh()
410 : }
411 :
412 0 : function anchorInto(
413 : node: RasterData,
414 : item: FloatingItem,
415 : floatCanvas: HTMLCanvasElement
416 : ): boolean {
417 0 : const targetEntry = content.get(node.contentId)
418 0 : if (!targetEntry) return false
419 0 : const fb = placedBounds(item.transform)
420 0 : const tb = placedBounds(node.transform)
421 0 : const ux = Math.min(tb.x, fb.x)
422 0 : const uy = Math.min(tb.y, fb.y)
423 0 : const uw = Math.max(tb.x + tb.w, fb.x + fb.w) - ux
424 0 : const uh = Math.max(tb.y + tb.h, fb.y + fb.h) - uy
425 0 : if (uw > 16384 || uh > 16384) return false
426 0 : const oldTransform = { ...node.transform }
427 0 : const canvas = document.createElement('canvas')
428 0 : canvas.width = uw
429 0 : canvas.height = uh
430 0 : const ctx = canvas.getContext('2d')
431 0 : if (!ctx) return false
432 0 : ctx.imageSmoothingEnabled = true
433 0 : ctx.imageSmoothingQuality = 'high'
434 0 : drawPlacedInto(ctx, targetEntry.canvas, node.transform, ux, uy)
435 0 : drawPlacedInto(ctx, floatCanvas, item.transform, ux, uy)
436 0 : const before = snapshotRaster(node)
437 0 : node.contentId = content.register(canvas)
438 0 : node.url = undefined
439 0 : node.naturalWidth = uw
440 0 : node.naturalHeight = uh
441 0 : node.transform = { x: ux, y: uy, w: uw, h: uh, rotation: 0 }
442 0 : if (node.mask) {
443 0 : const maskEntry = content.get(node.mask.contentId)
444 0 : const bakedMask = maskEntry
445 : ? bakeMaskInto(
446 : maskEntry.canvas,
447 : oldTransform,
448 : { x: ux, y: uy, w: uw, h: uh },
449 : 'white'
450 : )
451 : : null
452 0 : if (bakedMask) {
453 0 : node.mask = {
454 : ...node.mask,
455 : contentId: content.register(bakedMask),
456 : url: undefined
457 : }
458 : }
459 : }
460 0 : history.push(
461 : new BakeRasterCommand(
462 : 'Anchor',
463 : node,
464 : before,
465 : snapshotRaster(node),
466 : content
467 : )
468 : )
469 0 : return true
470 : }
471 :
472 0 : function anchorAsNewLayer(
473 : item: FloatingItem,
474 : entry: Size2D & {
475 : canvas: HTMLCanvasElement
476 : uploadedUrl: string | null
477 : }
478 : ): void {
479 0 : const kind = getNodeKind('raster')
480 0 : if (isIdentityPlacement(item.transform, entry.width, entry.height)) {
481 0 : addNodeInternal(
482 : kind.create({
483 : name: item.name ?? 'Layer',
484 : contentId: item.contentId,
485 : url: entry.uploadedUrl ?? undefined,
486 : naturalWidth: entry.width,
487 : naturalHeight: entry.height,
488 : transform: { ...item.transform }
489 : } as Partial<RasterData>) as SceneNode
490 : )
491 0 : return
492 : }
493 0 : const baked = bakePlaced(entry.canvas, item.transform)
494 0 : if (!baked) {
495 0 : addNodeInternal(
496 : kind.create({
497 : name: item.name ?? 'Layer',
498 : contentId: item.contentId,
499 : url: entry.uploadedUrl ?? undefined,
500 : naturalWidth: entry.width,
501 : naturalHeight: entry.height,
502 : transform: { ...item.transform }
503 : } as Partial<RasterData>) as SceneNode
504 : )
505 0 : return
506 : }
507 0 : const cid = content.register(baked.canvas)
508 0 : addNodeInternal(
509 : kind.create({
510 : name: item.name ?? 'Layer',
511 : contentId: cid,
512 : naturalWidth: baked.bounds.w,
513 : naturalHeight: baked.bounds.h,
514 : transform: {
515 : x: baked.bounds.x,
516 : y: baked.bounds.y,
517 : w: baked.bounds.w,
518 : h: baked.bounds.h,
519 : rotation: 0
520 : }
521 : } as Partial<RasterData>) as SceneNode
522 : )
523 : }
524 :
525 0 : function anchorFloatingImpl(target?: 'active' | 'new'): void {
526 0 : if (!floating) return
527 0 : const item = floating
528 0 : const entry = content.get(item.contentId)
529 0 : if (!entry) {
530 0 : floating = null
531 0 : floatSession = { mode: 'idle' }
532 0 : refresh()
533 0 : return
534 : }
535 : const mode: 'active' | 'new' = target ?? (activeRaster() ? 'active' : 'new')
536 0 : if (mode === 'active') {
537 0 : const node = activeRaster()
538 0 : if (node && !node.locks.content && anchorInto(node, item, entry.canvas)) {
539 0 : floating = null
540 0 : floatSession = { mode: 'idle' }
541 0 : refresh()
542 0 : return
543 : }
544 : }
545 0 : floating = null
546 0 : floatSession = { mode: 'idle' }
547 0 : anchorAsNewLayer(item, entry)
548 : }
549 :
550 0 : function floatingPress(pt: Vec2): void {
551 0 : if (!floating) return
552 0 : const t = floating.transform
553 : const tol = 8 / Math.max(1e-3, zoomLevel)
554 0 : const h = hitHandle(t, pt, tol)
555 0 : if (h === 'rotate') {
556 0 : floatSession = { mode: 'rotate', before: { ...t }, grab: angleTo(t, pt) }
557 0 : return
558 : }
559 0 : if (h) {
560 0 : floatSession = { mode: 'resize', handle: h, before: { ...t } }
561 0 : return
562 : }
563 0 : if (insideBox(t, pt)) {
564 0 : floatSession = { mode: 'move', start: pt, before: { ...t } }
565 0 : return
566 : }
567 0 : anchorFloatingImpl()
568 : }
569 :
570 0 : function floatingMotion(e: PointerEvent, pt: Vec2): void {
571 0 : if (!floating || floatSession.mode === 'idle') return
572 0 : const s = floatSession
573 0 : if (s.mode === 'move') {
574 0 : floating.transform = applyMove(
575 : s.before,
576 : pt.x - s.start.x,
577 : pt.y - s.start.y
578 : )
579 0 : } else if (s.mode === 'resize') {
580 0 : floating.transform = applyResize(s.before, s.handle, pt, 1, e.shiftKey)
581 : } else {
582 0 : floating.transform = applyRotate(
583 : s.before,
584 : s.before.rotation,
585 : s.grab,
586 : pt,
587 : e.shiftKey ? Math.PI / 12 : 0
588 : )
589 : }
590 0 : refresh()
591 : }
592 :
593 2 : return {
594 : history,
595 : content,
596 : overlay,
597 : document: () => doc,
598 : setTool(id) {
599 2 : toolId = id
600 2 : makeTool()
601 2 : buildOverlay()
602 2 : notify()
603 : },
604 : activeToolId: () => toolId,
605 : transformApply: () => (isTransformTool(tool) ? tool.apply() : false),
606 : activeNodeId: activeNodeIdOf,
607 : setActiveNode: setActive,
608 : selectedNodeIds: liveSelectedIds,
609 : setSelectedNodes: setSelected,
610 : pointerDown(e, pt) {
611 0 : if (floating) {
612 0 : floatingPress(pt)
613 0 : return
614 : }
615 0 : tool?.onButtonPress(e, pt)
616 : },
617 : pointerMove(e, pt) {
618 0 : if (floating) {
619 0 : floatingMotion(e, pt)
620 0 : return
621 : }
622 0 : tool?.onMotion(e, pt)
623 : },
624 : pointerUp(e, pt) {
625 0 : if (floating) {
626 0 : floatSession = { mode: 'idle' }
627 0 : return
628 : }
629 0 : tool?.onButtonRelease(e, pt)
630 : },
631 : cursorAt(pt) {
632 0 : if (floating) return 'default'
633 0 : return tool?.cursorFor(pt) ?? 'default'
634 : },
635 : addNode(node, index, parentId) {
636 6 : const found =
637 : parentId && parentId !== doc.root.id
638 : ? findNode(doc.root, parentId)?.node
639 : : undefined
640 : const parent = found?.kind === 'group' ? found : undefined
641 6 : addNodeInternal(node, index, parent)
642 : },
643 : moveNode(id, dir) {
644 1 : const loc = findNode(doc.root, id)
645 1 : if (!loc) return false
646 1 : const { parent, node, index } = loc
647 1 : const siblingIndex = index + dir
648 1 : const sib =
649 : siblingIndex < 0 ? undefined : parent.children.at(siblingIndex)
650 1 : let toParent: GroupData
651 1 : let toIndex: number
652 1 : if (sib && sib.kind === 'group') {
653 0 : toParent = sib
654 0 : toIndex = dir === 1 ? 0 : toParent.children.length
655 1 : } else if (sib) {
656 1 : toParent = parent
657 1 : toIndex = index + dir
658 0 : } else if (parent !== doc.root) {
659 0 : const ploc = findNode(doc.root, parent.id)
660 0 : if (!ploc) return false
661 0 : toParent = ploc.parent
662 0 : toIndex = dir === 1 ? ploc.index + 1 : ploc.index
663 : } else {
664 0 : return false
665 : }
666 1 : parent.children.splice(index, 1)
667 1 : const to = Math.max(0, Math.min(toIndex, toParent.children.length))
668 1 : toParent.children.splice(to, 0, node)
669 1 : history.push(
670 : new ReorderCommand('Reorder', node, parent, index, toParent, to)
671 : )
672 1 : refresh()
673 1 : return true
674 : },
675 : moveNodeTo(id, parentId, toIndex) {
676 0 : const loc = findNode(doc.root, id)
677 0 : if (!loc) return false
678 0 : const target =
679 : parentId && parentId !== doc.root.id
680 : ? findNode(doc.root, parentId)?.node
681 : : doc.root
682 0 : if (!target || target.kind !== 'group') return false
683 0 : const toParent = target
684 0 : if (loc.node.kind === 'group') {
685 0 : if (toParent.id === loc.node.id) return false
686 0 : if (findNode(loc.node, toParent.id)) return false
687 : }
688 0 : let to = Math.max(0, Math.min(toIndex, toParent.children.length))
689 0 : loc.parent.children.splice(loc.index, 1)
690 0 : if (toParent === loc.parent && loc.index < to) to -= 1
691 0 : to = Math.max(0, Math.min(to, toParent.children.length))
692 0 : if (toParent === loc.parent && to === loc.index) {
693 0 : loc.parent.children.splice(loc.index, 0, loc.node)
694 0 : return false
695 : }
696 0 : toParent.children.splice(to, 0, loc.node)
697 0 : history.push(
698 : new ReorderCommand(
699 : 'Reorder',
700 : loc.node,
701 : loc.parent,
702 : loc.index,
703 : toParent,
704 : to
705 : )
706 : )
707 0 : refresh()
708 0 : return true
709 : },
710 : setZoom(z) {
711 11 : zoomLevel = z
712 : },
713 : guides: () => (doc.guides ?? []).map((g) => ({ ...g })),
714 : render,
715 : takePresentDamage() {
716 11 : const dmg = { full: presentFull, rect: presentRect }
717 11 : presentFull = false
718 11 : presentRect = null
719 11 : return dmg
720 : },
721 : buildOverlay,
722 : invalidate: refresh,
723 : undo() {
724 1 : history.undo()
725 1 : refresh()
726 : },
727 : redo() {
728 1 : history.redo()
729 1 : refresh()
730 : },
731 : selectionBounds() {
732 0 : return selectionChannel()?.bounds ?? null
733 : },
734 : selectAll() {
735 0 : const rect: Rect = { x: 0, y: 0, w: doc.width, h: doc.height }
736 0 : return commitSelection(
737 : 'Select All',
738 : fullSelectionCanvas(doc.width, doc.height),
739 : rect
740 : )
741 : },
742 : selectNone() {
743 0 : return commitSelection('Select None', null, null)
744 : },
745 : floating: () => floating,
746 : startFloating(contentId, width, height, name) {
747 0 : if (floating) anchorFloatingImpl()
748 0 : const sel = selectionChannel()
749 0 : const target = sel?.bounds ?? { x: 0, y: 0, w: doc.width, h: doc.height }
750 0 : if (sel) commitSelection('Select None', null, null)
751 0 : const x = Math.round(target.x + (target.w - width) / 2)
752 0 : const y = Math.round(target.y + (target.h - height) / 2)
753 0 : floating = {
754 : contentId,
755 : name,
756 : transform: {
757 : x:
758 : width <= doc.width
759 : ? Math.max(0, Math.min(x, doc.width - width))
760 : : x,
761 : y:
762 : height <= doc.height
763 : ? Math.max(0, Math.min(y, doc.height - height))
764 : : y,
765 : w: width,
766 : h: height,
767 : rotation: 0
768 : }
769 : }
770 0 : floatSession = { mode: 'idle' }
771 0 : refresh()
772 : },
773 : anchorFloating(target) {
774 0 : anchorFloatingImpl(target)
775 : },
776 : cancelFloating() {
777 0 : if (!floating) return
778 0 : floating = null
779 0 : floatSession = { mode: 'idle' }
780 0 : collectGarbage()
781 0 : refresh()
782 : }
783 : }
784 : }
|