Skip to content

Commit

Permalink
Merge pull request #1015 from fcollonval/fcollonval/issue1014
Browse files Browse the repository at this point in the history
Revert using default browser
  • Loading branch information
fcollonval authored Aug 19, 2021
2 parents fb53341 + a4be112 commit 0907a0d
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 63 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@jupyterlab/git",
"version": "0.32.1",
"version": "0.32.2",
"description": "A JupyterLab extension for version control using git",
"main": "lib/index.js",
"types": "lib/index.d.ts",
Expand Down
29 changes: 10 additions & 19 deletions src/commandsAndMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@ import {
showDialog,
showErrorMessage,
Toolbar,
ToolbarButton,
WidgetTracker
ToolbarButton
} from '@jupyterlab/apputils';
import { PathExt, URLExt } from '@jupyterlab/coreutils';
import { FileBrowser } from '@jupyterlab/filebrowser';
import { FileBrowser, FileBrowserModel } from '@jupyterlab/filebrowser';
import { Contents, ContentsManager } from '@jupyterlab/services';
import { ISettingRegistry } from '@jupyterlab/settingregistry';
import { ITerminal } from '@jupyterlab/terminal';
Expand All @@ -33,9 +32,9 @@ import {
diffIcon,
discardIcon,
gitIcon,
historyIcon,
openIcon,
removeIcon,
historyIcon
removeIcon
} from './style/icons';
import {
CommandIDs,
Expand Down Expand Up @@ -101,7 +100,7 @@ function pluralizedContextLabel(singular: string, plural: string) {
export function addCommands(
app: JupyterFrontEnd,
gitModel: GitExtension,
browserTracker: WidgetTracker<FileBrowser>,
fileBrowserModel: FileBrowserModel,
settings: ISettingRegistry.ISettings,
trans: TranslationBundle
): void {
Expand Down Expand Up @@ -178,7 +177,7 @@ export function addCommands(
'Create an empty Git repository or reinitialize an existing one'
),
execute: async () => {
const currentPath = browserTracker.currentWidget.model.path;
const currentPath = fileBrowserModel.path;
const result = await showDialog({
title: trans.__('Initialize a Repository'),
body: trans.__('Do you really want to make this directory a Git Repo?'),
Expand Down Expand Up @@ -311,14 +310,14 @@ export function addCommands(
gitModel,
Operation.Clone,
trans,
{ path: browserTracker.currentWidget.model.path, url: result.value }
{ path: fileBrowserModel.path, url: result.value }
);
logger.log({
message: trans.__('Successfully cloned'),
level: Level.SUCCESS,
details
});
await browserTracker.currentWidget.model.refresh();
await fileBrowserModel.refresh();
} catch (error) {
console.error(
'Encountered an error when cloning the repository. Error: ',
Expand Down Expand Up @@ -1044,17 +1043,9 @@ export function addMenuItems(
*/
export function addFileBrowserContextMenu(
model: IGitExtension,
tracker: WidgetTracker<FileBrowser>,
filebrowser: FileBrowser,
contextMenu: ContextMenuSvg
): void {
function getSelectedBrowserItems(): Contents.IModel[] {
const widget = tracker.currentWidget;
if (!widget) {
return [];
}
return toArray(widget.selectedItems());
}

let gitMenu: Menu;
let _commands: ContextCommandIDs[];
let _paths: string[];
Expand All @@ -1063,7 +1054,7 @@ export function addFileBrowserContextMenu(
const wasShown = menu.isVisible;
const parent = menu.parentMenu;

const items = getSelectedBrowserItems();
const items = toArray(filebrowser.selectedItems());
const statuses = new Set<Git.Status>(
items
.map(item => model.getFile(item.path)?.status)
Expand Down
37 changes: 14 additions & 23 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ import { Git, IGitExtension } from './tokens';
import { addCloneButton } from './widgets/gitClone';
import { GitWidget } from './widgets/GitWidget';

export { DiffModel } from './components/diff/model';
export { NotebookDiff } from './components/diff/NotebookDiff';
export { PlainTextDiff } from './components/diff/PlainTextDiff';
export { DiffModel } from './components/diff/model';
export { Git, IGitExtension } from './tokens';

/**
Expand Down Expand Up @@ -74,7 +74,11 @@ async function activate(
let settings: ISettingRegistry.ISettings;
let serverSettings: Git.IServerSettings;
// Get a reference to the default file browser extension
let filebrowser = factory.tracker.currentWidget;
// We don't use the current tracked browser because extension like jupyterlab-github
// or jupyterlab-gitlab are defining new filebrowsers that we don't support.
// And it is unlikely that another browser than the default will be used.
// Ref: https://github.com/jupyterlab/jupyterlab-git/issues/1014
const fileBrowser = factory.defaultBrowser;
translator = translator || nullTranslator;
const trans = translator.load('jupyterlab_git');

Expand Down Expand Up @@ -133,8 +137,8 @@ async function activate(
gitExtension = new GitExtension(docmanager, app.docRegistry, settings);

// Whenever we restore the application, sync the Git extension path
Promise.all([app.restored, filebrowser.model.restored]).then(() => {
gitExtension.pathRepository = filebrowser.model.path;
Promise.all([app.restored, fileBrowser.model.restored]).then(() => {
gitExtension.pathRepository = fileBrowser.model.path;
});

const onPathChanged = (
Expand All @@ -145,28 +149,15 @@ async function activate(
};

// Whenever the file browser path changes, sync the Git extension path
filebrowser.model.pathChanged.connect(onPathChanged);
fileBrowser.model.pathChanged.connect(onPathChanged);

const refreshBrowser = () => {
filebrowser.model.refresh();
fileBrowser.model.refresh();
};

// Whenever the `HEAD` of the Git repository changes, refresh the file browser
gitExtension.headChanged.connect(refreshBrowser);

// Handle file browser changes
factory.tracker.currentChanged.connect((_, browser) => {
filebrowser.model.pathChanged.disconnect(onPathChanged);

filebrowser = browser;
gitExtension.pathRepository = filebrowser.model.path;
filebrowser.model.pathChanged.connect(onPathChanged);

if (settings) {
addCloneButton(gitExtension, filebrowser, app.commands);
}
});

// Whenever a user adds/renames/saves/deletes/modifies a file within the lab environment, refresh the Git status
app.serviceManager.contents.fileChanged.connect(() =>
gitExtension.refreshStatus()
Expand All @@ -175,14 +166,14 @@ async function activate(
// Provided we were able to load application settings, create the extension widgets
if (settings) {
// Add JupyterLab commands
addCommands(app, gitExtension, factory.tracker, settings, trans);
addCommands(app, gitExtension, fileBrowser.model, settings, trans);

// Create the Git widget sidebar
const gitPlugin = new GitWidget(
gitExtension,
settings,
app.commands,
factory.tracker,
fileBrowser.model,
trans
);
gitPlugin.id = 'jp-git-sessions';
Expand All @@ -205,13 +196,13 @@ async function activate(
}

// Add a clone button to the file browser extension toolbar
addCloneButton(gitExtension, filebrowser, app.commands);
addCloneButton(gitExtension, fileBrowser, app.commands);

// Add the status bar widget
addStatusBarWidget(statusBar, gitExtension, settings, trans);

// Add the context menu items for the default file browser
addFileBrowserContextMenu(gitExtension, factory.tracker, app.contextMenu);
addFileBrowserContextMenu(gitExtension, fileBrowser, app.contextMenu);
}

// Register diff providers
Expand Down
33 changes: 13 additions & 20 deletions src/widgets/GitWidget.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ReactWidget, UseSignal, WidgetTracker } from '@jupyterlab/apputils';
import { FileBrowser } from '@jupyterlab/filebrowser';
import { ReactWidget, UseSignal } from '@jupyterlab/apputils';
import { FileBrowserModel } from '@jupyterlab/filebrowser';
import { ISettingRegistry } from '@jupyterlab/settingregistry';
import { TranslationBundle } from '@jupyterlab/translation';
import { CommandRegistry } from '@lumino/commands';
Expand All @@ -22,7 +22,7 @@ export class GitWidget extends ReactWidget {
model: GitExtension,
settings: ISettingRegistry.ISettings,
commands: CommandRegistry,
browserTracker: WidgetTracker<FileBrowser>,
fileBrowserModel: FileBrowserModel,
trans: TranslationBundle,
options?: Widget.IOptions
) {
Expand All @@ -32,7 +32,7 @@ export class GitWidget extends ReactWidget {

this._trans = trans;
this._commands = commands;
this._browserTracker = browserTracker;
this._fileBrowserModel = fileBrowserModel;
this._model = model;
this._settings = settings;

Expand Down Expand Up @@ -67,21 +67,14 @@ export class GitWidget extends ReactWidget {
<LoggerContext.Consumer>
{logger => (
<React.Fragment>
<UseSignal
signal={this._browserTracker.currentChanged}
initialArgs={this._browserTracker.currentWidget}
>
{(tracker, filebrowser) => (
<GitPanel
commands={this._commands}
filebrowser={filebrowser.model}
logger={logger}
model={this._model}
settings={this._settings}
trans={this._trans}
/>
)}
</UseSignal>
<GitPanel
commands={this._commands}
filebrowser={this._fileBrowserModel}
logger={logger}
model={this._model}
settings={this._settings}
trans={this._trans}
/>
<UseSignal
signal={logger.signal}
initialArgs={{ message: '', level: Level.INFO } as ILogMessage}
Expand All @@ -104,7 +97,7 @@ export class GitWidget extends ReactWidget {
}

private _commands: CommandRegistry;
private _browserTracker: WidgetTracker<FileBrowser>;
private _fileBrowserModel: FileBrowserModel;
private _model: GitExtension;
private _settings: ISettingRegistry.ISettings;
private _trans: TranslationBundle;
Expand Down

0 comments on commit 0907a0d

Please sign in to comment.