|
| 1 | +import { Dialog, showDialog } from '@jupyterlab/apputils'; |
| 2 | +import { IRenderMimeRegistry } from '@jupyterlab/rendermime'; |
| 3 | +import * as React from 'react'; |
| 4 | +import { classes } from 'typestyle'; |
| 5 | +import { GitExtension } from '../model'; |
| 6 | +import { |
| 7 | + fileButtonStyle, |
| 8 | + fileGitButtonStyle, |
| 9 | + fileIconStyle, |
| 10 | + fileLabelStyle, |
| 11 | + fileStyle |
| 12 | +} from '../style/FileItemStyle'; |
| 13 | +import { |
| 14 | + changeStageButtonStyle, |
| 15 | + diffFileButtonStyle, |
| 16 | + discardFileButtonStyle |
| 17 | +} from '../style/GitStageStyle'; |
| 18 | +import { Git } from '../tokens'; |
| 19 | +import { openListedFile, parseFileExtension } from '../utils'; |
| 20 | +import { isDiffSupported } from './diff/Diff'; |
| 21 | +import { openDiffView } from './diff/DiffWidget'; |
| 22 | +import { ISpecialRef } from './diff/model'; |
| 23 | + |
| 24 | +export interface IFileItemSimpleProps { |
| 25 | + file: Git.IStatusFileResult; |
| 26 | + stage: string; |
| 27 | + model: GitExtension; |
| 28 | + discardFile: (file: string) => Promise<void>; |
| 29 | + renderMime: IRenderMimeRegistry; |
| 30 | +} |
| 31 | + |
| 32 | +export class FileItemSimple extends React.Component<IFileItemSimpleProps, {}> { |
| 33 | + constructor(props: IFileItemSimpleProps) { |
| 34 | + super(props); |
| 35 | + } |
| 36 | + |
| 37 | + getDiffFileIconClass() { |
| 38 | + return classes( |
| 39 | + fileButtonStyle, |
| 40 | + changeStageButtonStyle, |
| 41 | + fileGitButtonStyle, |
| 42 | + diffFileButtonStyle |
| 43 | + ); |
| 44 | + } |
| 45 | + |
| 46 | + getDiscardFileIconClass() { |
| 47 | + return classes( |
| 48 | + fileButtonStyle, |
| 49 | + changeStageButtonStyle, |
| 50 | + fileGitButtonStyle, |
| 51 | + discardFileButtonStyle |
| 52 | + ); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Callback method discarding unstanged changes for selected file. |
| 57 | + * It shows modal asking for confirmation and when confirmed make |
| 58 | + * server side call to git checkout to discard changes in selected file. |
| 59 | + */ |
| 60 | + async discardSelectedFileChanges() { |
| 61 | + const result = await showDialog({ |
| 62 | + title: 'Discard changes', |
| 63 | + body: `Are you sure you want to permanently discard changes to ${ |
| 64 | + this.props.file.from |
| 65 | + }? This action cannot be undone.`, |
| 66 | + buttons: [Dialog.cancelButton(), Dialog.warnButton({ label: 'Discard' })] |
| 67 | + }); |
| 68 | + if (result.button.accept) { |
| 69 | + this.props.discardFile(this.props.file.to); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + render() { |
| 74 | + return ( |
| 75 | + <li className={fileStyle}> |
| 76 | + <span |
| 77 | + className={classes( |
| 78 | + fileIconStyle, |
| 79 | + parseFileExtension(this.props.file.to) |
| 80 | + )} |
| 81 | + /> |
| 82 | + <span |
| 83 | + className={fileLabelStyle} |
| 84 | + onDoubleClick={() => |
| 85 | + openListedFile( |
| 86 | + this.props.file.x, |
| 87 | + this.props.file.y, |
| 88 | + this.props.file.to, |
| 89 | + this.props.model |
| 90 | + ) |
| 91 | + } |
| 92 | + title={this.props.file.to} |
| 93 | + > |
| 94 | + {this.props.file.to} |
| 95 | + </span> |
| 96 | + {this.props.stage === 'Changed' && ( |
| 97 | + <React.Fragment> |
| 98 | + <button |
| 99 | + className={`jp-Git-button ${this.getDiscardFileIconClass()}`} |
| 100 | + title={'Discard this change'} |
| 101 | + onClick={() => { |
| 102 | + this.discardSelectedFileChanges(); |
| 103 | + }} |
| 104 | + /> |
| 105 | + {isDiffSupported(this.props.file.to) && |
| 106 | + this.diffButton({ specialRef: 'WORKING' })} |
| 107 | + </React.Fragment> |
| 108 | + )} |
| 109 | + {this.props.stage === 'Staged' && |
| 110 | + isDiffSupported(this.props.file.to) && |
| 111 | + this.diffButton({ specialRef: 'INDEX' })} |
| 112 | + </li> |
| 113 | + ); |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * Creates a button element which is used to request diff from within the |
| 118 | + * Git panel. |
| 119 | + * |
| 120 | + * @param currentRef the ref to diff against the git 'HEAD' ref |
| 121 | + */ |
| 122 | + private diffButton(currentRef: ISpecialRef): JSX.Element { |
| 123 | + return ( |
| 124 | + <button |
| 125 | + className={`jp-Git-button ${this.getDiffFileIconClass()}`} |
| 126 | + title={'Diff this file'} |
| 127 | + onClick={async () => { |
| 128 | + await openDiffView( |
| 129 | + this.props.file.to, |
| 130 | + this.props.model, |
| 131 | + { |
| 132 | + previousRef: { gitRef: 'HEAD' }, |
| 133 | + currentRef: { specialRef: currentRef.specialRef } |
| 134 | + }, |
| 135 | + this.props.renderMime |
| 136 | + ); |
| 137 | + }} |
| 138 | + /> |
| 139 | + ); |
| 140 | + } |
| 141 | +} |
0 commit comments