Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature(provider): implement comment tombstones for threads #877

Merged
merged 10 commits into from
Nov 20, 2024
33 changes: 27 additions & 6 deletions packages/provider/src/TiptapCollabProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,14 +151,15 @@ export class TiptapCollabProvider extends HocuspocusProvider {
return this.getYThreads().get(index)
}

createThread(data: Omit<TCollabThread, 'id' | 'createdAt' | 'updatedAt' | 'comments'>) {
createThread(data: Omit<TCollabThread, 'id' | 'createdAt' | 'updatedAt' | 'comments' | 'deletedComments'>) {
let createdThread: TCollabThread = {} as TCollabThread

this.document.transact(() => {
const thread = new Y.Map()
thread.set('id', uuidv4())
thread.set('createdAt', (new Date()).toISOString())
thread.set('comments', new Y.Array())
thread.set('deletedComments', new Y.Array())

this.getYThreads().push([thread])
createdThread = this.updateThread(String(thread.get('id')), data)
Expand Down Expand Up @@ -205,14 +206,24 @@ export class TiptapCollabProvider extends HocuspocusProvider {
this.getYThreads().delete(index, 1)
}

getThreadComments(threadId: TCollabThread['id']): TCollabComment[] | null {
getThreadComments(threadId: TCollabThread['id'], deleted?: boolean): TCollabComment[] | null {
const index = this.getThreadIndex(threadId)

if (index === null) {
return null
}

return this.getThread(threadId)?.comments ?? []
return (!deleted ? this.getThread(threadId)?.comments : this.getThread(threadId)?.deletedComments) ?? []
}

getAllThreadComments(threadId: TCollabThread['id']): TCollabComment[] | null {
const index = this.getThreadIndex(threadId)

if (index === null) {
return null
}

return [...this.getThreadComments(threadId) ?? [], ...this.getThreadComments(threadId, true) ?? []].sort((a, b) => a.createdAt - b.createdAt)
}

getThreadComment(threadId: TCollabThread['id'], commentId: TCollabComment['id']): TCollabComment | null {
Expand All @@ -222,7 +233,7 @@ export class TiptapCollabProvider extends HocuspocusProvider {
return null
}

return this.getThread(threadId)?.comments.find(comment => comment.id === commentId) ?? null
return this.getAllThreadComments(threadId)?.find(comment => comment.id === commentId) ?? null
bdbch marked this conversation as resolved.
Show resolved Hide resolved
}

addComment(threadId: TCollabThread['id'], data: Omit<TCollabComment, 'id' | 'updatedAt' | 'createdAt'>) {
Expand Down Expand Up @@ -281,7 +292,7 @@ export class TiptapCollabProvider extends HocuspocusProvider {
return updatedThread
}

deleteComment(threadId: TCollabThread['id'], commentId: TCollabComment['id']) {
deleteComment(threadId: TCollabThread['id'], commentId: TCollabComment['id'], deleteThread?: boolean) {
bdbch marked this conversation as resolved.
Show resolved Hide resolved
const thread = this.getYThread(threadId)

if (thread === null) return null
Expand All @@ -297,12 +308,22 @@ export class TiptapCollabProvider extends HocuspocusProvider {

// if the first comment of a thread is deleted we also
// delete the thread itself as the source comment is gone
if (commentIndex === 0) {
if (commentIndex === 0 && deleteThread) {
this.deleteThread(threadId)
return
}

if (commentIndex > 0) {
const comment = thread.get('comments').get(commentIndex)
const newComment = new Y.Map()

newComment.set('id', comment.get('id'))
newComment.set('createdAt', comment.get('createdAt'))
newComment.set('updatedAt', (new Date()).toISOString())
bdbch marked this conversation as resolved.
Show resolved Hide resolved
newComment.set('data', comment.get('data'))
newComment.set('content', null)
bdbch marked this conversation as resolved.
Show resolved Hide resolved

thread.get('deletedComments').push([newComment])
thread.get('comments').delete(commentIndex)
}

Expand Down
1 change: 1 addition & 0 deletions packages/provider/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ export type TCollabThread<Data = any, CommentData = any> = {
updatedAt: number;
resolvedAt?: string; // (new Date()).toISOString()
comments: TCollabComment<CommentData>[];
deletedComments: TCollabComment<CommentData>[];
data: Data
}

Expand Down