diff --git a/packages/plugins/script/src/js/method.js b/packages/plugins/script/src/js/method.js index 6c9d6a975..740c2cd86 100644 --- a/packages/plugins/script/src/js/method.js +++ b/packages/plugins/script/src/js/method.js @@ -11,7 +11,7 @@ */ import { ref, reactive, watchEffect, onActivated, nextTick } from 'vue' -import { useCanvas, useModal, useNotify } from '@opentiny/tiny-engine-controller' +import { useCanvas, useLayout, useModal } from '@opentiny/tiny-engine-controller' import { string2Ast, ast2String, insertName, formatString } from '@opentiny/tiny-engine-common/js/ast' import { constants } from '@opentiny/tiny-engine-utils' import { getSchema } from '@opentiny/tiny-engine-canvas' @@ -81,12 +81,23 @@ export const saveMethod = ({ name, content }) => { methods[name].value = content } +const tryString2Ast = (content) => { + try { + return string2Ast(content) + } catch (error) { + return null + } +} + const saveMethods = () => { if (!state.isChanged) { return false } - if (state.hasError) { + const editorContent = monaco.value.getEditor().getValue() + const ast = tryString2Ast(editorContent) + + if (state.hasError || !ast) { message({ status: 'error', message: '代码静态检查有错误,请先修改后再保存' @@ -95,8 +106,6 @@ const saveMethods = () => { return false } - const editorContent = monaco.value.getEditor().getValue() - const ast = string2Ast(editorContent) getSchema().methods = {} ast.program.body.forEach((declaration, index) => { @@ -116,10 +125,9 @@ const saveMethods = () => { }) useCanvas().setSaved(false) state.isChanged = false - useNotify({ - type: 'success', - message: '保存成功!' - }) + + const { PLUGIN_NAME, getPluginApi } = useLayout() + getPluginApi(PLUGIN_NAME.save).openCommon() return true }