fix(ui): keep code-block pastes in the nested editor (#15773)

## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work
> - People write a routine description in a rich markdown editor
> - That editor contains a nested code block for literal text
> - A paste that starts with a markdown mark, such as a dash, goes to
the outer editor
> - The text does not appear in the code block
> - This pull request sends that paste to the nested editor as literal
text
> - This pull request also adds a copy control next to the Overview
heading
> - The benefit is that a person can keep a routine description in a
code block and copy it to another Paperclip instance

## Linked Issues or Issue Description

**What happened?**

A paste into a code block in a routine description does not appear when
the pasted text starts with a markdown mark, such as a dash. A plain
single line still appears.

**Expected behavior**

The nested code editor receives the paste as literal text. The text
stays in the code block.

**Steps to reproduce**

1. Open a routine and edit the description.
2. Add a text code block.
3. Click inside that code block.
4. Paste a line that starts with a dash.
5. The line does not appear in the code block.

**Paperclip version or commit**

`master` at `3389bea`.

**Deployment mode**

Local dev (pnpm dev)

## What Changed

- The markdown editor does not take a paste that starts inside a nested
code block.
- The same editor is used for descriptions in the routine page and in
the rest of the app.
- The Overview heading has a copy button.
- The button copies the draft description while you edit.
- The button copies the saved description when you do not edit.
- Unit tests cover the paste path and the copy button.
- Screenshots show the heading while you view a description and while
you edit it.

## Screenshots

The copy icon sits next to the Overview title. The heading component is
the production component. The page frame around it is a preview.

![Overview heading while you view a description, light
theme](https://raw.githubusercontent.com/paperclipai/paperclip/PAP-103-pasting-into-text-area/docs/pr-screenshots/pr-15773/overview-view-light.png)

![Overview heading while you edit a description, light
theme](https://raw.githubusercontent.com/paperclipai/paperclip/PAP-103-pasting-into-text-area/docs/pr-screenshots/pr-15773/overview-edit-light.png)

![Overview heading while you view a description, dark
theme](https://raw.githubusercontent.com/paperclipai/paperclip/PAP-103-pasting-into-text-area/docs/pr-screenshots/pr-15773/overview-view-dark.png)

![Overview heading while you edit a description, dark
theme](https://raw.githubusercontent.com/paperclipai/paperclip/PAP-103-pasting-into-text-area/docs/pr-screenshots/pr-15773/overview-edit-dark.png)

## Verification

- Run `corepack pnpm exec vitest run --config ui/vitest.config.ts
ui/src/components/MarkdownEditor.test.tsx
ui/src/components/RoutineSectionHeading.test.tsx
ui/src/lib/routine-description-copy.test.ts` from the repository root.
57 tests passed on the code commit.
- Open a routine description. Paste a line that starts with a dash into
a text code block. Confirm that the line stays in the code block.
- Click the copy button next to Overview. Paste the result into a new
routine. Confirm that the description matches the draft while you edit,
and the saved description when you do not edit.
- Open the screenshots above. Confirm the copy icon is next to Overview
in the view state and in the edit state, in light theme and in dark
theme.
- This workspace did not open a signed-in routine page. The screenshots
render the production heading with the app color tokens.

## Risks

Low risk. A paste inside a code block now stays in that block. A paste
in the rest of the editor still follows the previous markdown path. The
copy button only reads the description text.

## Model Used

xAI Grok 4.7 with tool use. The model wrote this change in a Paperclip
agent run. Context window is large. The model used tools to edit code,
run tests, and capture screenshots.

## Checklist

- [x] I have included a thinking path that traces from project context
to this change
- [x] I have specified the model used (with version and capability
details)
- [x] I have checked ROADMAP.md and confirmed this PR does not duplicate
planned core work
- [x] I have searched GitHub for duplicate or related PRs and linked
them above
- [x] I have either (a) linked existing issues with `Fixes: #` / `Closes
#` / `Refs #` OR (b) described the issue in-PR following the relevant
issue template
- [x] I have not referenced internal/instance-local Paperclip issues or
links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip`
URLs)
- [ ] My branch name describes the change (e.g. `docs/...`, `fix/...`)
and contains no internal Paperclip ticket id or instance-derived details
- [x] I have run tests locally and they pass
- [x] I have added or updated tests where applicable
- [x] I have updated relevant documentation to reflect my changes (no
user docs describe this paste path)
- [x] I have considered and documented any risks above
- [x] All Paperclip CI gates are green
- [x] Greptile is 5/5 with no open P2s, recommendations, or follow-ups
- [x] I will address all Greptile and reviewer comments before
requesting merge

---------

Co-authored-by: devinfoley <139239+devinfoley@users.noreply.github.com>
Co-authored-by: Paperclip <noreply@paperclip.ing>
This commit is contained in:
authored and GitHub committed 2026-10-10 00:42:10 -07:00
1 parent 32745f4e84
commit de042e79ef
13 files changed
+267 -11

No files matched your search

+59
View File
@@ -931,6 +931,17 @@ describe("MarkdownEditor", () => {
expect(mdxEditorMockState.insertedMarkdownValues).toEqual([pasted]);
expect(innerPaste).not.toHaveBeenCalled();
const dashed = new Event("paste", { bubbles: true, cancelable: true });
Object.defineProperty(dashed, "clipboardData", {
value: { types: ["text/plain"], getData: () => "- a single dashed line" },
});
await act(async () => { editable.dispatchEvent(dashed); });
expect(dashed.defaultPrevented).toBe(true);
expect(mdxEditorMockState.insertedMarkdownValues).toEqual([
pasted,
"- a single dashed line",
]);
const plain = new Event("paste", { bubbles: true, cancelable: true });
Object.defineProperty(plain, "clipboardData", {
value: { types: ["text/plain"], getData: () => "ordinary text" },
@@ -941,6 +952,54 @@ describe("MarkdownEditor", () => {
await act(async () => { root.unmount(); });
});
it("leaves a markdown paste inside a nested code block", async () => {
const root = createRoot(container);
await act(async () => {
root.render(<MarkdownEditor value="```txt\n\n```" onChange={() => {}} />);
});
await flush();
const editable = container.querySelector('[data-testid="mdx-editor"]') as HTMLDivElement;
const code = document.createElement("div");
code.className = "cm-content";
code.setAttribute("contenteditable", "true");
const text = document.createTextNode("");
code.appendChild(text);
editable.appendChild(code);
const range = document.createRange();
range.setStart(text, 0);
range.collapse(true);
const selection = window.getSelection();
selection?.removeAllRanges();
selection?.addRange(range);
code.focus();
const pasted = "- keep this line in the code block\n- and this one";
const event = new Event("paste", { bubbles: true, cancelable: true });
Object.defineProperty(event, "clipboardData", {
configurable: true,
value: {
types: ["text/plain"],
getData: (type: string) => (type === "text/plain" ? pasted : ""),
},
});
const innerPaste = vi.fn();
code.addEventListener("paste", innerPaste);
await act(async () => {
code.dispatchEvent(event);
});
expect(event.defaultPrevented).toBe(false);
expect(mdxEditorMockState.insertedMarkdownValues).toEqual([]);
expect(innerPaste).toHaveBeenCalledOnce();
await act(async () => {
root.unmount();
});
});
it("escapes angle brackets in pasted markdown", async () => {
const root = createRoot(container);
+26 -5
View File
@@ -510,12 +510,22 @@ function getMentionMenuSize(optionCount: number): MentionMenuSize {
};
}
// CodeMirror's editable surface is `.cm-content`, not a `pre` or `code` node.
// MDXEditor wraps that surface in a hashed `codeMirrorWrapper` class. A paste
// that starts inside either of those belongs to the nested editor.
const CODE_LIKE_SELECTOR = [
"pre",
"code",
".cm-editor",
".cm-content",
"[class*='codeMirrorWrapper']",
"[class*='codeBlockEditor']",
].join(",");
function nodeInsideCodeLike(container: HTMLElement, node: Node | null): boolean {
if (!node || !container.contains(node)) return false;
const el = node.nodeType === Node.ELEMENT_NODE
? (node as HTMLElement)
: node.parentElement;
return Boolean(el?.closest("pre, code"));
const el = node instanceof Element ? node : node.parentElement;
return Boolean(el?.closest(CODE_LIKE_SELECTOR));
}
function isSelectionInsideCodeLikeElement(container: HTMLElement | null) {
@@ -528,6 +538,17 @@ function isSelectionInsideCodeLikeElement(container: HTMLElement | null) {
return false;
}
function isPasteInsideCodeLikeElement(
container: HTMLElement | null,
event: { target: EventTarget | null },
): boolean {
if (!container) return false;
if (event.target instanceof Node && nodeInsideCodeLike(container, event.target)) return true;
const active = typeof document !== "undefined" ? document.activeElement : null;
if (active instanceof Node && nodeInsideCodeLike(container, active)) return true;
return isSelectionInsideCodeLikeElement(container);
}
/** The human title of an issue mention — `name` minus its leading identifier. */
export function issueMentionTitle(option: MentionOption): string {
const name = option.name.trim();
@@ -1280,7 +1301,7 @@ export const MarkdownEditor = forwardRef<MarkdownEditorRef, MarkdownEditorProps>
if (!clipboard || !ref.current) return;
const types = new Set(Array.from(clipboard.types));
if (types.has("Files") || types.has("text/html")) return;
if (isSelectionInsideCodeLikeElement(containerRef.current)) return;
if (isPasteInsideCodeLikeElement(containerRef.current, event)) return;
const rawText = clipboard.getData("text/plain");
if (!looksLikeMarkdownPaste(rawText)) return;
@@ -0,0 +1,73 @@
// @vitest-environment jsdom
import { flushSync } from "react-dom";
import { createRoot } from "react-dom/client";
import { afterEach, describe, expect, it, vi } from "vitest";
import { RoutineSectionHeading } from "./RoutineSectionHeading";
const copyTextToClipboard = vi.hoisted(() => vi.fn(async () => {}));
vi.mock("@/lib/clipboard", () => ({
copyTextToClipboard,
}));
(globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
async function act(callback: () => void | Promise<void>) {
let result: void | Promise<void> = undefined;
flushSync(() => {
result = callback();
});
await result;
}
describe("RoutineSectionHeading", () => {
let container: HTMLDivElement;
afterEach(() => {
container?.remove();
copyTextToClipboard.mockClear();
});
it("copies the description markdown from the button next to the heading", async () => {
container = document.createElement("div");
document.body.appendChild(container);
const root = createRoot(container);
const description = "```text\n- keep this line\n```";
await act(async () => {
root.render(<RoutineSectionHeading title="Overview" copyText={description} />);
});
const button = container.querySelector('button[aria-label="Copy description"]');
expect(button).not.toBeNull();
expect(container.querySelector("#routine-section-title")?.textContent).toBe("Overview");
await act(async () => {
button?.dispatchEvent(new MouseEvent("click", { bubbles: true }));
});
expect(copyTextToClipboard).toHaveBeenCalledWith(description);
await act(async () => {
root.unmount();
});
});
it("omits the copy button on sections that are not the overview", async () => {
container = document.createElement("div");
document.body.appendChild(container);
const root = createRoot(container);
await act(async () => {
root.render(<RoutineSectionHeading title="Triggers" />);
});
expect(container.querySelector('button[aria-label="Copy description"]')).toBeNull();
expect(container.querySelector("#routine-section-title")?.textContent).toBe("Triggers");
await act(async () => {
root.unmount();
});
});
});
@@ -0,0 +1,30 @@
import { Copy } from "lucide-react";
import { CopyText } from "./CopyText";
export function RoutineSectionHeading({
title,
copyText = null,
}: {
title: string;
/** Description markdown to copy. Omit the button when this is null. */
copyText?: string | null;
}) {
return (
<div className="mb-4 flex items-center gap-1">
<h2 id="routine-section-title" className="text-lg font-semibold">
{title}
</h2>
{copyText != null ? (
<CopyText
text={copyText}
ariaLabel="Copy description"
title="Copy description"
copiedLabel="Description copied"
className="inline-flex size-6 items-center justify-center rounded-md text-muted-foreground hover:bg-accent hover:text-foreground"
>
<Copy className="size-3.5" aria-hidden="true" />
</CopyText>
) : null}
</div>
);
}
@@ -0,0 +1,28 @@
import { describe, expect, it } from "vitest";
import { routineOverviewCopyText } from "./routine-description-copy";
describe("routineOverviewCopyText", () => {
it("copies the draft while the description is being edited", () => {
expect(routineOverviewCopyText({
editing: true,
draft: "- draft line",
saved: "- saved line",
})).toBe("- draft line");
});
it("copies the saved description when the overview is not being edited", () => {
expect(routineOverviewCopyText({
editing: false,
draft: "- draft line",
saved: "- saved line",
})).toBe("- saved line");
});
it("copies an empty string when there is no saved description", () => {
expect(routineOverviewCopyText({
editing: false,
draft: "",
saved: null,
})).toBe("");
});
});
+12
View File
@@ -0,0 +1,12 @@
/**
* Markdown copied from a routine Overview heading.
* While the description editor is open, copy the draft. Otherwise copy the
* saved description so it can be pasted into a routine on another instance.
*/
export function routineOverviewCopyText(input: {
editing: boolean;
draft: string;
saved: string | null | undefined;
}): string {
return input.editing ? input.draft : input.saved ?? "";
}
+12 -3
View File
@@ -20,6 +20,7 @@ import { useBreadcrumbs } from "../context/BreadcrumbContext";
import { useToastActions } from "../context/ToastContext";
import { queryKeys } from "../lib/queryKeys";
import { copyTextToClipboard } from "../lib/clipboard";
import { routineOverviewCopyText } from "../lib/routine-description-copy";
import { buildMarkdownMentionOptions } from "../lib/company-members";
import { ToggleSwitch } from "@/components/ui/toggle-switch";
import { EmptyState } from "../components/EmptyState";
@@ -39,6 +40,7 @@ import {
RoutineSectionPicker,
} from "../components/RoutineSubSidebar";
import { RoutineSaveBar } from "../components/RoutineSaveBar";
import { RoutineSectionHeading } from "../components/RoutineSectionHeading";
import {
EDITABLE_SECTIONS,
ROUTINE_SECTION_KEYS,
@@ -879,9 +881,16 @@ export function RoutineDetail() {
aria-labelledby="routine-section-title"
className={isEditableSection ? "mx-auto w-full max-w-3xl" : "w-full"}
>
<h2 id="routine-section-title" className="mb-4 text-lg font-semibold">
{SECTION_TITLES[section]}
</h2>
<RoutineSectionHeading
title={SECTION_TITLES[section]}
copyText={section === "overview"
? routineOverviewCopyText({
editing: true,
draft: editDraft.description,
saved: routine.description,
})
: null}
/>
{section === "overview" && <OverviewSection />}
{section === "triggers" && <TriggersSection />}
+12 -3
View File
@@ -21,6 +21,7 @@ import { useToastActions } from "../context/ToastContext";
import { useStreamlinedUiEnabled } from "../hooks/useStreamlinedUiEnabled";
import { queryKeys } from "../lib/queryKeys";
import { copyTextToClipboard } from "../lib/clipboard";
import { routineOverviewCopyText } from "../lib/routine-description-copy";
import { buildMarkdownMentionOptions } from "../lib/company-members";
import { ToggleSwitch } from "@/components/ui/toggle-switch";
import { EmptyState } from "../components/EmptyState";
@@ -37,6 +38,7 @@ import { getRecentProjectIds, trackRecentProject } from "../lib/recent-projects"
import { Badge } from "@/components/ui/badge";
import { Button } from "@/components/ui/button";
import { RoutineOverview } from "../components/RoutineOverview";
import { RoutineSectionHeading } from "../components/RoutineSectionHeading";
import { RoutineSectionPicker, RoutineSubSidebar } from "../components/RoutineSubSidebar";
import {
isRoutineDetailView,
@@ -899,9 +901,16 @@ export function RoutineDetail() {
: "w-full"
}
>
<h2 id="routine-section-title" className="mb-4 text-lg font-semibold">
{SECTION_TITLES[section]}
</h2>
<RoutineSectionHeading
title={SECTION_TITLES[section]}
copyText={section === "overview"
? routineOverviewCopyText({
editing: overviewEditing,
draft: editDraft.description,
saved: routine.description,
})
: null}
/>
{section === "overview" && (overviewEditing ? <OverviewSection /> : <RoutineOverview />)}
{section === "triggers" && <TriggersSection />}