-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor copy and paste into own feature
- Loading branch information
Showing
5 changed files
with
88 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { ContainerModule } from "inversify"; | ||
import { TYPES, configureCommand } from "sprotty"; | ||
import { CopyPasteKeyListener } from "./keyListener"; | ||
import { PasteElementsCommand } from "./pasteCommand"; | ||
|
||
/** | ||
* This feature allows the user to copy and paste elements. | ||
* When ctrl+c is pressed, all selected elements are copied into an internal array. | ||
* When ctrl+v is pressed, all elements in the internal array are pasted with an fixed offset. | ||
* Nodes are copied with their ports and edges are copied if source and target were copied as well. | ||
*/ | ||
export const copyPasteModule = new ContainerModule((bind, unbind, isBound, rebind) => { | ||
const context = { bind, unbind, isBound, rebind }; | ||
bind(TYPES.KeyListener).to(CopyPasteKeyListener).inSingletonScope(); | ||
configureCommand(context, PasteElementsCommand); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { injectable } from "inversify"; | ||
import { PasteElementsAction } from "./pasteCommand"; | ||
import { CommitModelAction, KeyListener, SModelElementImpl, SModelRootImpl, isSelected } from "sprotty"; | ||
import { Action } from "sprotty-protocol"; | ||
import { matchesKeystroke } from "sprotty/lib/utils/keyboard"; | ||
|
||
/** | ||
* This class is responsible for listening to ctrl+c and ctrl+v events. | ||
* On copy the selected elements are copied into an internal array. | ||
* On paste the {@link PasteElementsAction} is executed to paste the elements. | ||
* This is done inside a command, so that it can be undone/redone. | ||
*/ | ||
@injectable() | ||
export class CopyPasteKeyListener implements KeyListener { | ||
private copyElements: SModelElementImpl[] = []; | ||
|
||
keyUp(_element: SModelElementImpl, _event: KeyboardEvent): Action[] { | ||
return []; | ||
} | ||
|
||
keyDown(element: SModelElementImpl, event: KeyboardEvent): Action[] { | ||
if (matchesKeystroke(event, "KeyC", "ctrl")) { | ||
return this.copy(element.root); | ||
} else if (matchesKeystroke(event, "KeyV", "ctrl")) { | ||
return this.paste(); | ||
} | ||
|
||
return []; | ||
} | ||
|
||
/** | ||
* Copy all selected elements into the "clipboard" (the internal element array) | ||
*/ | ||
private copy(root: SModelRootImpl): Action[] { | ||
this.copyElements = []; // Clear the clipboard | ||
|
||
// Find selected elements | ||
root.index | ||
.all() | ||
.filter((element) => isSelected(element)) | ||
.forEach((e) => this.copyElements.push(e)); | ||
|
||
return []; | ||
} | ||
|
||
/** | ||
* Pastes elements by creating new elements and copying the properties of the copied elements. | ||
* This is done inside a command, so that it can be undone/redone. | ||
*/ | ||
private paste(): Action[] { | ||
return [PasteElementsAction.create(this.copyElements), CommitModelAction.create()]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters