editor.js
editor.js copied to clipboard
Is there any way to get the block data inside Blocktune API
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?
You can try this
const index = state.editor.blocks.getCurrentBlockIndex()
const block = state.editor.blocks.getBlockByIndex(index)
block.save().then((val) => {
console.log( val)
})