From 25fb5acabee7450126e7e045bee21c96c994796b Mon Sep 17 00:00:00 2001 From: eight Date: Wed, 21 Nov 2018 23:47:28 +0800 Subject: [PATCH] Fix: cycle through editors (#572) * Fix: cycle through editors * Fix: command is broken --- edit/codemirror-default.js | 2 +- edit/sections-editor.js | 32 ++++++++++++-------------------- 2 files changed, 13 insertions(+), 21 deletions(-) diff --git a/edit/codemirror-default.js b/edit/codemirror-default.js index 2885b08b..d0f6b8c2 100644 --- a/edit/codemirror-default.js +++ b/edit/codemirror-default.js @@ -230,7 +230,7 @@ // editor commands for (const name of ['save', 'toggleStyle', 'nextEditor', 'prevEditor']) { - CodeMirror.commands[name] = () => editor[name](); + CodeMirror.commands[name] = (...args) => editor[name](...args); } // CodeMirror convenience commands diff --git a/edit/sections-editor.js b/edit/sections-editor.js index a06223d3..79605374 100644 --- a/edit/sections-editor.js +++ b/edit/sections-editor.js @@ -201,35 +201,27 @@ function createSectionsEditor(style) { } function nextEditor(cm, cycle = true) { - if (!cycle) { - for (const section of sections) { - if (section.isRemoved()) { - continue; - } - if (cm === section.cm) { - return; - } - break; - } + if (!cycle && findLast(sections, s => !s.isRemoved()).cm === cm) { + return; } return nextPrevEditor(cm, 1); } function prevEditor(cm, cycle = true) { - if (!cycle) { - for (let i = sections.length - 1; i >= 0; i--) { - if (sections[i].isRemoved()) { - continue; - } - if (cm === sections[i].cm) { - return; - } - break; - } + if (!cycle && sections.find(s => !s.isRemoved()).cm === cm) { + return; } 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) { const editors = getEditors(); cm = editors[(editors.indexOf(cm) + direction + editors.length) % editors.length];