Skip to content

Commit d650b01

Browse files
committed
Update the examples to use the new menu definitions from settings
1 parent fe27afb commit d650b01

File tree

58 files changed

+521
-452
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+521
-452
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ jlpm run build
6666
jupyter lab
6767
```
6868

69-
The examples currently target **JupyterLab 3.x**.
69+
The examples currently target **JupyterLab 3.1 or later**.
7070

7171
If you would like to use the examples with JupyterLab 2.x, check out the [2.x branch](https://github.com/jupyterlab/extension-examples/tree/2.x).
7272

command-palette/package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@
1818
},
1919
"files": [
2020
"lib/**/*.{d.ts,eot,gif,html,jpg,js,js.map,json,png,svg,woff2,ttf}",
21-
"style/**/*.{css,eot,gif,html,jpg,json,png,svg,woff2,ttf}",
22-
"style/index.js"
21+
"style/**/*.{css,eot,js,gif,html,jpg,json,png,svg,woff2,ttf}"
2322
],
2423
"main": "lib/index.js",
2524
"types": "lib/index.d.ts",

commands/package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@
1818
},
1919
"files": [
2020
"lib/**/*.{d.ts,eot,gif,html,jpg,js,js.map,json,png,svg,woff2,ttf}",
21-
"style/**/*.{css,eot,gif,html,jpg,json,png,svg,woff2,ttf}",
22-
"style/index.js"
21+
"style/**/*.{css,eot,js,gif,html,jpg,json,png,svg,woff2,ttf}"
2322
],
2423
"main": "lib/index.js",
2524
"types": "lib/index.d.ts",

completer/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ fetch(
124124
[_JupyterLab_'s completer-extension](https://github.com/jupyterlab/jupyterlab/tree/master/packages/completer-extension) includes a notebooks plugin that registers notebooks for code completion. Your extension will override the notebooks plugin's behavior, so you can [disable notebooks](https://jupyterlab.readthedocs.io/en/stable/extension/extension_dev.html#disabling-other-extensions) in your `.package.json`:
125125
126126
```json5
127-
// package.json#L83-L90
127+
// package.json#L81-L88
128128

129129
"jupyterlab": {
130130
"extension": true,

completer/package.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@
1818
},
1919
"files": [
2020
"lib/**/*.{d.ts,eot,gif,html,jpg,js,js.map,json,png,svg,woff2,ttf}",
21-
"style/**/*.{css,eot,gif,html,jpg,json,png,svg,woff2,ttf}",
22-
"style/index.js"
21+
"style/**/*.{css,eot,js,gif,html,jpg,json,png,svg,woff2,ttf}"
2322
],
2423
"main": "lib/index.js",
2524
"types": "lib/index.d.ts",
@@ -52,7 +51,6 @@
5251
"@jupyterlab/codeeditor": "^3.1.0",
5352
"@jupyterlab/completer": "^3.1.0",
5453
"@jupyterlab/launcher": "^3.1.0",
55-
"@jupyterlab/mainmenu": "^3.1.0",
5654
"@jupyterlab/notebook": "^3.1.0",
5755
"@jupyterlab/outputarea": "^3.1.0",
5856
"@jupyterlab/statedb": "^3.1.0",

context-menu/README.md

Lines changed: 24 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ This is a basic example to show how to add a new entry to an existent context me
66

77
![context menu example](preview.gif)
88

9-
In JupyterLab plugins can expose context menus to offer an easy way to execute commands and perform actions. In this example, you will learn how to add a new entry to the file browser context menu, and at the same time how to register a new file type.
9+
In JupyterLab plugins can expose context menus to offer an easy way to execute commands and perform actions. In this example, you will learn how to add a new entry to the file browser context menu to be displayed on files with extension `.example`.
1010

1111
> It is strongly recommended to read [commands](https://github.com/jupyterlab/extension-examples/tree/master/commands) example before diving into this one.
1212
@@ -18,50 +18,23 @@ First of all, you will start looking into the declaration of the extension:
1818

1919
<!-- prettier-ignore-start -->
2020
```ts
21-
// src/index.ts#L9-L14
21+
// src/index.ts#L9-L13
2222

2323
const extension: JupyterFrontEndPlugin<void> = {
2424
id: 'context-menu',
2525
autoStart: true,
2626
requires: [IFileBrowserFactory],
27-
optional: [],
2827
activate: (app: JupyterFrontEnd, factory: IFileBrowserFactory) => {
2928
```
3029
<!-- prettier-ignore-end -->
3130
3231
For this extension, you need to require `IFileBrowserFactory` to track the file browser item clicked by the user.
3332
34-
The example shows you how to create a new file type and add the entry to the context menu only to this file type. The first step is optional, you can also add your button to an existing file type (or all of them!).
35-
36-
To register a new file type, you need to call the `addFileType()` method of `docRegistry` property present in the `JupyterFrontEnd` object. This method requires an `IFileType` object with some properties to define your file type. The most important are:
37-
38-
- `name`: the new file type.
39-
- `extension`: the list of extensions.
40-
- `fileFormat`: the file content format (_base64_, _json_ or _text_).
41-
- `contentType`: the file type (_directory_, _notebook_ or _file_).
42-
- `mimeType`: the content mime type.
33+
The first step is to define the command that will be executed when clicking on the context menu entry. If you want to access the item information, you need to use the `IFileBrowserFactory` object to obtain the file browser selected item.
4334
4435
<!-- prettier-ignore-start -->
4536
```ts
46-
// src/index.ts#L15-L23
47-
48-
app.docRegistry.addFileType({
49-
name: 'example',
50-
icon: runIcon,
51-
displayName: 'Example File',
52-
extensions: ['.example'],
53-
fileFormat: 'text',
54-
contentType: 'file',
55-
mimeTypes: ['text/plain'],
56-
});
57-
```
58-
<!-- prettier-ignore-end -->
59-
60-
The next step is to define the command that will be executed when clicking on the context menu entry. If you want to access the item information, you need to use the `IFileBrowserFactory` object to obtain the file browser selected item.
61-
62-
<!-- prettier-ignore-start -->
63-
```ts
64-
// src/index.ts#L25-L38
37+
// src/index.ts#L14-L27
6538

6639
app.commands.addCommand('jlab-examples/context-menu:open', {
6740
label: 'Example',
@@ -80,24 +53,32 @@ app.commands.addCommand('jlab-examples/context-menu:open', {
8053
```
8154
<!-- prettier-ignore-end -->
8255
83-
Finally, you can add the command to a context menu using the `addItem()` method present in the `contextMenu` property. This method requires an `IItemOptions` object with the following properties:
56+
Then, you can add the command to a context menu using the settings file.
57+
You will need to define a context menu item under the property `context` of the special key
58+
`jupyter.lab.menus`. And the properties of the context menu item are:
8459
8560
- `command`: the command to execute.
86-
- `selector`: the CSS classes of the element where you want to add the entry.
87-
- `rank`: the position in the context menu
61+
- `selector`: the CSS classes of the element on which you want to add the entry.
62+
- `rank`: (optional) number to order the context menu entries
8863
8964
<!-- prettier-ignore-start -->
90-
```ts
91-
// src/index.ts#L40-L44
92-
93-
app.contextMenu.addItem({
94-
command: 'jlab-examples/context-menu:open',
95-
selector: '.jp-DirListing-item[data-file-type="example"]',
96-
rank: 0,
97-
});
65+
```json5
66+
// schema/plugin.json#L4-L12
67+
68+
"jupyter.lab.menus": {
69+
"context": [
70+
{
71+
"command": "jlab-examples/context-menu:open",
72+
"selector": ".jp-DirListing-item[data-file-type=\"example\"]",
73+
"rank": 0
74+
}
75+
]
76+
},
9877
```
9978
<!-- prettier-ignore-end -->
10079
101-
The `selector` can be any valid [CSS selector](https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Selectors). In this case, the first part is the CSS class that identifies the file browser items `.jp-DirListing-item` and the second part `[data-file-type="example"]` is a attribute value to be found on the item. You can omit the second part to add the button to every file type.
80+
The `selector` can be any valid [CSS selector](https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Selectors). In this case, the first part is the CSS class that identifies the file browser items `.jp-DirListing-item` and the second part `[data-file-type="example"]` is a attribute value to be found on the item; the `data-file-type` attribute is set with the file extension. You can omit the second part to add the command to every file type.
10281
10382
You can find some of the CSS classes that identify different widgets in JupyterLab in the [developer documentation](https://jupyterlab.readthedocs.io/en/stable/developer/css.html#commonly-used-css-selectors).
83+
84+
> See also the [documentation](https://jupyterlab.readthedocs.io/en/stable/extension/extension_points.html#context-menu).

context-menu/package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
},
1919
"files": [
2020
"lib/**/*.{d.ts,eot,gif,html,jpg,js,js.map,json,png,svg,woff2,ttf}",
21-
"style/**/*.{css,eot,gif,html,jpg,json,png,svg,woff2,ttf}",
22-
"style/index.js"
21+
"style/**/*.{css,eot,js,gif,html,jpg,json,png,svg,woff2,ttf}",
22+
"schema/**/*.json"
2323
],
2424
"main": "lib/index.js",
2525
"types": "lib/index.d.ts",
@@ -73,7 +73,8 @@
7373
],
7474
"jupyterlab": {
7575
"extension": true,
76-
"outputDir": "jupyterlab_examples_context_menu/labextension"
76+
"outputDir": "jupyterlab_examples_context_menu/labextension",
77+
"schemaDir": "schema"
7778
},
7879
"styleModule": "style/index.js"
7980
}

context-menu/schema/plugin.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"title": "Context Menu Example",
3+
"description": "Context Menu Example settings.",
4+
"jupyter.lab.menus": {
5+
"context": [
6+
{
7+
"command": "jlab-examples/context-menu:open",
8+
"selector": ".jp-DirListing-item[data-file-type=\"example\"]",
9+
"rank": 0
10+
}
11+
]
12+
},
13+
"additionalProperties": false,
14+
"type": "object"
15+
}

context-menu/src/index.ts

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,13 @@ import {
44
} from '@jupyterlab/application';
55
import { IFileBrowserFactory } from '@jupyterlab/filebrowser';
66
import { showDialog, Dialog } from '@jupyterlab/apputils';
7-
import { buildIcon, runIcon } from '@jupyterlab/ui-components';
7+
import { buildIcon } from '@jupyterlab/ui-components';
88

99
const extension: JupyterFrontEndPlugin<void> = {
1010
id: 'context-menu',
1111
autoStart: true,
1212
requires: [IFileBrowserFactory],
13-
optional: [],
1413
activate: (app: JupyterFrontEnd, factory: IFileBrowserFactory) => {
15-
app.docRegistry.addFileType({
16-
name: 'example',
17-
icon: runIcon,
18-
displayName: 'Example File',
19-
extensions: ['.example'],
20-
fileFormat: 'text',
21-
contentType: 'file',
22-
mimeTypes: ['text/plain'],
23-
});
24-
2514
app.commands.addCommand('jlab-examples/context-menu:open', {
2615
label: 'Example',
2716
caption: "Example context menu button for file browser's items.",
@@ -36,12 +25,6 @@ const extension: JupyterFrontEndPlugin<void> = {
3625
}).catch((e) => console.log(e));
3726
},
3827
});
39-
40-
app.contextMenu.addItem({
41-
command: 'jlab-examples/context-menu:open',
42-
selector: '.jp-DirListing-item[data-file-type="example"]',
43-
rank: 0,
44-
});
4528
},
4629
};
4730

custom-log-console/README.md

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,16 @@ First of all, you will start by looking into the declaration of the extension:
2525

2626
<!-- prettier-ignore-start -->
2727
```ts
28-
// src/index.ts#L28-L39
28+
// src/index.ts#L26-L35
2929

3030
const extension: JupyterFrontEndPlugin<void> = {
3131
id: 'custom-log-console',
3232
autoStart: true,
33-
requires: [ICommandPalette, IRenderMimeRegistry, IMainMenu, ILayoutRestorer],
34-
optional: [],
33+
requires: [ICommandPalette, IRenderMimeRegistry, ILayoutRestorer],
3534
activate: (
3635
app: JupyterFrontEnd,
3736
palette: ICommandPalette,
3837
rendermime: IRenderMimeRegistry,
39-
mainMenu: IMainMenu,
4038
restorer: ILayoutRestorer
4139
) => {
4240
```
@@ -48,7 +46,7 @@ In the `activate` function, the first step is to declare `logConsolePanel` and `
4846
4947
<!-- prettier-ignore-start -->
5048
```ts
51-
// src/index.ts#L42-L43
49+
// src/index.ts#L38-L39
5250

5351
let logConsolePanel: LogConsolePanel = null;
5452
let logConsoleWidget: MainAreaWidget<LogConsolePanel> = null;
@@ -59,7 +57,7 @@ The next step is to create a new widget when clicking on the button to open the
5957
6058
<!-- prettier-ignore-start -->
6159
```ts
62-
// src/index.ts#L76-L76
60+
// src/index.ts#L72-L72
6361

6462
const createLogConsoleWidget = (): void => {
6563
```
@@ -69,7 +67,7 @@ To initialize a new `LogConsoleWidget` you have to create a `LogConsolePanel` to
6967
7068
<!-- prettier-ignore-start -->
7169
```ts
72-
// src/index.ts#L77-L82
70+
// src/index.ts#L73-L78
7371

7472
logConsolePanel = new LogConsolePanel(
7573
new LoggerRegistry({
@@ -84,7 +82,7 @@ The `source` property identifies where the message comes from and is necessary t
8482
8583
<!-- prettier-ignore-start -->
8684
```ts
87-
// src/index.ts#L84-L84
85+
// src/index.ts#L80-L80
8886

8987
logConsolePanel.source = 'custom-log-console';
9088
```
@@ -94,7 +92,7 @@ Now you are ready to initialize a new `MainAreaWidget` passing the `logConsolePa
9492
9593
<!-- prettier-ignore-start -->
9694
```ts
97-
// src/index.ts#L86-L88
95+
// src/index.ts#L82-L84
9896

9997
logConsoleWidget = new MainAreaWidget<LogConsolePanel>({
10098
content: logConsolePanel,
@@ -106,7 +104,7 @@ The last step of the function `createLogConsoleWidget` is to establish how to pr
106104
107105
<!-- prettier-ignore-start -->
108106
```ts
109-
// src/index.ts#L112-L116
107+
// src/index.ts#L108-L112
110108

111109
logConsoleWidget.disposed.connect(() => {
112110
logConsoleWidget = null;
@@ -121,7 +119,7 @@ To launch a new log console, you can add a new command. In this case, you can us
121119
<!-- prettier-ignore-start -->
122120
123121
```ts
124-
// src/index.ts#L125-L136
122+
// src/index.ts#L121-L132
125123

126124
commands.addCommand('jlab-examples/custom-log-console:open', {
127125
label: 'Custom Log Console',
@@ -142,7 +140,7 @@ Finally, you can send log messages calling `log` method present on the `logger`
142140
- HTML message with `IHtmlLog`:
143141
<!-- prettier-ignore-start -->
144142
```ts
145-
// src/index.ts#L147-L153
143+
// src/index.ts#L143-L149
146144

147145
const msg: IHtmlLog = {
148146
type: 'html',
@@ -158,7 +156,7 @@ logConsolePanel?.logger?.log(msg);
158156
<!-- prettier-ignore-start -->
159157
160158
```ts
161-
// src/index.ts#L161-L167
159+
// src/index.ts#L157-L163
162160

163161
const msg: ITextLog = {
164162
type: 'text',
@@ -175,7 +173,7 @@ logConsolePanel?.logger?.log(msg);
175173
176174
<!-- prettier-ignore-start -->
177175
```ts
178-
// src/index.ts#L175-L188
176+
// src/index.ts#L171-L184
179177

180178
const data: nbformat.IOutput = {
181179
output_type: 'display_data',

0 commit comments

Comments
 (0)