Skip to content

Commit

Permalink
fix: Move to layer page when selecting a layer
Browse files Browse the repository at this point in the history
fix #89
  • Loading branch information
Michele committed Aug 29, 2019
1 parent 91a1228 commit d6e91cb
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 2 deletions.
2 changes: 1 addition & 1 deletion resources/app/components/List/Tree/LayerNode.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ const LayerNode = ({
const shouldCenterOnSelf: boolean = type === LayerType.artboard || type === LayerType.page;
setSelectedLayer(id);

const idToCenterOn = shouldCenterOnSelf ? id : Object.entries(colors)
const idToCenterOn: string = shouldCenterOnSelf ? id : Object.entries(colors)
.reduce((acc: any, keyValue: any) => ([...acc, ...keyValue[1]]), [])
.find(innerLayer => innerLayer.id === id)
.parents
Expand Down
3 changes: 2 additions & 1 deletion resources/app/enums/layer-type.enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ export enum LayerType {
shapePath = 'ShapePath',
text = 'Text',
shape = 'Shape',
}
document = 'Document',
}
40 changes: 40 additions & 0 deletions src/helpers/get-page.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@

import { Layer } from 'sketch';
import {getPage} from './get-page';
import { LayerType } from '../../resources/app/enums/layer-type.enum';

describe('getPage', () => {
test('should return the layer if is a page', () => {
const layer = {
type: LayerType.page
}

expect(getPage(layer as Layer)).toBe(layer);
});

test('should return the first parent page', () => {
const page = {
type: LayerType.page,
parent: {
type: LayerType.document
}
}
const layer = {
type: LayerType.shapePath,
parent: {
type: LayerType.artboard,
parent: page
}
}

expect(getPage(layer as Layer)).toBe(page);
});

test('should throw an error if the layer has no parent page', () => {
const layer = {
type: LayerType.shapePath
}

expect(() => getPage(layer as Layer)).toThrow()
})
});
14 changes: 14 additions & 0 deletions src/helpers/get-page.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Page, Layer } from 'sketch';
import { LayerType } from '../../resources/app/enums/layer-type.enum';

export const getPage = (layer: Layer): Page => {
if (layer.type === LayerType.page) {
return layer;
}

if (!layer.parent) {
throw new Error(`Layer is not child on any page! ${JSON.stringify(layer, null, 2)}`);
}

return getPage(layer.parent as Layer);
}
6 changes: 6 additions & 0 deletions src/my-command.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import BrowserWindow from 'sketch-module-web-view';
import sketch, { UI, Settings } from 'sketch'; // eslint-disable-line import/no-unresolved
import { getPage } from './helpers/get-page';
import { browserWindowSize } from '../constants.ts';
import { replaceColorInLayers } from './helpers/replace-color-in-layers.ts';
import getColors from './get-colors.ts';
Expand Down Expand Up @@ -38,6 +39,11 @@ export default function () {

const sketchLayer = document.getLayerWithID(layerID);
const layerToCenterOn = document.getLayerWithID(idToCenterOn);
const page = getPage(layerToCenterOn);

if (document.selectedPage !== page) {
document.selectedPage = page;
}

document.centerOnLayer(layerToCenterOn);
document.selectedLayers = [sketchLayer];
Expand Down

0 comments on commit d6e91cb

Please sign in to comment.