@@ -23,15 +23,24 @@ import {
2323
2424export 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
72102export 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 ,
0 commit comments