@pairdraw/scene-graph 0.19.1 → 0.19.2

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 (89) hide show
  1. package/dist/behaviours/index.d.ts +2 -2
  2. package/dist/behaviours/index.js +2 -2
  3. package/dist/behaviours/spec.d.ts +6 -1
  4. package/dist/behaviours/spec.d.ts.map +1 -1
  5. package/dist/behaviours/spec.js +62 -5
  6. package/dist/behaviours/spec.js.map +1 -1
  7. package/dist/canvas-layout.js +66 -0
  8. package/dist/canvas-layout.js.map +1 -0
  9. package/dist/coordinate.js +1 -1
  10. package/dist/css/index.d.ts +2 -2
  11. package/dist/css/index.js +2 -2
  12. package/dist/css/text.d.ts +6 -1
  13. package/dist/css/text.d.ts.map +1 -1
  14. package/dist/css/text.js +20 -1
  15. package/dist/css/text.js.map +1 -1
  16. package/dist/generated/index.d.ts +2 -2
  17. package/dist/generated/index.js +2 -2
  18. package/dist/generated/shape-with-text.d.ts +7 -2
  19. package/dist/generated/shape-with-text.d.ts.map +1 -1
  20. package/dist/generated/shape-with-text.js +20 -3
  21. package/dist/generated/shape-with-text.js.map +1 -1
  22. package/dist/graph.d.ts.map +1 -1
  23. package/dist/graph.js +11 -5
  24. package/dist/graph.js.map +1 -1
  25. package/dist/group-bounds.d.ts +4 -1
  26. package/dist/group-bounds.d.ts.map +1 -1
  27. package/dist/group-bounds.js +11 -1
  28. package/dist/group-bounds.js.map +1 -1
  29. package/dist/hit-test.d.ts.map +1 -1
  30. package/dist/hit-test.js +47 -24
  31. package/dist/hit-test.js.map +1 -1
  32. package/dist/index.d.ts +2 -2
  33. package/dist/node-defaults.d.ts +21 -1
  34. package/dist/node-defaults.d.ts.map +1 -1
  35. package/dist/node-defaults.js +22 -14
  36. package/dist/node-defaults.js.map +1 -1
  37. package/dist/resize.d.ts.map +1 -1
  38. package/dist/resize.js +3 -6
  39. package/dist/resize.js.map +1 -1
  40. package/dist/scaling/node.d.ts.map +1 -1
  41. package/dist/scaling/node.js +23 -3
  42. package/dist/scaling/node.js.map +1 -1
  43. package/dist/shared-styles.d.ts +3 -1
  44. package/dist/shared-styles.d.ts.map +1 -1
  45. package/dist/shared-styles.js +7 -1
  46. package/dist/shared-styles.js.map +1 -1
  47. package/dist/text/path.js +11 -0
  48. package/dist/text/path.js.map +1 -0
  49. package/dist/text/picture.d.ts.map +1 -1
  50. package/dist/text/picture.js +5 -5
  51. package/dist/text/picture.js.map +1 -1
  52. package/dist/transfer/copied.d.ts +14 -0
  53. package/dist/transfer/copied.d.ts.map +1 -0
  54. package/dist/transfer/copied.js +52 -0
  55. package/dist/transfer/copied.js.map +1 -0
  56. package/dist/transfer/index.d.ts +2 -1
  57. package/dist/transfer/index.js +2 -1
  58. package/dist/transfer/nodes.js +7 -0
  59. package/dist/transfer/nodes.js.map +1 -1
  60. package/dist/types.d.ts +3 -2
  61. package/dist/types.d.ts.map +1 -1
  62. package/dist/types.js.map +1 -1
  63. package/dist/undo.d.ts +6 -0
  64. package/dist/undo.d.ts.map +1 -1
  65. package/dist/undo.js +13 -0
  66. package/dist/undo.js.map +1 -1
  67. package/dist/variables/index.d.ts.map +1 -1
  68. package/dist/variables/index.js.map +1 -1
  69. package/package.json +1 -1
  70. package/src/behaviours/spec.ts +94 -4
  71. package/src/canvas-layout.ts +76 -0
  72. package/src/css/index.ts +6 -1
  73. package/src/css/text.ts +24 -0
  74. package/src/generated/shape-with-text.ts +28 -2
  75. package/src/graph.ts +14 -4
  76. package/src/group-bounds.ts +13 -0
  77. package/src/hit-test.ts +95 -39
  78. package/src/node-defaults.ts +22 -13
  79. package/src/resize.ts +3 -14
  80. package/src/scaling/node.ts +27 -3
  81. package/src/shared-styles.ts +8 -0
  82. package/src/text/path.ts +14 -0
  83. package/src/text/picture.ts +5 -2
  84. package/src/transfer/copied.ts +72 -0
  85. package/src/transfer/index.ts +1 -0
  86. package/src/transfer/nodes.ts +10 -0
  87. package/src/types.ts +2 -1
  88. package/src/undo.ts +11 -0
  89. package/src/variables/index.ts +1 -4
