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

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.xdnf.cn/news/17333.html

相关文章:

  • 低代码系统的技术深度:超越“可视化操作”的架构与实现挑战
  • 【机器学习篇】02day.python机器学习篇Scikit-learn基础操作
  • 疯狂星期四文案网第30天运营日记
  • 自学嵌入式 day43 中断系统
  • 数据结构与算法的认识
  • Linux 防火墙(firewalld)详解与配置
  • 【概念学习】早期神经网络
  • IPS知识点
  • spring-dubbo
  • ##Anolis OS 8.10 安装oracle19c
  • 从零开始的CAD|CAE开发: 单柱绕流+多柱绕流
  • vue封装一个cascade级联 多选 全选组件 ,原生写法Input,Checkbox,Button
  • 看不见的伪造痕迹:AI时代的鉴伪攻防战
  • Codeforces Round 987 (Div. 2)
  • 数据结构—队列和栈
  • 问题定位排查手记1 | 从Windows端快速检查连接状态
  • Java面试宝典:类加载器分层设计与核心机制解析
  • PyCharm vs. VSCode 到底哪个更好用
  • C++、STL面试题总结(二)
  • 图论(邻接表)DFS
  • SpringBoot 接入SSE实现消息实时推送的优点,原理以及实现
  • 【Linux系统】进程间通信:命名管道
  • 生成模型实战 | Transformer详解与实现
  • 分布式光伏气象站:安装与维护
  • 人大金仓数据库逻辑备份与恢复命令
  • 基于模式识别的订单簿大单自动化处理系统
  • Git 分支迁移完整指南(结合分支图分析)
  • JavaWeb(04)
  • 每日五个pyecharts可视化图表-bars(5)
  • SQL的条件查询