mirror of
https://github.com/Tencent/tmagic-editor.git
synced 2025-09-01 04:09:49 +08:00
parent
0f0ec183a8
commit
0eab817a11
@ -319,6 +319,7 @@ export default defineComponent({
|
|||||||
|
|
||||||
provide('services', services);
|
provide('services', services);
|
||||||
|
|
||||||
|
provide('codeOptions', props.codeOptions);
|
||||||
provide(
|
provide(
|
||||||
'stageOptions',
|
'stageOptions',
|
||||||
reactive({
|
reactive({
|
||||||
|
@ -5,12 +5,8 @@
|
|||||||
class="m-editor-container"
|
class="m-editor-container"
|
||||||
:init-values="`${codeContent}`"
|
:init-values="`${codeContent}`"
|
||||||
@save="saveCodeDraft"
|
@save="saveCodeDraft"
|
||||||
:options="{
|
:language="language"
|
||||||
tabSize: 2,
|
:options="codeOptions"
|
||||||
fontSize: 16,
|
|
||||||
formatOnPaste: true,
|
|
||||||
readOnly: !editable,
|
|
||||||
}"
|
|
||||||
></magic-code-editor>
|
></magic-code-editor>
|
||||||
<div class="m-editor-content-bottom" v-if="editable">
|
<div class="m-editor-content-bottom" v-if="editable">
|
||||||
<TMagicButton type="primary" class="button" @click="saveCode">保存</TMagicButton>
|
<TMagicButton type="primary" class="button" @click="saveCode">保存</TMagicButton>
|
||||||
@ -22,7 +18,7 @@
|
|||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
<script lang="ts" setup>
|
<script lang="ts" setup>
|
||||||
import { inject, ref, watchEffect } from 'vue';
|
import { computed, inject, ref, watchEffect } from 'vue';
|
||||||
import type * as monaco from 'monaco-editor';
|
import type * as monaco from 'monaco-editor';
|
||||||
|
|
||||||
import { TMagicButton, tMagicMessage, tMagicMessageBox } from '@tmagic/design';
|
import { TMagicButton, tMagicMessage, tMagicMessageBox } from '@tmagic/design';
|
||||||
@ -31,9 +27,6 @@ import { datetimeFormatter } from '@tmagic/utils';
|
|||||||
import MagicCodeEditor from '../layouts/CodeEditor.vue';
|
import MagicCodeEditor from '../layouts/CodeEditor.vue';
|
||||||
import type { Services } from '../type';
|
import type { Services } from '../type';
|
||||||
|
|
||||||
// 草稿提示延时,避免点击保存时出现两次提醒
|
|
||||||
const draftTipTimeOut = 100;
|
|
||||||
|
|
||||||
const props = withDefaults(
|
const props = withDefaults(
|
||||||
defineProps<{
|
defineProps<{
|
||||||
/** 代码id */
|
/** 代码id */
|
||||||
@ -44,22 +37,30 @@ const props = withDefaults(
|
|||||||
editable?: boolean;
|
editable?: boolean;
|
||||||
/** 是否自动保存草稿 */
|
/** 是否自动保存草稿 */
|
||||||
autoSaveDraft?: boolean;
|
autoSaveDraft?: boolean;
|
||||||
|
/** 编辑器参数 */
|
||||||
|
codeOptions?: Object;
|
||||||
|
/** 编辑器语言 */
|
||||||
|
language?: string;
|
||||||
}>(),
|
}>(),
|
||||||
{
|
{
|
||||||
editable: true,
|
editable: true,
|
||||||
autoSaveDraft: true,
|
autoSaveDraft: true,
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
const emit = defineEmits(['save', 'close']);
|
const emit = defineEmits(['save', 'close', 'saveAndClose']);
|
||||||
|
|
||||||
const services = inject<Services>('services');
|
const services = inject<Services>('services');
|
||||||
|
|
||||||
const codeContent = ref<string>('');
|
const codeContent = ref<string>('');
|
||||||
|
const editorContent = ref<string>('');
|
||||||
const codeEditor = ref<InstanceType<typeof MagicCodeEditor>>();
|
const codeEditor = ref<InstanceType<typeof MagicCodeEditor>>();
|
||||||
// 原始代码内容
|
// 原始代码内容
|
||||||
const originCodeContent = ref<string | null>(null);
|
const originCodeContent = ref<string | null>(null);
|
||||||
// 是否展示草稿保存提示语
|
|
||||||
const shouldShowDraftTip = ref(true);
|
const codeOptions = computed(() => ({
|
||||||
|
...props.codeOptions,
|
||||||
|
readOnly: !props.editable,
|
||||||
|
}));
|
||||||
|
|
||||||
watchEffect(() => {
|
watchEffect(() => {
|
||||||
codeContent.value = props.content;
|
codeContent.value = props.content;
|
||||||
@ -83,63 +84,45 @@ const saveCodeDraft = async (codeValue: string) => {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
services?.codeBlockService.setCodeDraft(props.id, codeValue);
|
services?.codeBlockService.setCodeDraft(props.id, codeValue);
|
||||||
|
tMagicMessage.success(`代码草稿保存成功 ${datetimeFormatter(new Date())}`);
|
||||||
setTimeout(() => {
|
|
||||||
if (shouldShowDraftTip.value) {
|
|
||||||
tMagicMessage.success(`代码草稿保存成功 ${datetimeFormatter(new Date())}`);
|
|
||||||
}
|
|
||||||
}, draftTipTimeOut);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// 保存代码
|
// 保存代码
|
||||||
const saveCode = async (): Promise<boolean> => {
|
const saveCode = (): void => {
|
||||||
if (!codeEditor.value || !props.editable) return true;
|
if (!codeEditor.value || !props.editable) return;
|
||||||
|
// 代码内容
|
||||||
try {
|
editorContent.value = (codeEditor.value.getEditor() as monaco.editor.IStandaloneCodeEditor)?.getValue();
|
||||||
// 代码内容
|
emit('save', editorContent.value);
|
||||||
const editorContent = (codeEditor.value.getEditor() as monaco.editor.IStandaloneCodeEditor)?.getValue();
|
|
||||||
/* eslint no-eval: "off" */
|
|
||||||
eval(editorContent);
|
|
||||||
// 不重复提示
|
|
||||||
shouldShowDraftTip.value = false;
|
|
||||||
// 删除草稿
|
|
||||||
services?.codeBlockService.removeCodeDraft(props.id);
|
|
||||||
emit('save', editorContent);
|
|
||||||
shouldShowDraftTip.value = true;
|
|
||||||
return true;
|
|
||||||
} catch (e: any) {
|
|
||||||
tMagicMessage.error(e.stack);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const beforeClose = async () => {
|
// 保存并关闭
|
||||||
const codeDraft = services?.codeBlockService.getCodeDraft(props.id);
|
const saveAndClose = (): void => {
|
||||||
if (!codeDraft || !props.autoSaveDraft) {
|
if (!codeEditor.value || !props.editable) return;
|
||||||
return await saveCode();
|
// 代码内容
|
||||||
}
|
editorContent.value = (codeEditor.value.getEditor() as monaco.editor.IStandaloneCodeEditor)?.getValue();
|
||||||
let saveRes = true;
|
emit('saveAndClose', editorContent.value);
|
||||||
await tMagicMessageBox
|
|
||||||
.confirm('您有代码修改未保存,是否保存后再关闭?', '提示', {
|
|
||||||
confirmButtonText: '确认',
|
|
||||||
cancelButtonText: '取消',
|
|
||||||
type: 'warning',
|
|
||||||
})
|
|
||||||
.then(async () => {
|
|
||||||
// 保存之后再关闭
|
|
||||||
saveRes = await saveCode();
|
|
||||||
})
|
|
||||||
.catch(() => {
|
|
||||||
// 删除草稿 直接关闭
|
|
||||||
services?.codeBlockService.removeCodeDraft(props.id);
|
|
||||||
});
|
|
||||||
return saveRes;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// 关闭弹窗
|
// 关闭弹窗
|
||||||
const close = async () => {
|
const close = async () => {
|
||||||
const shouldClose = await beforeClose();
|
const codeDraft = services?.codeBlockService.getCodeDraft(props.id);
|
||||||
if (shouldClose) {
|
if (codeDraft) {
|
||||||
|
tMagicMessageBox
|
||||||
|
.confirm('您有代码修改未保存,是否保存后再关闭?', '提示', {
|
||||||
|
confirmButtonText: '确认',
|
||||||
|
cancelButtonText: '取消',
|
||||||
|
type: 'warning',
|
||||||
|
})
|
||||||
|
.then(async () => {
|
||||||
|
// 保存之后再关闭
|
||||||
|
saveAndClose();
|
||||||
|
})
|
||||||
|
.catch(() => {
|
||||||
|
// 删除草稿 直接关闭
|
||||||
|
services?.codeBlockService.removeCodeDraft(props.id);
|
||||||
|
emit('close');
|
||||||
|
});
|
||||||
|
} else {
|
||||||
emit('close');
|
emit('close');
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -11,7 +11,10 @@
|
|||||||
:content="codeContent"
|
:content="codeContent"
|
||||||
:editable="editable"
|
:editable="editable"
|
||||||
:autoSaveDraft="autoSaveDraft"
|
:autoSaveDraft="autoSaveDraft"
|
||||||
|
:codeOptions="codeOptions"
|
||||||
|
language="javascript"
|
||||||
@save="saveCode"
|
@save="saveCode"
|
||||||
|
@saveAndClose="saveAndClose"
|
||||||
@close="close"
|
@close="close"
|
||||||
></CodeDraftEditor>
|
></CodeDraftEditor>
|
||||||
</TMagicCard>
|
</TMagicCard>
|
||||||
@ -32,6 +35,7 @@ const props = withDefaults(
|
|||||||
content: string;
|
content: string;
|
||||||
editable?: boolean;
|
editable?: boolean;
|
||||||
autoSaveDraft?: boolean;
|
autoSaveDraft?: boolean;
|
||||||
|
codeOptions?: object;
|
||||||
}>(),
|
}>(),
|
||||||
{
|
{
|
||||||
editable: true,
|
editable: true,
|
||||||
@ -43,22 +47,47 @@ const services = inject<Services>('services');
|
|||||||
|
|
||||||
const codeName = ref<string>('');
|
const codeName = ref<string>('');
|
||||||
const codeContent = ref<string>('');
|
const codeContent = ref<string>('');
|
||||||
|
const evalRes = ref(true);
|
||||||
|
|
||||||
watchEffect(() => {
|
watchEffect(() => {
|
||||||
codeName.value = props.name;
|
codeName.value = props.name;
|
||||||
codeContent.value = props.content;
|
codeContent.value = props.content;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// 保存前钩子
|
||||||
|
const beforeSave = (codeValue: string): boolean => {
|
||||||
|
try {
|
||||||
|
// eslint-disable-next-line no-eval
|
||||||
|
eval(codeValue);
|
||||||
|
return true;
|
||||||
|
} catch (e: any) {
|
||||||
|
tMagicMessage.error(e.stack);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
// 保存代码
|
// 保存代码
|
||||||
const saveCode = async (codeValue: string): Promise<void> => {
|
const saveCode = async (codeValue: string): Promise<void> => {
|
||||||
if (!props.editable) return;
|
if (!props.editable) return;
|
||||||
|
evalRes.value = beforeSave(codeValue);
|
||||||
|
if (evalRes.value) {
|
||||||
|
// 存入dsl
|
||||||
|
await services?.codeBlockService.setCodeDslById(props.id, {
|
||||||
|
name: codeName.value,
|
||||||
|
content: codeValue,
|
||||||
|
});
|
||||||
|
tMagicMessage.success('代码保存成功');
|
||||||
|
// 删除草稿
|
||||||
|
services?.codeBlockService.removeCodeDraft(props.id);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
// 存入dsl
|
// 保存并关闭
|
||||||
await services?.codeBlockService.setCodeDslById(props.id, {
|
const saveAndClose = async (codeValue: string) => {
|
||||||
name: codeName.value,
|
await saveCode(codeValue);
|
||||||
content: codeValue,
|
if (evalRes.value) {
|
||||||
});
|
close();
|
||||||
tMagicMessage.success('代码保存成功');
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// 关闭弹窗
|
// 关闭弹窗
|
||||||
|
@ -48,6 +48,7 @@
|
|||||||
:content="codeConfig.content"
|
:content="codeConfig.content"
|
||||||
:editable="!!editable"
|
:editable="!!editable"
|
||||||
:autoSaveDraft="mode === CodeEditorMode.EDITOR"
|
:autoSaveDraft="mode === CodeEditorMode.EDITOR"
|
||||||
|
:codeOptions="codeOptions"
|
||||||
></FunctionEditor>
|
></FunctionEditor>
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
@ -68,6 +69,7 @@ import { CodeEditorMode } from '../../../type';
|
|||||||
import { serializeConfig } from '../../../utils/editor';
|
import { serializeConfig } from '../../../utils/editor';
|
||||||
|
|
||||||
const services = inject<Services>('services');
|
const services = inject<Services>('services');
|
||||||
|
const codeOptions = inject('codeOptions', {});
|
||||||
|
|
||||||
const left = ref(200);
|
const left = ref(200);
|
||||||
const currentTitle = ref('');
|
const currentTitle = ref('');
|
||||||
|
Loading…
x
Reference in New Issue
Block a user