@kendew-agency/react-feedback-layer 0.2.2 → 0.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +189 -15
- package/dist/{comment.types-CSGuc1D_.d.cts → comment.types-C0m3sQ_Q.d.cts} +70 -4
- package/dist/{comment.types-CSGuc1D_.d.ts → comment.types-C0m3sQ_Q.d.ts} +70 -4
- package/dist/index.cjs +115 -19
- package/dist/index.d.cts +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +114 -12
- package/dist/types.d.cts +2 -2
- package/dist/types.d.ts +2 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -40,9 +40,9 @@ import {
|
|
|
40
40
|
} from '@kendew-agency/react-feedback-layer';
|
|
41
41
|
|
|
42
42
|
function App() {
|
|
43
|
-
const handleConfirm = async (comments) => {
|
|
43
|
+
const handleConfirm = async (comments, projectId) => {
|
|
44
44
|
// Save comments to your database
|
|
45
|
-
await saveToDatabase(comments);
|
|
45
|
+
await saveToDatabase(comments, projectId);
|
|
46
46
|
};
|
|
47
47
|
|
|
48
48
|
const handleResolve = async (comments) => {
|
|
@@ -50,11 +50,16 @@ function App() {
|
|
|
50
50
|
await resolveInDatabase(comments);
|
|
51
51
|
};
|
|
52
52
|
|
|
53
|
+
const handleError = (error) => {
|
|
54
|
+
console.error('Comment error:', error.code, error.message);
|
|
55
|
+
};
|
|
56
|
+
|
|
53
57
|
return (
|
|
54
58
|
<CommentContextProvider
|
|
55
59
|
currentUser={{ name: 'John Doe', id: 'user-123' }}
|
|
56
60
|
onConfirm={handleConfirm}
|
|
57
61
|
onResolve={handleResolve}
|
|
62
|
+
onError={handleError}
|
|
58
63
|
initialComments={[]}
|
|
59
64
|
>
|
|
60
65
|
<CommentOverlay>
|
|
@@ -132,13 +137,13 @@ interface CommentOverlayProps {
|
|
|
132
137
|
};
|
|
133
138
|
|
|
134
139
|
// Callback when user confirms draft comments
|
|
135
|
-
onConfirm: (comments: ConfirmedComment[]) => Promise<void>;
|
|
140
|
+
onConfirm: (comments: ConfirmedComment[], projectId?: string) => Promise<void>;
|
|
136
141
|
|
|
137
142
|
// Callback when user resolves comments
|
|
138
143
|
onResolve: (comments: ConfirmedComment[]) => Promise<void>;
|
|
139
144
|
|
|
140
|
-
// Callback to handle errors
|
|
141
|
-
onError
|
|
145
|
+
// Callback to handle errors
|
|
146
|
+
onError: (error: KnownError) => void;
|
|
142
147
|
|
|
143
148
|
// Optional configuration for the comment layer
|
|
144
149
|
config?: {
|
|
@@ -152,6 +157,9 @@ interface CommentOverlayProps {
|
|
|
152
157
|
hideResolving?: boolean;
|
|
153
158
|
};
|
|
154
159
|
};
|
|
160
|
+
|
|
161
|
+
// Optional project ID to attach to all comments in this overlay
|
|
162
|
+
projectId?: string;
|
|
155
163
|
}
|
|
156
164
|
```
|
|
157
165
|
|
|
@@ -161,8 +169,9 @@ interface CommentOverlayProps {
|
|
|
161
169
|
<CommentContextProvider
|
|
162
170
|
currentUser={{ name: 'Jane Smith', id: '456', avatar: '/avatar.jpg' }}
|
|
163
171
|
initialComments={existingComments}
|
|
164
|
-
|
|
165
|
-
|
|
172
|
+
projectId="proj_123456789"
|
|
173
|
+
onConfirm={async (comments, projectId) => {
|
|
174
|
+
await api.createComments(comments, projectId);
|
|
166
175
|
}}
|
|
167
176
|
onResolve={async (comments) => {
|
|
168
177
|
await api.resolveComments(comments);
|
|
@@ -472,6 +481,10 @@ interface CommentContext {
|
|
|
472
481
|
confirmComments: () => Promise<{ error: Error | DOMException | null }>;
|
|
473
482
|
resolveComments: () => Promise<{ error: Error | DOMException | null }>;
|
|
474
483
|
updateCommentVisibility: (visibility: Partial<CommentVisibility>) => void;
|
|
484
|
+
triggerError: (error: KnownError) => void;
|
|
485
|
+
|
|
486
|
+
// Events - subscribe to comment lifecycle changes
|
|
487
|
+
events: CommentEvents;
|
|
475
488
|
}
|
|
476
489
|
```
|
|
477
490
|
|
|
@@ -540,6 +553,131 @@ function CustomCommentContent() {
|
|
|
540
553
|
}
|
|
541
554
|
```
|
|
542
555
|
|
|
556
|
+
#### `events` (via `useComments()`)
|
|
557
|
+
|
|
558
|
+
The `events` object provides a subscription-based system for reacting to comment lifecycle changes. Each event method accepts a callback and returns an `unsubscribe` function.
|
|
559
|
+
|
|
560
|
+
**Available Events:**
|
|
561
|
+
|
|
562
|
+
| Event | Trigger | Callback Signature |
|
|
563
|
+
|-------|---------|-------------------|
|
|
564
|
+
| `onFocusChange` | Focus moves to a different comment | `(commentId: string \| null, comment: CommentType \| undefined) => void` |
|
|
565
|
+
| `onCommentsConfirmed` | Draft comments are successfully saved | `(comments: CommentType[]) => void` |
|
|
566
|
+
| `onCommentsResolved` | Comments are successfully resolved | `(comments: CommentType[]) => void` |
|
|
567
|
+
| `onCommentDeleted` | A comment is deleted | `(commentId: string) => void` |
|
|
568
|
+
| `onCommentRegistered` | A new draft comment is created | `(comment: CommentType) => void` |
|
|
569
|
+
|
|
570
|
+
**Basic Usage:**
|
|
571
|
+
|
|
572
|
+
```tsx
|
|
573
|
+
import { useComments } from '@kendew-agency/react-feedback-layer';
|
|
574
|
+
import { useEffect } from 'react';
|
|
575
|
+
|
|
576
|
+
function CommentTracker() {
|
|
577
|
+
const { events } = useComments();
|
|
578
|
+
|
|
579
|
+
useEffect(() => {
|
|
580
|
+
const unsubscribe = events.onFocusChange((commentId, comment) => {
|
|
581
|
+
console.log('Focus changed to:', commentId);
|
|
582
|
+
if (comment) {
|
|
583
|
+
highlightElement(comment.position);
|
|
584
|
+
}
|
|
585
|
+
});
|
|
586
|
+
|
|
587
|
+
return unsubscribe;
|
|
588
|
+
}, [events]);
|
|
589
|
+
|
|
590
|
+
useEffect(() => {
|
|
591
|
+
const unsubscribe = events.onCommentsConfirmed((comments) => {
|
|
592
|
+
console.log(`${comments.length} comments saved`);
|
|
593
|
+
toast.success('Comments saved successfully!');
|
|
594
|
+
});
|
|
595
|
+
|
|
596
|
+
return unsubscribe;
|
|
597
|
+
}, [events]);
|
|
598
|
+
|
|
599
|
+
return null;
|
|
600
|
+
}
|
|
601
|
+
```
|
|
602
|
+
|
|
603
|
+
**Multiple Subscriptions:**
|
|
604
|
+
|
|
605
|
+
```tsx
|
|
606
|
+
function AnalyticsTracker() {
|
|
607
|
+
const { events } = useComments();
|
|
608
|
+
|
|
609
|
+
useEffect(() => {
|
|
610
|
+
const unsubs = [
|
|
611
|
+
events.onCommentRegistered((comment) => {
|
|
612
|
+
analytics.track('comment_created', {
|
|
613
|
+
position: comment.position,
|
|
614
|
+
user: comment.user?.name,
|
|
615
|
+
});
|
|
616
|
+
}),
|
|
617
|
+
events.onCommentsConfirmed((comments) => {
|
|
618
|
+
analytics.track('comments_confirmed', { count: comments.length });
|
|
619
|
+
}),
|
|
620
|
+
events.onCommentsResolved((comments) => {
|
|
621
|
+
analytics.track('comments_resolved', { count: comments.length });
|
|
622
|
+
}),
|
|
623
|
+
events.onCommentDeleted((id) => {
|
|
624
|
+
analytics.track('comment_deleted', { commentId: id });
|
|
625
|
+
}),
|
|
626
|
+
];
|
|
627
|
+
|
|
628
|
+
return () => unsubs.forEach((unsub) => unsub());
|
|
629
|
+
}, [events]);
|
|
630
|
+
|
|
631
|
+
return null;
|
|
632
|
+
}
|
|
633
|
+
```
|
|
634
|
+
|
|
635
|
+
**Syncing with External State:**
|
|
636
|
+
|
|
637
|
+
```tsx
|
|
638
|
+
function CommentSidebar() {
|
|
639
|
+
const { events, allComments } = useComments();
|
|
640
|
+
const [activeId, setActiveId] = useState<string | null>(null);
|
|
641
|
+
|
|
642
|
+
useEffect(() => {
|
|
643
|
+
return events.onFocusChange((commentId) => {
|
|
644
|
+
setActiveId(commentId);
|
|
645
|
+
// Scroll sidebar to the active comment
|
|
646
|
+
if (commentId) {
|
|
647
|
+
document.getElementById(`sidebar-${commentId}`)?.scrollIntoView({
|
|
648
|
+
behavior: 'smooth',
|
|
649
|
+
});
|
|
650
|
+
}
|
|
651
|
+
});
|
|
652
|
+
}, [events]);
|
|
653
|
+
|
|
654
|
+
return (
|
|
655
|
+
<aside className="comment-sidebar">
|
|
656
|
+
{allComments.map((comment) => (
|
|
657
|
+
<div
|
|
658
|
+
key={comment.id}
|
|
659
|
+
id={`sidebar-${comment.id}`}
|
|
660
|
+
className={comment.id === activeId ? 'active' : ''}
|
|
661
|
+
>
|
|
662
|
+
<p>{comment.content}</p>
|
|
663
|
+
<small>{comment.user?.name}</small>
|
|
664
|
+
</div>
|
|
665
|
+
))}
|
|
666
|
+
</aside>
|
|
667
|
+
);
|
|
668
|
+
}
|
|
669
|
+
```
|
|
670
|
+
|
|
671
|
+
**Type Definitions:**
|
|
672
|
+
|
|
673
|
+
```tsx
|
|
674
|
+
import type {
|
|
675
|
+
CommentEvents,
|
|
676
|
+
CommentEventMap,
|
|
677
|
+
Unsubscribe,
|
|
678
|
+
} from '@kendew-agency/react-feedback-layer/types';
|
|
679
|
+
```
|
|
680
|
+
|
|
543
681
|
### Types
|
|
544
682
|
|
|
545
683
|
Import types from the `/types` subpath:
|
|
@@ -570,6 +708,7 @@ type CommentType = {
|
|
|
570
708
|
resolvedAt?: Date;
|
|
571
709
|
status: "draft" | "published" | "resolving" | "resolved";
|
|
572
710
|
indicator?: Indicator | null;
|
|
711
|
+
projectId?: string;
|
|
573
712
|
};
|
|
574
713
|
```
|
|
575
714
|
|
|
@@ -675,10 +814,10 @@ function App() {
|
|
|
675
814
|
return (
|
|
676
815
|
<CommentContextProvider
|
|
677
816
|
currentUser={{ name: 'John Doe', id: '123' }}
|
|
678
|
-
onConfirm={async (comments) => {
|
|
817
|
+
onConfirm={async (comments, projectId) => {
|
|
679
818
|
const response = await fetch('/api/comments', {
|
|
680
819
|
method: 'POST',
|
|
681
|
-
body: JSON.stringify(comments),
|
|
820
|
+
body: JSON.stringify({ comments, projectId }),
|
|
682
821
|
});
|
|
683
822
|
if (!response.ok) {
|
|
684
823
|
throw new Error('Failed to save comments');
|
|
@@ -749,6 +888,7 @@ function App() {
|
|
|
749
888
|
subscription={subscription}
|
|
750
889
|
onConfirm={saveComments}
|
|
751
890
|
onResolve={resolveComments}
|
|
891
|
+
onError={(error) => console.error(error)}
|
|
752
892
|
>
|
|
753
893
|
{/* ... */}
|
|
754
894
|
</CommentContextProvider>
|
|
@@ -919,6 +1059,27 @@ function VisibilityControls() {
|
|
|
919
1059
|
|
|
920
1060
|
## Advanced Patterns
|
|
921
1061
|
|
|
1062
|
+
### Project ID
|
|
1063
|
+
|
|
1064
|
+
Attach a project ID to all comments created within an overlay. This is useful for multi-project setups where you need to associate comments with a specific project in your database.
|
|
1065
|
+
|
|
1066
|
+
```tsx
|
|
1067
|
+
<CommentContextProvider
|
|
1068
|
+
currentUser={currentUser}
|
|
1069
|
+
projectId="proj_123456789"
|
|
1070
|
+
onConfirm={async (comments, projectId) => {
|
|
1071
|
+
// projectId is passed through to your callback
|
|
1072
|
+
await api.createComments(comments, projectId);
|
|
1073
|
+
}}
|
|
1074
|
+
onResolve={resolveComments}
|
|
1075
|
+
onError={handleError}
|
|
1076
|
+
>
|
|
1077
|
+
{children}
|
|
1078
|
+
</CommentContextProvider>
|
|
1079
|
+
```
|
|
1080
|
+
|
|
1081
|
+
Each comment created within this provider will have the `projectId` field set, which you can use for filtering and organizing comments per project.
|
|
1082
|
+
|
|
922
1083
|
### Programmatic Comment Creation
|
|
923
1084
|
|
|
924
1085
|
```tsx
|
|
@@ -948,9 +1109,9 @@ function CustomTool() {
|
|
|
948
1109
|
|
|
949
1110
|
### Error Handling
|
|
950
1111
|
|
|
951
|
-
|
|
1112
|
+
The `onError` callback is required and handles all error types, including overlay interaction errors like attempting to add a comment while editing or dismissing an empty comment.
|
|
952
1113
|
|
|
953
|
-
**1. Using the `onError` callback
|
|
1114
|
+
**1. Using the `onError` callback:**
|
|
954
1115
|
|
|
955
1116
|
```tsx
|
|
956
1117
|
<CommentContextProvider
|
|
@@ -958,9 +1119,8 @@ You can handle errors in two ways:
|
|
|
958
1119
|
onConfirm={saveComments}
|
|
959
1120
|
onResolve={resolveComments}
|
|
960
1121
|
onError={(error) => {
|
|
961
|
-
console.error('Comment operation failed:', error);
|
|
962
|
-
|
|
963
|
-
toast.error('Failed to save comments. Please try again.');
|
|
1122
|
+
console.error('Comment operation failed:', error.code, error.message);
|
|
1123
|
+
toast.error(error.message);
|
|
964
1124
|
}}
|
|
965
1125
|
>
|
|
966
1126
|
{children}
|
|
@@ -1006,7 +1166,10 @@ switch(e.code){
|
|
|
1006
1166
|
break
|
|
1007
1167
|
case "CONFIRM_ERROR":
|
|
1008
1168
|
alert('Failed to submit your comments')
|
|
1009
|
-
break
|
|
1169
|
+
break
|
|
1170
|
+
case "INSERT_ERROR":
|
|
1171
|
+
alert('Failed to insert your comment')
|
|
1172
|
+
break
|
|
1010
1173
|
default:
|
|
1011
1174
|
alert('An unknown error occured')
|
|
1012
1175
|
}
|
|
@@ -1176,6 +1339,17 @@ MIT © [Kendew Agency](https://github.com/Kendew-Agency)
|
|
|
1176
1339
|
|
|
1177
1340
|
A list if breaking changes that could impact the way you configured the package
|
|
1178
1341
|
|
|
1342
|
+
### 0.3.1
|
|
1343
|
+
- Added `events` to the `useComments()` hook. You can now subscribe to comment lifecycle events such as `onFocusChange`, `onCommentsConfirmed`, `onCommentsResolved`, `onCommentDeleted`, and `onCommentRegistered`. Each event method returns an unsubscribe function for easy cleanup.
|
|
1344
|
+
- New types exported: `CommentEvents`, `CommentEventMap`, `Unsubscribe`.
|
|
1345
|
+
|
|
1346
|
+
### 0.3.0
|
|
1347
|
+
- Added `projectId` prop to `CommentContextProvider`. When set, the project ID is attached to every new comment and passed as a second argument to the `onConfirm` callback. Update your `onConfirm` signature from `(comments) => ...` to `(comments, projectId?) => ...`.
|
|
1348
|
+
- `onError` is now a required prop on `CommentContextProvider`. If you previously omitted it, you must provide an error handler.
|
|
1349
|
+
- Added `InsertError` error class and `INSERT_ERROR` error code. The overlay now triggers `onError` instead of using `alert()` when a user tries to add a comment while editing or dismisses an empty comment.
|
|
1350
|
+
- Added `triggerError` method to the comment context (available via `useComments()`), allowing programmatic error triggering through the `onError` callback.
|
|
1351
|
+
- `CommentType` now includes an optional `projectId` field.
|
|
1352
|
+
|
|
1179
1353
|
### 0.2.0
|
|
1180
1354
|
- Reworked the subscription system. The system remains in beta and may change in the future. Configurations made with version 0.1.2 or older will need adjustment after updating.
|
|
1181
1355
|
- `mode` was defined twice in props. It has been removed as a root prop and is now only part of the config.
|
|
@@ -1,5 +1,49 @@
|
|
|
1
1
|
import { ReactNode } from 'react';
|
|
2
2
|
|
|
3
|
+
/**
|
|
4
|
+
* Event map for comment-related events
|
|
5
|
+
* Each key is an event name and the value is the callback signature
|
|
6
|
+
*/
|
|
7
|
+
type CommentEventMap = {
|
|
8
|
+
/**
|
|
9
|
+
* Triggered when focus changes to a different comment
|
|
10
|
+
* @param commentId - The id of the newly focused comment, or null if unfocused
|
|
11
|
+
* @param comment - The focused comment object, or undefined if unfocused
|
|
12
|
+
*/
|
|
13
|
+
onFocusChange: (commentId: string | null, comment: CommentType | undefined) => void;
|
|
14
|
+
/**
|
|
15
|
+
* Triggered when comments are confirmed (saved)
|
|
16
|
+
* @param comments - The comments that were confirmed
|
|
17
|
+
*/
|
|
18
|
+
onCommentsConfirmed: (comments: CommentType[]) => void;
|
|
19
|
+
/**
|
|
20
|
+
* Triggered when comments are resolved
|
|
21
|
+
* @param comments - The comments that were resolved
|
|
22
|
+
*/
|
|
23
|
+
onCommentsResolved: (comments: CommentType[]) => void;
|
|
24
|
+
/**
|
|
25
|
+
* Triggered when a comment is deleted
|
|
26
|
+
* @param commentId - The id of the deleted comment
|
|
27
|
+
*/
|
|
28
|
+
onCommentDeleted: (commentId: string) => void;
|
|
29
|
+
/**
|
|
30
|
+
* Triggered when a new comment is registered (draft created)
|
|
31
|
+
* @param comment - The newly registered comment
|
|
32
|
+
*/
|
|
33
|
+
onCommentRegistered: (comment: CommentType) => void;
|
|
34
|
+
};
|
|
35
|
+
/**
|
|
36
|
+
* A function to unsubscribe from an event
|
|
37
|
+
*/
|
|
38
|
+
type Unsubscribe = () => void;
|
|
39
|
+
/**
|
|
40
|
+
* The events object exposed via useComments()
|
|
41
|
+
* Each method accepts a callback and returns an unsubscribe function
|
|
42
|
+
*/
|
|
43
|
+
type CommentEvents = {
|
|
44
|
+
[K in keyof CommentEventMap]: (callback: CommentEventMap[K]) => Unsubscribe;
|
|
45
|
+
};
|
|
46
|
+
|
|
3
47
|
declare class BaseError extends Error {
|
|
4
48
|
code: OverlayErrorCode;
|
|
5
49
|
status: number;
|
|
@@ -12,7 +56,7 @@ declare class ConfirmError extends BaseError {
|
|
|
12
56
|
constructor(message?: string);
|
|
13
57
|
}
|
|
14
58
|
|
|
15
|
-
type OverlayErrorCode = "RESOLVE_ERROR" | "CONFIRM_ERROR" | "UNKNOWN_ERROR";
|
|
59
|
+
type OverlayErrorCode = "RESOLVE_ERROR" | "CONFIRM_ERROR" | "UNKNOWN_ERROR" | "INSERT_ERROR";
|
|
16
60
|
type KnownError = ConfirmError | ResolveError;
|
|
17
61
|
|
|
18
62
|
type CommentState = {
|
|
@@ -46,6 +90,11 @@ type CommentState = {
|
|
|
46
90
|
* Optional configuration for the comment layer
|
|
47
91
|
*/
|
|
48
92
|
config?: Config | undefined;
|
|
93
|
+
/**
|
|
94
|
+
* Attached a project ID to the current overlay
|
|
95
|
+
* @description This ID will be passed to the comment actions. You can attach comments to a project by storing this ID in your database
|
|
96
|
+
*/
|
|
97
|
+
projectId?: string | undefined;
|
|
49
98
|
};
|
|
50
99
|
/**
|
|
51
100
|
* The state of the comment overlay
|
|
@@ -97,8 +146,9 @@ type CommentOverlayProps = {
|
|
|
97
146
|
* Callback with all newly confirmed comments
|
|
98
147
|
*
|
|
99
148
|
* @param comments as the comments pushed by the user
|
|
149
|
+
* @param projectId as the project id if provided
|
|
100
150
|
*/
|
|
101
|
-
onConfirm: (comments: ConfirmedComment[]) => Promise<void>;
|
|
151
|
+
onConfirm: (comments: ConfirmedComment[], projectId?: string) => Promise<void>;
|
|
102
152
|
/**
|
|
103
153
|
* Callback to handle resolved comments
|
|
104
154
|
*
|
|
@@ -110,11 +160,20 @@ type CommentOverlayProps = {
|
|
|
110
160
|
*
|
|
111
161
|
* @param error as the error that occurred
|
|
112
162
|
*/
|
|
113
|
-
onError
|
|
163
|
+
onError: (error: KnownError) => void;
|
|
114
164
|
/**
|
|
115
165
|
* Optional configuration for the comment layer
|
|
116
166
|
*/
|
|
117
167
|
config?: Config;
|
|
168
|
+
/**
|
|
169
|
+
* Attached a project ID to the current overlay
|
|
170
|
+
* @description This ID will be passed to the comment actions. You can attach comments to a project by storing this ID in your database
|
|
171
|
+
* @example
|
|
172
|
+
* ```ts
|
|
173
|
+
* projectId: "proj_123456789"
|
|
174
|
+
* ```
|
|
175
|
+
*/
|
|
176
|
+
projectId?: string;
|
|
118
177
|
};
|
|
119
178
|
interface RealtimeSubscription<T> {
|
|
120
179
|
subscribe(onUpdate: (data: T) => void): void;
|
|
@@ -183,6 +242,11 @@ type CommentType = {
|
|
|
183
242
|
* @description shows a box around the content a comment is tied to
|
|
184
243
|
*/
|
|
185
244
|
indicator?: Indicator | null;
|
|
245
|
+
/**
|
|
246
|
+
* The id of the project a comment is connected to
|
|
247
|
+
* @description useful for filtering comments by project
|
|
248
|
+
*/
|
|
249
|
+
projectId?: string | undefined;
|
|
186
250
|
};
|
|
187
251
|
type Status = "draft" | "published" | "resolving" | "resolved";
|
|
188
252
|
type ConfirmedStatus = Extract<Status, "published" | "resolved">;
|
|
@@ -248,9 +312,11 @@ type CommentContext = {
|
|
|
248
312
|
error: Error | DOMException | null;
|
|
249
313
|
}>;
|
|
250
314
|
updateCommentVisibility: (visibility: Partial<CommentVisibility>) => void;
|
|
315
|
+
triggerError: (error: KnownError) => void;
|
|
316
|
+
events: CommentEvents;
|
|
251
317
|
};
|
|
252
318
|
type CommentWithStatus<T extends Status> = Omit<CommentType, "status"> & {
|
|
253
319
|
status: T;
|
|
254
320
|
};
|
|
255
321
|
|
|
256
|
-
export type { CommentType as C, Indicator as I, KnownError as K, OverlayErrorCode as O, Position as P, RealtimeSubscription as R, Status as S, User as U, CommentOverlayProps as a, CommentContext as b, CommentOverlayState as c, CommentVisibility as d, ConfirmedStatus as e, ConfirmedComment as f, CommentWithStatus as g, CommentState as h, Config as i };
|
|
322
|
+
export type { CommentType as C, Indicator as I, KnownError as K, OverlayErrorCode as O, Position as P, RealtimeSubscription as R, Status as S, User as U, CommentOverlayProps as a, CommentContext as b, CommentOverlayState as c, CommentVisibility as d, ConfirmedStatus as e, ConfirmedComment as f, CommentWithStatus as g, CommentState as h, Config as i, CommentEventMap as j, Unsubscribe as k, CommentEvents as l };
|
|
@@ -1,5 +1,49 @@
|
|
|
1
1
|
import { ReactNode } from 'react';
|
|
2
2
|
|
|
3
|
+
/**
|
|
4
|
+
* Event map for comment-related events
|
|
5
|
+
* Each key is an event name and the value is the callback signature
|
|
6
|
+
*/
|
|
7
|
+
type CommentEventMap = {
|
|
8
|
+
/**
|
|
9
|
+
* Triggered when focus changes to a different comment
|
|
10
|
+
* @param commentId - The id of the newly focused comment, or null if unfocused
|
|
11
|
+
* @param comment - The focused comment object, or undefined if unfocused
|
|
12
|
+
*/
|
|
13
|
+
onFocusChange: (commentId: string | null, comment: CommentType | undefined) => void;
|
|
14
|
+
/**
|
|
15
|
+
* Triggered when comments are confirmed (saved)
|
|
16
|
+
* @param comments - The comments that were confirmed
|
|
17
|
+
*/
|
|
18
|
+
onCommentsConfirmed: (comments: CommentType[]) => void;
|
|
19
|
+
/**
|
|
20
|
+
* Triggered when comments are resolved
|
|
21
|
+
* @param comments - The comments that were resolved
|
|
22
|
+
*/
|
|
23
|
+
onCommentsResolved: (comments: CommentType[]) => void;
|
|
24
|
+
/**
|
|
25
|
+
* Triggered when a comment is deleted
|
|
26
|
+
* @param commentId - The id of the deleted comment
|
|
27
|
+
*/
|
|
28
|
+
onCommentDeleted: (commentId: string) => void;
|
|
29
|
+
/**
|
|
30
|
+
* Triggered when a new comment is registered (draft created)
|
|
31
|
+
* @param comment - The newly registered comment
|
|
32
|
+
*/
|
|
33
|
+
onCommentRegistered: (comment: CommentType) => void;
|
|
34
|
+
};
|
|
35
|
+
/**
|
|
36
|
+
* A function to unsubscribe from an event
|
|
37
|
+
*/
|
|
38
|
+
type Unsubscribe = () => void;
|
|
39
|
+
/**
|
|
40
|
+
* The events object exposed via useComments()
|
|
41
|
+
* Each method accepts a callback and returns an unsubscribe function
|
|
42
|
+
*/
|
|
43
|
+
type CommentEvents = {
|
|
44
|
+
[K in keyof CommentEventMap]: (callback: CommentEventMap[K]) => Unsubscribe;
|
|
45
|
+
};
|
|
46
|
+
|
|
3
47
|
declare class BaseError extends Error {
|
|
4
48
|
code: OverlayErrorCode;
|
|
5
49
|
status: number;
|
|
@@ -12,7 +56,7 @@ declare class ConfirmError extends BaseError {
|
|
|
12
56
|
constructor(message?: string);
|
|
13
57
|
}
|
|
14
58
|
|
|
15
|
-
type OverlayErrorCode = "RESOLVE_ERROR" | "CONFIRM_ERROR" | "UNKNOWN_ERROR";
|
|
59
|
+
type OverlayErrorCode = "RESOLVE_ERROR" | "CONFIRM_ERROR" | "UNKNOWN_ERROR" | "INSERT_ERROR";
|
|
16
60
|
type KnownError = ConfirmError | ResolveError;
|
|
17
61
|
|
|
18
62
|
type CommentState = {
|
|
@@ -46,6 +90,11 @@ type CommentState = {
|
|
|
46
90
|
* Optional configuration for the comment layer
|
|
47
91
|
*/
|
|
48
92
|
config?: Config | undefined;
|
|
93
|
+
/**
|
|
94
|
+
* Attached a project ID to the current overlay
|
|
95
|
+
* @description This ID will be passed to the comment actions. You can attach comments to a project by storing this ID in your database
|
|
96
|
+
*/
|
|
97
|
+
projectId?: string | undefined;
|
|
49
98
|
};
|
|
50
99
|
/**
|
|
51
100
|
* The state of the comment overlay
|
|
@@ -97,8 +146,9 @@ type CommentOverlayProps = {
|
|
|
97
146
|
* Callback with all newly confirmed comments
|
|
98
147
|
*
|
|
99
148
|
* @param comments as the comments pushed by the user
|
|
149
|
+
* @param projectId as the project id if provided
|
|
100
150
|
*/
|
|
101
|
-
onConfirm: (comments: ConfirmedComment[]) => Promise<void>;
|
|
151
|
+
onConfirm: (comments: ConfirmedComment[], projectId?: string) => Promise<void>;
|
|
102
152
|
/**
|
|
103
153
|
* Callback to handle resolved comments
|
|
104
154
|
*
|
|
@@ -110,11 +160,20 @@ type CommentOverlayProps = {
|
|
|
110
160
|
*
|
|
111
161
|
* @param error as the error that occurred
|
|
112
162
|
*/
|
|
113
|
-
onError
|
|
163
|
+
onError: (error: KnownError) => void;
|
|
114
164
|
/**
|
|
115
165
|
* Optional configuration for the comment layer
|
|
116
166
|
*/
|
|
117
167
|
config?: Config;
|
|
168
|
+
/**
|
|
169
|
+
* Attached a project ID to the current overlay
|
|
170
|
+
* @description This ID will be passed to the comment actions. You can attach comments to a project by storing this ID in your database
|
|
171
|
+
* @example
|
|
172
|
+
* ```ts
|
|
173
|
+
* projectId: "proj_123456789"
|
|
174
|
+
* ```
|
|
175
|
+
*/
|
|
176
|
+
projectId?: string;
|
|
118
177
|
};
|
|
119
178
|
interface RealtimeSubscription<T> {
|
|
120
179
|
subscribe(onUpdate: (data: T) => void): void;
|
|
@@ -183,6 +242,11 @@ type CommentType = {
|
|
|
183
242
|
* @description shows a box around the content a comment is tied to
|
|
184
243
|
*/
|
|
185
244
|
indicator?: Indicator | null;
|
|
245
|
+
/**
|
|
246
|
+
* The id of the project a comment is connected to
|
|
247
|
+
* @description useful for filtering comments by project
|
|
248
|
+
*/
|
|
249
|
+
projectId?: string | undefined;
|
|
186
250
|
};
|
|
187
251
|
type Status = "draft" | "published" | "resolving" | "resolved";
|
|
188
252
|
type ConfirmedStatus = Extract<Status, "published" | "resolved">;
|
|
@@ -248,9 +312,11 @@ type CommentContext = {
|
|
|
248
312
|
error: Error | DOMException | null;
|
|
249
313
|
}>;
|
|
250
314
|
updateCommentVisibility: (visibility: Partial<CommentVisibility>) => void;
|
|
315
|
+
triggerError: (error: KnownError) => void;
|
|
316
|
+
events: CommentEvents;
|
|
251
317
|
};
|
|
252
318
|
type CommentWithStatus<T extends Status> = Omit<CommentType, "status"> & {
|
|
253
319
|
status: T;
|
|
254
320
|
};
|
|
255
321
|
|
|
256
|
-
export type { CommentType as C, Indicator as I, KnownError as K, OverlayErrorCode as O, Position as P, RealtimeSubscription as R, Status as S, User as U, CommentOverlayProps as a, CommentContext as b, CommentOverlayState as c, CommentVisibility as d, ConfirmedStatus as e, ConfirmedComment as f, CommentWithStatus as g, CommentState as h, Config as i };
|
|
322
|
+
export type { CommentType as C, Indicator as I, KnownError as K, OverlayErrorCode as O, Position as P, RealtimeSubscription as R, Status as S, User as U, CommentOverlayProps as a, CommentContext as b, CommentOverlayState as c, CommentVisibility as d, ConfirmedStatus as e, ConfirmedComment as f, CommentWithStatus as g, CommentState as h, Config as i, CommentEventMap as j, Unsubscribe as k, CommentEvents as l };
|
package/dist/index.cjs
CHANGED
|
@@ -44,10 +44,10 @@ __export(src_exports, {
|
|
|
44
44
|
module.exports = __toCommonJS(src_exports);
|
|
45
45
|
|
|
46
46
|
// src/components/comment-overlay.tsx
|
|
47
|
-
var
|
|
47
|
+
var import_react3 = require("react");
|
|
48
48
|
|
|
49
49
|
// src/contexts/comment-context.tsx
|
|
50
|
-
var
|
|
50
|
+
var import_react2 = require("react");
|
|
51
51
|
|
|
52
52
|
// src/lib/tx.ts
|
|
53
53
|
var import_tryx_handler = require("@j31/tryx-handler");
|
|
@@ -70,6 +70,11 @@ var BaseError = class extends Error {
|
|
|
70
70
|
this.status = status;
|
|
71
71
|
}
|
|
72
72
|
};
|
|
73
|
+
var InsertError = class extends BaseError {
|
|
74
|
+
constructor(message = "An error occured while inserting your comments") {
|
|
75
|
+
super(message, "INSERT_ERROR", 500);
|
|
76
|
+
}
|
|
77
|
+
};
|
|
73
78
|
var ResolveError = class extends BaseError {
|
|
74
79
|
constructor(message = "An error occured while resolving your comments") {
|
|
75
80
|
super(message, "RESOLVE_ERROR", 500);
|
|
@@ -81,9 +86,46 @@ var ConfirmError = class extends BaseError {
|
|
|
81
86
|
}
|
|
82
87
|
};
|
|
83
88
|
|
|
89
|
+
// src/lib/use-comment-events.ts
|
|
90
|
+
var import_react = require("react");
|
|
91
|
+
var useCommentEvents = () => {
|
|
92
|
+
const listenersRef = (0, import_react.useRef)({
|
|
93
|
+
onFocusChange: /* @__PURE__ */ new Set(),
|
|
94
|
+
onCommentsConfirmed: /* @__PURE__ */ new Set(),
|
|
95
|
+
onCommentsResolved: /* @__PURE__ */ new Set(),
|
|
96
|
+
onCommentDeleted: /* @__PURE__ */ new Set(),
|
|
97
|
+
onCommentRegistered: /* @__PURE__ */ new Set()
|
|
98
|
+
});
|
|
99
|
+
const subscribe = (0, import_react.useCallback)(
|
|
100
|
+
(event, callback) => {
|
|
101
|
+
listenersRef.current[event].add(callback);
|
|
102
|
+
return () => {
|
|
103
|
+
listenersRef.current[event].delete(callback);
|
|
104
|
+
};
|
|
105
|
+
},
|
|
106
|
+
[]
|
|
107
|
+
);
|
|
108
|
+
const emit = (0, import_react.useCallback)(
|
|
109
|
+
(event, ...args) => {
|
|
110
|
+
for (const listener of listenersRef.current[event]) {
|
|
111
|
+
listener(...args);
|
|
112
|
+
}
|
|
113
|
+
},
|
|
114
|
+
[]
|
|
115
|
+
);
|
|
116
|
+
const events = {
|
|
117
|
+
onFocusChange: (callback) => subscribe("onFocusChange", callback),
|
|
118
|
+
onCommentsConfirmed: (callback) => subscribe("onCommentsConfirmed", callback),
|
|
119
|
+
onCommentsResolved: (callback) => subscribe("onCommentsResolved", callback),
|
|
120
|
+
onCommentDeleted: (callback) => subscribe("onCommentDeleted", callback),
|
|
121
|
+
onCommentRegistered: (callback) => subscribe("onCommentRegistered", callback)
|
|
122
|
+
};
|
|
123
|
+
return { events, emit };
|
|
124
|
+
};
|
|
125
|
+
|
|
84
126
|
// src/contexts/comment-context.tsx
|
|
85
127
|
var import_jsx_runtime = require("react/jsx-runtime");
|
|
86
|
-
var CommentContext = (0,
|
|
128
|
+
var CommentContext = (0, import_react2.createContext)(null);
|
|
87
129
|
var commentReducer = (state, action) => {
|
|
88
130
|
switch (action.type) {
|
|
89
131
|
case "REGISTER": {
|
|
@@ -96,6 +138,7 @@ var commentReducer = (state, action) => {
|
|
|
96
138
|
...state.comments,
|
|
97
139
|
{
|
|
98
140
|
id: newId,
|
|
141
|
+
projectId: state.projectId,
|
|
99
142
|
user: state.currentUser,
|
|
100
143
|
position: {
|
|
101
144
|
x: action.position.x,
|
|
@@ -214,16 +257,29 @@ var CommentContextProvider = ({
|
|
|
214
257
|
hideResolving: false
|
|
215
258
|
}
|
|
216
259
|
},
|
|
260
|
+
projectId,
|
|
217
261
|
onError
|
|
218
262
|
}) => {
|
|
219
|
-
const [state, dispatch] = (0,
|
|
263
|
+
const [state, dispatch] = (0, import_react2.useReducer)(commentReducer, {
|
|
220
264
|
comments: initialComments || [],
|
|
221
265
|
overlayState: initialOverlayState || "inactive",
|
|
222
266
|
currentUser,
|
|
223
267
|
focussedComment: null,
|
|
224
|
-
config
|
|
268
|
+
config,
|
|
269
|
+
projectId
|
|
225
270
|
});
|
|
226
|
-
|
|
271
|
+
const { events, emit } = useCommentEvents();
|
|
272
|
+
const prevFocusRef = (0, import_react2.useRef)(null);
|
|
273
|
+
(0, import_react2.useEffect)(() => {
|
|
274
|
+
if (state.focussedComment !== prevFocusRef.current) {
|
|
275
|
+
const comment = state.comments.find(
|
|
276
|
+
(c) => c.id === state.focussedComment
|
|
277
|
+
);
|
|
278
|
+
emit("onFocusChange", state.focussedComment, comment);
|
|
279
|
+
prevFocusRef.current = state.focussedComment;
|
|
280
|
+
}
|
|
281
|
+
}, [state.focussedComment, state.comments, emit]);
|
|
282
|
+
(0, import_react2.useEffect)(() => {
|
|
227
283
|
if (!subscription) return;
|
|
228
284
|
subscription.subscribe((data) => {
|
|
229
285
|
dispatch({ type: "APPEND_COMMENT", comment: data });
|
|
@@ -239,8 +295,19 @@ var CommentContextProvider = ({
|
|
|
239
295
|
indicatorPosition: indicatorPosition || null
|
|
240
296
|
});
|
|
241
297
|
};
|
|
298
|
+
const prevCommentsLengthRef = (0, import_react2.useRef)(state.comments.length);
|
|
299
|
+
(0, import_react2.useEffect)(() => {
|
|
300
|
+
if (state.comments.length > prevCommentsLengthRef.current) {
|
|
301
|
+
const lastComment = state.comments[state.comments.length - 1];
|
|
302
|
+
if (lastComment && lastComment.status === "draft") {
|
|
303
|
+
emit("onCommentRegistered", lastComment);
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
prevCommentsLengthRef.current = state.comments.length;
|
|
307
|
+
}, [state.comments, emit]);
|
|
242
308
|
const deleteComment = (id) => {
|
|
243
309
|
dispatch({ type: "DELETE", id });
|
|
310
|
+
emit("onCommentDeleted", id);
|
|
244
311
|
};
|
|
245
312
|
const updateComment = (id, newComment) => {
|
|
246
313
|
dispatch({ type: "EDIT", id, newComment });
|
|
@@ -286,17 +353,23 @@ var CommentContextProvider = ({
|
|
|
286
353
|
draftComments.map((c) => ({
|
|
287
354
|
...c,
|
|
288
355
|
status: "published"
|
|
289
|
-
}))
|
|
356
|
+
})),
|
|
357
|
+
projectId
|
|
290
358
|
)
|
|
291
359
|
);
|
|
292
360
|
if (error) {
|
|
293
361
|
if (onError) {
|
|
294
362
|
onError(new ConfirmError(error.message));
|
|
295
363
|
}
|
|
364
|
+
dispatch({ type: "CHANGE_OVERLAYSTATE", to: "idle" });
|
|
296
365
|
return { error: new ConfirmError(error.message) };
|
|
297
366
|
}
|
|
298
367
|
dispatch({ type: "CHANGE_OVERLAYSTATE", to: "idle" });
|
|
299
368
|
dispatch({ type: "RESET_DRAFT_COMMENTS" });
|
|
369
|
+
emit(
|
|
370
|
+
"onCommentsConfirmed",
|
|
371
|
+
draftComments.map((c) => ({ ...c, status: "published" }))
|
|
372
|
+
);
|
|
300
373
|
return { error: null };
|
|
301
374
|
};
|
|
302
375
|
const resolveComments = async () => {
|
|
@@ -314,10 +387,15 @@ var CommentContextProvider = ({
|
|
|
314
387
|
if (onError) {
|
|
315
388
|
onError(new ResolveError(error.message));
|
|
316
389
|
}
|
|
390
|
+
dispatch({ type: "CHANGE_OVERLAYSTATE", to: "idle" });
|
|
317
391
|
return { error: new ResolveError(error.message) };
|
|
318
392
|
}
|
|
319
|
-
dispatch({ type: "RESET_RESOLVING_COMMENTS" });
|
|
320
393
|
dispatch({ type: "CHANGE_OVERLAYSTATE", to: "idle" });
|
|
394
|
+
dispatch({ type: "RESET_RESOLVING_COMMENTS" });
|
|
395
|
+
emit(
|
|
396
|
+
"onCommentsResolved",
|
|
397
|
+
resolvingComments.map((c) => ({ ...c, status: "resolved" }))
|
|
398
|
+
);
|
|
321
399
|
return { error: null };
|
|
322
400
|
};
|
|
323
401
|
const updateCommentVisibility = (visibility) => {
|
|
@@ -326,6 +404,11 @@ var CommentContextProvider = ({
|
|
|
326
404
|
const changeOverlayState = (state2) => {
|
|
327
405
|
dispatch({ type: "CHANGE_OVERLAYSTATE", to: state2 });
|
|
328
406
|
};
|
|
407
|
+
const triggerError = (error) => {
|
|
408
|
+
if (onError) {
|
|
409
|
+
onError(error);
|
|
410
|
+
}
|
|
411
|
+
};
|
|
329
412
|
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
330
413
|
CommentContext.Provider,
|
|
331
414
|
{
|
|
@@ -348,14 +431,16 @@ var CommentContextProvider = ({
|
|
|
348
431
|
confirmComments,
|
|
349
432
|
currentUser,
|
|
350
433
|
resolveComments,
|
|
351
|
-
updateCommentVisibility
|
|
434
|
+
updateCommentVisibility,
|
|
435
|
+
triggerError,
|
|
436
|
+
events
|
|
352
437
|
},
|
|
353
438
|
children
|
|
354
439
|
}
|
|
355
440
|
);
|
|
356
441
|
};
|
|
357
442
|
var useComments = () => {
|
|
358
|
-
const context = (0,
|
|
443
|
+
const context = (0, import_react2.useContext)(CommentContext);
|
|
359
444
|
if (!context) {
|
|
360
445
|
throw new Error("useComments must be used within a CommentContextProvider");
|
|
361
446
|
}
|
|
@@ -384,16 +469,19 @@ var CommentOverlay = ({ children, ...rest }) => {
|
|
|
384
469
|
registerComment,
|
|
385
470
|
focusOnComment,
|
|
386
471
|
getActiveComment,
|
|
387
|
-
changeOverlayState
|
|
472
|
+
changeOverlayState,
|
|
473
|
+
triggerError
|
|
388
474
|
} = useComments();
|
|
389
|
-
const [preview, setPreview] = (0,
|
|
390
|
-
const startRef = (0,
|
|
391
|
-
const draggingRef = (0,
|
|
392
|
-
const overlayRef = (0,
|
|
475
|
+
const [preview, setPreview] = (0, import_react3.useState)(null);
|
|
476
|
+
const startRef = (0, import_react3.useRef)(null);
|
|
477
|
+
const draggingRef = (0, import_react3.useRef)(false);
|
|
478
|
+
const overlayRef = (0, import_react3.useRef)(null);
|
|
393
479
|
const onPointerDown = (e) => {
|
|
394
480
|
if (overlayState === "editing") {
|
|
395
|
-
|
|
396
|
-
|
|
481
|
+
triggerError(
|
|
482
|
+
new InsertError(
|
|
483
|
+
"You are editing a comment, please finish editing the current comment first"
|
|
484
|
+
)
|
|
397
485
|
);
|
|
398
486
|
return;
|
|
399
487
|
}
|
|
@@ -468,6 +556,14 @@ var CommentOverlay = ({ children, ...rest }) => {
|
|
|
468
556
|
{
|
|
469
557
|
"data-comment-guard": true,
|
|
470
558
|
onClick: () => {
|
|
559
|
+
if (getActiveComment()?.content === "") {
|
|
560
|
+
triggerError(
|
|
561
|
+
new InsertError(
|
|
562
|
+
"Comment is empty, either delete the comment or add some content"
|
|
563
|
+
)
|
|
564
|
+
);
|
|
565
|
+
return;
|
|
566
|
+
}
|
|
471
567
|
focusOnComment(null);
|
|
472
568
|
changeOverlayState("idle");
|
|
473
569
|
},
|
|
@@ -595,7 +691,7 @@ var ResolveComments = ({ asChild, ...rest }) => {
|
|
|
595
691
|
};
|
|
596
692
|
|
|
597
693
|
// src/components/comment.tsx
|
|
598
|
-
var
|
|
694
|
+
var import_react4 = require("react");
|
|
599
695
|
var import_react_slot2 = require("@radix-ui/react-slot");
|
|
600
696
|
|
|
601
697
|
// src/contexts/scoped-comment.tsx
|
|
@@ -621,7 +717,7 @@ var CommentRoot = ({
|
|
|
621
717
|
}) => {
|
|
622
718
|
const { getActiveComment } = useComments();
|
|
623
719
|
const isActive = getActiveComment()?.id === comment.id;
|
|
624
|
-
const [draft, setDraft] = (0,
|
|
720
|
+
const [draft, setDraft] = (0, import_react4.useState)(defaultDraft ?? comment.content ?? "");
|
|
625
721
|
return /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(
|
|
626
722
|
CommentScopeContext.Provider,
|
|
627
723
|
{
|
package/dist/index.d.cts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
2
2
|
import * as React from 'react';
|
|
3
3
|
import { HTMLProps, ReactNode, ElementType, ButtonHTMLAttributes } from 'react';
|
|
4
|
-
import { C as CommentType, a as CommentOverlayProps$1, b as CommentContext } from './comment.types-
|
|
4
|
+
import { C as CommentType, a as CommentOverlayProps$1, b as CommentContext } from './comment.types-C0m3sQ_Q.cjs';
|
|
5
5
|
|
|
6
6
|
interface CommentOverlayProps extends HTMLProps<HTMLDivElement> {
|
|
7
7
|
children: ReactNode;
|
|
@@ -73,7 +73,7 @@ declare const Comment: {
|
|
|
73
73
|
*
|
|
74
74
|
* @param initialComments as the initial comments from the document
|
|
75
75
|
*/
|
|
76
|
-
declare const CommentContextProvider: ({ children, initialComments, initialOverlayState, onConfirm, onResolve, currentUser, subscription, config, onError, }: CommentOverlayProps$1) => react_jsx_runtime.JSX.Element;
|
|
76
|
+
declare const CommentContextProvider: ({ children, initialComments, initialOverlayState, onConfirm, onResolve, currentUser, subscription, config, projectId, onError, }: CommentOverlayProps$1) => react_jsx_runtime.JSX.Element;
|
|
77
77
|
declare const useComments: () => CommentContext;
|
|
78
78
|
|
|
79
79
|
type CommentScope = {
|
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
2
2
|
import * as React from 'react';
|
|
3
3
|
import { HTMLProps, ReactNode, ElementType, ButtonHTMLAttributes } from 'react';
|
|
4
|
-
import { C as CommentType, a as CommentOverlayProps$1, b as CommentContext } from './comment.types-
|
|
4
|
+
import { C as CommentType, a as CommentOverlayProps$1, b as CommentContext } from './comment.types-C0m3sQ_Q.js';
|
|
5
5
|
|
|
6
6
|
interface CommentOverlayProps extends HTMLProps<HTMLDivElement> {
|
|
7
7
|
children: ReactNode;
|
|
@@ -73,7 +73,7 @@ declare const Comment: {
|
|
|
73
73
|
*
|
|
74
74
|
* @param initialComments as the initial comments from the document
|
|
75
75
|
*/
|
|
76
|
-
declare const CommentContextProvider: ({ children, initialComments, initialOverlayState, onConfirm, onResolve, currentUser, subscription, config, onError, }: CommentOverlayProps$1) => react_jsx_runtime.JSX.Element;
|
|
76
|
+
declare const CommentContextProvider: ({ children, initialComments, initialOverlayState, onConfirm, onResolve, currentUser, subscription, config, projectId, onError, }: CommentOverlayProps$1) => react_jsx_runtime.JSX.Element;
|
|
77
77
|
declare const useComments: () => CommentContext;
|
|
78
78
|
|
|
79
79
|
type CommentScope = {
|
package/dist/index.js
CHANGED
|
@@ -1,11 +1,17 @@
|
|
|
1
1
|
// src/components/comment-overlay.tsx
|
|
2
2
|
import {
|
|
3
|
-
useRef,
|
|
3
|
+
useRef as useRef3,
|
|
4
4
|
useState
|
|
5
5
|
} from "react";
|
|
6
6
|
|
|
7
7
|
// src/contexts/comment-context.tsx
|
|
8
|
-
import {
|
|
8
|
+
import {
|
|
9
|
+
createContext,
|
|
10
|
+
useContext,
|
|
11
|
+
useEffect,
|
|
12
|
+
useReducer,
|
|
13
|
+
useRef as useRef2
|
|
14
|
+
} from "react";
|
|
9
15
|
|
|
10
16
|
// src/lib/tx.ts
|
|
11
17
|
import { TryX } from "@j31/tryx-handler";
|
|
@@ -28,6 +34,11 @@ var BaseError = class extends Error {
|
|
|
28
34
|
this.status = status;
|
|
29
35
|
}
|
|
30
36
|
};
|
|
37
|
+
var InsertError = class extends BaseError {
|
|
38
|
+
constructor(message = "An error occured while inserting your comments") {
|
|
39
|
+
super(message, "INSERT_ERROR", 500);
|
|
40
|
+
}
|
|
41
|
+
};
|
|
31
42
|
var ResolveError = class extends BaseError {
|
|
32
43
|
constructor(message = "An error occured while resolving your comments") {
|
|
33
44
|
super(message, "RESOLVE_ERROR", 500);
|
|
@@ -39,6 +50,43 @@ var ConfirmError = class extends BaseError {
|
|
|
39
50
|
}
|
|
40
51
|
};
|
|
41
52
|
|
|
53
|
+
// src/lib/use-comment-events.ts
|
|
54
|
+
import { useCallback, useRef } from "react";
|
|
55
|
+
var useCommentEvents = () => {
|
|
56
|
+
const listenersRef = useRef({
|
|
57
|
+
onFocusChange: /* @__PURE__ */ new Set(),
|
|
58
|
+
onCommentsConfirmed: /* @__PURE__ */ new Set(),
|
|
59
|
+
onCommentsResolved: /* @__PURE__ */ new Set(),
|
|
60
|
+
onCommentDeleted: /* @__PURE__ */ new Set(),
|
|
61
|
+
onCommentRegistered: /* @__PURE__ */ new Set()
|
|
62
|
+
});
|
|
63
|
+
const subscribe = useCallback(
|
|
64
|
+
(event, callback) => {
|
|
65
|
+
listenersRef.current[event].add(callback);
|
|
66
|
+
return () => {
|
|
67
|
+
listenersRef.current[event].delete(callback);
|
|
68
|
+
};
|
|
69
|
+
},
|
|
70
|
+
[]
|
|
71
|
+
);
|
|
72
|
+
const emit = useCallback(
|
|
73
|
+
(event, ...args) => {
|
|
74
|
+
for (const listener of listenersRef.current[event]) {
|
|
75
|
+
listener(...args);
|
|
76
|
+
}
|
|
77
|
+
},
|
|
78
|
+
[]
|
|
79
|
+
);
|
|
80
|
+
const events = {
|
|
81
|
+
onFocusChange: (callback) => subscribe("onFocusChange", callback),
|
|
82
|
+
onCommentsConfirmed: (callback) => subscribe("onCommentsConfirmed", callback),
|
|
83
|
+
onCommentsResolved: (callback) => subscribe("onCommentsResolved", callback),
|
|
84
|
+
onCommentDeleted: (callback) => subscribe("onCommentDeleted", callback),
|
|
85
|
+
onCommentRegistered: (callback) => subscribe("onCommentRegistered", callback)
|
|
86
|
+
};
|
|
87
|
+
return { events, emit };
|
|
88
|
+
};
|
|
89
|
+
|
|
42
90
|
// src/contexts/comment-context.tsx
|
|
43
91
|
import { jsx } from "react/jsx-runtime";
|
|
44
92
|
var CommentContext = createContext(null);
|
|
@@ -54,6 +102,7 @@ var commentReducer = (state, action) => {
|
|
|
54
102
|
...state.comments,
|
|
55
103
|
{
|
|
56
104
|
id: newId,
|
|
105
|
+
projectId: state.projectId,
|
|
57
106
|
user: state.currentUser,
|
|
58
107
|
position: {
|
|
59
108
|
x: action.position.x,
|
|
@@ -172,6 +221,7 @@ var CommentContextProvider = ({
|
|
|
172
221
|
hideResolving: false
|
|
173
222
|
}
|
|
174
223
|
},
|
|
224
|
+
projectId,
|
|
175
225
|
onError
|
|
176
226
|
}) => {
|
|
177
227
|
const [state, dispatch] = useReducer(commentReducer, {
|
|
@@ -179,8 +229,20 @@ var CommentContextProvider = ({
|
|
|
179
229
|
overlayState: initialOverlayState || "inactive",
|
|
180
230
|
currentUser,
|
|
181
231
|
focussedComment: null,
|
|
182
|
-
config
|
|
232
|
+
config,
|
|
233
|
+
projectId
|
|
183
234
|
});
|
|
235
|
+
const { events, emit } = useCommentEvents();
|
|
236
|
+
const prevFocusRef = useRef2(null);
|
|
237
|
+
useEffect(() => {
|
|
238
|
+
if (state.focussedComment !== prevFocusRef.current) {
|
|
239
|
+
const comment = state.comments.find(
|
|
240
|
+
(c) => c.id === state.focussedComment
|
|
241
|
+
);
|
|
242
|
+
emit("onFocusChange", state.focussedComment, comment);
|
|
243
|
+
prevFocusRef.current = state.focussedComment;
|
|
244
|
+
}
|
|
245
|
+
}, [state.focussedComment, state.comments, emit]);
|
|
184
246
|
useEffect(() => {
|
|
185
247
|
if (!subscription) return;
|
|
186
248
|
subscription.subscribe((data) => {
|
|
@@ -197,8 +259,19 @@ var CommentContextProvider = ({
|
|
|
197
259
|
indicatorPosition: indicatorPosition || null
|
|
198
260
|
});
|
|
199
261
|
};
|
|
262
|
+
const prevCommentsLengthRef = useRef2(state.comments.length);
|
|
263
|
+
useEffect(() => {
|
|
264
|
+
if (state.comments.length > prevCommentsLengthRef.current) {
|
|
265
|
+
const lastComment = state.comments[state.comments.length - 1];
|
|
266
|
+
if (lastComment && lastComment.status === "draft") {
|
|
267
|
+
emit("onCommentRegistered", lastComment);
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
prevCommentsLengthRef.current = state.comments.length;
|
|
271
|
+
}, [state.comments, emit]);
|
|
200
272
|
const deleteComment = (id) => {
|
|
201
273
|
dispatch({ type: "DELETE", id });
|
|
274
|
+
emit("onCommentDeleted", id);
|
|
202
275
|
};
|
|
203
276
|
const updateComment = (id, newComment) => {
|
|
204
277
|
dispatch({ type: "EDIT", id, newComment });
|
|
@@ -244,17 +317,23 @@ var CommentContextProvider = ({
|
|
|
244
317
|
draftComments.map((c) => ({
|
|
245
318
|
...c,
|
|
246
319
|
status: "published"
|
|
247
|
-
}))
|
|
320
|
+
})),
|
|
321
|
+
projectId
|
|
248
322
|
)
|
|
249
323
|
);
|
|
250
324
|
if (error) {
|
|
251
325
|
if (onError) {
|
|
252
326
|
onError(new ConfirmError(error.message));
|
|
253
327
|
}
|
|
328
|
+
dispatch({ type: "CHANGE_OVERLAYSTATE", to: "idle" });
|
|
254
329
|
return { error: new ConfirmError(error.message) };
|
|
255
330
|
}
|
|
256
331
|
dispatch({ type: "CHANGE_OVERLAYSTATE", to: "idle" });
|
|
257
332
|
dispatch({ type: "RESET_DRAFT_COMMENTS" });
|
|
333
|
+
emit(
|
|
334
|
+
"onCommentsConfirmed",
|
|
335
|
+
draftComments.map((c) => ({ ...c, status: "published" }))
|
|
336
|
+
);
|
|
258
337
|
return { error: null };
|
|
259
338
|
};
|
|
260
339
|
const resolveComments = async () => {
|
|
@@ -272,10 +351,15 @@ var CommentContextProvider = ({
|
|
|
272
351
|
if (onError) {
|
|
273
352
|
onError(new ResolveError(error.message));
|
|
274
353
|
}
|
|
354
|
+
dispatch({ type: "CHANGE_OVERLAYSTATE", to: "idle" });
|
|
275
355
|
return { error: new ResolveError(error.message) };
|
|
276
356
|
}
|
|
277
|
-
dispatch({ type: "RESET_RESOLVING_COMMENTS" });
|
|
278
357
|
dispatch({ type: "CHANGE_OVERLAYSTATE", to: "idle" });
|
|
358
|
+
dispatch({ type: "RESET_RESOLVING_COMMENTS" });
|
|
359
|
+
emit(
|
|
360
|
+
"onCommentsResolved",
|
|
361
|
+
resolvingComments.map((c) => ({ ...c, status: "resolved" }))
|
|
362
|
+
);
|
|
279
363
|
return { error: null };
|
|
280
364
|
};
|
|
281
365
|
const updateCommentVisibility = (visibility) => {
|
|
@@ -284,6 +368,11 @@ var CommentContextProvider = ({
|
|
|
284
368
|
const changeOverlayState = (state2) => {
|
|
285
369
|
dispatch({ type: "CHANGE_OVERLAYSTATE", to: state2 });
|
|
286
370
|
};
|
|
371
|
+
const triggerError = (error) => {
|
|
372
|
+
if (onError) {
|
|
373
|
+
onError(error);
|
|
374
|
+
}
|
|
375
|
+
};
|
|
287
376
|
return /* @__PURE__ */ jsx(
|
|
288
377
|
CommentContext.Provider,
|
|
289
378
|
{
|
|
@@ -306,7 +395,9 @@ var CommentContextProvider = ({
|
|
|
306
395
|
confirmComments,
|
|
307
396
|
currentUser,
|
|
308
397
|
resolveComments,
|
|
309
|
-
updateCommentVisibility
|
|
398
|
+
updateCommentVisibility,
|
|
399
|
+
triggerError,
|
|
400
|
+
events
|
|
310
401
|
},
|
|
311
402
|
children
|
|
312
403
|
}
|
|
@@ -342,16 +433,19 @@ var CommentOverlay = ({ children, ...rest }) => {
|
|
|
342
433
|
registerComment,
|
|
343
434
|
focusOnComment,
|
|
344
435
|
getActiveComment,
|
|
345
|
-
changeOverlayState
|
|
436
|
+
changeOverlayState,
|
|
437
|
+
triggerError
|
|
346
438
|
} = useComments();
|
|
347
439
|
const [preview, setPreview] = useState(null);
|
|
348
|
-
const startRef =
|
|
349
|
-
const draggingRef =
|
|
350
|
-
const overlayRef =
|
|
440
|
+
const startRef = useRef3(null);
|
|
441
|
+
const draggingRef = useRef3(false);
|
|
442
|
+
const overlayRef = useRef3(null);
|
|
351
443
|
const onPointerDown = (e) => {
|
|
352
444
|
if (overlayState === "editing") {
|
|
353
|
-
|
|
354
|
-
|
|
445
|
+
triggerError(
|
|
446
|
+
new InsertError(
|
|
447
|
+
"You are editing a comment, please finish editing the current comment first"
|
|
448
|
+
)
|
|
355
449
|
);
|
|
356
450
|
return;
|
|
357
451
|
}
|
|
@@ -426,6 +520,14 @@ var CommentOverlay = ({ children, ...rest }) => {
|
|
|
426
520
|
{
|
|
427
521
|
"data-comment-guard": true,
|
|
428
522
|
onClick: () => {
|
|
523
|
+
if (getActiveComment()?.content === "") {
|
|
524
|
+
triggerError(
|
|
525
|
+
new InsertError(
|
|
526
|
+
"Comment is empty, either delete the comment or add some content"
|
|
527
|
+
)
|
|
528
|
+
);
|
|
529
|
+
return;
|
|
530
|
+
}
|
|
429
531
|
focusOnComment(null);
|
|
430
532
|
changeOverlayState("idle");
|
|
431
533
|
},
|
package/dist/types.d.cts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { C as CommentType, P as Position, I as Indicator, c as CommentOverlayState, d as CommentVisibility } from './comment.types-
|
|
2
|
-
export { b as CommentContext, a as CommentOverlayProps, h as CommentState, g as CommentWithStatus, i as Config, f as ConfirmedComment, e as ConfirmedStatus, K as KnownError, O as OverlayErrorCode, R as RealtimeSubscription, S as Status, U as User } from './comment.types-
|
|
1
|
+
import { C as CommentType, P as Position, I as Indicator, c as CommentOverlayState, d as CommentVisibility } from './comment.types-C0m3sQ_Q.cjs';
|
|
2
|
+
export { b as CommentContext, j as CommentEventMap, l as CommentEvents, a as CommentOverlayProps, h as CommentState, g as CommentWithStatus, i as Config, f as ConfirmedComment, e as ConfirmedStatus, K as KnownError, O as OverlayErrorCode, R as RealtimeSubscription, S as Status, k as Unsubscribe, U as User } from './comment.types-C0m3sQ_Q.cjs';
|
|
3
3
|
import 'react';
|
|
4
4
|
|
|
5
5
|
/**
|
package/dist/types.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { C as CommentType, P as Position, I as Indicator, c as CommentOverlayState, d as CommentVisibility } from './comment.types-
|
|
2
|
-
export { b as CommentContext, a as CommentOverlayProps, h as CommentState, g as CommentWithStatus, i as Config, f as ConfirmedComment, e as ConfirmedStatus, K as KnownError, O as OverlayErrorCode, R as RealtimeSubscription, S as Status, U as User } from './comment.types-
|
|
1
|
+
import { C as CommentType, P as Position, I as Indicator, c as CommentOverlayState, d as CommentVisibility } from './comment.types-C0m3sQ_Q.js';
|
|
2
|
+
export { b as CommentContext, j as CommentEventMap, l as CommentEvents, a as CommentOverlayProps, h as CommentState, g as CommentWithStatus, i as Config, f as ConfirmedComment, e as ConfirmedStatus, K as KnownError, O as OverlayErrorCode, R as RealtimeSubscription, S as Status, k as Unsubscribe, U as User } from './comment.types-C0m3sQ_Q.js';
|
|
3
3
|
import 'react';
|
|
4
4
|
|
|
5
5
|
/**
|