-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathElementStyle.js
More file actions
114 lines (95 loc) · 3.21 KB
/
ElementStyle.js
File metadata and controls
114 lines (95 loc) · 3.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
This component is part of an Element component wich can be of many types: Table, Line, ..., Container
The Container type can nest another Elements inside.
Due to that I had issues with the styles and this component updates the element styles by creating the CSS name based on the Element.id
*/
import React from "react";
import { connect } from "react-redux";
import { Style } from "react-style-tag";
import _flatten from 'lodash/flatten'
import _omit from 'lodash/omit'
import _pick from 'lodash/pick'
import _isEmpty from 'lodash/isEmpty'
import _fromPairs from 'lodash/fromPairs'
const objectToCSS = objStyle => {
const stringStyles = objStyle ?
Object.entries(objStyle)
.sort()
.map(([k, v]) => {
const mapper = {
'backgroundColor': 'background-color',
'borderColor': 'border-color',
'fontColor': 'color'
}[k] || k;
return [mapper, v].join(': ')
})
.join(';\n') :
''
return stringStyles.length ? `${stringStyles};` : ''
}
const extractTableStyles = objStyle => {
const tableSupportedStyles = [
'border',
'borderColor'
]
return [_omit(objStyle, tableSupportedStyles), _pick(objStyle, tableSupportedStyles)]
}
//sepparates styles from table or element.
//Required to render the border on the table.
const splitStyles = (objStyle, type) => {
return (type === 'table') ? extractTableStyles(objStyle) : [objStyle, {}]
}
export const recursiveParentId = (el, allElements) => {
if (!el) return []
if (!el.parentElementId) return [el.elementId]
return [el.elementId, ...recursiveParentId(allElements.find(this_el => this_el.elementId === el.parentElementId), allElements)]
}
const getAllPaths = allElements => {
return _fromPairs(allElements.map(el => [
el.elementId,
'.Element-' + recursiveParentId(el, allElements).reverse().join(' .Element-')
]))
}
const ElementStyle = ({allElements}) => {
const allPaths = getAllPaths(allElements)
const generateStyleForElement = ({elementId, width, height, top, left, style, header, type, parentElementId, fixed}, index) => {
const [elemStyle, tableStyle] = splitStyles(style, type)
let cssPath = allPaths[elementId] || ''
const tableCSS = _isEmpty(tableStyle) ? '' : `
${cssPath} .TableElement table,
${cssPath} .TableElement td {
${objectToCSS(tableStyle)}
}
`
const tableHeaderCSS = _isEmpty(header) ? '' : `
${cssPath} .TableElement table tr:first-child {
${objectToCSS(header)}
}
`
return `
${cssPath} .Element-content {
border: 1px dashed gray;
background: none;
position: absolute;
width: ${width}px;
height: ${height}px;
/* z-index: ${(index + 1) * 10}; */
${objectToCSS(elemStyle)}
}
${tableCSS}
${tableHeaderCSS}
`
}
return allElements.map((elem, k) => <Style key={k}>{generateStyleForElement(elem, k)}</Style>)
}
const mapStateToProps = ({app, blueprints}) => {
const blueprintId = app.selection.blueprint
const allElements = _flatten(blueprints.byId[blueprintId].pages.map(p => p.elements))
.concat(blueprints.byId[blueprintId].fixedElements)
return {
allElements
}
}
export default connect(
mapStateToProps
)(ElementStyle);