Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add blocks.append(type, data) API method #715

Closed
gohabereg opened this issue Apr 10, 2019 · 10 comments
Closed

Add blocks.append(type, data) API method #715

gohabereg opened this issue Apr 10, 2019 · 10 comments
Assignees

Comments

@gohabereg
Copy link
Member

This method should allow to append (or insert) new Blocks programmatically

Example:

editor.blocks.append('paragraph', {text: 'Hello world'});

editor.blocks.insert(4, 'header', {text: 'Hello world'});
@neSpecc
Copy link
Member

neSpecc commented Apr 10, 2019

I thought, the position can be an optional argument of the append

@gohabereg
Copy link
Member Author

Better to name it insert then

@khaydarov
Copy link
Member

khaydarov commented Apr 10, 2019

I tend to think that it is better to use output format as a argument of append function, like

{
  type: 'header',
  data: { 
    ....
  } 
}

and second argument will be position

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

@neSpecc @gohabereg

@neSpecc
Copy link
Member

neSpecc commented Apr 11, 2019

@cjagannath please, describe the use case of that feature. How you would generate a block's data structure? It is an internal logic of a plugin. And type is the variable that defined by plugins users.

I can't see a good implementation of it, except hardcoding.

@dimensi
Copy link

dimensi commented Apr 11, 2019

@neSpecc for inline tool for example. We can use this api for extract text from selection and create new Header or Paragraph element.
2019-04-11 12-41-11 2019-04-11 12_43_58

@neSpecc
Copy link
Member

neSpecc commented Apr 11, 2019

@dimensi to create a Header, you should pass for example

{
   text: 'Extracted fragment',
   level: 3
}

How you will create this structure? Your Inline Tool doesn't know about properties that used by Header data (I mean text and level). So you are only able to hard code them.

@neSpecc
Copy link
Member

neSpecc commented Apr 11, 2019

For blocks convertation we already develop an ability (#704). Any other use cases?

@dimensi
Copy link

dimensi commented Apr 11, 2019

@dimensi to create a Header, you should pass for example

{
   text: 'Extracted fragment',
   level: 3
}

How you will create this structure? Your Inline Tool doesn't know about properties that used by Header data (I mean text and label). So you are only able to hard code them.

Yes hardcode, for use inline tools like this, inline tool must check through validation whether or not there's a Header class. Something like that

{
  render() {
   const blocks = this.api.getBlocks()  // this api not implement
   const hasHeader = blocks.findIndex(el => el.class instanceof Header)
   if (hasHeader) return button;
   return;
  }
}

I think api like that need for more complex tools in editor.js

@cjagannath
Copy link

@cjagannath please, describe the use case of that feature. How you would generate a block's data structure? It is an internal logic of a plugin. And type is the variable that defined by plugins users.

I can't see a good implementation of it, except hardcoding.

For my use case, I would like to offer a custom tools experience where all the tools are listed on the left. Users can pick any tool and add it to the editor. The screenshot below would give an idea. The default tools menu(+) would have a sub set of the tools.

image

As a user, I would know the list of plugins that I want to use. Some plugins will be built by me for my specific case. So I know all their types and data structure. The API would make it very easy to pick and add tools.

If one has to think as an author, I agree it's hard coding the plug-in info. That's why in my request #717 I had asked about extending the tools menu UI. But that could become too complex.

Hope I am clear. Let me know if any more info is required.

Thank you for listening.

@gohabereg
Copy link
Member Author

Resolved by #826

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants