Skip to content

Commit

Permalink
Paste when nothing is selected, fix #5523
Browse files Browse the repository at this point in the history
  • Loading branch information
lexoyo committed Nov 18, 2023
1 parent 8025b95 commit db79f33
Showing 1 changed file with 26 additions and 14 deletions.
40 changes: 26 additions & 14 deletions src/commands/view/PasteComponent.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { isArray, contains } from 'underscore';
import Component from '../../dom_components/model/Component';
import { CommandObject } from './CommandAbstract';
import Editor from '../../editor';

export default {
run(ed, s, opts = {}) {
Expand All @@ -11,23 +12,24 @@ export default {
if (clp && lastSelected) {
ed.getSelectedAll().forEach(selected => {
const { collection } = selected;
if (!collection) return;

let added;
const at = selected.index() + 1;
const addOpts = { at, action: opts.action || 'paste-component' };
if (collection) {
const at = selected.index() + 1;
const addOpts = { at, action: opts.action || 'paste-component' };

if (contains(clp, selected) && selected.get('copyable')) {
// @ts-ignore
added = collection.add(selected.clone(), addOpts);
} else {
const copyable = clp.filter(cop => cop.get('copyable'));
const pasteable = copyable.filter(cop => ed.Components.canMove(selected.parent()!, cop).result);
added = collection.add(
if (contains(clp, selected) && selected.get('copyable')) {
// @ts-ignore
pasteable.map(cop => cop.clone()),
addOpts
);
added = collection.add(selected.clone(), addOpts);
} else {
added = doAdd(ed, clp, selected.parent()!, addOpts);
}
} else {
// Page body is selected
// Paste at the end of the body
const pageBody = em.Pages.getSelected()?.getMainComponent();
const addOpts = { at: pageBody?.components().length || 0, action: opts.action || 'paste-component' };

added = doAdd(ed, clp, pageBody as Component, addOpts);
}

added = isArray(added) ? added : [added];
Expand All @@ -38,3 +40,13 @@ export default {
}
},
} as CommandObject;

function doAdd(ed: Editor, clp: Component[], parent: Component, addOpts: any): Component[] | Component {
const copyable = clp.filter(cop => cop.get('copyable'));
const pasteable = copyable.filter(cop => ed.Components.canMove(parent, cop).result);
return parent.components().add(
// @ts-ignore
pasteable.map(cop => cop.clone()),
addOpts
);
}

0 comments on commit db79f33

Please sign in to comment.