event fixes WIP

This commit is contained in:
zadam
2020-03-06 22:17:07 +01:00
parent 26599f057c
commit e10d23289e
18 changed files with 359 additions and 1239 deletions

View File

@@ -1,7 +1,6 @@
import treeService from '../services/tree.js';
import noteAutocompleteService from "../services/note_autocomplete.js";
import utils from "../services/utils.js";
import appContext from "../services/app_context.js";
const $dialog = $("#add-link-dialog");
const $form = $("#add-link-form");

View File

@@ -181,7 +181,7 @@ export default class TabManager extends Component {
const tabContext = new TabContext(tabId);
this.child(tabContext);
await this.triggerEvent('newTabOpened', {tabId: tabContext.tabId});
await this.triggerEvent('newTabOpened', {tabContext});
return tabContext;
}

View File

@@ -47,7 +47,7 @@ class BasicWidget extends Component {
$widget.addClass('component')
.prop('component', this);
this.toggle(this.isEnabled());
this.toggleInt(this.isEnabled());
if (this.cssEl) {
const css = this.cssEl.trim().startsWith('<style>') ? this.cssEl : `<style>${this.cssEl}</style>`;
@@ -75,8 +75,12 @@ class BasicWidget extends Component {
*/
doRender() {}
toggle(show) {
this.$widget.toggle(show);
toggleInt(show) {
this.$widget.toggleClass('hidden-int', !show);
}
toggleExt(show) {
this.$widget.toggleClass('hidden-ext', !show);
}
isVisible() {

View File

@@ -41,8 +41,9 @@ export default class CollapsibleWidget extends TabAwareWidget {
this.$bodyWrapper.collapse("show");
}
this.$bodyWrapper.on('hidden.bs.collapse', () => options.save(widgetName + 'Collapsed', 'true'));
this.$bodyWrapper.on('shown.bs.collapse', () => options.save(widgetName + 'Collapsed', 'false'));
// using immediate variants of the event so that the previous collapse is not caught
this.$bodyWrapper.on('hide.bs.collapse', () => options.save(widgetName + 'Collapsed', 'true'));
this.$bodyWrapper.on('show.bs.collapse', () => options.save(widgetName + 'Collapsed', 'false'));
this.$body = this.$bodyWrapper.find('.card-body');

View File

@@ -24,7 +24,7 @@ const TPL = `
.note-detail {
height: 100%;
min-height: 0;
}
}
</style>
</div>
`;
@@ -59,6 +59,10 @@ export default class NoteDetailWidget extends TabAwareWidget {
});
}
isEnabled() {
return true;
}
doRender() {
this.$widget = $(TPL);
@@ -89,18 +93,7 @@ export default class NoteDetailWidget extends TabAwareWidget {
return this.$widget;
}
isEnabled() {
return this.tabContext && this.tabContext.isActive();
}
async refresh() {
if (!this.isEnabled()) {
this.toggle(false);
return;
}
this.toggle(true);
this.type = await this.getWidgetType();
this.mime = this.note ? this.note.mime : null;
@@ -130,7 +123,7 @@ export default class NoteDetailWidget extends TabAwareWidget {
setupClasses() {
for (const clazz of Array.from(this.$widget[0].classList)) { // create copy to safely iterate over while removing classes
if (clazz !== 'note-detail') {
if (clazz !== 'note-detail' && !clazz.startsWith('hidden-')) {
this.$widget.removeClass(clazz);
}
}
@@ -156,14 +149,16 @@ export default class NoteDetailWidget extends TabAwareWidget {
}
async getWidgetType() {
if (!this.note) {
const note = this.note;
if (!note) {
return "empty";
}
let type = this.note.type;
let type = note.type;
if (type === 'text' && !this.tabContext.autoBookDisabled
&& this.note.hasChildren()
&& note.hasChildren()
&& utils.isDesktop()) {
const noteComplement = await this.tabContext.getNoteComplement();
@@ -173,7 +168,7 @@ export default class NoteDetailWidget extends TabAwareWidget {
}
}
if (this.note.isProtected && !protectedSessionHolder.isProtectedSessionAvailable()) {
if (note.isProtected && !protectedSessionHolder.isProtectedSessionAvailable()) {
type = 'protected-session';
}

View File

@@ -425,7 +425,7 @@ export default class NoteTreeWidget extends TabAwareWidget {
}
async refresh() {
this.toggle(this.isEnabled());
this.toggleInt(this.isEnabled());
const oldActiveNode = this.getActiveNode();

View File

@@ -79,10 +79,10 @@ export default class PromotedAttributesWidget extends TabAwareWidget {
// we replace the whole content in one step so there can't be any race conditions
// (previously we saw promoted attributes doubling)
this.$container.empty().append($tbody);
this.toggle(true);
this.toggleInt(true);
}
else {
this.toggle(false);
this.toggleInt(false);
}
return attributes;

View File

@@ -170,7 +170,7 @@ export default class SearchBoxWidget extends BasicWidget {
searchInSubtreeEvent({noteId}) {
noteId = noteId || appContext.tabManager.getActiveTabNoteId();
this.toggle(true);
this.toggleInt(true);
this.$searchInput.val(`@in=${noteId} @text*=*`);
}

View File

@@ -34,13 +34,13 @@ export default class SearchResultsWidget extends BasicWidget {
this.$searchResults = this.$widget;
this.$searchResultsInner = this.$widget.find(".search-results-list");
this.toggle(false);
this.toggleInt(false);
return this.$widget;
}
searchResultsEvent({results}) {
this.toggle(true);
this.toggleInt(true);
this.$searchResultsInner.empty();
this.$searchResults.show();

View File

@@ -2,11 +2,6 @@ import BasicWidget from "./basic_widget.js";
import appContext from "../services/app_context.js";
export default class TabAwareWidget extends BasicWidget {
setTabContextEvent({tabContext}) {
/** @var {TabContext} */
this.tabContext = tabContext;
}
isTab(tabId) {
return this.tabContext && this.tabContext.tabId === tabId;
}
@@ -27,36 +22,15 @@ export default class TabAwareWidget extends BasicWidget {
return this.tabContext && this.tabContext.notePath;
}
async tabNoteSwitchedEvent({tabId, notePath}) {
// if notePath does not match then the tabContext has been switched to another note in the mean time
if (this.isTab(tabId) && this.notePath === notePath) {
await this.noteSwitched();
}
}
async noteTypeMimeChangedEvent({noteId}) {
if (this.noteId === noteId) {
await this.refresh();
}
}
async noteSwitched() {
await this.refresh();
}
async activeTabChanged() {
await this.refresh();
}
isEnabled() {
return !!this.note && this.tabContext.isActive();
return !!this.note;
}
async refresh() {
if (this.isEnabled()) {
const start = Date.now();
this.toggle(true);
this.toggleInt(true);
await this.refreshWithNote(this.note, this.notePath);
const end = Date.now();
@@ -66,29 +40,55 @@ export default class TabAwareWidget extends BasicWidget {
}
}
else {
this.toggle(false);
this.toggleInt(false);
}
}
async refreshWithNote(note, notePath) {}
async tabNoteSwitchedEvent({tabId, notePath}) {
// if notePath does not match then the tabContext has been switched to another note in the mean time
if (this.notePath === notePath) {
await this.noteSwitched();
}
}
async noteSwitched() {
await this.refresh();
}
async activeTabChangedEvent({tabId}) {
this.tabContext = appContext.tabManager.getTabContextById(tabId);
if (this.tabContext.tabId === appContext.tabManager.getActiveTabContext().tabId) {
await this.activeTabChanged();
}
await this.activeTabChanged();
}
async activeTabChanged() {
await this.refresh();
}
// when note is both switched and activated, this should not produce double refresh
async tabNoteSwitchedAndActivatedEvent({tabId, notePath}) {
this.tabContext = appContext.tabManager.getTabContextById(tabId);
if (this.tabContext.tabId === appContext.tabManager.getActiveTabContext().tabId
&& this.notePath === notePath) {
// 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();
}
}
this.tabContext = appContext.tabManager.getActiveTabContext();
setTabContextEvent({tabContext}) {
/** @var {TabContext} */
this.tabContext = tabContext;
}
async newTabOpenedEvent({tabContext}) {
/** @var {TabContext} */
this.tabContext = tabContext;
}
async noteTypeMimeChangedEvent({noteId}) {
if (this.isNote(noteId)) {
await this.refresh();
}
}

View File

@@ -1,6 +1,5 @@
import TabAwareWidget from "./tab_aware_widget.js";
import keyboardActionsService from "../services/keyboard_actions.js";
import appContext from "../services/app_context.js";
export default class TabCachingWidget extends TabAwareWidget {
constructor(widgetFactory) {
@@ -11,7 +10,7 @@ export default class TabCachingWidget extends TabAwareWidget {
}
isEnabled() {
return this.tabContext && this.tabContext.isActive();
return !!this.tabContext;
}
doRender() {
@@ -31,7 +30,11 @@ export default class TabCachingWidget extends TabAwareWidget {
return Promise.resolve();
}
async newTabOpenedEvent({tabId}) {
async newTabOpenedEvent({tabContext}) {
super.newTabOpenedEvent({tabContext});
const {tabId} = tabContext;
if (this.widgets[tabId]) {
return;
}
@@ -39,20 +42,20 @@ export default class TabCachingWidget extends TabAwareWidget {
this.widgets[tabId] = this.widgetFactory();
const $renderedWidget = this.widgets[tabId].render();
this.widgets[tabId].toggleExt(this.widgets[tabId]);
this.$widget.after($renderedWidget);
keyboardActionsService.updateDisplayedShortcuts($renderedWidget);
await this.widgets[tabId].handleEvent('setTabContext', {
tabContext: appContext.tabManager.getTabContextById(tabId)
});
await this.widgets[tabId].handleEvent('newTabOpened', {tabContext});
this.child(this.widgets[tabId]); // add as child only once it is ready (rendered with tabContext)
}
async refreshWithNote() {
for (const widget of Object.values(this.widgets)) {
widget.toggle(false);
widget.toggleExt(false);
}
if (!this.tabContext) {
@@ -64,7 +67,7 @@ export default class TabCachingWidget extends TabAwareWidget {
const widget = this.widgets[this.tabContext.tabId];
if (widget) {
widget.toggle(widget.isEnabled());
widget.toggleExt(true);
}
else {
console.error(`Widget for tab ${this.tabContext.tabId} not found.`);
@@ -82,12 +85,11 @@ export default class TabCachingWidget extends TabAwareWidget {
}
}
toggle(show) {
toggleInt(show) {} // not needed
toggleByTab(show) {
for (const tabId in this.widgets) {
this.widgets[tabId].toggle(
show
&& this.isTab(tabId)
&& this.widgets[tabId].isEnabled());
this.widgets[tabId].toggleExt(show && this.isTab(tabId));
}
}
}

View File

@@ -420,8 +420,8 @@ export default class TabRowWidget extends BasicWidget {
if (tabEl) tabEl.setAttribute('active', '');
}
newTabOpenedEvent({tabId}) {
this.addTab(tabId);
newTabOpenedEvent({tabContext}) {
this.addTab(tabContext.tabId);
}
removeTab(tabId) {

View File

@@ -145,7 +145,7 @@ export default class TextTypeWidget extends TypeWidget {
const noteComplement = await this.tabContext.getNoteComplement();
this.spacedUpdate.allowUpdateWithoutChange(() => {
await this.spacedUpdate.allowUpdateWithoutChange(() => {
this.textEditor.setData(noteComplement.content);
});
}

View File

@@ -7,21 +7,21 @@ export default class TypeWidget extends TabAwareWidget {
/**
* @param {NoteShort} note
*/
doRefresh(note) {}
async doRefresh(note) {}
async refresh() {
const thisWidgetType = this.constructor.getType();
const noteWidgetType = await this.parent.getWidgetType();
if (thisWidgetType !== noteWidgetType) {
this.toggle(false);
this.toggleInt(false);
this.cleanup();
}
else {
this.toggle(true);
this.toggleInt(true);
this.doRefresh(this.note);
await this.doRefresh(this.note);
}
}