From ce8683e8e011f336554cc6358e2e884fe089d145 Mon Sep 17 00:00:00 2001
From: Alexander Onnikov <Alexander.Onnikov@xored.com>
Date: Sat, 13 Jul 2024 22:19:39 +0700
Subject: [PATCH] UBERF-7587 Add table hotkeys (#6066)

Signed-off-by: Alexander Onnikov <Alexander.Onnikov@xored.com>
---
 .../src/components/extension/table/table.ts   | 26 ++++++++++++++++++-
 1 file changed, 25 insertions(+), 1 deletion(-)

diff --git a/plugins/text-editor-resources/src/components/extension/table/table.ts b/plugins/text-editor-resources/src/components/extension/table/table.ts
index a6dd71606b..f9f6426f31 100644
--- a/plugins/text-editor-resources/src/components/extension/table/table.ts
+++ b/plugins/text-editor-resources/src/components/extension/table/table.ts
@@ -13,12 +13,36 @@
 // limitations under the License.
 //
 
+import { type Editor } from '@tiptap/core'
 import TiptapTable from '@tiptap/extension-table'
-import TableNodeView from './TableNodeView.svelte'
+import { CellSelection } from '@tiptap/pm/tables'
 import { SvelteNodeViewRenderer } from '../../node-view'
+import TableNodeView from './TableNodeView.svelte'
+import { isTableSelected } from './utils'
 
 export const Table = TiptapTable.extend({
+  addKeyboardShortcuts () {
+    return {
+      'Mod-Backspace': () => handleDelete(this.editor),
+      'Mod-Delete': () => handleDelete(this.editor)
+    }
+  },
   addNodeView () {
     return SvelteNodeViewRenderer(TableNodeView, {})
   }
 })
+
+function handleDelete (editor: Editor): boolean {
+  const { selection } = editor.state
+  if (selection instanceof CellSelection) {
+    if (isTableSelected(selection)) {
+      return editor.commands.deleteTable()
+    } else if (selection.isColSelection()) {
+      return editor.commands.deleteColumn()
+    } else if (selection.isRowSelection()) {
+      return editor.commands.deleteRow()
+    }
+  }
+
+  return false
+}