react hook使用UEditor引入秀米图文排版

本文主要是介绍react hook使用UEditor引入秀米图文排版,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

里面坑比较多,细节也比较多

以下使用的是react 18 + ice3.0,使用其他react脚手架的配置基本相同,例如umi4

1.下载UEditor

进入UEditor仓库,找到版本v1.4.3.3,点击进去
在这里插入图片描述
接着下载ueditor1_4_3_3-utf8-jsp.zip版本

在这里插入图片描述
下载好以后开始安装依赖:npm install ,依赖安装完成后就可以打包了,最终我们项目要的就是这个打包后的文件。

打包需要使用grunt:

  • 全局安装npm install -g grunt-cli
  • 在项目ueditor中安装grunt依赖,npm install grunt --save-dev
  • 在项目ueditor输入指令grunt default来生成一个dist文件
    在这里插入图片描述

2.react项目配置UEditor

在这里插入图片描述
复制dist\utf8-php下的所有文件到react项目中的public\editor\ueditor下面(没有这个目录就新建一个)。
如下,注意标黄的文件是后面步骤加上的,没有这个文件是正常的:在这里插入图片描述
接着引入UEditor

找到src\document.tsx文件,就这个文件就相当于其他项目中的index.html,具体使用参考document定制html,在这里引入UEditor,我们的文件存放的地址在public下面,这里写路径的时候不用写public,直接从editor开始写,/editor/ueditor/ueditor.config.js/editor/ueditor/ueditor.all.js
在这里插入图片描述

3.react组件引入UEditor

新建src\components\ReactUeditor文件夹,分别以下几个文件
在这里插入图片描述

index.tsx

import React, { useState, useRef } from "react";
import UEditor from "./InputUEditor";
// import "./PublishArticle.less";
import { Form } from "antd";function PublishArticle() {const [form] = Form.useForm();const ueRef: any = useRef(null);//编辑器的宽度const [config] = useState({initialFrameWidth: "100%",initialFrameHeight: 400,});//编辑器的默认值const [initData] = useState("");//富文本失焦就触发setContent函数设置表单的content内容const setContent = (e) => {form.setFieldsValue({content: ueRef.current.getUEContent(),});};return (<div className="PublishArticle-wrap"><UEditorid="container"ref={ueRef}config={config}initData={initData}setContent={(e) => setContent(e)}></UEditor></div>);
}export default PublishArticle;

InputUEditor.tsx

