@@ -11,7 +11,7 @@ import {
1111 IModel ,
1212 Setting ,
1313 fetchPost ,
14- Protyle , openWindow
14+ Protyle , openWindow , IOperation
1515} from "siyuan" ;
1616import "@/index.scss" ;
1717
@@ -68,7 +68,7 @@ export default class PluginSample extends Plugin {
6868 } ) ;
6969
7070 const statusIconTemp = document . createElement ( "template" ) ;
71- statusIconTemp . innerHTML = `<div class="toolbar__item b3-tooltips b3-tooltips__w " aria-label="Remove plugin-sample Data">
71+ statusIconTemp . innerHTML = `<div class="toolbar__item ariaLabel " aria-label="Remove plugin-sample Data">
7272 <svg>
7373 <use xlink:href="#iconTrashcan"></use>
7474 </svg>
@@ -85,27 +85,6 @@ export default class PluginSample extends Plugin {
8585 element : statusIconTemp . content . firstElementChild as HTMLElement ,
8686 } ) ;
8787
88- let tabDiv = document . createElement ( "div" ) ;
89- new HelloExample ( {
90- target : tabDiv ,
91- props : {
92- app : this . app ,
93- }
94- } ) ;
95- this . customTab = this . addTab ( {
96- type : TAB_TYPE ,
97- init ( ) {
98- this . element . appendChild ( tabDiv ) ;
99- console . log ( this . element ) ;
100- } ,
101- beforeDestroy ( ) {
102- console . log ( "before destroy tab:" , TAB_TYPE ) ;
103- } ,
104- destroy ( ) {
105- console . log ( "destroy tab:" , TAB_TYPE ) ;
106- }
107- } ) ;
108-
10988 this . addCommand ( {
11089 langKey : "showDialog" ,
11190 hotkey : "⇧⌘O" ,
@@ -244,6 +223,26 @@ export default class PluginSample extends Plugin {
244223 // this.loadData(STORAGE_NAME);
245224 this . settingUtils . load ( ) ;
246225 console . log ( `frontend: ${ getFrontend ( ) } ; backend: ${ getBackend ( ) } ` ) ;
226+ let tabDiv = document . createElement ( "div" ) ;
227+ new HelloExample ( {
228+ target : tabDiv ,
229+ props : {
230+ app : this . app ,
231+ }
232+ } ) ;
233+ this . customTab = this . addTab ( {
234+ type : TAB_TYPE ,
235+ init ( ) {
236+ this . element . appendChild ( tabDiv ) ;
237+ console . log ( this . element ) ;
238+ } ,
239+ beforeDestroy ( ) {
240+ console . log ( "before destroy tab:" , TAB_TYPE ) ;
241+ } ,
242+ destroy ( ) {
243+ console . log ( "destroy tab:" , TAB_TYPE ) ;
244+ }
245+ } ) ;
247246 }
248247
249248 async onunload ( ) {
@@ -272,19 +271,38 @@ export default class PluginSample extends Plugin {
272271 } ) ;
273272 }
274273
274+ private eventBusPaste ( event : any ) {
275+ // 如果需异步处理请调用 preventDefault, 否则会进行默认处理
276+ event . preventDefault ( ) ;
277+ // 如果使用了 preventDefault,必须调用 resolve,否则程序会卡死
278+ event . detail . resolve ( {
279+ textPlain : event . detail . textPlain . trim ( ) ,
280+ } ) ;
281+ }
282+
275283 private eventBusLog ( { detail } : any ) {
276284 console . log ( detail ) ;
277285 }
278286
279287 private blockIconEvent ( { detail } : any ) {
280- const ids : string [ ] = [ ] ;
281- detail . blockElements . forEach ( ( item : HTMLElement ) => {
282- ids . push ( item . getAttribute ( "data-node-id" ) ) ;
283- } ) ;
284288 detail . menu . addItem ( {
285289 iconHTML : "" ,
286- type : "readonly" ,
287- label : "IDs<br>" + ids . join ( "<br>" ) ,
290+ label : this . i18n . removeSpace ,
291+ click : ( ) => {
292+ const doOperations : IOperation [ ] = [ ] ;
293+ detail . blockElements . forEach ( ( item : HTMLElement ) => {
294+ const editElement = item . querySelector ( '[contenteditable="true"]' ) ;
295+ if ( editElement ) {
296+ editElement . textContent = editElement . textContent . replace ( / / g, "" ) ;
297+ doOperations . push ( {
298+ id : item . dataset . nodeId ,
299+ data : item . outerHTML ,
300+ action : "update"
301+ } ) ;
302+ }
303+ } ) ;
304+ detail . protyle . getInstance ( ) . transaction ( doOperations ) ;
305+ }
288306 } ) ;
289307 }
290308
@@ -488,9 +506,15 @@ export default class PluginSample extends Plugin {
488506 }
489507 } , {
490508 icon : "iconSelect" ,
491- label : "On loaded-protyle" ,
509+ label : "On loaded-protyle-static " ,
492510 click : ( ) => {
493- this . eventBus . on ( "loaded-protyle" , this . eventBusLog ) ;
511+ this . eventBus . on ( "loaded-protyle-static" , this . eventBusLog ) ;
512+ }
513+ } , {
514+ icon : "iconClose" ,
515+ label : "Off loaded-protyle-static" ,
516+ click : ( ) => {
517+ this . eventBus . off ( "loaded-protyle-static" , this . eventBusLog ) ;
494518 }
495519 } , {
496520 icon : "iconSelect" ,
@@ -516,11 +540,17 @@ export default class PluginSample extends Plugin {
516540 click : ( ) => {
517541 this . eventBus . off ( "destroy-protyle" , this . eventBusLog ) ;
518542 }
543+ } , {
544+ icon : "iconSelect" ,
545+ label : "On open-menu-doctree" ,
546+ click : ( ) => {
547+ this . eventBus . on ( "open-menu-doctree" , this . eventBusLog ) ;
548+ }
519549 } , {
520550 icon : "iconClose" ,
521- label : "Off loaded-protyle " ,
551+ label : "Off open-menu-doctree " ,
522552 click : ( ) => {
523- this . eventBus . off ( "loaded-protyle " , this . eventBusLog ) ;
553+ this . eventBus . off ( "open-menu-doctree " , this . eventBusLog ) ;
524554 }
525555 } , {
526556 icon : "iconSelect" ,
@@ -630,6 +660,18 @@ export default class PluginSample extends Plugin {
630660 click : ( ) => {
631661 this . eventBus . off ( "input-search" , this . eventBusLog ) ;
632662 }
663+ } , {
664+ icon : "iconSelect" ,
665+ label : "On paste" ,
666+ click : ( ) => {
667+ this . eventBus . on ( "paste" , this . eventBusPaste ) ;
668+ }
669+ } , {
670+ icon : "iconClose" ,
671+ label : "Off paste" ,
672+ click : ( ) => {
673+ this . eventBus . off ( "paste" , this . eventBusPaste ) ;
674+ }
633675 } , {
634676 icon : "iconSelect" ,
635677 label : "On open-siyuan-url-plugin" ,
0 commit comments