From a3e6ecddbb0410e2b9cdc3f322a8b03411f5c9b9 Mon Sep 17 00:00:00 2001 From: n00o Date: Tue, 24 Oct 2023 11:21:47 -0400 Subject: [PATCH] Add better CodeMirror folding options Add XML fold to Response and Body. Add count of object directly inside of fold. Include hotkeys Ctrl-Y to fold and Ctrl-I to unfold all XML/JSON data. --- .../src/components/CodeEditor/index.js | 29 +++++++++++++++++++ packages/bruno-app/src/pages/Bruno/index.js | 1 + 2 files changed, 30 insertions(+) diff --git a/packages/bruno-app/src/components/CodeEditor/index.js b/packages/bruno-app/src/components/CodeEditor/index.js index 6ad999e6b..b1a115c9e 100644 --- a/packages/bruno-app/src/components/CodeEditor/index.js +++ b/packages/bruno-app/src/components/CodeEditor/index.js @@ -70,6 +70,35 @@ export default class CodeEditor extends React.Component { 'Ctrl-F': 'findPersistent', Tab: function (cm) { cm.replaceSelection(' ', 'end'); + }, + 'Ctrl-Y': 'foldAll', + 'Cmd-Y': 'foldAll', + 'Ctrl-I': 'unfoldAll', + 'Cmd-I': 'unfoldAll' + }, + foldOptions: { + widget: (from, to) => { + var count = undefined; + var internal = this.editor.getRange(from, to); + if (this.props.mode == 'application/ld+json') { + if (this.editor.getLine(from.line).endsWith('[')) { + var toParse = '[' + internal + ']'; + } else var toParse = '{' + internal + '}'; + try { + count = Object.keys(JSON.parse(toParse)).length; + } catch (e) {} + } else if (this.props.mode == 'application/xml') { + var doc = new DOMParser(); + try { + //add header element and remove prefix namespaces for DOMParser + var dcm = doc.parseFromString( + ' ' + internal.replace(/(?<=\<|<\/)\w+:/g, '') + '', + 'application/xml' + ); + count = dcm.documentElement.children.length; + } catch (e) {} + } + return count ? `\u21A4${count}\u21A6` : '\u2194'; } } })); diff --git a/packages/bruno-app/src/pages/Bruno/index.js b/packages/bruno-app/src/pages/Bruno/index.js index 9f4de2434..f899933b9 100644 --- a/packages/bruno-app/src/pages/Bruno/index.js +++ b/packages/bruno-app/src/pages/Bruno/index.js @@ -21,6 +21,7 @@ if (!SERVER_RENDERED) { require('codemirror/addon/edit/matchbrackets'); require('codemirror/addon/fold/brace-fold'); require('codemirror/addon/fold/foldgutter'); + require('codemirror/addon/fold/xml-fold'); require('codemirror/addon/hint/show-hint'); require('codemirror/addon/lint/lint'); require('codemirror/addon/mode/overlay');