import React, { useEffect, useImperativeHandle, forwardRef } from "react";
let editor: any = null;const UEditor = (props, ref) => {useEffect(() => {setConfig(props.initData, props.config, props.setContent);return () => {// editor?.destroy();// editor.removeListener(); //不要打开,否则返回有问题报错};}, [props.initData, props.config, props.setContent]);// 初始化编辑器const setConfig = (initData, config, setContent) => {editor =window.UE &&window.UE.getEditor("editor", {// enableAutoSave: false,// autoHeightEnabled: false,autoFloatEnabled: false,initialFrameHeight: (config && config.initialFrameHeight) || 450,initialFrameWidth: (config && config.initialFrameWidth) || "100%",zIndex: 1,});editor.ready(() => {if (initData) {editor.setContent(initData); //设置默认值/表单回显}});editor.addListener("blur", function () {setContent(editor.getContent());});};useImperativeHandle(ref, () => ({getUEContent: () => {return editor.getContent(); //获取编辑器内容},}));return <script id="editor" type="text/plain" />;
};export default forwardRef(UEditor);

页面组件引入

import ReactUeditor from "@/components/ReactUeditor";<Form.Item name="contents"><ReactUeditor></ReactUeditor>
</Form.Item>

这个时候就能看到效果了
在这里插入图片描述

4.接入秀米

参考地址

引入秀米图标和样式
在这里插入图片描述
xiumi-ue-dialog-v5.js

/*** Created by shunchen_yang on 16/10/25.*/UE.registerUI("dialog", function (editor, uiName) {var btn = new UE.ui.Button({name: "/* xiumi-connect */",title: "秀米",onclick: function () {var dialog = new UE.ui.Dialog({iframeUrl: "/editor/ueditor/xiumi-ue-dialog-v5.html",editor: editor,name: "xiumi-connect",title: "秀米图文消息助手",cssRules:"width: " +(window.innerWidth - 80) +"px;" +"height: " +(window.innerHeight - 280) +"px;z-index:100000",});dialog.render();dialog.open();},});return btn;
});

xiumi-ue-v5.css

.edui-button.edui-for-xiumi-connect .edui-button-wrap .edui-button-body .edui-icon {background-image: url("./xiumi-connect-icon.png") !important;background-size: contain;
}

通过iframe 嵌入秀米
在这里插入图片描述

internal.js

(function () {var parent = window.parent;//dialog对象dialog = parent.$EDITORUI[window.frameElement.id.replace(/_iframe$/, "")];//当前打开dialog的编辑器实例editor = dialog.editor;UE = parent.UE;domUtils = UE.dom.domUtils;utils = UE.utils;browser = UE.browser;ajax = UE.ajax;$G = function (id) {return document.getElementById(id);};//focus元素$focus = function (node) {setTimeout(function () {if (browser.ie) {var r = node.createTextRange();r.collapse(false);r.select();} else {node.focus();}}, 0);};utils.loadFile(document, {href:editor.options.themePath +editor.options.theme +"/dialogbase.css?cache=" +Math.random(),tag: "link",type: "text/css",rel: "stylesheet",});lang = editor.getLang(dialog.className.split("-")[2]);
if (lang) {domUtils.on(window, "load", function () {var langImgPath =editor.options.langPath + editor.options.lang + "/images/";//针对静态资源for (var i in lang["static"]) {var dom = $G(i);if (!dom) continue;var tagName = dom.tagName,content = lang["static"][i];if (content.src) {//clonecontent = utils.extend({}, content, false);content.src = langImgPath + content.src;}if (content.style) {content = utils.extend({}, content, false);content.style = content.style.replace(/url\s*\(/g,"url(" + langImgPath);}switch (tagName.toLowerCase()) {case "var":dom.parentNode.replaceChild(document.createTextNode(content), dom);break;case "select":var ops = dom.options;for (var j = 0, oj; (oj = ops[j]); ) {oj.innerHTML = content.options[j++];}for (var p in content) {p != "options" && dom.setAttribute(p, content[p]);}break;default:domUtils.setAttributes(dom, content);}}});}
})();

xiumi-ue-dialog-v5.html

<!DOCTYPE html>
<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>XIUMI connect</title><style>html,body {padding: 0;margin: 0;}#xiumi {position: absolute;width: 100%;height: 100%;border: none;box-sizing: border-box;}</style>
</head><body><iframe id="xiumi" src="https://xiumi.us/studio/v5#/paper"></iframe><script type="text/javascript" src="dialogs/internal.js"></script><script>var xiumi = document.getElementById('xiumi');var xiumi_url = "https://xiumi.us";console.log("xiumi_url is %o", xiumi_url);xiumi.onload = function () {console.log("postMessage to %o", xiumi_url);// "XIUMI:3rdEditor:Connect" 是特定标识符,不能修改,大小写敏感xiumi.contentWindow.postMessage('XIUMI:3rdEditor:Connect', xiumi_url);};document.addEventListener("mousewheel", function (event) {event.preventDefault();event.stopPropagation();});window.addEventListener('message', function (event) {console.log("Received message from xiumi, origin: %o %o", event.origin, xiumi_url);if (event.origin == xiumi_url) {console.log("Inserting html");editor.execCommand('insertHtml', event.data);console.log("Xiumi dialog is closing");dialog.close();}}, false);</script>
</body></html>

将注册的图标添加到编辑器中
在这里插入图片描述

这篇关于react hook使用UEditor引入秀米图文排版的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

Java中的Cursor使用详解

《Java中的Cursor使用详解》本文介绍了Java中的Cursor接口及其在大数据集处理中的优势,包括逐行读取、分页处理、流控制、动态改变查询、并发控制和减少网络流量等,感兴趣的朋友一起看看吧... 最近看代码,有一段代码涉及到Cursor,感觉写法挺有意思的。注意是Cursor,而不是Consumer

Vue ElementUI中Upload组件批量上传的实现代码

《VueElementUI中Upload组件批量上传的实现代码》ElementUI中Upload组件批量上传通过获取upload组件的DOM、文件、上传地址和数据,封装uploadFiles方法,使... ElementUI中Upload组件如何批量上传首先就是upload组件 <el-upl

前端知识点之Javascript选择输入框confirm用法

《前端知识点之Javascript选择输入框confirm用法》:本文主要介绍JavaScript中的confirm方法的基本用法、功能特点、注意事项及常见用途,文中通过代码介绍的非常详细,对大家... 目录1. 基本用法2. 功能特点①阻塞行为:confirm 对话框会阻塞脚本的执行,直到用户作出选择。②

Node.js net模块的使用示例

《Node.jsnet模块的使用示例》本文主要介绍了Node.jsnet模块的使用示例,net模块支持TCP通信,处理TCP连接和数据传输,具有一定的参考价值,感兴趣的可以了解一下... 目录简介引入 net 模块核心概念TCP (传输控制协议)Socket服务器TCP 服务器创建基本服务器服务器配置选项服

如何使用CSS3实现波浪式图片墙

《如何使用CSS3实现波浪式图片墙》:本文主要介绍了如何使用CSS3的transform属性和动画技巧实现波浪式图片墙,通过设置图片的垂直偏移量,并使用动画使其周期性地改变位置,可以创建出动态且具有波浪效果的图片墙,同时,还强调了响应式设计的重要性,以确保图片墙在不同设备上都能良好显示,详细内容请阅读本文,希望能对你有所帮助...

CSS3 最强二维布局系统之Grid 网格布局

《CSS3最强二维布局系统之Grid网格布局》CS3的Grid网格布局是目前最强的二维布局系统,可以同时对列和行进行处理,将网页划分成一个个网格,可以任意组合不同的网格,做出各种各样的布局,本文介... 深入学习 css3 目前最强大的布局系统 Grid 网格布局Grid 网格布局的基本认识Grid 网

HTML5中下拉框<select>标签的属性和样式详解

《HTML5中下拉框<select>标签的属性和样式详解》在HTML5中,下拉框(select标签)作为表单的重要组成部分,为用户提供了一个从预定义选项中选择值的方式,本文将深入探讨select标签的... 在html5中,下拉框(<select>标签)作为表单的重要组成部分,为用户提供了一个从预定义选项中

Rust中的注释使用解读

《Rust中的注释使用解读》本文介绍了Rust中的行注释、块注释和文档注释的使用方法,通过示例展示了如何在实际代码中应用这些注释,以提高代码的可读性和可维护性... 目录Rust 中的注释使用指南1. 行注释示例:行注释2. 块注释示例:块注释3. 文档注释示例:文档注释4. 综合示例总结Rust 中的注释

前端 CSS 动态设置样式::class、:style 等技巧(推荐)

《前端CSS动态设置样式::class、:style等技巧(推荐)》:本文主要介绍了Vue.js中动态绑定类名和内联样式的两种方法:对象语法和数组语法,通过对象语法,可以根据条件动态切换类名或样式;通过数组语法,可以同时绑定多个类名或样式,此外,还可以结合计算属性来生成复杂的类名或样式对象,详细内容请阅读本文,希望能对你有所帮助...

Linux使用cut进行文本提取的操作方法

《Linux使用cut进行文本提取的操作方法》Linux中的cut命令是一个命令行实用程序,用于从文件或标准输入中提取文本行的部分,本文给大家介绍了Linux使用cut进行文本提取的操作方法,文中有详... 目录简介基础语法常用选项范围选择示例用法-f:字段选择-d:分隔符-c:字符选择-b:字节选择--c