Skip to content

Commit

Permalink
No commit message
Browse files Browse the repository at this point in the history
  • Loading branch information
Josef Gabrielsson committed Aug 5, 2024
1 parent 45f527d commit 6989a6a
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions nightly/modules/keyboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ app.module.keyboard = {

app.listeners.add(document, 'keydown', function (e) {
self._keytranslated(e)
self._keyuntranslated(e)
})
},

Expand Down Expand Up @@ -46,6 +47,39 @@ app.module.keyboard = {
}
},

_keyuntranslated: function (e) {
var untranslate = e.target.getAttribute('keyboard-untranslate')
if (untranslate && e.key === untranslate.split(':')[0]) {

var value = untranslate.split(':')[1],
selection = window.getSelection(),
range = selection.getRangeAt(0),
startNode = range.startContainer,
startOffset = range.startOffset,
text = startNode.textContent

if (startOffset >= 2) {
var twoSpacesBefore = text.substring(startOffset - 2, startOffset)
if (twoSpacesBefore === value) {
// Remove the two spaces
var newText = text.substring(0, startOffset - 2) + text.substring(startOffset)
startNode.textContent = newText

// Optionally, reposition the cursor to the start of the selection
var newRange = document.createRange()
newRange.setStart(startNode, startOffset - 2)
newRange.collapse(true)
selection.removeAllRanges()
selection.addRange(newRange)

e.preventDefault() // Prevent default backspace behavior.
} else {
console.log('skip')
}
}
}
},

_keypressed: function (e) {
var isBodyScope = document.activeElement === document.body ? 'body' : false

Expand Down

0 comments on commit 6989a6a

Please sign in to comment.