From 7348314c7877b402900676e23bc5cb27b24b4d4a Mon Sep 17 00:00:00 2001 From: Alex Bozarth Date: Fri, 11 Oct 2019 17:18:25 -0700 Subject: [PATCH] Moved back settingRegistry dependant commands Due to a still unclear issue, when passing settingRegistry to functions it changes the beahvior of the commands that use it. This change moves the six commands that interact with settingRegistry back to index.ts from commands.ts where they were oved in #6904 This fixes #7295 --- packages/fileeditor-extension/src/commands.ts | 179 ------------------ packages/fileeditor-extension/src/index.ts | 106 ++++++++++- 2 files changed, 105 insertions(+), 180 deletions(-) diff --git a/packages/fileeditor-extension/src/commands.ts b/packages/fileeditor-extension/src/commands.ts index a9f369b8e649..59b516477596 100644 --- a/packages/fileeditor-extension/src/commands.ts +++ b/packages/fileeditor-extension/src/commands.ts @@ -128,19 +128,6 @@ export namespace Commands { tracker: WidgetTracker>, browserFactory: IFileBrowserFactory ) { - // Add a command to change font size. - addChangeFontSizeCommand(commands, config, settingRegistry, id); - - addLineNumbersCommand(commands, config, settingRegistry, id, isEnabled); - - addWordWrapCommand(commands, config, settingRegistry, id, isEnabled); - - addChangeTabsCommand(commands, config, settingRegistry, id); - - addMatchBracketsCommand(commands, config, settingRegistry, id, isEnabled); - - addAutoClosingBracketsCommand(commands, config, settingRegistry, id); - addCreateConsoleCommand(commands, tracker, isEnabled); addRunCodeCommand(commands, tracker, isEnabled); @@ -156,172 +143,6 @@ export namespace Commands { addCreateNewMarkdownCommand(commands, browserFactory); } - /** - * Add a command to change font size for File Editor - */ - export function addChangeFontSizeCommand( - commands: CommandRegistry, - config: CodeEditor.IConfig, - settingRegistry: ISettingRegistry, - id: string - ) { - 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 - }); - } - - /** - * Add the Line Numbers command - */ - export function addLineNumbersCommand( - commands: CommandRegistry, - config: CodeEditor.IConfig, - settingRegistry: ISettingRegistry, - id: string, - isEnabled: () => boolean - ) { - 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' - }); - } - - /** - * Add the Word Wrap command - */ - export function addWordWrapCommand( - commands: CommandRegistry, - config: CodeEditor.IConfig, - settingRegistry: ISettingRegistry, - id: string, - isEnabled: () => boolean - ) { - 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' - }); - } - - /** - * Add command for changing tabs size or type in File Editor - */ - export function addChangeTabsCommand( - commands: CommandRegistry, - config: CodeEditor.IConfig, - settingRegistry: ISettingRegistry, - id: string - ) { - 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; - } - }); - } - - /** - * Add the Match Brackets command - */ - export function addMatchBracketsCommand( - commands: CommandRegistry, - config: CodeEditor.IConfig, - settingRegistry: ISettingRegistry, - id: string, - isEnabled: () => boolean - ) { - 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 - }); - } - - /** - * Add the Auto Close Brackets for Text Editor command - */ - export function addAutoClosingBracketsCommand( - commands: CommandRegistry, - config: CodeEditor.IConfig, - settingRegistry: ISettingRegistry, - id: string - ) { - 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 - }); - } - /** * Add the Create Console for Editor command */ diff --git a/packages/fileeditor-extension/src/index.ts b/packages/fileeditor-extension/src/index.ts index 31722dd5df8e..fb9d796f75a7 100644 --- a/packages/fileeditor-extension/src/index.ts +++ b/packages/fileeditor-extension/src/index.ts @@ -36,7 +36,7 @@ import { JSONObject } from '@phosphor/coreutils'; import { Menu } from '@phosphor/widgets'; -import { Commands, EDITOR_ICON_CLASS, FACTORY } from './commands'; +import { CommandIDs, Commands, EDITOR_ICON_CLASS, FACTORY } from './commands'; export { Commands } from './commands'; @@ -242,6 +242,110 @@ 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 => { + 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.addCommands( commands, config,