forked from
treethought.xyz/obsidian-atmosphere
Various AT Protocol integrations with obsidian
1import { Plugin, WorkspaceLeaf } from "obsidian";
2import { DEFAULT_SETTINGS, AtProtoSettings, SettingTab } from "./settings";
3import { AtmosphereView, VIEW_TYPE_ATMOSPHERE_BOOKMARKS } from "./views/bookmarks";
4import { publishFileAsDocument } from "./commands/publishDocument";
5import { StandardFeedView, VIEW_ATMOSPHERE_STANDARD_FEED } from "views/standardfeed";
6import { ATClient } from "lib/client";
7import { Clipper } from "lib/clipper";
8
9export default class AtmospherePlugin extends Plugin {
10 settings: AtProtoSettings = DEFAULT_SETTINGS;
11 client: ATClient;
12 clipper: Clipper;
13
14 async onload() {
15 await this.loadSettings();
16
17 const creds = {
18 identifier: this.settings.identifier,
19 password: this.settings.appPassword,
20 };
21 this.client = new ATClient(creds);
22 this.clipper = new Clipper(this);
23
24 this.registerView(VIEW_TYPE_ATMOSPHERE_BOOKMARKS, (leaf) => {
25 return new AtmosphereView(leaf, this);
26 });
27
28 this.registerView(VIEW_ATMOSPHERE_STANDARD_FEED, (leaf) => {
29 return new StandardFeedView(leaf, this);
30 });
31
32 this.addRibbonIcon("layers", "Atmosphere bookmarks", () => {
33 void this.activateView(VIEW_TYPE_ATMOSPHERE_BOOKMARKS);
34 });
35
36 this.addRibbonIcon("rss", "Atmosphere feed", () => {
37 void this.activateView(VIEW_ATMOSPHERE_STANDARD_FEED);
38 });
39
40 this.addCommand({
41 id: "open-bookmarks",
42 name: "Open bookmarks",
43 callback: () => { void this.activateView(VIEW_TYPE_ATMOSPHERE_BOOKMARKS); },
44 });
45
46 this.addCommand({
47 id: "open-feed",
48 name: "Open feed",
49 callback: () => { void this.activateView(VIEW_ATMOSPHERE_STANDARD_FEED); },
50 });
51
52 this.addCommand({
53 id: "publish-note",
54 name: "Publish note",
55 editorCheckCallback: (checking: boolean,) => {
56 const file = this.app.workspace.getActiveFile();
57
58 if (file) {
59 if (!checking) {
60 void publishFileAsDocument(this)
61 }
62
63 return true
64 }
65
66 return false;
67 },
68 });
69
70 this.addSettingTab(new SettingTab(this.app, this));
71 }
72
73
74
75 async activateView(v: string) {
76 const { workspace } = this.app;
77
78 let leaf: WorkspaceLeaf | null = null;
79 const leaves = workspace.getLeavesOfType(v);
80
81 if (leaves.length > 0) {
82 // A leaf with our view already exists, use that
83 leaf = leaves[0] as WorkspaceLeaf;
84 void workspace.revealLeaf(leaf);
85 return;
86 }
87
88 // Our view could not be found in the workspace, create a new leaf
89 leaf = workspace.getMostRecentLeaf()
90 await leaf?.setViewState({ type: v, active: true });
91
92 // "Reveal" the leaf in case it is in a collapsed sidebar
93 if (leaf) {
94 void workspace.revealLeaf(leaf);
95 }
96 }
97
98 async loadSettings() {
99 this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData() as Partial<AtProtoSettings>);
100 }
101
102 async saveSettings() {
103 await this.saveData(this.settings);
104 }
105
106 onunload() { }
107}