From c82b62f953aba3e9c2db6b02c42f5879345fc9c6 Mon Sep 17 00:00:00 2001 From: David Bolack Date: Fri, 19 Jul 2024 16:03:44 -0500 Subject: [PATCH] Add Code folding on CSS style tab --- client/homebrew/editor/editor.jsx | 2 +- shared/naturalcrit/codeEditor/codeEditor.jsx | 9 ++++-- shared/naturalcrit/codeEditor/fold-css.js | 32 +++++++++++++++++++ .../{fold-code.js => fold-pages.js} | 0 4 files changed, 39 insertions(+), 4 deletions(-) create mode 100644 shared/naturalcrit/codeEditor/fold-css.js rename shared/naturalcrit/codeEditor/{fold-code.js => fold-pages.js} (100%) diff --git a/client/homebrew/editor/editor.jsx b/client/homebrew/editor/editor.jsx index efcc9c861..b28c5dcf3 100644 --- a/client/homebrew/editor/editor.jsx +++ b/client/homebrew/editor/editor.jsx @@ -367,7 +367,7 @@ const Editor = createClass({ view={this.state.view} value={this.props.brew.style ?? DEFAULT_STYLE_TEXT} onChange={this.props.onStyleChange} - enableFolding={false} + enableFolding={true} editorTheme={this.state.editorTheme} rerenderParent={this.rerenderParent} /> ; diff --git a/shared/naturalcrit/codeEditor/codeEditor.jsx b/shared/naturalcrit/codeEditor/codeEditor.jsx index e624694f1..a42484827 100644 --- a/shared/naturalcrit/codeEditor/codeEditor.jsx +++ b/shared/naturalcrit/codeEditor/codeEditor.jsx @@ -39,8 +39,10 @@ if(typeof window !== 'undefined'){ //Autocompletion require('codemirror/addon/hint/show-hint.js'); - const foldCode = require('./fold-code'); - foldCode.registerHomebreweryHelper(CodeMirror); + const foldPagesCode = require('./fold-pages'); + foldPagesCode.registerHomebreweryHelper(CodeMirror); + const foldCSSCode = require('./fold-css'); + foldCSSCode.registerHomebreweryHelper(CodeMirror); } const CodeEditor = createClass({ @@ -411,7 +413,7 @@ const CodeEditor = createClass({ foldOptions : function(cm){ return { scanUp : true, - rangeFinder : CodeMirror.fold.homebrewery, + rangeFinder : this.props.language === 'css' ? CodeMirror.fold.homebrewerycss : CodeMirror.fold.homebrewery, widget : (from, to)=>{ let text = ''; let currentLine = from.line; @@ -450,3 +452,4 @@ const CodeEditor = createClass({ }); module.exports = CodeEditor; + diff --git a/shared/naturalcrit/codeEditor/fold-css.js b/shared/naturalcrit/codeEditor/fold-css.js new file mode 100644 index 000000000..29501c4c6 --- /dev/null +++ b/shared/naturalcrit/codeEditor/fold-css.js @@ -0,0 +1,32 @@ +module.exports = { + registerHomebreweryHelper : function(CodeMirror) { + CodeMirror.registerHelper('fold', 'homebrewerycss', function(cm, start) { + const startMatcher = /\{[ \t]*$/; + const endMatcher = /\}[ \t]*$/; + const prevLine = cm.getLine(start.line); + + if((start.line === cm.firstLine()) && (!cm.getLine(start.line).match(startMatcher))) return null; + + if(start.line === cm.firstLine() || prevLine.match(startMatcher)) { + const lastLineNo = cm.lastLine(); + let end = start.line + 1; + let braceCount = 1; + + while (end < lastLineNo) { + const curLine = cm.getLine(end); + if(curLine.match(startMatcher)) braceCount++; + if(curLine.match(endMatcher)) braceCount--; + if(braceCount == 0) break; + ++end; + } + + return { + from : CodeMirror.Pos(start.line, 0), + to : CodeMirror.Pos(end, cm.getLine(end).length) + }; + } + + return null; + }); + } +}; diff --git a/shared/naturalcrit/codeEditor/fold-code.js b/shared/naturalcrit/codeEditor/fold-pages.js similarity index 100% rename from shared/naturalcrit/codeEditor/fold-code.js rename to shared/naturalcrit/codeEditor/fold-pages.js