Repository navigation
Expand file tree
/
Copy pathcode.js
More file actions
276 lines (220 loc) · 10.3 KB
/
Copy pathcode.js
File metadata and controls
276 lines (220 loc) · 10.3 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
import addOnSandboxSdk from "add-on-sdk-document-sandbox";
import { editor, colorUtils, constants } from "express-document-sdk";
const { runtime } = addOnSandboxSdk.instance;
function start() {
console.log("Brand Kit Extractor sandbox initialized");
runtime.exposeApi({
async applyBrandKit(brandKit) {
try {
console.log("Applying brand kit to document:", brandKit);
const results = [];
// Apply brand colors to selected elements
if (brandKit.colors) {
const colorResult = await applyBrandColors(brandKit.colors);
if (colorResult) results.push("Applied brand colors");
}
// Apply brand fonts to selected text elements
if (brandKit.fonts) {
const fontResult = await applyBrandFonts(brandKit.fonts);
if (fontResult) results.push("Applied brand fonts");
}
// Insert brand logo
if (brandKit.logo) {
const logoResult = await insertBrandLogo(brandKit.logo);
if (logoResult) results.push("Inserted brand logo");
}
// Add design description as a text element if available
if (brandKit.designDescription) {
const descResult = await insertDesignDescription(brandKit.designDescription);
if (descResult) results.push("Added design philosophy");
}
console.log("Brand kit application results:", results);
return {
success: true,
message: `Applied brand kit: ${results.join(', ')}`,
results
};
} catch (error) {
console.error("Error applying brand kit:", error);
return { success: false, error: error.message };
}
},
async applyBrandColors(colors) {
try {
const selection = editor.context.selection;
if (selection.length === 0) {
// Create demo elements if nothing selected
await createDemoElements(colors);
return true;
}
// Apply colors to selected elements
let appliedCount = 0;
const colorArray = Object.values(colors);
for (const element of selection) {
if (canHaveFill(element)) {
const colorIndex = appliedCount % colorArray.length;
const colorFill = editor.makeColorFill(colorUtils.fromHex(colorArray[colorIndex]));
element.fill = colorFill;
appliedCount++;
}
}
return appliedCount > 0;
} catch (error) {
console.error("Error applying brand colors:", error);
return false;
}
},
async applyBrandFonts(fonts) {
try {
const selection = editor.context.selection;
if (selection.length === 0) {
// Create demo text if nothing selected
await createDemoText(fonts);
return true;
}
// Apply fonts to selected text elements
let appliedCount = 0;
const fontArray = Object.values(fonts);
for (const element of selection) {
if (element.type === constants.SceneNodeType.text && element.fullContent) {
const content = element.fullContent;
const fontIndex = appliedCount % fontArray.length;
content.characterStyleRanges = [{
length: content.text.length,
fontFamily: fontArray[fontIndex]
}];
appliedCount++;
}
}
return appliedCount > 0;
} catch (error) {
console.error("Error applying brand fonts:", error);
return false;
}
},
async insertBrandLogo(logoUrl) {
try {
const artboard = editor.documentRoot.pages.first.artboards.first;
if (!artboard) return false;
// Create a placeholder for the logo (since we can't load external images in sandbox)
const logoPlaceholder = editor.createRectangle();
logoPlaceholder.width = 120;
logoPlaceholder.height = 60;
logoPlaceholder.translation = { x: 30, y: 30 };
// Use a brand color for the placeholder
const logoFill = editor.makeColorFill(colorUtils.fromHex("#2C3E50"));
logoPlaceholder.fill = logoFill;
// Add logo text
const logoText = editor.createText("LOGO");
logoText.translation = { x: 60, y: 55 };
if (logoText.fullContent) {
const content = logoText.fullContent;
content.characterStyleRanges = [{
length: content.text.length,
fontFamily: "Arial",
fontSize: 16,
fontWeight: "bold"
}];
}
// Add to document
artboard.children.append(logoPlaceholder);
artboard.children.append(logoText);
console.log("Inserted brand logo placeholder");
return true;
} catch (error) {
console.error("Error inserting brand logo:", error);
return false;
}
},
async insertDesignDescription(description) {
try {
const artboard = editor.documentRoot.pages.first.artboards.first;
if (!artboard) return false;
// Create title text
const titleText = editor.createText("Design Philosophy");
titleText.translation = { x: 30, y: 120 };
if (titleText.fullContent) {
const content = titleText.fullContent;
content.characterStyleRanges = [{
length: content.text.length,
fontFamily: "Arial",
fontSize: 18,
fontWeight: "bold"
}];
}
// Create description text (truncated for display)
const shortDesc = description.length > 200 ? description.substring(0, 200) + "..." : description;
const descText = editor.createText(shortDesc);
descText.translation = { x: 30, y: 150 };
if (descText.fullContent) {
const content = descText.fullContent;
content.characterStyleRanges = [{
length: content.text.length,
fontFamily: "Arial",
fontSize: 12,
fontWeight: "normal"
}];
}
// Add to document
artboard.children.append(titleText);
artboard.children.append(descText);
console.log("Inserted design description");
return true;
} catch (error) {
console.error("Error inserting design description:", error);
return false;
}
}
});
}
async function createDemoElements(colors) {
try {
const artboard = editor.documentRoot.pages.first.artboards.first;
if (!artboard) return;
const colorArray = Object.values(colors);
// Create demo rectangles with brand colors
colorArray.forEach((color, index) => {
const rect = editor.createRectangle();
rect.width = 80;
rect.height = 80;
rect.translation = { x: 50 + (index * 100), y: 100 };
const colorFill = editor.makeColorFill(colorUtils.fromHex(color));
rect.fill = colorFill;
artboard.children.append(rect);
});
console.log("Created demo elements with brand colors");
} catch (error) {
console.error("Error creating demo elements:", error);
}
}
async function createDemoText(fonts) {
try {
const artboard = editor.documentRoot.pages.first.artboards.first;
if (!artboard) return;
const fontArray = Object.values(fonts);
// Create demo text with brand fonts
fontArray.forEach((font, index) => {
const text = editor.createText(`Brand Text ${index + 1}`);
text.translation = { x: 50, y: 200 + (index * 50) };
if (text.fullContent) {
const content = text.fullContent;
content.characterStyleRanges = [{
length: content.text.length,
fontFamily: font,
fontSize: 18
}];
}
artboard.children.append(text);
});
console.log("Created demo text with brand fonts");
} catch (error) {
console.error("Error creating demo text:", error);
}
}
function canHaveFill(element) {
return element.type === constants.SceneNodeType.rectangle ||
element.type === constants.SceneNodeType.ellipse ||
element.type === constants.SceneNodeType.complexShape ||
element.type === constants.SceneNodeType.solidColorShape;
}
start();