Skip to content

EspoCRM – Open Source CRM Application

shortcuts dev

Yuri Kuznetsova8c5048079f8
diff --git a/client/modules/crm/src/views/calendar/calendar-page.js b/client/modules/crm/src/views/calendar/calendar-page.js
index 606b3e81c5..b910bb727e 100644
--- a/client/modules/crm/src/views/calendar/calendar-page.js
+++ b/client/modules/crm/src/views/calendar/calendar-page.js
@@ -28,6 +28,8 @@
import View from 'view';
import CalendarEditViewModal from 'crm:views/calendar/modals/edit-view';
+import {inject} from 'di';
+import {ShortcutManager} from 'helpers/site/shortcut-manager';
class CalendarPage extends View {
@@ -55,6 +57,13 @@ class CalendarPage extends View {
},
}
+ /**
+ * @private
+ * @type {ShortcutManager}
+ */
+ @inject(ShortcutManager)
+ shortcutManager
+
/**
* A shortcut-key => action map.
*
@@ -87,6 +96,14 @@ class CalendarPage extends View {
this.handleShortcutKeyArrowRight(e);
},
/** @this CalendarPage */
+ 'Control+ArrowLeft': function (e) {
+ this.handleShortcutKeyArrowLeft(e);
+ },
+ /** @this CalendarPage */
+ 'Control+ArrowRight': function (e) {
+ this.handleShortcutKeyArrowRight(e);
+ },
+ /** @this CalendarPage */
'Minus': function (e) {
this.handleShortcutKeyMinus(e);
},
@@ -160,13 +177,11 @@ class CalendarPage extends View {
}
}
- this.events['keydown.main'] = e => {
- const key = Espo.Utils.getKeyFromKeyEvent(e);
+ this.shortcutManager.add(this, this.shortcutKeys);
- if (typeof this.shortcutKeys[key] === 'function') {
- this.shortcutKeys[key].call(this, e.originalEvent);
- }
- }
+ this.once('remove', () => {
+ this.shortcutManager.remove(this);
+ });
if (!this.mode || ~this.fullCalendarModeList.indexOf(this.mode) || this.mode.indexOf('view-') === 0) {
this.setupCalendar();
diff --git a/client/src/helpers/site/shortcut-manager.js b/client/src/helpers/site/shortcut-manager.js
new file mode 100644
index 0000000000..18a917452d
--- /dev/null
+++ b/client/src/helpers/site/shortcut-manager.js
@@ -0,0 +1,146 @@
+/************************************************************************
+ * This file is part of EspoCRM.
+ *
+ * EspoCRM – Open Source CRM application.
+ * Copyright (C) 2014-2025 Yurii Kuznietsov, Taras Machyshyn, Oleksii Avramenko
+ * Website: https://www.espocrm.com
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ *
+ * The interactive user interfaces in modified source and object code versions
+ * of this program must display Appropriate Legal Notices, as required under
+ * Section 5 of the GNU Affero General Public License version 3.
+ *
+ * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
+ * these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
+ ************************************************************************/
+
+import {register} from 'di';
+
+/** @typedef {import('view').default} View */
+/** @typedef {string|function(KeyboardEvent): void} Key */
+
+@register()
+export class ShortcutManager {
+
+ /**
+ * @private
+ * @type {number}
+ */
+ level = 0
+
+ /**
+ * @private
+ * @type {{
+ * view: View[],
+ * keys: Record.<string, Key>,
+ * level: number,
+ * }[]}
+ */
+ items
+
+ constructor() {
+ this.items = [];
+
+ document.addEventListener('keydown', event => this.handle(event));
+ }
+
+ /**
+ * Add a view and keys.
+ *
+ * @param {import('view').default} view
+ * @param {Record.<string, Key>} keys
+ * @param {{stack: boolean}} [options]
+ */
+ add(view, keys, options = {}) {
+ if (this.items.find(it => it.view === view)) {
+ return;
+ }
+
+ if (options.stack) {
+ this.level ++;
+ }
+
+ this.items.push({
+ view: view,
+ keys: keys,
+ level: this.level,
+ });
+ }
+
+ /**
+ * Remove a view.
+ *
+ * @param {import('view').default} view
+ */
+ remove(view) {
+ const index = this.items.findIndex(it => it.view === view);
+
+ if (index < 0) {
+ return;
+ }
+
+ this.items.splice(index, 1);
+
+ let maxLevel = 0;
+
+ for (const item of this.items) {
+ if (item.level > maxLevel) {
+ maxLevel = item.level;
+ }
+ }
+
+ this.level = maxLevel;
+ }
+
+ /**
+ * Handle.
+ *
+ * @param {KeyboardEvent} event
+ */
+ handle(event) {
+ const items = this.items.filter(it => it.level === this.level);
+
+ if (items.length === 0) {
+ return;
+ }
+
+ const key = Espo.Utils.getKeyFromKeyEvent(event);
+
+ for (const item of items) {
+ const subject = item.keys[key];
+
+ if (!subject) {
+ continue;
+ }
+
+ if (typeof subject === 'function') {
+ subject.call(item.view, event);
+
+ break;
+ }
+
+ event.preventDefault();
+ event.stopPropagation();
+
+ const methodName = 'action' + Espo.Utils.upperCaseFirst(subject);
+
+ if (typeof item.view[methodName] === 'function') {
+ item.view[methodName]();
+
+ break;
+ }
+ }
+ }
+}
diff --git a/client/src/views/detail.js b/client/src/views/detail.js
index a2c3c36f30..c36ef5bdc8 100644
--- a/client/src/views/detail.js
+++ b/client/src/views/detail.js
@@ -755,6 +755,20 @@ class DetailView extends MainView {
return 'detail';
}
+
+ setupReuse(params) {
+ const recordView = this.getRecordView();
+
+ if (!recordView) {
+ return;
+ }
+
+ if (!recordView.setupReuse) {
+ return;
+ }
+
+ recordView.setupReuse();
+ }
}
export default DetailView;
diff --git a/client/src/views/edit.js b/client/src/views/edit.js
index 66a9ea5b6a..7b89f5ddfb 100644
--- a/client/src/views/edit.js
+++ b/client/src/views/edit.js
@@ -238,6 +238,20 @@ class EditView extends MainView {
this.setPageTitle(title);
}
+
+ setupReuse(params) {
+ const recordView = this.getRecordView();
+
+ if (!recordView) {
+ return;
+ }
+
+ if (!recordView.setupReuse) {
+ return;
+ }
+
+ recordView.setupReuse();
+ }
}
export default EditView;
diff --git a/client/src/views/email/list.js b/client/src/views/email/list.js
index 4f54b46754..feaaceb2b6 100644
--- a/client/src/views/email/list.js
+++ b/client/src/views/email/list.js
@@ -404,6 +404,8 @@ class EmailListView extends ListView {
/** @inheritDoc */
setupReuse(params) {
+ super.setupReuse(params);
+
this.applyRoutingParams(params);
this.initDroppable();
this.initStickableFolders();
diff --git a/client/src/views/list-with-categories.js b/client/src/views/list-with-categories.js
index 562630488f..be06e60d9d 100644
--- a/client/src/views/list-with-categories.js
+++ b/client/src/views/list-with-categories.js
@@ -166,6 +166,8 @@ class ListWithCategories extends ListView {
* @inheritDoc
*/
setupReuse(params) {
+ super.setupReuse(params);
+
this.applyRoutingParams(params);
}
diff --git a/client/src/views/main.js b/client/src/views/main.js
index 77747de280..1c9ace86ce 100644
--- a/client/src/views/main.js
+++ b/client/src/views/main.js
@@ -29,6 +29,8 @@
/** @module views/main */
import View from 'view';
+import {inject} from 'di';
+import {ShortcutManager} from 'helpers/site/shortcut-manager';
/**
* A base main view. The detail, edit, list views to be extended from.
@@ -110,6 +112,13 @@ class MainView extends View {
*/
shortcutKeys = null
+ /**
+ * @private
+ * @type {ShortcutManager}
+ */
+ @inject(ShortcutManager)
+ shortcutManager
+
/** @inheritDoc */
events = {
/** @this MainView */
@@ -236,37 +245,23 @@ class MainView extends View {
}
}
- setupFinal() {
- if (this.shortcutKeys) {
- this.events['keydown.main'] = e => {
- const key = Espo.Utils.getKeyFromKeyEvent(e);
-
- if (typeof this.shortcutKeys[key] === 'function') {
- this.shortcutKeys[key].call(this, e.originalEvent);
-
- return;
- }
-
- const actionName = this.shortcutKeys[key];
-
- if (!actionName) {
- return;
- }
-
- e.preventDefault();
- e.stopPropagation();
-
- const methodName = 'action' + Espo.Utils.upperCaseFirst(actionName);
+ /**
+ * @private
+ */
+ initShortcuts() {
+ if (!this.shortcutKeys) {
+ return;
+ }
- if (typeof this[methodName] === 'function') {
- this[methodName]();
+ this.shortcutManager.add(this, this.shortcutKeys);
- return;
- }
+ this.once('remove', () => {
+ this.shortcutManager.remove(this);
+ });
+ }
- this[actionName]();
- };
- }
+ setupFinal() {
+ this.initShortcuts();
}
/**
@@ -804,7 +799,9 @@ class MainView extends View {
* @public
* @param {Object.<string, *>} params Routing params.
*/
- setupReuse(params) {}
+ setupReuse(params) {
+ this.initShortcuts();
+ }
}
export default MainView;
diff --git a/client/src/views/modal.js b/client/src/views/modal.js
index 14be9847aa..e903d1c627 100644
--- a/client/src/views/modal.js
+++ b/client/src/views/modal.js
@@ -31,6 +31,7 @@
import View from 'view';
import {inject} from 'di';
import ModalBarProvider from 'helpers/site/modal-bar-provider';
+import {ShortcutManager} from 'helpers/site/shortcut-manager';
/**
* A base modal view. Can be extended or used directly.
@@ -297,6 +298,13 @@ class ModalView extends View {
@inject(ModalBarProvider)
modalBarProvider
+ /**
+ * @private
+ * @type {ShortcutManager}
+ */
+ @inject(ShortcutManager)
+ shortcutManager
+
/**
* @inheritDoc
*/
@@ -426,6 +434,8 @@ class ModalView extends View {
if (this.getParentView()) {
this.getParentView().trigger('modal-shown');
}
+
+ this.initShortcuts();
});
this.once('remove', () => {
@@ -443,41 +453,23 @@ class ModalView extends View {
this.on('after:expand', () => this.afterExpand());
}
- setupFinal() {
- if (this.shortcutKeys) {
- this.events['keydown.modal-base'] = e => {
- const key = Espo.Utils.getKeyFromKeyEvent(e);
-
- if (typeof this.shortcutKeys[key] === 'function') {
- this.shortcutKeys[key].call(this, e.originalEvent);
-
- return;
- }
-
- const actionName = this.shortcutKeys[key];
-
- if (!actionName) {
- return;
- }
-
- if (this.hasActionItem(actionName) && !this.hasAvailableActionItem(actionName)) {
- return;
- }
-
- e.preventDefault();
- e.stopPropagation();
-
- const methodName = 'action' + Espo.Utils.upperCaseFirst(actionName);
+ /**
+ * @private
+ */
+ initShortcuts() {
+ if (!this.shortcutKeys) {
+ return;
+ }
- if (typeof this[methodName] === 'function') {
- this[methodName]();
+ this.shortcutManager.add(this, this.shortcutKeys, {stack: true});
- return;
- }
+ this.once('remove', () => {
+ this.shortcutManager.remove(this);
+ });
+ }
- this[actionName]();
- };
- }
+ setupFinal() {
+ this.initShortcuts();
}
/**
@@ -640,6 +632,8 @@ class ModalView extends View {
this.trigger('close');
this.remove();
}
+
+ this.shortcutManager.remove(this);
}
/**
diff --git a/client/src/views/record/detail.js b/client/src/views/record/detail.js
index d485f885bd..e5293edee8 100644
--- a/client/src/views/record/detail.js
+++ b/client/src/views/record/detail.js
@@ -34,6 +34,8 @@ import ActionItemSetup from 'helpers/action-item-setup';
import StickyBarHelper from 'helpers/record/misc/sticky-bar';
import SelectTemplateModalView from 'views/modals/select-template';
import DebounceHelper from 'helpers/util/debounce';
+import {inject} from 'di';
+import {ShortcutManager} from 'helpers/site/shortcut-manager';
/**
* A detail record view.
@@ -69,6 +71,13 @@ class DetailRecordView extends BaseRecordView {
* @property {boolean} [shortcutKeysEnabled] Enable shortcut keys.
*/
+ /**
+ * @private
+ * @type {ShortcutManager}
+ */
+ @inject(ShortcutManager)
+ shortcutManager
+
/**
* @param {module:views/record/detail~options | Object.<string, *>} options Options.
*/
@@ -2263,39 +2272,23 @@ class DetailRecordView extends BaseRecordView {
}
}
- setupFinal() {
- this.build();
-
+ /**
+ * @private
+ */
+ initShortcuts() {
if (this.shortcutKeys && this.options.shortcutKeysEnabled) {
- this.events['keydown.record-detail'] = e => {
- const key = Espo.Utils.getKeyFromKeyEvent(e);
-
- if (typeof this.shortcutKeys[key] === 'function') {
- this.shortcutKeys[key].call(this, e.originalEvent);
-
- return;
- }
-
- const actionName = this.shortcutKeys[key];
-
- if (!actionName) {
- return;
- }
+ this.shortcutManager.add(this, this.shortcutKeys);
- e.preventDefault();
- e.stopPropagation();
-
- const methodName = 'action' + Espo.Utils.upperCaseFirst(actionName);
-
- if (typeof this[methodName] === 'function') {
- this[methodName]();
+ this.once('remove', () => {
+ this.shortcutManager.remove(this);
+ });
+ }
+ }
- return;
- }
+ setupFinal() {
+ this.build();
- this[actionName]();
- };
- }
+ this.initShortcuts();
if (!this.options.focusForCreate) {
this.once('after:render', () => this.focusOnFirstDiv());
@@ -4265,6 +4258,14 @@ class DetailRecordView extends BaseRecordView {
getMode() {
return this.mode;
}
+
+ /**
+ * @internal
+ * @since 9.2.0
+ */
+ setupReuse() {
+ this.initShortcuts();
+ }
}
export default DetailRecordView;