figma-json-plugin
Advanced tools
Comparing version 0.0.5-alpha.2 to 0.0.5-alpha.3
@@ -1,3 +0,3 @@ | ||
/// <reference types="@figma/plugin-typings" /> | ||
import * as F from "./figma-json"; | ||
import "@figma/plugin-typings"; | ||
export * from "./figma-json"; | ||
@@ -4,0 +4,0 @@ export declare const readBlacklist: Set<string>; |
"use strict"; | ||
(() => { | ||
var __defProp = Object.defineProperty; | ||
var __defProps = Object.defineProperties; | ||
var __getOwnPropDescs = Object.getOwnPropertyDescriptors; | ||
var __getOwnPropSymbols = Object.getOwnPropertySymbols; | ||
var __hasOwnProp = Object.prototype.hasOwnProperty; | ||
var __propIsEnum = Object.prototype.propertyIsEnumerable; | ||
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; | ||
var __spreadValues = (a, b) => { | ||
for (var prop in b || (b = {})) | ||
if (__hasOwnProp.call(b, prop)) | ||
var __defProp = Object.defineProperty; | ||
var __defProps = Object.defineProperties; | ||
var __getOwnPropDesc = Object.getOwnPropertyDescriptor; | ||
var __getOwnPropDescs = Object.getOwnPropertyDescriptors; | ||
var __getOwnPropNames = Object.getOwnPropertyNames; | ||
var __getOwnPropSymbols = Object.getOwnPropertySymbols; | ||
var __hasOwnProp = Object.prototype.hasOwnProperty; | ||
var __propIsEnum = Object.prototype.propertyIsEnumerable; | ||
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; | ||
var __spreadValues = (a, b) => { | ||
for (var prop in b || (b = {})) | ||
if (__hasOwnProp.call(b, prop)) | ||
__defNormalProp(a, prop, b[prop]); | ||
if (__getOwnPropSymbols) | ||
for (var prop of __getOwnPropSymbols(b)) { | ||
if (__propIsEnum.call(b, prop)) | ||
__defNormalProp(a, prop, b[prop]); | ||
if (__getOwnPropSymbols) | ||
for (var prop of __getOwnPropSymbols(b)) { | ||
if (__propIsEnum.call(b, prop)) | ||
__defNormalProp(a, prop, b[prop]); | ||
} | ||
return a; | ||
}; | ||
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b)); | ||
var __objRest = (source, exclude) => { | ||
var target = {}; | ||
for (var prop in source) | ||
if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0) | ||
} | ||
return a; | ||
}; | ||
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b)); | ||
var __objRest = (source, exclude) => { | ||
var target = {}; | ||
for (var prop in source) | ||
if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0) | ||
target[prop] = source[prop]; | ||
if (source != null && __getOwnPropSymbols) | ||
for (var prop of __getOwnPropSymbols(source)) { | ||
if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop)) | ||
target[prop] = source[prop]; | ||
if (source != null && __getOwnPropSymbols) | ||
for (var prop of __getOwnPropSymbols(source)) { | ||
if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop)) | ||
target[prop] = source[prop]; | ||
} | ||
return target; | ||
}; | ||
var __export = (target, all) => { | ||
for (var name in all) | ||
__defProp(target, name, { get: all[name], enumerable: true }); | ||
}; | ||
var __copyProps = (to, from, except, desc) => { | ||
if (from && typeof from === "object" || typeof from === "function") { | ||
for (let key of __getOwnPropNames(from)) | ||
if (!__hasOwnProp.call(to, key) && key !== except) | ||
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); | ||
} | ||
return to; | ||
}; | ||
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); | ||
var __async = (__this, __arguments, generator) => { | ||
return new Promise((resolve, reject) => { | ||
var fulfilled = (value) => { | ||
try { | ||
step(generator.next(value)); | ||
} catch (e) { | ||
reject(e); | ||
} | ||
return target; | ||
}; | ||
var __async = (__this, __arguments, generator) => { | ||
return new Promise((resolve, reject) => { | ||
var fulfilled = (value) => { | ||
try { | ||
step(generator.next(value)); | ||
} catch (e) { | ||
reject(e); | ||
} | ||
}; | ||
var rejected = (value) => { | ||
try { | ||
step(generator.throw(value)); | ||
} catch (e) { | ||
reject(e); | ||
} | ||
}; | ||
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected); | ||
step((generator = generator.apply(__this, __arguments)).next()); | ||
}); | ||
}; | ||
}; | ||
var rejected = (value) => { | ||
try { | ||
step(generator.throw(value)); | ||
} catch (e) { | ||
reject(e); | ||
} | ||
}; | ||
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected); | ||
step((generator = generator.apply(__this, __arguments)).next()); | ||
}); | ||
}; | ||
// src/figma-json.ts | ||
var MixedValue = "__Symbol(figma.mixed)__"; | ||
// src/index.ts | ||
var src_exports = {}; | ||
__export(src_exports, { | ||
MixedValue: () => MixedValue, | ||
decodeFont: () => decodeFont, | ||
dump: () => dump, | ||
encodeFont: () => encodeFont, | ||
fontsToLoad: () => fontsToLoad, | ||
insert: () => insert, | ||
preflightFonts: () => preflightFonts, | ||
readBlacklist: () => readBlacklist, | ||
writeBlacklist: () => writeBlacklist | ||
}); | ||
module.exports = __toCommonJS(src_exports); | ||
// src/updateImageHashes.ts | ||
function updateImageHashes(n, updates) { | ||
n = __spreadValues({}, n); | ||
if ("children" in n && n.children !== void 0) { | ||
const children = n.children.map((c) => updateImageHashes(c, updates)); | ||
n = __spreadProps(__spreadValues({}, n), { children }); | ||
} | ||
const fixFills = (fills) => { | ||
return fills.map((f) => { | ||
if (f.type === "IMAGE" && typeof f.imageHash === "string") { | ||
const imageHash = updates.get(f.imageHash) || null; | ||
if (typeof f.imageHash === "string") { | ||
return __spreadProps(__spreadValues({}, f), { imageHash }); | ||
} else { | ||
return f; | ||
} | ||
// src/figma-json.ts | ||
var MixedValue = "__Symbol(figma.mixed)__"; | ||
// src/index.ts | ||
var import_plugin_typings = require("@figma/plugin-typings"); | ||
// src/updateImageHashes.ts | ||
function updateImageHashes(n, updates) { | ||
n = __spreadValues({}, n); | ||
if ("children" in n && n.children !== void 0) { | ||
const children = n.children.map((c) => updateImageHashes(c, updates)); | ||
n = __spreadProps(__spreadValues({}, n), { children }); | ||
} | ||
const fixFills = (fills) => { | ||
return fills.map((f) => { | ||
if (f.type === "IMAGE" && typeof f.imageHash === "string") { | ||
const imageHash = updates.get(f.imageHash) || null; | ||
if (typeof f.imageHash === "string") { | ||
return __spreadProps(__spreadValues({}, f), { imageHash }); | ||
} else { | ||
return f; | ||
} | ||
}); | ||
}; | ||
if ("fills" in n && n.fills !== void 0 && n.fills !== "__Symbol(figma.mixed)__") { | ||
n.fills = fixFills(n.fills); | ||
} | ||
if ("backgrounds" in n && n.backgrounds !== void 0) { | ||
n.backgrounds = fixFills(n.backgrounds); | ||
} | ||
return n; | ||
} else { | ||
return f; | ||
} | ||
}); | ||
}; | ||
if ("fills" in n && n.fills !== void 0 && n.fills !== "__Symbol(figma.mixed)__") { | ||
n.fills = fixFills(n.fills); | ||
} | ||
if ("backgrounds" in n && n.backgrounds !== void 0) { | ||
n.backgrounds = fixFills(n.backgrounds); | ||
} | ||
return n; | ||
} | ||
// src/index.ts | ||
var readBlacklist = /* @__PURE__ */ new Set([ | ||
"parent", | ||
"removed", | ||
"stuckNodes", | ||
"__proto__", | ||
"id", | ||
"remote", | ||
"hasMissingFont", | ||
"absoluteTransform", | ||
"absoluteRenderBounds", | ||
"vectorNetwork" | ||
]); | ||
var writeBlacklist = /* @__PURE__ */ new Set([ | ||
"id", | ||
"componentPropertyReferences", | ||
"variantProperties", | ||
"vectorNetwork" | ||
]); | ||
function notUndefined(x) { | ||
return x !== void 0; | ||
} | ||
function dump(n) { | ||
return __async(this, null, function* () { | ||
const imageHashes = /* @__PURE__ */ new Set(); | ||
const _dumpObject = (n2, keys) => keys.reduce((o, k) => { | ||
const v = n2[k]; | ||
if (k === "imageHash" && typeof v === "string") { | ||
imageHashes.add(v); | ||
// src/index.ts | ||
var readBlacklist = /* @__PURE__ */ new Set([ | ||
"parent", | ||
"removed", | ||
"stuckNodes", | ||
"__proto__", | ||
"id", | ||
"remote", | ||
"hasMissingFont", | ||
"absoluteTransform", | ||
"absoluteRenderBounds", | ||
"vectorNetwork" | ||
]); | ||
var writeBlacklist = /* @__PURE__ */ new Set([ | ||
"id", | ||
"componentPropertyReferences", | ||
"variantProperties", | ||
"vectorNetwork" | ||
]); | ||
function notUndefined(x) { | ||
return x !== void 0; | ||
} | ||
function dump(n) { | ||
return __async(this, null, function* () { | ||
const imageHashes = /* @__PURE__ */ new Set(); | ||
const _dumpObject = (n2, keys) => keys.reduce((o, k) => { | ||
const v = n2[k]; | ||
if (k === "imageHash" && typeof v === "string") { | ||
imageHashes.add(v); | ||
} | ||
o[k] = _dump(v); | ||
return o; | ||
}, {}); | ||
const _dump = (n2) => { | ||
switch (typeof n2) { | ||
case "object": { | ||
if (Array.isArray(n2)) { | ||
return n2.map((v) => _dump(v)); | ||
} else if (n2 === null) { | ||
return null; | ||
} else if (n2.__proto__ !== void 0) { | ||
const keys = [...Object.keys(n2), ...Object.keys(n2.__proto__)].filter( | ||
(k) => !readBlacklist.has(k) | ||
); | ||
return _dumpObject(n2, keys); | ||
} else { | ||
const keys = Object.keys(n2); | ||
return _dumpObject(n2, keys); | ||
} | ||
} | ||
o[k] = _dump(v); | ||
return o; | ||
}, {}); | ||
const _dump = (n2) => { | ||
switch (typeof n2) { | ||
case "object": { | ||
if (Array.isArray(n2)) { | ||
return n2.map((v) => _dump(v)); | ||
} else if (n2 === null) { | ||
return null; | ||
} else if (n2.__proto__ !== void 0) { | ||
const keys = [...Object.keys(n2), ...Object.keys(n2.__proto__)].filter( | ||
(k) => !readBlacklist.has(k) | ||
); | ||
return _dumpObject(n2, keys); | ||
} else { | ||
const keys = Object.keys(n2); | ||
return _dumpObject(n2, keys); | ||
} | ||
case "function": | ||
return void 0; | ||
case "symbol": | ||
if (n2 === figma.mixed) { | ||
return "__Symbol(figma.mixed)__"; | ||
} else { | ||
return String(n2); | ||
} | ||
case "function": | ||
return void 0; | ||
case "symbol": | ||
if (n2 === figma.mixed) { | ||
return "__Symbol(figma.mixed)__"; | ||
} else { | ||
return String(n2); | ||
} | ||
default: | ||
return n2; | ||
} | ||
}; | ||
const objects = n.map(_dump); | ||
const dataRequests = [...imageHashes].map((hash) => __async(this, null, function* () { | ||
const im = figma.getImageByHash(hash); | ||
if (im === null) { | ||
throw new Error(`Image not found: ${hash}`); | ||
} | ||
const dat = yield im.getBytesAsync(); | ||
return [hash, dat]; | ||
})); | ||
const images = Object.fromEntries(yield Promise.all(dataRequests)); | ||
return { | ||
objects, | ||
images: {} | ||
}; | ||
}); | ||
default: | ||
return n2; | ||
} | ||
}; | ||
const objects = n.map(_dump); | ||
const dataRequests = [...imageHashes].map((hash) => __async(this, null, function* () { | ||
const im = figma.getImageByHash(hash); | ||
if (im === null) { | ||
throw new Error(`Image not found: ${hash}`); | ||
} | ||
const dat = yield im.getBytesAsync(); | ||
return [hash, dat]; | ||
})); | ||
const images = Object.fromEntries(yield Promise.all(dataRequests)); | ||
return { | ||
objects, | ||
images: {} | ||
}; | ||
}); | ||
} | ||
function loadFonts(n) { | ||
return __async(this, null, function* () { | ||
console.log("starting font load..."); | ||
const fontNames = fontsToLoad(n); | ||
console.log("loading fonts:", fontNames); | ||
yield Promise.all(fontNames.map((f) => figma.loadFontAsync(f))); | ||
console.log("done loading fonts."); | ||
}); | ||
} | ||
function encodeFont({ family, style }) { | ||
if (family.includes("|") || style.includes("|")) { | ||
throw new Error(`Cannot encode a font with "|" in the name.`); | ||
} | ||
function loadFonts(n) { | ||
return __async(this, null, function* () { | ||
console.log("starting font load..."); | ||
const fontNames = fontsToLoad(n); | ||
console.log("loading fonts:", fontNames); | ||
yield Promise.all(fontNames.map((f) => figma.loadFontAsync(f))); | ||
console.log("done loading fonts."); | ||
}); | ||
return [family, style].join("|"); | ||
} | ||
function decodeFont(f) { | ||
const s = f.split("|"); | ||
if (s.length !== 2) { | ||
throw new Error(`Unable to decode font string: ${f}`); | ||
} | ||
function encodeFont({ family, style }) { | ||
if (family.includes("|") || style.includes("|")) { | ||
throw new Error(`Cannot encode a font with "|" in the name.`); | ||
const [family, style] = s; | ||
return { family, style }; | ||
} | ||
function preflightFonts(dump2, availableFonts) { | ||
const requiredFonts = fontsToLoad(dump2); | ||
const availableFontsSet = new Set(availableFonts.map(encodeFont)); | ||
const missingFonts = requiredFonts.filter( | ||
(f) => !availableFontsSet.has(encodeFont(f)) | ||
); | ||
const usedFonts = requiredFonts.filter( | ||
(f) => availableFontsSet.has(encodeFont(f)) | ||
); | ||
return { | ||
requiredFonts, | ||
missingFonts, | ||
usedFonts | ||
}; | ||
} | ||
function resizeOrLog(f, width, height, withoutConstraints) { | ||
if (width > 0.01 && height > 0.01) { | ||
if (withoutConstraints) { | ||
f.resizeWithoutConstraints(width, height); | ||
} else { | ||
f.resize(width, height); | ||
} | ||
return [family, style].join("|"); | ||
} else { | ||
const generic = f; | ||
const { type } = generic; | ||
console.log( | ||
`Couldn't resize item: ${JSON.stringify({ | ||
type, | ||
width, | ||
height | ||
})}` | ||
); | ||
} | ||
function decodeFont(f) { | ||
const s = f.split("|"); | ||
if (s.length !== 2) { | ||
throw new Error(`Unable to decode font string: ${f}`); | ||
} | ||
function fontsToLoad(n) { | ||
const fonts = /* @__PURE__ */ new Set(); | ||
const addFonts = (json) => { | ||
switch (json.type) { | ||
case "COMPONENT": | ||
case "FRAME": | ||
case "GROUP": | ||
const { children = [] } = json; | ||
children.forEach(addFonts); | ||
return; | ||
case "TEXT": | ||
const { fontName } = json; | ||
if (typeof fontName === "object") { | ||
fonts.add(encodeFont(fontName)); | ||
} else if (fontName === "__Symbol(figma.mixed)__") { | ||
console.log("encountered mixed fontName: ", fontName); | ||
} | ||
} | ||
const [family, style] = s; | ||
return { family, style }; | ||
}; | ||
try { | ||
n.objects.forEach(addFonts); | ||
} catch (err) { | ||
console.error("error searching for fonts:", err); | ||
} | ||
function preflightFonts(dump2, availableFonts) { | ||
const requiredFonts = fontsToLoad(dump2); | ||
const availableFontsSet = new Set(availableFonts.map(encodeFont)); | ||
const missingFonts = requiredFonts.filter( | ||
(f) => !availableFontsSet.has(encodeFont(f)) | ||
); | ||
const usedFonts = requiredFonts.filter( | ||
(f) => availableFontsSet.has(encodeFont(f)) | ||
); | ||
return { | ||
requiredFonts, | ||
missingFonts, | ||
usedFonts | ||
}; | ||
} | ||
function resizeOrLog(f, width, height, withoutConstraints) { | ||
if (width > 0.01 && height > 0.01) { | ||
if (withoutConstraints) { | ||
f.resizeWithoutConstraints(width, height); | ||
} else { | ||
f.resize(width, height); | ||
const fontNames = [...fonts].map((fstr) => decodeFont(fstr)); | ||
return fontNames; | ||
} | ||
function safeAssign(n, dict) { | ||
for (let k in dict) { | ||
try { | ||
if (writeBlacklist.has(k)) { | ||
continue; | ||
} | ||
} else { | ||
const generic = f; | ||
const { type } = generic; | ||
console.log( | ||
`Couldn't resize item: ${JSON.stringify({ | ||
type, | ||
width, | ||
height | ||
})}` | ||
); | ||
const v = dict[k]; | ||
if (v === MixedValue || v === void 0) { | ||
continue; | ||
} | ||
n[k] = v; | ||
} catch (error) { | ||
console.error("assignment failed for key", k, error); | ||
} | ||
} | ||
function fontsToLoad(n) { | ||
const fonts = /* @__PURE__ */ new Set(); | ||
const addFonts = (json) => { | ||
} | ||
function applyPluginData(n, pluginData) { | ||
if (pluginData === void 0) { | ||
return; | ||
} | ||
Object.entries(pluginData).map(([k, v]) => n.setPluginData(k, v)); | ||
} | ||
function insert(n) { | ||
return __async(this, null, function* () { | ||
const offset = { x: 0, y: 0 }; | ||
console.log("starting insert."); | ||
yield loadFonts(n); | ||
console.log("creating images."); | ||
const jsonImages = Object.entries(n.images); | ||
const hashUpdates = /* @__PURE__ */ new Map(); | ||
const figim = jsonImages.map(([hash, bytes]) => { | ||
console.log("Adding with hash: ", hash); | ||
const im = figma.createImage(bytes); | ||
hashUpdates.set(hash, im.hash); | ||
return [hash, im]; | ||
}); | ||
console.log("updating figma based on new hashes."); | ||
const objects = n.objects.map((n2) => updateImageHashes(n2, hashUpdates)); | ||
console.log("inserting."); | ||
const insertSceneNode = (json, target) => { | ||
const factories = { | ||
RECTANGLE: () => figma.createRectangle(), | ||
LINE: () => figma.createLine(), | ||
ELLIPSE: () => figma.createEllipse(), | ||
POLYGON: () => figma.createPolygon(), | ||
STAR: () => figma.createStar(), | ||
VECTOR: () => figma.createVector(), | ||
TEXT: () => figma.createText(), | ||
FRAME: () => figma.createFrame(), | ||
COMPONENT: () => figma.createComponent() | ||
}; | ||
const addToParent = (n3) => { | ||
if (n3 && n3.parent !== target) { | ||
target.appendChild(n3); | ||
} | ||
}; | ||
let n2; | ||
switch (json.type) { | ||
case "COMPONENT": | ||
case "FRAME": | ||
case "GROUP": | ||
const { children = [] } = json; | ||
children.forEach(addFonts); | ||
return; | ||
case "TEXT": | ||
const { fontName } = json; | ||
if (typeof fontName === "object") { | ||
fonts.add(encodeFont(fontName)); | ||
} else if (fontName === "__Symbol(figma.mixed)__") { | ||
console.log("encountered mixed fontName: ", fontName); | ||
} | ||
} | ||
}; | ||
try { | ||
n.objects.forEach(addFonts); | ||
} catch (err) { | ||
console.error("error searching for fonts:", err); | ||
} | ||
const fontNames = [...fonts].map((fstr) => decodeFont(fstr)); | ||
return fontNames; | ||
} | ||
function safeAssign(n, dict) { | ||
for (let k in dict) { | ||
try { | ||
if (writeBlacklist.has(k)) { | ||
continue; | ||
case "COMPONENT": { | ||
const _a = json, { | ||
type, | ||
children = [], | ||
width, | ||
height, | ||
strokeCap, | ||
strokeJoin, | ||
pluginData | ||
} = _a, rest = __objRest(_a, [ | ||
"type", | ||
"children", | ||
"width", | ||
"height", | ||
"strokeCap", | ||
"strokeJoin", | ||
"pluginData" | ||
]); | ||
const f = factories[json.type](); | ||
addToParent(f); | ||
resizeOrLog(f, width, height); | ||
safeAssign(f, rest); | ||
applyPluginData(f, pluginData); | ||
children.forEach((c) => insertSceneNode(c, f)); | ||
n2 = f; | ||
break; | ||
} | ||
const v = dict[k]; | ||
if (v === MixedValue || v === void 0) { | ||
continue; | ||
case "GROUP": { | ||
const _b = json, { | ||
type, | ||
children = [], | ||
width, | ||
height, | ||
pluginData | ||
} = _b, rest = __objRest(_b, [ | ||
"type", | ||
"children", | ||
"width", | ||
"height", | ||
"pluginData" | ||
]); | ||
const nodes = children.map((c) => insertSceneNode(c, target)).filter(notUndefined); | ||
const f = figma.group(nodes, target); | ||
safeAssign(f, rest); | ||
n2 = f; | ||
break; | ||
} | ||
n[k] = v; | ||
} catch (error) { | ||
console.error("assignment failed for key", k, error); | ||
} | ||
} | ||
} | ||
function applyPluginData(n, pluginData) { | ||
if (pluginData === void 0) { | ||
return; | ||
} | ||
Object.entries(pluginData).map(([k, v]) => n.setPluginData(k, v)); | ||
} | ||
function insert(n) { | ||
return __async(this, null, function* () { | ||
const offset = { x: 0, y: 0 }; | ||
console.log("starting insert."); | ||
yield loadFonts(n); | ||
console.log("creating images."); | ||
const jsonImages = Object.entries(n.images); | ||
const hashUpdates = /* @__PURE__ */ new Map(); | ||
const figim = jsonImages.map(([hash, bytes]) => { | ||
console.log("Adding with hash: ", hash); | ||
const im = figma.createImage(bytes); | ||
hashUpdates.set(hash, im.hash); | ||
return [hash, im]; | ||
}); | ||
console.log("updating figma based on new hashes."); | ||
const objects = n.objects.map((n2) => updateImageHashes(n2, hashUpdates)); | ||
console.log("inserting."); | ||
const insertSceneNode = (json, target) => { | ||
const factories = { | ||
RECTANGLE: () => figma.createRectangle(), | ||
LINE: () => figma.createLine(), | ||
ELLIPSE: () => figma.createEllipse(), | ||
POLYGON: () => figma.createPolygon(), | ||
STAR: () => figma.createStar(), | ||
VECTOR: () => figma.createVector(), | ||
TEXT: () => figma.createText(), | ||
FRAME: () => figma.createFrame(), | ||
COMPONENT: () => figma.createComponent() | ||
}; | ||
const addToParent = (n3) => { | ||
if (n3 && n3.parent !== target) { | ||
target.appendChild(n3); | ||
case "BOOLEAN_OPERATION": { | ||
const _c = json, { type, children, width, height, pluginData } = _c, rest = __objRest(_c, ["type", "children", "width", "height", "pluginData"]); | ||
const f = figma.createBooleanOperation(); | ||
safeAssign(f, rest); | ||
applyPluginData(f, pluginData); | ||
resizeOrLog(f, width, height); | ||
n2 = f; | ||
break; | ||
} | ||
case "RECTANGLE": | ||
case "ELLIPSE": | ||
case "LINE": | ||
case "POLYGON": | ||
case "VECTOR": { | ||
const _d = json, { type, width, height, pluginData } = _d, rest = __objRest(_d, ["type", "width", "height", "pluginData"]); | ||
const f = factories[json.type](); | ||
safeAssign( | ||
f, | ||
rest | ||
); | ||
applyPluginData(f, pluginData); | ||
resizeOrLog(f, width, height, true); | ||
n2 = f; | ||
break; | ||
} | ||
case "TEXT": { | ||
const _e = json, { type, width, height, fontName, pluginData } = _e, rest = __objRest(_e, ["type", "width", "height", "fontName", "pluginData"]); | ||
const f = figma.createText(); | ||
if (fontName !== "__Symbol(figma.mixed)__") { | ||
f.fontName = fontName; | ||
} | ||
}; | ||
let n2; | ||
switch (json.type) { | ||
case "FRAME": | ||
case "COMPONENT": { | ||
const _a = json, { | ||
type, | ||
children = [], | ||
width, | ||
height, | ||
strokeCap, | ||
strokeJoin, | ||
pluginData | ||
} = _a, rest = __objRest(_a, [ | ||
"type", | ||
"children", | ||
"width", | ||
"height", | ||
"strokeCap", | ||
"strokeJoin", | ||
"pluginData" | ||
]); | ||
const f = factories[json.type](); | ||
addToParent(f); | ||
resizeOrLog(f, width, height); | ||
safeAssign(f, rest); | ||
applyPluginData(f, pluginData); | ||
children.forEach((c) => insertSceneNode(c, f)); | ||
n2 = f; | ||
break; | ||
} | ||
case "GROUP": { | ||
const _b = json, { | ||
type, | ||
children = [], | ||
width, | ||
height, | ||
pluginData | ||
} = _b, rest = __objRest(_b, [ | ||
"type", | ||
"children", | ||
"width", | ||
"height", | ||
"pluginData" | ||
]); | ||
const nodes = children.map((c) => insertSceneNode(c, target)).filter(notUndefined); | ||
const f = figma.group(nodes, target); | ||
safeAssign(f, rest); | ||
n2 = f; | ||
break; | ||
} | ||
case "BOOLEAN_OPERATION": { | ||
const _c = json, { type, children, width, height, pluginData } = _c, rest = __objRest(_c, ["type", "children", "width", "height", "pluginData"]); | ||
const f = figma.createBooleanOperation(); | ||
safeAssign(f, rest); | ||
applyPluginData(f, pluginData); | ||
resizeOrLog(f, width, height); | ||
n2 = f; | ||
break; | ||
} | ||
case "RECTANGLE": | ||
case "ELLIPSE": | ||
case "LINE": | ||
case "POLYGON": | ||
case "VECTOR": { | ||
const _d = json, { type, width, height, pluginData } = _d, rest = __objRest(_d, ["type", "width", "height", "pluginData"]); | ||
const f = factories[json.type](); | ||
safeAssign( | ||
f, | ||
rest | ||
); | ||
applyPluginData(f, pluginData); | ||
resizeOrLog(f, width, height, true); | ||
n2 = f; | ||
break; | ||
} | ||
case "TEXT": { | ||
const _e = json, { type, width, height, fontName, pluginData } = _e, rest = __objRest(_e, ["type", "width", "height", "fontName", "pluginData"]); | ||
const f = figma.createText(); | ||
if (fontName !== "__Symbol(figma.mixed)__") { | ||
f.fontName = fontName; | ||
} | ||
safeAssign(f, rest); | ||
applyPluginData(f, pluginData); | ||
resizeOrLog(f, width, height); | ||
n2 = f; | ||
break; | ||
} | ||
default: { | ||
console.log(`element type not supported: ${json.type}`); | ||
break; | ||
} | ||
safeAssign(f, rest); | ||
applyPluginData(f, pluginData); | ||
resizeOrLog(f, width, height); | ||
n2 = f; | ||
break; | ||
} | ||
if (n2) { | ||
target.appendChild(n2); | ||
} else { | ||
console.warn("Unable to do anything with", json); | ||
default: { | ||
console.log(`element type not supported: ${json.type}`); | ||
break; | ||
} | ||
return n2; | ||
}; | ||
return objects.map((o) => { | ||
const n2 = insertSceneNode(o, figma.currentPage); | ||
if (n2 !== void 0) { | ||
n2.x += offset.x; | ||
n2.y += offset.y; | ||
n2.name = `${n2.name} Copy`; | ||
} else { | ||
console.error("returned undefined for json", o); | ||
} | ||
return n2; | ||
}).filter(notUndefined); | ||
}); | ||
} | ||
})(); | ||
} | ||
if (n2) { | ||
target.appendChild(n2); | ||
} else { | ||
console.warn("Unable to do anything with", json); | ||
} | ||
return n2; | ||
}; | ||
return objects.map((o) => { | ||
const n2 = insertSceneNode(o, figma.currentPage); | ||
if (n2 !== void 0) { | ||
n2.x += offset.x; | ||
n2.y += offset.y; | ||
n2.name = `${n2.name} Copy`; | ||
} else { | ||
console.error("returned undefined for json", o); | ||
} | ||
return n2; | ||
}).filter(notUndefined); | ||
}); | ||
} | ||
// Annotate the CommonJS export names for ESM import in node: | ||
0 && (module.exports = { | ||
MixedValue, | ||
decodeFont, | ||
dump, | ||
encodeFont, | ||
fontsToLoad, | ||
insert, | ||
preflightFonts, | ||
readBlacklist, | ||
writeBlacklist | ||
}); |
{ | ||
"name": "figma-json-plugin", | ||
"version": "0.0.5-alpha.2", | ||
"version": "0.0.5-alpha.3", | ||
"description": "Dump a hierarchy to JSON within a Figma document, or insert a dumped JSON hierarchy. Intended for use within Figma plugins.", | ||
"main": "dist/index.js", | ||
"module": "dist/index.mjs", | ||
"types": "dist/index.d.ts", | ||
@@ -14,10 +15,12 @@ "browser": "dist/browser.js", | ||
"build": "yarn build:lib && yarn build:types", | ||
"build:lib": "esbuild src/index.ts --bundle --outfile=dist/index.js", | ||
"build:lib": "node scripts/build.js", | ||
"build:plugin": "esbuild plugin/plugin.ts --bundle --outfile=dist/plugin.js", | ||
"build:ui": "esbuild plugin/ui.tsx --bundle --outfile=dist/ui.js", | ||
"build:types": "tsc", | ||
"publish-prerelease": "yarn build && yarn publish --prerelease", | ||
"test": "jest", | ||
"test:watch": "jest --watch", | ||
"format": "prettier --write .", | ||
"clean": "rm -rf dist" | ||
"clean": "rm -rf dist", | ||
"prepack": "yarn clean && yarn build" | ||
}, | ||
@@ -42,3 +45,2 @@ "files": [ | ||
"react-json-view": "^1.19.1", | ||
"ts-loader": "^6.2.0", | ||
"typescript": "^4.7.4" | ||
@@ -50,5 +52,4 @@ }, | ||
"isomorphic-unfetch": "^3.0.0", | ||
"styled-components": "^4.3.2", | ||
"swr": "^0.1.14" | ||
"styled-components": "^4.3.2" | ||
} | ||
} |
52083
4
15
8
1576
- Removedswr@^0.1.14
- Removedfast-deep-equal@2.0.1(transitive)
- Removedswr@0.1.18(transitive)