2020-01-12 19:05:09 +01:00
|
|
|
import BasicWidget from "./basic_widget.js";
|
2020-02-16 18:11:32 +01:00
|
|
|
import appContext from "../services/app_context.js";
|
2020-01-12 19:05:09 +01:00
|
|
|
|
|
|
|
export default class TabAwareWidget extends BasicWidget {
|
2020-01-19 20:18:02 +01:00
|
|
|
isTab(tabId) {
|
|
|
|
return this.tabContext && this.tabContext.tabId === tabId;
|
|
|
|
}
|
|
|
|
|
|
|
|
isNote(noteId) {
|
2020-01-27 22:58:03 +01:00
|
|
|
return this.noteId === noteId;
|
|
|
|
}
|
|
|
|
|
|
|
|
get note() {
|
|
|
|
return this.tabContext && this.tabContext.note;
|
|
|
|
}
|
|
|
|
|
|
|
|
get noteId() {
|
|
|
|
return this.note && this.note.noteId;
|
2020-01-19 20:18:02 +01:00
|
|
|
}
|
|
|
|
|
2020-01-28 21:54:28 +01:00
|
|
|
get notePath() {
|
|
|
|
return this.tabContext && this.tabContext.notePath;
|
|
|
|
}
|
|
|
|
|
2020-02-08 21:54:39 +01:00
|
|
|
isEnabled() {
|
2020-03-06 22:17:07 +01:00
|
|
|
return !!this.note;
|
2020-02-02 20:02:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
async refresh() {
|
2020-02-08 21:54:39 +01:00
|
|
|
if (this.isEnabled()) {
|
2020-02-02 20:02:08 +01:00
|
|
|
const start = Date.now();
|
|
|
|
|
2020-03-06 22:17:07 +01:00
|
|
|
this.toggleInt(true);
|
2020-02-02 20:02:08 +01:00
|
|
|
await this.refreshWithNote(this.note, this.notePath);
|
|
|
|
|
|
|
|
const end = Date.now();
|
|
|
|
|
2020-02-08 10:40:58 +01:00
|
|
|
if (glob.PROFILING_LOG && end - start > 10) {
|
2020-02-02 20:02:08 +01:00
|
|
|
console.log(`Refresh of ${this.componentId} took ${end-start}ms`);
|
|
|
|
}
|
2020-01-19 11:37:24 +01:00
|
|
|
}
|
|
|
|
else {
|
2020-03-06 22:17:07 +01:00
|
|
|
this.toggleInt(false);
|
2020-01-19 11:37:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-27 12:41:15 +01:00
|
|
|
async refreshWithNote(note, notePath) {}
|
2020-01-12 19:05:09 +01:00
|
|
|
|
2020-03-07 13:40:46 +01:00
|
|
|
async tabNoteSwitchedEvent({tabContext, notePath}) {
|
2020-03-06 22:17:07 +01:00
|
|
|
// if notePath does not match then the tabContext has been switched to another note in the mean time
|
2020-03-15 18:57:03 +01:00
|
|
|
if (tabContext.notePath === notePath) {
|
2020-03-06 22:17:07 +01:00
|
|
|
await this.noteSwitched();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async noteSwitched() {
|
|
|
|
await this.refresh();
|
|
|
|
}
|
|
|
|
|
2020-03-07 13:40:46 +01:00
|
|
|
async activeTabChangedEvent({tabContext}) {
|
|
|
|
this.tabContext = tabContext;
|
2020-01-12 19:05:09 +01:00
|
|
|
|
2020-03-06 22:17:07 +01:00
|
|
|
await this.activeTabChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
async activeTabChanged() {
|
|
|
|
await this.refresh();
|
2020-01-12 19:05:09 +01:00
|
|
|
}
|
2020-02-01 22:29:32 +01:00
|
|
|
|
2020-02-27 12:26:42 +01:00
|
|
|
// when note is both switched and activated, this should not produce double refresh
|
2020-03-07 13:40:46 +01:00
|
|
|
async tabNoteSwitchedAndActivatedEvent({tabContext, notePath}) {
|
|
|
|
this.tabContext = tabContext;
|
2020-02-29 22:04:46 +01:00
|
|
|
|
2020-03-06 22:17:07 +01:00
|
|
|
// if notePath does not match then the tabContext has been switched to another note in the mean time
|
|
|
|
if (this.notePath === notePath) {
|
|
|
|
await this.refresh();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
setTabContextEvent({tabContext}) {
|
|
|
|
/** @var {TabContext} */
|
|
|
|
this.tabContext = tabContext;
|
|
|
|
}
|
2020-02-28 11:46:35 +01:00
|
|
|
|
2020-03-06 22:17:07 +01:00
|
|
|
async noteTypeMimeChangedEvent({noteId}) {
|
|
|
|
if (this.isNote(noteId)) {
|
2020-02-28 00:31:12 +01:00
|
|
|
await this.refresh();
|
|
|
|
}
|
2020-02-27 12:26:42 +01:00
|
|
|
}
|
|
|
|
|
2020-02-27 12:41:15 +01:00
|
|
|
async treeCacheReloadedEvent() {
|
|
|
|
await this.refresh();
|
2020-02-01 22:29:32 +01:00
|
|
|
}
|
2020-02-04 22:46:17 +01:00
|
|
|
|
2020-02-27 12:41:15 +01:00
|
|
|
async lazyLoadedEvent() {
|
2020-02-06 20:04:43 +01:00
|
|
|
if (!this.tabContext) { // has not been loaded yet
|
2020-02-16 18:11:32 +01:00
|
|
|
this.tabContext = appContext.tabManager.getActiveTabContext();
|
2020-02-06 20:04:43 +01:00
|
|
|
}
|
|
|
|
|
2020-02-27 12:41:15 +01:00
|
|
|
await this.refresh();
|
2020-02-04 22:46:17 +01:00
|
|
|
}
|
2020-01-12 19:05:09 +01:00
|
|
|
}
|