From 911e3aa589714933ed3edc7cb74781cf16d3a067 Mon Sep 17 00:00:00 2001 From: Sam Ho Date: Tue, 6 Aug 2024 13:06:51 +0100 Subject: [PATCH] feat: comment with keybinding (cmd + /) in JSON payload interface (#2634) Co-authored-by: Sam Ho --- .../src/components/CodeEditor/index.js | 31 +++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/packages/bruno-app/src/components/CodeEditor/index.js b/packages/bruno-app/src/components/CodeEditor/index.js index c67b22668..85f6c2da4 100644 --- a/packages/bruno-app/src/components/CodeEditor/index.js +++ b/packages/bruno-app/src/components/CodeEditor/index.js @@ -16,6 +16,7 @@ import stripJsonComments from 'strip-json-comments'; let CodeMirror; const SERVER_RENDERED = typeof navigator === 'undefined' || global['PREVENT_CODEMIRROR_RENDER'] === true; +const TAB_SIZE = 2; if (!SERVER_RENDERED) { CodeMirror = require('codemirror'); @@ -121,7 +122,7 @@ export default class CodeEditor extends React.Component { value: this.props.value || '', lineNumbers: true, lineWrapping: true, - tabSize: 2, + tabSize: TAB_SIZE, mode: this.props.mode || 'application/ld+json', keyMap: 'sublime', autoCloseBrackets: true, @@ -169,7 +170,33 @@ export default class CodeEditor extends React.Component { 'Ctrl-Y': 'foldAll', 'Cmd-Y': 'foldAll', 'Ctrl-I': 'unfoldAll', - 'Cmd-I': 'unfoldAll' + 'Cmd-I': 'unfoldAll', + 'Cmd-/': (cm) => { + // comment/uncomment every selected line(s) + const selections = cm.listSelections(); + selections.forEach((range) => { + for (let i = range.from().line; i <= range.to().line; i++) { + const selectedLine = cm.getLine(i); + // if commented line, remove comment + if (selectedLine.trim().startsWith('//')) { + cm.replaceRange( + selectedLine.replace(/^(\s*)\/\/\s?/, '$1'), + { line: i, ch: 0 }, + { line: i, ch: selectedLine.length } + ); + continue; + } + // otherwise add comment + cm.replaceRange( + selectedLine.search(/\S|$/) >= TAB_SIZE + ? ' '.repeat(TAB_SIZE) + '// ' + selectedLine.trim() + : '// ' + selectedLine, + { line: i, ch: 0 }, + { line: i, ch: selectedLine.length } + ); + } + }); + } }, foldOptions: { widget: (from, to) => {