Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/architecture.md
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ Paragraphs, headings, quotes, list items and table cells have `dir="auto"`, so e
## Clipboard

- **Copy and cut** write `text/plain` as Markdown and `text/html` as rendered HTML. With a block selection they copy the whole blocks. The HTML carries the Markdown in a `data-mbe-markdown` attribute, so copying between editors is lossless.
- **Paste** tries, in order: the editor's own HTML, other `text/html` (`htmlToDoc`, which reads tags and inline styles, including Google Docs' wrapper), then `text/plain` as Markdown. HTML from a code editor or terminal, which only colors the source (VS Code's has its own `vscode-editor-data` type; others are `<div>`s and `<span>`s with no formatting, see `isStyledText`), is skipped, so Markdown copied from them pastes formatted, not as `# Title` text. Mod+Shift+V pastes plain text. A URL pasted over a selection becomes a link. A URL is a scheme with `//`, such as `https://` or `obsidian://`, or `mailto:`, that `resolveUrl` allows (`linkableUrl`). Pasted on its own, such a URL that Markdown does not autolink, like `obsidian://…`, becomes a link too, saved as `<obsidian://…>`.
- **Paste** tries, in order: the editor's own HTML, other `text/html` (`htmlToDoc`, which reads tags and inline styles, including Google Docs' wrapper), then `text/plain` as Markdown. HTML from a code editor or terminal, which only colors the source (VS Code's has its own `vscode-editor-data` type; others are `<div>`s and `<span>`s with no formatting, see `isStyledText`), is skipped, so Markdown copied from them pastes formatted, not as `# Title` text. Mod+Shift+V pastes plain text. Inside inline code or a code/raw block, paste inserts the clipboard text literally and keeps the code format. A URL pasted over a selection outside code becomes a link. A URL is a scheme with `//`, such as `https://` or `obsidian://`, or `mailto:`, that `resolveUrl` allows (`linkableUrl`). Pasted on its own, such a URL that Markdown does not autolink, like `obsidian://…`, becomes a link too, saved as `<obsidian://…>`.

## Markdown

Expand Down
54 changes: 54 additions & 0 deletions e2e/clipboard.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,60 @@ test('a pasted URL with any scheme the URL policy allows becomes a link', async
}
});

for (const url of ['https://example.com/', 'obsidian://open?vault=notes']) {
for (const [name, from, to] of [['caret', 3, 3], ['selection', 2, 5], ['whole span', 0, 7]] as const) {
test(`pasting ${url} in inline code at ${name} keeps it literal`, async ({ page }) => {
await page.evaluate(({ from, to }) => {
const editor = (window as any).editor;
editor.setOptions({ resolveUrl: (url: string) => url });
editor.setMarkdown('`literal`\n');
editor.setSelection({ anchor: { block: 0, offset: from }, focus: { block: 0, offset: to } });
}, { from, to });
await paste(page, { 'text/plain': url });
const text = 'literal'.slice(0, from) + url + 'literal'.slice(to);
expect(await md(page)).toBe('`' + text + '`\n');
await expect(page.locator('#editor code')).toHaveText(text);
await expect(page.locator('#editor a.mbe-link')).toHaveCount(0);
await page.keyboard.press('ControlOrMeta+z');
expect(await md(page)).toBe('`literal`\n');
});
}
}

test('pasting over an inline code span preserves the surrounding prose', async ({ page }) => {
await page.evaluate(() => {
const editor = (window as any).editor;
editor.setMarkdown('before `literal` after\n');
editor.setSelection({ anchor: { block: 0, offset: 7 }, focus: { block: 0, offset: 14 } });
});
await paste(page, { 'text/plain': 'https://example.com/' });
expect(await md(page)).toBe('before `https://example.com/` after\n');
await expect(page.locator('#editor code')).toHaveText('https://example.com/');
await expect(page.locator('#editor a.mbe-link')).toHaveCount(0);
});

test('pasting rich content into inline code inserts its plain text', async ({ page }) => {
await page.evaluate(() => {
const editor = (window as any).editor;
editor.setMarkdown('**`literal`**\n');
editor.setSelection({ anchor: { block: 0, offset: 0 }, focus: { block: 0, offset: 7 } });
});
await paste(page, { 'text/plain': '**bold** https://example.com/', 'text/html': '<b>bold</b> <a href="https://example.com/">https://example.com/</a>' });
expect(await md(page)).toBe('**`**bold** https://example.com/`**\n');
await expect(page.locator('#editor strong code')).toHaveText('**bold** https://example.com/');
await expect(page.locator('#editor a.mbe-link')).toHaveCount(0);
});

test('inline code paste follows the active mark toggle', async ({ page }) => {
await page.evaluate(() => (window as any).editor.commands.toggleCode());
await paste(page, { 'text/plain': 'https://example.com/' });
expect(await md(page)).toBe('`https://example.com/`\n');
await page.evaluate(() => (window as any).editor.commands.toggleCode());
await paste(page, { 'text/plain': 'https://other.example/' });
await expect(page.locator('#editor a.mbe-link')).toHaveAttribute('href', 'https://other.example/');
await expect(page.locator('#editor code')).toHaveText('https://example.com/');
});

test('Ctrl+Shift+V pastes plain text without parsing Markdown', async ({ page }) => {
// Simulate the keystroke without triggering the browser's own paste, then the paste event.
await page.evaluate(() =>
Expand Down
11 changes: 11 additions & 0 deletions packages/editor/src/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import {
containerContent,
createState,
endOf,
formatAt,
getBlock,
imageBlockOffset,
inlineLength,
Expand Down Expand Up @@ -2840,6 +2841,16 @@ export class Editor {
this.run((s) => cmd.insertText(s, text));
return;
}
if (cmd.isMarkActive(this.state, 'code')) {
this.run((s) => {
const { from } = orderedRange(s.doc, s.selection);
const content = containerContent(getBlock(s.doc, from.blockId), from.cell)!;
Comment thread
boojack marked this conversation as resolved.
Resolved
// Capture the marks before replacing a whole code span removes them.
const marks = s.storedMarks ?? formatAt(content, from.offset + (isCollapsed(s.selection) ? 0 : 1)).marks;
Comment thread
boojack marked this conversation as resolved.
Resolved
return cmd.insertText({ ...s, storedMarks: marks }, text);
});
return;
}
// A URL pasted over selected text makes it a link.
const url = this.pastedUrl(text, plainOnly);
if (url !== null) {
Expand Down
Loading