Skip to content
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ export interface ReactDiffViewerProps {
lineId: string,
event: React.MouseEvent<HTMLTableCellElement>,
) => void;
onLineContentClick?: (params: {
direction: 'leftCode' | 'rightCode';
words: string;
}) => void;
// Array of line ids to highlight lines.
highlightLines?: string[];
// Style overrides.
Expand Down Expand Up @@ -167,6 +171,25 @@ class DiffViewer extends React.Component<ReactDiffViewerProps, ReactDiffViewerSt
return (): void => { };
};

private onLineContentClickProxy: (params: { direction: number; value: string | Array<any>; }) => any = ({ direction, value }) => {
let words: string | Array<any>
let _direction: 'leftCode' | 'rightCode'
if (!this.props.onLineContentClick) return () => { }
if (this.props.onLineContentClick) return (event: React.MouseEvent<HTMLTableCellElement>): void => {
event.stopPropagation()
event.preventDefault()
words = (value as string)
_direction = 1===direction? 'rightCode': 'leftCode'
if ('[object String]'!==Object.prototype.toString.call(value)) {
words = (value as Array<any>).map((item: { value: string }): string => item.value).join('')
}
this.props.onLineContentClick({
direction: _direction,
words
})
}
}

/**
* Maps over the word diff and constructs the required React elements to show word diff.
*
Expand Down Expand Up @@ -277,6 +300,7 @@ class DiffViewer extends React.Component<ReactDiffViewerProps, ReactDiffViewerSt
</pre>
</td>
<td
onClick={ this.onLineContentClickProxy({ direction: type, value }) }
className={cn(this.styles.content, {
[this.styles.emptyLine]: !content,
[this.styles.diffAdded]: added,
Expand Down