Enable the default RTE with a command
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.
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()
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.
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?

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
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.
which button? how do you call the command tlb-edit?
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',
});
}
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?
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.
maybe there is some event overlapping between the click and focus, try to call it in this way
...
setTimeout(() => editor.getSelected().view.enableEditing(), 0)
Perfect now it's working. Thank you so much for your help! :+1:
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'
});
enableEditingis attached to to the view and you can get its instance from the model, so select the component and run this in consoleeditor.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
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.