keyboard handlers for tabs

This commit is contained in:
zadam 2020-01-20 20:51:22 +01:00
parent 9bc1f5af45
commit c5eac8f438
8 changed files with 57 additions and 49 deletions

18
package-lock.json generated
View File

@ -1,6 +1,6 @@
{ {
"name": "trilium", "name": "trilium",
"version": "0.40.0-beta", "version": "0.40.1",
"lockfileVersion": 1, "lockfileVersion": 1,
"requires": true, "requires": true,
"dependencies": { "dependencies": {
@ -3987,12 +3987,12 @@
} }
}, },
"file-type": { "file-type": {
"version": "13.1.0", "version": "13.1.1",
"resolved": "https://registry.npmjs.org/file-type/-/file-type-13.1.0.tgz", "resolved": "https://registry.npmjs.org/file-type/-/file-type-13.1.1.tgz",
"integrity": "sha512-nr4fSvwYSlQl7YmaWS8rsvDrAm6VgCeb2ysHh18+YBSH4RxewhPKUQrj2XRuEMBNnH6E4xw+yWTL7+jiMrh6GA==", "integrity": "sha512-HEb3tepyq8KzKSFEGMJSIxqn8uC1n3AM8OKME5+BIgq0bErRzcDPOdmnyPKtfjStSpIvuk0Rle8mvuG4824caQ==",
"requires": { "requires": {
"readable-web-to-node-stream": "^2.0.0", "readable-web-to-node-stream": "^2.0.0",
"strtok3": "^5.0.1", "strtok3": "^5.0.2",
"token-types": "^2.0.0", "token-types": "^2.0.0",
"typedarray-to-buffer": "^3.1.5" "typedarray-to-buffer": "^3.1.5"
} }
@ -9649,11 +9649,11 @@
} }
}, },
"strtok3": { "strtok3": {
"version": "5.0.1", "version": "5.0.2",
"resolved": "https://registry.npmjs.org/strtok3/-/strtok3-5.0.1.tgz", "resolved": "https://registry.npmjs.org/strtok3/-/strtok3-5.0.2.tgz",
"integrity": "sha512-AWliiIjyb87onqO8pM+1Hozm+PPcR4YYIWbFUT5OKQ+tOMwgdT8HwJd/IS8v3/gKdAtE5aE2p3FhcWqryuZPLQ==", "integrity": "sha512-EFeVpFC5qDsqPEJSrIYyS/ueFBknGhgSK9cW+YAJF/cgJG/KSjoK7X6rK5xnpcLe7y1LVkVFCXWbAb+ClNKzKQ==",
"requires": { "requires": {
"@tokenizer/token": "^0.1.0", "@tokenizer/token": "^0.1.1",
"debug": "^4.1.1", "debug": "^4.1.1",
"peek-readable": "^3.1.0" "peek-readable": "^3.1.0"
} }

View File

@ -37,7 +37,7 @@
"electron-window-state": "5.0.3", "electron-window-state": "5.0.3",
"express": "4.17.1", "express": "4.17.1",
"express-session": "1.17.0", "express-session": "1.17.0",
"file-type": "13.1.0", "file-type": "13.1.1",
"fs-extra": "8.1.0", "fs-extra": "8.1.0",
"helmet": "3.21.2", "helmet": "3.21.2",
"html": "1.0.0", "html": "1.0.0",

View File

@ -264,11 +264,17 @@ class AppContext {
} }
} }
async openEmptyTab() { async openAndActivateEmptyTab() {
const tabContext = this.openEmptyTab();
await this.activateTab(tabContext.tabId);
}
openEmptyTab() {
const tabContext = new TabContext(this, this.tabRow); const tabContext = new TabContext(this, this.tabRow);
this.tabContexts.push(tabContext); this.tabContexts.push(tabContext);
await this.activateTab(tabContext.tabId); return tabContext;
} }
async filterTabs(noteId) { async filterTabs(noteId) {
@ -279,7 +285,7 @@ class AppContext {
} }
if (this.tabContexts.length === 0) { if (this.tabContexts.length === 0) {
this.openEmptyTab() this.openAndActivateEmptyTab()
} }
await this.saveOpenTabs(); await this.saveOpenTabs();
@ -327,7 +333,7 @@ class AppContext {
} }
newTabListener() { newTabListener() {
this.openEmptyTab(); this.openAndActivateEmptyTab();
} }
async removeTab(tabId) { async removeTab(tabId) {
@ -339,11 +345,11 @@ class AppContext {
} }
if (this.tabContexts.length === 0) { if (this.tabContexts.length === 0) {
this.openEmptyTab(); this.openAndActivateEmptyTab();
} }
else { else {
const oldIdx = tabIdsInOrder.findIndex(tid => tid === tabId); const oldIdx = tabIdsInOrder.findIndex(tid => tid === tabId);
const newActiveTabId = tabIdsInOrder[oldIdx === tabIdsInOrder.length ? oldIdx - 1 : oldIdx + 1]; const newActiveTabId = tabIdsInOrder[oldIdx === tabIdsInOrder.length - 1 ? oldIdx - 1 : oldIdx + 1];
if (newActiveTabId) { if (newActiveTabId) {
this.activateTab(newActiveTabId); this.activateTab(newActiveTabId);
@ -378,6 +384,30 @@ class AppContext {
// run async // run async
bundleService.executeRelationBundles(activeTabContext.note, 'runOnNoteChange', activeTabContext); bundleService.executeRelationBundles(activeTabContext.note, 'runOnNoteChange', activeTabContext);
} }
activateNextTabListener() {
const tabIdsInOrder = this.tabRow.getTabIdsInOrder();
const oldIdx = tabIdsInOrder.findIndex(tid => tid === this.activeTabId);
const newActiveTabId = tabIdsInOrder[oldIdx === tabIdsInOrder.length - 1 ? 0 : oldIdx + 1];
this.activateTab(newActiveTabId);
}
activatePreviousTabListener() {
const tabIdsInOrder = this.tabRow.getTabIdsInOrder();
const oldIdx = tabIdsInOrder.findIndex(tid => tid === this.activeTabId);
const newActiveTabId = tabIdsInOrder[oldIdx === 0 ? tabIdsInOrder.length - 1 : oldIdx - 1];
this.activateTab(newActiveTabId);
}
closeActiveTabListener() {
this.removeTab(this.activeTabId);
}
openNewTabListener() {
this.openAndActivateEmptyTab();
}
} }
const appContext = new AppContext(); const appContext = new AppContext();

View File

@ -10,9 +10,9 @@ const keyboardActionsLoaded = server.get('keyboard-actions').then(actions => {
for (const shortcut of action.effectiveShortcuts || []) { for (const shortcut of action.effectiveShortcuts || []) {
if (shortcut && !shortcut.startsWith("global:")) { // global shortcuts should be handled in the electron code if (shortcut && !shortcut.startsWith("global:")) { // global shortcuts should be handled in the electron code
const eventName = action.actionName.charAt(0).toUpperCase() + action.actionName.slice(1); const eventName = action.actionName.charAt(0).toLowerCase() + action.actionName.slice(1);
utils.bindGlobalShortcut(shortcut, appContext.trigger(eventName)); utils.bindGlobalShortcut(shortcut, () => appContext.trigger(eventName));
} }
} }
} }
@ -103,34 +103,6 @@ function updateDisplayedShortcuts($container) {
$(() => updateDisplayedShortcuts($(document))); $(() => updateDisplayedShortcuts($(document)));
setGlobalActionHandler('OpenNewTab', () => {
appContext.openEmptyTab();
});
setGlobalActionHandler('CloseActiveTab', () => {
if (this.tabRow.activeTabEl) {
this.tabRow.removeTab(this.tabRow.activeTabId);
}
});
setGlobalActionHandler('ActivateNextTab', () => {
const nextTab = this.tabRow.nextTabEl;
if (nextTab) {
// FIXME
this.tabRow.activateTab(nextTab);
}
});
setGlobalActionHandler('ActivatePreviousTab', () => {
const prevTab = this.tabRow.previousTabEl;
if (prevTab) {
// FIXME
this.tabRow.activateTab(prevTab);
}
});
export default { export default {
setGlobalActionHandler, setGlobalActionHandler,
setElementActionHandler, setElementActionHandler,

View File

@ -31,6 +31,8 @@ class TabContext extends Component {
this.attributes = new Attributes(this.appContext, this); this.attributes = new Attributes(this.appContext, this);
this.children.push(this.attributes); this.children.push(this.attributes);
this.trigger('tabOpened', {tabId: this.tabId});
} }
async setNote(notePath) { async setNote(notePath) {

View File

@ -24,7 +24,7 @@ class AttributesWidget extends StandardWidget {
} }
async refreshWithNote() { async refreshWithNote() {
const attributes = await this.tabContext.attributes.getAttributes();console.log("attributes", attributes); const attributes = await this.tabContext.attributes.getAttributes();
const ownedAttributes = attributes.filter(attr => attr.noteId === this.tabContext.note.noteId); const ownedAttributes = attributes.filter(attr => attr.noteId === this.tabContext.note.noteId);
if (attributes.length === 0) { if (attributes.length === 0) {

View File

@ -64,7 +64,7 @@ class NoteInfoWidget extends StandardWidget {
} }
syncDataListener({data}) { syncDataListener({data}) {
if (data.find(sd => sd.entityName === 'notes' && sd.entityId === this.tabContext.note.noteId)) { if (data.find(sd => sd.entityName === 'notes' && this.isNote(sd.entityId))) {
this.refresh(); this.refresh();
} }
} }

View File

@ -473,6 +473,10 @@ export default class TabRowWidget extends BasicWidget {
tabEl.setAttribute('active', ''); tabEl.setAttribute('active', '');
} }
tabOpenedListener({tabId}) {
this.addTab(tabId);
}
removeTab(tabId) { removeTab(tabId) {
const tabEl = this.getTabById(tabId)[0]; const tabEl = this.getTabById(tabId)[0];
@ -595,7 +599,7 @@ export default class TabRowWidget extends BasicWidget {
if (destinationIndex < originIndex) { if (destinationIndex < originIndex) {
tabEl.parentNode.insertBefore(tabEl, this.tabEls[destinationIndex]); tabEl.parentNode.insertBefore(tabEl, this.tabEls[destinationIndex]);
} else { } else {
const beforeEl = this.tabEls[destinationIndex + 1] || this.$newTab; const beforeEl = this.tabEls[destinationIndex + 1] || this.$newTab[0];
tabEl.parentNode.insertBefore(tabEl, beforeEl); tabEl.parentNode.insertBefore(tabEl, beforeEl);
} }