figma-mcp-console 0.5.3 → 0.5.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Binary file
Binary file
Binary file
Binary file
@@ -41,6 +41,38 @@ function summarize(n) {
41
41
  return s;
42
42
  }
43
43
 
44
+ // The file key needed to build share links. Published plugins can read it from
45
+ // figma.fileKey, but development plugins (imported from a manifest) cannot, so
46
+ // we fall back to a key the user pastes once — their file URL — persisted in
47
+ // the file via pluginData so it survives reopening.
48
+ const FILE_KEY_DATA = "figma-mcp-file-key";
49
+ let fileKey = figma.fileKey || "";
50
+ try {
51
+ if (!fileKey) fileKey = figma.root.getPluginData(FILE_KEY_DATA) || "";
52
+ } catch (e) {}
53
+
54
+ function setFileKey(key) {
55
+ fileKey = key || "";
56
+ try {
57
+ figma.root.setPluginData(FILE_KEY_DATA, fileKey);
58
+ } catch (e) {}
59
+ }
60
+
61
+ // nodeLink builds a shareable figma.com URL that jumps straight to a node (the
62
+ // same link as Figma's "Copy link to selection"). Returns null when no file
63
+ // key is known yet, so callers simply omit the link.
64
+ function nodeLink(id) {
65
+ if (!fileKey) return null;
66
+ return (
67
+ "https://www.figma.com/design/" +
68
+ fileKey +
69
+ "/" +
70
+ encodeURIComponent(figma.root.name) +
71
+ "?node-id=" +
72
+ id.replace(/:/g, "-")
73
+ );
74
+ }
75
+
44
76
  function serialize(n, depth) {
45
77
  const s = summarize(n);
46
78
  if ("visible" in n) s.visible = n.visible;
@@ -158,7 +190,12 @@ const handlers = {
158
190
  },
159
191
 
160
192
  async get_selection() {
161
- return figma.currentPage.selection.map(summarize);
193
+ return figma.currentPage.selection.map((n) => {
194
+ const s = summarize(n);
195
+ const link = nodeLink(n.id);
196
+ if (link) s.link = link;
197
+ return s;
198
+ });
162
199
  },
163
200
 
164
201
  async get_design_context(params) {
@@ -714,10 +751,59 @@ const handlers = {
714
751
  },
715
752
  };
716
753
 
754
+ // ---------- live selection panel ----------
755
+ // Mirror the current selection into the plugin window so the user can grab a
756
+ // frame's name and shareable link without going through the AI. Refreshes on
757
+ // every selection change and resizes the window to fit the list.
758
+ // selectionGen guards against stale posts: SVG export is async, so a newer
759
+ // selection change may land while an older one is still exporting.
760
+ let selectionGen = 0;
761
+
762
+ async function postSelection() {
763
+ const gen = ++selectionGen;
764
+ const sel = figma.currentPage.selection;
765
+ const items = [];
766
+ for (const n of sel) {
767
+ const item = { name: n.name, type: n.type, link: nodeLink(n.id) };
768
+ // Frames are copied as links; every other element (vectors, instances, …)
769
+ // is copied as SVG, so pre-export it now to keep the UI copy synchronous
770
+ // (clipboard writes must happen inside the click gesture). Skip huge
771
+ // selections to avoid stalling on a big export the user won't copy.
772
+ if (n.type !== "FRAME" && sel.length <= 50 && typeof n.exportAsync === "function") {
773
+ try {
774
+ item.svg = await n.exportAsync({ format: "SVG_STRING" });
775
+ } catch (e) {
776
+ item.svgError = String((e && e.message) || e);
777
+ }
778
+ if (gen !== selectionGen) return; // superseded by a newer selection
779
+ }
780
+ items.push(item);
781
+ }
782
+ const needsKey = !fileKey && items.some((it) => it.type === "FRAME");
783
+ figma.ui.postMessage({ selection: items, hasKey: !!fileKey });
784
+ // Room for: base card + filter bar (30) + optional Copy-all header (22) +
785
+ // the paste-URL prompt when a frame needs a key (64) + up to 6 visible rows
786
+ // (the list scrolls beyond that).
787
+ const rows = Math.min(items.length, 6);
788
+ const h = items.length
789
+ ? 162 + (items.length > 1 ? 22 : 0) + (needsKey ? 64 : 0) + rows * 30
790
+ : 132;
791
+ figma.ui.resize(300, h);
792
+ }
793
+ figma.on("selectionchange", postSelection);
794
+ postSelection();
795
+
717
796
  // ---------- dispatch ----------
718
797
 
719
798
  figma.ui.onmessage = async (msg) => {
720
- if (!msg || !msg.id || !msg.command) return;
799
+ if (!msg) return;
800
+ // Control message from the UI: the user pasted the file URL to enable links.
801
+ if (typeof msg.setFileKey === "string") {
802
+ setFileKey(msg.setFileKey);
803
+ postSelection();
804
+ return;
805
+ }
806
+ if (!msg.id || !msg.command) return;
721
807
  const h = handlers[msg.command];
722
808
  try {
723
809
  if (!h) throw new Error("unknown command: " + msg.command);
@@ -2,10 +2,12 @@
2
2
  * {
3
3
  box-sizing: border-box;
4
4
  }
5
+
5
6
  html,
6
7
  body {
7
8
  height: 100%;
8
9
  }
10
+
9
11
  body {
10
12
  font:
11
13
  12px/1.5 -apple-system,
@@ -19,6 +21,7 @@
19
21
  display: flex;
20
22
  flex-direction: column;
21
23
  }
24
+
22
25
  .card {
23
26
  background: #fff;
24
27
  border: 1px solid #e6e8f0;
@@ -26,6 +29,7 @@
26
29
  padding: 9px 11px;
27
30
  box-shadow: 0 1px 2px rgba(30, 34, 60, 0.04);
28
31
  }
32
+
29
33
  #status-row {
30
34
  display: flex;
31
35
  align-items: center;
@@ -35,6 +39,7 @@
35
39
  font-size: 13px;
36
40
  color: #1f2330;
37
41
  }
42
+
38
43
  #dot {
39
44
  flex: none;
40
45
  width: 9px;
@@ -46,16 +51,181 @@
46
51
  background 0.2s,
47
52
  box-shadow 0.2s;
48
53
  }
54
+
49
55
  #dot.on {
50
56
  background: #22a06b;
51
57
  box-shadow: 0 0 0 3px rgba(34, 160, 107, 0.15);
52
58
  }
