From 726305cfbd872c521adf1e6b54eb9f29235dbd97 Mon Sep 17 00:00:00 2001 From: yuhongda Date: Mon, 19 Jun 2023 17:33:39 +0800 Subject: [PATCH] feat: choices support move for multiselect --- lib/elements/multiselect.js | 23 +++++++++++++++++++++++ lib/util/action.js | 9 +++++++-- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/lib/elements/multiselect.js b/lib/elements/multiselect.js index 99b393fa..193396cd 100644 --- a/lib/elements/multiselect.js +++ b/lib/elements/multiselect.js @@ -119,6 +119,28 @@ class MultiselectPrompt extends Prompt { this.render(); } + /** + * move current item up + */ + moveUp() { + if (this.cursor === 0) return this.bell(); + const choice = this.value.splice(this.cursor, 1)[0]; + this.value.splice(this.cursor - 1, 0, choice); + this.cursor--; + this.render(); + } + + /** + * move current item down + */ + moveDown() { + if (this.cursor + 1 === this.value.length) return this.bell(); + const choice = this.value.splice(this.cursor, 1)[0]; + this.value.splice(this.cursor + 1, 0, choice); + this.cursor++; + this.render(); + } + left() { this.value[this.cursor].selected = false; this.render(); @@ -172,6 +194,7 @@ class MultiselectPrompt extends Prompt { return '\nInstructions:\n' + ` ${figures.arrowUp}/${figures.arrowDown}: Highlight option\n` + ` ${figures.arrowLeft}/${figures.arrowRight}/[space]: Toggle selection\n` + + ` [shift]+${figures.arrowUp}/[shift]+${figures.arrowDown}: Move option\n` + (this.maxChoices === undefined ? ` a: Toggle all\n` : '') + ` enter/return: Complete answer`; } diff --git a/lib/util/action.js b/lib/util/action.js index fefbd947..7e430208 100644 --- a/lib/util/action.js +++ b/lib/util/action.js @@ -2,7 +2,7 @@ module.exports = (key, isSelect) => { if (key.meta && key.name !== 'escape') return; - + if (key.ctrl) { if (key.name === 'a') return 'first'; if (key.name === 'c') return 'abort'; @@ -10,7 +10,12 @@ module.exports = (key, isSelect) => { if (key.name === 'e') return 'last'; if (key.name === 'g') return 'reset'; } - + + if (key.shift) { + if (key.name === 'up') return 'moveUp'; + if (key.name === 'down') return 'moveDown'; + } + if (isSelect) { if (key.name === 'j') return 'down'; if (key.name === 'k') return 'up';