File size: 1,895 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
import { Migration } from '@react-page/editor';
import isEmpty from 'lodash.isempty';
import type { Element, Node, Text } from 'slate';
// this is for slate 0.50.0
// eslint-disable-next-line @typescript-eslint/no-explicit-any
type OldMark = {
object: 'mark';
type: string;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
data?: { [key: string]: any };
};
type OldTextNode = {
object: 'text';
text: string;
marks?: OldMark[];
};
type OldElementNode = {
object: 'block' | 'inline';
type: string;
isVoid: boolean;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
data: { [key: string]: any };
nodes: OldNode[];
};
type OldNode = OldElementNode | OldTextNode;
const migrateTextNode = (oldNode: OldTextNode): Text => {
return {
text: oldNode.text,
...(oldNode.marks?.reduce(
(acc, mark) => ({
...acc,
[mark.type]: !isEmpty(mark.data) ? mark.data : true,
}),
{}
) ?? {}),
};
};
const migrateElementNode = (node: OldElementNode): Element => {
return {
data: node.data ?? {},
type: node.type,
children: node.nodes?.map(migrateNode) ?? [],
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} as any;
};
const migrateNode = (oldNode: OldNode): Node => {
if (oldNode.object === 'text') {
return migrateTextNode(oldNode);
} else {
return migrateElementNode(oldNode);
}
};
const migration = new Migration({
toVersion: '0.0.4',
fromVersionRange: '^0.0.3',
migrate: (state) => {
if (!state) {
return {};
}
const slate = state.serialized?.document?.nodes?.map(migrateNode) ?? [];
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const result: any = { slate };
if (state.importFromHtml) {
result.importFromHtml = state.importFromHtml;
}
return result;
},
});
export default migration;
|