@block-kit/react 1.0.20 → 1.0.22

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,11 +1,10 @@
1
1
  import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
2
  import { BLOCK_ID_KEY, BLOCK_KEY, EDITOR_EVENT, EDITOR_STATE } from "@block-kit/core";
3
3
  import { useMemoFn } from "@block-kit/utils/dist/es/hooks";
4
- import React, { useLayoutEffect, useMemo, useRef, useState } from "react";
4
+ import React, { useEffect, useLayoutEffect, useMemo, useRef, useState } from "react";
5
5
  import { withWrapLineNodes } from "../plugin/modules/wrap";
6
6
  import { rewriteRemoveChild } from "../utils/dirty-dom";
7
7
  import { JSX_TO_STATE } from "../utils/weak-map";
8
- import { PaintEffectModel } from "./effect";
9
8
  import { LineModel } from "./line";
10
9
  import { Placeholder } from "./ph";
11
10
  const BlockView = props => {
@@ -18,7 +17,6 @@ const BlockView = props => {
18
17
  const setModel = useMemoFn((ref) => {
19
18
  if (ref) {
20
19
  // ref 触发时机最早 ref -> layout effect -> effect
21
- // 需要保证 editor 稳定, 重渲染并不会触发执行, 导致 DOM 映射关系失效
22
20
  editor.model.setBlockModel(ref, state);
23
21
  rewriteRemoveChild(ref);
24
22
  }
@@ -48,6 +46,29 @@ const BlockView = props => {
48
46
  editor.event.off(EDITOR_EVENT.CONTENT_CHANGE, onContentChange);
49
47
  };
50
48
  }, [editor.event, onContentChange]);
49
+ /**
50
+ * 视图更新需要重新设置选区, 依赖于行状态变更
51
+ */
52
+ useLayoutEffect(() => {
53
+ const selection = editor.selection.get();
54
+ // 同步计算完成后更新浏览器选区, 等待 Paint
55
+ if (editor.state.isFocused() && selection) {
56
+ editor.logger.debug("UpdateDOMSelection");
57
+ editor.selection.updateDOMSelection(true);
58
+ }
59
+ }, [editor, lines]);
60
+ /**
61
+ * 视图更新需要触发视图绘制完成事件, 依赖于行状态变更
62
+ */
63
+ useEffect(() => {
64
+ // state -> parent -> node -> child ->|
65
+ // effect <- parent <- node <- child <-|
66
+ editor.logger.debug("OnPaint");
67
+ editor.state.set(EDITOR_STATE.PAINTING, false);
68
+ Promise.resolve().then(() => {
69
+ editor.event.trigger(EDITOR_EVENT.PAINT, {});
70
+ });
71
+ }, [editor, lines]);
51
72
  /**
52
73
  * 处理行节点
53
74
  */
@@ -72,7 +93,7 @@ const BlockView = props => {
72
93
  const children = useMemo(() => {
73
94
  return withWrapLineNodes(editor, elements);
74
95
  }, [editor, elements]);
75
- return (_jsxs("div", { [BLOCK_KEY]: true, [BLOCK_ID_KEY]: state.key, ref: setModel, children: [_jsx(PaintEffectModel, { editor: editor, lines: lines }), _jsx(Placeholder, { editor: editor, lines: lines, placeholder: props.placeholder }), children] }));
96
+ return (_jsxs("div", { [BLOCK_KEY]: true, [BLOCK_ID_KEY]: state.key, ref: setModel, children: [_jsx(Placeholder, { editor: editor, lines: lines, placeholder: props.placeholder }), children] }));
76
97
  };
77
98
  /** Block Model */
78
99
  export const BlockModel = React.memo(BlockView);
@@ -1,6 +1,7 @@
1
1
  import { jsx as _jsx } from "react/jsx-runtime";
2
2
  import { EDITOR_KEY, Point, Range } from "@block-kit/core";
3
3
  import { cs } from "@block-kit/utils";
4
+ import { useUpdateEffect } from "@block-kit/utils/dist/es/hooks";
4
5
  import { useEffect, useLayoutEffect, useRef } from "react";
5
6
  import { useEditorStatic } from "../hooks/use-editor";
6
7
  import { useReadonly } from "../hooks/use-readonly";
@@ -22,6 +23,10 @@ export const Editable = props => {
22
23
  !preventDestroy && editor.destroy();
23
24
  };
24
25
  }, [editor, preventDestroy]);
