mjml icon indicating copy to clipboard operation
mjml copied to clipboard

Cloning an element links them together

Open lahdekorpi opened this issue 3 years ago • 5 comments

When cloning an element, the new element has all its attributes and content linked to the old. If you do any modifications to either one, both of them are modified. This also happens when copying elements (ctrl + c, ctrl + v). clone-bug

lahdekorpi avatar Feb 26 '21 09:02 lahdekorpi

Looking at the component JSON, these buttons get a "__symbol": "randomid here" when cloned. I'm assuming this is the reason why this behavior persists even when saving and reloading the contents.

lahdekorpi avatar Feb 26 '21 09:02 lahdekorpi

Ended up doing:

removeSymbols(model) {
	if (typeof model.attributes?.__symbol !== "undefined") {
		delete model.attributes.__symbol;
	}

	if (typeof model.attributes?.__symbols !== "undefined") {
		delete model.attributes.__symbols;
	}
}

And calling it on component:update and component:add. Not really sure if this has any side-effects but so far everything works. Does anybody know the use of the __symbol and __symbols? There is nothing about those in the documentation.

lahdekorpi avatar Feb 26 '21 09:02 lahdekorpi

Hi @lahdekorpi , where should I put this code for make it working?

yucomds avatar Mar 08 '21 14:03 yucomds

Hi @lahdekorpi , where should I put this code for make it working?

Something like this should work:

function removeSymbols(model) {
	if (typeof model.attributes?.__symbol !== "undefined") {
		delete model.attributes.__symbol;
	}

	if (typeof model.attributes?.__symbols !== "undefined") {
		delete model.attributes.__symbols;
	}
}
editor.on("add", model => removeSymbols(model));
editor.on("component:update", model => removeSymbols(model));

But just to be clear, I have no idea if this breaks anything else and this is a nasty hack, not a solution.

lahdekorpi avatar Mar 09 '21 14:03 lahdekorpi

Ok thank you. I'll try it in my application. Maybe this issue could be related to this one --> https://github.com/artf/grapesjs/issues/3325

yucomds avatar Mar 10 '21 09:03 yucomds

Yeah, this is related to an old bug in the core library.

artf avatar Oct 01 '22 12:10 artf