Skip to content

Commit ca9d832

Browse files
ipavelledsraveclassic
authored andcommitted
feat: expand sketch schema for support foreign symbols
1 parent 6219546 commit ca9d832

File tree

12 files changed

+96
-17
lines changed

12 files changed

+96
-17
lines changed

src/language/typescript/common/utils.ts

+4
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ export const getKindValue = (kind: Kind, value: string): string => {
5959

6060
export const getControllerName = (name: string): string => `${name}Controller`;
6161

62+
const COMMENT_PATTERN = /\/\*(.*)\*\//;
63+
const REPLACE_COMMENT_PATTERN = new RegExp(COMMENT_PATTERN, 'g');
64+
export const escapeCommpent = (value: string) => value.replace(REPLACE_COMMENT_PATTERN, '\\/*$1*\\/');
65+
6266
export const UNSAFE_PROPERTY_PATTERN = /[^a-zA-Z_0-9]/;
6367
const REPLACE_PATTERN = new RegExp(UNSAFE_PROPERTY_PATTERN, 'g');
6468
export const getSafePropertyName = (value: string): string =>

src/language/typescript/sketch-121/serializers/document.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { serializeForeignTextStyle } from './objects/foreign-text-style';
1313
import { Option } from 'fp-ts/lib/Option';
1414
import { file, fragment, FSEntity } from '../../../../utils/fs';
1515
import { serializeAssetCollection } from './objects/asset-collection';
16-
import { serializePage } from './pages/page';
16+
import { serializePage } from './objects/page';
1717

1818
export const serializeDocument = combineReader(
1919
serializeSharedStyleContainer,

src/language/typescript/sketch-121/serializers/pages/layer.ts renamed to src/language/typescript/sketch-121/serializers/objects/layer.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { Layer } from '../../../../../schema/sketch-121/pages/layer';
1+
import { Layer } from '../../../../../schema/sketch-121/objects/layer';
22
import { Either } from 'fp-ts/lib/Either';
3-
import { getJSDoc } from '../../../common/utils';
4-
import { serializeStyle } from '../objects/style';
3+
import { getJSDoc, escapeCommpent } from '../../../common/utils';
4+
import { serializeStyle } from './style';
55
import { combineEither } from '@devexperts/utils/dist/adt/either.utils';
66
import { combineReader } from '@devexperts/utils/dist/adt/reader.utils';
77
import { context } from '../../utils';
@@ -29,7 +29,7 @@ export const serializeLayer = combineReader(context, context => (layer: Layer, j
2929
layerStyle,
3030
nestedLayersStyles,
3131
(pageStyle, nestedPagesStyles) => `
32-
${getJSDoc([...(jsdoc || []), layer.name, layer.do_objectID])}
32+
${getJSDoc([...(jsdoc || []), escapeCommpent(layer.name), layer.do_objectID])}
3333
export const ${safeName}: { name: string; styles: Partial<CSSStyleDeclaration> } = {
3434
name: '${layer.name}',
3535
styles: {

src/language/typescript/sketch-121/serializers/pages/page.ts renamed to src/language/typescript/sketch-121/serializers/objects/page.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Page } from '../../../../../schema/sketch-121/pages/page';
1+
import { Page } from '../../../../../schema/sketch-121/objects/page';
22
import { either } from 'fp-ts';
33
import { combineReader } from '@devexperts/utils/dist/adt/reader.utils';
44
import { pipe } from 'fp-ts/lib/pipeable';

src/schema/sketch-121/abstract-document.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@ import { SharedTextStyleContainer, SharedTextStyleContainerCodec } from './objec
55
import { ForeignLayerStyle, ForeignLayerStyleCodec } from './objects/foreign-layer-style';
66
import { ForeignTextStyle, ForeignTextStyleCodec } from './objects/foreign-text-style';
77
import { AssetCollection, AssetCollectionCodec } from './objects/asset-collection';
8-
import { ObjectID, ObjectIDCodec } from './objects//object-id';
8+
import { ObjectID, ObjectIDCodec } from './objects/object-id';
9+
import { ForeignSymbol, ForeignSymbolCodec } from './objects/foreign-symbol';
910

1011
export interface AbstractDocument {
1112
readonly do_objectID: ObjectID;
1213
readonly assets: AssetCollection;
1314
readonly foreignLayerStyles: ForeignLayerStyle[];
1415
readonly foreignTextStyles: ForeignTextStyle[];
16+
readonly foreignSymbols: ForeignSymbol[];
1517
readonly layerTextStyles: SharedTextStyleContainer;
1618
readonly layerStyles: SharedStyleContainer;
1719
}
@@ -21,6 +23,7 @@ export const AbstractDocumentCodec: Codec<AbstractDocument> = type({
2123
assets: AssetCollectionCodec,
2224
foreignLayerStyles: array(ForeignLayerStyleCodec),
2325
foreignTextStyles: array(ForeignTextStyleCodec),
26+
foreignSymbols: array(ForeignSymbolCodec),
2427
layerTextStyles: SharedTextStyleContainerCodec,
2528
layerStyles: SharedStyleContainerCodec,
2629
});

src/schema/sketch-121/document.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Codec } from '../../utils/io-ts';
22
import { array, intersection, type } from 'io-ts';
33
import { AbstractDocument, AbstractDocumentCodec } from './abstract-document';
4-
import { Page, PageCodec } from './pages/page';
4+
import { Page, PageCodec } from './objects/page';
55

66
export interface Document extends AbstractDocument {
77
readonly pages: Page[];
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { literal, union } from 'io-ts';
2+
import { Codec } from '../../../utils/io-ts';
3+
4+
export type LayerClass =
5+
| 'symbolInstance'
6+
| 'symbolMaster'
7+
| 'group'
8+
| 'oval'
9+
| 'text'
10+
| 'rectangle'
11+
| 'shapePath'
12+
| 'shapeGroup'
13+
| 'artboard';
14+
15+
export const LayerClassCodec: Codec<LayerClass> = union(
16+
[
17+
literal('symbolInstance'),
18+
literal('symbolMaster'),
19+
literal('group'),
20+
literal('oval'),
21+
literal('text'),
22+
literal('rectangle'),
23+
literal('shapePath'),
24+
literal('shapeGroup'),
25+
literal('artboard'),
26+
],
27+
'LayerClass',
28+
);

src/schema/sketch-121/meta.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import { Codec } from '../../utils/io-ts';
2-
import { literal, type } from 'io-ts';
2+
import { literal, type, union } from 'io-ts';
33

44
export interface Meta {
5-
readonly version: 121;
5+
readonly version: 121 | 122 | 123;
66
}
77

88
export const MetaCodec: Codec<Meta> = type(
99
{
10-
version: literal(121),
10+
version: union([literal(121), literal(122), literal(123)]),
1111
},
1212
'Meta',
1313
);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { type } from 'io-ts';
2+
import { Layer, LayerCodec } from './layer';
3+
import { ObjectID, ObjectIDCodec } from './object-id';
4+
import { Codec } from '../../../utils/io-ts';
5+
6+
export interface ForeignSymbol {
7+
do_objectID: ObjectID;
8+
originalMaster: Layer;
9+
symbolMaster: Layer;
10+
}
11+
12+
export const ForeignSymbolCodec: Codec<ForeignSymbol> = type(
13+
{
14+
do_objectID: ObjectIDCodec,
15+
originalMaster: LayerCodec,
16+
symbolMaster: LayerCodec,
17+
},
18+
'ForeignSymbol',
19+
);

src/schema/sketch-121/pages/layer.ts renamed to src/schema/sketch-121/objects/layer.ts

+12-4
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,36 @@
11
import { Codec } from '../../../utils/io-ts';
2-
import { Style, StyleCodec } from '../objects/style';
3-
import { ObjectID, ObjectIDCodec } from '../objects/object-id';
2+
import { Style, StyleCodec } from './style';
3+
import { ObjectID, ObjectIDCodec } from './object-id';
44
import { string, type, array, recursion, boolean } from 'io-ts';
55
import { optionFromNullable } from 'io-ts-types/lib/optionFromNullable';
66
import { Option } from 'fp-ts/lib/Option';
7+
import { OverrideValue, OverrideValueCodec } from './override-value';
8+
import { LayerClass, LayerClassCodec } from '../enums/layer-class';
79

810
export interface Layer {
9-
readonly _class: string;
11+
readonly _class: LayerClass;
1012
readonly do_objectID: ObjectID;
1113
readonly name: string;
1214
readonly style: Style;
1315
readonly layers: Option<Layer[]>;
1416
readonly isVisible: boolean;
17+
readonly overrideValues: Option<OverrideValue[]>;
18+
readonly sharedStyleID: Option<ObjectID>;
19+
readonly symbolID: Option<ObjectID>;
1520
}
1621

1722
export const LayerCodec: Codec<Layer> = recursion('Layer', () =>
1823
type(
1924
{
20-
_class: string,
25+
_class: LayerClassCodec,
2126
do_objectID: ObjectIDCodec,
2227
name: string,
2328
style: StyleCodec,
2429
isVisible: boolean,
2530
layers: optionFromNullable(array(LayerCodec)),
31+
overrideValues: optionFromNullable(array(OverrideValueCodec)),
32+
sharedStyleID: optionFromNullable(ObjectIDCodec),
33+
symbolID: optionFromNullable(ObjectIDCodec),
2634
},
2735
'Layer',
2836
),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { string, type } from 'io-ts';
2+
import { Codec } from '../../../utils/io-ts';
3+
4+
export interface OverrideValue {
5+
readonly _class: string;
6+
readonly overrideName: string;
7+
readonly value: string;
8+
}
9+
10+
export const OverrideValueCodec: Codec<OverrideValue> = type(
11+
{
12+
_class: string,
13+
value: string,
14+
overrideName: string,
15+
},
16+
'OverrideValue',
17+
);

src/schema/sketch-121/pages/page.ts renamed to src/schema/sketch-121/objects/page.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Codec } from '../../../utils/io-ts';
2-
import { Style, StyleCodec } from '../objects/style';
3-
import { ObjectID, ObjectIDCodec } from '../objects/object-id';
2+
import { Style, StyleCodec } from './style';
3+
import { ObjectID, ObjectIDCodec } from './object-id';
44
import { string, type, array } from 'io-ts';
55
import { Layer, LayerCodec } from './layer';
66

0 commit comments

Comments
 (0)