File size: 3,896 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 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
import type { CSSProperties } from 'react';
import { LI, LISTS_TYPE_PREFIX } from '../plugins/lists/constants';
import type { SlatePlugin } from '../types/SlatePlugin';
import type { SlateComponentPluginDefinition } from '../types/slatePluginDefinitions';
import createListItemPlugin from './createListItemPlugin';
import type { HtmlBlockData } from './createSimpleHtmlBlockPlugin';
import createSimpleHtmlBlockPlugin from './createSimpleHtmlBlockPlugin';
type ListDef = {
type: string;
icon?: JSX.Element;
label?: string;
hotKey?: string;
tagName: keyof JSX.IntrinsicElements;
noButton?: boolean; // for Li, this is automatically
getStyle?: () => CSSProperties;
listItem?: {
type: string;
tagName: keyof JSX.IntrinsicElements;
};
};
type ListItemDef<T> = SlateComponentPluginDefinition<HtmlBlockData<T>>;
type CustomizeFunction<T, CT> = (def: ListItemDef<T>) => ListItemDef<CT & T>;
type ListCustomizers<T, CT> = {
customizeList?: CustomizeFunction<T, CT>;
customizeListItem?: CustomizeFunction<T, CT>;
};
function createSlatePlugins<T, CT>(
def: ListDef,
customizers: ListCustomizers<T, CT> = {}
) {
const listItem = def.listItem ?? {
tagName: 'li',
type: LI,
};
return [
createSimpleHtmlBlockPlugin<T>({
type: def.type,
icon: def.icon,
label: def.label,
noButton: def.noButton,
tagName: def.tagName,
getStyle: def.getStyle,
customAdd: async (editor) => {
const { getActiveList, increaseListIndention } = await import(
'./utils/listUtils'
);
const currentList = getActiveList(editor);
if (!currentList) {
increaseListIndention(
editor,
{
listItemType: listItem.type,
},
def.type
);
} else {
// change type
const { Transforms } = await import('slate');
Transforms.setNodes(
editor,
{
type: def.type,
},
{
at: currentList[1],
}
);
}
},
customRemove: async (editor) => {
const { decreaseListIndention } = await import('./utils/listUtils');
decreaseListIndention(editor, {
listItemType: listItem.type,
});
},
})(customizers.customizeList),
createListItemPlugin<T>(listItem)(customizers.customizeListItem),
];
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function mergeCustomizer(c1: any, c2: any): any {
return {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
customizeList(def: any) {
const def2 = c1?.customizeList ? c1.customizeList(def) : def;
return c2?.customizeList ? c2.customizeList(def2) : def2;
},
// eslint-disable-next-line @typescript-eslint/no-explicit-any
customizeListItem(def: any) {
const def2 = c1?.customizeList ? c1.customizeListItem(def) : def;
return c2?.customizeList ? c2.customizeListItem(def2) : def2;
},
};
}
// eslint-disable-next-line @typescript-eslint/ban-types
function createListPlugin<T = {}>(defRaw: ListDef) {
const def: ListDef = {
...defRaw,
type: LISTS_TYPE_PREFIX + defRaw.type,
listItem: defRaw.listItem ?? {
tagName: 'li',
type: LI,
},
};
const inner = function <TIn, TOut>(
innerdef: ListDef,
customizersIn?: ListCustomizers<TIn, TOut>
) {
const customizablePlugin = function <CT>(
customizers: ListCustomizers<TOut, CT>
) {
return inner(innerdef, mergeCustomizer(customizersIn, customizers));
};
customizablePlugin.toPlugin = (): SlatePlugin[] =>
createSlatePlugins<TIn, TOut>(innerdef, customizersIn).map((plugin) =>
plugin.toPlugin()
);
return customizablePlugin;
};
return inner<T, T>(def);
}
export default createListPlugin;
|