Fix: cycle through editors (#572)

* Fix: cycle through editors

* Fix: command is broken
This commit is contained in:
eight 2018-11-21 23:47:28 +08:00 committed by Rob Garrison
parent 9250d5c624
commit 25fb5acabe
2 changed files with 13 additions and 21 deletions

View File

@ -230,7 +230,7 @@
// editor commands // editor commands
for (const name of ['save', 'toggleStyle', 'nextEditor', 'prevEditor']) { for (const name of ['save', 'toggleStyle', 'nextEditor', 'prevEditor']) {
CodeMirror.commands[name] = () => editor[name](); CodeMirror.commands[name] = (...args) => editor[name](...args);
} }
// CodeMirror convenience commands // CodeMirror convenience commands

View File

@ -201,35 +201,27 @@ function createSectionsEditor(style) {
} }
function nextEditor(cm, cycle = true) { function nextEditor(cm, cycle = true) {
if (!cycle) { if (!cycle && findLast(sections, s => !s.isRemoved()).cm === cm) {
for (const section of sections) { return;
if (section.isRemoved()) {
continue;
}
if (cm === section.cm) {
return;
}
break;
}
} }
return nextPrevEditor(cm, 1); return nextPrevEditor(cm, 1);
} }
function prevEditor(cm, cycle = true) { function prevEditor(cm, cycle = true) {
if (!cycle) { if (!cycle && sections.find(s => !s.isRemoved()).cm === cm) {
for (let i = sections.length - 1; i >= 0; i--) { return;
if (sections[i].isRemoved()) {
continue;
}
if (cm === sections[i].cm) {
return;
}
break;
}
} }
return nextPrevEditor(cm, -1); return nextPrevEditor(cm, -1);
} }
function findLast(arr, match) {
for (let i = arr.length - 1; i >= 0; i--) {
if (match(arr[i])) {
return arr[i];
}
}
}
function nextPrevEditor(cm, direction) { function nextPrevEditor(cm, direction) {
const editors = getEditors(); const editors = getEditors();
cm = editors[(editors.indexOf(cm) + direction + editors.length) % editors.length]; cm = editors[(editors.indexOf(cm) + direction + editors.length) % editors.length];