forked from juliang22/ObsidianTimestampNotes
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.ts
More file actions
379 lines (329 loc) · 12.5 KB
/
Copy pathmain.ts
File metadata and controls
379 lines (329 loc) · 12.5 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
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
import { Editor, MarkdownView, Plugin, Modal, App, Notice } from 'obsidian';
import ReactPlayer from 'react-player/lazy'
import { VideoView, VIDEO_VIEW } from './view/VideoView';
import { TimestampPluginSettings, TimestampPluginSettingTab, DEFAULT_SETTINGS } from 'settings';
import * as http from "http";
import { AddressInfo } from "node:net";
import { server, startServer, PORT, localVideoRedirect, subtitleRedirect } from "handlers/server";
import { isLocalFile, cleanUrl, isSameVideo } from "handlers/misc";
import { getBiliInfo, isBiliUrl } from 'handlers/bilibili';
const ERRORS: { [key: string]: string } = {
"INVALID_URL": "\n> [!error] Invalid Video URL\n> The highlighted link is not a valid video url. Please try again with a valid link.\n",
"NO_ACTIVE_VIDEO": "\n> [!caution] Select Video\n> A video needs to be opened before using this hotkey.\n Highlight your video link and input your 'Open video player' hotkey to register a video.\n",
}
export default class TimestampPlugin extends Plugin {
settings: TimestampPluginSettings;
player: ReactPlayer;
setPlaying: React.Dispatch<React.SetStateAction<boolean>>;
editor: Editor;
server: http.Server;
async onload() {
// Register view
this.registerView(
VIDEO_VIEW,
(leaf) => new VideoView(leaf)
);
// Register settings
await this.loadSettings();
// Start local server
if (!server) await startServer(this.settings.port);
this.server = server;
// Markdown processor that turns timestamps into buttons
this.registerMarkdownCodeBlockProcessor("timestamp", (source, el, ctx) => {
// Match mm:ss or hh:mm:ss timestamp format
const regExp = /\d+:\d+:\d+|\d+:\d+/g;
const rows = source.split("\n").filter((row) => row.length > 0);
rows.forEach((row) => {
const match = row.match(regExp);
if (match) {
//create button for each timestamp
const div = el.createEl("div");
const button = div.createEl("button");
button.innerText = match[0];
button.style.backgroundColor = this.settings.timestampColor;
button.style.color = this.settings.timestampTextColor;
// convert timestamp to seconds and seek to that position when clicked
button.addEventListener("click", () => {
const timeArr = match[0].split(":").map((v) => parseInt(v));
const [hh, mm, ss] = timeArr.length === 2 ? [0, ...timeArr] : timeArr;
const seconds = (hh || 0) * 3600 + (mm || 0) * 60 + (ss || 0);
if (this.player) this.player.seekTo(seconds);
});
div.appendChild(button);
}
})
});
// Markdown processor that turns video urls into buttons to open views of the video
this.registerMarkdownCodeBlockProcessor("timestamp-url", (source, el, ctx) => {
const url = source.trim();
// Creates button for video url
const div = el.createEl("div");
const button = div.createEl("button");
button.innerText = url;
button.style.backgroundColor = "GRAY";
button.style.color = this.settings.urlTextColor;
if (isLocalFile(url) || ReactPlayer.canPlay(url) || isBiliUrl(url)) {
button.style.backgroundColor = this.settings.urlColor;
button.addEventListener("click", () => {
if (isSameVideo(this.player, url)) {
this.player?.seekTo(0);
} else {
this.activateView(url, this.editor);
}
});
} else {
if (this.editor) {
this.editor.replaceSelection(this.editor.getSelection() + "\n" + ERRORS["INVALID_URL"]);
}
}
});
// Command that gets selected video link and sends it to view which passes it to React component
this.addCommand({
id: 'trigger-player',
name: 'Open video player (copy video url and use hotkey)',
editorCallback: async (editor: Editor, view: MarkdownView) => {
// Get selected text or clipboard content and match against video url to convert link to video video id
const url = editor.getSelection().trim() || (await navigator.clipboard.readText()).trim();
// Activate the view with the valid link
if (isLocalFile(url) || ReactPlayer.canPlay(url) || isBiliUrl(url)) {
this.activateView(url, editor);
this.settings.noteTitle ?
editor.replaceSelection("\n" + this.settings.noteTitle + "\n" + "```timestamp-url \n " + url + "\n ```\n") :
editor.replaceSelection("```timestamp-url \n " + url + "\n ```\n")
this.editor = editor;
} else {
editor.replaceSelection(ERRORS["INVALID_URL"])
}
editor.setCursor(editor.getCursor().line + 1)
}
});
// This command inserts the timestamp of the playing video into the editor
this.addCommand({
id: 'timestamp-insert',
name: 'Insert timestamp based on videos current play time',
editorCallback: (editor: Editor, view: MarkdownView) => {
if (!this.player) {
editor.replaceSelection(ERRORS["NO_ACTIVE_VIDEO"])
return
}
// convert current video time into timestamp
const leadingZero = (num: number) => num < 10 ? "0" + num.toFixed(0) : num.toFixed(0);
const totalSeconds = Number(this.player.getCurrentTime().toFixed(2));
const hours = Math.floor(totalSeconds / 3600);
const minutes = Math.floor((totalSeconds - (hours * 3600)) / 60);
const seconds = totalSeconds - (hours * 3600) - (minutes * 60);
const time = (hours > 0 ? leadingZero(hours) + ":" : "") + leadingZero(minutes) + ":" + leadingZero(seconds);
// insert timestamp into editor
editor.replaceSelection("```timestamp \n " + time + "\n ```\n")
}
});
//Command that play/pauses the video
this.addCommand({
id: 'pause-player',
name: 'Pause player',
callback: () => {
this.setPlaying(!this.player.props.playing)
}
});
// Seek forward by set amount of seconds
this.addCommand({
id: 'seek-forward',
name: 'Seek Forward',
callback: () => {
if (this.player) this.player.seekTo(this.player.getCurrentTime() + parseInt(this.settings.forwardSeek));
}
});
// Seek backwards by set amount of seconds
this.addCommand({
id: 'seek-backward',
name: 'Seek Backward',
callback: () => {
if (this.player) this.player.seekTo(this.player.getCurrentTime() - parseInt(this.settings.backwardsSeek));
}
});
// // This adds a complex command that can check whether the current state of the app allows execution of the command
// this.addCommand({
// id: 'open-sample-modal-complex',
// name: 'Open sample modal (complex)',
// editorCallback: (editor: Editor, view: MarkdownView) => {
// this.editor = editor;
// new SampleModal(this.app, this.activateView.bind(this), editor).open();
// // This command will only show up in Command Palette when the check function returns true
// return true;
// }
// });
this.addCommand({
id: "add-local-media",
name: "Open Local Media",
editorCallback: (editor: Editor, view: MarkdownView) => {
const input = document.createElement("input");
input.setAttribute("type", "file");
input.accept = "video/*, audio/*, .mpd, .flv";
input.onchange = (e: any) => {
var url = e.target.files[0].path.trim();
this.activateView(url, this.editor);
editor.replaceSelection("\n" + "```timestamp-url \n " + url + "\n ```\n");
};
input.click();
},
});
this.addCommand({
id: "add-subtitles",
name: "Add subtitle file",
callback: async () => {
if (!this.player) {
return new Notice("Player is not working right now")
}
var input = document.createElement("input");
input.type = "file";
input.accept = ".srt,.vtt";
input.onchange = (e: any) => {
var files = e.target.files;
for (let i = 0; i < files.length; i++) {
var file = files[i];
var track = document.createElement("track");
track.kind = "subtitles";
track.label = file.name;
track.src = subtitleRedirect(file.path);
// track.mode = i == files.length - 1 ? "showing" : "hidden";
this.player.getInternalPlayer().appendChild(track);
}
};
input.click();
},
});
this.addCommand({
id: "video-snapshot",
name: "Take and copy to clipboard snapshot from video",
callback: async () => {
// https://github.com/ilkkao/capture-video-frame/blob/master/capture-video-frame.js
if (!this.player) return;
var video = document.querySelector("video");
if (!video || video.videoHeight==0 || video.videoWidth==0) {
return new Notice("Current player is not supported for taking snapshot!");
}
var canvas = document.createElement("canvas");
canvas.width = video.videoWidth;
canvas.height = video.videoHeight;
canvas.getContext("2d").drawImage(video, 0, 0);
// https://stackoverflow.com/a/60401130
canvas.toBlob(async (blob) => {
navigator.clipboard
.write([
new ClipboardItem({
[blob.type]: blob,
}),
])
.then(async () => {
// document.execCommand("paste");
new Notice("Snapshot copied to clipboard!");
});
});
},
});
// This adds a settings tab so the user can configure various aspects of the plugin
this.addSettingTab(new TimestampPluginSettingTab(this.app, this));
}
async onunload() {
this.player = null;
this.editor = null;
this.setPlaying = null;
this.app.workspace.detachLeavesOfType(VIDEO_VIEW);
this.server.close();
}
// This is called when a valid url is found => it activates the View which loads the React view
async activateView(url: string, editor: Editor) {
// this.app.workspace.detachLeavesOfType(VIDEO_VIEW);
if (this.app.workspace.getLeavesOfType(VIDEO_VIEW).length == 0) {
await this.app.workspace.getRightLeaf(false).setViewState({
type: VIDEO_VIEW,
active: true,
});
}
// this.app.workspace.revealLeaf(
// this.app.workspace.getLeavesOfType(VIDEO_VIEW)[0]
// );
// This triggers the React component to be loaded
this.app.workspace.getLeavesOfType(VIDEO_VIEW).forEach(async (leaf) => {
if (leaf.view instanceof VideoView) {
var localhost_url: string;
var subtitles: any[] = [];
if (isLocalFile(url)) {
localhost_url = localVideoRedirect(url);
url = url.toString().replace(/^\"(.+)\"$/, "$1");
} else if (isBiliUrl(url)) {
var bili_info = await getBiliInfo(url);
localhost_url = bili_info.url;
subtitles = bili_info.subtitles;
}
else{
url = cleanUrl(url)
}
const setupPlayer = (player: ReactPlayer, setPlaying: React.Dispatch<React.SetStateAction<boolean>>) => {
this.player = player;
this.setPlaying = setPlaying;
}
const setupError = (err: string) => {
editor.replaceSelection(editor.getSelection() + `\n> [!error] Streaming Error \n> ${err}\n`);
}
const saveTimeOnUnload = async () => {
if (this.player) {
this.settings.urlStartTimeMap.set(url, Number(this.player.getCurrentTime().toFixed(0)));
}
await this.saveSettings();
}
// create a new video instance, sets up state/unload functionality, and passes in a start time if available else 0
leaf.setEphemeralState({
url: localhost_url || url,
main_url: localhost_url ? url : null,
setupPlayer,
setupError,
saveTimeOnUnload,
start: this.settings.startAtLastPosition ? ~~this.settings.urlStartTimeMap.get(url) : 0,
subtitles: subtitles,
});
await this.saveSettings();
}
});
}
async loadSettings() {
// Fix for a weird bug that turns default map into a normal object when loaded
const data = await this.loadData()
if (data) {
const map = new Map(Object.keys(data.urlStartTimeMap).map(k => [k, data.urlStartTimeMap[k]]))
this.settings = { ...DEFAULT_SETTINGS, ...data, urlStartTimeMap: map };
} else {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}
}
async saveSettings() {
await this.saveData(this.settings);
}
}
// class SampleModal extends Modal {
// editor: Editor;
// activateView: (url: string, editor: Editor) => void;
// constructor(app: App, activateView: (url: string, editor: Editor) => void, editor: Editor) {
// super(app);
// this.activateView = activateView;
// this.editor = editor;
// }
// onOpen() {
// const { contentEl } = this;
// // add an input field to contentEl
// const input = contentEl.createEl('input');
// input.setAttribute("type", "file");
// input.onchange = (e: any) => {
// // accept local video input and make a url from input
// const url = URL.createObjectURL(e.target.files[0]);
// this.activateView(url, this.editor);
// // Can't get the buttons to work with local videos unfortunately
// // this.editor.replaceSelection("\n" + "```timestamp-url \n " + url.trim() + "\n ```\n")
// this.close();
// }
// }
// onClose() {
// const { contentEl } = this;
// contentEl.empty();
// }
// }