Repository navigation
fix(composer): scope multiline block formatting #3246
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
270512b
fix(composer): scope block formatting to the caret line
bbbf24b
fix(composer): normalize multiline block conversions
6ab556e
fix(composer): exclude boundary endpoint blocks
c714365
fix(composer): preserve partial list selections
65d5052
fix(composer): branch on restored selection
5316c59
test(composer): cover block transaction helpers
21987a8
fix(composer): preserve inline atoms in code
fb91387
test(composer): assert partial list structure
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
There are no files selected for viewing
120 changes: 120 additions & 0 deletions
120
desktop/src/features/messages/lib/selectionBlockFormatting.test.mjs
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,120 @@ | ||
| import assert from "node:assert/strict"; | ||
| import test from "node:test"; | ||
|
|
||
| import { getSchema } from "@tiptap/core"; | ||
| import StarterKit from "@tiptap/starter-kit"; | ||
| import { EditorState, TextSelection } from "@tiptap/pm/state"; | ||
|
|
||
| import { isolateSelectionForBlockFormatting } from "./selectionBlockFormatting.ts"; | ||
|
|
||
| // Matching useRichTextEditor's StarterKit configuration (minus things | ||
| // irrelevant to block isolation). | ||
| const schema = getSchema([ | ||
| StarterKit.configure({ | ||
| hardBreak: { keepMarks: true }, | ||
| heading: false, | ||
| trailingNode: false, | ||
| link: false, | ||
| }), | ||
| ]); | ||
|
|
||
| const para = (...content) => schema.nodes.paragraph.create(null, content); | ||
| const br = () => schema.nodes.hardBreak.create(); | ||
| const t = (text) => schema.text(text); | ||
|
|
||
| function doc(...content) { | ||
| return schema.nodes.doc.create(null, content); | ||
| } | ||
|
|
||
| function stateWithCaret(documentNode, caret) { | ||
| return EditorState.create({ | ||
| doc: documentNode, | ||
| selection: TextSelection.create(documentNode, caret), | ||
| }); | ||
| } | ||
|
|
||
| function paragraphTexts(documentNode) { | ||
| const texts = []; | ||
| documentNode.forEach((node) => { | ||
| texts.push(node.textContent); | ||
| }); | ||
| return texts; | ||
| } | ||
|
|
||
| test("caret between hard breaks isolates only its line", () => { | ||
| // <p>before␍target␍after</p> with the caret inside "target". | ||
| const state = stateWithCaret( | ||
| doc(para(t("before"), br(), t("target"), br(), t("after"))), | ||
| 10, | ||
| ); | ||
|
|
||
| const transaction = state.tr; | ||
| assert.equal(isolateSelectionForBlockFormatting(transaction), true); | ||
|
|
||
| const next = state.apply(transaction); | ||
| assert.deepEqual(paragraphTexts(next.doc), ["before", "target", "after"]); | ||
| assert.equal(next.selection.empty, true); | ||
| assert.equal(next.selection.$from.parent.textContent, "target"); | ||
| }); | ||
|
|
||
| test("caret on an empty trailing line isolates an empty paragraph", () => { | ||
| // "before" + Shift+Enter, caret at the end — the reported bug shape. | ||
| const state = stateWithCaret(doc(para(t("before"), br())), 8); | ||
|
|
||
| const transaction = state.tr; | ||
| assert.equal(isolateSelectionForBlockFormatting(transaction), true); | ||
|
|
||
| const next = state.apply(transaction); | ||
| assert.deepEqual(paragraphTexts(next.doc), ["before", ""]); | ||
| assert.equal(next.selection.empty, true); | ||
| assert.equal(next.selection.$from.parent.textContent, ""); | ||
| }); | ||
|
|
||
| test("caret on the first line splits only after that line", () => { | ||
| const state = stateWithCaret(doc(para(t("first"), br(), t("rest"))), 3); | ||
|
|
||
| const transaction = state.tr; | ||
| assert.equal(isolateSelectionForBlockFormatting(transaction), true); | ||
|
|
||
| const next = state.apply(transaction); | ||
| assert.deepEqual(paragraphTexts(next.doc), ["first", "rest"]); | ||
| assert.equal(next.selection.$from.parent.textContent, "first"); | ||
| }); | ||
|
|
||
| test("caret on the last line splits only before that line", () => { | ||
| const state = stateWithCaret(doc(para(t("rest"), br(), t("last"))), 8); | ||
|
|
||
| const transaction = state.tr; | ||
| assert.equal(isolateSelectionForBlockFormatting(transaction), true); | ||
|
|
||
| const next = state.apply(transaction); | ||
| assert.deepEqual(paragraphTexts(next.doc), ["rest", "last"]); | ||
| assert.equal(next.selection.$from.parent.textContent, "last"); | ||
| }); | ||
|
|
||
| test("caret in a single-line paragraph is a no-op", () => { | ||
| const state = stateWithCaret(doc(para(t("only line"))), 4); | ||
|
|
||
| const transaction = state.tr; | ||
| assert.equal(isolateSelectionForBlockFormatting(transaction), false); | ||
| assert.equal(transaction.steps.length, 0); | ||
| }); | ||
|
|
||
| test("selection isolation still splits around the selected text", () => { | ||
| const documentNode = doc(para(t("before selected after"))); | ||
| const state = EditorState.create({ | ||
| doc: documentNode, | ||
| // "selected" spans positions 8..16. | ||
| selection: TextSelection.create(documentNode, 8, 16), | ||
| }); | ||
|
|
||
| const transaction = state.tr; | ||
| assert.equal(isolateSelectionForBlockFormatting(transaction), true); | ||
|
|
||
| const next = state.apply(transaction); | ||
| assert.deepEqual(paragraphTexts(next.doc), ["before ", "selected", " after"]); | ||
| assert.equal( | ||
| next.doc.textBetween(next.selection.from, next.selection.to), | ||
| "selected", | ||
| ); | ||
| }); |
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.