Commit 3b905a18 authored by rockyl's avatar rockyl

init

parent 9552a48b
This diff is collapsed.
...@@ -2,13 +2,18 @@ ...@@ -2,13 +2,18 @@
Object.defineProperty(exports, '__esModule', { value: true }); Object.defineProperty(exports, '__esModule', { value: true });
function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; } var path = require('path');
var path = _interopDefault(require('path'));
require('color'); require('color');
var generateUUID = _interopDefault(require('uuid/v4')); var generateUUID = require('uuid/v4');
var hash = _interopDefault(require('object-hash')); var hash = require('object-hash');
var zlib = _interopDefault(require('zlib')); var zlib = require('zlib');
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
var path__default = /*#__PURE__*/_interopDefaultLegacy(path);
var generateUUID__default = /*#__PURE__*/_interopDefaultLegacy(generateUUID);
var hash__default = /*#__PURE__*/_interopDefaultLegacy(hash);
var zlib__default = /*#__PURE__*/_interopDefaultLegacy(zlib);
/** /**
* Created by rockyl on 2019-08-09. * Created by rockyl on 2019-08-09.
...@@ -110,9 +115,9 @@ async function execute(psdFile, options) { ...@@ -110,9 +115,9 @@ async function execute(psdFile, options) {
const isCenter = mode === 'center'; const isCenter = mode === 'center';
let viewRoot = { let viewRoot = {
name: path.basename(psdFile.name, '.psd'), name: path__default['default'].basename(psdFile.name, '.psd'),
type: 'node', type: 'node',
uuid: generateUUID(), uuid: generateUUID__default['default'](),
}; };
const assets = []; const assets = [];
...@@ -214,7 +219,7 @@ async function execute(psdFile, options) { ...@@ -214,7 +219,7 @@ async function execute(psdFile, options) {
let viewNode = { let viewNode = {
name, name,
properties, properties,
uuid: generateUUID(), uuid: generateUUID__default['default'](),
}; };
let dealLater = true; let dealLater = true;
let px = x;// - (!singleView && isSecondLayer ? offset.x : 0); let px = x;// - (!singleView && isSecondLayer ? offset.x : 0);
...@@ -266,7 +271,7 @@ async function execute(psdFile, options) { ...@@ -266,7 +271,7 @@ async function execute(psdFile, options) {
} else { } else {
viewNode.type = 'image'; viewNode.type = 'image';
let uuid = generateUUID(); let uuid = generateUUID__default['default']();
const ext = '.png'; const ext = '.png';
let dataUrl; let dataUrl;
...@@ -280,7 +285,7 @@ async function execute(psdFile, options) { ...@@ -280,7 +285,7 @@ async function execute(psdFile, options) {
if (dataUrl) { if (dataUrl) {
let base64Data = dataUrl.replace(/^data:image\/\w+;base64,/, ""); let base64Data = dataUrl.replace(/^data:image\/\w+;base64,/, "");
let buffer = new Buffer(base64Data, 'base64'); let buffer = new Buffer(base64Data, 'base64');
const fileNameHash = hash(buffer); const fileNameHash = hash__default['default'](buffer);
if (imageHashMap.hasOwnProperty(fileNameHash)) { if (imageHashMap.hasOwnProperty(fileNameHash)) {
uuid = imageHashMap[fileNameHash]; uuid = imageHashMap[fileNameHash];
} else { } else {
...@@ -317,7 +322,7 @@ async function execute(psdFile, options) { ...@@ -317,7 +322,7 @@ async function execute(psdFile, options) {
let buf = new Buffer(dataString); let buf = new Buffer(dataString);
return await new Promise((resolve, reject) => { return await new Promise((resolve, reject) => {
zlib.gzip(buf, function (err, res) { zlib__default['default'].gzip(buf, function (err, res) {
if (err) { if (err) {
reject(err); reject(err);
} else { } else {
......
This diff is collapsed.
(function (global, factory) { (function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('path'), require('color'), require('uuid/v4'), require('object-hash'), require('zlib')) : typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('path'), require('color'), require('uuid/v4'), require('object-hash'), require('zlib')) :
typeof define === 'function' && define.amd ? define(['exports', 'path', 'color', 'uuid/v4', 'object-hash', 'zlib'], factory) : typeof define === 'function' && define.amd ? define(['exports', 'path', 'color', 'uuid/v4', 'object-hash', 'zlib'], factory) :
(global = global || self, factory(global['psd-parse-web'] = {}, global.path, global.color, global.generateUUID, global.hash, global.zlib)); (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global['psd-parse-web'] = {}, global.path, null, global.generateUUID, global.hash, global.zlib));
}(this, function (exports, path, color, generateUUID, hash, zlib) { 'use strict'; }(this, (function (exports, path, color, generateUUID, hash, zlib) { 'use strict';
path = path && path.hasOwnProperty('default') ? path['default'] : path; function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
color = color && color.hasOwnProperty('default') ? color['default'] : color;
generateUUID = generateUUID && generateUUID.hasOwnProperty('default') ? generateUUID['default'] : generateUUID; var path__default = /*#__PURE__*/_interopDefaultLegacy(path);
hash = hash && hash.hasOwnProperty('default') ? hash['default'] : hash; var generateUUID__default = /*#__PURE__*/_interopDefaultLegacy(generateUUID);
zlib = zlib && zlib.hasOwnProperty('default') ? zlib['default'] : zlib; var hash__default = /*#__PURE__*/_interopDefaultLegacy(hash);
var zlib__default = /*#__PURE__*/_interopDefaultLegacy(zlib);
/** /**
* Created by rockyl on 2019-08-09. * Created by rockyl on 2019-08-09.
...@@ -110,9 +111,9 @@ ...@@ -110,9 +111,9 @@
const isCenter = mode === 'center'; const isCenter = mode === 'center';
let viewRoot = { let viewRoot = {
name: path.basename(psdFile.name, '.psd'), name: path__default['default'].basename(psdFile.name, '.psd'),
type: 'node', type: 'node',
uuid: generateUUID(), uuid: generateUUID__default['default'](),
}; };
const assets = []; const assets = [];
...@@ -214,7 +215,7 @@ ...@@ -214,7 +215,7 @@
let viewNode = { let viewNode = {
name, name,
properties, properties,
uuid: generateUUID(), uuid: generateUUID__default['default'](),
}; };
let dealLater = true; let dealLater = true;
let px = x;// - (!singleView && isSecondLayer ? offset.x : 0); let px = x;// - (!singleView && isSecondLayer ? offset.x : 0);
...@@ -266,7 +267,7 @@ ...@@ -266,7 +267,7 @@
} else { } else {
viewNode.type = 'image'; viewNode.type = 'image';
let uuid = generateUUID(); let uuid = generateUUID__default['default']();
const ext = '.png'; const ext = '.png';
let dataUrl; let dataUrl;
...@@ -280,7 +281,7 @@ ...@@ -280,7 +281,7 @@
if (dataUrl) { if (dataUrl) {
let base64Data = dataUrl.replace(/^data:image\/\w+;base64,/, ""); let base64Data = dataUrl.replace(/^data:image\/\w+;base64,/, "");
let buffer = new Buffer(base64Data, 'base64'); let buffer = new Buffer(base64Data, 'base64');
const fileNameHash = hash(buffer); const fileNameHash = hash__default['default'](buffer);
if (imageHashMap.hasOwnProperty(fileNameHash)) { if (imageHashMap.hasOwnProperty(fileNameHash)) {
uuid = imageHashMap[fileNameHash]; uuid = imageHashMap[fileNameHash];
} else { } else {
...@@ -317,7 +318,7 @@ ...@@ -317,7 +318,7 @@
let buf = new Buffer(dataString); let buf = new Buffer(dataString);
return await new Promise((resolve, reject) => { return await new Promise((resolve, reject) => {
zlib.gzip(buf, function (err, res) { zlib__default['default'].gzip(buf, function (err, res) {
if (err) { if (err) {
reject(err); reject(err);
} else { } else {
...@@ -333,5 +334,5 @@ ...@@ -333,5 +334,5 @@
Object.defineProperty(exports, '__esModule', { value: true }); Object.defineProperty(exports, '__esModule', { value: true });
})); })));
//# sourceMappingURL=index.umd.js.map //# sourceMappingURL=index.umd.js.map
This diff is collapsed.
...@@ -2,9 +2,8 @@ ...@@ -2,9 +2,8 @@
<module type="WEB_MODULE" version="4"> <module type="WEB_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true"> <component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output /> <exclude-output />
<content url="file://$MODULE_DIR$"> <content url="file://$MODULE_DIR$" />
<excludeFolder url="file://$MODULE_DIR$/dist" /> <orderEntry type="inheritedJdk" />
</content>
<orderEntry type="sourceFolder" forTests="false" /> <orderEntry type="sourceFolder" forTests="false" />
</component> </component>
</module> </module>
\ No newline at end of file
/**
* Created by rockyl on 2019-09-26.
*
* 导出zeroing的视图
*/
import {getTree} from "./psd-tree";
import {walkNode} from "./utils";
import path from 'path'
import Color from 'color'
import generateUUID from 'uuid/v4'
import hash from 'object-hash';
import zlib from 'zlib';
const relativePosPrefixMap = {
l: {field: 'left',},
t: {field: 'top',},
r: {field: 'right',},
b: {field: 'bottom',},
h: {field: 'horizonCenter',},
v: {field: 'verticalCenter',},
wp: {field: 'width',},
hp: {field: 'height',},
lp: {field: 'left',},
tp: {field: 'top',},
rp: {field: 'right',},
bp: {field: 'bottom',},
};
const offsetAll = 176;
const enterChar = String.fromCharCode(13);
export async function execute(psdFile, options) {
const tree = await getTree(psdFile);
const {mode = 'none', singleView = true} = options;
let offset = {x: 0, y: 0};
let cutSize = {x: 0, y: 0};
if (mode !== 'none') {
cutSize.y = offsetAll;
}
switch (mode) {
case 'top':
offset.y = offsetAll;
break;
case 'center':
offset.y = offsetAll / 2;
break;
}
const isCenter = mode === 'center';
let viewRoot = {
name: path.basename(psdFile.name, '.psd'),
type: 'node',
uuid: generateUUID(),
};
const assets = [];
const imageHashMap = {};
let {width: stageWidthOrigin, height: stageHeightOrigin} = tree;
const stageWidth = stageWidthOrigin - cutSize.x || 0;
const stageHeight = stageHeightOrigin - cutSize.y || 0;
await walkNode(tree, async function (node, parent) {
let {name} = node;
const {x, y, width, height, alpha, visible, origin: {layer, layer: {typeTool, solidColor}}} = node;
//console.log('walk node:', name);
let properties = {
width, height, alpha, visible,
};
const isSecondLayer = singleView && !parent.origin.parent || !singleView && parent.origin.parent && !parent.origin.parent.parent;
const shouldVerticalCenter = isSecondLayer && isCenter;
const {width: parentWidth, height: parentHeight} = parent;
if (name.includes('|') || shouldVerticalCenter) {
try {
let arr = name.split('|');
name = arr[0];
let paramsStr = arr[1];
let relativePos;
if(paramsStr){
let params = paramsStr.split(';');
relativePos = params[0];
}else if(shouldVerticalCenter){
relativePos = 'v';
}
if (relativePos) {
let items = relativePos.split(',');
for (let item of items) {
let result = item.match(/[a-zA-Z]+/);
if (!result) {
continue;
}
let prefix = result[0];
let mapItem = relativePosPrefixMap[prefix];
if (mapItem) {
let {field,} = mapItem;
let value = item.substr(prefix.length);
let hasValue = value.length > 0;
let fieldChar = prefix[0];
if (!hasValue) {
switch (fieldChar) {
case 'l':
value = x - offset.x;
break;
case 't':
value = y - offset.y;
break;
case 'r':
value = stageWidth - (x - offset.x) - width;
break;
case 'b':
value = stageHeight - (y - offset.y) - height;
break;
case 'h':
value = x + width / 2 - stageWidthOrigin / 2;
break;
case 'v':
value = y + height / 2 - stageHeightOrigin / 2;
break;
}
}
let isPercent = prefix.endsWith('p');
if (isPercent) {
if (!hasValue) {
switch (fieldChar) {
case 'l':
case 'r':
value /= stageWidth;
break;
case 't':
case 'b':
value /= stageHeight;
break;
}
value = Math.floor(value * 100);
}
value += '%';
} else {
value = parseFloat(value);
if (isNaN(value)) {
value = 0;
}
}
properties[field] = value;
}
}
}
} catch (e) {
console.log(e);
}
}
let viewNode = {
name,
properties,
uuid: generateUUID(),
};
let dealLater = true;
let px = x;// - (!singleView && isSecondLayer ? offset.x : 0);
let py = y;// - (!singleView && isSecondLayer ? offset.y : 0);
if (px !== 0) {
properties.x = px;
}
if (py !== 0) {
properties.y = py;
}
if (typeTool) {
let fontInfo = typeTool();
let text = fontInfo.textValue;
if (text.indexOf(enterChar) < 0) { //如果未包含回车说明是多行,去掉宽度
delete properties['width'];
}
properties.text = text;
const sizes = fontInfo.sizes();
const colors = fontInfo.colors();
properties.size = sizes ? sizes[0] || 20 : 20;
let [r, g, b, a] = colors[0];
properties.fillColor = `rgba(${r}, ${g}, ${b}, ${a / 255})`;
viewNode.type = 'label';
dealLater = false;
} else if (solidColor && layer.vectorMask) {
let paths = layer.vectorMask().paths;
if (paths[2].numPoints === 4) {
let isRect = true;
for (let i = 3; i < paths.length; i++) {
if (paths[i].recordType !== 2) {
isRect = false;
break;
}
}
if (isRect) {
viewNode.type = 'rect';
const {r, g, b} = solidColor();
properties.fillColor = `rgba(${r}, ${g}, ${b}, 1)`;
dealLater = false;
}
}
}
if (dealLater) {
if (node.hasOwnProperty('children')) {
viewNode.type = 'node';
} else {
viewNode.type = 'image';
let uuid = generateUUID();
const ext = '.png';
let dataUrl;
try {
let img = node.origin.toPng();
dataUrl = img.src;
} catch (e) {
}
if (dataUrl) {
let base64Data = dataUrl.replace(/^data:image\/\w+;base64,/, "");
let buffer = new Buffer(base64Data, 'base64');
const fileNameHash = hash(buffer);
if (imageHashMap.hasOwnProperty(fileNameHash)) {
uuid = imageHashMap[fileNameHash];
} else {
imageHashMap[fileNameHash] = uuid;
assets.push({
name,
ext,
uuid,
base64Data,
hash: fileNameHash,
});
}
properties.source = 'asset://' + uuid;
}
}
}
let viewParent = parent.view || viewRoot;
if (!viewParent.hasOwnProperty('children')) {
viewParent.children = [];
}
viewParent.children.push(viewNode);
node.view = viewNode;
});
let data = {
view: viewRoot,
assets,
};
let dataString = JSON.stringify(data);
let buf = new Buffer(dataString);
return await new Promise((resolve, reject) => {
zlib.gzip(buf, function (err, res) {
if (err) {
reject(err);
} else {
console.log(res.length);
resolve(res);
}
})
})
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment