Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
P
psd-parse-web
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
劳工
psd-parse-web
Commits
05b0ebd5
Commit
05b0ebd5
authored
Apr 22, 2020
by
rockyl
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
增加相对位置的解析
parent
3b226df5
Changes
8
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
152 additions
and
55 deletions
+152
-55
index.es.js
dist/index.es.js
+37
-13
index.es.js.map
dist/index.es.js.map
+1
-1
index.js
dist/index.js
+37
-13
index.js.map
dist/index.js.map
+1
-1
index.umd.js
dist/index.umd.js
+37
-13
index.umd.js.map
dist/index.umd.js.map
+1
-1
package.json
package.json
+1
-0
zeroing.js
src/zeroing.js
+37
-13
No files found.
dist/index.es.js
View file @
05b0ebd5
...
@@ -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
);
}
}
...
...
dist/index.es.js.map
View file @
05b0ebd5
{
"version"
:
3
,
"file"
:
"index.es.js"
,
"sources"
:
[
"../src/psd-tree.js"
,
"../src/utils.js"
,
"../src/zeroing.js"
],
"sourcesContent"
:
[
"/**
\n
* Created by rockyl on 2019-08-09.
\n
*/
\n\n
const PSD = window['require']('psd');
\n\n
export async function getTree(file) {\n
\t
let psd = await PSD.fromDroppedFile(file);
\n\n\t
const root = {};
\n\t
walk(psd.tree(), root);
\n\n\t
return root;
\n
}
\n\n
function walk(psNode, dataNode) {\n
\t
const {left: pLeft = 0, top: pTop = 0,} = psNode.parent || {};
\n\t
const {left, top, width, height, name, layer: {opacity, visible}} = psNode;
\n\t
const x = left - pLeft;
\n\t
const y = top - pTop;
\n\n\t
Object.assign(dataNode, {x, y, width, height, alpha: opacity / 255, visible, name, origin: psNode, label: `${name} > [${x}, ${y}, ${width}, ${height}]`});
\n\t
if (psNode.children() && psNode.children().length > 0){\n
\t\t
dataNode.children = [];
\n\t
}
\n\n\t
let children = psNode.children();
\n\t
for (let i = children.length - 1; i >= 0; i--) {\n
\t\t
const childPsNode = children[i];
\n\n\t\t
const childDataNode = {};
\n\t\t
dataNode.children.push(childDataNode);
\n\t\t
walk(childPsNode, childDataNode)
\n\t
}
\n
}
\n
"
,
"/**
\n
* Created by rockyl on 2019-08-10.
\n
*/
\n\n
export async function walkNode(node, callback, includeSelf = false) {\n
\t
if (includeSelf) {\n
\t\t
await callback(node, null);
\n\t
}
\n\t
if (node.children && node.children.length > 0) {\n
\t\t
for (let childNode of node.children) {\n
\t\t\t
await callback(childNode, node);
\n\t\t\t
const result = await walkNode(childNode, callback);
\n\t\t\t
if (result === true)
{
\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t}\n
}
\n\n
export async function walkObject(obj, callback) {\n
\t
if(typeof obj ===
\"
object
\"
){\n
\t\t
for (let key of Object.keys(obj)) {\n
\t\t\t
const value = obj[key];
\n\t\t\t
await callback(key, value, obj);
\n\t\t\t
const result = await walkObject(value, callback);
\n\t\t\t
if (result === true)
{
\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t}\n
}
\n
"
,
"/**
\n
* Created by rockyl on 2019-09-26.
\n
*
\n
* 导出zeroing的视图
\n
*/
\n\n
import
{
getTree
}
from
\"
./psd-tree
\"
;
\n
import
{
walkNode
}
from
\"
./utils
\"
;
\n
import path from 'path'
\n
import Color from 'color'
\n
import generateUUID from 'uuid/v4'
\n
import hash from 'object-hash';
\n
import zlib from 'zlib';
\n\n
export async function execute(psdFile, options) {\n
\t
const tree = await getTree(psdFile);
\n\n\t
let viewRoot = {\n
\t\t
name: path.basename(psdFile.name, '.psd'),
\n\t\t
type: 'node',
\n\t\t
uuid: generateUUID(),
\n\t
};
\n\n\t
const assets = [];
\n\t
const imageHashMap = {};
\n\n\t
await walkNode(tree, async function (node, parent) {\n
\t\t
const {name, x, y, width, height, alpha, visible, origin: {layer, layer: {typeTool, solidColor}}} = node;
\n\t\t
console.log('walk node:', name);
\n\t\t
let properties = {\n
\t\t\t
width, height, alpha, visible,
\n\t\t
};
\n\t\t
let viewNode = {\n
\t\t\t
name,
\n\t\t\t
properties,
\n\t\t\t
uuid: generateUUID(),
\n\t\t
};
\n\t\t
let dealLater = true;
\n\t\t
if (x !== 0) {\n
\t\t\t
properties.x = x;
\n\t\t
}
\n\t\t
if (y !== 0) {\n
\t\t\t
properties.y = y;
\n\t\t
}
\n\n\t\t
if (typeTool) {\n
\t\t\t
let fontInfo = typeTool();
\n\t\t\t
//const fonts = fontInfo.fonts();
\n\t\t\t
//const styles = fontInfo.styles();
\n\t\t\t
//const
{
RunLengthArray
}
= fontInfo.engineData.EngineDict.StyleRun;
\n\n\t\t\t
properties.text = fontInfo.textValue;
\n\t\t\t
const sizes = fontInfo.sizes();
\n\t\t\t
const colors = fontInfo.colors();
\n\t\t\t
properties.size = sizes ? sizes[0] || 20 : 20;
\n\t\t\t
let [r, g, b, a] = colors[0];
\n\t\t\t
properties.fillColor = `rgba(${r}, ${g}, ${b}, ${a / 255})`;
\n\t\t\t
/*properties.textflow = {\n
\t\t\t\t
fonts, styles, RunLengthArray,
\n\t\t\t
};*/
\n\t\t\t
viewNode.type = 'label';
\n\t\t\t
dealLater = false;
\n\t\t
} else if (solidColor) {\n
\t\t\t
const {r, g, b} = solidColor();
\n\t\t\t
let color = Color({r, g, b});
\n\n\t\t\t
let paths = layer.vectorMask().paths;
\n\t\t\t
if (paths[2].numPoints === 4) {\n
\t\t\t\t
let isRect = true;
\n\t\t\t\t
for (let i = 3; i < paths.length; i++) {\n
\t\t\t\t\t
if (paths[i].recordType !== 2) {\n
\t\t\t\t\t\t
isRect = false;
\n\t\t\t\t\t\t
break;
\n\t\t\t\t\t
}
\n\t\t\t\t
}
\n\t\t\t\t
if (isRect) {\n
\t\t\t\t\t
viewNode.type = 'rect';
\n\t\t\t\t\t
properties.fillColor = '#' + color.rgbNumber().toString(16);
\n\t\t\t\t\t
dealLater = false;
\n\t\t\t\t
}
\n\t\t\t
}
\n\t\t
}
\n\n\t\t
if (dealLater) {\n
\t\t\t
if (node.hasOwnProperty('children')) {\n
\t\t\t\t
viewNode.type = 'node';
\n\t\t\t
} else {\n
\t\t\t\t
viewNode.type = 'image';
\n\n\t\t\t\t
let uuid = generateUUID();
\n\t\t\t\t
const ext = '.png';
\n\n\t\t\t\t
let dataUrl;
\n\t\t\t\t
try {\n
\t\t\t\t\t
let img = node.origin.toPng();
\n\t\t\t\t\t
dataUrl = img.src;
\n\t\t\t\t
}catch (e)
{
\n\n\t\t\t\t
}
\n\n\t\t\t\t
if (dataUrl) {\n
\t\t\t\t\t
let base64Data = dataUrl.replace(/^data:image
\\
/
\\
w+;base64,/,
\"\"
);
\n\t\t\t\t\t
let buffer = new Buffer(base64Data, 'base64');
\n\t\t\t\t\t
const fileNameHash = hash(buffer);
\n\t\t\t\t\t
if (imageHashMap.hasOwnProperty(fileNameHash)) {\n
\t\t\t\t\t\t
uuid = imageHashMap[fileNameHash];
\n\t\t\t\t\t
} else {\n
\t\t\t\t\t\t
imageHashMap[fileNameHash] = uuid;
\n\t\t\t\t\t\t
assets.push({\n
\t\t\t\t\t\t\t
name,
\n\t\t\t\t\t\t\t
ext,
\n\t\t\t\t\t\t\t
uuid,
\n\t\t\t\t\t\t\t
base64Data,
\n\t\t\t\t\t\t\t
hash: fileNameHash,
\n\t\t\t\t\t\t
});
\n\t\t\t\t\t
}
\n\n\t\t\t\t\t
properties.source = 'asset://' + uuid;
\n\t\t\t\t
}
\n\t\t\t
}
\n\t\t
}
\n\n\t\t
let viewParent = parent.view || viewRoot;
\n\t\t
if (!viewParent.hasOwnProperty('children')) {\n
\t\t\t
viewParent.children = [];
\n\t\t
}
\n\t\t
viewParent.children.push(viewNode);
\n\n\t\t
node.view = viewNode;
\n\t
});
\n\n\t
let data = {\n
\t\t
view: viewRoot,
\n\t\t
assets,
\n\t
};
\n\n\t
let dataString = JSON.stringify(data);
\n\t
console.log(dataString.length);
\n\n\t
let buf = new Buffer(dataString);
\n\t
return await new Promise((resolve, reject)=>
{
\n\t\tzlib.gzip(buf,function(err,res){\n\t\t\tif(err){\n\t\t\t\treject(err);\n\t\t\t}else{\n\t\t\t\tconsole.log(res.length);\n\t\t\t\tresolve(res);\n\t\t\t}\n\t\t})\n\t})\n}\n"],"names":[],"mappings":";;;;;;AAAA;;;;AAIA,MAAM,GAAG,GAAG,MAAM,CAAC,SAAS,CAAC,CAAC,KAAK,CAAC,CAAC;;AAErC,AAAO,eAAe,OAAO,CAAC,IAAI,EAAE;CACnC,IAAI,GAAG,GAAG,MAAM,GAAG,CAAC,eAAe,CAAC,IAAI,CAAC,CAAC;;CAE1C,MAAM,IAAI,GAAG,EAAE,CAAC;CAChB,IAAI,CAAC,GAAG,CAAC,IAAI,EAAE,EAAE,IAAI,CAAC,CAAC;;CAEvB,OAAO,IAAI,CAAC;CACZ;;AAED,SAAS,IAAI,CAAC,MAAM,EAAE,QAAQ,EAAE;CAC/B,MAAM,CAAC,IAAI,EAAE,KAAK,GAAG,CAAC,EAAE,GAAG,EAAE,IAAI,GAAG,CAAC,EAAE,GAAG,MAAM,CAAC,MAAM,IAAI,EAAE,CAAC;CAC9D,MAAM,CAAC,IAAI,EAAE,GAAG,EAAE,KAAK,EAAE,MAAM,EAAE,IAAI,EAAE,KAAK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
:
[
"/**
\n
* Created by rockyl on 2019-08-09.
\n
*/
\n\n
const PSD = window['require']('psd');
\n\n
export async function getTree(file) {\n
\t
let psd = await PSD.fromDroppedFile(file);
\n\n\t
const root = {};
\n\t
walk(psd.tree(), root);
\n\n\t
return root;
\n
}
\n\n
function walk(psNode, dataNode) {\n
\t
const {left: pLeft = 0, top: pTop = 0,} = psNode.parent || {};
\n\t
const {left, top, width, height, name, layer: {opacity, visible}} = psNode;
\n\t
const x = left - pLeft;
\n\t
const y = top - pTop;
\n\n\t
Object.assign(dataNode, {x, y, width, height, alpha: opacity / 255, visible, name, origin: psNode, label: `${name} > [${x}, ${y}, ${width}, ${height}]`});
\n\t
if (psNode.children() && psNode.children().length > 0){\n
\t\t
dataNode.children = [];
\n\t
}
\n\n\t
let children = psNode.children();
\n\t
for (let i = children.length - 1; i >= 0; i--) {\n
\t\t
const childPsNode = children[i];
\n\n\t\t
const childDataNode = {};
\n\t\t
dataNode.children.push(childDataNode);
\n\t\t
walk(childPsNode, childDataNode)
\n\t
}
\n
}
\n
"
,
"/**
\n
* Created by rockyl on 2019-08-10.
\n
*/
\n\n
export async function walkNode(node, callback, includeSelf = false) {\n
\t
if (includeSelf) {\n
\t\t
await callback(node, null);
\n\t
}
\n\t
if (node.children && node.children.length > 0) {\n
\t\t
for (let childNode of node.children) {\n
\t\t\t
await callback(childNode, node);
\n\t\t\t
const result = await walkNode(childNode, callback);
\n\t\t\t
if (result === true)
{
\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t}\n
}
\n\n
export async function walkObject(obj, callback) {\n
\t
if(typeof obj ===
\"
object
\"
){\n
\t\t
for (let key of Object.keys(obj)) {\n
\t\t\t
const value = obj[key];
\n\t\t\t
await callback(key, value, obj);
\n\t\t\t
const result = await walkObject(value, callback);
\n\t\t\t
if (result === true)
{
\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t}\n
}
\n
"
,
"/**
\n
* Created by rockyl on 2019-09-26.
\n
*
\n
* 导出zeroing的视图
\n
*/
\n\n
import
{
getTree
}
from
\"
./psd-tree
\"
;
\n
import
{
walkNode
}
from
\"
./utils
\"
;
\n
import path from 'path'
\n
import Color from 'color'
\n
import generateUUID from 'uuid/v4'
\n
import hash from 'object-hash';
\n
import zlib from 'zlib';
\n\n
const relativePosPrefixMap = {\n
\t
l: 'left',
\n\t
t: 'top',
\n\t
r: 'right',
\n\t
b: 'bottom',
\n\t
h: 'horizonCenter',
\n\t
v: 'verticalCenter',
\n\t
wp: 'percentWidth',
\n\t
hp: 'percentHeight',
\n
};
\n\n
export async function execute(psdFile, options) {\n
\t
const tree = await getTree(psdFile);
\n\n\t
let viewRoot = {\n
\t\t
name: path.basename(psdFile.name, '.psd'),
\n\t\t
type: 'node',
\n\t\t
uuid: generateUUID(),
\n\t
};
\n\n\t
const assets = [];
\n\t
const imageHashMap = {};
\n\n\t
await walkNode(tree, async function (node, parent) {\n
\t\t
let
{
name
}
= node;
\n\t\t
const {x, y, width, height, alpha, visible, origin: {layer, layer: {typeTool, solidColor}}} = node;
\n\t\t
console.log('walk node:', name);
\n\t\t
let properties = {\n
\t\t\t
width, height, alpha, visible,
\n\t\t
};
\n\n\t\t
if (name.includes('|')) {\n
\t\t\t
let arr = name.split('|');
\n\t\t\t
name = arr[0];
\n\t\t\t
let paramsStr = arr[0];
\n\t\t\t
let params = paramsStr.split(';');
\n\t\t\t
let relativePos = params[0];
\n\t\t\t
if (relativePos) {\n
\t\t\t\t
let items = relativePos.split(',');
\n\t\t\t\t
for (let item of items) {\n
\t\t\t\t\t
let prefix = item[0];
\n\t\t\t\t\t
let field = relativePosPrefixMap[prefix];
\n\t\t\t\t\t
if (field) {\n
\t\t\t\t\t\t
properties[field] = parseFloat(item.substr(1));
\n\t\t\t\t\t
}
\n\t\t\t\t
}
\n\t\t\t
}
\n\t\t
}
\n\n\t\t
let viewNode = {\n
\t\t\t
name,
\n\t\t\t
properties,
\n\t\t\t
uuid: generateUUID(),
\n\t\t
};
\n\t\t
let dealLater = true;
\n\t\t
if (x !== 0) {\n
\t\t\t
properties.x = x;
\n\t\t
}
\n\t\t
if (y !== 0) {\n
\t\t\t
properties.y = y;
\n\t\t
}
\n\n\t\t
if (typeTool) {\n
\t\t\t
let fontInfo = typeTool();
\n\t\t\t
properties.text = fontInfo.textValue;
\n\t\t\t
const sizes = fontInfo.sizes();
\n\t\t\t
const colors = fontInfo.colors();
\n\t\t\t
properties.size = sizes ? sizes[0] || 20 : 20;
\n\t\t\t
let [r, g, b, a] = colors[0];
\n\t\t\t
properties.fillColor = `rgba(${r}, ${g}, ${b}, ${a / 255})`;
\n\t\t\t
viewNode.type = 'label';
\n\t\t\t
dealLater = false;
\n\t\t
} else if (solidColor) {\n
\t\t\t
const {r, g, b} = solidColor();
\n\t\t\t
let color = Color({r, g, b});
\n\n\t\t\t
let paths = layer.vectorMask().paths;
\n\t\t\t
if (paths[2].numPoints === 4) {\n
\t\t\t\t
let isRect = true;
\n\t\t\t\t
for (let i = 3; i < paths.length; i++) {\n
\t\t\t\t\t
if (paths[i].recordType !== 2) {\n
\t\t\t\t\t\t
isRect = false;
\n\t\t\t\t\t\t
break;
\n\t\t\t\t\t
}
\n\t\t\t\t
}
\n\t\t\t\t
if (isRect) {\n
\t\t\t\t\t
viewNode.type = 'rect';
\n\t\t\t\t\t
properties.fillColor = '#' + color.rgbNumber().toString(16);
\n\t\t\t\t\t
dealLater = false;
\n\t\t\t\t
}
\n\t\t\t
}
\n\t\t
}
\n\n\t\t
if (dealLater) {\n
\t\t\t
if (node.hasOwnProperty('children')) {\n
\t\t\t\t
viewNode.type = 'node';
\n\t\t\t
} else {\n
\t\t\t\t
viewNode.type = 'image';
\n\n\t\t\t\t
let uuid = generateUUID();
\n\t\t\t\t
const ext = '.png';
\n\n\t\t\t\t
let dataUrl;
\n\t\t\t\t
try {\n
\t\t\t\t\t
let img = node.origin.toPng();
\n\t\t\t\t\t
dataUrl = img.src;
\n\t\t\t\t
} catch (e)
{
\n\n\t\t\t\t
}
\n\n\t\t\t\t
if (dataUrl) {\n
\t\t\t\t\t
let base64Data = dataUrl.replace(/^data:image
\\
/
\\
w+;base64,/,
\"\"
);
\n\t\t\t\t\t
let buffer = new Buffer(base64Data, 'base64');
\n\t\t\t\t\t
const fileNameHash = hash(buffer);
\n\t\t\t\t\t
if (imageHashMap.hasOwnProperty(fileNameHash)) {\n
\t\t\t\t\t\t
uuid = imageHashMap[fileNameHash];
\n\t\t\t\t\t
} else {\n
\t\t\t\t\t\t
imageHashMap[fileNameHash] = uuid;
\n\t\t\t\t\t\t
assets.push({\n
\t\t\t\t\t\t\t
name,
\n\t\t\t\t\t\t\t
ext,
\n\t\t\t\t\t\t\t
uuid,
\n\t\t\t\t\t\t\t
base64Data,
\n\t\t\t\t\t\t\t
hash: fileNameHash,
\n\t\t\t\t\t\t
});
\n\t\t\t\t\t
}
\n\n\t\t\t\t\t
properties.source = 'asset://' + uuid;
\n\t\t\t\t
}
\n\t\t\t
}
\n\t\t
}
\n\n\t\t
let viewParent = parent.view || viewRoot;
\n\t\t
if (!viewParent.hasOwnProperty('children')) {\n
\t\t\t
viewParent.children = [];
\n\t\t
}
\n\t\t
viewParent.children.push(viewNode);
\n\n\t\t
node.view = viewNode;
\n\t
});
\n\n\t
let data = {\n
\t\t
view: viewRoot,
\n\t\t
assets,
\n\t
};
\n\n\t
let dataString = JSON.stringify(data);
\n\t
console.log(dataString.length);
\n\n\t
let buf = new Buffer(dataString);
\n\t
return await new Promise((resolve, reject) => {\n
\t\t
zlib.gzip(buf, function (err, res) {\n
\t\t\t
if (err)
{
\n\t\t\t\treject(err);\n\t\t\t
}
else
{
\n\t\t\t\tconsole.log(res.length);\n\t\t\t\tresolve(res);\n\t\t\t}\n\t\t})\n\t})\n}\n"],"names":[],"mappings":";;;;;;AAAA;;;;AAIA,MAAM,GAAG,GAAG,MAAM,CAAC,SAAS,CAAC,CAAC,KAAK,CAAC,CAAC;;AAErC,AAAO,eAAe,OAAO,CAAC,IAAI,EAAE;CACnC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,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;IACxB,OAAO,CAAC,GAAG,CAAC,CAAC;IACb;GACD,EAAC;EACF,CAAC;CACF;;;;"
}
\ No newline at end of file
\ No newline at end of file
dist/index.js
View file @
05b0ebd5
...
@@ -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
);
}
}
...
...
dist/index.js.map
View file @
05b0ebd5
{
"version"
:
3
,
"file"
:
"index.js"
,
"sources"
:
[
"../src/psd-tree.js"
,
"../src/utils.js"
,
"../src/zeroing.js"
],
"sourcesContent"
:
[
"/**
\n
* Created by rockyl on 2019-08-09.
\n
*/
\n\n
const PSD = window['require']('psd');
\n\n
export async function getTree(file) {\n
\t
let psd = await PSD.fromDroppedFile(file);
\n\n\t
const root = {};
\n\t
walk(psd.tree(), root);
\n\n\t
return root;
\n
}
\n\n
function walk(psNode, dataNode) {\n
\t
const {left: pLeft = 0, top: pTop = 0,} = psNode.parent || {};
\n\t
const {left, top, width, height, name, layer: {opacity, visible}} = psNode;
\n\t
const x = left - pLeft;
\n\t
const y = top - pTop;
\n\n\t
Object.assign(dataNode, {x, y, width, height, alpha: opacity / 255, visible, name, origin: psNode, label: `${name} > [${x}, ${y}, ${width}, ${height}]`});
\n\t
if (psNode.children() && psNode.children().length > 0){\n
\t\t
dataNode.children = [];
\n\t
}
\n\n\t
let children = psNode.children();
\n\t
for (let i = children.length - 1; i >= 0; i--) {\n
\t\t
const childPsNode = children[i];
\n\n\t\t
const childDataNode = {};
\n\t\t
dataNode.children.push(childDataNode);
\n\t\t
walk(childPsNode, childDataNode)
\n\t
}
\n
}
\n
"
,
"/**
\n
* Created by rockyl on 2019-08-10.
\n
*/
\n\n
export async function walkNode(node, callback, includeSelf = false) {\n
\t
if (includeSelf) {\n
\t\t
await callback(node, null);
\n\t
}
\n\t
if (node.children && node.children.length > 0) {\n
\t\t
for (let childNode of node.children) {\n
\t\t\t
await callback(childNode, node);
\n\t\t\t
const result = await walkNode(childNode, callback);
\n\t\t\t
if (result === true)
{
\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t}\n
}
\n\n
export async function walkObject(obj, callback) {\n
\t
if(typeof obj ===
\"
object
\"
){\n
\t\t
for (let key of Object.keys(obj)) {\n
\t\t\t
const value = obj[key];
\n\t\t\t
await callback(key, value, obj);
\n\t\t\t
const result = await walkObject(value, callback);
\n\t\t\t
if (result === true)
{
\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t}\n
}
\n
"
,
"/**
\n
* Created by rockyl on 2019-09-26.
\n
*
\n
* 导出zeroing的视图
\n
*/
\n\n
import
{
getTree
}
from
\"
./psd-tree
\"
;
\n
import
{
walkNode
}
from
\"
./utils
\"
;
\n
import path from 'path'
\n
import Color from 'color'
\n
import generateUUID from 'uuid/v4'
\n
import hash from 'object-hash';
\n
import zlib from 'zlib';
\n\n
export async function execute(psdFile, options) {\n
\t
const tree = await getTree(psdFile);
\n\n\t
let viewRoot = {\n
\t\t
name: path.basename(psdFile.name, '.psd'),
\n\t\t
type: 'node',
\n\t\t
uuid: generateUUID(),
\n\t
};
\n\n\t
const assets = [];
\n\t
const imageHashMap = {};
\n\n\t
await walkNode(tree, async function (node, parent) {\n
\t\t
const {name, x, y, width, height, alpha, visible, origin: {layer, layer: {typeTool, solidColor}}} = node;
\n\t\t
console.log('walk node:', name);
\n\t\t
let properties = {\n
\t\t\t
width, height, alpha, visible,
\n\t\t
};
\n\t\t
let viewNode = {\n
\t\t\t
name,
\n\t\t\t
properties,
\n\t\t\t
uuid: generateUUID(),
\n\t\t
};
\n\t\t
let dealLater = true;
\n\t\t
if (x !== 0) {\n
\t\t\t
properties.x = x;
\n\t\t
}
\n\t\t
if (y !== 0) {\n
\t\t\t
properties.y = y;
\n\t\t
}
\n\n\t\t
if (typeTool) {\n
\t\t\t
let fontInfo = typeTool();
\n\t\t\t
//const fonts = fontInfo.fonts();
\n\t\t\t
//const styles = fontInfo.styles();
\n\t\t\t
//const
{
RunLengthArray
}
= fontInfo.engineData.EngineDict.StyleRun;
\n\n\t\t\t
properties.text = fontInfo.textValue;
\n\t\t\t
const sizes = fontInfo.sizes();
\n\t\t\t
const colors = fontInfo.colors();
\n\t\t\t
properties.size = sizes ? sizes[0] || 20 : 20;
\n\t\t\t
let [r, g, b, a] = colors[0];
\n\t\t\t
properties.fillColor = `rgba(${r}, ${g}, ${b}, ${a / 255})`;
\n\t\t\t
/*properties.textflow = {\n
\t\t\t\t
fonts, styles, RunLengthArray,
\n\t\t\t
};*/
\n\t\t\t
viewNode.type = 'label';
\n\t\t\t
dealLater = false;
\n\t\t
} else if (solidColor) {\n
\t\t\t
const {r, g, b} = solidColor();
\n\t\t\t
let color = Color({r, g, b});
\n\n\t\t\t
let paths = layer.vectorMask().paths;
\n\t\t\t
if (paths[2].numPoints === 4) {\n
\t\t\t\t
let isRect = true;
\n\t\t\t\t
for (let i = 3; i < paths.length; i++) {\n
\t\t\t\t\t
if (paths[i].recordType !== 2) {\n
\t\t\t\t\t\t
isRect = false;
\n\t\t\t\t\t\t
break;
\n\t\t\t\t\t
}
\n\t\t\t\t
}
\n\t\t\t\t
if (isRect) {\n
\t\t\t\t\t
viewNode.type = 'rect';
\n\t\t\t\t\t
properties.fillColor = '#' + color.rgbNumber().toString(16);
\n\t\t\t\t\t
dealLater = false;
\n\t\t\t\t
}
\n\t\t\t
}
\n\t\t
}
\n\n\t\t
if (dealLater) {\n
\t\t\t
if (node.hasOwnProperty('children')) {\n
\t\t\t\t
viewNode.type = 'node';
\n\t\t\t
} else {\n
\t\t\t\t
viewNode.type = 'image';
\n\n\t\t\t\t
let uuid = generateUUID();
\n\t\t\t\t
const ext = '.png';
\n\n\t\t\t\t
let dataUrl;
\n\t\t\t\t
try {\n
\t\t\t\t\t
let img = node.origin.toPng();
\n\t\t\t\t\t
dataUrl = img.src;
\n\t\t\t\t
}catch (e)
{
\n\n\t\t\t\t
}
\n\n\t\t\t\t
if (dataUrl) {\n
\t\t\t\t\t
let base64Data = dataUrl.replace(/^data:image
\\
/
\\
w+;base64,/,
\"\"
);
\n\t\t\t\t\t
let buffer = new Buffer(base64Data, 'base64');
\n\t\t\t\t\t
const fileNameHash = hash(buffer);
\n\t\t\t\t\t
if (imageHashMap.hasOwnProperty(fileNameHash)) {\n
\t\t\t\t\t\t
uuid = imageHashMap[fileNameHash];
\n\t\t\t\t\t
} else {\n
\t\t\t\t\t\t
imageHashMap[fileNameHash] = uuid;
\n\t\t\t\t\t\t
assets.push({\n
\t\t\t\t\t\t\t
name,
\n\t\t\t\t\t\t\t
ext,
\n\t\t\t\t\t\t\t
uuid,
\n\t\t\t\t\t\t\t
base64Data,
\n\t\t\t\t\t\t\t
hash: fileNameHash,
\n\t\t\t\t\t\t
});
\n\t\t\t\t\t
}
\n\n\t\t\t\t\t
properties.source = 'asset://' + uuid;
\n\t\t\t\t
}
\n\t\t\t
}
\n\t\t
}
\n\n\t\t
let viewParent = parent.view || viewRoot;
\n\t\t
if (!viewParent.hasOwnProperty('children')) {\n
\t\t\t
viewParent.children = [];
\n\t\t
}
\n\t\t
viewParent.children.push(viewNode);
\n\n\t\t
node.view = viewNode;
\n\t
});
\n\n\t
let data = {\n
\t\t
view: viewRoot,
\n\t\t
assets,
\n\t
};
\n\n\t
let dataString = JSON.stringify(data);
\n\t
console.log(dataString.length);
\n\n\t
let buf = new Buffer(dataString);
\n\t
return await new Promise((resolve, reject)=>
{
\n\t\tzlib.gzip(buf,function(err,res){\n\t\t\tif(err){\n\t\t\t\treject(err);\n\t\t\t}else{\n\t\t\t\tconsole.log(res.length);\n\t\t\t\tresolve(res);\n\t\t\t}\n\t\t})\n\t})\n}\n"],"names":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U,GAAG,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC;CACtC,OAAO,CAAC,GAAG,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC;;CAE/B,IAAI,GAAG,GAAG,IAAI,MAAM,CAAC,UAAU,CAAC,CAAC;CACjC,OAAO,MAAM,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,MAAM,GAAG;EAC3C,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,SAAS,GAAG,CAAC,GAAG,CAAC;GAC9B,GAAG,GAAG,CAAC;IACN,MAAM,CAAC,GAAG,CAAC,CAAC;IACZ,IAAI;IACJ,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;IACxB,OAAO,CAAC,GAAG,CAAC,CAAC;IACb;GACD,EAAC;EACF,CAAC;CACF;;;;;"
}
{
"version"
:
3
,
"file"
:
"index.js"
,
"sources"
:
[
"../src/psd-tree.js"
,
"../src/utils.js"
,
"../src/zeroing.js"
],
"sourcesContent"
:
[
"/**
\n
* Created by rockyl on 2019-08-09.
\n
*/
\n\n
const PSD = window['require']('psd');
\n\n
export async function getTree(file) {\n
\t
let psd = await PSD.fromDroppedFile(file);
\n\n\t
const root = {};
\n\t
walk(psd.tree(), root);
\n\n\t
return root;
\n
}
\n\n
function walk(psNode, dataNode) {\n
\t
const {left: pLeft = 0, top: pTop = 0,} = psNode.parent || {};
\n\t
const {left, top, width, height, name, layer: {opacity, visible}} = psNode;
\n\t
const x = left - pLeft;
\n\t
const y = top - pTop;
\n\n\t
Object.assign(dataNode, {x, y, width, height, alpha: opacity / 255, visible, name, origin: psNode, label: `${name} > [${x}, ${y}, ${width}, ${height}]`});
\n\t
if (psNode.children() && psNode.children().length > 0){\n
\t\t
dataNode.children = [];
\n\t
}
\n\n\t
let children = psNode.children();
\n\t
for (let i = children.length - 1; i >= 0; i--) {\n
\t\t
const childPsNode = children[i];
\n\n\t\t
const childDataNode = {};
\n\t\t
dataNode.children.push(childDataNode);
\n\t\t
walk(childPsNode, childDataNode)
\n\t
}
\n
}
\n
"
,
"/**
\n
* Created by rockyl on 2019-08-10.
\n
*/
\n\n
export async function walkNode(node, callback, includeSelf = false) {\n
\t
if (includeSelf) {\n
\t\t
await callback(node, null);
\n\t
}
\n\t
if (node.children && node.children.length > 0) {\n
\t\t
for (let childNode of node.children) {\n
\t\t\t
await callback(childNode, node);
\n\t\t\t
const result = await walkNode(childNode, callback);
\n\t\t\t
if (result === true)
{
\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t}\n
}
\n\n
export async function walkObject(obj, callback) {\n
\t
if(typeof obj ===
\"
object
\"
){\n
\t\t
for (let key of Object.keys(obj)) {\n
\t\t\t
const value = obj[key];
\n\t\t\t
await callback(key, value, obj);
\n\t\t\t
const result = await walkObject(value, callback);
\n\t\t\t
if (result === true)
{
\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t}\n
}
\n
"
,
"/**
\n
* Created by rockyl on 2019-09-26.
\n
*
\n
* 导出zeroing的视图
\n
*/
\n\n
import
{
getTree
}
from
\"
./psd-tree
\"
;
\n
import
{
walkNode
}
from
\"
./utils
\"
;
\n
import path from 'path'
\n
import Color from 'color'
\n
import generateUUID from 'uuid/v4'
\n
import hash from 'object-hash';
\n
import zlib from 'zlib';
\n\n
const relativePosPrefixMap = {\n
\t
l: 'left',
\n\t
t: 'top',
\n\t
r: 'right',
\n\t
b: 'bottom',
\n\t
h: 'horizonCenter',
\n\t
v: 'verticalCenter',
\n\t
wp: 'percentWidth',
\n\t
hp: 'percentHeight',
\n
};
\n\n
export async function execute(psdFile, options) {\n
\t
const tree = await getTree(psdFile);
\n\n\t
let viewRoot = {\n
\t\t
name: path.basename(psdFile.name, '.psd'),
\n\t\t
type: 'node',
\n\t\t
uuid: generateUUID(),
\n\t
};
\n\n\t
const assets = [];
\n\t
const imageHashMap = {};
\n\n\t
await walkNode(tree, async function (node, parent) {\n
\t\t
let
{
name
}
= node;
\n\t\t
const {x, y, width, height, alpha, visible, origin: {layer, layer: {typeTool, solidColor}}} = node;
\n\t\t
console.log('walk node:', name);
\n\t\t
let properties = {\n
\t\t\t
width, height, alpha, visible,
\n\t\t
};
\n\n\t\t
if (name.includes('|')) {\n
\t\t\t
let arr = name.split('|');
\n\t\t\t
name = arr[0];
\n\t\t\t
let paramsStr = arr[0];
\n\t\t\t
let params = paramsStr.split(';');
\n\t\t\t
let relativePos = params[0];
\n\t\t\t
if (relativePos) {\n
\t\t\t\t
let items = relativePos.split(',');
\n\t\t\t\t
for (let item of items) {\n
\t\t\t\t\t
let prefix = item[0];
\n\t\t\t\t\t
let field = relativePosPrefixMap[prefix];
\n\t\t\t\t\t
if (field) {\n
\t\t\t\t\t\t
properties[field] = parseFloat(item.substr(1));
\n\t\t\t\t\t
}
\n\t\t\t\t
}
\n\t\t\t
}
\n\t\t
}
\n\n\t\t
let viewNode = {\n
\t\t\t
name,
\n\t\t\t
properties,
\n\t\t\t
uuid: generateUUID(),
\n\t\t
};
\n\t\t
let dealLater = true;
\n\t\t
if (x !== 0) {\n
\t\t\t
properties.x = x;
\n\t\t
}
\n\t\t
if (y !== 0) {\n
\t\t\t
properties.y = y;
\n\t\t
}
\n\n\t\t
if (typeTool) {\n
\t\t\t
let fontInfo = typeTool();
\n\t\t\t
properties.text = fontInfo.textValue;
\n\t\t\t
const sizes = fontInfo.sizes();
\n\t\t\t
const colors = fontInfo.colors();
\n\t\t\t
properties.size = sizes ? sizes[0] || 20 : 20;
\n\t\t\t
let [r, g, b, a] = colors[0];
\n\t\t\t
properties.fillColor = `rgba(${r}, ${g}, ${b}, ${a / 255})`;
\n\t\t\t
viewNode.type = 'label';
\n\t\t\t
dealLater = false;
\n\t\t
} else if (solidColor) {\n
\t\t\t
const {r, g, b} = solidColor();
\n\t\t\t
let color = Color({r, g, b});
\n\n\t\t\t
let paths = layer.vectorMask().paths;
\n\t\t\t
if (paths[2].numPoints === 4) {\n
\t\t\t\t
let isRect = true;
\n\t\t\t\t
for (let i = 3; i < paths.length; i++) {\n
\t\t\t\t\t
if (paths[i].recordType !== 2) {\n
\t\t\t\t\t\t
isRect = false;
\n\t\t\t\t\t\t
break;
\n\t\t\t\t\t
}
\n\t\t\t\t
}
\n\t\t\t\t
if (isRect) {\n
\t\t\t\t\t
viewNode.type = 'rect';
\n\t\t\t\t\t
properties.fillColor = '#' + color.rgbNumber().toString(16);
\n\t\t\t\t\t
dealLater = false;
\n\t\t\t\t
}
\n\t\t\t
}
\n\t\t
}
\n\n\t\t
if (dealLater) {\n
\t\t\t
if (node.hasOwnProperty('children')) {\n
\t\t\t\t
viewNode.type = 'node';
\n\t\t\t
} else {\n
\t\t\t\t
viewNode.type = 'image';
\n\n\t\t\t\t
let uuid = generateUUID();
\n\t\t\t\t
const ext = '.png';
\n\n\t\t\t\t
let dataUrl;
\n\t\t\t\t
try {\n
\t\t\t\t\t
let img = node.origin.toPng();
\n\t\t\t\t\t
dataUrl = img.src;
\n\t\t\t\t
} catch (e)
{
\n\n\t\t\t\t
}
\n\n\t\t\t\t
if (dataUrl) {\n
\t\t\t\t\t
let base64Data = dataUrl.replace(/^data:image
\\
/
\\
w+;base64,/,
\"\"
);
\n\t\t\t\t\t
let buffer = new Buffer(base64Data, 'base64');
\n\t\t\t\t\t
const fileNameHash = hash(buffer);
\n\t\t\t\t\t
if (imageHashMap.hasOwnProperty(fileNameHash)) {\n
\t\t\t\t\t\t
uuid = imageHashMap[fileNameHash];
\n\t\t\t\t\t
} else {\n
\t\t\t\t\t\t
imageHashMap[fileNameHash] = uuid;
\n\t\t\t\t\t\t
assets.push({\n
\t\t\t\t\t\t\t
name,
\n\t\t\t\t\t\t\t
ext,
\n\t\t\t\t\t\t\t
uuid,
\n\t\t\t\t\t\t\t
base64Data,
\n\t\t\t\t\t\t\t
hash: fileNameHash,
\n\t\t\t\t\t\t
});
\n\t\t\t\t\t
}
\n\n\t\t\t\t\t
properties.source = 'asset://' + uuid;
\n\t\t\t\t
}
\n\t\t\t
}
\n\t\t
}
\n\n\t\t
let viewParent = parent.view || viewRoot;
\n\t\t
if (!viewParent.hasOwnProperty('children')) {\n
\t\t\t
viewParent.children = [];
\n\t\t
}
\n\t\t
viewParent.children.push(viewNode);
\n\n\t\t
node.view = viewNode;
\n\t
});
\n\n\t
let data = {\n
\t\t
view: viewRoot,
\n\t\t
assets,
\n\t
};
\n\n\t
let dataString = JSON.stringify(data);
\n\t
console.log(dataString.length);
\n\n\t
let buf = new Buffer(dataString);
\n\t
return await new Promise((resolve, reject) => {\n
\t\t
zlib.gzip(buf, function (err, res) {\n
\t\t\t
if (err)
{
\n\t\t\t\treject(err);\n\t\t\t
}
else
{
\n\t\t\t\tconsole.log(res.length);\n\t\t\t\tresolve(res);\n\t\t\t}\n\t\t})\n\t})\n}\n"],"names":[],"mappings":";;;;;;;;;;;;AAAA;;;;AAIA,MAAM,GAAG,GAAG,MAAM,CAAC,SAAS,CAAC,CAAC,KAAK,CAAC,CAAC;;AAErC,AAAO,eAAe,OAAO,CAAC,IAAI,EAAE;CACnC,IAAI,GAAG,GAAG,MAAM,GAAG,CAAC,eAAe,CAAC,IAAI,CAAC,CAAC;;CAE1C,MAAM,IAAI,GAAG,EAAE,CAAC;CAChB,IAAI,CAAC,GAAG,CAAC,IAAI,EAAE,EAAE,IAAI,CAAC,CAAC;;CAEvB,OAAO,IAAI,CAAC;CACZ;;AAED,SAAS,IAAI,CAAC,MAAM,EAAE,QAAQ,EAAE;CAC/B,MAAM,CAAC,IAAI,EAAE,KAAK,GAAG,CAAC,EAAE,GAAG,EAAE,IAAI,GAAG,CAAC,EAAE,GAAG,MAAM,CAAC,MAAM,IAAI,EAAE,CAAC;CAC9D,MAAM,CAAC,IAAI,EAAE,GAAG,EAAE,KAAK,EAAE,MAAM,EAAE,IAAI,EAAE,KAAK,EAAE,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC,GAAG,MAAM,CAAC;CAC3E,MAAM,CAAC,GAAG,IAAI,GAAG,KAAK,CAAC;CACvB,MAAM,CAAC,G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AAQ,GAAG,EAAE,CAAC;GACzB;EACD,UAAU,CAAC,QAAQ,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;;EAEnC,IAAI,CAAC,IAAI,GAAG,QAAQ,CAAC;EACrB,CAAC,CAAC;;CAEH,IAAI,IAAI,GAAG;EACV,IAAI,EAAE,QAAQ;EACd,MAAM;EACN,CAAC;;CAEF,IAAI,UAAU,GAAG,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC;CACtC,OAAO,CAAC,GAAG,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC;;CAE/B,IAAI,GAAG,GAAG,IAAI,MAAM,CAAC,UAAU,CAAC,CAAC;CACjC,OAAO,MAAM,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,MAAM,KAAK;EAC7C,IAAI,CAAC,IAAI,CAAC,GAAG,EAAE,UAAU,GAAG,EAAE,GAAG,EAAE;GAClC,IAAI,GAAG,EAAE;IACR,MAAM,CAAC,GAAG,CAAC,CAAC;IACZ,MAAM;IACN,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;IACxB,OAAO,CAAC,GAAG,CAAC,CAAC;IACb;GACD,EAAC;EACF,CAAC;CACF;;;;;"
}
\ No newline at end of file
\ No newline at end of file
dist/index.umd.js
View file @
05b0ebd5
...
@@ -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
);
}
}
...
...
dist/index.umd.js.map
View file @
05b0ebd5
{
"version"
:
3
,
"file"
:
"index.umd.js"
,
"sources"
:
[
"../src/psd-tree.js"
,
"../src/utils.js"
,
"../src/zeroing.js"
],
"sourcesContent"
:
[
"/**
\n
* Created by rockyl on 2019-08-09.
\n
*/
\n\n
const PSD = window['require']('psd');
\n\n
export async function getTree(file) {\n
\t
let psd = await PSD.fromDroppedFile(file);
\n\n\t
const root = {};
\n\t
walk(psd.tree(), root);
\n\n\t
return root;
\n
}
\n\n
function walk(psNode, dataNode) {\n
\t
const {left: pLeft = 0, top: pTop = 0,} = psNode.parent || {};
\n\t
const {left, top, width, height, name, layer: {opacity, visible}} = psNode;
\n\t
const x = left - pLeft;
\n\t
const y = top - pTop;
\n\n\t
Object.assign(dataNode, {x, y, width, height, alpha: opacity / 255, visible, name, origin: psNode, label: `${name} > [${x}, ${y}, ${width}, ${height}]`});
\n\t
if (psNode.children() && psNode.children().length > 0){\n
\t\t
dataNode.children = [];
\n\t
}
\n\n\t
let children = psNode.children();
\n\t
for (let i = children.length - 1; i >= 0; i--) {\n
\t\t
const childPsNode = children[i];
\n\n\t\t
const childDataNode = {};
\n\t\t
dataNode.children.push(childDataNode);
\n\t\t
walk(childPsNode, childDataNode)
\n\t
}
\n
}
\n
"
,
"/**
\n
* Created by rockyl on 2019-08-10.
\n
*/
\n\n
export async function walkNode(node, callback, includeSelf = false) {\n
\t
if (includeSelf) {\n
\t\t
await callback(node, null);
\n\t
}
\n\t
if (node.children && node.children.length > 0) {\n
\t\t
for (let childNode of node.children) {\n
\t\t\t
await callback(childNode, node);
\n\t\t\t
const result = await walkNode(childNode, callback);
\n\t\t\t
if (result === true)
{
\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t}\n
}
\n\n
export async function walkObject(obj, callback) {\n
\t
if(typeof obj ===
\"
object
\"
){\n
\t\t
for (let key of Object.keys(obj)) {\n
\t\t\t
const value = obj[key];
\n\t\t\t
await callback(key, value, obj);
\n\t\t\t
const result = await walkObject(value, callback);
\n\t\t\t
if (result === true)
{
\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t}\n
}
\n
"
,
"/**
\n
* Created by rockyl on 2019-09-26.
\n
*
\n
* 导出zeroing的视图
\n
*/
\n\n
import
{
getTree
}
from
\"
./psd-tree
\"
;
\n
import
{
walkNode
}
from
\"
./utils
\"
;
\n
import path from 'path'
\n
import Color from 'color'
\n
import generateUUID from 'uuid/v4'
\n
import hash from 'object-hash';
\n
import zlib from 'zlib';
\n\n
export async function execute(psdFile, options) {\n
\t
const tree = await getTree(psdFile);
\n\n\t
let viewRoot = {\n
\t\t
name: path.basename(psdFile.name, '.psd'),
\n\t\t
type: 'node',
\n\t\t
uuid: generateUUID(),
\n\t
};
\n\n\t
const assets = [];
\n\t
const imageHashMap = {};
\n\n\t
await walkNode(tree, async function (node, parent) {\n
\t\t
const {name, x, y, width, height, alpha, visible, origin: {layer, layer: {typeTool, solidColor}}} = node;
\n\t\t
console.log('walk node:', name);
\n\t\t
let properties = {\n
\t\t\t
width, height, alpha, visible,
\n\t\t
};
\n\t\t
let viewNode = {\n
\t\t\t
name,
\n\t\t\t
properties,
\n\t\t\t
uuid: generateUUID(),
\n\t\t
};
\n\t\t
let dealLater = true;
\n\t\t
if (x !== 0) {\n
\t\t\t
properties.x = x;
\n\t\t
}
\n\t\t
if (y !== 0) {\n
\t\t\t
properties.y = y;
\n\t\t
}
\n\n\t\t
if (typeTool) {\n
\t\t\t
let fontInfo = typeTool();
\n\t\t\t
//const fonts = fontInfo.fonts();
\n\t\t\t
//const styles = fontInfo.styles();
\n\t\t\t
//const
{
RunLengthArray
}
= fontInfo.engineData.EngineDict.StyleRun;
\n\n\t\t\t
properties.text = fontInfo.textValue;
\n\t\t\t
const sizes = fontInfo.sizes();
\n\t\t\t
const colors = fontInfo.colors();
\n\t\t\t
properties.size = sizes ? sizes[0] || 20 : 20;
\n\t\t\t
let [r, g, b, a] = colors[0];
\n\t\t\t
properties.fillColor = `rgba(${r}, ${g}, ${b}, ${a / 255})`;
\n\t\t\t
/*properties.textflow = {\n
\t\t\t\t
fonts, styles, RunLengthArray,
\n\t\t\t
};*/
\n\t\t\t
viewNode.type = 'label';
\n\t\t\t
dealLater = false;
\n\t\t
} else if (solidColor) {\n
\t\t\t
const {r, g, b} = solidColor();
\n\t\t\t
let color = Color({r, g, b});
\n\n\t\t\t
let paths = layer.vectorMask().paths;
\n\t\t\t
if (paths[2].numPoints === 4) {\n
\t\t\t\t
let isRect = true;
\n\t\t\t\t
for (let i = 3; i < paths.length; i++) {\n
\t\t\t\t\t
if (paths[i].recordType !== 2) {\n
\t\t\t\t\t\t
isRect = false;
\n\t\t\t\t\t\t
break;
\n\t\t\t\t\t
}
\n\t\t\t\t
}
\n\t\t\t\t
if (isRect) {\n
\t\t\t\t\t
viewNode.type = 'rect';
\n\t\t\t\t\t
properties.fillColor = '#' + color.rgbNumber().toString(16);
\n\t\t\t\t\t
dealLater = false;
\n\t\t\t\t
}
\n\t\t\t
}
\n\t\t
}
\n\n\t\t
if (dealLater) {\n
\t\t\t
if (node.hasOwnProperty('children')) {\n
\t\t\t\t
viewNode.type = 'node';
\n\t\t\t
} else {\n
\t\t\t\t
viewNode.type = 'image';
\n\n\t\t\t\t
let uuid = generateUUID();
\n\t\t\t\t
const ext = '.png';
\n\n\t\t\t\t
let dataUrl;
\n\t\t\t\t
try {\n
\t\t\t\t\t
let img = node.origin.toPng();
\n\t\t\t\t\t
dataUrl = img.src;
\n\t\t\t\t
}catch (e)
{
\n\n\t\t\t\t
}
\n\n\t\t\t\t
if (dataUrl) {\n
\t\t\t\t\t
let base64Data = dataUrl.replace(/^data:image
\\
/
\\
w+;base64,/,
\"\"
);
\n\t\t\t\t\t
let buffer = new Buffer(base64Data, 'base64');
\n\t\t\t\t\t
const fileNameHash = hash(buffer);
\n\t\t\t\t\t
if (imageHashMap.hasOwnProperty(fileNameHash)) {\n
\t\t\t\t\t\t
uuid = imageHashMap[fileNameHash];
\n\t\t\t\t\t
} else {\n
\t\t\t\t\t\t
imageHashMap[fileNameHash] = uuid;
\n\t\t\t\t\t\t
assets.push({\n
\t\t\t\t\t\t\t
name,
\n\t\t\t\t\t\t\t
ext,
\n\t\t\t\t\t\t\t
uuid,
\n\t\t\t\t\t\t\t
base64Data,
\n\t\t\t\t\t\t\t
hash: fileNameHash,
\n\t\t\t\t\t\t
});
\n\t\t\t\t\t
}
\n\n\t\t\t\t\t
properties.source = 'asset://' + uuid;
\n\t\t\t\t
}
\n\t\t\t
}
\n\t\t
}
\n\n\t\t
let viewParent = parent.view || viewRoot;
\n\t\t
if (!viewParent.hasOwnProperty('children')) {\n
\t\t\t
viewParent.children = [];
\n\t\t
}
\n\t\t
viewParent.children.push(viewNode);
\n\n\t\t
node.view = viewNode;
\n\t
});
\n\n\t
let data = {\n
\t\t
view: viewRoot,
\n\t\t
assets,
\n\t
};
\n\n\t
let dataString = JSON.stringify(data);
\n\t
console.log(dataString.length);
\n\n\t
let buf = new Buffer(dataString);
\n\t
return await new Promise((resolve, reject)=>
{
\n\t\tzlib.gzip(buf,function(err,res){\n\t\t\tif(err){\n\t\t\t\treject(err);\n\t\t\t}else{\n\t\t\t\tconsole.log(res.length);\n\t\t\t\tresolve(res);\n\t\t\t}\n\t\t})\n\t})\n}\n"],"names":[],"mappings":";;;;;;;;;;;;CAAA;CACA;CACA;;CAEA,MAAM,GAAG,GAAG,MAAM,CAAC,SAAS,CAAC,CAAC,KAAK,CAAC,CAAC;;AAErC,CAAO,eAAe,OAAO,CAAC,IAAI,EAAE;CACpC,CAAC,IAAI,GAAG,GAAG,MAAM,GAAG,CAAC,eAAe,CAAC,IAAI,CAAC,CAAC;;CAE3C,CAAC,MAAM,IAAI,GAAG,EAAE,CAAC;CACjB,CAAC,IAAI,CAAC,GAAG,CAAC,IAAI,EAAE,EAAE,IAAI,CAAC,CAAC;;CAExB,CAAC,OAAO,IAAI,CAAC;CACb,CAAC;;CAED,SAAS,IAAI,CAAC,MAAM,EAAE,QAAQ,EAAE;CAChC,CAAC,MAAM,CAAC,IAAI,EAAE,KAAK,GAAG,CAAC,EAAE,GAAG,EAAE,IAAI,GAAG,CAAC,EAAE,GAAG,MAAM,CAAC,MAAM,IAAI,EAAE,CAAC;CAC/D,CAAC,MAAM,CAAC,IAAI,EAAE,GAAG,EAAE,KAAK,EAAE,MAAM,EAAE,IAAI,EAAE,KAAK,EAAE,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC,GAAG,MAAM,CAAC;CAC5E,CAAC,MAAM,CAAC,GAAG,IAAI,GAAG,KA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AG;CACZ,EAAE,IAAI,EAAE,QAAQ;CAChB,EAAE,MAAM;CACR,EAAE,CAAC;;CAEH,CAAC,IAAI,UAAU,GAAG,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC;CACvC,CAAC,OAAO,CAAC,GAAG,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC;;CAEhC,CAAC,IAAI,GAAG,GAAG,IAAI,MAAM,CAAC,UAAU,CAAC,CAAC;CAClC,CAAC,OAAO,MAAM,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,MAAM,GAAG;CAC7C,EAAE,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,SAAS,GAAG,CAAC,GAAG,CAAC;CACjC,GAAG,GAAG,GAAG,CAAC;CACV,IAAI,MAAM,CAAC,GAAG,CAAC,CAAC;CAChB,IAAI,IAAI;CACR,IAAI,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;CAC5B,IAAI,OAAO,CAAC,GAAG,CAAC,CAAC;CACjB,IAAI;CACJ,GAAG,EAAC;CACJ,EAAE,CAAC;CACH,CAAC;;;;;;;;;;;;;"
}
{
"version"
:
3
,
"file"
:
"index.umd.js"
,
"sources"
:
[
"../src/psd-tree.js"
,
"../src/utils.js"
,
"../src/zeroing.js"
],
"sourcesContent"
:
[
"/**
\n
* Created by rockyl on 2019-08-09.
\n
*/
\n\n
const PSD = window['require']('psd');
\n\n
export async function getTree(file) {\n
\t
let psd = await PSD.fromDroppedFile(file);
\n\n\t
const root = {};
\n\t
walk(psd.tree(), root);
\n\n\t
return root;
\n
}
\n\n
function walk(psNode, dataNode) {\n
\t
const {left: pLeft = 0, top: pTop = 0,} = psNode.parent || {};
\n\t
const {left, top, width, height, name, layer: {opacity, visible}} = psNode;
\n\t
const x = left - pLeft;
\n\t
const y = top - pTop;
\n\n\t
Object.assign(dataNode, {x, y, width, height, alpha: opacity / 255, visible, name, origin: psNode, label: `${name} > [${x}, ${y}, ${width}, ${height}]`});
\n\t
if (psNode.children() && psNode.children().length > 0){\n
\t\t
dataNode.children = [];
\n\t
}
\n\n\t
let children = psNode.children();
\n\t
for (let i = children.length - 1; i >= 0; i--) {\n
\t\t
const childPsNode = children[i];
\n\n\t\t
const childDataNode = {};
\n\t\t
dataNode.children.push(childDataNode);
\n\t\t
walk(childPsNode, childDataNode)
\n\t
}
\n
}
\n
"
,
"/**
\n
* Created by rockyl on 2019-08-10.
\n
*/
\n\n
export async function walkNode(node, callback, includeSelf = false) {\n
\t
if (includeSelf) {\n
\t\t
await callback(node, null);
\n\t
}
\n\t
if (node.children && node.children.length > 0) {\n
\t\t
for (let childNode of node.children) {\n
\t\t\t
await callback(childNode, node);
\n\t\t\t
const result = await walkNode(childNode, callback);
\n\t\t\t
if (result === true)
{
\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t}\n
}
\n\n
export async function walkObject(obj, callback) {\n
\t
if(typeof obj ===
\"
object
\"
){\n
\t\t
for (let key of Object.keys(obj)) {\n
\t\t\t
const value = obj[key];
\n\t\t\t
await callback(key, value, obj);
\n\t\t\t
const result = await walkObject(value, callback);
\n\t\t\t
if (result === true)
{
\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t}\n
}
\n
"
,
"/**
\n
* Created by rockyl on 2019-09-26.
\n
*
\n
* 导出zeroing的视图
\n
*/
\n\n
import
{
getTree
}
from
\"
./psd-tree
\"
;
\n
import
{
walkNode
}
from
\"
./utils
\"
;
\n
import path from 'path'
\n
import Color from 'color'
\n
import generateUUID from 'uuid/v4'
\n
import hash from 'object-hash';
\n
import zlib from 'zlib';
\n\n
const relativePosPrefixMap = {\n
\t
l: 'left',
\n\t
t: 'top',
\n\t
r: 'right',
\n\t
b: 'bottom',
\n\t
h: 'horizonCenter',
\n\t
v: 'verticalCenter',
\n\t
wp: 'percentWidth',
\n\t
hp: 'percentHeight',
\n
};
\n\n
export async function execute(psdFile, options) {\n
\t
const tree = await getTree(psdFile);
\n\n\t
let viewRoot = {\n
\t\t
name: path.basename(psdFile.name, '.psd'),
\n\t\t
type: 'node',
\n\t\t
uuid: generateUUID(),
\n\t
};
\n\n\t
const assets = [];
\n\t
const imageHashMap = {};
\n\n\t
await walkNode(tree, async function (node, parent) {\n
\t\t
let
{
name
}
= node;
\n\t\t
const {x, y, width, height, alpha, visible, origin: {layer, layer: {typeTool, solidColor}}} = node;
\n\t\t
console.log('walk node:', name);
\n\t\t
let properties = {\n
\t\t\t
width, height, alpha, visible,
\n\t\t
};
\n\n\t\t
if (name.includes('|')) {\n
\t\t\t
let arr = name.split('|');
\n\t\t\t
name = arr[0];
\n\t\t\t
let paramsStr = arr[0];
\n\t\t\t
let params = paramsStr.split(';');
\n\t\t\t
let relativePos = params[0];
\n\t\t\t
if (relativePos) {\n
\t\t\t\t
let items = relativePos.split(',');
\n\t\t\t\t
for (let item of items) {\n
\t\t\t\t\t
let prefix = item[0];
\n\t\t\t\t\t
let field = relativePosPrefixMap[prefix];
\n\t\t\t\t\t
if (field) {\n
\t\t\t\t\t\t
properties[field] = parseFloat(item.substr(1));
\n\t\t\t\t\t
}
\n\t\t\t\t
}
\n\t\t\t
}
\n\t\t
}
\n\n\t\t
let viewNode = {\n
\t\t\t
name,
\n\t\t\t
properties,
\n\t\t\t
uuid: generateUUID(),
\n\t\t
};
\n\t\t
let dealLater = true;
\n\t\t
if (x !== 0) {\n
\t\t\t
properties.x = x;
\n\t\t
}
\n\t\t
if (y !== 0) {\n
\t\t\t
properties.y = y;
\n\t\t
}
\n\n\t\t
if (typeTool) {\n
\t\t\t
let fontInfo = typeTool();
\n\t\t\t
properties.text = fontInfo.textValue;
\n\t\t\t
const sizes = fontInfo.sizes();
\n\t\t\t
const colors = fontInfo.colors();
\n\t\t\t
properties.size = sizes ? sizes[0] || 20 : 20;
\n\t\t\t
let [r, g, b, a] = colors[0];
\n\t\t\t
properties.fillColor = `rgba(${r}, ${g}, ${b}, ${a / 255})`;
\n\t\t\t
viewNode.type = 'label';
\n\t\t\t
dealLater = false;
\n\t\t
} else if (solidColor) {\n
\t\t\t
const {r, g, b} = solidColor();
\n\t\t\t
let color = Color({r, g, b});
\n\n\t\t\t
let paths = layer.vectorMask().paths;
\n\t\t\t
if (paths[2].numPoints === 4) {\n
\t\t\t\t
let isRect = true;
\n\t\t\t\t
for (let i = 3; i < paths.length; i++) {\n
\t\t\t\t\t
if (paths[i].recordType !== 2) {\n
\t\t\t\t\t\t
isRect = false;
\n\t\t\t\t\t\t
break;
\n\t\t\t\t\t
}
\n\t\t\t\t
}
\n\t\t\t\t
if (isRect) {\n
\t\t\t\t\t
viewNode.type = 'rect';
\n\t\t\t\t\t
properties.fillColor = '#' + color.rgbNumber().toString(16);
\n\t\t\t\t\t
dealLater = false;
\n\t\t\t\t
}
\n\t\t\t
}
\n\t\t
}
\n\n\t\t
if (dealLater) {\n
\t\t\t
if (node.hasOwnProperty('children')) {\n
\t\t\t\t
viewNode.type = 'node';
\n\t\t\t
} else {\n
\t\t\t\t
viewNode.type = 'image';
\n\n\t\t\t\t
let uuid = generateUUID();
\n\t\t\t\t
const ext = '.png';
\n\n\t\t\t\t
let dataUrl;
\n\t\t\t\t
try {\n
\t\t\t\t\t
let img = node.origin.toPng();
\n\t\t\t\t\t
dataUrl = img.src;
\n\t\t\t\t
} catch (e)
{
\n\n\t\t\t\t
}
\n\n\t\t\t\t
if (dataUrl) {\n
\t\t\t\t\t
let base64Data = dataUrl.replace(/^data:image
\\
/
\\
w+;base64,/,
\"\"
);
\n\t\t\t\t\t
let buffer = new Buffer(base64Data, 'base64');
\n\t\t\t\t\t
const fileNameHash = hash(buffer);
\n\t\t\t\t\t
if (imageHashMap.hasOwnProperty(fileNameHash)) {\n
\t\t\t\t\t\t
uuid = imageHashMap[fileNameHash];
\n\t\t\t\t\t
} else {\n
\t\t\t\t\t\t
imageHashMap[fileNameHash] = uuid;
\n\t\t\t\t\t\t
assets.push({\n
\t\t\t\t\t\t\t
name,
\n\t\t\t\t\t\t\t
ext,
\n\t\t\t\t\t\t\t
uuid,
\n\t\t\t\t\t\t\t
base64Data,
\n\t\t\t\t\t\t\t
hash: fileNameHash,
\n\t\t\t\t\t\t
});
\n\t\t\t\t\t
}
\n\n\t\t\t\t\t
properties.source = 'asset://' + uuid;
\n\t\t\t\t
}
\n\t\t\t
}
\n\t\t
}
\n\n\t\t
let viewParent = parent.view || viewRoot;
\n\t\t
if (!viewParent.hasOwnProperty('children')) {\n
\t\t\t
viewParent.children = [];
\n\t\t
}
\n\t\t
viewParent.children.push(viewNode);
\n\n\t\t
node.view = viewNode;
\n\t
});
\n\n\t
let data = {\n
\t\t
view: viewRoot,
\n\t\t
assets,
\n\t
};
\n\n\t
let dataString = JSON.stringify(data);
\n\t
console.log(dataString.length);
\n\n\t
let buf = new Buffer(dataString);
\n\t
return await new Promise((resolve, reject) => {\n
\t\t
zlib.gzip(buf, function (err, res) {\n
\t\t\t
if (err)
{
\n\t\t\t\treject(err);\n\t\t\t
}
else
{
\n\t\t\t\tconsole.log(res.length);\n\t\t\t\tresolve(res);\n\t\t\t}\n\t\t})\n\t})\n}\n"],"names":[],"mappings":";;;;;;;;;;;;CAAA;CACA;CACA;;CAEA,MAAM,GAAG,GAAG,MAAM,CAAC,SAAS,CAAC,CAAC,KAAK,CAAC,CAAC;;AAErC,CAAO,eAAe,OAAO,CAAC,IAAI,EAAE;CACpC,CAAC,IAAI,GAAG,GAAG,MAAM,GAAG,CAAC,eAAe,CAAC,IAAI,CAAC,CAAC;;CAE3C,CAAC,MAAM,IAAI,GAAG,EAAE,CAAC;CACjB,CAAC,IAAI,CAAC,GAAG,CAAC,IAAI,EAAE,EAAE,IAAI,CAAC,CAAC;;CAExB,CAAC,OAAO,IAAI,CAAC;CACb,CAAC;;CAED,SAAS,IAAI,CAAC,MAAM,EAAE,QAAQ,EAAE;CAChC,CAAC,MAAM,CAAC,IAAI,EAAE,KAAK,GAAG,CAAC,EAAE,GAAG,EAAE,IAAI,GAAG,CAAC,EAAE,GAAG,MAAM,CAAC,MAAM,IAAI,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C,QAAQ,CAAC,CAAC;;CAErC,EAAE,IAAI,CAAC,IAAI,GAAG,QAAQ,CAAC;CACvB,EAAE,CAAC,CAAC;;CAEJ,CAAC,IAAI,IAAI,GAAG;CACZ,EAAE,IAAI,EAAE,QAAQ;CAChB,EAAE,MAAM;CACR,EAAE,CAAC;;CAEH,CAAC,IAAI,UAAU,GAAG,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC;CACvC,CAAC,OAAO,CAAC,GAAG,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC;;CAEhC,CAAC,IAAI,GAAG,GAAG,IAAI,MAAM,CAAC,UAAU,CAAC,CAAC;CAClC,CAAC,OAAO,MAAM,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,MAAM,KAAK;CAC/C,EAAE,IAAI,CAAC,IAAI,CAAC,GAAG,EAAE,UAAU,GAAG,EAAE,GAAG,EAAE;CACrC,GAAG,IAAI,GAAG,EAAE;CACZ,IAAI,MAAM,CAAC,GAAG,CAAC,CAAC;CAChB,IAAI,MAAM;CACV,IAAI,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;CAC5B,IAAI,OAAO,CAAC,GAAG,CAAC,CAAC;CACjB,IAAI;CACJ,GAAG,EAAC;CACJ,EAAE,CAAC;CACH,CAAC;;;;;;;;;;;;;"
}
\ No newline at end of file
\ No newline at end of file
package.json
View file @
05b0ebd5
...
@@ -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"
:
{
...
...
src/zeroing.js
View file @
05b0ebd5
...
@@ -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
);
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment