@lobehub/editor 1.5.9 → 1.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (61) hide show
  1. package/es/common/sys.d.ts +2 -0
  2. package/es/common/sys.js +5 -0
  3. package/es/const/hotkey.d.ts +3 -0
  4. package/es/const/hotkey.js +58 -0
  5. package/es/editor-kernel/kernel.d.ts +3 -0
  6. package/es/editor-kernel/kernel.js +20 -1
  7. package/es/index.d.ts +2 -0
  8. package/es/index.js +4 -0
  9. package/es/plugins/code/plugin/index.d.ts +1 -0
  10. package/es/plugins/code/plugin/index.js +7 -6
  11. package/es/plugins/code/plugin/registry.d.ts +4 -1
  12. package/es/plugins/code/plugin/registry.js +15 -13
  13. package/es/plugins/code/react/CodeReactPlugin.js +5 -2
  14. package/es/plugins/code/react/type.d.ts +1 -0
  15. package/es/plugins/common/plugin/index.d.ts +1 -0
  16. package/es/plugins/common/plugin/index.js +4 -1
  17. package/es/plugins/common/plugin/register.d.ts +4 -1
  18. package/es/plugins/common/plugin/register.js +43 -14
  19. package/es/plugins/common/react/ReactPlainText.js +6 -3
  20. package/es/plugins/common/react/type.d.ts +1 -0
  21. package/es/plugins/link/node/LinkNode.js +1 -3
  22. package/es/plugins/link/plugin/index.d.ts +4 -0
  23. package/es/plugins/link/plugin/index.js +11 -1
  24. package/es/plugins/link/plugin/registry.d.ts +9 -0
  25. package/es/plugins/link/plugin/registry.js +108 -0
  26. package/es/plugins/link/react/ReactLinkPlugin.js +23 -50
  27. package/es/plugins/link/react/components/LinkEdit.js +41 -21
  28. package/es/plugins/link/react/components/LinkToolbar.js +58 -24
  29. package/es/plugins/link/react/type.d.ts +1 -0
  30. package/es/plugins/link/utils/index.d.ts +5 -0
  31. package/es/plugins/link/utils/index.js +14 -0
  32. package/es/plugins/list/plugin/index.d.ts +1 -0
  33. package/es/plugins/list/plugin/index.js +9 -76
  34. package/es/plugins/list/plugin/registry.d.ts +6 -0
  35. package/es/plugins/list/plugin/registry.js +98 -0
  36. package/es/plugins/list/react/ReactListPlugin.js +5 -2
  37. package/es/plugins/list/react/type.d.ts +1 -0
  38. package/es/plugins/math/react/component/MathEditor.js +41 -5
  39. package/es/plugins/math/react/component/MathEditorContainer.js +1 -0
  40. package/es/plugins/math/react/component/MathEditorContent.d.ts +2 -0
  41. package/es/plugins/math/react/component/MathEditorContent.js +7 -3
  42. package/es/plugins/mention/command/index.d.ts +1 -1
  43. package/es/plugins/mention/command/index.js +2 -2
  44. package/es/plugins/mention/node/MentionNode.d.ts +5 -5
  45. package/es/plugins/mention/node/MentionNode.js +11 -11
  46. package/es/plugins/slash/react/ReactSlashPlugin.js +17 -15
  47. package/es/plugins/slash/service/i-slash-service.d.ts +2 -1
  48. package/es/react/ChatInputActions/components/ActionItem.js +8 -2
  49. package/es/react/ChatInputActions/components/ActionRender.js +10 -3
  50. package/es/react/ChatInputActions/type.d.ts +2 -1
  51. package/es/react/hooks/useEditorState/index.js +44 -8
  52. package/es/types/hotkey.d.ts +71 -0
  53. package/es/types/hotkey.js +71 -0
  54. package/es/types/kernel.d.ts +9 -0
  55. package/es/utils/hotkey/isHotkeyMatch.d.ts +1 -0
  56. package/es/utils/hotkey/isHotkeyMatch.js +9 -0
  57. package/es/utils/hotkey/parseHotkeys.d.ts +6 -0
  58. package/es/utils/hotkey/parseHotkeys.js +42 -0
  59. package/es/utils/hotkey/registerHotkey.d.ts +15 -0
  60. package/es/utils/hotkey/registerHotkey.js +32 -0
  61. package/package.json +2 -1
@@ -1,5 +1,7 @@
1
1
  import type { CommandListener, CommandListenerPriority, CommandPayloadType, DecoratorNode, LexicalCommand, LexicalEditor, LexicalNodeConfig } from 'lexical';
2
2
  import type DataSource from "../editor-kernel/data-source";
3
+ import { HotkeyId } from "./hotkey";
4
+ import { HotkeyOptions, HotkeysEvent } from "../utils/hotkey/registerHotkey";
3
5
  import { ILocaleKeys } from './locale';
4
6
  export type Commands = Map<LexicalCommand<unknown>, Array<Set<CommandListener<unknown>>>>;
5
7
  export type CommandsClean = Map<LexicalCommand<unknown>, () => void>;
@@ -120,6 +122,13 @@ export interface IEditor {
120
122
  * @returns A teardown function to clean up the listener.
121
123
  */
122
124
  registerHighCommand<P>(command: LexicalCommand<P>, listener: CommandListener<P>, priority: CommandListenerPriority): () => void;
125
+ /**
126
+ * Register keyboard shortcut
127
+ * @param hotkey
128
+ * @param callback
129
+ * @param options
130
+ */
131
+ registerHotkey(hotkey: HotkeyId, callback: (event: KeyboardEvent, handler: HotkeysEvent) => void, options?: HotkeyOptions): () => void;
123
132
  /**
124
133
  * Register internationalization text
125
134
  * @param locale Internationalization text object
@@ -0,0 +1 @@
1
+ export declare const isHotkeyMatch: (event: KeyboardEvent, hotkey: string) => boolean;
@@ -0,0 +1,9 @@
1
+ import { isModifierMatch } from 'lexical';
2
+ import { mapCode, parseHotkey } from "./parseHotkeys";
3
+ export var isHotkeyMatch = function isHotkeyMatch(event, hotkey) {
4
+ var keys = parseHotkey(hotkey);
5
+ var modifierMatch = isModifierMatch(event, keys.modifiers);
6
+ if (!modifierMatch) return false;
7
+ if (mapCode(event.code) !== keys.key) return false;
8
+ return true;
9
+ };
@@ -0,0 +1,6 @@
1
+ import type { ModifierCombination } from "../../types/hotkey";
2
+ export declare function mapCode(key: string): string;
3
+ export declare function parseHotkey(hotkey: string): {
4
+ key?: string;
5
+ modifiers: ModifierCombination;
6
+ };
@@ -0,0 +1,42 @@
1
+ import { isApple } from "../../common/sys";
2
+ var reservedModifierKeywords = new Set(['shift', 'alt', 'meta', 'mod', 'ctrl', 'control']);
3
+ var mappedKeys = {
4
+ AltLeft: 'alt',
5
+ AltRight: 'alt',
6
+ ControlLeft: 'ctrl',
7
+ ControlRight: 'ctrl',
8
+ MetaLeft: 'meta',
9
+ MetaRight: 'meta',
10
+ OSLeft: 'meta',
11
+ OSRight: 'meta',
12
+ ShiftLeft: 'shift',
13
+ ShiftRight: 'shift',
14
+ down: 'arrowdown',
15
+ esc: 'escape',
16
+ left: 'arrowleft',
17
+ return: 'enter',
18
+ right: 'arrowright',
19
+ up: 'arrowup'
20
+ };
21
+ export function mapCode(key) {
22
+ return (mappedKeys[key.trim()] || key.trim()).toLowerCase().replace(/key|digit|numpad/, '');
23
+ }
24
+ export function parseHotkey(hotkey) {
25
+ var keys = [];
26
+ keys = hotkey.toLocaleLowerCase().split('+').map(function (k) {
27
+ return mapCode(k);
28
+ });
29
+ var modifiers = {
30
+ altKey: keys.includes('alt'),
31
+ ctrlKey: keys.includes('ctrl') || keys.includes('control') || (!isApple ? keys.includes('mod') : false),
32
+ metaKey: keys.includes('meta') || (isApple ? keys.includes('mod') : false),
33
+ shiftKey: keys.includes('shift')
34
+ };
35
+ var singleCharKeys = keys.find(function (k) {
36
+ return !reservedModifierKeywords.has(k);
37
+ });
38
+ return {
39
+ key: singleCharKeys,
40
+ modifiers: modifiers
41
+ };
42
+ }
@@ -0,0 +1,15 @@
1
+ import { HotkeyId, HotkeyItem, ModifierCombination } from "../../types/hotkey";
2
+ export type HotkeysEvent = {
3
+ key?: string;
4
+ keys: string;
5
+ modifiers: ModifierCombination;
6
+ scopes?: string[];
7
+ };
8
+ export type HotkeyOptions = {
9
+ enabled?: boolean;
10
+ preventDefault?: boolean;
11
+ stopImmediatePropagation?: boolean;
12
+ stopPropagation?: boolean;
13
+ };
14
+ export declare const registerHotkey: (hotkey: HotkeyItem, callback: (event: KeyboardEvent, handler: HotkeysEvent) => void, options?: HotkeyOptions) => (e: KeyboardEvent) => boolean;
15
+ export declare const getHotkeyById: (id: HotkeyId) => HotkeyItem;
@@ -0,0 +1,32 @@
1
+ function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
2
+ function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
3
+ function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
4
+ function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
5
+ function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
6
+ function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
7
+ import { HOTKEYS_REGISTRATION } from "../../const/hotkey";
8
+ import { createDebugLogger } from "../debug";
9
+ import { parseHotkey } from "./parseHotkeys";
10
+ import { isHotkeyMatch } from "./isHotkeyMatch";
11
+ var logger = createDebugLogger('hotkey');
12
+ export var registerHotkey = function registerHotkey(hotkey, callback) {
13
+ var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
14
+ return function (e) {
15
+ if (!isHotkeyMatch(e, hotkey.keys)) return false;
16
+ var keys = parseHotkey(hotkey.keys);
17
+ if (options.preventDefault) e.preventDefault();
18
+ if (options.stopPropagation) e.stopPropagation();
19
+ callback(e, _objectSpread(_objectSpread({
20
+ keys: hotkey.keys
21
+ }, keys), {}, {
22
+ scopes: hotkey.scopes
23
+ }));
24
+ logger.debug("\u2328\uFE0F Hotkey matched: ".concat(hotkey.id, " [").concat(hotkey.keys, "]"), hotkey);
25
+ return true;
26
+ };
27
+ };
28
+ export var getHotkeyById = function getHotkeyById(id) {
29
+ return HOTKEYS_REGISTRATION.find(function (hotkey) {
30
+ return hotkey.id === id;
31
+ });
32
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lobehub/editor",
3
- "version": "1.5.9",
3
+ "version": "1.6.0",
4
4
  "description": "A powerful and extensible rich text editor built on Meta's Lexical framework, providing a modern editing experience with React integration.",
5
5
  "keywords": [
6
6
  "lobehub",
@@ -56,6 +56,7 @@
56
56
  "react-layout-kit": "^2.0.0",
57
57
  "react-merge-refs": "^3.0.2",
58
58
  "shiki": "^3.9.2",
59
+ "ts-key-enum": "^3.0.13",
59
60
  "use-merge-value": "^1.2.0"
60
61
  },
61
62
  "peerDependencies": {