1
1
import {
2
2
addIcon ,
3
3
Component ,
4
+ editorLivePreviewField ,
5
+ livePreviewState ,
4
6
MarkdownPostProcessor ,
5
7
MarkdownPostProcessorContext ,
6
8
MarkdownPreviewRenderer ,
@@ -71,6 +73,14 @@ declare module "obsidian" {
71
73
namespace MarkdownPreviewRenderer {
72
74
function unregisterCodeBlockPostProcessor ( lang : string ) : void ;
73
75
}
76
+ interface Editor {
77
+ cm : {
78
+ state : EditorState ;
79
+ focus : ( ) => void ;
80
+ posAtDOM : ( el : HTMLElement ) => number ;
81
+ dispatch : ( tr : TransactionSpec ) => void ;
82
+ } ;
83
+ }
74
84
}
75
85
76
86
import AdmonitionSetting from "./settings" ;
@@ -79,6 +89,7 @@ import { InsertAdmonitionModal } from "./modal";
79
89
import { IconName } from "@fortawesome/fontawesome-svg-core" ;
80
90
import CalloutManager from "./callout/manager" ;
81
91
import { AdmonitionSuggest } from "./suggest/suggest" ;
92
+ import { EditorState , TransactionSpec } from "@codemirror/state" ;
82
93
83
94
const DEFAULT_APP_SETTINGS : AdmonitionSettings = {
84
95
userAdmonitions : { } ,
@@ -354,6 +365,28 @@ ${editor.getDoc().getSelection()}
354
365
) ;
355
366
}
356
367
el . replaceWith ( admonitionElement ) ;
368
+
369
+ const view = app . workspace . getActiveViewOfType ( MarkdownView ) ;
370
+ //hack
371
+ //@ts -ignore
372
+ if ( view ?. editor ?. cm ?. state ?. field ( editorLivePreviewField ) ) {
373
+ const editor = view . editor . cm ;
374
+ admonitionElement . onClickEvent ( ( ev ) => {
375
+ if ( ev . defaultPrevented || ev . detail > 1 || ev . shiftKey )
376
+ return ;
377
+ try {
378
+ const pos = editor . posAtDOM ( admonitionElement ) ;
379
+ editor . focus ( ) ;
380
+ editor . dispatch ( {
381
+ selection : {
382
+ head : pos ,
383
+ anchor : pos
384
+ }
385
+ } ) ;
386
+ } catch ( e ) { }
387
+ } ) ;
388
+ }
389
+
357
390
return admonitionElement ;
358
391
} catch ( e ) {
359
392
console . error ( e ) ;
0 commit comments