update keyboard shortcuts

This commit is contained in:
rlx 2019-01-25 12:39:10 +05:30
parent b4434ec404
commit 22f085527d

View file

@ -42,9 +42,21 @@ txtjs.addNoteFromSelection = function() {
txtjs.postMessage('addNote', note) 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() { txtjs.cancelEdit = function() {
let editing = document.querySelector('g.editing') let editing = Array.from(document.querySelectorAll('g.editing'))
editing && editing.classList.remove('editing') editing.forEach(function(element) {
element.classList.remove('editing')
})
} }
txtjs.createSVGElement = function(name) { txtjs.createSVGElement = function(name) {
@ -52,24 +64,27 @@ txtjs.createSVGElement = function(name) {
} }
txtjs.editNote = function() { txtjs.editNote = function() {
let editing = document.querySelector('g.editing') let editing = Array.from(document.querySelectorAll('g.editing'))
let note = txtjs.getNoteFromSelection() let note = txtjs.getNoteFromSelection()
if (!editing || !note) { if (editing.length == 0 || !note) {
return return
} }
let id = txtjs.getNoteId(editing) let id = txtjs.getNoteId(editing[0])
note = Object.assign(Ox.getObjectById(txtjs.notes, id), { note = Object.assign(Ox.getObjectById(txtjs.notes, id), {
position: note.position, position: note.position,
text: note.text text: note.text
}) })
document.querySelector('svg').removeChild(editing) editing.forEach(function(element) {
element.parentElement.removeChild(element)
})
txtjs.renderNote(note) txtjs.renderNote(note)
txtjs.selectNote(note.id) getSelection().removeAllRanges()
txtjs.postMessage('editNote', { txtjs.postMessage('editNote', {
id: id, id: id,
position: note.position, position: note.position,
text: note.text text: note.text
}) })
txtjs.selectNote(note.id)
} }
txtjs.getNewId = function() { txtjs.getNewId = function() {
@ -156,6 +171,12 @@ txtjs.getPosition = function(range) {
index += nodes[i].textContent.length 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 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) { txtjs.noteExists = function(note) {
return txtjs.notes.some(function(note_) { return txtjs.notes.some(function(note_) {
return note_.position == note.position return note_.position == note.position
@ -350,21 +361,27 @@ txtjs.renderText = function(text) {
window.addEventListener('scroll', onScroll) window.addEventListener('scroll', onScroll)
document.addEventListener('keydown', function(e) { document.addEventListener('keydown', function(e) {
console.log(e.keyCode) console.log(e.keyCode)
if (e.keyCode == 13) { // ENTER 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() txtjs.editNote()
} else {
txtjs.addNoteFromSelection()
}
} else if (e.keyCode == 27) { // ESCAPE } else if (e.keyCode == 27) { // ESCAPE
txtjs.selectNote(null) if (document.querySelector('g.editing')) {
txtjs.cancelEdit() txtjs.cancelEdit()
}
if (document.querySelector('g.selected')) {
txtjs.selectNote(null)
}
} else if (e.keyCode == 37) { // LEFT } else if (e.keyCode == 37) { // LEFT
txtjs.selectNextNote(-1) txtjs.selectNextNote(-1)
} else if (e.keyCode == 39) { // RIGHT } else if (e.keyCode == 39) { // RIGHT
txtjs.selectNextNote(1) 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') let style = document.createElement('style')
@ -465,7 +482,7 @@ txtjs.selectNote = function(id) {
} }
if (id) { if (id) {
let editing = Array.from(document.querySelectorAll('g.editing')) 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) { editing.forEach(function(element) {
element.classList.remove('editing') element.classList.remove('editing')
}) })