@@ -11,8 +11,26 @@ const defaultConfig: TConfig = {
11
11
export const createFigma = ( config : TConfig ) : PluginAPI => {
12
12
const joinedConfig = { ...defaultConfig , ...config } ;
13
13
14
+ class UIAPIStub {
15
+ onmessage : MessageEventHandler | undefined ;
16
+
17
+ postMessage ( pluginMessage : any , options ?: UIPostMessageOptions ) : void {
18
+ const message = {
19
+ data : { pluginMessage, pluginId : "000000000000000000" } ,
20
+ type : "message"
21
+ } ;
22
+
23
+ // @ts -ignore
24
+ if ( global && global . onmessage ) {
25
+ // @ts -ignore
26
+ global . onmessage ( message ) ;
27
+ }
28
+ }
29
+ }
30
+
14
31
class ChildrenMixinStub implements ChildrenMixin {
15
32
children : Array < any > ;
33
+
16
34
appendChild ( item ) {
17
35
if ( ! this . children ) {
18
36
this . children = [ ] ;
@@ -40,6 +58,7 @@ export const createFigma = (config: TConfig): PluginAPI => {
40
58
item . parent = this ;
41
59
this . children . push ( item ) ;
42
60
}
61
+
43
62
insertChild ( index : number , child : any ) {
44
63
if ( ! this . children ) {
45
64
this . children = [ ] ;
@@ -73,12 +92,14 @@ export const createFigma = (config: TConfig): PluginAPI => {
73
92
child . parent = this ;
74
93
this . children . splice ( index , 0 , child ) ;
75
94
}
95
+
76
96
findAll ( callback ) {
77
97
if ( ! this . children ) {
78
98
return [ ] ;
79
99
}
80
100
return this . children . filter ( callback ) ;
81
101
}
102
+
82
103
findOne ( callback ) {
83
104
if ( ! this . children ) {
84
105
return null ;
@@ -101,12 +122,14 @@ export const createFigma = (config: TConfig): PluginAPI => {
101
122
}
102
123
this . pluginData [ key ] = value ;
103
124
}
125
+
104
126
getPluginData ( key : string ) {
105
127
if ( ! this . pluginData ) {
106
128
return ;
107
129
}
108
130
return this . pluginData [ key ] ;
109
131
}
132
+
110
133
setSharedPluginData ( namespace : string , key : string , value : string ) {
111
134
if ( ! this . sharedPluginData ) {
112
135
this . sharedPluginData = { } ;
@@ -116,12 +139,14 @@ export const createFigma = (config: TConfig): PluginAPI => {
116
139
}
117
140
this . pluginData [ key ] = value ;
118
141
}
142
+
119
143
getSharedPluginData ( namespace : string , key : string ) {
120
144
if ( ! this . sharedPluginData || ! this . sharedPluginData [ namespace ] ) {
121
145
return ;
122
146
}
123
147
return this . pluginData [ namespace ] [ key ] ;
124
148
}
149
+
125
150
remove ( ) {
126
151
this . removed = true ;
127
152
if ( this . parent ) {
@@ -147,6 +172,7 @@ export const createFigma = (config: TConfig): PluginAPI => {
147
172
this . width = width ;
148
173
this . height = height ;
149
174
}
175
+
150
176
resizeWithoutConstraints ( width , height ) {
151
177
this . width = width ;
152
178
this . height = height ;
@@ -156,53 +182,63 @@ export const createFigma = (config: TConfig): PluginAPI => {
156
182
class RectangleNodeStub {
157
183
type = "RECTANGLE" ;
158
184
}
185
+
159
186
applyMixins ( RectangleNodeStub , [ BaseNodeMixinStub , LayoutMixinStub ] ) ;
160
187
161
188
class TextNodeStub {
162
189
type = "TEXT" ;
163
190
}
191
+
164
192
applyMixins ( TextNodeStub , [ BaseNodeMixinStub , LayoutMixinStub ] ) ;
165
193
166
194
class DocumentNodeStub {
167
195
type = "DOCUMENT" ;
168
196
children = [ ] ;
169
197
}
198
+
170
199
applyMixins ( DocumentNodeStub , [ BaseNodeMixinStub , ChildrenMixinStub ] ) ;
171
200
172
201
class PageNodeStub {
173
202
type = "PAGE" ;
174
203
children = [ ] ;
175
204
}
205
+
176
206
applyMixins ( PageNodeStub , [ BaseNodeMixinStub , ChildrenMixinStub ] ) ;
177
207
178
208
class FrameNodeStub {
179
209
type = "FRAME" ;
180
210
children = [ ] ;
181
211
}
212
+
182
213
applyMixins ( FrameNodeStub , [ BaseNodeMixinStub , ChildrenMixinStub ] ) ;
183
214
184
215
class GroupNodeStub {
185
216
type = "GROUP" ;
186
217
}
218
+
187
219
applyMixins ( GroupNodeStub , [ BaseNodeMixinStub , ChildrenMixinStub ] ) ;
188
220
189
221
class ComponentNodeStub {
190
222
type = "COMPONENT" ;
191
223
children = [ ] ;
192
224
}
225
+
193
226
applyMixins ( ComponentNodeStub , [ BaseNodeMixinStub , ChildrenMixinStub ] ) ;
194
227
195
228
// @ts -ignore
196
229
class PluginApiStub implements PluginAPI {
197
230
root : DocumentNode ;
198
231
currentPage : PageNode ;
232
+ readonly ui : UIAPI ;
199
233
200
234
constructor ( ) {
201
235
// @ts -ignore
202
236
this . root = new DocumentNodeStub ( ) ;
203
237
// @ts -ignore
204
238
this . currentPage = new PageNodeStub ( ) ;
205
239
this . root . appendChild ( this . currentPage ) ;
240
+ // @ts -ignore
241
+ this . ui = new UIAPIStub ( ) ;
206
242
}
207
243
208
244
// @ts -ignore
@@ -218,24 +254,28 @@ export const createFigma = (config: TConfig): PluginAPI => {
218
254
this . currentPage . appendChild ( result ) ;
219
255
return result ;
220
256
}
257
+
221
258
// @ts -ignore
222
259
createComponent ( ) {
223
260
const result : any = new ComponentNodeStub ( ) ;
224
261
this . currentPage . appendChild ( result ) ;
225
262
return result ;
226
263
}
264
+
227
265
// @ts -ignore
228
266
createRectangle ( ) {
229
267
const result : any = new RectangleNodeStub ( ) ;
230
268
this . currentPage . appendChild ( result ) ;
231
269
return result ;
232
270
}
271
+
233
272
// @ts -ignore
234
273
createText ( ) {
235
274
const result : any = new TextNodeStub ( ) ;
236
275
this . currentPage . appendChild ( result ) ;
237
276
return result ;
238
277
}
278
+
239
279
// @ts -ignore
240
280
group ( nodes : any , parent : any , index ) {
241
281
if ( joinedConfig . simulateErrors && ( ! nodes || nodes . length === 0 ) ) {
@@ -259,3 +299,12 @@ export const createFigma = (config: TConfig): PluginAPI => {
259
299
// @ts -ignore
260
300
return new PluginApiStub ( ) ;
261
301
} ;
302
+
303
+ export const createParentPostMessage = ( figma : PluginAPI ) => (
304
+ message : { pluginMessage : any } ,
305
+ target : string
306
+ ) => {
307
+ if ( figma . ui . onmessage ) {
308
+ figma . ui . onmessage ( message . pluginMessage , { origin : null } ) ;
309
+ }
310
+ } ;
0 commit comments