@tldraw/commenting 0.0.0-bootstrap
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 +11 -0
- package/commenting.css +832 -0
- package/dist-cjs/canvas/cluster-input.js +40 -0
- package/dist-cjs/canvas/cluster-input.js.map +7 -0
- package/dist-cjs/canvas/comment-body.js +31 -0
- package/dist-cjs/canvas/comment-body.js.map +7 -0
- package/dist-cjs/canvas/comment-render.js +72 -0
- package/dist-cjs/canvas/comment-render.js.map +7 -0
- package/dist-cjs/canvas/comment-store.js +48 -0
- package/dist-cjs/canvas/comment-store.js.map +7 -0
- package/dist-cjs/canvas/comment-tool.js +160 -0
- package/dist-cjs/canvas/comment-tool.js.map +7 -0
- package/dist-cjs/canvas/comments-filter-menu.js +109 -0
- package/dist-cjs/canvas/comments-filter-menu.js.map +7 -0
- package/dist-cjs/canvas/comments-menu-item.js +42 -0
- package/dist-cjs/canvas/comments-menu-item.js.map +7 -0
- package/dist-cjs/canvas/comments-overflow-menu.js +64 -0
- package/dist-cjs/canvas/comments-overflow-menu.js.map +7 -0
- package/dist-cjs/canvas/comments-overlay.js +1081 -0
- package/dist-cjs/canvas/comments-overlay.js.map +7 -0
- package/dist-cjs/canvas/comments-sidebar.js +134 -0
- package/dist-cjs/canvas/comments-sidebar.js.map +7 -0
- package/dist-cjs/canvas/hooks.js +45 -0
- package/dist-cjs/canvas/hooks.js.map +7 -0
- package/dist-cjs/canvas/license.js +28 -0
- package/dist-cjs/canvas/license.js.map +7 -0
- package/dist-cjs/canvas/options.js +45 -0
- package/dist-cjs/canvas/options.js.map +7 -0
- package/dist-cjs/canvas/region-options.js +49 -0
- package/dist-cjs/canvas/region-options.js.map +7 -0
- package/dist-cjs/canvas/rich-text.js +28 -0
- package/dist-cjs/canvas/rich-text.js.map +7 -0
- package/dist-cjs/canvas/sidebar-filters.js +30 -0
- package/dist-cjs/canvas/sidebar-filters.js.map +7 -0
- package/dist-cjs/canvas/state.js +76 -0
- package/dist-cjs/canvas/state.js.map +7 -0
- package/dist-cjs/canvas/thread-state.js +81 -0
- package/dist-cjs/canvas/thread-state.js.map +7 -0
- package/dist-cjs/clustering/computeClusterTable.js +83 -0
- package/dist-cjs/clustering/computeClusterTable.js.map +7 -0
- package/dist-cjs/clustering/mst.js +117 -0
- package/dist-cjs/clustering/mst.js.map +7 -0
- package/dist-cjs/clustering/replay.js +275 -0
- package/dist-cjs/clustering/replay.js.map +7 -0
- package/dist-cjs/clustering/runtime.js +134 -0
- package/dist-cjs/clustering/runtime.js.map +7 -0
- package/dist-cjs/clustering/schedule.js +170 -0
- package/dist-cjs/clustering/schedule.js.map +7 -0
- package/dist-cjs/clustering/types.js +17 -0
- package/dist-cjs/clustering/types.js.map +7 -0
- package/dist-cjs/index.d.ts +868 -0
- package/dist-cjs/index.js +126 -0
- package/dist-cjs/index.js.map +7 -0
- package/dist-cjs/ui/avatar.js +43 -0
- package/dist-cjs/ui/avatar.js.map +7 -0
- package/dist-cjs/ui/byline.js +35 -0
- package/dist-cjs/ui/byline.js.map +7 -0
- package/dist-cjs/ui/comment-card.js +37 -0
- package/dist-cjs/ui/comment-card.js.map +7 -0
- package/dist-cjs/ui/comment-composer.js +136 -0
- package/dist-cjs/ui/comment-composer.js.map +7 -0
- package/dist-cjs/ui/comment-extensions.js +37 -0
- package/dist-cjs/ui/comment-extensions.js.map +7 -0
- package/dist-cjs/ui/comment-mention.js +42 -0
- package/dist-cjs/ui/comment-mention.js.map +7 -0
- package/dist-cjs/ui/comment-pin.js +29 -0
- package/dist-cjs/ui/comment-pin.js.map +7 -0
- package/dist-cjs/ui/comment-text.js +29 -0
- package/dist-cjs/ui/comment-text.js.map +7 -0
- package/dist-cjs/ui/comment-thread.js +45 -0
- package/dist-cjs/ui/comment-thread.js.map +7 -0
- package/dist-cjs/ui/comments-list.js +106 -0
- package/dist-cjs/ui/comments-list.js.map +7 -0
- package/dist-cjs/ui/count-badge.js +28 -0
- package/dist-cjs/ui/count-badge.js.map +7 -0
- package/dist-cjs/ui/empty-state.js +31 -0
- package/dist-cjs/ui/empty-state.js.map +7 -0
- package/dist-cjs/ui/format-time.js +46 -0
- package/dist-cjs/ui/format-time.js.map +7 -0
- package/dist-cjs/ui/mention-list.js +70 -0
- package/dist-cjs/ui/mention-list.js.map +7 -0
- package/dist-cjs/ui/mention-suggestion.js +206 -0
- package/dist-cjs/ui/mention-suggestion.js.map +7 -0
- package/dist-cjs/ui/mention.js +31 -0
- package/dist-cjs/ui/mention.js.map +7 -0
- package/dist-cjs/ui/reaction.js +31 -0
- package/dist-cjs/ui/reaction.js.map +7 -0
- package/dist-cjs/ui/reactions.js +34 -0
- package/dist-cjs/ui/reactions.js.map +7 -0
- package/dist-cjs/ui/render-markdown.js +63 -0
- package/dist-cjs/ui/render-markdown.js.map +7 -0
- package/dist-cjs/ui/send-button.js +28 -0
- package/dist-cjs/ui/send-button.js.map +7 -0
- package/dist-esm/canvas/cluster-input.mjs +20 -0
- package/dist-esm/canvas/cluster-input.mjs.map +7 -0
- package/dist-esm/canvas/comment-body.mjs +11 -0
- package/dist-esm/canvas/comment-body.mjs.map +7 -0
- package/dist-esm/canvas/comment-render.mjs +52 -0
- package/dist-esm/canvas/comment-render.mjs.map +7 -0
- package/dist-esm/canvas/comment-store.mjs +28 -0
- package/dist-esm/canvas/comment-store.mjs.map +7 -0
- package/dist-esm/canvas/comment-tool.mjs +142 -0
- package/dist-esm/canvas/comment-tool.mjs.map +7 -0
- package/dist-esm/canvas/comments-filter-menu.mjs +99 -0
- package/dist-esm/canvas/comments-filter-menu.mjs.map +7 -0
- package/dist-esm/canvas/comments-menu-item.mjs +22 -0
- package/dist-esm/canvas/comments-menu-item.mjs.map +7 -0
- package/dist-esm/canvas/comments-overflow-menu.mjs +53 -0
- package/dist-esm/canvas/comments-overflow-menu.mjs.map +7 -0
- package/dist-esm/canvas/comments-overlay.mjs +1094 -0
- package/dist-esm/canvas/comments-overlay.mjs.map +7 -0
- package/dist-esm/canvas/comments-sidebar.mjs +120 -0
- package/dist-esm/canvas/comments-sidebar.mjs.map +7 -0
- package/dist-esm/canvas/hooks.mjs +25 -0
- package/dist-esm/canvas/hooks.mjs.map +7 -0
- package/dist-esm/canvas/license.mjs +8 -0
- package/dist-esm/canvas/license.mjs.map +7 -0
- package/dist-esm/canvas/options.mjs +27 -0
- package/dist-esm/canvas/options.mjs.map +7 -0
- package/dist-esm/canvas/region-options.mjs +29 -0
- package/dist-esm/canvas/region-options.mjs.map +7 -0
- package/dist-esm/canvas/rich-text.mjs +8 -0
- package/dist-esm/canvas/rich-text.mjs.map +7 -0
- package/dist-esm/canvas/sidebar-filters.mjs +10 -0
- package/dist-esm/canvas/sidebar-filters.mjs.map +7 -0
- package/dist-esm/canvas/state.mjs +60 -0
- package/dist-esm/canvas/state.mjs.map +7 -0
- package/dist-esm/canvas/thread-state.mjs +61 -0
- package/dist-esm/canvas/thread-state.mjs.map +7 -0
- package/dist-esm/clustering/computeClusterTable.mjs +63 -0
- package/dist-esm/clustering/computeClusterTable.mjs.map +7 -0
- package/dist-esm/clustering/mst.mjs +97 -0
- package/dist-esm/clustering/mst.mjs.map +7 -0
- package/dist-esm/clustering/replay.mjs +255 -0
- package/dist-esm/clustering/replay.mjs.map +7 -0
- package/dist-esm/clustering/runtime.mjs +114 -0
- package/dist-esm/clustering/runtime.mjs.map +7 -0
- package/dist-esm/clustering/schedule.mjs +150 -0
- package/dist-esm/clustering/schedule.mjs.map +7 -0
- package/dist-esm/clustering/types.mjs +1 -0
- package/dist-esm/clustering/types.mjs.map +7 -0
- package/dist-esm/index.d.mts +868 -0
- package/dist-esm/index.mjs +139 -0
- package/dist-esm/index.mjs.map +7 -0
- package/dist-esm/ui/avatar.mjs +23 -0
- package/dist-esm/ui/avatar.mjs.map +7 -0
- package/dist-esm/ui/byline.mjs +15 -0
- package/dist-esm/ui/byline.mjs.map +7 -0
- package/dist-esm/ui/comment-card.mjs +17 -0
- package/dist-esm/ui/comment-card.mjs.map +7 -0
- package/dist-esm/ui/comment-composer.mjs +116 -0
- package/dist-esm/ui/comment-composer.mjs.map +7 -0
- package/dist-esm/ui/comment-extensions.mjs +17 -0
- package/dist-esm/ui/comment-extensions.mjs.map +7 -0
- package/dist-esm/ui/comment-mention.mjs +22 -0
- package/dist-esm/ui/comment-mention.mjs.map +7 -0
- package/dist-esm/ui/comment-pin.mjs +9 -0
- package/dist-esm/ui/comment-pin.mjs.map +7 -0
- package/dist-esm/ui/comment-text.mjs +9 -0
- package/dist-esm/ui/comment-text.mjs.map +7 -0
- package/dist-esm/ui/comment-thread.mjs +25 -0
- package/dist-esm/ui/comment-thread.mjs.map +7 -0
- package/dist-esm/ui/comments-list.mjs +86 -0
- package/dist-esm/ui/comments-list.mjs.map +7 -0
- package/dist-esm/ui/count-badge.mjs +8 -0
- package/dist-esm/ui/count-badge.mjs.map +7 -0
- package/dist-esm/ui/empty-state.mjs +11 -0
- package/dist-esm/ui/empty-state.mjs.map +7 -0
- package/dist-esm/ui/format-time.mjs +26 -0
- package/dist-esm/ui/format-time.mjs.map +7 -0
- package/dist-esm/ui/mention-list.mjs +50 -0
- package/dist-esm/ui/mention-list.mjs.map +7 -0
- package/dist-esm/ui/mention-suggestion.mjs +186 -0
- package/dist-esm/ui/mention-suggestion.mjs.map +7 -0
- package/dist-esm/ui/mention.mjs +11 -0
- package/dist-esm/ui/mention.mjs.map +7 -0
- package/dist-esm/ui/reaction.mjs +11 -0
- package/dist-esm/ui/reaction.mjs.map +7 -0
- package/dist-esm/ui/reactions.mjs +14 -0
- package/dist-esm/ui/reactions.mjs.map +7 -0
- package/dist-esm/ui/render-markdown.mjs +45 -0
- package/dist-esm/ui/render-markdown.mjs.map +7 -0
- package/dist-esm/ui/send-button.mjs +8 -0
- package/dist-esm/ui/send-button.mjs.map +7 -0
- package/package.json +71 -0
- package/src/canvas/canvas.css +148 -0
- package/src/canvas/cluster-input.test.ts +223 -0
- package/src/canvas/cluster-input.ts +29 -0
- package/src/canvas/comment-body.tsx +21 -0
- package/src/canvas/comment-render.test.ts +90 -0
- package/src/canvas/comment-render.ts +92 -0
- package/src/canvas/comment-store.ts +55 -0
- package/src/canvas/comment-tool.tsx +200 -0
- package/src/canvas/comments-filter-menu.tsx +104 -0
- package/src/canvas/comments-menu-item.tsx +24 -0
- package/src/canvas/comments-overflow-menu.tsx +62 -0
- package/src/canvas/comments-overlay.tsx +1460 -0
- package/src/canvas/comments-sidebar.tsx +184 -0
- package/src/canvas/hooks.ts +28 -0
- package/src/canvas/license.ts +15 -0
- package/src/canvas/options.test.ts +127 -0
- package/src/canvas/options.ts +111 -0
- package/src/canvas/region-options.ts +57 -0
- package/src/canvas/rich-text.ts +15 -0
- package/src/canvas/sidebar-filters.ts +25 -0
- package/src/canvas/state.ts +102 -0
- package/src/canvas/thread-state.ts +91 -0
- package/src/clustering/computeClusterTable.test.ts +299 -0
- package/src/clustering/computeClusterTable.ts +82 -0
- package/src/clustering/locality.test.ts +189 -0
- package/src/clustering/mst.test.ts +383 -0
- package/src/clustering/mst.ts +134 -0
- package/src/clustering/replay.test.ts +547 -0
- package/src/clustering/replay.ts +326 -0
- package/src/clustering/runtime.test.ts +596 -0
- package/src/clustering/runtime.ts +153 -0
- package/src/clustering/schedule.test.ts +514 -0
- package/src/clustering/schedule.ts +184 -0
- package/src/clustering/types.ts +102 -0
- package/src/index.ts +96 -0
- package/src/ui/avatar.tsx +31 -0
- package/src/ui/byline.tsx +23 -0
- package/src/ui/comment-card.tsx +32 -0
- package/src/ui/comment-composer.tsx +179 -0
- package/src/ui/comment-extensions.ts +25 -0
- package/src/ui/comment-mention.ts +47 -0
- package/src/ui/comment-pin.tsx +21 -0
- package/src/ui/comment-text.tsx +12 -0
- package/src/ui/comment-thread.tsx +56 -0
- package/src/ui/comments-list.tsx +139 -0
- package/src/ui/comments.css +678 -0
- package/src/ui/count-badge.tsx +9 -0
- package/src/ui/empty-state.tsx +16 -0
- package/src/ui/format-time.ts +29 -0
- package/src/ui/mention-list.tsx +106 -0
- package/src/ui/mention-suggestion.test.ts +18 -0
- package/src/ui/mention-suggestion.tsx +285 -0
- package/src/ui/mention.tsx +9 -0
- package/src/ui/reaction.tsx +16 -0
- package/src/ui/reactions.tsx +15 -0
- package/src/ui/render-markdown.tsx +72 -0
- package/src/ui/send-button.tsx +15 -0
|
@@ -0,0 +1,1094 @@
|
|
|
1
|
+
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
|
|
2
|
+
import {
|
|
3
|
+
memo,
|
|
4
|
+
useCallback,
|
|
5
|
+
useEffect,
|
|
6
|
+
useMemo,
|
|
7
|
+
useRef,
|
|
8
|
+
useState
|
|
9
|
+
} from "react";
|
|
10
|
+
import { createPortal } from "react-dom";
|
|
11
|
+
import {
|
|
12
|
+
createComment,
|
|
13
|
+
createCommentThread,
|
|
14
|
+
getFirstCharacter,
|
|
15
|
+
react,
|
|
16
|
+
TldrawUiIcon,
|
|
17
|
+
useContainer,
|
|
18
|
+
useEditor,
|
|
19
|
+
usePassThroughMouseOverEvents,
|
|
20
|
+
usePassThroughWheelEvents,
|
|
21
|
+
useTranslation,
|
|
22
|
+
useValue
|
|
23
|
+
} from "tldraw";
|
|
24
|
+
import { computeClusterTable } from "../clustering/computeClusterTable.mjs";
|
|
25
|
+
import { createClusterRuntime } from "../clustering/runtime.mjs";
|
|
26
|
+
import { CommentCard } from "../ui/comment-card.mjs";
|
|
27
|
+
import { CommentComposer } from "../ui/comment-composer.mjs";
|
|
28
|
+
import { EMPTY_COMMENT, isCommentEmpty } from "../ui/comment-extensions.mjs";
|
|
29
|
+
import { CommentPin } from "../ui/comment-pin.mjs";
|
|
30
|
+
import { CommentThread } from "../ui/comment-thread.mjs";
|
|
31
|
+
import { CountBadge } from "../ui/count-badge.mjs";
|
|
32
|
+
import { isMentionPickerOpen } from "../ui/mention-suggestion.mjs";
|
|
33
|
+
import { collectClusterLeaves } from "./cluster-input.mjs";
|
|
34
|
+
import { CommentBody } from "./comment-body.mjs";
|
|
35
|
+
import { UNKNOWN_AUTHOR } from "./comment-render.mjs";
|
|
36
|
+
import { getCommentRecord, putCommentRecords, removeCommentRecords } from "./comment-store.mjs";
|
|
37
|
+
import { useCommentThreads, useThreadComments } from "./hooks.mjs";
|
|
38
|
+
import { useCommentingEnabled } from "./license.mjs";
|
|
39
|
+
import {
|
|
40
|
+
getCommentingOptions,
|
|
41
|
+
useCommentingOptions
|
|
42
|
+
} from "./options.mjs";
|
|
43
|
+
import {
|
|
44
|
+
DEFAULT_REGION_COMMENT_OPTIONS,
|
|
45
|
+
setRegionCommentOptions
|
|
46
|
+
} from "./region-options.mjs";
|
|
47
|
+
import {
|
|
48
|
+
commentsHidden,
|
|
49
|
+
commitCommentMutation,
|
|
50
|
+
openThreadId,
|
|
51
|
+
pendingComment,
|
|
52
|
+
regionDraft,
|
|
53
|
+
toggleCommentsHidden,
|
|
54
|
+
usePendingComment
|
|
55
|
+
} from "./state.mjs";
|
|
56
|
+
import { anchorPagePoint, regionPinPoint, shapeAnchorAt } from "./thread-state.mjs";
|
|
57
|
+
const stop = (e) => e.stopPropagation();
|
|
58
|
+
const initialOf = (name) => (getFirstCharacter(name.trim()) || "?").toUpperCase();
|
|
59
|
+
const CLUSTER_FADE_MS = 150;
|
|
60
|
+
const CLUSTER_EXPAND_ZOOM_MS = 450;
|
|
61
|
+
const draftAvatar = /* @__PURE__ */ jsx(CommentPin, { children: /* @__PURE__ */ jsxs(
|
|
62
|
+
"svg",
|
|
63
|
+
{
|
|
64
|
+
viewBox: "0 0 24 24",
|
|
65
|
+
width: "15",
|
|
66
|
+
height: "15",
|
|
67
|
+
fill: "none",
|
|
68
|
+
stroke: "currentColor",
|
|
69
|
+
strokeWidth: "2",
|
|
70
|
+
strokeLinecap: "round",
|
|
71
|
+
strokeLinejoin: "round",
|
|
72
|
+
"aria-hidden": "true",
|
|
73
|
+
children: [
|
|
74
|
+
/* @__PURE__ */ jsx("path", { d: "M12 20h9" }),
|
|
75
|
+
/* @__PURE__ */ jsx("path", { d: "M16.5 3.5a2.12 2.12 0 0 1 3 3L7 19l-4 1 1-4 12.5-12.5z" })
|
|
76
|
+
]
|
|
77
|
+
}
|
|
78
|
+
) });
|
|
79
|
+
function toCardProps(comment, props, components) {
|
|
80
|
+
const Body = components.CommentBody;
|
|
81
|
+
const body = Body ? /* @__PURE__ */ jsx(Body, { comment }) : /* @__PURE__ */ jsx(CommentBody, { richText: comment.body, resolveName: props.resolveName });
|
|
82
|
+
return {
|
|
83
|
+
author: props.resolveName(comment.authorId) ?? UNKNOWN_AUTHOR,
|
|
84
|
+
body,
|
|
85
|
+
date: new Date(comment.createdAt).toISOString(),
|
|
86
|
+
you: comment.authorId === props.currentUserId,
|
|
87
|
+
edited: comment.editedAt != null
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
function CanvasComments(props) {
|
|
91
|
+
const commentingEnabled = useCommentingEnabled();
|
|
92
|
+
if (!commentingEnabled) return null;
|
|
93
|
+
return /* @__PURE__ */ jsx(CanvasCommentsLayer, { ...props });
|
|
94
|
+
}
|
|
95
|
+
function CanvasCommentsLayer(props) {
|
|
96
|
+
const editor = useEditor();
|
|
97
|
+
const options = useCommentingOptions();
|
|
98
|
+
const container = useContainer();
|
|
99
|
+
const regionOptions = useMemo(
|
|
100
|
+
() => ({ ...DEFAULT_REGION_COMMENT_OPTIONS, ...props.regionOptions }),
|
|
101
|
+
[props.regionOptions]
|
|
102
|
+
);
|
|
103
|
+
useEffect(() => setRegionCommentOptions(editor, regionOptions), [editor, regionOptions]);
|
|
104
|
+
const layerRef = useRef(null);
|
|
105
|
+
usePassThroughWheelEvents(layerRef);
|
|
106
|
+
usePassThroughMouseOverEvents(layerRef);
|
|
107
|
+
const deepLinkHandled = useRef(false);
|
|
108
|
+
const threads = useCommentThreads(editor);
|
|
109
|
+
const pending = usePendingComment();
|
|
110
|
+
const openId = useValue("open thread id", () => openThreadId.get(editor), [editor]);
|
|
111
|
+
const impreciseShapeAnchor = props.impreciseShapeAnchor ?? options.impreciseShapeAnchor;
|
|
112
|
+
const [movedThreadIds, setMovedThreadIds] = useState(EMPTY_SET);
|
|
113
|
+
const adoptOnRebuild = useRef(false);
|
|
114
|
+
const clusterLeaves = useValue(
|
|
115
|
+
"comment cluster leaves",
|
|
116
|
+
() => collectClusterLeaves(
|
|
117
|
+
editor,
|
|
118
|
+
threads.filter((thread) => !movedThreadIds.has(thread.id)),
|
|
119
|
+
openThreadId.get(editor),
|
|
120
|
+
impreciseShapeAnchor
|
|
121
|
+
),
|
|
122
|
+
[editor, threads, impreciseShapeAnchor, movedThreadIds]
|
|
123
|
+
);
|
|
124
|
+
const clusterZoomBounds = useValue(
|
|
125
|
+
"comment cluster zoom bounds",
|
|
126
|
+
() => getClusterZoomBounds(editor),
|
|
127
|
+
[editor]
|
|
128
|
+
);
|
|
129
|
+
const latestModel = useMemo(() => {
|
|
130
|
+
const table = computeClusterTable(clusterLeaves, clusterZoomBounds);
|
|
131
|
+
const runtime = createClusterRuntime(table);
|
|
132
|
+
runtime.seed(editor.getZoomLevel());
|
|
133
|
+
return { runtime, table };
|
|
134
|
+
}, [clusterLeaves, clusterZoomBounds, editor]);
|
|
135
|
+
const [renderedModel, setRenderedModel] = useState(latestModel);
|
|
136
|
+
let clusterModel = renderedModel;
|
|
137
|
+
if (renderedModel !== latestModel && (adoptOnRebuild.current || hasRemovedLeaves(renderedModel.table, latestModel.table))) {
|
|
138
|
+
adoptOnRebuild.current = false;
|
|
139
|
+
latestModel.runtime.seedFrom(editor.getZoomLevel(), renderedModel.runtime.getVisible());
|
|
140
|
+
setRenderedModel(latestModel);
|
|
141
|
+
clusterModel = latestModel;
|
|
142
|
+
}
|
|
143
|
+
const newlyMovedIds = findMovedClusteredLeafIds(clusterModel, latestModel);
|
|
144
|
+
if (newlyMovedIds.length > 0) {
|
|
145
|
+
console.debug(`[comments] pins popped out of clustering: ${newlyMovedIds.join(", ")}`);
|
|
146
|
+
const next = new Set(movedThreadIds);
|
|
147
|
+
for (const id of newlyMovedIds) next.add(id);
|
|
148
|
+
setMovedThreadIds(next);
|
|
149
|
+
}
|
|
150
|
+
useEffect(() => {
|
|
151
|
+
if (movedThreadIds.size === 0) return;
|
|
152
|
+
let lastZoom = editor.getZoomLevel();
|
|
153
|
+
return react("rejoin moved comment pins on zoom out", () => {
|
|
154
|
+
const zoom = editor.getZoomLevel();
|
|
155
|
+
const prevZoom = lastZoom;
|
|
156
|
+
lastZoom = zoom;
|
|
157
|
+
if (zoom >= prevZoom) return;
|
|
158
|
+
adoptOnRebuild.current = true;
|
|
159
|
+
setMovedThreadIds(EMPTY_SET);
|
|
160
|
+
});
|
|
161
|
+
}, [movedThreadIds, editor]);
|
|
162
|
+
useEffect(() => {
|
|
163
|
+
if (clusterModel === latestModel) return;
|
|
164
|
+
let lastZoom = editor.getZoomLevel();
|
|
165
|
+
return react("adopt pending cluster model on zoom out", () => {
|
|
166
|
+
const zoom = editor.getZoomLevel();
|
|
167
|
+
const prevZoom = lastZoom;
|
|
168
|
+
lastZoom = zoom;
|
|
169
|
+
if (zoom >= prevZoom) return;
|
|
170
|
+
latestModel.runtime.seedFrom(zoom, clusterModel.runtime.getVisible());
|
|
171
|
+
setRenderedModel(latestModel);
|
|
172
|
+
});
|
|
173
|
+
}, [clusterModel, latestModel, editor]);
|
|
174
|
+
const orphanThreads = useMemo(() => {
|
|
175
|
+
if (clusterModel === latestModel) return [];
|
|
176
|
+
const renderedIds = new Set(clusterModel.table.leaves.map((leaf) => leaf.id));
|
|
177
|
+
const latestIds = new Set(latestModel.table.leaves.map((leaf) => leaf.id));
|
|
178
|
+
return threads.filter((thread) => latestIds.has(thread.id) && !renderedIds.has(thread.id));
|
|
179
|
+
}, [clusterModel, latestModel, threads]);
|
|
180
|
+
const movedThreads = useMemo(
|
|
181
|
+
() => threads.filter((thread) => movedThreadIds.has(thread.id) && thread.id !== openId),
|
|
182
|
+
[threads, movedThreadIds, openId]
|
|
183
|
+
);
|
|
184
|
+
const clusterCursor = useValue(
|
|
185
|
+
"comment cluster cursor",
|
|
186
|
+
() => {
|
|
187
|
+
clusterModel.runtime.onCamera(editor.getZoomLevel());
|
|
188
|
+
return clusterModel.runtime.k;
|
|
189
|
+
},
|
|
190
|
+
[clusterModel, editor]
|
|
191
|
+
);
|
|
192
|
+
const visibleNodes = useMemo(() => {
|
|
193
|
+
const nodes = Array.from(clusterModel.runtime.getVisible().values());
|
|
194
|
+
console.debug(
|
|
195
|
+
`[comments] cluster cursor k=${clusterCursor} \u2192 re-rendering ${nodes.length} visible nodes`
|
|
196
|
+
);
|
|
197
|
+
return nodes;
|
|
198
|
+
}, [clusterModel, clusterCursor]);
|
|
199
|
+
const fadeNodes = useFadeVisibleNodes(visibleNodes, clusterModel);
|
|
200
|
+
const threadsById = useMemo(
|
|
201
|
+
() => new Map(threads.map((thread) => [thread.id, thread])),
|
|
202
|
+
[threads]
|
|
203
|
+
);
|
|
204
|
+
const openThread = openId ? threadsById.get(openId) : null;
|
|
205
|
+
const hidden = useValue("comments hidden", () => commentsHidden.get(editor), [editor]);
|
|
206
|
+
useEffect(() => {
|
|
207
|
+
return () => {
|
|
208
|
+
openThreadId.set(editor, null);
|
|
209
|
+
pendingComment.set(editor, null);
|
|
210
|
+
};
|
|
211
|
+
}, [editor]);
|
|
212
|
+
useEffect(() => {
|
|
213
|
+
if (deepLinkHandled.current) return;
|
|
214
|
+
const id = new URLSearchParams(window.location.search).get("comment");
|
|
215
|
+
if (!id) {
|
|
216
|
+
deepLinkHandled.current = true;
|
|
217
|
+
return;
|
|
218
|
+
}
|
|
219
|
+
const record = getCommentRecord(editor, id);
|
|
220
|
+
if (!record) return;
|
|
221
|
+
let thread;
|
|
222
|
+
if (record.typeName === "comment") {
|
|
223
|
+
thread = threadsById.get(record.threadId);
|
|
224
|
+
} else {
|
|
225
|
+
thread = record;
|
|
226
|
+
}
|
|
227
|
+
if (!thread) return;
|
|
228
|
+
deepLinkHandled.current = true;
|
|
229
|
+
revealDeepLinkedThread(
|
|
230
|
+
editor,
|
|
231
|
+
thread,
|
|
232
|
+
clusterModel.table,
|
|
233
|
+
clusterZoomBounds,
|
|
234
|
+
options,
|
|
235
|
+
impreciseShapeAnchor
|
|
236
|
+
);
|
|
237
|
+
openThreadId.set(editor, thread.id);
|
|
238
|
+
}, [clusterModel.table, clusterZoomBounds, editor, threadsById, impreciseShapeAnchor, options]);
|
|
239
|
+
const zoomToClusterSplit = useCallback(
|
|
240
|
+
(node) => {
|
|
241
|
+
const event = clusterModel.table.events.find((e) => e.result.id === node.id);
|
|
242
|
+
if (!event || !Number.isFinite(event.zSplit)) return;
|
|
243
|
+
const zoom = clamp(
|
|
244
|
+
event.zSplit * options.clusterSplitZoomFactor,
|
|
245
|
+
clusterZoomBounds.minZoom,
|
|
246
|
+
clusterZoomBounds.maxZoom
|
|
247
|
+
);
|
|
248
|
+
centerOnPointAtZoom(editor, node.centroid, zoom, CLUSTER_EXPAND_ZOOM_MS);
|
|
249
|
+
},
|
|
250
|
+
[clusterModel, clusterZoomBounds, editor, options]
|
|
251
|
+
);
|
|
252
|
+
useEffect(() => {
|
|
253
|
+
const onKeyDown = (e) => {
|
|
254
|
+
if (e.key !== "Escape" || openThreadId.get(editor) === null) return;
|
|
255
|
+
if (isMentionPickerOpen()) return;
|
|
256
|
+
const target = e.target;
|
|
257
|
+
if (target && target.closest(".cmt-editing")) return;
|
|
258
|
+
openThreadId.set(editor, null);
|
|
259
|
+
e.preventDefault();
|
|
260
|
+
e.stopPropagation();
|
|
261
|
+
};
|
|
262
|
+
document.addEventListener("keydown", onKeyDown, true);
|
|
263
|
+
return () => document.removeEventListener("keydown", onKeyDown, true);
|
|
264
|
+
}, [editor]);
|
|
265
|
+
useEffect(() => {
|
|
266
|
+
const onKeyDown = (e) => {
|
|
267
|
+
if (e.code !== "KeyC" || !e.shiftKey || e.metaKey || e.ctrlKey || e.altKey) return;
|
|
268
|
+
const target = e.target;
|
|
269
|
+
if (target && target.closest('input, textarea, [contenteditable="true"]')) return;
|
|
270
|
+
toggleCommentsHidden(editor);
|
|
271
|
+
e.preventDefault();
|
|
272
|
+
};
|
|
273
|
+
document.addEventListener("keydown", onKeyDown, true);
|
|
274
|
+
return () => document.removeEventListener("keydown", onKeyDown, true);
|
|
275
|
+
}, [editor]);
|
|
276
|
+
if (hidden) return null;
|
|
277
|
+
return createPortal(
|
|
278
|
+
/* @__PURE__ */ jsxs("div", { ref: layerRef, className: "cmt-canvas-layer", children: [
|
|
279
|
+
options.enableClustering ? /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
280
|
+
fadeNodes.map(({ node, phase }) => {
|
|
281
|
+
let content;
|
|
282
|
+
if (node.count === 1) {
|
|
283
|
+
const thread = threadsById.get(node.id);
|
|
284
|
+
if (!thread) return null;
|
|
285
|
+
content = /* @__PURE__ */ jsx(
|
|
286
|
+
ThreadPin,
|
|
287
|
+
{
|
|
288
|
+
editor,
|
|
289
|
+
thread,
|
|
290
|
+
...props,
|
|
291
|
+
regionOptions
|
|
292
|
+
}
|
|
293
|
+
);
|
|
294
|
+
} else {
|
|
295
|
+
content = /* @__PURE__ */ jsx(ClusterBadge, { editor, node, onExpand: zoomToClusterSplit });
|
|
296
|
+
}
|
|
297
|
+
return /* @__PURE__ */ jsx("div", { className: clusterFadeClassName(phase), children: content }, `cluster-fade:${node.id}`);
|
|
298
|
+
}),
|
|
299
|
+
orphanThreads.map((thread) => /* @__PURE__ */ jsx(
|
|
300
|
+
ThreadPin,
|
|
301
|
+
{
|
|
302
|
+
editor,
|
|
303
|
+
thread,
|
|
304
|
+
...props,
|
|
305
|
+
regionOptions
|
|
306
|
+
},
|
|
307
|
+
thread.id
|
|
308
|
+
)),
|
|
309
|
+
movedThreads.map((thread) => /* @__PURE__ */ jsx(
|
|
310
|
+
ThreadPin,
|
|
311
|
+
{
|
|
312
|
+
editor,
|
|
313
|
+
thread,
|
|
314
|
+
...props,
|
|
315
|
+
regionOptions
|
|
316
|
+
},
|
|
317
|
+
thread.id
|
|
318
|
+
))
|
|
319
|
+
] }) : (
|
|
320
|
+
// Clustering off: every thread renders as its own live pin (each returns null when it's
|
|
321
|
+
// not on the current page or its anchor is missing). The open thread is excluded here and
|
|
322
|
+
// rendered once below, mirroring how the clustering path keeps it out of the cluster leaves —
|
|
323
|
+
// otherwise it would mount a second, stacked pin.
|
|
324
|
+
(threads.filter((thread) => thread.id !== openId).map((thread) => /* @__PURE__ */ jsx(
|
|
325
|
+
ThreadPin,
|
|
326
|
+
{
|
|
327
|
+
editor,
|
|
328
|
+
thread,
|
|
329
|
+
...props,
|
|
330
|
+
regionOptions
|
|
331
|
+
},
|
|
332
|
+
thread.id
|
|
333
|
+
)))
|
|
334
|
+
),
|
|
335
|
+
openThread && /* @__PURE__ */ jsx(
|
|
336
|
+
ThreadPin,
|
|
337
|
+
{
|
|
338
|
+
editor,
|
|
339
|
+
thread: openThread,
|
|
340
|
+
...props,
|
|
341
|
+
regionOptions
|
|
342
|
+
},
|
|
343
|
+
`open:${openThread.id}`
|
|
344
|
+
),
|
|
345
|
+
/* @__PURE__ */ jsx(RegionDraftBox, { editor }),
|
|
346
|
+
pending?.anchor.type === "region" && /* @__PURE__ */ jsx(RegionBox, { editor, box: pending.anchor }),
|
|
347
|
+
pending && props.currentUserId && /* @__PURE__ */ jsx(PendingComposer, { editor, pending, ...props })
|
|
348
|
+
] }),
|
|
349
|
+
container
|
|
350
|
+
);
|
|
351
|
+
}
|
|
352
|
+
const EMPTY_SET = /* @__PURE__ */ new Set();
|
|
353
|
+
const MOVED_LEAF_EPSILON = 1e-6;
|
|
354
|
+
function useFadeVisibleNodes(nodes, resetKey) {
|
|
355
|
+
const resetKeyRef = useRef(resetKey);
|
|
356
|
+
const didReset = resetKeyRef.current !== resetKey;
|
|
357
|
+
if (didReset) {
|
|
358
|
+
resetKeyRef.current = resetKey;
|
|
359
|
+
}
|
|
360
|
+
const [fadeNodes, setFadeNodes] = useState(() => toPresentFadeNodes(nodes));
|
|
361
|
+
const renderedNodes = didReset ? toPresentFadeNodes(nodes) : fadeNodes;
|
|
362
|
+
useEffect(() => {
|
|
363
|
+
setFadeNodes(toPresentFadeNodes(nodes));
|
|
364
|
+
}, [resetKey]);
|
|
365
|
+
useEffect(() => {
|
|
366
|
+
if (didReset) return;
|
|
367
|
+
setFadeNodes((previous) => reconcileFadeNodes(previous, nodes));
|
|
368
|
+
}, [didReset, nodes]);
|
|
369
|
+
const hasEntering = renderedNodes.some((item) => item.phase === "entering");
|
|
370
|
+
useEffect(() => {
|
|
371
|
+
if (!hasEntering) return;
|
|
372
|
+
const frame = requestClusterFadeFrame(() => {
|
|
373
|
+
setFadeNodes(
|
|
374
|
+
(previous) => previous.map((item) => item.phase === "entering" ? { ...item, phase: "present" } : item)
|
|
375
|
+
);
|
|
376
|
+
});
|
|
377
|
+
return () => cancelClusterFadeFrame(frame);
|
|
378
|
+
}, [hasEntering, renderedNodes]);
|
|
379
|
+
const hasExiting = renderedNodes.some((item) => item.phase === "exiting");
|
|
380
|
+
useEffect(() => {
|
|
381
|
+
if (!hasExiting) return;
|
|
382
|
+
const timeout = window.setTimeout(() => {
|
|
383
|
+
setFadeNodes((previous) => previous.filter((item) => item.phase !== "exiting"));
|
|
384
|
+
}, CLUSTER_FADE_MS);
|
|
385
|
+
return () => window.clearTimeout(timeout);
|
|
386
|
+
}, [hasExiting, renderedNodes]);
|
|
387
|
+
return renderedNodes;
|
|
388
|
+
}
|
|
389
|
+
function toPresentFadeNodes(nodes) {
|
|
390
|
+
return nodes.map((node) => ({ node, phase: "present" }));
|
|
391
|
+
}
|
|
392
|
+
function reconcileFadeNodes(previous, nextNodes) {
|
|
393
|
+
const previousById = new Map(previous.map((item) => [item.node.id, item]));
|
|
394
|
+
const nextIds = new Set(nextNodes.map((node) => node.id));
|
|
395
|
+
const next = [];
|
|
396
|
+
for (const node of nextNodes) {
|
|
397
|
+
const previousItem = previousById.get(node.id);
|
|
398
|
+
next.push({
|
|
399
|
+
node,
|
|
400
|
+
phase: previousItem && previousItem.phase !== "exiting" ? previousItem.phase : previousItem ? "present" : "entering"
|
|
401
|
+
});
|
|
402
|
+
}
|
|
403
|
+
for (const item of previous) {
|
|
404
|
+
if (nextIds.has(item.node.id)) continue;
|
|
405
|
+
next.push(item.phase === "exiting" ? item : { ...item, phase: "exiting" });
|
|
406
|
+
}
|
|
407
|
+
return next;
|
|
408
|
+
}
|
|
409
|
+
function requestClusterFadeFrame(callback) {
|
|
410
|
+
if (typeof requestAnimationFrame === "function") return requestAnimationFrame(callback);
|
|
411
|
+
return window.setTimeout(() => callback(0), 16);
|
|
412
|
+
}
|
|
413
|
+
function cancelClusterFadeFrame(frame) {
|
|
414
|
+
if (typeof cancelAnimationFrame === "function") cancelAnimationFrame(frame);
|
|
415
|
+
else window.clearTimeout(frame);
|
|
416
|
+
}
|
|
417
|
+
function clusterFadeClassName(phase) {
|
|
418
|
+
return `cmt-cluster-fade cmt-cluster-fade--${phase}`;
|
|
419
|
+
}
|
|
420
|
+
function findMovedClusteredLeafIds(rendered, latest) {
|
|
421
|
+
if (rendered.table === latest.table) return [];
|
|
422
|
+
const visible = rendered.runtime.getVisible();
|
|
423
|
+
const latestById = new Map(latest.table.leaves.map((leaf) => [leaf.id, leaf]));
|
|
424
|
+
const moved = [];
|
|
425
|
+
for (const leaf of rendered.table.leaves) {
|
|
426
|
+
if (visible.has(leaf.id)) continue;
|
|
427
|
+
const current = latestById.get(leaf.id);
|
|
428
|
+
if (!current) continue;
|
|
429
|
+
if (Math.abs(current.centroid.x - leaf.centroid.x) > MOVED_LEAF_EPSILON || Math.abs(current.centroid.y - leaf.centroid.y) > MOVED_LEAF_EPSILON) {
|
|
430
|
+
moved.push(leaf.id);
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
return moved;
|
|
434
|
+
}
|
|
435
|
+
function hasRemovedLeaves(rendered, latest) {
|
|
436
|
+
if (rendered.leaves.length === 0) return false;
|
|
437
|
+
const latestIds = new Set(latest.leaves.map((leaf) => leaf.id));
|
|
438
|
+
return rendered.leaves.some((leaf) => !latestIds.has(leaf.id));
|
|
439
|
+
}
|
|
440
|
+
function getClusterZoomBounds(editor) {
|
|
441
|
+
const cameraOptions = editor.getCameraOptions();
|
|
442
|
+
const baseZoom = cameraOptions.constraints ? editor.getBaseZoom() : 1;
|
|
443
|
+
const zoomSteps = cameraOptions.zoomSteps;
|
|
444
|
+
return {
|
|
445
|
+
minZoom: zoomSteps[0] * baseZoom,
|
|
446
|
+
maxZoom: zoomSteps[zoomSteps.length - 1] * baseZoom
|
|
447
|
+
};
|
|
448
|
+
}
|
|
449
|
+
function revealDeepLinkedThread(editor, thread, table, zoomBounds, options, impreciseShapeAnchor) {
|
|
450
|
+
if (thread.pageId !== editor.getCurrentPageId()) {
|
|
451
|
+
editor.setCurrentPage(thread.pageId);
|
|
452
|
+
}
|
|
453
|
+
const point = anchorPagePoint(editor, thread.anchor, impreciseShapeAnchor);
|
|
454
|
+
if (!point) return;
|
|
455
|
+
if (options.enableClustering) {
|
|
456
|
+
const parentEvent = findDirectParentEvent(table, thread.id);
|
|
457
|
+
if (parentEvent && Number.isFinite(parentEvent.zSplit) && parentEvent.zSplit <= zoomBounds.maxZoom) {
|
|
458
|
+
const zoom = clamp(
|
|
459
|
+
parentEvent.zSplit * options.clusterSplitZoomFactor,
|
|
460
|
+
zoomBounds.minZoom,
|
|
461
|
+
zoomBounds.maxZoom
|
|
462
|
+
);
|
|
463
|
+
centerOnPointAtZoom(editor, point, zoom);
|
|
464
|
+
return;
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
editor.centerOnPoint(point, { animation: { duration: 200 } });
|
|
468
|
+
}
|
|
469
|
+
function findDirectParentEvent(table, threadId) {
|
|
470
|
+
return table.events.find((event) => event.children.some((child) => child.id === threadId));
|
|
471
|
+
}
|
|
472
|
+
function centerOnPointAtZoom(editor, point, zoom, duration = 200) {
|
|
473
|
+
const viewport = editor.getViewportScreenBounds();
|
|
474
|
+
editor.setCamera(
|
|
475
|
+
{
|
|
476
|
+
x: viewport.w / (2 * zoom) - point.x,
|
|
477
|
+
y: viewport.h / (2 * zoom) - point.y,
|
|
478
|
+
z: zoom
|
|
479
|
+
},
|
|
480
|
+
{ animation: { duration } }
|
|
481
|
+
);
|
|
482
|
+
}
|
|
483
|
+
function clamp(value, min, max) {
|
|
484
|
+
return Math.max(min, Math.min(max, value));
|
|
485
|
+
}
|
|
486
|
+
const ClusterBadge = memo(function ClusterBadge2({
|
|
487
|
+
editor,
|
|
488
|
+
node,
|
|
489
|
+
onExpand
|
|
490
|
+
}) {
|
|
491
|
+
const point = useValue(
|
|
492
|
+
"cluster badge point",
|
|
493
|
+
() => {
|
|
494
|
+
const pagePoint = editor.pageToViewport(node.centroid);
|
|
495
|
+
if (!isInInflatedViewport(editor, pagePoint)) return null;
|
|
496
|
+
return pagePoint;
|
|
497
|
+
},
|
|
498
|
+
[editor, node]
|
|
499
|
+
);
|
|
500
|
+
if (!point) return null;
|
|
501
|
+
return /* @__PURE__ */ jsx(
|
|
502
|
+
"div",
|
|
503
|
+
{
|
|
504
|
+
className: "cmt-canvas-cluster",
|
|
505
|
+
style: { left: point.x, top: point.y },
|
|
506
|
+
onPointerDown: stop,
|
|
507
|
+
onClick: (e) => {
|
|
508
|
+
e.stopPropagation();
|
|
509
|
+
onExpand(node);
|
|
510
|
+
},
|
|
511
|
+
children: /* @__PURE__ */ jsx(CountBadge, { count: node.count })
|
|
512
|
+
}
|
|
513
|
+
);
|
|
514
|
+
});
|
|
515
|
+
function isInInflatedViewport(editor, point) {
|
|
516
|
+
const viewport = editor.getViewportScreenBounds();
|
|
517
|
+
const margin = getCommentingOptions(editor).clusterCullMargin;
|
|
518
|
+
return point.x >= -margin && point.y >= -margin && point.x <= viewport.w + margin && point.y <= viewport.h + margin;
|
|
519
|
+
}
|
|
520
|
+
function ThreadPopover({
|
|
521
|
+
container,
|
|
522
|
+
style,
|
|
523
|
+
children
|
|
524
|
+
}) {
|
|
525
|
+
const ref = useRef(null);
|
|
526
|
+
usePassThroughWheelEvents(ref);
|
|
527
|
+
usePassThroughMouseOverEvents(ref);
|
|
528
|
+
return createPortal(
|
|
529
|
+
/* @__PURE__ */ jsx("div", { ref, className: "cmt-canvas-popover", style, onPointerDown: stop, children }),
|
|
530
|
+
container
|
|
531
|
+
);
|
|
532
|
+
}
|
|
533
|
+
function RegionBox({
|
|
534
|
+
editor,
|
|
535
|
+
box,
|
|
536
|
+
movable,
|
|
537
|
+
onPreview,
|
|
538
|
+
onCommit
|
|
539
|
+
}) {
|
|
540
|
+
const rect = useValue(
|
|
541
|
+
"region rect",
|
|
542
|
+
() => {
|
|
543
|
+
const topLeft = editor.pageToViewport({ x: box.x, y: box.y });
|
|
544
|
+
const zoom = editor.getZoomLevel();
|
|
545
|
+
return { left: topLeft.x, top: topLeft.y, width: box.w * zoom, height: box.h * zoom };
|
|
546
|
+
},
|
|
547
|
+
[editor, box.x, box.y, box.w, box.h]
|
|
548
|
+
);
|
|
549
|
+
const grabRef = useRef(null);
|
|
550
|
+
const translated = (e) => {
|
|
551
|
+
const g = grabRef.current;
|
|
552
|
+
const p = editor.screenToPage({ x: e.clientX, y: e.clientY });
|
|
553
|
+
return { ...g.box, x: g.box.x + (p.x - g.page.x), y: g.box.y + (p.y - g.page.y) };
|
|
554
|
+
};
|
|
555
|
+
const startMove = (e) => {
|
|
556
|
+
e.stopPropagation();
|
|
557
|
+
grabRef.current = { page: editor.screenToPage({ x: e.clientX, y: e.clientY }), box };
|
|
558
|
+
e.currentTarget.setPointerCapture(e.pointerId);
|
|
559
|
+
};
|
|
560
|
+
const onMove = (e) => {
|
|
561
|
+
if (grabRef.current) onPreview?.(translated(e));
|
|
562
|
+
};
|
|
563
|
+
const endMove = (e) => {
|
|
564
|
+
if (!grabRef.current) return;
|
|
565
|
+
const bounds = translated(e);
|
|
566
|
+
grabRef.current = null;
|
|
567
|
+
if (e.currentTarget.hasPointerCapture(e.pointerId))
|
|
568
|
+
e.currentTarget.releasePointerCapture(e.pointerId);
|
|
569
|
+
onCommit?.(bounds);
|
|
570
|
+
};
|
|
571
|
+
return /* @__PURE__ */ jsx(
|
|
572
|
+
"div",
|
|
573
|
+
{
|
|
574
|
+
className: movable ? "cmt-canvas-region cmt-canvas-region--movable" : "cmt-canvas-region",
|
|
575
|
+
style: rect,
|
|
576
|
+
onPointerDown: movable ? startMove : void 0,
|
|
577
|
+
onPointerMove: movable ? onMove : void 0,
|
|
578
|
+
onPointerUp: movable ? endMove : void 0
|
|
579
|
+
}
|
|
580
|
+
);
|
|
581
|
+
}
|
|
582
|
+
function RegionDraftBox({ editor }) {
|
|
583
|
+
const box = useValue("region draft", () => regionDraft.get(editor), [editor]);
|
|
584
|
+
if (!box) return null;
|
|
585
|
+
return /* @__PURE__ */ jsx(RegionBox, { editor, box });
|
|
586
|
+
}
|
|
587
|
+
const REGION_CORNERS = [
|
|
588
|
+
{ x: 0, y: 0, cursor: "nwse-resize" },
|
|
589
|
+
{ x: 1, y: 0, cursor: "nesw-resize" },
|
|
590
|
+
{ x: 0, y: 1, cursor: "nesw-resize" },
|
|
591
|
+
{ x: 1, y: 1, cursor: "nwse-resize" }
|
|
592
|
+
];
|
|
593
|
+
const REGION_EDGES = [
|
|
594
|
+
{ x: 0.5, y: 0, cursor: "ns-resize" },
|
|
595
|
+
{ x: 1, y: 0.5, cursor: "ew-resize" },
|
|
596
|
+
{ x: 0.5, y: 1, cursor: "ns-resize" },
|
|
597
|
+
{ x: 0, y: 0.5, cursor: "ew-resize" }
|
|
598
|
+
];
|
|
599
|
+
const REGION_HANDLE_MARGIN_PX = 12;
|
|
600
|
+
function resizeRegion(box, handle, cursor) {
|
|
601
|
+
const controlsX = handle.x !== 0.5;
|
|
602
|
+
const controlsY = handle.y !== 0.5;
|
|
603
|
+
const fixedX = box.x + (1 - handle.x) * box.w;
|
|
604
|
+
const fixedY = box.y + (1 - handle.y) * box.h;
|
|
605
|
+
return {
|
|
606
|
+
x: controlsX ? Math.min(fixedX, cursor.x) : box.x,
|
|
607
|
+
y: controlsY ? Math.min(fixedY, cursor.y) : box.y,
|
|
608
|
+
w: controlsX ? Math.abs(cursor.x - fixedX) : box.w,
|
|
609
|
+
h: controlsY ? Math.abs(cursor.y - fixedY) : box.h
|
|
610
|
+
};
|
|
611
|
+
}
|
|
612
|
+
function RegionResizeHandles({
|
|
613
|
+
editor,
|
|
614
|
+
box,
|
|
615
|
+
handles,
|
|
616
|
+
onPreview,
|
|
617
|
+
onCommit
|
|
618
|
+
}) {
|
|
619
|
+
const boxRef = useRef(null);
|
|
620
|
+
const points = useValue(
|
|
621
|
+
"region handle points",
|
|
622
|
+
() => handles.map((h) => {
|
|
623
|
+
const p = editor.pageToViewport({ x: box.x + h.x * box.w, y: box.y + h.y * box.h });
|
|
624
|
+
return { ...h, key: `${h.x}-${h.y}`, left: p.x, top: p.y };
|
|
625
|
+
}),
|
|
626
|
+
[editor, box.x, box.y, box.w, box.h, handles]
|
|
627
|
+
);
|
|
628
|
+
const startResize = (e) => {
|
|
629
|
+
e.stopPropagation();
|
|
630
|
+
boxRef.current = box;
|
|
631
|
+
e.currentTarget.setPointerCapture(e.pointerId);
|
|
632
|
+
};
|
|
633
|
+
const resizedTo = (h, e) => resizeRegion(boxRef.current, h, editor.screenToPage({ x: e.clientX, y: e.clientY }));
|
|
634
|
+
const onResize = (h) => (e) => {
|
|
635
|
+
if (boxRef.current) onPreview(resizedTo(h, e));
|
|
636
|
+
};
|
|
637
|
+
const endResize = (h) => (e) => {
|
|
638
|
+
if (!boxRef.current) return;
|
|
639
|
+
const bounds = resizedTo(h, e);
|
|
640
|
+
boxRef.current = null;
|
|
641
|
+
if (e.currentTarget.hasPointerCapture(e.pointerId))
|
|
642
|
+
e.currentTarget.releasePointerCapture(e.pointerId);
|
|
643
|
+
onCommit(bounds);
|
|
644
|
+
};
|
|
645
|
+
return /* @__PURE__ */ jsx(Fragment, { children: points.map((h) => /* @__PURE__ */ jsx(
|
|
646
|
+
"div",
|
|
647
|
+
{
|
|
648
|
+
className: "cmt-canvas-region-handle",
|
|
649
|
+
style: { left: h.left, top: h.top, cursor: h.cursor },
|
|
650
|
+
onPointerDown: startResize,
|
|
651
|
+
onPointerMove: onResize(h),
|
|
652
|
+
onPointerUp: endResize(h)
|
|
653
|
+
},
|
|
654
|
+
h.key
|
|
655
|
+
)) });
|
|
656
|
+
}
|
|
657
|
+
const ThreadPin = memo(function ThreadPin2({
|
|
658
|
+
editor,
|
|
659
|
+
thread,
|
|
660
|
+
regionOptions,
|
|
661
|
+
...props
|
|
662
|
+
}) {
|
|
663
|
+
const {
|
|
664
|
+
currentUserId,
|
|
665
|
+
resolveName,
|
|
666
|
+
onPostComment,
|
|
667
|
+
isCommentUnread,
|
|
668
|
+
onCommentRead,
|
|
669
|
+
getMentionSuggestions,
|
|
670
|
+
renderMentionSuggestion
|
|
671
|
+
} = props;
|
|
672
|
+
const options = useCommentingOptions();
|
|
673
|
+
const impreciseShapeAnchor = props.impreciseShapeAnchor ?? options.impreciseShapeAnchor;
|
|
674
|
+
const container = useContainer();
|
|
675
|
+
const comments = useThreadComments(editor, thread.id);
|
|
676
|
+
const msg = useTranslation();
|
|
677
|
+
const open = useValue("thread open", () => openThreadId.get(editor) === thread.id, [
|
|
678
|
+
editor,
|
|
679
|
+
thread.id
|
|
680
|
+
]);
|
|
681
|
+
const [reply, setReply] = useState(EMPTY_COMMENT);
|
|
682
|
+
const [editingId, setEditingId] = useState(null);
|
|
683
|
+
const [editText, setEditText] = useState(EMPTY_COMMENT);
|
|
684
|
+
const [dragPagePoint, setDragPagePoint] = useState(null);
|
|
685
|
+
const [resizeBounds, setResizeBounds] = useState(null);
|
|
686
|
+
const [pinHovered, setPinHovered] = useState(false);
|
|
687
|
+
const pointerInRegion = useValue(
|
|
688
|
+
"pointer in region",
|
|
689
|
+
() => {
|
|
690
|
+
if (thread.anchor.type !== "region" || thread.pageId !== editor.getCurrentPageId())
|
|
691
|
+
return false;
|
|
692
|
+
const m = REGION_HANDLE_MARGIN_PX / editor.getZoomLevel();
|
|
693
|
+
const p = editor.inputs.getCurrentPagePoint();
|
|
694
|
+
const a = thread.anchor;
|
|
695
|
+
return p.x >= a.x - m && p.x <= a.x + a.w + m && p.y >= a.y - m && p.y <= a.y + a.h + m;
|
|
696
|
+
},
|
|
697
|
+
[editor, thread.anchor, thread.pageId]
|
|
698
|
+
);
|
|
699
|
+
const revealed = open || resizeBounds != null || regionOptions.reveal === "pointer" && pointerInRegion || regionOptions.reveal === "pin-hover" && pinHovered;
|
|
700
|
+
const resizeHandles = useMemo(
|
|
701
|
+
() => regionOptions.resize === "edges" ? REGION_EDGES : REGION_CORNERS.filter(
|
|
702
|
+
(c) => c.x !== regionOptions.pinCorner.x || c.y !== regionOptions.pinCorner.y
|
|
703
|
+
),
|
|
704
|
+
[regionOptions.resize, regionOptions.pinCorner]
|
|
705
|
+
);
|
|
706
|
+
const dragRef = useRef(null);
|
|
707
|
+
const markerRef = useRef(null);
|
|
708
|
+
useEffect(() => {
|
|
709
|
+
if (!open) return;
|
|
710
|
+
const onPointerDown = (e) => {
|
|
711
|
+
const target = e.target;
|
|
712
|
+
if (!target) return;
|
|
713
|
+
if (target.closest(".cmt-canvas-popover")) return;
|
|
714
|
+
const marker = markerRef.current;
|
|
715
|
+
if (marker && marker.contains(target)) return;
|
|
716
|
+
if (target.closest(".cmt-canvas-region-handle, .cmt-canvas-region--movable")) return;
|
|
717
|
+
if (target.closest(".tlui-menu, [data-radix-popper-content-wrapper], .cmt-mention-popup"))
|
|
718
|
+
return;
|
|
719
|
+
openThreadId.set(editor, null);
|
|
720
|
+
};
|
|
721
|
+
document.addEventListener("pointerdown", onPointerDown, true);
|
|
722
|
+
return () => document.removeEventListener("pointerdown", onPointerDown, true);
|
|
723
|
+
}, [open, editor]);
|
|
724
|
+
const point = useValue(
|
|
725
|
+
"pin point",
|
|
726
|
+
() => {
|
|
727
|
+
if (thread.pageId !== editor.getCurrentPageId()) return null;
|
|
728
|
+
const pagePoint = anchorPagePoint(editor, thread.anchor, impreciseShapeAnchor);
|
|
729
|
+
return pagePoint ? editor.pageToViewport(pagePoint) : null;
|
|
730
|
+
},
|
|
731
|
+
[editor, thread.anchor, thread.pageId, impreciseShapeAnchor]
|
|
732
|
+
);
|
|
733
|
+
const visible = point !== null;
|
|
734
|
+
useEffect(() => {
|
|
735
|
+
if (!open || !visible || !isCommentUnread || !onCommentRead) return;
|
|
736
|
+
for (const comment of comments) {
|
|
737
|
+
if (isCommentUnread(comment.id)) {
|
|
738
|
+
onCommentRead(comment.id);
|
|
739
|
+
}
|
|
740
|
+
}
|
|
741
|
+
}, [open, visible, comments, isCommentUnread, onCommentRead]);
|
|
742
|
+
if (!point) return null;
|
|
743
|
+
const postReply = () => {
|
|
744
|
+
if (isCommentEmpty(reply) || !currentUserId) return;
|
|
745
|
+
commitCommentMutation(editor, () => {
|
|
746
|
+
const comment = createComment({
|
|
747
|
+
threadId: thread.id,
|
|
748
|
+
pageId: thread.pageId,
|
|
749
|
+
authorId: currentUserId,
|
|
750
|
+
body: reply
|
|
751
|
+
});
|
|
752
|
+
putCommentRecords(editor, [comment]);
|
|
753
|
+
if (onPostComment) onPostComment(comment);
|
|
754
|
+
});
|
|
755
|
+
setReply(EMPTY_COMMENT);
|
|
756
|
+
};
|
|
757
|
+
const toggleResolve = () => {
|
|
758
|
+
if (!currentUserId) return;
|
|
759
|
+
commitCommentMutation(editor, () => {
|
|
760
|
+
putCommentRecords(editor, [
|
|
761
|
+
{
|
|
762
|
+
...thread,
|
|
763
|
+
resolved: thread.resolved ? null : { at: Date.now(), by: currentUserId }
|
|
764
|
+
}
|
|
765
|
+
]);
|
|
766
|
+
});
|
|
767
|
+
};
|
|
768
|
+
const deleteThread = () => {
|
|
769
|
+
openThreadId.set(editor, null);
|
|
770
|
+
commitCommentMutation(
|
|
771
|
+
editor,
|
|
772
|
+
() => removeCommentRecords(editor, [thread.id, ...comments.map((c) => c.id)])
|
|
773
|
+
);
|
|
774
|
+
};
|
|
775
|
+
const startEdit = (comment) => {
|
|
776
|
+
setEditingId(comment.id);
|
|
777
|
+
setEditText(comment.body);
|
|
778
|
+
};
|
|
779
|
+
const saveEdit = () => {
|
|
780
|
+
const comment = comments.find((c) => c.id === editingId);
|
|
781
|
+
if (!comment || isCommentEmpty(editText)) return;
|
|
782
|
+
commitCommentMutation(editor, () => {
|
|
783
|
+
putCommentRecords(editor, [{ ...comment, body: editText, editedAt: Date.now() }]);
|
|
784
|
+
});
|
|
785
|
+
setEditingId(null);
|
|
786
|
+
};
|
|
787
|
+
const renderComment = (card, index) => {
|
|
788
|
+
const comment = comments[index];
|
|
789
|
+
if (editingId === comment.id) {
|
|
790
|
+
return /* @__PURE__ */ jsx(
|
|
791
|
+
"div",
|
|
792
|
+
{
|
|
793
|
+
className: "cmt-editing",
|
|
794
|
+
onKeyDown: (e) => {
|
|
795
|
+
if (e.key === "Escape") {
|
|
796
|
+
setEditingId(null);
|
|
797
|
+
e.stopPropagation();
|
|
798
|
+
}
|
|
799
|
+
},
|
|
800
|
+
children: /* @__PURE__ */ jsx(
|
|
801
|
+
CommentComposer,
|
|
802
|
+
{
|
|
803
|
+
author: card.author,
|
|
804
|
+
placeholder: msg("comments.edit-placeholder"),
|
|
805
|
+
value: editText,
|
|
806
|
+
onChange: setEditText,
|
|
807
|
+
onSubmit: saveEdit,
|
|
808
|
+
sendLabel: msg("comments.save"),
|
|
809
|
+
disabled: isCommentEmpty(editText),
|
|
810
|
+
getMentionSuggestions,
|
|
811
|
+
renderMentionSuggestion,
|
|
812
|
+
autoFocus: true
|
|
813
|
+
}
|
|
814
|
+
)
|
|
815
|
+
}
|
|
816
|
+
);
|
|
817
|
+
}
|
|
818
|
+
return /* @__PURE__ */ jsx(
|
|
819
|
+
CommentCard,
|
|
820
|
+
{
|
|
821
|
+
...card,
|
|
822
|
+
actions: comment.authorId === currentUserId ? /* @__PURE__ */ jsx(
|
|
823
|
+
"button",
|
|
824
|
+
{
|
|
825
|
+
className: "cmt-thread__action",
|
|
826
|
+
title: msg("comments.edit"),
|
|
827
|
+
onClick: () => startEdit(comment),
|
|
828
|
+
children: /* @__PURE__ */ jsx(TldrawUiIcon, { icon: "dots-horizontal", label: msg("comments.edit"), small: true })
|
|
829
|
+
}
|
|
830
|
+
) : void 0
|
|
831
|
+
}
|
|
832
|
+
);
|
|
833
|
+
};
|
|
834
|
+
const headerActions = /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
835
|
+
currentUserId && /* @__PURE__ */ jsx(
|
|
836
|
+
"button",
|
|
837
|
+
{
|
|
838
|
+
className: "cmt-thread__action",
|
|
839
|
+
title: msg(thread.resolved ? "comments.reopen" : "comments.resolve"),
|
|
840
|
+
onClick: toggleResolve,
|
|
841
|
+
children: /* @__PURE__ */ jsx(
|
|
842
|
+
TldrawUiIcon,
|
|
843
|
+
{
|
|
844
|
+
icon: "check",
|
|
845
|
+
label: msg(thread.resolved ? "comments.reopen" : "comments.resolve"),
|
|
846
|
+
small: true
|
|
847
|
+
}
|
|
848
|
+
)
|
|
849
|
+
}
|
|
850
|
+
),
|
|
851
|
+
currentUserId && /* @__PURE__ */ jsx(
|
|
852
|
+
"button",
|
|
853
|
+
{
|
|
854
|
+
className: "cmt-thread__action",
|
|
855
|
+
title: msg("comments.delete"),
|
|
856
|
+
onClick: deleteThread,
|
|
857
|
+
children: /* @__PURE__ */ jsx(TldrawUiIcon, { icon: "trash", label: msg("comments.delete"), small: true })
|
|
858
|
+
}
|
|
859
|
+
),
|
|
860
|
+
/* @__PURE__ */ jsx(
|
|
861
|
+
"button",
|
|
862
|
+
{
|
|
863
|
+
className: "cmt-thread__action",
|
|
864
|
+
title: msg("comments.dismiss"),
|
|
865
|
+
onClick: () => openThreadId.set(editor, null),
|
|
866
|
+
children: /* @__PURE__ */ jsx(TldrawUiIcon, { icon: "cross-2", label: msg("comments.dismiss"), small: true })
|
|
867
|
+
}
|
|
868
|
+
)
|
|
869
|
+
] });
|
|
870
|
+
const PinContent = options.components.PinContent;
|
|
871
|
+
const pinContent = PinContent ? /* @__PURE__ */ jsx(PinContent, { thread, comments }) : initialOf(resolveName(thread.createdBy) ?? UNKNOWN_AUTHOR);
|
|
872
|
+
const isRegion = thread.anchor.type === "region";
|
|
873
|
+
const pinMovable = regionOptions.move !== "body";
|
|
874
|
+
const bodyMovable = regionOptions.move !== "pin";
|
|
875
|
+
const startDrag = (e) => {
|
|
876
|
+
e.stopPropagation();
|
|
877
|
+
dragRef.current = { startX: e.clientX, startY: e.clientY, moved: false };
|
|
878
|
+
e.currentTarget.setPointerCapture(e.pointerId);
|
|
879
|
+
};
|
|
880
|
+
const onDrag = (e) => {
|
|
881
|
+
const drag = dragRef.current;
|
|
882
|
+
if (!drag) return;
|
|
883
|
+
if (isRegion && !pinMovable) return;
|
|
884
|
+
if (!drag.moved && Math.hypot(e.clientX - drag.startX, e.clientY - drag.startY) < 4) return;
|
|
885
|
+
drag.moved = true;
|
|
886
|
+
setDragPagePoint(editor.screenToPage({ x: e.clientX, y: e.clientY }));
|
|
887
|
+
};
|
|
888
|
+
const endDrag = (e) => {
|
|
889
|
+
const drag = dragRef.current;
|
|
890
|
+
dragRef.current = null;
|
|
891
|
+
if (e.currentTarget.hasPointerCapture(e.pointerId)) {
|
|
892
|
+
e.currentTarget.releasePointerCapture(e.pointerId);
|
|
893
|
+
}
|
|
894
|
+
if (!drag) return;
|
|
895
|
+
if (!drag.moved) {
|
|
896
|
+
openThreadId.set(editor, openThreadId.get(editor) === thread.id ? null : thread.id);
|
|
897
|
+
return;
|
|
898
|
+
}
|
|
899
|
+
const pagePoint = editor.screenToPage({ x: e.clientX, y: e.clientY });
|
|
900
|
+
setDragPagePoint(null);
|
|
901
|
+
let anchor;
|
|
902
|
+
if (thread.anchor.type === "region") {
|
|
903
|
+
anchor = {
|
|
904
|
+
...thread.anchor,
|
|
905
|
+
x: pagePoint.x - regionOptions.pinCorner.x * thread.anchor.w,
|
|
906
|
+
y: pagePoint.y - regionOptions.pinCorner.y * thread.anchor.h
|
|
907
|
+
};
|
|
908
|
+
} else {
|
|
909
|
+
const hit = editor.getShapeAtPoint(pagePoint, { hitInside: true });
|
|
910
|
+
anchor = hit ? shapeAnchorAt(editor, hit.id, pagePoint, e.altKey) : { type: "point", x: pagePoint.x, y: pagePoint.y };
|
|
911
|
+
}
|
|
912
|
+
commitCommentMutation(editor, () => putCommentRecords(editor, [{ ...thread, anchor }]), "drag");
|
|
913
|
+
};
|
|
914
|
+
const livePinPage = resizeBounds ? regionPinPoint(resizeBounds, regionOptions.pinCorner) : dragPagePoint;
|
|
915
|
+
const renderPoint = livePinPage ? editor.pageToViewport(livePinPage) : point;
|
|
916
|
+
const regionAnchor = thread.anchor.type === "region" ? thread.anchor : void 0;
|
|
917
|
+
const movedRegion = regionAnchor && dragPagePoint ? {
|
|
918
|
+
...regionAnchor,
|
|
919
|
+
x: dragPagePoint.x - regionOptions.pinCorner.x * regionAnchor.w,
|
|
920
|
+
y: dragPagePoint.y - regionOptions.pinCorner.y * regionAnchor.h
|
|
921
|
+
} : regionAnchor;
|
|
922
|
+
const regionBoxBounds = resizeBounds ?? movedRegion;
|
|
923
|
+
const commitResize = (bounds) => {
|
|
924
|
+
setResizeBounds(null);
|
|
925
|
+
editor.run(
|
|
926
|
+
() => putCommentRecords(editor, [{ ...thread, anchor: { type: "region", ...bounds } }]),
|
|
927
|
+
{
|
|
928
|
+
history: "ignore"
|
|
929
|
+
}
|
|
930
|
+
);
|
|
931
|
+
};
|
|
932
|
+
return /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
933
|
+
regionBoxBounds && (dragPagePoint || revealed) && /* @__PURE__ */ jsx(
|
|
934
|
+
RegionBox,
|
|
935
|
+
{
|
|
936
|
+
editor,
|
|
937
|
+
box: regionBoxBounds,
|
|
938
|
+
movable: bodyMovable && !dragPagePoint,
|
|
939
|
+
onPreview: setResizeBounds,
|
|
940
|
+
onCommit: commitResize
|
|
941
|
+
}
|
|
942
|
+
),
|
|
943
|
+
regionBoxBounds && revealed && !dragPagePoint && regionOptions.resize !== "none" && /* @__PURE__ */ jsx(
|
|
944
|
+
RegionResizeHandles,
|
|
945
|
+
{
|
|
946
|
+
editor,
|
|
947
|
+
box: regionBoxBounds,
|
|
948
|
+
handles: resizeHandles,
|
|
949
|
+
onPreview: setResizeBounds,
|
|
950
|
+
onCommit: commitResize
|
|
951
|
+
}
|
|
952
|
+
),
|
|
953
|
+
/* @__PURE__ */ jsxs(
|
|
954
|
+
"div",
|
|
955
|
+
{
|
|
956
|
+
className: open ? "cmt-canvas-pin cmt-canvas-pin--open" : "cmt-canvas-pin",
|
|
957
|
+
style: { left: renderPoint.x, top: renderPoint.y },
|
|
958
|
+
children: [
|
|
959
|
+
/* @__PURE__ */ jsx(
|
|
960
|
+
"div",
|
|
961
|
+
{
|
|
962
|
+
ref: markerRef,
|
|
963
|
+
className: "cmt-canvas-pin__marker",
|
|
964
|
+
onPointerDown: startDrag,
|
|
965
|
+
onPointerMove: onDrag,
|
|
966
|
+
onPointerUp: endDrag,
|
|
967
|
+
onPointerEnter: () => setPinHovered(true),
|
|
968
|
+
onPointerLeave: () => setPinHovered(false),
|
|
969
|
+
children: /* @__PURE__ */ jsx(CommentPin, { resolved: thread.resolved != null, open, children: pinContent })
|
|
970
|
+
}
|
|
971
|
+
),
|
|
972
|
+
open && /* @__PURE__ */ jsx(
|
|
973
|
+
ThreadPopover,
|
|
974
|
+
{
|
|
975
|
+
container,
|
|
976
|
+
style: { left: renderPoint.x + 36, top: renderPoint.y - 28 },
|
|
977
|
+
children: /* @__PURE__ */ jsx(
|
|
978
|
+
CommentThread,
|
|
979
|
+
{
|
|
980
|
+
header: msg("comments.thread-title"),
|
|
981
|
+
headerActions,
|
|
982
|
+
renderComment,
|
|
983
|
+
comments: comments.map((c) => toCardProps(c, props, options.components)),
|
|
984
|
+
resolvedBanner: thread.resolved ? msg("comments.resolved-by").replace(
|
|
985
|
+
"{name}",
|
|
986
|
+
resolveName(thread.resolved.by) ?? UNKNOWN_AUTHOR
|
|
987
|
+
) : void 0,
|
|
988
|
+
composer: currentUserId && !thread.resolved ? {
|
|
989
|
+
author: resolveName(currentUserId) ?? UNKNOWN_AUTHOR,
|
|
990
|
+
placeholder: msg("comments.reply-placeholder"),
|
|
991
|
+
sendLabel: msg("comments.send"),
|
|
992
|
+
value: reply,
|
|
993
|
+
onChange: setReply,
|
|
994
|
+
onSubmit: postReply,
|
|
995
|
+
disabled: isCommentEmpty(reply),
|
|
996
|
+
getMentionSuggestions,
|
|
997
|
+
renderMentionSuggestion
|
|
998
|
+
} : void 0
|
|
999
|
+
}
|
|
1000
|
+
)
|
|
1001
|
+
}
|
|
1002
|
+
)
|
|
1003
|
+
]
|
|
1004
|
+
}
|
|
1005
|
+
)
|
|
1006
|
+
] });
|
|
1007
|
+
});
|
|
1008
|
+
function PendingComposer({
|
|
1009
|
+
editor,
|
|
1010
|
+
pending,
|
|
1011
|
+
currentUserId,
|
|
1012
|
+
resolveName,
|
|
1013
|
+
onPostComment,
|
|
1014
|
+
getMentionSuggestions,
|
|
1015
|
+
renderMentionSuggestion
|
|
1016
|
+
}) {
|
|
1017
|
+
const [text, setText] = useState(EMPTY_COMMENT);
|
|
1018
|
+
const ref = useRef(null);
|
|
1019
|
+
const msg = useTranslation();
|
|
1020
|
+
const container = useContainer();
|
|
1021
|
+
usePassThroughWheelEvents(ref);
|
|
1022
|
+
usePassThroughMouseOverEvents(ref);
|
|
1023
|
+
const point = useValue("composer point", () => editor.pageToViewport(pending.point), [
|
|
1024
|
+
editor,
|
|
1025
|
+
pending.point
|
|
1026
|
+
]);
|
|
1027
|
+
useEffect(() => {
|
|
1028
|
+
const onPointerDown = (e) => {
|
|
1029
|
+
const el = ref.current;
|
|
1030
|
+
const target = e.target;
|
|
1031
|
+
if (!el || !target) return;
|
|
1032
|
+
if (el.contains(target) || target.closest(".cmt-mention-popup")) return;
|
|
1033
|
+
pendingComment.set(editor, null);
|
|
1034
|
+
};
|
|
1035
|
+
document.addEventListener("pointerdown", onPointerDown, true);
|
|
1036
|
+
return () => document.removeEventListener("pointerdown", onPointerDown, true);
|
|
1037
|
+
}, [editor]);
|
|
1038
|
+
const submit = () => {
|
|
1039
|
+
if (isCommentEmpty(text) || !currentUserId) return;
|
|
1040
|
+
commitCommentMutation(editor, () => {
|
|
1041
|
+
const pageId = editor.getCurrentPageId();
|
|
1042
|
+
const thread = createCommentThread({
|
|
1043
|
+
pageId,
|
|
1044
|
+
anchor: pending.anchor,
|
|
1045
|
+
createdBy: currentUserId
|
|
1046
|
+
});
|
|
1047
|
+
const comment = createComment({
|
|
1048
|
+
threadId: thread.id,
|
|
1049
|
+
pageId,
|
|
1050
|
+
authorId: currentUserId,
|
|
1051
|
+
body: text
|
|
1052
|
+
});
|
|
1053
|
+
putCommentRecords(editor, [thread, comment]);
|
|
1054
|
+
if (onPostComment) onPostComment(comment);
|
|
1055
|
+
});
|
|
1056
|
+
setText(EMPTY_COMMENT);
|
|
1057
|
+
pendingComment.set(editor, null);
|
|
1058
|
+
};
|
|
1059
|
+
return createPortal(
|
|
1060
|
+
/* @__PURE__ */ jsx(
|
|
1061
|
+
"div",
|
|
1062
|
+
{
|
|
1063
|
+
ref,
|
|
1064
|
+
className: "cmt-canvas-composer",
|
|
1065
|
+
style: { left: point.x, top: point.y },
|
|
1066
|
+
onPointerDown: stop,
|
|
1067
|
+
onKeyDown: (e) => {
|
|
1068
|
+
if (e.key === "Escape" && !isMentionPickerOpen()) pendingComment.set(editor, null);
|
|
1069
|
+
},
|
|
1070
|
+
children: /* @__PURE__ */ jsx(
|
|
1071
|
+
CommentComposer,
|
|
1072
|
+
{
|
|
1073
|
+
author: currentUserId ? resolveName(currentUserId) ?? UNKNOWN_AUTHOR : "",
|
|
1074
|
+
placeholder: msg("comments.add-placeholder"),
|
|
1075
|
+
sendLabel: msg("comments.send"),
|
|
1076
|
+
value: text,
|
|
1077
|
+
onChange: setText,
|
|
1078
|
+
onSubmit: submit,
|
|
1079
|
+
disabled: isCommentEmpty(text),
|
|
1080
|
+
getMentionSuggestions,
|
|
1081
|
+
renderMentionSuggestion,
|
|
1082
|
+
autoFocus: true,
|
|
1083
|
+
leading: draftAvatar
|
|
1084
|
+
}
|
|
1085
|
+
)
|
|
1086
|
+
}
|
|
1087
|
+
),
|
|
1088
|
+
container
|
|
1089
|
+
);
|
|
1090
|
+
}
|
|
1091
|
+
export {
|
|
1092
|
+
CanvasComments
|
|
1093
|
+
};
|
|
1094
|
+
//# sourceMappingURL=comments-overlay.mjs.map
|