forked from bpampuch/pdfmake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStyleContextStack.js
216 lines (185 loc) · 5.07 KB
/
StyleContextStack.js
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import { isString, isValue } from './helpers/variableType';
/**
* Used for style inheritance and style overrides
*/
class StyleContextStack {
/**
* @param {object} styleDictionary named styles dictionary
* @param {object} defaultStyle optional default style definition
*/
constructor(styleDictionary, defaultStyle = {}) {
this.styleDictionary = styleDictionary;
this.defaultStyle = defaultStyle;
this.styleOverrides = [];
}
/**
* Creates cloned version of current stack
*
* @returns {StyleContextStack} current stack snapshot
*/
clone() {
let stack = new StyleContextStack(this.styleDictionary, this.defaultStyle);
this.styleOverrides.forEach(item => {
stack.styleOverrides.push(item);
});
return stack;
}
/**
* Pushes style-name or style-overrides-object onto the stack for future evaluation
*
* @param {string|object} styleNameOrOverride style-name (referring to styleDictionary) or
* a new dictionary defining overriding properties
*/
push(styleNameOrOverride) {
this.styleOverrides.push(styleNameOrOverride);
}
/**
* Removes last style-name or style-overrides-object from the stack
*
* @param {number} howMany optional number of elements to be popped (if not specified,
* one element will be removed from the stack)
*/
pop(howMany = 1) {
while (howMany-- > 0) {
this.styleOverrides.pop();
}
}
/**
* Creates a set of named styles or/and a style-overrides-object based on the item,
* pushes those elements onto the stack for future evaluation and returns the number
* of elements pushed, so they can be easily poped then.
*
* @param {object} item - an object with optional style property and/or style overrides
* @returns {number} the number of items pushed onto the stack
*/
autopush(item) {
if (isString(item)) {
return 0;
}
let styleNames = [];
if (item.style) {
if (Array.isArray(item.style)) {
styleNames = item.style;
} else {
styleNames = [item.style];
}
}
for (let i = 0, l = styleNames.length; i < l; i++) {
this.push(styleNames[i]);
}
let styleProperties = [
'font',
'fontSize',
'fontFeatures',
'bold',
'italics',
'alignment',
'color',
'columnGap',
'fillColor',
'fillOpacity',
'decoration',
'decorationStyle',
'decorationColor',
'background',
'lineHeight',
'characterSpacing',
'noWrap',
'markerColor',
'leadingIndent',
'sup',
'sub'
//'tableCellPadding'
// 'cellBorder',
// 'headerCellBorder',
// 'oddRowCellBorder',
// 'evenRowCellBorder',
// 'tableBorder'
];
let styleOverrideObject = {};
let pushStyleOverrideObject = false;
styleProperties.forEach(key => {
if (isValue(item[key])) {
styleOverrideObject[key] = item[key];
pushStyleOverrideObject = true;
}
});
if (pushStyleOverrideObject) {
this.push(styleOverrideObject);
}
return styleNames.length + (pushStyleOverrideObject ? 1 : 0);
}
/**
* Automatically pushes elements onto the stack, using autopush based on item,
* executes callback and then pops elements back. Returns value returned by callback
*
* @param {object} item - an object with optional style property and/or style overrides
* @param {Function} callback to be called between autopush and pop
* @returns {object} value returned by callback
*/
auto(item, callback) {
let pushedItems = this.autopush(item);
let result = callback();
if (pushedItems > 0) {
this.pop(pushedItems);
}
return result;
}
/**
* Evaluates stack and returns value of a named property
*
* @param {string} property - property name
* @returns {?any} property value or null if not found
*/
getProperty(property) {
if (this.styleOverrides) {
for (let i = this.styleOverrides.length - 1; i >= 0; i--) {
let item = this.styleOverrides[i];
if (isString(item)) { // named-style-override
let style = this.styleDictionary[item];
if (style && isValue(style[property])) {
return style[property];
}
} else if (isValue(item[property])) { // style-overrides-object
return item[property];
}
}
}
return this.defaultStyle && this.defaultStyle[property];
}
/**
* @param {object} item
* @param {StyleContextStack} styleContextStack
* @param {string} property
* @param {any} defaultValue
* @returns {any}
*/
static getStyleProperty(item, styleContextStack, property, defaultValue) {
let value;
if (isValue(item[property])) { // item defines this property
return item[property];
}
if (!styleContextStack) {
return defaultValue;
}
styleContextStack.auto(item, () => {
value = styleContextStack.getProperty(property);
});
return isValue(value) ? value : defaultValue;
}
/**
* @param {object} source
* @param {object} destination
* @returns {object}
*/
static copyStyle(source = {}, destination = {}) {
// TODO: default style to source
for (let key in source) {
if (key != 'text' && source.hasOwnProperty(key)) {
destination[key] = source[key];
}
}
return destination;
}
}
export default StyleContextStack;