Skip to content

Commit

Permalink
Merge pull request #3832 from udecode/feat/comments-initial-fragment
Browse files Browse the repository at this point in the history
Store `initialFragment` on TComment
  • Loading branch information
zbeyens authored Dec 8, 2024
2 parents d8c3510 + 1d8473b commit 90c2c21
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/nasty-eyes-smile.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@udecode/plate-comments': minor
---

Add an optional `initialFragment` property to `TComment`, which is populated with the fragment of text the comment is initially added to.
26 changes: 26 additions & 0 deletions packages/comments/src/lib/queries/getCommentFragment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import type { Range } from 'slate';

import {
type SlateEditor,
getEndPoint,
getFragment,
getStartPoint,
} from '@udecode/plate-common';

import { getCommentNodesById } from './getCommentNodesById';

export const getCommentFragment = (editor: SlateEditor, id: string) => {
const nodes = getCommentNodesById(editor, id);

if (nodes.length === 0) return;

const firstNodePath = nodes[0][1];
const lastNodePath = nodes.at(-1)![1];

const range: Range = {
anchor: getStartPoint(editor, firstNodePath),
focus: getEndPoint(editor, lastNodePath),
};

return getFragment(editor, range);
};
1 change: 1 addition & 0 deletions packages/comments/src/lib/queries/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@

export * from './findCommentNode';
export * from './findCommentNodeById';
export * from './getCommentFragment';
export * from './getCommentNodeEntries';
export * from './getCommentNodesById';
5 changes: 4 additions & 1 deletion packages/comments/src/lib/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { TText, Value } from '@udecode/plate-common';
import type { TDescendant, TText, Value } from '@udecode/plate-common';

export interface CommentUser {
id: string;
Expand All @@ -18,6 +18,9 @@ export interface TComment {
/** Slate value of the document. */
value: Value;

/** The fragment of text that the comment was originally added to. */
initialFragment?: TDescendant[];

/** Whether the comment is resolved. */
isResolved?: boolean;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ import { nanoid } from '@udecode/plate-common';
import {
createPrimitiveComponent,
useEditorPlugin,
useEditorRef,
} from '@udecode/plate-common/react';

import { getCommentFragment } from '../../lib/queries/getCommentFragment';
import { CommentsPlugin } from '../CommentsPlugin';
import {
SCOPE_ACTIVE_COMMENT,
Expand Down Expand Up @@ -38,6 +40,8 @@ export const useCommentNewSubmitButton = ({
newText,
submitButtonText,
}: ReturnType<typeof useCommentNewSubmitButtonState>) => {
const editor = useEditorRef();

return {
props: {
children: submitButtonText,
Expand All @@ -55,6 +59,7 @@ export const useCommentNewSubmitButton = ({
}
: {
id: activeCommentId!,
initialFragment: getCommentFragment(editor, activeCommentId!),
value: newValue,
}
);
Expand Down

0 comments on commit 90c2c21

Please sign in to comment.