@kendew-agency/react-feedback-layer 0.3.0 → 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 +132 -0
- package/dist/{comment.types-D2tC4rfE.d.cts → comment.types-C0m3sQ_Q.d.cts} +46 -1
- package/dist/{comment.types-D2tC4rfE.d.ts → comment.types-C0m3sQ_Q.d.ts} +46 -1
- package/dist/index.cjs +84 -14
- package/dist/index.d.cts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +83 -7
- package/dist/types.d.cts +2 -2
- package/dist/types.d.ts +2 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -482,6 +482,9 @@ interface CommentContext {
|
|
|
482
482
|
resolveComments: () => Promise<{ error: Error | DOMException | null }>;
|
|
483
483
|
updateCommentVisibility: (visibility: Partial<CommentVisibility>) => void;
|
|
484
484
|
triggerError: (error: KnownError) => void;
|
|
485
|
+
|
|
486
|
+
// Events - subscribe to comment lifecycle changes
|
|
487
|
+
events: CommentEvents;
|
|
485
488
|
}
|
|
486
489
|
```
|
|
487
490
|
|
|
@@ -550,6 +553,131 @@ function CustomCommentContent() {
|
|
|
550
553
|
}
|
|
551
554
|
```
|
|
552
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
|
+
|
|
553
681
|
### Types
|
|
554
682
|
|
|
555
683
|
Import types from the `/types` subpath:
|
|
@@ -1211,6 +1339,10 @@ MIT © [Kendew Agency](https://github.com/Kendew-Agency)
|
|
|
1211
1339
|
|
|
1212
1340
|
A list if breaking changes that could impact the way you configured the package
|
|
1213
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
|
+
|
|
1214
1346
|
### 0.3.0
|
|
1215
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?) => ...`.
|
|
1216
1348
|
- `onError` is now a required prop on `CommentContextProvider`. If you previously omitted it, you must provide an error handler.
|
|
@@ -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;
|
|
@@ -269,9 +313,10 @@ type CommentContext = {
|
|
|
269
313
|
}>;
|
|
270
314
|
updateCommentVisibility: (visibility: Partial<CommentVisibility>) => void;
|
|
271
315
|
triggerError: (error: KnownError) => void;
|
|
316
|
+
events: CommentEvents;
|
|
272
317
|
};
|
|
273
318
|
type CommentWithStatus<T extends Status> = Omit<CommentType, "status"> & {
|
|
274
319
|
status: T;
|
|
275
320
|
};
|
|
276
321
|
|
|
277
|
-
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;
|
|
@@ -269,9 +313,10 @@ type CommentContext = {
|
|
|
269
313
|
}>;
|
|
270
314
|
updateCommentVisibility: (visibility: Partial<CommentVisibility>) => void;
|
|
271
315
|
triggerError: (error: KnownError) => void;
|
|
316
|
+
events: CommentEvents;
|
|
272
317
|
};
|
|
273
318
|
type CommentWithStatus<T extends Status> = Omit<CommentType, "status"> & {
|
|
274
319
|
status: T;
|
|
275
320
|
};
|
|
276
321
|
|
|
277
|
-
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");
|
|
@@ -86,9 +86,46 @@ var ConfirmError = class extends BaseError {
|
|
|
86
86
|
}
|
|
87
87
|
};
|
|
88
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
|
+
|
|
89
126
|
// src/contexts/comment-context.tsx
|
|
90
127
|
var import_jsx_runtime = require("react/jsx-runtime");
|
|
91
|
-
var CommentContext = (0,
|
|
128
|
+
var CommentContext = (0, import_react2.createContext)(null);
|
|
92
129
|
var commentReducer = (state, action) => {
|
|
93
130
|
switch (action.type) {
|
|
94
131
|
case "REGISTER": {
|
|
@@ -223,7 +260,7 @@ var CommentContextProvider = ({
|
|
|
223
260
|
projectId,
|
|
224
261
|
onError
|
|
225
262
|
}) => {
|
|
226
|
-
const [state, dispatch] = (0,
|
|
263
|
+
const [state, dispatch] = (0, import_react2.useReducer)(commentReducer, {
|
|
227
264
|
comments: initialComments || [],
|
|
228
265
|
overlayState: initialOverlayState || "inactive",
|
|
229
266
|
currentUser,
|
|
@@ -231,7 +268,18 @@ var CommentContextProvider = ({
|
|
|
231
268
|
config,
|
|
232
269
|
projectId
|
|
233
270
|
});
|
|
234
|
-
|
|
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)(() => {
|
|
235
283
|
if (!subscription) return;
|
|
236
284
|
subscription.subscribe((data) => {
|
|
237
285
|
dispatch({ type: "APPEND_COMMENT", comment: data });
|
|
@@ -247,8 +295,19 @@ var CommentContextProvider = ({
|
|
|
247
295
|
indicatorPosition: indicatorPosition || null
|
|
248
296
|
});
|
|
249
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]);
|
|
250
308
|
const deleteComment = (id) => {
|
|
251
309
|
dispatch({ type: "DELETE", id });
|
|
310
|
+
emit("onCommentDeleted", id);
|
|
252
311
|
};
|
|
253
312
|
const updateComment = (id, newComment) => {
|
|
254
313
|
dispatch({ type: "EDIT", id, newComment });
|
|
@@ -302,10 +361,15 @@ var CommentContextProvider = ({
|
|
|
302
361
|
if (onError) {
|
|
303
362
|
onError(new ConfirmError(error.message));
|
|
304
363
|
}
|
|
364
|
+
dispatch({ type: "CHANGE_OVERLAYSTATE", to: "idle" });
|
|
305
365
|
return { error: new ConfirmError(error.message) };
|
|
306
366
|
}
|
|
307
367
|
dispatch({ type: "CHANGE_OVERLAYSTATE", to: "idle" });
|
|
308
368
|
dispatch({ type: "RESET_DRAFT_COMMENTS" });
|
|
369
|
+
emit(
|
|
370
|
+
"onCommentsConfirmed",
|
|
371
|
+
draftComments.map((c) => ({ ...c, status: "published" }))
|
|
372
|
+
);
|
|
309
373
|
return { error: null };
|
|
310
374
|
};
|
|
311
375
|
const resolveComments = async () => {
|
|
@@ -323,10 +387,15 @@ var CommentContextProvider = ({
|
|
|
323
387
|
if (onError) {
|
|
324
388
|
onError(new ResolveError(error.message));
|
|
325
389
|
}
|
|
390
|
+
dispatch({ type: "CHANGE_OVERLAYSTATE", to: "idle" });
|
|
326
391
|
return { error: new ResolveError(error.message) };
|
|
327
392
|
}
|
|
328
|
-
dispatch({ type: "RESET_RESOLVING_COMMENTS" });
|
|
329
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
|
+
);
|
|
330
399
|
return { error: null };
|
|
331
400
|
};
|
|
332
401
|
const updateCommentVisibility = (visibility) => {
|
|
@@ -363,14 +432,15 @@ var CommentContextProvider = ({
|
|
|
363
432
|
currentUser,
|
|
364
433
|
resolveComments,
|
|
365
434
|
updateCommentVisibility,
|
|
366
|
-
triggerError
|
|
435
|
+
triggerError,
|
|
436
|
+
events
|
|
367
437
|
},
|
|
368
438
|
children
|
|
369
439
|
}
|
|
370
440
|
);
|
|
371
441
|
};
|
|
372
442
|
var useComments = () => {
|
|
373
|
-
const context = (0,
|
|
443
|
+
const context = (0, import_react2.useContext)(CommentContext);
|
|
374
444
|
if (!context) {
|
|
375
445
|
throw new Error("useComments must be used within a CommentContextProvider");
|
|
376
446
|
}
|
|
@@ -402,10 +472,10 @@ var CommentOverlay = ({ children, ...rest }) => {
|
|
|
402
472
|
changeOverlayState,
|
|
403
473
|
triggerError
|
|
404
474
|
} = useComments();
|
|
405
|
-
const [preview, setPreview] = (0,
|
|
406
|
-
const startRef = (0,
|
|
407
|
-
const draggingRef = (0,
|
|
408
|
-
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);
|
|
409
479
|
const onPointerDown = (e) => {
|
|
410
480
|
if (overlayState === "editing") {
|
|
411
481
|
triggerError(
|
|
@@ -621,7 +691,7 @@ var ResolveComments = ({ asChild, ...rest }) => {
|
|
|
621
691
|
};
|
|
622
692
|
|
|
623
693
|
// src/components/comment.tsx
|
|
624
|
-
var
|
|
694
|
+
var import_react4 = require("react");
|
|
625
695
|
var import_react_slot2 = require("@radix-ui/react-slot");
|
|
626
696
|
|
|
627
697
|
// src/contexts/scoped-comment.tsx
|
|
@@ -647,7 +717,7 @@ var CommentRoot = ({
|
|
|
647
717
|
}) => {
|
|
648
718
|
const { getActiveComment } = useComments();
|
|
649
719
|
const isActive = getActiveComment()?.id === comment.id;
|
|
650
|
-
const [draft, setDraft] = (0,
|
|
720
|
+
const [draft, setDraft] = (0, import_react4.useState)(defaultDraft ?? comment.content ?? "");
|
|
651
721
|
return /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(
|
|
652
722
|
CommentScopeContext.Provider,
|
|
653
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;
|
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;
|
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";
|
|
@@ -44,6 +50,43 @@ var ConfirmError = class extends BaseError {
|
|
|
44
50
|
}
|
|
45
51
|
};
|
|
46
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
|
+
|
|
47
90
|
// src/contexts/comment-context.tsx
|
|
48
91
|
import { jsx } from "react/jsx-runtime";
|
|
49
92
|
var CommentContext = createContext(null);
|
|
@@ -189,6 +232,17 @@ var CommentContextProvider = ({
|
|
|
189
232
|
config,
|
|
190
233
|
projectId
|
|
191
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]);
|
|
192
246
|
useEffect(() => {
|
|
193
247
|
if (!subscription) return;
|
|
194
248
|
subscription.subscribe((data) => {
|
|
@@ -205,8 +259,19 @@ var CommentContextProvider = ({
|
|
|
205
259
|
indicatorPosition: indicatorPosition || null
|
|
206
260
|
});
|
|
207
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]);
|
|
208
272
|
const deleteComment = (id) => {
|
|
209
273
|
dispatch({ type: "DELETE", id });
|
|
274
|
+
emit("onCommentDeleted", id);
|
|
210
275
|
};
|
|
211
276
|
const updateComment = (id, newComment) => {
|
|
212
277
|
dispatch({ type: "EDIT", id, newComment });
|
|
@@ -260,10 +325,15 @@ var CommentContextProvider = ({
|
|
|
260
325
|
if (onError) {
|
|
261
326
|
onError(new ConfirmError(error.message));
|
|
262
327
|
}
|
|
328
|
+
dispatch({ type: "CHANGE_OVERLAYSTATE", to: "idle" });
|
|
263
329
|
return { error: new ConfirmError(error.message) };
|
|
264
330
|
}
|
|
265
331
|
dispatch({ type: "CHANGE_OVERLAYSTATE", to: "idle" });
|
|
266
332
|
dispatch({ type: "RESET_DRAFT_COMMENTS" });
|
|
333
|
+
emit(
|
|
334
|
+
"onCommentsConfirmed",
|
|
335
|
+
draftComments.map((c) => ({ ...c, status: "published" }))
|
|
336
|
+
);
|
|
267
337
|
return { error: null };
|
|
268
338
|
};
|
|
269
339
|
const resolveComments = async () => {
|
|
@@ -281,10 +351,15 @@ var CommentContextProvider = ({
|
|
|
281
351
|
if (onError) {
|
|
282
352
|
onError(new ResolveError(error.message));
|
|
283
353
|
}
|
|
354
|
+
dispatch({ type: "CHANGE_OVERLAYSTATE", to: "idle" });
|
|
284
355
|
return { error: new ResolveError(error.message) };
|
|
285
356
|
}
|
|
286
|
-
dispatch({ type: "RESET_RESOLVING_COMMENTS" });
|
|
287
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
|
+
);
|
|
288
363
|
return { error: null };
|
|
289
364
|
};
|
|
290
365
|
const updateCommentVisibility = (visibility) => {
|
|
@@ -321,7 +396,8 @@ var CommentContextProvider = ({
|
|
|
321
396
|
currentUser,
|
|
322
397
|
resolveComments,
|
|
323
398
|
updateCommentVisibility,
|
|
324
|
-
triggerError
|
|
399
|
+
triggerError,
|
|
400
|
+
events
|
|
325
401
|
},
|
|
326
402
|
children
|
|
327
403
|
}
|
|
@@ -361,9 +437,9 @@ var CommentOverlay = ({ children, ...rest }) => {
|
|
|
361
437
|
triggerError
|
|
362
438
|
} = useComments();
|
|
363
439
|
const [preview, setPreview] = useState(null);
|
|
364
|
-
const startRef =
|
|
365
|
-
const draggingRef =
|
|
366
|
-
const overlayRef =
|
|
440
|
+
const startRef = useRef3(null);
|
|
441
|
+
const draggingRef = useRef3(false);
|
|
442
|
+
const overlayRef = useRef3(null);
|
|
367
443
|
const onPointerDown = (e) => {
|
|
368
444
|
if (overlayState === "editing") {
|
|
369
445
|
triggerError(
|
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
|
/**
|