1- import { Plugin } from "obsidian" ;
1+ import { App , Plugin } from "obsidian" ;
22
3- let lastPos : DOMRect | null = null ;
4- let styleCount = 0 ;
5- export default class NinjaCursorPlugin extends Plugin {
6- wrapperElement : HTMLDivElement ;
3+
4+ class NinjaCursorForWindow {
5+
6+ lastPos : DOMRect | null = null ;
7+ styleCount = 0 ;
8+ wrapperElement : HTMLDivElement | null ;
79 cursorElement : HTMLSpanElement ;
10+ app : App ;
11+ bufferedDocument : Document ;
12+ bufferedWindow : Window ;
813
9- async onload ( ) {
10- this . wrapperElement = document . createElement ( "div" ) ;
14+ constructor ( app : App , aw : Window , ad : Document , registerDomEvent : CallableFunction ) {
15+ this . app = app ;
16+ // buffering
17+ this . bufferedWindow = aw ;
18+ this . bufferedDocument = ad ;
19+ this . wrapperElement = ad . createElement ( "div" ) ;
1120 this . wrapperElement . addClass ( "cursorWrapper" ) ;
12- this . cursorElement = document . createElement ( "span" ) ;
21+ this . cursorElement = ad . createElement ( "span" ) ;
1322 this . wrapperElement . appendChild ( this . cursorElement ) ;
14- document . body . appendChild ( this . wrapperElement ) ;
15- const root = document . documentElement ;
16- root . style . setProperty ( "--cursor-x1" , `${ 0 } ` ) ;
17- root . style . setProperty ( "--cursor-y1" , `${ 0 } ` ) ;
18- root . style . setProperty ( "--cursor-x2" , `${ 0 } ` ) ;
19- root . style . setProperty ( "--cursor-y2" , `${ 0 } ` ) ;
23+ ad . body . appendChild ( this . wrapperElement ) ;
2024 this . cursorElement . addClass ( "x-cursor" ) ;
25+ const root = ad . documentElement ;
2126
2227 const moveCursor = ( ) => {
23- const selection = window . getSelection ( ) ;
28+ const selection = aw . getSelection ( ) ;
2429 if ( ! selection ) {
2530 console . log ( "Could not find selection" ) ;
2631 return ;
@@ -33,7 +38,7 @@ export default class NinjaCursorPlugin extends Plugin {
3338 return ;
3439 }
3540 if ( rect . x == 0 && rect . y == 0 ) {
36- const textRange = document . createRange ( ) ;
41+ const textRange = ad . createRange ( ) ;
3742 textRange . setStart ( range . startContainer , range . startOffset ) ;
3843 textRange . setEndAfter ( range . startContainer ) ;
3944 let textRect = textRange . getBoundingClientRect ( ) ;
@@ -56,16 +61,16 @@ export default class NinjaCursorPlugin extends Plugin {
5661 }
5762 rect = textRect ;
5863 }
59- if ( lastPos == null ) {
60- lastPos = rect ;
64+ if ( this . lastPos == null ) {
65+ this . lastPos = rect ;
6166 return ;
6267 }
63- if ( lastPos . x == rect . x && lastPos . y == rect . y ) {
68+ if ( this . lastPos . x == rect . x && this . lastPos . y == rect . y ) {
6469 return ;
6570 }
66- styleCount = ( styleCount + 1 ) % 2 ;
67- const dx = rect . x - lastPos . x ;
68- const dy = lastPos . y - rect . y ;
71+ this . styleCount = ( this . styleCount + 1 ) % 2 ;
72+ const dx = rect . x - this . lastPos . x ;
73+ const dy = this . lastPos . y - rect . y ;
6974 const cursorDragAngle = Math . atan2 ( dx , dy ) + Math . PI / 2 ;
7075 const cursorDragDistance = Math . sqrt ( dx * dx + dy * dy ) ;
7176
@@ -86,37 +91,88 @@ export default class NinjaCursorPlugin extends Plugin {
8691 `${ cursorDragAngle } rad`
8792 ) ;
8893 root . style . setProperty ( "--cursor-height" , `${ rect . height } px` ) ;
89- root . style . setProperty ( "--cursor-x1" , `${ lastPos . x } px` ) ;
90- root . style . setProperty ( "--cursor-y1" , `${ lastPos . y } px` ) ;
94+ root . style . setProperty ( "--cursor-x1" , `${ this . lastPos . x } px` ) ;
95+ root . style . setProperty ( "--cursor-y1" , `${ this . lastPos . y } px` ) ;
9196 root . style . setProperty ( "--cursor-x2" , `${ rect . x } px` ) ;
9297 root . style . setProperty ( "--cursor-y2" , `${ rect . y } px` ) ;
9398 this . cursorElement . removeClass ( "x-cursor0" ) ;
9499 this . cursorElement . removeClass ( "x-cursor1" ) ;
95100 this . cursorElement . getAnimations ( ) . forEach ( ( anim ) => anim . cancel ( ) ) ;
96101
97- window . requestAnimationFrame ( ( time ) => {
98- window . requestAnimationFrame ( ( time ) => {
99- this . cursorElement . addClass ( `x-cursor${ styleCount } ` ) ;
100- lastPos = rect ;
101- } ) ;
102+ aw . requestAnimationFrame ( ( time ) => {
103+ this . cursorElement . addClass ( `x-cursor${ this . styleCount } ` ) ;
104+ this . lastPos = rect ;
102105 } ) ;
103106 } ;
104- this . registerDomEvent ( window , "keydown" , ( ev ) => {
107+
108+
109+ registerDomEvent ( aw , "keydown" , ( ) => {
110+ moveCursor ( ) ;
111+ } ) ;
112+ registerDomEvent ( aw , "keyup" , ( ) => {
113+ moveCursor ( ) ;
114+ } ) ;
115+ registerDomEvent ( aw , "mousedown" , ( ) => {
105116 moveCursor ( ) ;
106117 } ) ;
107- this . registerDomEvent ( window , "keyup " , ( ev ) => {
118+ registerDomEvent ( aw , "mouseup " , ( ) => {
108119 moveCursor ( ) ;
109120 } ) ;
110- this . registerDomEvent ( window , "mousedown " , ( ) => {
121+ registerDomEvent ( aw , "touchend " , ( ) => {
111122 moveCursor ( ) ;
112123 } ) ;
124+ registerDomEvent ( aw , "touchstart" , ( ) => {
125+ moveCursor ( ) ;
126+ } ) ;
127+ }
128+
129+ unload ( ) {
130+ if ( this . wrapperElement ) {
131+ const doc = this . wrapperElement . doc ;
132+ if ( doc ) {
133+ doc . body . removeChild ( this . wrapperElement ) ;
134+ this . wrapperElement = null ;
135+ }
136+ }
137+ }
138+
139+
140+ }
141+ export default class NinjaCursorPlugin extends Plugin {
142+
143+ Cursors : NinjaCursorForWindow [ ] = [ ] ;
144+
145+ async onload ( ) {
146+
147+
148+ this . registerEvent ( this . app . workspace . on ( "window-open" , ( win ) => {
149+ console . log ( "Open by window-open" )
150+ const exist = this . Cursors . find ( e => e . bufferedWindow == win . win ) ;
151+ if ( ! exist ) {
152+ const w = new NinjaCursorForWindow ( app , win . win , win . doc , this . registerDomEvent . bind ( this ) ) ;
153+ this . Cursors . push ( w ) ;
154+ }
155+ } ) ) ;
156+ this . registerEvent ( this . app . workspace . on ( "window-close" , ( win ) => {
157+ const target = this . Cursors . find ( e => e . bufferedWindow == win . win ) ;
158+ if ( target ) {
159+ target . unload ( ) ;
160+ this . Cursors . remove ( target ) ;
161+ }
162+ } ) ) ;
163+
164+ console . log ( "Open by init" )
165+ const w = new NinjaCursorForWindow ( app , window , document , this . registerDomEvent . bind ( this ) ) ;
166+ this . Cursors . push ( w ) ;
113167 }
114168
115169 onunload ( ) {
116- document . body . removeChild ( this . wrapperElement ) ;
170+ for ( const v of this . Cursors ) {
171+ v . unload ( ) ;
172+ }
117173 }
118174
119- async loadSettings ( ) { }
175+ async loadSettings ( ) { }
120176
121- async saveSettings ( ) { }
177+ async saveSettings ( ) { }
122178}
0 commit comments