59
+
53
60
  #hint {
54
61
  color: #6b6f7d;
55
62
  margin-top: 8px;
56
63
  font-size: 11px;
57
64
  line-height: 1.45;
58
65
  }
66
+
67
+ #result {
68
+ display: none;
69
+ margin-top: 10px;
70
+ border-top: 1px solid #eceef5;
71
+ padding-top: 9px;
72
+ }
73
+
74
+ #result.show {
75
+ display: block;
76
+ }
77
+
78
+ .res-head {
79
+ display: flex;
80
+ align-items: center;
81
+ justify-content: space-between;
82
+ margin-bottom: 6px;
83
+ }
84
+
85
+ .res-title {
86
+ font-weight: 600;
87
+ font-size: 11px;
88
+ color: #1f2330;
89
+ }
90
+
91
+ .res-list {
92
+ display: flex;
93
+ flex-direction: column;
94
+ gap: 4px;
95
+ max-height: 190px;
96
+ overflow: auto;
97
+ }
98
+
99
+ .res-row {
100
+ display: flex;
101
+ align-items: center;
102
+ gap: 6px;
103
+ padding: 5px 7px;
104
+ border: 1px solid #e6e8f0;
105
+ border-radius: 7px;
106
+ background: #fbfcfe;
107
+ }
108
+
109
+ .res-name {
110
+ flex: 1;
111
+ min-width: 0;
112
+ overflow: hidden;
113
+ text-overflow: ellipsis;
114
+ white-space: nowrap;
115
+ font-size: 11px;
116
+ color: #2c2c34;
117
+ }
118
+
119
+ .res-type {
120
+ flex: none;
121
+ font-size: 9px;
122
+ color: #a0a4b0;
123
+ text-transform: uppercase;
124
+ letter-spacing: 0.03em;
125
+ }
126
+
127
+ .copy-btn {
128
+ flex: none;
129
+ border: 1px solid #d6d9e6;
130
+ background: #fff;
131
+ color: #4b5168;
132
+ border-radius: 6px;
133
+ padding: 2px 8px;
134
+ font-size: 10px;
135
+ cursor: pointer;
136
+ }
137
+
138
+ .copy-btn:hover {
139
+ background: #f0f2f8;
140
+ }
141
+
142
+ .copy-btn.done {
143
+ color: #22a06b;
144
+ border-color: #bfe6d3;
145
+ background: #f0faf5;
146
+ }
147
+
148
+ .copy-all {
149
+ padding: 1px 6px;
150
+ font-size: 10px;
151
+ }
152
+
153
+ .filter-row {
154
+ display: flex;
155
+ gap: 4px;
156
+ margin-bottom: 8px;
157
+ }
158
+
159
+ .filter-btn {
160
+ flex: none;
161
+ border: 1px solid #d6d9e6;
162
+ background: #fff;
163
+ color: #6b6f7d;
164
+ border-radius: 6px;
165
+ padding: 3px 9px;
166
+ font-size: 10px;
167
+ cursor: pointer;
168
+ }
169
+
170
+ .filter-btn:hover {
171
+ background: #f0f2f8;
172
+ }
173
+
174
+ .filter-btn.on {
175
+ background: #2c2c34;
176
+ border-color: #2c2c34;
177
+ color: #fff;
178
+ }
179
+
180
+ .res-empty {
181
+ font-size: 11px;
182
+ color: #a0a4b0;
183
+ padding: 6px 2px;
184
+ }
185
+
186
+ .key-prompt {
187
+ margin-bottom: 8px;
188
+ padding: 7px 8px;
189
+ border: 1px solid #ffe0a6;
190
+ background: #fff9ec;
191
+ border-radius: 7px;
192
+ }
193
+
194
+ .key-note {
195
+ font-size: 10px;
196
+ color: #8a6d3b;
197
+ line-height: 1.4;
198
+ margin-bottom: 5px;
199
+ }
200
+
201
+ .key-input-row {
202
+ display: flex;
203
+ gap: 4px;
204
+ }
205
+
206
+ .key-input {
207
+ flex: 1;
208
+ min-width: 0;
209
+ border: 1px solid #d6d9e6;
210
+ border-radius: 6px;
211
+ padding: 3px 6px;
212
+ font-size: 10px;
213
+ }
214
+
215
+ .key-save {
216
+ flex: none;
217
+ border: 1px solid #d6d9e6;
218
+ background: #fff;
219
+ border-radius: 6px;
220
+ padding: 3px 10px;
221
+ font-size: 10px;
222
+ cursor: pointer;
223
+ }
224
+
225
+ .key-save:hover {
226
+ background: #f0f2f8;
227
+ }
228
+
59
229
  #credit {
