Commit efabca6d authored by 张九刚's avatar 张九刚

新增mode适配

parent f10828d2
{ {
"name": "psd-parse-web", "name": "psd-parse-web",
"version": "2.0.4", "version": "2.0.5",
"main": "src/index.js", "main": "src/index.js",
"module": "dist/index.es.js", "module": "dist/index.es.js",
"license": "MIT", "license": "MIT",
......
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
*/ */
var { getTree } = require("./psd-tree"); var { getTree } = require("./psd-tree");
var { walkNode, trimCustom } = require("./utils"); var { walkNode, trimCustom, getPageStyle } = require("./utils");
var path = require('path'); var path = require('path');
var Color = require('color'); var Color = require('color');
var generateUUID = require('uuid/v4'); var generateUUID = require('uuid/v4');
...@@ -30,7 +30,7 @@ const relativePosPrefixMap = { ...@@ -30,7 +30,7 @@ const relativePosPrefixMap = {
async function compilePsdToJson(psdFile, options) { async function compilePsdToJson(psdFile, options) {
const { assetsPath } = options; const { mode, assetsPath } = options;
if (!assetsPath) { if (!assetsPath) {
console.log("没有指定assets目录"); console.log("没有指定assets目录");
} }
...@@ -60,12 +60,12 @@ async function compilePsdToJson(psdFile, options) { ...@@ -60,12 +60,12 @@ async function compilePsdToJson(psdFile, options) {
console.warn(`${page.name}不合法,请保持设计稿根目录为全部文件夹形式`); console.warn(`${page.name}不合法,请保持设计稿根目录为全部文件夹形式`);
} else { } else {
const { x, y, width, height } = page; const { x, y, width, height } = page;
const viewNode = { let viewNode = {
name: folderName, name: folderName,
nodeType: 'Div', nodeType: 'Div',
properties: { properties: {
style: { style: {
width, height, left: x, top: y, position: "absolute" width, height, ...getPageStyle(mode, x, y)
}, },
attrs: { attrs: {
...@@ -75,12 +75,15 @@ async function compilePsdToJson(psdFile, options) { ...@@ -75,12 +75,15 @@ async function compilePsdToJson(psdFile, options) {
uuid: generateUUID(), uuid: generateUUID(),
children: [] children: []
}; };
viewRoot.children.push(viewNode); viewRoot.children.push(viewNode);
} }
await walkNode(page, async (node, parent) => { await walkNode(page, async (node, parent) => {
let { name } = node; let { name } = node;
name = trimCustom(name); name = trimCustom(name);
const { x, y, width, height, origin: { layer, layer: { typeTool, solidColor } } } = node; const { x, y, width, height, origin: { layer, layer: { typeTool, solidColor } } } = node;
let properties = { let properties = {
style: { style: {
...@@ -93,7 +96,7 @@ async function compilePsdToJson(psdFile, options) { ...@@ -93,7 +96,7 @@ async function compilePsdToJson(psdFile, options) {
}; };
let viewNode = { let viewNode = {
name, name:name.split('|')[0], //如果设置了适配的话,作为名字截取掉
properties, properties,
uuid: generateUUID(), uuid: generateUUID(),
}; };
...@@ -213,15 +216,9 @@ async function compilePsdToJson(psdFile, options) { ...@@ -213,15 +216,9 @@ async function compilePsdToJson(psdFile, options) {
return data; return data;
} }
/**
* 单页解析
* @param {*} node
* @param {*} parent
* @param {*} folderName
*/
async function compilePage(node, parent, folderName, assetsPath) {
}
function savePng(png, output) { function savePng(png, output) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
......
...@@ -23,6 +23,75 @@ async function walkObject(obj, callback) { ...@@ -23,6 +23,75 @@ async function walkObject(obj, callback) {
} }
} }
} }
const DesignHeight = 1624;
const SafetyHeight = 1206;
const HalfHeight = (DesignHeight - SafetyHeight) / 2;
// 不裁剪 ,顶部裁剪,上下裁剪,底部裁剪
const designCutModes = { 'none': 'none', 'top': 'top', 'center': 'center', 'bottom': 'bottom' };
/**
* 获取页面的样式
*/
function getPageStyle(mode, xx, yy) {
let result = {};
switch (mode) {
case designCutModes.top: //顶部裁剪就是底对齐
result = {
bottom: 0,
left: 0,
position: "absolute"
}
break;
case designCutModes.center:
result = {
transform: "translate(-50%, -50%)",
top: "50%",
left: "50%",
position: "fixed"
}
break;
case designCutModes.bottom:
result = {
top: 0,
left: 0,
position: "fixed"
}
break;
default:
result = {
top: xx,
left: yy,
position: "absolute"
}
break;
}
return result;
}
function getXYBySign(value, sign, xx, yy, mode) {
switch (mode) {
case designCutModes.none:
break;
case designCutModes.top:
break;
case designCutModes.center:
break;
case designCutModes.bottom:
break;
default:
break;
}
}
function getValueBySign() {
}
function trimCustom(str) { function trimCustom(str) {
var result; var result;
result = str.replace(/(^\s+)|(\s+$)/g, ""); result = str.replace(/(^\s+)|(\s+$)/g, "");
...@@ -32,5 +101,7 @@ function trimCustom(str) { ...@@ -32,5 +101,7 @@ function trimCustom(str) {
module.exports = { module.exports = {
walkNode, walkNode,
walkObject, walkObject,
trimCustom trimCustom,
getXYBySign,
getPageStyle
} }
\ No newline at end of file
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