Commit 005b9d53 authored by 张九刚's avatar 张九刚

修改node版本

parent 21a0f975
......@@ -11,7 +11,7 @@ var Color = require('color');
var generateUUID = require('uuid/v4');
var fs = require("fs-extra");
var hash = require('object-hash');
var zlib =require('zlib');
var zlib = require('zlib');
const relativePosPrefixMap = {
l: { field: 'left', },
t: { field: 'top', },
......@@ -29,8 +29,16 @@ const relativePosPrefixMap = {
const offsetAll = 176;
async function execute(psdFile, options = {}) {
console.log("psdFile:",psdFile);
async function execute(psdFile, options) {
console.log("psdFile:", psdFile);
const { imagesPath } = options;
if(!imagesPath)
{
console.log("没有指定cache目录");
}
const tree = await getTree(psdFile);
const { mode = 'none', singleView = true } = options;
......@@ -53,7 +61,7 @@ async function execute(psdFile, options = {}) {
let viewRoot = {
name: path.basename(psdFile, '.psd'),
componentName: 'Div',
nodeType: 'Div',
uuid: generateUUID(),
};
......@@ -195,7 +203,7 @@ async function execute(psdFile, options = {}) {
properties.style.fontSize = sizes ? sizes[0] || 20 : 20;
let [r, g, b, a] = colors[0];
properties.style.color = `rgba(${r}, ${g}, ${b}, ${a / 255})`;
viewNode.componentName = 'Label';
viewNode.nodeType = 'Label';
dealLater = false;
} else if (solidColor && layer.vectorMask) {
let paths = layer.vectorMask().paths;
......@@ -208,7 +216,7 @@ async function execute(psdFile, options = {}) {
}
}
if (isRect) {
viewNode.componentName = 'Div';
viewNode.nodeType = 'Div';
const { r, g, b } = solidColor();
properties.style.backgroundColor = `rgba(${r}, ${g}, ${b}, 1)`;
dealLater = false;
......@@ -218,24 +226,28 @@ async function execute(psdFile, options = {}) {
if (dealLater) {
if (node.hasOwnProperty('children')) {
viewNode.componentName = 'Div';
viewNode.nodeType = 'Div';
} else {
viewNode.componentName = 'Image';
viewNode.nodeType = 'Image';
let uuid = generateUUID();
const ext = '.png';
let dataUrl;
let buffer;
try {
let img = node.origin.toPng();
dataUrl = img.src;
const imageFilePath = path.join(imagesPath, uuid + ext);
buffer = await savePng(img, imageFilePath).catch(e => {
});
dataUrl = buffer.toString('base64');
} catch (e) {
}
if (dataUrl) {
// let base64Data = dataUrl.replace(/^data:image\/\w+;base64,/, "");
// let buffer = new Buffer.from(base64Data, 'base64');
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];
......@@ -263,6 +275,11 @@ async function execute(psdFile, options = {}) {
node.view = viewNode;
});
if (viewRoot.children && viewRoot.children.length) {
viewRoot.children.map(item => {
item.viewType = 'Page'
})
}
let data = {
pluginVersion: "0.0.1",
reference: "psd",
......@@ -285,6 +302,22 @@ async function execute(psdFile, options = {}) {
})
}
function savePng(png, output) {
return new Promise((resolve, reject) => {
let buffer, buffers = [];
png.pack()
.on('error', reject)
.on('data', (data) => buffers.push(data))
.on('end', () => {
buffer = Buffer.concat(buffers)
})
.pipe(fs.createWriteStream(output))
.on('finish', () => {
resolve(buffer);
});
});
}
......
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