Skip to content

Commit

Permalink
force update the text in the text editor only at startup and when edi…
Browse files Browse the repository at this point in the history
…ting in the graph

This is a rework of the DOT source handling to avoid a race caused by
constantly feeding back the DOT source to the text editor. This race
seems to have been latent, but an upcoming commit that changes to the
new root API in React 18, which seems much faster, exposes it.

Now, the DOT source is only fed back to the text editor when
specifically requested. This is currenty done at startup and when
editing the graph in the graph view. This is not expected to interfere
with a user editing the text, since it should not be possible to do
that at the same time.

This also removes the now unnecessary workaround for
#236
implemented in 0062951.
  • Loading branch information
magjac committed Dec 17, 2023
1 parent ee5a76d commit 825aa82
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
9 changes: 1 addition & 8 deletions src/TextEditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ class TextEditor extends React.Component {
render() {
const { classes } = this.props;
var annotations = null;
let dotSrc = this.props.dotSrc;
if (this.props.error) {
annotations = [{
row: this.props.error.line - 1,
Expand All @@ -81,12 +80,6 @@ class TextEditor extends React.Component {
}
}
this.prevNumLines = this.props.error.numLines;
// If there's an error, the user may have updated the DOT source since
// the error occurred, without us yet having gotten an onChange event. We
// therefore read out the current text from the editor and feed that back
// into it as the "value" prop, instead of the possibly outdated DOT
// source, which would otherwise overwrite the user's updates.
dotSrc = this.editor.getSession().getDocument().$lines.join("\n");
}
this.prevError = this.props.error;
const locations = this.props.selectedGraphComponents.reduce(
Expand Down Expand Up @@ -128,7 +121,7 @@ class TextEditor extends React.Component {
onFocus={this.props.onFocus}
onBlur={this.props.onBlur}
name="text-editor"
value={dotSrc}
value={this.props.dotSrc}
// viewport height - app bar - 2 * padding
height={this.props.height}
width={this.props.width}
Expand Down
14 changes: 11 additions & 3 deletions src/pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ class Index extends React.Component {
initialized: false,
name: localStorage.getItem('name') || '',
dotSrc: dotSrc,
forceNewDotSrc: true,
dotSrcLastChangeTime: +localStorage.getItem('dotSrcLastChangeTime') || Date.now(),
svg: localStorage.getItem('svg') || '',
hasUndo: false,
Expand Down Expand Up @@ -149,11 +150,18 @@ class Index extends React.Component {
});
}

handleTextChange = (text, undoRedoState) => {
handleTextChangeFromGraph = (text) => {
const forceNewDotSrc = true;
const undoRedoState = null;
this.handleTextChange(text, undoRedoState, forceNewDotSrc);
}

handleTextChange = (text, undoRedoState, forceNewDotSrc) => {
this.setPersistentState((state) => {
const newState = {
name: state.name || (text ? this.createUntitledName(state.projects) : ''),
dotSrc: text,
forceNewDotSrc: forceNewDotSrc,
};
if (!this.disableDotSrcLastChangeTimeUpdate) {
newState.dotSrcLastChangeTime = Date.now();
Expand Down Expand Up @@ -843,7 +851,7 @@ class Index extends React.Component {
// allocated viewport width - 2 * padding
width={`calc(${columns.textEditor * 100 / 12}vw - 2 * 12px)`}
height={`calc(100vh - 64px - 2 * 12px - ${this.updatedSnackbarIsOpen ? "64px" : "0px"})`}
dotSrc={this.state.dotSrc}
dotSrc={this.state.forceNewDotSrc ? this.state.dotSrc : null}
onTextChange={this.handleTextChange}
onFocus={this.handleTextEditorFocus}
onBlur={this.handleTextEditorBlur}
Expand Down Expand Up @@ -885,7 +893,7 @@ class Index extends React.Component {
defaultNodeAttributes={this.state.defaultNodeAttributes}
defaultEdgeAttributes={this.state.defaultEdgeAttributes}
onFocus={this.handleGraphFocus}
onTextChange={this.handleTextChange}
onTextChange={this.handleTextChangeFromGraph}
onHelp={this.handleKeyboardShortcutsClick}
onSelect={this.handleGraphComponentSelect}
onUndo={this.undo}
Expand Down

0 comments on commit 825aa82

Please sign in to comment.