ConverScript.vue
3.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<template>
<div>
<BasicForm @register="registerForm">
<template #scriptContent>
<Card title="脚本内容" :bodyStyle="{ padding: 0, height: '280px' }">
<div ref="aceRef" class="overflow-hidden"></div>
</Card>
<Button @click="handleCopy" class="mt-4">
<template #icon>
<CopyOutlined />
</template>
copy
</Button>
</template>
</BasicForm>
</div>
</template>
<script setup lang="ts">
import { ref, unref } from 'vue';
import { formSchema } from './config.data';
import { BasicForm, useForm } from '/@/components/Form';
import ace from 'ace-builds';
import { Card, Button } from 'ant-design-vue';
import 'ace-builds/src-noconflict/theme-chrome'; // 默认设置的主题
import 'ace-builds/src-noconflict/mode-javascript'; // 默认设置的语言模式
import { beautify } from 'ace-builds/src-noconflict/ext-beautify.js';
import { CopyOutlined } from '@ant-design/icons-vue';
import { useCopyToClipboard } from '/@/hooks/web/useCopyToClipboard';
import { useMessage } from '/@/hooks/web/useMessage';
defineEmits(['register']);
const { createMessage } = useMessage();
const { clipboardRef, copiedRef } = useCopyToClipboard();
const aceEditor = ref();
const aceRef = ref();
const [registerForm, { validate, resetFields }] = useForm({
labelWidth: 120,
schemas: formSchema,
showActionButtonGroup: false,
});
// 初始化编辑器
const initEditor = (jsScript?: string) => {
aceEditor.value = ace.edit(aceRef.value, {
maxLines: 12, // 最大行数,超过会自动出现滚动条
minLines: 12, // 最小行数,还未到最大行数时,编辑器会自动伸缩大小
fontSize: 14, // 编辑器内字体大小
theme: 'ace/theme/chrome', // 默认设置的主题
mode: 'ace/mode/javascript', // 默认设置的语言模式
tabSize: 2, // 制表符设置为 4 个空格大小
});
aceEditor.value.setOptions({
enableBasicAutocompletion: true,
enableLiveAutocompletion: true,
});
aceEditor.value.setValue(
jsScript ??
`
var trimSource =source.replaceAll(" ","");
if(trimSource.length==26 && trimSource.startsWith("020308")){
var str = "";
for(var i = 6;i<20;i+=2){
str += String.fromCharCode(parseInt(trimSource[i]+trimSource[i+1],16));
}`
);
beautify(aceEditor.value.session);
};
const handleCopy = () => {
const valueRef = aceEditor.value.getValue();
const value = unref(valueRef);
if (!value) {
createMessage.warning('请输入要拷贝的内容!');
return;
}
clipboardRef.value = value;
if (unref(copiedRef)) {
createMessage.success('复制成功!');
}
};
const getFormData = async () => {
const value = await validate();
if (!value) return;
return value;
};
const resetFormData = () => {
resetFields();
};
defineExpose({
initEditor,
getFormData,
resetFormData,
});
</script>
<style lang="less" scoped>
@import url('./ConverScriptModal.less');
</style>