grapesjs icon indicating copy to clipboard operation
grapesjs copied to clipboard

Enable the default RTE with a command

Open phil1995 opened this issue 9 years ago • 13 comments

Hello artf,

is it possible to trigger the default RTE with a command? I've found that it should be enableEditing() but it won't work. In my case I have selected a editable Component and it shows a Edit Item in the component-toolbar. But I can't figure out how to trigger it the same way like a doubleclick in this component would do it.

phil1995 avatar Sep 09 '17 14:09 phil1995

enableEditing is attached to to the view and you can get its instance from the model, so select the component and run this in console editor.getSelected().view.enableEditing()

artf avatar Sep 11 '17 20:09 artf

Ok thanks for the fast answer! I've tried it but nothing happen when I call it. Not even an error occurs. in my var editor = grapesjs.init({..} I have the following command:

commands: {
      defaults: [{
        id: 'tlb-edit',
          run:  function(editor){
          editor.getSelected().view.enableEditing();
        },
    }]
    },

Sorry for the weird formatting but otherwise Github display it not longer as code.

phil1995 avatar Sep 11 '17 20:09 phil1995

I've tried it but nothing happens when I call it. Not even an error occurs.

weird... I just tried it in the demo and it works, are you sure you have selected an editable component? text-edit

GitHub tip: https://guides.github.com/features/mastering-markdown/ use triple backquotes (and name of the language for colors) if you want the formatted code

artf avatar Sep 11 '17 20:09 artf

Okay I have found the error for me. I initialized the editor inside the document.ready function and with this I got an reference error (but only on console and not with the button). But my problem is now when I call it with the button like in my snippet above, still nothing happens only when I use the code via console.

phil1995 avatar Sep 13 '17 12:09 phil1995

which button? how do you call the command tlb-edit?

artf avatar Sep 14 '17 12:09 artf

I added a new button to the component toolbar in the Component.js with:

if(model.get('editable')) {
        tb.push({
          attributes: {class: 'fa fa-pencil'},
          command: 'tlb-edit',
        });
      }

phil1995 avatar Sep 14 '17 12:09 phil1995

Try to add some debug inside the command (eg. debugger;/console.log) and call it via the editor to see if it's actually there

editor.runCommand('tlb-edit');

ps: is there a reason of why you're editing the source file?

artf avatar Sep 14 '17 13:09 artf

The command is working because when I placed an alert inside it for example:

commands: {
      defaults: [{
        id: 'tlb-edit',

        run:  function(editor){
          editor.getSelected().view.enableEditing();
          alert("Hello world!");
        },
    }]
    },

On click it shows the alert "Hello world!" correctly. I edit the source file because I want to delete later the things which I don't need to get a lot smaller code.

phil1995 avatar Sep 14 '17 13:09 phil1995

maybe there is some event overlapping between the click and focus, try to call it in this way

...
setTimeout(() => editor.getSelected().view.enableEditing(), 0)

artf avatar Sep 14 '17 18:09 artf

Perfect now it's working. Thank you so much for your help! :+1:

phil1995 avatar Sep 14 '17 18:09 phil1995

Do my API call correctly, want to add a RTE commands to add a link: http://grapesjs.com/demo.html

 editor.Commands.add('tlb', {
    run:  function(editor, sender){
      editor.getSelected().view.enableEditing();
      //sethref for link etc
      alert('Hello world!');
    },
    stop:  function(editor, sender){
    },
  });
    editor.RichTextEditor.add('tlb', {
    command: 'tlb',
    class: 'fa fa-link',
    title: 'test'
 
  });  


edwardjiawei avatar Oct 04 '17 01:10 edwardjiawei

enableEditing is attached to to the view and you can get its instance from the model, so select the component and run this in console editor.getSelected().view.enableEditing()

How do i get the instance of view from the model? I'm trying to get a similar implementation as in question

sakshigarg9 avatar Mar 07 '19 06:03 sakshigarg9

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

lock[bot] avatar Mar 10 '20 20:03 lock[bot]