Custom html DOM changes does reflected when saving changes to database
Question
I created new toolbar option called text-align using following code
const rteStudio = studio.RichTextEditor;
rteStudio.add('text-align', {
icon: "<select id=\"textAlignSelector\" class=\"gjs-field lm select\">" +
"<option value=''>Select alignment</option>" +
"<option value=\"left\">Left</option> " +
"<option value=\"center\">Center</option> " +
"<option value=\"right\">Right</option> " +
"</select>",
attributes: { title: 'Text Align'},
event: 'change',
result: (rteStudio, action) => {
var val = action.btn.firstChild.value;
var fontTag = "";
if(action.btn.className && (action.btn.className.indexOf("gjs-rte-active") < 0)) {
action.btn.className += " ".concat("gjs-rte-active");
}
var el = rteStudio.getCurrentEl();
if (el && val != "" ) {
debugger;
if(el.tagName == "SPAN") {
el = el.parentNode;
}
el.style.textAlign = val;
rteStudio.syncActions();
}
//rteStudio.insertHTML(fontTag)
}
});
In order to make above code work, I added following function into grapes.js file :
{
key: "getCurrentEl",
value: function getCurrentEl(value) {
var lastNode;
var doc = this.doc;
var sel = doc.getSelection();
if (sel && sel.rangeCount) {
var node = doc.createElement('div');
var range = sel.getRangeAt(0);
return range.commonAncestorContainer.parentNode;
}
else {
return null;
}
}
}
Due to above code, my changes working in the editor in UI refer:

I am getting html of editor using following code :
studio.runCommand('gjs-get-inlined-html')
Now, the issue is, in the HTML got using above command, I don't see my formatting changes done using above code.
Please help me to fix this issue. Due to this, I can't save changes done using text-align feature into database
Answers (3)
Be sure to use the latest version and try to call sync:content on selected component before calling the desired command, eg.
editor.getSelected().trigger('sync:content');
editor.runCommand('gjs-get-inlined-html')
@artf, Thanks for the answer. I tried with that but unfortunately not working. I am getting that using following code:
studioCommand.add('com-save', {
run: function(studio, sender) {
studio.getSelected().trigger("sync:content")
var studioContent = studio.runCommand('gjs-get-inlined-html');
// Saving studioContent in the database using ajax call
}
});
I already did this but nothing happens. Please correct me if something wrong.
Again, highly appreciate your help on this.
@jadejahardipsinh grapesjs version?
Related Questions and Answers
Continue research with similar issue discussions.
Issue #2189
[Question] How to add "Ordered/Unordered List" option in rte/custom block
Hi I'm trying to create an ordered list with grapesjs. I tried adding the following to the rte: And this works if my page is pre-filled wit...
Issue #3218
How to add new HTML element with inline styles?
I would like to add a new HTML element, with inline styles, around the rte.selection(). With the following example, the HTML is OK, but pro...
Issue #1661
I've created html table and cannot drag new elements to table cells
Hello. if I create html table with next block of code, everything works, I'm able to drop a Quote, for example, to any cell: But next code...
Issue #698
editor.getSelected().toHTML() is not consistent
Hi everyone, I created a new component type, 'html-block', which is a component in which you can add your html code. I added an 'Edit' butt...
Paid Plugins That Match This Issue
Curated by issue keywords and label relevance to help you ship faster.
Loading paid plugin recommendations...
Browse Plugin Categories
Jump directly to plugin category pages on the marketplace.