当前位置: 首页 > news >正文

vite项目中集成vditor文档编辑器

使用vditor

1 下载npm包

npm i vditor

2 新建一个编辑器组件,对vditor进行封装

注意哦这里有三个主题

let theme = ‘classic’   指的是编辑器的颜色

let contentTheme = ‘light’   指的是markdown文档的颜色

let codeTheme = ‘github’   指的是代码块的颜色

<template><div class="vditor-container"><div ref="editorRef" class="vditor-editor"></div></div>
</template><script setup>
import { ref, onMounted, onBeforeUnmount, watch } from 'vue'
import Vditor from 'vditor'
import 'vditor/dist/index.css'const props = defineProps({modelValue: { type: String, default: '' },placeholder: { type: String, default: '请输入内容...' },height: { type: [Number, String], default: '100%' },theme: { type: String, default: 'classic' }, // 'classic' | 'dark'toolbar: { type: Array, default: () => ['headings', 'bold', 'italic', 'strike', 'link', '|','list', 'ordered-list', 'check', 'outdent', 'indent', '|','quote', 'line', 'code', 'inline-code', 'insert-before', 'insert-after', '|','upload', 'table', '|', 'undo', 'redo', '|', 'edit-mode', 'both', 'preview', 'fullscreen']}
})const emit = defineEmits(['update:modelValue', 'init', 'focus', 'blur'])const editorRef = ref(null)
let vditorInstance = null// 初始化 Vditor
const initVditor = () => {const systemTheme = localStorage.getItem('theme');let theme = 'classic'let contentTheme = 'light'let codeTheme = 'github'if(systemTheme === 'dark') {theme = 'dark'contentTheme = 'dark'//  github-dark nordcodeTheme = 'github-dark'}vditorInstance = new Vditor(editorRef.value, {height: props.height,theme: theme,preview: {theme: {current: contentTheme},hljs: {defaultLang: 'html',style: codeTheme,lineNumber: true,langs: ["vue","mermaid", "echarts", "mindmap", "plantuml", "abc", "graphviz", "flowchart", "apache","js", "ts", "html","markmap",// common"properties", "apache", "bash", "c", "csharp", "cpp", "css", "coffeescript", "diff", "go", "xml", "http","json", "java", "javascript", "kotlin", "less", "lua", "makefile", "markdown", "nginx", "objectivec", "php","php-template", "perl", "plaintext", "python", "python-repl", "r", "ruby", "rust", "scss", "sql", "shell","swift", "ini", "typescript", "vbnet", "yaml","ada", "clojure", "dart", "erb", "fortran", "gradle", "haskell", "julia", "julia-repl", "lisp", "matlab","pgsql", "powershell", "sql_more", "stata", "cmake", "mathematica",// ext"solidity", "yul"]},},placeholder: props.placeholder,toolbar: props.toolbar,cache: { enable: false }, // 禁用缓存after() {vditorInstance.setValue(props.modelValue)emit('init', vditorInstance)},input: (value) => {emit('update:modelValue', value)},focus: () => emit('focus'),blur: () => emit('blur')})
}// 监听主题变化
watch(() => props.theme, (newTheme) => {if (vditorInstance) {vditorInstance.setTheme(newTheme)}
})// 监听内容变化(外部修改)
watch(() => props.modelValue, (newVal) => {if (newVal !== vditorInstance?.getValue()) {vditorInstance.setValue(newVal)}
})onMounted(() => {initVditor()
})onBeforeUnmount(() => {if (vditorInstance) {vditorInstance.destroy()vditorInstance = null}
})// 暴露方法给父组件
defineExpose({getVditor: () => vditorInstance,getValue: () => vditorInstance?.getValue(),setValue: (value) => vditorInstance?.setValue(value),focus: () => vditorInstance?.focus(),blur: () => vditorInstance?.blur()
})
</script><style scoped>
.vditor-container {width: 100%;
}
.vditor-editor {width: 100%;
}
</style>

3 使用编辑器

<template><div><h1>Vditor 编辑器示例</h1><button @click="toggleTheme">切换主题</button><button @click="getEditorContent">获取内容</button><VditorEditorv-model="content":theme="theme":height="600"@init="onEditorInit"/></div>
</template><script setup>
import { ref } from 'vue'
import VditorEditor from "@/components/VditorEditor/VditorEditor.vue";const content = ref('# Hello Markdown!\n\n试试输入 **Markdown** 语法。')
const theme = ref('classic') // 'classic' 或 'dark'
const editorInstance = ref(null)const toggleTheme = () => {theme.value = theme.value === 'classic' ? 'dark' : 'classic'
}const onEditorInit = (editor) => {editorInstance.value = editorconsole.log('编辑器初始化完成', editor)
}const getEditorContent = () => {alert(content.value || '无内容')
}
</script>

这样你的项目就继承了代码编辑器喽

http://www.lryc.cn/news/612056.html

相关文章:

  • 八股——IM项目
  • 【20205CVPR-目标检测方向】基于事件的高效目标检测:具有空间和时间注意力的混合神经网络
  • 【2025CVPR-目标检测方向】FIRE:通过频率引导重建误差对扩散生成的图像进行鲁棒检测
  • 18650电池组PACK自动化生产线:高效与品质的融合
  • 无人机航拍数据集|第3期 无人机军事目标目标检测YOLO数据集3556张yolov11/yolov8/yolov5可训练
  • OpenHarmony源码解析之init进程
  • C++ 操作 Redis 客户端
  • Docker swarm 常用的命令集合
  • Pipeline功能实现Redis批处理(项目批量查询点赞情况的应用)
  • 数据结构——双向链表及makefile
  • NineData 新增支持 AWS ElastiCache 复制链路
  • windows搬运文件脚本
  • 互斥锁与条件变量
  • 自然语言处理基础—(1)
  • 深入理解 C++ 中的stdpriority_queue:从原理到实战的高效优先级管理
  • ssm复习
  • 【嵌入式电机控制#26】BLDC:三相模拟采集
  • springboot项目前后端通用下载方法、问题和解决方案
  • PyTorch生成式人工智能(26)——使用PyTorch构建GPT模型
  • AVDTP Media Packet 传输全流程解析:从 SDP 到连接终止
  • 基于AntDesign二次封装table组件
  • 思途AOP学习笔记 0806
  • nginx代理出https,request.getRequestURL()得到http问题解决
  • 界面规范的其他框架实现-列表-layui实现
  • TypeError: crypto$2.getRandomValues is not a function
  • 北大、蚂蚁三个维度解构高效隐私保护机器学习:前沿进展+发展方向
  • AlexNet训练和测试CIFAR10
  • Python金融分析:从基础到量化交易的完整指南
  • 如何定位一个高并发场景下API响应时间从200ms突增到2s的问题
  • 生成模型实战 | Transformer详解与实现