NextJS开发:nextjs中使用CkEditor5

2023-11-20 17:30
文章标签 使用 开发 nextjs ckeditor5

本文主要是介绍NextJS开发:nextjs中使用CkEditor5,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

NextJS项目中需要使用CkEditor5作为富文本编辑器,按照官网React CkEditor5手册使用出现如下错误:

node_modules/@ckeditor/ckeditor5-react/dist/index.js (5:242) @ eval⨯ ReferenceError: self is not defined

还是因为nextjs的服务器端渲染造成的错误,富文本编辑器一般用在表单提交页面,没有使用ssr的必要,想要解决上面问题,动态导入组件,禁用ssr就可以解决。

1、封装ckeditor组件

"use client"import { CKEditor } from '@ckeditor/ckeditor5-react';
import ClassicEditor from '@ckeditor/ckeditor5-build-classic';export function CustomCkEditor(props: {data: string, onChange: (content: string)=>void}) {const editorConfiguration = {toolbar: ['heading', //类型'|','bold', //加粗'italic', //斜体'link', //超链接'bulletedList',// 无序列表'numberedList', //有序列表'|','indent', //左缩进'outdent', //右缩进'|','imageUpload', //图片上传'blockQuote', //引用'insertTable', //插入图标//'mediaEmbed', //视频上传'undo', //撤销'redo'//重做]};return (<CKEditorconfig={ editorConfiguration }editor={ ClassicEditor }data={ props.data }onReady={ editor => {// You can store the "editor" and use when it is needed.console.log( 'Editor is ready to use!', editor );} }onChange={ ( event, editor ) => {const data = editor.getData();props.onChange(data)console.log( { event, editor, data } );} }onBlur={ ( event, editor ) => {console.log( 'Blur.', editor );} }onFocus={ ( event, editor ) => {console.log( 'Focus.', editor );} }/>)
}

2、动态导入的Form组件

"use client"import React from "react";
import { CustomCkEditor } from "@/components/app/custom-ckeditor";
import { Input } from "@/components/ui/input"
import { Label } from "@/components/ui/label"export function ReleaseForm(props: {}) 
{const [title, setTitle] = React.useState("");const [content, setContent] = React.useState("");const buildCkEditor = () => {return (<div className="release-ckeditor w-full overflow-visible text-black"><CustomCkEditor data={content} onChange={(data: string)=>setContent(data)}/></div>)}const handleSubmit = () => {}return (<><div className="mx-auto mt-5 lg:w-4/5 overflow-visible p-10 bg-white dark:bg-gray-900"><div className="text-lg font-bold">发布帖子</div><div className="w-full mt-5"><Label className=" text-base ">标题 <span className="ml-2 text-xs">(最多60)</span></Label><Input value={title} placeholder="请输入标题" className="mt-1 focus-visible:ring-0" onChange={(e)=>{setTitle(e.target.value)}}/><div className="h-4 text-red-600 text-xs mt-1"></div></div><Label className=" text-base">内容</Label><div className=" h-1"></div>{buildCkEditor()}<div className="w-full text-center"><Button className="mt-8 w-28" onClick={handleSubmit}>提交</CustomButton></div></div></>)
}

3、导入Form、禁用ssr

"use client"
import React from "react";
import { Input } from "@/components/ui/input"
import { Label } from "@/components/ui/label"
import { CustomButton } from "@/components/app/custom-button";
import { ReleasePropsDialog } from "./(components)/release-props-dialog";import dynamic from 'next/dynamic'
import { KeyPair } from "@/define/type";
//import { ReleaseForm } from "./(components)/release-form";export default function ForumReleasePage({ params }: { params: { topic: string } }) {//动态导入,禁用ssr,否则报错ReferenceError: self is not definedconst ReleaseForm = dynamic(() => import('./(components)/release-form').then((mod) => mod.ReleaseForm), { ssr: false })return (<><ReleaseForm/></>)
}

注意:这里之所以把所有表单界面和表单业务操作封装到ReleaseForm组件内,是因为测试发现如果只是直接动态导入CustomCkEditor,当绑定的内容改变,动态组件就会重新加载刷新出现闪烁现象

这篇关于NextJS开发:nextjs中使用CkEditor5的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/396263

相关文章

使用Docker构建Python Flask程序的详细教程

《使用Docker构建PythonFlask程序的详细教程》在当今的软件开发领域,容器化技术正变得越来越流行,而Docker无疑是其中的佼佼者,本文我们就来聊聊如何使用Docker构建一个简单的Py... 目录引言一、准备工作二、创建 Flask 应用程序三、创建 dockerfile四、构建 Docker

Python使用vllm处理多模态数据的预处理技巧

《Python使用vllm处理多模态数据的预处理技巧》本文深入探讨了在Python环境下使用vLLM处理多模态数据的预处理技巧,我们将从基础概念出发,详细讲解文本、图像、音频等多模态数据的预处理方法,... 目录1. 背景介绍1.1 目的和范围1.2 预期读者1.3 文档结构概述1.4 术语表1.4.1 核

Python使用pip工具实现包自动更新的多种方法

《Python使用pip工具实现包自动更新的多种方法》本文深入探讨了使用Python的pip工具实现包自动更新的各种方法和技术,我们将从基础概念开始,逐步介绍手动更新方法、自动化脚本编写、结合CI/C... 目录1. 背景介绍1.1 目的和范围1.2 预期读者1.3 文档结构概述1.4 术语表1.4.1 核

Conda与Python venv虚拟环境的区别与使用方法详解

《Conda与Pythonvenv虚拟环境的区别与使用方法详解》随着Python社区的成长,虚拟环境的概念和技术也在不断发展,:本文主要介绍Conda与Pythonvenv虚拟环境的区别与使用... 目录前言一、Conda 与 python venv 的核心区别1. Conda 的特点2. Python v

Spring Boot中WebSocket常用使用方法详解

《SpringBoot中WebSocket常用使用方法详解》本文从WebSocket的基础概念出发,详细介绍了SpringBoot集成WebSocket的步骤,并重点讲解了常用的使用方法,包括简单消... 目录一、WebSocket基础概念1.1 什么是WebSocket1.2 WebSocket与HTTP

C#中Guid类使用小结

《C#中Guid类使用小结》本文主要介绍了C#中Guid类用于生成和操作128位的唯一标识符,用于数据库主键及分布式系统,支持通过NewGuid、Parse等方法生成,感兴趣的可以了解一下... 目录前言一、什么是 Guid二、生成 Guid1. 使用 Guid.NewGuid() 方法2. 从字符串创建

Python使用python-can实现合并BLF文件

《Python使用python-can实现合并BLF文件》python-can库是Python生态中专注于CAN总线通信与数据处理的强大工具,本文将使用python-can为BLF文件合并提供高效灵活... 目录一、python-can 库:CAN 数据处理的利器二、BLF 文件合并核心代码解析1. 基础合

Python使用OpenCV实现获取视频时长的小工具

《Python使用OpenCV实现获取视频时长的小工具》在处理视频数据时,获取视频的时长是一项常见且基础的需求,本文将详细介绍如何使用Python和OpenCV获取视频时长,并对每一行代码进行深入解析... 目录一、代码实现二、代码解析1. 导入 OpenCV 库2. 定义获取视频时长的函数3. 打开视频文

Spring IoC 容器的使用详解(最新整理)

《SpringIoC容器的使用详解(最新整理)》文章介绍了Spring框架中的应用分层思想与IoC容器原理,通过分层解耦业务逻辑、数据访问等模块,IoC容器利用@Component注解管理Bean... 目录1. 应用分层2. IoC 的介绍3. IoC 容器的使用3.1. bean 的存储3.2. 方法注

Python内置函数之classmethod函数使用详解

《Python内置函数之classmethod函数使用详解》:本文主要介绍Python内置函数之classmethod函数使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录1. 类方法定义与基本语法2. 类方法 vs 实例方法 vs 静态方法3. 核心特性与用法(1编程客