-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.js
More file actions
130 lines (115 loc) · 3.48 KB
/
Copy pathcode.js
File metadata and controls
130 lines (115 loc) · 3.48 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
figma.showUI(__html__, { width: 400, height: 400 });
// UI에 현재 선택 상태를 동기화하는 함수
function syncSelectionToUI() {
const selection = figma.currentPage.selection;
const count = selection.length;
const icCount = selection.filter(node => node.name.toLowerCase().includes('ic')).length;
figma.ui.postMessage({ type: 'selection-changed', count, icCount });
}
// selectionchange 이벤트에서 UI로 선택 개수 전달
figma.on('selectionchange', syncSelectionToUI);
// 플러그인 실행 시에도 동기화
syncSelectionToUI();
// 선택된 노드를 SVG로 내보내기
async function exportSelectedNodesToSvg() {
const selectedNodes = figma.currentPage.selection;
if (selectedNodes.length === 0) {
figma.ui.postMessage({
type: 'error',
message: '내보내려는 아이콘을 선택해주세요.'
});
return;
}
const exports = [];
for (const node of selectedNodes) {
try {
const svg = await node.exportAsync({
format: 'SVG'
});
const svgString = String.fromCharCode.apply(null, svg);
exports.push({
name: node.name,
svg: svgString
});
} catch (error) {
figma.ui.postMessage({
type: 'error',
message: `"${node.name}" 내보내기 실패: ${error.message}`
});
}
}
figma.ui.postMessage({
type: 'svg-exports',
data: exports
});
}
// 터미널 명령어 실행
async function runCommand(command, cwd) {
try {
// 명령어 실행
const result = await figma.execCommand(command, cwd);
// 성공 응답 전송
figma.ui.postMessage({
type: 'command-result',
data: {
success: true,
result
}
});
} catch (error) {
// 실패 응답 전송
figma.ui.postMessage({
type: 'command-result',
data: {
success: false,
error: error.message
}
});
}
}
// 메세지 핸들러
figma.ui.onmessage = async (msg) => {
if (msg.type === 'export-svg') {
const selection = figma.currentPage.selection;
if (selection.length === 0) {
figma.notify('선택된 아이콘이 없습니다.');
return;
}
const exports = [];
for (const node of selection) {
try {
const svg = await node.exportAsync({ format: 'SVG' });
const svgString = String.fromCharCode.apply(null, svg);
exports.push({
name: node.name,
data: svgString
});
} catch (error) {
figma.notify(`${node.name} 내보내기 실패: ${error.message}`);
}
}
figma.ui.postMessage({ type: 'svg-exports', data: exports });
} else if (msg.type === 'run-command') {
await runCommand(msg.data.command, msg.data.cwd);
} else if (msg.type === 'close') {
figma.closePlugin();
} else if (msg.type === 'get-token') {
const token = await figma.clientStorage.getAsync('githubToken');
figma.ui.postMessage({
type: 'token-loaded',
token: token
});
} else if (msg.type === 'set-token') {
await figma.clientStorage.setAsync('githubToken', msg.token);
} else if (msg.type === 'delete-token') {
await figma.clientStorage.deleteAsync('githubToken');
} else if (msg.type === 'delete-svg') {
const selection = figma.currentPage.selection;
if (selection.length === 0) {
figma.notify('선택된 아이콘이 없습니다.');
return;
}
const names = selection.map(node => node.name);
figma.ui.postMessage({ type: 'delete-names', data: names });
}
};