@kendew-agency/react-feedback-layer 0.3.0 → 0.3.2
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 +147 -1
- 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 +134 -18
- package/dist/index.d.cts +65 -5
- package/dist/index.d.ts +65 -5
- package/dist/index.js +133 -11
- package/dist/types.d.cts +2 -2
- package/dist/types.d.ts +2 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -267,8 +267,9 @@ Container for a single comment with positioning and active state management.
|
|
|
267
267
|
```tsx
|
|
268
268
|
interface CommentRootProps extends HTMLProps<HTMLDivElement> {
|
|
269
269
|
comment: CommentType;
|
|
270
|
-
children?: ReactNode;
|
|
271
270
|
defaultDraft?: string; // Initial draft content
|
|
271
|
+
withPosition?: boolean; // Apply absolute positioning from comment.position (default: true)
|
|
272
|
+
children?: ReactNode;
|
|
272
273
|
}
|
|
273
274
|
```
|
|
274
275
|
|
|
@@ -280,6 +281,16 @@ interface CommentRootProps extends HTMLProps<HTMLDivElement> {
|
|
|
280
281
|
</Comment.Root>
|
|
281
282
|
```
|
|
282
283
|
|
|
284
|
+
**Rendering without positioning (e.g. in a list):**
|
|
285
|
+
|
|
286
|
+
Set `withPosition={false}` to skip the `position`, `top` and `left` styles derived from `comment.position`. Styles passed via `style` are still applied.
|
|
287
|
+
|
|
288
|
+
```tsx
|
|
289
|
+
<Comment.Root comment={comment} withPosition={false}>
|
|
290
|
+
{/* Sub-components */}
|
|
291
|
+
</Comment.Root>
|
|
292
|
+
```
|
|
293
|
+
|
|
283
294
|
##### `Comment.Indicator`
|
|
284
295
|
|
|
285
296
|
Visual indicator showing the area a comment refers to (when created via drag).
|
|
@@ -482,6 +493,9 @@ interface CommentContext {
|
|
|
482
493
|
resolveComments: () => Promise<{ error: Error | DOMException | null }>;
|
|
483
494
|
updateCommentVisibility: (visibility: Partial<CommentVisibility>) => void;
|
|
484
495
|
triggerError: (error: KnownError) => void;
|
|
496
|
+
|
|
497
|
+
// Events - subscribe to comment lifecycle changes
|
|
498
|
+
events: CommentEvents;
|
|
485
499
|
}
|
|
486
500
|
```
|
|
487
501
|
|
|
@@ -550,6 +564,131 @@ function CustomCommentContent() {
|
|
|
550
564
|
}
|
|
551
565
|
```
|
|
552
566
|
|
|
567
|
+
#### `events` (via `useComments()`)
|
|
568
|
+
|
|
569
|
+
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.
|
|
570
|
+
|
|
571
|
+
**Available Events:**
|
|
572
|
+
|
|
573
|
+
| Event | Trigger | Callback Signature |
|
|
574
|
+
|-------|---------|-------------------|
|
|
575
|
+
| `onFocusChange` | Focus moves to a different comment | `(commentId: string \| null, comment: CommentType \| undefined) => void` |
|
|
576
|
+
| `onCommentsConfirmed` | Draft comments are successfully saved | `(comments: CommentType[]) => void` |
|
|
577
|
+
| `onCommentsResolved` | Comments are successfully resolved | `(comments: CommentType[]) => void` |
|
|
578
|
+
| `onCommentDeleted` | A comment is deleted | `(commentId: string) => void` |
|
|
579
|
+
| `onCommentRegistered` | A new draft comment is created | `(comment: CommentType) => void` |
|
|
580
|
+
|
|
581
|
+
**Basic Usage:**
|
|
582
|
+
|
|
583
|
+
```tsx
|
|
584
|
+
import { useComments } from '@kendew-agency/react-feedback-layer';
|
|
585
|
+
import { useEffect } from 'react';
|
|
586
|
+
|
|
587
|
+
function CommentTracker() {
|
|
588
|
+
const { events } = useComments();
|
|
589
|
+
|
|
590
|
+
useEffect(() => {
|
|
591
|
+
const unsubscribe = events.onFocusChange((commentId, comment) => {
|
|
592
|
+
console.log('Focus changed to:', commentId);
|
|
593
|
+
if (comment) {
|
|
594
|
+
highlightElement(comment.position);
|
|
595
|
+
}
|
|
596
|
+
});
|
|
597
|
+
|
|
598
|
+
return unsubscribe;
|
|
599
|
+
}, [events]);
|
|
600
|
+
|
|
601
|
+
useEffect(() => {
|
|
602
|
+
const unsubscribe = events.onCommentsConfirmed((comments) => {
|
|
603
|
+
console.log(`${comments.length} comments saved`);
|
|
604
|
+
toast.success('Comments saved successfully!');
|
|
605
|
+
});
|
|
606
|
+
|
|
607
|
+
return unsubscribe;
|
|
608
|
+
}, [events]);
|
|
609
|
+
|
|
610
|
+
return null;
|
|
611
|
+
}
|
|
612
|
+
```
|
|
613
|
+
|
|
614
|
+
**Multiple Subscriptions:**
|
|
615
|
+
|
|
616
|
+
```tsx
|
|
617
|
+
function AnalyticsTracker() {
|
|
618
|
+
const { events } = useComments();
|
|
619
|
+
|
|
620
|
+
useEffect(() => {
|
|
621
|
+
const unsubs = [
|
|
622
|
+
events.onCommentRegistered((comment) => {
|
|
623
|
+
analytics.track('comment_created', {
|
|
624
|
+
position: comment.position,
|
|
625
|
+
user: comment.user?.name,
|
|
626
|
+
});
|
|
627
|
+
}),
|
|
628
|
+
events.onCommentsConfirmed((comments) => {
|
|
629
|
+
analytics.track('comments_confirmed', { count: comments.length });
|
|
630
|
+
}),
|
|
631
|
+
events.onCommentsResolved((comments) => {
|
|
632
|
+
analytics.track('comments_resolved', { count: comments.length });
|
|
633
|
+
}),
|
|
634
|
+
events.onCommentDeleted((id) => {
|
|
635
|
+
analytics.track('comment_deleted', { commentId: id });
|
|
636
|
+
}),
|
|
637
|
+
];
|
|
638
|
+
|
|
639
|
+
return () => unsubs.forEach((unsub) => unsub());
|
|
640
|
+
}, [events]);
|
|
641
|
+
|
|
642
|
+
return null;
|
|
643
|
+
}
|
|
644
|
+
```
|
|
645
|
+
|
|
646
|
+
**Syncing with External State:**
|
|
647
|
+
|
|
648
|
+
```tsx
|
|
649
|
+
function CommentSidebar() {
|
|
650
|
+
const { events, allComments } = useComments();
|
|
651
|
+
const [activeId, setActiveId] = useState<string | null>(null);
|
|
652
|
+
|
|
653
|
+
useEffect(() => {
|
|
654
|
+
return events.onFocusChange((commentId) => {
|
|
655
|
+
setActiveId(commentId);
|
|
656
|
+
// Scroll sidebar to the active comment
|
|
657
|
+
if (commentId) {
|
|
658
|
+
document.getElementById(`sidebar-${commentId}`)?.scrollIntoView({
|
|
659
|
+
behavior: 'smooth',
|
|
660
|
+
});
|
|
661
|
+
}
|
|
662
|
+
});
|
|
663
|
+
}, [events]);
|
|
664
|
+
|
|
665
|
+
return (
|
|
666
|
+
<aside className="comment-sidebar">
|
|
667
|
+
{allComments.map((comment) => (
|
|
668
|
+
<div
|
|
669
|
+
key={comment.id}
|
|
670
|
+
id={`sidebar-${comment.id}`}
|
|
671
|
+
className={comment.id === activeId ? 'active' : ''}
|
|
672
|
+
>
|
|
673
|
+
<p>{comment.content}</p>
|
|
674
|
+
<small>{comment.user?.name}</small>
|
|
675
|
+
</div>
|
|
676
|
+
))}
|
|
677
|
+
</aside>
|
|
678
|
+
);
|
|
679
|
+
}
|
|
680
|
+
```
|
|
681
|
+
|
|
682
|
+
**Type Definitions:**
|
|
683
|
+
|
|
684
|
+
```tsx
|
|
685
|
+
import type {
|
|
686
|
+
CommentEvents,
|
|
687
|
+
CommentEventMap,
|
|
688
|
+
Unsubscribe,
|
|
689
|
+
} from '@kendew-agency/react-feedback-layer/types';
|
|
690
|
+
```
|
|
691
|
+
|
|
553
692
|
### Types
|
|
554
693
|
|
|
555
694
|
Import types from the `/types` subpath:
|
|
@@ -1211,6 +1350,13 @@ MIT © [Kendew Agency](https://github.com/Kendew-Agency)
|
|
|
1211
1350
|
|
|
1212
1351
|
A list if breaking changes that could impact the way you configured the package
|
|
1213
1352
|
|
|
1353
|
+
### 0.3.2
|
|
1354
|
+
- Added `withPosition` prop to `Comment.Root`. It defaults to `true`, so existing behavior is unchanged. Set it to `false` to skip the absolute positioning styles (`position`, `top`, `left`), for example when rendering comments in a list.
|
|
1355
|
+
|
|
1356
|
+
### 0.3.1
|
|
1357
|
+
- 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.
|
|
1358
|
+
- New types exported: `CommentEvents`, `CommentEventMap`, `Unsubscribe`.
|
|
1359
|
+
|
|
1214
1360
|
### 0.3.0
|
|
1215
1361
|
- 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
1362
|
- `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
|
|
@@ -641,13 +711,19 @@ function useCommentScope() {
|
|
|
641
711
|
var import_jsx_runtime5 = require("react/jsx-runtime");
|
|
642
712
|
var CommentRoot = ({
|
|
643
713
|
comment,
|
|
644
|
-
children,
|
|
645
714
|
defaultDraft,
|
|
715
|
+
withPosition = true,
|
|
716
|
+
children,
|
|
646
717
|
...rest
|
|
647
718
|
}) => {
|
|
648
719
|
const { getActiveComment } = useComments();
|
|
649
720
|
const isActive = getActiveComment()?.id === comment.id;
|
|
650
|
-
const [draft, setDraft] = (0,
|
|
721
|
+
const [draft, setDraft] = (0, import_react4.useState)(defaultDraft ?? comment.content ?? "");
|
|
722
|
+
const positionStyles = {
|
|
723
|
+
position: "absolute",
|
|
724
|
+
top: comment.position.y,
|
|
725
|
+
left: comment.position.x
|
|
726
|
+
};
|
|
651
727
|
return /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(
|
|
652
728
|
CommentScopeContext.Provider,
|
|
653
729
|
{
|
|
@@ -659,10 +735,8 @@ var CommentRoot = ({
|
|
|
659
735
|
"data-comment": comment.id,
|
|
660
736
|
"data-active": isActive,
|
|
661
737
|
style: {
|
|
662
|
-
top: comment.position.y,
|
|
663
|
-
left: comment.position.x,
|
|
664
|
-
position: "absolute",
|
|
665
738
|
zIndex: isActive ? 40 : 30,
|
|
739
|
+
...withPosition ? positionStyles : {},
|
|
666
740
|
...rest.style ?? {}
|
|
667
741
|
},
|
|
668
742
|
children
|
|
@@ -776,18 +850,60 @@ var CommentResolve = ({ asChild, ...rest }) => {
|
|
|
776
850
|
);
|
|
777
851
|
};
|
|
778
852
|
var Comment = {
|
|
853
|
+
/**
|
|
854
|
+
* The root component for a comment, must be used to wrap all other comment components.
|
|
855
|
+
*
|
|
856
|
+
* @param comment as the current comment from the comments array
|
|
857
|
+
* @param defaultDraft as the optional initial value for the comment edit state
|
|
858
|
+
* @param withPosition as whether to inject positioning styles from the comment's position. Disable if you want to display the comment in a list. Defaults to true
|
|
859
|
+
*/
|
|
779
860
|
// biome-ignore lint/style/useNamingConvention: Modular export
|
|
780
861
|
Root: CommentRoot,
|
|
862
|
+
/**
|
|
863
|
+
* The comment indicator
|
|
864
|
+
*
|
|
865
|
+
* @description Shows a dashed line that indicates a section this comment relates to
|
|
866
|
+
*/
|
|
781
867
|
// biome-ignore lint/style/useNamingConvention: Modular export
|
|
782
868
|
Indicator: CommentIndicator,
|
|
869
|
+
/**
|
|
870
|
+
* The comment's static safed content
|
|
871
|
+
*
|
|
872
|
+
* @description Displays the comment's content as a `<p>` tag
|
|
873
|
+
* @param asChild as whether to use the child as the base element instead of the default `<p>` tag. This allows for more flexibility
|
|
874
|
+
*/
|
|
783
875
|
// biome-ignore lint/style/useNamingConvention: Modular export
|
|
784
876
|
Content: CommentContent,
|
|
877
|
+
/**
|
|
878
|
+
* The comment's editable content
|
|
879
|
+
*
|
|
880
|
+
* @description A textarea to edit the comment's content
|
|
881
|
+
* @param asChild as whether to use the child as the base element instead of the default `<textarea>` tag. This allows for more flexibility
|
|
882
|
+
*/
|
|
785
883
|
// biome-ignore lint/style/useNamingConvention: Modular export
|
|
786
884
|
Edit: CommentEdit,
|
|
885
|
+
/**
|
|
886
|
+
* Confirms the changes made to a comment
|
|
887
|
+
*
|
|
888
|
+
* @description Updates the comment's content with the current draft
|
|
889
|
+
* @param asChild as whether to use the child as the base element instead of the default `<button>` tag. This allows for more flexibility
|
|
890
|
+
*/
|
|
787
891
|
// biome-ignore lint/style/useNamingConvention: Modular export
|
|
788
892
|
Confirm: CommentConfirm,
|
|
893
|
+
/**
|
|
894
|
+
* Deletes a comment
|
|
895
|
+
*
|
|
896
|
+
* @description Removes the comment from the comment list
|
|
897
|
+
* @param asChild as whether to use the child as the base element instead of the default `<button>` tag. This allows for more flexibility
|
|
898
|
+
*/
|
|
789
899
|
// biome-ignore lint/style/useNamingConvention: Modular export
|
|
790
900
|
Delete: CommentDelete,
|
|
901
|
+
/**
|
|
902
|
+
* Toggles the resolving status of a comment
|
|
903
|
+
*
|
|
904
|
+
* @description Toggles the comment's resolving status, which will show a different UI state
|
|
905
|
+
* @param asChild as whether to use the child as the base element instead of the default `<button>` tag. This allows for more flexibility
|
|
906
|
+
*/
|
|
791
907
|
// biome-ignore lint/style/useNamingConvention: Modular export
|
|
792
908
|
Resolve: CommentResolve
|
|
793
909
|
};
|
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;
|
|
@@ -23,9 +23,26 @@ type CommentRendererProps = {
|
|
|
23
23
|
}>;
|
|
24
24
|
};
|
|
25
25
|
/**
|
|
26
|
+
* Comment rendering component
|
|
27
|
+
* @remarks
|
|
28
|
+
* This component is used to render all comments based on their state.
|
|
29
|
+
* This component is optional and the samen could be built using the `useComments()` hook
|
|
26
30
|
*
|
|
27
|
-
* @param
|
|
28
|
-
* @
|
|
31
|
+
* @param Comment - Component for all open comments
|
|
32
|
+
* @param DraftComment - Component for all draft comments
|
|
33
|
+
* @param ResolvingComment - Component for all resolving comments
|
|
34
|
+
* @param ResolvedComment - Component for all resolved comments
|
|
35
|
+
* @returns mapped arrays of all comment types
|
|
36
|
+
* @description This component is used to render all comments based on their type
|
|
37
|
+
* @example
|
|
38
|
+
* ```tsx
|
|
39
|
+
* <CommentRenderer
|
|
40
|
+
* Comment={MyCommentComponent}
|
|
41
|
+
* DraftComment={MyDraftCommentComponent}
|
|
42
|
+
* ResolvingComment={MyResolvingCommentComponent}
|
|
43
|
+
* ResolvedComment={MyResolvedCommentComponent}
|
|
44
|
+
* />
|
|
45
|
+
* ```
|
|
29
46
|
*/
|
|
30
47
|
declare const CommentRenderer: ({ Comment, DraftComment, ResolvingComment, ResolvedComment, }: CommentRendererProps) => react_jsx_runtime.JSX.Element | null;
|
|
31
48
|
|
|
@@ -38,8 +55,9 @@ declare const ResolveComments: ({ asChild, ...rest }: CommentButtonProps) => rea
|
|
|
38
55
|
|
|
39
56
|
interface CommentRootProps extends HTMLProps<HTMLDivElement> {
|
|
40
57
|
comment: CommentType;
|
|
41
|
-
children?: ReactNode;
|
|
42
58
|
defaultDraft?: string;
|
|
59
|
+
withPosition?: boolean;
|
|
60
|
+
children?: ReactNode;
|
|
43
61
|
}
|
|
44
62
|
interface CommentIndicatorProps extends HTMLProps<HTMLDivElement> {
|
|
45
63
|
}
|
|
@@ -59,12 +77,54 @@ interface CommentResolveProps extends ButtonHTMLAttributes<HTMLButtonElement> {
|
|
|
59
77
|
asChild?: boolean;
|
|
60
78
|
}
|
|
61
79
|
declare const Comment: {
|
|
62
|
-
|
|
80
|
+
/**
|
|
81
|
+
* The root component for a comment, must be used to wrap all other comment components.
|
|
82
|
+
*
|
|
83
|
+
* @param comment as the current comment from the comments array
|
|
84
|
+
* @param defaultDraft as the optional initial value for the comment edit state
|
|
85
|
+
* @param withPosition as whether to inject positioning styles from the comment's position. Disable if you want to display the comment in a list. Defaults to true
|
|
86
|
+
*/
|
|
87
|
+
Root: ({ comment, defaultDraft, withPosition, children, ...rest }: CommentRootProps) => react_jsx_runtime.JSX.Element;
|
|
88
|
+
/**
|
|
89
|
+
* The comment indicator
|
|
90
|
+
*
|
|
91
|
+
* @description Shows a dashed line that indicates a section this comment relates to
|
|
92
|
+
*/
|
|
63
93
|
Indicator: ({ ...rest }: CommentIndicatorProps) => react_jsx_runtime.JSX.Element | null;
|
|
94
|
+
/**
|
|
95
|
+
* The comment's static safed content
|
|
96
|
+
*
|
|
97
|
+
* @description Displays the comment's content as a `<p>` tag
|
|
98
|
+
* @param asChild as whether to use the child as the base element instead of the default `<p>` tag. This allows for more flexibility
|
|
99
|
+
*/
|
|
64
100
|
Content: ({ asChild, ...rest }: CommentContentProps) => react_jsx_runtime.JSX.Element;
|
|
101
|
+
/**
|
|
102
|
+
* The comment's editable content
|
|
103
|
+
*
|
|
104
|
+
* @description A textarea to edit the comment's content
|
|
105
|
+
* @param asChild as whether to use the child as the base element instead of the default `<textarea>` tag. This allows for more flexibility
|
|
106
|
+
*/
|
|
65
107
|
Edit: ({ asChild, ...rest }: CommentEditProps) => react_jsx_runtime.JSX.Element;
|
|
108
|
+
/**
|
|
109
|
+
* Confirms the changes made to a comment
|
|
110
|
+
*
|
|
111
|
+
* @description Updates the comment's content with the current draft
|
|
112
|
+
* @param asChild as whether to use the child as the base element instead of the default `<button>` tag. This allows for more flexibility
|
|
113
|
+
*/
|
|
66
114
|
Confirm: ({ asChild, ...rest }: CommentConfirmProps) => react_jsx_runtime.JSX.Element;
|
|
115
|
+
/**
|
|
116
|
+
* Deletes a comment
|
|
117
|
+
*
|
|
118
|
+
* @description Removes the comment from the comment list
|
|
119
|
+
* @param asChild as whether to use the child as the base element instead of the default `<button>` tag. This allows for more flexibility
|
|
120
|
+
*/
|
|
67
121
|
Delete: ({ asChild, ...rest }: CommentDeleteProps) => react_jsx_runtime.JSX.Element;
|
|
122
|
+
/**
|
|
123
|
+
* Toggles the resolving status of a comment
|
|
124
|
+
*
|
|
125
|
+
* @description Toggles the comment's resolving status, which will show a different UI state
|
|
126
|
+
* @param asChild as whether to use the child as the base element instead of the default `<button>` tag. This allows for more flexibility
|
|
127
|
+
*/
|
|
68
128
|
Resolve: ({ asChild, ...rest }: CommentResolveProps) => react_jsx_runtime.JSX.Element | null;
|
|
69
129
|
};
|
|
70
130
|
|
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;
|
|
@@ -23,9 +23,26 @@ type CommentRendererProps = {
|
|
|
23
23
|
}>;
|
|
24
24
|
};
|
|
25
25
|
/**
|
|
26
|
+
* Comment rendering component
|
|
27
|
+
* @remarks
|
|
28
|
+
* This component is used to render all comments based on their state.
|
|
29
|
+
* This component is optional and the samen could be built using the `useComments()` hook
|
|
26
30
|
*
|
|
27
|
-
* @param
|
|
28
|
-
* @
|
|
31
|
+
* @param Comment - Component for all open comments
|
|
32
|
+
* @param DraftComment - Component for all draft comments
|
|
33
|
+
* @param ResolvingComment - Component for all resolving comments
|
|
34
|
+
* @param ResolvedComment - Component for all resolved comments
|
|
35
|
+
* @returns mapped arrays of all comment types
|
|
36
|
+
* @description This component is used to render all comments based on their type
|
|
37
|
+
* @example
|
|
38
|
+
* ```tsx
|
|
39
|
+
* <CommentRenderer
|
|
40
|
+
* Comment={MyCommentComponent}
|
|
41
|
+
* DraftComment={MyDraftCommentComponent}
|
|
42
|
+
* ResolvingComment={MyResolvingCommentComponent}
|
|
43
|
+
* ResolvedComment={MyResolvedCommentComponent}
|
|
44
|
+
* />
|
|
45
|
+
* ```
|
|
29
46
|
*/
|
|
30
47
|
declare const CommentRenderer: ({ Comment, DraftComment, ResolvingComment, ResolvedComment, }: CommentRendererProps) => react_jsx_runtime.JSX.Element | null;
|
|
31
48
|
|
|
@@ -38,8 +55,9 @@ declare const ResolveComments: ({ asChild, ...rest }: CommentButtonProps) => rea
|
|
|
38
55
|
|
|
39
56
|
interface CommentRootProps extends HTMLProps<HTMLDivElement> {
|
|
40
57
|
comment: CommentType;
|
|
41
|
-
children?: ReactNode;
|
|
42
58
|
defaultDraft?: string;
|
|
59
|
+
withPosition?: boolean;
|
|
60
|
+
children?: ReactNode;
|
|
43
61
|
}
|
|
44
62
|
interface CommentIndicatorProps extends HTMLProps<HTMLDivElement> {
|
|
45
63
|
}
|
|
@@ -59,12 +77,54 @@ interface CommentResolveProps extends ButtonHTMLAttributes<HTMLButtonElement> {
|
|
|
59
77
|
asChild?: boolean;
|
|
60
78
|
}
|
|
61
79
|
declare const Comment: {
|
|
62
|
-
|
|
80
|
+
/**
|
|
81
|
+
* The root component for a comment, must be used to wrap all other comment components.
|
|
82
|
+
*
|
|
83
|
+
* @param comment as the current comment from the comments array
|
|
84
|
+
* @param defaultDraft as the optional initial value for the comment edit state
|
|
85
|
+
* @param withPosition as whether to inject positioning styles from the comment's position. Disable if you want to display the comment in a list. Defaults to true
|
|
86
|
+
*/
|
|
87
|
+
Root: ({ comment, defaultDraft, withPosition, children, ...rest }: CommentRootProps) => react_jsx_runtime.JSX.Element;
|
|
88
|
+
/**
|
|
89
|
+
* The comment indicator
|
|
90
|
+
*
|
|
91
|
+
* @description Shows a dashed line that indicates a section this comment relates to
|
|
92
|
+
*/
|
|
63
93
|
Indicator: ({ ...rest }: CommentIndicatorProps) => react_jsx_runtime.JSX.Element | null;
|
|
94
|
+
/**
|
|
95
|
+
* The comment's static safed content
|
|
96
|
+
*
|
|
97
|
+
* @description Displays the comment's content as a `<p>` tag
|
|
98
|
+
* @param asChild as whether to use the child as the base element instead of the default `<p>` tag. This allows for more flexibility
|
|
99
|
+
*/
|
|
64
100
|
Content: ({ asChild, ...rest }: CommentContentProps) => react_jsx_runtime.JSX.Element;
|
|
101
|
+
/**
|
|
102
|
+
* The comment's editable content
|
|
103
|
+
*
|
|
104
|
+
* @description A textarea to edit the comment's content
|
|
105
|
+
* @param asChild as whether to use the child as the base element instead of the default `<textarea>` tag. This allows for more flexibility
|
|
106
|
+
*/
|
|
65
107
|
Edit: ({ asChild, ...rest }: CommentEditProps) => react_jsx_runtime.JSX.Element;
|
|
108
|
+
/**
|
|
109
|
+
* Confirms the changes made to a comment
|
|
110
|
+
*
|
|
111
|
+
* @description Updates the comment's content with the current draft
|
|
112
|
+
* @param asChild as whether to use the child as the base element instead of the default `<button>` tag. This allows for more flexibility
|
|
113
|
+
*/
|
|
66
114
|
Confirm: ({ asChild, ...rest }: CommentConfirmProps) => react_jsx_runtime.JSX.Element;
|
|
115
|
+
/**
|
|
116
|
+
* Deletes a comment
|
|
117
|
+
*
|
|
118
|
+
* @description Removes the comment from the comment list
|
|
119
|
+
* @param asChild as whether to use the child as the base element instead of the default `<button>` tag. This allows for more flexibility
|
|
120
|
+
*/
|
|
67
121
|
Delete: ({ asChild, ...rest }: CommentDeleteProps) => react_jsx_runtime.JSX.Element;
|
|
122
|
+
/**
|
|
123
|
+
* Toggles the resolving status of a comment
|
|
124
|
+
*
|
|
125
|
+
* @description Toggles the comment's resolving status, which will show a different UI state
|
|
126
|
+
* @param asChild as whether to use the child as the base element instead of the default `<button>` tag. This allows for more flexibility
|
|
127
|
+
*/
|
|
68
128
|
Resolve: ({ asChild, ...rest }: CommentResolveProps) => react_jsx_runtime.JSX.Element | null;
|
|
69
129
|
};
|
|
70
130
|
|
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(
|
|
@@ -601,13 +677,19 @@ function useCommentScope() {
|
|
|
601
677
|
import { jsx as jsx5 } from "react/jsx-runtime";
|
|
602
678
|
var CommentRoot = ({
|
|
603
679
|
comment,
|
|
604
|
-
children,
|
|
605
680
|
defaultDraft,
|
|
681
|
+
withPosition = true,
|
|
682
|
+
children,
|
|
606
683
|
...rest
|
|
607
684
|
}) => {
|
|
608
685
|
const { getActiveComment } = useComments();
|
|
609
686
|
const isActive = getActiveComment()?.id === comment.id;
|
|
610
687
|
const [draft, setDraft] = useState2(defaultDraft ?? comment.content ?? "");
|
|
688
|
+
const positionStyles = {
|
|
689
|
+
position: "absolute",
|
|
690
|
+
top: comment.position.y,
|
|
691
|
+
left: comment.position.x
|
|
692
|
+
};
|
|
611
693
|
return /* @__PURE__ */ jsx5(
|
|
612
694
|
CommentScopeContext.Provider,
|
|
613
695
|
{
|
|
@@ -619,10 +701,8 @@ var CommentRoot = ({
|
|
|
619
701
|
"data-comment": comment.id,
|
|
620
702
|
"data-active": isActive,
|
|
621
703
|
style: {
|
|
622
|
-
top: comment.position.y,
|
|
623
|
-
left: comment.position.x,
|
|
624
|
-
position: "absolute",
|
|
625
704
|
zIndex: isActive ? 40 : 30,
|
|
705
|
+
...withPosition ? positionStyles : {},
|
|
626
706
|
...rest.style ?? {}
|
|
627
707
|
},
|
|
628
708
|
children
|
|
@@ -736,18 +816,60 @@ var CommentResolve = ({ asChild, ...rest }) => {
|
|
|
736
816
|
);
|
|
737
817
|
};
|
|
738
818
|
var Comment = {
|
|
819
|
+
/**
|
|
820
|
+
* The root component for a comment, must be used to wrap all other comment components.
|
|
821
|
+
*
|
|
822
|
+
* @param comment as the current comment from the comments array
|
|
823
|
+
* @param defaultDraft as the optional initial value for the comment edit state
|
|
824
|
+
* @param withPosition as whether to inject positioning styles from the comment's position. Disable if you want to display the comment in a list. Defaults to true
|
|
825
|
+
*/
|
|
739
826
|
// biome-ignore lint/style/useNamingConvention: Modular export
|
|
740
827
|
Root: CommentRoot,
|
|
828
|
+
/**
|
|
829
|
+
* The comment indicator
|
|
830
|
+
*
|
|
831
|
+
* @description Shows a dashed line that indicates a section this comment relates to
|
|
832
|
+
*/
|
|
741
833
|
// biome-ignore lint/style/useNamingConvention: Modular export
|
|
742
834
|
Indicator: CommentIndicator,
|
|
835
|
+
/**
|
|
836
|
+
* The comment's static safed content
|
|
837
|
+
*
|
|
838
|
+
* @description Displays the comment's content as a `<p>` tag
|
|
839
|
+
* @param asChild as whether to use the child as the base element instead of the default `<p>` tag. This allows for more flexibility
|
|
840
|
+
*/
|
|
743
841
|
// biome-ignore lint/style/useNamingConvention: Modular export
|
|
744
842
|
Content: CommentContent,
|
|
843
|
+
/**
|
|
844
|
+
* The comment's editable content
|
|
845
|
+
*
|
|
846
|
+
* @description A textarea to edit the comment's content
|
|
847
|
+
* @param asChild as whether to use the child as the base element instead of the default `<textarea>` tag. This allows for more flexibility
|
|
848
|
+
*/
|
|
745
849
|
// biome-ignore lint/style/useNamingConvention: Modular export
|
|
746
850
|
Edit: CommentEdit,
|
|
851
|
+
/**
|
|
852
|
+
* Confirms the changes made to a comment
|
|
853
|
+
*
|
|
854
|
+
* @description Updates the comment's content with the current draft
|
|
855
|
+
* @param asChild as whether to use the child as the base element instead of the default `<button>` tag. This allows for more flexibility
|
|
856
|
+
*/
|
|
747
857
|
// biome-ignore lint/style/useNamingConvention: Modular export
|
|
748
858
|
Confirm: CommentConfirm,
|
|
859
|
+
/**
|
|
860
|
+
* Deletes a comment
|
|
861
|
+
*
|
|
862
|
+
* @description Removes the comment from the comment list
|
|
863
|
+
* @param asChild as whether to use the child as the base element instead of the default `<button>` tag. This allows for more flexibility
|
|
864
|
+
*/
|
|
749
865
|
// biome-ignore lint/style/useNamingConvention: Modular export
|
|
750
866
|
Delete: CommentDelete,
|
|
867
|
+
/**
|
|
868
|
+
* Toggles the resolving status of a comment
|
|
869
|
+
*
|
|
870
|
+
* @description Toggles the comment's resolving status, which will show a different UI state
|
|
871
|
+
* @param asChild as whether to use the child as the base element instead of the default `<button>` tag. This allows for more flexibility
|
|
872
|
+
*/
|
|
751
873
|
// biome-ignore lint/style/useNamingConvention: Modular export
|
|
752
874
|
Resolve: CommentResolve
|
|
753
875
|
};
|
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
|
/**
|