fastKey.tsx 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import { Cell, Graph } from "@antv/x6";
  2. import { TopicType } from "@/enum";
  3. import { MindMapProjectInfo } from "@/types";
  4. import { handleSetAttr } from "@/utils/hander";
  5. import {
  6. addPeerTopic,
  7. addChildrenTopic,
  8. deleteTopics,
  9. handleMindmapPaste,
  10. } from "./mindmapHander";
  11. export const bindMindmapKeys = (
  12. graph: Graph,
  13. setMindProjectInfo?: (data: any) => void
  14. ) => {
  15. // Enter 增加同级主题
  16. graph.bindKey("enter", (e: KeyboardEvent) => {
  17. e.preventDefault();
  18. const branch = graph
  19. .getSelectedCells()
  20. .find((cell) => cell.data?.type !== TopicType.sub && cell.isNode());
  21. if (branch) {
  22. addPeerTopic(branch, graph, setMindProjectInfo);
  23. return;
  24. }
  25. const sub = graph
  26. .getSelectedCells()
  27. .find((cell) => cell.data?.type === TopicType.sub && cell.isNode());
  28. if (sub) {
  29. sub.isNode() && addPeerTopic(sub, graph, setMindProjectInfo);
  30. }
  31. });
  32. // Tab 增加子主题
  33. graph.bindKey(["tab", "insert"], (e: KeyboardEvent) => {
  34. e.preventDefault();
  35. const node = graph.getSelectedCells().find((cell) => cell.isNode());
  36. if (node) {
  37. node.isNode() && addChildrenTopic(node, graph, setMindProjectInfo);
  38. }
  39. });
  40. // delete 删除
  41. graph.bindKey("delete", (e: KeyboardEvent) => {
  42. e.preventDefault();
  43. const topicIds = graph
  44. .getSelectedCells()
  45. .filter((cell) => {
  46. return cell.isNode() && !(cell.data?.ignoreDrag || cell.data?.lock )
  47. })
  48. .map((cell) => cell.id);
  49. deleteTopics(topicIds, graph, setMindProjectInfo);
  50. });
  51. // Ctrl + A 全选
  52. graph.bindKey("ctrl+a", (e: KeyboardEvent) => {
  53. const cells = graph.getNodes();
  54. graph.select(cells);
  55. });
  56. // Ctrl + + 放大
  57. graph.bindKey("ctrl+=", (e: KeyboardEvent) => {
  58. graph.zoomTo(graph.zoom() + 0.1);
  59. });
  60. // Ctrl + - 缩小
  61. graph.bindKey("ctrl+-", (e: KeyboardEvent) => {
  62. graph.zoomTo(graph.zoom() - 0.1);
  63. });
  64. // esc 取消
  65. graph.bindKey("esc", (e: KeyboardEvent) => {
  66. graph.cleanSelection();
  67. graph.trigger("cancel");
  68. });
  69. // Ctrl+Alt+M 插入评论
  70. graph.bindKey("ctrl+alt+m", (e: KeyboardEvent) => {
  71. // todo
  72. });
  73. // Ctrl+B 加粗
  74. graph.bindKey("ctrl+b", (e: KeyboardEvent) => {
  75. handleSetAttr(graph, "text.bold", true);
  76. });
  77. // Ctrl+I 斜体
  78. graph.bindKey("ctrl+i", (e: KeyboardEvent) => {
  79. handleSetAttr(graph, "text.italic", true);
  80. });
  81. // Ctrl+U 下划线
  82. graph.bindKey("ctrl+u", (e: KeyboardEvent) => {
  83. handleSetAttr(graph, "text.textDecoration", "underline");
  84. });
  85. // Ctrl+c 复制
  86. graph.bindKey("ctrl+c", (e: KeyboardEvent) => {
  87. localStorage.setItem(
  88. "mindmap-copy-data",
  89. JSON.stringify(graph.getSelectedCells())
  90. );
  91. navigator.clipboard.writeText(" ");
  92. });
  93. // Ctrl+x 剪切
  94. // graph.bindKey("ctrl+x", (e: KeyboardEvent) => {
  95. // graph.cut(graph.getSelectedCells());
  96. // });
  97. // Ctrl+v 粘贴
  98. graph.bindKey("ctrl+v", (e: KeyboardEvent) => {
  99. setMindProjectInfo && handleMindmapPaste(graph, setMindProjectInfo);
  100. });
  101. // Ctrl+z 撤销
  102. graph.bindKey("ctrl+z", (e: KeyboardEvent) => {
  103. graph.undo();
  104. });
  105. // Ctrl+y 重做
  106. graph.bindKey("ctrl+y", (e: KeyboardEvent) => {
  107. graph.redo();
  108. });
  109. };