-
-
Notifications
You must be signed in to change notification settings - Fork 389
Expand file tree
/
Copy pathjss-plugin-extend.tsx
More file actions
172 lines (124 loc) · 6.45 KB
/
jss-plugin-extend.tsx
File metadata and controls
172 lines (124 loc) · 6.45 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
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
// jss (builds css using javascript):
import type {
Plugin,
JssStyle,
Rule,
StyleSheet,
} from 'jss' // base technology of our nodestrap components
// others:
import warning from 'tiny-warning'
// utilities:
type LiteralObject = { [key: string]: any }
const isLiteralObject = (object: any): object is LiteralObject => object && (typeof(object) === 'object') && !Array.isArray(object);
// upgrade `JssStyle` definition:
type Optional<T> = T|null|undefined
type ExtendableObject = JssStyle|string // extend using a JssStyle object or using a rule name
type SingleExtend = Optional<ExtendableObject>
type Extend = SingleExtend|SingleExtend[]
type Style = JssStyle & { extend?: Optional<Extend> } // add `extend` prop into `JssStyle`
// export the upgraded `JssStyle`:
export type { Style, Style as ExtendableStyle, Style as JssStyle }
const isStyle = (object: any): object is Style => isLiteralObject(object);
const mergeExtend = (style: Style, rule?: Rule, sheet?: StyleSheet): void => {
const extend = style.extend;
if (!extend) return; // nothing to extend
// if `extend` is an `Array` => loop it
// otherwise => convert to single `Array` => loop it
for (const singleExtend of (Array.isArray(extend) ? extend : [extend])) {
if (!singleExtend) continue; // null & undefined => skip
//#region extend using a `Style`
if (isStyle(singleExtend)) {
mergeStyle(style, singleExtend, rule, sheet);
} // if
//#endregion extend using a `Style`
//#region extend using a rule name
else if (typeof(singleExtend) === 'string') {
if (sheet) {
const refRule = sheet.getRule(singleExtend) as Optional<Rule>;
if (refRule) {
if (refRule === rule) {
warning(false, `[JSS] A rule tries to extend itself \n${rule.toString()}`);
// TODO: detect circular ref, causing infinite recursive
}
else {
// now it seems the `refRule` is not `rule` nor circular ref
// warning: calling `mergeStyle` might causing infinite recursive if the `refRule` is `rule` or circular ref
const ruleStyle = (refRule.options?.parent as any)?.rules?.raw?.[singleExtend] as Optional<Style>;
if (ruleStyle) {
mergeStyle(style, ruleStyle, rule, sheet);
} // if
} // if
} // if
} // if
} // if
//#endregion extend using a rule name
} // for
// the `extend` operation has been completed => remove unused `extend` prop:
// delete style.extend;
style.extend = null; // maybe it's safer not to remove the `style`'s prop, instead set it to `null`
}
const mergeLiteral = (style: Style & LiteralObject, newStyle: Style, rule?: Rule, sheet?: StyleSheet): void => {
for (const [name, newValue] of Object.entries(newStyle)) { // loop through `newStyle`'s props
// `extend` is a special prop that we don't handle here:
if (name === 'extend') continue; // skip `extend` prop
if (!isStyle(newValue)) {
// `newValue` is not a `Style` => unmergeable => add/overwrite `newValue` into `style`:
style[name] = newValue; // add/overwrite
}
else {
// `newValue` is a `Style` => possibility to merge with `currentValue`
const currentValue = style[name];
if (!isStyle(currentValue)) {
// `currentValue` is not a `Style` => unmergeable => add/overwrite `newValue` into `style`:
style[name] = newValue; // add/overwrite
}
else {
// both `newValue` & `currentValue` are `Style` => merge them recursively (deeply):
const currentValueClone = {...currentValue} as Style; // clone the `currentValue` to avoid side effect, because the `currentValue` is not **the primary object** we're working on
mergeStyle(currentValueClone, newValue, rule, sheet);
style[name] = currentValueClone; // set the mutated `currentValueClone` back to `style`
} // if
} // if
} // for
}
// we export `mergeStyle` so it reusable:
export const mergeStyle = (style: Style, newStyle: Style, rule?: Rule, sheet?: StyleSheet): void => {
const newStyleClone = {...newStyle} as Style; // clone the `newStyle` to avoid side effect, because the `newStyle` is not **the primary object** we're working on
mergeExtend(newStyleClone, rule, sheet);
mergeLiteral(style, newStyleClone, rule, sheet);
}
export default function pluginExtend(): Plugin { return {
onProcessStyle: (style: Style, rule: Rule, sheet?: StyleSheet): Style => {
mergeExtend(style, rule, sheet);
return style;
},
onChangeValue: (value: any, prop: string, rule: Rule): string|null|false => {
if (prop !== 'extend') return value; // do not modify any props other than `extend`
const __prevObject = '__prevObject';
if (typeof(value) === 'object') {
const ruleProp = (rule as any).prop;
if (typeof(ruleProp) === 'function') {
for (const [propName, propVal] of Object.entries(value)) {
ruleProp(propName, propVal);
} // for
// store the object to the rule, so we can remove all props we've set later:
(rule as any)[__prevObject] = value;
} // if
}
else if ((value === null) || (value === false)) {
// remove all props we've set before (if any):
const prevObject = (rule as any)[__prevObject];
if (prevObject) {
const ruleProp = (rule as any).prop;
if (typeof(ruleProp) === 'function') {
for (const propName of Object.keys(prevObject)) {
ruleProp(propName, null);
} // for
} // if
// clear the stored object:
delete (rule as any)[__prevObject];
} // if
} // if
return null; // do not set the value in the core
},
}}