|
| 1 | +// MEC clipboard — Nuke-style portable copy/paste for ComfyUI nodes. |
| 2 | +// |
| 3 | +// Ports the spirit of NukeMax's NkScript clipboard, but is fully |
| 4 | +// self-contained: no server endpoint, no TCL grammar. The payload is |
| 5 | +// plain JSON wrapped in a recognisable header so it round-trips through |
| 6 | +// the OS clipboard between workflows / instances / machines. |
| 7 | +// |
| 8 | +// Hotkeys (chosen to avoid clashing with NukeMax's Ctrl+Shift+C/V and |
| 9 | +// LiteGraph's own Ctrl+C/V): |
| 10 | +// Ctrl+Alt+C → copy selected nodes (all widget values, links, |
| 11 | +// relative positions, sizes, colours) to the clipboard. |
| 12 | +// Ctrl+Alt+V → paste the clipboard payload at the cursor. |
| 13 | +// |
| 14 | +// The payload includes EVERY widget value by name, so even if the user |
| 15 | +// pastes into a different workflow / different ComfyUI install (as long |
| 16 | +// as the same node packs are installed), the settings come across. |
| 17 | + |
| 18 | +import { app } from "../../scripts/app.js"; |
| 19 | + |
| 20 | +const CLIP_HEADER = "# MEC.clipboard 1.0"; |
| 21 | + |
| 22 | +function _toast(msg, severity = "info") { |
| 23 | + try { |
| 24 | + app.extensionManager?.toast?.add({ |
| 25 | + severity, |
| 26 | + summary: "MEC Clipboard", |
| 27 | + detail: msg, |
| 28 | + life: 3500, |
| 29 | + }); |
| 30 | + } catch (_) { console.log("[MEC.clipboard]", msg); } |
| 31 | +} |
| 32 | + |
| 33 | +function _selectedNodes() { |
| 34 | + const sel = app.canvas?.selected_nodes; |
| 35 | + if (!sel) return []; |
| 36 | + return Object.values(sel); |
| 37 | +} |
| 38 | + |
| 39 | +function _serializeNode(n) { |
| 40 | + // Capture every widget value by NAME (so widget-order changes don't |
| 41 | + // break the paste). |
| 42 | + const widgets = {}; |
| 43 | + for (const w of n.widgets || []) { |
| 44 | + if (!w.name || w.value === undefined) continue; |
| 45 | + // Skip DOM-widget values that aren't JSON-serialisable (canvas |
| 46 | + // payloads, typed arrays). serializeValue() if available. |
| 47 | + let v = w.value; |
| 48 | + try { |
| 49 | + if (typeof w.serializeValue === "function") { |
| 50 | + v = w.serializeValue(n, n.widgets.indexOf(w)); |
| 51 | + } |
| 52 | + JSON.stringify(v); |
| 53 | + } catch (_) { continue; } |
| 54 | + widgets[w.name] = v; |
| 55 | + } |
| 56 | + return { |
| 57 | + id: n.id, |
| 58 | + type: n.comfyClass || n.type, |
| 59 | + title: n.title, |
| 60 | + pos: [Math.round(n.pos?.[0] ?? 0), Math.round(n.pos?.[1] ?? 0)], |
| 61 | + size: n.size ? [Math.round(n.size[0]), Math.round(n.size[1])] : undefined, |
| 62 | + color: n.color, |
| 63 | + bgcolor: n.bgcolor, |
| 64 | + flags: n.flags, |
| 65 | + properties: n.properties, |
| 66 | + widgets, |
| 67 | + }; |
| 68 | +} |
| 69 | + |
| 70 | +function _gatherSubgraph(nodes) { |
| 71 | + const ids = new Set(nodes.map((n) => n.id)); |
| 72 | + const out_nodes = nodes.map(_serializeNode); |
| 73 | + const out_links = []; |
| 74 | + const links = app.graph?.links || {}; |
| 75 | + for (const k in links) { |
| 76 | + const l = links[k]; |
| 77 | + if (!l) continue; |
| 78 | + if (ids.has(l.origin_id) && ids.has(l.target_id)) { |
| 79 | + out_links.push({ |
| 80 | + src_id: l.origin_id, src_slot: l.origin_slot, |
| 81 | + dst_id: l.target_id, dst_slot: l.target_slot, |
| 82 | + type: l.type, |
| 83 | + }); |
| 84 | + } |
| 85 | + } |
| 86 | + return { version: 1, nodes: out_nodes, links: out_links }; |
| 87 | +} |
| 88 | + |
| 89 | +async function _copy() { |
| 90 | + const nodes = _selectedNodes(); |
| 91 | + if (!nodes.length) { _toast("Nothing selected", "warn"); return; } |
| 92 | + const payload = _gatherSubgraph(nodes); |
| 93 | + const text = CLIP_HEADER + "\n" + JSON.stringify(payload, null, 2); |
| 94 | + try { |
| 95 | + await navigator.clipboard.writeText(text); |
| 96 | + _toast(`Copied ${nodes.length} node(s) with full metadata`, "success"); |
| 97 | + } catch (e) { |
| 98 | + _toast("Clipboard write denied: " + (e?.message || e), "error"); |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +function _findHeader(text) { |
| 103 | + if (!text) return null; |
| 104 | + const i = text.indexOf(CLIP_HEADER); |
| 105 | + if (i < 0) return null; |
| 106 | + const j = text.indexOf("{", i); |
| 107 | + if (j < 0) return null; |
| 108 | + try { return JSON.parse(text.slice(j)); } catch (_) { return null; } |
| 109 | +} |
| 110 | + |
| 111 | +async function _paste() { |
| 112 | + let text = ""; |
| 113 | + try { text = await navigator.clipboard.readText(); } |
| 114 | + catch (e) { _toast("Clipboard read denied (browser permission)", "error"); return; } |
| 115 | + const data = _findHeader(text); |
| 116 | + if (!data || !Array.isArray(data.nodes)) { |
| 117 | + _toast("Clipboard does not contain MEC payload", "warn"); return; |
| 118 | + } |
| 119 | + const cursor = app.canvas?.graph_mouse || [0, 0]; |
| 120 | + let originX = null, originY = null; |
| 121 | + const idMap = {}; // old_id -> new node |
| 122 | + for (const nd of data.nodes) { |
| 123 | + const node = LiteGraph.createNode(nd.type); |
| 124 | + if (!node) { |
| 125 | + console.warn("[MEC.clipboard] unknown node type:", nd.type); |
| 126 | + _toast(`Unknown node type "${nd.type}" — install the pack`, "warn"); |
| 127 | + continue; |
| 128 | + } |
| 129 | + if (originX === null) { originX = nd.pos[0]; originY = nd.pos[1]; } |
| 130 | + node.pos = [ |
| 131 | + cursor[0] + (nd.pos[0] - originX), |
| 132 | + cursor[1] + (nd.pos[1] - originY), |
| 133 | + ]; |
| 134 | + if (nd.title) node.title = nd.title; |
| 135 | + if (nd.color) node.color = nd.color; |
| 136 | + if (nd.bgcolor) node.bgcolor = nd.bgcolor; |
| 137 | + if (nd.flags) node.flags = { ...(node.flags || {}), ...nd.flags }; |
| 138 | + if (nd.properties) node.properties = { ...(node.properties || {}), ...nd.properties }; |
| 139 | + app.graph.add(node); |
| 140 | + if (Array.isArray(nd.size)) { |
| 141 | + // Apply AFTER add() so LiteGraph doesn't auto-resize on top of us. |
| 142 | + node.size = [nd.size[0], nd.size[1]]; |
| 143 | + } |
| 144 | + // Apply widgets by NAME. |
| 145 | + for (const [wname, wval] of Object.entries(nd.widgets || {})) { |
| 146 | + const w = (node.widgets || []).find((x) => x.name === wname); |
| 147 | + if (!w) continue; |
| 148 | + try { |
| 149 | + w.value = wval; |
| 150 | + w.callback?.(wval, app.canvas, node); |
| 151 | + } catch (err) { |
| 152 | + console.warn("[MEC.clipboard] widget set failed:", wname, err); |
| 153 | + } |
| 154 | + } |
| 155 | + idMap[nd.id] = node; |
| 156 | + } |
| 157 | + // Re-wire links by old IDs. |
| 158 | + for (const l of data.links || []) { |
| 159 | + const src = idMap[l.src_id], dst = idMap[l.dst_id]; |
| 160 | + if (!src || !dst) continue; |
| 161 | + try { src.connect(l.src_slot, dst, l.dst_slot); } |
| 162 | + catch (err) { console.warn("[MEC.clipboard] link failed", err); } |
| 163 | + } |
| 164 | + // Select the freshly pasted nodes. |
| 165 | + try { |
| 166 | + app.canvas.deselectAllNodes?.(); |
| 167 | + for (const n of Object.values(idMap)) app.canvas.selectNode?.(n, true); |
| 168 | + } catch (_) {} |
| 169 | + app.graph.setDirtyCanvas(true, true); |
| 170 | + _toast(`Pasted ${Object.keys(idMap).length} node(s)`, "success"); |
| 171 | +} |
| 172 | + |
| 173 | +window.addEventListener("keydown", (ev) => { |
| 174 | + if (!ev.ctrlKey || !ev.altKey || ev.shiftKey) return; |
| 175 | + // Ignore if focus is in a text input — let the user paste into fields. |
| 176 | + const t = ev.target; |
| 177 | + if (t && (t.tagName === "INPUT" || t.tagName === "TEXTAREA" || t.isContentEditable)) return; |
| 178 | + const k = ev.key.toLowerCase(); |
| 179 | + if (k === "c") { ev.preventDefault(); _copy(); } |
| 180 | + else if (k === "v") { ev.preventDefault(); _paste(); } |
| 181 | +}); |
| 182 | + |
| 183 | +app.registerExtension({ |
| 184 | + name: "MEC.Clipboard", |
| 185 | + async setup() { |
| 186 | + console.log("[MEC.clipboard] portable JSON copy/paste loaded (Ctrl+Alt+C / Ctrl+Alt+V)"); |
| 187 | + const origMenu = LGraphCanvas.prototype.getCanvasMenuOptions; |
| 188 | + LGraphCanvas.prototype.getCanvasMenuOptions = function () { |
| 189 | + const opts = origMenu.apply(this, arguments) || []; |
| 190 | + opts.push(null); |
| 191 | + opts.push({ content: "MEC: Copy node(s) with metadata (Ctrl+Alt+C)", callback: _copy }); |
| 192 | + opts.push({ content: "MEC: Paste node(s) from clipboard (Ctrl+Alt+V)", callback: _paste }); |
| 193 | + return opts; |
| 194 | + }; |
| 195 | + // Also expose on per-node right-click menu. |
| 196 | + const origNodeMenu = LGraphCanvas.prototype.getNodeMenuOptions; |
| 197 | + LGraphCanvas.prototype.getNodeMenuOptions = function (node) { |
| 198 | + const opts = origNodeMenu.apply(this, arguments) || []; |
| 199 | + opts.push(null); |
| 200 | + opts.push({ content: "MEC: Copy with metadata", callback: () => { |
| 201 | + // Ensure this node is in the selection. |
| 202 | + try { app.canvas.selectNode?.(node, true); } catch (_) {} |
| 203 | + _copy(); |
| 204 | + }}); |
| 205 | + return opts; |
| 206 | + }; |
| 207 | + }, |
| 208 | +}); |
0 commit comments