@@ -36,12 +36,23 @@ export default class DrawioPlugin extends Plugin {
3636 public platform : SyFrontendTypes
3737 public readonly version = version
3838
39+ private _mutationObserver ;
3940 private _openMenuImageHandler ;
4041 private _globalKeyDownHandler ;
4142
4243 async onload ( ) {
4344 this . initMetaInfo ( ) ;
4445
46+ this . _mutationObserver = this . setAddImageBlockMuatationObserver ( document . body , ( blockElement : HTMLElement ) => {
47+ const imageElement = blockElement . querySelector ( "img" ) as HTMLImageElement ;
48+ if ( imageElement ) {
49+ const imageURL = imageElement . getAttribute ( "data-src" ) ;
50+ this . getDrawioImageInfo ( imageURL ) . then ( ( imageInfo ) => {
51+ this . updateAttrLabel ( imageInfo , blockElement ) ;
52+ } ) ;
53+ }
54+ } ) ;
55+
4556 this . protyleSlash = [ {
4657 filter : [ "drawio" , "draw.io" ] ,
4758 id : "drawio" ,
@@ -61,6 +72,7 @@ export default class DrawioPlugin extends Plugin {
6172 }
6273
6374 onunload ( ) {
75+ if ( this . _mutationObserver ) this . _mutationObserver . disconnect ( ) ;
6476 if ( this . _openMenuImageHandler ) this . eventBus . off ( "open-menu-image" , this . _openMenuImageHandler ) ;
6577 if ( this . _globalKeyDownHandler ) document . documentElement . removeEventListener ( "keydown" , this . _globalKeyDownHandler ) ;
6678 }
@@ -85,6 +97,38 @@ export default class DrawioPlugin extends Plugin {
8597 }
8698 }
8799
100+ public setAddImageBlockMuatationObserver ( element : HTMLElement , callback : ( blockElement : HTMLElement ) => void ) : MutationObserver {
101+ const mutationObserver = new MutationObserver ( mutations => {
102+ for ( const mutation of mutations ) {
103+ if ( mutation . type === 'childList' ) {
104+ mutation . addedNodes . forEach ( node => {
105+ if ( node . nodeType === Node . ELEMENT_NODE ) {
106+ const addedElement = node as HTMLElement ;
107+ if ( addedElement . matches ( "div[data-type='NodeParagraph']" ) ) {
108+ if ( addedElement . querySelector ( ".img[data-type='img'] img" ) ) {
109+ callback ( addedElement as HTMLElement ) ;
110+ }
111+ } else {
112+ addedElement . querySelectorAll ( "div[data-type='NodeParagraph']" ) . forEach ( ( blockElement : HTMLElement ) => {
113+ if ( blockElement . querySelector ( ".img[data-type='img'] img" ) ) {
114+ callback ( blockElement ) ;
115+ }
116+ } )
117+ }
118+ }
119+ } ) ;
120+ }
121+ }
122+ } ) ;
123+
124+ mutationObserver . observe ( element , {
125+ childList : true ,
126+ subtree : true
127+ } ) ;
128+
129+ return mutationObserver ;
130+ }
131+
88132 public async getDrawioImageInfo ( imageURL : string ) : Promise < DrawioImageInfo | null > {
89133 const imageURLRegex = / ^ a s s e t s \/ .+ \. s v g $ / ;
90134 if ( ! imageURLRegex . test ( imageURL ) ) return null ;
@@ -151,6 +195,25 @@ export default class DrawioPlugin extends Plugin {
151195 fetchPost ( "/api/file/putFile" , formData , callback ) ;
152196 }
153197
198+ public updateAttrLabel ( imageInfo : DrawioImageInfo , blockElement : HTMLElement ) {
199+ if ( ! imageInfo ) return ;
200+
201+ const attrElement = blockElement . querySelector ( ".protyle-attr" ) as HTMLDivElement ;
202+ if ( attrElement ) {
203+ const pageCount = ( imageInfo . data . match ( / n a m e = & q u o t ; / g) || [ ] ) . length ;
204+ const labelHTML = `<span>draw.io${ pageCount > 1 ? `:${ pageCount } ` : '' } </span>` ;
205+ let labelElement = attrElement . querySelector ( ".label--embed-drawio" ) as HTMLDivElement ;
206+ if ( labelElement ) {
207+ labelElement . innerHTML = labelHTML ;
208+ } else {
209+ labelElement = document . createElement ( "div" ) ;
210+ labelElement . classList . add ( "label--embed-drawio" ) ;
211+ labelElement . innerHTML = labelHTML ;
212+ attrElement . prepend ( labelElement ) ;
213+ }
214+ }
215+ }
216+
154217 private openMenuImageHandler ( { detail } ) {
155218 const selectedElement = detail . element ;
156219 const imageElement = selectedElement . querySelector ( "img" ) as HTMLImageElement ;
@@ -245,6 +308,10 @@ export default class DrawioPlugin extends Plugin {
245308 fetch ( imageInfo . imageURL , { cache : 'reload' } ) . then ( ( ) => {
246309 document . querySelectorAll ( `img[data-src='${ imageInfo . imageURL } ']` ) . forEach ( imageElement => {
247310 ( imageElement as HTMLImageElement ) . src = imageInfo . imageURL ;
311+ const blockElement = imageElement . closest ( "div[data-type='NodeParagraph']" ) as HTMLElement ;
312+ if ( blockElement ) {
313+ this . updateAttrLabel ( imageInfo , blockElement ) ;
314+ }
248315 } ) ;
249316 } ) ;
250317 } ) ;
0 commit comments