60
230
  margin-top: auto;
61
231
  padding-top: 10px;
@@ -63,10 +233,12 @@
63
233
  color: #a0a4b0;
64
234
  font-size: 10px;
65
235
  }
236
+
66
237
  #credit a {
67
238
  color: #8288a0;
68
239
  text-decoration: none;
69
240
  }
241
+
70
242
  #credit a:hover {
71
243
  text-decoration: underline;
72
244
  }
@@ -80,11 +252,11 @@
80
252
  Keep this window open while AI works. Open the plugin in each file you want
81
253
  it to edit.
82
254
  </div>
255
+ <div id="result"></div>
83
256
  </div>
84
257
  <div id="credit">
85
- <a href="https://github.com/hoangann2000/figma-mcp-console" target="_blank"
86
- >@hoangann2000</a
87
- >
258
+ <a href="https://github.com/hoangann2000/figma-mcp-console" target="_blank">@hoangann2000</a>
259
+ <a href="https://github.com/fuvavi" target="_blank">@fuvavi</a>
88
260
  </div>
89
261
 
90
262
  <script>
@@ -160,7 +332,7 @@
160
332
  sock.onerror = () => {
161
333
  try {
162
334
  sock.close();
163
- } catch (e) {}
335
+ } catch (e) { }
164
336
  };
165
337
  }
166
338
 
@@ -173,8 +345,178 @@
173
345
  }, RETRY);
174
346
  }
175
347
 
