mirror of
https://github.com/docmost/docmost.git
synced 2026-08-24 00:32:13 +10:00
feat(client): add compare selection mode to page history list
This commit is contained in:
@@ -1289,5 +1289,9 @@
|
|||||||
"{{count}} rows deleted_one": "1 row deleted",
|
"{{count}} rows deleted_one": "1 row deleted",
|
||||||
"{{count}} rows deleted_other": "{{count}} rows deleted",
|
"{{count}} rows deleted_other": "{{count}} rows deleted",
|
||||||
"{{count}} selected_one": "1 selected",
|
"{{count}} selected_one": "1 selected",
|
||||||
"{{count}} selected_other": "{{count}} selected"
|
"{{count}} selected_other": "{{count}} selected",
|
||||||
|
"Compare": "Compare",
|
||||||
|
"Compare versions": "Compare versions",
|
||||||
|
"Select version from {{date}}": "Select version from {{date}}",
|
||||||
|
"Version actions for {{date}}": "Version actions for {{date}}"
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
.history {
|
.history {
|
||||||
display: block;
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
width: 100%;
|
width: 100%;
|
||||||
padding: var(--mantine-spacing-md);
|
|
||||||
color: light-dark(var(--mantine-color-black), var(--mantine-color-dark-0));
|
color: light-dark(var(--mantine-color-black), var(--mantine-color-dark-0));
|
||||||
|
|
||||||
@mixin hover {
|
@mixin hover {
|
||||||
@@ -12,6 +12,28 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.historyButton {
|
||||||
|
flex: 1;
|
||||||
|
min-width: 0;
|
||||||
|
color: inherit;
|
||||||
|
}
|
||||||
|
|
||||||
|
.compareCheckbox {
|
||||||
|
padding-left: var(--mantine-spacing-xs);
|
||||||
|
}
|
||||||
|
|
||||||
|
.itemMenu {
|
||||||
|
opacity: 0;
|
||||||
|
margin-right: var(--mantine-spacing-xs);
|
||||||
|
}
|
||||||
|
|
||||||
|
.history:hover .itemMenu,
|
||||||
|
.history:focus-within .itemMenu,
|
||||||
|
.history.active .itemMenu,
|
||||||
|
.itemMenu[aria-expanded="true"] {
|
||||||
|
opacity: 1;
|
||||||
|
}
|
||||||
|
|
||||||
.historyEditor {
|
.historyEditor {
|
||||||
:global(.ProseMirror) {
|
:global(.ProseMirror) {
|
||||||
padding: 0 !important;
|
padding: 0 !important;
|
||||||
|
|||||||
@@ -1,10 +1,21 @@
|
|||||||
import { Text, Group, UnstyledButton, Avatar, Tooltip } from "@mantine/core";
|
import {
|
||||||
|
Text,
|
||||||
|
Group,
|
||||||
|
UnstyledButton,
|
||||||
|
Avatar,
|
||||||
|
Tooltip,
|
||||||
|
ActionIcon,
|
||||||
|
Checkbox,
|
||||||
|
Menu,
|
||||||
|
} from "@mantine/core";
|
||||||
|
import { IconDots } from "@tabler/icons-react";
|
||||||
import { CustomAvatar } from "@/components/ui/custom-avatar.tsx";
|
import { CustomAvatar } from "@/components/ui/custom-avatar.tsx";
|
||||||
import { formattedDate } from "@/lib/time";
|
import { formattedDate } from "@/lib/time";
|
||||||
import classes from "./css/history.module.css";
|
import classes from "./css/history.module.css";
|
||||||
import clsx from "clsx";
|
import clsx from "clsx";
|
||||||
import { IPageHistory } from "@/features/page-history/types/page.types";
|
import { IPageHistory } from "@/features/page-history/types/page.types";
|
||||||
import { memo, useCallback } from "react";
|
import { memo, useCallback } from "react";
|
||||||
|
import { useTranslation } from "react-i18next";
|
||||||
|
|
||||||
const MAX_VISIBLE_AVATARS = 5;
|
const MAX_VISIBLE_AVATARS = 5;
|
||||||
|
|
||||||
@@ -15,6 +26,13 @@ interface HistoryItemProps {
|
|||||||
onHover?: (id: string, index: number) => void;
|
onHover?: (id: string, index: number) => void;
|
||||||
onHoverEnd?: () => void;
|
onHoverEnd?: () => void;
|
||||||
isActive: boolean;
|
isActive: boolean;
|
||||||
|
compareMode: boolean;
|
||||||
|
isChecked: boolean;
|
||||||
|
isCheckboxDisabled: boolean;
|
||||||
|
canCompare: boolean;
|
||||||
|
onToggleCompare: (id: string) => void;
|
||||||
|
onStartCompare: (id: string) => void;
|
||||||
|
onRestore?: (id: string, index: number) => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
const HistoryItem = memo(function HistoryItem({
|
const HistoryItem = memo(function HistoryItem({
|
||||||
@@ -24,10 +42,24 @@ const HistoryItem = memo(function HistoryItem({
|
|||||||
onHover,
|
onHover,
|
||||||
onHoverEnd,
|
onHoverEnd,
|
||||||
isActive,
|
isActive,
|
||||||
|
compareMode,
|
||||||
|
isChecked,
|
||||||
|
isCheckboxDisabled,
|
||||||
|
canCompare,
|
||||||
|
onToggleCompare,
|
||||||
|
onStartCompare,
|
||||||
|
onRestore,
|
||||||
}: HistoryItemProps) {
|
}: HistoryItemProps) {
|
||||||
|
const { t } = useTranslation();
|
||||||
|
const date = formattedDate(new Date(historyItem.createdAt));
|
||||||
|
|
||||||
const handleClick = useCallback(() => {
|
const handleClick = useCallback(() => {
|
||||||
onSelect(historyItem.id, index);
|
if (compareMode) {
|
||||||
}, [onSelect, historyItem.id, index]);
|
onToggleCompare(historyItem.id);
|
||||||
|
} else {
|
||||||
|
onSelect(historyItem.id, index);
|
||||||
|
}
|
||||||
|
}, [compareMode, onToggleCompare, onSelect, historyItem.id, index]);
|
||||||
|
|
||||||
const handleMouseEnter = useCallback(() => {
|
const handleMouseEnter = useCallback(() => {
|
||||||
onHover?.(historyItem.id, index);
|
onHover?.(historyItem.id, index);
|
||||||
@@ -37,63 +69,115 @@ const HistoryItem = memo(function HistoryItem({
|
|||||||
const hasContributors = contributors && contributors.length > 0;
|
const hasContributors = contributors && contributors.length > 0;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<UnstyledButton
|
<div
|
||||||
p="xs"
|
className={clsx(classes.history, { [classes.active]: isActive })}
|
||||||
onClick={handleClick}
|
|
||||||
onMouseEnter={handleMouseEnter}
|
onMouseEnter={handleMouseEnter}
|
||||||
onMouseLeave={onHoverEnd}
|
onMouseLeave={onHoverEnd}
|
||||||
className={clsx(classes.history, { [classes.active]: isActive })}
|
|
||||||
>
|
>
|
||||||
<Text size="sm">{formattedDate(new Date(historyItem.createdAt))}</Text>
|
{compareMode && (
|
||||||
|
<Checkbox
|
||||||
|
size="xs"
|
||||||
|
className={classes.compareCheckbox}
|
||||||
|
checked={isChecked}
|
||||||
|
disabled={isCheckboxDisabled}
|
||||||
|
onChange={() => onToggleCompare(historyItem.id)}
|
||||||
|
aria-label={t("Select version from {{date}}", { date })}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
|
||||||
<Group gap={6} wrap="nowrap" mt={4}>
|
<UnstyledButton
|
||||||
{hasContributors ? (
|
p="xs"
|
||||||
<>
|
onClick={handleClick}
|
||||||
<Tooltip.Group openDelay={300} closeDelay={100}>
|
className={classes.historyButton}
|
||||||
<Avatar.Group spacing={8}>
|
>
|
||||||
{contributors.slice(0, MAX_VISIBLE_AVATARS).map((contributor) => (
|
<Text size="sm">{date}</Text>
|
||||||
<Tooltip key={contributor.id} label={contributor.name} withArrow>
|
|
||||||
<CustomAvatar
|
<Group gap={6} wrap="nowrap" mt={4}>
|
||||||
size="sm"
|
{hasContributors ? (
|
||||||
avatarUrl={contributor.avatarUrl}
|
<>
|
||||||
name={contributor.name}
|
<Tooltip.Group openDelay={300} closeDelay={100}>
|
||||||
/>
|
<Avatar.Group spacing={8}>
|
||||||
</Tooltip>
|
{contributors
|
||||||
))}
|
.slice(0, MAX_VISIBLE_AVATARS)
|
||||||
{contributors.length > MAX_VISIBLE_AVATARS && (
|
.map((contributor) => (
|
||||||
<Tooltip
|
<Tooltip
|
||||||
withArrow
|
key={contributor.id}
|
||||||
label={contributors.slice(MAX_VISIBLE_AVATARS).map((c) => (
|
label={contributor.name}
|
||||||
<div key={c.id}>{c.name}</div>
|
withArrow
|
||||||
|
>
|
||||||
|
<CustomAvatar
|
||||||
|
size="sm"
|
||||||
|
avatarUrl={contributor.avatarUrl}
|
||||||
|
name={contributor.name}
|
||||||
|
/>
|
||||||
|
</Tooltip>
|
||||||
))}
|
))}
|
||||||
>
|
{contributors.length > MAX_VISIBLE_AVATARS && (
|
||||||
<Avatar size="sm" color="gray">
|
<Tooltip
|
||||||
+{contributors.length - MAX_VISIBLE_AVATARS}
|
withArrow
|
||||||
</Avatar>
|
label={contributors
|
||||||
</Tooltip>
|
.slice(MAX_VISIBLE_AVATARS)
|
||||||
)}
|
.map((c) => (
|
||||||
</Avatar.Group>
|
<div key={c.id}>{c.name}</div>
|
||||||
</Tooltip.Group>
|
))}
|
||||||
{contributors.length === 1 && (
|
>
|
||||||
|
<Avatar size="sm" color="gray">
|
||||||
|
+{contributors.length - MAX_VISIBLE_AVATARS}
|
||||||
|
</Avatar>
|
||||||
|
</Tooltip>
|
||||||
|
)}
|
||||||
|
</Avatar.Group>
|
||||||
|
</Tooltip.Group>
|
||||||
|
{contributors.length === 1 && (
|
||||||
|
<Text size="sm" c="dimmed" lineClamp={1}>
|
||||||
|
{contributors[0].name}
|
||||||
|
</Text>
|
||||||
|
)}
|
||||||
|
</>
|
||||||
|
) : (
|
||||||
|
<>
|
||||||
|
<CustomAvatar
|
||||||
|
size="sm"
|
||||||
|
avatarUrl={historyItem.lastUpdatedBy?.avatarUrl}
|
||||||
|
name={historyItem.lastUpdatedBy?.name}
|
||||||
|
/>
|
||||||
<Text size="sm" c="dimmed" lineClamp={1}>
|
<Text size="sm" c="dimmed" lineClamp={1}>
|
||||||
{contributors[0].name}
|
{historyItem.lastUpdatedBy?.name}
|
||||||
</Text>
|
</Text>
|
||||||
|
</>
|
||||||
|
)}
|
||||||
|
</Group>
|
||||||
|
</UnstyledButton>
|
||||||
|
|
||||||
|
{!compareMode && (
|
||||||
|
<Menu shadow="md" width={180} position="bottom-end">
|
||||||
|
<Menu.Target>
|
||||||
|
<ActionIcon
|
||||||
|
variant="subtle"
|
||||||
|
color="gray"
|
||||||
|
className={classes.itemMenu}
|
||||||
|
aria-label={t("Version actions for {{date}}", { date })}
|
||||||
|
onClick={(e) => e.stopPropagation()}
|
||||||
|
>
|
||||||
|
<IconDots size={18} />
|
||||||
|
</ActionIcon>
|
||||||
|
</Menu.Target>
|
||||||
|
<Menu.Dropdown>
|
||||||
|
<Menu.Item
|
||||||
|
disabled={!canCompare}
|
||||||
|
onClick={() => onStartCompare(historyItem.id)}
|
||||||
|
>
|
||||||
|
{t("Compare")}
|
||||||
|
</Menu.Item>
|
||||||
|
{onRestore && (
|
||||||
|
<Menu.Item onClick={() => onRestore(historyItem.id, index)}>
|
||||||
|
{t("Restore")}
|
||||||
|
</Menu.Item>
|
||||||
)}
|
)}
|
||||||
</>
|
</Menu.Dropdown>
|
||||||
) : (
|
</Menu>
|
||||||
<>
|
)}
|
||||||
<CustomAvatar
|
</div>
|
||||||
size="sm"
|
|
||||||
avatarUrl={historyItem.lastUpdatedBy?.avatarUrl}
|
|
||||||
name={historyItem.lastUpdatedBy?.name}
|
|
||||||
/>
|
|
||||||
<Text size="sm" c="dimmed" lineClamp={1}>
|
|
||||||
{historyItem.lastUpdatedBy?.name}
|
|
||||||
</Text>
|
|
||||||
</>
|
|
||||||
)}
|
|
||||||
</Group>
|
|
||||||
</UnstyledButton>
|
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -6,8 +6,12 @@ import HistoryItem from "@/features/page-history/components/history-item";
|
|||||||
import {
|
import {
|
||||||
activeHistoryIdAtom,
|
activeHistoryIdAtom,
|
||||||
activeHistoryPrevIdAtom,
|
activeHistoryPrevIdAtom,
|
||||||
|
compareModeAtom,
|
||||||
|
comparePairAtom,
|
||||||
|
compareSelectionAtom,
|
||||||
historyAtoms,
|
historyAtoms,
|
||||||
} from "@/features/page-history/atoms/history-atoms";
|
} from "@/features/page-history/atoms/history-atoms";
|
||||||
|
import { resolveComparePair } from "@/features/page-history/utils/resolve-compare-pair";
|
||||||
import { useAtom, useSetAtom } from "jotai";
|
import { useAtom, useSetAtom } from "jotai";
|
||||||
import { useCallback, useEffect, useMemo, useRef } from "react";
|
import { useCallback, useEffect, useMemo, useRef } from "react";
|
||||||
import {
|
import {
|
||||||
@@ -32,6 +36,10 @@ function HistoryList({ pageId }: Props) {
|
|||||||
const [activeHistoryId, setActiveHistoryId] = useAtom(activeHistoryIdAtom);
|
const [activeHistoryId, setActiveHistoryId] = useAtom(activeHistoryIdAtom);
|
||||||
const setActiveHistoryPrevId = useSetAtom(activeHistoryPrevIdAtom);
|
const setActiveHistoryPrevId = useSetAtom(activeHistoryPrevIdAtom);
|
||||||
const setHistoryModalOpen = useSetAtom(historyAtoms);
|
const setHistoryModalOpen = useSetAtom(historyAtoms);
|
||||||
|
const [compareMode, setCompareMode] = useAtom(compareModeAtom);
|
||||||
|
const [compareSelection, setCompareSelection] = useAtom(compareSelectionAtom);
|
||||||
|
// @ts-ignore
|
||||||
|
const setComparePair = useSetAtom(comparePairAtom);
|
||||||
|
|
||||||
const {
|
const {
|
||||||
data: pageHistoryData,
|
data: pageHistoryData,
|
||||||
@@ -79,10 +87,60 @@ function HistoryList({ pageId }: Props) {
|
|||||||
|
|
||||||
const handleSelect = useCallback(
|
const handleSelect = useCallback(
|
||||||
(id: string, index: number) => {
|
(id: string, index: number) => {
|
||||||
|
// @ts-ignore
|
||||||
|
setComparePair(null);
|
||||||
setActiveHistoryId(id);
|
setActiveHistoryId(id);
|
||||||
setActiveHistoryPrevId(historyItems[index + 1]?.id ?? "");
|
setActiveHistoryPrevId(historyItems[index + 1]?.id ?? "");
|
||||||
},
|
},
|
||||||
[historyItems, setActiveHistoryId, setActiveHistoryPrevId],
|
[historyItems, setActiveHistoryId, setActiveHistoryPrevId, setComparePair],
|
||||||
|
);
|
||||||
|
|
||||||
|
const handleToggleCompare = useCallback(
|
||||||
|
(id: string) => {
|
||||||
|
setCompareSelection((prev) => {
|
||||||
|
if (prev.includes(id)) return prev.filter((item) => item !== id);
|
||||||
|
if (prev.length >= 2) return prev;
|
||||||
|
return [...prev, id];
|
||||||
|
});
|
||||||
|
},
|
||||||
|
[setCompareSelection],
|
||||||
|
);
|
||||||
|
|
||||||
|
const handleStartCompare = useCallback(
|
||||||
|
(id: string) => {
|
||||||
|
// @ts-ignore
|
||||||
|
setComparePair(null);
|
||||||
|
setCompareMode(true);
|
||||||
|
setCompareSelection([id]);
|
||||||
|
},
|
||||||
|
[setComparePair, setCompareMode, setCompareSelection],
|
||||||
|
);
|
||||||
|
|
||||||
|
const handleCancelCompare = useCallback(() => {
|
||||||
|
setCompareMode(false);
|
||||||
|
setCompareSelection([]);
|
||||||
|
}, [setCompareMode, setCompareSelection]);
|
||||||
|
|
||||||
|
const handleConfirmCompare = useCallback(() => {
|
||||||
|
const pair = resolveComparePair(historyItems, compareSelection);
|
||||||
|
if (!pair) return;
|
||||||
|
setComparePair(pair);
|
||||||
|
setCompareMode(false);
|
||||||
|
setCompareSelection([]);
|
||||||
|
}, [
|
||||||
|
historyItems,
|
||||||
|
compareSelection,
|
||||||
|
setComparePair,
|
||||||
|
setCompareMode,
|
||||||
|
setCompareSelection,
|
||||||
|
]);
|
||||||
|
|
||||||
|
const handleRestoreItem = useCallback(
|
||||||
|
(id: string, index: number) => {
|
||||||
|
handleSelect(id, index);
|
||||||
|
confirmRestore(id);
|
||||||
|
},
|
||||||
|
[handleSelect, confirmRestore],
|
||||||
);
|
);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
@@ -138,6 +196,16 @@ function HistoryList({ pageId }: Props) {
|
|||||||
onHover={handleHover}
|
onHover={handleHover}
|
||||||
onHoverEnd={clearPrefetchTimeout}
|
onHoverEnd={clearPrefetchTimeout}
|
||||||
isActive={historyItem.id === activeHistoryId}
|
isActive={historyItem.id === activeHistoryId}
|
||||||
|
compareMode={compareMode}
|
||||||
|
isChecked={compareSelection.includes(historyItem.id)}
|
||||||
|
isCheckboxDisabled={
|
||||||
|
!compareSelection.includes(historyItem.id) &&
|
||||||
|
compareSelection.length >= 2
|
||||||
|
}
|
||||||
|
canCompare={historyItems.length >= 2}
|
||||||
|
onToggleCompare={handleToggleCompare}
|
||||||
|
onStartCompare={handleStartCompare}
|
||||||
|
onRestore={canRestore ? handleRestoreItem : undefined}
|
||||||
/>
|
/>
|
||||||
))}
|
))}
|
||||||
{hasNextPage && <div ref={loadMoreRef} style={{ height: 1 }} />}
|
{hasNextPage && <div ref={loadMoreRef} style={{ height: 1 }} />}
|
||||||
@@ -148,22 +216,44 @@ function HistoryList({ pageId }: Props) {
|
|||||||
)}
|
)}
|
||||||
</ScrollArea>
|
</ScrollArea>
|
||||||
|
|
||||||
{canRestore && (
|
{compareMode ? (
|
||||||
<>
|
<>
|
||||||
<Divider />
|
<Divider />
|
||||||
<Group p="xs" wrap="nowrap">
|
<Group p="xs" wrap="nowrap">
|
||||||
<Button
|
<Button
|
||||||
variant="default"
|
variant="default"
|
||||||
size="compact-md"
|
size="compact-md"
|
||||||
onClick={() => setHistoryModalOpen(false)}
|
onClick={handleCancelCompare}
|
||||||
>
|
>
|
||||||
{t("Cancel")}
|
{t("Cancel")}
|
||||||
</Button>
|
</Button>
|
||||||
<Button size="compact-md" onClick={() => confirmRestore()}>
|
<Button
|
||||||
{t("Restore")}
|
size="compact-md"
|
||||||
|
disabled={compareSelection.length !== 2}
|
||||||
|
onClick={handleConfirmCompare}
|
||||||
|
>
|
||||||
|
{t("Compare versions")}
|
||||||
</Button>
|
</Button>
|
||||||
</Group>
|
</Group>
|
||||||
</>
|
</>
|
||||||
|
) : (
|
||||||
|
canRestore && (
|
||||||
|
<>
|
||||||
|
<Divider />
|
||||||
|
<Group p="xs" wrap="nowrap">
|
||||||
|
<Button
|
||||||
|
variant="default"
|
||||||
|
size="compact-md"
|
||||||
|
onClick={() => setHistoryModalOpen(false)}
|
||||||
|
>
|
||||||
|
{t("Cancel")}
|
||||||
|
</Button>
|
||||||
|
<Button size="compact-md" onClick={() => confirmRestore()}>
|
||||||
|
{t("Restore")}
|
||||||
|
</Button>
|
||||||
|
</Group>
|
||||||
|
</>
|
||||||
|
)
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
|
|||||||
Reference in New Issue
Block a user