1
0
mirror of https://github.com/sahadev/vue-component-creater-ui.git synced 2025-06-08 06:16:24 +08:00

支持暴露JS代码以及通过JS代码初始化逻辑

This commit is contained in:
shangbin 2022-01-10 20:42:24 +08:00
parent 52aeaaafeb
commit 2439ab2678
5 changed files with 79 additions and 19 deletions

2
package-lock.json generated
View File

@ -12684,7 +12684,7 @@
}, },
"vue-loader-v16": { "vue-loader-v16": {
"version": "npm:vue-loader@16.8.3", "version": "npm:vue-loader@16.8.3",
"resolved": "https://rg.cnpmjs.org/vue-loader/download/vue-loader-16.8.3.tgz", "resolved": "https://r.cnpmjs.org/vue-loader/download/vue-loader-16.8.3.tgz",
"integrity": "sha1-1D5nXe9bqTRdbH8FkUwT2GGZcIc=", "integrity": "sha1-1D5nXe9bqTRdbH8FkUwT2GGZcIc=",
"dev": true, "dev": true,
"optional": true, "optional": true,

View File

@ -1,10 +1,10 @@
<template> <template>
<vcc :initCodeEntity="codeStructure" @updateCodeEntity="onCodeUpdate"></vcc> <vcc :initCodeEntity="codeInfoEntity" @updateCodeEntity="onCodeUpdate"></vcc>
</template> </template>
<script> <script>
// VCC // VCC
const initCodeStr = '{"template":{"lc_id":"root","__children":[{"div":{"class":"container","style":"min-height: 100%; padding-bottom: 100px;","lc_id":"container","__text__":"Hello欢迎使用LCG请往此区域拖拽组件","__children":[{"el-button":{"lc-mark":"","type":"danger","lc_id":"COAAYXizyI","__children":[],"__text__":"危险按钮","@click":"onButtonClick","size":"small"}}]}}]}}' const initCodeStr = '{"template":{"lc_id":"root","__children":[{"div":{"class":"container","style":"min-height: 100%; padding-bottom: 100px;","lc_id":"container","__text__":"Hello欢迎使用LCG请往此区域拖拽组件","__children":[{"div": {"__text__": "{{showText}}", "lc_id": "text"}},{"el-button":{"lc-mark":"","type":"danger","lc_id":"COAAYXizyI","__children":[],"__text__":"{{showValue}}","@click":"hello","size":"small"}}]}}]}}'
export default { export default {
components: { components: {
@ -12,14 +12,32 @@ export default {
}, },
data() { data() {
return { return {
codeStructure: JSON.parse(initCodeStr), codeInfoEntity: {
codeStructure: JSON.parse(initCodeStr),
JSCode: `
{
data() {
return {
showValue: "开启预览模式后,点击我显示预设逻辑",
showText: "这里的值声明于预设JS代码"
};
},
methods: {
hello() {
alert("来自预设逻辑代码的问候");
},
},
}`
},
} }
}, },
mounted() { mounted() {
}, },
methods: { methods: {
onCodeUpdate(newCodeEntity) { onCodeUpdate({ codeRawVueInfo, JSCode }) {
// //
// codeRawVueInfotemplate
// JSCodeJS
} }
} }
} }

View File

@ -23,7 +23,8 @@
</div> </div>
<div class="copy"> <div class="copy">
<el-link :underline="false" href="https://vcc3.sahadev.tech/" style="color: red; margin-right: 10px;" class="animate__animated animate__headShake animate__infinite"> <el-link :underline="false" href="https://vcc3.sahadev.tech/" style="color: red; margin-right: 10px;"
class="animate__animated animate__headShake animate__infinite">
👉🏻 尝试拥有更多组件库的Vue3版本</el-link> 👉🏻 尝试拥有更多组件库的Vue3版本</el-link>
<div> <div>
<el-alert title="遇到问题?" type="info"> <el-alert title="遇到问题?" type="info">
@ -54,7 +55,7 @@
<code-structure @save="onSaveAttr" @remove="onRemove" ref="codeStructure" :visible.sync="structureVisible" <code-structure @save="onSaveAttr" @remove="onRemove" ref="codeStructure" :visible.sync="structureVisible"
@codeRefresh="generateVueCode" @onLevelChange="onLevelChange"> @codeRefresh="generateVueCode" @onLevelChange="onLevelChange">
</code-structure> </code-structure>
<CodeEditor :codeDialogVisible.sync="jsDialogVisible" @saveJSCode="saveJSCode"></CodeEditor> <CodeEditor :codeDialogVisible.sync="jsDialogVisible" @saveJSCode="saveJSCode" ref="codeEditor"></CodeEditor>
<VueEditor :vueDialogVisible.sync="vueDialogVisible" @codeParseSucess="codeParseSucess"></VueEditor> <VueEditor :vueDialogVisible.sync="vueDialogVisible" @codeParseSucess="codeParseSucess"></VueEditor>
</div> </div>
@ -96,7 +97,10 @@ export default {
iconCode: ("https://static.imonkey.xueersi.com/download/vcc-resource/icon/code-working-outline.svg"), iconCode: ("https://static.imonkey.xueersi.com/download/vcc-resource/icon/code-working-outline.svg"),
iconClear: ("https://static.imonkey.xueersi.com/download/vcc-resource/icon/trash-outline.svg"), iconClear: ("https://static.imonkey.xueersi.com/download/vcc-resource/icon/trash-outline.svg"),
viewMode: false viewMode: false,
codeRawVueInfo: "",
JSCode: ""
}; };
}, },
watch: { watch: {
@ -111,8 +115,12 @@ export default {
} }
}, },
initCodeEntity(newVal) { initCodeEntity(newVal) {
if (newVal) { if (newVal.JSCode) {
this.mainPanelProvider.render(newVal); this.mainPanelProvider.saveJSCode(this.convertLogicCode(newVal.JSCode));
}
if (newVal.codeStructure) {
this.mainPanelProvider.render(newVal.codeStructure);
} }
} }
}, },
@ -135,6 +143,17 @@ export default {
updated() { }, updated() { },
destoryed() { }, destoryed() { },
methods: { methods: {
convertLogicCode(JSCode) {
try {
const JSCodeInfo = eval(`(function(){return ${JSCode.replace(/\s+/g, "")}})()`);
// JS
this.JSCode = JSCode;
this.$refs.codeEditor.updateLogicCode(JSCode);
return JSCodeInfo;
} catch (e) {
console.warn(`外部逻辑代码解析出错,解析的逻辑代码为: ${JSCode}, Error: ${e}`);
}
},
initShortcut() { initShortcut() {
keymaster('⌘+z, ctrl+z', () => { keymaster('⌘+z, ctrl+z', () => {
@ -163,12 +182,23 @@ export default {
if (this.$refs.codeStructure) { if (this.$refs.codeStructure) {
this.$refs.codeStructure.updateCode(codeRawVueInfo); this.$refs.codeStructure.updateCode(codeRawVueInfo);
} }
this.$emit('updateCodeEntity', codeRawVueInfo); this.codeRawVueInfo = codeRawVueInfo;
this.notifyParent();
}).onNodeDeleted(() => { }).onNodeDeleted(() => {
this.currentEditRawInfo = null; this.currentEditRawInfo = null;
}).onSelectElement(rawInfo => { }).onSelectElement(rawInfo => {
this.currentEditRawInfo = rawInfo; this.currentEditRawInfo = rawInfo;
}).render(this.initCodeEntity ? this.initCodeEntity : this.getFakeData()); }).saveJSCodeOnly(this.convertLogicCode(this.initCodeEntity.JSCode))
.render(this.initCodeEntity.codeStructure ? this.initCodeEntity.codeStructure : this.getFakeData());
},
//
notifyParent() {
this.$emit('updateCodeEntity', {
codeRawVueInfo: this.codeRawVueInfo,
JSCode: this.JSCode
});
}, },
// //
@ -238,8 +268,11 @@ export default {
this.mainPanelProvider.undo(); this.mainPanelProvider.undo();
}, },
saveJSCode(code) { saveJSCode({ JSCodeInfo: code, JSCode }) {
this.mainPanelProvider.saveJSCode(code); this.mainPanelProvider.saveJSCode(code);
// JS
this.JSCode = JSCode;
this.notifyParent();
}, },
codeParseSucess(vueCodeEntity) { codeParseSucess(vueCodeEntity) {

View File

@ -83,9 +83,10 @@ export default {
updated() { }, updated() { },
destoryed() { }, destoryed() { },
methods: { methods: {
// updateLogicCode(newCode) {
request() { if (newCode) {
// this.code = newCode;
}
}, },
handleClose() { handleClose() {
this.$emit("update:codeDialogVisible", false); this.$emit("update:codeDialogVisible", false);
@ -93,11 +94,14 @@ export default {
onSave() { onSave() {
const code = this.$refs.codeEditor.getEditorCode(); const code = this.$refs.codeEditor.getEditorCode();
// //
const temp = code.replace(/.+\*\/\s*/gs, ""); const temp = code.replace(/.+\*\/\s*/gs, "").replace(/\s+/g, "");
try { try {
// //
const JSCodeInfo = eval(`(function(){return ${temp}})()`); const JSCodeInfo = eval(`(function(){return ${temp}})()`);
this.$emit("saveJSCode", JSCodeInfo); this.$emit("saveJSCode", {
JSCodeInfo,
JSCode: temp
});
this.handleClose(); this.handleClose();
this.error = ''; this.error = '';
} catch (error) { } catch (error) {

View File

@ -129,11 +129,16 @@ export class MainPanelProvider {
} }
saveJSCode(code) { saveJSCode(code) {
this.externalJS = code; this.saveJSCodeOnly(code);
this.codeGenerator.setExternalJS(code); this.codeGenerator.setExternalJS(code);
this.reRender(); this.reRender();
} }
saveJSCodeOnly(code) {
this.externalJS = code || {};
return this;
}
/** /**
* 生成一个新的待挂载容器 * 生成一个新的待挂载容器
*/ */