update keyboard shortcuts

This commit is contained in:
rlx 2019-01-25 12:39:10 +05:30
parent b4434ec404
commit 22f085527d
1 changed files with 45 additions and 28 deletions

View File

@ -42,9 +42,21 @@ txtjs.addNoteFromSelection = function() {
txtjs.postMessage('addNote', note)
}
txtjs.beginEdit = function() {
let selected = txtjs.getSelectedNote()
if (!selected || !selected.elements[0].classList.contains('editable')) {
return
}
selected.elements.forEach(function(element) {
element.classList.add('editing')
})
}
txtjs.cancelEdit = function() {
let editing = document.querySelector('g.editing')
editing && editing.classList.remove('editing')
let editing = Array.from(document.querySelectorAll('g.editing'))
editing.forEach(function(element) {
element.classList.remove('editing')
})
}
txtjs.createSVGElement = function(name) {
@ -52,24 +64,27 @@ txtjs.createSVGElement = function(name) {
}
txtjs.editNote = function() {
let editing = document.querySelector('g.editing')
let editing = Array.from(document.querySelectorAll('g.editing'))
let note = txtjs.getNoteFromSelection()
if (!editing || !note) {
if (editing.length == 0 || !note) {
return
}
let id = txtjs.getNoteId(editing)
let id = txtjs.getNoteId(editing[0])
note = Object.assign(Ox.getObjectById(txtjs.notes, id), {
position: note.position,
text: note.text
})
document.querySelector('svg').removeChild(editing)
editing.forEach(function(element) {
element.parentElement.removeChild(element)
})
txtjs.renderNote(note)
txtjs.selectNote(note.id)
getSelection().removeAllRanges()
txtjs.postMessage('editNote', {
id: id,
position: note.position,
text: note.text
})
txtjs.selectNote(note.id)
}
txtjs.getNewId = function() {
@ -156,6 +171,12 @@ txtjs.getPosition = function(range) {
index += nodes[i].textContent.length
}
}
while (' \n'.includes(txtjs.text.substr(start, 1))) {
start++
}
while (' \n'.includes(txtjs.text.substr(end - 1, 1))) {
end--
}
return start + ':' + end
}
@ -197,16 +218,6 @@ txtjs.getSelectedNote = function() {
})
}
txtjs.moveNote = function() {
let selected = txtjs.getSelectedNote()
if (!selected || !selected.elements[0].classList.contains('editable')) {
return
}
selected.elements.forEach(function(element) {
element.classList.add('editing')
})
}
txtjs.noteExists = function(note) {
return txtjs.notes.some(function(note_) {
return note_.position == note.position
@ -350,21 +361,27 @@ txtjs.renderText = function(text) {
window.addEventListener('scroll', onScroll)
document.addEventListener('keydown', function(e) {
console.log(e.keyCode)
if (e.keyCode == 13) { // ENTER
txtjs.editNote()
if (e.keyCode == 8 || e.keyCode == 46) { // BACKSPACE || DELETE
txtjs.removeNote()
} else if (e.keyCode == 13) { // ENTER
if (e.shiftKey) {
txtjs.beginEdit()
} else if (document.querySelector('g.editing')) {
txtjs.editNote()
} else {
txtjs.addNoteFromSelection()
}
} else if (e.keyCode == 27) { // ESCAPE
txtjs.selectNote(null)
txtjs.cancelEdit()
if (document.querySelector('g.editing')) {
txtjs.cancelEdit()
}
if (document.querySelector('g.selected')) {
txtjs.selectNote(null)
}
} else if (e.keyCode == 37) { // LEFT
txtjs.selectNextNote(-1)
} else if (e.keyCode == 39) { // RIGHT
txtjs.selectNextNote(1)
} else if (e.keyCode == 46) { // DELETE
txtjs.removeNote()
} else if (e.keyCode == 77) { // M
txtjs.moveNote()
} else if (e.keyCode == 78) { // N
txtjs.addNoteFromSelection()
}
})
let style = document.createElement('style')
@ -465,7 +482,7 @@ txtjs.selectNote = function(id) {
}
if (id) {
let editing = Array.from(document.querySelectorAll('g.editing'))
if (editing.length && txtjs.getNoteId(editing) != id) {
if (editing.length && txtjs.getNoteId(editing[0]) != id) {
editing.forEach(function(element) {
element.classList.remove('editing')
})