77} from "siyuan" ;
88import "@/index.scss" ;
99import PluginInfoString from '@/../plugin.json' ;
10- import { unescapeHTML , escapeHTML , base64ToUnicode } from "./utils" ;
10+ import { base64ToUnicode , HTMLToElement } from "./utils" ;
1111import defaultImageContent from "@/../default.svg?raw" ;
1212
1313let PluginInfo = {
@@ -22,6 +22,8 @@ const {
2222 version,
2323} = PluginInfo
2424
25+ const STORAGE_NAME = "config.json" ;
26+
2527export default class DrawioPlugin extends Plugin {
2628 // Run as mobile
2729 public isMobile : boolean
@@ -40,14 +42,19 @@ export default class DrawioPlugin extends Plugin {
4042 private _openMenuImageHandler ;
4143 private _globalKeyDownHandler ;
4244
45+ private settingItems : SettingItem [ ] ;
46+
4347 async onload ( ) {
4448 this . initMetaInfo ( ) ;
49+ this . initSetting ( ) ;
4550
4651 this . _mutationObserver = this . setAddImageBlockMuatationObserver ( document . body , ( blockElement : HTMLElement ) => {
52+ if ( this . data [ STORAGE_NAME ] . labelDisplay === "noLabel" ) return ;
53+
4754 const imageElement = blockElement . querySelector ( "img" ) as HTMLImageElement ;
4855 if ( imageElement ) {
4956 const imageURL = imageElement . getAttribute ( "data-src" ) ;
50- this . getDrawioImageInfo ( imageURL ) . then ( ( imageInfo ) => {
57+ this . getDrawioImageInfo ( imageURL , false ) . then ( ( imageInfo ) => {
5158 this . updateAttrLabel ( imageInfo , blockElement ) ;
5259 } ) ;
5360 }
@@ -77,8 +84,105 @@ export default class DrawioPlugin extends Plugin {
7784 if ( this . _globalKeyDownHandler ) document . documentElement . removeEventListener ( "keydown" , this . _globalKeyDownHandler ) ;
7885 }
7986
80- // openSetting() {
81- // }
87+ openSetting ( ) {
88+ const dialogHTML = `
89+ <div class="b3-dialog__content"></div>
90+ <div class="b3-dialog__action">
91+ <button class="b3-button b3-button--cancel" data-type="cancel">${ window . siyuan . languages . cancel } </button>
92+ <div class="fn__space"></div>
93+ <button class="b3-button b3-button--text" data-type="confirm">${ window . siyuan . languages . save } </button>
94+ </div>
95+ ` ;
96+
97+ const dialog = new Dialog ( {
98+ title : this . displayName ,
99+ content : dialogHTML ,
100+ width : this . isMobile ? "92vw" : "768px" ,
101+ height : "80vh" ,
102+ hideCloseIcon : this . isMobile ,
103+ } ) ;
104+
105+ // 配置的处理拷贝自思源源码
106+ const contentElement = dialog . element . querySelector ( ".b3-dialog__content" ) ;
107+ this . settingItems . forEach ( ( item ) => {
108+ let html = "" ;
109+ let actionElement = item . actionElement ;
110+ if ( ! item . actionElement && item . createActionElement ) {
111+ actionElement = item . createActionElement ( ) ;
112+ }
113+ const tagName = actionElement ?. classList . contains ( "b3-switch" ) ? "label" : "div" ;
114+ if ( typeof item . direction === "undefined" ) {
115+ item . direction = ( ! actionElement || "TEXTAREA" === actionElement . tagName ) ? "row" : "column" ;
116+ }
117+ if ( item . direction === "row" ) {
118+ html = `<${ tagName } class="b3-label">
119+ <div class="fn__block">
120+ ${ item . title }
121+ ${ item . description ? `<div class="b3-label__text">${ item . description } </div>` : "" }
122+ <div class="fn__hr"></div>
123+ </div>
124+ </${ tagName } >` ;
125+ } else {
126+ html = `<${ tagName } class="fn__flex b3-label config__item">
127+ <div class="fn__flex-1">
128+ ${ item . title }
129+ ${ item . description ? `<div class="b3-label__text">${ item . description } </div>` : "" }
130+ </div>
131+ <span class="fn__space${ actionElement ? "" : " fn__none" } "></span>
132+ </${ tagName } >` ;
133+ }
134+ contentElement . insertAdjacentHTML ( "beforeend" , html ) ;
135+ if ( actionElement ) {
136+ if ( [ "INPUT" , "TEXTAREA" ] . includes ( actionElement . tagName ) ) {
137+ dialog . bindInput ( actionElement as HTMLInputElement , ( ) => {
138+ ( dialog . element . querySelector ( ".b3-dialog__action [data-type='confirm']" ) as HTMLElement ) . dispatchEvent ( new CustomEvent ( "click" ) ) ;
139+ } ) ;
140+ }
141+ if ( item . direction === "row" ) {
142+ contentElement . lastElementChild . lastElementChild . insertAdjacentElement ( "beforeend" , actionElement ) ;
143+ actionElement . classList . add ( "fn__block" ) ;
144+ } else {
145+ actionElement . classList . remove ( "fn__block" ) ;
146+ actionElement . classList . add ( "fn__flex-center" , "fn__size200" ) ;
147+ contentElement . lastElementChild . insertAdjacentElement ( "beforeend" , actionElement ) ;
148+ }
149+ }
150+ } ) ;
151+
152+ ( dialog . element . querySelector ( ".b3-dialog__action [data-type='cancel']" ) as HTMLElement ) . addEventListener ( "click" , ( ) => {
153+ dialog . destroy ( ) ;
154+ } ) ;
155+ ( dialog . element . querySelector ( ".b3-dialog__action [data-type='confirm']" ) as HTMLElement ) . addEventListener ( "click" , ( ) => {
156+ this . data [ STORAGE_NAME ] . labelDisplay = ( dialog . element . querySelector ( "[data-type='labelDisplay']" ) as HTMLSelectElement ) . value ;
157+ this . saveData ( STORAGE_NAME , this . data [ STORAGE_NAME ] ) ;
158+ dialog . destroy ( ) ;
159+ } ) ;
160+ }
161+
162+ private async initSetting ( ) {
163+ await this . loadData ( STORAGE_NAME ) ;
164+ if ( ! this . data [ STORAGE_NAME ] ) {
165+ this . data [ STORAGE_NAME ] = {
166+ labelDisplay : "showLabelOnHover" ,
167+ } ;
168+ }
169+
170+ this . settingItems = [
171+ {
172+ title : "标签显示" ,
173+ direction : "column" ,
174+ description : "图像块右上角的标签显示(修改后需刷新文档生效)" ,
175+ createActionElement : ( ) => {
176+ const options = [ "noLabel" , "showLabelAlways" , "showLabelOnHover" ] ;
177+ const optionsHTML = options . map ( option => {
178+ const isSelected = String ( option ) === String ( this . data [ STORAGE_NAME ] . labelDisplay ) ;
179+ return `<option value="${ option } "${ isSelected ? " selected" : "" } >${ this . i18n [ option ] } </option>` ;
180+ } ) . join ( "" ) ;
181+ return HTMLToElement ( `<select class="b3-select fn__flex-center" data-type="labelDisplay">${ optionsHTML } </select>` ) ;
182+ } ,
183+ } ,
184+ ] ;
185+ }
82186
83187 private initMetaInfo ( ) {
84188 const frontEnd = getFrontend ( ) ;
@@ -129,11 +233,11 @@ export default class DrawioPlugin extends Plugin {
129233 return mutationObserver ;
130234 }
131235
132- public async getDrawioImageInfo ( imageURL : string ) : Promise < DrawioImageInfo | null > {
236+ public async getDrawioImageInfo ( imageURL : string , reload : boolean ) : Promise < DrawioImageInfo | null > {
133237 const imageURLRegex = / ^ a s s e t s \/ .+ \. s v g $ / ;
134238 if ( ! imageURLRegex . test ( imageURL ) ) return null ;
135239
136- const svgContent = await this . getDrawioImage ( imageURL ) ;
240+ const svgContent = await this . getDrawioImage ( imageURL , reload ) ;
137241 if ( ! svgContent ) return null ;
138242
139243 if ( ! svgContent . includes ( "mxfile" ) ) return null ;
@@ -175,8 +279,8 @@ export default class DrawioPlugin extends Plugin {
175279 } ) ;
176280 }
177281
178- public async getDrawioImage ( imageURL : string ) : Promise < string > {
179- const response = await fetch ( imageURL , { cache : 'reload' } ) ;
282+ public async getDrawioImage ( imageURL : string , reload : boolean ) : Promise < string > {
283+ const response = await fetch ( imageURL , { cache : reload ? 'reload' : 'default ' } ) ;
180284 if ( ! response . ok ) return "" ;
181285 const svgContent = await response . text ( ) ;
182286 return svgContent ;
@@ -198,6 +302,8 @@ export default class DrawioPlugin extends Plugin {
198302 public updateAttrLabel ( imageInfo : DrawioImageInfo , blockElement : HTMLElement ) {
199303 if ( ! imageInfo ) return ;
200304
305+ if ( this . data [ STORAGE_NAME ] . labelDisplay === "noLabel" ) return ;
306+
201307 const attrElement = blockElement . querySelector ( ".protyle-attr" ) as HTMLDivElement ;
202308 if ( attrElement ) {
203309 const pageCount = ( imageInfo . data . match ( / n a m e = & q u o t ; / g) || [ ] ) . length ;
@@ -208,6 +314,9 @@ export default class DrawioPlugin extends Plugin {
208314 } else {
209315 labelElement = document . createElement ( "div" ) ;
210316 labelElement . classList . add ( "label--embed-drawio" ) ;
317+ if ( this . data [ STORAGE_NAME ] . labelDisplay === "showLabelAlways" ) {
318+ labelElement . classList . add ( "label--embed-drawio--always" ) ;
319+ }
211320 labelElement . innerHTML = labelHTML ;
212321 attrElement . prepend ( labelElement ) ;
213322 }
@@ -218,7 +327,7 @@ export default class DrawioPlugin extends Plugin {
218327 const selectedElement = detail . element ;
219328 const imageElement = selectedElement . querySelector ( "img" ) as HTMLImageElement ;
220329 const imageURL = imageElement . dataset . src ;
221- this . getDrawioImageInfo ( imageURL ) . then ( ( imageInfo : DrawioImageInfo ) => {
330+ this . getDrawioImageInfo ( imageURL , true ) . then ( ( imageInfo : DrawioImageInfo ) => {
222331 if ( imageInfo ) {
223332 window . siyuan . menus . menu . addItem ( {
224333 id : "edit-drawio" ,
@@ -255,7 +364,7 @@ export default class DrawioPlugin extends Plugin {
255364 <div class="edit-dialog-header resize__move"></div>
256365 <div class="edit-dialog-container">
257366 <div class="edit-dialog-editor">
258- <iframe src="/plugins/siyuan-embed-drawio/draw/index.html?proto=json&embed=1${ this . isMobile ? "&ui=min" : "" } &lang=${ window . siyuan . config . lang . split ( '_' ) [ 0 ] } "></iframe>
367+ <iframe src="/plugins/siyuan-embed-drawio/draw/index.html?proto=json&embed=1${ this . isMobile ? "&ui=min" : "" } &lang=${ window . siyuan . config . lang . split ( '_' ) [ 0 ] } "></iframe>
259368 </div>
260369 <div class="fn__hr--b"></div>
261370 </div>
@@ -294,8 +403,8 @@ export default class DrawioPlugin extends Plugin {
294403
295404 const onSave = ( message : any ) => {
296405 postMessage ( {
297- action : 'export' ,
298- format :'xmlsvg'
406+ action : 'export' ,
407+ format : 'xmlsvg'
299408 } ) ;
300409 }
301410
@@ -310,7 +419,7 @@ export default class DrawioPlugin extends Plugin {
310419
311420 this . updateDrawioImage ( imageInfo , ( ) => {
312421 postMessage ( {
313- action : 'status' ,
422+ action : 'status' ,
314423 messageKey : 'allChangesSaved' ,
315424 modified : false
316425 } ) ;
@@ -332,13 +441,10 @@ export default class DrawioPlugin extends Plugin {
332441 }
333442
334443 const messageEventHandler = ( event ) => {
335- if ( event . data && event . data . length > 0 )
336- {
337- try
338- {
444+ if ( event . data && event . data . length > 0 ) {
445+ try {
339446 var message = JSON . parse ( event . data ) ;
340- if ( message != null )
341- {
447+ if ( message != null ) {
342448 // console.log(message.event);
343449 if ( message . event == "init" ) {
344450 onInit ( message ) ;
@@ -354,8 +460,7 @@ export default class DrawioPlugin extends Plugin {
354460 }
355461 }
356462 }
357- catch ( err )
358- {
463+ catch ( err ) {
359464 console . error ( err ) ;
360465 }
361466 }
@@ -373,7 +478,7 @@ export default class DrawioPlugin extends Plugin {
373478 <div class="edit-dialog-header resize__move"></div>
374479 <div class="edit-dialog-container">
375480 <div class="edit-dialog-editor">
376- <iframe src="/plugins/siyuan-embed-drawio/draw/index.html?proto=json&embed=1${ this . isMobile ? "&ui=min" : "" } &lang=${ window . siyuan . config . lang . split ( '_' ) [ 0 ] } &lightbox=1"></iframe>
481+ <iframe src="/plugins/siyuan-embed-drawio/draw/index.html?proto=json&embed=1${ this . isMobile ? "&ui=min" : "" } &lang=${ window . siyuan . config . lang . split ( '_' ) [ 0 ] } &lightbox=1"></iframe>
377482 </div>
378483 <div class="fn__hr--b"></div>
379484 </div>
@@ -411,21 +516,17 @@ export default class DrawioPlugin extends Plugin {
411516 }
412517
413518 const messageEventHandler = ( event ) => {
414- if ( event . data && event . data . length > 0 )
415- {
416- try
417- {
519+ if ( event . data && event . data . length > 0 ) {
520+ try {
418521 var message = JSON . parse ( event . data ) ;
419- if ( message != null )
420- {
522+ if ( message != null ) {
421523 // console.log(message.event);
422524 if ( message . event == "init" ) {
423525 onInit ( message ) ;
424526 }
425527 }
426528 }
427- catch ( err )
428- {
529+ catch ( err ) {
429530 console . error ( err ) ;
430531 }
431532 }
0 commit comments