-
Notifications
You must be signed in to change notification settings - Fork 46
feat: add configurable textShortcuts prop for block and inline shortcuts #498
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
janicduplessis
wants to merge
17
commits into
software-mansion:main
Choose a base branch
from
janicduplessis:feat/configurable-text-shortcuts
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.
Open
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
fe87d78
feat: add configurable textShortcuts prop for block and inline shortcuts
janicduplessis 6466e66
fix: default textShortcuts to built-in list shortcuts to avoid breaki…
janicduplessis cdb3372
fix: use std::string::empty() instead of has_value() for codegen type…
janicduplessis 9a16241
Merge branch 'main' into feat/configurable-text-shortcuts
kacperzolkiewski a4e9aa9
fix(iOS): shortcuts detection
kacperzolkiewski 3cb0da8
fix(android): shortcut detection
kacperzolkiewski 807af51
fix(iOS): forward shortcut verification
kacperzolkiewski 7770ab2
fix(android): refactor shortcuts detection
kacperzolkiewski 4743e6c
fix: refactor textShortcuts API
kacperzolkiewski 015a401
docs: add textShortcuts API
kacperzolkiewski 4a274d7
fix: add space after shortcut
kacperzolkiewski 757ae0f
Update ios/utils/ShortcutsUtils.mm
kacperzolkiewski dd1d46e
Update ios/utils/ShortcutsUtils.mm
kacperzolkiewski fd28dcf
Update ios/utils/ShortcutsUtils.mm
kacperzolkiewski 8d830b8
Update ios/utils/ShortcutsUtils.mm
kacperzolkiewski d76abcd
Update ios/utils/ShortcutsUtils.mm
kacperzolkiewski 86350ed
fix: review changes
kacperzolkiewski 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
128 changes: 128 additions & 0 deletions
128
android/src/main/java/com/swmansion/enriched/textinput/utils/ShortcutsHandler.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,128 @@ | ||
| package com.swmansion.enriched.textinput.utils | ||
|
|
||
| import android.text.Editable | ||
| import com.swmansion.enriched.textinput.EnrichedTextInputView | ||
|
|
||
| class ShortcutsHandler( | ||
| private val view: EnrichedTextInputView, | ||
| ) { | ||
| fun afterTextChanged( | ||
| s: Editable, | ||
| endCursorPosition: Int, | ||
| previousTextLength: Int, | ||
| ) { | ||
| handleConfigurableShortcuts(s, endCursorPosition, previousTextLength) | ||
| handleInlineShortcuts(s, endCursorPosition, previousTextLength) | ||
| } | ||
|
|
||
| private fun handleConfigurableShortcuts( | ||
| s: Editable, | ||
| endCursorPosition: Int, | ||
| previousTextLength: Int, | ||
| ) { | ||
| val shortcuts = view.textShortcuts | ||
| if (shortcuts.isEmpty()) return | ||
| if (previousTextLength >= s.length) return | ||
|
|
||
| val cursorPosition = endCursorPosition.coerceAtMost(s.length) | ||
| val (start, end) = s.getParagraphBounds(cursorPosition) | ||
| val paragraphText = s.substring(start, end) | ||
|
|
||
| for ((trigger, styleName) in shortcuts) { | ||
| if (isInlineShortcutStyle(styleName)) continue | ||
| if (trigger.isEmpty()) continue | ||
| if (!paragraphText.startsWith(trigger)) continue | ||
|
|
||
| val resolvedStyle = resolveStyleName(styleName) ?: continue | ||
|
|
||
| s.replace(start, start + trigger.length, "") | ||
| view.toggleStyle(resolvedStyle) | ||
| return | ||
| } | ||
| } | ||
|
|
||
| private fun inlineShortcutsSorted(): List<Pair<String, String>> = | ||
| view.textShortcuts | ||
| .filter { (trigger, styleName) -> | ||
| isInlineShortcutStyle(styleName) && trigger.isNotEmpty() | ||
| }.sortedByDescending { it.first.length } | ||
|
|
||
| // Delimiter at [delimStart] is part of a longer inline trigger (e.g. `*` | ||
| // inside `**`). | ||
| private fun isDelimiterPartOfLongerInlineTrigger( | ||
| trigger: String, | ||
| delimStart: Int, | ||
| text: String, | ||
| inlineShortcuts: List<Pair<String, String>>, | ||
| ): Boolean { | ||
| val delimEnd = delimStart + trigger.length | ||
|
|
||
| for ((longerTrigger, _) in inlineShortcuts) { | ||
| if (longerTrigger.length <= trigger.length) continue | ||
| if (!longerTrigger.endsWith(trigger)) continue | ||
|
|
||
| val longerStart = delimEnd - longerTrigger.length | ||
| if (longerStart < 0 || longerStart + longerTrigger.length > text.length) continue | ||
|
|
||
| if (text.substring(longerStart, longerStart + longerTrigger.length) == longerTrigger) { | ||
| return true | ||
| } | ||
| } | ||
|
|
||
| return false | ||
| } | ||
|
|
||
| private fun handleInlineShortcuts( | ||
| s: Editable, | ||
| endCursorPosition: Int, | ||
| previousTextLength: Int, | ||
| ) { | ||
| val shortcuts = view.textShortcuts | ||
| if (shortcuts.isEmpty()) return | ||
| if (previousTextLength >= s.length) return | ||
|
|
||
| val cursorPosition = endCursorPosition.coerceAtMost(s.length) | ||
| val text = s.toString() | ||
| val (paraStart, _) = s.getParagraphBounds(cursorPosition) | ||
| val inlineShortcuts = inlineShortcutsSorted() | ||
|
|
||
| for ((trigger, styleName) in inlineShortcuts) { | ||
| val resolvedStyle = resolveStyleName(styleName) ?: continue | ||
|
|
||
| if (cursorPosition < trigger.length) continue | ||
| val closingDelim = text.substring(cursorPosition - trigger.length, cursorPosition) | ||
| if (closingDelim != trigger) continue | ||
|
|
||
| val closeDelimStart = cursorPosition - trigger.length | ||
|
|
||
| val searchText = text.substring(paraStart, closeDelimStart) | ||
| val openIdx = searchText.lastIndexOf(trigger) | ||
| if (openIdx < 0) continue | ||
|
|
||
| val openAbsolute = paraStart + openIdx | ||
|
|
||
| if (isDelimiterPartOfLongerInlineTrigger(trigger, openAbsolute, text, inlineShortcuts)) { | ||
| continue | ||
| } | ||
|
|
||
| val contentStart = openAbsolute + trigger.length | ||
| val contentEnd = closeDelimStart | ||
| if (contentEnd <= contentStart) continue | ||
|
|
||
| if (isStyleBlockedOnRange(resolvedStyle, contentStart, contentEnd, s, view.htmlStyle)) { | ||
| continue | ||
| } | ||
|
|
||
| s.delete(closeDelimStart, cursorPosition) | ||
| s.delete(openAbsolute, openAbsolute + trigger.length) | ||
|
|
||
| val adjustedStart = openAbsolute | ||
| val adjustedEnd = contentEnd - trigger.length | ||
|
|
||
| view.inlineStyles?.applyStyleOnRange(resolvedStyle, adjustedStart, adjustedEnd) | ||
| view.setSelection(adjustedEnd, adjustedEnd) | ||
| view.spanState?.setStart(resolvedStyle, null) | ||
| return | ||
| } | ||
| } | ||
| } |
51 changes: 51 additions & 0 deletions
51
android/src/main/java/com/swmansion/enriched/textinput/utils/StyleUtils.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,51 @@ | ||
| package com.swmansion.enriched.textinput.utils | ||
|
|
||
| import android.text.Spannable | ||
| import com.swmansion.enriched.textinput.spans.EnrichedSpans | ||
| import com.swmansion.enriched.textinput.styles.HtmlStyle | ||
|
|
||
| fun resolveStyleName(name: String): String? = | ||
| when (name) { | ||
| "h1" -> EnrichedSpans.H1 | ||
| "h2" -> EnrichedSpans.H2 | ||
| "h3" -> EnrichedSpans.H3 | ||
| "h4" -> EnrichedSpans.H4 | ||
| "h5" -> EnrichedSpans.H5 | ||
| "h6" -> EnrichedSpans.H6 | ||
| "blockquote" -> EnrichedSpans.BLOCK_QUOTE | ||
| "codeblock" -> EnrichedSpans.CODE_BLOCK | ||
| "unordered_list" -> EnrichedSpans.UNORDERED_LIST | ||
| "ordered_list" -> EnrichedSpans.ORDERED_LIST | ||
| "checkbox_list" -> EnrichedSpans.CHECKBOX_LIST | ||
| "bold" -> EnrichedSpans.BOLD | ||
| "italic" -> EnrichedSpans.ITALIC | ||
| "underline" -> EnrichedSpans.UNDERLINE | ||
| "strikethrough" -> EnrichedSpans.STRIKETHROUGH | ||
| "inline_code" -> EnrichedSpans.INLINE_CODE | ||
| else -> null | ||
| } | ||
|
|
||
| fun isInlineShortcutStyle(styleName: String): Boolean { | ||
| val resolvedStyle = resolveStyleName(styleName) ?: return false | ||
| return EnrichedSpans.inlineSpans.containsKey(resolvedStyle) | ||
| } | ||
|
|
||
| fun isStyleBlockedOnRange( | ||
| styleName: String, | ||
| start: Int, | ||
| end: Int, | ||
| spannable: Spannable, | ||
| htmlStyle: HtmlStyle, | ||
| ): Boolean { | ||
| val mergingConfig = | ||
| EnrichedSpans.getMergingConfigForStyle(styleName, htmlStyle) ?: return false | ||
|
|
||
| for (blockingStyleName in mergingConfig.blockingStyles) { | ||
| val spanClass = EnrichedSpans.allSpans[blockingStyleName]?.clazz ?: continue | ||
| if (spannable.getSpans(start, end, spanClass).isNotEmpty()) { | ||
| return true | ||
| } | ||
| } | ||
|
|
||
| return false | ||
| } |
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.
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.