[英]TinyMCE Self-hosted with React
我正在尝试使用 react 创建一个简单的自托管 TinyMCE。 目前我的项目只是显示一个简单的文本区域,没有工具栏或任何样式。 我认为集成 tinymce 的一个好方法是使用模块加载器。 我读到 React 已经通过使用 npx create-react-app 包含了 webpack。
Projectstructor
root
|-public
|-skins
|-src
|-App.js
|-TinyEditorComponent.js
|-webpack.config.js
我一步一步做了什么:
npx create-react-app my-app
$ npm install --save @tinymce/tinymce-react
cp -r node_modules/tinymce/skins 皮肤
创建文件 webpack.config.js
const config = { module: { loaders: [ { test: require.resolve("tinymce/tinymce"), loaders: ["imports?this=>window", "exports?window.tinymce"], }, { test: /tinymce\\/(themes|plugins)\\//, loaders: ["imports?this=>window"], }, ], }, };
文本编辑器组件:
import React, { Component } from "react";
// Import TinyMCE
import tinymce from "tinymce/tinymce";
// Default icons are required for TinyMCE 5.3 or above
import "tinymce/icons/default";
// A theme is also required
import "tinymce/themes/silver";
// Any plugins you want to use has to be imported
import "tinymce/plugins/paste";
import "tinymce/plugins/link";
class TinyEditorComponent extends Component {
constructor() {
super();
this.state = { editor: null };
}
componentDidMount() {
tinymce.init({
selector: `#${this.props.id}`,
skin_url: `./skins/content/dark`,
plugins: [
"advlist autolink lists link image charmap print preview anchor",
"searchreplace visualblocks code fullscreen",
"insertdatetime media table paste code help wordcount",
],
toolbar: "undo redo | formatselect | bold italic backcolor |",
setup: editor => {
this.setState({ editor });
editor.on("keyup change", () => {
const content = editor.getContent();
this.props.onEditorChange(content);
});
},
});
}
componentWillUnmount() {
tinymce.remove(this.state.editor);
}
render() {
return (
<textarea
id={this.props.id}
value={this.props.content}
onChange={e => console.log(e)}
/>
);
}
}
export default TinyEditorComponent;
我不确定如何为 tinymce 覆盖和配置 webpack,有很多旧方法,但什么是正确的方法。 通过注入 webconfig?
理想情况下,您应该使用他们自己的 ReactComponent。 这个答案需要来自https://www.tiny.cloud/docs/integrations/react/ 的知识
您应该通过 NPM 或 Yarn 安装组件“@tinymce/tinymce-react”,无论您喜欢哪种。 安装后导入编辑器
import { Editor } from '@tinymce/tinymce-react';
然后你可以像这样使用它
<Editor
initialValue="<p>This is the initial content of the editor</p>"
init={{
height: 500,
menubar: false,
plugins: [
'advlist autolink lists link image charmap print preview anchor',
'searchreplace visualblocks code fullscreen',
'insertdatetime media table paste code help wordcount'
],
toolbar:
'undo redo | formatselect | bold italic backcolor | \
alignleft aligncenter alignright alignjustify | \
bullist numlist outdent indent | removeformat | help'
}}
/>
然后将您自托管的 TinyMCE 副本添加到您的公用文件夹中。 编辑 public 文件夹中的 index.html 并在<head>
标签中添加以下内容
<script src="tinymce.min.js"></script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.