为你的编辑器添加离线支持
Collaboration
通过使用 Y IndexedDB 扩展,轻松为你的协作编辑器添加离线功能。这个来自 Y.js 生态系统的工具增强了你的编辑器,具备离线数据存储和同步能力。
集成离线支持
首先,将 Y IndexedDB 适配器添加到你的项目中:
npm install y-indexeddb将 Y Indexeddb 与 Y 文档连接,以便本地存储。
import { Editor } from '@tiptap/core'
import Collaboration from '@tiptap/extension-collaboration'
import * as Y from 'yjs'
import { IndexeddbPersistence } from 'y-indexeddb'
const ydoc = new Y.Doc()
// 设置 IndexedDB 用于本地存储 Y 文档
new IndexeddbPersistence('example-document', ydoc)
const editor = new Editor({
extensions: [
// 其他扩展...
Collaboration.configure({
document: ydoc,
}),
],
})IndexedDB 适配器确保文档的每一次更改都被存储在浏览器的本地。这意味着即使你关闭标签页、失去网络连接或离线编辑,你的工作都会被保存。当你重新联网时,它会自动同步这些更改。