WebOrbiton
v1.0.0.9

Publisium

137 lines · 3.6 KB
  1. (function (global) {
  2. 'use strict';
  3. const STORAGE_KEY = 'publisium_editor_shortcuts';
  4. const DEFAULT_SHORTCUTS = {
  5. bold: { label: 'Bold', combo: 'Ctrl+B' },
  6. italic: { label: 'Italic', combo: 'Ctrl+I' },
  7. underline: { label: 'Underline', combo: 'Ctrl+U' },
  8. strikethrough: { label: 'Strikethrough', combo: 'Ctrl+Shift+X' },
  9. add_block: { label: 'Add block below', combo: 'Ctrl+Enter' },
  10. remove_block: { label: 'Remove block', combo: 'Ctrl+Backspace' },
  11. move_block_up: { label: 'Move block up', combo: 'Ctrl+Shift+ArrowUp' },
  12. move_block_down: { label: 'Move block down', combo: 'Ctrl+Shift+ArrowDown' },
  13. save: { label: 'Save', combo: 'Ctrl+S' },
  14. };
  15. function loadOverrides() {
  16. try {
  17. const raw = window.localStorage.getItem(STORAGE_KEY);
  18. if (!raw) {
  19. return {};
  20. }
  21. const parsed = JSON.parse(raw);
  22. return parsed && typeof parsed === 'object' ? parsed : {};
  23. } catch (error) {
  24. return {};
  25. }
  26. }
  27. function saveOverrides(overrides) {
  28. try {
  29. window.localStorage.setItem(STORAGE_KEY, JSON.stringify(overrides));
  30. return true;
  31. } catch (error) {
  32. return false;
  33. }
  34. }
  35. function getShortcuts() {
  36. const overrides = loadOverrides();
  37. const result = {};
  38. Object.keys(DEFAULT_SHORTCUTS).forEach((key) => {
  39. result[key] = {
  40. label: DEFAULT_SHORTCUTS[key].label,
  41. combo: overrides[key] || DEFAULT_SHORTCUTS[key].combo,
  42. isDefault: !overrides[key],
  43. };
  44. });
  45. return result;
  46. }
  47. function setShortcut(action, combo) {
  48. if (!DEFAULT_SHORTCUTS[action]) {
  49. return false;
  50. }
  51. const overrides = loadOverrides();
  52. if (!combo || combo === DEFAULT_SHORTCUTS[action].combo) {
  53. delete overrides[action];
  54. } else {
  55. overrides[action] = combo;
  56. }
  57. return saveOverrides(overrides);
  58. }
  59. function resetShortcut(action) {
  60. const overrides = loadOverrides();
  61. delete overrides[action];
  62. return saveOverrides(overrides);
  63. }
  64. function resetAllShortcuts() {
  65. return saveOverrides({});
  66. }
  67. function comboFromEvent(event) {
  68. const parts = [];
  69. if (event.ctrlKey || event.metaKey) {
  70. parts.push('Ctrl');
  71. }
  72. if (event.altKey) {
  73. parts.push('Alt');
  74. }
  75. if (event.shiftKey) {
  76. parts.push('Shift');
  77. }
  78. let key = event.key;
  79. const ignoredKeys = ['Control', 'Alt', 'Shift', 'Meta'];
  80. if (ignoredKeys.indexOf(key) !== -1) {
  81. return null;
  82. }
  83. if (key === ' ') {
  84. key = 'Space';
  85. } else if (key.length === 1) {
  86. key = key.toUpperCase();
  87. }
  88. parts.push(key);
  89. return parts.join('+');
  90. }
  91. function matchesCombo(event, combo) {
  92. if (!combo) {
  93. return false;
  94. }
  95. const eventCombo = comboFromEvent(event);
  96. return eventCombo === combo;
  97. }
  98. function findMatchingAction(event, shortcuts) {
  99. const eventCombo = comboFromEvent(event);
  100. if (!eventCombo) {
  101. return null;
  102. }
  103. const keys = Object.keys(shortcuts);
  104. for (let i = 0; i < keys.length; i++) {
  105. if (shortcuts[keys[i]].combo === eventCombo) {
  106. return keys[i];
  107. }
  108. }
  109. return null;
  110. }
  111. global.PublisiumShortcuts = {
  112. DEFAULT_SHORTCUTS: DEFAULT_SHORTCUTS,
  113. getShortcuts: getShortcuts,
  114. setShortcut: setShortcut,
  115. resetShortcut: resetShortcut,
  116. resetAllShortcuts: resetAllShortcuts,
  117. comboFromEvent: comboFromEvent,
  118. matchesCombo: matchesCombo,
  119. findMatchingAction: findMatchingAction,
  120. };
  121. })(window);
  122. ​