RTE in Modal
Question
Hello,
I'm trying to get TinyMCE to work as an editor in a modal instead of inline.
I am able to get the selected components html with target.getEl().innerHTML but this doesn't include any styling.
When setting the content back with replaceWith(html) all styling is lost on the component.
Is there a way to update/replace the html of a component without losing styles?
`
const cmd = {
run(editor, sender, options = {}) {
var target = options.target || editor.getSelected();
const modal = editor.Modal;
const STATE = {
VueInstance: null,
Html: target.getEl().innerHTML
};
const vueElement = document.createElement('div');
vueElement.innerHTML = `<editor :init="EditorConfig" v-model="Html"></editor>`;
modal.onceOpen(() => {
STATE.VueInstance = new Vue({
el: vueElement,
data() {
return {
EditorConfig: config,
Html: STATE.Html
}
},
components: {
Editor
}
});
});
modal.onceClose(() => {
if(STATE.VueInstance) {
const html = STATE.VueInstance.Html;
//target.components(html);
const originalStyle = target.styleToString();
const newComp = target.replaceWith(html);
if(_.isArray(newComp)) {
newComp.forEach(i => {
i.setStyle(originalStyle);
})
}
else {
newComp.setStyle(originalStyle);
}
STATE.VueInstance.$destroy();
}
});
const modalOpts = {
title: 'Edit',
content: vueElement
};
modal.open(modalOpts)
}
};
export const EditModalCommand = cmd;
`Answers (1)
You can use this to get a component and it's style;
const target = editor.getSelected();
let html = target.toHTML();
let css = editor.CodeManager.getCode(target, 'css', {
cssc: editor.CssComposer
});
html = `<style>${css}</style>` + html;
Related Questions and Answers
Continue research with similar issue discussions.
Issue #1669
[Question] Custom link modal doesn't update the current text but add another text with link
Hello Artur, I'm trying to have a custom modal when clicked link button in RTE. but it doesn't convert selected text to link but what it do...
Issue #4257
Background modification doesn't work for non-images
GrapesJS version [X] I confirm to use the latest version of GrapesJS What browser are you using? Chrome v100 Reproducible demo link https:/...
Issue #4083
Set root does not work
GrapesJS version[X] I confirm to use the latest version of GrapesJSWhat browser are you using? Safari 15.0Reproducible demo link https://js...
Issue #3631
How to load dynamic content in the final HTML file?
I looked everywhere, including the issues section and tried a few things. But really unsure how to load dynamic content in my components al...
Paid Plugins That Match This Issue
Curated by issue keywords and label relevance to help you ship faster.
Loading paid plugin recommendations...
Check the open-source GrapesJS plugins on GitHub or run a quick search in our free catalog.
Browse free plugins →Premium plugins ship with support, regular updates, and production-ready features — save days of integration work.
Browse premium plugins →Related tutorials
In-depth guides on the same topic.
Browse Plugin Categories
Jump directly to plugin category pages on the marketplace.