348
+ // ---- live selection list (name + copyable link, filterable by type) ----
349
+ let selItems = []; // everything currently selected in Figma
350
+ let shownItems = []; // selItems after the active type filter
351
+ let selFilter = "all"; // all | frame | vector
352
+ let hasKey = false; // whether the main thread knows this file's key
353
+
354
+ // Pull the file key out of a pasted Figma URL (…/design/<key>/…), or accept
355
+ // a bare key. Returns "" when nothing usable is found.
356
+ function parseFileKey(s) {
357
+ s = (s || "").trim();
358
+ const m = s.match(/figma\.com\/(?:file|design|board|proto)\/([A-Za-z0-9]+)/i);
359
+ if (m) return m[1];
360
+ if (/^[A-Za-z0-9]{10,}$/.test(s)) return s;
361
+ return "";
362
+ }
363
+
364
+ const FILTERS = [
365
+ { key: "all", label: "All" },
366
+ { key: "frame", label: "Frame" },
367
+ { key: "vector", label: "Vector" },
368
+ { key: "instance", label: "Instance" },
369
+ ];
370
+
371
+ function esc(s) {
372
+ return String(s)
373
+ .replace(/&/g, "&amp;")
374
+ .replace(/</g, "&lt;")
375
+ .replace(/>/g, "&gt;")
376
+ .replace(/"/g, "&quot;");
377
+ }
378
+
379
+ function matches(it) {
380
+ if (selFilter === "frame") return it.type === "FRAME";
381
+ if (selFilter === "vector") return it.type === "VECTOR";
382
+ if (selFilter === "instance") return it.type === "INSTANCE";
383
+ return true;
384
+ }
385
+
386
+ // Frames copy as a link block; every other element copies its exported SVG.
387
+ function copyLabelFor(it) {
388
+ return it.type === "FRAME" ? "Copy" : "Copy SVG";
389
+ }
390
+
391
+ // Copyable text for one item. Frames use the requested link format:
392
+ // # <name>:
393
+ // <link>
394
+ // Other elements return their SVG (or the export error, so the click still
395
+ // gives feedback instead of silently copying nothing).
396
+ function copyValueForItem(it) {
397
+ if (it.type === "FRAME") return "# " + it.name + ":\n" + (it.link || "");
398
+ return it.svg || it.svgError || "";
399
+ }
400
+
401
+ function renderSelection(items) {
402
+ if (items) selItems = items;
403
+ drawSelection();
404
+ }
405
+
406
+ function drawSelection() {
407
+ const box = document.getElementById("result");
408
+ if (!selItems.length) {
409
+ box.className = "";
410
+ box.innerHTML = "";
411
+ return;
412
+ }
413
+ shownItems = selItems.filter(matches);
414
+
415
+ // Only frames need a link (and therefore the file key); a pure vector /
416
+ // instance selection copies SVG, so don't nag for a URL then.
417
+ const needsKey = !hasKey && selItems.some((it) => it.type === "FRAME");
418
+ const keyPrompt = !needsKey
419
+ ? ""
420
+ : '<div class="key-prompt">' +
421
+ '<div class="key-note">Links are empty: this file has no key yet. Paste the file URL once ' +
422
+ "(Share → Copy link, or the browser address bar):</div>" +
423
+ '<div class="key-input-row">' +
424
+ '<input class="key-input" id="key-input" placeholder="https://www.figma.com/design/…" />' +
425
+ '<button class="key-save" id="key-save">Save</button>' +
426
+ "</div></div>";
427
+
428
+ const filterBar =
429
+ '<div class="filter-row">' +
430
+ FILTERS.map(
431
+ (f) =>
432
+ '<button class="filter-btn' + (selFilter === f.key ? " on" : "") + '" data-f="' + f.key + '">' +
433
+ f.label + "</button>",
434
+ ).join("") +
435
+ "</div>";
436
+
437
+ const head =
438
+ '<div class="res-head"><span class="res-title">Selected (' + shownItems.length + ")</span>" +
439
+ (shownItems.length > 1 ? '<button class="copy-btn copy-all" data-all="1">Copy all</button>' : "") +
440
+ "</div>";
441
+
442
+ const body = shownItems.length
443
+ ? '<div class="res-list">' +
444
+ shownItems
445
+ .map(
446
+ (it, i) =>
447
+ '<div class="res-row">' +
448
+ '<span class="res-name" title="' + esc(it.name) + '">' + esc(it.name) + "</span>" +
449
+ '<span class="res-type">' + esc(it.type) + "</span>" +
450
+ '<button class="copy-btn" data-i="' + i + '">' + copyLabelFor(it) + "</button>" +
451
+ "</div>",
452
+ )
453
+ .join("") +
454
+ "</div>"
455
+ : '<div class="res-empty">No matching nodes selected.</div>';
456
+
457
+ box.innerHTML = keyPrompt + filterBar + head + body;
458
+ box.className = "show";
459
+ }
460
+
461
+ // What each button copies: a row copies its own value (link block for a
462
+ // frame, SVG otherwise); "Copy all" joins every shown item with a blank
463
+ // line between — filter to one type first for a homogeneous result.
464
+ function copyValueFor(btn) {
465
+ if (btn.hasAttribute("data-all")) {
466
+ return shownItems.map(copyValueForItem).join("\n\n");
467
+ }
468
+ const it = shownItems[+btn.getAttribute("data-i")];
469
+ return it ? copyValueForItem(it) : "";
470
+ }
471
+
472
+ // navigator.clipboard is often unavailable in the plugin iframe, so use the
473
+ // textarea + execCommand fallback which works reliably here.
474
+ function copyText(text) {
475
+ const ta = document.createElement("textarea");
476
+ ta.value = text;
477
+ ta.style.position = "fixed";
478
+ ta.style.opacity = "0";
479
+ document.body.appendChild(ta);
480
+ ta.select();
481
+ try {
482
+ document.execCommand("copy");
483
+ } catch (e) { }
484
+ document.body.removeChild(ta);
485
+ }
486
+
487
+ document.getElementById("result").addEventListener("click", (e) => {
488
+ if (e.target.id === "key-save") {
489
+ const inp = document.getElementById("key-input");
490
+ const key = parseFileKey(inp ? inp.value : "");
491
+ if (key) {
492
+ parent.postMessage({ pluginMessage: { setFileKey: key } }, "*");
493
+ } else if (inp) {
494
+ inp.value = "";
495
+ inp.placeholder = "Couldn't read a file key from that — paste the full URL";
496
+ }
497
+ return;
498
+ }
499
+ const fb = e.target.closest(".filter-btn");
500
+ if (fb) {
501
+ selFilter = fb.getAttribute("data-f");
502
+ drawSelection();
503
+ return;
504
+ }
505
+ const btn = e.target.closest(".copy-btn");
506
+ if (!btn) return;
507
+ copyText(copyValueFor(btn));
508
+ const old = btn.textContent;
509
+ btn.textContent = "Copied";
510
+ btn.classList.add("done");
511
+ setTimeout(() => {
512
+ btn.textContent = old;
513
+ btn.classList.remove("done");
514
+ }, 1200);
515
+ });
516
+
176
517
  // Relay plugin main-thread frames: the register frame is cached here and
177
- // re-sent on every reconnect; everything else goes to the server.
518
+ // re-sent on every reconnect, the selection frame drives the list above, and
519
+ // everything else goes to the server.
178
520
  window.onmessage = (e) => {
179
521
  const msg = e.data.pluginMessage;
180
522
  if (!msg) return;
@@ -184,6 +526,11 @@
184
526
  if (ws) setStatus(true, label());
185
527
  return;
186
528
  }
529
+ if (Array.isArray(msg.selection)) {
530
+ hasKey = !!msg.hasKey;
531
+ renderSelection(msg.selection);
532
+ return;
533
+ }
187
534
  if (ws && ws.readyState === WebSocket.OPEN) {
188
535
  ws.send(JSON.stringify(msg));
189
536
  }
@@ -195,4 +542,4 @@
195
542
  });
196
543
 
197
544
  connect();
198
- </script>
545
+ </script>
Binary file
Binary file
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "figma-mcp-console",
3
- "version": "0.5.3",
3
+ "version": "0.5.4",
4
4
  "scripts": {
5
5
  "prepublishOnly": "cp ../README.md ./README.md"
6
6
  },