ferrum_flow/plugins/
delete.rs1use crate::{
2 Edge, Node,
3 canvas::Command,
4 plugin::{FlowEvent, Plugin},
5};
6
7pub struct DeletePlugin;
8
9impl DeletePlugin {
10 pub fn new() -> Self {
11 Self {}
12 }
13}
14
15impl Plugin for DeletePlugin {
16 fn name(&self) -> &'static str {
17 "delete"
18 }
19 fn setup(&mut self, _ctx: &mut crate::plugin::InitPluginContext) {}
20 fn on_event(
21 &mut self,
22 event: &FlowEvent,
23 ctx: &mut crate::plugin::PluginContext,
24 ) -> crate::plugin::EventResult {
25 if let FlowEvent::Input(crate::plugin::InputEvent::KeyDown(ev)) = event {
26 if ev.keystroke.key == "delete" || ev.keystroke.key == "backspace" {
27 ctx.execute_command(DeleteCommand::new(&ctx));
28 return crate::plugin::EventResult::Stop;
29 }
30 }
31 crate::plugin::EventResult::Continue
32 }
33}
34
35struct DeleteCommand {
36 selected_edge: Vec<Edge>,
37 selected_node: Vec<Node>,
38}
39
40impl DeleteCommand {
41 fn new(ctx: &crate::plugin::PluginContext) -> Self {
42 Self {
43 selected_edge: ctx
44 .graph
45 .selected_edge
46 .iter()
47 .filter_map(|id| ctx.graph.edges.get(id).cloned())
48 .collect(),
49 selected_node: ctx
50 .graph
51 .selected_node
52 .iter()
53 .filter_map(|id| ctx.get_node(id).cloned())
54 .collect(),
55 }
56 }
57}
58
59impl Command for DeleteCommand {
60 fn name(&self) -> &'static str {
61 "delete"
62 }
63 fn execute(&mut self, ctx: &mut crate::canvas::CanvasState) {
64 ctx.remove_selected_edge();
65 ctx.remove_selected_node();
66 }
67 fn undo(&mut self, ctx: &mut crate::canvas::CanvasState) {
68 for edge in &self.selected_edge {
69 ctx.add_edge(edge.clone());
70 ctx.add_selected_edge(edge.id, true);
71 }
72
73 for node in &self.selected_node {
74 ctx.add_node(node.clone());
75 ctx.add_selected_node(node.id, true);
76 }
77 }
78}