diff --git a/client/homebrew/editor/editor.jsx b/client/homebrew/editor/editor.jsx index cb5d122f1..1ecdcb22b 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..3186e39f1 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,11 +413,11 @@ 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; - const maxLength = 50; + let maxLength = 50; let foldPreviewText = ''; while (currentLine <= to.line && text.length <= maxLength) { @@ -430,10 +432,15 @@ const CodeEditor = createClass({ } } text = foldPreviewText || `Lines ${from.line+1}-${to.line+1}`; + text = text.replace('{', '').trim(); + + // Truncate data URLs at `data:` + const startOfData = text.indexOf('data:'); + if(startOfData > 0) + maxLength = Math.min(startOfData + 5, maxLength); - text = text.trim(); if(text.length > maxLength) - text = `${text.substr(0, maxLength)}...`; + text = `${text.slice(0, maxLength)}...`; return `\u21A4 ${text} \u21A6`; } @@ -450,3 +457,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..338cab176 --- /dev/null +++ b/shared/naturalcrit/codeEditor/fold-css.js @@ -0,0 +1,44 @@ +module.exports = { + registerHomebreweryHelper : function(CodeMirror) { + CodeMirror.registerHelper('fold', 'homebrewerycss', function(cm, start) { + + // BRACE FOLDING + const startMatcher = /\{[ \t]*$/; + const endMatcher = /\}[ \t]*$/; + const activeLine = cm.getLine(start.line); + + + if(activeLine.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) + }; + } + + // @import and data-url folding + const importMatcher = /^@import.*?;/; + const dataURLMatcher = /url\(.*?data\:.*\)/; + + if(activeLine.match(importMatcher) || activeLine.match(dataURLMatcher)) { + return { + from : CodeMirror.Pos(start.line, 0), + to : CodeMirror.Pos(start.line, activeLine.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