BUG: Component script function runs twice after page save
Question
GrapesJS version
- I confirm to use the latest version of GrapesJS
What browser are you using?
112.0.5615.137 (Official Build) (arm64)
Reproducible demo link
https://grapesjs.com/demo.html
Describe the bug
Component definition:
editor.DomComponents.addType("my-test", {
model: {
defaults: {
name: "Testing",
components: "Hello testing",
script: function () {
alert("hello");
},
},
},
});
When dropped, saved, and reload there are two places that call the same script. One in the saved HTML/JSON and one added by Grapesjs itself.
<script class="">
var items = document.querySelectorAll('#if1fgr');
for (var i = 0, len = items.length; i < len; i++) {
(function(){
alert("hello");
}.bind(items[i]))();
}
</script>
------------------------------------------------------------------------------------------------------------------
<div data-id="if1fgr">
<script>
setTimeout(function() {
var item = document.getElementById('if1fgr');
if (!item) return;
(function(){
alert("hello");
;}.bind(item))({})
}, 1);
</script>
</div>
How to reproduce the bug?
- Add a custom component type with a script function.
- Drop it and save the page.
- Reloading the page runs the script function twice.
What is the expected behavior? The script function should be running only once.
What is the current behavior? The script function runs twice.
Code of Conduct
- I agree to follow this project's Code of Conduct
Answers (1)
As explained here you should not reimport the output HTML to edit the project, only the JSON.
Related Questions and Answers
Continue research with similar issue discussions.
Issue #5173
BUG: editor.Css.setRule API not working as expected, it is not updating the rule existing rule.
GrapesJS version [X] I confirm to use the latest version of GrapesJS What browser are you using? Version 112.0.5615.137 (Official Build) (a...
Issue #5352
BUG: Uncaught SyntaxError: Unexpected token '{' at CanvasView.ts:590:12
GrapesJS version [X] I confirm to use the latest version of GrapesJS What browser are you using? Version 1.56.20 Chromium: 115.0.5790.171 (...
Issue #4094
BUG: When drag and drop block into editor in Chrome, not able to drag and drop block
GrapesJS version [X] I confirm to use the latest version of GrapesJS What browser are you using? Version 97.0.4692.71 (Official Build) (64-...
Issue #5086
BUG: Style:property:update and Property onChange trigger excessively
GrapesJS version [X] I confirm to use the latest version of GrapesJS What browser are you using? Chrome Version 112.0.5615.137 (Official Bu...
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.