Implement toggleBlock

This commit is contained in:
Peter Stuifzand 2020-11-02 22:43:05 +01:00
parent 56802dc106
commit 8384c115c6

View File

@ -42,8 +42,7 @@ function editor(root, inputData, options) {
keymap.mapKey('Escape', 'leaveEditor') keymap.mapKey('Escape', 'leaveEditor')
keymap.mapKey('C-S-ArrowUp', 'blockMoveBackward') keymap.mapKey('C-S-ArrowUp', 'blockMoveBackward')
keymap.mapKey('C-S-ArrowDown', 'blockMoveForward') keymap.mapKey('C-S-ArrowDown', 'blockMoveForward')
keymap.mapKey('C-.', 'expandBlock') keymap.mapKey('C-.', 'toggleBlock')
keymap.mapKey('C-S->', 'collapseBlock')
function createStore(inputData) { function createStore(inputData) {
let data = [ let data = [
@ -157,7 +156,8 @@ function editor(root, inputData, options) {
blockMoveForward, blockMoveForward,
expandBlock, expandBlock,
collapseBlock collapseBlock,
toggleBlock
}; };
root.classList.add('root') root.classList.add('root')
@ -613,9 +613,11 @@ function editor(root, inputData, options) {
return false return false
} }
function expandBlock(event) { function toggleBlock(event, open) {
let open = true
store.update(cursor.getId(store), function (item) { store.update(cursor.getId(store), function (item) {
if (open === undefined) {
open = item.fold === 'closed'
}
item.fold = open ? 'open' : 'closed' item.fold = open ? 'open' : 'closed'
return item return item
}) })
@ -623,14 +625,12 @@ function editor(root, inputData, options) {
return false return false
} }
function expandBlock(event) {
return toggleBlock(event, true)
}
function collapseBlock(event) { function collapseBlock(event) {
let open = false return toggleBlock(event, false)
store.update(cursor.getId(store), function (item) {
item.fold = open ? 'open' : 'closed'
return item
})
trigger('change')
return false
} }
$(root).on('keydown', function (event) { $(root).on('keydown', function (event) {