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

修改node版本

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