Skip to content

Commit 2d8a238

Browse files
WangXinhaiPanJiaChen
authored andcommitted
perf[ThemePicker]: refine updateStyle function (#554)
* theme replacing should cut tons of irrelevant css
1 parent 30d4d85 commit 2d8a238

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

src/components/ThemePicker/index.vue

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,18 @@ export default {
6767
6868
methods: {
6969
updateStyle(style, oldCluster, newCluster) {
70-
let newStyle = style
70+
const colorOverrides = [] // only capture color overides
7171
oldCluster.forEach((color, index) => {
72-
newStyle = newStyle.replace(new RegExp(color, 'ig'), newCluster[index])
72+
const value = newCluster[index]
73+
const color_plain = color.replace(/([()])/g, '\\$1')
74+
const repl = new RegExp(`(^|})([^{]+{[^{}]+)${color_plain}\\b([^}]*)(?=})`, 'gi')
75+
const nestRepl = new RegExp(color_plain, 'ig') // for greed matching before the 'color'
76+
let v
77+
while ((v = repl.exec(style))) {
78+
colorOverrides.push(v[2].replace(nestRepl, value) + value + v[3] + '}') // '}' not captured in the regexp repl to reserve it as locator-boundary
79+
}
7380
})
74-
return newStyle
81+
return colorOverrides.join('')
7582
},
7683
7784
getCSSString(url, callback, variable) {

0 commit comments

Comments
 (0)