|
| 1 | +import { Git, IGitExtension } from './tokens'; |
| 2 | +import * as fileStyle from './style/BrowserFile'; |
| 3 | +import { DirListing, FileBrowser } from '@jupyterlab/filebrowser'; |
| 4 | +import { Contents } from '@jupyterlab/services'; |
| 5 | +import { DocumentRegistry } from '@jupyterlab/docregistry'; |
| 6 | +import { ITranslator } from '@jupyterlab/translation'; |
| 7 | + |
| 8 | +const statusStyles: Map<Git.StatusCode, string> = new Map([ |
| 9 | + // note: the classes cannot repeat, |
| 10 | + // otherwise the assignments will be overwritten |
| 11 | + ['M', fileStyle.modified], |
| 12 | + ['A', fileStyle.added], |
| 13 | + ['D', fileStyle.deleted], |
| 14 | + ['R', fileStyle.renamed], |
| 15 | + ['C', fileStyle.copied], |
| 16 | + ['U', fileStyle.updated], |
| 17 | + ['?', fileStyle.untracked], |
| 18 | + ['!', fileStyle.ignored] |
| 19 | +]); |
| 20 | + |
| 21 | +class GitListingRenderer extends DirListing.Renderer { |
| 22 | + constructor(private gitExtension: IGitExtension) { |
| 23 | + super(); |
| 24 | + } |
| 25 | + |
| 26 | + updateItemNode( |
| 27 | + node: HTMLElement, |
| 28 | + model: Contents.IModel, |
| 29 | + fileType?: DocumentRegistry.IFileType, |
| 30 | + translator?: ITranslator |
| 31 | + ) { |
| 32 | + super.updateItemNode(node, model, fileType, translator); |
| 33 | + const file = this.gitExtension.getFile(model.path); |
| 34 | + let status_code: Git.StatusCode = null; |
| 35 | + if (file) { |
| 36 | + status_code = file.status === 'staged' ? file.x : file.y; |
| 37 | + } |
| 38 | + |
| 39 | + for (const [otherStatus, className] of statusStyles.entries()) { |
| 40 | + if (status_code === otherStatus) { |
| 41 | + node.classList.add(className); |
| 42 | + } else { |
| 43 | + node.classList.remove(className); |
| 44 | + } |
| 45 | + } |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +export function substituteListingRenderer( |
| 50 | + extension: IGitExtension, |
| 51 | + fileBrowser: FileBrowser |
| 52 | +): void { |
| 53 | + // eslint-disable-next-line @typescript-eslint/ban-ts-comment |
| 54 | + // @ts-ignore |
| 55 | + const listing: DirListing = fileBrowser._listing; |
| 56 | + const renderer = new GitListingRenderer(extension); |
| 57 | + // eslint-disable-next-line @typescript-eslint/ban-ts-comment |
| 58 | + // @ts-ignore |
| 59 | + listing._renderer = renderer; |
| 60 | +} |
0 commit comments