@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
|
@@ -1,19 +1,13 @@
|
|
|
1
1
|
import {useEffect, useId, useRef, useState} from 'react';
|
|
2
|
-
import {useHistorySelector} from '@docusaurus/theme-common';
|
|
3
|
-
import useDocusaurusContext from '@docusaurus/useDocusaurusContext';
|
|
4
|
-
import {useAlternatePageUtils} from '@docusaurus/theme-common/internal';
|
|
5
2
|
import {translate} from '@docusaurus/Translate';
|
|
6
3
|
import IconLanguage from '@theme/Icon/Language';
|
|
4
|
+
import useLocaleLinks from '../../hooks/useLocaleLinks.js';
|
|
7
5
|
import controls from '../../styles/components/navigation/NavigationControls.module.scss';
|
|
8
6
|
import styles from '../../styles/components/navigation/LanguageDropdown.module.scss';
|
|
9
7
|
|
|
10
8
|
export default function LanguageDropdown({mobile = false}) {
|
|
11
|
-
const
|
|
12
|
-
|
|
13
|
-
} = useDocusaurusContext();
|
|
14
|
-
const {createUrl} = useAlternatePageUtils();
|
|
15
|
-
const search = useHistorySelector((history) => history.location.search);
|
|
16
|
-
const hash = useHistorySelector((history) => history.location.hash);
|
|
9
|
+
const localeLinks = useLocaleLinks();
|
|
10
|
+
const current = localeLinks.find((link) => link.current);
|
|
17
11
|
const [open, setOpen] = useState(false);
|
|
18
12
|
const root = useRef(null);
|
|
19
13
|
const button = useRef(null);
|
|
@@ -33,12 +27,12 @@ export default function LanguageDropdown({mobile = false}) {
|
|
|
33
27
|
};
|
|
34
28
|
}, [open]);
|
|
35
29
|
|
|
36
|
-
// The
|
|
30
|
+
// The top bar keeps it above the narrow breakpoint; below, the More menu lists the languages.
|
|
37
31
|
if (mobile) return null;
|
|
38
32
|
|
|
39
33
|
return (
|
|
40
34
|
<div
|
|
41
|
-
className={styles.dropdown}
|
|
35
|
+
className={`${styles.dropdown} ${controls['wide-only']}`}
|
|
42
36
|
ref={root}
|
|
43
37
|
onKeyDown={(event) => {
|
|
44
38
|
if (event.key === 'Escape') {
|
|
@@ -52,7 +46,7 @@ export default function LanguageDropdown({mobile = false}) {
|
|
|
52
46
|
type="button"
|
|
53
47
|
className={`clean-btn ${controls.control} ${styles.trigger}`}
|
|
54
48
|
title={label}
|
|
55
|
-
aria-label={`${label}: ${
|
|
49
|
+
aria-label={`${label}: ${current?.label}`}
|
|
56
50
|
aria-expanded={open}
|
|
57
51
|
aria-controls={id}
|
|
58
52
|
onClick={() => setOpen((value) => !value)}
|
|
@@ -89,16 +83,16 @@ export default function LanguageDropdown({mobile = false}) {
|
|
|
89
83
|
hidden={!open}
|
|
90
84
|
aria-label={label}
|
|
91
85
|
>
|
|
92
|
-
{
|
|
93
|
-
<li key={locale}>
|
|
86
|
+
{localeLinks.map((link) => (
|
|
87
|
+
<li key={link.locale}>
|
|
94
88
|
<a
|
|
95
|
-
href={
|
|
96
|
-
lang={
|
|
97
|
-
hrefLang={
|
|
98
|
-
aria-current={
|
|
89
|
+
href={link.href}
|
|
90
|
+
lang={link.lang}
|
|
91
|
+
hrefLang={link.lang}
|
|
92
|
+
aria-current={link.current ? 'true' : undefined}
|
|
99
93
|
>
|
|
100
|
-
<span>{
|
|
101
|
-
{
|
|
94
|
+
<span>{link.label}</span>
|
|
95
|
+
{link.current && <span aria-hidden="true">✓</span>}
|
|
102
96
|
</a>
|
|
103
97
|
</li>
|
|
104
98
|
))}
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
import {useEffect, useId, useRef, useState} from 'react';
|
|
2
|
+
import {useColorMode, useThemeConfig} from '@docusaurus/theme-common';
|
|
3
|
+
import useIsBrowser from '@docusaurus/useIsBrowser';
|
|
4
|
+
import {translate} from '@docusaurus/Translate';
|
|
5
|
+
import IconLightMode from '@theme/Icon/LightMode';
|
|
6
|
+
import IconDarkMode from '@theme/Icon/DarkMode';
|
|
7
|
+
import useColorModeSwitch, {colorModeLabel} from '../../hooks/useColorModeSwitch.js';
|
|
8
|
+
import useLocaleLinks from '../../hooks/useLocaleLinks.js';
|
|
9
|
+
import RecentUpdatesLink from './RecentUpdatesLink.jsx';
|
|
10
|
+
import controls from '../../styles/components/navigation/NavigationControls.module.scss';
|
|
11
|
+
import styles from '../../styles/components/navigation/MoreMenu.module.scss';
|
|
12
|
+
|
|
13
|
+
// On narrow screens the top bar has no room for every control, so recent
|
|
14
|
+
// updates, the color mode, and the languages move into this menu.
|
|
15
|
+
export default function MoreMenu({mobile = false}) {
|
|
16
|
+
const {
|
|
17
|
+
navbar: {items},
|
|
18
|
+
colorMode: {disableSwitch},
|
|
19
|
+
} = useThemeConfig();
|
|
20
|
+
const {colorMode, setColorMode} = useColorMode();
|
|
21
|
+
const isBrowser = useIsBrowser();
|
|
22
|
+
const toggleTheme = useColorModeSwitch(setColorMode);
|
|
23
|
+
const localeLinks = useLocaleLinks();
|
|
24
|
+
const [open, setOpen] = useState(false);
|
|
25
|
+
const root = useRef(null);
|
|
26
|
+
const button = useRef(null);
|
|
27
|
+
const id = useId();
|
|
28
|
+
const label = translate({id: 'docnest.navigation.more', message: 'More'});
|
|
29
|
+
|
|
30
|
+
useEffect(() => {
|
|
31
|
+
if (!open) return undefined;
|
|
32
|
+
const closeOutside = (event) => {
|
|
33
|
+
if (!root.current?.contains(event.target)) setOpen(false);
|
|
34
|
+
};
|
|
35
|
+
document.addEventListener('pointerdown', closeOutside);
|
|
36
|
+
document.addEventListener('focusin', closeOutside);
|
|
37
|
+
return () => {
|
|
38
|
+
document.removeEventListener('pointerdown', closeOutside);
|
|
39
|
+
document.removeEventListener('focusin', closeOutside);
|
|
40
|
+
};
|
|
41
|
+
}, [open]);
|
|
42
|
+
|
|
43
|
+
if (mobile) return null;
|
|
44
|
+
|
|
45
|
+
// Only the controls this site shows in the top bar.
|
|
46
|
+
const hasUpdates = items.some((item) => item.type === 'custom-recentUpdates');
|
|
47
|
+
const hasLanguages =
|
|
48
|
+
localeLinks.length > 1 && items.some((item) => item.type === 'custom-languageDropdown');
|
|
49
|
+
const close = () => setOpen(false);
|
|
50
|
+
|
|
51
|
+
return (
|
|
52
|
+
<div
|
|
53
|
+
className={`${styles.dropdown} ${controls['narrow-only']}`}
|
|
54
|
+
ref={root}
|
|
55
|
+
onKeyDown={(event) => {
|
|
56
|
+
if (event.key === 'Escape') {
|
|
57
|
+
close();
|
|
58
|
+
button.current?.focus();
|
|
59
|
+
}
|
|
60
|
+
}}
|
|
61
|
+
>
|
|
62
|
+
<button
|
|
63
|
+
ref={button}
|
|
64
|
+
type="button"
|
|
65
|
+
className={`clean-btn ${controls.control}`}
|
|
66
|
+
aria-label={label}
|
|
67
|
+
title={label}
|
|
68
|
+
aria-expanded={open}
|
|
69
|
+
aria-controls={id}
|
|
70
|
+
onClick={() => setOpen((shown) => !shown)}
|
|
71
|
+
>
|
|
72
|
+
<svg
|
|
73
|
+
width="20"
|
|
74
|
+
height="20"
|
|
75
|
+
viewBox="0 0 24 24"
|
|
76
|
+
fill="currentColor"
|
|
77
|
+
aria-hidden="true"
|
|
78
|
+
>
|
|
79
|
+
<circle
|
|
80
|
+
cx="5"
|
|
81
|
+
cy="12"
|
|
82
|
+
r="1.8"
|
|
83
|
+
/>
|
|
84
|
+
<circle
|
|
85
|
+
cx="12"
|
|
86
|
+
cy="12"
|
|
87
|
+
r="1.8"
|
|
88
|
+
/>
|
|
89
|
+
<circle
|
|
90
|
+
cx="19"
|
|
91
|
+
cy="12"
|
|
92
|
+
r="1.8"
|
|
93
|
+
/>
|
|
94
|
+
</svg>
|
|
95
|
+
</button>
|
|
96
|
+
<div
|
|
97
|
+
id={id}
|
|
98
|
+
className={styles.panel}
|
|
99
|
+
hidden={!open}
|
|
100
|
+
>
|
|
101
|
+
<ul className="menu__list">
|
|
102
|
+
{hasUpdates && (
|
|
103
|
+
<RecentUpdatesLink
|
|
104
|
+
mobile
|
|
105
|
+
onClick={close}
|
|
106
|
+
/>
|
|
107
|
+
)}
|
|
108
|
+
{!disableSwitch && (
|
|
109
|
+
<li className="menu__list-item">
|
|
110
|
+
<button
|
|
111
|
+
type="button"
|
|
112
|
+
className={`clean-btn menu__link ${controls['menu-link']} ${styles.action}`}
|
|
113
|
+
disabled={!isBrowser}
|
|
114
|
+
onClick={toggleTheme}
|
|
115
|
+
>
|
|
116
|
+
<IconLightMode
|
|
117
|
+
className={controls['light-icon']}
|
|
118
|
+
width={20}
|
|
119
|
+
height={20}
|
|
120
|
+
aria-hidden="true"
|
|
121
|
+
/>
|
|
122
|
+
<IconDarkMode
|
|
123
|
+
className={controls['dark-icon']}
|
|
124
|
+
width={20}
|
|
125
|
+
height={20}
|
|
126
|
+
aria-hidden="true"
|
|
127
|
+
/>
|
|
128
|
+
<span>{colorModeLabel(colorMode)}</span>
|
|
129
|
+
</button>
|
|
130
|
+
</li>
|
|
131
|
+
)}
|
|
132
|
+
</ul>
|
|
133
|
+
{hasLanguages && (
|
|
134
|
+
<>
|
|
135
|
+
<p className={styles.heading}>
|
|
136
|
+
{translate({id: 'docnest.language.label', message: 'Change language'})}
|
|
137
|
+
</p>
|
|
138
|
+
<ul className="menu__list">
|
|
139
|
+
{localeLinks.map((link) => (
|
|
140
|
+
<li
|
|
141
|
+
key={link.locale}
|
|
142
|
+
className="menu__list-item"
|
|
143
|
+
>
|
|
144
|
+
<a
|
|
145
|
+
className={`menu__link ${styles.language}`}
|
|
146
|
+
href={link.href}
|
|
147
|
+
lang={link.lang}
|
|
148
|
+
hrefLang={link.lang}
|
|
149
|
+
aria-current={link.current ? 'true' : undefined}
|
|
150
|
+
>
|
|
151
|
+
<span>{link.label}</span>
|
|
152
|
+
{link.current && <span aria-hidden="true">✓</span>}
|
|
153
|
+
</a>
|
|
154
|
+
</li>
|
|
155
|
+
))}
|
|
156
|
+
</ul>
|
|
157
|
+
</>
|
|
158
|
+
)}
|
|
159
|
+
</div>
|
|
160
|
+
</div>
|
|
161
|
+
);
|
|
162
|
+
}
|
|
@@ -10,7 +10,9 @@ export default function RecentUpdatesLink({mobile = false, onClick}) {
|
|
|
10
10
|
const link = (
|
|
11
11
|
<Link
|
|
12
12
|
to={url}
|
|
13
|
-
className={
|
|
13
|
+
className={
|
|
14
|
+
mobile ? `menu__link ${styles['menu-link']}` : `${styles.control} ${styles['wide-only']}`
|
|
15
|
+
}
|
|
14
16
|
aria-label={label}
|
|
15
17
|
title={label}
|
|
16
18
|
onClick={onClick}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import {useEffect, useRef} from 'react';
|
|
2
|
+
import {flushSync} from 'react-dom';
|
|
3
|
+
import {translate} from '@docusaurus/Translate';
|
|
4
|
+
|
|
5
|
+
// The label names the mode the button switches to.
|
|
6
|
+
export function colorModeLabel(colorMode) {
|
|
7
|
+
return colorMode === 'dark'
|
|
8
|
+
? translate({id: 'docnest.theme.light', message: 'Switch to light mode'})
|
|
9
|
+
: translate({id: 'docnest.theme.dark', message: 'Switch to dark mode'});
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
// Switches between light and dark, with a view transition where the browser
|
|
13
|
+
// supports one and motion is not reduced.
|
|
14
|
+
export default function useColorModeSwitch(onChange) {
|
|
15
|
+
const transitionRef = useRef(null);
|
|
16
|
+
|
|
17
|
+
useEffect(() => () => transitionRef.current?.skipTransition(), []);
|
|
18
|
+
|
|
19
|
+
return function toggleTheme() {
|
|
20
|
+
transitionRef.current?.skipTransition();
|
|
21
|
+
// Read at update time so rapid clicks still alternate correctly.
|
|
22
|
+
const updateTheme = () =>
|
|
23
|
+
onChange(document.documentElement.dataset.theme === 'dark' ? 'light' : 'dark');
|
|
24
|
+
|
|
25
|
+
if (
|
|
26
|
+
!document.startViewTransition ||
|
|
27
|
+
window.matchMedia('(prefers-reduced-motion: reduce)').matches
|
|
28
|
+
) {
|
|
29
|
+
updateTheme();
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const transition = document.startViewTransition(() => flushSync(updateTheme));
|
|
34
|
+
transitionRef.current = transition;
|
|
35
|
+
// Skipping an interrupted transition rejects ready, but still applies its update.
|
|
36
|
+
transition.ready.catch(() => {});
|
|
37
|
+
const clearTransition = () => {
|
|
38
|
+
if (transitionRef.current === transition) transitionRef.current = null;
|
|
39
|
+
};
|
|
40
|
+
transition.finished.then(clearTransition, clearTransition);
|
|
41
|
+
};
|
|
42
|
+
}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import {useEffect, useState} from 'react';
|
|
2
|
+
import {UNBOUNDED, intersectRect, toLineBoxes} from '../logic/comment-boxes.js';
|
|
3
|
+
|
|
4
|
+
// Text inside a scrolling block (code, wide tables) must not be drawn outside it.
|
|
5
|
+
function clipRectOf(element, root, cache) {
|
|
6
|
+
if (!element || element === root || !root.contains(element)) return UNBOUNDED;
|
|
7
|
+
if (cache.has(element)) return cache.get(element);
|
|
8
|
+
const parentClip = clipRectOf(element.parentElement, root, cache);
|
|
9
|
+
const style = window.getComputedStyle(element);
|
|
10
|
+
const clips = style.overflowX !== 'visible' || style.overflowY !== 'visible';
|
|
11
|
+
const clip =
|
|
12
|
+
parentClip && clips ? intersectRect(parentClip, element.getBoundingClientRect()) : parentClip;
|
|
13
|
+
cache.set(element, clip);
|
|
14
|
+
return clip;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function textNodesIn(range) {
|
|
18
|
+
const ancestor = range.commonAncestorContainer;
|
|
19
|
+
if (ancestor.nodeType === Node.TEXT_NODE) return [ancestor];
|
|
20
|
+
const walker = document.createTreeWalker(ancestor, NodeFilter.SHOW_TEXT);
|
|
21
|
+
const nodes = [];
|
|
22
|
+
for (let node = walker.nextNode(); node; node = walker.nextNode()) {
|
|
23
|
+
if (range.intersectsNode(node)) nodes.push(node);
|
|
24
|
+
}
|
|
25
|
+
return nodes;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// Range.getClientRects() also returns whole element boxes for fully selected
|
|
29
|
+
// paragraphs, so measure each selected text fragment instead.
|
|
30
|
+
function textRects(range, root, cache) {
|
|
31
|
+
const rects = [];
|
|
32
|
+
for (const node of textNodesIn(range)) {
|
|
33
|
+
if (!node.textContent.trim()) continue;
|
|
34
|
+
const clip = clipRectOf(node.parentElement, root, cache);
|
|
35
|
+
if (!clip) continue;
|
|
36
|
+
const part = document.createRange();
|
|
37
|
+
part.selectNodeContents(node);
|
|
38
|
+
if (node === range.startContainer) part.setStart(node, range.startOffset);
|
|
39
|
+
if (node === range.endContainer) part.setEnd(node, range.endOffset);
|
|
40
|
+
for (const rect of part.getClientRects()) {
|
|
41
|
+
const visible = intersectRect(rect, clip);
|
|
42
|
+
if (visible) rects.push(visible);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
return rects;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// Measures each entry's range as boxes relative to rootRef. Boxes live inside
|
|
49
|
+
// rootRef, so page scrolling moves them with the text; layout changes re-measure.
|
|
50
|
+
export default function useHighlightBoxes(rootRef, entries) {
|
|
51
|
+
const [boxes, setBoxes] = useState({});
|
|
52
|
+
|
|
53
|
+
useEffect(() => {
|
|
54
|
+
const root = rootRef.current;
|
|
55
|
+
if (!root) return undefined;
|
|
56
|
+
let active = true;
|
|
57
|
+
let frame = 0;
|
|
58
|
+
const measure = () => {
|
|
59
|
+
frame = 0;
|
|
60
|
+
if (!active) return;
|
|
61
|
+
const origin = root.getBoundingClientRect();
|
|
62
|
+
const cache = new Map();
|
|
63
|
+
const next = {};
|
|
64
|
+
for (const {key, range} of entries) {
|
|
65
|
+
next[key] = toLineBoxes(textRects(range, root, cache), origin);
|
|
66
|
+
}
|
|
67
|
+
setBoxes(next);
|
|
68
|
+
};
|
|
69
|
+
const schedule = () => {
|
|
70
|
+
if (active && !frame) frame = window.requestAnimationFrame(measure);
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
measure();
|
|
74
|
+
const observer = new ResizeObserver(schedule);
|
|
75
|
+
observer.observe(root);
|
|
76
|
+
root.addEventListener('scroll', schedule, {capture: true, passive: true});
|
|
77
|
+
window.addEventListener('resize', schedule);
|
|
78
|
+
document.fonts?.ready.then(schedule);
|
|
79
|
+
return () => {
|
|
80
|
+
active = false;
|
|
81
|
+
window.cancelAnimationFrame(frame);
|
|
82
|
+
observer.disconnect();
|
|
83
|
+
root.removeEventListener('scroll', schedule, {capture: true});
|
|
84
|
+
window.removeEventListener('resize', schedule);
|
|
85
|
+
};
|
|
86
|
+
}, [rootRef, entries]);
|
|
87
|
+
|
|
88
|
+
return boxes;
|
|
89
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import {useHistorySelector} from '@docusaurus/theme-common';
|
|
2
|
+
import {useAlternatePageUtils} from '@docusaurus/theme-common/internal';
|
|
3
|
+
import useDocusaurusContext from '@docusaurus/useDocusaurusContext';
|
|
4
|
+
|
|
5
|
+
// The current page in every site language, keeping the query and the heading.
|
|
6
|
+
export default function useLocaleLinks() {
|
|
7
|
+
const {
|
|
8
|
+
i18n: {currentLocale, locales, localeConfigs},
|
|
9
|
+
} = useDocusaurusContext();
|
|
10
|
+
const {createUrl} = useAlternatePageUtils();
|
|
11
|
+
const search = useHistorySelector((history) => history.location.search);
|
|
12
|
+
const hash = useHistorySelector((history) => history.location.hash);
|
|
13
|
+
return locales.map((locale) => ({
|
|
14
|
+
locale,
|
|
15
|
+
href: `${createUrl({locale, fullyQualified: false})}${search}${hash}`,
|
|
16
|
+
label: localeConfigs[locale].label,
|
|
17
|
+
lang: localeConfigs[locale].htmlLang,
|
|
18
|
+
current: locale === currentLocale,
|
|
19
|
+
}));
|
|
20
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import {useEffect, useState} from 'react';
|
|
2
|
+
|
|
3
|
+
// Touch selections change while the reader drags the native handles and end
|
|
4
|
+
// without a pointer event, so a selection counts once it stops changing.
|
|
5
|
+
const SETTLE_DELAY = 250;
|
|
6
|
+
|
|
7
|
+
// Returns a copy of the current text selection when it lies inside contentRef,
|
|
8
|
+
// or null when nothing usable is selected.
|
|
9
|
+
export default function useTextSelection(contentRef, controlsRef) {
|
|
10
|
+
const [selection, setSelection] = useState(null);
|
|
11
|
+
|
|
12
|
+
useEffect(() => {
|
|
13
|
+
let timer = 0;
|
|
14
|
+
let mousePressed = false;
|
|
15
|
+
|
|
16
|
+
const evaluate = () => {
|
|
17
|
+
const current = document.getSelection();
|
|
18
|
+
const content = contentRef.current;
|
|
19
|
+
if (!content || !current || current.rangeCount === 0 || current.isCollapsed) {
|
|
20
|
+
setSelection(null);
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
const range = current.getRangeAt(0);
|
|
24
|
+
const inside = content.contains(range.commonAncestorContainer);
|
|
25
|
+
setSelection(inside && current.toString().trim() ? range.cloneRange() : null);
|
|
26
|
+
};
|
|
27
|
+
const schedule = () => {
|
|
28
|
+
window.clearTimeout(timer);
|
|
29
|
+
if (!mousePressed) timer = window.setTimeout(evaluate, SETTLE_DELAY);
|
|
30
|
+
};
|
|
31
|
+
// A mouse drag is finished on release, so show the button without waiting.
|
|
32
|
+
const handlePointerDown = (event) => {
|
|
33
|
+
if (event.pointerType !== 'mouse' || event.button !== 0) return;
|
|
34
|
+
if (controlsRef.current?.contains(event.target)) return;
|
|
35
|
+
mousePressed = true;
|
|
36
|
+
};
|
|
37
|
+
const handlePointerUp = () => {
|
|
38
|
+
if (!mousePressed) return;
|
|
39
|
+
mousePressed = false;
|
|
40
|
+
window.clearTimeout(timer);
|
|
41
|
+
timer = window.setTimeout(evaluate, 0);
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
document.addEventListener('selectionchange', schedule);
|
|
45
|
+
document.addEventListener('pointerdown', handlePointerDown, true);
|
|
46
|
+
document.addEventListener('pointerup', handlePointerUp, true);
|
|
47
|
+
return () => {
|
|
48
|
+
window.clearTimeout(timer);
|
|
49
|
+
document.removeEventListener('selectionchange', schedule);
|
|
50
|
+
document.removeEventListener('pointerdown', handlePointerDown, true);
|
|
51
|
+
document.removeEventListener('pointerup', handlePointerUp, true);
|
|
52
|
+
};
|
|
53
|
+
}, [contentRef, controlsRef]);
|
|
54
|
+
|
|
55
|
+
return selection;
|
|
56
|
+
}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
// Geometry for document comment highlights. Rects use viewport coordinates
|
|
2
|
+
// ({left, top, right, bottom}); boxes are relative to the comment layer.
|
|
3
|
+
|
|
4
|
+
export const UNBOUNDED = {left: -Infinity, top: -Infinity, right: Infinity, bottom: Infinity};
|
|
5
|
+
|
|
6
|
+
export function intersectRect(first, second) {
|
|
7
|
+
const left = Math.max(first.left, second.left);
|
|
8
|
+
const top = Math.max(first.top, second.top);
|
|
9
|
+
const right = Math.min(first.right, second.right);
|
|
10
|
+
const bottom = Math.min(first.bottom, second.bottom);
|
|
11
|
+
return right > left && bottom > top ? {left, top, right, bottom} : null;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function sameLine(first, second) {
|
|
15
|
+
const overlap = Math.min(first.bottom, second.bottom) - Math.max(first.top, second.top);
|
|
16
|
+
const smallerHeight = Math.min(first.bottom - first.top, second.bottom - second.top);
|
|
17
|
+
return overlap >= smallerHeight / 2;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
// Inline text produces one rect per text fragment. Fragments on the same line
|
|
21
|
+
// that touch become one box, while separate cells or columns stay apart.
|
|
22
|
+
export function toLineBoxes(rects, origin, gap = 2) {
|
|
23
|
+
const boxes = rects
|
|
24
|
+
.filter((rect) => rect.right - rect.left >= 1 && rect.bottom - rect.top >= 1)
|
|
25
|
+
.map((rect) => ({
|
|
26
|
+
left: rect.left - origin.left,
|
|
27
|
+
top: rect.top - origin.top,
|
|
28
|
+
right: rect.right - origin.left,
|
|
29
|
+
bottom: rect.bottom - origin.top,
|
|
30
|
+
}))
|
|
31
|
+
.sort((first, second) => first.top - second.top || first.left - second.left);
|
|
32
|
+
const lines = [];
|
|
33
|
+
for (const box of boxes) {
|
|
34
|
+
const line = lines.find((candidate) => sameLine(candidate.bounds, box));
|
|
35
|
+
if (line) {
|
|
36
|
+
line.boxes.push(box);
|
|
37
|
+
line.bounds.top = Math.min(line.bounds.top, box.top);
|
|
38
|
+
line.bounds.bottom = Math.max(line.bounds.bottom, box.bottom);
|
|
39
|
+
} else {
|
|
40
|
+
lines.push({bounds: {top: box.top, bottom: box.bottom}, boxes: [box]});
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
const merged = [];
|
|
44
|
+
for (const line of lines) {
|
|
45
|
+
const sorted = line.boxes.sort((first, second) => first.left - second.left);
|
|
46
|
+
let current = {...sorted[0]};
|
|
47
|
+
for (const box of sorted.slice(1)) {
|
|
48
|
+
if (box.left <= current.right + gap) {
|
|
49
|
+
current.right = Math.max(current.right, box.right);
|
|
50
|
+
current.top = Math.min(current.top, box.top);
|
|
51
|
+
current.bottom = Math.max(current.bottom, box.bottom);
|
|
52
|
+
} else {
|
|
53
|
+
merged.push(current);
|
|
54
|
+
current = {...box};
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
merged.push(current);
|
|
58
|
+
}
|
|
59
|
+
return merged.sort((first, second) => first.top - second.top || first.left - second.left);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// Controls open just below the end of the highlighted text.
|
|
63
|
+
export function anchorBelow(boxes) {
|
|
64
|
+
const last = boxes?.at(-1);
|
|
65
|
+
return last ? {x: last.right, y: last.bottom} : null;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// keys are checked in order, so pass the newest comment first.
|
|
69
|
+
export function findBoxOwner(boxesByKey, keys, point, tolerance = 2) {
|
|
70
|
+
return (
|
|
71
|
+
keys.find((key) =>
|
|
72
|
+
(boxesByKey[key] ?? []).some(
|
|
73
|
+
(box) =>
|
|
74
|
+
point.x >= box.left - tolerance &&
|
|
75
|
+
point.x <= box.right + tolerance &&
|
|
76
|
+
point.y >= box.top - tolerance &&
|
|
77
|
+
point.y <= box.bottom + tolerance,
|
|
78
|
+
),
|
|
79
|
+
) ?? null
|
|
80
|
+
);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// Controls read their position from CSS variables so the stylesheet can keep
|
|
84
|
+
// them inside the document column.
|
|
85
|
+
export function anchorStyle(anchor) {
|
|
86
|
+
return {'--comment-anchor-x': `${anchor.x}px`, '--comment-anchor-y': `${anchor.y}px`};
|
|
87
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
// Range.START_TO_START, written out so the order can be checked outside a browser.
|
|
2
|
+
const START_TO_START = 0;
|
|
3
|
+
|
|
4
|
+
// Selected text keeps the line breaks between paragraphs and cells; the list
|
|
5
|
+
// shows it on one line.
|
|
6
|
+
export function commentQuote(text) {
|
|
7
|
+
return text.replace(/\s+/g, ' ').trim();
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
function compareStart(first, second) {
|
|
11
|
+
try {
|
|
12
|
+
return first.range.compareBoundaryPoints(START_TO_START, second.range);
|
|
13
|
+
} catch {
|
|
14
|
+
// A range whose text has left the page has no place in the order.
|
|
15
|
+
return 0;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
// The list follows the reading order of the document, not the writing order.
|
|
20
|
+
export function inDocumentOrder(comments) {
|
|
21
|
+
return [...comments].sort((first, second) => compareStart(first, second) || first.id - second.id);
|
|
22
|
+
}
|
|
@@ -1,5 +1,10 @@
|
|
|
1
|
+
import {CommentDrawerProvider} from './CommentDrawer.jsx';
|
|
1
2
|
import {SidebarVisibilityProvider} from './SidebarVisibility.jsx';
|
|
2
3
|
|
|
3
4
|
export default function AppProviders({children}) {
|
|
4
|
-
return
|
|
5
|
+
return (
|
|
6
|
+
<SidebarVisibilityProvider>
|
|
7
|
+
<CommentDrawerProvider>{children}</CommentDrawerProvider>
|
|
8
|
+
</SidebarVisibilityProvider>
|
|
9
|
+
);
|
|
5
10
|
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import {createContext, useContext, useMemo, useState} from 'react';
|
|
2
|
+
import {useLocation} from '@docusaurus/router';
|
|
3
|
+
|
|
4
|
+
// The document that owns the comments publishes them here, so the top bar
|
|
5
|
+
// button and the drawer beside the document can show them.
|
|
6
|
+
const PublishContext = createContext(() => {});
|
|
7
|
+
const CommentDrawerContext = createContext(null);
|
|
8
|
+
|
|
9
|
+
export function CommentDrawerProvider({children}) {
|
|
10
|
+
const {pathname} = useLocation();
|
|
11
|
+
// {comments, openComment} from the document on screen, or null on other pages.
|
|
12
|
+
const [page, setPage] = useState(null);
|
|
13
|
+
const [open, setOpen] = useState(false);
|
|
14
|
+
// Comments belong to one document, so the drawer closes when the page changes.
|
|
15
|
+
const [drawerPath, setDrawerPath] = useState(pathname);
|
|
16
|
+
if (drawerPath !== pathname) {
|
|
17
|
+
setDrawerPath(pathname);
|
|
18
|
+
setOpen(false);
|
|
19
|
+
}
|
|
20
|
+
const value = useMemo(() => ({page, open, setOpen}), [page, open]);
|
|
21
|
+
return (
|
|
22
|
+
<PublishContext.Provider value={setPage}>
|
|
23
|
+
<CommentDrawerContext.Provider value={value}>{children}</CommentDrawerContext.Provider>
|
|
24
|
+
</PublishContext.Provider>
|
|
25
|
+
);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// Separate from the drawer state so publishing does not re-render the document.
|
|
29
|
+
export const usePublishComments = () => useContext(PublishContext);
|
|
30
|
+
export const useCommentDrawer = () => useContext(CommentDrawerContext);
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
.content {
|
|
2
|
+
max-width: 48rem;
|
|
3
|
+
margin-inline: auto;
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
.heading {
|
|
7
|
+
display: flex;
|
|
8
|
+
align-items: center;
|
|
9
|
+
gap: clamp(1rem, 3vw, 1.5rem);
|
|
10
|
+
margin-bottom: 1.5rem;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
.logo {
|
|
14
|
+
flex-shrink: 0;
|
|
15
|
+
width: clamp(4rem, 12vw, 6rem);
|
|
16
|
+
height: auto;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
.title {
|
|
20
|
+
min-width: 0;
|
|
21
|
+
margin: 0;
|
|
22
|
+
font-size: clamp(1.75rem, 5vw, 3rem);
|
|
23
|
+
line-height: 1.2;
|
|
24
|
+
overflow-wrap: anywhere;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
.actions {
|
|
28
|
+
display: flex;
|
|
29
|
+
flex-wrap: wrap;
|
|
30
|
+
gap: 0.75rem;
|
|
31
|
+
margin-top: 1.5rem;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// Links do not center their label like buttons, so center both explicitly.
|
|
35
|
+
.actions > * {
|
|
36
|
+
display: inline-flex;
|
|
37
|
+
align-items: center;
|
|
38
|
+
justify-content: center;
|
|
39
|
+
min-height: 44px;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
.details {
|
|
43
|
+
margin-top: 2rem;
|
|
44
|
+
color: var(--ifm-color-emphasis-700);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
.details summary {
|
|
48
|
+
cursor: pointer;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
.details pre {
|
|
52
|
+
margin-top: 0.75rem;
|
|
53
|
+
white-space: pre-wrap;
|
|
54
|
+
overflow-wrap: anywhere;
|
|
55
|
+
}
|