dev*_*rus 6 javascript tinymce reactjs webpack
我正在尝试使用 react 创建一个简单的自托管 TinyMCE。目前我的项目只是显示一个简单的文本区域,没有工具栏或任何样式。我认为集成 tinymce 的一个好方法是使用模块加载器。我读到 React 已经通过使用 npx create-react-app 包含了 webpack。
Projectstructor
root
|-public
|-skins
|-src
|-App.js
|-TinyEditorComponent.js
|-webpack.config.js
Run Code Online (Sandbox Code Playgroud)
我一步一步做了什么:
npx create-react-app my-app
$ npm install --save @tinymce/tinymce-react
cp -r node_modules/tinymce/skins 皮肤
创建文件 webpack.config.js
Run Code Online (Sandbox Code Playgroud)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;
Run Code Online (Sandbox Code Playgroud)
我不确定如何为 tinymce 覆盖和配置 webpack,有很多旧方法,但什么是正确的方法。通过注入 webconfig?
理想情况下,您应该使用他们自己的 ReactComponent。这个答案需要来自https://www.tiny.cloud/docs/integrations/react/的知识
@tinymce/tinymce-react您应该通过npm或yarn您喜欢的方式安装该组件。安装后导入编辑器
import { Editor } from '@tinymce/tinymce-react';
Run Code Online (Sandbox Code Playgroud)
然后你可以像这样使用它
<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'
}}
/>
Run Code Online (Sandbox Code Playgroud)
然后将您的 TinyMCE 自托管副本添加到您的公用文件夹中。编辑 public 文件夹中的 index.html 并在<head>标签中添加以下内容
<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'
}}
/>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
509 次 |
| 最近记录: |