diff --git a/client/homebrew/editor/editor.jsx b/client/homebrew/editor/editor.jsx index f5c1766a8..efcc9c861 100644 --- a/client/homebrew/editor/editor.jsx +++ b/client/homebrew/editor/editor.jsx @@ -48,6 +48,9 @@ const Editor = createClass({ }; }, + editor : React.createRef(null), + codeEditor : React.createRef(null), + isText : function() {return this.state.view == 'text';}, isStyle : function() {return this.state.view == 'style';}, isMeta : function() {return this.state.view == 'meta';}, @@ -80,15 +83,15 @@ const Editor = createClass({ }, updateEditorSize : function() { - if(this.refs.codeEditor) { - let paneHeight = this.refs.main.parentNode.clientHeight; + if(this.codeEditor.current) { + let paneHeight = this.editor.current.parentNode.clientHeight; paneHeight -= SNIPPETBAR_HEIGHT; - this.refs.codeEditor.codeMirror.setSize(null, paneHeight); + this.codeEditor.current.codeMirror.setSize(null, paneHeight); } }, handleInject : function(injectText){ - this.refs.codeEditor?.injectText(injectText, false); + this.codeEditor.current?.injectText(injectText, false); }, handleViewChange : function(newView){ @@ -99,7 +102,7 @@ const Editor = createClass({ }, getCurrentPage : function(){ - const lines = this.props.brew.text.split('\n').slice(0, this.refs.codeEditor.getCursorPosition().line + 1); + const lines = this.props.brew.text.split('\n').slice(0, this.codeEditor.current.getCursorPosition().line + 1); return _.reduce(lines, (r, line)=>{ if( (this.props.renderer == 'legacy' && line.indexOf('\\page') !== -1) @@ -111,9 +114,9 @@ const Editor = createClass({ }, highlightCustomMarkdown : function(){ - if(!this.refs.codeEditor) return; + if(!this.codeEditor.current) return; if(this.state.view === 'text') { - const codeMirror = this.refs.codeEditor.codeMirror; + const codeMirror = this.codeEditor.current.codeMirror; codeMirror.operation(()=>{ // Batch CodeMirror styling //reset custom text styles @@ -302,23 +305,23 @@ const Editor = createClass({ targetLine = lineCount - 1; //Scroll to `\page`, which is one line back. - let currentY = this.refs.codeEditor.codeMirror.getScrollInfo().top; - let targetY = this.refs.codeEditor.codeMirror.heightAtLine(targetLine, 'local', true); + let currentY = this.codeEditor.current.codeMirror.getScrollInfo().top; + let targetY = this.codeEditor.current.codeMirror.heightAtLine(targetLine, 'local', true); //Scroll 1/10 of the way every 10ms until 1px off. const incrementalScroll = setInterval(()=>{ currentY += (targetY - currentY) / 10; - this.refs.codeEditor.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.refs.codeEditor.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.refs.codeEditor.codeMirror.scrollTo(null, targetY); // Scroll any remaining difference - this.refs.codeEditor.setCursorPosition({ line: targetLine + 1, ch: 0 }); - this.refs.codeEditor.codeMirror.addLineClass(targetLine + 1, 'wrap', 'sourceMoveFlash'); + 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'); clearInterval(incrementalScroll); } }, 10); @@ -328,7 +331,7 @@ const Editor = createClass({ //Called when there are changes to the editor's dimensions update : function(){ - this.refs.codeEditor?.updateSize(); + this.codeEditor.current?.updateSize(); }, updateEditorTheme : function(newTheme){ @@ -347,7 +350,7 @@ const Editor = createClass({ if(this.isText()){ return <> +
+ cursorPos={this.codeEditor.current?.getCursorPosition() || {}} /> {this.renderEditor()}
diff --git a/client/homebrew/pages/editPage/editPage.jsx b/client/homebrew/pages/editPage/editPage.jsx index d5af310b5..3e7bd0c2a 100644 --- a/client/homebrew/pages/editPage/editPage.jsx +++ b/client/homebrew/pages/editPage/editPage.jsx @@ -54,6 +54,8 @@ const EditPage = createClass({ currentEditorPage : 0 }; }, + + editor : React.createRef(null), savedBrew : null, componentDidMount : function(){ @@ -101,7 +103,7 @@ const EditPage = createClass({ }, handleSplitMove : function(){ - this.refs.editor.update(); + this.editor.current.update(); }, handleTextChange : function(text){ @@ -113,7 +115,7 @@ const EditPage = createClass({ brew : { ...prevState.brew, text: text }, isPending : true, htmlErrors : htmlErrors, - currentEditorPage : this.refs.editor.getCurrentPage() - 1 //Offset index since Marked starts pages at 0 + currentEditorPage : this.editor.current.getCurrentPage() - 1 //Offset index since Marked starts pages at 0 }), ()=>{if(this.state.autoSave) this.trySave();}); }, @@ -390,9 +392,9 @@ const EditPage = createClass({ {this.renderNavbar()}
- + ({ brew : { ...prevState.brew, text: text }, - currentEditorPage : this.refs.editor.getCurrentPage() - 1 //Offset index since Marked starts pages at 0 + currentEditorPage : this.editor.current.getCurrentPage() - 1 //Offset index since Marked starts pages at 0 })); }, renderNavbar : function(){ @@ -79,9 +82,9 @@ const HomePage = createClass({ {this.renderNavbar()}
- + ({ brew : { ...prevState.brew, text: text }, htmlErrors : htmlErrors, - currentEditorPage : this.refs.editor.getCurrentPage() - 1 //Offset index since Marked starts pages at 0 + currentEditorPage : this.editor.current.getCurrentPage() - 1 //Offset index since Marked starts pages at 0 })); localStorage.setItem(BREWKEY, text); }, @@ -212,9 +214,9 @@ const NewPage = createClass({ return
{this.renderNavbar()}
- + {/* Apply CSS from Style tab */} {this.renderStyle()} -
+
{this.renderPages()}
; diff --git a/shared/naturalcrit/codeEditor/codeEditor.jsx b/shared/naturalcrit/codeEditor/codeEditor.jsx index 9f908d4bd..e624694f1 100644 --- a/shared/naturalcrit/codeEditor/codeEditor.jsx +++ b/shared/naturalcrit/codeEditor/codeEditor.jsx @@ -62,6 +62,8 @@ const CodeEditor = createClass({ }; }, + editor : React.createRef(null), + componentDidMount : function() { this.buildEditor(); const newDoc = CodeMirror.Doc(this.props.value, this.props.language); @@ -101,7 +103,7 @@ const CodeEditor = createClass({ }, buildEditor : function() { - this.codeMirror = CodeMirror(this.refs.editor, { + this.codeMirror = CodeMirror(this.editor.current, { lineNumbers : true, lineWrapping : this.props.wrap, indentWithTabs : false, @@ -442,7 +444,7 @@ const CodeEditor = createClass({ render : function(){ return <> -
+
; } }); diff --git a/shared/naturalcrit/splitPane/splitPane.jsx b/shared/naturalcrit/splitPane/splitPane.jsx index b53d9de48..55af5e386 100644 --- a/shared/naturalcrit/splitPane/splitPane.jsx +++ b/shared/naturalcrit/splitPane/splitPane.jsx @@ -23,6 +23,9 @@ const SplitPane = createClass({ }; }, + pane1 : React.createRef(null), + pane2 : React.createRef(null), + componentDidMount : function() { const dividerPos = window.localStorage.getItem(this.props.storageKey); if(dividerPos){ @@ -136,7 +139,6 @@ const SplitPane = createClass({ render : function(){ return
{React.cloneElement(this.props.children[0], { @@ -146,7 +148,7 @@ const SplitPane = createClass({ })} {this.renderDivider()} - {this.props.children[1]} + {this.props.children[1]}
; } });