Vue3 代码块高亮显示并可使用富文本编辑器编辑(highlight.js + wangEditor)

这篇具有很好参考价值的文章主要介绍了Vue3 代码块高亮显示并可使用富文本编辑器编辑(highlight.js + wangEditor)。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

在Vue项目中实现以下功能:

  功能1. 在页面中显示代码,并将其中的关键字高亮显示。

  功能2. 允许对代码块进行编辑,编辑时代码关键字也高亮显示。

  功能3. 可在编辑器中添加多个代码块,动态渲染代码关键字高亮。

 

Step1: 安装所需插件(本文使用npm安装,若需使用其他方式请查阅官方文档)

安装代码高亮显示插件highlight.js,官方网站:http://highlight.cndoc.wiki

npm install highlight.js

安装highlight.jsvue的集成插件highlightjs/vue-plugin,官方文档:https://github.com/highlightjs/vue-plugin

注意:本文编写时,以下命令会自动安装2.1.0版本,为vue2对应版本,使用vue3需手动将package.json中版本改为2.1.2版本。

npm install @highlightjs/vue-plugin

安装富文本编辑器插件wangEditor,以及对应的vue集成插件,官方网站:https://www.wangeditor.com/

npm install @wangeditor/editor
npm install @wangeditor/editor-for-vue@next

Step2:使用highlight.js实现功能1 -- 在页面中显示代码,并将其中的关键字高亮显示。

<script setup lang="ts">
import { ref } from 'vue';
import 'highlight.js/styles/stackoverflow-light.css'
import 'highlight.js/lib/common';
import hljsVuePlugin from "@highlightjs/vue-plugin";

const code = ref(`let hello = 'Hello World!';
console.log(hello);`)
</script>

<template>
  <hljsVuePlugin.component :code="code" />
</template>

页面效果

Vue3 代码块高亮显示并可使用富文本编辑器编辑(highlight.js + wangEditor)

Step3:使用wangEditor实现功能2 -- 允许对代码块进行编辑,编辑时代码关键字也高亮显示。

<script setup lang="ts">
import { onBeforeUnmount, ref, shallowRef } from 'vue';
import '@wangeditor/editor/dist/css/style.css';
import { Editor, Toolbar } from '@wangeditor/editor-for-vue';

const editorRef = shallowRef();
const valueHtml = ref(`<pre id="w-e-element-18" data-slate-node="element"><code id="w-e-element-19" data-slate-node="element"><span id="w-e-text-20" data-slate-node="text"><span data-slate-leaf="true"><span data-slate-string="true" class="token keyword">let</span></span><span data-slate-leaf="true"><span data-slate-string="true"> hello </span></span><span data-slate-leaf="true"><span class="token operator" data-slate-string="true">=</span></span><span data-slate-leaf="true"><span data-slate-string="true"> </span></span><span data-slate-leaf="true"><span class="token string" data-slate-string="true">'Hello World!'</span></span><span data-slate-leaf="true"><span class="token punctuation" data-slate-string="true">;</span></span><span data-slate-leaf="true"><span data-slate-string="true">
console</span></span><span data-slate-leaf="true"><span class="token punctuation" data-slate-string="true">.</span></span><span data-slate-leaf="true"><span data-slate-string="true" class="token function">log</span></span><span data-slate-leaf="true"><span class="token punctuation" data-slate-string="true">(</span></span><span data-slate-leaf="true"><span data-slate-string="true">hello</span></span><span data-slate-leaf="true"><span class="token punctuation" data-slate-string="true">)</span></span><span data-slate-leaf="true"><span class="token punctuation" data-slate-string="true">;</span></span></span></code></pre>`);

const toolbarConfig = {
  excludeKeys: []
}
const editorConfig = { placeholder: '请输入内容...' }

// 组件销毁时,也及时销毁编辑器
onBeforeUnmount(() => {
    const editor = editorRef.value
    if (editor == null) return
    editor.destroy()
})

