Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Paste when nothing is selected, fix #5523 #5524

Merged
merged 2 commits into from
Nov 29, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
);
}