shortcut improvements

This commit is contained in:
zadam 2022-11-29 16:16:57 +01:00
parent eecfce3cc9
commit e759b4846a
9 changed files with 88 additions and 46 deletions

View File

@ -21,7 +21,7 @@ const NOTE_TYPE_ICONS = {
"mermaid": "bx bx-selection", "mermaid": "bx bx-selection",
"canvas": "bx bx-pen", "canvas": "bx bx-pen",
"web-view": "bx bx-globe-alt", "web-view": "bx bx-globe-alt",
"shortcut": "bx bx-up-arrow-circle", "shortcut": "bx bx-link",
"doc": "bx bxs-file-doc" "doc": "bx bxs-file-doc"
}; };
@ -827,9 +827,7 @@ class NoteShort {
} }
isLaunchBarConfig() { isLaunchBarConfig() {
// launch bar config should be max 2 levels deep return this.type === 'shortcut' || this.noteId.startsWith("lb_");
return this.noteId.startsWith("lb_")
|| this.getParentBranchIds().find(branchId => branchId.startsWith("lb_"));
} }
} }

View File

@ -6,11 +6,17 @@ export default class ButtonFromNoteWidget extends ButtonWidget {
constructor() { constructor() {
super(); super();
this.settings.buttonNoteId = null; this.settings.buttonNoteIdProvider = null;
this.settings.defaultIconProvider = null;
} }
buttonNoteId(noteId) { buttonNoteIdProvider(provider) {
this.settings.buttonNoteId = noteId; this.settings.buttonNoteIdProvider = provider;
return this;
}
defaultIconProvider(provider) {
this.settings.defaultIconProvider = provider;
return this; return this;
} }
@ -21,18 +27,32 @@ export default class ButtonFromNoteWidget extends ButtonWidget {
} }
updateIcon() { updateIcon() {
froca.getNote(this.settings.buttonNoteId).then(note => { const buttonNoteId = this.settings.buttonNoteIdProvider();
this.settings.icon = note.getLabelValue("iconClass");
if (!buttonNoteId && this.settings.defaultIconProvider()) {
this.settings.icon = this.settings.defaultIconProvider();
this.refreshIcon();
} else {
froca.getNote(buttonNoteId).then(note => {
this.settings.icon = note.getIcon();
this.refreshIcon(); this.refreshIcon();
}); });
} }
}
entitiesReloadedEvent({loadResults}) { entitiesReloadedEvent({loadResults}) {
const buttonNote = froca.getNoteFromCache(this.buttonNoteIdProvider());
if (!buttonNote) {
return;
}
if (loadResults.getAttributes(this.componentId).find(attr => if (loadResults.getAttributes(this.componentId).find(attr =>
attr.type === 'label' attr.type === 'label'
&& attr.name === 'iconClass' && attr.name === 'iconClass'
&& attributeService.isAffecting(attr, this.note))) { && attributeService.isAffecting(attr, buttonNote))) {
this.updateIcon(); this.updateIcon();
} }

View File

@ -8,7 +8,7 @@ export default class BackInHistoryButtonWidget extends AbstractHistoryNavigation
.title("Go to previous note.") .title("Go to previous note.")
.command("backInNoteHistory") .command("backInNoteHistory")
.titlePlacement("right") .titlePlacement("right")
.buttonNoteId('lb_backinhistory') .buttonNoteIdProvider(() => 'lb_backinhistory')
.onContextMenu(e => this.showContextMenu(e)); .onContextMenu(e => this.showContextMenu(e));
} }
} }

View File

@ -8,7 +8,7 @@ export default class ForwardInHistoryButtonWidget extends AbstractHistoryNavigat
.title("Go to next note.") .title("Go to next note.")
.command("forwardInNoteHistory") .command("forwardInNoteHistory")
.titlePlacement("right") .titlePlacement("right")
.buttonNoteId('lb_forwardinhistory') .buttonNoteIdProvider(() => 'lb_forwardinhistory')
.onContextMenu(e => this.showContextMenu(e)); .onContextMenu(e => this.showContextMenu(e));
} }
} }

View File

@ -9,6 +9,8 @@ import ProtectedSessionStatusWidget from "../buttons/protected_session_status.js
import SyncStatusWidget from "../sync_status.js"; import SyncStatusWidget from "../sync_status.js";
import BackInHistoryButtonWidget from "../buttons/history/history_back.js"; import BackInHistoryButtonWidget from "../buttons/history/history_back.js";
import ForwardInHistoryButtonWidget from "../buttons/history/history_forward.js"; import ForwardInHistoryButtonWidget from "../buttons/history/history_forward.js";
import dialogService from "../../services/dialog.js";
import ButtonFromNoteWidget from "../buttons/button_from_note.js";
export default class ShortcutContainer extends FlexContainer { export default class ShortcutContainer extends FlexContainer {
constructor() { constructor() {
@ -65,17 +67,29 @@ export default class ShortcutContainer extends FlexContainer {
return; return;
} }
if (shortcut.getLabelValue("command")) { const shortcutType = shortcut.getLabelValue("shortcutType");
if (shortcutType === 'command') {
this.child(new ButtonWidget() this.child(new ButtonWidget()
.title(shortcut.title) .title(shortcut.title)
.icon(shortcut.getIcon()) .icon(shortcut.getIcon())
.command(shortcut.getLabelValue("command"))); .command(shortcut.getLabelValue("command")));
} else if (shortcut.hasRelation('targetNote')) { } else if (shortcutType === 'note') {
this.child(new ButtonWidget() this.child(new ButtonFromNoteWidget()
.title(shortcut.title) .title(shortcut.title)
.icon(shortcut.getIcon()) .buttonNoteIdProvider(() => shortcut.getRelationValue('targetNote'))
.onClick(() => appContext.tabManager.openTabWithNoteWithHoisting(shortcut.getRelationValue('targetNote'), true))); .defaultIconProvider(() => shortcut.getIcon())
} else if (shortcut.hasRelation('script')) { .onClick(() => {
const targetNoteId = shortcut.getRelationValue('targetNote');
if (!targetNoteId) {
dialogService.info("This shortcut doesn't define target note.");
return;
}
appContext.tabManager.openTabWithNoteWithHoisting(targetNoteId, true)
}));
} else if (shortcutType === 'script') {
this.child(new ButtonWidget() this.child(new ButtonWidget()
.title(shortcut.title) .title(shortcut.title)
.icon(shortcut.getIcon()) .icon(shortcut.getIcon())
@ -84,13 +98,13 @@ export default class ShortcutContainer extends FlexContainer {
await script.executeScript(); await script.executeScript();
})); }));
} else if (shortcut.hasRelation('widget')) { } else if (shortcutType === 'customWidget') {
const widget = await shortcut.getRelationTarget('widget'); const widget = await shortcut.getRelationTarget('widget');
const res = await widget.executeScript(); const res = await widget.executeScript();
this.child(res); this.child(res);
} else { } else if (shortcutType === 'builtinWidget') {
const builtinWidget = shortcut.getLabelValue("builtinWidget"); const builtinWidget = shortcut.getLabelValue("builtinWidget");
if (builtinWidget) { if (builtinWidget) {
@ -117,9 +131,11 @@ export default class ShortcutContainer extends FlexContainer {
} else if (builtinWidget === 'forwardInHistoryButton') { } else if (builtinWidget === 'forwardInHistoryButton') {
this.child(new ForwardInHistoryButtonWidget()); this.child(new ForwardInHistoryButtonWidget());
} else { } else {
console.log(`Unrecognized builtin widget ${builtinWidget} for shortcut ${shortcut.noteId} "${shortcut.title}"`); console.warn(`Unrecognized builtin widget ${builtinWidget} for shortcut ${shortcut.noteId} "${shortcut.title}"`);
} }
} }
} else {
console.warn(`Unrecognized shortcut type ${shortcutType} for shortcut '${shortcut.noteId}' title ${shortcut.title}`);
} }
} }
catch (e) { catch (e) {

View File

@ -1566,15 +1566,15 @@ export default class NoteTreeWidget extends NoteContextAwareWidget {
} }
addWidgetShortcutCommand({node}) { addWidgetShortcutCommand({node}) {
this.createShortcutNote(node, 'widget'); this.createShortcutNote(node, 'customWidget');
} }
addSpacerShortcutCommand({node}) { addSpacerShortcutCommand({node}) {
this.createShortcutNote(node, 'spacer'); this.createShortcutNote(node, 'spacer');
} }
async createShortcutNote(node, type) { async createShortcutNote(node, shortcutType) {
const resp = await server.post(`special-notes/shortcuts/${node.data.noteId}/${type}`); const resp = await server.post(`special-notes/shortcuts/${node.data.noteId}/${shortcutType}`);
if (!resp.success) { if (!resp.success) {
alert(resp.message); alert(resp.message);

View File

@ -67,7 +67,7 @@ function getHoistedNote() {
} }
function createShortcut(req) { function createShortcut(req) {
return specialNotesService.createShortcut(req.params.parentNoteId, req.params.type); return specialNotesService.createShortcut(req.params.parentNoteId, req.params.shortcutType);
} }
function resetShortcut(req) { function resetShortcut(req) {

View File

@ -327,7 +327,7 @@ function register(app) {
apiRoute(POST, '/api/special-notes/search-note', specialNotesRoute.createSearchNote); apiRoute(POST, '/api/special-notes/search-note', specialNotesRoute.createSearchNote);
apiRoute(POST, '/api/special-notes/save-search-note', specialNotesRoute.saveSearchNote); apiRoute(POST, '/api/special-notes/save-search-note', specialNotesRoute.saveSearchNote);
apiRoute(POST, '/api/special-notes/shortcuts/:noteId/reset', specialNotesRoute.resetShortcut); apiRoute(POST, '/api/special-notes/shortcuts/:noteId/reset', specialNotesRoute.resetShortcut);
apiRoute(POST, '/api/special-notes/shortcuts/:parentNoteId/:type', specialNotesRoute.createShortcut); apiRoute(POST, '/api/special-notes/shortcuts/:parentNoteId/:shortcutType', specialNotesRoute.createShortcut);
// :filename is not used by trilium, but instead used for "save as" to assign a human-readable filename // :filename is not used by trilium, but instead used for "save as" to assign a human-readable filename
route(GET, '/api/images/:noteId/:filename', [auth.checkApiAuthOrElectron], imageRoute.returnImage); route(GET, '/api/images/:noteId/:filename', [auth.checkApiAuthOrElectron], imageRoute.returnImage);

View File

@ -6,6 +6,7 @@ const cls = require("./cls");
const dateUtils = require("./date_utils"); const dateUtils = require("./date_utils");
const LBTPL_ROOT = "lbtpl_root"; const LBTPL_ROOT = "lbtpl_root";
const LBTPL_COMMAND = "lbtpl_command";
const LBTPL_NOTE_SHORTCUT = "lbtpl_noteshortcut"; const LBTPL_NOTE_SHORTCUT = "lbtpl_noteshortcut";
const LBTPL_SCRIPT = "lbtpl_script"; const LBTPL_SCRIPT = "lbtpl_script";
const LBTPL_BUILTIN_WIDGET = "lbtpl_builtinwidget"; const LBTPL_BUILTIN_WIDGET = "lbtpl_builtinwidget";
@ -382,7 +383,7 @@ function createMissingSpecialNotes() {
} }
if (shortcut.command) { if (shortcut.command) {
note.addRelation('template', LBTPL_NOTE_SHORTCUT); note.addRelation('template', LBTPL_COMMAND);
note.addLabel('command', shortcut.command); note.addLabel('command', shortcut.command);
} else if (shortcut.builtinWidget) { } else if (shortcut.builtinWidget) {
if (shortcut.builtinWidget === 'spacer') { if (shortcut.builtinWidget === 'spacer') {
@ -409,17 +410,19 @@ function createMissingSpecialNotes() {
} }
} }
function createShortcut(parentNoteId, type) { function createShortcut(parentNoteId, shortcutType) {
let note; let note;
if (type === 'note') { if (shortcutType === 'note') {
note = noteService.createNewNote({ note = noteService.createNewNote({
title: "Note shortcut", title: "Note shortcut",
type: 'shortcut', type: 'shortcut',
content: '', content: '',
parentNoteId: parentNoteId parentNoteId: parentNoteId
}).note; }).note;
} else if (type === 'script') {
note.addRelation('template', LBTPL_NOTE_SHORTCUT);
} else if (shortcutType === 'script') {
note = noteService.createNewNote({ note = noteService.createNewNote({
title: "Script shortcut", title: "Script shortcut",
type: 'shortcut', type: 'shortcut',
@ -427,9 +430,8 @@ function createShortcut(parentNoteId, type) {
parentNoteId: parentNoteId parentNoteId: parentNoteId
}).note; }).note;
note.addLabel('relation:script', 'promoted'); note.addRelation('template', LBTPL_SCRIPT);
note.addLabel('docName', 'launchbar_script_shortcut'); } else if (shortcutType === 'customWidget') {
} else if (type === 'widget') {
note = noteService.createNewNote({ note = noteService.createNewNote({
title: "Widget shortcut", title: "Widget shortcut",
type: 'shortcut', type: 'shortcut',
@ -437,9 +439,8 @@ function createShortcut(parentNoteId, type) {
parentNoteId: parentNoteId parentNoteId: parentNoteId
}).note; }).note;
note.addLabel('relation:widget', 'promoted'); note.addRelation('template', LBTPL_CUSTOM_WIDGET);
note.addLabel('docName', 'launchbar_widget_shortcut'); } else if (shortcutType === 'spacer') {
} else if (type === 'spacer') {
note = noteService.createNewNote({ note = noteService.createNewNote({
title: "Spacer", title: "Spacer",
type: 'shortcut', type: 'shortcut',
@ -447,15 +448,9 @@ function createShortcut(parentNoteId, type) {
parentNoteId: parentNoteId parentNoteId: parentNoteId
}).note; }).note;
note.addLabel('builtinWidget', 'spacer'); note.addRelation('template', LBTPL_SPACER);
note.addLabel('iconClass', 'bx bx-move-vertical');
note.addLabel('label:baseSize', 'promoted,number');
note.addLabel('baseSize', '40');
note.addLabel('label:growthFactor', 'promoted,number');
note.addLabel('growthFactor', '0');
note.addLabel('docName', 'launchbar_spacer');
} else { } else {
throw new Error(`Unrecognized shortcut type ${type}`); throw new Error(`Unrecognized shortcut type ${shortcutType}`);
} }
return { return {
@ -476,6 +471,19 @@ function createShortcutTemplates() {
}); });
} }
if (!(LBTPL_COMMAND in becca.notes)) {
const tpl = noteService.createNewNote({
branchId: LBTPL_COMMAND,
noteId: LBTPL_COMMAND,
title: 'Command shortcut',
type: 'doc',
content: '',
parentNoteId: LBTPL_ROOT
}).note;
tpl.addLabel('shortcutType', 'command');
}
if (!(LBTPL_NOTE_SHORTCUT in becca.notes)) { if (!(LBTPL_NOTE_SHORTCUT in becca.notes)) {
const tpl = noteService.createNewNote({ const tpl = noteService.createNewNote({
branchId: LBTPL_NOTE_SHORTCUT, branchId: LBTPL_NOTE_SHORTCUT,
@ -549,7 +557,7 @@ function createShortcutTemplates() {
parentNoteId: LBTPL_ROOT parentNoteId: LBTPL_ROOT
}).note; }).note;
tpl.addLabel('shortcutType', 'builtinWidget'); tpl.addLabel('shortcutType', 'customWidget');
tpl.addLabel('relation:widget', 'promoted'); tpl.addLabel('relation:widget', 'promoted');
tpl.addLabel('docName', 'launchbar_widget_shortcut'); tpl.addLabel('docName', 'launchbar_widget_shortcut');
} }