Skip to content

Commit 556e1f5

Browse files
committed
feat/schema-css-object
1 parent 800eb39 commit 556e1f5

6 files changed

Lines changed: 18 additions & 40 deletions

File tree

packages/canvas/render/src/material-function/page-getter.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import RenderMain from '../RenderMain'
44
import { handleScopedCss } from './handle-scoped-css'
55
import { utils } from '@opentiny/tiny-engine-utils'
66

7-
const { obJectCssToString } = utils
7+
const { objectCssToString } = utils
88
const pageSchema: Record<string, any> = {}
99

1010
async function fetchPageSchema(pageId: string) {
@@ -34,7 +34,7 @@ export function initStyle(key: string, content: string | object) {
3434
document.head.appendChild(styleSheet)
3535
}
3636

37-
handleScopedCss(key, obJectCssToString(content)).then((scopedCss) => {
37+
handleScopedCss(key, objectCssToString(content)).then((scopedCss) => {
3838
styleSheet.textContent = scopedCss.css
3939
})
4040
}

packages/plugins/page/src/composable/usePage.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,14 +115,14 @@ export interface MaterialsOptions {
115115
}
116116

117117
const generateCssString = (pageOptions: PageOptions, materialsOptions: MaterialsOptions) => {
118-
let cssObject: any = {}
118+
let cssObject: Record<string, any> = {}
119119
const parseStyle = (styleString: string) => {
120-
const styleObj: any = {}
120+
const styleObj: Record<string, string> = {}
121121
const styleItems = styleString.split(';')
122122
styleItems.forEach((item: string) => {
123123
if (item) {
124124
const stylekeyValue = item.split(':')
125-
styleObj[stylekeyValue[0]] = stylekeyValue[1].trim()
125+
styleObj[stylekeyValue[0].trim()] = stylekeyValue[1].trim()
126126
}
127127
})
128128
return styleObj

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

Lines changed: 7 additions & 29 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 } from '../../js/parser'
112+
import { stringify, getSelectorArr, parser } from '../../js/parser'
113113
114114
const { getSchema, propsUpdateKey, setProp } = useProperties()
115115
@@ -447,39 +447,17 @@ watchEffect(() => {
447447
selectorValidator(classNameState.newSelector)
448448
})
449449
450-
const cssStringToObject = (cssString) => {
451-
cssString = cssString.replace(/\s+/g, ' ').trim()
452-
const result = {}
453-
const regex = /([^{]+)\{([^}]+)\}/g
454-
455-
let match
456-
while ((match = regex.exec(cssString)) !== null) {
457-
const selector = match[1].trim()
458-
const properties = match[2].trim()
459-
460-
const propertiesObj = {}
461-
462-
const propertyRegex = /([^{;]+):([^;]+)/g
463-
let propertyMatch
464-
465-
while ((propertyMatch = propertyRegex.exec(properties)) !== null) {
466-
const key = propertyMatch[1].trim()
467-
const value = propertyMatch[2].trim()
468-
propertiesObj[key] = value
469-
}
470-
471-
result[selector] = propertiesObj
472-
}
473-
return result
474-
}
475-
476450
const save = ({ content }) => {
477-
const cssString = cssStringToObject(content)
451+
const { styleObject } = parser(content)
452+
const cssObject = {}
453+
for (const styleKey in styleObject) {
454+
cssObject[styleKey] = styleObject[styleKey].rules
455+
}
478456
const { addHistory } = useHistory()
479457
const { updateRect } = useCanvas().canvasApi.value
480458
const { updateSchema } = useCanvas()
481459
482-
updateSchema({ css: cssString })
460+
updateSchema({ css: cssObject })
483461
state.schemaUpdateKey++
484462
addHistory()
485463
updateRect()

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import { constants, utils } from '@opentiny/tiny-engine-utils'
1818
import { parser, stringify, getSelectorArr } from './parser'
1919

2020
const { EXPRESSION_TYPE } = constants
21-
const { generateRandomLetters, parseExpression, obJectCssToString } = utils
21+
const { generateRandomLetters, parseExpression, objectCssToString } = utils
2222

2323
const state = reactive({
2424
// 当前选中节点的 style,解析成对象返回
@@ -177,7 +177,7 @@ export const initStylePanelWatch = () => {
177177
watch(
178178
() => useCanvas().getPageSchema?.()?.css,
179179
(value) => {
180-
state.cssContent = obJectCssToString(value) || ''
180+
state.cssContent = objectCssToString(value) || ''
181181

182182
// 解析css
183183
const { parseList, selectors, styleObject } = parser(state.cssContent)

packages/utils/src/utils/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -455,11 +455,11 @@ export const obj2StyleString = (obj: any) => {
455455

456456
/**
457457
* JSON转样式字符串
458-
* @param {*} string
458+
* @param {*} object
459459
* @returns
460460
*/
461461

462-
export const obJectCssToString = (css) => {
462+
export const objectCssToString = (css) => {
463463
if (typeof css === 'string') {
464464
return css
465465
}

packages/vue-generator/src/generator/vue/sfc/generateStyle.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { utils } from '@opentiny/tiny-engine-utils'
22

3-
const { obJectCssToString } = utils
3+
const { objectCssToString } = utils
44
export const generateStyleTag = (schema, config = {}) => {
55
const { css } = schema
66
const { scoped = true, lang = '' } = config
@@ -15,6 +15,6 @@ export const generateStyleTag = (schema, config = {}) => {
1515
if (lang) {
1616
langDesc = `lang=${langDesc}`
1717
}
18-
const cssString = obJectCssToString(css)
18+
const cssString = objectCssToString(css)
1919
return `<style ${langDesc} ${scopedStr}> ${cssString || ''} </style>`
2020
}

0 commit comments

Comments
 (0)