Skip to content

Commit

Permalink
Customizable file editor toolbar
Browse files Browse the repository at this point in the history
  • Loading branch information
fcollonval committed Jul 5, 2021
1 parent 420d0f7 commit 9fa65ce
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 32 deletions.
114 changes: 86 additions & 28 deletions packages/fileeditor-extension/schema/plugin.json
@@ -1,4 +1,6 @@
{
"title": "Text Editor",
"description": "Text editor settings.",
"jupyter.lab.setting-icon": "ui-components:text-editor",
"jupyter.lab.setting-icon-label": "Editor",
"jupyter.lab.menus": {
Expand Down Expand Up @@ -144,8 +146,44 @@
}
]
},
"title": "Text Editor",
"description": "Text editor settings.",
"jupyter.lab.toolbars": {
"HTML Viewer": []
},
"jupyter.lab.transform": true,
"properties": {
"editorConfig": {
"title": "Editor Configuration",
"description": "The configuration for all text editors.\nIf `fontFamily`, `fontSize` or `lineHeight` are `null`,\nvalues from current theme are used.",
"$ref": "#/definitions/editorConfig",
"default": {
"autoClosingBrackets": true,
"cursorBlinkRate": 530,
"fontFamily": null,
"fontSize": null,
"lineHeight": null,
"lineNumbers": true,
"lineWrap": "on",
"matchBrackets": true,
"readOnly": false,
"insertSpaces": true,
"tabSize": 4,
"wordWrapColumn": 80,
"rulers": [],
"codeFolding": false
}
},
"toolbar": {
"title": "Text editor toolbar items",
"description": "Note: To disable a toolbar item,\ncopy it to User Preferences and add the\n\"disabled\" key. Toolbar description:",
"items": {
"$ref": "#/definitions/toolbarItem"
},
"type": "array",
"default": []
}
},
"additionalProperties": false,
"type": "object",
"definitions": {
"editorConfig": {
"properties": {
Expand Down Expand Up @@ -202,31 +240,51 @@
},
"additionalProperties": false,
"type": "object"
},
"toolbarItem": {
"properties": {
"name": {
"title": "Unique name",
"type": "string"
},
"args": {
"title": "Command arguments",
"type": "object"
},
"command": {
"title": "Command id",
"type": "string",
"default": ""
},
"disabled": {
"title": "Whether the item is disabled or not",
"type": "boolean",
"default": false
},
"icon": {
"title": "Item icon id",
"description": "If defined, it will override the command icon",
"type": "string"
},
"label": {
"title": "Item icon label",
"description": "If defined, it will override the command label",
"type": "string"
},
"type": {
"title": "Item type",
"type": "string",
"enum": ["command", "spacer"]
},
"rank": {
"title": "Item rank",
"type": "number",
"minimum": 0
}
},
"required": ["name"],
"additionalProperties": false,
"type": "object"
}
},
"properties": {
"editorConfig": {
"title": "Editor Configuration",
"description": "The configuration for all text editors.\nIf `fontFamily`, `fontSize` or `lineHeight` are `null`,\nvalues from current theme are used.",
"$ref": "#/definitions/editorConfig",
"default": {
"autoClosingBrackets": true,
"cursorBlinkRate": 530,
"fontFamily": null,
"fontSize": null,
"lineHeight": null,
"lineNumbers": true,
"lineWrap": "on",
"matchBrackets": true,
"readOnly": false,
"insertSpaces": true,
"tabSize": 4,
"wordWrapColumn": 80,
"rulers": [],
"codeFolding": false
}
}
},
"additionalProperties": false,
"type": "object"
}
}
28 changes: 24 additions & 4 deletions packages/fileeditor-extension/src/index.ts
Expand Up @@ -11,13 +11,15 @@ import {
JupyterFrontEndPlugin
} from '@jupyterlab/application';
import {
createToolbarFactory,
ICommandPalette,
ISessionContextDialogs,
IToolbarWidgetRegistry,
WidgetTracker
} from '@jupyterlab/apputils';
import { CodeEditor, IEditorServices } from '@jupyterlab/codeeditor';
import { IConsoleTracker } from '@jupyterlab/console';
import { IDocumentWidget } from '@jupyterlab/docregistry';
import { DocumentRegistry, IDocumentWidget } from '@jupyterlab/docregistry';
import { IFileBrowserFactory } from '@jupyterlab/filebrowser';
import {
FileEditor,
Expand Down Expand Up @@ -54,7 +56,8 @@ const plugin: JupyterFrontEndPlugin<IEditorTracker> = {
ILauncher,
IMainMenu,
ILayoutRestorer,
ISessionContextDialogs
ISessionContextDialogs,
IToolbarWidgetRegistry
],
provides: IEditorTracker,
autoStart: true
Expand Down Expand Up @@ -155,17 +158,34 @@ function activate(
launcher: ILauncher | null,
menu: IMainMenu | null,
restorer: ILayoutRestorer | null,
sessionDialogs: ISessionContextDialogs | null
sessionDialogs: ISessionContextDialogs | null,
toolbarRegistry: IToolbarWidgetRegistry | null
): IEditorTracker {
const id = plugin.id;
const trans = translator.load('jupyterlab');
const namespace = 'editor';
let toolbarFactory:
| ((widget: IDocumentWidget<FileEditor>) => DocumentRegistry.IToolbarItem[])
| undefined;

if (toolbarRegistry) {
toolbarFactory = createToolbarFactory(
toolbarRegistry,
settingRegistry,
FACTORY,
id,
translator
);
}

const factory = new FileEditorFactory({
editorServices,
factoryOptions: {
name: FACTORY,
fileTypes: ['markdown', '*'], // Explicitly add the markdown fileType so
defaultFor: ['markdown', '*'] // it outranks the defaultRendered viewer.
defaultFor: ['markdown', '*'], // it outranks the defaultRendered viewer.
toolbarFactory,
translator
}
});
const { commands, restored, shell } = app;
Expand Down

0 comments on commit 9fa65ce

Please sign in to comment.