Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

shared styles arrays #360

Merged
merged 9 commits into from Feb 4, 2019
Copy path View file
@@ -1,6 +1,7 @@
{
"unreleased": [
"[New] Add `colors` and `gradients` properties on Document",
"[Improved] Shared styles are now document properties and can be mutated",
"[Improved] `console.clear` will now clear the DevTools console",
"[Improved] Be more aggressive in finding the selected document",
"[New] Add `noise` and `pattern` properties on Fill"
Copy path View file
@@ -158,15 +158,15 @@ export class WrappedObject {
}

arr.reverse = () => {
Array.prototype.reverse.call(arr)
Array.prototype.reverse.apply(arr)
descriptor.set.bind(this)(arr)
}
arr.sort = compareFunction => {
Array.prototype.reverse.call(arr, [compareFunction])
Array.prototype.reverse.apply(arr, [compareFunction])
descriptor.set.bind(this)(arr)
}
arr.fill = (value, start, end) => {
Array.prototype.reverse.call(arr, [value, start, end])
Array.prototype.reverse.apply(arr, [value, start, end])
descriptor.set.bind(this)(arr)
}

@@ -191,22 +191,24 @@ export class WrappedObject {
removedItems.push(descriptor.removeItem.bind(this)(i))
}

const addedItems = []

items.forEach((item, i) => {
descriptor.insertItem.bind(this)(item, start + i)
addedItems.push(descriptor.insertItem.bind(this)(item, start + i))
})

// call the native function
Array.prototype.splice.call(arr, [start, count, ...items])
Array.prototype.splice.apply(arr, [start, count, ...addedItems])
return removedItems
}

arr.push = (...items) => {
arr.splice(arr.length, 0, ...items)
return arr.length + items.length
return arr.length
}
arr.unshift = (...items) => {
arr.splice(0, 0, ...items)
return arr.length + items.length
return arr.length
}
arr.pop = () => arr.splice(arr.length - 1)[0]
arr.shift = () => arr.splice(0, 1)[0]
Copy path View file
@@ -75,13 +75,15 @@ Group.define('layers', {
},
insertItem(item, index) {
if (this.isImmutable()) {
return
return undefined
}
const layer = wrapObject(item)
if (layer._object.parentGroup()) {
layer._object.removeFromParent()
}
this._object.insertLayer_atIndex(layer._object, index)

return layer
},
removeItem(index) {
if (this.isImmutable()) {
Copy path View file
@@ -309,11 +309,12 @@ Layer.define('exportFormats', {
},
insertItem(item, index) {
if (this.isImmutable()) {
return
return undefined
}
const arr = toArray(this._object.exportOptions().exportFormats() || [])
arr.splice(index, 0, item)
this.exportFormats = arr
return wrapObject(item, Types.ExportFormat)
},
removeItem(index) {
if (this.isImmutable()) {
Copy path View file
@@ -1,13 +1,14 @@
import { toArray } from 'util'
import { toArray, isNativeObject } from 'util'
import { WrappedObject, DefinedPropertiesKey } from '../WrappedObject'
import { Page } from '../layers/Page'
import { Selection } from './Selection'
import { getURLFromPath } from '../utils'
import { getURLFromPath, isWrappedObject } from '../utils'
import { wrapObject } from '../wrapNativeObject'
import { Types } from '../enums'
import { Factory } from '../Factory'
import { StyleType } from '../style/Style'
import { ColorAsset, GradientAsset } from '../assets'
import { SharedStyle } from './SharedStyle'

export const SaveModeType = {
Save: NSSaveOperation,
@@ -182,6 +183,9 @@ export class Document extends WrappedObject {
}

getSharedLayerStyles() {
console.warn(
`\`document.getSharedLayerStyles()\` is deprecated. Use \`document.sharedLayerStyles\` instead.`
)
const documentData = this._getMSDocumentData()
return toArray(documentData.allLayerStyles()).map(wrapObject)
}
@@ -191,6 +195,9 @@ export class Document extends WrappedObject {
}

getSharedTextStyles() {
console.warn(
`\`document.getSharedTextStyles()\` is deprecated. Use \`document.sharedTextStyles\` instead.`
)
const documentData = this._getMSDocumentData()
return toArray(documentData.allTextStyles()).map(wrapObject)
}
@@ -413,7 +420,7 @@ Document.define('pages', {
},
insertItem(item, index) {
if (this.isImmutable()) {
return
return undefined
}
const wrapped = wrapObject(item, Types.Page)
if (wrapped._object.documentData()) {
@@ -426,6 +433,7 @@ Document.define('pages', {
} else {
this._object.documentData().insertPage_atIndex(wrapped._object, index)
}
return wrapped
},
removeItem(index) {
if (this.isImmutable()) {
@@ -517,11 +525,12 @@ Document.define('colors', {
},
insertItem(color, index) {
if (this.isImmutable()) {
return
return undefined
}
const assets = this._getMSDocumentData().assets()
const wrapped = ColorAsset.from(color)
assets.insertColorAsset_atIndex(wrapped._object, index)
return wrapped
},
removeItem(index) {
if (this.isImmutable()) {
@@ -562,11 +571,12 @@ Document.define('gradients', {
},
insertItem(gradient, index) {
if (this.isImmutable()) {
return
return undefined
}
const assets = this._getMSDocumentData().assets()
const wrapped = GradientAsset.from(gradient)
assets.insertGradientAsset_atIndex(wrapped._object, index)
return wrapped
},
removeItem(index) {
if (this.isImmutable()) {
@@ -576,3 +586,163 @@ Document.define('gradients', {
return documentData.assets().removeGradientAssetAtIndex(index)
},
})

Document.define('sharedLayerStyles', {
array: true,
get() {
if (!this._object) {
return []
}
const documentData = this._getMSDocumentData()
return toArray(documentData.allLayerStyles()).map(wrapObject)
},
set(sharedLayerStyles) {
if (this.isImmutable()) {
return
}
const documentData = this._getMSDocumentData()
const container = documentData.sharedObjectContainerOfType(1)

// remove the existing shared styles
container.removeAllSharedObjects()

container.addSharedObjects(
toArray(sharedLayerStyles).map(item => {
let sharedStyle

if (isWrappedObject(item)) {
sharedStyle = item.sketchObject
} else if (isNativeObject(item)) {
sharedStyle = item
} else {
const wrappedStyle = wrapObject(item.style, Types.Style)

sharedStyle = MSSharedStyle.alloc().initWithName_style(
item.name,
wrappedStyle.sketchObject
)
}
return sharedStyle
})
)
},
insertItem(item, index) {
if (this.isImmutable()) {
return undefined
}

const documentData = this._getMSDocumentData()

let sharedStyle

if (isWrappedObject(item)) {
sharedStyle = item.sketchObject
} else if (isNativeObject(item)) {
sharedStyle = item
} else {
const wrappedStyle = wrapObject(item.style, Types.Style)

sharedStyle = MSSharedStyle.alloc().initWithName_style(
item.name,
wrappedStyle.sketchObject
)
}

const container = documentData.sharedObjectContainerOfType(1)

container.insertSharedObject_atIndex(sharedStyle, index)

return new SharedStyle({ sketchObject: sharedStyle })
},
removeItem(index) {
if (this.isImmutable()) {
return undefined
}
const documentData = this._getMSDocumentData()
const container = documentData.sharedObjectContainerOfType(1)

const removed = documentData.allLayerStyles()[index]
container.removeSharedObjectAtIndex(index)
return wrapObject(removed, Types.SharedStyle)
},
})

Document.define('sharedTextStyles', {
array: true,
get() {
if (!this._object) {
return []
}
const documentData = this._getMSDocumentData()
return toArray(documentData.allTextStyles()).map(wrapObject)
},
set(sharedLayerStyles) {
if (this.isImmutable()) {
return
}
const documentData = this._getMSDocumentData()
const container = documentData.sharedObjectContainerOfType(2)

// remove the existing shared styles
container.removeAllSharedObjects()

container.addSharedObjects(
toArray(sharedLayerStyles).map(item => {
let sharedStyle

if (isWrappedObject(item)) {
sharedStyle = item.sketchObject
} else if (isNativeObject(item)) {
sharedStyle = item
} else {
const wrappedStyle = wrapObject(item.style, Types.Style)

sharedStyle = MSSharedStyle.alloc().initWithName_style(
item.name,
wrappedStyle.sketchObject
)
}
return sharedStyle
})
)
},
insertItem(item, index) {
if (this.isImmutable()) {
return undefined
}

const documentData = this._getMSDocumentData()

let sharedStyle

if (isWrappedObject(item)) {
sharedStyle = item.sketchObject
} else if (isNativeObject(item)) {
sharedStyle = item
} else {
const wrappedStyle = wrapObject(item.style, Types.Style)

sharedStyle = MSSharedStyle.alloc().initWithName_style(
item.name,
wrappedStyle.sketchObject
)
}

const container = documentData.sharedObjectContainerOfType(2)

container.insertSharedObject_atIndex(sharedStyle, index)

return new SharedStyle({ sketchObject: sharedStyle })
},
removeItem(index) {
if (this.isImmutable()) {
return undefined
}
const documentData = this._getMSDocumentData()
const container = documentData.sharedObjectContainerOfType(2)

const removed = documentData.allTextStyles()[index]
container.removeSharedObjectAtIndex(index)
return wrapObject(removed, Types.SharedStyle)
},
})
@@ -15,13 +15,16 @@ export class SharedStyle extends WrappedObject {
constructor(master = {}) {
if (!master.sketchObject) {
throw new Error(
`Cannot create a SharedStyle directly, use SharedStyle.fromStyle({ name, style, document }) instead.`
`Cannot create a SharedStyle directly, use \`document.sharedLayerStyles.push({ name, style })\` (or \`document.sharedTextStyles\`) instead.`
)
}
super(master)
}

static fromStyle({ name, style, document } = {}) {
console.warn(
`\`SharedStyle.fromStyle({ name, style, document })\` is deprecated. Use \`document.sharedLayerStyles.push({ name, style })\` (or \`document.sharedTextStyles\`) instead.`
)
const documentData = wrapObject(document)._getMSDocumentData()
const wrappedStyle = wrapObject(style, Types.Style)

@@ -76,7 +76,19 @@ test('should look for a shared layer style by its id', (context, document) => {
test('should list all the shared layer styles', (context, document) => {
const { sharedStyle } = createSharedStyle(document, Shape)

expect(document.getSharedLayerStyles()).toEqual([sharedStyle])
expect(document.sharedLayerStyles.length).toBe(1)
expect(document.sharedLayerStyles[0]).toEqual(sharedStyle)

document.sharedLayerStyles.push({
name: 'test2',
style: { fills: ['#000'] },
})

expect(document.sharedLayerStyles.length).toBe(2)

document.sharedLayerStyles.pop()

expect(document.sharedLayerStyles.length).toBe(1)
})

test('should look for a shared text style by its id', (context, document) => {
@@ -88,7 +100,8 @@ test('should look for a shared text style by its id', (context, document) => {
test('should list all the shared text styles', (context, document) => {
const { sharedStyle } = createSharedStyle(document, Text)

expect(document.getSharedTextStyles()).toEqual([sharedStyle])
expect(document.sharedTextStyles.length).toBe(1)
expect(document.sharedTextStyles[0]).toEqual(sharedStyle)
})

test('should reset document colors', (context, document) => {
Oops, something went wrong.
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.