Skip to content

Commit 792c439

Browse files
authored
fix: add bn-thread-orphaned CSS class to distinguish orphaned threads (#2737)
When a thread's referenced text is deleted from the document, the thread becomes orphaned. This adds a `bn-thread-orphaned` CSS class to the Thread component's card element so consumers can target these threads with custom styling or behavior. Closes #2735
1 parent f94e464 commit 792c439

2 files changed

Lines changed: 24 additions & 10 deletions

File tree

packages/react/src/components/Comments/Thread.tsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ export type ThreadProps = {
2222
* editor for replies, and add a `selected` CSS class to the thread.
2323
*/
2424
selected?: boolean;
25+
/**
26+
* A boolean flag for whether the thread is orphaned (i.e. the referenced text
27+
* has been deleted from the document). Adds a `bn-thread-orphaned` CSS class
28+
* to the thread.
29+
*/
30+
orphaned?: boolean;
2531
/**
2632
* The text in the editor that the thread refers to. See the
2733
* [`ThreadsSidebar`](https://github.com/TypeCellOS/BlockNote/tree/main/packages/react/src/components/Comments/ThreadsSidebar.tsx#L137)
@@ -55,6 +61,7 @@ export type ThreadProps = {
5561
export const Thread = ({
5662
thread,
5763
selected,
64+
orphaned,
5865
referenceText,
5966
maxCommentsBeforeCollapse,
6067
onFocus,
@@ -94,7 +101,7 @@ export const Thread = ({
94101

95102
return (
96103
<Components.Comments.Card
97-
className={"bn-thread"}
104+
className={mergeCSSClasses("bn-thread", orphaned && "bn-thread-orphaned")}
98105
headerText={referenceText}
99106
onFocus={onFocus}
100107
onBlur={onBlur}

packages/react/src/components/Comments/ThreadsSidebar.tsx

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ type ThreadItemProps = {
1313
editor: BlockNoteEditor<any, any, any>;
1414
maxCommentsBeforeCollapse?: number;
1515
referenceText: string;
16+
orphaned?: boolean;
1617
};
1718

1819
/**
@@ -25,6 +26,7 @@ const ThreadItem = React.memo(
2526
selectedThreadId,
2627
maxCommentsBeforeCollapse,
2728
referenceText,
29+
orphaned,
2830
}: ThreadItemProps) => {
2931
const comments = useExtension(CommentsExtension);
3032

@@ -76,6 +78,7 @@ const ThreadItem = React.memo(
7678
<Thread
7779
thread={thread}
7880
selected={thread.id === selectedThreadId}
81+
orphaned={orphaned}
7982
referenceText={referenceText}
8083
maxCommentsBeforeCollapse={maxCommentsBeforeCollapse}
8184
onFocus={onFocus}
@@ -206,27 +209,30 @@ export function ThreadsSidebar(props: {
206209
threadPositions,
207210
);
208211

209-
const ret: Array<{ thread: ThreadData; referenceText: string }> = [];
212+
const ret: Array<{
213+
thread: ThreadData;
214+
referenceText: string;
215+
orphaned: boolean;
216+
}> = [];
210217

211218
for (const thread of sortedThreads) {
219+
const threadPosition = threadPositions.get(thread.id);
220+
const orphaned = threadPosition === undefined;
221+
212222
if (!thread.resolved) {
213223
if (props.filter === "open" || props.filter === "all") {
214224
ret.push({
215225
thread,
216-
referenceText: getReferenceText(
217-
editor,
218-
threadPositions.get(thread.id),
219-
),
226+
referenceText: getReferenceText(editor, threadPosition),
227+
orphaned,
220228
});
221229
}
222230
} else {
223231
if (props.filter === "resolved" || props.filter === "all") {
224232
ret.push({
225233
thread,
226-
referenceText: getReferenceText(
227-
editor,
228-
threadPositions.get(thread.id),
229-
),
234+
referenceText: getReferenceText(editor, threadPosition),
235+
orphaned,
230236
});
231237
}
232238
}
@@ -244,6 +250,7 @@ export function ThreadsSidebar(props: {
244250
selectedThreadId={selectedThreadId}
245251
editor={editor}
246252
referenceText={thread.referenceText}
253+
orphaned={thread.orphaned}
247254
maxCommentsBeforeCollapse={props.maxCommentsBeforeCollapse}
248255
/>
249256
))}

0 commit comments

Comments
 (0)