Skip to content

Commit e03dd77

Browse files
committed
Use React.RefObject instead of id
1 parent eac6a5d commit e03dd77

File tree

1 file changed

+15
-29
lines changed

1 file changed

+15
-29
lines changed

src/components/diff/PlainTextDiff.tsx

Lines changed: 15 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import * as React from 'react';
66
import { IDiffProps } from './Diff';
77
import { httpGitRequest } from '../../git';
88
import { mergeView } from './mergeview';
9-
import { getRefValue, IDiffContext, IGitRef, ISpecialRef } from './model';
9+
import { IDiffContext } from './model';
1010

1111
interface ICurrentReference {
1212
special?: 'WORKING' | 'INDEX';
@@ -32,6 +32,7 @@ export class PlainTextDiff extends React.Component<
3232
constructor(props: IPlainTextDiffProps) {
3333
super(props);
3434
this.state = { errorMessage: null };
35+
this._mergeViewRef = React.createRef<HTMLDivElement>();
3536
}
3637

3738
componentDidMount() {
@@ -54,13 +55,7 @@ export class PlainTextDiff extends React.Component<
5455
return (
5556
<div className="jp-git-diff-Widget">
5657
<div className="jp-git-diff-root">
57-
<div
58-
id={this._generateID(
59-
this.props.path,
60-
this.props.diffContext.currentRef
61-
)}
62-
className="jp-git-PlainText-diff"
63-
/>
58+
<div ref={this._mergeViewRef} className="jp-git-PlainText-diff" />
6459
</div>
6560
</div>
6661
);
@@ -120,12 +115,6 @@ export class PlainTextDiff extends React.Component<
120115
}
121116
}
122117

123-
private _generateID(path: string, currentRef: IGitRef | ISpecialRef): string {
124-
return `diffviewer-${this.props.path.replace('/', '-')}-${getRefValue(
125-
this.props.diffContext.currentRef
126-
)}`;
127-
}
128-
129118
/**
130119
* Creates and adds a diff viewer to the DOM with given content
131120
*
@@ -135,22 +124,19 @@ export class PlainTextDiff extends React.Component<
135124
private _addDiffViewer(prevContent: string, currContent: string) {
136125
const mode = Mode.findByFileName(this.props.path);
137126

138-
mergeView(
139-
document.getElementById(
140-
this._generateID(this.props.path, this.props.diffContext.currentRef)
141-
),
142-
{
143-
value: currContent,
144-
orig: prevContent,
145-
lineNumbers: true,
146-
mode: mode.mime,
147-
theme: 'jupyter',
148-
connect: 'align',
149-
collapseIdentical: true,
150-
revertButtons: false
151-
}
152-
);
127+
mergeView(this._mergeViewRef.current, {
128+
value: currContent,
129+
orig: prevContent,
130+
lineNumbers: true,
131+
mode: mode.mime,
132+
theme: 'jupyter',
133+
connect: 'align',
134+
collapseIdentical: true,
135+
revertButtons: false
136+
});
153137
}
138+
139+
private _mergeViewRef: React.RefObject<HTMLDivElement>;
154140
}
155141

156142
/**

0 commit comments

Comments
 (0)