Skip to content

Commit bb146dd

Browse files
committed
feat(plainShapeObject): add function to create plain shape object
1 parent 687901b commit bb146dd

3 files changed

Lines changed: 132 additions & 44 deletions

File tree

README.md

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,51 @@
11
# Wilderness DOM node
22

3-
Converts SVG DOM node <> Frame Shape.
3+
A set of functions to convert between SVG DOM nodes,
4+
Plain Shape Objects and Frame Shapes.
45

56
**Only 2.8kb gzipped.**
67

7-
Firstly, let's define a *Frame Shape*. A Frame Shape is an object
8-
commonly used internally within
8+
## Definitions
9+
10+
### Plain Shape Object
11+
12+
A Plain Shape Object is the most basic way of defining shapes within
913
[Wilderness](https://github.com/colinmeinke/wilderness).
14+
The core properties of a Plain Shape Object can be found in the
15+
[SVG Points spec](https://github.com/colinmeinke/svg-points#readme).
16+
17+
### Frame Shape
18+
19+
A Frame Shape is an object commonly used internally within Wilderness.
1020
A Frame Shape has two properties, `attributes` and a `points`
1121
[see the points spec](https://github.com/colinmeinke/points).
1222

13-
## frameShape
23+
## Functions
24+
25+
### plainShapeObject
26+
27+
The `plainShapeObject` function converts a SVG DOM node to a Plain
28+
Shape Object. It will also add all of the node's HTML attributes as
29+
properties of the Plain Shape Object.
30+
31+
```js
32+
import { frameShape } from 'wilderness-dom-node'
33+
34+
console.log(
35+
plainShapeObject(document.querySelector('rect'))
36+
)
37+
38+
// {
39+
// type: 'rect',
40+
// x: 20,
41+
// y: 20,
42+
// width: 60,
43+
// height: 60,
44+
// fill: 'yellow'
45+
// }
46+
```
47+
48+
### frameShape
1449

1550
The `frameShape` function converts a SVG DOM node to a Frame Shape.
1651

@@ -35,7 +70,7 @@ console.log(
3570
// }
3671
```
3772

38-
## node
73+
### node
3974

4075
The `node` function converts a Frame Shape to a SVG DOM node.
4176

@@ -60,7 +95,7 @@ document.querySelector('svg').appendChild(
6095
)
6196
```
6297

63-
## updateNode
98+
### updateNode
6499

65100
The `updateNode` function updates the attributes of a SVG DOM node given
66101
a Frame Shape.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@
7373
"dependencies": {
7474
"svg-points": "^6.0.0"
7575
},
76-
"description": "Converts SVG DOM node <> Frame Shape",
76+
"description": "A set of functions to convert between SVG DOM nodes, Plain Shape Objects and Frame Shapes",
7777
"devDependencies": {
7878
"babel-cli": "^6.24.1",
7979
"babel-plugin-external-helpers": "^6.22.0",

src/index.js

Lines changed: 90 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,16 @@ import { toPath, toPoints } from 'svg-points'
2929
* @typedef {Object} Node
3030
*/
3131

32+
/**
33+
* The data from a Node that is useful in Frame Shape and Plain Shape Object creation.
34+
*
35+
* @typedef {Object} NodeData
36+
*
37+
* @property {Object} attributes - All HTML attributes of the Node.
38+
* @property {Object[]} childNodes
39+
* @property {string} type - The nodeName of the Node.
40+
*/
41+
3242
/**
3343
* Wilderness' accepted node types.
3444
*/
@@ -104,7 +114,7 @@ const diff = (current, next) => {
104114
}
105115

106116
/**
107-
* Creates a FrameShape from a Node.
117+
* Creates a Frame Shape from a Node.
108118
*
109119
* @param {Node} node
110120
*
@@ -114,30 +124,23 @@ const diff = (current, next) => {
114124
* frameShapeFromNode(node)
115125
*/
116126
const frameShape = el => {
117-
const { attributes: attrs, childNodes, nodeName: type } = el
118-
const attributes = {}
119-
120-
if (el.hasAttributes()) {
121-
[ ...attrs ].map(({ name, value }) => {
122-
attributes[ name ] = value
123-
})
124-
}
127+
const { attributes, childNodes, type } = nodeData(el)
125128

126129
if (type === 'g') {
127130
return {
128131
attributes,
129-
childFrameShapes: [ ...childNodes ].filter(validNode).map(frameShape)
132+
childFrameShapes: childNodes.filter(validNode).map(frameShape)
130133
}
131134
}
132135

133136
return {
134137
attributes: removeCoreProps(type, attributes),
135-
points: toPoints(plainShapeObject(type, attributes))
138+
points: toPoints(plainShapeObjectFromAttrs(type, attributes))
136139
}
137140
}
138141

139142
/**
140-
* Creates a group Node from a FrameShape array.
143+
* Creates a group Node from a Frame Shape array.
141144
*
142145
* @param {FrameShape[]} childFrameShapes
143146
*
@@ -154,6 +157,51 @@ const groupNode = childFrameShapes => {
154157
return group
155158
}
156159

160+
/**
161+
* Creates a Node from a Frame Shape.
162+
*
163+
* @param {FrameShape} frameShape
164+
*
165+
* @returns {Node}
166+
*
167+
* @example
168+
* node(frameShape)
169+
*/
170+
const node = ({ attributes, childFrameShapes, points }) => {
171+
const el = childFrameShapes
172+
? groupNode(childFrameShapes)
173+
: pathNode(points)
174+
175+
Object.keys(attributes).map(attr => {
176+
el.setAttribute(attr, attributes[ attr ])
177+
})
178+
179+
return el
180+
}
181+
182+
/**
183+
* Creates Node Data given a Node.
184+
*
185+
* @param {Node} el
186+
*
187+
* @returns {NodeData}
188+
*
189+
* @example
190+
* nodeData(el)
191+
*/
192+
const nodeData = el => {
193+
const { attributes: attrs, childNodes, nodeName: type } = el
194+
const attributes = {}
195+
196+
if (el.hasAttributes()) {
197+
[ ...attrs ].map(({ name, value }) => {
198+
attributes[ name ] = value
199+
})
200+
}
201+
202+
return { attributes, childNodes: [ ...childNodes ], type }
203+
}
204+
157205
/**
158206
* Creates a path Node from Points.
159207
*
@@ -172,6 +220,33 @@ const pathNode = points => {
172220
return path
173221
}
174222

223+
/**
224+
* Creates a Plain Shape Object from a Node.
225+
*
226+
* @param {Node} el
227+
*
228+
* @returns {PlainShapeObject}
229+
*
230+
* @example
231+
* plainShapeObject(el)
232+
*/
233+
const plainShapeObject = el => {
234+
const { attributes, childNodes, type } = nodeData(el)
235+
236+
if (type === 'g') {
237+
return {
238+
...attributes,
239+
type,
240+
shapes: childNodes.filter(validNode).map(plainShapeObject)
241+
}
242+
}
243+
244+
return {
245+
...attributes,
246+
...plainShapeObjectFromAttrs(type, attributes)
247+
}
248+
}
249+
175250
/**
176251
* Creates a Plain Shape Object from type and an attribute object.
177252
*
@@ -181,9 +256,9 @@ const pathNode = points => {
181256
* @returns {PlainShapeObject}
182257
*
183258
* @example
184-
* plainShapeObject('rect', attributes)
259+
* plainShapeObjectFromAttrs('rect', attributes)
185260
*/
186-
const plainShapeObject = (type, attributes) => {
261+
const plainShapeObjectFromAttrs = (type, attributes) => {
187262
const props = coreProps(type)
188263
const result = { type }
189264

@@ -198,28 +273,6 @@ const plainShapeObject = (type, attributes) => {
198273
return result
199274
}
200275

201-
/**
202-
* Creates a Node from a FrameShape.
203-
*
204-
* @param {FrameShape} frameShape
205-
*
206-
* @returns {Node}
207-
*
208-
* @example
209-
* node(frameShape)
210-
*/
211-
const node = ({ attributes, childFrameShapes, points }) => {
212-
const el = childFrameShapes
213-
? groupNode(childFrameShapes)
214-
: pathNode(points)
215-
216-
Object.keys(attributes).map(attr => {
217-
el.setAttribute(attr, attributes[ attr ])
218-
})
219-
220-
return el
221-
}
222-
223276
/**
224277
* Removes type's core props from attributes object.
225278
*
@@ -245,7 +298,7 @@ const removeCoreProps = (type, attributes) => {
245298
}
246299

247300
/**
248-
* Updates a Node from a FrameShape.
301+
* Updates a Node from a Frame Shape.
249302
*
250303
* @param {Node} el
251304
* @param {FrameShape} frameShape

0 commit comments

Comments
 (0)