Problem trying to inject new HTML content in the Editor
Question
Hi @artf, I'm now working on doing content in multi-languages inside the editor but I would like to know how can I save in the Dom HTML new HTML elements closest to the element selected (or update the these elements as new components inside the editor). For instance, I'm including some hidden div inside the HTML but when I'm trying to save the html doing getHTML from the editor I'm not capable of getting this new HTML content.
Thanks in advance.
Answers (2)
I've just fixed my problem doing a workaround after researching on the grapesjs core. I'm going to explain the solution that I did:
- I'm using a popup dialog with a bootstrap switcher to select text language. The complicate issue is how to update the component model that are not selected on the editor. To accomplish this I had to do the following one:
const sel = ContentBuilder.getEditor().getSelected();
/**
* This code update new elements inside the model when an user change the languages.
* @type {*|Model|Component}
*/
const coll = sel.collection; // I got the collection elements of the element selected
const at = coll.indexOf(sel) + 1; // After the element selected I will add a new component
let added;
// I need to add special attributes to a new component in the editor for my goal.
// Add special attributes to the element selected with DEFAULT LANG CONDITION
sel.addAttributes({'attr-lang-id':element_clicked.attr("attr-lang-id")});
sel.addAttributes({'attr-condition':MYM_LANG_DEFAULT});
// Cloning new component closest to the selected one.
added = coll.add(sel.clone(), { at });
// Adding language attribute to later on control the language text injected
added.addAttributes({'attr-lang-id':element_clicked.attr("attr-lang-id")});
added.addAttributes({'attr-condition':language_selected});
added.addClass(MYM_LANG_HIDDEN);
// Set the text content
added.set('content', text);
added = Array.isArray(added) ? added : [added];
// To call to clone event
added.forEach(add => ContentBuilder.getEditor().trigger('component:clone', add));
- If I switch off the language option I need to delete all the new components and update the editor model:
// Get component selected on the Editor
const comp = ContentBuilder.getEditor().getSelected();
const coll = comp.collection;
const at = coll.indexOf(comp);
// We need to remove from the selected one attributes like
//.removeAttr("attr-lang-id")
//.removeAttr("attr-condition", MYM_LANG_DEFAULT)
const attr = comp.getAttributes(); // To get all attributes from the model
delete attr['attr-lang-id']; // To delete uuid
delete attr['attr-condition']; // To delete condition settings
comp.setAttributes(attr);
// Now I need to look for all components that are not the default one that I want to keep on the editor. So I will remove everything but the component that is on the position 0.
if (coll.length > 1) {
// To remove all elements but the first one
coll.models.splice(1);
}
comp.emitUpdate();
This is how it works: http://recordit.co/zcrpR0MRyv
For sure that exist a easy way to implement this solution but I couldn't find the good methods to do it.
Thanks in advance.
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.
Related Questions and Answers
Continue research with similar issue discussions.
Issue #1423
[QUESTIONS] Breadcrumb
Hi @artf, Now I'm working on creating a breadcrumb similar to the LayerManager but I would like to know if already exists a method that I c...
Issue #1111
How to add parent class when drag element
Hi @artf, Hope you are doing fine. Please let us know how we can add the parent class of div when new element is dragged inside that div li...
Issue #1306
Inserting text in an canvas element is not updated on the HTML content after saving it.
H @artf, I have a pitfalls in the middle of saving the HTML content. Right now we have a popup modal dialog where I can select some special...
Issue #1393
[Question] Add grapesjs color picker to a new trait type?
I'm building a a new trait type, and would like to be able to include the grapesjs color picker inside it - any suggestions on how to do th...
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.