@@ -0,0 +1,72 @@
1
+ import { isEqual, mapValues } from 'es-toolkit'
2
+
3
+ import { componentPropertyDefinitions } from '../components/properties'
4
+ import type { SceneGraph } from '../graph'
5
+ import { remapInstanceOverrideState } from '../instances/overrides'
6
+ import type { ComponentPropertyDefinition, SceneNode } from '../types'
7
+
8
+ function swapPropertyIds(definitions: readonly ComponentPropertyDefinition[]): string[] {
9
+ return definitions
10
+ .filter((definition) => definition.type === 'INSTANCE_SWAP')
11
+ .map((definition) => definition.id)
12
+ }
13
+
14
+ function remappedReferences(
15
+ node: SceneNode,
16
+ mapped: (id: string) => string,
17
+ swapProperties: ReadonlySet<string>
18
+ ): Partial<SceneNode> {
19
+ const changes: Partial<SceneNode> = {}
20
+ if (node.componentId && mapped(node.componentId) !== node.componentId)
21
+ changes.componentId = mapped(node.componentId)
22
+ // Only what a mapping changes: every write is an update collaboration sends.
23
+ const overrides = remapInstanceOverrideState(node.instanceOverrides, {
24
+ node: mapped,
25
+ variable: (id) => id
26
+ })
27
+ if (!isEqual(overrides, node.instanceOverrides)) changes.instanceOverrides = overrides
28
+ const definitions = node.componentPropertyDefinitions.map((definition) =>
29
+ definition.type === 'INSTANCE_SWAP' && definition.defaultValue
30
+ ? { ...definition, defaultValue: mapped(definition.defaultValue) }
31
+ : definition
32
+ )
33
+ if (!isEqual(definitions, node.componentPropertyDefinitions))
34
+ changes.componentPropertyDefinitions = definitions
35
+ const assignments = mapValues(node.componentPropertyAssignments, (value, id) =>
36
+ swapProperties.has(id) ? mapped(value) : value
37
+ )
38
+ if (!isEqual(assignments, node.componentPropertyAssignments))
39
+ changes.componentPropertyAssignments = assignments
40
+ for (const field of ['connectorStart', 'connectorEnd'] as const) {
41
+ const endpoint = node[field]
42
+ if (endpoint?.nodeId && mapped(endpoint.nodeId) !== endpoint.nodeId)
43
+ changes[field] = { ...endpoint, nodeId: mapped(endpoint.nodeId) }
44
+ }
45
+ return changes
46
+ }
47
+
48
+ /**
49
+ * Points copied layers at one another, as `prepareNodeTransfer` does for a paste: an instance's
50
+ * main component, the layers its overrides change and the components they swap in, the
51
+ * components an instance-swap property defaults to or is set to, and the layers a connector's
52
+ * ends follow. `mappedIds` maps each copied layer to its copy, such as a library copy or a design
53
+ * JSX render folded into the layers it replaces; a reference outside the copy stays as it is, and
54
+ * so do variable IDs, since the copies keep their source's variables.
55
+ */
56
+ export function remapCopiedReferences(
57
+ graph: SceneGraph,
58
+ mappedIds: ReadonlyMap<string, string>
59
+ ): void {
60
+ const mapped = (id: string) => mappedIds.get(id) ?? id
61
+ const copies = [...mappedIds.values()].flatMap((id) => graph.getNode(id) ?? [])
62
+ const copiedSwaps = copies.flatMap((node) => swapPropertyIds(node.componentPropertyDefinitions))
63
+ for (const node of copies) {
64
+ // An instance's swap property may be defined on a component the copy leaves behind.
65
+ const swapProperties = new Set([
66
+ ...copiedSwaps,
67
+ ...swapPropertyIds(componentPropertyDefinitions(graph, node))
68
+ ])
69
+ const changes = remappedReferences(node, mapped, swapProperties)
70
+ if (Object.keys(changes).length > 0) graph.updateNode(node.id, changes)
71
+ }
72
+ }
@@ -4,3 +4,4 @@ export { applyGraphTransfer, type GraphTransferPlan } from './apply'
4
4
  export { prepareGraphTransfer, type GraphTransferInput } from './plan'
