@@ -60,7 +60,11 @@ class TaskTimerWidget extends WidgetType {
6060
6161 // Create a simple text-based widget
6262 this . dom = createDiv ( { cls : 'task-timer-widget' } ) ;
63- this . dom . style . cssText = 'margin: 2px 0; font-size: 0.9em; color: var(--text-muted);' ;
63+ this . dom . style . cssText = 'display: block; margin: 4px 0; padding: 2px 0; font-size: 0.9em; color: var(--text-muted);' ;
64+
65+ // Add debug info
66+ console . log ( "[TaskTimer] Creating widget for line" , this . lineFrom , "blockId:" , this . existingBlockId ) ;
67+
6468 this . updateTimerState ( ) ;
6569 this . createContent ( ) ;
6670 return this . dom ;
@@ -82,6 +86,7 @@ class TaskTimerWidget extends WidgetType {
8286 startSpan . addEventListener ( 'click' , ( e ) => {
8387 e . preventDefault ( ) ;
8488 e . stopPropagation ( ) ;
89+ console . log ( "[TaskTimer] Start button clicked" ) ;
8590 this . startTimer ( ) ;
8691 } ) ;
8792 } else {
@@ -132,26 +137,52 @@ class TaskTimerWidget extends WidgetType {
132137 // If no existing block ID, generate one and insert it
133138 if ( ! taskId ) {
134139 const blockId = this . timerManager . generateBlockId ( this . settings . blockRefPrefix ) ;
135- taskId = `${ this . filePath } #^${ blockId } ` ;
136140
137- // Insert block reference into the task line
138- const editorInfo = this . state . field ( editorInfoField ) ;
139- if ( editorInfo ?. editor ) {
141+ // Get the active editor through the app
142+ const activeView = ( window as any ) . app ?. workspace ?. getActiveViewOfType ( ( window as any ) . app ?. viewRegistry ?. getViewCreatorByType ( "markdown" ) ) ;
143+ const editor = activeView ?. editor ;
144+
145+ console . log ( "[TaskTimer] Active view:" , activeView ) ;
146+ console . log ( "[TaskTimer] Editor:" , editor ) ;
147+
148+ if ( editor ) {
140149 const line = this . state . doc . lineAt ( this . lineFrom ) ;
141150 const lineText = line . text ;
142151 const updatedText = lineText . trimEnd ( ) + ` ^${ blockId } ` ;
143152
144- editorInfo . editor . replaceRange ( updatedText ,
145- { line : line . number - 1 , ch : 0 } ,
146- { line : line . number - 1 , ch : lineText . length }
147- ) ;
153+ console . log ( `[TaskTimer] Inserting block ID at line ${ line . number } : ^${ blockId } ` ) ;
154+ console . log ( "[TaskTimer] Original text:" , lineText ) ;
155+ console . log ( "[TaskTimer] Updated text:" , updatedText ) ;
156+
157+ try {
158+ // Use Obsidian's Editor API
159+ const lineNumber = line . number - 1 ;
160+ editor . replaceRange ( updatedText ,
161+ { line : lineNumber , ch : 0 } ,
162+ { line : lineNumber , ch : lineText . length }
163+ ) ;
164+
165+ // Update our local reference
166+ this . existingBlockId = blockId ;
167+ taskId = `${ this . filePath } #^${ blockId } ` ;
168+ } catch ( err ) {
169+ console . error ( "[TaskTimer] Error replacing text:" , err ) ;
170+ return ;
171+ }
172+ } else {
173+ console . error ( "[TaskTimer] No editor available to insert block ID" ) ;
174+ // Try alternative approach
175+ const editorInfo = this . state . field ( editorInfoField ) ;
176+ console . log ( "[TaskTimer] EditorInfo from state:" , editorInfo ) ;
177+ return ;
148178 }
149179 }
150180
151181 console . log ( `[TaskTimer] Starting timer for task: ${ taskId } ` ) ;
152182 this . timerManager . startTimer ( taskId ) ;
153183 this . startRealtimeUpdates ( ) ;
154184 this . updateTimerState ( ) ;
185+ this . refreshUI ( ) ; // Force UI refresh
155186 console . log ( "[TaskTimer] Timer started successfully" ) ;
156187 } catch ( error ) {
157188 console . error ( "[TaskTimer] Error starting timer:" , error ) ;
@@ -437,11 +468,11 @@ function createTaskTimerDecorations(state: EditorState): DecorationSet {
437468 file . path ,
438469 existingBlockId
439470 ) ,
440- side : - 1 ,
471+ side : - 1 , // Place before the line
441472 block : true // This is now allowed in StateField
442473 } ) ;
443474
444- // Add decoration at the start of the line
475+ // Add decoration at the start of the line (this will appear above the task)
445476 decorations . push ( timerDeco . range ( line . from ) ) ;
446477 console . log ( "[TaskTimer] Added timer decoration for line:" , i ) ;
447478 }
0 commit comments