|
| 1 | +import { Mark } from "@tiptap/core"; |
| 2 | + |
| 3 | +export const SuggestionAddMark = Mark.create({ |
| 4 | + name: "insertion", |
| 5 | + inclusive: false, |
| 6 | + extendMarkSchema(extension) { |
| 7 | + if (extension.name !== "insertion") { |
| 8 | + return {}; |
| 9 | + } |
| 10 | + return { |
| 11 | + blocknoteIgnore: true, |
| 12 | + inclusive: false, |
| 13 | + excludes: "deletion modification insertion", |
| 14 | + attrs: { |
| 15 | + id: { validate: "number" }, |
| 16 | + }, |
| 17 | + toDOM(mark, inline) { |
| 18 | + return [ |
| 19 | + "ins", |
| 20 | + { |
| 21 | + "data-id": String(mark.attrs["id"]), |
| 22 | + "data-inline": String(inline), |
| 23 | + ...(!inline && { style: "display: block" }), |
| 24 | + }, |
| 25 | + 0, |
| 26 | + ]; |
| 27 | + }, |
| 28 | + parseDOM: [ |
| 29 | + { |
| 30 | + tag: "ins", |
| 31 | + getAttrs(node) { |
| 32 | + if (!node.dataset["id"]) return false; |
| 33 | + return { |
| 34 | + id: parseInt(node.dataset["id"], 10), |
| 35 | + }; |
| 36 | + }, |
| 37 | + }, |
| 38 | + ], |
| 39 | + }; |
| 40 | + }, |
| 41 | +}); |
| 42 | + |
| 43 | +export const SuggestionDeleteMark = Mark.create({ |
| 44 | + name: "deletion", |
| 45 | + inclusive: false, |
| 46 | + |
| 47 | + extendMarkSchema(extension) { |
| 48 | + if (extension.name !== "deletion") { |
| 49 | + return {}; |
| 50 | + } |
| 51 | + return { |
| 52 | + blocknoteIgnore: true, |
| 53 | + inclusive: false, |
| 54 | + |
| 55 | + excludes: "insertion modification deletion", |
| 56 | + attrs: { |
| 57 | + id: { validate: "number" }, |
| 58 | + }, |
| 59 | + toDOM(mark, inline) { |
| 60 | + return [ |
| 61 | + "del", |
| 62 | + { |
| 63 | + "data-id": String(mark.attrs["id"]), |
| 64 | + "data-inline": String(inline), |
| 65 | + ...(!inline && { style: "display: block" }), |
| 66 | + }, |
| 67 | + 0, |
| 68 | + ]; |
| 69 | + }, |
| 70 | + parseDOM: [ |
| 71 | + { |
| 72 | + tag: "del", |
| 73 | + getAttrs(node) { |
| 74 | + if (!node.dataset["id"]) return false; |
| 75 | + return { |
| 76 | + id: parseInt(node.dataset["id"], 10), |
| 77 | + }; |
| 78 | + }, |
| 79 | + }, |
| 80 | + ], |
| 81 | + }; |
| 82 | + }, |
| 83 | +}); |
| 84 | + |
| 85 | +export const SuggestionModificationMark = Mark.create({ |
| 86 | + name: "modification", |
| 87 | + inclusive: false, |
| 88 | + |
| 89 | + extendMarkSchema(extension) { |
| 90 | + if (extension.name !== "modification") { |
| 91 | + return {}; |
| 92 | + } |
| 93 | + return { |
| 94 | + blocknoteIgnore: true, |
| 95 | + inclusive: false, |
| 96 | + excludes: "deletion insertion", |
| 97 | + attrs: { |
| 98 | + id: { validate: "number" }, |
| 99 | + type: { validate: "string" }, |
| 100 | + attrName: { default: null, validate: "string|null" }, |
| 101 | + previousValue: { default: null }, |
| 102 | + newValue: { default: null }, |
| 103 | + }, |
| 104 | + toDOM(mark, inline) { |
| 105 | + return [ |
| 106 | + inline ? "span" : "div", |
| 107 | + { |
| 108 | + "data-type": "modification", |
| 109 | + "data-id": String(mark.attrs["id"]), |
| 110 | + "data-mod-type": mark.attrs["type"] as string, |
| 111 | + "data-mod-prev-val": JSON.stringify(mark.attrs["previousValue"]), |
| 112 | + // TODO: Try to serialize marks with toJSON? |
| 113 | + "data-mod-new-val": JSON.stringify(mark.attrs["newValue"]), |
| 114 | + }, |
| 115 | + 0, |
| 116 | + ]; |
| 117 | + }, |
| 118 | + parseDOM: [ |
| 119 | + { |
| 120 | + tag: "span[data-type='modification']", |
| 121 | + getAttrs(node) { |
| 122 | + if (!node.dataset["id"]) return false; |
| 123 | + return { |
| 124 | + id: parseInt(node.dataset["id"], 10), |
| 125 | + type: node.dataset["modType"], |
| 126 | + previousValue: node.dataset["modPrevVal"], |
| 127 | + newValue: node.dataset["modNewVal"], |
| 128 | + }; |
| 129 | + }, |
| 130 | + }, |
| 131 | + { |
| 132 | + tag: "div[data-type='modification']", |
| 133 | + getAttrs(node) { |
| 134 | + if (!node.dataset["id"]) return false; |
| 135 | + return { |
| 136 | + id: parseInt(node.dataset["id"], 10), |
| 137 | + type: node.dataset["modType"], |
| 138 | + previousValue: node.dataset["modPrevVal"], |
| 139 | + }; |
| 140 | + }, |
| 141 | + }, |
| 142 | + ], |
| 143 | + }; |
| 144 | + }, |
| 145 | +}); |
0 commit comments