Added "Paste code block"

This commit is contained in:
Patrik Lindefors 2022-05-30 10:17:32 +02:00
parent b9a5d8bcf9
commit 580d075bc8
1 changed files with 12 additions and 2 deletions

14
main.ts
View File

@ -21,13 +21,13 @@ export default class CodeBlockPlugin extends Plugin {
await this.loadSettings(); await this.loadSettings();
this.addCommand({ this.addCommand({
id: 'Code block', id: 'Add code block',
name: 'Add code block', name: 'Add code block',
editorCallback: (editor: Editor) => { editorCallback: (editor: Editor) => {
const selection = editor.getSelection(); const selection = editor.getSelection();
if (selection.length == 0) { if (selection.length == 0) {
const pos = editor.getCursor(); const pos = editor.getCursor();
editor.replaceRange( '```\n\n```\n', pos); editor.replaceRange('```\n\n```\n', pos);
editor.setCursor(pos.line + 1); editor.setCursor(pos.line + 1);
return; return;
} }
@ -35,6 +35,16 @@ export default class CodeBlockPlugin extends Plugin {
} }
}); });
this.addCommand({
id: 'Paste code block',
name: 'Paste code block',
editorCallback: (editor: Editor) => {
navigator.clipboard.readText().then(text => {
editor.replaceSelection(CodeBlockPlugin.addCodeBlock(this.getLanguage(text), text));
});
}
});
this.addSettingTab(new CodeBlockTab(this.app, this)); this.addSettingTab(new CodeBlockTab(this.app, this));
} }