Repository navigation
feat: add a per-block line wrap toggle to code block headers #1021
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 all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f8d7df1
feat: add a per-block line wrap toggle to code block headers
Abdullah1738 d25182f
fix(ios): write the checked state a task toggle produces into the mar…
hryhoriiK97 887f088
fix: keep wrap state across task toggles and stroke the Android wrap …
hryhoriiK97 dda187b
fix: update the logic acording to latest review
eszlamczyk 2d2927e
fix: apply inline cr
eszlamczyk 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
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
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
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
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
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
36 changes: 36 additions & 0 deletions
36
packages/react-native-enriched-markdown/__tests__/ios/ENRMCodeBlockWrapStateTests.mm
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,36 @@ | ||
| #import "../../ios/utils/ENRMCodeBlockWrapState.h" | ||
| #import <XCTest/XCTest.h> | ||
|
|
||
| @interface ENRMCodeBlockWrapStateTests : XCTestCase | ||
| @end | ||
|
|
||
| @implementation ENRMCodeBlockWrapStateTests | ||
|
|
||
| - (void)testKeepsStateWhileTheDocumentGrows | ||
| { | ||
| XCTAssertTrue(ENRMKeepsCodeBlockWrapState("a\nb", "a\nb\nc")); | ||
| // The unfinished last line may be rewritten. | ||
| XCTAssertTrue(ENRMKeepsCodeBlockWrapState("a\n<b", "a\n<br>\nc")); | ||
| // A single-line document has no stable prefix yet. | ||
| XCTAssertTrue(ENRMKeepsCodeBlockWrapState("abc", "xyz")); | ||
| } | ||
|
|
||
| - (void)testResetsWhenAnEarlierLineChanges | ||
| { | ||
| XCTAssertFalse(ENRMKeepsCodeBlockWrapState("a\nb\nc", "x\nb\nc")); | ||
| XCTAssertFalse(ENRMKeepsCodeBlockWrapState("a\nb\nc", "a\nx\nc")); | ||
| // Shorter than the stable prefix. | ||
| XCTAssertFalse(ENRMKeepsCodeBlockWrapState("a\nb\nc", "a\n")); | ||
| } | ||
|
|
||
| - (void)testCursorCountsInVisitOrder | ||
| { | ||
| ENRMCodeBlockWrapCursor *cursor = [[ENRMCodeBlockWrapCursor alloc] initWithWrapped:[NSSet setWithArray:@[ @1, @3 ]]]; | ||
| XCTAssertFalse([cursor nextIsWrapped]); | ||
| XCTAssertTrue([cursor nextIsWrapped]); | ||
| XCTAssertFalse([cursor nextIsWrapped]); | ||
| XCTAssertTrue([cursor nextIsWrapped]); | ||
| XCTAssertFalse([cursor nextIsWrapped]); | ||
| } | ||
|
|
||
| @end |
108 changes: 108 additions & 0 deletions
108
...ched-markdown/android/src/main/java/com/swmansion/enriched/markdown/CodeBlockWrapState.kt
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,108 @@ | ||
| package com.swmansion.enriched.markdown | ||
|
|
||
| import com.swmansion.enriched.markdown.segments.CodeBlockContainerView | ||
|
|
||
| // Code block wrap state is keyed by block position, so it only stays meaningful | ||
| // while the document grows in place. Streaming may rewrite its unfinished last | ||
| // line (an HTML tag closing, a link relabeled); anything earlier changing - a | ||
| // recycled cell, an edited message, a block inserted above - starts unwrapped. | ||
| // Compares in place: this runs on every streamed chunk while a block is wrapped. | ||
| internal fun keepsCodeBlockWrapState( | ||
| previous: String, | ||
| next: String, | ||
| ): Boolean { | ||
| val cut = previous.lastIndexOf('\n') | ||
| return cut < 0 || next.regionMatches(0, previous, 0, cut) | ||
| } | ||
|
|
||
| /** | ||
| * Which code blocks of one EnrichedMarkdown the user wrapped with the header | ||
| * toggle, and the only writer of that choice. | ||
| * | ||
| * Blocks are identified by document-order index, counted pre-order across | ||
| * nesting (a block inside a blockquote takes the next index, like a top-level | ||
| * one); [CodeBlockWrapCursor] counts the same way in the view-free measure | ||
| * pass. The set lives here rather than on the views, which are rebuilt on | ||
| * style and font-scale changes, and is mirrored into MeasurementStore under the | ||
| * view tag for that measure pass. | ||
| */ | ||
| internal class CodeBlockWrapState( | ||
| private val viewTag: () -> Int, | ||
| private val codeBlockViews: () -> Sequence<CodeBlockContainerView>, | ||
| private val onHeightChanged: () -> Unit, | ||
| ) { | ||
| // Matches the prop's default, for a host that never receives the prop. | ||
| var enabled: Boolean = true | ||
| private set | ||
|
|
||
| var indices: Set<Int> = emptySet() | ||
| private set | ||
|
|
||
| // The markdown prop as last received, which local task toggles don't touch. | ||
| private var lastMarkdownProp: String = "" | ||
|
|
||
| /** Returns whether the wrapped set was cleared, so the height must be recomputed. */ | ||
| fun setEnabled(value: Boolean): Boolean { | ||
| if (enabled == value) return false | ||
| enabled = value | ||
| val cleared = !value && indices.isNotEmpty() | ||
| if (cleared) write(emptySet()) | ||
| sync() | ||
| return cleared | ||
| } | ||
|
|
||
| /** | ||
| * Applies the reset rule to a new markdown prop; [rendered] is the document on | ||
| * screen, which includes local task toggles. The prop is compared with the | ||
| * previous prop, so an app that ignores a checkbox tap and keeps streaming its | ||
| * own copy keeps the wrapped blocks; a prop equal to what's rendered is the app | ||
| * echoing a task toggle back and keeps them too. Returns whether the wrapped | ||
| * set was cleared. | ||
| */ | ||
| fun onMarkdownProp( | ||
| next: String, | ||
| rendered: String, | ||
| ): Boolean { | ||
| val previous = lastMarkdownProp | ||
| lastMarkdownProp = next | ||
| if (indices.isEmpty() || next == rendered || keepsCodeBlockWrapState(previous, next)) return false | ||
| write(emptySet()) | ||
| return true | ||
| } | ||
|
|
||
| /** Pushes the wrapped set and the toggle onto the current code block views. */ | ||
| fun sync() { | ||
| codeBlockViews().forEachIndexed { index, view -> | ||
| view.onWrapToggle = ::toggle | ||
| view.showsWrapButton = enabled | ||
| view.wrapped = enabled && index in indices | ||
| } | ||
| } | ||
|
|
||
| private fun toggle(view: CodeBlockContainerView) { | ||
| if (!enabled) return | ||
| val index = codeBlockViews().indexOf(view) | ||
| if (index < 0) return | ||
| write(if (index in indices) indices - index else indices + index) | ||
| sync() | ||
| onHeightChanged() | ||
| } | ||
|
|
||
| private fun write(value: Set<Int>) { | ||
| indices = value | ||
| MeasurementStore.updateWrappedCodeBlocks(viewTag(), value) | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Hands out the wrapped flag of each code block in the order a view-free measure | ||
| * pass reaches them: pre-order, descending into blockquotes, the same order | ||
| * [CodeBlockWrapState] indexes the views in. | ||
| */ | ||
| class CodeBlockWrapCursor( | ||
| private val wrapped: Set<Int>, | ||
| ) { | ||
| private var nextIndex = 0 | ||
|
|
||
| fun nextIsWrapped(): Boolean = nextIndex++ in wrapped | ||
| } |
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
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.
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.
Unrelated change to the pr, fixes the bug that made screens crash on changing the text size on the fly