From 7b00ec9fcfb009cda9e6f2b6576bd61eb4bda26d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADctor=20Losada=20Hern=C3=A1ndez?= Date: Wed, 21 Jan 2026 15:44:33 +0100 Subject: [PATCH] stable version i think --- client/components/codeEditor/close-tag.js | 2 +- client/components/codeEditor/codeEditor.jsx | 173 +++++++++--------- client/homebrew/editor/editor.jsx | 70 +++---- client/homebrew/homebrew.jsx | 2 +- .../pages/basePages/uiPage/uiPage.jsx | 3 +- client/homebrew/pages/editPage/editPage.jsx | 3 +- client/homebrew/pages/homePage/homePage.jsx | 4 +- client/homebrew/pages/newPage/newPage.jsx | 3 +- client/homebrew/pages/sharePage/sharePage.jsx | 3 +- client/homebrew/pages/userPage/userPage.jsx | 3 +- client/homebrew/pages/vaultPage/vaultPage.jsx | 3 +- 11 files changed, 140 insertions(+), 129 deletions(-) diff --git a/client/components/codeEditor/close-tag.js b/client/components/codeEditor/close-tag.js index ebf9f932..84cf6216 100644 --- a/client/components/codeEditor/close-tag.js +++ b/client/components/codeEditor/close-tag.js @@ -43,6 +43,6 @@ export default { const map = { name: 'autoCloseCurlyBraces' }; map[`'{'`] = function(cm) { return autoCloseCurlyBraces(CodeMirror, cm); }; map[`'}'`] = function(cm) { return autoCloseCurlyBraces(CodeMirror, cm, true); }; - codeMirror.addKeyMap(map); + codeMirror?.addKeyMap(map); } }; \ No newline at end of file diff --git a/client/components/codeEditor/codeEditor.jsx b/client/components/codeEditor/codeEditor.jsx index 58e7973c..3ec98668 100644 --- a/client/components/codeEditor/codeEditor.jsx +++ b/client/components/codeEditor/codeEditor.jsx @@ -5,6 +5,7 @@ import createReactClass from 'create-react-class'; import _ from 'lodash'; import closeTag from './close-tag'; import autoCompleteEmoji from './autocompleteEmoji'; +let CodeMirror; const CodeEditor = createReactClass({ displayName : 'CodeEditor', @@ -28,7 +29,9 @@ const CodeEditor = createReactClass({ editor : React.createRef(null), async componentDidMount() { - const Codemirror = (await import('codemirror')).default; + CodeMirror = (await import('codemirror')).default; + this.CodeMirror = CodeMirror; + await import('codemirror/mode/gfm/gfm.js'); await import('codemirror/mode/css/css.js'); await import('codemirror/mode/javascript/javascript.js'); @@ -50,17 +53,15 @@ const CodeEditor = createReactClass({ // import 'codemirror/addon/edit/trailingspace.js'; - this.CodeMirror = Codemirror; - // register helpers dynamically as well const foldPagesCode = (await import('./fold-pages')).default; const foldCSSCode = (await import('./fold-css')).default; - foldPagesCode.registerHomebreweryHelper(Codemirror); - foldCSSCode.registerHomebreweryHelper(Codemirror); + foldPagesCode.registerHomebreweryHelper(CodeMirror); + foldCSSCode.registerHomebreweryHelper(CodeMirror); this.buildEditor(); - const newDoc = Codemirror.Doc(this.props.value, this.props.language); - this.codeMirror.swapDoc(newDoc); + const newDoc = CodeMirror?.Doc(this.props.value, this.props.language); + this.codeMirror?.swapDoc(newDoc); }, @@ -69,12 +70,12 @@ const CodeEditor = createReactClass({ let newDoc; if(!this.state.docs[this.props.view]) { - newDoc = CodeMirror.Doc(this.props.value, this.props.language); + newDoc = CodeMirror?.Doc(this.props.value, this.props.language); } else { newDoc = this.state.docs[this.props.view]; } - const oldDoc = { [prevProps.view]: this.codeMirror.swapDoc(newDoc) }; + const oldDoc = { [prevProps.view]: this.codeMirror?.swapDoc(newDoc) }; this.setState((prevState)=>({ docs : _.merge({}, prevState.docs, oldDoc) @@ -82,17 +83,17 @@ const CodeEditor = createReactClass({ this.props.rerenderParent(); } else if(this.codeMirror?.getValue() != this.props.value) { //update editor contents if brew.text is changed from outside - this.codeMirror.setValue(this.props.value); + this.codeMirror?.setValue(this.props.value); } if(this.props.enableFolding) { - this.codeMirror.setOption('foldOptions', this.foldOptions(this.codeMirror)); + this.codeMirror?.setOption('foldOptions', this.foldOptions(this.codeMirror)); } else { - this.codeMirror.setOption('foldOptions', false); + this.codeMirror?.setOption('foldOptions', false); } if(prevProps.editorTheme !== this.props.editorTheme){ - this.codeMirror.setOption('theme', this.props.editorTheme); + this.codeMirror?.setOption('theme', this.props.editorTheme); } }, @@ -180,8 +181,8 @@ const CodeEditor = createReactClass({ closeTag.autoCloseCurlyBraces(CodeMirror, this.codeMirror); autoCompleteEmoji.showAutocompleteEmoji(CodeMirror, this.codeMirror); - // Note: codeMirror passes a copy of itself in this callback. cm === this.codeMirror. Either one works. - this.codeMirror.on('change', (cm)=>{this.props.onChange(cm.getValue());}); + // Note: codeMirror passes a copy of itself in this callback. cm === this.codeMirror?. Either one works. + this.codeMirror?.on('change', (cm)=>{this.props.onChange(cm.getValue());}); this.updateSize(); }, @@ -195,84 +196,84 @@ const CodeEditor = createReactClass({ }, dedent : function () { - this.codeMirror.execCommand('indentLess'); + this.codeMirror?.execCommand('indentLess'); }, makeHeader : function (number) { - const selection = this.codeMirror.getSelection(); + const selection = this.codeMirror?.getSelection(); const header = Array(number).fill('#').join(''); - this.codeMirror.replaceSelection(`${header} ${selection}`, 'around'); - const cursor = this.codeMirror.getCursor(); - this.codeMirror.setCursor({ line: cursor.line, ch: cursor.ch + selection.length + number + 1 }); + this.codeMirror?.replaceSelection(`${header} ${selection}`, 'around'); + const cursor = this.codeMirror?.getCursor(); + this.codeMirror?.setCursor({ line: cursor.line, ch: cursor.ch + selection.length + number + 1 }); }, makeBold : function() { - const selection = this.codeMirror.getSelection(), t = selection.slice(0, 2) === '**' && selection.slice(-2) === '**'; - this.codeMirror.replaceSelection(t ? selection.slice(2, -2) : `**${selection}**`, 'around'); + const selection = this.codeMirror?.getSelection(), t = selection.slice(0, 2) === '**' && selection.slice(-2) === '**'; + this.codeMirror?.replaceSelection(t ? selection.slice(2, -2) : `**${selection}**`, 'around'); if(selection.length === 0){ - const cursor = this.codeMirror.getCursor(); - this.codeMirror.setCursor({ line: cursor.line, ch: cursor.ch - 2 }); + const cursor = this.codeMirror?.getCursor(); + this.codeMirror?.setCursor({ line: cursor.line, ch: cursor.ch - 2 }); } }, makeItalic : function() { - const selection = this.codeMirror.getSelection(), t = selection.slice(0, 1) === '*' && selection.slice(-1) === '*'; - this.codeMirror.replaceSelection(t ? selection.slice(1, -1) : `*${selection}*`, 'around'); + const selection = this.codeMirror?.getSelection(), t = selection.slice(0, 1) === '*' && selection.slice(-1) === '*'; + this.codeMirror?.replaceSelection(t ? selection.slice(1, -1) : `*${selection}*`, 'around'); if(selection.length === 0){ - const cursor = this.codeMirror.getCursor(); - this.codeMirror.setCursor({ line: cursor.line, ch: cursor.ch - 1 }); + const cursor = this.codeMirror?.getCursor(); + this.codeMirror?.setCursor({ line: cursor.line, ch: cursor.ch - 1 }); } }, makeSuper : function() { - const selection = this.codeMirror.getSelection(), t = selection.slice(0, 1) === '^' && selection.slice(-1) === '^'; - this.codeMirror.replaceSelection(t ? selection.slice(1, -1) : `^${selection}^`, 'around'); + const selection = this.codeMirror?.getSelection(), t = selection.slice(0, 1) === '^' && selection.slice(-1) === '^'; + this.codeMirror?.replaceSelection(t ? selection.slice(1, -1) : `^${selection}^`, 'around'); if(selection.length === 0){ - const cursor = this.codeMirror.getCursor(); - this.codeMirror.setCursor({ line: cursor.line, ch: cursor.ch - 1 }); + const cursor = this.codeMirror?.getCursor(); + this.codeMirror?.setCursor({ line: cursor.line, ch: cursor.ch - 1 }); } }, makeSub : function() { - const selection = this.codeMirror.getSelection(), t = selection.slice(0, 2) === '^^' && selection.slice(-2) === '^^'; - this.codeMirror.replaceSelection(t ? selection.slice(2, -2) : `^^${selection}^^`, 'around'); + const selection = this.codeMirror?.getSelection(), t = selection.slice(0, 2) === '^^' && selection.slice(-2) === '^^'; + this.codeMirror?.replaceSelection(t ? selection.slice(2, -2) : `^^${selection}^^`, 'around'); if(selection.length === 0){ - const cursor = this.codeMirror.getCursor(); - this.codeMirror.setCursor({ line: cursor.line, ch: cursor.ch - 2 }); + const cursor = this.codeMirror?.getCursor(); + this.codeMirror?.setCursor({ line: cursor.line, ch: cursor.ch - 2 }); } }, makeNbsp : function() { - this.codeMirror.replaceSelection(' ', 'end'); + this.codeMirror?.replaceSelection(' ', 'end'); }, makeSpace : function() { - const selection = this.codeMirror.getSelection(); + const selection = this.codeMirror?.getSelection(); const t = selection.slice(0, 8) === '{{width:' && selection.slice(0 -4) === '% }}'; if(t){ const percent = parseInt(selection.slice(8, -4)) + 10; - this.codeMirror.replaceSelection(percent < 90 ? `{{width:${percent}% }}` : '{{width:100% }}', 'around'); + this.codeMirror?.replaceSelection(percent < 90 ? `{{width:${percent}% }}` : '{{width:100% }}', 'around'); } else { - this.codeMirror.replaceSelection(`{{width:10% }}`, 'around'); + this.codeMirror?.replaceSelection(`{{width:10% }}`, 'around'); } }, removeSpace : function() { - const selection = this.codeMirror.getSelection(); + const selection = this.codeMirror?.getSelection(); const t = selection.slice(0, 8) === '{{width:' && selection.slice(0 -4) === '% }}'; if(t){ const percent = parseInt(selection.slice(8, -4)) - 10; - this.codeMirror.replaceSelection(percent > 10 ? `{{width:${percent}% }}` : '', 'around'); + this.codeMirror?.replaceSelection(percent > 10 ? `{{width:${percent}% }}` : '', 'around'); } }, newColumn : function() { - this.codeMirror.replaceSelection('\n\\column\n\n', 'end'); + this.codeMirror?.replaceSelection('\n\\column\n\n', 'end'); }, newPage : function() { - this.codeMirror.replaceSelection('\n\\page\n\n', 'end'); + this.codeMirror?.replaceSelection('\n\\page\n\n', 'end'); }, injectText : function(injectText, overwrite=true) { @@ -285,29 +286,29 @@ const CodeEditor = createReactClass({ }, makeUnderline : function() { - const selection = this.codeMirror.getSelection(), t = selection.slice(0, 3) === '' && selection.slice(-4) === ''; - this.codeMirror.replaceSelection(t ? selection.slice(3, -4) : `${selection}`, 'around'); + const selection = this.codeMirror?.getSelection(), t = selection.slice(0, 3) === '' && selection.slice(-4) === ''; + this.codeMirror?.replaceSelection(t ? selection.slice(3, -4) : `${selection}`, 'around'); if(selection.length === 0){ - const cursor = this.codeMirror.getCursor(); - this.codeMirror.setCursor({ line: cursor.line, ch: cursor.ch - 4 }); + const cursor = this.codeMirror?.getCursor(); + this.codeMirror?.setCursor({ line: cursor.line, ch: cursor.ch - 4 }); } }, makeSpan : function() { - const selection = this.codeMirror.getSelection(), t = selection.slice(0, 2) === '{{' && selection.slice(-2) === '}}'; - this.codeMirror.replaceSelection(t ? selection.slice(2, -2) : `{{ ${selection}}}`, 'around'); + const selection = this.codeMirror?.getSelection(), t = selection.slice(0, 2) === '{{' && selection.slice(-2) === '}}'; + this.codeMirror?.replaceSelection(t ? selection.slice(2, -2) : `{{ ${selection}}}`, 'around'); if(selection.length === 0){ - const cursor = this.codeMirror.getCursor(); - this.codeMirror.setCursor({ line: cursor.line, ch: cursor.ch - 2 }); + const cursor = this.codeMirror?.getCursor(); + this.codeMirror?.setCursor({ line: cursor.line, ch: cursor.ch - 2 }); } }, makeDiv : function() { - const selection = this.codeMirror.getSelection(), t = selection.slice(0, 2) === '{{' && selection.slice(-2) === '}}'; - this.codeMirror.replaceSelection(t ? selection.slice(2, -2) : `{{\n${selection}\n}}`, 'around'); + const selection = this.codeMirror?.getSelection(), t = selection.slice(0, 2) === '{{' && selection.slice(-2) === '}}'; + this.codeMirror?.replaceSelection(t ? selection.slice(2, -2) : `{{\n${selection}\n}}`, 'around'); if(selection.length === 0){ - const cursor = this.codeMirror.getCursor(); - this.codeMirror.setCursor({ line: cursor.line - 1, ch: cursor.ch }); // set to -2? if wanting to enter classes etc. if so, get rid of first \n when replacing selection + const cursor = this.codeMirror?.getCursor(); + this.codeMirror?.setCursor({ line: cursor.line - 1, ch: cursor.ch }); // set to -2? if wanting to enter classes etc. if so, get rid of first \n when replacing selection } }, @@ -315,7 +316,7 @@ const CodeEditor = createReactClass({ let regex; let cursorPos; let newComment; - const selection = this.codeMirror.getSelection(); + const selection = this.codeMirror?.getSelection(); if(this.props.language === 'gfm'){ regex = /^\s*()\s*$/gs; cursorPos = 4; @@ -325,44 +326,44 @@ const CodeEditor = createReactClass({ cursorPos = 3; newComment = `/* ${selection} */`; } - this.codeMirror.replaceSelection(regex.test(selection) == true ? selection.replace(regex, '$2') : newComment, 'around'); + this.codeMirror?.replaceSelection(regex.test(selection) == true ? selection.replace(regex, '$2') : newComment, 'around'); if(selection.length === 0){ - const cursor = this.codeMirror.getCursor(); - this.codeMirror.setCursor({ line: cursor.line, ch: cursor.ch - cursorPos }); + const cursor = this.codeMirror?.getCursor(); + this.codeMirror?.setCursor({ line: cursor.line, ch: cursor.ch - cursorPos }); }; }, makeLink : function() { const isLink = /^\[(.*)\]\((.*)\)$/; - const selection = this.codeMirror.getSelection().trim(); + const selection = this.codeMirror?.getSelection().trim(); let match; if(match = isLink.exec(selection)){ const altText = match[1]; const url = match[2]; - this.codeMirror.replaceSelection(`${altText} ${url}`); - const cursor = this.codeMirror.getCursor(); - this.codeMirror.setSelection({ line: cursor.line, ch: cursor.ch - url.length }, { line: cursor.line, ch: cursor.ch }); + this.codeMirror?.replaceSelection(`${altText} ${url}`); + const cursor = this.codeMirror?.getCursor(); + this.codeMirror?.setSelection({ line: cursor.line, ch: cursor.ch - url.length }, { line: cursor.line, ch: cursor.ch }); } else { - this.codeMirror.replaceSelection(`[${selection || 'alt text'}](url)`); - const cursor = this.codeMirror.getCursor(); - this.codeMirror.setSelection({ line: cursor.line, ch: cursor.ch - 4 }, { line: cursor.line, ch: cursor.ch - 1 }); + this.codeMirror?.replaceSelection(`[${selection || 'alt text'}](url)`); + const cursor = this.codeMirror?.getCursor(); + this.codeMirror?.setSelection({ line: cursor.line, ch: cursor.ch - 4 }, { line: cursor.line, ch: cursor.ch - 1 }); } }, makeList : function(listType) { - const selectionStart = this.codeMirror.getCursor('from'), selectionEnd = this.codeMirror.getCursor('to'); - this.codeMirror.setSelection( + const selectionStart = this.codeMirror?.getCursor('from'), selectionEnd = this.codeMirror?.getCursor('to'); + this.codeMirror?.setSelection( { line: selectionStart.line, ch: 0 }, - { line: selectionEnd.line, ch: this.codeMirror.getLine(selectionEnd.line).length } + { line: selectionEnd.line, ch: this.codeMirror?.getLine(selectionEnd.line).length } ); - const newSelection = this.codeMirror.getSelection(); + const newSelection = this.codeMirror?.getSelection(); const regex = /^\d+\.\s|^-\s/gm; if(newSelection.match(regex) != null){ // if selection IS A LIST - this.codeMirror.replaceSelection(newSelection.replace(regex, ''), 'around'); + this.codeMirror?.replaceSelection(newSelection.replace(regex, ''), 'around'); } else { // if selection IS NOT A LIST - listType == 'UL' ? this.codeMirror.replaceSelection(newSelection.replace(/^/gm, `- `), 'around') : - this.codeMirror.replaceSelection(newSelection.replace(/^/gm, (()=>{ + listType == 'UL' ? this.codeMirror?.replaceSelection(newSelection.replace(/^/gm, `- `), 'around') : + this.codeMirror?.replaceSelection(newSelection.replace(/^/gm, (()=>{ let n = 1; return ()=>{ return `${n++}. `; @@ -372,39 +373,39 @@ const CodeEditor = createReactClass({ }, foldAllCode : function() { - this.codeMirror.execCommand('foldAll'); + this.codeMirror?.execCommand('foldAll'); }, unfoldAllCode : function() { - this.codeMirror.execCommand('unfoldAll'); + this.codeMirror?.execCommand('unfoldAll'); }, //=-- Externally used -==// setCursorPosition : function(line, char){ setTimeout(()=>{ - this.codeMirror.focus(); - this.codeMirror.doc.setCursor(line, char); + this.codeMirror?.focus(); + this.codeMirror?.doc.setCursor(line, char); }, 10); }, getCursorPosition : function(){ - return this.codeMirror.getCursor(); + return this.codeMirror?.getCursor(); }, getTopVisibleLine : function(){ - const rect = this.codeMirror.getWrapperElement().getBoundingClientRect(); - const topVisibleLine = this.codeMirror.lineAtHeight(rect.top, 'window'); + const rect = this.codeMirror?.getWrapperElement().getBoundingClientRect(); + const topVisibleLine = this.codeMirror?.lineAtHeight(rect.top, 'window'); return topVisibleLine; }, updateSize : function(){ - this.codeMirror.refresh(); + this.codeMirror?.refresh(); }, redo : function(){ - return this.codeMirror.redo(); + return this.codeMirror?.redo(); }, undo : function(){ - return this.codeMirror.undo(); + return this.codeMirror?.undo(); }, historySize : function(){ - return this.codeMirror.doc.historySize(); + return this.codeMirror?.doc.historySize(); }, foldOptions : function(cm){ @@ -418,7 +419,7 @@ const CodeEditor = createReactClass({ let foldPreviewText = ''; while (currentLine <= to.line && text.length <= maxLength) { - const currentText = this.codeMirror.getLine(currentLine); + const currentText = this.codeMirror?.getLine(currentLine); currentLine++; if(currentText[0] == '#'){ foldPreviewText = currentText; diff --git a/client/homebrew/editor/editor.jsx b/client/homebrew/editor/editor.jsx index 1f19a14c..e58d5344 100644 --- a/client/homebrew/editor/editor.jsx +++ b/client/homebrew/editor/editor.jsx @@ -76,8 +76,8 @@ const Editor = createReactClass({ document.getElementById('BrewRenderer').addEventListener('keydown', this.handleControlKeys); document.addEventListener('keydown', this.handleControlKeys); - this.codeEditor.current.codeMirror.on('cursorActivity', (cm)=>{this.updateCurrentCursorPage(cm.getCursor());}); - this.codeEditor.current.codeMirror.on('scroll', _.throttle(()=>{this.updateCurrentViewPage(this.codeEditor.current.getTopVisibleLine());}, 200)); + this.codeEditor.current.codeMirror?.on('cursorActivity', (cm)=>{this.updateCurrentCursorPage(cm.getCursor());}); + this.codeEditor.current.codeMirror?.on('scroll', _.throttle(()=>{this.updateCurrentViewPage(this.codeEditor.current.getTopVisibleLine());}, 200)); const editorTheme = window.localStorage.getItem(EDITOR_THEME_KEY); if(editorTheme) { @@ -156,21 +156,21 @@ const Editor = createReactClass({ this.setState({ view : newView }, ()=>{ - this.codeEditor.current?.codeMirror.focus(); + this.codeEditor.current?.codeMirror?.focus(); }); }, highlightCustomMarkdown : function(){ - if(!this.codeEditor.current) return; + if(!this.codeEditor.current?.codeMirror) return; if((this.state.view === 'text') ||(this.state.view === 'snippet')) { const codeMirror = this.codeEditor.current.codeMirror; - codeMirror.operation(()=>{ // Batch CodeMirror styling + codeMirror?.operation(()=>{ // Batch CodeMirror styling const foldLines = []; //reset custom text styles - const customHighlights = codeMirror.getAllMarks().filter((mark)=>{ + const customHighlights = codeMirror?.getAllMarks().filter((mark)=>{ // Record details of folded sections if(mark.__isFold) { const fold = mark.find(); @@ -191,10 +191,10 @@ const Editor = createReactClass({ const textOrSnip = this.state.view === 'text'; //reset custom line styles - codeMirror.removeLineClass(lineNumber, 'background', 'pageLine'); - codeMirror.removeLineClass(lineNumber, 'background', 'snippetLine'); - codeMirror.removeLineClass(lineNumber, 'text'); - codeMirror.removeLineClass(lineNumber, 'wrap', 'sourceMoveFlash'); + codeMirror?.removeLineClass(lineNumber, 'background', 'pageLine'); + codeMirror?.removeLineClass(lineNumber, 'background', 'snippetLine'); + codeMirror?.removeLineClass(lineNumber, 'text'); + codeMirror?.removeLineClass(lineNumber, 'wrap', 'sourceMoveFlash'); // Don't process lines inside folded text // If the current lineNumber is inside any folded marks, skip line styling @@ -210,19 +210,19 @@ const Editor = createReactClass({ else if(this.state.view !== 'text') userSnippetCount += 1; // add back the original class 'background' but also add the new class '.pageline' - codeMirror.addLineClass(lineNumber, 'background', tabHighlight); + codeMirror?.addLineClass(lineNumber, 'background', tabHighlight); const pageCountElement = Object.assign(document.createElement('span'), { className : 'editor-page-count', textContent : textOrSnip ? editorPageCount : userSnippetCount }); - codeMirror.setBookmark({ line: lineNumber, ch: line.length }, pageCountElement); + codeMirror?.setBookmark({ line: lineNumber, ch: line.length }, pageCountElement); }; - // New Codemirror styling for V3 renderer + // New CodeMirror styling for V3 renderer if(this.props.renderer === 'V3') { if(line.match(/^\\column(?:break)?$/)){ - codeMirror.addLineClass(lineNumber, 'text', 'columnSplit'); + codeMirror?.addLineClass(lineNumber, 'text', 'columnSplit'); } // definition lists @@ -231,14 +231,14 @@ const Editor = createReactClass({ const regex = /^([^\n]*?:?\s?)(::[^\n]*)(?:\n|$)/ymd; // the `d` flag, for match indices, throws an ESLint error. let match; while ((match = regex.exec(line)) != null){ - codeMirror.markText({ line: lineNumber, ch: match.indices[0][0] }, { line: lineNumber, ch: match.indices[0][1] }, { className: 'dl-highlight' }); - codeMirror.markText({ line: lineNumber, ch: match.indices[1][0] }, { line: lineNumber, ch: match.indices[1][1] }, { className: 'dt-highlight' }); - codeMirror.markText({ line: lineNumber, ch: match.indices[2][0] }, { line: lineNumber, ch: match.indices[2][1] }, { className: 'dd-highlight' }); + codeMirror?.markText({ line: lineNumber, ch: match.indices[0][0] }, { line: lineNumber, ch: match.indices[0][1] }, { className: 'dl-highlight' }); + codeMirror?.markText({ line: lineNumber, ch: match.indices[1][0] }, { line: lineNumber, ch: match.indices[1][1] }, { className: 'dt-highlight' }); + codeMirror?.markText({ line: lineNumber, ch: match.indices[2][0] }, { line: lineNumber, ch: match.indices[2][1] }, { className: 'dd-highlight' }); const ddIndex = match.indices[2][0]; const colons = /::/g; const colonMatches = colons.exec(match[2]); if(colonMatches !== null){ - codeMirror.markText({ line: lineNumber, ch: colonMatches.index + ddIndex }, { line: lineNumber, ch: colonMatches.index + colonMatches[0].length + ddIndex }, { className: 'dl-colon-highlight' }); + codeMirror?.markText({ line: lineNumber, ch: colonMatches.index + ddIndex }, { line: lineNumber, ch: colonMatches.index + colonMatches[0].length + ddIndex }, { className: 'dl-colon-highlight' }); } } } @@ -255,7 +255,7 @@ const Editor = createReactClass({ const match = subRegex.exec(line) || superRegex.exec(line); if(match) { isSuper = !subRegex.lastIndex; - codeMirror.markText({ line: lineNumber, ch: match.index }, { line: lineNumber, ch: match.index + match[0].length }, { className: isSuper ? 'superscript' : 'subscript' }); + codeMirror?.markText({ line: lineNumber, ch: match.index }, { line: lineNumber, ch: match.index + match[0].length }, { className: isSuper ? 'superscript' : 'subscript' }); } startIndex = line.indexOf('^', Math.max(startIndex + 1, subRegex.lastIndex, superRegex.lastIndex)); } @@ -266,7 +266,7 @@ const Editor = createReactClass({ const regex = /(?:^|[^{\n])({(?=((?:[:=](?:"[\w,\-()#%. ]*"|[\w\-()#%.]*)|[^"':={}\s]*)*))\2})/gm; let match; while ((match = regex.exec(line)) != null) { - codeMirror.markText({ line: lineNumber, ch: line.indexOf(match[1]) }, { line: lineNumber, ch: line.indexOf(match[1]) + match[1].length }, { className: 'injection' }); + codeMirror?.markText({ line: lineNumber, ch: line.indexOf(match[1]) }, { line: lineNumber, ch: line.indexOf(match[1]) + match[1].length }, { className: 'injection' }); } } // Highlight inline spans {{content}} @@ -284,7 +284,7 @@ const Editor = createReactClass({ blockCount = 0; continue; } - codeMirror.markText({ line: lineNumber, ch: match.index }, { line: lineNumber, ch: match.index + match[0].length }, { className: 'inline-block' }); + codeMirror?.markText({ line: lineNumber, ch: match.index }, { line: lineNumber, ch: match.index + match[0].length }, { className: 'inline-block' }); } } else if(line.trimLeft().startsWith('{{') || line.trimLeft().startsWith('}}')){ // Highlight block divs {{\n Content \n}} @@ -293,7 +293,7 @@ const Editor = createReactClass({ const match = line.match(/^ *{{(?=((?:[:=](?:"[\w,\-()#%. ]*"|[\w\-()#%.]*)|[^"':={}\s]*)*))\1 *$|^ *}}$/); if(match) endCh = match.index+match[0].length; - codeMirror.markText({ line: lineNumber, ch: 0 }, { line: lineNumber, ch: endCh }, { className: 'block' }); + codeMirror?.markText({ line: lineNumber, ch: 0 }, { line: lineNumber, ch: endCh }, { className: 'block' }); } // Emojis @@ -314,11 +314,11 @@ const Editor = createReactClass({ const endPos = { line: lineNumber, ch: match.index + match[0].length }; // Iterate over conflicting marks and clear them - const marks = codeMirror.findMarks(startPos, endPos); + const marks = codeMirror?.findMarks(startPos, endPos); marks.forEach(function(marker) { if(!marker.__isFold) marker.clear(); }); - codeMirror.markText(startPos, endPos, { className: 'emoji' }); + codeMirror?.markText(startPos, endPos, { className: 'emoji' }); } startIndex = line.indexOf(':', Math.max(startIndex + 1, emojiRegex.lastIndex)); } @@ -378,44 +378,46 @@ const Editor = createReactClass({ const textString = this.props.brew.text.split(textSplit).slice(0, targetPage-1).join(textSplit); const targetLine = textString.match('\n') ? textString.split('\n').length - 1 : -1; - let currentY = this.codeEditor.current.codeMirror.getScrollInfo().top; - let targetY = this.codeEditor.current.codeMirror.heightAtLine(targetLine, 'local', true); + let currentY = this.codeEditor.current.codeMirror?.getScrollInfo().top; + let targetY = this.codeEditor.current.codeMirror?.heightAtLine(targetLine, 'local', true); let scrollingTimeout; const checkIfScrollComplete = ()=>{ // Prevent interrupting a scroll in progress if user clicks multiple times clearTimeout(scrollingTimeout); // Reset the timer every time a scroll event occurs scrollingTimeout = setTimeout(()=>{ isJumping = false; - this.codeEditor.current.codeMirror.off('scroll', checkIfScrollComplete); + this.codeEditor.current.codeMirror?.off('scroll', checkIfScrollComplete); }, 150); // If 150 ms pass without a scroll event, assume scrolling is done }; isJumping = true; checkIfScrollComplete(); - this.codeEditor.current.codeMirror.on('scroll', checkIfScrollComplete); + if (this.codeEditor.current?.codeMirror) { + this.codeEditor.current.codeMirror?.on('scroll', checkIfScrollComplete); + } if(smooth) { //Scroll 1/10 of the way every 10ms until 1px off. const incrementalScroll = setInterval(()=>{ currentY += (targetY - currentY) / 10; - this.codeEditor.current.codeMirror.scrollTo(null, currentY); + this.codeEditor.current.codeMirror?.scrollTo(null, currentY); // Update target: target height is not accurate until within +-10 lines of the visible window if(Math.abs(targetY - currentY > 100)) - targetY = this.codeEditor.current.codeMirror.heightAtLine(targetLine, 'local', true); + targetY = this.codeEditor.current.codeMirror?.heightAtLine(targetLine, 'local', true); // End when close enough if(Math.abs(targetY - currentY) < 1) { - this.codeEditor.current.codeMirror.scrollTo(null, targetY); // Scroll any remaining difference + this.codeEditor.current.codeMirror?.scrollTo(null, targetY); // Scroll any remaining difference this.codeEditor.current.setCursorPosition({ line: targetLine + 1, ch: 0 }); - this.codeEditor.current.codeMirror.addLineClass(targetLine + 1, 'wrap', 'sourceMoveFlash'); + this.codeEditor.current.codeMirror?.addLineClass(targetLine + 1, 'wrap', 'sourceMoveFlash'); clearInterval(incrementalScroll); } }, 10); } else { - this.codeEditor.current.codeMirror.scrollTo(null, targetY); // Scroll any remaining difference + this.codeEditor.current.codeMirror?.scrollTo(null, targetY); // Scroll any remaining difference this.codeEditor.current.setCursorPosition({ line: targetLine + 1, ch: 0 }); - this.codeEditor.current.codeMirror.addLineClass(targetLine + 1, 'wrap', 'sourceMoveFlash'); + this.codeEditor.current.codeMirror?.addLineClass(targetLine + 1, 'wrap', 'sourceMoveFlash'); } }, diff --git a/client/homebrew/homebrew.jsx b/client/homebrew/homebrew.jsx index 18a3993a..e38293bd 100644 --- a/client/homebrew/homebrew.jsx +++ b/client/homebrew/homebrew.jsx @@ -80,4 +80,4 @@ const Homebrew = (props)=>{ ); }; -export default Homebrew; \ No newline at end of file +module.exports = Homebrew; \ No newline at end of file diff --git a/client/homebrew/pages/basePages/uiPage/uiPage.jsx b/client/homebrew/pages/basePages/uiPage/uiPage.jsx index c15f69d0..91b4396c 100644 --- a/client/homebrew/pages/basePages/uiPage/uiPage.jsx +++ b/client/homebrew/pages/basePages/uiPage/uiPage.jsx @@ -6,7 +6,8 @@ import Nav from 'client/homebrew/navbar/nav.jsx'; import Navbar from 'client/homebrew/navbar/navbar.jsx'; import NewBrewItem from 'client/homebrew/navbar/newbrew.navitem.jsx'; import HelpNavItem from 'client/homebrew/navbar/help.navitem.jsx'; -import { both as RecentNavItem } from 'client/homebrew/navbar/recent.navitem.jsx'; +import RecentNavItems from 'client/homebrew/navbar/recent.navitem.jsx'; +const { both: RecentNavItem } = RecentNavItems; import Account from 'client/homebrew/navbar/account.navitem.jsx'; diff --git a/client/homebrew/pages/editPage/editPage.jsx b/client/homebrew/pages/editPage/editPage.jsx index 3eb26186..1b228d8b 100644 --- a/client/homebrew/pages/editPage/editPage.jsx +++ b/client/homebrew/pages/editPage/editPage.jsx @@ -22,7 +22,8 @@ import ErrorNavItem from 'client/homebrew/navbar/error-navitem.jsx' import HelpNavItem from 'client/homebrew/navbar/help.navitem.jsx'; import VaultNavItem from 'client/homebrew/navbar/vault.navitem.jsx'; import PrintNavItem from 'client/homebrew/navbar/print.navitem.jsx'; -import { both as RecentNavItem } from 'client/homebrew/navbar/recent.navitem.jsx'; +import RecentNavItems from 'client/homebrew/navbar/recent.navitem.jsx'; +const { both: RecentNavItem } = RecentNavItems; // Page specific imports import { Meta } from 'vitreum/headtags'; diff --git a/client/homebrew/pages/homePage/homePage.jsx b/client/homebrew/pages/homePage/homePage.jsx index fb5caecd..f3d01dd8 100644 --- a/client/homebrew/pages/homePage/homePage.jsx +++ b/client/homebrew/pages/homePage/homePage.jsx @@ -22,7 +22,9 @@ import ErrorNavItem from 'client/homebrew/navbar/error-navitem.jsx' import HelpNavItem from 'client/homebrew/navbar/help.navitem.jsx'; import VaultNavItem from 'client/homebrew/navbar/vault.navitem.jsx'; import PrintNavItem from 'client/homebrew/navbar/print.navitem.jsx'; -import { both as RecentNavItem } from 'client/homebrew/navbar/recent.navitem.jsx'; +import RecentNavItems from 'client/homebrew/navbar/recent.navitem.jsx'; +const { both: RecentNavItem } = RecentNavItems; + // Page specific imports import { Meta } from 'vitreum/headtags'; diff --git a/client/homebrew/pages/newPage/newPage.jsx b/client/homebrew/pages/newPage/newPage.jsx index fb595ab9..5099a98a 100644 --- a/client/homebrew/pages/newPage/newPage.jsx +++ b/client/homebrew/pages/newPage/newPage.jsx @@ -22,7 +22,8 @@ import ErrorNavItem from 'client/homebrew/navbar/error-navitem.jsx' import HelpNavItem from 'client/homebrew/navbar/help.navitem.jsx'; import VaultNavItem from 'client/homebrew/navbar/vault.navitem.jsx'; import PrintNavItem from 'client/homebrew/navbar/print.navitem.jsx'; -import { both as RecentNavItem } from 'client/homebrew/navbar/recent.navitem.jsx'; +import RecentNavItems from 'client/homebrew/navbar/recent.navitem.jsx'; +const { both: RecentNavItem } = RecentNavItems; // Page specific imports import { Meta } from 'vitreum/headtags'; diff --git a/client/homebrew/pages/sharePage/sharePage.jsx b/client/homebrew/pages/sharePage/sharePage.jsx index 44466db5..90d6cb3d 100644 --- a/client/homebrew/pages/sharePage/sharePage.jsx +++ b/client/homebrew/pages/sharePage/sharePage.jsx @@ -6,7 +6,8 @@ import Nav from 'client/homebrew/navbar/nav.jsx'; import Navbar from 'client/homebrew/navbar/navbar.jsx'; import MetadataNav from 'client/homebrew/navbar/metadata.navitem.jsx'; import PrintNavItem from 'client/homebrew/navbar/print.navitem.jsx'; -import { both as RecentNavItem } from 'client/homebrew/navbar/recent.navitem.jsx'; +import RecentNavItems from 'client/homebrew/navbar/recent.navitem.jsx'; +const { both: RecentNavItem } = RecentNavItems; import Account from 'client/homebrew/navbar/account.navitem.jsx'; import BrewRenderer from '../../brewRenderer/brewRenderer.jsx'; diff --git a/client/homebrew/pages/userPage/userPage.jsx b/client/homebrew/pages/userPage/userPage.jsx index f6f0cfa3..2a4c47c6 100644 --- a/client/homebrew/pages/userPage/userPage.jsx +++ b/client/homebrew/pages/userPage/userPage.jsx @@ -5,7 +5,8 @@ import ListPage from '../basePages/listPage/listPage.jsx'; import Nav from 'client/homebrew/navbar/nav.jsx'; import Navbar from 'client/homebrew/navbar/navbar.jsx'; -import { both as RecentNavItem } from 'client/homebrew/navbar/recent.navitem.jsx'; +import RecentNavItems from 'client/homebrew/navbar/recent.navitem.jsx'; +const { both: RecentNavItem } = RecentNavItems; import Account from 'client/homebrew/navbar/account.navitem.jsx'; import NewBrew from 'client/homebrew/navbar/newbrew.navitem.jsx'; import HelpNavItem from 'client/homebrew/navbar/help.navitem.jsx'; diff --git a/client/homebrew/pages/vaultPage/vaultPage.jsx b/client/homebrew/pages/vaultPage/vaultPage.jsx index fd2dff06..61ae8901 100644 --- a/client/homebrew/pages/vaultPage/vaultPage.jsx +++ b/client/homebrew/pages/vaultPage/vaultPage.jsx @@ -5,7 +5,8 @@ import React, { useState, useEffect, useRef } from 'react'; import Nav from 'client/homebrew/navbar/nav.jsx'; import Navbar from 'client/homebrew/navbar/navbar.jsx'; -import { both as RecentNavItem } from 'client/homebrew/navbar/recent.navitem.jsx'; +import RecentNavItems from 'client/homebrew/navbar/recent.navitem.jsx'; +const { both: RecentNavItem } = RecentNavItems; import Account from 'client/homebrew/navbar/account.navitem.jsx'; import NewBrew from 'client/homebrew/navbar/newbrew.navitem.jsx'; import HelpNavItem from 'client/homebrew/navbar/help.navitem.jsx';