@lobehub/editor 1.28.0 → 1.29.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.
@@ -1,4 +1,4 @@
1
- import { LexicalEditor, LexicalNode, NodeKey } from 'lexical';
1
+ import { ElementNode, LexicalEditor, LexicalNode, NodeKey } from 'lexical';
2
2
  import type { IEditorKernel, IServiceID } from "../types";
3
3
  export declare const DOM_ELEMENT_TYPE = 1;
4
4
  export declare const DOM_TEXT_TYPE = 3;
@@ -30,3 +30,5 @@ export declare function getKernelFromEditor(editor: LexicalEditor): IEditorKerne
30
30
  * @returns
31
31
  */
32
32
  export declare function compareNodeOrder(nodeA: LexicalNode, nodeB: LexicalNode): number;
33
+ export declare function closest(node: LexicalNode | ElementNode | null, test: (node: LexicalNode) => boolean): LexicalNode | null;
34
+ export declare function $closestNodeType(node: LexicalNode | ElementNode | null, type: string | string[]): LexicalNode | null;
@@ -108,4 +108,19 @@ export function compareNodeOrder(nodeA, nodeB) {
108
108
 
109
109
  // This case should not happen as we checked for equality at the start
110
110
  return 0;
111
+ }
112
+ export function closest(node, test) {
113
+ var current = node;
114
+ while (current) {
115
+ if (test(current)) {
116
+ return current;
117
+ }
118
+ current = current.getParent();
119
+ }
120
+ return null;
121
+ }
122
+ export function $closestNodeType(node, type) {
123
+ return closest(node, function (n) {
124
+ return Array.isArray(type) ? type.includes(n.getType()) : n.getType() === type;
125
+ });
111
126
  }
@@ -326,7 +326,7 @@ export function registerRichKeydown(editor, kernel, options) {
326
326
  return false;
327
327
  }, COMMAND_PRIORITY_EDITOR));
328
328
  }
329
- var NEEDS_FOLLOWING_PARAGRAPH_TYPES = new Set(['code', 'table']);
329
+ var NEEDS_FOLLOWING_PARAGRAPH_TYPES = new Set(['code', 'table', 'block-image']);
330
330
  export function registerLastElement(editor) {
331
331
  var isProcessing = false;
332
332
  return editor.registerUpdateListener(function (_ref3) {
@@ -7,6 +7,7 @@ function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
7
7
  import { mergeRegister } from '@lexical/utils';
8
8
  import { $getNodeByKey, $getSelection, $isBlockElementNode, $isRangeSelection } from 'lexical';
9
9
  import { memo, useRef, useState } from 'react';
10
+ import { $closestNodeType } from "../../../../editor-kernel";
10
11
  import { useLexicalEditor } from "../../../../editor-kernel/react";
11
12
  import { $canShowPlaceholderCurry } from "../../utils";
12
13
  import { useStyles } from "./style";
@@ -55,7 +56,8 @@ var Placeholder = /*#__PURE__*/memo(function (_ref) {
55
56
  while (node && !$isBlockElementNode(node)) {
56
57
  node = node.getParent();
57
58
  }
58
- if (node) {
59
+ var tableNode = $closestNodeType(node, ['tablecell', 'heading']);
60
+ if (node && !tableNode) {
59
61
  var dom = editor.getElementByKey(node.getKey());
60
62
  if (dom && hasOnlyBrChild(dom)) {
61
63
  if (currentPlaceHolderRef.current && currentPlaceHolderRef.current !== dom) {
@@ -7,4 +7,4 @@ export declare const INSERT_IMAGE_COMMAND: import("lexical").LexicalCommand<{
7
7
  }>;
8
8
  export declare function registerImageCommand(editor: LexicalEditor, handleUpload: (file: File) => Promise<{
9
9
  url: string;
10
- }>): () => void;
10
+ }>, defaultBlockImage?: boolean): () => void;
@@ -9,11 +9,13 @@ function isImageFile(file) {
9
9
  return file.type.startsWith('image/');
10
10
  }
11
11
  export function registerImageCommand(editor, handleUpload) {
12
+ var defaultBlockImage = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
12
13
  return editor.registerCommand(INSERT_IMAGE_COMMAND, function (payload) {
13
14
  var file = payload.file,
14
15
  range = payload.range,
15
16
  block = payload.block,
16
17
  maxWidth = payload.maxWidth;
18
+ var isBlock = block !== null && block !== void 0 ? block : defaultBlockImage;
17
19
  if (!isImageFile(file)) {
18
20
  return false; // Not an image file
19
21
  }
@@ -26,7 +28,7 @@ export function registerImageCommand(editor, handleUpload) {
26
28
  }
27
29
  $setSelection(rangeSelection);
28
30
  }
29
- var imageNode = block ? $createBlockImageNode({
31
+ var imageNode = isBlock ? $createBlockImageNode({
30
32
  altText: file.name,
31
33
  maxWidth: maxWidth || 800,
32
34
  src: placeholderURL
@@ -36,7 +38,7 @@ export function registerImageCommand(editor, handleUpload) {
36
38
  src: placeholderURL
37
39
  });
38
40
  $insertNodes([imageNode]); // Insert a zero-width space to ensure the image is not the last child
39
- if ($isRootOrShadowRoot(imageNode.getParentOrThrow())) {
41
+ if (!isBlock && $isRootOrShadowRoot(imageNode.getParentOrThrow())) {
40
42
  $wrapNodeInElement(imageNode, $createParagraphNode).selectEnd();
41
43
  }
42
44
  handleUpload(file).then(function (res) {
@@ -45,7 +45,8 @@ export var ImagePlugin = (_class = /*#__PURE__*/function (_KernelPlugin) {
45
45
  var _this$kernel$requireS,
46
46
  _this$kernel$requireS2,
47
47
  _this$kernel$requireS3,
48
- _this2 = this;
48
+ _this2 = this,
49
+ _this$config;
49
50
  (_this$kernel$requireS = this.kernel.requireService(IMarkdownShortCutService)) === null || _this$kernel$requireS === void 0 || _this$kernel$requireS.registerMarkdownWriter(ImageNode.getType(), function (ctx, node) {
50
51
  if ($isImageNode(node)) {
51
52
  ctx.appendLine("![".concat(node.altText, "](").concat(node.src, ")"));
@@ -83,7 +84,7 @@ export var ImagePlugin = (_class = /*#__PURE__*/function (_KernelPlugin) {
83
84
  return _ref.apply(this, arguments);
84
85
  };
85
86
  }(), UPLOAD_PRIORITY_HIGH);
86
- this.register(registerImageCommand(editor, this.config.handleUpload));
87
+ this.register(registerImageCommand(editor, this.config.handleUpload, (_this$config = this.config) === null || _this$config === void 0 ? void 0 : _this$config.defaultBlockImage));
87
88
  }
88
89
  }, {
89
90
  key: "getImageWidth",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lobehub/editor",
3
- "version": "1.28.0",
3
+ "version": "1.29.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",