fix: 快捷键增加判断

This commit is contained in:
林熠 2020-08-12 22:41:06 +08:00
parent 9a12eb2a61
commit e18a2311d8
2 changed files with 18 additions and 3 deletions

View File

@ -77,7 +77,9 @@ hotkey.bind(['backspace', 'del'], (e: KeyboardEvent) => {
const topItems = sel.getTopNodes();
// TODO: check can remove
topItems.forEach((node) => {
if (node.canPerformAction('remove')) {
doc.removeNode(node);
}
});
sel.clear();
});
@ -102,8 +104,13 @@ hotkey.bind(['command+c', 'ctrl+c', 'command+x', 'ctrl+x'], (e, action) => {
}
e.preventDefault();
const selected = doc.selection.getTopNodes(true);
if (!selected || selected.length < 1) return;
let selected = doc.selection.getTopNodes(true);
selected = selected.filter((node) => {
return node.canPerformAction('copy');
})
if (!selected || selected.length < 1) {
return;
}
const componentsMap = {};
const componentsTree = selected.map((item) => item.export(TransformStage.Clone));

View File

@ -703,6 +703,14 @@ export class Node<Schema extends NodeSchema = NodeSchema> {
this.document.destroyNode(this);
}
/**
* action
*/
canPerformAction(action: string): boolean {
const availableActions = this.componentMeta?.availableActions?.map((action) => action.name);
return availableActions.indexOf(action) >= 0;
}
// ======= compatible apis ====
isEmpty(): boolean {
return this.children ? this.children.isEmpty() : true;