Skip to content

Commit

Permalink
Merge pull request #7364 from meeseeksmachine/auto-backport-of-pr-735…
Browse files Browse the repository at this point in the history
…0-on-1.x

Backport PR #7350 on branch 1.x (Moved Text Editor config var into commands.ts)
  • Loading branch information
jasongrout committed Oct 15, 2019
2 parents 99f1d3e + c308fd1 commit 3b270a7
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 55 deletions.
58 changes: 45 additions & 13 deletions packages/fileeditor-extension/src/commands.ts
Expand Up @@ -81,6 +81,8 @@ export const MARKDOWN_ICON_CLASS = 'jp-MarkdownIcon';
*/
export const FACTORY = 'Editor';

let config: CodeEditor.IConfig = { ...CodeEditor.defaultConfig };

/**
* A utility class for adding commands and menu items,
* for use by the File Editor extension or other Editor extensions.
Expand Down Expand Up @@ -116,30 +118,66 @@ export namespace Commands {
};
}

/**
* Update the setting values.
*/
export function updateSettings(
settings: ISettingRegistry.ISettings,
commands: CommandRegistry
): void {
config = {
...CodeEditor.defaultConfig,
...(settings.get('editorConfig').composite as JSONObject)
};

// Trigger a refresh of the rendered commands
commands.notifyCommandChanged();
}

/**
* Update the settings of the current tracker instances.
*/
export function updateTracker(
tracker: WidgetTracker<IDocumentWidget<FileEditor>>
): void {
tracker.forEach(widget => {
updateWidget(widget.content);
});
}

/**
* Update the settings of a widget.
*/
export function updateWidget(widget: FileEditor): void {
const editor = widget.editor;
Object.keys(config).forEach((key: keyof CodeEditor.IConfig) => {
editor.setOption(key, config[key]);
});
}

/**
* Wrapper function for adding the default File Editor commands
*/
export function addCommands(
commands: CommandRegistry,
config: CodeEditor.IConfig,
settingRegistry: ISettingRegistry,
id: string,
isEnabled: () => boolean,
tracker: WidgetTracker<IDocumentWidget<FileEditor>>,
browserFactory: IFileBrowserFactory
) {
// Add a command to change font size.
addChangeFontSizeCommand(commands, config, settingRegistry, id);
addChangeFontSizeCommand(commands, settingRegistry, id);

addLineNumbersCommand(commands, config, settingRegistry, id, isEnabled);
addLineNumbersCommand(commands, settingRegistry, id, isEnabled);

addWordWrapCommand(commands, config, settingRegistry, id, isEnabled);
addWordWrapCommand(commands, settingRegistry, id, isEnabled);

addChangeTabsCommand(commands, config, settingRegistry, id);
addChangeTabsCommand(commands, settingRegistry, id);

addMatchBracketsCommand(commands, config, settingRegistry, id, isEnabled);
addMatchBracketsCommand(commands, settingRegistry, id, isEnabled);

addAutoClosingBracketsCommand(commands, config, settingRegistry, id);
addAutoClosingBracketsCommand(commands, settingRegistry, id);

addCreateConsoleCommand(commands, tracker, isEnabled);

Expand All @@ -161,7 +199,6 @@ export namespace Commands {
*/
export function addChangeFontSizeCommand(
commands: CommandRegistry,
config: CodeEditor.IConfig,
settingRegistry: ISettingRegistry,
id: string
) {
Expand Down Expand Up @@ -196,7 +233,6 @@ export namespace Commands {
*/
export function addLineNumbersCommand(
commands: CommandRegistry,
config: CodeEditor.IConfig,
settingRegistry: ISettingRegistry,
id: string,
isEnabled: () => boolean
Expand All @@ -221,7 +257,6 @@ export namespace Commands {
*/
export function addWordWrapCommand(
commands: CommandRegistry,
config: CodeEditor.IConfig,
settingRegistry: ISettingRegistry,
id: string,
isEnabled: () => boolean
Expand Down Expand Up @@ -251,7 +286,6 @@ export namespace Commands {
*/
export function addChangeTabsCommand(
commands: CommandRegistry,
config: CodeEditor.IConfig,
settingRegistry: ISettingRegistry,
id: string
) {
Expand Down Expand Up @@ -279,7 +313,6 @@ export namespace Commands {
*/
export function addMatchBracketsCommand(
commands: CommandRegistry,
config: CodeEditor.IConfig,
settingRegistry: ISettingRegistry,
id: string,
isEnabled: () => boolean
Expand All @@ -304,7 +337,6 @@ export namespace Commands {
*/
export function addAutoClosingBracketsCommand(
commands: CommandRegistry,
config: CodeEditor.IConfig,
settingRegistry: ISettingRegistry,
id: string
) {
Expand Down
49 changes: 7 additions & 42 deletions packages/fileeditor-extension/src/index.ts
Expand Up @@ -166,8 +166,6 @@ function activate(
tracker.currentWidget !== null &&
tracker.currentWidget === shell.currentWidget;

let config: CodeEditor.IConfig = { ...CodeEditor.defaultConfig };

// Handle state restoration.
if (restorer) {
void restorer.restore(tracker, {
Expand All @@ -177,52 +175,20 @@ function activate(
});
}

/**
* Update the setting values.
*/
function updateSettings(settings: ISettingRegistry.ISettings): void {
config = {
...CodeEditor.defaultConfig,
...(settings.get('editorConfig').composite as JSONObject)
};

// Trigger a refresh of the rendered commands
app.commands.notifyCommandChanged();
}

/**
* Update the settings of the current tracker instances.
*/
function updateTracker(): void {
tracker.forEach(widget => {
updateWidget(widget.content);
});
}

/**
* Update the settings of a widget.
*/
function updateWidget(widget: FileEditor): void {
const editor = widget.editor;
Object.keys(config).forEach((key: keyof CodeEditor.IConfig) => {
editor.setOption(key, config[key]);
});
}

// Add a console creator to the File menu
// Fetch the initial state of the settings.
Promise.all([settingRegistry.load(id), restored])
.then(([settings]) => {
updateSettings(settings);
updateTracker();
Commands.updateSettings(settings, commands);
Commands.updateTracker(tracker);
settings.changed.connect(() => {
updateSettings(settings);
updateTracker();
Commands.updateSettings(settings, commands);
Commands.updateTracker(tracker);
});
})
.catch((reason: Error) => {
console.error(reason.message);
updateTracker();
Commands.updateTracker(tracker);
});

factory.widgetCreated.connect((sender, widget) => {
Expand All @@ -233,18 +199,17 @@ function activate(
void tracker.save(widget);
});
void tracker.add(widget);
updateWidget(widget.content);
Commands.updateWidget(widget.content);
});
app.docRegistry.addWidgetFactory(factory);

// Handle the settings of new widgets.
tracker.widgetAdded.connect((sender, widget) => {
updateWidget(widget.content);
Commands.updateWidget(widget.content);
});

Commands.addCommands(
commands,
config,
settingRegistry,
id,
isEnabled,
Expand Down

0 comments on commit 3b270a7

Please sign in to comment.