简体   繁体   English

TinyMCE 自托管 React

[英]TinyMCE Self-hosted with React

i am trying to create a simple self-hosted TinyMCE with react.我正在尝试使用 react 创建一个简单的自托管 TinyMCE。 Currently my project is just show a simple textarea there is no toolbar or any style.目前我的项目只是显示一个简单的文本区域,没有工具栏或任何样式。 I think a good way to integrate tinymce is by using a module loader.我认为集成 tinymce 的一个好方法是使用模块加载器。 I read that react already includes webpack by using npx create-react-app.我读到 React 已经通过使用 npx create-react-app 包含了 webpack。

Projectstructor
    root
    |-public
    |-skins
    |-src 
       |-App.js
       |-TinyEditorComponent.js
    |-webpack.config.js

What I have done step by step:我一步一步做了什么:

  1. npx create-react-app my-app npx create-react-app my-app

  2. $ npm install --save @tinymce/tinymce-react $ npm install --save @tinymce/tinymce-react

  3. cp -r node_modules/tinymce/skins skins cp -r node_modules/tinymce/skins 皮肤

  4. create file webpack.config.js创建文件 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"], }, ], }, };

TextEditor Component:文本编辑器组件:

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;

I am not sure how to override and configure webpack for tinymce, there are alot of old ways but what is the right one.我不确定如何为 tinymce 覆盖和配置 webpack,有很多旧方法,但什么是正确的方法。 By injecting webconfig?通过注入 webconfig?

Ideally, you should be using their own ReactComponent.理想情况下,您应该使用他们自己的 ReactComponent。 this answer takes knowledge from https://www.tiny.cloud/docs/integrations/react/这个答案需要来自https://www.tiny.cloud/docs/integrations/react/ 的知识

You should install the component '@tinymce/tinymce-react' via NPM or Yarn whichever is your preference.您应该通过 NPM 或 Yarn 安装组件“@tinymce/tinymce-react”,无论您喜欢哪种。 once you have installed that you import the editor安装后导入编辑器

import { Editor } from '@tinymce/tinymce-react';

You can then use it like so然后你可以像这样使用它

<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'
      }}
/>

then add your self-hosted copy of TinyMCE to your public folder.然后将您自托管的 TinyMCE 副本添加到您的公用文件夹中。 edit index.html inside the public folder and add the following inside your <head> tag编辑 public 文件夹中的 index.html 并在<head>标签中添加以下内容

<script src="tinymce.min.js"></script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM