Commit 05b0ebd5 authored by rockyl's avatar rockyl

增加相对位置的解析

parent 3b226df5
...@@ -65,6 +65,17 @@ async function walkNode(node, callback, includeSelf = false) { ...@@ -65,6 +65,17 @@ async function walkNode(node, callback, includeSelf = false) {
* 导出zeroing的视图 * 导出zeroing的视图
*/ */
const relativePosPrefixMap = {
l: 'left',
t: 'top',
r: 'right',
b: 'bottom',
h: 'horizonCenter',
v: 'verticalCenter',
wp: 'percentWidth',
hp: 'percentHeight',
};
async function execute(psdFile, options) { async function execute(psdFile, options) {
const tree = await getTree(psdFile); const tree = await getTree(psdFile);
...@@ -78,11 +89,31 @@ async function execute(psdFile, options) { ...@@ -78,11 +89,31 @@ async function execute(psdFile, options) {
const imageHashMap = {}; const imageHashMap = {};
await walkNode(tree, async function (node, parent) { await walkNode(tree, async function (node, parent) {
const {name, x, y, width, height, alpha, visible, origin: {layer, layer: {typeTool, solidColor}}} = node; let {name} = node;
const {x, y, width, height, alpha, visible, origin: {layer, layer: {typeTool, solidColor}}} = node;
console.log('walk node:', name); console.log('walk node:', name);
let properties = { let properties = {
width, height, alpha, visible, width, height, alpha, visible,
}; };
if (name.includes('|')) {
let arr = name.split('|');
name = arr[0];
let paramsStr = arr[0];
let params = paramsStr.split(';');
let relativePos = params[0];
if (relativePos) {
let items = relativePos.split(',');
for (let item of items) {
let prefix = item[0];
let field = relativePosPrefixMap[prefix];
if (field) {
properties[field] = parseFloat(item.substr(1));
}
}
}
}
let viewNode = { let viewNode = {
name, name,
properties, properties,
...@@ -98,19 +129,12 @@ async function execute(psdFile, options) { ...@@ -98,19 +129,12 @@ async function execute(psdFile, options) {
if (typeTool) { if (typeTool) {
let fontInfo = typeTool(); let fontInfo = typeTool();
//const fonts = fontInfo.fonts();
//const styles = fontInfo.styles();
//const {RunLengthArray} = fontInfo.engineData.EngineDict.StyleRun;
properties.text = fontInfo.textValue; properties.text = fontInfo.textValue;
const sizes = fontInfo.sizes(); const sizes = fontInfo.sizes();
const colors = fontInfo.colors(); const colors = fontInfo.colors();
properties.size = sizes ? sizes[0] || 20 : 20; properties.size = sizes ? sizes[0] || 20 : 20;
let [r, g, b, a] = colors[0]; let [r, g, b, a] = colors[0];
properties.fillColor = `rgba(${r}, ${g}, ${b}, ${a / 255})`; properties.fillColor = `rgba(${r}, ${g}, ${b}, ${a / 255})`;
/*properties.textflow = {
fonts, styles, RunLengthArray,
};*/
viewNode.type = 'label'; viewNode.type = 'label';
dealLater = false; dealLater = false;
} else if (solidColor) { } else if (solidColor) {
...@@ -147,7 +171,7 @@ async function execute(psdFile, options) { ...@@ -147,7 +171,7 @@ async function execute(psdFile, options) {
try { try {
let img = node.origin.toPng(); let img = node.origin.toPng();
dataUrl = img.src; dataUrl = img.src;
}catch (e) { } catch (e) {
} }
...@@ -191,11 +215,11 @@ async function execute(psdFile, options) { ...@@ -191,11 +215,11 @@ async function execute(psdFile, options) {
console.log(dataString.length); console.log(dataString.length);
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.gzip(buf, function (err, res) {
if(err){ if (err) {
reject(err); reject(err);
}else{ } else {
console.log(res.length); console.log(res.length);
resolve(res); resolve(res);
} }
......
This diff is collapsed.
...@@ -71,6 +71,17 @@ async function walkNode(node, callback, includeSelf = false) { ...@@ -71,6 +71,17 @@ async function walkNode(node, callback, includeSelf = false) {
* 导出zeroing的视图 * 导出zeroing的视图
*/ */
const relativePosPrefixMap = {
l: 'left',
t: 'top',
r: 'right',
b: 'bottom',
h: 'horizonCenter',
v: 'verticalCenter',
wp: 'percentWidth',
hp: 'percentHeight',
};
async function execute(psdFile, options) { async function execute(psdFile, options) {
const tree = await getTree(psdFile); const tree = await getTree(psdFile);
...@@ -84,11 +95,31 @@ async function execute(psdFile, options) { ...@@ -84,11 +95,31 @@ async function execute(psdFile, options) {
const imageHashMap = {}; const imageHashMap = {};
await walkNode(tree, async function (node, parent) { await walkNode(tree, async function (node, parent) {
const {name, x, y, width, height, alpha, visible, origin: {layer, layer: {typeTool, solidColor}}} = node; let {name} = node;
const {x, y, width, height, alpha, visible, origin: {layer, layer: {typeTool, solidColor}}} = node;
console.log('walk node:', name); console.log('walk node:', name);
let properties = { let properties = {
width, height, alpha, visible, width, height, alpha, visible,
}; };
if (name.includes('|')) {
let arr = name.split('|');
name = arr[0];
let paramsStr = arr[0];
let params = paramsStr.split(';');
let relativePos = params[0];
if (relativePos) {
let items = relativePos.split(',');
for (let item of items) {
let prefix = item[0];
let field = relativePosPrefixMap[prefix];
if (field) {
properties[field] = parseFloat(item.substr(1));
}
}
}
}
let viewNode = { let viewNode = {
name, name,
properties, properties,
...@@ -104,19 +135,12 @@ async function execute(psdFile, options) { ...@@ -104,19 +135,12 @@ async function execute(psdFile, options) {
if (typeTool) { if (typeTool) {
let fontInfo = typeTool(); let fontInfo = typeTool();
//const fonts = fontInfo.fonts();
//const styles = fontInfo.styles();
//const {RunLengthArray} = fontInfo.engineData.EngineDict.StyleRun;
properties.text = fontInfo.textValue; properties.text = fontInfo.textValue;
const sizes = fontInfo.sizes(); const sizes = fontInfo.sizes();
const colors = fontInfo.colors(); const colors = fontInfo.colors();
properties.size = sizes ? sizes[0] || 20 : 20; properties.size = sizes ? sizes[0] || 20 : 20;
let [r, g, b, a] = colors[0]; let [r, g, b, a] = colors[0];
properties.fillColor = `rgba(${r}, ${g}, ${b}, ${a / 255})`; properties.fillColor = `rgba(${r}, ${g}, ${b}, ${a / 255})`;
/*properties.textflow = {
fonts, styles, RunLengthArray,
};*/
viewNode.type = 'label'; viewNode.type = 'label';
dealLater = false; dealLater = false;
} else if (solidColor) { } else if (solidColor) {
...@@ -153,7 +177,7 @@ async function execute(psdFile, options) { ...@@ -153,7 +177,7 @@ async function execute(psdFile, options) {
try { try {
let img = node.origin.toPng(); let img = node.origin.toPng();
dataUrl = img.src; dataUrl = img.src;
}catch (e) { } catch (e) {
} }
...@@ -197,11 +221,11 @@ async function execute(psdFile, options) { ...@@ -197,11 +221,11 @@ async function execute(psdFile, options) {
console.log(dataString.length); console.log(dataString.length);
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.gzip(buf, function (err, res) {
if(err){ if (err) {
reject(err); reject(err);
}else{ } else {
console.log(res.length); console.log(res.length);
resolve(res); resolve(res);
} }
......
This diff is collapsed.
...@@ -71,6 +71,17 @@ ...@@ -71,6 +71,17 @@
* 导出zeroing的视图 * 导出zeroing的视图
*/ */
const relativePosPrefixMap = {
l: 'left',
t: 'top',
r: 'right',
b: 'bottom',
h: 'horizonCenter',
v: 'verticalCenter',
wp: 'percentWidth',
hp: 'percentHeight',
};
async function execute(psdFile, options) { async function execute(psdFile, options) {
const tree = await getTree(psdFile); const tree = await getTree(psdFile);
...@@ -84,11 +95,31 @@ ...@@ -84,11 +95,31 @@
const imageHashMap = {}; const imageHashMap = {};
await walkNode(tree, async function (node, parent) { await walkNode(tree, async function (node, parent) {
const {name, x, y, width, height, alpha, visible, origin: {layer, layer: {typeTool, solidColor}}} = node; let {name} = node;
const {x, y, width, height, alpha, visible, origin: {layer, layer: {typeTool, solidColor}}} = node;
console.log('walk node:', name); console.log('walk node:', name);
let properties = { let properties = {
width, height, alpha, visible, width, height, alpha, visible,
}; };
if (name.includes('|')) {
let arr = name.split('|');
name = arr[0];
let paramsStr = arr[0];
let params = paramsStr.split(';');
let relativePos = params[0];
if (relativePos) {
let items = relativePos.split(',');
for (let item of items) {
let prefix = item[0];
let field = relativePosPrefixMap[prefix];
if (field) {
properties[field] = parseFloat(item.substr(1));
}
}
}
}
let viewNode = { let viewNode = {
name, name,
properties, properties,
...@@ -104,19 +135,12 @@ ...@@ -104,19 +135,12 @@
if (typeTool) { if (typeTool) {
let fontInfo = typeTool(); let fontInfo = typeTool();
//const fonts = fontInfo.fonts();
//const styles = fontInfo.styles();
//const {RunLengthArray} = fontInfo.engineData.EngineDict.StyleRun;
properties.text = fontInfo.textValue; properties.text = fontInfo.textValue;
const sizes = fontInfo.sizes(); const sizes = fontInfo.sizes();
const colors = fontInfo.colors(); const colors = fontInfo.colors();
properties.size = sizes ? sizes[0] || 20 : 20; properties.size = sizes ? sizes[0] || 20 : 20;
let [r, g, b, a] = colors[0]; let [r, g, b, a] = colors[0];
properties.fillColor = `rgba(${r}, ${g}, ${b}, ${a / 255})`; properties.fillColor = `rgba(${r}, ${g}, ${b}, ${a / 255})`;
/*properties.textflow = {
fonts, styles, RunLengthArray,
};*/
viewNode.type = 'label'; viewNode.type = 'label';
dealLater = false; dealLater = false;
} else if (solidColor) { } else if (solidColor) {
...@@ -153,7 +177,7 @@ ...@@ -153,7 +177,7 @@
try { try {
let img = node.origin.toPng(); let img = node.origin.toPng();
dataUrl = img.src; dataUrl = img.src;
}catch (e) { } catch (e) {
} }
...@@ -197,11 +221,11 @@ ...@@ -197,11 +221,11 @@
console.log(dataString.length); console.log(dataString.length);
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.gzip(buf, function (err, res) {
if(err){ if (err) {
reject(err); reject(err);
}else{ } else {
console.log(res.length); console.log(res.length);
resolve(res); resolve(res);
} }
......
This diff is collapsed.
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
"module": "dist/index.es.js", "module": "dist/index.es.js",
"license": "MIT", "license": "MIT",
"scripts": { "scripts": {
"dev": "rollup -c -w",
"build": "rollup -c" "build": "rollup -c"
}, },
"dependencies": { "dependencies": {
......
...@@ -12,6 +12,17 @@ import generateUUID from 'uuid/v4' ...@@ -12,6 +12,17 @@ import generateUUID from 'uuid/v4'
import hash from 'object-hash'; import hash from 'object-hash';
import zlib from 'zlib'; import zlib from 'zlib';
const relativePosPrefixMap = {
l: 'left',
t: 'top',
r: 'right',
b: 'bottom',
h: 'horizonCenter',
v: 'verticalCenter',
wp: 'percentWidth',
hp: 'percentHeight',
};
export async function execute(psdFile, options) { export async function execute(psdFile, options) {
const tree = await getTree(psdFile); const tree = await getTree(psdFile);
...@@ -25,11 +36,31 @@ export async function execute(psdFile, options) { ...@@ -25,11 +36,31 @@ export async function execute(psdFile, options) {
const imageHashMap = {}; const imageHashMap = {};
await walkNode(tree, async function (node, parent) { await walkNode(tree, async function (node, parent) {
const {name, x, y, width, height, alpha, visible, origin: {layer, layer: {typeTool, solidColor}}} = node; let {name} = node;
const {x, y, width, height, alpha, visible, origin: {layer, layer: {typeTool, solidColor}}} = node;
console.log('walk node:', name); console.log('walk node:', name);
let properties = { let properties = {
width, height, alpha, visible, width, height, alpha, visible,
}; };
if (name.includes('|')) {
let arr = name.split('|');
name = arr[0];
let paramsStr = arr[0];
let params = paramsStr.split(';');
let relativePos = params[0];
if (relativePos) {
let items = relativePos.split(',');
for (let item of items) {
let prefix = item[0];
let field = relativePosPrefixMap[prefix];
if (field) {
properties[field] = parseFloat(item.substr(1));
}
}
}
}
let viewNode = { let viewNode = {
name, name,
properties, properties,
...@@ -45,19 +76,12 @@ export async function execute(psdFile, options) { ...@@ -45,19 +76,12 @@ export async function execute(psdFile, options) {
if (typeTool) { if (typeTool) {
let fontInfo = typeTool(); let fontInfo = typeTool();
//const fonts = fontInfo.fonts();
//const styles = fontInfo.styles();
//const {RunLengthArray} = fontInfo.engineData.EngineDict.StyleRun;
properties.text = fontInfo.textValue; properties.text = fontInfo.textValue;
const sizes = fontInfo.sizes(); const sizes = fontInfo.sizes();
const colors = fontInfo.colors(); const colors = fontInfo.colors();
properties.size = sizes ? sizes[0] || 20 : 20; properties.size = sizes ? sizes[0] || 20 : 20;
let [r, g, b, a] = colors[0]; let [r, g, b, a] = colors[0];
properties.fillColor = `rgba(${r}, ${g}, ${b}, ${a / 255})`; properties.fillColor = `rgba(${r}, ${g}, ${b}, ${a / 255})`;
/*properties.textflow = {
fonts, styles, RunLengthArray,
};*/
viewNode.type = 'label'; viewNode.type = 'label';
dealLater = false; dealLater = false;
} else if (solidColor) { } else if (solidColor) {
...@@ -94,7 +118,7 @@ export async function execute(psdFile, options) { ...@@ -94,7 +118,7 @@ export async function execute(psdFile, options) {
try { try {
let img = node.origin.toPng(); let img = node.origin.toPng();
dataUrl = img.src; dataUrl = img.src;
}catch (e) { } catch (e) {
} }
...@@ -138,11 +162,11 @@ export async function execute(psdFile, options) { ...@@ -138,11 +162,11 @@ export async function execute(psdFile, options) {
console.log(dataString.length); console.log(dataString.length);
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.gzip(buf, function (err, res) {
if(err){ if (err) {
reject(err); reject(err);
}else{ } else {
console.log(res.length); console.log(res.length);
resolve(res); 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