@dino-ovum/theme 0.1.0 → 0.2.0
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/package.json +1 -1
- package/src/app/components/ErrorPageContent.jsx +36 -0
- package/src/app/components/LibraryLayout.jsx +2 -0
- package/src/app/components/NotFoundContent.jsx +24 -0
- package/src/app/components/StatusPage.jsx +54 -0
- package/src/app/components/comments/CommentButton.jsx +19 -0
- package/src/app/components/comments/CommentDrawer.jsx +101 -0
- package/src/app/components/comments/CommentPopover.jsx +89 -0
- package/src/app/components/comments/DocumentComments.jsx +174 -0
- package/src/app/components/comments/HighlightLayer.jsx +30 -0
- package/src/app/components/navigation/ColorModeToggle.jsx +3 -34
- package/src/app/components/navigation/CommentsToggle.jsx +49 -0
- package/src/app/components/navigation/LanguageDropdown.jsx +14 -20
- package/src/app/components/navigation/MoreMenu.jsx +162 -0
- package/src/app/components/navigation/RecentUpdatesLink.jsx +3 -1
- package/src/app/hooks/useColorModeSwitch.js +42 -0
- package/src/app/hooks/useHighlightBoxes.js +89 -0
- package/src/app/hooks/useLocaleLinks.js +20 -0
- package/src/app/hooks/useTextSelection.js +56 -0
- package/src/app/logic/comment-boxes.js +87 -0
- package/src/app/logic/comment-list.js +22 -0
- package/src/app/providers/AppProviders.jsx +6 -1
- package/src/app/providers/CommentDrawer.jsx +30 -0
- package/src/app/styles/components/StatusPage.module.scss +55 -0
- package/src/app/styles/components/comments/CommentDrawer.module.scss +189 -0
- package/src/app/styles/components/comments/DocumentComments.module.scss +91 -0
- package/src/app/styles/components/navigation/MoreMenu.module.scss +41 -0
- package/src/app/styles/components/navigation/NavigationControls.module.scss +37 -0
- package/src/app/styles/theme/navigation.scss +10 -0
- package/src/app/styles/tokens/colors.css +10 -0
- package/src/app/styles/tokens/layout.css +1 -0
- package/src/index.js +4 -0
- package/src/theme/DocItem/Content/index.jsx +17 -0
- package/src/theme/ErrorPageContent.jsx +1 -0
- package/src/theme/NavbarItem/ComponentTypes.jsx +4 -0
- package/src/theme/NotFound/Content/index.jsx +1 -0
- package/src/translations/ja.json +54 -0
- package/src/translations/ko.json +54 -0
package/package.json
CHANGED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import Translate from '@docusaurus/Translate';
|
|
2
|
+
import styles from '../styles/components/StatusPage.module.scss';
|
|
3
|
+
import StatusPage from './StatusPage.jsx';
|
|
4
|
+
|
|
5
|
+
export default function ErrorPageContent({error, tryAgain}) {
|
|
6
|
+
return (
|
|
7
|
+
<StatusPage
|
|
8
|
+
title={<Translate id="docnest.error.title">Page Error</Translate>}
|
|
9
|
+
description={
|
|
10
|
+
<p>
|
|
11
|
+
<Translate id="docnest.error.description">
|
|
12
|
+
We couldn't display this page. Try again, or head back home.
|
|
13
|
+
</Translate>
|
|
14
|
+
</p>
|
|
15
|
+
}
|
|
16
|
+
actions={
|
|
17
|
+
<button
|
|
18
|
+
type="button"
|
|
19
|
+
className="button button--primary"
|
|
20
|
+
onClick={tryAgain}
|
|
21
|
+
>
|
|
22
|
+
<Translate id="docnest.error.retry">Try again</Translate>
|
|
23
|
+
</button>
|
|
24
|
+
}
|
|
25
|
+
>
|
|
26
|
+
{error && (
|
|
27
|
+
<details className={styles.details}>
|
|
28
|
+
<summary>
|
|
29
|
+
<Translate id="docnest.error.details">Error details</Translate>
|
|
30
|
+
</summary>
|
|
31
|
+
<pre>{error.message}</pre>
|
|
32
|
+
</details>
|
|
33
|
+
)}
|
|
34
|
+
</StatusPage>
|
|
35
|
+
);
|
|
36
|
+
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import BackToTopButton from '@theme/BackToTopButton';
|
|
2
2
|
import {useSidebarVisibility} from '../providers/SidebarVisibility.jsx';
|
|
3
|
+
import CommentDrawer from './comments/CommentDrawer.jsx';
|
|
3
4
|
import LibraryNavigation from './navigation/LibraryNavigation.jsx';
|
|
4
5
|
import styles from '../styles/components/LibraryLayout.module.scss';
|
|
5
6
|
import {translate} from '@docusaurus/Translate';
|
|
@@ -31,6 +32,7 @@ export default function LibraryLayout({children, lang}) {
|
|
|
31
32
|
>
|
|
32
33
|
<div className={styles.content}>{children}</div>
|
|
33
34
|
</main>
|
|
35
|
+
<CommentDrawer />
|
|
34
36
|
</div>
|
|
35
37
|
);
|
|
36
38
|
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import Translate from '@docusaurus/Translate';
|
|
2
|
+
import StatusPage from './StatusPage.jsx';
|
|
3
|
+
|
|
4
|
+
export default function NotFoundContent({className}) {
|
|
5
|
+
return (
|
|
6
|
+
<StatusPage
|
|
7
|
+
className={className}
|
|
8
|
+
title={<Translate id="theme.NotFound.title">Page Not Found</Translate>}
|
|
9
|
+
description={
|
|
10
|
+
<>
|
|
11
|
+
<p>
|
|
12
|
+
<Translate id="theme.NotFound.p1">We could not find what you were looking for.</Translate>
|
|
13
|
+
</p>
|
|
14
|
+
<p>
|
|
15
|
+
<Translate id="theme.NotFound.p2">
|
|
16
|
+
Please contact the owner of the site that linked you to the original URL and let them
|
|
17
|
+
know their link is broken.
|
|
18
|
+
</Translate>
|
|
19
|
+
</p>
|
|
20
|
+
</>
|
|
21
|
+
}
|
|
22
|
+
/>
|
|
23
|
+
);
|
|
24
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import clsx from 'clsx';
|
|
2
|
+
import Translate from '@docusaurus/Translate';
|
|
3
|
+
import useBaseUrl from '@docusaurus/useBaseUrl';
|
|
4
|
+
import {useThemeConfig} from '@docusaurus/theme-common';
|
|
5
|
+
import Heading from '@theme/Heading';
|
|
6
|
+
import ThemedImage from '@theme/ThemedImage';
|
|
7
|
+
import {SITE_ROUTES} from '../routes.js';
|
|
8
|
+
import styles from '../styles/components/StatusPage.module.scss';
|
|
9
|
+
|
|
10
|
+
// Shared frame for the error and not-found pages: site logo, title, and the home action.
|
|
11
|
+
export default function StatusPage({className, title, description, actions, children}) {
|
|
12
|
+
const {
|
|
13
|
+
navbar: {logo},
|
|
14
|
+
} = useThemeConfig();
|
|
15
|
+
const light = useBaseUrl(logo?.src ?? '');
|
|
16
|
+
const dark = useBaseUrl(logo?.srcDark ?? logo?.src ?? '');
|
|
17
|
+
const home = useBaseUrl(SITE_ROUTES.home);
|
|
18
|
+
|
|
19
|
+
return (
|
|
20
|
+
<main className={clsx('container margin-vert--xl', className)}>
|
|
21
|
+
<div className={styles.content}>
|
|
22
|
+
<div className={styles.heading}>
|
|
23
|
+
{logo?.src && (
|
|
24
|
+
<ThemedImage
|
|
25
|
+
className={styles.logo}
|
|
26
|
+
alt=""
|
|
27
|
+
width={96}
|
|
28
|
+
height={96}
|
|
29
|
+
sources={{light, dark}}
|
|
30
|
+
/>
|
|
31
|
+
)}
|
|
32
|
+
<Heading
|
|
33
|
+
as="h1"
|
|
34
|
+
className={styles.title}
|
|
35
|
+
>
|
|
36
|
+
{title}
|
|
37
|
+
</Heading>
|
|
38
|
+
</div>
|
|
39
|
+
{description}
|
|
40
|
+
<div className={styles.actions}>
|
|
41
|
+
{actions}
|
|
42
|
+
{/* A full page load leaves a broken client state behind. */}
|
|
43
|
+
<a
|
|
44
|
+
className="button button--secondary button--outline"
|
|
45
|
+
href={home}
|
|
46
|
+
>
|
|
47
|
+
<Translate id="docnest.error.home">Back home</Translate>
|
|
48
|
+
</a>
|
|
49
|
+
</div>
|
|
50
|
+
{children}
|
|
51
|
+
</div>
|
|
52
|
+
</main>
|
|
53
|
+
);
|
|
54
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import clsx from 'clsx';
|
|
2
|
+
import Translate from '@docusaurus/Translate';
|
|
3
|
+
import {anchorStyle} from '../../logic/comment-boxes.js';
|
|
4
|
+
import styles from '../../styles/components/comments/DocumentComments.module.scss';
|
|
5
|
+
|
|
6
|
+
export default function CommentButton({anchor, onClick}) {
|
|
7
|
+
return (
|
|
8
|
+
<button
|
|
9
|
+
type="button"
|
|
10
|
+
className={clsx('button button--primary button--sm', styles['add-button'])}
|
|
11
|
+
style={anchorStyle(anchor)}
|
|
12
|
+
// Pressing the mouse here must not clear the text selection.
|
|
13
|
+
onMouseDown={(event) => event.preventDefault()}
|
|
14
|
+
onClick={onClick}
|
|
15
|
+
>
|
|
16
|
+
<Translate id="docnest.comments.add">Add comment</Translate>
|
|
17
|
+
</button>
|
|
18
|
+
);
|
|
19
|
+
}
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import {useMemo} from 'react';
|
|
2
|
+
import {useWindowSize} from '@docusaurus/theme-common';
|
|
3
|
+
import Translate, {translate} from '@docusaurus/Translate';
|
|
4
|
+
import {inDocumentOrder} from '../../logic/comment-list.js';
|
|
5
|
+
import {useCommentDrawer} from '../../providers/CommentDrawer.jsx';
|
|
6
|
+
import styles from '../../styles/components/comments/CommentDrawer.module.scss';
|
|
7
|
+
|
|
8
|
+
export const COMMENT_DRAWER_ID = 'document-comment-drawer';
|
|
9
|
+
|
|
10
|
+
// The comments of the document on screen. It opens like the library
|
|
11
|
+
// navigation from the other side: beside the document on wide screens and
|
|
12
|
+
// over it on narrow ones.
|
|
13
|
+
export default function CommentDrawer() {
|
|
14
|
+
const {page, open, setOpen} = useCommentDrawer();
|
|
15
|
+
const isMobile = useWindowSize() === 'mobile';
|
|
16
|
+
const comments = useMemo(() => (page ? inDocumentOrder(page.comments) : []), [page]);
|
|
17
|
+
if (!page) return null;
|
|
18
|
+
|
|
19
|
+
const title = translate({id: 'docnest.comments.title', message: 'Comments on this page'});
|
|
20
|
+
const closeLabel = translate({id: 'docnest.comments.close', message: 'Close'});
|
|
21
|
+
const choose = (id) => {
|
|
22
|
+
// Over the document the drawer would hide the highlight it scrolls to.
|
|
23
|
+
if (isMobile) setOpen(false);
|
|
24
|
+
page.openComment(id);
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
return (
|
|
28
|
+
<>
|
|
29
|
+
<div
|
|
30
|
+
className={styles.backdrop}
|
|
31
|
+
data-open={open}
|
|
32
|
+
aria-hidden="true"
|
|
33
|
+
onClick={() => setOpen(false)}
|
|
34
|
+
/>
|
|
35
|
+
<aside
|
|
36
|
+
id={COMMENT_DRAWER_ID}
|
|
37
|
+
className={styles.drawer}
|
|
38
|
+
data-open={open}
|
|
39
|
+
inert={!open}
|
|
40
|
+
aria-hidden={!open || undefined}
|
|
41
|
+
aria-label={title}
|
|
42
|
+
>
|
|
43
|
+
<div className={styles.panel}>
|
|
44
|
+
<div className={styles.header}>
|
|
45
|
+
<h2 className={styles.title}>
|
|
46
|
+
{title}
|
|
47
|
+
<span className={styles.count}>{comments.length}</span>
|
|
48
|
+
</h2>
|
|
49
|
+
<button
|
|
50
|
+
type="button"
|
|
51
|
+
className={`clean-btn ${styles.close}`}
|
|
52
|
+
aria-label={closeLabel}
|
|
53
|
+
title={closeLabel}
|
|
54
|
+
onClick={() => setOpen(false)}
|
|
55
|
+
>
|
|
56
|
+
<svg
|
|
57
|
+
width="16"
|
|
58
|
+
height="16"
|
|
59
|
+
viewBox="0 0 24 24"
|
|
60
|
+
fill="none"
|
|
61
|
+
stroke="currentColor"
|
|
62
|
+
strokeWidth="1.8"
|
|
63
|
+
strokeLinecap="round"
|
|
64
|
+
aria-hidden="true"
|
|
65
|
+
>
|
|
66
|
+
<path d="M6 6l12 12M18 6 6 18" />
|
|
67
|
+
</svg>
|
|
68
|
+
</button>
|
|
69
|
+
</div>
|
|
70
|
+
{comments.length === 0 ? (
|
|
71
|
+
<p className={styles.empty}>
|
|
72
|
+
<Translate id="docnest.comments.empty">
|
|
73
|
+
Select text in the document to add a comment.
|
|
74
|
+
</Translate>
|
|
75
|
+
</p>
|
|
76
|
+
) : (
|
|
77
|
+
<ol className={styles.list}>
|
|
78
|
+
{comments.map((comment) => (
|
|
79
|
+
<li key={comment.id}>
|
|
80
|
+
<button
|
|
81
|
+
type="button"
|
|
82
|
+
className={styles.item}
|
|
83
|
+
onClick={() => choose(comment.id)}
|
|
84
|
+
>
|
|
85
|
+
<span className={styles.quote}>{comment.quote}</span>
|
|
86
|
+
<span className={styles.text}>{comment.text}</span>
|
|
87
|
+
</button>
|
|
88
|
+
</li>
|
|
89
|
+
))}
|
|
90
|
+
</ol>
|
|
91
|
+
)}
|
|
92
|
+
<p className={styles.notice}>
|
|
93
|
+
<Translate id="docnest.comments.notice">
|
|
94
|
+
Comments disappear when you reload or leave this page.
|
|
95
|
+
</Translate>
|
|
96
|
+
</p>
|
|
97
|
+
</div>
|
|
98
|
+
</aside>
|
|
99
|
+
</>
|
|
100
|
+
);
|
|
101
|
+
}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import Translate, {translate} from '@docusaurus/Translate';
|
|
2
|
+
import {anchorStyle} from '../../logic/comment-boxes.js';
|
|
3
|
+
import styles from '../../styles/components/comments/DocumentComments.module.scss';
|
|
4
|
+
|
|
5
|
+
// compose: write a new comment for the selected text. view: read or delete one.
|
|
6
|
+
export default function CommentPopover({
|
|
7
|
+
anchor,
|
|
8
|
+
mode,
|
|
9
|
+
text,
|
|
10
|
+
draft,
|
|
11
|
+
onDraftChange,
|
|
12
|
+
onSave,
|
|
13
|
+
onDelete,
|
|
14
|
+
onClose,
|
|
15
|
+
}) {
|
|
16
|
+
const label = translate({id: 'docnest.comments.label', message: 'Comment'});
|
|
17
|
+
return (
|
|
18
|
+
<div
|
|
19
|
+
role="dialog"
|
|
20
|
+
aria-label={label}
|
|
21
|
+
className={styles.popover}
|
|
22
|
+
style={anchorStyle(anchor)}
|
|
23
|
+
>
|
|
24
|
+
{mode === 'compose' ? (
|
|
25
|
+
<>
|
|
26
|
+
<textarea
|
|
27
|
+
className={styles.input}
|
|
28
|
+
value={draft}
|
|
29
|
+
aria-label={label}
|
|
30
|
+
placeholder={translate({
|
|
31
|
+
id: 'docnest.comments.placeholder',
|
|
32
|
+
message: 'Write a comment',
|
|
33
|
+
})}
|
|
34
|
+
autoFocus
|
|
35
|
+
onChange={(event) => onDraftChange(event.target.value)}
|
|
36
|
+
onKeyDown={(event) => {
|
|
37
|
+
if (event.key === 'Enter' && (event.metaKey || event.ctrlKey)) {
|
|
38
|
+
event.preventDefault();
|
|
39
|
+
onSave();
|
|
40
|
+
}
|
|
41
|
+
}}
|
|
42
|
+
/>
|
|
43
|
+
<p className={styles.notice}>
|
|
44
|
+
<Translate id="docnest.comments.notice">
|
|
45
|
+
Comments disappear when you reload or leave this page.
|
|
46
|
+
</Translate>
|
|
47
|
+
</p>
|
|
48
|
+
<div className={styles.actions}>
|
|
49
|
+
<button
|
|
50
|
+
type="button"
|
|
51
|
+
className="button button--secondary button--sm"
|
|
52
|
+
onClick={onClose}
|
|
53
|
+
>
|
|
54
|
+
<Translate id="docnest.comments.cancel">Cancel</Translate>
|
|
55
|
+
</button>
|
|
56
|
+
<button
|
|
57
|
+
type="button"
|
|
58
|
+
className="button button--primary button--sm"
|
|
59
|
+
disabled={!draft.trim()}
|
|
60
|
+
onClick={onSave}
|
|
61
|
+
>
|
|
62
|
+
<Translate id="docnest.comments.save">Add</Translate>
|
|
63
|
+
</button>
|
|
64
|
+
</div>
|
|
65
|
+
</>
|
|
66
|
+
) : (
|
|
67
|
+
<>
|
|
68
|
+
<p className={styles.text}>{text}</p>
|
|
69
|
+
<div className={styles.actions}>
|
|
70
|
+
<button
|
|
71
|
+
type="button"
|
|
72
|
+
className="button button--secondary button--sm"
|
|
73
|
+
onClick={onDelete}
|
|
74
|
+
>
|
|
75
|
+
<Translate id="docnest.comments.delete">Delete</Translate>
|
|
76
|
+
</button>
|
|
77
|
+
<button
|
|
78
|
+
type="button"
|
|
79
|
+
className="button button--primary button--sm"
|
|
80
|
+
onClick={onClose}
|
|
81
|
+
>
|
|
82
|
+
<Translate id="docnest.comments.close">Close</Translate>
|
|
83
|
+
</button>
|
|
84
|
+
</div>
|
|
85
|
+
</>
|
|
86
|
+
)}
|
|
87
|
+
</div>
|
|
88
|
+
);
|
|
89
|
+
}
|
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
import {useCallback, useEffect, useMemo, useRef, useState} from 'react';
|
|
2
|
+
import useHighlightBoxes from '../../hooks/useHighlightBoxes.js';
|
|
3
|
+
import useTextSelection from '../../hooks/useTextSelection.js';
|
|
4
|
+
import {anchorBelow, findBoxOwner} from '../../logic/comment-boxes.js';
|
|
5
|
+
import {commentQuote} from '../../logic/comment-list.js';
|
|
6
|
+
import {usePublishComments} from '../../providers/CommentDrawer.jsx';
|
|
7
|
+
import CommentButton from './CommentButton.jsx';
|
|
8
|
+
import CommentPopover from './CommentPopover.jsx';
|
|
9
|
+
import HighlightLayer from './HighlightLayer.jsx';
|
|
10
|
+
import styles from '../../styles/components/comments/DocumentComments.module.scss';
|
|
11
|
+
|
|
12
|
+
const SELECTION_KEY = 'selection';
|
|
13
|
+
const DRAFT_KEY = 'draft';
|
|
14
|
+
// A click on these keeps its normal behavior instead of opening a comment.
|
|
15
|
+
const INTERACTIVE = 'a, button, input, select, textarea, summary, label';
|
|
16
|
+
|
|
17
|
+
// Temporary comments on the document body. Comments live only in this
|
|
18
|
+
// component's state: nothing is sent, stored, or written to the Markdown source.
|
|
19
|
+
export default function DocumentComments({children}) {
|
|
20
|
+
const rootRef = useRef(null);
|
|
21
|
+
const contentRef = useRef(null);
|
|
22
|
+
const controlsRef = useRef(null);
|
|
23
|
+
const nextIdRef = useRef(1);
|
|
24
|
+
const [comments, setComments] = useState([]);
|
|
25
|
+
const [popover, setPopover] = useState(null);
|
|
26
|
+
const [draft, setDraft] = useState('');
|
|
27
|
+
const selection = useTextSelection(contentRef, controlsRef);
|
|
28
|
+
const publish = usePublishComments();
|
|
29
|
+
|
|
30
|
+
const entries = useMemo(
|
|
31
|
+
() => [
|
|
32
|
+
...comments.map(({id, range}) => ({key: String(id), range})),
|
|
33
|
+
...(popover?.mode === 'compose' ? [{key: DRAFT_KEY, range: popover.range}] : []),
|
|
34
|
+
...(selection && !popover ? [{key: SELECTION_KEY, range: selection}] : []),
|
|
35
|
+
],
|
|
36
|
+
[comments, popover, selection],
|
|
37
|
+
);
|
|
38
|
+
const boxes = useHighlightBoxes(rootRef, entries);
|
|
39
|
+
|
|
40
|
+
useEffect(() => {
|
|
41
|
+
if (!popover) return undefined;
|
|
42
|
+
const close = () => {
|
|
43
|
+
setPopover(null);
|
|
44
|
+
setDraft('');
|
|
45
|
+
};
|
|
46
|
+
const handleKeyDown = (event) => {
|
|
47
|
+
if (event.key === 'Escape') close();
|
|
48
|
+
};
|
|
49
|
+
// A written draft stays open until it is added or cancelled.
|
|
50
|
+
const handlePointerDown = (event) => {
|
|
51
|
+
if (controlsRef.current?.contains(event.target)) return;
|
|
52
|
+
if (popover.mode === 'view' || !draft.trim()) close();
|
|
53
|
+
};
|
|
54
|
+
document.addEventListener('keydown', handleKeyDown);
|
|
55
|
+
document.addEventListener('pointerdown', handlePointerDown, true);
|
|
56
|
+
return () => {
|
|
57
|
+
document.removeEventListener('keydown', handleKeyDown);
|
|
58
|
+
document.removeEventListener('pointerdown', handlePointerDown, true);
|
|
59
|
+
};
|
|
60
|
+
}, [popover, draft]);
|
|
61
|
+
|
|
62
|
+
const startComment = () => {
|
|
63
|
+
setPopover({mode: 'compose', range: selection});
|
|
64
|
+
setDraft('');
|
|
65
|
+
// The draft highlight replaces the native selection, which touch browsers
|
|
66
|
+
// clear on their own once the text field takes focus.
|
|
67
|
+
document.getSelection()?.removeAllRanges();
|
|
68
|
+
};
|
|
69
|
+
const saveComment = () => {
|
|
70
|
+
const text = draft.trim();
|
|
71
|
+
if (!text || popover?.mode !== 'compose') return;
|
|
72
|
+
const id = nextIdRef.current;
|
|
73
|
+
nextIdRef.current += 1;
|
|
74
|
+
const quote = commentQuote(popover.range.toString());
|
|
75
|
+
setComments((current) => [...current, {id, range: popover.range, text, quote}]);
|
|
76
|
+
setPopover(null);
|
|
77
|
+
setDraft('');
|
|
78
|
+
};
|
|
79
|
+
const deleteComment = (id) => {
|
|
80
|
+
setComments((current) => current.filter((comment) => comment.id !== id));
|
|
81
|
+
setPopover(null);
|
|
82
|
+
};
|
|
83
|
+
const closePopover = () => {
|
|
84
|
+
setPopover(null);
|
|
85
|
+
setDraft('');
|
|
86
|
+
};
|
|
87
|
+
const openCommentAt = (event) => {
|
|
88
|
+
if (popover?.mode === 'compose' || event.target.closest(INTERACTIVE)) return;
|
|
89
|
+
// The click that ends a drag selection is not a click on a highlight.
|
|
90
|
+
if (!document.getSelection()?.isCollapsed) return;
|
|
91
|
+
const origin = rootRef.current.getBoundingClientRect();
|
|
92
|
+
const newestFirst = comments.map(({id}) => String(id)).reverse();
|
|
93
|
+
const key = findBoxOwner(boxes, newestFirst, {
|
|
94
|
+
x: event.clientX - origin.left,
|
|
95
|
+
y: event.clientY - origin.top,
|
|
96
|
+
});
|
|
97
|
+
if (key) setPopover({mode: 'view', id: Number(key)});
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
// Chosen in the drawer: bring the highlight into view and open it there.
|
|
101
|
+
// A comment being written stays open, as it does for clicks on highlights.
|
|
102
|
+
const composing = popover?.mode === 'compose';
|
|
103
|
+
const openComment = useCallback(
|
|
104
|
+
(id) => {
|
|
105
|
+
if (composing) return;
|
|
106
|
+
setPopover({mode: 'view', id});
|
|
107
|
+
const highlight = rootRef.current?.querySelector(`[data-comment-key="${id}"]`);
|
|
108
|
+
const reduceMotion = window.matchMedia('(prefers-reduced-motion: reduce)').matches;
|
|
109
|
+
highlight?.scrollIntoView({block: 'center', behavior: reduceMotion ? 'auto' : 'smooth'});
|
|
110
|
+
},
|
|
111
|
+
[composing],
|
|
112
|
+
);
|
|
113
|
+
|
|
114
|
+
// The top bar button and the drawer live outside this component.
|
|
115
|
+
useEffect(() => {
|
|
116
|
+
publish({comments, openComment});
|
|
117
|
+
return () => publish(null);
|
|
118
|
+
}, [publish, comments, openComment]);
|
|
119
|
+
|
|
120
|
+
const viewing =
|
|
121
|
+
popover?.mode === 'view' ? comments.find((comment) => comment.id === popover.id) : null;
|
|
122
|
+
const highlights = [
|
|
123
|
+
...comments.map(({id}) => ({
|
|
124
|
+
key: String(id),
|
|
125
|
+
boxes: boxes[String(id)] ?? [],
|
|
126
|
+
variant: viewing?.id === id ? 'active' : 'comment',
|
|
127
|
+
})),
|
|
128
|
+
...(popover?.mode === 'compose'
|
|
129
|
+
? [{key: DRAFT_KEY, boxes: boxes[DRAFT_KEY] ?? [], variant: 'draft'}]
|
|
130
|
+
: []),
|
|
131
|
+
];
|
|
132
|
+
const buttonAnchor = selection && !popover ? anchorBelow(boxes[SELECTION_KEY]) : null;
|
|
133
|
+
const popoverAnchor =
|
|
134
|
+
popover?.mode === 'compose'
|
|
135
|
+
? anchorBelow(boxes[DRAFT_KEY])
|
|
136
|
+
: viewing
|
|
137
|
+
? anchorBelow(boxes[String(viewing.id)])
|
|
138
|
+
: null;
|
|
139
|
+
|
|
140
|
+
return (
|
|
141
|
+
<div
|
|
142
|
+
ref={rootRef}
|
|
143
|
+
className={styles.root}
|
|
144
|
+
>
|
|
145
|
+
<div
|
|
146
|
+
ref={contentRef}
|
|
147
|
+
onClick={openCommentAt}
|
|
148
|
+
>
|
|
149
|
+
{children}
|
|
150
|
+
</div>
|
|
151
|
+
<HighlightLayer highlights={highlights} />
|
|
152
|
+
<div ref={controlsRef}>
|
|
153
|
+
{buttonAnchor && (
|
|
154
|
+
<CommentButton
|
|
155
|
+
anchor={buttonAnchor}
|
|
156
|
+
onClick={startComment}
|
|
157
|
+
/>
|
|
158
|
+
)}
|
|
159
|
+
{popoverAnchor && (
|
|
160
|
+
<CommentPopover
|
|
161
|
+
anchor={popoverAnchor}
|
|
162
|
+
mode={popover.mode}
|
|
163
|
+
text={viewing?.text}
|
|
164
|
+
draft={draft}
|
|
165
|
+
onDraftChange={setDraft}
|
|
166
|
+
onSave={saveComment}
|
|
167
|
+
onDelete={() => deleteComment(viewing.id)}
|
|
168
|
+
onClose={closePopover}
|
|
169
|
+
/>
|
|
170
|
+
)}
|
|
171
|
+
</div>
|
|
172
|
+
</div>
|
|
173
|
+
);
|
|
174
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import clsx from 'clsx';
|
|
2
|
+
import styles from '../../styles/components/comments/DocumentComments.module.scss';
|
|
3
|
+
|
|
4
|
+
// Boxes drawn over the document text. They ignore the pointer so the text,
|
|
5
|
+
// links, and new selections underneath keep working.
|
|
6
|
+
export default function HighlightLayer({highlights}) {
|
|
7
|
+
return (
|
|
8
|
+
<div
|
|
9
|
+
className={styles.layer}
|
|
10
|
+
aria-hidden="true"
|
|
11
|
+
>
|
|
12
|
+
{highlights.flatMap(({key, boxes, variant}) =>
|
|
13
|
+
boxes.map((box, index) => (
|
|
14
|
+
<span
|
|
15
|
+
key={`${key}-${index}`}
|
|
16
|
+
// The drawer scrolls to the first box of a comment.
|
|
17
|
+
data-comment-key={key}
|
|
18
|
+
className={clsx(styles.highlight, styles[`highlight-${variant}`])}
|
|
19
|
+
style={{
|
|
20
|
+
left: box.left,
|
|
21
|
+
top: box.top,
|
|
22
|
+
width: box.right - box.left,
|
|
23
|
+
height: box.bottom - box.top,
|
|
24
|
+
}}
|
|
25
|
+
/>
|
|
26
|
+
)),
|
|
27
|
+
)}
|
|
28
|
+
</div>
|
|
29
|
+
);
|
|
30
|
+
}
|
|
@@ -1,46 +1,15 @@
|
|
|
1
|
-
import {useEffect, useRef} from 'react';
|
|
2
1
|
import styles from '../../styles/components/navigation/NavigationControls.module.scss';
|
|
3
|
-
import {flushSync} from 'react-dom';
|
|
4
2
|
import useIsBrowser from '@docusaurus/useIsBrowser';
|
|
5
3
|
import {useColorMode} from '@docusaurus/theme-common';
|
|
6
4
|
import IconLightMode from '@theme/Icon/LightMode';
|
|
7
5
|
import IconDarkMode from '@theme/Icon/DarkMode';
|
|
8
|
-
import {
|
|
6
|
+
import useColorModeSwitch, {colorModeLabel} from '../../hooks/useColorModeSwitch.js';
|
|
9
7
|
|
|
10
8
|
export default function ColorModeToggle({className, buttonClassName, onChange}) {
|
|
11
9
|
const {colorMode} = useColorMode();
|
|
12
10
|
const isBrowser = useIsBrowser();
|
|
13
|
-
const
|
|
14
|
-
const label =
|
|
15
|
-
colorMode === 'dark'
|
|
16
|
-
? translate({id: 'docnest.theme.light', message: 'Switch to light mode'})
|
|
17
|
-
: translate({id: 'docnest.theme.dark', message: 'Switch to dark mode'});
|
|
18
|
-
|
|
19
|
-
useEffect(() => () => transitionRef.current?.skipTransition(), []);
|
|
20
|
-
|
|
21
|
-
function toggleTheme() {
|
|
22
|
-
transitionRef.current?.skipTransition();
|
|
23
|
-
// Read at update time so rapid clicks still alternate correctly.
|
|
24
|
-
const updateTheme = () =>
|
|
25
|
-
onChange(document.documentElement.dataset.theme === 'dark' ? 'light' : 'dark');
|
|
26
|
-
|
|
27
|
-
if (
|
|
28
|
-
!document.startViewTransition ||
|
|
29
|
-
window.matchMedia('(prefers-reduced-motion: reduce)').matches
|
|
30
|
-
) {
|
|
31
|
-
updateTheme();
|
|
32
|
-
return;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
const transition = document.startViewTransition(() => flushSync(updateTheme));
|
|
36
|
-
transitionRef.current = transition;
|
|
37
|
-
// Skipping an interrupted transition rejects ready, but still applies its update.
|
|
38
|
-
transition.ready.catch(() => {});
|
|
39
|
-
const clearTransition = () => {
|
|
40
|
-
if (transitionRef.current === transition) transitionRef.current = null;
|
|
41
|
-
};
|
|
42
|
-
transition.finished.then(clearTransition, clearTransition);
|
|
43
|
-
}
|
|
11
|
+
const toggleTheme = useColorModeSwitch(onChange);
|
|
12
|
+
const label = colorModeLabel(colorMode);
|
|
44
13
|
|
|
45
14
|
return (
|
|
46
15
|
<div className={`${styles['theme-toggle']} ${className ?? ''}`}>
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import {translate} from '@docusaurus/Translate';
|
|
2
|
+
import {COMMENT_DRAWER_ID} from '../comments/CommentDrawer.jsx';
|
|
3
|
+
import {useCommentDrawer} from '../../providers/CommentDrawer.jsx';
|
|
4
|
+
import styles from '../../styles/components/navigation/NavigationControls.module.scss';
|
|
5
|
+
|
|
6
|
+
export default function CommentsToggle({mobile = false}) {
|
|
7
|
+
const {page, open, setOpen} = useCommentDrawer();
|
|
8
|
+
// Shown only on documents, and kept in the top bar at every viewport size.
|
|
9
|
+
if (mobile || !page) return null;
|
|
10
|
+
|
|
11
|
+
const count = page.comments.length;
|
|
12
|
+
const label = open
|
|
13
|
+
? translate({id: 'docnest.comments.hide', message: 'Hide comments'})
|
|
14
|
+
: translate({id: 'docnest.comments.show', message: 'Show comments'});
|
|
15
|
+
return (
|
|
16
|
+
<button
|
|
17
|
+
type="button"
|
|
18
|
+
className={`clean-btn ${styles.control} ${styles['comments-toggle']}`}
|
|
19
|
+
aria-label={count ? `${label} (${count})` : label}
|
|
20
|
+
title={label}
|
|
21
|
+
aria-expanded={open}
|
|
22
|
+
aria-controls={COMMENT_DRAWER_ID}
|
|
23
|
+
onClick={() => setOpen((shown) => !shown)}
|
|
24
|
+
>
|
|
25
|
+
<svg
|
|
26
|
+
width="20"
|
|
27
|
+
height="20"
|
|
28
|
+
viewBox="0 0 24 24"
|
|
29
|
+
fill="none"
|
|
30
|
+
stroke="currentColor"
|
|
31
|
+
strokeWidth="1.7"
|
|
32
|
+
strokeLinecap="round"
|
|
33
|
+
strokeLinejoin="round"
|
|
34
|
+
aria-hidden="true"
|
|
35
|
+
>
|
|
36
|
+
<path d="M5 4.5h14a1.5 1.5 0 0 1 1.5 1.5v9a1.5 1.5 0 0 1-1.5 1.5h-8l-5 4v-4H5A1.5 1.5 0 0 1 3.5 15V6A1.5 1.5 0 0 1 5 4.5Z" />
|
|
37
|
+
<path d="M8 9h8M8 12h5" />
|
|
38
|
+
</svg>
|
|
39
|
+
{count > 0 && (
|
|
40
|
+
<span
|
|
41
|
+
className={styles.badge}
|
|
42
|
+
aria-hidden="true"
|
|
43
|
+
>
|
|
44
|
+
{count}
|
|
45
|
+
</span>
|
|
46
|
+
)}
|
|
47
|
+
</button>
|
|
48
|
+
);
|
|
49
|
+
}
|