Pasting a component in root body layer throws TypeError
Thanks @zachsnoek yeah, as the wrapper (body) is a root component it doesn't have the related collection. Probably we might need to update the paste logic but your current fix is a good patch for now to avoid such an error so, the PR is highly welcome π
Read full answer below βQuestion
GrapesJS version
- I confirm to use the latest version of GrapesJS
What browser are you using?
Chrome v89
Reproducible demo link
https://grapesjs.com/demo.html
Describe the bug
How to reproduce the bug?
- In the official demo, navigate to the layer manager and copy any layer with
cmd+c - Click the root "Body" layer and paste with
cmd+p - Navigate to the browser console and see the error
What is the expected behavior? Pasting in the body layer probably shouldn't be allowed, but it shouldn't throw an error.
What is the current behavior?
Pasting in the root Body layer throws this exception:
Uncaught TypeError: Cannot read property 'indexOf' of undefined
at PasteComponent.js:13
at Array.forEach (<anonymous>)
at run (PasteComponent.js:10)
at r.callRun (CommandAbstract.js:90)
at Object.runCommand (index.js:388)
at Object.method (index.js:158)
at w (keymaster.js:143)
at HTMLDocument.<anonymous> (keymaster.js:330)
The error is coming from the paste commend; comp.collection is undefined.
I don't know this codebase well enough to know if .collection on this component should return children. We were able to fix this temporarily by overriding the paste command and adding the following guard:
// ...
const coll = comp.collection;
if (!coll) {
return;
}
const at = coll.indexOf(comp) + 1;
// ...
If this is a fine solution, let me know and I'll happily open a PR.
Code of Conduct
- I agree to follow this project's Code of Conduct
Answers (4)
Thanks @zachsnoek yeah, as the wrapper (body) is a root component it doesn't have the related collection. Probably we might need to update the paste logic but your current fix is a good patch for now to avoid such an error so, the PR is highly welcome π
Thanks @zachsnoek yeah, as the wrapper (body) is a root component it doesn't have the related collection. Probably we might need to update the paste logic but your current fix is a good patch for now to avoid such an error so, the PR is highly welcome π
Sounds good; I'll put up a PR by the end of the week :+1:
Guys, I'm facing a similar problem To me, the error happens on FileUploader

I've tried to enter on grapes demo to see if this behavior happens there too, and it's happening!
I realize that If I clean my LocalStorage and reload the page, then the Editor loads the standard template, and I can drop any block inside it that will work well, but if I clean the editor and try to drop a Text block on Editor, the Editor saves an image tag in HTML key Take a look bellow

Any Idea that can help to solve this problem?
Thanks for reporting this, @zachsnoek.
Great suggestion about Pasting a component in root body layer throws TypeError! While this specific feature isn't yet in the core API, there are several ways to achieve similar behavior.
Using the event system:
editor.on('component:update', (component) => {
// your logic here
});
Alternative approaches:
- Listen to
selector:addfor CSS selector changes - Use
selector:customfor custom rules - Tap into the
change:*events for fine-grained tracking - Build a plugin that extends the editor with this capability
Making it official: If this feature would benefit many users, consider opening a formal Feature Request on the GrapesJS repo with:
- A detailed use case
- Code example showing the desired behavior
- Why this matters for your workflow
The core team is receptive to well-motivated feature requests backed by real use cases.
Related Questions and Answers
Continue research with similar issue discussions.
Issue #6281
Layer Manager does not allow reordering on same level. only nesting is allowed on top level(right under body) older versions (e.g: 0.21.8
GrapesJS version [X] I confirm to use the latest version of GrapesJS What browser are you using? latest chrome, firefox, edge Reproducible...
Issue #6582
Single pointer mode of operation is not provided for editing element drag-and-drop actions.
GrapesJS version [x] I confirm to use the latest version of GrapesJS What browser are you using? Edge: Version 135.0.2535.51 (Official buil...
Issue #5378
Uncaught TypeError: Cannot read properties of undefined (reading 'Canvas') while dragging a component in Layer Manager Panel
GrapesJS version [X] I confirm to use the latest version of GrapesJS What browser are you using? Chrome v116.0.5845.97 Reproducible demo li...
Issue #6032
Image Component allows upload and selection of video files via drag and drop
GrapesJS version [X] I confirm to use the latest version of GrapesJS What browser are you using? Chrome v126 Reproducible demo link https:/...
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.
Tutorial
Find the Right GrapesJS Plugin in Seconds: Smarter Discovery Is Live
We're shipping a set of discovery upgrades. New label filters, a proper compatibility switch for GrapesJS vs Studio, one-click and a smarter sort bar.
Tutorial
GJS Market 2.0 - Donations, Tracking, Labels and Better Product Discovery
Weβve rolled out a new set of GrapesJS marketplace updates across GJS Market, focused on improving how creators distribute products
Tutorial
Preset DevFuture 2.0 β New Update Released
Preset continues evolving as a clean, production-ready starting point for building visual editors powered by GrapesJS
Browse Plugin Categories
Jump directly to plugin category pages on the marketplace.