-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.ts
More file actions
86 lines (71 loc) · 2.36 KB
/
main.ts
File metadata and controls
86 lines (71 loc) · 2.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import { Editor, MarkdownView, Notice, Plugin } from "obsidian";
import { readClipboardContent } from "./src/clipboard";
import { cleanCurrentNote, cleanEditorSelections } from "./src/editor-actions";
import { DEFAULT_SETTINGS, type CleanPasteSettings } from "./src/settings-data";
import {
CleanPasteSettingTab,
} from "./src/settings";
export default class CleanPastePlugin extends Plugin {
settings: CleanPasteSettings = DEFAULT_SETTINGS;
async onload(): Promise<void> {
await this.loadSettings();
this.addRibbonIcon("wand", "Clean current note", () => {
this.cleanActiveNote();
});
this.addCommand({
id: "clean-selected-text",
name: "Clean selected text",
editorCallback: (editor) => {
const didClean = cleanEditorSelections(editor, this.settings);
new Notice(didClean ? "Paste cleaned" : "Select text to clean");
},
});
this.addCommand({
id: "paste-clean-from-clipboard",
name: "Paste clean from clipboard",
editorCallback: (editor) => void this.pasteCleanFromClipboard(editor),
});
this.addCommand({
id: "clean-current-note",
name: "Clean current note",
editorCallback: (editor) => this.cleanNoteInEditor(editor),
});
this.addSettingTab(new CleanPasteSettingTab(this.app, this));
}
async saveSettings(): Promise<void> {
await this.saveData(this.settings);
}
private async loadSettings(): Promise<void> {
const saved = (await this.loadData()) as Partial<CleanPasteSettings> | null;
this.settings = { ...DEFAULT_SETTINGS, ...saved };
}
private async pasteCleanFromClipboard(editor: Editor): Promise<void> {
try {
const text = await readClipboardContent(this.settings);
if (!text) {
new Notice("Clipboard is empty");
return;
}
editor.replaceSelection(text);
new Notice("Paste cleaned");
} catch (error) {
console.error("Failed to read clipboard", error);
new Notice("Unable to read clipboard");
}
}
private cleanActiveNote(): void {
const editor = this.getActiveEditor();
if (!editor) {
new Notice("Open a note to clean");
return;
}
this.cleanNoteInEditor(editor);
}
private cleanNoteInEditor(editor: Editor): void {
const didClean = cleanCurrentNote(editor, this.settings);
new Notice(didClean ? "Note cleaned" : "Nothing to clean");
}
private getActiveEditor(): Editor | null {
return this.app.workspace.getActiveViewOfType(MarkdownView)?.editor ?? null;
}
}