Skip to content

Commit 293b154

Browse files
committed
fix: indexof issue
1 parent 0579932 commit 293b154

2 files changed

Lines changed: 44 additions & 24 deletions

File tree

packages/settings/styles/src/components/classNamesContainer/index.vue

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ import { useProperties, useCanvas, useHistory, useHelp } from '@opentiny/tiny-en
109109
import { LinkButton } from '@opentiny/tiny-engine-common'
110110
import { CodeConfigurator } from '@opentiny/tiny-engine-configurator'
111111
import useStyle, { updateGlobalStyleStr } from '../../js/useStyle'
112-
import { stringify, getSelectorArr, parser } from '../../js/parser'
112+
import { stringify, getSelectorArr, buildCssObjectFromContent } from '../../js/parser'
113113
114114
const { getSchema, propsUpdateKey, setProp } = useProperties()
115115
@@ -448,29 +448,7 @@ watchEffect(() => {
448448
})
449449
450450
const save = ({ content }) => {
451-
const { parseList, styleObject } = parser(content)
452-
const cssObject = {}
453-
454-
// 保证存入 cssObject 的键值顺序与编辑器中的源码字符顺序一致
455-
parseList.forEach((item) => {
456-
// parser 中的 handleRules 没有给普通 rule 赋 type 属性,只具备 selectors 和 style
457-
if (!item.type && item.selectors) {
458-
if (styleObject[item.selectors]) {
459-
cssObject[item.selectors] = styleObject[item.selectors].rules
460-
}
461-
} else if (item.type === 'atrule') {
462-
const rawValue = item.style?.value || ''
463-
const braceIdx = rawValue.indexOf('{')
464-
const key = braceIdx !== -1 ? rawValue.slice(0, braceIdx).trim() : rawValue.trim()
465-
const value = braceIdx !== -1 ? rawValue.slice(braceIdx).trim() : ''
466-
467-
if (cssObject[key] !== undefined) {
468-
cssObject[key] = Array.isArray(cssObject[key]) ? [...cssObject[key], value] : [cssObject[key], value]
469-
} else {
470-
cssObject[key] = value
471-
}
472-
}
473-
})
451+
const cssObject = buildCssObjectFromContent(content)
474452
475453
const { addHistory } = useHistory()
476454
const { updateRect } = useCanvas().canvasApi.value

packages/settings/styles/src/js/parser.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ const handleAtRules = (node: any) => {
4242

4343
return {
4444
type,
45+
hasBlock: node.nodes !== undefined,
4546
style: {
4647
type,
4748
value: rawString
@@ -143,6 +144,47 @@ export const parser = (css: string) => {
143144
}
144145
}
145146

147+
/**
148+
* 根据编辑器 css 内容生成对象,保留源码顺序并支持 at-rule
149+
* @param {string} content
150+
* @returns {Record<string, any>}
151+
*/
152+
export const buildCssObjectFromContent = (content: string) => {
153+
const { parseList, styleObject } = parser(content)
154+
const cssObject = {}
155+
156+
// 保证存入 cssObject 的键值顺序与编辑器中的源码字符顺序一致
157+
parseList.forEach((item) => {
158+
// parser 中的 handleRules 没有给普通 rule 赋 type 属性,只具备 selectors 和 style
159+
if (!item.type && item.selectors) {
160+
if (styleObject[item.selectors]) {
161+
cssObject[item.selectors] = styleObject[item.selectors].rules
162+
}
163+
} else if (item.type === 'atrule') {
164+
const rawValue = item.style?.value || ''
165+
let key = ''
166+
let value = ''
167+
168+
if (item.hasBlock) {
169+
const braceIdx = rawValue.indexOf('{')
170+
key = braceIdx !== -1 ? rawValue.slice(0, braceIdx).trim() : rawValue.trim()
171+
value = braceIdx !== -1 ? rawValue.slice(braceIdx).trim() : ''
172+
} else {
173+
key = rawValue.trim()
174+
value = ''
175+
}
176+
177+
if (cssObject[key] !== undefined) {
178+
cssObject[key] = Array.isArray(cssObject[key]) ? [...cssObject[key], value] : [cssObject[key], value]
179+
} else {
180+
cssObject[key] = value
181+
}
182+
}
183+
})
184+
185+
return cssObject
186+
}
187+
146188
/**
147189
* 拿到组合选择器的数组,比如 .test1.test2 得到 ['.test1', '.test2']
148190
* @param {string} selector

0 commit comments

Comments
 (0)