Skip to content

Commit dd5a813

Browse files
committed
Fixed Tiptap Velt Comments
1 parent a5ae8d2 commit dd5a813

File tree

2 files changed

+36
-5
lines changed

2 files changed

+36
-5
lines changed

apps/javascript/crdt/text-editors/tiptap/tiptap-crdt-demo/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"@tiptap/starter-kit": "^3.14.0",
2121
"@veltdev/client": "^4.6.10",
2222
"@veltdev/tiptap-crdt": "^4.5.9-beta.2",
23+
"@veltdev/tiptap-velt-comments": "^4.5.8-beta.3",
2324
"@veltdev/types": "^4.6.10",
2425
"y-prosemirror": "^1.2.12",
2526
"yjs": "^13.6.18"

apps/javascript/crdt/text-editors/tiptap/tiptap-crdt-demo/src/components/document/tiptap.js

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import BubbleMenu from '@tiptap/extension-bubble-menu';
1212
import CollaborationCaret from '@tiptap/extension-collaboration-caret';
1313
import { Mark, mergeAttributes } from '@tiptap/core';
1414
import { 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
1719
const 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

Comments
 (0)