@@ -29,9 +29,16 @@ export interface LinkPreviewProvider {
2929 */
3030function getValidProvider (
3131 providers : LinkPreviewProvider [ ] ,
32- node : ProsemirrorNode
33- ) : { url : string ; provider : LinkPreviewProvider } | null {
34- const n = node . isText ? node : node . content . firstChild ;
32+ node : ProsemirrorNode ,
33+ parent : ProsemirrorNode
34+ ) : {
35+ url : string ;
36+ provider : LinkPreviewProvider ;
37+ } | null {
38+ if ( ! node ?. isText || ! parent ) {
39+ return null ;
40+ }
41+ const n = node ; // TODO CLEANUP
3542 const url = n ?. marks . find ( ( m ) => m . type . name === "link" ) ?. attrs
3643 ?. href as string ;
3744
@@ -47,7 +54,7 @@ function getValidProvider(
4754 }
4855
4956 // full preview providers require links to be in a paragraph by themselves
50- if ( ! provider . textOnly && ! isStandalonePreviewableLink ( node ) ) {
57+ if ( ! provider . textOnly && ! isStandalonePreviewableLink ( parent ) ) {
5158 continue ;
5259 }
5360
@@ -93,8 +100,8 @@ function getValidNodes(
93100 } [ ] = [ ] ;
94101
95102 // iterate over current document structure
96- currState . doc . descendants ( ( node , pos ) => {
97- const provider = getValidProvider ( providers , node ) ;
103+ currState . doc . descendants ( ( node , pos , parent ) => {
104+ const provider = getValidProvider ( providers , node , parent ) ;
98105
99106 if ( provider ) {
100107 validNodes . push ( { provider, pos, node } ) ;
@@ -143,14 +150,20 @@ function generateRecentPreviewDecorations(
143150 if ( ! n . isTextOnly && n . content ) {
144151 // if the url is in the cache, insert the link preview
145152 linkPreviewDecorations . push ( insertLinkPreview ( n . pos , n . content ) ) ;
146- } else {
147- const node = doc . nodeAt ( n . pos ) ;
153+ } else if ( ! n . content ) {
148154 // otherwise, add the loading styles
155+ // attach the node decorations to the text's parent node
156+ const resolved = doc . resolve ( n . pos ) ;
157+ const parentPos = resolved . posAtIndex ( 0 , resolved . depth - 1 ) ;
149158 linkPreviewDecorations . push (
150- Decoration . node ( n . pos , n . pos + node . nodeSize , {
151- class : "is-loading js-link-preview-loading" ,
152- title : "Loading..." ,
153- } )
159+ Decoration . node (
160+ parentPos ,
161+ parentPos + resolved . parent . nodeSize ,
162+ {
163+ class : "is-loading js-link-preview-loading" ,
164+ title : "Loading..." ,
165+ }
166+ )
154167 ) ;
155168 }
156169 } ) ;
@@ -173,7 +186,9 @@ function insertLinkPreview(pos: number, content: Node | null) {
173186 placeholder . appendChild ( content . cloneNode ( true ) ) ;
174187 }
175188
176- return Decoration . widget ( pos , placeholder ) ;
189+ return Decoration . widget ( pos , placeholder , {
190+ side : - 1 ,
191+ } ) ;
177192}
178193
179194/**
0 commit comments