Vue封装sql编辑器

1.封装sql编辑器组件

TypeScript 复制代码
<template>
	<div :id="id"></div>
</template>

<script lang="ts" setup>
import EditorWorker from 'monaco-editor/esm/vs/editor/editor.worker?worker'
import * as monaco from 'monaco-editor'
import { language as sqlLanguage } from 'monaco-editor/esm/vs/basic-languages/sql/sql.js'
import { nextTick, ref, onBeforeUnmount, onMounted } from 'vue'

import { ElMessage } from 'element-plus'
const language = ref('sql')
const editorTheme = ref('vs-dark')

const props = defineProps({
	id: {
		type: String,
		required: true
	},
	value: {
		type: String,
		required: false,
		default: () => ''
	}
})
//
// MonacoEditor start
//
onBeforeUnmount(() => {
	editor.dispose()
	console.log('editor dispose')
})

onMounted(() => {
	editorInit()
	console.log('editor init')
})

// @ts-ignore
self.MonacoEnvironment = {
	getWorker(_: string, label: string) {
		return new EditorWorker()
	}
}

let editor = null

const editorInit = () => {
	nextTick(() => {
		!editor
			? (editor = monaco.editor.create(document.getElementById(props.id) as HTMLElement, {
					value: props.value, // 编辑器初始显示文字
					language: language.value, // 语言支持自行查阅demo
					automaticLayout: true, // 自适应布局
					theme: editorTheme.value, // 官方自带三种主题vs, hc-black, or vs-dark
					//foldingStrategy: 'indentation',
					renderLineHighlight: 'all', // 行亮
					selectOnLineNumbers: true, // 显示行号
					minimap: {
						enabled: true
					},
					cursorStyle: 'line', //光标样式
					readOnly: true, // 只读
					fontSize: 16, // 字体大小
					autoIndent: true, //自动布局
					useTabStops: false,
					scrollBeyondLastLine: false, // 取消代码后面一大段空白
					overviewRulerBorder: false // 不要滚动条的边框
			  }))
			: editor.setValue(props.value)
	})
}

const setEditorValue = (value: any) => {
	editor.setValue(value)
}
const getEditorValue = () => {
	return editor.getValue()
}

defineExpose({
	setEditorValue,
	getEditorValue
})
</script>

<style scoped></style>

2.调用组件

TypeScript 复制代码
	<el-form-item label="sql语句" label-width="auto" prop="sqlText">
							<ReadOnlyStudio id="apiSqlId" ref="sqlStudioRef" style="height: 260px; width: 100%"></ReadOnlyStudio>
						</el-form-item>
TypeScript 复制代码
const sqlStudioRef = ref()


const init = (id?: number, authId: any) => {
	visible.value = true
	dataForm.id = ''

	// 重置表单数据
	if (basicDataFormRef.value) {
		basicDataFormRef.value.resetFields()
	}
	if (apiSqlFormRef.value) {
		sqlStudioRef.value.setEditorValue('')
	}

	if (id) {
		getApiConfig(id, authId)
	}
}


const getApiConfig = (id: number, authId: any) => {
	useApiConfigApi(id).then(res => {
		Object.assign(dataForm, res.data)
		Object.assign(basicDataForm, res.data)
		Object.assign(apiSqlForm, res.data)
		sqlStudioRef.value.setEditorValue(apiSqlForm.sqlText)
	})
	if (authId) {
		//获取授权信息
		getAuthInfoApi(authId).then(res => {
			authDataForm.id = authId
			authDataForm.limited = res.data.requestTimes == -1 ? false : true
			authDataForm.requestTimes = res.data.requestTimes
			authDataForm.requestedTimes = res.data.requestedTimes
			authDataForm.requestedSuccessTimes = res.data.requestedSuccessTimes
			authDataForm.requestedFailedTimes = res.data.requestedFailedTimes
			authDataForm.startTime = res.data.startTime
			authDataForm.endTime = res.data.endTime
			ifAuth.value = true
		})
	} else {
		ifAuth.value = false
	}
}
相关推荐
余槐i9 小时前
PostgreSQL 函数索引报错 must be immutable:3种绕法与代价
数据库·后端·sql·postgresql·性能优化
码艺-Alimjan10 小时前
Tauri 源码备份 · 打包 & 解压 总结,源码收集之路
前端·javascript·vue.js
顽疲12 小时前
Java vue 养老系统源码详解:月账单 care_month_bill 与 Quartz 扣费 Job 实践
java·开发语言·vue.js·vue养老·springboot养老·java养老
毕业设计70314 小时前
(免费领源码)基于node的学生公寓管理系统06412- java、PHP、python、C#、小程序、大数据、单片机、网络工程等)
vue.js·mysql·node.js·maven·mybatis·express·b/s
不爱吃鱼的蔓热莓14 小时前
释放VSCode插件占用系统盘垃圾
ide·vscode·编辑器
古夕14 小时前
地址变了,页面是空的:一次 Vue Router 菜单切换复盘
前端·vue.js
数据工匠老o16 小时前
一张 5 亿行的表按月分区后,清理历史数据从几小时变成了几秒
数据库·sql
hwscom16 小时前
几分钟开防护,SQL和XSS注入全被护卫神挡住
数据库·sql·xss
挖掘狂人18 小时前
SQL 注入:一行代码如何撬开整个数据库
后端·sql·数据分析
易知微EasyV数据可视化18 小时前
会插卡片的编辑器:在 Agent 输入框里混排文字与结构化内容
人工智能·经验分享·编辑器·agent·设计