@getoutline/y-prosemirror

中文文档
1.3.9发布于 2025/07/10MITTypeScriptESM + CJS近 30 天下载 2634

Yjs 的亲子约束

中文使用文档
文档目录4 节
  1. 特征
  2. 示例
  3. 撤消/ 重写
  4. 许可证

y- prosemir( 阳极)

表达器 装订 对 - 说吧 演示

此绑定图将 Y. Xml Fragment 映射到ProseMirror 状态 .

特征

  • 同步ProseMiror状态
  • 共享光标
  • 共享 Undo / 重做( 每个客户端都有自己的倒数/ 重做历史)
  • 当并行编辑导致文档计划无效时成功恢复

示例

import { ySyncPlugin, yCursorPlugin, yUndoPlugin, undo, redo, initProseMirrorDoc } from 'y-prosemirror'
import { exampleSetup } from 'prosemirror-example-setup'
import { keymap } from 'prosemirror-keymap'
..

const type = ydocument.get('prosemirror', Y.XmlFragment)
const { doc, mapping } = initProseMirrorDoc(type, schema)

const prosemirrorView = new EditorView(document.querySelector('#editor'), {
  state: EditorState.create({
    doc,
    schema,
    plugins: [
        ySyncPlugin(type, { mapping }),
        yCursorPlugin(provider.awareness),
        yUndoPlugin(),
        keymap({
          'Mod-z': undo,
          'Mod-y': redo,
          'Mod-Shift-z': redo
        })
      ].concat(exampleSetup({ schema }))
  })
})

还有看 这里 以实例为例.

远程光标

共享光标取决于大多数提供者导出的意识实例。 这个 认识协议 处理非永久数据,如用户数量、用户名、光标位置和颜色。 您可以像这样更改用户的名称和颜色 :

example.binding.awareness.setLocalStateField('user', { color: '#008833', name: 'My real name' })

为了渲染光标信息,您需要嵌入用户图标的自定义 CSS 。 这是一个模板,您可以用来设计光标信息.

/* this is a rough fix for the first cursor position when the first paragraph is empty */
.ProseMirror > .ProseMirror-yjs-cursor:first-child {
  margin-top: 16px;
}
.ProseMirror p:first-child, .ProseMirror h1:first-child, .ProseMirror h2:first-child, .ProseMirror h3:first-child, .ProseMirror h4:first-child, .ProseMirror h5:first-child, .ProseMirror h6:first-child {
  margin-top: 16px
}
/* This gives the remote user caret. The colors are automatically overwritten*/
.ProseMirror-yjs-cursor {
  position: relative;
  margin-left: -1px;
  margin-right: -1px;
  border-left: 1px solid black;
  border-right: 1px solid black;
  border-color: orange;
  word-break: normal;
  pointer-events: none;
}
/* This renders the username above the caret */
.ProseMirror-yjs-cursor > div {
  position: absolute;
  top: -1.05em;
  left: -1px;
  font-size: 13px;
  background-color: rgb(250, 129, 0);
  font-family: serif;
  font-style: normal;
  font-weight: normal;
  line-height: normal;
  user-select: none;
  color: white;
  padding-left: 2px;
  padding-right: 2px;
  white-space: nowrap;
}

您也可以通过在 yCursorPlugin 中指定光标构建器来覆盖默认的部件多姆

/**
 * This function receives the remote users "user" awareness state.
 */
export const myCursorBuilder = user => {
  const cursor = document.createElement('span')
  cursor.classList.add('ProseMirror-yjs-cursor')
  cursor.setAttribute('style', `border-color: ${user.color}`)
  const userDiv = document.createElement('div')
  userDiv.setAttribute('style', `background-color: ${user.color}`)
  userDiv.insertBefore(document.createTextNode(user.name), null)
  cursor.insertBefore(userDiv, null)
  return cursor
}

