Compare commits
2 Commits
feat/273-t
...
feat/275-c
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ba87f4ee24 | ||
|
|
5280392fc4 |
@@ -0,0 +1,68 @@
|
||||
import { describe, it, expect, vi } from "vitest";
|
||||
import { render } from "@testing-library/react";
|
||||
|
||||
// Covers the read-only render branch (PR #278): the language <Select> renders
|
||||
// only when `editor.isEditable`; in read-only the copy button still shows.
|
||||
// Mocks mirror the #146 structural harness (footnote-views.structure.test.tsx),
|
||||
// except Select becomes a detectable node so we can assert its presence/absence.
|
||||
vi.mock("@tiptap/react", () => ({
|
||||
NodeViewWrapper: ({ children }: any) => <div>{children}</div>,
|
||||
NodeViewContent: (props: any) => <div data-node-view-content="" {...props} />,
|
||||
}));
|
||||
vi.mock("react-i18next", () => ({
|
||||
useTranslation: () => ({ t: (key: string) => key }),
|
||||
}));
|
||||
vi.mock("@mantine/core", () => ({
|
||||
Group: ({ children }: any) => <div>{children}</div>,
|
||||
Select: () => <div data-testid="language-select" />,
|
||||
Tooltip: ({ children }: any) => <>{children}</>,
|
||||
ActionIcon: ({ children, onClick }: any) => (
|
||||
<button data-testid="copy-button" onClick={onClick}>
|
||||
{children}
|
||||
</button>
|
||||
),
|
||||
}));
|
||||
vi.mock("@/components/common/copy-button", () => ({
|
||||
CopyButton: ({ children }: any) => children({ copied: false, copy: () => {} }),
|
||||
}));
|
||||
vi.mock("@tabler/icons-react", () => ({
|
||||
IconCheck: () => null,
|
||||
IconCopy: () => null,
|
||||
}));
|
||||
vi.mock("@/features/editor/components/code-block/mermaid-view.tsx", () => ({
|
||||
default: () => null,
|
||||
}));
|
||||
|
||||
import CodeBlockView from "./code-block-view";
|
||||
|
||||
const makeProps = (isEditable: boolean) =>
|
||||
({
|
||||
node: { attrs: { language: "javascript" }, textContent: "", nodeSize: 1 },
|
||||
editor: {
|
||||
state: { selection: { from: 0, to: 0 } },
|
||||
isEditable,
|
||||
commands: {},
|
||||
on: vi.fn(),
|
||||
off: vi.fn(),
|
||||
},
|
||||
extension: {
|
||||
options: { lowlight: { listLanguages: () => ["javascript", "python"] } },
|
||||
},
|
||||
getPos: () => 0,
|
||||
updateAttributes: () => {},
|
||||
deleteNode: () => {},
|
||||
}) as any;
|
||||
|
||||
describe("CodeBlockView language selector visibility (#278)", () => {
|
||||
it("renders the language selector when the editor is editable", () => {
|
||||
const { queryByTestId } = render(<CodeBlockView {...makeProps(true)} />);
|
||||
expect(queryByTestId("language-select")).not.toBeNull();
|
||||
expect(queryByTestId("copy-button")).not.toBeNull();
|
||||
});
|
||||
|
||||
it("hides the language selector in read-only but keeps the copy button", () => {
|
||||
const { queryByTestId } = render(<CodeBlockView {...makeProps(false)} />);
|
||||
expect(queryByTestId("language-select")).toBeNull();
|
||||
expect(queryByTestId("copy-button")).not.toBeNull();
|
||||
});
|
||||
});
|
||||
@@ -50,10 +50,10 @@ export default function CodeBlockView(props: NodeViewProps) {
|
||||
{/* #146: the editable <pre><code> (contentDOM) MUST come first in the DOM.
|
||||
With the non-editable menu rendered before it, the browser's click
|
||||
hit-testing snapped the caret up one line. Render content first; the
|
||||
menu is rendered after it and lifted back above visually via flex
|
||||
`order: -1` (the `.codeBlock` wrapper is a flex column — see
|
||||
code-block.module.css). It stays fully in flow as a full-width row
|
||||
above the code: no overlay/absolute positioning. The second #146
|
||||
menu is rendered after it and floated into the top-right corner as an
|
||||
absolute overlay (see `.menuGroup` in code-block.module.css, anchored
|
||||
to the `position: relative` `.codeBlock` wrapper in code.css). It no
|
||||
longer takes a full-width row above the code. The second #146
|
||||
mitigation lives in editor-paste-handler.tsx (reflowAfterPaste). */}
|
||||
<pre
|
||||
spellCheck="false"
|
||||
@@ -67,22 +67,23 @@ export default function CodeBlockView(props: NodeViewProps) {
|
||||
<NodeViewContent as="code" className={`language-${language}`} />
|
||||
</pre>
|
||||
|
||||
<Group
|
||||
justify="flex-end"
|
||||
contentEditable={false}
|
||||
className={classes.menuGroup}
|
||||
>
|
||||
<Select
|
||||
placeholder="auto"
|
||||
checkIconPosition="right"
|
||||
data={extension.options.lowlight.listLanguages().sort()}
|
||||
value={languageValue}
|
||||
onChange={changeLanguage}
|
||||
searchable
|
||||
style={{ maxWidth: "130px" }}
|
||||
classNames={{ input: classes.selectInput }}
|
||||
disabled={!editor.isEditable}
|
||||
/>
|
||||
<Group contentEditable={false} className={classes.menuGroup}>
|
||||
{/* In read-only (published) there is no language selector at all —
|
||||
only the copy button. When editable the selector is hidden until
|
||||
the block is hovered/focused (or its dropdown is open) via the
|
||||
`.languageSelect` class (see code-block.module.css). */}
|
||||
{editor.isEditable && (
|
||||
<Select
|
||||
placeholder="auto"
|
||||
checkIconPosition="right"
|
||||
data={extension.options.lowlight.listLanguages().sort()}
|
||||
value={languageValue}
|
||||
onChange={changeLanguage}
|
||||
searchable
|
||||
style={{ maxWidth: "130px" }}
|
||||
classNames={{ root: classes.languageSelect, input: classes.selectInput }}
|
||||
/>
|
||||
)}
|
||||
|
||||
<CopyButton value={node?.textContent} timeout={2000}>
|
||||
{({ copied, copy }) => (
|
||||
|
||||
@@ -17,15 +17,37 @@
|
||||
justify-content: center;
|
||||
}
|
||||
|
||||
/* #146: the menu now follows the <pre> in the DOM (so the editable contentDOM is
|
||||
FIRST and click hit-testing is correct). Lift it back ABOVE the code visually
|
||||
with flex `order` — the .codeBlock wrapper is a flex column (see code.css) —
|
||||
so the menu still reads as a row above the code, exactly as before, without
|
||||
sitting in-flow before the contentDOM. */
|
||||
/* #146: the menu follows the <pre> in the DOM (so the editable contentDOM is
|
||||
FIRST and click hit-testing is correct). Instead of sitting in-flow, it is
|
||||
floated into the top-right corner as an absolute overlay anchored to the
|
||||
`position: relative` .codeBlock wrapper (see code.css), so it no longer
|
||||
takes a full-width row above the code. The Mantine dropdown is portaled, so
|
||||
it is never clipped by the overlay. */
|
||||
.menuGroup {
|
||||
order: -1;
|
||||
position: absolute;
|
||||
top: 8px;
|
||||
right: 8px;
|
||||
z-index: 1;
|
||||
gap: 4px;
|
||||
|
||||
@media print {
|
||||
display: none;
|
||||
}
|
||||
}
|
||||
|
||||
/* The language selector is hidden until the block is hovered, or the selector
|
||||
itself is focused / its dropdown is open. It keeps its width in the flex
|
||||
Group (only opacity toggles) so the copy button never jumps, and
|
||||
`pointer-events: none` while hidden lets clicks fall through to the code.
|
||||
`.codeBlock` is the global NodeViewWrapper class → use :global(). */
|
||||
.languageSelect {
|
||||
opacity: 0;
|
||||
pointer-events: none;
|
||||
transition: opacity 150ms ease;
|
||||
}
|
||||
|
||||
:global(.codeBlock):hover .languageSelect,
|
||||
.languageSelect:focus-within {
|
||||
opacity: 1;
|
||||
pointer-events: auto;
|
||||
}
|
||||
|
||||
@@ -1,9 +1,12 @@
|
||||
.ProseMirror {
|
||||
.codeBlock {
|
||||
/* #146: flex column so the menu (rendered AFTER <pre> in the DOM, so the
|
||||
editable contentDOM is first) is lifted back above the code via `order`. */
|
||||
/* #146: flex column keeps the editable <pre> (first in the DOM so click
|
||||
hit-testing is correct) laid out above any Mermaid diagram. `position:
|
||||
relative` anchors the control panel, which is floated into the top-right
|
||||
corner as an absolute overlay (see `.menuGroup` in code-block.module.css). */
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
position: relative;
|
||||
padding: 4px;
|
||||
border-radius: var(--mantine-radius-default);
|
||||
background-color: light-dark(var(--mantine-color-gray-0), var(--mantine-color-dark-8));
|
||||
|
||||
@@ -1,10 +1,8 @@
|
||||
import { Button, Group, Paper, Text } from "@mantine/core";
|
||||
import { IconClockHour4, IconTrash } from "@tabler/icons-react";
|
||||
import { useState } from "react";
|
||||
import { IconClockHour4 } from "@tabler/icons-react";
|
||||
import { Trans, useTranslation } from "react-i18next";
|
||||
import { useTimeAgo } from "@/hooks/use-time-ago.tsx";
|
||||
import { usePageQuery } from "@/features/page/queries/page-query.ts";
|
||||
import { useTreeMutation } from "@/features/page/tree/hooks/use-tree-mutation.ts";
|
||||
import {
|
||||
useToggleTemporaryMutation,
|
||||
syncTemporaryExpiresInCache,
|
||||
@@ -33,11 +31,6 @@ export function TemporaryNoteBanner({ slugId }: TemporaryNoteBannerProps) {
|
||||
const spaceAbility = useSpaceAbility(space?.membership?.permissions);
|
||||
const expiresTimeAgo = useTimeAgo(page?.temporaryExpiresAt);
|
||||
const toggleTemporary = useToggleTemporaryMutation();
|
||||
// Reuse the exact soft-delete path the tree/header menus use: optimistic
|
||||
// tree removal, the "Page moved to trash" undo-toast, the deletedAt cache
|
||||
// stamp, and the redirect to space home (which unmounts this banner).
|
||||
const { handleDelete: trashPage } = useTreeMutation(page?.spaceId ?? "");
|
||||
const [isDeleting, setIsDeleting] = useState(false);
|
||||
|
||||
// Don't show on a note that is already in trash; the deleted-page banner
|
||||
// owns that state.
|
||||
@@ -45,16 +38,6 @@ export function TemporaryNoteBanner({ slugId }: TemporaryNoteBannerProps) {
|
||||
|
||||
const canEdit = spaceAbility.can(SpaceCaslAction.Edit, SpaceCaslSubject.Page);
|
||||
|
||||
const handleTrashNow = async () => {
|
||||
// No confirm modal by convention — the undo-toast is the safety net.
|
||||
setIsDeleting(true);
|
||||
try {
|
||||
await trashPage(page.id);
|
||||
} finally {
|
||||
setIsDeleting(false);
|
||||
}
|
||||
};
|
||||
|
||||
const handleMakePermanent = async () => {
|
||||
try {
|
||||
const res = await toggleTemporary.mutateAsync({
|
||||
@@ -87,28 +70,16 @@ export function TemporaryNoteBanner({ slugId }: TemporaryNoteBannerProps) {
|
||||
</Text>
|
||||
</Group>
|
||||
{canEdit && (
|
||||
<Group gap="xs" wrap="nowrap">
|
||||
<Button
|
||||
size="xs"
|
||||
variant="subtle"
|
||||
color="red"
|
||||
leftSection={<IconTrash size={16} />}
|
||||
onClick={handleTrashNow}
|
||||
loading={isDeleting}
|
||||
>
|
||||
{t("Move to trash")}
|
||||
</Button>
|
||||
<Button
|
||||
size="xs"
|
||||
variant="light"
|
||||
color="orange"
|
||||
leftSection={<IconClockHour4 size={16} />}
|
||||
onClick={handleMakePermanent}
|
||||
loading={toggleTemporary.isPending}
|
||||
>
|
||||
{t("Make permanent")}
|
||||
</Button>
|
||||
</Group>
|
||||
<Button
|
||||
size="xs"
|
||||
variant="light"
|
||||
color="orange"
|
||||
leftSection={<IconClockHour4 size={16} />}
|
||||
onClick={handleMakePermanent}
|
||||
loading={toggleTemporary.isPending}
|
||||
>
|
||||
{t("Make permanent")}
|
||||
</Button>
|
||||
)}
|
||||
</Group>
|
||||
</Paper>
|
||||
|
||||
Reference in New Issue
Block a user