* Copyright (c) 2023 - present TinyEngine Authors.
* Copyright (c) 2023 - present Huawei Cloud Computing Technologies Co., Ltd.
*
* Use of this source code is governed by an MIT-style license.
*
* THE OPEN SOURCE SOFTWARE IN THIS PRODUCT IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL,
* BUT WITHOUT ANY WARRANTY, WITHOUT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR FITNESS FOR
* A PARTICULAR PURPOSE. SEE THE APPLICABLE LICENSES FOR MORE DETAILS.
*
*/
import { reactive, watch } from 'vue'
import { useHistory, useCanvas } from '@opentiny/tiny-engine-meta-register'
import { obj2StyleStr, styleStrRemoveRoot } from './cssConvert'
import { CSS_TYPE } from './cssType'
export default ({ style, pageState }) => {
const { addHistory } = useHistory()
const editor = reactive({
type: '',
show: false,
created: false,
content: ''
})
const close = () => {
editor.show = false
}
const open = (type = CSS_TYPE.Style) => {
if (!editor.created) {
editor.created = true
}
editor.show = true
editor.type = type
if (type === CSS_TYPE.Style) {
editor.content = obj2StyleStr(style.value)
} else if (type === CSS_TYPE.Css) {
editor.content = pageState.pageSchema.css || ''
}
}
const save = (content) => {
if (editor.type === CSS_TYPE.Style) {
if (pageState.currentSchema?.props) {
pageState.currentSchema.props.style = styleStrRemoveRoot(content)
addHistory()
}
} else if (editor.type === CSS_TYPE.Css) {
const { updateSchema } = useCanvas()
updateSchema({ css: content })
addHistory()
}
}
watch(
() => style.value,
() => {
if (editor.show && editor.type === CSS_TYPE.Style) {
editor.content = obj2StyleStr(style.value)
}
}
)
return {
editor,
open,
save,
close
}
}