|
1 | 1 | import { Node } from "@tiptap/core";
|
2 |
| -import { PropSchema } from "../blocks/types"; |
| 2 | +import { camelToDataKebab } from "../blocks/internal"; |
| 3 | +import { Props, PropSchema } from "../blocks/types"; |
3 | 4 | import {
|
4 | 5 | InlineContentConfig,
|
5 | 6 | InlineContentImplementation,
|
6 | 7 | InlineContentSchemaFromSpecs,
|
7 | 8 | InlineContentSpec,
|
8 | 9 | InlineContentSpecs,
|
9 | 10 | } from "./types";
|
| 11 | +import { mergeCSSClasses } from "../../../../shared/utils"; |
| 12 | + |
| 13 | +// Function that adds necessary classes and attributes to the `dom` element |
| 14 | +// returned from a custom inline content's 'render' function, to ensure no data |
| 15 | +// is lost on internal copy & paste. |
| 16 | +export function addInlineContentAttributes< |
| 17 | + IType extends string, |
| 18 | + PSchema extends PropSchema |
| 19 | +>( |
| 20 | + element: HTMLElement, |
| 21 | + inlineContentType: IType, |
| 22 | + inlineContentProps: Props<PSchema>, |
| 23 | + propSchema: PSchema |
| 24 | +): HTMLElement { |
| 25 | + // Sets inline content section class |
| 26 | + element.className = mergeCSSClasses( |
| 27 | + "bn-inline-content-section", |
| 28 | + element.className |
| 29 | + ); |
| 30 | + // Sets content type attribute |
| 31 | + element.setAttribute("data-inline-content-type", inlineContentType); |
| 32 | + // Adds props as HTML attributes in kebab-case with "data-" prefix. Skips props |
| 33 | + // set to their default values. |
| 34 | + Object.entries(inlineContentProps) |
| 35 | + .filter(([prop, value]) => value !== propSchema[prop].default) |
| 36 | + .map(([prop, value]) => { |
| 37 | + return [camelToDataKebab(prop), value]; |
| 38 | + }) |
| 39 | + .forEach(([prop, value]) => element.setAttribute(prop, value)); |
| 40 | + |
| 41 | + return element; |
| 42 | +} |
10 | 43 |
|
11 | 44 | // This helper function helps to instantiate a InlineContentSpec with a
|
12 | 45 | // config and implementation that conform to the type of Config
|
|
0 commit comments