React wangEditor 自定义菜单

wangEditor 初始化

在使用wangeditor的过程中,我们可以根据自己的需求自定义菜单

首先我们看wangEditor的引入过程:

导入:

import E from 'wangeditor';

创建区域:

  <div id="div1" ></div>

初始化:

	componentDidMount() {
	   
		const defaultMenu=[ 'head','bold', 'fontSize','fontName','italic','underline','foreColor','undo','redo','justify',]
		const editor = new E('#div1')
			editor.config.onchange=html=>{
				   this.setState({
					    editorContent:html
				    })
			}
			editor.config.menus=defaultMenu
			editor.zIndex.baseZIndex = 100
			editor.config.withCredentials = true
			editor.create();

	}

实现效果:

自定义菜单

开始自定义菜单,还是上面的类中操作:

引入菜单:

const {$, BtnMenu }=E;

创建菜单类:

class Image2 extends BtnMenu {
	constructor(editor){
		const $elem = $(
			`<div class="w-e-menu" data-title="图片">
				<i class="w-e-icon-image">
				</i>
			</div>`
		)
		super($elem, editor)
	}
	// 菜单点击事件
	clickHandler(){
		console.log(1);
	}
	tryChangeActive(){
		    const editor = this.editor
		    if (editor.cmd.queryCommandState('image2')) {
		        this.active()
		    } else {
		        this.unActive()
		    }
	}
}

完善:

	componentDidMount() {
	    const menuKeyVideo = "image2"
		const defaultMenu=[ 'head','bold', 'fontSize','fontName','italic','underline','foreColor','undo','redo','justify',]
		E.registerMenu(menuKeyVideo,Image2)
		const editor = new E('#div1')
	    editor.config.onchange=html=>{
				   this.setState({
					    editorContent:html
				    })
		 }
		editor.config.menus=defaultMenu
		editor.zIndex.baseZIndex = 100
		editor.config.withCredentials = true
		editor.create();
	}

查看效果:

 点击图片查看效果:

 希望对你有所帮助

React 中使用 wangEditor 富文本编辑器时,可以在创建编辑器实例时通过 options 参数来配置 insertFn 函数。该函数用于在编辑器中插入自定义的内容,例如插入图片、插入视频等。 下面是一个示例代码: ```javascript import React, { useRef, useEffect } from 'react' import wangEditor from 'wangeditor' function MyEditor() { const editorRef = useRef(null) useEffect(() => { const editor = new wangEditor(editorRef.current) editor.config.customInsert = true // 开启自定义插入内容 editor.config.menus = ['bold', 'italic', 'underline', 'image'] // 配置可用的菜单项 editor.create() // 定义 insertFn 函数 editor.config.insertFn = (content) => { // 在这里编写插入自定义内容的逻辑 // 例如插入图片 editor.cmd.do('insertHtml', `<img src="${content}" />`) } }, []) const handleInsertImage = () => { const imageUrl = 'https://example.com/image.jpg' editorRef.current.querySelector('.w-e-toolbar [data-title="图片"]').click() editorRef.current.querySelector('.w-e-menu [data-url-input]').value = imageUrl editorRef.current.querySelector('.w-e-menu [data-ok]').click() editorRef.current.focus() } return ( <div> <div ref={editorRef} /> <button onClick={handleInsertImage}>插入图片</button> </div> ) } ``` 在上面的代码中,我们在创建编辑器实例时通过 options 参数配置了 insertFn 函数。该函数接收一个参数 content,表示要插入的内容。在这个例子中,我们在 insertFn 函数中使用了 `editor.cmd.do('insertHtml', content)` 来插入自定义的内容。在这里我们插入的是一个 img 标签。 同时,我们还在渲染组件时添加了一个插入图片的按钮,点击该按钮会弹出 wangEditor 的图片上传框(需要在菜单中配置),并在用户选择图片后自动插入到编辑器中。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值