Skip to content

使用yjs ,editor无法正常渲染 #4804

Closed

Description

@Capyhw

Description

我发现官网上的示例代码也存在这个问题
https://platejs.org/docs/yjs#%E5%A4%9A%E6%96%87%E6%A1%A3%E5%A4%84%E7%90%86

Image

在我本地代码中,尝试把skipInitialization 注释掉,一切正常

Image

但是有一个额外的bug,不知道是我的写法问题还是什么,每次刷新,都会追加内容

PixPin_2025-12-25_18-47-54.mp4

Reproduction URL

No response

Reproduction steps

1. Go to '...'
2. Click on '....'
3. Scroll down to '....'
4. See error

Plate version

52.0.1

Slate React version

0.120.0

Screenshots

Logs

Browsers

Chrome

Activity

  1. dosubot commented on Dec 25, 2025

    @dosubot

    Hi @Capyhw! I'm Dosu and I’m helping the plate team.

    你遇到的问题是 Plate 和 Yjs 初始化时机冲突导致的。正确做法是:

    1. 创建编辑器时必须设置 skipInitialization: true,这样 Plate 不会自动初始化内容,避免和 Yjs 状态冲突。官方文档说明
    2. 在组件挂载后(比如 React 的 useEffect),手动调用 editor.getApi(YjsPlugin).yjs.init(),并传入文档 id 和初始内容。只有当 Yjs 文档是空的,初始内容才会被插入,否则会同步远端内容,不会重复追加。源码逻辑
    3. 组件卸载时记得调用 editor.getApi(YjsPlugin).yjs.destroy(),避免内存泄漏。
    4. 如果不设置 skipInitialization: true,Plate 会在 Yjs 内容还没同步时插入初始值,每次刷新都可能追加内容,导致重复。相关讨论
    5. 推荐的 React 代码模式如下:
    const editor = useMemo(() => createPlateEditor({
      plugins: [/* ... */],
      skipInitialization: true,
    }), []);
    
    useEffect(() => {
      // 等 Yjs provider ready 后再初始化
      editor.getApi(YjsPlugin).yjs.init({
        id: docId,
        value: initialValue,
      });
      return () => editor.getApi(YjsPlugin).yjs.destroy();
    }, [editor, docId, initialValue]);
    1. 如果遇到多余空段落,是因为 Plate 的规范化逻辑在 Yjs 内容还没同步时插入了空 block,这不是你的写法问题,是时机问题。可以在 Yjs 内容同步后过滤掉空段落,或者确保 Yjs provider 尽快同步内容。相关说明

    更多细节和例子可以参考 官方 Yjs 插件文档 和 协作示例。

    To reply, just mention @dosu.


    How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other

    Chat with Dosu Open in Cursor Join Discord Share on X

  2. felixfeng33 commented on Dec 31, 2025

    @felixfeng33
    Collaborator

    我花费较多时间,将potion中的bug修复了,platejs中的还没来得及修复,有时间会修复

  3. locked and limited conversation to collaborators on Dec 31, 2025
  4. converted this issue into a discussion #4805 on Dec 31, 2025
  5. added theissue type on Oct 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions