From 94fbd642ce1d3e36298e2c42393a341dbe90822b Mon Sep 17 00:00:00 2001 From: Alex Bozarth Date: Thu, 25 Jul 2019 16:24:55 -0700 Subject: [PATCH 01/12] Refactor fileeditor-extension for modularization An initial POC for modularizing the features added in the activate function to allow other extensions to call them. This inital PR sets the ground work and includes the modularization of the `menu.addItems` code block. Part of the ongoing Issue #6901 --- packages/fileeditor-extension/package.json | 1 + packages/fileeditor-extension/src/commands.ts | 146 ++++++++++++++++++ packages/fileeditor-extension/src/index.ts | 124 ++------------- 3 files changed, 156 insertions(+), 115 deletions(-) create mode 100644 packages/fileeditor-extension/src/commands.ts diff --git a/packages/fileeditor-extension/package.json b/packages/fileeditor-extension/package.json index 7789da6f3a23..c7cb7c959a3b 100644 --- a/packages/fileeditor-extension/package.json +++ b/packages/fileeditor-extension/package.json @@ -47,6 +47,7 @@ "@jupyterlab/launcher": "^1.0.1", "@jupyterlab/mainmenu": "^1.0.1", "@jupyterlab/statusbar": "^1.0.1", + "@phosphor/commands": "^1.6.3", "@phosphor/coreutils": "^1.3.1", "@phosphor/widgets": "^1.8.0" }, diff --git a/packages/fileeditor-extension/src/commands.ts b/packages/fileeditor-extension/src/commands.ts new file mode 100644 index 000000000000..0399e69eb6cb --- /dev/null +++ b/packages/fileeditor-extension/src/commands.ts @@ -0,0 +1,146 @@ +// Copyright (c) Jupyter Development Team. +// Distributed under the terms of the Modified BSD License. + +import { WidgetTracker } from '@jupyterlab/apputils'; + +import { IConsoleTracker } from '@jupyterlab/console'; + +import { IDocumentWidget } from '@jupyterlab/docregistry'; + +import { FileEditor } from '@jupyterlab/fileeditor'; + +import { + IEditMenu, + IFileMenu, + IMainMenu, + IRunMenu, + IViewMenu +} from '@jupyterlab/mainmenu'; + +import { CommandRegistry } from '@phosphor/commands'; + +import { JSONObject } from '@phosphor/coreutils'; + +import { Menu } from '@phosphor/widgets'; + +import { CommandIDs } from './index'; + +export default class Commands { + static addMenuItems( + menu: IMainMenu, + commands: CommandRegistry, + tracker: WidgetTracker>, + consoleTracker: IConsoleTracker, + createConsole: (widget: IDocumentWidget) => Promise + ) { + // Add the editing commands to the settings menu. + const tabMenu = new Menu({ commands }); + tabMenu.title.label = 'Text Editor Indentation'; + let args: JSONObject = { + insertSpaces: false, + size: 4, + name: 'Indent with Tab' + }; + let command = 'fileeditor:change-tabs'; + tabMenu.addItem({ command, args }); + + for (let size of [1, 2, 4, 8]) { + let args: JSONObject = { + insertSpaces: true, + size, + name: `Spaces: ${size} ` + }; + tabMenu.addItem({ command, args }); + } + + menu.settingsMenu.addGroup( + [ + { + command: CommandIDs.changeFontSize, + args: { name: 'Increase Text Editor Font Size', delta: +1 } + }, + { + command: CommandIDs.changeFontSize, + args: { name: 'Decrease Text Editor Font Size', delta: -1 } + }, + { type: 'submenu', submenu: tabMenu }, + { command: CommandIDs.autoClosingBrackets } + ], + 30 + ); + + // Add new text file creation to the file menu. + menu.fileMenu.newMenu.addGroup([{ command: CommandIDs.createNew }], 30); + + // Add new markdown file creation to the file menu. + menu.fileMenu.newMenu.addGroup( + [{ command: CommandIDs.createNewMarkdown }], + 30 + ); + + // Add undo/redo hooks to the edit menu. + menu.editMenu.undoers.add({ + tracker, + undo: widget => { + widget.content.editor.undo(); + }, + redo: widget => { + widget.content.editor.redo(); + } + } as IEditMenu.IUndoer>); + + // Add editor view options. + menu.viewMenu.editorViewers.add({ + tracker, + toggleLineNumbers: widget => { + const lineNumbers = !widget.content.editor.getOption('lineNumbers'); + widget.content.editor.setOption('lineNumbers', lineNumbers); + }, + toggleWordWrap: widget => { + const oldValue = widget.content.editor.getOption('lineWrap'); + const newValue = oldValue === 'off' ? 'on' : 'off'; + widget.content.editor.setOption('lineWrap', newValue); + }, + toggleMatchBrackets: widget => { + const matchBrackets = !widget.content.editor.getOption('matchBrackets'); + widget.content.editor.setOption('matchBrackets', matchBrackets); + }, + lineNumbersToggled: widget => + widget.content.editor.getOption('lineNumbers'), + wordWrapToggled: widget => + widget.content.editor.getOption('lineWrap') !== 'off', + matchBracketsToggled: widget => + widget.content.editor.getOption('matchBrackets') + } as IViewMenu.IEditorViewer>); + + // Add a console creator the the Kernel menu. + menu.fileMenu.consoleCreators.add({ + tracker, + name: 'Editor', + createConsole + } as IFileMenu.IConsoleCreator>); + + // Add a code runner to the Run menu. + menu.runMenu.codeRunners.add({ + tracker, + noun: 'Code', + isEnabled: current => + !!consoleTracker.find(c => c.session.path === current.context.path), + run: () => commands.execute(CommandIDs.runCode), + runAll: () => commands.execute(CommandIDs.runAllCode), + restartAndRunAll: current => { + const console = consoleTracker.find( + console => console.session.path === current.context.path + ); + if (console) { + return console.session.restart().then(restarted => { + if (restarted) { + void commands.execute(CommandIDs.runAllCode); + } + return restarted; + }); + } + } + } as IRunMenu.ICodeRunner>); + } +} diff --git a/packages/fileeditor-extension/src/index.ts b/packages/fileeditor-extension/src/index.ts index 2379f659544c..9e6e4a54a34e 100644 --- a/packages/fileeditor-extension/src/index.ts +++ b/packages/fileeditor-extension/src/index.ts @@ -32,13 +32,7 @@ import { import { ILauncher } from '@jupyterlab/launcher'; -import { - IEditMenu, - IFileMenu, - IMainMenu, - IRunMenu, - IViewMenu -} from '@jupyterlab/mainmenu'; +import { IMainMenu } from '@jupyterlab/mainmenu'; import { IStatusBar } from '@jupyterlab/statusbar'; @@ -46,6 +40,8 @@ import { JSONObject, ReadonlyJSONObject } from '@phosphor/coreutils'; import { Menu } from '@phosphor/widgets'; +import Commands from './commands'; + /** * The class name for the text editor icon from the default theme. */ @@ -64,7 +60,7 @@ const FACTORY = 'Editor'; /** * The command IDs used by the fileeditor plugin. */ -namespace CommandIDs { +export namespace CommandIDs { export const createNew = 'fileeditor:create-new'; export const createNewMarkdown = 'fileeditor:create-new-markdown-file'; @@ -636,115 +632,13 @@ function activate( } if (menu) { - // Add the editing commands to the settings menu. - const tabMenu = new Menu({ commands }); - tabMenu.title.label = 'Text Editor Indentation'; - let args: JSONObject = { - insertSpaces: false, - size: 4, - name: 'Indent with Tab' - }; - let command = 'fileeditor:change-tabs'; - tabMenu.addItem({ command, args }); - - for (let size of [1, 2, 4, 8]) { - let args: JSONObject = { - insertSpaces: true, - size, - name: `Spaces: ${size} ` - }; - tabMenu.addItem({ command, args }); - } - - menu.settingsMenu.addGroup( - [ - { - command: CommandIDs.changeFontSize, - args: { name: 'Increase Text Editor Font Size', delta: +1 } - }, - { - command: CommandIDs.changeFontSize, - args: { name: 'Decrease Text Editor Font Size', delta: -1 } - }, - { type: 'submenu', submenu: tabMenu }, - { command: CommandIDs.autoClosingBrackets } - ], - 30 - ); - - // Add new text file creation to the file menu. - menu.fileMenu.newMenu.addGroup([{ command: CommandIDs.createNew }], 30); - - // Add new markdown file creation to the file menu. - menu.fileMenu.newMenu.addGroup( - [{ command: CommandIDs.createNewMarkdown }], - 30 - ); - - // Add undo/redo hooks to the edit menu. - menu.editMenu.undoers.add({ - tracker, - undo: widget => { - widget.content.editor.undo(); - }, - redo: widget => { - widget.content.editor.redo(); - } - } as IEditMenu.IUndoer>); - - // Add editor view options. - menu.viewMenu.editorViewers.add({ - tracker, - toggleLineNumbers: widget => { - const lineNumbers = !widget.content.editor.getOption('lineNumbers'); - widget.content.editor.setOption('lineNumbers', lineNumbers); - }, - toggleWordWrap: widget => { - const oldValue = widget.content.editor.getOption('lineWrap'); - const newValue = oldValue === 'off' ? 'on' : 'off'; - widget.content.editor.setOption('lineWrap', newValue); - }, - toggleMatchBrackets: widget => { - const matchBrackets = !widget.content.editor.getOption('matchBrackets'); - widget.content.editor.setOption('matchBrackets', matchBrackets); - }, - lineNumbersToggled: widget => - widget.content.editor.getOption('lineNumbers'), - wordWrapToggled: widget => - widget.content.editor.getOption('lineWrap') !== 'off', - matchBracketsToggled: widget => - widget.content.editor.getOption('matchBrackets') - } as IViewMenu.IEditorViewer>); - - // Add a console creator the the Kernel menu. - menu.fileMenu.consoleCreators.add({ + Commands.addMenuItems( + menu, + commands, tracker, - name: 'Editor', + consoleTracker, createConsole - } as IFileMenu.IConsoleCreator>); - - // Add a code runner to the Run menu. - menu.runMenu.codeRunners.add({ - tracker, - noun: 'Code', - isEnabled: current => - !!consoleTracker.find(c => c.session.path === current.context.path), - run: () => commands.execute(CommandIDs.runCode), - runAll: () => commands.execute(CommandIDs.runAllCode), - restartAndRunAll: current => { - const console = consoleTracker.find( - console => console.session.path === current.context.path - ); - if (console) { - return console.session.restart().then(restarted => { - if (restarted) { - void commands.execute(CommandIDs.runAllCode); - } - return restarted; - }); - } - } - } as IRunMenu.ICodeRunner>); + ); } app.contextMenu.addItem({ From abb9e0c886c9670517dd2bc680df6862f701b8e0 Mon Sep 17 00:00:00 2001 From: Alex Bozarth Date: Fri, 26 Jul 2019 14:48:44 -0700 Subject: [PATCH 02/12] Moved palette.addItem code into commands.ts --- packages/fileeditor-extension/src/commands.ts | 38 ++++++++++++++++++- packages/fileeditor-extension/src/index.ts | 34 +---------------- 2 files changed, 38 insertions(+), 34 deletions(-) diff --git a/packages/fileeditor-extension/src/commands.ts b/packages/fileeditor-extension/src/commands.ts index 0399e69eb6cb..bc4546174646 100644 --- a/packages/fileeditor-extension/src/commands.ts +++ b/packages/fileeditor-extension/src/commands.ts @@ -1,7 +1,7 @@ // Copyright (c) Jupyter Development Team. // Distributed under the terms of the Modified BSD License. -import { WidgetTracker } from '@jupyterlab/apputils'; +import { ICommandPalette, WidgetTracker } from '@jupyterlab/apputils'; import { IConsoleTracker } from '@jupyterlab/console'; @@ -26,6 +26,42 @@ import { Menu } from '@phosphor/widgets'; import { CommandIDs } from './index'; export default class Commands { + static addPaletteItems(palette: ICommandPalette) { + const category = 'Text Editor'; + let args: JSONObject = { + insertSpaces: false, + size: 4, + name: 'Indent with Tab' + }; + let command = 'fileeditor:change-tabs'; + palette.addItem({ command, args, category }); + + for (let size of [1, 2, 4, 8]) { + let args: JSONObject = { + insertSpaces: true, + size, + name: `Spaces: ${size} ` + }; + palette.addItem({ command, args, category }); + } + + args = { isPalette: true }; + command = CommandIDs.createNew; + palette.addItem({ command, args, category }); + + args = { isPalette: true }; + command = CommandIDs.createNewMarkdown; + palette.addItem({ command, args, category }); + + args = { name: 'Increase Font Size', delta: 1 }; + command = CommandIDs.changeFontSize; + palette.addItem({ command, args, category }); + + args = { name: 'Decrease Font Size', delta: -1 }; + command = CommandIDs.changeFontSize; + palette.addItem({ command, args, category }); + } + static addMenuItems( menu: IMainMenu, commands: CommandRegistry, diff --git a/packages/fileeditor-extension/src/index.ts b/packages/fileeditor-extension/src/index.ts index 9e6e4a54a34e..a4ed6db693bc 100644 --- a/packages/fileeditor-extension/src/index.ts +++ b/packages/fileeditor-extension/src/index.ts @@ -596,39 +596,7 @@ function activate( } if (palette) { - const category = 'Text Editor'; - let args: JSONObject = { - insertSpaces: false, - size: 4, - name: 'Indent with Tab' - }; - let command = 'fileeditor:change-tabs'; - palette.addItem({ command, args, category }); - - for (let size of [1, 2, 4, 8]) { - let args: JSONObject = { - insertSpaces: true, - size, - name: `Spaces: ${size} ` - }; - palette.addItem({ command, args, category }); - } - - args = { isPalette: true }; - command = CommandIDs.createNew; - palette.addItem({ command, args, category }); - - args = { isPalette: true }; - command = CommandIDs.createNewMarkdown; - palette.addItem({ command, args, category }); - - args = { name: 'Increase Font Size', delta: 1 }; - command = CommandIDs.changeFontSize; - palette.addItem({ command, args, category }); - - args = { name: 'Decrease Font Size', delta: -1 }; - command = CommandIDs.changeFontSize; - palette.addItem({ command, args, category }); + Commands.addPaletteItems(palette); } if (menu) { From bea95562aaa2b27e536a061384b9601b89ed1fe3 Mon Sep 17 00:00:00 2001 From: Alex Bozarth Date: Fri, 26 Jul 2019 14:52:17 -0700 Subject: [PATCH 03/12] Moved contextMenu.addItem code to commands.ts --- packages/fileeditor-extension/src/commands.ts | 12 ++++++++++++ packages/fileeditor-extension/src/index.ts | 9 +-------- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/packages/fileeditor-extension/src/commands.ts b/packages/fileeditor-extension/src/commands.ts index bc4546174646..61ca91bfb28f 100644 --- a/packages/fileeditor-extension/src/commands.ts +++ b/packages/fileeditor-extension/src/commands.ts @@ -24,6 +24,7 @@ import { JSONObject } from '@phosphor/coreutils'; import { Menu } from '@phosphor/widgets'; import { CommandIDs } from './index'; +import { JupyterFrontEnd } from '@jupyterlab/application'; export default class Commands { static addPaletteItems(palette: ICommandPalette) { @@ -179,4 +180,15 @@ export default class Commands { } } as IRunMenu.ICodeRunner>); } + + static addContextMenuItems(app: JupyterFrontEnd) { + app.contextMenu.addItem({ + command: CommandIDs.createConsole, + selector: '.jp-FileEditor' + }); + app.contextMenu.addItem({ + command: CommandIDs.markdownPreview, + selector: '.jp-FileEditor' + }); + } } diff --git a/packages/fileeditor-extension/src/index.ts b/packages/fileeditor-extension/src/index.ts index a4ed6db693bc..cf09272c0f37 100644 --- a/packages/fileeditor-extension/src/index.ts +++ b/packages/fileeditor-extension/src/index.ts @@ -609,14 +609,7 @@ function activate( ); } - app.contextMenu.addItem({ - command: CommandIDs.createConsole, - selector: '.jp-FileEditor' - }); - app.contextMenu.addItem({ - command: CommandIDs.markdownPreview, - selector: '.jp-FileEditor' - }); + Commands.addContextMenuItems(app); return tracker; } From 608a73a6021099984a0a3350d471c78ad1823a3a Mon Sep 17 00:00:00 2001 From: Alex Bozarth Date: Fri, 26 Jul 2019 14:55:15 -0700 Subject: [PATCH 04/12] moved launcher.add code to commands.ts --- packages/fileeditor-extension/src/commands.ts | 19 ++++++++++++++++++- packages/fileeditor-extension/src/index.ts | 12 +----------- 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/packages/fileeditor-extension/src/commands.ts b/packages/fileeditor-extension/src/commands.ts index 61ca91bfb28f..b80ac2457534 100644 --- a/packages/fileeditor-extension/src/commands.ts +++ b/packages/fileeditor-extension/src/commands.ts @@ -1,6 +1,8 @@ // Copyright (c) Jupyter Development Team. // Distributed under the terms of the Modified BSD License. +import { JupyterFrontEnd } from '@jupyterlab/application'; + import { ICommandPalette, WidgetTracker } from '@jupyterlab/apputils'; import { IConsoleTracker } from '@jupyterlab/console'; @@ -9,6 +11,8 @@ import { IDocumentWidget } from '@jupyterlab/docregistry'; import { FileEditor } from '@jupyterlab/fileeditor'; +import { ILauncher } from '@jupyterlab/launcher'; + import { IEditMenu, IFileMenu, @@ -24,9 +28,22 @@ import { JSONObject } from '@phosphor/coreutils'; import { Menu } from '@phosphor/widgets'; import { CommandIDs } from './index'; -import { JupyterFrontEnd } from '@jupyterlab/application'; export default class Commands { + static addLauncherItems(launcher: ILauncher) { + launcher.add({ + command: CommandIDs.createNew, + category: 'Other', + rank: 1 + }); + + launcher.add({ + command: CommandIDs.createNewMarkdown, + category: 'Other', + rank: 2 + }); + } + static addPaletteItems(palette: ICommandPalette) { const category = 'Text Editor'; let args: JSONObject = { diff --git a/packages/fileeditor-extension/src/index.ts b/packages/fileeditor-extension/src/index.ts index cf09272c0f37..ac3ac868dbd3 100644 --- a/packages/fileeditor-extension/src/index.ts +++ b/packages/fileeditor-extension/src/index.ts @@ -582,17 +582,7 @@ function activate( // Add a launcher item if the launcher is available. if (launcher) { - launcher.add({ - command: CommandIDs.createNew, - category: 'Other', - rank: 1 - }); - - launcher.add({ - command: CommandIDs.createNewMarkdown, - category: 'Other', - rank: 2 - }); + Commands.addLauncherItems(launcher); } if (palette) { From 62522e80dc677636c68752dc671a4d5bf9e9d1b9 Mon Sep 17 00:00:00 2001 From: Alex Bozarth Date: Fri, 26 Jul 2019 15:29:47 -0700 Subject: [PATCH 05/12] moved commands.addCommand code to commands.ts --- packages/fileeditor-extension/src/commands.ts | 346 +++++++++++++++++- packages/fileeditor-extension/src/index.ts | 326 +---------------- 2 files changed, 357 insertions(+), 315 deletions(-) diff --git a/packages/fileeditor-extension/src/commands.ts b/packages/fileeditor-extension/src/commands.ts index b80ac2457534..e6b73fce8e38 100644 --- a/packages/fileeditor-extension/src/commands.ts +++ b/packages/fileeditor-extension/src/commands.ts @@ -5,9 +5,23 @@ import { JupyterFrontEnd } from '@jupyterlab/application'; import { ICommandPalette, WidgetTracker } from '@jupyterlab/apputils'; +import { CodeEditor } from '@jupyterlab/codeeditor'; + import { IConsoleTracker } from '@jupyterlab/console'; -import { IDocumentWidget } from '@jupyterlab/docregistry'; +import { + ISettingRegistry, + MarkdownCodeBlocks, + PathExt +} from '@jupyterlab/coreutils'; + +import { + ABCWidgetFactory, + DocumentRegistry, + IDocumentWidget +} from '@jupyterlab/docregistry'; + +import { IFileBrowserFactory } from '@jupyterlab/filebrowser'; import { FileEditor } from '@jupyterlab/fileeditor'; @@ -23,13 +37,341 @@ import { import { CommandRegistry } from '@phosphor/commands'; -import { JSONObject } from '@phosphor/coreutils'; +import { JSONObject, ReadonlyJSONObject } from '@phosphor/coreutils'; import { Menu } from '@phosphor/widgets'; import { CommandIDs } from './index'; +/** + * The class name for the text editor icon from the default theme. + */ +export const EDITOR_ICON_CLASS = 'jp-MaterialIcon jp-TextEditorIcon'; + +/** + * The class name for the text editor icon from the default theme. + */ +export const MARKDOWN_ICON_CLASS = 'jp-MaterialIcon jp-MarkdownIcon'; + export default class Commands { + static getCreateConsoleFunction( + commands: CommandRegistry + ): ( + widget: IDocumentWidget, + args?: ReadonlyJSONObject + ) => Promise { + return async function createConsole( + widget: IDocumentWidget, + args?: ReadonlyJSONObject + ): Promise { + const options = args || {}; + const console = await commands.execute('console:create', { + activate: options['activate'], + name: widget.context.contentsModel.name, + path: widget.context.path, + preferredLanguage: widget.context.model.defaultKernelLanguage, + ref: widget.id, + insertMode: 'split-bottom' + }); + + widget.context.pathChanged.connect((sender, value) => { + console.session.setPath(value); + console.session.setName(widget.context.contentsModel.name); + }); + }; + } + + static addCommands( + commands: CommandRegistry, + config: CodeEditor.IConfig, + settingRegistry: ISettingRegistry, + id: string, + isEnabled: () => boolean, + tracker: WidgetTracker>, + browserFactory: IFileBrowserFactory, + factory: ABCWidgetFactory< + IDocumentWidget, + DocumentRegistry.ICodeModel + > + ) { + // Add a command to change font size. + commands.addCommand(CommandIDs.changeFontSize, { + execute: args => { + const delta = Number(args['delta']); + if (Number.isNaN(delta)) { + console.error( + `${CommandIDs.changeFontSize}: delta arg must be a number` + ); + return; + } + const style = window.getComputedStyle(document.documentElement); + const cssSize = parseInt( + style.getPropertyValue('--jp-code-font-size'), + 10 + ); + const currentSize = config.fontSize || cssSize; + config.fontSize = currentSize + delta; + return settingRegistry + .set(id, 'editorConfig', (config as unknown) as JSONObject) + .catch((reason: Error) => { + console.error(`Failed to set ${id}: ${reason.message}`); + }); + }, + label: args => args['name'] as string + }); + + commands.addCommand(CommandIDs.lineNumbers, { + execute: () => { + config.lineNumbers = !config.lineNumbers; + return settingRegistry + .set(id, 'editorConfig', (config as unknown) as JSONObject) + .catch((reason: Error) => { + console.error(`Failed to set ${id}: ${reason.message}`); + }); + }, + isEnabled, + isToggled: () => config.lineNumbers, + label: 'Line Numbers' + }); + + type wrappingMode = 'on' | 'off' | 'wordWrapColumn' | 'bounded'; + + commands.addCommand(CommandIDs.lineWrap, { + execute: args => { + config.lineWrap = (args['mode'] as wrappingMode) || 'off'; + return settingRegistry + .set(id, 'editorConfig', (config as unknown) as JSONObject) + .catch((reason: Error) => { + console.error(`Failed to set ${id}: ${reason.message}`); + }); + }, + isEnabled, + isToggled: args => { + const lineWrap = (args['mode'] as wrappingMode) || 'off'; + return config.lineWrap === lineWrap; + }, + label: 'Word Wrap' + }); + + commands.addCommand(CommandIDs.changeTabs, { + label: args => args['name'] as string, + execute: args => { + config.tabSize = (args['size'] as number) || 4; + config.insertSpaces = !!args['insertSpaces']; + return settingRegistry + .set(id, 'editorConfig', (config as unknown) as JSONObject) + .catch((reason: Error) => { + console.error(`Failed to set ${id}: ${reason.message}`); + }); + }, + isToggled: args => { + const insertSpaces = !!args['insertSpaces']; + const size = (args['size'] as number) || 4; + return config.insertSpaces === insertSpaces && config.tabSize === size; + } + }); + + commands.addCommand(CommandIDs.matchBrackets, { + execute: () => { + config.matchBrackets = !config.matchBrackets; + return settingRegistry + .set(id, 'editorConfig', (config as unknown) as JSONObject) + .catch((reason: Error) => { + console.error(`Failed to set ${id}: ${reason.message}`); + }); + }, + label: 'Match Brackets', + isEnabled, + isToggled: () => config.matchBrackets + }); + + commands.addCommand(CommandIDs.autoClosingBrackets, { + execute: () => { + config.autoClosingBrackets = !config.autoClosingBrackets; + return settingRegistry + .set(id, 'editorConfig', (config as unknown) as JSONObject) + .catch((reason: Error) => { + console.error(`Failed to set ${id}: ${reason.message}`); + }); + }, + label: 'Auto Close Brackets for Text Editor', + isToggled: () => config.autoClosingBrackets + }); + + commands.addCommand(CommandIDs.createConsole, { + execute: args => { + const widget = tracker.currentWidget; + + if (!widget) { + return; + } + + return Commands.getCreateConsoleFunction(commands)(widget, args); + }, + isEnabled, + label: 'Create Console for Editor' + }); + + commands.addCommand(CommandIDs.runCode, { + execute: () => { + // Run the appropriate code, taking into account a ```fenced``` code block. + const widget = tracker.currentWidget.content; + + if (!widget) { + return; + } + + let code = ''; + const editor = widget.editor; + const path = widget.context.path; + const extension = PathExt.extname(path); + const selection = editor.getSelection(); + const { start, end } = selection; + let selected = start.column !== end.column || start.line !== end.line; + + if (selected) { + // Get the selected code from the editor. + const start = editor.getOffsetAt(selection.start); + const end = editor.getOffsetAt(selection.end); + + code = editor.model.value.text.substring(start, end); + } else if (MarkdownCodeBlocks.isMarkdown(extension)) { + const { text } = editor.model.value; + const blocks = MarkdownCodeBlocks.findMarkdownCodeBlocks(text); + + for (let block of blocks) { + if (block.startLine <= start.line && start.line <= block.endLine) { + code = block.code; + selected = true; + break; + } + } + } + + if (!selected) { + // no selection, submit whole line and advance + code = editor.getLine(selection.start.line); + const cursor = editor.getCursorPosition(); + if (cursor.line + 1 === editor.lineCount) { + let text = editor.model.value.text; + editor.model.value.text = text + '\n'; + } + editor.setCursorPosition({ + line: cursor.line + 1, + column: cursor.column + }); + } + + const activate = false; + if (code) { + return commands.execute('console:inject', { activate, code, path }); + } else { + return Promise.resolve(void 0); + } + }, + isEnabled, + label: 'Run Code' + }); + + commands.addCommand(CommandIDs.runAllCode, { + execute: () => { + let widget = tracker.currentWidget.content; + + if (!widget) { + return; + } + + let code = ''; + let editor = widget.editor; + let text = editor.model.value.text; + let path = widget.context.path; + let extension = PathExt.extname(path); + + if (MarkdownCodeBlocks.isMarkdown(extension)) { + // For Markdown files, run only code blocks. + const blocks = MarkdownCodeBlocks.findMarkdownCodeBlocks(text); + for (let block of blocks) { + code += block.code; + } + } else { + code = text; + } + + const activate = false; + if (code) { + return commands.execute('console:inject', { activate, code, path }); + } else { + return Promise.resolve(void 0); + } + }, + isEnabled, + label: 'Run All Code' + }); + + commands.addCommand(CommandIDs.markdownPreview, { + execute: () => { + let widget = tracker.currentWidget; + if (!widget) { + return; + } + let path = widget.context.path; + return commands.execute('markdownviewer:open', { + path, + options: { + mode: 'split-right' + } + }); + }, + isVisible: () => { + let widget = tracker.currentWidget; + return ( + (widget && PathExt.extname(widget.context.path) === '.md') || false + ); + }, + label: 'Show Markdown Preview' + }); + + // Function to create a new untitled text file, given + // the current working directory. + const createNew = (cwd: string, ext: string = 'txt') => { + return commands + .execute('docmanager:new-untitled', { + path: cwd, + type: 'file', + ext + }) + .then(model => { + return commands.execute('docmanager:open', { + path: model.path, + factory: factory.name + }); + }); + }; + + // Add a command for creating a new text file. + commands.addCommand(CommandIDs.createNew, { + label: args => (args['isPalette'] ? 'New Text File' : 'Text File'), + caption: 'Create a new text file', + iconClass: args => (args['isPalette'] ? '' : EDITOR_ICON_CLASS), + execute: args => { + let cwd = args['cwd'] || browserFactory.defaultBrowser.model.path; + return createNew(cwd as string); + } + }); + + // Add a command for creating a new Markdown file. + commands.addCommand(CommandIDs.createNewMarkdown, { + label: args => + args['isPalette'] ? 'New Markdown File' : 'Markdown File', + caption: 'Create a new markdown file', + iconClass: args => (args['isPalette'] ? '' : MARKDOWN_ICON_CLASS), + execute: args => { + let cwd = args['cwd'] || browserFactory.defaultBrowser.model.path; + return createNew(cwd as string, 'md'); + } + }); + } + static addLauncherItems(launcher: ILauncher) { launcher.add({ command: CommandIDs.createNew, diff --git a/packages/fileeditor-extension/src/index.ts b/packages/fileeditor-extension/src/index.ts index ac3ac868dbd3..791144cacce1 100644 --- a/packages/fileeditor-extension/src/index.ts +++ b/packages/fileeditor-extension/src/index.ts @@ -13,11 +13,7 @@ import { CodeEditor, IEditorServices } from '@jupyterlab/codeeditor'; import { IConsoleTracker } from '@jupyterlab/console'; -import { - ISettingRegistry, - MarkdownCodeBlocks, - PathExt -} from '@jupyterlab/coreutils'; +import { ISettingRegistry } from '@jupyterlab/coreutils'; import { IDocumentWidget } from '@jupyterlab/docregistry'; @@ -36,21 +32,11 @@ import { IMainMenu } from '@jupyterlab/mainmenu'; import { IStatusBar } from '@jupyterlab/statusbar'; -import { JSONObject, ReadonlyJSONObject } from '@phosphor/coreutils'; +import { JSONObject } from '@phosphor/coreutils'; import { Menu } from '@phosphor/widgets'; -import Commands from './commands'; - -/** - * The class name for the text editor icon from the default theme. - */ -const EDITOR_ICON_CLASS = 'jp-MaterialIcon jp-TextEditorIcon'; - -/** - * The class name for the text editor icon from the default theme. - */ -const MARKDOWN_ICON_CLASS = 'jp-MaterialIcon jp-MarkdownIcon'; +import Commands, { EDITOR_ICON_CLASS } from './commands'; /** * The name of the factory that creates editor widgets. @@ -283,302 +269,16 @@ function activate( updateWidget(widget.content); }); - // Add a command to change font size. - commands.addCommand(CommandIDs.changeFontSize, { - execute: args => { - const delta = Number(args['delta']); - if (Number.isNaN(delta)) { - console.error( - `${CommandIDs.changeFontSize}: delta arg must be a number` - ); - return; - } - const style = window.getComputedStyle(document.documentElement); - const cssSize = parseInt( - style.getPropertyValue('--jp-code-font-size'), - 10 - ); - const currentSize = config.fontSize || cssSize; - config.fontSize = currentSize + delta; - return settingRegistry - .set(id, 'editorConfig', (config as unknown) as JSONObject) - .catch((reason: Error) => { - console.error(`Failed to set ${id}: ${reason.message}`); - }); - }, - label: args => args['name'] as string - }); - - commands.addCommand(CommandIDs.lineNumbers, { - execute: () => { - config.lineNumbers = !config.lineNumbers; - return settingRegistry - .set(id, 'editorConfig', (config as unknown) as JSONObject) - .catch((reason: Error) => { - console.error(`Failed to set ${id}: ${reason.message}`); - }); - }, - isEnabled, - isToggled: () => config.lineNumbers, - label: 'Line Numbers' - }); - - type wrappingMode = 'on' | 'off' | 'wordWrapColumn' | 'bounded'; - - commands.addCommand(CommandIDs.lineWrap, { - execute: args => { - const lineWrap = (args['mode'] as wrappingMode) || 'off'; - config.lineWrap = lineWrap; - return settingRegistry - .set(id, 'editorConfig', (config as unknown) as JSONObject) - .catch((reason: Error) => { - console.error(`Failed to set ${id}: ${reason.message}`); - }); - }, - isEnabled, - isToggled: args => { - const lineWrap = (args['mode'] as wrappingMode) || 'off'; - return config.lineWrap === lineWrap; - }, - label: 'Word Wrap' - }); - - commands.addCommand(CommandIDs.changeTabs, { - label: args => args['name'] as string, - execute: args => { - config.tabSize = (args['size'] as number) || 4; - config.insertSpaces = !!args['insertSpaces']; - return settingRegistry - .set(id, 'editorConfig', (config as unknown) as JSONObject) - .catch((reason: Error) => { - console.error(`Failed to set ${id}: ${reason.message}`); - }); - }, - isToggled: args => { - const insertSpaces = !!args['insertSpaces']; - const size = (args['size'] as number) || 4; - return config.insertSpaces === insertSpaces && config.tabSize === size; - } - }); - - commands.addCommand(CommandIDs.matchBrackets, { - execute: () => { - config.matchBrackets = !config.matchBrackets; - return settingRegistry - .set(id, 'editorConfig', (config as unknown) as JSONObject) - .catch((reason: Error) => { - console.error(`Failed to set ${id}: ${reason.message}`); - }); - }, - label: 'Match Brackets', - isEnabled, - isToggled: () => config.matchBrackets - }); - - commands.addCommand(CommandIDs.autoClosingBrackets, { - execute: () => { - config.autoClosingBrackets = !config.autoClosingBrackets; - return settingRegistry - .set(id, 'editorConfig', (config as unknown) as JSONObject) - .catch((reason: Error) => { - console.error(`Failed to set ${id}: ${reason.message}`); - }); - }, - label: 'Auto Close Brackets for Text Editor', - isToggled: () => config.autoClosingBrackets - }); - - async function createConsole( - widget: IDocumentWidget, - args?: ReadonlyJSONObject - ): Promise { - const options = args || {}; - const console = await commands.execute('console:create', { - activate: options['activate'], - name: widget.context.contentsModel.name, - path: widget.context.path, - preferredLanguage: widget.context.model.defaultKernelLanguage, - ref: widget.id, - insertMode: 'split-bottom' - }); - - widget.context.pathChanged.connect((sender, value) => { - console.session.setPath(value); - console.session.setName(widget.context.contentsModel.name); - }); - } - - commands.addCommand(CommandIDs.createConsole, { - execute: args => { - const widget = tracker.currentWidget; - - if (!widget) { - return; - } - - return createConsole(widget, args); - }, - isEnabled, - label: 'Create Console for Editor' - }); - - commands.addCommand(CommandIDs.runCode, { - execute: () => { - // Run the appropriate code, taking into account a ```fenced``` code block. - const widget = tracker.currentWidget.content; - - if (!widget) { - return; - } - - let code = ''; - const editor = widget.editor; - const path = widget.context.path; - const extension = PathExt.extname(path); - const selection = editor.getSelection(); - const { start, end } = selection; - let selected = start.column !== end.column || start.line !== end.line; - - if (selected) { - // Get the selected code from the editor. - const start = editor.getOffsetAt(selection.start); - const end = editor.getOffsetAt(selection.end); - - code = editor.model.value.text.substring(start, end); - } else if (MarkdownCodeBlocks.isMarkdown(extension)) { - const { text } = editor.model.value; - const blocks = MarkdownCodeBlocks.findMarkdownCodeBlocks(text); - - for (let block of blocks) { - if (block.startLine <= start.line && start.line <= block.endLine) { - code = block.code; - selected = true; - break; - } - } - } - - if (!selected) { - // no selection, submit whole line and advance - code = editor.getLine(selection.start.line); - const cursor = editor.getCursorPosition(); - if (cursor.line + 1 === editor.lineCount) { - let text = editor.model.value.text; - editor.model.value.text = text + '\n'; - } - editor.setCursorPosition({ - line: cursor.line + 1, - column: cursor.column - }); - } - - const activate = false; - if (code) { - return commands.execute('console:inject', { activate, code, path }); - } else { - return Promise.resolve(void 0); - } - }, - isEnabled, - label: 'Run Code' - }); - - commands.addCommand(CommandIDs.runAllCode, { - execute: () => { - let widget = tracker.currentWidget.content; - - if (!widget) { - return; - } - - let code = ''; - let editor = widget.editor; - let text = editor.model.value.text; - let path = widget.context.path; - let extension = PathExt.extname(path); - - if (MarkdownCodeBlocks.isMarkdown(extension)) { - // For Markdown files, run only code blocks. - const blocks = MarkdownCodeBlocks.findMarkdownCodeBlocks(text); - for (let block of blocks) { - code += block.code; - } - } else { - code = text; - } - - const activate = false; - if (code) { - return commands.execute('console:inject', { activate, code, path }); - } else { - return Promise.resolve(void 0); - } - }, + Commands.addCommands( + commands, + config, + settingRegistry, + id, isEnabled, - label: 'Run All Code' - }); - - commands.addCommand(CommandIDs.markdownPreview, { - execute: () => { - let widget = tracker.currentWidget; - if (!widget) { - return; - } - let path = widget.context.path; - return commands.execute('markdownviewer:open', { - path, - options: { - mode: 'split-right' - } - }); - }, - isVisible: () => { - let widget = tracker.currentWidget; - return ( - (widget && PathExt.extname(widget.context.path) === '.md') || false - ); - }, - label: 'Show Markdown Preview' - }); - - // Function to create a new untitled text file, given - // the current working directory. - const createNew = (cwd: string, ext: string = 'txt') => { - return commands - .execute('docmanager:new-untitled', { - path: cwd, - type: 'file', - ext - }) - .then(model => { - return commands.execute('docmanager:open', { - path: model.path, - factory: FACTORY - }); - }); - }; - - // Add a command for creating a new text file. - commands.addCommand(CommandIDs.createNew, { - label: args => (args['isPalette'] ? 'New Text File' : 'Text File'), - caption: 'Create a new text file', - iconClass: args => (args['isPalette'] ? '' : EDITOR_ICON_CLASS), - execute: args => { - let cwd = args['cwd'] || browserFactory.defaultBrowser.model.path; - return createNew(cwd as string); - } - }); - - // Add a command for creating a new Markdown file. - commands.addCommand(CommandIDs.createNewMarkdown, { - label: args => (args['isPalette'] ? 'New Markdown File' : 'Markdown File'), - caption: 'Create a new markdown file', - iconClass: args => (args['isPalette'] ? '' : MARKDOWN_ICON_CLASS), - execute: args => { - let cwd = args['cwd'] || browserFactory.defaultBrowser.model.path; - return createNew(cwd as string, 'md'); - } - }); + tracker, + browserFactory, + factory + ); // Add a launcher item if the launcher is available. if (launcher) { @@ -595,7 +295,7 @@ function activate( commands, tracker, consoleTracker, - createConsole + Commands.getCreateConsoleFunction(commands) ); } From d26408c79d44f2b6ecd47e5f33a2be14490722af Mon Sep 17 00:00:00 2001 From: Alex Bozarth Date: Wed, 7 Aug 2019 14:01:34 -0700 Subject: [PATCH 06/12] Further modularize addContextMenuItems --- packages/fileeditor-extension/src/commands.ts | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/packages/fileeditor-extension/src/commands.ts b/packages/fileeditor-extension/src/commands.ts index e6b73fce8e38..c5f5dfb7820f 100644 --- a/packages/fileeditor-extension/src/commands.ts +++ b/packages/fileeditor-extension/src/commands.ts @@ -540,11 +540,20 @@ export default class Commands { } as IRunMenu.ICodeRunner>); } + // Functions for adding items to the context menu static addContextMenuItems(app: JupyterFrontEnd) { + this.addCreateConsoleToContextMenu(app); + this.addMarkdownPreviewToContextMenu(app); + } + + static addCreateConsoleToContextMenu(app: JupyterFrontEnd) { app.contextMenu.addItem({ command: CommandIDs.createConsole, selector: '.jp-FileEditor' }); + } + + static addMarkdownPreviewToContextMenu(app: JupyterFrontEnd) { app.contextMenu.addItem({ command: CommandIDs.markdownPreview, selector: '.jp-FileEditor' From ade9ba90e4b037cdec817f8469c2a9f65580b4ab Mon Sep 17 00:00:00 2001 From: Alex Bozarth Date: Wed, 7 Aug 2019 14:54:51 -0700 Subject: [PATCH 07/12] modularized addMenuItems --- packages/fileeditor-extension/src/commands.ts | 64 ++++++++++++++++--- packages/fileeditor-extension/src/index.ts | 8 +-- 2 files changed, 57 insertions(+), 15 deletions(-) diff --git a/packages/fileeditor-extension/src/commands.ts b/packages/fileeditor-extension/src/commands.ts index c5f5dfb7820f..57709618dce5 100644 --- a/packages/fileeditor-extension/src/commands.ts +++ b/packages/fileeditor-extension/src/commands.ts @@ -426,10 +426,34 @@ export default class Commands { menu: IMainMenu, commands: CommandRegistry, tracker: WidgetTracker>, - consoleTracker: IConsoleTracker, - createConsole: (widget: IDocumentWidget) => Promise + consoleTracker: IConsoleTracker ) { // Add the editing commands to the settings menu. + this.addEditingCommandsToSettingsMenu(menu, commands); + + // Add new text file creation to the file menu. + this.addCreateNewFileToFileMenu(menu); + + // Add new markdown file creation to the file menu. + this.addCreateNewMarkdownFileToFileMenu(menu); + + // Add undo/redo hooks to the edit menu. + this.addUndoRedoToEditMenu(menu, tracker); + + // Add editor view options. + this.addEditorViewerToViewMenu(menu, tracker); + + // Add a console creator the the Kernel menu. + this.addConsoleCreatorToKernelMenu(menu, commands, tracker); + + // Add a code runner to the Run menu. + this.addCodeRunnersToRunMenu(menu, commands, tracker, consoleTracker); + } + + static addEditingCommandsToSettingsMenu( + menu: IMainMenu, + commands: CommandRegistry + ) { const tabMenu = new Menu({ commands }); tabMenu.title.label = 'Text Editor Indentation'; let args: JSONObject = { @@ -464,17 +488,23 @@ export default class Commands { ], 30 ); + } - // Add new text file creation to the file menu. + static addCreateNewFileToFileMenu(menu: IMainMenu) { menu.fileMenu.newMenu.addGroup([{ command: CommandIDs.createNew }], 30); + } - // Add new markdown file creation to the file menu. + static addCreateNewMarkdownFileToFileMenu(menu: IMainMenu) { menu.fileMenu.newMenu.addGroup( [{ command: CommandIDs.createNewMarkdown }], 30 ); + } - // Add undo/redo hooks to the edit menu. + static addUndoRedoToEditMenu( + menu: IMainMenu, + tracker: WidgetTracker> + ) { menu.editMenu.undoers.add({ tracker, undo: widget => { @@ -484,8 +514,12 @@ export default class Commands { widget.content.editor.redo(); } } as IEditMenu.IUndoer>); + } - // Add editor view options. + static addEditorViewerToViewMenu( + menu: IMainMenu, + tracker: WidgetTracker> + ) { menu.viewMenu.editorViewers.add({ tracker, toggleLineNumbers: widget => { @@ -508,15 +542,29 @@ export default class Commands { matchBracketsToggled: widget => widget.content.editor.getOption('matchBrackets') } as IViewMenu.IEditorViewer>); + } - // Add a console creator the the Kernel menu. + static addConsoleCreatorToKernelMenu( + menu: IMainMenu, + commands: CommandRegistry, + tracker: WidgetTracker> + ) { + let createConsole: ( + widget: IDocumentWidget + ) => Promise = this.getCreateConsoleFunction(commands); menu.fileMenu.consoleCreators.add({ tracker, name: 'Editor', createConsole } as IFileMenu.IConsoleCreator>); + } - // Add a code runner to the Run menu. + static addCodeRunnersToRunMenu( + menu: IMainMenu, + commands: CommandRegistry, + tracker: WidgetTracker>, + consoleTracker: IConsoleTracker + ) { menu.runMenu.codeRunners.add({ tracker, noun: 'Code', diff --git a/packages/fileeditor-extension/src/index.ts b/packages/fileeditor-extension/src/index.ts index 791144cacce1..13257fc545ac 100644 --- a/packages/fileeditor-extension/src/index.ts +++ b/packages/fileeditor-extension/src/index.ts @@ -290,13 +290,7 @@ function activate( } if (menu) { - Commands.addMenuItems( - menu, - commands, - tracker, - consoleTracker, - Commands.getCreateConsoleFunction(commands) - ); + Commands.addMenuItems(menu, commands, tracker, consoleTracker); } Commands.addContextMenuItems(app); From 620ebe0eb8600fbb58697f7676d1e6a36a482303 Mon Sep 17 00:00:00 2001 From: Alex Bozarth Date: Tue, 20 Aug 2019 14:24:23 -0700 Subject: [PATCH 08/12] Move CommandIDs and modulerize addPaletteItems and addLauncherItems --- packages/fileeditor-extension/src/commands.ts | 86 +++++++++++++++---- packages/fileeditor-extension/src/index.ts | 29 ------- 2 files changed, 71 insertions(+), 44 deletions(-) diff --git a/packages/fileeditor-extension/src/commands.ts b/packages/fileeditor-extension/src/commands.ts index 204597e9da5d..40db1fab5ea5 100644 --- a/packages/fileeditor-extension/src/commands.ts +++ b/packages/fileeditor-extension/src/commands.ts @@ -41,7 +41,34 @@ import { JSONObject, ReadonlyJSONObject } from '@phosphor/coreutils'; import { Menu } from '@phosphor/widgets'; -import { CommandIDs } from './index'; +/** + * The command IDs used by the fileeditor plugin. + */ +export namespace CommandIDs { + export const createNew = 'fileeditor:create-new'; + + export const createNewMarkdown = 'fileeditor:create-new-markdown-file'; + + export const changeFontSize = 'fileeditor:change-font-size'; + + export const lineNumbers = 'fileeditor:toggle-line-numbers'; + + export const lineWrap = 'fileeditor:toggle-line-wrap'; + + export const changeTabs = 'fileeditor:change-tabs'; + + export const matchBrackets = 'fileeditor:toggle-match-brackets'; + + export const autoClosingBrackets = 'fileeditor:toggle-autoclosing-brackets'; + + export const createConsole = 'fileeditor:create-console'; + + export const runCode = 'fileeditor:run-code'; + + export const runAllCode = 'fileeditor:run-all'; + + export const markdownPreview = 'fileeditor:markdown-preview'; +} /** * The class name for the text editor icon from the default theme. @@ -373,12 +400,20 @@ export default class Commands { } static addLauncherItems(launcher: ILauncher) { + this.addCreateNewToLauncher(launcher); + + this.addCreateNewMarkdownToLauncher(launcher); + } + + static addCreateNewToLauncher(launcher: ILauncher) { launcher.add({ command: CommandIDs.createNew, category: 'Other', rank: 1 }); + } + static addCreateNewMarkdownToLauncher(launcher: ILauncher) { launcher.add({ command: CommandIDs.createNewMarkdown, category: 'Other', @@ -387,14 +422,25 @@ export default class Commands { } static addPaletteItems(palette: ICommandPalette) { - const category = 'Text Editor'; + this.addChangeTabsCommandsToPalette(palette); + + this.addCreateNewCommandToPalette(palette); + + this.addCreateNewMarkdownCommandToPalette(palette); + + this.addChangeFontSizeCommandsToPalette(palette); + } + + private static paletteCategory = 'Text Editor'; + + static addChangeTabsCommandsToPalette(palette: ICommandPalette) { let args: JSONObject = { insertSpaces: false, size: 4, name: 'Indent with Tab' }; let command = 'fileeditor:change-tabs'; - palette.addItem({ command, args, category }); + palette.addItem({ command, args, category: this.paletteCategory }); for (let size of [1, 2, 4, 8]) { let args: JSONObject = { @@ -402,24 +448,34 @@ export default class Commands { size, name: `Spaces: ${size} ` }; - palette.addItem({ command, args, category }); + palette.addItem({ command, args, category: this.paletteCategory }); } + } + + static addCreateNewCommandToPalette(palette: ICommandPalette) { + palette.addItem({ + command: CommandIDs.createNew, + args: { isPalette: true }, + category: this.paletteCategory + }); + } - args = { isPalette: true }; - command = CommandIDs.createNew; - palette.addItem({ command, args, category }); + static addCreateNewMarkdownCommandToPalette(palette: ICommandPalette) { + palette.addItem({ + command: CommandIDs.createNewMarkdown, + args: { isPalette: true }, + category: this.paletteCategory + }); + } - args = { isPalette: true }; - command = CommandIDs.createNewMarkdown; - palette.addItem({ command, args, category }); + static addChangeFontSizeCommandsToPalette(palette: ICommandPalette) { + let command = CommandIDs.changeFontSize; - args = { name: 'Increase Font Size', delta: 1 }; - command = CommandIDs.changeFontSize; - palette.addItem({ command, args, category }); + let args = { name: 'Increase Font Size', delta: 1 }; + palette.addItem({ command, args, category: this.paletteCategory }); args = { name: 'Decrease Font Size', delta: -1 }; - command = CommandIDs.changeFontSize; - palette.addItem({ command, args, category }); + palette.addItem({ command, args, category: this.paletteCategory }); } static addMenuItems( diff --git a/packages/fileeditor-extension/src/index.ts b/packages/fileeditor-extension/src/index.ts index 5c51ceec457c..915356a0cd11 100644 --- a/packages/fileeditor-extension/src/index.ts +++ b/packages/fileeditor-extension/src/index.ts @@ -43,35 +43,6 @@ import Commands, { EDITOR_ICON_CLASS } from './commands'; */ const FACTORY = 'Editor'; -/** - * The command IDs used by the fileeditor plugin. - */ -export namespace CommandIDs { - export const createNew = 'fileeditor:create-new'; - - export const createNewMarkdown = 'fileeditor:create-new-markdown-file'; - - export const changeFontSize = 'fileeditor:change-font-size'; - - export const lineNumbers = 'fileeditor:toggle-line-numbers'; - - export const lineWrap = 'fileeditor:toggle-line-wrap'; - - export const changeTabs = 'fileeditor:change-tabs'; - - export const matchBrackets = 'fileeditor:toggle-match-brackets'; - - export const autoClosingBrackets = 'fileeditor:toggle-autoclosing-brackets'; - - export const createConsole = 'fileeditor:create-console'; - - export const runCode = 'fileeditor:run-code'; - - export const runAllCode = 'fileeditor:run-all'; - - export const markdownPreview = 'fileeditor:markdown-preview'; -} - /** * The editor tracker extension. */ From c507a1adce088cc7233191cd70ed2b2a4c987c60 Mon Sep 17 00:00:00 2001 From: Alex Bozarth Date: Tue, 20 Aug 2019 15:57:44 -0700 Subject: [PATCH 09/12] Add function comments for documentation --- packages/fileeditor-extension/src/commands.ts | 82 +++++++++++++++++-- 1 file changed, 76 insertions(+), 6 deletions(-) diff --git a/packages/fileeditor-extension/src/commands.ts b/packages/fileeditor-extension/src/commands.ts index 40db1fab5ea5..a32e3af0d511 100644 --- a/packages/fileeditor-extension/src/commands.ts +++ b/packages/fileeditor-extension/src/commands.ts @@ -80,8 +80,15 @@ export const EDITOR_ICON_CLASS = 'jp-MaterialIcon jp-TextEditorIcon'; */ export const MARKDOWN_ICON_CLASS = 'jp-MarkdownIcon'; +/** + * A utility class for adding commands and menu items, + * for use by the File Editor extension or other Editor extensions. + */ export default class Commands { - static getCreateConsoleFunction( + /** + * Accessor function that returns the createConsole function for use by various commands + */ + private static getCreateConsoleFunction( commands: CommandRegistry ): ( widget: IDocumentWidget, @@ -108,6 +115,9 @@ export default class Commands { }; } + /** + * Wrapper function for adding the default File Editor commands + */ static addCommands( commands: CommandRegistry, config: CodeEditor.IConfig, @@ -399,12 +409,18 @@ export default class Commands { }); } + /** + * Wrapper function for adding the default launcher items for File Editor + */ static addLauncherItems(launcher: ILauncher) { this.addCreateNewToLauncher(launcher); this.addCreateNewMarkdownToLauncher(launcher); } + /** + * Add Create New Text File to the Launcher + */ static addCreateNewToLauncher(launcher: ILauncher) { launcher.add({ command: CommandIDs.createNew, @@ -413,6 +429,9 @@ export default class Commands { }); } + /** + * Add Create New Markdown to the Launcher + */ static addCreateNewMarkdownToLauncher(launcher: ILauncher) { launcher.add({ command: CommandIDs.createNewMarkdown, @@ -421,6 +440,9 @@ export default class Commands { }); } + /** + * Wrapper function for adding the default items to the File Editor palette + */ static addPaletteItems(palette: ICommandPalette) { this.addChangeTabsCommandsToPalette(palette); @@ -431,8 +453,14 @@ export default class Commands { this.addChangeFontSizeCommandsToPalette(palette); } + /** + * The category for File Editor palette commands for use in addToPalette functions + */ private static paletteCategory = 'Text Editor'; + /** + * Add commands to change the tab indentation to the File Editor palette + */ static addChangeTabsCommandsToPalette(palette: ICommandPalette) { let args: JSONObject = { insertSpaces: false, @@ -452,6 +480,9 @@ export default class Commands { } } + /** + * Add a Create New File command to the File Editor palette + */ static addCreateNewCommandToPalette(palette: ICommandPalette) { palette.addItem({ command: CommandIDs.createNew, @@ -460,6 +491,9 @@ export default class Commands { }); } + /** + * Add a Create New Markdown command to the File Editor palette + */ static addCreateNewMarkdownCommandToPalette(palette: ICommandPalette) { palette.addItem({ command: CommandIDs.createNewMarkdown, @@ -468,6 +502,9 @@ export default class Commands { }); } + /** + * Add commands to change the font size to the File Editor palette + */ static addChangeFontSizeCommandsToPalette(palette: ICommandPalette) { let command = CommandIDs.changeFontSize; @@ -478,6 +515,9 @@ export default class Commands { palette.addItem({ command, args, category: this.paletteCategory }); } + /** + * Wrapper function for adding the default menu items for File Editor + */ static addMenuItems( menu: IMainMenu, commands: CommandRegistry, @@ -499,13 +539,17 @@ export default class Commands { // Add editor view options. this.addEditorViewerToViewMenu(menu, tracker); - // Add a console creator the the Kernel menu. - this.addConsoleCreatorToKernelMenu(menu, commands, tracker); + // Add a console creator the the file menu. + this.addConsoleCreatorToFileMenu(menu, commands, tracker); - // Add a code runner to the Run menu. + // Add a code runner to the run menu. this.addCodeRunnersToRunMenu(menu, commands, tracker, consoleTracker); } + /** + * Add File Editor editing commands to the Settings menu, including: + * Indent with Tab, Tab Spaces, Change Font Size, and auto closing brackets + */ static addEditingCommandsToSettingsMenu( menu: IMainMenu, commands: CommandRegistry @@ -546,10 +590,16 @@ export default class Commands { ); } + /** + * Add a Create New File command to the File menu + */ static addCreateNewFileToFileMenu(menu: IMainMenu) { menu.fileMenu.newMenu.addGroup([{ command: CommandIDs.createNew }], 30); } + /** + * Add a Create New Markdown File command to the File menu + */ static addCreateNewMarkdownFileToFileMenu(menu: IMainMenu) { menu.fileMenu.newMenu.addGroup( [{ command: CommandIDs.createNewMarkdown }], @@ -557,6 +607,9 @@ export default class Commands { ); } + /** + * Add File Editor undo and redo widgets to the Edit menu + */ static addUndoRedoToEditMenu( menu: IMainMenu, tracker: WidgetTracker> @@ -572,6 +625,9 @@ export default class Commands { } as IEditMenu.IUndoer>); } + /** + * Add a File Editor editor viewer to the View Menu + */ static addEditorViewerToViewMenu( menu: IMainMenu, tracker: WidgetTracker> @@ -600,7 +656,10 @@ export default class Commands { } as IViewMenu.IEditorViewer>); } - static addConsoleCreatorToKernelMenu( + /** + * Add a File Editor console creator to the File menu + */ + static addConsoleCreatorToFileMenu( menu: IMainMenu, commands: CommandRegistry, tracker: WidgetTracker> @@ -615,6 +674,9 @@ export default class Commands { } as IFileMenu.IConsoleCreator>); } + /** + * Add a File Editor code runner to the Run menu + */ static addCodeRunnersToRunMenu( menu: IMainMenu, commands: CommandRegistry, @@ -644,12 +706,17 @@ export default class Commands { } as IRunMenu.ICodeRunner>); } - // Functions for adding items to the context menu + /** + * Wrapper function for adding the default items to the File Editor context menu + */ static addContextMenuItems(app: JupyterFrontEnd) { this.addCreateConsoleToContextMenu(app); this.addMarkdownPreviewToContextMenu(app); } + /** + * Add a Create Console item to the File Editor context menu + */ static addCreateConsoleToContextMenu(app: JupyterFrontEnd) { app.contextMenu.addItem({ command: CommandIDs.createConsole, @@ -657,6 +724,9 @@ export default class Commands { }); } + /** + * Add a Markdown Preview item to the File Editor context menu + */ static addMarkdownPreviewToContextMenu(app: JupyterFrontEnd) { app.contextMenu.addItem({ command: CommandIDs.markdownPreview, From fbf1c26239b5aa601f56ae769902a717d76a37ad Mon Sep 17 00:00:00 2001 From: Alex Bozarth Date: Tue, 20 Aug 2019 17:06:35 -0700 Subject: [PATCH 10/12] modulerized addCommands function --- packages/fileeditor-extension/src/commands.ts | 212 +++++++++++++++--- packages/fileeditor-extension/src/index.ts | 10 +- 2 files changed, 184 insertions(+), 38 deletions(-) diff --git a/packages/fileeditor-extension/src/commands.ts b/packages/fileeditor-extension/src/commands.ts index a32e3af0d511..f92ad31af589 100644 --- a/packages/fileeditor-extension/src/commands.ts +++ b/packages/fileeditor-extension/src/commands.ts @@ -15,11 +15,7 @@ import { PathExt } from '@jupyterlab/coreutils'; -import { - ABCWidgetFactory, - DocumentRegistry, - IDocumentWidget -} from '@jupyterlab/docregistry'; +import { IDocumentWidget } from '@jupyterlab/docregistry'; import { IFileBrowserFactory } from '@jupyterlab/filebrowser'; @@ -80,13 +76,18 @@ export const EDITOR_ICON_CLASS = 'jp-MaterialIcon jp-TextEditorIcon'; */ export const MARKDOWN_ICON_CLASS = 'jp-MarkdownIcon'; +/** + * The name of the factory that creates editor widgets. + */ +export const FACTORY = 'Editor'; + /** * A utility class for adding commands and menu items, * for use by the File Editor extension or other Editor extensions. */ export default class Commands { /** - * Accessor function that returns the createConsole function for use by various commands + * Accessor function that returns the createConsole function for use by Create Console commands */ private static getCreateConsoleFunction( commands: CommandRegistry @@ -125,13 +126,57 @@ export default class Commands { id: string, isEnabled: () => boolean, tracker: WidgetTracker>, - browserFactory: IFileBrowserFactory, - factory: ABCWidgetFactory< - IDocumentWidget, - DocumentRegistry.ICodeModel - > + browserFactory: IFileBrowserFactory ) { // Add a command to change font size. + this.addChangeFontSizeCommand(commands, config, settingRegistry, id); + + this.addLineNumbersCommand( + commands, + config, + settingRegistry, + id, + isEnabled + ); + + this.addWordWrapCommand(commands, config, settingRegistry, id, isEnabled); + + this.addChangeTabsCommand(commands, config, settingRegistry, id); + + this.addMatchBracketsCommand( + commands, + config, + settingRegistry, + id, + isEnabled + ); + + this.addAutoClosingBracketsCommand(commands, config, settingRegistry, id); + + this.addCreateConsoleCommand(commands, tracker, isEnabled); + + this.addRunCodeCommand(commands, tracker, isEnabled); + + this.addRunAllCodeCommand(commands, tracker, isEnabled); + + this.addMarkdownPreviewCommand(commands, tracker); + + // Add a command for creating a new text file. + this.addCreateNewCommand(commands, browserFactory); + + // Add a command for creating a new Markdown file. + this.addCreateNewMarkdownCommand(commands, browserFactory); + } + + /** + * Add a command to change font size for File Editor + */ + static addChangeFontSizeCommand( + commands: CommandRegistry, + config: CodeEditor.IConfig, + settingRegistry: ISettingRegistry, + id: string + ) { commands.addCommand(CommandIDs.changeFontSize, { execute: args => { const delta = Number(args['delta']); @@ -156,7 +201,18 @@ export default class Commands { }, label: args => args['name'] as string }); + } + /** + * Add the Line Numbers command + */ + static addLineNumbersCommand( + commands: CommandRegistry, + config: CodeEditor.IConfig, + settingRegistry: ISettingRegistry, + id: string, + isEnabled: () => boolean + ) { commands.addCommand(CommandIDs.lineNumbers, { execute: () => { config.lineNumbers = !config.lineNumbers; @@ -170,7 +226,18 @@ export default class Commands { isToggled: () => config.lineNumbers, label: 'Line Numbers' }); + } + /** + * Add the Word Wrap command + */ + static addWordWrapCommand( + commands: CommandRegistry, + config: CodeEditor.IConfig, + settingRegistry: ISettingRegistry, + id: string, + isEnabled: () => boolean + ) { type wrappingMode = 'on' | 'off' | 'wordWrapColumn' | 'bounded'; commands.addCommand(CommandIDs.lineWrap, { @@ -189,7 +256,17 @@ export default class Commands { }, label: 'Word Wrap' }); + } + /** + * Add command for changing tabs size or type in File Editor + */ + static addChangeTabsCommand( + commands: CommandRegistry, + config: CodeEditor.IConfig, + settingRegistry: ISettingRegistry, + id: string + ) { commands.addCommand(CommandIDs.changeTabs, { label: args => args['name'] as string, execute: args => { @@ -207,7 +284,18 @@ export default class Commands { return config.insertSpaces === insertSpaces && config.tabSize === size; } }); + } + /** + * Add the Match Brackets command + */ + static addMatchBracketsCommand( + commands: CommandRegistry, + config: CodeEditor.IConfig, + settingRegistry: ISettingRegistry, + id: string, + isEnabled: () => boolean + ) { commands.addCommand(CommandIDs.matchBrackets, { execute: () => { config.matchBrackets = !config.matchBrackets; @@ -221,7 +309,17 @@ export default class Commands { isEnabled, isToggled: () => config.matchBrackets }); + } + /** + * Add the Auto Close Brackets for Text Editor command + */ + static addAutoClosingBracketsCommand( + commands: CommandRegistry, + config: CodeEditor.IConfig, + settingRegistry: ISettingRegistry, + id: string + ) { commands.addCommand(CommandIDs.autoClosingBrackets, { execute: () => { config.autoClosingBrackets = !config.autoClosingBrackets; @@ -234,7 +332,16 @@ export default class Commands { label: 'Auto Close Brackets for Text Editor', isToggled: () => config.autoClosingBrackets }); + } + /** + * Add the Create Console for Editor command + */ + static addCreateConsoleCommand( + commands: CommandRegistry, + tracker: WidgetTracker>, + isEnabled: () => boolean + ) { commands.addCommand(CommandIDs.createConsole, { execute: args => { const widget = tracker.currentWidget; @@ -248,7 +355,16 @@ export default class Commands { isEnabled, label: 'Create Console for Editor' }); + } + /** + * Add the Run Code command + */ + static addRunCodeCommand( + commands: CommandRegistry, + tracker: WidgetTracker>, + isEnabled: () => boolean + ) { commands.addCommand(CommandIDs.runCode, { execute: () => { // Run the appropriate code, taking into account a ```fenced``` code block. @@ -309,7 +425,16 @@ export default class Commands { isEnabled, label: 'Run Code' }); + } + /** + * Add the Run All Code command + */ + static addRunAllCodeCommand( + commands: CommandRegistry, + tracker: WidgetTracker>, + isEnabled: () => boolean + ) { commands.addCommand(CommandIDs.runAllCode, { execute: () => { let widget = tracker.currentWidget.content; @@ -344,7 +469,15 @@ export default class Commands { isEnabled, label: 'Run All Code' }); + } + /** + * Add the command + */ + static addMarkdownPreviewCommand( + commands: CommandRegistry, + tracker: WidgetTracker> + ) { commands.addCommand(CommandIDs.markdownPreview, { execute: () => { let widget = tracker.currentWidget; @@ -367,36 +500,55 @@ export default class Commands { }, label: 'Show Markdown Preview' }); + } - // Function to create a new untitled text file, given - // the current working directory. - const createNew = (cwd: string, ext: string = 'txt') => { - return commands - .execute('docmanager:new-untitled', { - path: cwd, - type: 'file', - ext - }) - .then(model => { - return commands.execute('docmanager:open', { - path: model.path, - factory: factory.name - }); + /** + * Function to create a new untitled text file, given the current working directory. + */ + private static createNew = ( + commands: CommandRegistry, + cwd: string, + ext: string = 'txt' + ) => { + return commands + .execute('docmanager:new-untitled', { + path: cwd, + type: 'file', + ext + }) + .then(model => { + return commands.execute('docmanager:open', { + path: model.path, + factory: FACTORY }); - }; + }); + }; - // Add a command for creating a new text file. + /** + * Add the New File command + */ + static addCreateNewCommand( + commands: CommandRegistry, + browserFactory: IFileBrowserFactory + ) { commands.addCommand(CommandIDs.createNew, { label: args => (args['isPalette'] ? 'New Text File' : 'Text File'), caption: 'Create a new text file', iconClass: args => (args['isPalette'] ? '' : EDITOR_ICON_CLASS), execute: args => { let cwd = args['cwd'] || browserFactory.defaultBrowser.model.path; - return createNew(cwd as string); + return this.createNew(commands, cwd as string); } }); + } - // Add a command for creating a new Markdown file. + /** + * Add the New Markdown File command + */ + static addCreateNewMarkdownCommand( + commands: CommandRegistry, + browserFactory: IFileBrowserFactory + ) { commands.addCommand(CommandIDs.createNewMarkdown, { label: args => args['isPalette'] ? 'New Markdown File' : 'Markdown File', @@ -404,7 +556,7 @@ export default class Commands { iconClass: args => (args['isPalette'] ? '' : MARKDOWN_ICON_CLASS), execute: args => { let cwd = args['cwd'] || browserFactory.defaultBrowser.model.path; - return createNew(cwd as string, 'md'); + return this.createNew(commands, cwd as string, 'md'); } }); } diff --git a/packages/fileeditor-extension/src/index.ts b/packages/fileeditor-extension/src/index.ts index 915356a0cd11..bae5b3cc8e0f 100644 --- a/packages/fileeditor-extension/src/index.ts +++ b/packages/fileeditor-extension/src/index.ts @@ -36,12 +36,7 @@ import { JSONObject } from '@phosphor/coreutils'; import { Menu } from '@phosphor/widgets'; -import Commands, { EDITOR_ICON_CLASS } from './commands'; - -/** - * The name of the factory that creates editor widgets. - */ -const FACTORY = 'Editor'; +import Commands, { EDITOR_ICON_CLASS, FACTORY } from './commands'; /** * The editor tracker extension. @@ -252,8 +247,7 @@ function activate( id, isEnabled, tracker, - browserFactory, - factory + browserFactory ); // Add a launcher item if the launcher is available. From ab1f12dbd18766dbd4ad088dc3437f53999729e7 Mon Sep 17 00:00:00 2001 From: Alex Bozarth Date: Tue, 10 Sep 2019 14:39:21 -0700 Subject: [PATCH 11/12] Changed Commands from a class to a namespace Also added a missing export for Commands to be used externally --- packages/fileeditor-extension/src/commands.ts | 166 ++++++++---------- packages/fileeditor-extension/src/index.ts | 4 +- 2 files changed, 81 insertions(+), 89 deletions(-) diff --git a/packages/fileeditor-extension/src/commands.ts b/packages/fileeditor-extension/src/commands.ts index f92ad31af589..a9f369b8e649 100644 --- a/packages/fileeditor-extension/src/commands.ts +++ b/packages/fileeditor-extension/src/commands.ts @@ -85,11 +85,11 @@ export const FACTORY = 'Editor'; * A utility class for adding commands and menu items, * for use by the File Editor extension or other Editor extensions. */ -export default class Commands { +export namespace Commands { /** * Accessor function that returns the createConsole function for use by Create Console commands */ - private static getCreateConsoleFunction( + function getCreateConsoleFunction( commands: CommandRegistry ): ( widget: IDocumentWidget, @@ -119,7 +119,7 @@ export default class Commands { /** * Wrapper function for adding the default File Editor commands */ - static addCommands( + export function addCommands( commands: CommandRegistry, config: CodeEditor.IConfig, settingRegistry: ISettingRegistry, @@ -129,49 +129,37 @@ export default class Commands { browserFactory: IFileBrowserFactory ) { // Add a command to change font size. - this.addChangeFontSizeCommand(commands, config, settingRegistry, id); - - this.addLineNumbersCommand( - commands, - config, - settingRegistry, - id, - isEnabled - ); + addChangeFontSizeCommand(commands, config, settingRegistry, id); - this.addWordWrapCommand(commands, config, settingRegistry, id, isEnabled); + addLineNumbersCommand(commands, config, settingRegistry, id, isEnabled); - this.addChangeTabsCommand(commands, config, settingRegistry, id); + addWordWrapCommand(commands, config, settingRegistry, id, isEnabled); - this.addMatchBracketsCommand( - commands, - config, - settingRegistry, - id, - isEnabled - ); + addChangeTabsCommand(commands, config, settingRegistry, id); + + addMatchBracketsCommand(commands, config, settingRegistry, id, isEnabled); - this.addAutoClosingBracketsCommand(commands, config, settingRegistry, id); + addAutoClosingBracketsCommand(commands, config, settingRegistry, id); - this.addCreateConsoleCommand(commands, tracker, isEnabled); + addCreateConsoleCommand(commands, tracker, isEnabled); - this.addRunCodeCommand(commands, tracker, isEnabled); + addRunCodeCommand(commands, tracker, isEnabled); - this.addRunAllCodeCommand(commands, tracker, isEnabled); + addRunAllCodeCommand(commands, tracker, isEnabled); - this.addMarkdownPreviewCommand(commands, tracker); + addMarkdownPreviewCommand(commands, tracker); // Add a command for creating a new text file. - this.addCreateNewCommand(commands, browserFactory); + addCreateNewCommand(commands, browserFactory); // Add a command for creating a new Markdown file. - this.addCreateNewMarkdownCommand(commands, browserFactory); + addCreateNewMarkdownCommand(commands, browserFactory); } /** * Add a command to change font size for File Editor */ - static addChangeFontSizeCommand( + export function addChangeFontSizeCommand( commands: CommandRegistry, config: CodeEditor.IConfig, settingRegistry: ISettingRegistry, @@ -206,7 +194,7 @@ export default class Commands { /** * Add the Line Numbers command */ - static addLineNumbersCommand( + export function addLineNumbersCommand( commands: CommandRegistry, config: CodeEditor.IConfig, settingRegistry: ISettingRegistry, @@ -231,7 +219,7 @@ export default class Commands { /** * Add the Word Wrap command */ - static addWordWrapCommand( + export function addWordWrapCommand( commands: CommandRegistry, config: CodeEditor.IConfig, settingRegistry: ISettingRegistry, @@ -261,7 +249,7 @@ export default class Commands { /** * Add command for changing tabs size or type in File Editor */ - static addChangeTabsCommand( + export function addChangeTabsCommand( commands: CommandRegistry, config: CodeEditor.IConfig, settingRegistry: ISettingRegistry, @@ -289,7 +277,7 @@ export default class Commands { /** * Add the Match Brackets command */ - static addMatchBracketsCommand( + export function addMatchBracketsCommand( commands: CommandRegistry, config: CodeEditor.IConfig, settingRegistry: ISettingRegistry, @@ -314,7 +302,7 @@ export default class Commands { /** * Add the Auto Close Brackets for Text Editor command */ - static addAutoClosingBracketsCommand( + export function addAutoClosingBracketsCommand( commands: CommandRegistry, config: CodeEditor.IConfig, settingRegistry: ISettingRegistry, @@ -337,7 +325,7 @@ export default class Commands { /** * Add the Create Console for Editor command */ - static addCreateConsoleCommand( + export function addCreateConsoleCommand( commands: CommandRegistry, tracker: WidgetTracker>, isEnabled: () => boolean @@ -350,7 +338,7 @@ export default class Commands { return; } - return Commands.getCreateConsoleFunction(commands)(widget, args); + return getCreateConsoleFunction(commands)(widget, args); }, isEnabled, label: 'Create Console for Editor' @@ -360,7 +348,7 @@ export default class Commands { /** * Add the Run Code command */ - static addRunCodeCommand( + export function addRunCodeCommand( commands: CommandRegistry, tracker: WidgetTracker>, isEnabled: () => boolean @@ -430,7 +418,7 @@ export default class Commands { /** * Add the Run All Code command */ - static addRunAllCodeCommand( + export function addRunAllCodeCommand( commands: CommandRegistry, tracker: WidgetTracker>, isEnabled: () => boolean @@ -474,7 +462,7 @@ export default class Commands { /** * Add the command */ - static addMarkdownPreviewCommand( + export function addMarkdownPreviewCommand( commands: CommandRegistry, tracker: WidgetTracker> ) { @@ -505,11 +493,11 @@ export default class Commands { /** * Function to create a new untitled text file, given the current working directory. */ - private static createNew = ( + function createNew( commands: CommandRegistry, cwd: string, ext: string = 'txt' - ) => { + ) { return commands .execute('docmanager:new-untitled', { path: cwd, @@ -522,12 +510,12 @@ export default class Commands { factory: FACTORY }); }); - }; + } /** * Add the New File command */ - static addCreateNewCommand( + export function addCreateNewCommand( commands: CommandRegistry, browserFactory: IFileBrowserFactory ) { @@ -537,7 +525,7 @@ export default class Commands { iconClass: args => (args['isPalette'] ? '' : EDITOR_ICON_CLASS), execute: args => { let cwd = args['cwd'] || browserFactory.defaultBrowser.model.path; - return this.createNew(commands, cwd as string); + return createNew(commands, cwd as string); } }); } @@ -545,7 +533,7 @@ export default class Commands { /** * Add the New Markdown File command */ - static addCreateNewMarkdownCommand( + export function addCreateNewMarkdownCommand( commands: CommandRegistry, browserFactory: IFileBrowserFactory ) { @@ -556,7 +544,7 @@ export default class Commands { iconClass: args => (args['isPalette'] ? '' : MARKDOWN_ICON_CLASS), execute: args => { let cwd = args['cwd'] || browserFactory.defaultBrowser.model.path; - return this.createNew(commands, cwd as string, 'md'); + return createNew(commands, cwd as string, 'md'); } }); } @@ -564,16 +552,16 @@ export default class Commands { /** * Wrapper function for adding the default launcher items for File Editor */ - static addLauncherItems(launcher: ILauncher) { - this.addCreateNewToLauncher(launcher); + export function addLauncherItems(launcher: ILauncher) { + addCreateNewToLauncher(launcher); - this.addCreateNewMarkdownToLauncher(launcher); + addCreateNewMarkdownToLauncher(launcher); } /** * Add Create New Text File to the Launcher */ - static addCreateNewToLauncher(launcher: ILauncher) { + export function addCreateNewToLauncher(launcher: ILauncher) { launcher.add({ command: CommandIDs.createNew, category: 'Other', @@ -584,7 +572,7 @@ export default class Commands { /** * Add Create New Markdown to the Launcher */ - static addCreateNewMarkdownToLauncher(launcher: ILauncher) { + export function addCreateNewMarkdownToLauncher(launcher: ILauncher) { launcher.add({ command: CommandIDs.createNewMarkdown, category: 'Other', @@ -595,32 +583,32 @@ export default class Commands { /** * Wrapper function for adding the default items to the File Editor palette */ - static addPaletteItems(palette: ICommandPalette) { - this.addChangeTabsCommandsToPalette(palette); + export function addPaletteItems(palette: ICommandPalette) { + addChangeTabsCommandsToPalette(palette); - this.addCreateNewCommandToPalette(palette); + addCreateNewCommandToPalette(palette); - this.addCreateNewMarkdownCommandToPalette(palette); + addCreateNewMarkdownCommandToPalette(palette); - this.addChangeFontSizeCommandsToPalette(palette); + addChangeFontSizeCommandsToPalette(palette); } /** * The category for File Editor palette commands for use in addToPalette functions */ - private static paletteCategory = 'Text Editor'; + const paletteCategory = 'Text Editor'; /** * Add commands to change the tab indentation to the File Editor palette */ - static addChangeTabsCommandsToPalette(palette: ICommandPalette) { + export function addChangeTabsCommandsToPalette(palette: ICommandPalette) { let args: JSONObject = { insertSpaces: false, size: 4, name: 'Indent with Tab' }; let command = 'fileeditor:change-tabs'; - palette.addItem({ command, args, category: this.paletteCategory }); + palette.addItem({ command, args, category: paletteCategory }); for (let size of [1, 2, 4, 8]) { let args: JSONObject = { @@ -628,81 +616,83 @@ export default class Commands { size, name: `Spaces: ${size} ` }; - palette.addItem({ command, args, category: this.paletteCategory }); + palette.addItem({ command, args, category: paletteCategory }); } } /** * Add a Create New File command to the File Editor palette */ - static addCreateNewCommandToPalette(palette: ICommandPalette) { + export function addCreateNewCommandToPalette(palette: ICommandPalette) { palette.addItem({ command: CommandIDs.createNew, args: { isPalette: true }, - category: this.paletteCategory + category: paletteCategory }); } /** * Add a Create New Markdown command to the File Editor palette */ - static addCreateNewMarkdownCommandToPalette(palette: ICommandPalette) { + export function addCreateNewMarkdownCommandToPalette( + palette: ICommandPalette + ) { palette.addItem({ command: CommandIDs.createNewMarkdown, args: { isPalette: true }, - category: this.paletteCategory + category: paletteCategory }); } /** * Add commands to change the font size to the File Editor palette */ - static addChangeFontSizeCommandsToPalette(palette: ICommandPalette) { + export function addChangeFontSizeCommandsToPalette(palette: ICommandPalette) { let command = CommandIDs.changeFontSize; let args = { name: 'Increase Font Size', delta: 1 }; - palette.addItem({ command, args, category: this.paletteCategory }); + palette.addItem({ command, args, category: paletteCategory }); args = { name: 'Decrease Font Size', delta: -1 }; - palette.addItem({ command, args, category: this.paletteCategory }); + palette.addItem({ command, args, category: paletteCategory }); } /** * Wrapper function for adding the default menu items for File Editor */ - static addMenuItems( + export function addMenuItems( menu: IMainMenu, commands: CommandRegistry, tracker: WidgetTracker>, consoleTracker: IConsoleTracker ) { // Add the editing commands to the settings menu. - this.addEditingCommandsToSettingsMenu(menu, commands); + addEditingCommandsToSettingsMenu(menu, commands); // Add new text file creation to the file menu. - this.addCreateNewFileToFileMenu(menu); + addCreateNewFileToFileMenu(menu); // Add new markdown file creation to the file menu. - this.addCreateNewMarkdownFileToFileMenu(menu); + addCreateNewMarkdownFileToFileMenu(menu); // Add undo/redo hooks to the edit menu. - this.addUndoRedoToEditMenu(menu, tracker); + addUndoRedoToEditMenu(menu, tracker); // Add editor view options. - this.addEditorViewerToViewMenu(menu, tracker); + addEditorViewerToViewMenu(menu, tracker); // Add a console creator the the file menu. - this.addConsoleCreatorToFileMenu(menu, commands, tracker); + addConsoleCreatorToFileMenu(menu, commands, tracker); // Add a code runner to the run menu. - this.addCodeRunnersToRunMenu(menu, commands, tracker, consoleTracker); + addCodeRunnersToRunMenu(menu, commands, tracker, consoleTracker); } /** * Add File Editor editing commands to the Settings menu, including: * Indent with Tab, Tab Spaces, Change Font Size, and auto closing brackets */ - static addEditingCommandsToSettingsMenu( + export function addEditingCommandsToSettingsMenu( menu: IMainMenu, commands: CommandRegistry ) { @@ -745,14 +735,14 @@ export default class Commands { /** * Add a Create New File command to the File menu */ - static addCreateNewFileToFileMenu(menu: IMainMenu) { + export function addCreateNewFileToFileMenu(menu: IMainMenu) { menu.fileMenu.newMenu.addGroup([{ command: CommandIDs.createNew }], 30); } /** * Add a Create New Markdown File command to the File menu */ - static addCreateNewMarkdownFileToFileMenu(menu: IMainMenu) { + export function addCreateNewMarkdownFileToFileMenu(menu: IMainMenu) { menu.fileMenu.newMenu.addGroup( [{ command: CommandIDs.createNewMarkdown }], 30 @@ -762,7 +752,7 @@ export default class Commands { /** * Add File Editor undo and redo widgets to the Edit menu */ - static addUndoRedoToEditMenu( + export function addUndoRedoToEditMenu( menu: IMainMenu, tracker: WidgetTracker> ) { @@ -780,7 +770,7 @@ export default class Commands { /** * Add a File Editor editor viewer to the View Menu */ - static addEditorViewerToViewMenu( + export function addEditorViewerToViewMenu( menu: IMainMenu, tracker: WidgetTracker> ) { @@ -811,14 +801,14 @@ export default class Commands { /** * Add a File Editor console creator to the File menu */ - static addConsoleCreatorToFileMenu( + export function addConsoleCreatorToFileMenu( menu: IMainMenu, commands: CommandRegistry, tracker: WidgetTracker> ) { let createConsole: ( widget: IDocumentWidget - ) => Promise = this.getCreateConsoleFunction(commands); + ) => Promise = getCreateConsoleFunction(commands); menu.fileMenu.consoleCreators.add({ tracker, name: 'Editor', @@ -829,7 +819,7 @@ export default class Commands { /** * Add a File Editor code runner to the Run menu */ - static addCodeRunnersToRunMenu( + export function addCodeRunnersToRunMenu( menu: IMainMenu, commands: CommandRegistry, tracker: WidgetTracker>, @@ -861,15 +851,15 @@ export default class Commands { /** * Wrapper function for adding the default items to the File Editor context menu */ - static addContextMenuItems(app: JupyterFrontEnd) { - this.addCreateConsoleToContextMenu(app); - this.addMarkdownPreviewToContextMenu(app); + export function addContextMenuItems(app: JupyterFrontEnd) { + addCreateConsoleToContextMenu(app); + addMarkdownPreviewToContextMenu(app); } /** * Add a Create Console item to the File Editor context menu */ - static addCreateConsoleToContextMenu(app: JupyterFrontEnd) { + export function addCreateConsoleToContextMenu(app: JupyterFrontEnd) { app.contextMenu.addItem({ command: CommandIDs.createConsole, selector: '.jp-FileEditor' @@ -879,7 +869,7 @@ export default class Commands { /** * Add a Markdown Preview item to the File Editor context menu */ - static addMarkdownPreviewToContextMenu(app: JupyterFrontEnd) { + export function addMarkdownPreviewToContextMenu(app: JupyterFrontEnd) { app.contextMenu.addItem({ command: CommandIDs.markdownPreview, selector: '.jp-FileEditor' diff --git a/packages/fileeditor-extension/src/index.ts b/packages/fileeditor-extension/src/index.ts index bae5b3cc8e0f..31722dd5df8e 100644 --- a/packages/fileeditor-extension/src/index.ts +++ b/packages/fileeditor-extension/src/index.ts @@ -36,7 +36,9 @@ import { JSONObject } from '@phosphor/coreutils'; import { Menu } from '@phosphor/widgets'; -import Commands, { EDITOR_ICON_CLASS, FACTORY } from './commands'; +import { Commands, EDITOR_ICON_CLASS, FACTORY } from './commands'; + +export { Commands } from './commands'; /** * The editor tracker extension. From 1c162ad39d0af82186612d9abdb2111bedc01342 Mon Sep 17 00:00:00 2001 From: Jason Grout Date: Thu, 12 Sep 2019 01:21:04 -0700 Subject: [PATCH 12/12] integrity updates --- packages/fileeditor-extension/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/fileeditor-extension/package.json b/packages/fileeditor-extension/package.json index 1b2518b5e914..a480abb04dc7 100644 --- a/packages/fileeditor-extension/package.json +++ b/packages/fileeditor-extension/package.json @@ -47,7 +47,7 @@ "@jupyterlab/launcher": "^1.2.0-alpha.0", "@jupyterlab/mainmenu": "^1.2.0-alpha.0", "@jupyterlab/statusbar": "^1.2.0-alpha.0", - "@phosphor/commands": "^1.6.3", + "@phosphor/commands": "^1.7.0", "@phosphor/coreutils": "^1.3.1", "@phosphor/widgets": "^1.9.0" },