refactor(figma-api): type creation methods against plugin typings
This commit is contained in:
parent
f9a75b70a3
commit
7da08789cd
|
|
@ -8,7 +8,20 @@ type IsAssignable<Actual, Expected> = Actual extends Expected ? true : false
|
|||
|
||||
export type SupportedPluginAPI = Pick<
|
||||
PluginAPI,
|
||||
'base64Decode' | 'base64Encode' | 'loadFontAsync' | 'notify'
|
||||
| 'base64Decode'
|
||||
| 'base64Encode'
|
||||
| 'loadFontAsync'
|
||||
| 'notify'
|
||||
| 'createComponent'
|
||||
| 'createEllipse'
|
||||
| 'createFrame'
|
||||
| 'createLine'
|
||||
| 'createPolygon'
|
||||
| 'createRectangle'
|
||||
| 'createSection'
|
||||
| 'createStar'
|
||||
| 'createText'
|
||||
| 'createVector'
|
||||
>
|
||||
|
||||
export type FigmaAPICompatibility = Expect<IsAssignable<FigmaAPI, SupportedPluginAPI>>
|
||||
|
|
|
|||
|
|
@ -12,6 +12,18 @@ import type {
|
|||
import { copyFills, copyStrokes, copyEffects } from '#core/scene-graph/copy'
|
||||
import type { Rect, Vector } from '#core/types'
|
||||
|
||||
import type {
|
||||
FigmaComponentNode,
|
||||
FigmaEllipseNode,
|
||||
FigmaFrameNode,
|
||||
FigmaLineNode,
|
||||
FigmaPolygonNode,
|
||||
FigmaRectangleNode,
|
||||
FigmaSectionNode,
|
||||
FigmaStarNode,
|
||||
FigmaTextNode,
|
||||
FigmaVectorNode
|
||||
} from './node-types'
|
||||
import {
|
||||
FigmaNodeProxy,
|
||||
INTERNAL_ID,
|
||||
|
|
@ -24,6 +36,18 @@ import {
|
|||
const noop = () => undefined
|
||||
|
||||
export { FigmaNodeProxy } from './proxy'
|
||||
export type {
|
||||
FigmaComponentNode,
|
||||
FigmaEllipseNode,
|
||||
FigmaFrameNode,
|
||||
FigmaLineNode,
|
||||
FigmaPolygonNode,
|
||||
FigmaRectangleNode,
|
||||
FigmaSectionNode,
|
||||
FigmaStarNode,
|
||||
FigmaTextNode,
|
||||
FigmaVectorNode
|
||||
} from './node-types'
|
||||
export type { FigmaFont, FigmaFontName } from './proxy'
|
||||
|
||||
export function computeImageHash(data: Uint8Array): string {
|
||||
|
|
@ -131,44 +155,44 @@ export class FigmaAPI implements NodeProxyHost {
|
|||
return this.wrapNode(node.id)
|
||||
}
|
||||
|
||||
createFrame(): FigmaNodeProxy {
|
||||
return this._createNode('FRAME')
|
||||
createFrame(): FigmaFrameNode {
|
||||
return this._createNode('FRAME') as FigmaFrameNode
|
||||
}
|
||||
|
||||
createRectangle(): FigmaNodeProxy {
|
||||
return this._createNode('RECTANGLE')
|
||||
createRectangle(): FigmaRectangleNode {
|
||||
return this._createNode('RECTANGLE') as FigmaRectangleNode
|
||||
}
|
||||
|
||||
createEllipse(): FigmaNodeProxy {
|
||||
return this._createNode('ELLIPSE')
|
||||
createEllipse(): FigmaEllipseNode {
|
||||
return this._createNode('ELLIPSE') as FigmaEllipseNode
|
||||
}
|
||||
|
||||
createText(): FigmaNodeProxy {
|
||||
return this._createNode('TEXT')
|
||||
createText(): FigmaTextNode {
|
||||
return this._createNode('TEXT') as FigmaTextNode
|
||||
}
|
||||
|
||||
createLine(): FigmaNodeProxy {
|
||||
return this._createNode('LINE')
|
||||
createLine(): FigmaLineNode {
|
||||
return this._createNode('LINE') as FigmaLineNode
|
||||
}
|
||||
|
||||
createPolygon(): FigmaNodeProxy {
|
||||
return this._createNode('POLYGON')
|
||||
createPolygon(): FigmaPolygonNode {
|
||||
return this._createNode('POLYGON') as FigmaPolygonNode
|
||||
}
|
||||
|
||||
createStar(): FigmaNodeProxy {
|
||||
return this._createNode('STAR')
|
||||
createStar(): FigmaStarNode {
|
||||
return this._createNode('STAR') as FigmaStarNode
|
||||
}
|
||||
|
||||
createVector(): FigmaNodeProxy {
|
||||
return this._createNode('VECTOR')
|
||||
createVector(): FigmaVectorNode {
|
||||
return this._createNode('VECTOR') as FigmaVectorNode
|
||||
}
|
||||
|
||||
createComponent(): FigmaNodeProxy {
|
||||
return this._createNode('COMPONENT')
|
||||
createComponent(): FigmaComponentNode {
|
||||
return this._createNode('COMPONENT') as FigmaComponentNode
|
||||
}
|
||||
|
||||
createSection(): FigmaNodeProxy {
|
||||
return this._createNode('SECTION')
|
||||
createSection(): FigmaSectionNode {
|
||||
return this._createNode('SECTION') as FigmaSectionNode
|
||||
}
|
||||
|
||||
createPage(): FigmaNodeProxy {
|
||||
|
|
|
|||
14
packages/core/src/figma-api/node-types.ts
Normal file
14
packages/core/src/figma-api/node-types.ts
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
/// <reference types="@figma/plugin-typings" />
|
||||
|
||||
import type { FigmaNodeProxy } from './proxy'
|
||||
|
||||
export type FigmaRectangleNode = FigmaNodeProxy & RectangleNode
|
||||
export type FigmaFrameNode = FigmaNodeProxy & FrameNode
|
||||
export type FigmaTextNode = FigmaNodeProxy & TextNode
|
||||
export type FigmaEllipseNode = FigmaNodeProxy & EllipseNode
|
||||
export type FigmaLineNode = FigmaNodeProxy & LineNode
|
||||
export type FigmaVectorNode = FigmaNodeProxy & VectorNode
|
||||
export type FigmaPolygonNode = FigmaNodeProxy & PolygonNode
|
||||
export type FigmaStarNode = FigmaNodeProxy & StarNode
|
||||
export type FigmaComponentNode = FigmaNodeProxy & ComponentNode
|
||||
export type FigmaSectionNode = FigmaNodeProxy & SectionNode
|
||||
|
|
@ -37,15 +37,17 @@ export const createVector = defineTool({
|
|||
node.fills = [{ type: 'SOLID', color: parseColor(args.fill), opacity: 1, visible: true }]
|
||||
}
|
||||
if (args.stroke) {
|
||||
node.strokes = [
|
||||
{
|
||||
color: parseColor(args.stroke),
|
||||
weight: args.stroke_weight ?? 1,
|
||||
opacity: 1,
|
||||
visible: true,
|
||||
align: 'CENTER'
|
||||
}
|
||||
]
|
||||
figma.graph.updateNode(node.id, {
|
||||
strokes: [
|
||||
{
|
||||
color: parseColor(args.stroke),
|
||||
weight: args.stroke_weight ?? 1,
|
||||
opacity: 1,
|
||||
visible: true,
|
||||
align: 'CENTER'
|
||||
}
|
||||
]
|
||||
})
|
||||
}
|
||||
if (args.parent_id) {
|
||||
const parent = figma.getNodeById(args.parent_id)
|
||||
|
|
|
|||
Loading…
Reference in a new issue