Copy Paste Components
Question
Hello Grapesjs Team Ive seen the regular copy paste works as long as is made in the same page, Is it possible implement a a copy paste between 2 different pages with the editor opened
Thanks in advance
Answers (3)
This is our implementation of using localStorage in case someone needs the solution.
function newCopy(selected) {
window.localStorage.setItem('grapesjs_clipboard', JSON.stringify(selected));
}
function newPaste(selected) {
var components = JSON.parse(window.localStorage.getItem('grapesjs_clipboard'));
if (components) {
if (selected && selected.attributes.type !== 'wrapper') {
var index = selected.index();
// Invert the order so last item gets added first and gets pushed down as others get added.
components.reverse();
var currentSelection = selected.collection;
components.forEach(comp => {
// TODO: Add check for validity of paste.
var added = currentSelection.add(comp, {at: index + 1});
editor.trigger('component:paste', added);
});
selected.emitUpdate();
} else {
// No components are selected so just insert at the end.
editor.addComponents(components);
}
}
}
const commands = editor.Commands;
commands.add('core:copy', editor => {
const selected = [...editor.getSelectedAll()];
//Filter out components that are not copyable.
var filteredSelected = selected.filter(item => item.attributes.copyable == true);
if (filteredSelected.length) {
newCopy(filteredSelected);
}
});
commands.add('core:paste', editor => {
const selected = editor.getSelected();
newPaste(selected);
});
Clipboard API would have been a great cross browser solution but it's not yet widely used.
I didn't try it, I just read the code
You don't trigger the paste event when pasting in the body
Also the css is added in a style tag after each paste... There must be a better way to handle the styles, I'll take a look soon
Is it possible implement a a copy paste between 2 different pages with the editor opened
Well, if for 2 different pages you mean 2 different tabs, I don't think so. Obviously, you're dealing with different editor instances so you have to find your way to make them talk with each other (eg. localStorage if you're on the same domain)
Related Questions and Answers
Continue research with similar issue discussions.
Issue #3289
BUG: (0.16.41) Components have lost their styles
Version: 0.16.41 Are you able to reproduce the bug from the demo? [X] Yes [ ] No What is the expected behavior? 1) Copy selected parent com...
Issue #1329
Question: How to use same CSS for Multiple pages
Hello Grapejs Team, We've been working on Multi-page Website Builder. For each project there will be multiple html pages with single common...
Issue #1143
How to enable progamatic placeholder block insert
Hello grapesjs Team I succesfully created a component with traits and a block to insert it, when i drag n drop the block there is a green l...
Issue #1193
[QUESTION] Not able to initialize colorpicker !
Hi @artf , I made separate styles for mobile view . i want to initialize color-picker "onclick()". i want exactly same color-picker that yo...
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.