Skip to content

Commit

Permalink
Add feature of creating new markdown file from Launcher as well as Fi…
Browse files Browse the repository at this point in the history
…le > New (#5511)
  • Loading branch information
apsknight authored and blink1073 committed Oct 18, 2018
1 parent bf9d302 commit 5bcb89a
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions packages/fileeditor-extension/src/index.ts
Expand Up @@ -48,6 +48,11 @@ import { Menu } from '@phosphor/widgets';
*/
const EDITOR_ICON_CLASS = 'jp-TextEditorIcon';

/**
* The class name for the text editor icon from the default theme.
*/
const MARKDOWN_ICON_CLASS = 'jp-MarkdownIcon';

/**
* The name of the factory that creates editor widgets.
*/
Expand All @@ -59,6 +64,8 @@ const FACTORY = 'Editor';
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';
Expand Down Expand Up @@ -467,6 +474,43 @@ function activate(
});
}

// Function to create a new untitled markdown file, given
// the current working directory.
const createNewMarkdown = (cwd: string) => {
return commands
.execute('docmanager:new-untitled', {
path: cwd,
type: 'file',
ext: 'md'
})
.then(model => {
return commands.execute('docmanager:open', {
path: model.path,
factory: FACTORY
});
});
};

// Add a command for creating a new Markdown file.
commands.addCommand(CommandIDs.createNewMarkdown, {
label: 'Markdown File',
caption: 'Create a new markdown file',
iconClass: MARKDOWN_ICON_CLASS,
execute: args => {
let cwd = args['cwd'] || browserFactory.defaultBrowser.model.path;
return createNewMarkdown(cwd as string);
}
});

// Add a launcher item if the launcher is available.
if (launcher) {
launcher.add({
command: CommandIDs.createNewMarkdown,
category: 'Other',
rank: 2
});
}

if (palette) {
let args: JSONObject = {
insertSpaces: false,
Expand Down Expand Up @@ -534,6 +578,12 @@ function activate(
// 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,
Expand Down

0 comments on commit 5bcb89a

Please sign in to comment.