trilium/src/public/javascripts/services/tab_context.js

122 lines
3.3 KiB
JavaScript
Raw Normal View History

2019-05-02 05:06:18 +08:00
import protectedSessionHolder from "./protected_session_holder.js";
import server from "./server.js";
import utils from "./utils.js";
2020-01-12 19:30:30 +08:00
import appContext from "./app_context.js";
2020-01-25 16:56:08 +08:00
import treeService from "./tree.js";
2020-01-16 04:36:01 +08:00
import Component from "../widgets/component.js";
import treeCache from "./tree_cache.js";
2020-02-03 05:04:28 +08:00
import hoistedNoteService from "./hoisted_note.js";
2019-05-02 05:06:18 +08:00
2020-01-16 04:36:01 +08:00
class TabContext extends Component {
2019-05-12 01:44:58 +08:00
/**
2020-02-10 04:13:05 +08:00
* @param {string|null} tabId
2019-05-12 01:44:58 +08:00
*/
2020-02-27 17:03:14 +08:00
constructor(tabId = null) {
super();
2020-01-16 04:36:01 +08:00
2020-02-10 04:13:05 +08:00
this.tabId = tabId || utils.randomString(4);
2019-05-03 04:24:43 +08:00
}
2020-02-28 18:46:35 +08:00
setEmpty() {
this.triggerEvent('tabNoteSwitched', {
tabId: this.tabId,
notePath: this.notePath
});
}
2020-02-27 19:26:42 +08:00
async setNote(inputNotePath, triggerSwitchEvent = true) {
2020-01-25 00:54:47 +08:00
const notePath = await treeService.resolveNotePath(inputNotePath);
if (!notePath) {
console.error(`Cannot resolve note path ${inputNotePath}`);
return;
}
if (notePath === this.notePath) {
return;
}
2020-02-03 05:04:28 +08:00
if (await hoistedNoteService.checkNoteAccess(notePath) === false) {
return; // note is outside of hoisted subtree and user chose not to unhoist
}
2020-02-17 02:21:17 +08:00
await this.triggerEvent('beforeNoteSwitch', {tabId: this.tabId});
2020-02-04 04:56:45 +08:00
utils.closeActiveDialog();
this.notePath = notePath;
this.noteId = treeService.getNoteIdFromNotePath(notePath);
2020-02-02 18:44:08 +08:00
this.autoBookDisabled = false;
2019-05-15 04:29:47 +08:00
setTimeout(async () => {
// we include the note into recent list only if the user stayed on the note at least 5 seconds
if (notePath && notePath === this.notePath) {
await server.post('recent-notes', {
noteId: this.note.noteId,
notePath: this.notePath
});
2019-05-15 04:29:47 +08:00
}
}, 5000);
2020-01-19 18:03:34 +08:00
if (this.note.isProtected && protectedSessionHolder.isProtectedSessionAvailable()) {
// FIXME: there are probably more places where this should be done
protectedSessionHolder.touchProtectedSession();
}
2020-02-27 19:26:42 +08:00
if (triggerSwitchEvent) {
this.triggerEvent('tabNoteSwitched', {
tabId: this.tabId,
notePath: this.notePath
});
}
}
/** @property {NoteShort} */
get note() {
return treeCache.notes[this.noteId];
2020-02-01 18:33:31 +08:00
}
/** @return {NoteComplement} */
async getNoteComplement() {
if (!this.noteId) {
return null;
}
return await treeCache.getNoteComplement(this.noteId);
2020-02-01 18:33:31 +08:00
}
2020-01-22 04:43:23 +08:00
isActive() {
return appContext.tabManager.activeTabId === this.tabId;
2020-01-22 04:43:23 +08:00
}
2019-08-15 16:04:03 +08:00
getTabState() {
if (!this.notePath) {
return null;
}
return {
tabId: this.tabId,
notePath: this.notePath,
2020-02-09 04:23:42 +08:00
active: this.isActive()
2019-08-15 16:04:03 +08:00
}
}
2020-02-17 02:23:49 +08:00
async entitiesReloadedEvent({loadResults}) {
2020-02-10 05:31:52 +08:00
if (loadResults.isNoteReloaded(this.noteId)) {
const note = await treeCache.getNote(this.noteId);
if (note.isDeleted) {
this.noteId = null;
this.notePath = null;
2020-02-17 02:21:17 +08:00
this.triggerEvent('tabNoteSwitched', {
2020-02-10 05:31:52 +08:00
tabId: this.tabId,
notePath: this.notePath
});
}
2020-01-25 00:54:47 +08:00
}
2019-08-15 16:04:03 +08:00
}
2019-05-02 04:19:29 +08:00
}
2019-05-09 01:55:24 +08:00
export default TabContext;