1

我正在使用Editor.js,但我不知道如何通过 javascript 在编辑器中添加新块,例如

我已经通过 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!');
      }
    })

但我无法添加任何新文本,我尝试了以下方法:

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

它没有帮助,editor.configuration.data.blocks更新自己的值,但添加的值不会显示在 Editor.js 视图中。

4

2 回答 2

2

您可以使用blocks.insert()API 方法。从 2.15 版本开始提供此功能

https://editorjs.io/blocks#insert

于 2019-07-17T14:24:07.427 回答
1

希望我的这个例子对你有帮助。我正在使用 Firebase 保存照片。保存后,我想在editorjs中插入图像。这是我如何做的代码。

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); }
      })
于 2020-09-29T08:09:30.800 回答