|
| 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 | + ['M', fileStyle.modified], |
| 10 | + ['A', fileStyle.added], |
| 11 | + ['D', fileStyle.deleted], |
| 12 | + ['R', fileStyle.modified], |
| 13 | + ['C', fileStyle.modified], |
| 14 | + ['U', fileStyle.modified], |
| 15 | + ['?', fileStyle.untracked], |
| 16 | + ['!', fileStyle.ignored] |
| 17 | +]); |
| 18 | + |
| 19 | +class GitListingRenderer extends DirListing.Renderer { |
| 20 | + constructor(private gitExtension: IGitExtension) { |
| 21 | + super(); |
| 22 | + } |
| 23 | + |
| 24 | + updateItemNode( |
| 25 | + node: HTMLElement, |
| 26 | + model: Contents.IModel, |
| 27 | + fileType?: DocumentRegistry.IFileType, |
| 28 | + translator?: ITranslator |
| 29 | + ) { |
| 30 | + super.updateItemNode(node, model, fileType, translator); |
| 31 | + const file = this.gitExtension.getFile(model.path); |
| 32 | + console.log(model.path, 'file status', file?.status); |
| 33 | + let status_code: Git.StatusCode = null; |
| 34 | + if (file) { |
| 35 | + status_code = file.status === 'staged' ? file.x : file.y; |
| 36 | + console.log(model.path, 'file x, y', file.x, file.y); |
| 37 | + console.log(model.path, 'file status code', status_code); |
| 38 | + } |
| 39 | + |
| 40 | + for (const [otherStatus, className] of statusStyles.entries()) { |
| 41 | + if (status_code === otherStatus) { |
| 42 | + node.classList.add(className); |
| 43 | + } else { |
| 44 | + node.classList.remove(className); |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +export function substituteListingRenderer( |
| 51 | + extension: IGitExtension, |
| 52 | + fileBrowser: FileBrowser |
| 53 | +): void { |
| 54 | + // eslint-disable-next-line @typescript-eslint/ban-ts-comment |
| 55 | + // @ts-ignore |
| 56 | + const listing: DirListing = fileBrowser._listing; |
| 57 | + const renderer = new GitListingRenderer(extension); |
| 58 | + // eslint-disable-next-line @typescript-eslint/ban-ts-comment |
| 59 | + // @ts-ignore |
| 60 | + listing._renderer = renderer; |
| 61 | +} |
0 commit comments