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

symbol override control #449

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions src/flexToSketchJSON.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,6 @@ const flexToSketchJSON = (
}

const renderer = new Renderer();
const groupLayer = renderer.renderGroupLayer(node);

if (groupLayer._class === 'symbolInstance') {
return groupLayer;
}

const backingLayers = renderer.renderBackingLayers(node);

Expand All @@ -60,6 +55,12 @@ const flexToSketchJSON = (
// Filter out anything null, undefined
const layers = [...backingLayers, ...sublayers].filter(l => l);

const groupLayer = renderer.renderGroupLayer(node, layers);

if (groupLayer._class === 'symbolInstance') {
return groupLayer;
}

return { ...groupLayer, layers };
};

Expand Down
9 changes: 4 additions & 5 deletions src/renderers/SketchRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@ export default class SketchRenderer {
return 'Group';
}

renderGroupLayer({
layout,
style,
props,
}: TreeNode):
renderGroupLayer(
{ layout, style, props }: TreeNode,
_children: FileFormat.AnyLayer[],
):
| FileFormat.SymbolMaster
| FileFormat.Artboard
| FileFormat.Group
Expand Down
91 changes: 2 additions & 89 deletions src/renderers/SymbolInstanceRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,94 +9,7 @@ import {
import { TreeNode } from '../types';
import { getSymbolMasterById, SymbolInstanceProps } from '../symbol';
import getImageDataFromURL from '../utils/getImageDataFromURL';

type Override = {
type: 'symbolID' | 'stringValue' | 'layerStyle' | 'textStyle' | 'flowDestination' | 'image';
path: string;
name: string;
symbolID?: string;
};

const findInGroup = (layer: FileFormat.AnyGroup, type: string): FileFormat.AnyLayer | undefined =>
layer && layer.layers && layer.layers.find(l => l._class === type);

const hasImageFill = (layer: FileFormat.AnyLayer): boolean =>
// @ts-ignore
!!(layer.style && layer.style.fills && layer.style.fills.some(f => f.image));

const removeDuplicateOverrides = (overrides: Array<Override>): Array<Override> => {
const seen = {};

return overrides.filter(({ path }) => {
const isDuplicate = typeof seen[path] !== 'undefined';
seen[path] = true;

return !isDuplicate;
});
};

const extractOverridesReducer = (path: string) => (
overrides: Override[],
layer: FileFormat.AnyLayer,
): Override[] => {
if (layer._class === 'text') {
return overrides.concat({
type: 'stringValue',
path: `${path}${layer.do_objectID}`,
name: layer.name,
});
}

if (layer._class === 'group') {
// here we're doing some look-ahead to see if this group contains a group
// that contains text. this is the structure that will appear if the user
// creates a `<Text />` element with a custom name
const subGroup = findInGroup(layer, 'group') as FileFormat.Group;
const textLayer = findInGroup(subGroup, 'text') as FileFormat.Text;
if (textLayer) {
return overrides.concat({
type: 'stringValue',
path: `${path}${textLayer.do_objectID}`,
name: textLayer.name,
});
}

// here we're doing look-ahead to see if this group contains a shapeGroup
// with an image fill. if it does we can do an image override on that
// fill
const shapeGroup = findInGroup(layer, 'shapeGroup');
if (shapeGroup && hasImageFill(shapeGroup)) {
return overrides.concat({
type: 'image',
path: `${path}${shapeGroup.do_objectID}`,
name: layer.name,
});
}
}

if (layer._class === 'symbolInstance') {
return overrides.concat({
type: 'symbolID',
path: `${path}${layer.do_objectID}`,
name: layer.name,
symbolID: layer.symbolID,
});
}

if (
(layer._class === 'shapeGroup' || layer._class === 'artboard' || layer._class === 'group') &&
layer.layers
) {
return layer.layers.reduce(extractOverridesReducer(path), overrides);
}

return overrides;
};

const extractOverrides = (layers: FileFormat.AnyLayer[] = [], path?: string): Override[] => {
const overrides = layers.reduce(extractOverridesReducer(path || ''), []);
return removeDuplicateOverrides(overrides);
};
import { extractOverrides, Override } from '../utils/extractOverrides';

export default class SymbolInstanceRenderer extends SketchRenderer {
renderGroupLayer({ layout, props }: TreeNode<SymbolInstanceProps & { symbolID: string }>) {
Expand Down Expand Up @@ -127,7 +40,7 @@ export default class SymbolInstanceRenderer extends SketchRenderer {
const overrideValue = props.overrides[reference.name];
if (typeof overrideValue !== 'function' || typeof overrideValue.symbolID !== 'string') {
throw new Error(
`The overriden nested symbol needs to the constructor of another symbol.\n\nIn Symbol Instance: "${props.name}"\nFor Override: "${reference.name}"`,
`The overriden nested symbol needs to be the constructor of another symbol.\n\nIn Symbol Instance: "${props.name}"\nFor Override: "${reference.name}"`,
);
}

Expand Down
89 changes: 89 additions & 0 deletions src/utils/extractOverrides.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import { FileFormat1 as FileFormat } from '@sketch-hq/sketch-file-format-ts';

export type Override = {
type: 'symbolID' | 'stringValue' | 'layerStyle' | 'textStyle' | 'flowDestination' | 'image';
path: string;
name: string;
symbolID?: string;
};

const findInGroup = (layer: FileFormat.AnyGroup, type: string): FileFormat.AnyLayer | undefined =>
layer && layer.layers && layer.layers.find(l => l._class === type);

const hasImageFill = (layer: FileFormat.AnyLayer): boolean =>
// @ts-ignore
!!(layer.style && layer.style.fills && layer.style.fills.some(f => f.image));

const removeDuplicateOverrides = (overrides: Array<Override>): Array<Override> => {
const seen = {};

return overrides.filter(({ path }) => {
const isDuplicate = typeof seen[path] !== 'undefined';
seen[path] = true;

return !isDuplicate;
});
};

const extractOverridesReducer = (path: string) => (
overrides: Override[],
layer: FileFormat.AnyLayer,
): Override[] => {
if (layer._class === 'text') {
return overrides.concat({
type: 'stringValue',
path: `${path}${layer.do_objectID}`,
name: layer.name,
});
}

if (layer._class === 'group') {
// here we're doing some look-ahead to see if this group contains a group
// that contains text. this is the structure that will appear if the user
// creates a `<Text />` element with a custom name
const subGroup = findInGroup(layer, 'group') as FileFormat.Group;
const textLayer = findInGroup(subGroup, 'text') as FileFormat.Text;
if (textLayer) {
return overrides.concat({
type: 'stringValue',
path: `${path}${textLayer.do_objectID}`,
name: textLayer.name,
});
}

// here we're doing look-ahead to see if this group contains a shapeGroup
// with an image fill. if it does we can do an image override on that
// fill
const shapeGroup = findInGroup(layer, 'shapeGroup');
if (shapeGroup && hasImageFill(shapeGroup)) {
return overrides.concat({
type: 'image',
path: `${path}${shapeGroup.do_objectID}`,
name: layer.name,
});
}
}

if (layer._class === 'symbolInstance') {
return overrides.concat({
type: 'symbolID',
path: `${path}${layer.do_objectID}`,
name: layer.name,
symbolID: layer.symbolID,
});
}

if (
(layer._class === 'shapeGroup' || layer._class === 'artboard' || layer._class === 'group') &&
layer.layers
) {
return layer.layers.reduce(extractOverridesReducer(path), overrides);
}

return overrides;
};

export const extractOverrides = (layers: FileFormat.AnyLayer[] = [], path?: string): Override[] => {
const overrides = layers.reduce(extractOverridesReducer(path || ''), []);
return removeDuplicateOverrides(overrides);
};