editor.js icon indicating copy to clipboard operation
editor.js copied to clipboard

Is there any way to get the block data inside Blocktune API

Open cptiwari20 opened this issue 2 years ago • 1 comments

The question.

Why and how the question has come up.

I am trying to create a copy method inside editorjs for customized and native blocks. In the block API I am getting data = 'undefined;

class DuplicateBlockTune {
    static get isTune() {
      return true;
    }
  constructor({ api, data, block, config }) {
    // console.log({ api, data, block, config }) = there is no value in data and config fields
    this.api = api;
    this.block = block; - inside block.holder I am getting HTML block
    this.wrapper = null;
  }
...

is there any way I can get the normal JSON data of current block.

I am trying to create a new block with fake data,

 async duplicateBlock() {
    const selectedBlock = this.block;
    
    if (selectedBlock) {
      const { name, holder } = selectedBlock

      const defaultBlockData = await this.api.blocks.composeBlockData(name);
      const blockDataOverrides = { someProp: 'someValue' }; // TODO:: need to get the current block data
      const blockData = Object.assign(defaultBlockData, blockDataOverrides);

      const duplicateData = JSON.parse(JSON.stringify(blockData));
      this.api.blocks.insert(name, duplicateData);
    }
  }

how to get the JSON data?

cptiwari20 avatar Jun 08 '23 10:06 cptiwari20

You can try this

const index = state.editor.blocks.getCurrentBlockIndex()
const block = state.editor.blocks.getBlockByIndex(index)
block.save().then((val) => {
  console.log( val)
})

liuxzzz avatar Nov 30 '23 07:11 liuxzzz