Repository navigation
fix(docs): copy plain text from the read-only docs preview #9948
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
MaxonT
wants to merge
3
commits into
usebruno:main
Choose a base branch
from
MaxonT:contrib/9947-auto
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+170
−0
Open
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
packages/bruno-app/src/ui/RichTextEditor/extensions/EditorPlainTextCopy/index.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| import { Extension } from '@tiptap/core'; | ||
| import { Plugin, PluginKey } from '@tiptap/pm/state'; | ||
| import DOMPurify from 'dompurify'; | ||
|
|
||
| // Raw HTML nodes are opaque atoms with no text children, so `textBetween` would skip them. Their visible | ||
| // text is what the node view renders: the DOMPurify-sanitized markup (same config as EditorRawHtmlBlock). | ||
| const RAW_HTML_NODE_NAMES = new Set(['rawHtmlBlock', 'rawHtmlInline']); | ||
|
|
||
| const leafText = (node) => { | ||
| if (RAW_HTML_NODE_NAMES.has(node.type.name)) { | ||
| const wrapper = document.createElement('div'); | ||
| wrapper.innerHTML = DOMPurify.sanitize(node.attrs.html || '', { FORBID_TAGS: ['style'] }); | ||
| // The stored html keeps the trailing newline of its markdown block; the block separator already supplies it. | ||
| return (wrapper.textContent || '').replace(/^\n+|\n+$/g, ''); | ||
| } | ||
| const specLeafText = node.type.spec.leafText; | ||
| return typeof specLeafText === 'function' ? specLeafText(node) : ''; | ||
| }; | ||
|
|
||
| // `Markdown` is configured with `transformCopiedText` so that copying out of the editable editor | ||
| // yields Markdown. In the read-only docs preview the user sees rendered text, so copying should give | ||
| // back the selected text without list markers, heading hashes, emphasis delimiters or code fences. | ||
| // ProseMirror asks plugins for a clipboard text serializer in order and uses the first answer, so this | ||
| // extension runs before the Markdown one and answers only while the editor is not editable. | ||
| const EditorPlainTextCopy = Extension.create({ | ||
| name: 'plainTextCopy', | ||
| priority: 1000, | ||
|
|
||
| addProseMirrorPlugins() { | ||
| return [ | ||
| new Plugin({ | ||
| key: new PluginKey('plainTextCopy'), | ||
| props: { | ||
| clipboardTextSerializer: (slice) => { | ||
| if (this.editor.isEditable) { | ||
| return null; | ||
| } | ||
| return slice.content.textBetween(0, slice.content.size, '\n\n', leafText); | ||
| } | ||
| } | ||
| }) | ||
| ]; | ||
| } | ||
| }); | ||
|
|
||
| export default EditorPlainTextCopy; | ||
104 changes: 104 additions & 0 deletions
104
packages/bruno-app/src/ui/RichTextEditor/extensions/EditorPlainTextCopy/index.spec.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| import { Editor } from '@tiptap/core'; | ||
| import { TextSelection } from '@tiptap/pm/state'; | ||
| import createExtensions from '../index'; | ||
|
|
||
| const DOCS = [ | ||
| '### Heading with a green apple inside', | ||
| 'Plain paragraph with a green apple inside.', | ||
| '- A bullet item with a green apple inside.\n- Another bullet item with a blue notebook inside.', | ||
| '1. A numbered item with a blue notebook inside.\n2. A numbered item with a green apple inside.', | ||
| '**Bold text with a green apple inside.**', | ||
| '*Italic text with a green apple inside.*', | ||
| 'Inline code with `a green apple inside`.', | ||
| '```sql\nSELECT \'a green apple inside\' AS sample;\n```' | ||
| ].join('\n\n'); | ||
|
|
||
| const createEditor = (editable) => | ||
| new Editor({ | ||
| extensions: createExtensions().filter((ext) => !['rawHtmlBlock', 'rawHtmlTextBlock'].includes(ext.name)), | ||
| content: DOCS, | ||
| editable | ||
| }); | ||
|
|
||
| // Selects the n-th occurrence of `needle` in the document and returns what ProseMirror | ||
| // would put on the clipboard as text/plain for that selection. | ||
| const copyText = (editor, needle, occurrence = 0) => { | ||
| const { doc } = editor.state; | ||
| const found = []; | ||
| doc.descendants((node, pos) => { | ||
| if (!node.isText) return; | ||
| let idx = node.text.indexOf(needle); | ||
| while (idx !== -1) { | ||
| found.push(pos + idx); | ||
| idx = node.text.indexOf(needle, idx + 1); | ||
| } | ||
| }); | ||
| const from = found[occurrence]; | ||
| const selection = TextSelection.create(doc, from, from + needle.length); | ||
| const { text } = editor.view.serializeForClipboard(selection.content()); | ||
| return text; | ||
| }; | ||
|
|
||
| describe('EditorPlainTextCopy', () => { | ||
| describe('read-only (docs preview)', () => { | ||
| let editor; | ||
| beforeEach(() => { | ||
| editor = createEditor(false); | ||
| }); | ||
| afterEach(() => editor.destroy()); | ||
|
|
||
| // Every "green apple" in DOCS, in document order; the fenced block is the last one. | ||
| it.each([ | ||
| ['heading', 0], | ||
| ['paragraph', 1], | ||
| ['bullet item', 2], | ||
| ['numbered item', 3], | ||
| ['bold', 4], | ||
| ['italic', 5], | ||
| ['inline code', 6], | ||
| ['code block', 7] | ||
| ])('copies only the selected words from a %s', (_label, occurrence) => { | ||
| expect(copyText(editor, 'green apple', occurrence)).toBe('green apple'); | ||
| }); | ||
|
|
||
| it('copies a selection across blocks without Markdown syntax', () => { | ||
| const { doc } = editor.state; | ||
| const selection = TextSelection.create(doc, 1, doc.content.size - 1); | ||
| const { text } = editor.view.serializeForClipboard(selection.content()); | ||
| expect(text).not.toMatch(/^#|\*\*|^- |^1\. |```/m); | ||
| expect(text).toContain('Plain paragraph with a green apple inside.'); | ||
| }); | ||
| }); | ||
|
|
||
| describe('editable', () => { | ||
| it('keeps copying as Markdown', () => { | ||
| const editor = createEditor(true); | ||
| const { doc } = editor.state; | ||
| const slice = TextSelection.create(doc, 1, doc.content.size - 1).content(); | ||
| const { text } = editor.view.serializeForClipboard(slice); | ||
| editor.destroy(); | ||
| expect(text).toMatch(/^### Heading/m); | ||
| expect(text).toContain('- A bullet item'); | ||
| }); | ||
| }); | ||
|
|
||
| describe('raw HTML blocks (read-only)', () => { | ||
| it('includes the visible text of a raw HTML block between paragraphs', () => { | ||
| const editor = new Editor({ | ||
| extensions: createExtensions(), | ||
| content: 'First paragraph.\n\n<div><strong>green apple</strong></div>\n\nLast paragraph.', | ||
| editable: false | ||
| }); | ||
| let hasRawBlock = false; | ||
| editor.state.doc.descendants((node) => { | ||
| if (node.type.name === 'rawHtmlBlock') hasRawBlock = true; | ||
| }); | ||
| const { doc } = editor.state; | ||
| const slice = TextSelection.create(doc, 1, doc.content.size - 1).content(); | ||
| const { text } = editor.view.serializeForClipboard(slice); | ||
| editor.destroy(); | ||
| expect(hasRawBlock).toBe(true); | ||
| expect(text).toBe('First paragraph.\n\ngreen apple\n\nLast paragraph.'); | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Preserve line breaks inside raw HTML.
For
<div>green<br>apple</div>, this returnsgreenapple, although the preview displays two lines.textContentconcatenates descendant text; it does not add separators for HTML elements. The outertextBetweenseparator cannot restore breaks inside an opaque node. (dom.spec.whatwg.org)Preserve
<br>and block boundaries during extraction. Add this case beside the existing single-line HTML test.