|
| 1 | +import type { Patch } from "immer"; |
| 2 | +import type { Folder, Page, Pages } from "@webstudio-is/sdk"; |
| 3 | + |
| 4 | +export type PagesPatchChange = { |
| 5 | + namespace: string; |
| 6 | + patches: Patch[]; |
| 7 | + revisePatches?: Patch[]; |
| 8 | +}; |
| 9 | + |
| 10 | +const ID_PREFIX = "@"; |
| 11 | + |
| 12 | +const encodeId = (id: string) => `${ID_PREFIX}${id}`; |
| 13 | + |
| 14 | +const isIdSegment = (segment: string | number): segment is string => { |
| 15 | + return typeof segment === "string" && segment.startsWith(ID_PREFIX); |
| 16 | +}; |
| 17 | + |
| 18 | +const normalizeOnePatch = ( |
| 19 | + patch: Patch, |
| 20 | + revisePatches: Patch[], |
| 21 | + pages: Pages |
| 22 | +): Patch => { |
| 23 | + const [key, indexOrId, ...rest] = patch.path; |
| 24 | + if (typeof indexOrId !== "number") { |
| 25 | + return patch; |
| 26 | + } |
| 27 | + if (key !== "pages" && key !== "folders") { |
| 28 | + return patch; |
| 29 | + } |
| 30 | + |
| 31 | + const items: Array<Page | Folder> = |
| 32 | + key === "pages" ? pages.pages : pages.folders; |
| 33 | + |
| 34 | + if (patch.op === "add") { |
| 35 | + const id = (patch.value as { id?: string } | undefined)?.id; |
| 36 | + if (!id) { |
| 37 | + return patch; |
| 38 | + } |
| 39 | + return { ...patch, path: [key, encodeId(id), ...rest] }; |
| 40 | + } |
| 41 | + |
| 42 | + if (patch.op === "remove") { |
| 43 | + const revisePatch = revisePatches.find( |
| 44 | + (rp) => rp.op === "add" && rp.path[0] === key && rp.path[1] === indexOrId |
| 45 | + ); |
| 46 | + const id = (revisePatch?.value as { id?: string } | undefined)?.id; |
| 47 | + if (!id) { |
| 48 | + return patch; |
| 49 | + } |
| 50 | + return { ...patch, path: [key, encodeId(id), ...rest] }; |
| 51 | + } |
| 52 | + |
| 53 | + const id = (items[indexOrId] as { id?: string } | undefined)?.id; |
| 54 | + if (!id) { |
| 55 | + return patch; |
| 56 | + } |
| 57 | + return { ...patch, path: [key, encodeId(id), ...rest] }; |
| 58 | +}; |
| 59 | + |
| 60 | +const normalizeRevisePatch = ( |
| 61 | + revisePatch: Patch, |
| 62 | + patches: Patch[], |
| 63 | + pages: Pages |
| 64 | +): Patch => { |
| 65 | + const [key, indexOrId, ...rest] = revisePatch.path; |
| 66 | + if (typeof indexOrId !== "number") { |
| 67 | + return revisePatch; |
| 68 | + } |
| 69 | + if (key !== "pages" && key !== "folders") { |
| 70 | + return revisePatch; |
| 71 | + } |
| 72 | + |
| 73 | + const items: Array<Page | Folder> = |
| 74 | + key === "pages" ? pages.pages : pages.folders; |
| 75 | + |
| 76 | + if (revisePatch.op === "add") { |
| 77 | + const id = (revisePatch.value as { id?: string } | undefined)?.id; |
| 78 | + if (!id) { |
| 79 | + return revisePatch; |
| 80 | + } |
| 81 | + return { ...revisePatch, path: [key, encodeId(id), ...rest] }; |
| 82 | + } |
| 83 | + |
| 84 | + if (revisePatch.op === "remove") { |
| 85 | + const patch = patches.find( |
| 86 | + (p) => p.op === "add" && p.path[0] === key && p.path[1] === indexOrId |
| 87 | + ); |
| 88 | + const id = (patch?.value as { id?: string } | undefined)?.id; |
| 89 | + if (!id) { |
| 90 | + return revisePatch; |
| 91 | + } |
| 92 | + return { ...revisePatch, path: [key, encodeId(id), ...rest] }; |
| 93 | + } |
| 94 | + |
| 95 | + const id = (items[indexOrId] as { id?: string } | undefined)?.id; |
| 96 | + if (!id) { |
| 97 | + return revisePatch; |
| 98 | + } |
| 99 | + return { ...revisePatch, path: [key, encodeId(id), ...rest] }; |
| 100 | +}; |
| 101 | + |
| 102 | +const denormalizeOnePatch = ( |
| 103 | + patch: Patch, |
| 104 | + pages: Pages, |
| 105 | + { onMissing = "keep" }: { onMissing?: "keep" | "throw" } = {} |
| 106 | +): Patch => { |
| 107 | + const [key, indexOrId, ...rest] = patch.path; |
| 108 | + if (key !== "pages" && key !== "folders") { |
| 109 | + return patch; |
| 110 | + } |
| 111 | + if (isIdSegment(indexOrId as string | number) === false) { |
| 112 | + return patch; |
| 113 | + } |
| 114 | + |
| 115 | + const id = (indexOrId as string).slice(ID_PREFIX.length); |
| 116 | + const items: Array<Page | Folder> = |
| 117 | + key === "pages" ? pages.pages : pages.folders; |
| 118 | + |
| 119 | + if (patch.op === "add") { |
| 120 | + return { ...patch, path: [key, items.length, ...rest] }; |
| 121 | + } |
| 122 | + |
| 123 | + const index = items.findIndex((item) => item.id === id); |
| 124 | + if (index === -1) { |
| 125 | + if (onMissing === "throw") { |
| 126 | + throw new Error( |
| 127 | + `Unable to apply pages patch. Item "${id}" was not found.` |
| 128 | + ); |
| 129 | + } |
| 130 | + return patch; |
| 131 | + } |
| 132 | + |
| 133 | + return { ...patch, path: [key, index, ...rest] }; |
| 134 | +}; |
| 135 | + |
| 136 | +export const normalizePagesPatch = <ChangeType extends PagesPatchChange>( |
| 137 | + changes: ChangeType[], |
| 138 | + pages: Pages |
| 139 | +): ChangeType[] => |
| 140 | + changes.map((change) => { |
| 141 | + if (change.namespace !== "pages") { |
| 142 | + return change; |
| 143 | + } |
| 144 | + const revisePatches = change.revisePatches ?? []; |
| 145 | + return { |
| 146 | + ...change, |
| 147 | + patches: change.patches.map((patch) => |
| 148 | + normalizeOnePatch(patch, revisePatches, pages) |
| 149 | + ), |
| 150 | + revisePatches: revisePatches.map((revisePatch) => |
| 151 | + normalizeRevisePatch(revisePatch, change.patches, pages) |
| 152 | + ), |
| 153 | + }; |
| 154 | + }); |
| 155 | + |
| 156 | +export const denormalizePagesPatch = <ChangeType extends PagesPatchChange>( |
| 157 | + changes: ChangeType[], |
| 158 | + pages: Pages, |
| 159 | + options?: { onMissing?: "keep" | "throw" } |
| 160 | +): ChangeType[] => |
| 161 | + changes.map((change) => { |
| 162 | + if (change.namespace !== "pages") { |
| 163 | + return change; |
| 164 | + } |
| 165 | + const denormalizedChange = { |
| 166 | + ...change, |
| 167 | + patches: change.patches.map((patch) => |
| 168 | + denormalizeOnePatch(patch, pages, options) |
| 169 | + ), |
| 170 | + }; |
| 171 | + if (change.revisePatches === undefined) { |
| 172 | + return denormalizedChange; |
| 173 | + } |
| 174 | + return { |
| 175 | + ...denormalizedChange, |
| 176 | + revisePatches: change.revisePatches.map((revisePatch) => |
| 177 | + denormalizeOnePatch(revisePatch, pages, options) |
| 178 | + ), |
| 179 | + }; |
| 180 | + }); |
0 commit comments