简体   繁体   English

如何在 Editor.js 中添加新块

[英]How to add a new block in Editor.js

i am using Editor.js and i do not know how to add a new block in the editor via javascript, eg我正在使用Editor.js ,但我不知道如何通过 javascript 在编辑器中添加新块,例如

I've created the editor by docs:我已经通过文档创建了编辑器:

const editor = new EditorJS({
      holderId: 'codex-editor',
      autofocus: true,
      data: {
        "time": 1550476186479,
        "blocks": [
           {
            type: 'paragraph', 
            data: {
             text: 'Hello world'
            }
           }
        ]
      },
      onReady: () => {
        console.log('Editor.js is ready to work!');
      }
    })

But i cannot add any new text, i've tried the method:但我无法添加任何新文本,我尝试过以下方法:

const newBlock = {
      type: 'paragraph', 
      data: {
          text: 'Hello world'
      }
    };
editor.configuration.data.blocks.push(newBlock); 

It does not help, editor.configuration.data.blocks updates himself values but added values does not display in Editor.js view.它没有帮助, editor.configuration.data.blocks更新自己的值,但添加的值不会显示在 Editor.js 视图中。

You can use blocks.insert() API method.您可以使用blocks.insert() API 方法。 This feature is available since 2.15 version此功能从 2.15 版本开始可用

https://editorjs.io/blocks#insert https://editorjs.io/blocks#insert

hopefully this example of mine will help you.希望我的这个例子会对你有所帮助。 I was doing a photo saving using Firebase.我正在使用 Firebase 保存照片。 After saving it, I want to insert the image inside editorjs.保存后,我想在 editorjs 中插入图像。 Here's the code of how I do it.这是我如何做的代码。

return new Promise((resolve, reject) => {
    task.snapshotChanges()
      .pipe(
        finalize( () => {
          ref.getDownloadURL()
            .subscribe(url => {
              console.log(url);
              this.editor.blocks.insert(
                "image",
                {
                  file: {
                    url : "https://www.tesla.com/tesla_theme/assets/img/_vehicle_redesign/roadster_and_semi/roadster/hero.jpg"
                  },
                  caption : "Roadster // tesla.com",
                  withBorder : false,
                  withBackground : false,
                  stretched : true
                })
            },error => {
              console.log(error)
            })
        }))
      .subscribe( url => {
        if(url){ console.log(url); }
      })

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

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