mirror of
https://github.com/brokensandals/obsidian-paste-quote-plugin.git
synced 2026-07-22 05:37:56 +00:00
paste quote body
This commit is contained in:
parent
4e399ad168
commit
248543acd8
1 changed files with 19 additions and 7 deletions
26
main.ts
26
main.ts
|
|
@ -1,4 +1,5 @@
|
|||
import { App, Editor, MarkdownView, Modal, Notice, Plugin, PluginSettingTab, Setting } from 'obsidian';
|
||||
import { App, Editor, MarkdownView, Plugin, PluginSettingTab } from 'obsidian';
|
||||
import { parseQuote, Quote, replaceDoubleQuotes } from 'src/quotes';
|
||||
|
||||
// Remember to rename these classes and interfaces!
|
||||
|
||||
|
|
@ -16,20 +17,31 @@ export default class PasteQuotePlugin extends Plugin {
|
|||
async onload() {
|
||||
await this.loadSettings();
|
||||
|
||||
// This adds an editor command that can perform some operation on the current editor instance
|
||||
this.addCommand({
|
||||
id: 'paste-quote',
|
||||
name: 'Paste quote',
|
||||
editorCallback: (editor: Editor, view: MarkdownView) => {
|
||||
console.log(editor.getSelection());
|
||||
editor.replaceSelection('Sample Editor Command');
|
||||
}
|
||||
editorCallback: this.pasteQuote.bind(this),
|
||||
});
|
||||
|
||||
// This adds a settings tab so the user can configure various aspects of the plugin
|
||||
this.addSettingTab(new PasteQuoteSettingTab(this.app, this));
|
||||
}
|
||||
|
||||
async pasteQuote(editor: Editor, view: MarkdownView) {
|
||||
const clipboardText = await navigator.clipboard.readText();
|
||||
const quote = parseQuote(clipboardText);
|
||||
|
||||
const cursorPosition = editor.getCursor();
|
||||
const formattedQuote = this.formatQuote(quote.body, cursorPosition);
|
||||
|
||||
editor.replaceRange(formattedQuote, cursorPosition);
|
||||
}
|
||||
|
||||
formatQuote(quote: string, cursorPosition: CodeMirror.Position) {
|
||||
return cursorPosition.ch === 0
|
||||
? `> ${quote}`
|
||||
: `“${replaceDoubleQuotes(quote)}”`;
|
||||
}
|
||||
|
||||
onunload() {
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue