@lobehub/editor 1.24.0 → 1.26.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 (77) hide show
  1. package/README.md +9 -4
  2. package/es/editor-kernel/kernel.d.ts +3 -1
  3. package/es/editor-kernel/kernel.js +27 -8
  4. package/es/editor-kernel/react/useEditable.d.ts +3 -0
  5. package/es/editor-kernel/react/useEditable.js +32 -0
  6. package/es/index.d.ts +2 -0
  7. package/es/index.js +2 -0
  8. package/es/plugins/auto-complete/index.d.ts +2 -0
  9. package/es/plugins/auto-complete/index.js +2 -0
  10. package/es/plugins/auto-complete/node/placeholderNode.d.ts +30 -0
  11. package/es/plugins/auto-complete/node/placeholderNode.js +171 -0
  12. package/es/plugins/auto-complete/plugin/index.d.ts +17 -0
  13. package/es/plugins/auto-complete/plugin/index.js +398 -0
  14. package/es/plugins/auto-complete/react/ReactAutoCompletePlugin.d.ts +4 -0
  15. package/es/plugins/auto-complete/react/ReactAutoCompletePlugin.js +35 -0
  16. package/es/plugins/auto-complete/react/index.d.ts +1 -0
  17. package/es/plugins/auto-complete/react/index.js +1 -0
  18. package/es/plugins/auto-complete/react/style.d.ts +4 -0
  19. package/es/plugins/auto-complete/react/style.js +10 -0
  20. package/es/plugins/auto-complete/react/type.d.ts +11 -0
  21. package/es/plugins/auto-complete/react/type.js +1 -0
  22. package/es/plugins/common/plugin/index.js +0 -1
  23. package/es/plugins/common/plugin/register.js +3 -0
  24. package/es/plugins/common/react/ReactPlainText.js +9 -2
  25. package/es/plugins/common/react/style.js +1 -1
  26. package/es/plugins/common/react/type.d.ts +1 -0
  27. package/es/plugins/image/command/index.d.ts +2 -0
  28. package/es/plugins/image/command/index.js +10 -2
  29. package/es/plugins/image/node/basie-image-node.d.ts +1 -0
  30. package/es/plugins/image/node/basie-image-node.js +5 -0
  31. package/es/plugins/image/node/block-image-node.d.ts +31 -0
  32. package/es/plugins/image/node/block-image-node.js +209 -0
  33. package/es/plugins/image/node/image-node.d.ts +3 -0
  34. package/es/plugins/image/node/image-node.js +17 -0
  35. package/es/plugins/image/plugin/index.d.ts +4 -1
  36. package/es/plugins/image/plugin/index.js +51 -11
  37. package/es/plugins/image/react/ReactImagePlugin.js +3 -1
  38. package/es/plugins/image/react/components/Image.d.ts +4 -3
  39. package/es/plugins/image/react/components/Image.js +272 -24
  40. package/es/plugins/image/react/components/LazyImage.d.ts +7 -1
  41. package/es/plugins/image/react/components/LazyImage.js +11 -3
  42. package/es/plugins/image/react/components/style.d.ts +40 -0
  43. package/es/plugins/image/react/components/style.js +43 -0
  44. package/es/plugins/image/react/style.d.ts +10 -0
  45. package/es/plugins/image/react/style.js +10 -0
  46. package/es/plugins/image/react/type.d.ts +2 -0
  47. package/es/plugins/link/index.d.ts +1 -0
  48. package/es/plugins/link/index.js +2 -1
  49. package/es/plugins/link/plugin/index.js +3 -0
  50. package/es/plugins/link/react/ReactLinkPlugin.js +20 -2
  51. package/es/plugins/link/react/components/LinkEdit.js +4 -1
  52. package/es/plugins/link/react/components/LinkToolbar.d.ts +1 -0
  53. package/es/plugins/link/react/components/LinkToolbar.js +8 -2
  54. package/es/plugins/link/service/i-link-service.d.ts +11 -0
  55. package/es/plugins/link/service/i-link-service.js +46 -0
  56. package/es/plugins/markdown/service/shortcut.d.ts +7 -2
  57. package/es/plugins/markdown/service/shortcut.js +12 -1
  58. package/es/plugins/markdown/utils/index.d.ts +2 -0
  59. package/es/plugins/markdown/utils/index.js +4 -1
  60. package/es/plugins/toolbar/index.d.ts +1 -0
  61. package/es/plugins/toolbar/index.js +1 -0
  62. package/es/plugins/toolbar/react/index.d.ts +3 -0
  63. package/es/plugins/toolbar/react/index.js +80 -0
  64. package/es/plugins/toolbar/react/style.d.ts +1 -0
  65. package/es/plugins/toolbar/react/style.js +9 -0
  66. package/es/plugins/toolbar/react/type.d.ts +5 -0
  67. package/es/plugins/toolbar/react/type.js +1 -0
  68. package/es/plugins/toolbar/utils/getDOMRangeRect.d.ts +8 -0
  69. package/es/plugins/toolbar/utils/getDOMRangeRect.js +21 -0
  70. package/es/plugins/toolbar/utils/setFloatingElemPosition.d.ts +1 -0
  71. package/es/plugins/toolbar/utils/setFloatingElemPosition.js +54 -0
  72. package/es/react/ChatInput/style.js +3 -2
  73. package/es/react/Editor/Editor.js +2 -0
  74. package/es/react/Editor/type.d.ts +1 -0
  75. package/es/react/hooks/useEditorState/index.js +41 -33
  76. package/es/types/kernel.d.ts +14 -1
  77. package/package.json +1 -1
@@ -11,6 +11,7 @@ import { $createRangeSelection, $getNodeByKey, $getSelection, $isRangeSelection,
11
11
  import { EditIcon, ExternalLinkIcon, UnlinkIcon } from 'lucide-react';
12
12
  import { memo, useCallback, useRef, useState } from 'react';
13
13
  import { useLexicalEditor } from "../../../../editor-kernel/react";
14
+ import { useEditable } from "../../../../editor-kernel/react/useEditable";
14
15
  import { useTranslation } from "../../../../editor-kernel/react/useTranslation";
15
16
  import { getSelectedNode } from "../../utils";
16
17
  import { cleanPosition, updatePosition } from "../../../../utils/updatePosition";
@@ -19,7 +20,8 @@ import { useStyles } from "../style";
19
20
  import { EDIT_LINK_COMMAND } from "./LinkEdit";
20
21
  import { jsx as _jsx } from "react/jsx-runtime";
21
22
  var LinkToolbar = /*#__PURE__*/memo(function (_ref) {
22
- var editor = _ref.editor;
23
+ var editor = _ref.editor,
24
+ enable = _ref.enable;
23
25
  var divRef = useRef(null);
24
26
  var LinkRef = useRef(null);
25
27
  var _useStyles = useStyles(),
@@ -33,6 +35,8 @@ var LinkToolbar = /*#__PURE__*/memo(function (_ref) {
33
35
  });
34
36
  var t = useTranslation();
35
37
  var clearTimerRef = useRef(-1);
38
+ var _useEditable = useEditable(),
39
+ editable = _useEditable.editable;
36
40
  var handleEdit = useCallback(function () {
37
41
  if (!linkNode) return;
38
42
  editor.dispatchCommand(EDIT_LINK_COMMAND, {
@@ -90,6 +94,7 @@ var LinkToolbar = /*#__PURE__*/memo(function (_ref) {
90
94
  window.open(url, '_blank');
91
95
  }, [editor, linkNode]);
92
96
  useLexicalEditor(function (editor) {
97
+ if (!editable) return;
93
98
  return mergeRegister(editor.registerUpdateListener(function () {
94
99
  var selection = editor.read(function () {
95
100
  return $getSelection();
@@ -119,6 +124,7 @@ var LinkToolbar = /*#__PURE__*/memo(function (_ref) {
119
124
  state.current.isLink = false;
120
125
  }
121
126
  }), editor.registerCommand(HOVER_LINK_COMMAND, function (payload) {
127
+ if (!enable) return false;
122
128
  if (!payload.event.target || divRef.current === null) return false;
123
129
  // Cancel any pending hide timers when hovering a link again
124
130
  clearTimeout(clearTimerRef.current);
@@ -137,7 +143,7 @@ var LinkToolbar = /*#__PURE__*/memo(function (_ref) {
137
143
  clearTimerRef.current = setTimeout(handleCancel, 300);
138
144
  return true;
139
145
  }, COMMAND_PRIORITY_NORMAL));
140
- }, []);
146
+ }, [enable, editable]);
141
147
  return /*#__PURE__*/_jsx(ActionIconGroup, {
142
148
  className: styles.linkToolbar,
143
149
  items: [{
@@ -0,0 +1,11 @@
1
+ import EventEmitter from 'eventemitter3';
2
+ import { IServiceID } from "../../../types";
3
+ export interface ILinkService {
4
+ setLinkToolbar(enable: boolean): void;
5
+ }
6
+ export declare const ILinkService: IServiceID<ILinkService>;
7
+ export declare class LinkService extends EventEmitter<'linkToolbarChange'> implements ILinkService {
8
+ private _enableLinkToolbar;
9
+ get enableLinkToolbar(): boolean;
10
+ setLinkToolbar(enable: boolean): void;
11
+ }
@@ -0,0 +1,46 @@
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 _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
3
+ function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
4
+ function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
5
+ function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); }
6
+ function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
7
+ function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
8
+ function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); }
9
+ function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
10
+ function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
11
+ function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
12
+ 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; }
13
+ function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
14
+ 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); }
15
+ /* eslint-disable no-redeclare */
16
+ /* eslint-disable @typescript-eslint/no-redeclare */
17
+ import EventEmitter from 'eventemitter3';
18
+ import { genServiceId } from "../../../editor-kernel";
19
+ export var ILinkService = genServiceId('LinkService');
20
+ export var LinkService = /*#__PURE__*/function (_EventEmitter) {
21
+ _inherits(LinkService, _EventEmitter);
22
+ var _super = _createSuper(LinkService);
23
+ function LinkService() {
24
+ var _this;
25
+ _classCallCheck(this, LinkService);
26
+ for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
27
+ args[_key] = arguments[_key];
28
+ }
29
+ _this = _super.call.apply(_super, [this].concat(args));
30
+ _defineProperty(_assertThisInitialized(_this), "_enableLinkToolbar", true);
31
+ return _this;
32
+ }
33
+ _createClass(LinkService, [{
34
+ key: "enableLinkToolbar",
35
+ get: function get() {
36
+ return this._enableLinkToolbar;
37
+ }
38
+ }, {
39
+ key: "setLinkToolbar",
40
+ value: function setLinkToolbar(enable) {
41
+ this._enableLinkToolbar = enable;
42
+ this.emit('linkToolbarChange', enable);
43
+ }
44
+ }]);
45
+ return LinkService;
46
+ }(EventEmitter);
@@ -1,6 +1,7 @@
1
- import { ElementNode, LexicalNode, TextNode } from 'lexical';
1
+ import { BaseSelection, ElementNode, LexicalEditor, LexicalNode, TextNode } from 'lexical';
2
+ import { IRootNode } from "../../../editor-kernel/inode";
2
3
  import type { IEditorKernel, IServiceID } from "../../../types/kernel";
3
- import type { MarkdownReaderFunc, TransformerRecord, TransfromerRecordArray } from '../data-source/markdown/parse';
4
+ import { type MarkdownReaderFunc, type TransformerRecord, type TransfromerRecordArray } from '../data-source/markdown/parse';
4
5
  import type { Transformer } from './transformers';
5
6
  export interface IMarkdownWriterContext {
6
7
  /**
@@ -27,6 +28,8 @@ export declare const MARKDOWN_READER_LEVEL_HIGH = 1;
27
28
  export declare const MARKDOWN_READER_LEVEL_NORMAL = 2;
28
29
  export type MARKDOWN_READER_LEVEL = typeof MARKDOWN_READER_LEVEL_HIGH | typeof MARKDOWN_READER_LEVEL_NORMAL | typeof MARKDOWN_WRITER_LEVEL_MAX;
29
30
  export interface IMarkdownShortCutService {
31
+ insertIRootNode(editor: LexicalEditor, root: IRootNode, selection: BaseSelection): void;
32
+ parseMarkdownToLexical(markdown: string): IRootNode;
30
33
  /**
31
34
  * Register Markdown reader
32
35
  */
@@ -75,4 +78,6 @@ export declare class MarkdownShortCutService implements IMarkdownShortCutService
75
78
  runTransformers(parentNode: ElementNode, anchorNode: TextNode, anchorOffset: number, trigger?: 'enter'): boolean;
76
79
  registerMarkdownWriter(type: string, writer: (ctx: IMarkdownWriterContext, node: LexicalNode) => boolean | void): void;
77
80
  registerMarkdownReader<K extends keyof TransformerRecord>(type: K, reader: MarkdownReaderFunc<K>, level?: MARKDOWN_READER_LEVEL): void;
81
+ parseMarkdownToLexical(markdown: string): IRootNode;
82
+ insertIRootNode(editor: LexicalEditor, root: IRootNode, selection: BaseSelection): void;
78
83
  }
@@ -17,7 +17,8 @@ function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e
17
17
 
18
18
  import { genServiceId } from "../../../editor-kernel";
19
19
  import { createDebugLogger } from "../../../utils/debug";
20
- import { indexBy } from "../utils";
20
+ import { parseMarkdownToLexical as _parseMarkdownToLexical } from "../data-source/markdown/parse";
21
+ import { indexBy, insertIRootNode as _insertIRootNode } from "../utils";
21
22
  import { $runTextFormatTransformers, runElementTransformers, runTextMatchTransformers, testElementTransformers } from "./transformers";
22
23
  export var MARKDOWN_WRITER_LEVEL_MAX = 0;
23
24
  export var MARKDOWN_READER_LEVEL_HIGH = 1;
@@ -169,6 +170,16 @@ export var MarkdownShortCutService = /*#__PURE__*/function () {
169
170
  (_this$_markdownReader = this._markdownReaders[level][type]) === null || _this$_markdownReader === void 0 || _this$_markdownReader.push(reader);
170
171
  }
171
172
  }
173
+ }, {
174
+ key: "parseMarkdownToLexical",
175
+ value: function parseMarkdownToLexical(markdown) {
176
+ return _parseMarkdownToLexical(markdown, this.markdownReaders);
177
+ }
178
+ }, {
179
+ key: "insertIRootNode",
180
+ value: function insertIRootNode(editor, root, selection) {
181
+ _insertIRootNode(editor, root, selection);
182
+ }
172
183
  }]);
173
184
  return MarkdownShortCutService;
174
185
  }();
@@ -1,4 +1,5 @@
1
1
  import { BaseSelection, LexicalEditor, LexicalNode, TextNode } from 'lexical';
2
+ import { IRootNode } from "../../../editor-kernel/inode";
2
3
  /**
3
4
  * Returns true if the node can contain transformable markdown.
4
5
  * Code nodes cannot contain transformable markdown.
@@ -60,3 +61,4 @@ export declare function $addNodeStyle(node: TextNode): void;
60
61
  * @returns an Array of Lexical Node objects.
61
62
  */
62
63
  export declare function $generateNodesFromSerializedNodes(serializedNodes: Array<BaseSerializedNode>): Array<LexicalNode>;
64
+ export declare function insertIRootNode(editor: LexicalEditor, root: IRootNode, selection: BaseSelection): void;
@@ -6,7 +6,6 @@ function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol
6
6
  function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
7
7
  function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
8
8
  import { $caretFromPoint, $getCaretRange, $getChildCaret, $getRoot, $isElementNode, $isRangeSelection, $isTextNode, $isTextPointCaret, $parseSerializedNode, SELECTION_INSERT_CLIPBOARD_NODES_COMMAND } from 'lexical';
9
-
10
9
  /**
11
10
  * Returns true if the node can contain transformable markdown.
12
11
  * Code nodes cannot contain transformable markdown.
@@ -217,4 +216,8 @@ export function $generateNodesFromSerializedNodes(serializedNodes) {
217
216
  _iterator4.f();
218
217
  }
219
218
  return nodes;
219
+ }
220
+ export function insertIRootNode(editor, root, selection) {
221
+ var nodes = $generateNodesFromSerializedNodes(root.children);
222
+ $insertGeneratedNodes(editor, nodes, selection);
220
223
  }
@@ -0,0 +1 @@
1
+ export * from './react';
@@ -0,0 +1 @@
1
+ export * from "./react";
@@ -0,0 +1,3 @@
1
+ import { FC } from 'react';
2
+ import { ReactToolbarPluginProps } from './type';
3
+ export declare const ReactToolbarPlugin: FC<ReactToolbarPluginProps>;
@@ -0,0 +1,80 @@
1
+ function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
2
+ function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
3
+ function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
4
+ function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
5
+ function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
6
+ function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
7
+ import { mergeRegister } from '@lexical/utils';
8
+ import { Block } from '@lobehub/ui';
9
+ import { $getSelection, COMMAND_PRIORITY_LOW, SELECTION_CHANGE_COMMAND, getDOMSelection } from 'lexical';
10
+ import { useCallback, useRef } from 'react';
11
+ import { useLexicalComposerContext, useLexicalEditor } from "../../../editor-kernel/react";
12
+ import { ILinkService } from "../../link";
13
+ import { getDOMRangeRect } from "../utils/getDOMRangeRect";
14
+ import { setFloatingElemPosition } from "../utils/setFloatingElemPosition";
15
+ import { useStyles } from "./style";
16
+ import { jsx as _jsx } from "react/jsx-runtime";
17
+ export var ReactToolbarPlugin = function ReactToolbarPlugin(_ref) {
18
+ var className = _ref.className,
19
+ children = _ref.children;
20
+ var popupCharStylesEditorRef = useRef(null);
21
+ var anchorElemRef = useRef(null);
22
+ var _useLexicalComposerCo = useLexicalComposerContext(),
23
+ _useLexicalComposerCo2 = _slicedToArray(_useLexicalComposerCo, 1),
24
+ kernelEditor = _useLexicalComposerCo2[0];
25
+ var _useStyles = useStyles(),
26
+ cx = _useStyles.cx,
27
+ styles = _useStyles.styles;
28
+ var $updateTextFormatFloatingToolbar = useCallback(function (editor) {
29
+ if (!anchorElemRef.current) {
30
+ return;
31
+ }
32
+ var selection = $getSelection();
33
+ var popupCharStylesEditorElem = popupCharStylesEditorRef.current;
34
+ var nativeSelection = getDOMSelection(editor._window);
35
+ if (popupCharStylesEditorElem === null) {
36
+ return;
37
+ }
38
+ var rootElement = editor.getRootElement();
39
+ if (selection !== null && nativeSelection !== null && !nativeSelection.isCollapsed && rootElement !== null && rootElement.contains(nativeSelection.anchorNode)) {
40
+ var rangeRect = getDOMRangeRect(nativeSelection, rootElement);
41
+ setFloatingElemPosition(rangeRect, popupCharStylesEditorElem, anchorElemRef.current, false);
42
+ } else {
43
+ popupCharStylesEditorElem.style.opacity = '0';
44
+ popupCharStylesEditorElem.style.transform = 'translate(-10000px, -10000px)';
45
+ }
46
+ }, [anchorElemRef]);
47
+ useLexicalEditor(function () {
48
+ var service = kernelEditor.requireService(ILinkService);
49
+ if (service) {
50
+ service.setLinkToolbar(false);
51
+ return function () {
52
+ service.setLinkToolbar(true);
53
+ };
54
+ }
55
+ }, []);
56
+ useLexicalEditor(function (editor) {
57
+ return mergeRegister(editor.registerUpdateListener(function (_ref2) {
58
+ var editorState = _ref2.editorState;
59
+ editorState.read(function () {
60
+ $updateTextFormatFloatingToolbar(editor);
61
+ });
62
+ }), editor.registerCommand(SELECTION_CHANGE_COMMAND, function () {
63
+ $updateTextFormatFloatingToolbar(editor);
64
+ return false;
65
+ }, COMMAND_PRIORITY_LOW));
66
+ });
67
+ return /*#__PURE__*/_jsx("div", {
68
+ ref: anchorElemRef,
69
+ style: {
70
+ position: 'relative'
71
+ },
72
+ children: /*#__PURE__*/_jsx(Block, {
73
+ className: cx(styles, className),
74
+ padding: 4,
75
+ ref: popupCharStylesEditorRef,
76
+ variant: 'outlined',
77
+ children: children
78
+ })
79
+ });
80
+ };
@@ -0,0 +1 @@
1
+ export declare const useStyles: (props?: unknown) => import("antd-style").ReturnStyles<import("antd-style").SerializedStyles>;
@@ -0,0 +1,9 @@
1
+ var _templateObject;
2
+ function _taggedTemplateLiteral(strings, raw) { if (!raw) { raw = strings.slice(0); } return Object.freeze(Object.defineProperties(strings, { raw: { value: Object.freeze(raw) } })); }
3
+ import { createStyles } from 'antd-style';
4
+ export var useStyles = createStyles(function (_ref) {
5
+ var css = _ref.css,
6
+ token = _ref.token,
7
+ isDarkMode = _ref.isDarkMode;
8
+ return css(_templateObject || (_templateObject = _taggedTemplateLiteral(["\n will-change: transform;\n\n position: absolute;\n z-index: 10;\n inset-block-start: 0;\n inset-inline-start: 0;\n transform: translate(-10000px, -10000px);\n\n display: flex;\n\n border-color: ", ";\n\n vertical-align: middle;\n\n opacity: 0;\n background: ", ";\n box-shadow: ", ";\n\n transition: opacity 0.12s ", ";\n "])), token.colorFillSecondary, token.colorBgElevated, isDarkMode ? '0px 14px 28px -6px #0003,0px 2px 4px -1px #0000001f' : '0 14px 28px -6px #0000001a, 0 2px 4px -1px #0000000f', token.motionEaseOut);
9
+ });
@@ -0,0 +1,5 @@
1
+ import { ReactNode } from 'react';
2
+ export interface ReactToolbarPluginProps {
3
+ children?: ReactNode;
4
+ className?: string;
5
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ *
7
+ */
8
+ export declare function getDOMRangeRect(nativeSelection: Selection, rootElement: HTMLElement): DOMRect;
@@ -0,0 +1,21 @@
1
+ /**
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ *
7
+ */
8
+ export function getDOMRangeRect(nativeSelection, rootElement) {
9
+ var domRange = nativeSelection.getRangeAt(0);
10
+ var rect;
11
+ if (nativeSelection.anchorNode === rootElement) {
12
+ var inner = rootElement;
13
+ while (inner.firstElementChild !== null) {
14
+ inner = inner.firstElementChild;
15
+ }
16
+ rect = inner.getBoundingClientRect();
17
+ } else {
18
+ rect = domRange.getBoundingClientRect();
19
+ }
20
+ return rect;
21
+ }
@@ -0,0 +1 @@
1
+ export declare function setFloatingElemPosition(targetRect: DOMRect | null, floatingElem: HTMLElement, anchorElem: HTMLElement, isLink?: boolean, verticalGap?: number, horizontalOffset?: number): void;
@@ -0,0 +1,54 @@
1
+ /**
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ *
7
+ */
8
+ var VERTICAL_GAP = 4;
9
+ var HORIZONTAL_OFFSET = 0;
10
+ export function setFloatingElemPosition(targetRect, floatingElem, anchorElem) {
11
+ var isLink = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false;
12
+ var verticalGap = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : VERTICAL_GAP;
13
+ var horizontalOffset = arguments.length > 5 && arguments[5] !== undefined ? arguments[5] : HORIZONTAL_OFFSET;
14
+ var scrollerElem = anchorElem.parentElement;
15
+ if (targetRect === null || !scrollerElem) {
16
+ floatingElem.style.opacity = '0';
17
+ floatingElem.style.transform = 'translate(-10000px, -10000px)';
18
+ return;
19
+ }
20
+ var floatingElemRect = floatingElem.getBoundingClientRect();
21
+ var anchorElementRect = anchorElem.getBoundingClientRect();
22
+ var editorScrollerRect = scrollerElem.getBoundingClientRect();
23
+ var top = targetRect.top - floatingElemRect.height - verticalGap;
24
+ var left = targetRect.left - horizontalOffset;
25
+
26
+ // Check if text is end-aligned
27
+ var selection = window.getSelection();
28
+ if (selection && selection.rangeCount > 0) {
29
+ var range = selection.getRangeAt(0);
30
+ var textNode = range.startContainer;
31
+ if (textNode.nodeType === Node.ELEMENT_NODE || textNode.parentElement) {
32
+ var textElement = textNode.nodeType === Node.ELEMENT_NODE ? textNode : textNode.parentElement;
33
+ var textAlign = window.getComputedStyle(textElement).textAlign;
34
+ if (textAlign === 'right' || textAlign === 'end') {
35
+ // For end-aligned text, position the toolbar relative to the text end
36
+ left = targetRect.right - floatingElemRect.width + horizontalOffset;
37
+ }
38
+ }
39
+ }
40
+ if (top < editorScrollerRect.top) {
41
+ // adjusted height for link element if the element is at top
42
+ top += floatingElemRect.height + targetRect.height + verticalGap * (isLink ? 9 : 2);
43
+ }
44
+ if (left + floatingElemRect.width > editorScrollerRect.right) {
45
+ left = editorScrollerRect.right - floatingElemRect.width - horizontalOffset;
46
+ }
47
+ if (left < editorScrollerRect.left) {
48
+ left = editorScrollerRect.left + horizontalOffset;
49
+ }
50
+ top -= anchorElementRect.top + 12;
51
+ left -= anchorElementRect.left;
52
+ floatingElem.style.opacity = '1';
53
+ floatingElem.style.transform = "translate(".concat(left, "px, ").concat(top, "px)");
54
+ }
@@ -3,9 +3,10 @@ function _taggedTemplateLiteral(strings, raw) { if (!raw) { raw = strings.slice(
3
3
  import { createStyles } from 'antd-style';
4
4
  export var useStyles = createStyles(function (_ref) {
5
5
  var css = _ref.css,
6
- token = _ref.token;
6
+ token = _ref.token,
7
+ isDarkMode = _ref.isDarkMode;
7
8
  return {
8
- container: css(_templateObject || (_templateObject = _taggedTemplateLiteral(["\n position: relative;\n\n display: flex;\n flex-direction: column;\n\n height: 100%;\n border: 1px solid ", ";\n border-radius: ", "px;\n\n background-color: ", ";\n box-shadow:\n ", ",\n 0 32px 0 ", ";\n "])), token.colorBorderSecondary, token.borderRadiusLG, token.colorBgContainer, token.boxShadowTertiary, token.colorBgContainerSecondary),
9
+ container: css(_templateObject || (_templateObject = _taggedTemplateLiteral(["\n position: relative;\n\n display: flex;\n flex-direction: column;\n\n height: 100%;\n border: 1px solid ", ";\n border-radius: ", "px;\n\n background-color: ", ";\n box-shadow: ", ";\n "])), isDarkMode ? token.colorFillSecondary : token.colorFill, token.borderRadiusLG, token.colorBgElevated, isDarkMode ? '0px 4px 4px rgba(0, 0, 0, 0.4)' : '0px 4px 4px rgba(0, 0, 0, 0.04)'),
9
10
  editor: css(_templateObject2 || (_templateObject2 = _taggedTemplateLiteral(["\n cursor: text;\n\n overflow: hidden auto;\n flex: 1;\n\n width: 100%;\n padding-block: 8px 0;\n padding-inline: 12px;\n "]))),
10
11
  resizableContainer: css(_templateObject3 || (_templateObject3 = _taggedTemplateLiteral(["\n position: relative;\n\n display: flex;\n flex: 1 1 auto;\n flex-direction: column;\n align-self: flex-end;\n\n width: 100%;\n\n &:hover .resize-handle {\n opacity: 1;\n }\n "]))),
11
12
  resizeHandle: css(_templateObject4 || (_templateObject4 = _taggedTemplateLiteral(["\n position: absolute;\n inset-block-start: -4px;\n inset-inline-start: 50%;\n transform: translateX(-50%);\n\n width: 100%;\n height: 8px;\n\n opacity: 0;\n\n transition: opacity 0.2s ease-in-out;\n\n &::before {\n content: '';\n\n position: absolute;\n inset-block-start: 0;\n inset-inline-start: 50%;\n transform: translateX(-50%);\n\n width: 32px;\n height: 4px;\n border-radius: 4px;\n\n background-color: ", ";\n box-shadow: 0 1px 2px ", "20;\n }\n\n &:hover {\n opacity: 1 !important;\n\n &::before {\n background-color: ", ";\n box-shadow: 0 2px 4px ", "40;\n }\n }\n\n &:active {\n &::before {\n background-color: ", ";\n }\n }\n "])), token.colorPrimary, token.colorTextSecondary, token.colorPrimaryHover, token.colorTextSecondary, token.colorPrimaryActive)
@@ -28,6 +28,7 @@ var Editor = /*#__PURE__*/memo(function (_ref) {
28
28
  var content = _ref.content,
29
29
  style = _ref.style,
30
30
  className = _ref.className,
31
+ editable = _ref.editable,
31
32
  editor = _ref.editor,
32
33
  onInit = _ref.onInit,
33
34
  onChange = _ref.onChange,
@@ -99,6 +100,7 @@ var Editor = /*#__PURE__*/memo(function (_ref) {
99
100
  children: [memoPlugins, memoSlash, memoMention, /*#__PURE__*/_jsx(ReactPlainText, {
100
101
  autoFocus: autoFocus,
101
102
  className: className,
103
+ editable: editable,
102
104
  enablePasteMarkdown: enablePasteMarkdown,
103
105
  markdownOption: markdownOption,
104
106
  onBlur: onBlur,
@@ -11,6 +11,7 @@ export interface EditorProps extends Partial<ReactEditorContentProps>, Omit<Reac
11
11
  autoFocus?: boolean;
12
12
  children?: ReactNode;
13
13
  className?: string;
14
+ editable?: boolean;
14
15
  editor?: IEditor;
15
16
  /**
16
17
  * Enable automatic markdown formatting for pasted content
@@ -34,6 +34,7 @@ import { $findTopLevelElement, formatParagraph, getSelectedNode } from "./utils"
34
34
  * @returns Editor state and methods for toolbar functionality
35
35
  */
36
36
  export function useEditorState(editor) {
37
+ var _editor$isEditable;
37
38
  var _useState = useState(false),
38
39
  _useState2 = _slicedToArray(_useState, 2),
39
40
  canUndo = _useState2[0],
@@ -42,70 +43,76 @@ export function useEditorState(editor) {
42
43
  _useState4 = _slicedToArray(_useState3, 2),
43
44
  canRedo = _useState4[0],
44
45
  setCanRedo = _useState4[1];
45
- var _useState5 = useState(false),
46
+ var _useState5 = useState((_editor$isEditable = editor === null || editor === void 0 ? void 0 : editor.isEditable()) !== null && _editor$isEditable !== void 0 ? _editor$isEditable : true),
46
47
  _useState6 = _slicedToArray(_useState5, 2),
47
- isBold = _useState6[0],
48
- setIsBold = _useState6[1];
48
+ editable = _useState6[0],
49
+ setEditable = _useState6[1];
49
50
  var _useState7 = useState(false),
50
51
  _useState8 = _slicedToArray(_useState7, 2),
51
- isItalic = _useState8[0],
52
- setIsItalic = _useState8[1];
52
+ isBold = _useState8[0],
53
+ setIsBold = _useState8[1];
53
54
  var _useState9 = useState(false),
54
55
  _useState10 = _slicedToArray(_useState9, 2),
55
- isUnderline = _useState10[0],
56
- setIsUnderline = _useState10[1];
56
+ isItalic = _useState10[0],
57
+ setIsItalic = _useState10[1];
57
58
  var _useState11 = useState(false),
58
59
  _useState12 = _slicedToArray(_useState11, 2),
59
- isStrikethrough = _useState12[0],
60
- setIsStrikethrough = _useState12[1];
60
+ isUnderline = _useState12[0],
61
+ setIsUnderline = _useState12[1];
61
62
  var _useState13 = useState(false),
62
63
  _useState14 = _slicedToArray(_useState13, 2),
63
- isSubscript = _useState14[0],
64
- setIsSubscript = _useState14[1];
64
+ isStrikethrough = _useState14[0],
65
+ setIsStrikethrough = _useState14[1];
65
66
  var _useState15 = useState(false),
66
67
  _useState16 = _slicedToArray(_useState15, 2),
67
- isSuperscript = _useState16[0],
68
- setIsSuperscript = _useState16[1];
68
+ isSubscript = _useState16[0],
69
+ setIsSubscript = _useState16[1];
69
70
  var _useState17 = useState(false),
70
71
  _useState18 = _slicedToArray(_useState17, 2),
71
- isCode = _useState18[0],
72
- setIsCode = _useState18[1];
72
+ isSuperscript = _useState18[0],
73
+ setIsSuperscript = _useState18[1];
73
74
  var _useState19 = useState(false),
74
75
  _useState20 = _slicedToArray(_useState19, 2),
75
- isLink = _useState20[0],
76
- setIsLink = _useState20[1];
76
+ isCode = _useState20[0],
77
+ setIsCode = _useState20[1];
77
78
  var _useState21 = useState(false),
78
79
  _useState22 = _slicedToArray(_useState21, 2),
79
- isCodeblock = _useState22[0],
80
- setIsInCodeblok = _useState22[1];
80
+ isLink = _useState22[0],
81
+ setIsLink = _useState22[1];
81
82
  var _useState23 = useState(false),
82
83
  _useState24 = _slicedToArray(_useState23, 2),
83
- isBlockquote = _useState24[0],
84
- setIsInBlockquote = _useState24[1];
85
- var _useState25 = useState(null),
84
+ isCodeblock = _useState24[0],
85
+ setIsInCodeblok = _useState24[1];
86
+ var _useState25 = useState(false),
86
87
  _useState26 = _slicedToArray(_useState25, 2),
87
- codeblockLang = _useState26[0],
88
- setCodeblockLang = _useState26[1];
89
- var _useState27 = useState(true),
88
+ isBlockquote = _useState26[0],
89
+ setIsInBlockquote = _useState26[1];
90
+ var _useState27 = useState(null),
90
91
  _useState28 = _slicedToArray(_useState27, 2),
91
- isEmpty = _useState28[0],
92
- setIsEmpty = _useState28[1];
93
- var _useState29 = useState(false),
92
+ codeblockLang = _useState28[0],
93
+ setCodeblockLang = _useState28[1];
94
+ var _useState29 = useState(true),
94
95
  _useState30 = _slicedToArray(_useState29, 2),
95
- isSelected = _useState30[0],
96
- setIsSelected = _useState30[1];
97
- var _useState31 = useState(null),
96
+ isEmpty = _useState30[0],
97
+ setIsEmpty = _useState30[1];
98
+ var _useState31 = useState(false),
98
99
  _useState32 = _slicedToArray(_useState31, 2),
99
- blockType = _useState32[0],
100
- setBlockType = _useState32[1];
100
+ isSelected = _useState32[0],
101
+ setIsSelected = _useState32[1];
102
+ var _useState33 = useState(null),
103
+ _useState34 = _slicedToArray(_useState33, 2),
104
+ blockType = _useState34[0],
105
+ setBlockType = _useState34[1];
101
106
  var $handleHeadingNode = useCallback(function (selectedElement) {
102
107
  var type = $isHeadingNode(selectedElement) ? selectedElement.getTag() : selectedElement.getType();
103
108
  setBlockType(type);
104
109
  }, [setBlockType]);
105
110
  var $updateToolbar = useCallback(function () {
111
+ var _editor$isEditable2;
106
112
  var selection = $getSelection();
107
113
  var lexicalEditor = editor === null || editor === void 0 ? void 0 : editor.getLexicalEditor();
108
114
  setIsSelected(false);
115
+ setEditable((_editor$isEditable2 = editor === null || editor === void 0 ? void 0 : editor.isEditable()) !== null && _editor$isEditable2 !== void 0 ? _editor$isEditable2 : true);
109
116
  if (lexicalEditor) {
110
117
  setIsEmpty($isRootTextContentEmpty(lexicalEditor.isComposing(), false));
111
118
  }
@@ -417,6 +424,7 @@ export function useEditorState(editor) {
417
424
  code: code,
418
425
  codeblock: codeblock,
419
426
  codeblockLang: codeblockLang,
427
+ editable: editable,
420
428
  insertLink: insertLink,
421
429
  insertMath: insertMath,
422
430
  isBlockquote: isBlockquote,
@@ -14,6 +14,10 @@ export type IServiceID<Service> = {
14
14
  __serviceType?: Service;
15
15
  };
16
16
  export interface IKernelEventMap {
17
+ /**
18
+ * Editor editable state change event
19
+ */
20
+ editableChange: (editable: boolean) => void;
17
21
  /**
18
22
  * Editor error event
19
23
  */
@@ -84,6 +88,10 @@ export interface IEditor {
84
88
  * Get node editor instance
85
89
  */
86
90
  initNodeEditor(): LexicalEditor | null;
91
+ /**
92
+ * Check if editor is editable
93
+ */
94
+ isEditable(): boolean;
87
95
  /**
88
96
  * Check if editor content is empty
89
97
  * @returns true if editor content is empty, false otherwise
@@ -166,11 +174,16 @@ export interface IEditor {
166
174
  * @param content
167
175
  */
168
176
  setDocument(type: string, content: any): void;
177
+ /**
178
+ * Enable or disable editor editing capability
179
+ * @param editable
180
+ */
181
+ setEditable(editable: boolean): void;
169
182
  /**
170
183
  * Set document editor root node
171
184
  * @param dom
172
185
  */
173
- setRootElement(dom: HTMLElement): LexicalEditor;
186
+ setRootElement(dom: HTMLElement, editable?: boolean): LexicalEditor;
174
187
  /**
175
188
  * Get translation text
176
189
  * @param key Translation key
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lobehub/editor",
3
- "version": "1.24.0",
3
+ "version": "1.26.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",