reactjs - 在 EditorJs 中创建自定义元素

标签 reactjs editorjs

我添加了EditorJs我的 React js 应用程序中的插件:

import ReactDOM from "react-dom";
import React, { Component } from "react";

import EditorJs from "react-editor-js";

import { EDITOR_JS_TOOLS } from "./constants";

class ReactEditor extends Component {
  render() {
    return (
      <EditorJs
        tools={EDITOR_JS_TOOLS}
        data={{
          blocks: [
            {
              type: "header",
              data: {
                text: "Editor.js",
                level: 2
              }
            },
            {
              type: "paragraph",
              data: {
              }
            },
            {
              type: "header",
              data: {
                text: "Key features",
                level: 3
              }
            },
            {
              type: "list",
              data: {
                style: "unordered",
                items: [
                  "It is a block-styled editor",
                  "It returns clean data output in JSON",
                  "Designed to be extendable and pluggable with a simple API"
                ]
              }
            },
            {
              type: "header",
              data: {
                text: "What does it mean «block-styled editor»",
                level: 3
              }
            },
            {
              type: "paragraph",
              data: {
                text:
                  'Workspace in classic editors is made of a single contenteditable element, used to create different HTML markups. Editor.js <mark class="cdx-marker">workspace consists of separate Blocks: paragraphs, headings, images, lists, quotes, etc</mark>. Each of them is an independent contenteditable element (or more complex structure) provided by Plugin and united by Editor\'s Core.'
              }
            },
            {
              type: "paragraph",
              data: {
                text:
                  'There are dozens of <a href="https://github.com/editor-js">ready-to-use Blocks</a> and the <a href="https://editorjs.io/creating-a-block-tool">simple API</a> for creation any Block you need. For example, you can implement Blocks for Tweets, Instagram posts, surveys and polls, CTA-buttons and even games.'
              }
            },
            {
              type: "header",
              data: {
                text: "What does it mean clean data output",
                level: 3
              }
            },
            {
              type: "paragraph",
              data: {
                text:
                  "Classic WYSIWYG-editors produce raw HTML-markup with both content data and content appearance. On the contrary, Editor.js outputs JSON object with data of each Block. You can see an example below"
              }
            },
            {
              type: "paragraph",
              data: {
                text:
                  'Given data can be used as you want: render with HTML for <code class="inline-code">Web clients</code>, render natively for <code class="inline-code">mobile apps</code>, create markup for <code class="inline-code">Facebook Instant Articles</code> or <code class="inline-code">Google AMP</code>, generate an <code class="inline-code">audio version</code> and so on.'
              }
            },
            {
              type: "paragraph",
              data: {
                text:
                  "Clean data is useful to sanitize, validate and process on the backend."
              }
            },
            {
              type: "delimiter",
              data: {}
            },
            {
              type: "paragraph",
              data: {
                text:
                  "We have been working on this project more than three years. Several large media projects help us to test and debug the Editor, to make it's core more stable. At the same time we significantly improved the API. Now, it can be used to create any plugin for any task. Hope you enjoy. 😏"
              }
            },
            {
              type: "image",
              data: {
                file: {
                  url:
                    "https://codex.so/upload/redactor_images/o_e48549d1855c7fc1807308dd14990126.jpg"
                },
                caption: "",
                withBorder: true,
                stretched: false,
                withBackground: false
              }
            }
          ],
          version: "2.12.4"
        }}
      />
    );
  }
}

ReactDOM.render(<ReactEditor />, document.getElementById("root"));

根据文档我可以创建一个自定义元素:

render() {
  return (
    <EditorJs holder="custom">
      <div id="custom" />
    </EditorJs>
  );
}

问题:我想添加 input 作为自定义元素:<input type="text"/> ,但即使我做到了,我也无法做到:

<EditorJs holder="custom">
  <input id="custom" type="text"/>
</EditorJs>

谁知道如何在上面的插件中添加这个自定义元素?
演示:https://codesandbox.io/embed/react-editor-js-23opz

最佳答案

我在文档中发现我可以为 editor.js 创建一个插件: https://editorjs.io/the-first-plugin 。其中一个示例如下所示:

class SimpleImage {
  static get toolbox() {
    return {
      title: 'Image',
      icon: '<svg width="17" height="15" viewBox="0 0 336 276" xmlns="http://www.w3.org/2000/svg"><path d="M291 150V79c0-19-15-34-34-34H79c-19 0-34 15-34 34v42l67-44 81 72 56-29 42 30zm0 52l-43-30-56 30-81-67-66 39v23c0 19 15 34 34 34h178c17 0 31-13 34-29zM79 0h178c44 0 79 35 79 79v118c0 44-35 79-79 79H79c-44 0-79-35-79-79V79C0 35 35 0 79 0z"/></svg>'
    };
  }

  render() {
    return document.createElement('input');
  }

  save(blockContent) {
    return {
      url: blockContent.value
    }
  }
}

关于reactjs - 在 EditorJs 中创建自定义元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65550542/

相关文章:

javascript - 如果新旧值相同,react setState 是否执行?

reactjs - @azure/msal-browser untrusted_authority 错误

javascript - 使用editorJs时如何计算字符数

javascript - EditorJs 警告 "«blocks.stretchBlock()» is deprecated and will be removed in the next major release. Please use the «BlockAPI» instead."

html - 是否可以将 HTML 转换为 EditorJS json?

reactjs - 我可以忽略 eslint-loader 的意外保留字错误吗

javascript - react JS : Map over an array of objects to render in JSX

javascript - 如何解决 Promise pending 的问题