From 9fa65ce9f42ebbcf3ab661d9ddbaf8210e2a30bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Collonval?= Date: Mon, 5 Jul 2021 17:46:07 +0200 Subject: [PATCH] Customizable file editor toolbar --- .../fileeditor-extension/schema/plugin.json | 114 +++++++++++++----- packages/fileeditor-extension/src/index.ts | 28 ++++- 2 files changed, 110 insertions(+), 32 deletions(-) diff --git a/packages/fileeditor-extension/schema/plugin.json b/packages/fileeditor-extension/schema/plugin.json index 07bceee52f46..a7566fe9f609 100644 --- a/packages/fileeditor-extension/schema/plugin.json +++ b/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": { @@ -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": { @@ -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" + } } diff --git a/packages/fileeditor-extension/src/index.ts b/packages/fileeditor-extension/src/index.ts index fc3617c18646..10108f0d09f4 100644 --- a/packages/fileeditor-extension/src/index.ts +++ b/packages/fileeditor-extension/src/index.ts @@ -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, @@ -54,7 +56,8 @@ const plugin: JupyterFrontEndPlugin = { ILauncher, IMainMenu, ILayoutRestorer, - ISessionContextDialogs + ISessionContextDialogs, + IToolbarWidgetRegistry ], provides: IEditorTracker, autoStart: true @@ -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) => 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;