26
+ useUpdateEffect(() => {
27
+ // 需要保证 editor 实例稳定, 重渲染并不会触发 ref 执行, 导致 DOM 映射关系失效
28
+ editor.logger.warning("Confirm the uniqueness of the editor instance.", editor);
29
+ }, [editor]);
25
30
  useEffect(() => {
26
31
  // COMPAT: 这里有个奇怪的表现
27
32
  // 当自动聚焦时, 必须要先更新浏览器选区再聚焦
@@ -31,7 +31,6 @@ const react_1 = __importStar(require("react"));
31
31
  const wrap_1 = require("../plugin/modules/wrap");
32
32
  const dirty_dom_1 = require("../utils/dirty-dom");
33
33
  const weak_map_1 = require("../utils/weak-map");
34
- const effect_1 = require("./effect");
35
34
  const line_1 = require("./line");
36
35
  const ph_1 = require("./ph");
37
36
  const BlockView = props => {
@@ -44,7 +43,6 @@ const BlockView = props => {
44
43
  const setModel = (0, hooks_1.useMemoFn)((ref) => {
45
44
  if (ref) {
46
45
  // ref 触发时机最早 ref -> layout effect -> effect
47
- // 需要保证 editor 稳定, 重渲染并不会触发执行, 导致 DOM 映射关系失效
48
46
  editor.model.setBlockModel(ref, state);
49
47
  (0, dirty_dom_1.rewriteRemoveChild)(ref);
50
48
  }
@@ -74,6 +72,29 @@ const BlockView = props => {
74
72
  editor.event.off(core_1.EDITOR_EVENT.CONTENT_CHANGE, onContentChange);
75
73
  };
76
74
  }, [editor.event, onContentChange]);
75
+ /**
76
+ * 视图更新需要重新设置选区, 依赖于行状态变更
77
+ */
78
+ (0, react_1.useLayoutEffect)(() => {
79
+ const selection = editor.selection.get();
80
+ // 同步计算完成后更新浏览器选区, 等待 Paint
81
+ if (editor.state.isFocused() && selection) {
82
+ editor.logger.debug("UpdateDOMSelection");
83
+ editor.selection.updateDOMSelection(true);
84
+ }
85
+ }, [editor, lines]);
86
+ /**
87
+ * 视图更新需要触发视图绘制完成事件, 依赖于行状态变更
88
+ */
89
+ (0, react_1.useEffect)(() => {
90
+ // state -> parent -> node -> child ->|
91
+ // effect <- parent <- node <- child <-|
92
+ editor.logger.debug("OnPaint");
93
+ editor.state.set(core_1.EDITOR_STATE.PAINTING, false);
94
+ Promise.resolve().then(() => {
95
+ editor.event.trigger(core_1.EDITOR_EVENT.PAINT, {});
96
+ });
97
+ }, [editor, lines]);
77
98
  /**
78
99
  * 处理行节点
79
100
  */
@@ -98,7 +119,7 @@ const BlockView = props => {
98
119
  const children = (0, react_1.useMemo)(() => {
99
120
  return (0, wrap_1.withWrapLineNodes)(editor, elements);
100
121
  }, [editor, elements]);
101
- return ((0, jsx_runtime_1.jsxs)("div", { [core_1.BLOCK_KEY]: true, [core_1.BLOCK_ID_KEY]: state.key, ref: setModel, children: [(0, jsx_runtime_1.jsx)(effect_1.PaintEffectModel, { editor: editor, lines: lines }), (0, jsx_runtime_1.jsx)(ph_1.Placeholder, { editor: editor, lines: lines, placeholder: props.placeholder }), children] }));
122
+ return ((0, jsx_runtime_1.jsxs)("div", { [core_1.BLOCK_KEY]: true, [core_1.BLOCK_ID_KEY]: state.key, ref: setModel, children: [(0, jsx_runtime_1.jsx)(ph_1.Placeholder, { editor: editor, lines: lines, placeholder: props.placeholder }), children] }));
102
123
  };
103
124
  /** Block Model */
104
125
  exports.BlockModel = react_1.default.memo(BlockView);
@@ -4,6 +4,7 @@ exports.Editable = void 0;
4
4
  const jsx_runtime_1 = require("react/jsx-runtime");
5
5
  const core_1 = require("@block-kit/core");
6
6
  const utils_1 = require("@block-kit/utils");
7
+ const hooks_1 = require("@block-kit/utils/dist/es/hooks");
7
8
  const react_1 = require("react");
8
9
  const use_editor_1 = require("../hooks/use-editor");
9
10
  const use_readonly_1 = require("../hooks/use-readonly");
@@ -25,6 +26,10 @@ const Editable = props => {
25
26
  !preventDestroy && editor.destroy();
26
27
  };
27
28
  }, [editor, preventDestroy]);
29
+ (0, hooks_1.useUpdateEffect)(() => {
30
+ // 需要保证 editor 实例稳定, 重渲染并不会触发 ref 执行, 导致 DOM 映射关系失效
31
+ editor.logger.warning("Confirm the uniqueness of the editor instance.", editor);
32
+ }, [editor]);
28
33
  (0, react_1.useEffect)(() => {
29
34
  // COMPAT: 这里有个奇怪的表现
30
35
  // 当自动聚焦时, 必须要先更新浏览器选区再聚焦
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@block-kit/react",
3
- "version": "1.0.20",
3
+ "version": "1.0.22",
4
4
  "description": "",
5
5
  "main": "./dist/lib/index.js",
6
6
  "types": "./dist/es/index.d.ts",
@@ -39,9 +39,9 @@
39
39
  "react-dom": ">=17"
40
40
  },
41
41
  "dependencies": {
42
- "@block-kit/core": "1.0.20",
43
- "@block-kit/delta": "1.0.20",
44
- "@block-kit/utils": "1.0.20"
42
+ "@block-kit/core": "1.0.22",
43
+ "@block-kit/delta": "1.0.22",
44
+ "@block-kit/utils": "1.0.22"
45
45
  },
46
46
  "devDependencies": {
47
47
  "@babel/core": "7.20.12",
@@ -1,12 +0,0 @@
1
- import type { Editor, LineState } from "@block-kit/core";
2
- import React from "react";
3
- /**
4
- * 副作用依赖视图
5
- * - 父组件渲染会引发子组件渲染问题, memo 并未严格控制对比, 例如 ph 变更会导致重渲染
6
- * - 避免 memo 的渲染穿透问题, 参考 packages/react/test/render/effect.test.tsx
7
- * - 同样也可以直接将依赖即 [lines] 作为以来放置于 effect 中, 但抽离出独立组件能够更清晰
8
- */
9
- export declare const PaintEffectModel: React.NamedExoticComponent<{
10
- editor: Editor;
11
- lines: LineState[];
12
- }>;
@@ -1,36 +0,0 @@
1
- import { EDITOR_EVENT, EDITOR_STATE } from "@block-kit/core";
2
- import React, { useEffect, useLayoutEffect } from "react";
3
- const PaintEffectView = props => {
4
- const editor = props.editor;
5
- /**
6
- * 视图更新需要重新设置选区 无依赖数组
7
- */
8
- useLayoutEffect(() => {
9
- const selection = editor.selection.get();
10
- // 同步计算完成后更新浏览器选区, 等待 Paint
11
- if (editor.state.isFocused() && selection) {
12
- editor.logger.debug("UpdateDOMSelection");
13
- editor.selection.updateDOMSelection(true);
14
- }
15
- });
16
- /**
17
- * 视图更新需要触发视图绘制完成事件 无依赖数组
18
- * state -> parent -> node -> child ->|
19
- * effect <- parent <- node <- child <-|
20
- */
21
- useEffect(() => {
22
- editor.logger.debug("OnPaint");
23
- editor.state.set(EDITOR_STATE.PAINTING, false);
24
- Promise.resolve().then(() => {
25
- editor.event.trigger(EDITOR_EVENT.PAINT, {});
26
- });
27
- });
28
- return null;
29
- };
30
- /**
31
- * 副作用依赖视图
32
- * - 父组件渲染会引发子组件渲染问题, memo 并未严格控制对比, 例如 ph 变更会导致重渲染
33
- * - 避免 memo 的渲染穿透问题, 参考 packages/react/test/render/effect.test.tsx
34
- * - 同样也可以直接将依赖即 [lines] 作为以来放置于 effect 中, 但抽离出独立组件能够更清晰
35
- */
36
- export const PaintEffectModel = React.memo(PaintEffectView);
@@ -1,12 +0,0 @@
1
- import type { Editor, LineState } from "@block-kit/core";
2
- import React from "react";
3
- /**
4
- * 副作用依赖视图
5
- * - 父组件渲染会引发子组件渲染问题, memo 并未严格控制对比, 例如 ph 变更会导致重渲染
6
- * - 避免 memo 的渲染穿透问题, 参考 packages/react/test/render/effect.test.tsx
7
- * - 同样也可以直接将依赖即 [lines] 作为以来放置于 effect 中, 但抽离出独立组件能够更清晰
8
- */
9
- export declare const PaintEffectModel: React.NamedExoticComponent<{
10
- editor: Editor;
11
- lines: LineState[];
12
- }>;
@@ -1,62 +0,0 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
- var __importStar = (this && this.__importStar) || function (mod) {
19
- if (mod && mod.__esModule) return mod;
20
- var result = {};
21
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
- __setModuleDefault(result, mod);
23
- return result;
24
- };
25
- Object.defineProperty(exports, "__esModule", { value: true });
26
- exports.PaintEffectModel = void 0;
27
- const core_1 = require("@block-kit/core");
28
- const react_1 = __importStar(require("react"));
29
- const PaintEffectView = props => {
30
- const editor = props.editor;
31
- /**
32
- * 视图更新需要重新设置选区 无依赖数组
33
- */
34
- (0, react_1.useLayoutEffect)(() => {
35
- const selection = editor.selection.get();
36
- // 同步计算完成后更新浏览器选区, 等待 Paint
37
- if (editor.state.isFocused() && selection) {
38
- editor.logger.debug("UpdateDOMSelection");
39
- editor.selection.updateDOMSelection(true);
40
- }
41
- });
42
- /**
43
- * 视图更新需要触发视图绘制完成事件 无依赖数组
44
- * state -> parent -> node -> child ->|
45
- * effect <- parent <- node <- child <-|
46
- */
47
- (0, react_1.useEffect)(() => {
48
- editor.logger.debug("OnPaint");
49
- editor.state.set(core_1.EDITOR_STATE.PAINTING, false);
50
- Promise.resolve().then(() => {
51
- editor.event.trigger(core_1.EDITOR_EVENT.PAINT, {});
52
- });
53
- });
54
- return null;
55
- };
56
- /**
57
- * 副作用依赖视图
58
- * - 父组件渲染会引发子组件渲染问题, memo 并未严格控制对比, 例如 ph 变更会导致重渲染
59
- * - 避免 memo 的渲染穿透问题, 参考 packages/react/test/render/effect.test.tsx
60
- * - 同样也可以直接将依赖即 [lines] 作为以来放置于 effect 中, 但抽离出独立组件能够更清晰
61
- */
62
- exports.PaintEffectModel = react_1.default.memo(PaintEffectView);