index.ts
5.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import { tools } from 'evoltagent';
import { checkBorder } from './figma';
import { FigmaFrameInfo } from '../../types/figma-types';
import { ImageNode } from './types';
import { executeDownloadImages, fetchImages, findImageNodes } from './images';
import { cleanFigma, fetchFigmaNode, fetchFigmaImages } from './figma';
import { styleTool } from '../style-tool';
@tools({
fetchAndClean: {
description: 'Fetch and clean Figma document from URL',
params: [
{ name: 'fileId', type: 'string', description: 'Figma file ID' },
{ name: 'nodeId', type: 'string', description: 'Figma node ID' },
{ name: 'token', type: 'string', description: 'Figma API token' },
],
returns: {
type: 'FigmaFrameInfo',
description: 'Original Figma document fetched from the URL via official Figma API and cleaned by removing invisible nodes',
},
},
downloadImages: {
description: 'Detect and download image nodes from figma document',
params: [
{ name: 'fileId', type: 'string', description: 'Figma file ID' },
{ name: 'token', type: 'string', description: 'Figma API token' },
{ name: 'imageDir', type: 'string', description: 'Output directory path' },
{
name: 'document',
optional: true,
type: 'FigmaFrameInfo',
description: 'Figma document which is fetched and cleaned from the URL',
},
],
returns: {
type: '{ successCount: number; failCount: number; imageNodesMap: Map<string, ImageNode> }',
description: 'Download results of images from the Figma document with Map structure',
},
},
simplifyImageNodes: {
description: 'Simplify image nodes in figma document by replacing redundant properties with url',
params: [
{ name: 'node', type: 'FigmaFrameInfo', description: 'Figma node' },
{ name: 'imageNodes', type: 'Map<string, ImageNode>', description: 'Image nodes map with id as key' },
],
returns: {
type: 'FigmaFrameInfo',
description: 'Figma node with image nodes simplified and replaced with url',
},
},
processedStyle: {
description: 'Process styles in Figma document',
params: [{ name: 'node', type: 'FigmaFrameInfo', description: 'Figma node' }],
returns: {
type: 'FigmaFrameInfo',
description: 'Figma node with styles processed',
},
},
})
class FigmaTool {
async fetchAndClean(fileId: string, nodeId: string, token: string): Promise<FigmaFrameInfo | undefined> {
if (!fileId || !nodeId || !token) {
return undefined;
}
const document = await fetchFigmaNode(fileId, nodeId, token);
if (!document || !document?.children?.length) {
throw new Error('Failed to fetch Figma document');
}
const images = await fetchFigmaImages(fileId, nodeId, token);
const thumbnail = images?.[nodeId] || '';
if (!thumbnail) {
throw new Error('Failed to fetch Figma document thumbnail');
}
document.thumbnailUrl = thumbnail;
const cleanedDocument = cleanFigma(document);
return cleanedDocument;
}
async downloadImages(
fileId: string,
token: string,
imageDir: string,
document?: FigmaFrameInfo
): Promise<{ successCount: number; failCount: number; imageNodesMap: Map<string, ImageNode> }> {
if (!fileId) {
return { successCount: 0, failCount: 0, imageNodesMap: new Map() };
}
/* Detect images from the document */
const imageNodes = findImageNodes(document?.children || [], document?.absoluteBoundingBox);
const fetchedImages = await fetchImages(imageNodes, fileId, token);
if (!fetchedImages.length) {
return { successCount: 0, failCount: 0, imageNodesMap: new Map() };
}
return await executeDownloadImages(fetchedImages, imageDir);
}
simplifyImageNodes(node: FigmaFrameInfo, imageNodes: Map<string, ImageNode>): FigmaFrameInfo {
const imageTarget = imageNodes.get(node.id);
if (imageTarget) {
const basicInfo: FigmaFrameInfo = {
id: node.id,
name: node.name,
type: 'IMAGE',
url: imageTarget.url,
absoluteBoundingBox: node.absoluteBoundingBox,
absoluteRenderBounds: node.absoluteRenderBounds,
};
if (node.cornerRadius) {
basicInfo.cornerRadius = node.cornerRadius;
}
if (checkBorder(node)) {
basicInfo.strokes = node.strokes;
basicInfo.strokeWeight = node.strokeWeight;
basicInfo.strokeAlign = node.strokeAlign;
}
return basicInfo;
}
const result: FigmaFrameInfo = { ...node };
if (node.children && Array.isArray(node.children)) {
result.children = node.children.map(child => this.simplifyImageNodes(child, imageNodes));
}
return result;
}
processedStyle(node: FigmaFrameInfo): FigmaFrameInfo {
// Convert current node's styles using style-tool
const processedNode = styleTool.convert(node);
// Recursively process children
if (processedNode.children && Array.isArray(processedNode.children)) {
processedNode.children = processedNode.children.map(child => this.processedStyle(child));
}
return processedNode;
}
}
export const figmaTool = new FigmaTool();