@@ -12,6 +12,8 @@ import BubbleMenu from '@tiptap/extension-bubble-menu';
1212import CollaborationCaret from '@tiptap/extension-collaboration-caret' ;
1313import { Mark , mergeAttributes } from '@tiptap/core' ;
1414import { createVeltTipTapStore } from '@veltdev/tiptap-crdt' ;
15+ import { TiptapVeltComments , addComment , renderComments } from '@veltdev/tiptap-velt-comments' ;
16+ import { getVeltClient } from '../../lib/velt.js' ;
1517
1618// Initial content for the editor - same as React version
1719const initialContent = `
@@ -248,13 +250,20 @@ function createBubbleMenuToolbar(editor) {
248250 />
249251 </svg>
250252 ` ;
251- commentButton . addEventListener ( 'click' , ( e ) => {
253+ // Use mousedown instead of click to preserve selection (per Velt docs)
254+ commentButton . addEventListener ( 'mousedown' , ( e ) => {
252255 e . preventDefault ( ) ;
253256 e . stopPropagation ( ) ;
254- // Trigger Velt comment if available
255- const veltCommentTool = document . querySelector ( 'velt-comment-tool' ) ;
256- if ( veltCommentTool ) {
257- veltCommentTool . click ( ) ;
257+ // [Velt] Trigger comment creation on selected text using Velt TipTap Comments
258+ console . log ( '[TipTap] Comment button clicked, selection:' , editor . state . selection ) ;
259+ console . log ( '[TipTap] Selection empty?' , editor . state . selection . empty ) ;
260+ // Store editorId reference for use in click handler
261+ const currentEditorId = window . __veltEditorId ;
262+ try {
263+ addComment ( { editor, editorId : currentEditorId } ) ;
264+ console . log ( '[TipTap] addComment called successfully with editorId:' , currentEditorId ) ;
265+ } catch ( err ) {
266+ console . error ( '[TipTap] Error calling addComment:' , err ) ;
258267 }
259268 } ) ;
260269
@@ -321,6 +330,9 @@ export async function createTipTapEditor(container, veltClient, editorId, user)
321330
322331 console . log ( '[TipTap] Store created, initializing editor...' ) ;
323332
333+ // Store editorId globally for comment handler access
334+ window . __veltEditorId = editorId ;
335+
324336 // Remove loading spinner
325337 container . removeChild ( loadingSpinner ) ;
326338
@@ -343,6 +355,8 @@ export async function createTipTapEditor(container, veltClient, editorId, user)
343355 InlineH1 ,
344356 InlineH2 ,
345357 InlineH3 ,
358+ // [Velt] TipTap extension for Velt comments - enables comment markers and selection tracking
359+ TiptapVeltComments ,
346360 // BubbleMenu extension - must be configured before editor creation
347361 BubbleMenu . configure ( {
348362 element : bubbleMenuElement ,
@@ -402,6 +416,22 @@ export async function createTipTapEditor(container, veltClient, editorId, user)
402416 }
403417 } , 1000 ) ;
404418
419+ // [Velt] Subscribe to comment annotations and render them in the editor
420+ const client = getVeltClient ( ) ;
421+ if ( client ) {
422+ const commentElement = client . getCommentElement ( ) ;
423+ commentElement . getAllCommentAnnotations ( ) . subscribe ( ( annotations ) => {
424+ if ( editor && annotations ?. length ) {
425+ console . log ( '[TipTap] Rendering' , annotations . length , 'comment annotations' ) ;
426+ renderComments ( {
427+ editor,
428+ editorId : editorId ,
429+ commentAnnotations : annotations ,
430+ } ) ;
431+ }
432+ } ) ;
433+ }
434+
405435 } catch ( error ) {
406436 console . error ( '[TipTap] Failed to initialize:' , error ) ;
407437
0 commit comments