@jupyterlab/git
Version:
A JupyterLab extension for version control using git
70 lines (69 loc) • 2.54 kB
JavaScript
import { MainAreaWidget } from '@jupyterlab/apputils';
class PreviewMainAreaWidget extends MainAreaWidget {
constructor(options) {
var _a;
super(options);
if ((_a = options.isPreview) !== null && _a !== void 0 ? _a : true) {
PreviewMainAreaWidget.disposePreviewWidget(PreviewMainAreaWidget.previewWidget);
PreviewMainAreaWidget.previewWidget = this;
}
}
/**
* Dispose screen as a preview screen
*/
static disposePreviewWidget(isPreview) {
var _a;
return isPreview && ((_a = PreviewMainAreaWidget.previewWidget) === null || _a === void 0 ? void 0 : _a.dispose());
}
/**
* Pin the preview screen if user clicks on tab title
*/
static pinWidget(tabPosition, tabBar, diffWidget) {
// We need to wait for the tab node to be inserted in the DOM
setTimeout(() => {
// Get the most recent tab opened
const tab = tabPosition >= 0 ? tabBar.contentNode.children[tabPosition] : null;
const tabTitle = tab === null || tab === void 0 ? void 0 : tab.querySelector('.lm-TabBar-tabLabel');
if (!tabTitle) {
return;
}
tabTitle.classList.add('jp-git-tab-mod-preview');
const onClick = () => {
tabTitle.classList.remove('jp-git-tab-mod-preview');
tabTitle.removeEventListener('click', onClick, true);
if (PreviewMainAreaWidget.previewWidget === diffWidget) {
PreviewMainAreaWidget.previewWidget = null;
}
};
tabTitle.addEventListener('click', onClick, true);
diffWidget.disposed.connect(() => {
tabTitle.removeEventListener('click', onClick, true);
});
}, 0);
}
/**
* Callback just after the widget is attached to the DOM
*/
onAfterAttach(msg) {
super.onAfterAttach(msg);
this.node.addEventListener('click', this._onClick.bind(this), false);
}
/**
* Callback just before the widget is detached from the DOM
*/
onBeforeDetach(msg) {
this.node.removeEventListener('click', this._onClick.bind(this), false);
super.onBeforeAttach(msg);
}
/**
* Callback on click event in capture phase
*/
_onClick() {
PreviewMainAreaWidget.previewWidget = null;
}
}
/**
* Handle on the preview widget
*/
PreviewMainAreaWidget.previewWidget = null;
export { PreviewMainAreaWidget };