File size: 1,696 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 |
/* eslint-disable @typescript-eslint/ban-types */
import { getAlignmentFromElement } from '../plugins/paragraphs';
import type { SlateComponentPluginDefinition } from '../types/slatePluginDefinitions';
import createComponentPlugin from './createComponentPlugin';
type Def<T extends Record<string, unknown>> = Pick<
SlateComponentPluginDefinition<HtmlBlockData<T>>,
| 'type'
| 'icon'
| 'label'
| 'customAdd'
| 'customRemove'
| 'isDisabled'
| 'hotKey'
| 'onKeyDown'
| 'getInitialData'
| 'controls'
| 'getStyle'
> & {
replaceWithDefaultOnRemove?: boolean;
tagName: keyof JSX.IntrinsicElements;
getData?: (el: HTMLElement) => T | void;
noButton?: boolean;
};
export type DefaultBlockDataType = {
align: 'left' | 'right' | 'center' | 'justify';
};
export type HtmlBlockData<T> = T & DefaultBlockDataType;
function createSimpleHtmlBlockPlugin<T = {}>(def: Def<HtmlBlockData<T>>) {
return createComponentPlugin<HtmlBlockData<T>>({
type: def.type,
object: 'block',
hotKey: def.hotKey,
replaceWithDefaultOnRemove: def.replaceWithDefaultOnRemove,
icon: def.icon,
label: def.label,
onKeyDown: def.onKeyDown,
addToolbarButton: !def.noButton,
customAdd: def.customAdd,
customRemove: def.customRemove,
controls: def.controls,
addHoverButton: false,
deserialize: {
tagName: def.tagName,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
getData: def.getData || (getAlignmentFromElement as any),
},
getStyle: (data) => ({
textAlign: data.align,
...(def.getStyle?.(data) ?? {}),
}),
Component: def.tagName,
});
}
export default createSimpleHtmlBlockPlugin;
|