Skip to content

Commit

Permalink
Moved back settingRegistry dependant commands
Browse files Browse the repository at this point in the history
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 jupyterlab#6904

This fixes jupyterlab#7295
  • Loading branch information
ajbozarth committed Oct 12, 2019
1 parent e04e3af commit 7348314
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 180 deletions.
179 changes: 0 additions & 179 deletions packages/fileeditor-extension/src/commands.ts
Expand Up @@ -128,19 +128,6 @@ export namespace Commands {
tracker: WidgetTracker<IDocumentWidget<FileEditor>>,
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);
Expand All @@ -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
*/
Expand Down
106 changes: 105 additions & 1 deletion packages/fileeditor-extension/src/index.ts
Expand Up @@ -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';

Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 7348314

Please sign in to comment.