const prosemirrorView = new EditorView(document.querySelector('#editor'), {
  state: EditorState.create({
    schema,
    plugins: [
        ySyncPlugin(type),
        yCursorPlugin(provider.awareness, { cursorBuilder: myCursorBuilder }),
        yUndoPlugin(),
        keymap({
          'Mod-z': undo,
          'Mod-y': redo,
          'Mod-Shift-z': redo
        })
      ].concat(exampleSetup({ schema }))
  })
})

公用事业

软件包包括若干用于在 a Y.Doc和Prosemerr兼容数据结构。 这些可以有助于持续 用于数据存储或导入现有文档.

说明: 序列化和去序列化到 JSON 不会存储合作历史 步骤,因此不应用作主要储存。 你还需要 以存储 Y.Doc 二进制更新格式.

import { prosemirrorToYDoc } from 'y-prosemirror'

// Pass JSON previously output from Prosemirror
const doc = Node.fromJSON(schema, {
  type: "doc",
  content: [...]
})
const ydoc = prosemirrorToYDoc(doc)

因为JSON是常用的用具,所以有对应的方法可以跳过需要 创建Prosemerror节点.

import { prosemirrorJSONToYDoc } from 'y-prosemirror'

// Pass JSON previously output from Prosemirror
const ydoc = prosemirrorJSONToYDoc(schema, {
  type: "doc",
  content: [...]
})
import { yDocToProsemirror } from 'y-prosemirror'

// apply binary updates from elsewhere
const ydoc = new Y.Doc()
ydoc.applyUpdate(update)

const node = yDocToProsemirror(schema, ydoc)

因为JSON是常见的用例 输出JSON的等效方法 直接来说,这种方法不需要Prosemerror schema.

import { yDocToProsemirrorJSON } from 'y-prosemirror'

// apply binary updates from elsewhere
const ydoc = new Y.Doc()
ydoc.applyUpdate(update)

const node = yDocToProsemirrorJSON(ydoc)

撤消/ 重写

软件包导出 undo 财务报告和审定财务报表 redo 用于替换 亲子历史 绘制 mod-Z/Y 键 - 见 表达器 财务报告和审定财务报表 提示 实例.

撤消和重做被扩展到本地客户端, 所以一个对等点不会取消另一个对等点 变化。 见 Y. Undo 管理器 更多细节.

和前传一样,你可以设定交易的 addToHistory 元属性 来防止交易被反转。 这可能有助于方案 未由用户启动的文档更改 .

tr.setMeta("addToHistory", false);

许可证

麻省理工学院许可证 凯文·贾恩斯

版本与发布记录

@getoutline/y-prosemirror 共发布 5 个版本,首次发布于 2022/03/01;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.3.9latest2025/07/101.4 年前
1.3.82025/07/101.4 年前
1.3.72025/07/101.4 年前
1.0.182022/04/164 年前
1.0.162022/03/014 年前

关于 @getoutline/y-prosemirror 的常见问题

@getoutline/y-prosemirror 是什么?

Yjs 的亲子约束

如何安装 @getoutline/y-prosemirror?

在项目目录运行 npm install @getoutline/y-prosemirror,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:prosemirror-model(^1.7.1)、prosemirror-state(^1.2.3)、prosemirror-tables(^1.7.1)、prosemirror-view(^1.9.10)、y-protocols(^1.0.1)、yjs(^13.5.38)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@getoutline/y-prosemirror 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@getoutline/y-prosemirror 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@getoutline/y-prosemirror 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=16.0.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@getoutline/y-prosemirror 可以商用吗?

@getoutline/y-prosemirror 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@getoutline/y-prosemirror 的下载量有多少?

根据 npm 官方下载统计,@getoutline/y-prosemirror 在 2026/10/17 – 2026/11/15 这 30 天内被下载 2,634 次,日均约 88 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@getoutline/y-prosemirror 还在维护吗?

最新版本 1.3.9 发布于 2025/07/10(1.4 年前);至今共发布 5 个版本,其中过去 12 个月发布了 0 个;首次发布于 2022/03/01。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。