Browse Source

Paste when nothing is selected, fixes #5523 (#5524)

pull/5546/head
Alex Hoyau 2 years ago
committed by GitHub
parent
commit
0a651c3edd
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 38
      src/commands/view/PasteComponent.ts

38
src/commands/view/PasteComponent.ts

@ -1,6 +1,7 @@
import { isArray, contains } from 'underscore'; import { isArray, contains } from 'underscore';
import Component from '../../dom_components/model/Component'; import Component from '../../dom_components/model/Component';
import { CommandObject } from './CommandAbstract'; import { CommandObject } from './CommandAbstract';
import Editor from '../../editor';
export default { export default {
run(ed, s, opts = {}) { run(ed, s, opts = {}) {
@ -12,21 +13,24 @@ export default {
ed.getSelectedAll().forEach(sel => { ed.getSelectedAll().forEach(sel => {
const selected = sel.delegate?.copy?.(sel) || sel; const selected = sel.delegate?.copy?.(sel) || sel;
const { collection } = selected; const { collection } = selected;
if (!collection) return;
let added; let added;
const at = selected.index() + 1; if (collection) {
const addOpts = { at, action: opts.action || 'paste-component' }; const at = selected.index() + 1;
const addOpts = { at, action: opts.action || 'paste-component' };
if (contains(clp, selected) && selected.get('copyable')) { if (contains(clp, selected) && selected.get('copyable')) {
added = collection.add(selected.clone(), addOpts); // @ts-ignore
added = collection.add(selected.clone(), addOpts);
} else {
added = doAdd(ed, clp, selected.parent()!, addOpts);
}
} else { } else {
const copyable = clp.filter(cop => cop.get('copyable')); // Page body is selected
const pasteable = copyable.filter(cop => ed.Components.canMove(selected.parent()!, cop).result); // Paste at the end of the body
added = collection.add( const pageBody = em.Pages.getSelected()?.getMainComponent();
pasteable.map(cop => cop.clone()), const addOpts = { at: pageBody?.components().length || 0, action: opts.action || 'paste-component' };
addOpts
); added = doAdd(ed, clp, pageBody as Component, addOpts);
} }
added = isArray(added) ? added : [added]; added = isArray(added) ? added : [added];
@ -37,3 +41,13 @@ export default {
} }
}, },
} as CommandObject; } 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
);
}

Loading…
Cancel
Save