Skip to content

Commit

Permalink
feat: parse重载
Browse files Browse the repository at this point in the history
  • Loading branch information
army8735 committed Mar 29, 2024
1 parent e4098fd commit fb5e5a5
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 7 deletions.
12 changes: 12 additions & 0 deletions src/format/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,18 @@ export type JPolyline = JNode & {
props: PolylineProps;
};

export type JLayer = JNode
| JContainer
| JPage
| JArtBoard
| JSymbolMaster
| JSymbolInstance
| JGroup
| JShapeGroup
| JBitmap
| JText
| JPolyline;

export type JStyle = {
top: number | string;
right: number | string;
Expand Down
14 changes: 10 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { TAG_NAME, JFile, JSymbolMaster } from './format';
import { TAG_NAME, JFile, JSymbolMaster, JLayer } from './format';
import { openAndConvertSketchBuffer, convertSketch, openAndConvertSketchZip } from './format/sketch';
import refresh from './refresh';
import style from './style';
Expand All @@ -12,11 +12,17 @@ import SymbolMaster from './node/SymbolMaster';
import tools from './tools';
import control from './control';
import { version } from '../package.json';
import Root from './node/Root';

export default {
version,
parse(json: JFile, canvas: HTMLCanvasElement, dpi = 1) {
let { width, height } = canvas;
parse(json: JFile | JLayer, canvasOrRoot?: HTMLCanvasElement | Root, dpi = 1) {
if (arguments.length === 1 || canvasOrRoot instanceof Root) {
return parse(json as JLayer, canvasOrRoot as Root);
}
json = json as JFile;
canvasOrRoot = canvasOrRoot as HTMLCanvasElement;
let { width, height } = canvasOrRoot;
if (width <= 0) {
width = 1;
}
Expand All @@ -34,7 +40,7 @@ export default {
height,
},
});
root.appendTo(canvas);
root.appendTo(canvasOrRoot);

// symbolMaster优先初始化,其存在于控件页面的直接子节点,以及外部json,先收集起来
const smList: JSymbolMaster[] = [];
Expand Down
7 changes: 4 additions & 3 deletions src/node/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
JSymbolMaster,
JGroup,
JSymbolInstance,
JLayer,
} from '../format/';
import ArtBoard from './ArtBoard';
import Bitmap from './Bitmap';
Expand All @@ -25,7 +26,7 @@ import SymbolInstance from './SymbolInstance';
import Slice from './Slice';
import Root from './Root';

export function parse(json: JNode, root: Root): Node | undefined {
export function parse(json: JLayer, root?: Root): Node | undefined {
const tagName = json.tagName;
if (tagName === TAG_NAME.ART_BOARD || tagName === TAG_NAME.SYMBOL_MASTER) {
const children = [];
Expand All @@ -42,14 +43,14 @@ export function parse(json: JNode, root: Root): Node | undefined {
* 初始化前会先生成所有SymbolMaster的实例,包含内部和外部的,并存到root的symbolMasters下
* 后续进入控件页面时,页面是延迟初始化的,从json生成Node实例时,直接取缓存即可
*/
return root.symbolMasters[symbolId] || new SymbolMaster(props, children);
return root?.symbolMasters[symbolId] || new SymbolMaster(props, children);
}
return new ArtBoard(json.props as ArtBoardProps, children);
}
else if (tagName === TAG_NAME.SYMBOL_INSTANCE) {
const props = json.props as SymbolInstanceProps;
const symbolId = props.symbolId;
const sm = root.symbolMasters[symbolId];
const sm = root?.symbolMasters[symbolId];
// 应该有,前置逻辑保证被递归引用的maters先分析,可能人工或bug造成缺数据
if (sm) {
return new SymbolInstance(props, sm);
Expand Down

0 comments on commit fb5e5a5

Please sign in to comment.