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