Skip to content

Commit a926b80

Browse files
Merge branch 'opentiny:develop' into develop
2 parents 1c11943 + 63a69a4 commit a926b80

8 files changed

Lines changed: 69 additions & 25 deletions

File tree

packages/canvas/render/src/RenderMain.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,8 @@ export default defineComponent({
172172
() => activeSchema.css,
173173
(value) => {
174174
setPageCss(value)
175-
}
175+
},
176+
{ deep: true }
176177
)
177178

178179
const utilsWatchCanceler = window.host.watch(

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ import { defineComponent, h, ref, onMounted } from 'vue'
22
import { getController } from '../canvas-function'
33
import RenderMain from '../RenderMain'
44
import { handleScopedCss } from './handle-scoped-css'
5+
import { utils } from '@opentiny/tiny-engine-utils'
56

7+
const { objectCssToString } = utils
68
const pageSchema: Record<string, any> = {}
79

810
async function fetchPageSchema(pageId: string) {
@@ -16,7 +18,7 @@ async function fetchPageSchema(pageId: string) {
1618
// tailwindcss function and directive 特性需要使用 <style type="text/tailwindcss"> 标签
1719
// https://tailwindcss.com/docs/functions-and-directives
1820
// 所以原来 new CSSStyleSheet 的方式改成了 document.createElement('style') 的方式
19-
export function initStyle(key: string, content: string) {
21+
export function initStyle(key: string, content: string | object) {
2022
if (!content) {
2123
return
2224
}
@@ -32,7 +34,7 @@ export function initStyle(key: string, content: string) {
3234
document.head.appendChild(styleSheet)
3335
}
3436

35-
handleScopedCss(key, content).then((scopedCss) => {
37+
handleScopedCss(key, objectCssToString(content)).then((scopedCss) => {
3638
styleSheet.textContent = scopedCss.css
3739
})
3840
}

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

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -115,22 +115,31 @@ export interface MaterialsOptions {
115115
}
116116

117117
const generateCssString = (pageOptions: PageOptions, materialsOptions: MaterialsOptions) => {
118-
if (!pageOptions?.pageBaseStyle?.className || !pageOptions?.pageBaseStyle?.style) {
119-
return ''
118+
let cssObject: Record<string, any> = {}
119+
const parseStyle = (styleString: string) => {
120+
const styleObj: Record<string, string> = {}
121+
const styleItems = styleString.split(';')
122+
styleItems.forEach((item: string) => {
123+
if (item) {
124+
const stylekeyValue = item.split(':')
125+
styleObj[stylekeyValue[0].trim()] = stylekeyValue[1].trim()
126+
}
127+
})
128+
return styleObj
129+
}
130+
if (pageOptions?.pageBaseStyle?.className && pageOptions?.pageBaseStyle?.style) {
131+
cssObject[`.${pageOptions.pageBaseStyle.className}`] = parseStyle(pageOptions.pageBaseStyle.style)
120132
}
121133

122-
const formatCssRule = (className: string, style: string) => `.${className} {\n ${style.trim()}\n}\n`
123-
const baseStyle = `.${pageOptions.pageBaseStyle.className}{\r\n ${pageOptions.pageBaseStyle.style}\r\n}\r\n`
124-
125-
if (!materialsOptions.useBaseStyle) {
126-
return baseStyle
134+
if (materialsOptions.useBaseStyle) {
135+
cssObject = {
136+
...cssObject,
137+
[`.${materialsOptions.blockBaseStyle.className}`]: parseStyle(materialsOptions.blockBaseStyle.style),
138+
[`.${materialsOptions.componentBaseStyle.className}`]: parseStyle(materialsOptions.componentBaseStyle.style)
139+
}
127140
}
128141

129-
return [
130-
formatCssRule(pageOptions.pageBaseStyle.className, pageOptions.pageBaseStyle.style),
131-
formatCssRule(materialsOptions.blockBaseStyle.className, materialsOptions.blockBaseStyle.style),
132-
formatCssRule(materialsOptions.componentBaseStyle.className, materialsOptions.componentBaseStyle.style)
133-
].join('\n')
142+
return cssObject
134143
}
135144

136145
const getDefaultPage = () => {

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,8 @@ import { Select as TinySelect } from '@opentiny/vue'
108108
import { useProperties, useCanvas, useHistory, useHelp } from '@opentiny/tiny-engine-meta-register'
109109
import { LinkButton } from '@opentiny/tiny-engine-common'
110110
import { CodeConfigurator } from '@opentiny/tiny-engine-configurator'
111-
import { formatString } from '@opentiny/tiny-engine-common/js/ast'
112111
import useStyle, { updateGlobalStyleStr } from '../../js/useStyle'
113-
import { stringify, getSelectorArr } from '../../js/parser'
112+
import { stringify, getSelectorArr, parser } from '../../js/parser'
114113
115114
const { getSchema, propsUpdateKey, setProp } = useProperties()
116115
@@ -449,14 +448,17 @@ watchEffect(() => {
449448
})
450449
451450
const save = ({ content }) => {
452-
const cssString = formatString(content.replace(/"/g, "'"), 'css')
451+
const { styleObject } = parser(content)
452+
const cssObject = {}
453+
for (const styleKey in styleObject) {
454+
cssObject[styleKey] = styleObject[styleKey].rules
455+
}
453456
const { addHistory } = useHistory()
454457
const { updateRect } = useCanvas().canvasApi.value
455458
const { updateSchema } = useCanvas()
456459
457-
updateSchema({ css: cssString })
460+
updateSchema({ css: cssObject })
458461
state.schemaUpdateKey++
459-
460462
addHistory()
461463
updateRect()
462464
}

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

Lines changed: 3 additions & 3 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 } = utils
21+
const { generateRandomLetters, parseExpression, objectCssToString } = utils
2222

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

182182
// 解析css
183-
const { parseList, selectors, styleObject } = parser(value)
183+
const { parseList, selectors, styleObject } = parser(state.cssContent)
184184

185185
state.cssParseList = parseList
186186
state.selectors = selectors

packages/utils/src/utils/index.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,3 +452,29 @@ export const obj2StyleString = (obj: any) => {
452452
.join('; ')
453453
)
454454
}
455+
456+
/**
457+
* JSON转样式字符串
458+
* @param {*} object
459+
* @returns
460+
*/
461+
462+
export const objectCssToString = (css) => {
463+
if (typeof css === 'string') {
464+
return css
465+
}
466+
let cssString = ''
467+
468+
for (const selector in css) {
469+
const properties = css[selector]
470+
let ruleString = `${selector} {\r\n`
471+
472+
for (const property in properties) {
473+
ruleString += ` ${property}: ${properties[property]};\r\n`
474+
}
475+
476+
ruleString += '}\n'
477+
cssString += ruleString
478+
}
479+
return cssString
480+
}

packages/vue-generator/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
"homepage": "https://opentiny.design/tiny-engine",
3333
"dependencies": {
3434
"@opentiny/tiny-engine-builtin-component": "workspace:*",
35+
"@opentiny/tiny-engine-utils": "workspace:*",
3536
"@vue/compiler-sfc": "3.2.45",
3637
"@vue/shared": "^3.3.4",
3738
"vue": "^3.4.15",

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import { utils } from '@opentiny/tiny-engine-utils'
2+
3+
const { objectCssToString } = utils
14
export const generateStyleTag = (schema, config = {}) => {
25
const { css } = schema
36
const { scoped = true, lang = '' } = config
@@ -12,6 +15,6 @@ export const generateStyleTag = (schema, config = {}) => {
1215
if (lang) {
1316
langDesc = `lang=${langDesc}`
1417
}
15-
16-
return `<style ${langDesc} ${scopedStr}> ${css || ''} </style>`
18+
const cssString = objectCssToString(css)
19+
return `<style ${langDesc} ${scopedStr}> ${cssString || ''} </style>`
1720
}

0 commit comments

Comments
 (0)