const handleCreated = (editor: any) => {
  editorRef.value = editor // 记录 editor 实例,重要!
}
</script>

<template>
  <div>
    <Toolbar style="border-bottom: 1px solid #ccc" :editor="editorRef" 
      :defaultConfig="toolbarConfig" mode="default"/>
    <Editor style="height: 500px; overflow-y: hidden;" v-model="valueHtml"
      :defaultConfig="editorConfig" mode="default" @onCreated="handleCreated"/>
  </div>
</template>

页面效果

Vue3 代码块高亮显示并可使用富文本编辑器编辑(highlight.js + wangEditor)

Step4:使用结合highlight.js和wangEditer实现功能3 -- 可在编辑器中添加多个代码块,动态渲染代码关键字高亮。

<script setup lang="ts">
import { onBeforeUnmount, ref, shallowRef } from 'vue';

import 'highlight.js/styles/stackoverflow-light.css'
import 'highlight.js/lib/common';
import hljs from "highlight.js";

import '@wangeditor/editor/dist/css/style.css';
import { Editor, Toolbar } from '@wangeditor/editor-for-vue';

const editMode = ref(true);

//定义指令,自动使用highlight.js渲染所有<pre><dode>代码块
const vHigelight = {
  mounted(el :any) {
    let blocks = el.querySelectorAll('pre code');
    blocks.forEach((block: HTMLElement)=>{
      block.setAttribute('style', 'margin-top: 8px;');
      hljs.highlightBlock(block)
    })
  }
}

const editorRef = shallowRef();
const valueHtml = ref('');

const toolbarConfig = {
  excludeKeys: []
}
const editorConfig = { placeholder: '请输入内容...' }

// 组件销毁时,也及时销毁编辑器
onBeforeUnmount(() => {
    const editor = editorRef.value
    if (editor == null) return
    editor.destroy()
})

const handleCreated = (editor: any) => {
  editorRef.value = editor // 记录 editor 实例,重要!
}
</script>

<template>
  <div v-if="!editMode">
    <button @click="editMode = true">编辑</button>
    <div v-higelight v-html="valueHtml"></div>
  </div>
  
  <div v-if="editMode">
    <button @click="editMode = false">保存</button>
    <Toolbar style="border-bottom: 1px solid #ccc" :editor="editorRef" 
      :defaultConfig="toolbarConfig" mode="default"/>
    <Editor style="height: 500px; overflow-y: hidden;" v-model="valueHtml"
      :defaultConfig="editorConfig" mode="default" @onCreated="handleCreated"/>
  </div>
</template>

页面效果

Vue3 代码块高亮显示并可使用富文本编辑器编辑(highlight.js + wangEditor)

Vue3 代码块高亮显示并可使用富文本编辑器编辑(highlight.js + wangEditor)

代码Demo地址 https://gitee.com/Yang_Enzhe/Demos/tree/master/RichTextAndCodeHighlight文章来源地址https://www.toymoban.com/news/detail-420335.html

到了这里,关于Vue3 代码块高亮显示并可使用富文本编辑器编辑(highlight.js + wangEditor)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • vue使用富文本编辑器 Wangeditor 可显示编辑新增回显禁用

    npm install wangeditor import editorBar from \\\"@/components/ editor/ editor.vue\\\"; Vue.component(\\\'editorBar\\\', editorBar)  editor-bar v-model=\\\"form.nr\\\" :flag=\\\"false\\\" @change=\\\"getcontent\\\" / mothods:{      //获取富文本内容     getcontent (content) {        this.form.nr = content;     }, } editor-bar v-model=\\\"form.nr\\\" :flag=\\\"false\\\" @change=\\\"getc

    2024年02月13日
    浏览(85)
  • vue3富文本编辑器vue-quill-editor、图片缩放ImageResize详细配置及使用教程

    官网地址:https://vueup.github.io/vue-quill/ 效果图  1、安装 2、在vue.config.js中添加配置,否则quill-image-resize-module会出现Cannot read property ‘imports‘ of undefined报错问题 3、创建quillTool.js(用于添加超链接、视频) 4、完整代码

    2024年02月04日
    浏览(46)
  • Monaco Editor安装,vue3中使用,自定义高亮,自定义提示,附完整代码

    root中为高亮规则。[/curl/, {token: “string.escape”}]:表示 ‘curl’ 的高亮颜色为粉色 高亮颜色参考:https://microsoft.github.io/monaco-editor/monarch.html 效果: 效果: 1、父组件:HomeView.vue 父组件中传给子组件所需的组件高度、初始内容、高亮类型、是否只读 子组件通过editorChange方法给

    2024年02月16日
    浏览(44)
  • Vue3 中vue-quill富文本编辑器图片缩放

     导包   添加配置   注: 该编辑器已经不在维护了,很古老了,打包后如果报错,建议使用其他编辑器

    2024年04月25日
    浏览(37)
  • vue3富文本编辑器的二次封装开发-Tinymce

    欢迎点击领取 -《前端开发面试题进阶秘籍》:前端登顶之巅-最全面的前端知识点梳理总结 专享链接 简介 1、安装:pnpm add tinymce @tinymce/tinymce-vue === Vue3 + tinymce + @tinymce/tinymce-vue 2、功能实现图片上传、基金卡片插入、收益卡片插入、源代码复用、最大长度限制、自定义表情包

    2024年02月07日
    浏览(48)
  • 使用Electron + Vue3 + TS搭建桌面端应用并可热更新

    以下是必要的技术: Electron 13.0.0 Vue3 + TS Electron-updater Node 16.13.1 Element-plus Less Meansjs 安装Vue-cli(如果未安装): npm install -g @vue/cli 创建Vue3项目: vue create electron-vue3 启动项目: yarn serve 安装Electron: vue add electron-builder 启动项目: yarn electron:serve 如果报错,需要安装ts-loader: yar

    2023年04月26日
    浏览(79)
  • vue3项目使用pdf.js插件实现:搜索高亮、修改pdf.js显示的页码、向pdf.js传值、控制搜索、处理接口文件流

    官网地址:http://mozilla.github.io/pdf.js/ 中文文档地址:https://gitcode.gitcode.host/docs-cn/pdf.js-docs-cn/print.html PDF.js是基于HTML5技术构建的,用于展示可移植文档格式的文件(PDF),它可以在现代浏览器中使用且无需安装任何第三方插件。 pdf.js主要包含两个库文件 pdf.js:负责API解析 pdf.wor

    2024年02月13日
    浏览(59)
  • 【VUE3】ElementUI--el-date-picker下拉控件样式修改(高亮显示设置)

    可以看到在截图中这个日期默认高亮显示的是30号,但是我选中其他日期后30号这个数字的高亮并没有移除。 年、月的样式同理。 这显然是不符合需求的,但是又需要用到这些控件,所以就可以通过style来改写el-date-picker的对应样式,以使组件达到需求要求的效果。 于是我通

    2024年02月02日
    浏览(52)
  • vue3+ts+tinynce富文本编辑器+htmlDocx+file-saver 配合实现word下载

    vue3 请下载html-docx-js-typescript,否则会报错类型问题 row.report效果及数据 调用

    2024年02月11日
    浏览(39)
  • vue3代码编辑器组件codemirror-editor-vue3

    官方文档:https://github.com/RennCheung/codemirror-editor-vue3 国内镜像:https://renncheung.github.io/codemirror-editor-vue3/zh-CN/guide/getting-started 参考文档:https://codemirror.net/5/mode/index.html 点击参考文档,选择语言并点击在文章最后找到mode的格式 在配置项中修改mode,并引入对应语言js 如使用pyt

    2024年02月14日
    浏览(30)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包