/
SymbolMaster.js
183 lines (167 loc) · 5.03 KB
/
SymbolMaster.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import { toArray } from 'util'
import { DefinedPropertiesKey } from '../WrappedObject'
import { Artboard } from './Artboard'
import { Rectangle } from '../models/Rectangle'
import { Types } from '../enums'
import { Factory } from '../Factory'
import { wrapObject } from '../wrapNativeObject'
import { Override } from '../models/Override'
/**
* A Sketch symbol master.
*/
export class SymbolMaster extends Artboard {
/**
* Make a new symbol master.
*/
constructor(master = {}) {
if (!master.sketchObject) {
// eslint-disable-next-line no-param-reassign
master.sketchObject = Factory.createNative(SymbolMaster)
.alloc()
.initWithFrame(new Rectangle(0, 0, 100, 100).asCGRect())
}
super(master)
}
// Replace the artboard with a symbol master
static fromArtboard(artboard) {
const wrappedArtboard = wrapObject(artboard)
return SymbolMaster.fromNative(
MSSymbolMaster.convertArtboardToSymbol(wrappedArtboard.sketchObject)
)
}
// Replace the symbol with an artboard and detach all its instances converting them into groups.
toArtboard() {
const artboard = MSSymbolMaster.convertSymbolToArtboard(this._object)
return Artboard.fromNative(artboard)
}
// Returns a new SymbolInstance linked to this artboard, ready for inserting in the document
createNewInstance() {
return wrapObject(this._object.newSymbolInstance())
}
// Returns all instances of the artboard in the document, on all pages
getAllInstances() {
return toArray(this._object.allInstances()).map(wrapObject)
}
getLibrary() {
const libraryController = AppController.sharedInstance().librariesController()
const lib = libraryController.libraryForShareableObject(this._object)
if (!lib) {
const foreignObject = this._object.foreignObject()
if (foreignObject) {
return {
type: Types.Library,
id: String(foreignObject.libraryID()),
name: String(foreignObject.sourceLibraryName()),
enabled: false,
valid: false,
}
}
return null
}
return wrapObject(lib)
}
syncWithLibrary() {
if (this.isImmutable()) {
return false
}
const libraryController = AppController.sharedInstance().librariesController()
const lib = libraryController.libraryForShareableObject(this._object)
if (!lib) {
return false
}
const foreignObject = this._object.foreignObject()
if (!foreignObject) {
return false
}
libraryController.syncForeignObject_withMaster_fromLibrary(
foreignObject,
null,
lib
)
return true
}
unlinkFromLibrary() {
if (this.isImmutable()) {
return false
}
const libraryController = AppController.sharedInstance().librariesController()
const lib = libraryController.libraryForShareableObject(this._object)
if (!lib) {
return false
}
const foreignObject = this._object.foreignObject()
if (!foreignObject) {
return false
}
foreignObject.unlinkFromRemote()
return true
}
}
SymbolMaster.type = Types.SymbolMaster
SymbolMaster[DefinedPropertiesKey] = { ...Artboard[DefinedPropertiesKey] }
Factory.registerClass(SymbolMaster, MSSymbolMaster)
Factory.registerClass(SymbolMaster, MSImmutableSymbolMaster)
SymbolMaster.define('symbolId', {
get() {
return String(this._object.symbolID())
},
set() {
throw new Error('Changing the symbol ID of a SymbolMaster is forbidden.')
},
})
SymbolMaster.define('overrides', {
get() {
// undefined when immutable
if (!this._object.overrideProperies) {
return undefined
}
const overrideProperies = this._object.overrideProperies()
const overrides = toArray(this._object.availableOverrides())
// recursively find the overrides
function findChildrenOverrides(instance) {
const children = toArray(instance.children())
children.forEach(c => {
overrides.push(c)
findChildrenOverrides(c)
})
}
overrides.forEach(findChildrenOverrides)
return overrides.map(o => {
const wrapped = Override.fromNative(o)
const property = overrideProperies[o.overridePoint().name()]
Object.defineProperty(wrapped, '__symbolMaster', {
writable: false,
enumerable: false,
value: this,
})
if (property && !o.isVisible) {
Object.defineProperty(wrapped, '__editable', {
writable: true,
enumerable: false,
value: property.canOverride,
})
}
return wrapped
})
},
set(overrides) {
overrides.forEach(o => {
const overridePoint = MSOverridePoint.alloc().init()
overridePoint.name = o.id
this._object.setOverridePoint_editable(overridePoint, o.editable)
})
},
})
SymbolMaster.extendObject('background', {
includedInInstance: {
get() {
return Boolean(Number(this._object.includeBackgroundColorInInstance()))
},
set(included) {
if (this._parent.isImmutable()) {
return
}
this._object.setIncludeBackgroundColorInInstance(included)
},
},
})