generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.ts
174 lines (150 loc) · 4.94 KB
/
main.ts
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import { App, Editor, MarkdownView, Modal, Notice, Plugin, PluginSettingTab, RequestUrlResponse, Setting, requestUrl } from 'obsidian';
interface PluginSettings {
immichUrl: string;
immichApiKey: string;
immichAlbum: string;
immichAlbumKey: string;
}
const DEFAULT_SETTINGS: PluginSettings = {
immichUrl: '',
immichApiKey: '',
immichAlbum: '',
immichAlbumKey: ''
}
let cachedResult: RequestUrlResponse;
async function refreshCacheFromImmich(settings: PluginSettings) {
const url = new URL(settings.immichUrl + '/api/albums/' + settings.immichAlbum);
const result = await requestUrl({
url: url.toString(),
headers: {
'Accept': 'application/json',
'x-api-key': settings.immichApiKey.toString()
}
})
cachedResult = result;
}
export default class ObsidianImmich extends Plugin {
settings: PluginSettings;
async onload() {
await this.loadSettings();
this.addCommand({
id: 'insert-from-album',
name: 'Insert from album',
editorCallback: (editor: Editor) => {
new ImageSelectorModal(this.app, editor, this.settings).open();
}
});
this.addCommand({
id: 'force-refresh-album-cache',
name: 'Refresh album cache',
callback: () => {
refreshCacheFromImmich(this.settings);
}
});
// This adds a settings tab so the user can configure various aspects of the plugin
this.addSettingTab(new SettingTab(this.app, this));
}
onunload() {
}
async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}
async saveSettings() {
await this.saveData(this.settings);
}
}
class ImageSelectorModal extends Modal {
editor: Editor;
settings: PluginSettings;
page: number;
constructor(app: App, editor: Editor, settings: PluginSettings) {
super(app);
this.editor = editor;
this.settings = settings;
this.page = 0;
}
async onOpen() {
const {contentEl} = this;
if (cachedResult == null) {
await refreshCacheFromImmich(this.settings);
}
// Get the width of the viewport
const totalWidth = contentEl.innerWidth;
const imageDiv = contentEl.createDiv();
const bottomDiv = contentEl.createDiv();
let observer = new IntersectionObserver(() => {
const startIndex = this.page;
let endIndex = this.page + 16;
if (endIndex > cachedResult.json['assets'].length) {
endIndex = cachedResult.json['assets'].length;
}
this.page = endIndex;
for (let i = startIndex; i < endIndex; i++) {
const thumbUrl = this.settings.immichUrl + '/api/assets/' + cachedResult.json['assets'][i]['id'] + '/thumbnail?size=thumbnail&key=' + this.settings.immichAlbumKey;
let insertionText: string;
if (cachedResult.json['assets'][i]['type'] === "IMAGE") {
const previewUrl = this.settings.immichUrl + '/api/assets/' + cachedResult.json['assets'][i]['id'] + '/thumbnail?size=preview&key=' + this.settings.immichAlbumKey;
insertionText = '![](' + previewUrl + ')\n';
} else if (cachedResult.json['assets'][i]['type'] === "VIDEO") {
insertionText = '<video src="' + this.settings.immichUrl + '/api/assets/' + cachedResult.json['assets'][i]['id'] + '/video/playback?key=' + this.settings.immichAlbumKey + '"controls></video>\n';
}
const imgElement = imageDiv.createEl("img");
imgElement.src = thumbUrl;
imgElement.width = (totalWidth / 2) - 5;
imgElement.onclick = () => this.editor.replaceSelection(insertionText);
}
}, {threshold: [0.1]});
observer.observe(bottomDiv);
}
onClose() {
const {contentEl} = this;
contentEl.empty();
}
}
class SettingTab extends PluginSettingTab {
plugin: ObsidianImmich;
constructor(app: App, plugin: ObsidianImmich) {
super(app, plugin);
this.plugin = plugin;
}
display(): void {
const {containerEl} = this;
containerEl.empty();
new Setting(containerEl)
.setName('Immich URL')
.setDesc('Full URL to your immich instance.')
.addText(text => text
.setValue(this.plugin.settings.immichUrl)
.onChange(async (value) => {
this.plugin.settings.immichUrl = value;
await this.plugin.saveSettings();
}));
new Setting(containerEl)
.setName('Immich API key')
.setDesc('Obtained from {IMMICH_URL}/user-settings?isOpen=api-keys.')
.addText(text => text
.setValue(this.plugin.settings.immichApiKey)
.onChange(async (value) => {
this.plugin.settings.immichApiKey = value;
await this.plugin.saveSettings();
}));
new Setting(containerEl)
.setName('Immich album ID')
.setDesc('UUID for the `obsidian` album in immich.')
.addText(text => text
.setValue(this.plugin.settings.immichAlbum)
.onChange(async (value) => {
this.plugin.settings.immichAlbum = value;
await this.plugin.saveSettings();
}));
new Setting(containerEl)
.setName('Immich album share key')
.setDesc('Share key which shows up in the URL of your album.')
.addText(text => text
.setValue(this.plugin.settings.immichAlbumKey)
.onChange(async (value) => {
this.plugin.settings.immichAlbumKey = value;
await this.plugin.saveSettings();
}));
}
}