Skip to content

Commit ef05581

Browse files
Merge SketchAPI changes from Sketch repo to SketchAPI public repo for Sketch 95.2
1 parent 1c5d255 commit ef05581

File tree

5 files changed

+98
-66
lines changed

5 files changed

+98
-66
lines changed

Source/dom/layers/SymbolInstance.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ export class SymbolInstance extends StyledLayer {
7070
if (this.isImmutable()) {
7171
return this
7272
}
73-
this._object.ensureDetachHasUpdated()
74-
this._object.resizeToFitContentsIfNeededNoCache()
73+
this._object.ensureDetachHasUpdated()
74+
this._object.resizeToFitContentsIfNeeded()
7575
return this
7676
}
7777
}

Source/dom/layers/SymbolMaster.js

Lines changed: 15 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -142,53 +142,41 @@ SymbolMaster.define('symbolId', {
142142

143143
SymbolMaster.define('overrides', {
144144
get() {
145-
const method = (function (sketchVersion) {
146-
if (sketchVersion < 65) {
147-
return 'overrideProperies'
148-
}
149-
if (sketchVersion < 71) {
150-
return 'overrideProperties'
151-
}
152-
return 'overridePropertyDict'
153-
})(BCSketchInfo.shared().metadata().appVersion)
154-
155-
// undefined when immutable
156-
if (!this._object[method] || !this._object.availableOverrides) {
157-
return undefined
158-
}
159-
const overrideProperties = this._object[method]()
160-
const overrides = toArray(
145+
if (!this._object.availableOverrides) {
146+
return undefined
147+
}
148+
149+
const overrides = toArray(
161150
MSAvailableOverride.flattenAvailableOverrides(
162151
this._object.availableOverrides()
163152
)
164153
)
165154

166155
return overrides.map((o) => {
167156
const wrapped = Override.fromNative(o)
168-
const property = overrideProperties[o.overridePoint().name()]
169157
Object.defineProperty(wrapped, '__symbolMaster', {
170158
writable: false,
171159
enumerable: false,
172160
value: this,
173161
})
174-
if (property && !o.isVisible) {
175-
Object.defineProperty(wrapped, '__editable', {
176-
writable: true,
177-
enumerable: false,
178-
value: property.canOverride,
179-
})
180-
}
181162
return wrapped
182163
})
183164
},
184165
set(overrides) {
185166
if (this.isImmutable()) {
186167
return
187168
}
169+
170+
var dict = {}
171+
this._object.overridePoints().forEach((o) => {
172+
dict[o.name()] = o
173+
})
174+
188175
overrides.forEach((o) => {
189-
const overridePoint = MSOverridePoint.alloc().init()
190-
overridePoint.name = o.id
191-
this._object.setOverridePoint_editable(overridePoint, o.editable)
176+
const overridePoint = dict[o.id]
177+
if (overridePoint) {
178+
this._object.setOverridePoint_editable(overridePoint, o.editable)
179+
}
192180
})
193181
},
194182
})

Source/dom/layers/__tests__/SymbolInstance.test.js

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ test('should have overrides', (_context, document) => {
3232
const { master, text } = createSymbolMaster(document)
3333
const instance = master.createNewInstance()
3434
document.selectedPage.layers = document.selectedPage.layers.concat(instance)
35+
instance.sketchObject.ensureDetachHasUpdated()
3536

3637
expect(instance.overrides.length).toBe(6)
3738
const override = instance.overrides[0]
@@ -77,19 +78,19 @@ test('should detach an instance recursively', (_context, document) => {
7778
expect(group.type).toBe('Group')
7879
})
7980

80-
// test('should resize in response to smart layout changes', (_context, document) => {
81-
// const { master } = createSymbolMaster(document)
82-
// master.smartLayout = SmartLayout.LeftToRight
83-
// const instance = new SymbolInstance({
84-
// symbolId: master.symbolId,
85-
// parent: document.selectedPage,
86-
// })
87-
// const initialWidth = instance.frame.width
88-
// instance.overrides[0].value = '0'.repeat(1000)
89-
// instance.resizeWithSmartLayout()
90-
// const widthAfterSmartLayout = instance.frame.width
91-
// expect(widthAfterSmartLayout).toBeGreaterThan(initialWidth)
92-
// })
81+
test('should resize in response to smart layout changes', (_context, document) => {
82+
const { master } = createSymbolMaster(document)
83+
master.smartLayout = SmartLayout.LeftToRight
84+
const instance = new SymbolInstance({
85+
symbolId: master.symbolId,
86+
parent: document.selectedPage,
87+
})
88+
const initialWidth = instance.frame.width
89+
instance.overrides[0].value = 'A string that is long enough to cause a size change, hopefully in the positive direction'
90+
instance.resizeWithSmartLayout()
91+
const widthAfterSmartLayout = instance.frame.width
92+
expect(widthAfterSmartLayout).toBeGreaterThan(initialWidth)
93+
})
9394

9495
// test('should change an override value', (_context, document) => {
9596
// const { master } = createSymbolMaster(document)

Source/dom/models/Override.js

Lines changed: 63 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ export class Override extends WrappedObject {
1414
if (!this.__symbolInstance) {
1515
return undefined
1616
}
17-
this.__symbolInstance.sketchObject.ensureDetachHasUpdated()
17+
this.__symbolInstance.sketchObject.ensureDetachHasUpdated()
1818
return toArray(
1919
this.__symbolInstance.sketchObject.overrideContainer().flattenedChildren()
20-
).find((x) => x.availableOverride().overridePoint().name().isEqual(this.sketchObject.overridePoint().name()))
20+
).find((x) => x.availableOverride().overridePoint().fullPath().isEqual(this.sketchObject.overridePoint().fullPath()))
2121
}
2222

2323
getFrame() {
@@ -42,7 +42,7 @@ Override.define('path', {
4242

4343
Override.define('property', {
4444
get() {
45-
return String(this._object.overridePoint().property())
45+
return String(this._object.overridePoint().attributeName())
4646
},
4747
})
4848

@@ -72,6 +72,14 @@ Override.define('value', {
7272
if (this.property === 'image') {
7373
return ImageData.fromNative(value)
7474
}
75+
if (value !== null && value.isKindOfClass_(NSDictionary.class())) {
76+
// Map dictionary overrides into a javascript dictionary
77+
var map = {}
78+
Object.keys(value).forEach((name) => {
79+
map[name] = value[name]
80+
})
81+
return map
82+
}
7583
return String(this._object.currentValue())
7684
},
7785
set(value) {
@@ -91,17 +99,49 @@ Override.define('isDefault', {
9199

92100
Override.define('editable', {
93101
get() {
94-
if (typeof this.__editable !== 'undefined') {
95-
return this.__editable
102+
var overrides
103+
var master
104+
if (typeof this.__symbolMaster !== 'undefined') {
105+
master = this.__symbolMaster.sketchObject
106+
overrides = master.overridePoints
107+
} else if (typeof this.__symbolInstance !== 'undefined') {
108+
var masterGetter = this.__symbolInstance.sketchObject.symbolMaster
109+
if (masterGetter !== 'undefined') {
110+
master = masterGetter()
111+
}
112+
overrides = this.__symbolInstance.sketchObject.overridePoints
113+
}
114+
115+
if (typeof overrides == 'undefined') {
116+
// Can't get the override points - maybe this is an immutable?
117+
return false
118+
}
119+
if (typeof master == 'undefined') {
120+
throw new Error('Unable to find the symbol source for this override')
121+
}
122+
123+
var point
124+
overrides().forEach((o) => {
125+
if (o.fullPath().isEqual(this._object.overridePoint().fullPath())) {
126+
point = o
127+
}
128+
})
129+
130+
if (typeof point == 'undefined') {
131+
return false
132+
}
133+
134+
if (master.allowsOverrides() && master.isOverridePointEditable(point) && point.isConfigurable()) {
135+
return true
136+
} else {
137+
return false
96138
}
97-
return Boolean(Number(this._object.isEditable()))
98139
},
99140
set(editable) {
100141
// __symbolInstance is set when building the Override
101-
if (!this.__symbolMaster) {
142+
if (typeof this.__symbolMaster == 'undefined') {
102143
throw new Error('Can only set `editable` for a symbol master')
103144
}
104-
this.__editable = editable
105145
this.__symbolMaster.sketchObject.setOverridePoint_editable(
106146
this._object.overridePoint(),
107147
editable
@@ -143,20 +183,20 @@ Override.define('selected', {
143183
return
144184
}
145185

146-
var selectionItem = representation.selectionItem()
147-
if (!selectionItem) {
148-
return
149-
}
150-
151-
var page = this.__symbolInstance.sketchObject.parentPage()
152-
if (!page) {
153-
return
154-
}
155-
156-
if (selected) {
157-
page.changeSelectionByAddingItems_extendExisting([selectionItem], true)
158-
} else {
159-
page.changeSelectionByRemovingItems([selectionItem])
160-
}
186+
var selectionItem = representation.selectionItem()
187+
if (!selectionItem) {
188+
return
189+
}
190+
191+
var page = this.__symbolInstance.sketchObject.parentPage()
192+
if (!page) {
193+
return
194+
}
195+
196+
if (selected) {
197+
page.changeSelectionByAddingItems_extendExisting([selectionItem], true)
198+
} else {
199+
page.changeSelectionByRemovingItems([selectionItem])
200+
}
161201
},
162202
})

Source/test-utils.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,11 @@ export function createSymbolMaster(document) {
2121
})
2222

2323
// build the symbol master
24+
const master = SymbolMaster.fromArtboard(artboard)
25+
master.sketchObject.ensureDetachHasUpdated()
26+
2427
return {
25-
master: SymbolMaster.fromArtboard(artboard),
28+
master: master,
2629
text,
2730
artboard,
2831
}

0 commit comments

Comments
 (0)