Commit 9552a48b authored by rockyl's avatar rockyl

修改上下裁剪机制

parent 5cc49b8f
...@@ -88,6 +88,10 @@ async function execute(psdFile, options) { ...@@ -88,6 +88,10 @@ async function execute(psdFile, options) {
const tree = await getTree(psdFile); const tree = await getTree(psdFile);
const {mode = 'none', singleView = true} = options; const {mode = 'none', singleView = true} = options;
let offset = {x: 0, y: 0}; let offset = {x: 0, y: 0};
let cutSize = {x: 0, y: 0};
if (mode !== 'none') {
cutSize.y = offsetAll;
}
switch (mode) { switch (mode) {
case 'top': case 'top':
offset.y = offsetAll; offset.y = offsetAll;
...@@ -97,6 +101,8 @@ async function execute(psdFile, options) { ...@@ -97,6 +101,8 @@ async function execute(psdFile, options) {
break; break;
} }
const isCenter = mode === 'center';
let viewRoot = { let viewRoot = {
name: path.basename(psdFile.name, '.psd'), name: path.basename(psdFile.name, '.psd'),
type: 'node', type: 'node',
...@@ -106,6 +112,10 @@ async function execute(psdFile, options) { ...@@ -106,6 +112,10 @@ async function execute(psdFile, options) {
const assets = []; const assets = [];
const imageHashMap = {}; 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) { await walkNode(tree, async function (node, parent) {
let {name} = node; let {name} = node;
const {x, y, width, height, alpha, visible, origin: {layer, layer: {typeTool, solidColor}}} = node; const {x, y, width, height, alpha, visible, origin: {layer, layer: {typeTool, solidColor}}} = node;
...@@ -113,17 +123,21 @@ async function execute(psdFile, options) { ...@@ -113,17 +123,21 @@ async function execute(psdFile, options) {
let properties = { let properties = {
width, height, alpha, visible, width, height, alpha, visible,
}; };
const isSecondLayer = !parent.origin.parent; 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) {
if (name.includes('|')) {
try { try {
let arr = name.split('|'); let arr = name.split('|');
name = arr[0]; name = arr[0];
let paramsStr = arr[1]; let paramsStr = arr[1];
let params = paramsStr.split(';'); let relativePos;
let relativePos = params[0]; if(paramsStr){
let params = paramsStr.split(';');
relativePos = params[0];
}else if(shouldVerticalCenter){
relativePos = 'v';
}
if (relativePos) { if (relativePos) {
let items = relativePos.split(','); let items = relativePos.split(',');
for (let item of items) { for (let item of items) {
...@@ -147,16 +161,16 @@ async function execute(psdFile, options) { ...@@ -147,16 +161,16 @@ async function execute(psdFile, options) {
value = y - offset.y; value = y - offset.y;
break; break;
case 'r': case 'r':
value = parentWidth - (x - offset.x) - width; value = stageWidth - (x - offset.x) - width;
break; break;
case 'b': case 'b':
value = parentHeight - (y - offset.y) - height; value = stageHeight - (y - offset.y) - height;
break; break;
case 'h': case 'h':
value = x + width / 2 - parentWidth / 2; value = x + width / 2 - stageWidthOrigin / 2;
break; break;
case 'v': case 'v':
value = y + height / 2 - parentHeight / 2; value = y + height / 2 - stageHeightOrigin / 2;
break; break;
} }
} }
...@@ -166,11 +180,11 @@ async function execute(psdFile, options) { ...@@ -166,11 +180,11 @@ async function execute(psdFile, options) {
switch (fieldChar) { switch (fieldChar) {
case 'l': case 'l':
case 'r': case 'r':
value /= parentWidth; value /= stageWidth;
break; break;
case 't': case 't':
case 'b': case 'b':
value /= parentHeight; value /= stageHeight;
break; break;
} }
value = Math.floor(value * 100); value = Math.floor(value * 100);
......
This diff is collapsed.
...@@ -94,6 +94,10 @@ async function execute(psdFile, options) { ...@@ -94,6 +94,10 @@ async function execute(psdFile, options) {
const tree = await getTree(psdFile); const tree = await getTree(psdFile);
const {mode = 'none', singleView = true} = options; const {mode = 'none', singleView = true} = options;
let offset = {x: 0, y: 0}; let offset = {x: 0, y: 0};
let cutSize = {x: 0, y: 0};
if (mode !== 'none') {
cutSize.y = offsetAll;
}
switch (mode) { switch (mode) {
case 'top': case 'top':
offset.y = offsetAll; offset.y = offsetAll;
...@@ -103,6 +107,8 @@ async function execute(psdFile, options) { ...@@ -103,6 +107,8 @@ async function execute(psdFile, options) {
break; break;
} }
const isCenter = mode === 'center';
let viewRoot = { let viewRoot = {
name: path.basename(psdFile.name, '.psd'), name: path.basename(psdFile.name, '.psd'),
type: 'node', type: 'node',
...@@ -112,6 +118,10 @@ async function execute(psdFile, options) { ...@@ -112,6 +118,10 @@ async function execute(psdFile, options) {
const assets = []; const assets = [];
const imageHashMap = {}; 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) { await walkNode(tree, async function (node, parent) {
let {name} = node; let {name} = node;
const {x, y, width, height, alpha, visible, origin: {layer, layer: {typeTool, solidColor}}} = node; const {x, y, width, height, alpha, visible, origin: {layer, layer: {typeTool, solidColor}}} = node;
...@@ -119,17 +129,21 @@ async function execute(psdFile, options) { ...@@ -119,17 +129,21 @@ async function execute(psdFile, options) {
let properties = { let properties = {
width, height, alpha, visible, width, height, alpha, visible,
}; };
const isSecondLayer = !parent.origin.parent; 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) {
if (name.includes('|')) {
try { try {
let arr = name.split('|'); let arr = name.split('|');
name = arr[0]; name = arr[0];
let paramsStr = arr[1]; let paramsStr = arr[1];
let params = paramsStr.split(';'); let relativePos;
let relativePos = params[0]; if(paramsStr){
let params = paramsStr.split(';');
relativePos = params[0];
}else if(shouldVerticalCenter){
relativePos = 'v';
}
if (relativePos) { if (relativePos) {
let items = relativePos.split(','); let items = relativePos.split(',');
for (let item of items) { for (let item of items) {
...@@ -153,16 +167,16 @@ async function execute(psdFile, options) { ...@@ -153,16 +167,16 @@ async function execute(psdFile, options) {
value = y - offset.y; value = y - offset.y;
break; break;
case 'r': case 'r':
value = parentWidth - (x - offset.x) - width; value = stageWidth - (x - offset.x) - width;
break; break;
case 'b': case 'b':
value = parentHeight - (y - offset.y) - height; value = stageHeight - (y - offset.y) - height;
break; break;
case 'h': case 'h':
value = x + width / 2 - parentWidth / 2; value = x + width / 2 - stageWidthOrigin / 2;
break; break;
case 'v': case 'v':
value = y + height / 2 - parentHeight / 2; value = y + height / 2 - stageHeightOrigin / 2;
break; break;
} }
} }
...@@ -172,11 +186,11 @@ async function execute(psdFile, options) { ...@@ -172,11 +186,11 @@ async function execute(psdFile, options) {
switch (fieldChar) { switch (fieldChar) {
case 'l': case 'l':
case 'r': case 'r':
value /= parentWidth; value /= stageWidth;
break; break;
case 't': case 't':
case 'b': case 'b':
value /= parentHeight; value /= stageHeight;
break; break;
} }
value = Math.floor(value * 100); value = Math.floor(value * 100);
......
This diff is collapsed.
...@@ -94,6 +94,10 @@ ...@@ -94,6 +94,10 @@
const tree = await getTree(psdFile); const tree = await getTree(psdFile);
const {mode = 'none', singleView = true} = options; const {mode = 'none', singleView = true} = options;
let offset = {x: 0, y: 0}; let offset = {x: 0, y: 0};
let cutSize = {x: 0, y: 0};
if (mode !== 'none') {
cutSize.y = offsetAll;
}
switch (mode) { switch (mode) {
case 'top': case 'top':
offset.y = offsetAll; offset.y = offsetAll;
...@@ -103,6 +107,8 @@ ...@@ -103,6 +107,8 @@
break; break;
} }
const isCenter = mode === 'center';
let viewRoot = { let viewRoot = {
name: path.basename(psdFile.name, '.psd'), name: path.basename(psdFile.name, '.psd'),
type: 'node', type: 'node',
...@@ -112,6 +118,10 @@ ...@@ -112,6 +118,10 @@
const assets = []; const assets = [];
const imageHashMap = {}; 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) { await walkNode(tree, async function (node, parent) {
let {name} = node; let {name} = node;
const {x, y, width, height, alpha, visible, origin: {layer, layer: {typeTool, solidColor}}} = node; const {x, y, width, height, alpha, visible, origin: {layer, layer: {typeTool, solidColor}}} = node;
...@@ -119,17 +129,21 @@ ...@@ -119,17 +129,21 @@
let properties = { let properties = {
width, height, alpha, visible, width, height, alpha, visible,
}; };
const isSecondLayer = !parent.origin.parent; 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) {
if (name.includes('|')) {
try { try {
let arr = name.split('|'); let arr = name.split('|');
name = arr[0]; name = arr[0];
let paramsStr = arr[1]; let paramsStr = arr[1];
let params = paramsStr.split(';'); let relativePos;
let relativePos = params[0]; if(paramsStr){
let params = paramsStr.split(';');
relativePos = params[0];
}else if(shouldVerticalCenter){
relativePos = 'v';
}
if (relativePos) { if (relativePos) {
let items = relativePos.split(','); let items = relativePos.split(',');
for (let item of items) { for (let item of items) {
...@@ -153,16 +167,16 @@ ...@@ -153,16 +167,16 @@
value = y - offset.y; value = y - offset.y;
break; break;
case 'r': case 'r':
value = parentWidth - (x - offset.x) - width; value = stageWidth - (x - offset.x) - width;
break; break;
case 'b': case 'b':
value = parentHeight - (y - offset.y) - height; value = stageHeight - (y - offset.y) - height;
break; break;
case 'h': case 'h':
value = x + width / 2 - parentWidth / 2; value = x + width / 2 - stageWidthOrigin / 2;
break; break;
case 'v': case 'v':
value = y + height / 2 - parentHeight / 2; value = y + height / 2 - stageHeightOrigin / 2;
break; break;
} }
} }
...@@ -172,11 +186,11 @@ ...@@ -172,11 +186,11 @@
switch (fieldChar) { switch (fieldChar) {
case 'l': case 'l':
case 'r': case 'r':
value /= parentWidth; value /= stageWidth;
break; break;
case 't': case 't':
case 'b': case 'b':
value /= parentHeight; value /= stageHeight;
break; break;
} }
value = Math.floor(value * 100); value = Math.floor(value * 100);
......
This diff is collapsed.
...@@ -48,6 +48,8 @@ export async function execute(psdFile, options) { ...@@ -48,6 +48,8 @@ export async function execute(psdFile, options) {
break; break;
} }
const isCenter = mode === 'center';
let viewRoot = { let viewRoot = {
name: path.basename(psdFile.name, '.psd'), name: path.basename(psdFile.name, '.psd'),
type: 'node', type: 'node',
...@@ -57,9 +59,9 @@ export async function execute(psdFile, options) { ...@@ -57,9 +59,9 @@ export async function execute(psdFile, options) {
const assets = []; const assets = [];
const imageHashMap = {}; const imageHashMap = {};
let {width: stageWidth, height: stageHeight} = tree; let {width: stageWidthOrigin, height: stageHeightOrigin} = tree;
stageWidth -= cutSize.x || 0; const stageWidth = stageWidthOrigin - cutSize.x || 0;
stageHeight -= cutSize.y || 0; const stageHeight = stageHeightOrigin - cutSize.y || 0;
await walkNode(tree, async function (node, parent) { await walkNode(tree, async function (node, parent) {
let {name} = node; let {name} = node;
...@@ -68,17 +70,23 @@ export async function execute(psdFile, options) { ...@@ -68,17 +70,23 @@ export async function execute(psdFile, options) {
let properties = { let properties = {
width, height, alpha, visible, width, height, alpha, visible,
}; };
const isSecondLayer = !parent.origin.parent; const isSecondLayer = singleView && !parent.origin.parent || !singleView && parent.origin.parent && !parent.origin.parent.parent;
const shouldVerticalCenter = isSecondLayer && isCenter;
const {width: parentWidth, height: parentHeight} = parent; const {width: parentWidth, height: parentHeight} = parent;
if (name.includes('|')) { if (name.includes('|') || shouldVerticalCenter) {
try { try {
let arr = name.split('|'); let arr = name.split('|');
name = arr[0]; name = arr[0];
let paramsStr = arr[1]; let paramsStr = arr[1];
let params = paramsStr.split(';'); let relativePos;
let relativePos = params[0]; if(paramsStr){
let params = paramsStr.split(';');
relativePos = params[0];
}else if(shouldVerticalCenter){
relativePos = 'v';
}
if (relativePos) { if (relativePos) {
let items = relativePos.split(','); let items = relativePos.split(',');
for (let item of items) { for (let item of items) {
...@@ -102,16 +110,16 @@ export async function execute(psdFile, options) { ...@@ -102,16 +110,16 @@ export async function execute(psdFile, options) {
value = y - offset.y; value = y - offset.y;
break; break;
case 'r': case 'r':
value = parentWidth - (x - offset.x) - width; value = stageWidth - (x - offset.x) - width;
break; break;
case 'b': case 'b':
value = parentHeight - (y - offset.y) - height; value = stageHeight - (y - offset.y) - height;
break; break;
case 'h': case 'h':
value = x + width / 2 - parentWidth / 2; value = x + width / 2 - stageWidthOrigin / 2;
break; break;
case 'v': case 'v':
value = y + height / 2 - parentHeight / 2; value = y + height / 2 - stageHeightOrigin / 2;
break; break;
} }
} }
...@@ -121,11 +129,11 @@ export async function execute(psdFile, options) { ...@@ -121,11 +129,11 @@ export async function execute(psdFile, options) {
switch (fieldChar) { switch (fieldChar) {
case 'l': case 'l':
case 'r': case 'r':
value /= parentWidth; value /= stageWidth;
break; break;
case 't': case 't':
case 'b': case 'b':
value /= parentHeight; value /= stageHeight;
break; break;
} }
value = Math.floor(value * 100); value = Math.floor(value * 100);
......
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