5
5
  export { prepareNodeTransfer, type NodeTransferReferences } from './nodes'
6
6
  export { prepareVariableTransfer, type VariableTransferReferences } from './variables'
7
+ export { remapCopiedReferences } from './copied'
@@ -104,6 +104,16 @@ export function prepareNodeTransfer(
104
104
  node: (id) => refs.nodes.get(id) ?? id
105
105
  })
106
106
  }
107
+ // A reaction's destination keeps its name when the transfer leaves it behind; a copied frame
108
+ // with a copy of its destination points at the copy, as Figma's paste does.
109
+ result.prototypeReactions = (result.prototypeReactions ?? []).map((reaction) => ({
110
+ ...reaction,
111
+ actions: reaction.actions.map((action) =>
112
+ action.type === 'NODE' && action.nodeId
113
+ ? { ...action, nodeId: refs.nodes.get(action.nodeId) ?? action.nodeId }
114
+ : action
115
+ )
116
+ }))
107
117
  // A connector keeps the layers it was attached to when they are left behind; the end becomes
108
118
  // free where nothing by that ID exists, as a pasted Figma connector does.
109
119
  for (const field of ['connectorStart', 'connectorEnd'] as const) {
package/src/types.ts CHANGED
@@ -245,6 +245,7 @@ export type StrokeJoin = 'MITER' | 'BEVEL' | 'ROUND'
245
245
  export type SharedStyleType = 'FILL' | 'TEXT' | 'EFFECT' | 'GRID'
246
246
  export type SharedStyleKind = 'fill' | 'stroke' | 'text' | 'effect' | 'grid'
247
247
  export type MaskType = 'ALPHA' | 'VECTOR' | 'LUMINANCE'
248
+ export type BooleanOperation = 'UNION' | 'SUBTRACT' | 'INTERSECT' | 'EXCLUDE'
248
249
 
249
250
  export interface LayoutGrid {
250
251
  visible?: boolean
@@ -850,7 +851,7 @@ export interface SceneNode {
850
851
 
851
852
  vectorNetwork: VectorNetwork | null
852
853
  handleMirroring: HandleMirroring
853
- booleanOperation: 'UNION' | 'SUBTRACT' | 'INTERSECT' | 'EXCLUDE' | undefined
854
+ booleanOperation: BooleanOperation | undefined
854
855
  fillGeometry: GeometryPath[]
855
856
  strokeGeometry: GeometryPath[]
856
857
 
package/src/undo.ts CHANGED
@@ -214,6 +214,17 @@ export class UndoManager {
214
214
  return history
215
215
  }
216
216
 
217
+ /**
218
+ * Adds a step to a history kept aside while another graph is open, and returns it. The step's
219
+ * marks stay in its own graph, which is not the open one, and nothing is announced: the open
220
+ * document's history does not change.
221
+ */
222
+ pushIntoParked(history: UndoHistory, entry: UndoEntry): UndoHistory {
223
+ const undo = [...history.undo, entry]
224
+ const overflow = Number.isFinite(this.limit) && this.limit > 0 ? undo.length - this.limit : 0
225
+ return { undo: overflow > 0 ? undo.slice(overflow) : undo, redo: [] }
226
+ }
227
+
217
228
  /** Makes `history`, recorded on the graph now open, the committed one. */
218
229
  adoptHistory(history: UndoHistory | undefined): void {
219
230
  this.undoStack = history ? [...history.undo] : []
@@ -289,10 +289,7 @@ export function modesDefaultFirst(collection: VariableCollection): VariableColle
289
289
  * The collection's own array keeps the file's saved order, which an export writes back as it was.
290
290
  */
291
291
  export function modesInListedOrder(collection: VariableCollection): VariableCollectionMode[] {
292
- return sortBy(collection.modes, [
293
- (mode) => !mode.sortPosition,
294
- (mode) => mode.sortPosition ?? ''
295
- ])
292
+ return sortBy(collection.modes, [(mode) => !mode.sortPosition, (mode) => mode.sortPosition ?? ''])
296
293
  }
297
294
 
298
295
  export function getVariablesForCollection(graph: SceneGraph, collectionId: string): Variable[] {