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

146 lines
4.1 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 bundleService from "./bundle.js";
import utils from "./utils.js";
import optionsService from "./options.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";
2019-05-02 05:06:18 +08:00
let showSidebarInNewTab = true;
optionsService.addLoadListener(options => {
showSidebarInNewTab = options.is('showSidebarInNewTab');
});
2020-01-16 04:36:01 +08:00
class TabContext extends Component {
2019-05-12 01:44:58 +08:00
/**
2020-01-16 04:36:01 +08:00
* @param {AppContext} appContext
2020-01-13 02:05:09 +08:00
* @param {TabRowWidget} tabRow
* @param {object} state
2019-05-12 01:44:58 +08:00
*/
2020-01-16 04:36:01 +08:00
constructor(appContext, tabRow, state = {}) {
super(appContext);
2019-05-12 01:44:58 +08:00
this.tabRow = tabRow;
this.tabId = state.tabId || utils.randomString(4);
this.state = state;
2019-09-05 04:13:22 +08:00
2020-01-21 03:51:22 +08:00
this.trigger('tabOpened', {tabId: this.tabId});
2019-05-03 04:24:43 +08:00
}
2020-01-25 00:54:47 +08:00
async setNote(inputNotePath) {
const notePath = await treeService.resolveNotePath(inputNotePath);
if (!notePath) {
console.error(`Cannot resolve note path ${inputNotePath}`);
return;
}
if (notePath === this.notePath) {
return;
}
await this.trigger('beforeNoteSwitch', {tabId: this.tabId}, true);
this.notePath = notePath;
this.noteId = treeService.getNoteIdFromNotePath(notePath);
2020-02-02 18:44:08 +08:00
this.autoBookDisabled = false;
2020-01-19 01:01:16 +08:00
//this.cleanup(); // esp. on windows autocomplete is not getting closed automatically
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-25 03:15:53 +08:00
// should be done somewhere else ...
2019-09-05 04:13:22 +08:00
bundleService.executeRelationBundles(this.note, 'runOnNoteView', this);
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-03 01:46:50 +08:00
this.trigger('tabNoteSwitched', {
tabId: this.tabId,
notePath: this.notePath
});
2020-01-25 00:54:47 +08:00
this.trigger('openTabsChanged');
}
/** @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-20 04:12:53 +08:00
async remove() {
await this.trigger('beforeTabRemove', {tabId: this.tabId}, true);
this.trigger('tabRemoved', {tabId: this.tabId});
}
2020-01-22 04:43:23 +08:00
isActive() {
return this.appContext.activeTabId === this.tabId;
}
2019-08-15 16:04:03 +08:00
getTabState() {
if (!this.notePath) {
return null;
}
return {
tabId: this.tabId,
notePath: this.notePath,
2020-01-16 05:27:52 +08:00
active: this.tabRow.activeTabId === this.tabId
2019-08-15 16:04:03 +08:00
}
}
stateChanged() {
2020-01-25 00:54:47 +08:00
appContext.openTabsChangedListener();
}
noteDeletedListener({noteId}) {
2020-02-03 01:46:50 +08:00
if (this.noteId === noteId) {
this.noteId = null;
2020-01-25 00:54:47 +08:00
this.notePath = null;
2020-02-03 01:46:50 +08:00
this.trigger('tabNoteSwitched', {
tabId: this.tabId,
notePath: this.notePath
});
2020-01-25 00:54:47 +08:00
}
2019-08-15 16:04:03 +08:00
}
2020-01-29 04:54:28 +08:00
// FIXME
async _setTitleBar() {
document.title = "Trilium Notes";
const activeTabContext = this.getActiveTabContext();
if (activeTabContext && activeTabContext.notePath) {
const note = await treeCache.getNote(treeService.getNoteIdFromNotePath(activeTabContext.notePath));
// it helps navigating in history if note title is included in the title
document.title += " - " + note.title;
}
}
2019-05-02 04:19:29 +08:00
}
2019-05-09 01:55:24 +08:00
export default TabContext;