fix: table editor key map might conflict with textcomplete keymap

and avoid add/remove keymap on every updateActiveState

Signed-off-by: Max Wu <jackymaxj@gmail.com>
This commit is contained in:
Max Wu 2019-10-26 15:02:35 +08:00
parent e03a326c26
commit 052c787c0a
2 changed files with 31 additions and 23 deletions

View File

@ -3116,6 +3116,27 @@ function matchInContainer (text) {
} }
} }
const textCompleteKeyMap = {
Up: function () {
return false
},
Right: function () {
editor.doc.cm.execCommand('goCharRight')
},
Down: function () {
return false
},
Left: function () {
editor.doc.cm.execCommand('goCharLeft')
},
Enter: function () {
return false
},
Backspace: function () {
editor.doc.cm.execCommand('delCharBefore')
}
}
$(editor.getInputField()) $(editor.getInputField())
.textcomplete([ .textcomplete([
{ // emoji strategy { // emoji strategy
@ -3317,29 +3338,10 @@ $(editor.getInputField())
}, },
'textComplete:show': function (e) { 'textComplete:show': function (e) {
$(this).data('autocompleting', true) $(this).data('autocompleting', true)
editor.setOption('extraKeys', { editor.addKeyMap(textCompleteKeyMap)
Up: function () {
return false
},
Right: function () {
editor.doc.cm.execCommand('goCharRight')
},
Down: function () {
return false
},
Left: function () {
editor.doc.cm.execCommand('goCharLeft')
},
Enter: function () {
return false
},
Backspace: function () {
editor.doc.cm.execCommand('delCharBefore')
}
})
}, },
'textComplete:hide': function (e) { 'textComplete:hide': function (e) {
$(this).data('autocompleting', false) $(this).data('autocompleting', false)
editor.setOption('extraKeys', editorInstance.defaultExtraKeys) editor.removeKeyMap(textCompleteKeyMap)
} }
}) })

View File

@ -166,19 +166,25 @@ export function initTableEditor (editor) {
'Alt-Shift-Ctrl-Down': () => { tableEditor.moveRow(1, opts) }, 'Alt-Shift-Ctrl-Down': () => { tableEditor.moveRow(1, opts) },
'Alt-Shift-Cmd-Down': () => { tableEditor.moveRow(1, opts) } 'Alt-Shift-Cmd-Down': () => { tableEditor.moveRow(1, opts) }
}) })
let lastActive
// enable keymap if the cursor is in a table // enable keymap if the cursor is in a table
function updateActiveState () { function updateActiveState () {
const tableTools = $('.toolbar .table-tools') const tableTools = $('.toolbar .table-tools')
const active = tableEditor.cursorIsInTable(opts) const active = tableEditor.cursorIsInTable(opts)
// avoid to update if state not changed
if (lastActive === active) {
return
}
if (active) { if (active) {
tableTools.show() tableTools.show()
tableTools.parent().scrollLeft(tableTools.parent()[0].scrollWidth) tableTools.parent().scrollLeft(tableTools.parent()[0].scrollWidth)
editor.setOption('extraKeys', keyMap) editor.addKeyMap(keyMap)
} else { } else {
tableTools.hide() tableTools.hide()
editor.setOption('extraKeys', null) editor.removeKeyMap(keyMap)
tableEditor.resetSmartCursor() tableEditor.resetSmartCursor()
} }
lastActive = active
} }
// event subscriptions // event subscriptions
editor.on('cursorActivity', () => { editor.on('cursorActivity', () => {