figma-json-plugin
Advanced tools
Comparing version 0.0.2-3 to 0.0.3
449
dist/main.js
@@ -85,7 +85,12 @@ exports["figmaDump"] = | ||
/******/ // Load entry module and return exports | ||
/******/ return __webpack_require__(__webpack_require__.s = 0); | ||
/******/ return __webpack_require__(__webpack_require__.s = "./src/index.ts"); | ||
/******/ }) | ||
/************************************************************************/ | ||
/******/ ([ | ||
/* 0 */ | ||
/******/ ({ | ||
/***/ "./node_modules/base64-js/index.js": | ||
/*!*****************************************!*\ | ||
!*** ./node_modules/base64-js/index.js ***! | ||
\*****************************************/ | ||
/*! no static exports found */ | ||
/***/ (function(module, exports, __webpack_require__) { | ||
@@ -95,2 +100,166 @@ | ||
exports.byteLength = byteLength | ||
exports.toByteArray = toByteArray | ||
exports.fromByteArray = fromByteArray | ||
var lookup = [] | ||
var revLookup = [] | ||
var Arr = typeof Uint8Array !== 'undefined' ? Uint8Array : Array | ||
var code = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/' | ||
for (var i = 0, len = code.length; i < len; ++i) { | ||
lookup[i] = code[i] | ||
revLookup[code.charCodeAt(i)] = i | ||
} | ||
// Support decoding URL-safe base64 strings, as Node.js does. | ||
// See: https://en.wikipedia.org/wiki/Base64#URL_applications | ||
revLookup['-'.charCodeAt(0)] = 62 | ||
revLookup['_'.charCodeAt(0)] = 63 | ||
function getLens (b64) { | ||
var len = b64.length | ||
if (len % 4 > 0) { | ||
throw new Error('Invalid string. Length must be a multiple of 4') | ||
} | ||
// Trim off extra bytes after placeholder bytes are found | ||
// See: https://github.com/beatgammit/base64-js/issues/42 | ||
var validLen = b64.indexOf('=') | ||
if (validLen === -1) validLen = len | ||
var placeHoldersLen = validLen === len | ||
? 0 | ||
: 4 - (validLen % 4) | ||
return [validLen, placeHoldersLen] | ||
} | ||
// base64 is 4/3 + up to two characters of the original data | ||
function byteLength (b64) { | ||
var lens = getLens(b64) | ||
var validLen = lens[0] | ||
var placeHoldersLen = lens[1] | ||
return ((validLen + placeHoldersLen) * 3 / 4) - placeHoldersLen | ||
} | ||
function _byteLength (b64, validLen, placeHoldersLen) { | ||
return ((validLen + placeHoldersLen) * 3 / 4) - placeHoldersLen | ||
} | ||
function toByteArray (b64) { | ||
var tmp | ||
var lens = getLens(b64) | ||
var validLen = lens[0] | ||
var placeHoldersLen = lens[1] | ||
var arr = new Arr(_byteLength(b64, validLen, placeHoldersLen)) | ||
var curByte = 0 | ||
// if there are placeholders, only get up to the last complete 4 chars | ||
var len = placeHoldersLen > 0 | ||
? validLen - 4 | ||
: validLen | ||
var i | ||
for (i = 0; i < len; i += 4) { | ||
tmp = | ||
(revLookup[b64.charCodeAt(i)] << 18) | | ||
(revLookup[b64.charCodeAt(i + 1)] << 12) | | ||
(revLookup[b64.charCodeAt(i + 2)] << 6) | | ||
revLookup[b64.charCodeAt(i + 3)] | ||
arr[curByte++] = (tmp >> 16) & 0xFF | ||
arr[curByte++] = (tmp >> 8) & 0xFF | ||
arr[curByte++] = tmp & 0xFF | ||
} | ||
if (placeHoldersLen === 2) { | ||
tmp = | ||
(revLookup[b64.charCodeAt(i)] << 2) | | ||
(revLookup[b64.charCodeAt(i + 1)] >> 4) | ||
arr[curByte++] = tmp & 0xFF | ||
} | ||
if (placeHoldersLen === 1) { | ||
tmp = | ||
(revLookup[b64.charCodeAt(i)] << 10) | | ||
(revLookup[b64.charCodeAt(i + 1)] << 4) | | ||
(revLookup[b64.charCodeAt(i + 2)] >> 2) | ||
arr[curByte++] = (tmp >> 8) & 0xFF | ||
arr[curByte++] = tmp & 0xFF | ||
} | ||
return arr | ||
} | ||
function tripletToBase64 (num) { | ||
return lookup[num >> 18 & 0x3F] + | ||
lookup[num >> 12 & 0x3F] + | ||
lookup[num >> 6 & 0x3F] + | ||
lookup[num & 0x3F] | ||
} | ||
function encodeChunk (uint8, start, end) { | ||
var tmp | ||
var output = [] | ||
for (var i = start; i < end; i += 3) { | ||
tmp = | ||
((uint8[i] << 16) & 0xFF0000) + | ||
((uint8[i + 1] << 8) & 0xFF00) + | ||
(uint8[i + 2] & 0xFF) | ||
output.push(tripletToBase64(tmp)) | ||
} | ||
return output.join('') | ||
} | ||
function fromByteArray (uint8) { | ||
var tmp | ||
var len = uint8.length | ||
var extraBytes = len % 3 // if we have 1 byte left, pad 2 bytes | ||
var parts = [] | ||
var maxChunkLength = 16383 // must be multiple of 3 | ||
// go through the array every three bytes, we'll deal with trailing stuff later | ||
for (var i = 0, len2 = len - extraBytes; i < len2; i += maxChunkLength) { | ||
parts.push(encodeChunk( | ||
uint8, i, (i + maxChunkLength) > len2 ? len2 : (i + maxChunkLength) | ||
)) | ||
} | ||
// pad the end with zeros, but make sure to not forget the extra bytes | ||
if (extraBytes === 1) { | ||
tmp = uint8[len - 1] | ||
parts.push( | ||
lookup[tmp >> 2] + | ||
lookup[(tmp << 4) & 0x3F] + | ||
'==' | ||
) | ||
} else if (extraBytes === 2) { | ||
tmp = (uint8[len - 2] << 8) + uint8[len - 1] | ||
parts.push( | ||
lookup[tmp >> 10] + | ||
lookup[(tmp >> 4) & 0x3F] + | ||
lookup[(tmp << 2) & 0x3F] + | ||
'=' | ||
) | ||
} | ||
return parts.join('') | ||
} | ||
/***/ }), | ||
/***/ "./src/index.ts": | ||
/*!**********************!*\ | ||
!*** ./src/index.ts ***! | ||
\**********************/ | ||
/*! no static exports found */ | ||
/***/ (function(module, exports, __webpack_require__) { | ||
"use strict"; | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
@@ -118,6 +287,6 @@ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
// Copyright 2019 Andrew Pouliot | ||
const polyfill_1 = __webpack_require__(1); | ||
const base64_js_1 = __webpack_require__(2); | ||
const polyfill_1 = __webpack_require__(/*! ./polyfill */ "./src/polyfill.ts"); | ||
const base64_js_1 = __webpack_require__(/*! base64-js */ "./node_modules/base64-js/index.js"); | ||
// Anything that is readonly on a SceneNode should not be set! | ||
exports.blacklist = new Set([ | ||
exports.readBlacklist = new Set([ | ||
"parent", | ||
@@ -130,2 +299,4 @@ "removed", | ||
]); | ||
// Things in figmaJSON we are not writing right now | ||
exports.writeBlacklist = new Set(["id"]); | ||
function notUndefined(x) { | ||
@@ -154,3 +325,3 @@ return x !== undefined; | ||
// Merge keys from __proto__ with natural keys | ||
const keys = [...Object.keys(n), ...Object.keys(n.__proto__)].filter(k => !exports.blacklist.has(k)); | ||
const keys = [...Object.keys(n), ...Object.keys(n.__proto__)].filter(k => !exports.readBlacklist.has(k)); | ||
return _dumpObject(n, keys); | ||
@@ -195,2 +366,3 @@ } | ||
return __awaiter(this, void 0, void 0, function* () { | ||
console.log("starting font load..."); | ||
// Sets are dumb in JS, can't use FontName because it's an object ref | ||
@@ -205,4 +377,4 @@ // Normalize all fonts to their JSON representation | ||
case "GROUP": | ||
const { children } = json; | ||
children.map(addFonts); | ||
const { children = [] } = json; | ||
children.forEach(addFonts); | ||
return; | ||
@@ -219,4 +391,12 @@ case "TEXT": | ||
}; | ||
n.objects.forEach(addFonts); | ||
const fontNames = [...fonts].map(fstr => JSON.parse(fstr)); | ||
console.log("searching objects..."); | ||
try { | ||
n.objects.forEach(addFonts); | ||
} | ||
catch (err) { | ||
console.log("error searching for fonts:"); | ||
} | ||
// There seems to be a bug when we don't await any fonts…b | ||
const addl = { family: "SF Pro Text", style: "Regular" }; | ||
const fontNames = [...fonts, JSON.stringify(addl)].map(fstr => JSON.parse(fstr)); | ||
console.log("loading fonts:", fontNames); | ||
@@ -227,5 +407,30 @@ yield Promise.all(fontNames.map(f => figma.loadFontAsync(f))); | ||
} | ||
function safeAssign(n, dict) { | ||
for (let k in dict) { | ||
try { | ||
// I can't quite figure out how to get typescript to accept that if k is in dict | ||
const dictForceTS = dict; | ||
const v = dictForceTS[k]; | ||
// Bit of a nasty hack here, but ignore these mixed sentinels | ||
if (v === "__Symbol(figma.mixed)__") { | ||
continue; | ||
} | ||
n[k] = v; | ||
// console.log(`${k} = ${JSON.stringify(v)}`); | ||
} | ||
catch (error) { | ||
console.error("assignment failed for key", k, error); | ||
} | ||
} | ||
} | ||
function applyPluginData(n, pluginData) { | ||
if (pluginData === undefined) { | ||
return; | ||
} | ||
Object.entries(pluginData).map(([k, v]) => n.setPluginData(k, v)); | ||
} | ||
function insert(n) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const offset = { x: 0, y: 0 }; | ||
console.log("starting insert."); | ||
yield loadFonts(n); | ||
@@ -250,2 +455,3 @@ // Create all images | ||
const insertSceneNode = (json, target) => { | ||
console.log("in insertSceneNode with type", json.type); | ||
// Using lambdas here to make sure figma is bound as this | ||
@@ -268,2 +474,8 @@ // TODO: experiment whether this is necessary | ||
}; | ||
const addToParent = (n) => { | ||
// console.log("adding to parent", n); | ||
if (n && n.parent !== target) { | ||
target.appendChild(n); | ||
} | ||
}; | ||
let n; | ||
@@ -274,15 +486,33 @@ switch (json.type) { | ||
case "COMPONENT": { | ||
const { type, children, width, height } = json, rest = __rest(json, ["type", "children", "width", "height"]); | ||
const { type, children = [], width, height, pluginData } = json, rest = __rest(json, ["type", "children", "width", "height", "pluginData"]); | ||
const f = factories[json.type](); | ||
f.resizeWithoutConstraints(width, height); | ||
Object.assign(f, rest); | ||
addToParent(f); | ||
// console.log("size target:", { width, height }); | ||
f.resize(width, height); | ||
// console.log("size after:", { width: f.width, height: f.height }); | ||
safeAssign(f, rest); | ||
applyPluginData(f, pluginData); | ||
// console.log("building children: ", children); | ||
children.forEach(c => insertSceneNode(c, f)); | ||
// console.log("applied to children ", f); | ||
n = f; | ||
break; | ||
} | ||
case "GROUP": { | ||
const { type, children = [], width, height, pluginData } = json, rest = __rest(json, ["type", "children", "width", "height", "pluginData"]); | ||
const nodes = children | ||
.map(c => insertSceneNode(c, target)) | ||
.filter(notUndefined); | ||
console.log("created objects", nodes); | ||
const f = figma.group(nodes, target); | ||
safeAssign(f, rest); | ||
n = f; | ||
break; | ||
} | ||
case "BOOLEAN_OPERATION": { | ||
// TODO: this isn't optimal | ||
const { type, children, width, height } = json, rest = __rest(json, ["type", "children", "width", "height"]); | ||
const { type, children, width, height, pluginData } = json, rest = __rest(json, ["type", "children", "width", "height", "pluginData"]); | ||
const f = figma.createBooleanOperation(); | ||
Object.assign(f, rest); | ||
safeAssign(f, rest); | ||
applyPluginData(f, pluginData); | ||
f.resizeWithoutConstraints(width, height); | ||
@@ -297,6 +527,7 @@ n = f; | ||
case "VECTOR": { | ||
const { type, width, height } = json, rest = __rest(json, ["type", "width", "height"]); | ||
const { type, width, height, pluginData } = json, rest = __rest(json, ["type", "width", "height", "pluginData"]); | ||
const f = factories[json.type](); | ||
safeAssign(f, rest); | ||
applyPluginData(f, pluginData); | ||
f.resizeWithoutConstraints(width, height); | ||
Object.assign(f, rest); | ||
n = f; | ||
@@ -306,6 +537,12 @@ break; | ||
case "TEXT": { | ||
const { type, width, height } = json, rest = __rest(json, ["type", "width", "height"]); | ||
const { type, width, height, fontName, pluginData } = json, rest = __rest(json, ["type", "width", "height", "fontName", "pluginData"]); | ||
const f = figma.createText(); | ||
f.resizeWithoutConstraints(width, height); | ||
Object.assign(f, rest); | ||
// Need to assign this first, because of font-loading rules :O | ||
if (fontName !== "__Symbol(figma.mixed)__") { | ||
f.fontName = fontName; | ||
} | ||
safeAssign(f, rest); | ||
applyPluginData(f, pluginData); | ||
console.log("resizing in text"); | ||
f.resize(width, height); | ||
n = f; | ||
@@ -320,2 +557,3 @@ break; | ||
if (n) { | ||
console.log("appending child", n); | ||
target.appendChild(n); | ||
@@ -349,3 +587,8 @@ } | ||
/***/ }), | ||
/* 1 */ | ||
/***/ "./src/polyfill.ts": | ||
/*!*************************!*\ | ||
!*** ./src/polyfill.ts ***! | ||
\*************************/ | ||
/*! no static exports found */ | ||
/***/ (function(module, exports, __webpack_require__) { | ||
@@ -365,162 +608,4 @@ | ||
/***/ }), | ||
/* 2 */ | ||
/***/ (function(module, exports, __webpack_require__) { | ||
/***/ }) | ||
"use strict"; | ||
exports.byteLength = byteLength | ||
exports.toByteArray = toByteArray | ||
exports.fromByteArray = fromByteArray | ||
var lookup = [] | ||
var revLookup = [] | ||
var Arr = typeof Uint8Array !== 'undefined' ? Uint8Array : Array | ||
var code = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/' | ||
for (var i = 0, len = code.length; i < len; ++i) { | ||
lookup[i] = code[i] | ||
revLookup[code.charCodeAt(i)] = i | ||
} | ||
// Support decoding URL-safe base64 strings, as Node.js does. | ||
// See: https://en.wikipedia.org/wiki/Base64#URL_applications | ||
revLookup['-'.charCodeAt(0)] = 62 | ||
revLookup['_'.charCodeAt(0)] = 63 | ||
function getLens (b64) { | ||
var len = b64.length | ||
if (len % 4 > 0) { | ||
throw new Error('Invalid string. Length must be a multiple of 4') | ||
} | ||
// Trim off extra bytes after placeholder bytes are found | ||
// See: https://github.com/beatgammit/base64-js/issues/42 | ||
var validLen = b64.indexOf('=') | ||
if (validLen === -1) validLen = len | ||
var placeHoldersLen = validLen === len | ||
? 0 | ||
: 4 - (validLen % 4) | ||
return [validLen, placeHoldersLen] | ||
} | ||
// base64 is 4/3 + up to two characters of the original data | ||
function byteLength (b64) { | ||
var lens = getLens(b64) | ||
var validLen = lens[0] | ||
var placeHoldersLen = lens[1] | ||
return ((validLen + placeHoldersLen) * 3 / 4) - placeHoldersLen | ||
} | ||
function _byteLength (b64, validLen, placeHoldersLen) { | ||
return ((validLen + placeHoldersLen) * 3 / 4) - placeHoldersLen | ||
} | ||
function toByteArray (b64) { | ||
var tmp | ||
var lens = getLens(b64) | ||
var validLen = lens[0] | ||
var placeHoldersLen = lens[1] | ||
var arr = new Arr(_byteLength(b64, validLen, placeHoldersLen)) | ||
var curByte = 0 | ||
// if there are placeholders, only get up to the last complete 4 chars | ||
var len = placeHoldersLen > 0 | ||
? validLen - 4 | ||
: validLen | ||
var i | ||
for (i = 0; i < len; i += 4) { | ||
tmp = | ||
(revLookup[b64.charCodeAt(i)] << 18) | | ||
(revLookup[b64.charCodeAt(i + 1)] << 12) | | ||
(revLookup[b64.charCodeAt(i + 2)] << 6) | | ||
revLookup[b64.charCodeAt(i + 3)] | ||
arr[curByte++] = (tmp >> 16) & 0xFF | ||
arr[curByte++] = (tmp >> 8) & 0xFF | ||
arr[curByte++] = tmp & 0xFF | ||
} | ||
if (placeHoldersLen === 2) { | ||
tmp = | ||
(revLookup[b64.charCodeAt(i)] << 2) | | ||
(revLookup[b64.charCodeAt(i + 1)] >> 4) | ||
arr[curByte++] = tmp & 0xFF | ||
} | ||
if (placeHoldersLen === 1) { | ||
tmp = | ||
(revLookup[b64.charCodeAt(i)] << 10) | | ||
(revLookup[b64.charCodeAt(i + 1)] << 4) | | ||
(revLookup[b64.charCodeAt(i + 2)] >> 2) | ||
arr[curByte++] = (tmp >> 8) & 0xFF | ||
arr[curByte++] = tmp & 0xFF | ||
} | ||
return arr | ||
} | ||
function tripletToBase64 (num) { | ||
return lookup[num >> 18 & 0x3F] + | ||
lookup[num >> 12 & 0x3F] + | ||
lookup[num >> 6 & 0x3F] + | ||
lookup[num & 0x3F] | ||
} | ||
function encodeChunk (uint8, start, end) { | ||
var tmp | ||
var output = [] | ||
for (var i = start; i < end; i += 3) { | ||
tmp = | ||
((uint8[i] << 16) & 0xFF0000) + | ||
((uint8[i + 1] << 8) & 0xFF00) + | ||
(uint8[i + 2] & 0xFF) | ||
output.push(tripletToBase64(tmp)) | ||
} | ||
return output.join('') | ||
} | ||
function fromByteArray (uint8) { | ||
var tmp | ||
var len = uint8.length | ||
var extraBytes = len % 3 // if we have 1 byte left, pad 2 bytes | ||
var parts = [] | ||
var maxChunkLength = 16383 // must be multiple of 3 | ||
// go through the array every three bytes, we'll deal with trailing stuff later | ||
for (var i = 0, len2 = len - extraBytes; i < len2; i += maxChunkLength) { | ||
parts.push(encodeChunk( | ||
uint8, i, (i + maxChunkLength) > len2 ? len2 : (i + maxChunkLength) | ||
)) | ||
} | ||
// pad the end with zeros, but make sure to not forget the extra bytes | ||
if (extraBytes === 1) { | ||
tmp = uint8[len - 1] | ||
parts.push( | ||
lookup[tmp >> 2] + | ||
lookup[(tmp << 4) & 0x3F] + | ||
'==' | ||
) | ||
} else if (extraBytes === 2) { | ||
tmp = (uint8[len - 2] << 8) + uint8[len - 1] | ||
parts.push( | ||
lookup[tmp >> 10] + | ||
lookup[(tmp >> 4) & 0x3F] + | ||
lookup[(tmp << 2) & 0x3F] + | ||
'=' | ||
) | ||
} | ||
return parts.join('') | ||
} | ||
/***/ }) | ||
/******/ ]); | ||
/******/ }); |
@@ -0,3 +1,4 @@ | ||
import * as F from "./figma-json"; | ||
export default function genDefaults(): Promise<{ | ||
[key: string]: SceneNode; | ||
[key: string]: F.SceneNode; | ||
}>; |
@@ -1,4 +0,6 @@ | ||
import { DumpedFigma } from "./figmaJSON"; | ||
export declare const blacklist: Set<string>; | ||
export declare function dump(n: readonly SceneNode[]): Promise<DumpedFigma>; | ||
export declare function insert(n: DumpedFigma): Promise<SceneNode[]>; | ||
import * as F from "./figma-json"; | ||
export * from "./figma-json"; | ||
export declare const readBlacklist: Set<string>; | ||
export declare const writeBlacklist: Set<string>; | ||
export declare function dump(n: readonly SceneNode[]): Promise<F.DumpedFigma>; | ||
export declare function insert(n: F.DumpedFigma): Promise<SceneNode[]>; |
{ | ||
"name": "figma-json-plugin", | ||
"version": "0.0.2-3", | ||
"version": "0.0.3", | ||
"description": "Dump a hierarchy to JSON within a Figma document. Intended for use within Figma plugins.", | ||
@@ -5,0 +5,0 @@ "main": "dist/main.js", |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
35520
7
949
1