Skip to content

Commit d4724af

Browse files
Trigger unfold on double-click or single-click depending on configuration file (#11)
* Trigger (un)fold on double click * Make setting for singleclick * React on settings change * Do not toggle on dblclick if singleClickToUnfold is true * Do not toggle if currently making a selection * Fix initial value for singleClickToUnfold * Lint Co-authored-by: martinRenou <[email protected]>
1 parent e2d06d2 commit d4724af

File tree

4 files changed

+61
-13
lines changed

4 files changed

+61
-13
lines changed

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
}
2323
],
2424
"files": [
25+
"schema/**/*.json",
2526
"lib/**/*.{d.ts,eot,gif,html,jpg,js,js.map,json,png,svg,woff2,ttf}",
2627
"style/**/*.{css,.js,eot,gif,html,jpg,json,png,svg,woff2,ttf}"
2728
],
@@ -74,6 +75,7 @@
7475
],
7576
"styleModule": "style/index.js",
7677
"jupyterlab": {
78+
"schemaDir": "schema",
7779
"extension": true,
7880
"outputDir": "jupyterlab-unfold/labextension"
7981
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"title": "jupyterlab-unfold settings",
3+
"description": "Settings for the JupyterLab-unfold extension.",
4+
"type": "object",
5+
"properties": {
6+
"singleClickToUnfold": {
7+
"type": "boolean",
8+
"title": "Single Click To Unfold",
9+
"description": "Whether a single click triggers an (un)fold.",
10+
"default": false
11+
}
12+
}
13+
}

src/index.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import { addIcon } from '@jupyterlab/ui-components';
1111

1212
import { WidgetTracker, ToolbarButton } from '@jupyterlab/apputils';
1313

14+
import { ISettingRegistry } from '@jupyterlab/settingregistry';
15+
1416
import { ITranslator } from '@jupyterlab/translation';
1517

1618
import { IStateDB } from '@jupyterlab/statedb';
@@ -22,6 +24,8 @@ import { FileTreeBrowser, FilterFileTreeBrowserModel } from './unfold';
2224
*/
2325
const EXTENSION_ID = 'jupyterlab-unfold';
2426

27+
const SETTINGS_ID = 'jupyterlab-unfold:jupyterlab-unfold-settings';
28+
2529
/**
2630
* The file browser namespace token.
2731
*/
@@ -30,14 +34,17 @@ const namespace = 'filebrowser';
3034
const extension: JupyterFrontEndPlugin<IFileBrowserFactory> = {
3135
id: EXTENSION_ID,
3236
provides: IFileBrowserFactory,
33-
requires: [IDocumentManager, ITranslator],
37+
requires: [IDocumentManager, ITranslator, ISettingRegistry],
3438
optional: [IStateDB],
3539
activate: async (
3640
app: JupyterFrontEnd,
3741
docManager: IDocumentManager,
3842
translator: ITranslator,
43+
settings: ISettingRegistry,
3944
state: IStateDB | null
4045
): Promise<IFileBrowserFactory> => {
46+
const setting = await settings.load(SETTINGS_ID);
47+
4148
const tracker = new WidgetTracker<FileTreeBrowser>({ namespace });
4249
const createFileBrowser = (
4350
id: string,
@@ -63,6 +70,14 @@ const extension: JupyterFrontEndPlugin<IFileBrowserFactory> = {
6370
app
6471
});
6572

73+
widget.listing.singleClickToUnfold = setting.get('singleClickToUnfold')
74+
.composite as boolean;
75+
76+
setting.changed.connect(() => {
77+
widget.listing.singleClickToUnfold = setting.get('singleClickToUnfold')
78+
.composite as boolean;
79+
});
80+
6681
// Track the newly created file browser.
6782
void tracker.add(widget);
6883

src/unfold.ts

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,10 @@ export class DirTreeListing extends DirListing {
201201
super({ ...options, renderer: new FileTreeRenderer(options.model) });
202202
}
203203

204+
set singleClickToUnfold(value: boolean) {
205+
this._singleClickToUnfold = value;
206+
}
207+
204208
get headerNode(): HTMLElement {
205209
return document.createElement('div');
206210
}
@@ -220,21 +224,32 @@ export class DirTreeListing extends DirListing {
220224

221225
protected async handleFileSelect(event: MouseEvent): Promise<void> {
222226
super.handleFileSelect(event);
227+
const entry = this.modelForClick(event);
223228

224-
if (Object.keys(this.selection).length === 1) {
225-
const selection = Object.keys(this.selection)[0];
226-
227-
const entry = await this.model.getEntry(selection);
228-
229+
if (entry) {
229230
if (entry.type === 'directory') {
230-
this.model.path = '/' + selection;
231-
this.model.toggle(entry.path);
231+
this.model.path = '/' + entry.path;
232+
233+
if (
234+
this._singleClickToUnfold &&
235+
Object.keys(this.selection).length === 1
236+
) {
237+
this.model.toggle(entry.path);
238+
}
232239
} else {
233-
this.model.path = '/' + PathExt.dirname(selection);
240+
this.model.path = '/' + PathExt.dirname(entry.path);
234241
}
235242
}
236243
}
237244

245+
private async _eventDblClick(event: MouseEvent): Promise<void> {
246+
const entry = this.modelForClick(event);
247+
248+
if (entry?.type === 'directory' && !this._singleClickToUnfold) {
249+
this.model.toggle(entry.path);
250+
}
251+
}
252+
238253
private _eventDragEnter(event: IDragEvent): void {
239254
if (event.mimeData.hasData(CONTENTS_MIME)) {
240255
// @ts-ignore
@@ -362,6 +377,9 @@ export class DirTreeListing extends DirListing {
362377

363378
handleEvent(event: Event): void {
364379
switch (event.type) {
380+
case 'dblclick':
381+
this._eventDblClick(event as MouseEvent);
382+
break;
365383
case 'lm-dragenter':
366384
this._eventDragEnter(event as IDragEvent);
367385
break;
@@ -376,6 +394,8 @@ export class DirTreeListing extends DirListing {
376394
break;
377395
}
378396
}
397+
398+
private _singleClickToUnfold = false;
379399
}
380400

381401
/**
@@ -400,10 +420,6 @@ export class FilterFileTreeBrowserModel extends FilterFileBrowserModel {
400420
this._path = value;
401421
}
402422

403-
async getEntry(path: string): Promise<Contents.IModel> {
404-
return await this.contentManager.get(path);
405-
}
406-
407423
/**
408424
* Change directory.
409425
*
@@ -625,4 +641,6 @@ export class FileTreeBrowser extends FileBrowser {
625641
}
626642

627643
model: FilterFileTreeBrowserModel;
644+
645+
listing: DirTreeListing;
628646
}

0 commit comments

Comments
 (0)