Skip to content

Commit 39cefec

Browse files
committed
feat: add preference to disable table editor shortcuts
Signed-off-by: Yukai Huang <[email protected]>
1 parent 6a861a8 commit 39cefec

File tree

3 files changed

+62
-1
lines changed

3 files changed

+62
-1
lines changed

public/js/lib/editor/index.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -779,6 +779,41 @@ export default class Editor {
779779
overrideBrowserKeymap.change(() => {
780780
this.setOverrideBrowserKeymap()
781781
})
782+
783+
// Handle table editor shortcuts preference
784+
var disableTableShortcuts = $(
785+
'.ui-preferences-disable-table-shortcuts label > input[type="checkbox"]'
786+
)
787+
var cookieDisableTableShortcuts = Cookies.get(
788+
'preferences-disable-table-shortcuts'
789+
)
790+
if (cookieDisableTableShortcuts && cookieDisableTableShortcuts === 'true') {
791+
disableTableShortcuts.prop('checked', true)
792+
} else {
793+
disableTableShortcuts.prop('checked', false)
794+
}
795+
this.setTableShortcutsPreference()
796+
797+
disableTableShortcuts.change(() => {
798+
this.setTableShortcutsPreference()
799+
})
800+
}
801+
802+
setTableShortcutsPreference () {
803+
var disableTableShortcuts = $(
804+
'.ui-preferences-disable-table-shortcuts label > input[type="checkbox"]'
805+
)
806+
if (disableTableShortcuts.is(':checked')) {
807+
Cookies.set('preferences-disable-table-shortcuts', true, {
808+
expires: 365
809+
})
810+
} else {
811+
Cookies.remove('preferences-disable-table-shortcuts')
812+
}
813+
// Notify table editor about the preference change
814+
if (this.tableEditor) {
815+
this.tableEditor.setShortcutsEnabled(!disableTableShortcuts.is(':checked'))
816+
}
782817
}
783818

784819
init (textit) {

public/js/lib/editor/statusbar.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
</a>
1515
<ul class="dropdown-menu" aria-labelledby="preferencesLabel">
1616
<li class="ui-preferences-override-browser-keymap"><a><label>Allow override browser keymap&nbsp;&nbsp;<input type="checkbox"></label></a></li>
17+
<li class="ui-preferences-disable-table-shortcuts"><a><label>Disable table editor shortcuts&nbsp;&nbsp;<input type="checkbox"></label></a></li>
1718
</ul>
1819
</div>
1920
<div class="status-keymap dropup pull-right">

public/js/lib/editor/table-editor.js

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,28 @@ export function initTableEditor (editor) {
125125
smartCursor: true,
126126
formatType: FormatType.NORMAL
127127
})
128+
129+
// Flag to track if shortcuts are enabled
130+
let shortcutsEnabled = true
131+
132+
// Method to enable/disable shortcuts
133+
tableEditor.setShortcutsEnabled = function (enabled) {
134+
shortcutsEnabled = enabled
135+
// If shortcuts are disabled and currently active, remove the keymap
136+
if (!enabled && lastActive) {
137+
editor.removeKeyMap(keyMap)
138+
} else if (enabled && lastActive) {
139+
// If shortcuts are enabled and cursor is in table, add the keymap back
140+
editor.addKeyMap(keyMap)
141+
}
142+
}
143+
144+
// Check cookie for saved preference
145+
const cookieDisableTableShortcuts = window.Cookies && window.Cookies.get('preferences-disable-table-shortcuts')
146+
if (cookieDisableTableShortcuts && cookieDisableTableShortcuts === 'true') {
147+
shortcutsEnabled = false
148+
}
149+
128150
// keymap of the commands
129151
// from https://github.com/susisu/mte-demo/blob/master/src/main.js
130152
const keyMap = CodeMirror.normalizeKeyMap({
@@ -178,7 +200,10 @@ export function initTableEditor (editor) {
178200
if (active) {
179201
tableTools.show()
180202
tableTools.parent().scrollLeft(tableTools.parent()[0].scrollWidth)
181-
editor.addKeyMap(keyMap)
203+
// Only add keymap if shortcuts are enabled
204+
if (shortcutsEnabled) {
205+
editor.addKeyMap(keyMap)
206+
}
182207
} else {
183208
tableTools.hide()
184209
editor.removeKeyMap(keyMap)

0 commit comments

Comments
 (0)