Line data Source code
1 : import type { ContentStore } from '#/content'
2 : import type { Command, Direction } from '#/history'
3 : import { Dirty } from '#/history'
4 : import type { ChannelData, RasterData, Transform } from '#/node'
5 :
6 : export interface RasterSnapshot {
7 : contentId: string
8 : url?: string
9 : naturalWidth: number
10 : naturalHeight: number
11 : transform: Transform
12 : mask?: ChannelData
13 : }
14 :
15 0 : export function snapshotRaster(node: RasterData): RasterSnapshot {
16 0 : return {
17 : contentId: node.contentId,
18 : url: node.url,
19 : naturalWidth: node.naturalWidth,
20 : naturalHeight: node.naturalHeight,
21 : transform: { ...node.transform },
22 : mask: node.mask ? { ...node.mask } : undefined
23 : }
24 : }
25 :
26 2 : export class BakeRasterCommand implements Command {
27 : readonly dirtyMask = Dirty.DRAWABLE
28 :
29 : constructor(
30 0 : readonly label: string,
31 0 : private readonly node: RasterData,
32 0 : private readonly before: RasterSnapshot,
33 0 : private readonly after: RasterSnapshot,
34 0 : private readonly store: ContentStore
35 : ) {}
36 :
37 : apply(dir: Direction): void {
38 0 : const s = dir === 'undo' ? this.before : this.after
39 0 : this.node.contentId = s.contentId
40 0 : this.node.url = s.url
41 0 : this.node.naturalWidth = s.naturalWidth
42 0 : this.node.naturalHeight = s.naturalHeight
43 0 : this.node.transform = { ...s.transform }
44 0 : this.node.mask = s.mask ? { ...s.mask } : undefined
45 : }
46 :
47 : sizeBytes(): number {
48 0 : let n = 0
49 0 : const ids = [this.before.contentId, this.after.contentId]
50 0 : if (this.before.mask) ids.push(this.before.mask.contentId)
51 0 : if (this.after.mask) ids.push(this.after.mask.contentId)
52 0 : for (const id of ids) {
53 0 : const e = this.store.get(id)
54 0 : if (e) n += e.width * e.height * 4
55 : }
56 0 : return n
57 : }
58 :
59 : contentRefs(): string[] {
60 0 : const refs = [this.before.contentId, this.after.contentId]
61 0 : if (this.before.mask) refs.push(this.before.mask.contentId)
62 0 : if (this.after.mask) refs.push(this.after.mask.contentId)
63 0 : return refs.filter(Boolean)
64 : }
65 : }
|