Skip to content

Commit

Permalink
Merge pull request #3477 from Gazook89/fix-refs
Browse files Browse the repository at this point in the history
Fix `ref` issues
  • Loading branch information
calculuschild committed May 21, 2024
2 parents 77c0af4 + 2453b62 commit dcd34cc
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 41 deletions.
51 changes: 27 additions & 24 deletions client/homebrew/editor/editor.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';},
Expand Down Expand Up @@ -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){
Expand All @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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);
Expand All @@ -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){
Expand All @@ -347,7 +350,7 @@ const Editor = createClass({
if(this.isText()){
return <>
<CodeEditor key='codeEditor'
ref='codeEditor'
ref={this.codeEditor}
language='gfm'
view={this.state.view}
value={this.props.brew.text}
Expand All @@ -359,7 +362,7 @@ const Editor = createClass({
if(this.isStyle()){
return <>
<CodeEditor key='codeEditor'
ref='codeEditor'
ref={this.codeEditor}
language='css'
view={this.state.view}
value={this.props.brew.style ?? DEFAULT_STYLE_TEXT}
Expand All @@ -384,28 +387,28 @@ const Editor = createClass({
},

redo : function(){
return this.refs.codeEditor?.redo();
return this.codeEditor.current?.redo();
},

historySize : function(){
return this.refs.codeEditor?.historySize();
return this.codeEditor.current?.historySize();
},

undo : function(){
return this.refs.codeEditor?.undo();
return this.codeEditor.current?.undo();
},

foldCode : function(){
return this.refs.codeEditor?.foldAllCode();
return this.codeEditor.current?.foldAllCode();
},

unfoldCode : function(){
return this.refs.codeEditor?.unfoldAllCode();
return this.codeEditor.current?.unfoldAllCode();
},

render : function(){
return (
<div className='editor' ref='main'>
<div className='editor' ref={this.editor}>
<SnippetBar
brew={this.props.brew}
view={this.state.view}
Expand All @@ -421,7 +424,7 @@ const Editor = createClass({
historySize={this.historySize()}
currentEditorTheme={this.state.editorTheme}
updateEditorTheme={this.updateEditorTheme}
cursorPos={this.refs.codeEditor?.getCursorPosition() || {}} />
cursorPos={this.codeEditor.current?.getCursorPosition() || {}} />

{this.renderEditor()}
</div>
Expand Down
10 changes: 6 additions & 4 deletions client/homebrew/pages/editPage/editPage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ const EditPage = createClass({
currentEditorPage : 0
};
},

editor : React.createRef(null),
savedBrew : null,

componentDidMount : function(){
Expand Down Expand Up @@ -101,7 +103,7 @@ const EditPage = createClass({
},

handleSplitMove : function(){
this.refs.editor.update();
this.editor.current.update();
},

handleTextChange : function(text){
Expand All @@ -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();});
},

Expand Down Expand Up @@ -390,9 +392,9 @@ const EditPage = createClass({
{this.renderNavbar()}

<div className='content'>
<SplitPane onDragFinish={this.handleSplitMove} ref='pane'>
<SplitPane onDragFinish={this.handleSplitMove}>
<Editor
ref='editor'
ref={this.editor}
brew={this.state.brew}
onTextChange={this.handleTextChange}
onStyleChange={this.handleStyleChange}
Expand Down
11 changes: 7 additions & 4 deletions client/homebrew/pages/homePage/homePage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ const HomePage = createClass({
currentEditorPage : 0
};
},

editor : React.createRef(null),

handleSave : function(){
request.post('/api')
.send(this.state.brew)
Expand All @@ -50,12 +53,12 @@ const HomePage = createClass({
});
},
handleSplitMove : function(){
this.refs.editor.update();
this.editor.current.update();
},
handleTextChange : function(text){
this.setState((prevState)=>({
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(){
Expand All @@ -79,9 +82,9 @@ const HomePage = createClass({
{this.renderNavbar()}

<div className='content'>
<SplitPane onDragFinish={this.handleSplitMove} ref='pane'>
<SplitPane onDragFinish={this.handleSplitMove}>
<Editor
ref='editor'
ref={this.editor}
brew={this.state.brew}
onTextChange={this.handleTextChange}
renderer={this.state.brew.renderer}
Expand Down
10 changes: 6 additions & 4 deletions client/homebrew/pages/newPage/newPage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ const NewPage = createClass({
};
},

editor : React.createRef(null),

componentDidMount : function() {
document.addEventListener('keydown', this.handleControlKeys);

Expand Down Expand Up @@ -95,7 +97,7 @@ const NewPage = createClass({
},

handleSplitMove : function(){
this.refs.editor.update();
this.editor.current.update();
},

handleTextChange : function(text){
Expand All @@ -106,7 +108,7 @@ const NewPage = createClass({
this.setState((prevState)=>({
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);
},
Expand Down Expand Up @@ -212,9 +214,9 @@ const NewPage = createClass({
return <div className='newPage sitePage'>
{this.renderNavbar()}
<div className='content'>
<SplitPane onDragFinish={this.handleSplitMove} ref='pane'>
<SplitPane onDragFinish={this.handleSplitMove}>
<Editor
ref='editor'
ref={this.editor}
brew={this.state.brew}
onTextChange={this.handleTextChange}
onStyleChange={this.handleStyleChange}
Expand Down
2 changes: 1 addition & 1 deletion client/homebrew/pages/printPage/printPage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ const PrintPage = createClass({
<link href={`/themes/${rendererPath}/${themePath}/style.css`} type='text/css' rel='stylesheet'/>
{/* Apply CSS from Style tab */}
{this.renderStyle()}
<div className='pages' ref='pages' lang={this.state.brew.lang}>
<div className='pages' lang={this.state.brew.lang}>
{this.renderPages()}
</div>
</div>;
Expand Down
6 changes: 4 additions & 2 deletions shared/naturalcrit/codeEditor/codeEditor.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -442,7 +444,7 @@ const CodeEditor = createClass({
render : function(){
return <>
<link href={`../homebrew/cm-themes/${this.props.editorTheme}.css`} type='text/css' rel='stylesheet' />
<div className='codeEditor' ref='editor' style={this.props.style}/>
<div className='codeEditor' ref={this.editor} style={this.props.style}/>
</>;
}
});
Expand Down
6 changes: 4 additions & 2 deletions shared/naturalcrit/splitPane/splitPane.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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){
Expand Down Expand Up @@ -136,7 +139,6 @@ const SplitPane = createClass({
render : function(){
return <div className='splitPane' onPointerMove={this.handleMove} onPointerUp={this.handleUp}>
<Pane
ref='pane1'
width={this.state.currentDividerPos}
>
{React.cloneElement(this.props.children[0], {
Expand All @@ -146,7 +148,7 @@ const SplitPane = createClass({
})}
</Pane>
{this.renderDivider()}
<Pane ref='pane2' isDragging={this.state.isDragging}>{this.props.children[1]}</Pane>
<Pane isDragging={this.state.isDragging}>{this.props.children[1]}</Pane>
</div>;
}
});
Expand Down

0 comments on commit dcd34cc

Please sign in to comment.