v1.0.0.1
StocketBase
- (function (global) {
- 'use strict';
-
- const STORAGE_KEY = 'stocketbase_editor_shortcuts';
-
- const DEFAULT_SHORTCUTS = {
- bold: { label: 'Bold', combo: 'Ctrl+B' },
- italic: { label: 'Italic', combo: 'Ctrl+I' },
- underline: { label: 'Underline', combo: 'Ctrl+U' },
- strikethrough: { label: 'Strikethrough', combo: 'Ctrl+Shift+X' },
- add_block: { label: 'Add block below', combo: 'Ctrl+Enter' },
- remove_block: { label: 'Remove block', combo: 'Ctrl+Backspace' },
- move_block_up: { label: 'Move block up', combo: 'Ctrl+Shift+ArrowUp' },
- move_block_down: { label: 'Move block down', combo: 'Ctrl+Shift+ArrowDown' },
- save: { label: 'Save', combo: 'Ctrl+S' },
- };
-
- function loadOverrides() {
- try {
- const raw = window.localStorage.getItem(STORAGE_KEY);
- if (!raw) {
- return {};
- }
- const parsed = JSON.parse(raw);
- return parsed && typeof parsed === 'object' ? parsed : {};
- } catch (error) {
- return {};
- }
- }
-
- function saveOverrides(overrides) {
- try {
- window.localStorage.setItem(STORAGE_KEY, JSON.stringify(overrides));
- return true;
- } catch (error) {
- return false;
- }
- }
-
- function getShortcuts() {
- const overrides = loadOverrides();
- const result = {};
- Object.keys(DEFAULT_SHORTCUTS).forEach((key) => {
- result[key] = {
- label: DEFAULT_SHORTCUTS[key].label,
- combo: overrides[key] || DEFAULT_SHORTCUTS[key].combo,
- isDefault: !overrides[key],
- };
- });
- return result;
- }
-
- function setShortcut(action, combo) {
- if (!DEFAULT_SHORTCUTS[action]) {
- return false;
- }
- const overrides = loadOverrides();
- if (!combo || combo === DEFAULT_SHORTCUTS[action].combo) {
- delete overrides[action];
- } else {
- overrides[action] = combo;
- }
- return saveOverrides(overrides);
- }
-
- function resetShortcut(action) {
- const overrides = loadOverrides();
- delete overrides[action];
- return saveOverrides(overrides);
- }
-
- function resetAllShortcuts() {
- return saveOverrides({});
- }
-
- function comboFromEvent(event) {
- const parts = [];
- if (event.ctrlKey || event.metaKey) {
- parts.push('Ctrl');
- }
- if (event.altKey) {
- parts.push('Alt');
- }
- if (event.shiftKey) {
- parts.push('Shift');
- }
-
- let key = event.key;
- const ignoredKeys = ['Control', 'Alt', 'Shift', 'Meta'];
- if (ignoredKeys.indexOf(key) !== -1) {
- return null;
- }
-
- if (key === ' ') {
- key = 'Space';
- } else if (key.length === 1) {
- key = key.toUpperCase();
- }
-
- parts.push(key);
- return parts.join('+');
- }
-
- function matchesCombo(event, combo) {
- if (!combo) {
- return false;
- }
- const eventCombo = comboFromEvent(event);
- return eventCombo === combo;
- }
-
- function findMatchingAction(event, shortcuts) {
- const eventCombo = comboFromEvent(event);
- if (!eventCombo) {
- return null;
- }
- const keys = Object.keys(shortcuts);
- for (let i = 0; i < keys.length; i++) {
- if (shortcuts[keys[i]].combo === eventCombo) {
- return keys[i];
- }
- }
- return null;
- }
-
- global.StocketBaseShortcuts = {
- DEFAULT_SHORTCUTS: DEFAULT_SHORTCUTS,
- getShortcuts: getShortcuts,
- setShortcut: setShortcut,
- resetShortcut: resetShortcut,
- resetAllShortcuts: resetAllShortcuts,
- comboFromEvent: comboFromEvent,
- matchesCombo: matchesCombo,
- findMatchingAction: findMatchingAction,
- };
- })(window);
-