-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcode.ts
More file actions
205 lines (153 loc) · 5.28 KB
/
Copy pathcode.ts
File metadata and controls
205 lines (153 loc) · 5.28 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
import { stringify } from "circ-json"
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;
}
}
const arrowLike = ["ARROW_LINES", "ARROW_EQUILATERAL"]
function computeDirectionOfEdge(connector: ConnectorNode) {
if (arrowLike.includes(connector.connectorEndStrokeCap)) {
if (arrowLike.includes(connector.connectorStartStrokeCap)) {
return {directional: "BI"} as const
}
else {
return {dir: {
to: "connectorEnd",
from: "connectorStart"
}, directional: "UNI"} as const
}
}
else {
if (arrowLike.includes(connector.connectorStartStrokeCap)) {
return {dir: {
to: "connectorStart",
from: "connectorEnd"
}, directional: "UNI"} as const
}
else {
return {directional: "BI"} as const
}
}
}
// get all selected nodes
figma.skipInvisibleInstanceChildren = true
function getNodeFromId(id: string) {
return figma.getNodeById(id);
}
interface Element {
text: string;
id: string;
type: "NODE" | "EDGE",
}
interface Edge extends Element {
from: Node,
to?: Node,
fromSide?: ConnectorEndpointEndpointNodeIdAndMagnet["magnet"]
toSide?: ConnectorEndpointEndpointNodeIdAndMagnet["magnet"]
color?: {r: number, g: number, b: number},
edgeType: ConnectorNode["connectorLineType"],
directional: "UNI" | "BI"
}
interface Node extends Element {
edges: Edge[],
color?: {r: number, g: number, b: number}
}
const knownEdges = new Set<string>();
const knownNodes = new Map<string, Node>();
function traverse(node: ShapeWithTextNode) {
if (knownNodes.has(node.id)) {
return knownNodes.get(node.id);
}
const edges: Edge[] = []
const myNode: Node = {
text: node.name,
id: node.id,
type: "NODE",
edges,
color: (node as any)?.fills[0]?.color
}
knownNodes.set(node.id, myNode);
for (const connector of node.attachedConnectors) {
if (knownEdges.has(connector.id)) {
continue
}
const {directional, dir} = computeDirectionOfEdge(connector);
const from = directional === "BI" ? (connector.connectorStart as ConnectorEndpointEndpointNodeIdAndMagnet)?.endpointNodeId === myNode.id ? "connectorStart" : "connectorEnd" : dir.from;
const fromIsMy = (connector[from] as ConnectorEndpointEndpointNodeIdAndMagnet)?.endpointNodeId === myNode.id;
if (!fromIsMy) continue
const to = directional === "BI" ? (connector.connectorStart as ConnectorEndpointEndpointNodeIdAndMagnet)?.endpointNodeId === myNode.id ? "connectorEnd" : "connectorStart" : dir.to;
const edge: Edge = {
text: connector.name,
id: connector.id,
from: myNode,
type: "EDGE",
color: (connector.strokes[0] as SolidPaint)?.color,
fromSide: (connector[from] as ConnectorEndpointEndpointNodeIdAndMagnet)?.magnet,
toSide: (connector[to] as ConnectorEndpointEndpointNodeIdAndMagnet)?.magnet,
edgeType: connector.connectorLineType,
directional
}
edges.push(edge);
const otherEndpointNodeId = (connector[to] as ConnectorEndpointEndpointNodeIdAndMagnet)?.endpointNodeId
if (otherEndpointNodeId) {
const otherNode = getNodeFromId(otherEndpointNodeId);
if (otherNode && otherNode.type === "SHAPE_WITH_TEXT") {
edge.to = traverse(otherNode);
}
}
knownEdges.add(connector.id);
}
return myNode
}
const selection = figma.currentPage.selection;
if (selection.length !== 1) {
(async () => {
await ui.error("Please select a single node as root")
figma.closePlugin();
})()
}
else {
const elem = selection[0];
if (elem.type !== "SHAPE_WITH_TEXT") {
(async () => {
await ui.error("Please select a node (a box with text inside) in your flowchart as root")
figma.closePlugin();
})()
}
else {
const rootNode = traverse(elem);
console.log(rootNode);
const stringified = stringify(rootNode, 2);
console.log(stringified);
(async () => {
// open window with stringified json inside
figma.showUI(__html__, { themeColors: true, width: 500, height: 400 })
figma.ui.postMessage(stringified)
// 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
}
}
})()
}
}