微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在 EditorJs 中创建自定义元素

如何解决在 EditorJs 中创建自定义元素

我在我的 react js 应用程序中添加EditorJs 插件

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"
                ]
              }
            },data: {
                text: "What does it mean «block-styled editor»",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.'
              }
            },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.'
              }
            },data: {
                text: "What does it mean clean data output",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"
              }
            },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.'
              }
            },data: {
                text:
                  "Clean data is useful to sanitize,validate and process on the backend."
              }
            },{
              type: "delimiter",data: {}
            },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
    }
  }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。