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
	}
}
相关推荐
仿生狮子1 小时前
✂️ Nuxt 最简单的字体裁剪工具:Fontize
javascript·vue.js·nuxt.js
别惊醒渔人6 小时前
Vue3 Diff 优化:最长递增子序列 LIS
前端·javascript·vue.js
极梦网络无忧9 小时前
real-ai-editor:一款轻量、智能的纯前端 AI 富文本与 Markdown 编辑器
前端·人工智能·编辑器
用户831348593069810 小时前
Vue+Three.js实现PCB电路板3D交互:元器件点击高亮、部件显隐、模型自动旋转
vue.js·webgl·three.js
音视频工程实战11 小时前
PromptQL 新手入门与实战指南
数据库·sql·算法
清川渡水11 小时前
NL2SQL 的正确打开方式:从「AI 猜 SQL」到「置信度闭环」的工程化落地
数据库·人工智能·sql
ZYH_060111 小时前
InnoAI SQL 助手安装
linux·数据库·sql
GitLqr12 小时前
提升 Web 开发效率:我常用的 10 个 TypeScript 实战技巧
vue.js·react.js·typescript
IT界的老黄牛13 小时前
PostgreSQL 常用命令速查:MySQL 用户平滑上手,psql 元命令 + 库表管理 + 运维排查一篇通
sql·postgresql·psql·数据库运维·命令速查·mysql 对比
AI_paid_community16 小时前
机械研发人员如何使用 Claude?
java·vue.js