shortcut improvements

This commit is contained in:
zadam
2022-11-25 15:29:57 +01:00
parent 70552d58ac
commit 6883b71ce7
31 changed files with 248 additions and 94 deletions

View File

@@ -0,0 +1,40 @@
import ButtonWidget from "./button_widget.js";
import froca from "../../services/froca.js";
import attributeService from "../../services/attributes.js";
export default class ButtonFromNoteWidget extends ButtonWidget {
constructor() {
super();
this.settings.buttonNoteId = null;
}
buttonNoteId(noteId) {
this.settings.buttonNoteId = noteId;
return this;
}
doRender() {
super.doRender();
this.updateIcon();
}
updateIcon() {
froca.getNote(this.settings.buttonNoteId).then(note => {
this.settings.icon = note.getLabelValue("iconClass");
this.refreshIcon();
});
}
entitiesReloadedEvent({loadResults}) {
if (loadResults.getAttributes(this.componentId).find(attr =>
attr.type === 'label'
&& attr.name === 'iconClass'
&& attributeService.isAffecting(attr, this.note))) {
this.updateIcon();
}
}
}

View File

@@ -18,7 +18,12 @@ export default class ButtonWidget extends NoteContextAwareWidget {
super();
this.settings = {
titlePlacement: 'right'
titlePlacement: 'right',
title: null,
icon: null,
command: null,
onClick: null,
onContextMenu: null
};
}
@@ -39,6 +44,14 @@ export default class ButtonWidget extends NoteContextAwareWidget {
});
}
if (this.settings.onContextMenu) {
this.$widget.on("contextmenu", e => {
this.$widget.tooltip("hide");
this.settings.onContextMenu(e);
});
}
this.$widget.attr("data-placement", this.settings.titlePlacement);
this.$widget.tooltip({
@@ -70,8 +83,7 @@ export default class ButtonWidget extends NoteContextAwareWidget {
}
}
this.$widget
.addClass(this.settings.icon);
this.$widget.addClass(this.settings.icon);
}
initialRenderCompleteEvent() {
@@ -102,4 +114,8 @@ export default class ButtonWidget extends NoteContextAwareWidget {
this.settings.onClick = handler;
return this;
}
onContextMenu(handler) {
this.settings.onContextMenu = handler;
}
}

View File

@@ -124,11 +124,16 @@ const TPL = `
<kbd ></kbd>
</li>
<li class="dropdown-item options-button" data-trigger-command="showLaunchBarShortcuts">
<li class="dropdown-item" data-trigger-command="showLaunchBarShortcuts">
<span class="bx bx-sidebar"></span>
Configure launchbar shortcuts
</li>
<li class="dropdown-item" data-trigger-command="showShareSubtree">
<span class="bx bx-share-alt"></span>
Show share subtree
</li>
<li class="dropdown-item dropdown-submenu">
<span class="dropdown-toggle">
<span class="bx bx-empty"></span>
@@ -160,6 +165,11 @@ const TPL = `
Reload frontend
<kbd data-command="reloadFrontendApp"></kbd>
</li>
<li class="dropdown-item" data-trigger-command="showHiddenSubtree">
<span class="bx bx-empty"></span>
Show hidden subtree
</li>
</ul>
</li>

View File

@@ -0,0 +1,81 @@
import utils from "../../../services/utils.js";
import contextMenu from "../../../menus/context_menu.js";
import treeService from "../../../services/tree.js";
import ButtonFromNoteWidget from "../button_from_note.js";
export default class AbstractHistoryNavigationWidget extends ButtonFromNoteWidget {
isEnabled() {
return super.isEnabled() && utils.isElectron();
}
doRender() {
super.doRender();
this.webContents = utils.dynamicRequire('@electron/remote').getCurrentWebContents();
// without this the history is preserved across frontend reloads
this.webContents.clearHistory();
this.refresh();
}
async showContextMenu(e) {
e.preventDefault();
// API is broken and will be replaced: https://github.com/electron/electron/issues/33899
// until then no context menu
return;
if (this.webContents.history.length < 2) {
return;
}
let items = [];
const activeIndex = this.webContents.getActiveIndex();
for (const idx in this.webContents.history) {
const url = this.webContents.history[idx];
const [_, notePathWithTab] = url.split('#');
const [notePath, ntxId] = notePathWithTab.split('-');
const title = await treeService.getNotePathTitle(notePath);
items.push({
title,
idx,
uiIcon: idx == activeIndex ? "bx bx-radio-circle-marked" : // compare with type coercion!
(idx < activeIndex ? "bx bx-left-arrow-alt" : "bx bx-right-arrow-alt")
});
}
items.reverse();
if (items.length > 20) {
items = items.slice(0, 50);
}
contextMenu.show({
x: e.pageX,
y: e.pageY,
items,
selectMenuItemHandler: ({idx}) => this.webContents.goToIndex(idx)
});
}
refresh() {
if (!utils.isElectron()) {
return;
}
// disabling this because in electron 9 there's weird performance problem which makes these webContents calls
// block UI thread for > 1 second on specific notes (book notes displaying underlying render notes with scripts)
// this.$backInHistory.toggleClass('disabled', !this.webContents.canGoBack());
// this.$forwardInHistory.toggleClass('disabled', !this.webContents.canGoForward());
}
activeNoteChangedEvent() {
this.refresh();
}
}

View File

@@ -0,0 +1,14 @@
import AbstractHistoryNavigationWidget from "./abstract_history.js";
export default class BackInHistoryButtonWidget extends AbstractHistoryNavigationWidget {
constructor() {
super();
this.icon('bx-left-arrow-circle')
.title("Go to previous note.")
.command("backInNoteHistory")
.titlePlacement("right")
.buttonNoteId('lb_backinhistory')
.onContextMenu(e => this.showContextMenu(e));
}
}

View File

@@ -0,0 +1,14 @@
import AbstractHistoryNavigationWidget from "./abstract_history.js";
export default class ForwardInHistoryButtonWidget extends AbstractHistoryNavigationWidget {
constructor() {
super();
this.icon('bx-left-arrow-circle')
.title("Go to next note.")
.command("forwardInNoteHistory")
.titlePlacement("right")
.buttonNoteId('lb_forwardinhistory')
.onContextMenu(e => this.showContextMenu(e));
}
}

View File

@@ -11,6 +11,6 @@ export default class NoteRevisionsButton extends ButtonWidget {
}
isEnabled() {
return super.isEnabled() && this.note?.type !== 'shortcut';
return super.isEnabled() && !['shortcut', 'doc'].includes(this.note?.type);
}
}

View File

@@ -1,16 +0,0 @@
import ButtonWidget from "./button_widget.js";
export default class ShowNoteSourceButton extends ButtonWidget {
isEnabled() {
return super.isEnabled() && this.note && ['text', 'relation-map'].includes(this.note.type);
}
constructor() {
super();
this.icon('bx bx-code')
.title("Show Note Source")
.command("openNoteSourceDialog")
.titlePlacement("bottom");
}
}