Skip to content

Commit 467b33d

Browse files
committed
Merge branch 'default-blocks' of github.com:TypeCellOS/BlockNote into default-blocks
2 parents ac0d392 + fc98fb8 commit 467b33d

File tree

2 files changed

+45
-16
lines changed

2 files changed

+45
-16
lines changed

packages/core/src/schema/inlineContent/createSpec.ts

Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,24 @@ import {
2323

2424
export type CustomInlineContentImplementation<
2525
T extends CustomInlineContentConfig,
26-
// B extends BlockSchema,
27-
// I extends InlineContentSchema,
2826
S extends StyleSchema,
2927
> = {
28+
/**
29+
* Parses an external HTML element into a inline content of this type when it returns the block props object, otherwise undefined
30+
*/
31+
parse?: (el: HTMLElement) => Partial<Props<T["propSchema"]>> | undefined;
32+
33+
/**
34+
* Renders an inline content to DOM elements
35+
*/
3036
render: (
3137
/**
3238
* The custom inline content to render
3339
*/
3440
inlineContent: InlineContentFromConfig<T, S>,
41+
/**
42+
* A callback that allows overriding the inline content element
43+
*/
3544
updateInlineContent: (
3645
update: PartialCustomInlineContentFromConfig<T, S>,
3746
) => void,
@@ -46,14 +55,15 @@ export type CustomInlineContentImplementation<
4655
) => {
4756
dom: HTMLElement;
4857
contentDOM?: HTMLElement;
49-
// destroy?: () => void;
58+
destroy?: () => void;
5059
};
5160
};
5261

53-
export function getInlineContentParseRules(
54-
config: CustomInlineContentConfig,
55-
): TagParseRule[] {
56-
return [
62+
export function getInlineContentParseRules<C extends CustomInlineContentConfig>(
63+
config: C,
64+
customParseFunction?: CustomInlineContentImplementation<C, any>["parse"],
65+
) {
66+
const rules: TagParseRule[] = [
5767
{
5868
tag: `[data-inline-content-type="${config.type}"]`,
5969
contentElement: (element) => {
@@ -67,6 +77,26 @@ export function getInlineContentParseRules(
6777
},
6878
},
6979
];
80+
81+
if (customParseFunction) {
82+
rules.push({
83+
tag: "*",
84+
getAttrs(node: string | HTMLElement) {
85+
if (typeof node === "string") {
86+
return false;
87+
}
88+
89+
const props = customParseFunction?.(node);
90+
91+
if (props === undefined) {
92+
return false;
93+
}
94+
95+
return props;
96+
},
97+
});
98+
}
99+
return rules;
70100
}
71101

72102
export function createInlineContentSpec<
@@ -95,7 +125,10 @@ export function createInlineContentSpec<
95125
},
96126

97127
parseHTML() {
98-
return getInlineContentParseRules(inlineContentConfig);
128+
return getInlineContentParseRules(
129+
inlineContentConfig,
130+
inlineContentImplementation.parse,
131+
);
99132
},
100133

101134
renderHTML({ node }) {
@@ -132,14 +165,12 @@ export function createInlineContentSpec<
132165
editor.schema.styleSchema,
133166
) as any as InlineContentFromConfig<T, S>, // TODO: fix cast
134167
(update) => {
135-
if (typeof getPos === "boolean") {
136-
return;
137-
}
138-
139168
const content = inlineContentToNodes([update], editor.pmSchema);
140169

170+
const pos = getPos();
171+
141172
editor.transact((tr) =>
142-
tr.replaceWith(getPos(), getPos() + node.nodeSize, content),
173+
tr.replaceWith(pos, pos + node.nodeSize, content),
143174
);
144175
},
145176
editor,

packages/core/src/schema/styles/createSpec.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,13 @@ export function createStyleSpec<T extends StyleConfig>(
6262
};
6363

6464
if (styleConfig.propSchema === "boolean") {
65-
// @ts-ignore not sure why this is complaining
66-
renderResult = styleImplementation.render();
65+
renderResult = styleImplementation.render(mark.attrs.stringValue);
6766
} else if (styleConfig.propSchema === "string") {
6867
renderResult = styleImplementation.render(mark.attrs.stringValue);
6968
} else {
7069
throw new UnreachableCaseError(styleConfig.propSchema);
7170
}
7271

73-
// const renderResult = styleImplementation.render();
7472
return addStyleAttributes(
7573
renderResult,
7674
styleConfig.type,

0 commit comments

Comments
 (0)