|
| 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 | + // Set to non-default value to ensure it is not overridden by the migration rule. |
| 57 | + textAlignment: "right", |
| 58 | + }, |
| 59 | + }, |
| 60 | + ], |
| 61 | + }); |
| 62 | + |
| 63 | + prosemirrorJSONToYXmlFragment( |
| 64 | + editor.pmSchema, |
| 65 | + JSON.parse(JSON.stringify(editor.prosemirrorState.doc.toJSON())), |
| 66 | + fragment, |
| 67 | + ); |
| 68 | + |
| 69 | + expect(fragment.toJSON()).toMatchInlineSnapshot( |
| 70 | + // The color attributes are on the paragraph, not the blockContainer. |
| 71 | + `"<blockgroup><blockcontainer id="0"><paragraph backgroundColor="red" textAlignment="right" textColor="blue">Welcome to this demo!</paragraph></blockcontainer></blockgroup>"`, |
| 72 | + ); |
| 73 | + |
| 74 | + const tr = editor.prosemirrorState.tr; |
| 75 | + moveColorAttributes(fragment, tr); |
| 76 | + // The document will be unchanged because the color attributes are already on the paragraph. |
| 77 | + expect(tr.docChanged).toBe(false); |
| 78 | +}); |
| 79 | + |
| 80 | +it("can move color attributes on older documents multiple times", async () => { |
| 81 | + const doc = new Y.Doc(); |
| 82 | + const fragment = doc.getXmlFragment("doc"); |
| 83 | + const editor = BlockNoteEditor.create({ |
| 84 | + initialContent: [ |
| 85 | + { |
| 86 | + type: "paragraph", |
| 87 | + content: "Welcome to this demo!", |
| 88 | + }, |
| 89 | + ], |
| 90 | + }); |
| 91 | + |
| 92 | + // Because this was a previous schema, we are creating the YFragment manually |
| 93 | + const blockGroup = new Y.XmlElement("blockGroup"); |
| 94 | + const el = new Y.XmlElement("blockContainer"); |
| 95 | + el.setAttribute("id", "0"); |
| 96 | + el.setAttribute("backgroundColor", "red"); |
| 97 | + el.setAttribute("textColor", "blue"); |
| 98 | + const para = new Y.XmlElement("paragraph"); |
| 99 | + para.setAttribute("textAlignment", "left"); |
| 100 | + para.insert(0, [new Y.XmlText("Welcome to this demo!")]); |
| 101 | + el.insert(0, [para]); |
| 102 | + blockGroup.insert(0, [el]); |
| 103 | + fragment.insert(0, [blockGroup]); |
| 104 | + |
| 105 | + // Note that the blockContainer has the color attributes, but the paragraph does not. |
| 106 | + expect(fragment.toJSON()).toMatchInlineSnapshot( |
| 107 | + `"<blockgroup><blockcontainer backgroundColor="red" id="0" textColor="blue"><paragraph textAlignment="left">Welcome to this demo!</paragraph></blockcontainer></blockgroup>"`, |
| 108 | + ); |
| 109 | + |
| 110 | + const tr = editor.prosemirrorState.tr; |
| 111 | + moveColorAttributes(fragment, tr); |
| 112 | + // Note that the color attributes have been moved to the paragraph. |
| 113 | + expect(JSON.stringify(tr.doc.toJSON())).toMatchInlineSnapshot( |
| 114 | + `"{"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!"}]}]}]}]}"`, |
| 115 | + ); |
| 116 | + |
| 117 | + el.setAttribute("backgroundColor", "green"); |
| 118 | + el.setAttribute("textColor", "yellow"); |
| 119 | + |
| 120 | + expect(fragment.toJSON()).toMatchInlineSnapshot( |
| 121 | + `"<blockgroup><blockcontainer backgroundColor="green" id="0" textColor="yellow"><paragraph textAlignment="left">Welcome to this demo!</paragraph></blockcontainer></blockgroup>"`, |
| 122 | + ); |
| 123 | + |
| 124 | + const nextTr = editor.prosemirrorState.tr; |
| 125 | + moveColorAttributes(fragment, nextTr); |
| 126 | + // Note that the color attributes have been moved to the paragraph. |
| 127 | + expect(JSON.stringify(nextTr.doc.toJSON())).toMatchInlineSnapshot( |
| 128 | + `"{"type":"doc","content":[{"type":"blockGroup","content":[{"type":"blockContainer","attrs":{"id":"0"},"content":[{"type":"paragraph","attrs":{"backgroundColor":"green","textColor":"yellow","textAlignment":"left"},"content":[{"type":"text","text":"Welcome to this demo!"}]}]}]}]}"`, |
| 129 | + ); |
| 130 | +}); |
0 commit comments