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);
许可证
麻省理工学院许可证 凯文·贾恩斯