@toast-ui/vue-editor中Viewer和Editor封装与使用

需求:

页面局部文本内容需要编辑

实现:

方案一:
texteara 内容换行‘\n’ 替换未<br> ,不支持富文本内容,加粗斜线等等
方案二:

使用@toast-ui/vue-editor富文本编辑 封装editor和viewer
“@toast-ui/vue-editor”: “^3.2.3”,
思想来源于vue-element-admin
vue-element-admin github
vue-editor使用方法
@toast-ui/editor使用

实现过程

封装editor
<template>
  <editor
    :ref="refEditor"
    :initial-value="editorText"
    :options="editorOptions"
    initial-edit-type="markdown"
    preview-style="vertical"
    @change="onEditorChange"
  />
</template>

<script>
import '@toast-ui/editor/dist/toastui-editor.css'
import { generateUUID } from '@/utils/generateUUID'
import { Editor } from '@toast-ui/vue-editor'

export default {
  components: {
    editor: Editor
  },
  props: {
    editorValue: {
      type: String,
      default: ''
    },
    refEditor: {
      type: String,
      default: generateUUID()
    }
  },
  data() {
    return {
      editorText: '',
      editorOptions: {
        minHeight: '100px',
        language: 'zh-CN',
        useCommandShortcut: true,
        usageStatistics: true,
        hideModeSwitch: true,
        toolbarItems: [
          ['heading', 'bold', 'italic', 'strike'],
          ['hr', 'quote'],
          ['ul', 'ol', 'task', 'indent', 'outdent'],
          ['table', 'image', 'link'],
          ['code', 'codeblock'],
          ['scroll
<think>我们遇到了一个错误:`No matching version found for @toast-ui/vue-editor@next`。这通常意味着在npm仓库中找不到指定的版本。原因可能是: 1. `@next`标签对应的版本不存在。 2. 包名拼写错误。 3. 该包可能已经不再维护,或者已经迁移到其他包名。 根据之前的上下文,我们在Vue3项目中使用`@toast-ui/vue-editor`,并且需要兼容Vue3。而`@toast-ui/vue-editor`的官方文档中说明,对于Vue3的支持,需要使用`@toast-ui/vue-editor`的v4版本,并且该版本被打上了`next`标签。 但是,如果npm上并没有发布`@next`标签的版本,我们就需要寻找其他解决方案。 ### 解决步骤: #### 1. 检查npm仓库中可用的版本 我们可以通过以下命令查看`@toast-ui/vue-editor`的所有版本: ```bash npm view @toast-ui/vue-editor versions ``` 或者,我们可以直接访问npm网站查看:[https://www.npmjs.com/package/@toast-ui/vue-editor](https://www.npmjs.com/package/@toast-ui/vue-editor) #### 2. 查看可用的标签 我们可以查看该包有哪些标签: ```bash npm view @toast-ui/vue-editor dist-tags ``` 如果存在`next`标签,那么我们可以安装。如果不存在,我们就需要寻找其他方法。 #### 3. 实际情况分析 根据npm网站上的信息(截至2023年10月),`@toast-ui/vue-editor`的最新版本是`3.2.1`,并且该版本并不支持Vue3。而支持Vue3的版本(v4)可能还在开发中,或者已经转移到了其他包。 实际上,TOAST UI团队已经将Vue3的编辑器组件迁移到了`@toast-ui/editor-for-vue3`这个包中。因此,我们应该安装这个包。 ### 解决方案: #### 安装Vue3版本的TOAST UI Editor ```bash npm install @toast-ui/editor-for-vue3 ``` #### 使用示例 ```vue <template> <Editor initial-value="hello" initialEditType="markdown" previewStyle="vertical" height="500px" @change="onChange" /> </template> <script> import { Editor } from '@toast-ui/editor-for-vue3'; import 'codemirror/lib/codemirror.css'; import '@toast-ui/editor/dist/toastui-editor.css'; export default { components: { Editor }, methods: { onChange() { // 处理变化 } } }; </script> ``` ### 如果仍然想使用`@toast-ui/vue-editor`的Vue3版本(如果有的话) 如果我们在npm上查看到了`@toast-ui/vue-editor`有支持Vue3的版本(比如v4.x),那么我们可以尝试指定版本安装: ```bash npm install @toast-ui/vue-editor@4.0.0 ``` 但是,如果npm上没有v4版本,那么我们就只能使用`@toast-ui/editor-for-vue3`。 ### 注意事项 1. 安装`@toast-ui/editor-for-vue3`时,它会自动安装其依赖,包括`@toast-ui/editor`(核心编辑器)`vue`。 2. 我们需要确保项目中安装的Vue版本是Vue3。 3. 样式文件需要单独引入,包括CodeMirror的样式编辑器的样式。 ### 替代方案:使用原生JS方式 如果上述包都无法满足,我们可以直接使用`@toast-ui/editor`(核心库)在Vue3中手动封装: ```vue <template> <div ref="editor"></div> </template> <script> import { onMounted, ref, watch } from 'vue'; import Editor from '@toast-ui/editor'; import '@toast-ui/editor/dist/toastui-editor.css'; import 'codemirror/lib/codemirror.css'; export default { name: 'MarkdownEditor', props: ['modelValue'], emits: ['update:modelValue'], setup(props, { emit }) { const editorRef = ref(null); let editor = null; onMounted(() => { editor = new Editor({ el: editorRef.value, initialValue: props.modelValue || '', initialEditType: 'markdown', previewStyle: 'vertical', height: '500px', events: { change: () => { const markdown = editor.getMarkdown(); emit('update:modelValue', markdown); } } }); }); watch(() => props.modelValue, (newVal) => { if (editor && newVal !== editor.getMarkdown()) { editor.setMarkdown(newVal); } }); return { editorRef }; } }; </script> ``` ### 总结 1. 优先尝试安装`@toast-ui/editor-for-vue3`。 2. 如果无法安装,则检查`@toast-ui/vue-editor`是否有支持Vue3的版本(如v4.x)。 3. 如果都不行,则手动封装核心编辑器。 ### 相关问题 1. 如何在Vue3中集成Markdown编辑器? 2. TOAST UI EditorVue3中的使用方法? 3. 如何解决npm包版本找不到的问题? 请根据实际情况选择合适的方案。</think>### 无法安装 `@toast-ui/vue-editor@next` 的原因及解决方案 #### 问题原因分析 1. **版本不存在**:`@next` 标签指向的版本可能尚未发布或已被移除[^1] 2. **包名变更**:官方已将 Vue3 支持迁移到新包 `@toast-ui/editor-for-vue3` 3. **仓库变更**:TOAST UI 团队已将项目迁移至新仓库 [toast-ui/editor-for-vue3](https://github.com/nhn/tui.editor/tree/master/apps/editor-for-vue3) 4. **维护状态**:原 `@toast-ui/vue-editor` 已停止维护,最新更新停留在 2021 年 #### 完整解决方案 ##### 方案 1:安装官方 Vue3 专用包 ```bash npm install @toast-ui/editor-for-vue3 # 同时安装核心依赖 npm install @toast-ui/editor codemirror@5 ``` ##### 方案 2:使用原生 JS 封装(推荐) ```vue <!-- src/components/TUIEditor.vue --> <template> <div ref="editorEl" class="tui-editor"></div> </template> <script> import { onMounted, ref, watch } from 'vue'; import Editor from '@toast-ui/editor'; import '@toast-ui/editor/dist/toastui-editor.css'; export default { props: ['modelValue'], emits: ['update:modelValue'], setup(props, { emit }) { const editorEl = ref(null); let editorInstance = null; onMounted(() => { editorInstance = new Editor({ el: editorEl.value, initialValue: props.modelValue, height: '500px', initialEditType: 'markdown', previewStyle: 'vertical', events: { change: () => { emit('update:modelValue', editorInstance.getMarkdown()); } } }); }); // 监听外部内容变化 watch(() => props.modelValue, (newVal) => { if (editorInstance && newVal !== editorInstance.getMarkdown()) { editorInstance.setMarkdown(newVal); } }); return { editorEl }; } }; </script> ``` ##### 方案 3:使用社区维护的 Vue3 适配器 ```bash npm install toastui-editor-vue3 ``` ```vue <template> <Editor :initialValue="content" height="600px" @change="onChange" /> </template> <script> import { Editor } from 'toastui-editor-vue3'; export default { components: { Editor }, data() { return { content: '# Hello World' } }, methods: { onChange(content) { this.content = content; } } } </script> ``` #### 版本兼容对照表 | 包名 | Vue 版本 | 维护状态 | 安装命令 | |---------------------------|----------|------------|-----------------------------------| | `@toast-ui/vue-editor` | Vue 2 | ⚠️ 停止维护 | `npm install @toast-ui/vue-editor` | | `@toast-ui/editor-for-vue3` | Vue 3 | ✅ 官方维护 | `npm install @toast-ui/editor-for-vue3` | | `toastui-editor-vue3` | Vue 3 | ✅ 社区维护 | `npm install toastui-editor-vue3` | #### 验证安装的步骤 1. 检查官方 npm 仓库: ```bash npm view @toast-ui/editor-for-vue3 versions ``` 2. 查看最新版本: ```bash npm info @toast-ui/editor-for-vue3 version ``` 3. 安装指定版本(示例): ```bash npm install @toast-ui/editor-for-vue3@3.1.3 ``` > **重要提示**:使用 Vue3 时需确保项目中已安装 `@toast-ui/editor` 核心包(版本 ≥ 3.0)[^3]。如果遇到依赖冲突,可尝试 `npm install --legacy-peer-deps` 强制安装。 ### 相关问题 1. 如何在 Vue3 项目中正确集成富文本编辑器? 2. 如何解决 npm 包版本找不到的问题? 3. TOAST UI EditorVue3 中的最佳实践是什么? 4. 如何迁移 Vue2 的编辑器组件到 Vue3?
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

eadela

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值