mirror of
https://github.com/hcengineering/platform.git
synced 2025-04-30 04:05:40 +00:00
UBERF-9353 Fix markup to ydoc conversion (#7884)
This commit is contained in:
parent
d81d7f3f5d
commit
624f3a1cdb
1
.vscode/launch.json
vendored
1
.vscode/launch.json
vendored
@ -257,6 +257,7 @@
|
||||
"MINIO_SECRET_KEY": "minioadmin",
|
||||
"MINIO_ENDPOINT": "localhost"
|
||||
},
|
||||
"runtimeVersion": "20",
|
||||
"runtimeArgs": ["--nolazy", "-r", "ts-node/register"],
|
||||
"sourceMaps": true,
|
||||
"cwd": "${workspaceRoot}/pods/collaborator",
|
||||
|
@ -14,7 +14,8 @@
|
||||
//
|
||||
|
||||
import { Markup } from '@hcengineering/core'
|
||||
import { markupToYDoc, markupToYDocNoSchema, yDocToMarkup } from '../ydoc'
|
||||
import { markupToJSON } from '@hcengineering/text'
|
||||
import { jsonToYDocNoSchema, markupToYDoc, markupToYDocNoSchema, yDocToMarkup } from '../ydoc'
|
||||
|
||||
describe('ydoc', () => {
|
||||
const markups: Markup[] = [
|
||||
@ -60,4 +61,14 @@ describe('ydoc', () => {
|
||||
expect(JSON.parse(back)).toEqual(JSON.parse(markup))
|
||||
})
|
||||
})
|
||||
|
||||
describe.each(markups)('jsonToYDocNoSchema', (markup) => {
|
||||
it('converts json to ydoc', () => {
|
||||
const json = markupToJSON(markup)
|
||||
const ydoc1 = jsonToYDocNoSchema(json, 'test')
|
||||
const ydoc2 = markupToYDoc(markup, 'test')
|
||||
|
||||
expect(ydoc1.getXmlFragment('test').toJSON()).toEqual(ydoc2.getXmlFragment('test').toJSON())
|
||||
})
|
||||
})
|
||||
})
|
||||
|
@ -17,7 +17,14 @@ import { generateId, Markup } from '@hcengineering/core'
|
||||
import { Extensions, getSchema } from '@tiptap/core'
|
||||
import { Node, Schema } from '@tiptap/pm/model'
|
||||
import { prosemirrorJSONToYDoc, prosemirrorToYDoc, yDocToProsemirrorJSON } from 'y-prosemirror'
|
||||
import { Doc as YDoc, applyUpdate, encodeStateAsUpdate, XmlElement as YXmlElement, XmlText as YXmlText } from 'yjs'
|
||||
import {
|
||||
Doc as YDoc,
|
||||
XmlElement as YXmlElement,
|
||||
XmlFragment as YXmlFragment,
|
||||
XmlText as YXmlText,
|
||||
applyUpdate,
|
||||
encodeStateAsUpdate
|
||||
} from 'yjs'
|
||||
import { defaultExtensions, jsonToMarkup, markupToJSON, markupToPmNode, type MarkupNode } from '@hcengineering/text'
|
||||
|
||||
const defaultSchema = getSchema(defaultExtensions)
|
||||
@ -45,13 +52,11 @@ export function markupToYDocNoSchema (markup: Markup, field: string): YDoc {
|
||||
* @public
|
||||
*/
|
||||
export function jsonToYDocNoSchema (json: MarkupNode, field: string): YDoc {
|
||||
const nodes = json.type === 'doc' ? json.content ?? [] : [json]
|
||||
const content = nodes.map(nodeToYXmlElement)
|
||||
|
||||
const ydoc = new YDoc({ guid: generateId() })
|
||||
|
||||
const fragment = ydoc.getXmlFragment(field)
|
||||
fragment.push(content)
|
||||
|
||||
const nodes = json.type === 'doc' ? json.content ?? [] : [json]
|
||||
nodes.map((p) => nodeToYXmlElement(fragment, p))
|
||||
|
||||
return ydoc
|
||||
}
|
||||
@ -59,13 +64,13 @@ export function jsonToYDocNoSchema (json: MarkupNode, field: string): YDoc {
|
||||
/**
|
||||
* Convert ProseMirror JSON Node representation to YXmlElement
|
||||
* */
|
||||
function nodeToYXmlElement (node: MarkupNode): YXmlElement | YXmlText {
|
||||
function nodeToYXmlElement (parent: YXmlFragment, node: MarkupNode): YXmlElement | YXmlText {
|
||||
const elem = node.type === 'text' ? new YXmlText() : new YXmlElement(node.type)
|
||||
parent.push([elem])
|
||||
|
||||
if (elem instanceof YXmlElement) {
|
||||
if (node.content !== undefined && node.content.length > 0) {
|
||||
const content = node.content.map(nodeToYXmlElement)
|
||||
elem.push(content)
|
||||
node.content.map((p) => nodeToYXmlElement(elem, p))
|
||||
}
|
||||
} else {
|
||||
// https://github.com/yjs/y-prosemirror/blob/master/src/plugins/sync-plugin.js#L777
|
||||
|
Loading…
Reference in New Issue
Block a user