Inserting text in an canvas element is not updated on the HTML content after saving it.
Question
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 text and inject on the canvas in the selected element. The issue is when saving the HTML content the changes are not reflected on it. It likes I have to refresh or update the model but I don't know how to update it.
Note:
Only save the data after inserting more than one times. But the latest change added is not saved.
http://recordit.co/Mx1wggkdJT
`
var text = editorRTE.getSelected().view.$el[0].innerHTML;
String.prototype.splice = function(idx, rem, str) {
return this.slice(0, idx) + str + this.slice(idx + Math.abs(rem));
};
value = text.splice(caret, 0, value);
editorRTE.getSelected().view.$el[0].innerHTML = value;
editorRTE.getSelected().view.$el[0].focus();`
Can you help me please?Answers (3)
Hi @jvillena. Would you mind sharing some of your code please? I'm attempting to do the same thing but I just can't figure it out. I keep adding <span> tags as "placeholders" to insert the variable values, but they are always added to the front of the string and not at cursor position. Else if I do it another way, then the cursor keeps adding text into the span tag instead of behind it. In short I can't figure out how to insert the text at cursor position.
@artf You're right I updated the code only triggering on the event change in the component selected and then it was working fine.
I don't understand how did you change it to trigger on the event change in component select?
The issue is when saving the HTML content the changes are not reflected on it. I also have the same issue.
This is what I tried:
rte.add('mergefield', {
icon: '<b>{ # }</b>',
name: 'merge-field',
attributes: { title: 'Merge-field'},
result: rte => {
const anchor = rte.selection().anchorNode;
const nextSibling = anchor && anchor.nextSibling;
if (nextSibling && nextSibling.nodeName == 'span') {
rte.exec('unlink');
} else {
var tag = '<span data-gjs-type="mergefield" data-gjs-editable="false" data-gjs-stylable="false" data-highlightable="1" readonly="true"> {{ merge-field }} </span>'; //class="gjs-mergefield"
rte.insertHTML(tag);
}
}
});
domComps.addType('mergefield', {
model: dModel.extend({
toolbar: [{
attributes: { class: 'fa fa-arrows' },
//command: 'tlb-move',
}],
defaults: Object.assign({}, dModel.prototype.defaults, {
'custom-name': "Merge field",
tagName: 'span',
class: 'merge-field',
badgable: true,
highlightable: true,
editable: false,
droppable: false,
draggable: true,
removable: true,
traits: [{
label: "mergefield",
type: 'select',
name: 'value',
options: [
{ value: 'user.firstname', name: "user.firstname" },
{ value: 'user.lastname', name: "user.lastname" },
{ value: 'company.name', name: "company.name" },
{ value: 'company.business_regnr', name: "company.business_regnr1" },
{ value: 'system.date', name: "system.date" },
]
}],
style: {
display: 'inline-block',
border: '1px dashed #455699',
'text-align': 'center',
'background-color': '#BDCDF4',
'padding': '3px',
'border-radius': '5px',
'color': 'black'
},
}),
}, {
toolbar: [{
attributes: { class: 'fa fa-arrows' },
command: 'tlb-move',
}],
isComponent(el) {
if (el.tagName == 'SPAN' && el.dataset.mergefield == "1") {
return { type: 'mergefield' };
}
},
}),
view: dView.extend({
events: {
'click': function (e) { console.log("click"); },
'keydown': function (e) {
if (e.key == 'Backspace') {
e.target.parentNode.removeChild(e.target);
}
}
},
}),
});
I've just solved. I had to render the DomComponents after applying the changes. The solution was:
// First we apply the new text in the selection
editor.getSelected().set('content', value.trim());
// To update the HTML DOM we need to use render function.
editorRTE.DomComponents.render();
No, with this editorRTE.DomComponents.render() you're triggering the render of all component, please read carefully about Components, by looking at stuff like this editorRTE.getSelected().view.$el[0].innerHTML = value I think you just didn't get how really components work
Related Questions and Answers
Continue research with similar issue discussions.
Issue #1408
Why Text element converted into box ??
Hi @artf , I am using grapesjs newsletter plugin with ckeditor plugin. I dragged TEXT element in canvas and did some editing with ckeditor....
Issue #1363
Problem trying to inject new HTML content in the Editor
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...
Issue #1128
Bug in dragging selected text
hi @artf as you can see my screenshot when you drag textblock than select text in that element and drag it to bottom of content than text g...
Issue #1479
Custom element with pop-up
hello @artf I've make pop-up for select custom link data but data is save and also display when we click on export html. But data is not re...
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.