-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.ts
More file actions
60 lines (49 loc) · 2.44 KB
/
code.ts
File metadata and controls
60 lines (49 loc) · 2.44 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
// This plugin will open a modal to prompt the user to enter a number, and
// it will then create that many rectangles on the screen.
// This file holds the main code for the plugins. It has access to the *document*.
// You can access browser APIs in the <script> tag inside "ui.html" which has a
// full browser enviroment (see documentation).
figma.showUI(__html__);
// Calls to "parent.postMessage" from within the HTML page will trigger this
// callback. The callback will be passed the "pluginMessage" property of the
// posted message.
figma.ui.onmessage = async msg => {
// One way of distinguishing between different types of messages sent from
// your HTML page is to use an object with a "type" property like this.
if (msg.type === 'tsv') {
const allTexts = figma.currentPage.findAll().filter(item => {
return item.type === "TEXT"
}) as TextNode[];
let hash: { [index: string]: string } = {};
for (const line of msg.lines) {
const pair = line.split("\t");
if(pair.length != 2) {
continue
}
hash[pair[0]] = pair[1].trim()
}
console.log(hash);
const targetTexts = allTexts.filter(text => {
return hash[text.name]
});
//本来は結果を返して視覚的に見えるようにしてあげると親切かも、例外発生時もその旨を伝えたい
for (const target of targetTexts) {
let beforeCharacters = target.characters;
if (beforeCharacters == hash[target.name]) {
console.log("[Skipped] id: " + target.id + ", name: " + target.name + ", characters: " + target.characters);
continue;
}
try {
console.log("[Difference] ["+ target.characters + "] : [" + hash[target.name] +"]");
console.log("[Replace] id: " + target.id + ", name: " + target.name + ", characters: " + target.characters);
await figma.loadFontAsync({family: target.fontName["family"], style: target.fontName["style"]});
target.characters = hash[target.name];
} catch (error) {
console.log("loadFontAsync failed. " + target.id + " , " + error)
}
}
}
// Make sure to close the plugin when you're done. Otherwise the plugin will
// keep running, which shows the cancel button at the bottom of the screen.
figma.closePlugin();
};