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())
.textcomplete([
{ // emoji strategy
@ -3317,29 +3338,10 @@ $(editor.getInputField())
},
'textComplete:show': function (e) {
$(this).data('autocompleting', true)
editor.setOption('extraKeys', {
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.addKeyMap(textCompleteKeyMap)
},
'textComplete:hide': function (e) {
$(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-Cmd-Down': () => { tableEditor.moveRow(1, opts) }
})
let lastActive
// enable keymap if the cursor is in a table
function updateActiveState () {
const tableTools = $('.toolbar .table-tools')
const active = tableEditor.cursorIsInTable(opts)
// avoid to update if state not changed
if (lastActive === active) {
return
}
if (active) {
tableTools.show()
tableTools.parent().scrollLeft(tableTools.parent()[0].scrollWidth)
editor.setOption('extraKeys', keyMap)
editor.addKeyMap(keyMap)
} else {
tableTools.hide()
editor.setOption('extraKeys', null)
editor.removeKeyMap(keyMap)
tableEditor.resetSmartCursor()
}
lastActive = active
}
// event subscriptions
editor.on('cursorActivity', () => {