|
| 1 | +import { expect, it } from "vitest"; |
| 2 | +import * as Y from "yjs"; |
| 3 | +import { BlockNoteEditor } from "../../../../editor/BlockNoteEditor.js"; |
| 4 | +import { moveColorAttributes } from "./moveColorAttributes.js"; |
| 5 | +import { prosemirrorJSONToYXmlFragment } from "y-prosemirror"; |
| 6 | + |
| 7 | +it("can move color attributes on older documents", async () => { |
| 8 | + const doc = new Y.Doc(); |
| 9 | + const fragment = doc.getXmlFragment("doc"); |
| 10 | + const editor = BlockNoteEditor.create({ |
| 11 | + initialContent: [ |
| 12 | + { |
| 13 | + type: "paragraph", |
| 14 | + content: "Welcome to this demo!", |
| 15 | + }, |
| 16 | + ], |
| 17 | + }); |
| 18 | + |
| 19 | + // Because this was a previous schema, we are creating the YFragment manually |
| 20 | + const blockGroup = new Y.XmlElement("blockGroup"); |
| 21 | + const el = new Y.XmlElement("blockContainer"); |
| 22 | + el.setAttribute("id", "0"); |
| 23 | + el.setAttribute("backgroundColor", "red"); |
| 24 | + el.setAttribute("textColor", "blue"); |
| 25 | + const para = new Y.XmlElement("paragraph"); |
| 26 | + para.setAttribute("textAlignment", "left"); |
| 27 | + para.insert(0, [new Y.XmlText("Welcome to this demo!")]); |
| 28 | + el.insert(0, [para]); |
| 29 | + blockGroup.insert(0, [el]); |
| 30 | + fragment.insert(0, [blockGroup]); |
| 31 | + |
| 32 | + // Note that the blockContainer has the color attributes, but the paragraph does not. |
| 33 | + expect(fragment.toJSON()).toMatchInlineSnapshot( |
| 34 | + `"<blockgroup><blockcontainer backgroundColor="red" id="0" textColor="blue"><paragraph textAlignment="left">Welcome to this demo!</paragraph></blockcontainer></blockgroup>"`, |
| 35 | + ); |
| 36 | + |
| 37 | + const tr = editor.prosemirrorState.tr; |
| 38 | + moveColorAttributes(fragment, tr); |
| 39 | + // Note that the color attributes have been moved to the paragraph. |
| 40 | + expect(JSON.stringify(tr.doc.toJSON())).toMatchInlineSnapshot( |
| 41 | + `"{"type":"doc","content":[{"type":"blockGroup","content":[{"type":"blockContainer","attrs":{"id":"0"},"content":[{"type":"paragraph","attrs":{"backgroundColor":"red","textColor":"blue","textAlignment":"left"},"content":[{"type":"text","text":"Welcome to this demo!"}]}]}]}]}"`, |
| 42 | + ); |
| 43 | +}); |
| 44 | + |
| 45 | +it("does not move color attributes on newer documents", async () => { |
| 46 | + const doc = new Y.Doc(); |
| 47 | + const fragment = doc.getXmlFragment("doc"); |
| 48 | + const editor = BlockNoteEditor.create({ |
| 49 | + initialContent: [ |
| 50 | + { |
| 51 | + type: "paragraph", |
| 52 | + content: "Welcome to this demo!", |
| 53 | + props: { |
| 54 | + backgroundColor: "red", |
| 55 | + textColor: "blue", |
| 56 | + }, |
| 57 | + }, |
| 58 | + ], |
| 59 | + }); |
| 60 | + |
| 61 | + prosemirrorJSONToYXmlFragment( |
| 62 | + editor.pmSchema, |
| 63 | + JSON.parse(JSON.stringify(editor.prosemirrorState.doc.toJSON())), |
| 64 | + fragment, |
| 65 | + ); |
| 66 | + |
| 67 | + expect(fragment.toJSON()).toMatchInlineSnapshot( |
| 68 | + // The color attributes are on the paragraph, not the blockContainer. |
| 69 | + `"<blockgroup><blockcontainer id="0"><paragraph backgroundColor="red" textAlignment="left" textColor="blue">Welcome to this demo!</paragraph></blockcontainer></blockgroup>"`, |
| 70 | + ); |
| 71 | + |
| 72 | + const tr = editor.prosemirrorState.tr; |
| 73 | + moveColorAttributes(fragment, tr); |
| 74 | + // The document will be unchanged because the color attributes are already on the paragraph. |
| 75 | + expect(tr.docChanged).toBe(false); |
| 76 | +}); |
0 commit comments