File tree Expand file tree Collapse file tree 2 files changed +41
-4
lines changed
settings/styles/src/components/classNamesContainer Expand file tree Collapse file tree 2 files changed +41
-4
lines changed Original file line number Diff line number Diff line change @@ -448,11 +448,30 @@ watchEffect(() => {
448448})
449449
450450const save = ({ content }) => {
451- const { styleObject } = parser(content)
451+ const { parseList, styleObject } = parser(content)
452452 const cssObject = {}
453- Object.keys(styleObject).forEach((styleKey) => {
454- cssObject[styleKey] = styleObject[styleKey].rules
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+ }
455473 })
474+
456475 const { addHistory } = useHistory()
457476 const { updateRect } = useCanvas().canvasApi.value
458477 const { updateSchema } = useCanvas()
Original file line number Diff line number Diff line change @@ -464,9 +464,27 @@ export const objectCssToString = (css) => {
464464 return css
465465 }
466466 let cssString = ''
467+ let topString = ''
467468
468469 for ( const selector in css ) {
469470 const properties = css [ selector ]
471+
472+ if ( typeof properties === 'string' || ( Array . isArray ( properties ) && typeof properties [ 0 ] === 'string' ) ) {
473+ const isTopRule =
474+ selector . startsWith ( '@charset' ) || selector . startsWith ( '@import' ) || selector . startsWith ( '@namespace' )
475+ const propsArray = Array . isArray ( properties ) ? properties : [ properties ]
476+
477+ propsArray . forEach ( ( prop ) => {
478+ const line = prop ? `${ selector } ${ prop } \n` : `${ selector } \n`
479+ if ( isTopRule ) {
480+ topString += line
481+ } else {
482+ cssString += line
483+ }
484+ } )
485+ continue
486+ }
487+
470488 let ruleString = `${ selector } {\r\n`
471489
472490 for ( const property in properties ) {
@@ -476,5 +494,5 @@ export const objectCssToString = (css) => {
476494 ruleString += '}\n'
477495 cssString += ruleString
478496 }
479- return cssString
497+ return topString + cssString
480498}
You can’t perform that action at this time.
0 commit comments