-
Notifications
You must be signed in to change notification settings - Fork 0
/
code.ts
121 lines (102 loc) · 3.5 KB
/
code.ts
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
import gamutConversion from "clipboard-colorspace-conversion"
const ui = {
log: (message: string, options: Omit<NotificationOptions, "error"> = {timeout: 2000}) => {
let n: NotificationHandler;
const prom = new Promise<NotifyDequeueReason>((res) => {
n = figma.notify(message, {...options, onDequeue: res});
}) as Promise<NotifyDequeueReason> & {cancel: () => void}
prom.cancel = () => {n.cancel()}
return prom;
},
error: (message: string, options: Omit<NotificationOptions, "error" | "onDequeue"> = {timeout: 2000}) => {
let n: NotificationHandler;
const prom = new Promise<NotifyDequeueReason>((res) => {
n = figma.notify(message, {...options, error: true, onDequeue: res});
}) as Promise<NotifyDequeueReason> & {cancel: () => void}
prom.cancel = () => {n.cancel()}
return prom;
}
};
(async () => {
if (figma.editorType === "figma") {
const selection = figma.currentPage.selection;
if (selection.length !== 1) {
await ui.error("Please select a single node as root")
figma.closePlugin()
}
else {
const elem = selection[0];
if ("fills" in elem && "color" in (elem as any).fills[0]) {
if ((elem as any).fills[0].type === "SOLID") {
const color = (elem as any).fills[0].color;
console.log(color)
try {
const stringified = gamutConversion(`${color.r} ${color.g} ${color.b}`)
await copyText(stringified)
}
catch(e) {
console.error(e)
await ui.error("Failed at color gamut conversion")
figma.closePlugin()
}
}
else {
await ui.error("Please select a node with a solid fill")
figma.closePlugin()
}
}
else {
await ui.error("Please select a node with a fill")
figma.closePlugin()
}
}
}
// Runs this code if the plugin is run in FigJam
if (figma.editorType === "figjam") {
const selection = figma.currentPage.selection;
if (selection.length !== 1) {
await ui.error("Please select a single node as root")
figma.closePlugin()
}
else {
const elem = selection[0];
if ("fills" in elem && "color" in (elem as any).fills[0]) {
const color = (elem as any).fills[0].color;
console.log(color)
try {
const stringified = gamutConversion(`${color.r} ${color.g} ${color.b}`)
await copyText(stringified)
}
catch(e) {
console.error(e)
await ui.error("Failed at color gamut conversion")
figma.closePlugin()
}
}
else {
await ui.error("Please select a node with a fill")
figma.closePlugin()
}
}
}
})()
async function copyText(stringified: string) {
// open window with stringified json inside
figma.showUI(__html__, { themeColors: true, width: 500, height: 400 })
figma.ui.postMessage(stringified)
await new Promise<void>((res, rej) => {
// wait for message from window
// wait for message from window
figma.ui.onmessage = async (msg) => {
if (msg.type === "copy-success") {
figma.ui.close()
await ui.log("Copied to clipboard")
figma.closePlugin();
}
else if (msg.type === "copy-fail") {
await ui.error("Failed to copy to clipboard")
// we can leave the window open and just do nothing. When the user closes the window, the plugin will close automatically
}
}
})
}