Skip to content

Commit 2439ab2

Browse files
author
shangbin
committed
支持暴露JS代码以及通过JS代码初始化逻辑
1 parent 52aeaaa commit 2439ab2

File tree

5 files changed

+79
-19
lines changed

5 files changed

+79
-19
lines changed

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/App.vue

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,43 @@
11
<template>
2-
<vcc :initCodeEntity="codeStructure" @updateCodeEntity="onCodeUpdate"></vcc>
2+
<vcc :initCodeEntity="codeInfoEntity" @updateCodeEntity="onCodeUpdate"></vcc>
33
</template>
44

55
<script>
66
// 以这样一段结构初始化VCC组件
7-
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"}}]}}]}}'
7+
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"}}]}}]}}'
88
99
export default {
1010
components: {
1111
vcc: () => import('./components-v2/VCC.vue'),
1212
},
1313
data() {
1414
return {
15-
codeStructure: JSON.parse(initCodeStr),
15+
codeInfoEntity: {
16+
codeStructure: JSON.parse(initCodeStr),
17+
JSCode: `
18+
{
19+
data() {
20+
return {
21+
showValue: "开启预览模式后,点击我显示预设逻辑",
22+
showText: "这里的值声明于预设JS代码"
23+
};
24+
},
25+
methods: {
26+
hello() {
27+
alert("来自预设逻辑代码的问候");
28+
},
29+
},
30+
}`
31+
},
1632
}
1733
},
1834
mounted() {
1935
},
2036
methods: {
21-
onCodeUpdate(newCodeEntity) {
37+
onCodeUpdate({ codeRawVueInfo, JSCode }) {
2238
// 编辑后新的代码结构
39+
// codeRawVueInfo为template对象表示结构
40+
// JSCode为显式输入的JS逻辑
2341
}
2442
}
2543
}

src/components-v2/VCC.vue

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
</div>
2424

2525
<div class="copy">
26-
<el-link :underline="false" href="https://vcc3.sahadev.tech/" style="color: red; margin-right: 10px;" class="animate__animated animate__headShake animate__infinite">
26+
<el-link :underline="false" href="https://vcc3.sahadev.tech/" style="color: red; margin-right: 10px;"
27+
class="animate__animated animate__headShake animate__infinite">
2728
👉🏻 尝试拥有更多组件库的Vue3版本</el-link>
2829
<div>
2930
<el-alert title="遇到问题?" type="info">
@@ -54,7 +55,7 @@
5455
<code-structure @save="onSaveAttr" @remove="onRemove" ref="codeStructure" :visible.sync="structureVisible"
5556
@codeRefresh="generateVueCode" @onLevelChange="onLevelChange">
5657
</code-structure>
57-
<CodeEditor :codeDialogVisible.sync="jsDialogVisible" @saveJSCode="saveJSCode"></CodeEditor>
58+
<CodeEditor :codeDialogVisible.sync="jsDialogVisible" @saveJSCode="saveJSCode" ref="codeEditor"></CodeEditor>
5859
<VueEditor :vueDialogVisible.sync="vueDialogVisible" @codeParseSucess="codeParseSucess"></VueEditor>
5960
</div>
6061

@@ -96,7 +97,10 @@ export default {
9697
iconCode: ("https://static.imonkey.xueersi.com/download/vcc-resource/icon/code-working-outline.svg"),
9798
iconClear: ("https://static.imonkey.xueersi.com/download/vcc-resource/icon/trash-outline.svg"),
9899
99-
viewMode: false
100+
viewMode: false,
101+
102+
codeRawVueInfo: "",
103+
JSCode: ""
100104
};
101105
},
102106
watch: {
@@ -111,8 +115,12 @@ export default {
111115
}
112116
},
113117
initCodeEntity(newVal) {
114-
if (newVal) {
115-
this.mainPanelProvider.render(newVal);
118+
if (newVal.JSCode) {
119+
this.mainPanelProvider.saveJSCode(this.convertLogicCode(newVal.JSCode));
120+
}
121+
122+
if (newVal.codeStructure) {
123+
this.mainPanelProvider.render(newVal.codeStructure);
116124
}
117125
}
118126
},
@@ -135,6 +143,17 @@ export default {
135143
updated() { },
136144
destoryed() { },
137145
methods: {
146+
convertLogicCode(JSCode) {
147+
try {
148+
const JSCodeInfo = eval(`(function(){return ${JSCode.replace(/\s+/g, "")}})()`);
149+
// 保留JS代码
150+
this.JSCode = JSCode;
151+
this.$refs.codeEditor.updateLogicCode(JSCode);
152+
return JSCodeInfo;
153+
} catch (e) {
154+
console.warn(`外部逻辑代码解析出错,解析的逻辑代码为: ${JSCode}, Error: ${e}`);
155+
}
156+
},
138157
139158
initShortcut() {
140159
keymaster('⌘+z, ctrl+z', () => {
@@ -163,12 +182,23 @@ export default {
163182
if (this.$refs.codeStructure) {
164183
this.$refs.codeStructure.updateCode(codeRawVueInfo);
165184
}
166-
this.$emit('updateCodeEntity', codeRawVueInfo);
185+
this.codeRawVueInfo = codeRawVueInfo;
186+
187+
this.notifyParent();
167188
}).onNodeDeleted(() => {
168189
this.currentEditRawInfo = null;
169190
}).onSelectElement(rawInfo => {
170191
this.currentEditRawInfo = rawInfo;
171-
}).render(this.initCodeEntity ? this.initCodeEntity : this.getFakeData());
192+
}).saveJSCodeOnly(this.convertLogicCode(this.initCodeEntity.JSCode))
193+
.render(this.initCodeEntity.codeStructure ? this.initCodeEntity.codeStructure : this.getFakeData());
194+
},
195+
196+
// 通知父组件
197+
notifyParent() {
198+
this.$emit('updateCodeEntity', {
199+
codeRawVueInfo: this.codeRawVueInfo,
200+
JSCode: this.JSCode
201+
});
172202
},
173203
174204
// 指向将要插入哪个元素之前
@@ -238,8 +268,11 @@ export default {
238268
this.mainPanelProvider.undo();
239269
},
240270
241-
saveJSCode(code) {
271+
saveJSCode({ JSCodeInfo: code, JSCode }) {
242272
this.mainPanelProvider.saveJSCode(code);
273+
// 保留JS代码
274+
this.JSCode = JSCode;
275+
this.notifyParent();
243276
},
244277
245278
codeParseSucess(vueCodeEntity) {

src/components/JSCodeEditorDialog.vue

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,21 +83,25 @@ export default {
8383
updated() { },
8484
destoryed() { },
8585
methods: {
86-
// 在此自动生成
87-
request() {
88-
// 网络请求,可选
86+
updateLogicCode(newCode) {
87+
if (newCode) {
88+
this.code = newCode;
89+
}
8990
},
9091
handleClose() {
9192
this.$emit("update:codeDialogVisible", false);
9293
},
9394
onSave() {
9495
const code = this.$refs.codeEditor.getEditorCode();
9596
// 去掉注释
96-
const temp = code.replace(/.+\*\/\s*/gs, "");
97+
const temp = code.replace(/.+\*\/\s*/gs, "").replace(/\s+/g, "");
9798
try {
9899
// 转换为对象
99100
const JSCodeInfo = eval(`(function(){return ${temp}})()`);
100-
this.$emit("saveJSCode", JSCodeInfo);
101+
this.$emit("saveJSCode", {
102+
JSCodeInfo,
103+
JSCode: temp
104+
});
101105
this.handleClose();
102106
this.error = '';
103107
} catch (error) {

src/libs/main-panel.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,11 +129,16 @@ export class MainPanelProvider {
129129
}
130130

131131
saveJSCode(code) {
132-
this.externalJS = code;
132+
this.saveJSCodeOnly(code);
133133
this.codeGenerator.setExternalJS(code);
134134
this.reRender();
135135
}
136136

137+
saveJSCodeOnly(code) {
138+
this.externalJS = code || {};
139+
return this;
140+
}
141+
137142
/**
138143
* 生成一个新的待挂载容器
139144
*/

0 commit comments

Comments
 (0)