@cometchat/chat-uikit-react-native 5.6.0 → 5.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/android/src/main/java/com/reactnativecometchatuikit/CometChatKeyboardModule.kt +101 -0
- package/android/src/main/java/com/reactnativecometchatuikit/CometchatUiKitPackage.java +1 -0
- package/android/src/main/java/com/reactnativecometchatuikit/KeyboardDirectionWatcher.kt +64 -0
- package/android/src/main/java/com/reactnativecometchatuikit/KeyboardLanguageDirection.kt +44 -0
- package/android/src/main/java/com/reactnativecometchatuikit/RichTextEditorView.kt +287 -49
- package/ios/CometChatKeyboardModule.m +9 -0
- package/ios/CometChatKeyboardModule.swift +71 -0
- package/ios/KeyboardLanguageDirection.swift +24 -0
- package/ios/RichTextEditorView.swift +253 -51
- package/ios/RichTextEditorViewManager.m +1 -0
- package/package.json +1 -1
- package/src/CometChatAIAssistantChatHistory/CometChatAIAssistantChatHistory.tsx +41 -57
- package/src/CometChatAIAssistantChatHistory/Skeleton.tsx +4 -1
- package/src/CometChatAIAssistantChatHistory/style.ts +5 -2
- package/src/CometChatCompactMessageComposer/CometChatCompactMessageComposer.tsx +21 -8
- package/src/CometChatCompactMessageComposer/styles.ts +7 -7
- package/src/CometChatConversations/CometChatConversations.tsx +36 -30
- package/src/CometChatConversations/Skeleton.tsx +4 -1
- package/src/CometChatConversations/style.ts +1 -1
- package/src/CometChatGroupMembers/CometChatGroupMembers.tsx +35 -18
- package/src/CometChatGroupMembers/Skeleton.tsx +4 -1
- package/src/CometChatGroupMembers/style.ts +6 -4
- package/src/CometChatGroups/CometChatGroups.tsx +13 -6
- package/src/CometChatGroups/GroupsStyle.ts +1 -1
- package/src/CometChatGroups/Skeleton.tsx +4 -1
- package/src/CometChatMessageComposer/CometChatMessageComposer.tsx +44 -30
- package/src/CometChatMessageComposer/styles.tsx +1 -1
- package/src/CometChatMessageHeader/CometChatMessageHeader.tsx +22 -13
- package/src/CometChatMessageInformation/CometChatMessageInformation.tsx +8 -1
- package/src/CometChatMessageInformation/Skeleton.tsx +4 -1
- package/src/CometChatMessageList/CometChatMessageList.tsx +97 -128
- package/src/CometChatMessageList/SwipeToReplyView.tsx +61 -0
- package/src/CometChatMessageList/style.ts +32 -32
- package/src/CometChatMessageList/swipeToReply.ts +25 -0
- package/src/CometChatNotificationFeed/CometChatNotificationFeed.tsx +10 -3
- package/src/CometChatNotificationFeed/style.ts +1 -1
- package/src/CometChatNotificationFeed/utils.ts +17 -18
- package/src/CometChatPinnedMessages/CometChatPinnedMessages.tsx +8 -1
- package/src/CometChatPinnedMessages/style.ts +5 -5
- package/src/CometChatRichTextEditor/RichTextEditorViewNativeComponent.ts +2 -0
- package/src/CometChatRichTextEditor/index.tsx +3 -0
- package/src/CometChatSavedMessages/CometChatSavedMessages.tsx +14 -11
- package/src/CometChatSavedMessages/style.ts +3 -3
- package/src/CometChatSearch/CometChatSearch.tsx +38 -35
- package/src/CometChatSearch/Skeleton.tsx +4 -1
- package/src/CometChatSearch/searchLabels.ts +26 -0
- package/src/CometChatSearch/style.ts +7 -7
- package/src/CometChatThreadHeader/CometChatThreadHeader.tsx +11 -7
- package/src/CometChatThreadHeader/Header.tsx +2 -2
- package/src/CometChatUsers/CometChatUsers.tsx +8 -1
- package/src/CometChatUsers/Skeleton.tsx +4 -1
- package/src/CometChatUsers/style.ts +1 -1
- package/src/calls/CometChatCallButtons/CometChatCallButtons.tsx +8 -1
- package/src/calls/CometChatCallLogs/CometChatCallLogs.tsx +16 -4
- package/src/calls/CometChatCallLogs/Skeleton.tsx +4 -1
- package/src/calls/CometChatIncomingCall/CometChatIncomingCall.tsx +11 -3
- package/src/calls/CometChatOutgoingCall/CometChatOutgoingCall.tsx +10 -2
- package/src/extensions/CollaborativeBubble/style.ts +4 -4
- package/src/extensions/CollaborativeDocument/CollaborativeDocumentExtensionDecorator.tsx +1 -1
- package/src/extensions/CollaborativeWhiteboard/CollaborativeWhiteboardExtensionDecorator.tsx +1 -1
- package/src/extensions/LinkPreview/LinkPreviewBubble.tsx +11 -6
- package/src/extensions/LinkPreview/style.ts +6 -6
- package/src/extensions/Polls/Header.tsx +1 -1
- package/src/extensions/Polls/Polls.tsx +25 -12
- package/src/extensions/Polls/PollsBubble.tsx +10 -6
- package/src/extensions/Polls/style.ts +4 -4
- package/src/extensions/Stickers/CometChatStickerKeyboard/CometChatStickerKeyboard.tsx +2 -2
- package/src/extensions/Stickers/CometChatStickerKeyboard/Skeleton.tsx +1 -1
- package/src/extensions/Stickers/CometChatStickerKeyboard/style.ts +1 -1
- package/src/extensions/Stickers/StickersExtensionDecorator.tsx +2 -2
- package/src/index.ts +5 -1
- package/src/shared/CometChatUiKit/CometChatUIKit.ts +17 -0
- package/src/shared/CometChatUiKit/UIKitSettings.ts +8 -0
- package/src/shared/formatters/CometChatMentionsFormatter/CometChatMentionsFormatter.tsx +13 -10
- package/src/shared/formatters/CometChatRichTextFormatter/index.tsx +15 -7
- package/src/shared/formatters/CometChatTextFormatter.ts +11 -0
- package/src/shared/formatters/CometChatUrlsFormatter/index.tsx +7 -4
- package/src/shared/framework/MessageDataSource.tsx +17 -94
- package/src/shared/helper/LocalizedDateHelper.ts +64 -4
- package/src/shared/helper/helperFunctions.ts +71 -4
- package/src/shared/helper/keyboardDirection.ts +54 -0
- package/src/shared/helper/layoutDirection.ts +205 -0
- package/src/shared/helper/pluralRules.ts +67 -0
- package/src/shared/helper/textDirection.ts +118 -0
- package/src/shared/icons/Icon.tsx +37 -2
- package/src/shared/icons/components/format-list-numbered-rtl.tsx +15 -0
- package/src/shared/offline/OfflineViews.tsx +5 -1
- package/src/shared/offline/notices.ts +15 -21
- package/src/shared/resources/CometChatLocalizeNew/CometChatLocalizationHelper.ts +2 -0
- package/src/shared/resources/CometChatLocalizeNew/resources/ar/translation.json +720 -0
- package/src/shared/resources/CometChatLocalizeNew/resources/de/translation.json +50 -2
- package/src/shared/resources/CometChatLocalizeNew/resources/en/translation.json +51 -3
- package/src/shared/resources/CometChatLocalizeNew/resources/es/translation.json +50 -2
- package/src/shared/resources/CometChatLocalizeNew/resources/fr/translation.json +51 -3
- package/src/shared/resources/CometChatLocalizeNew/resources/hi/translation.json +50 -2
- package/src/shared/resources/CometChatLocalizeNew/resources/hu/translation.json +50 -2
- package/src/shared/resources/CometChatLocalizeNew/resources/it/translation.json +50 -2
- package/src/shared/resources/CometChatLocalizeNew/resources/ja/translation.json +50 -2
- package/src/shared/resources/CometChatLocalizeNew/resources/ko/translation.json +50 -2
- package/src/shared/resources/CometChatLocalizeNew/resources/lt/translation.json +58 -2
- package/src/shared/resources/CometChatLocalizeNew/resources/ms/translation.json +50 -2
- package/src/shared/resources/CometChatLocalizeNew/resources/nl/translation.json +50 -2
- package/src/shared/resources/CometChatLocalizeNew/resources/pt/translation.json +50 -2
- package/src/shared/resources/CometChatLocalizeNew/resources/ru/translation.json +66 -2
- package/src/shared/resources/CometChatLocalizeNew/resources/sv/translation.json +50 -2
- package/src/shared/resources/CometChatLocalizeNew/resources/tr/translation.json +50 -2
- package/src/shared/resources/CometChatLocalizeNew/resources/zh/translation.json +50 -2
- package/src/shared/resources/CometChatLocalizeNew/resources/zh-tw/translation.json +50 -2
- package/src/shared/utils/CometChatMessagePreview/CometChatMessagePreview.tsx +13 -12
- package/src/shared/utils/CometChatMessagePreview/style.ts +1 -1
- package/src/shared/utils/MessageUtils.tsx +14 -14
- package/src/shared/utils/PermissionUtil.ts +16 -5
- package/src/shared/utils/attachmentLimits.ts +3 -2
- package/src/shared/utils/conversationUtils.ts +3 -5
- package/src/shared/utils/fileIconFontSize.ts +10 -0
- package/src/shared/utils/fileSizeLabel.ts +16 -0
- package/src/shared/utils/groupActionText.ts +75 -0
- package/src/shared/views/CometChatAIAssistantMessageBubble/CometChatAIAssistantMessageBubble.tsx +12 -8
- package/src/shared/views/CometChatAttachmentPreview/CometChatAttachmentPreview.style.ts +1 -1
- package/src/shared/views/CometChatAttachmentPreview/CometChatAttachmentPreviewItem.tsx +5 -3
- package/src/shared/views/CometChatAttachmentPreview/CometChatAttachmentTile.tsx +22 -17
- package/src/shared/views/CometChatAttachmentViewer/CometChatAttachmentViewer.tsx +13 -9
- package/src/shared/views/CometChatAudioBubble/CometChatAudioBubble.tsx +16 -10
- package/src/shared/views/CometChatAudiosBubble/CometChatAudiosBubble.tsx +10 -6
- package/src/shared/views/CometChatBadge/CometChatBadge.tsx +8 -4
- package/src/shared/views/CometChatBottomSheet/CometChatBottomSheet.tsx +6 -2
- package/src/shared/views/CometChatComposerErrorBanner.tsx +3 -1
- package/src/shared/views/CometChatConfirmDialog/CometChatConfirmDialog.tsx +8 -3
- package/src/shared/views/CometChatConversationStarter/CometChatConversationStarter.tsx +2 -2
- package/src/shared/views/CometChatEmojiKeyboard/CometChatEmojiKeyboard.tsx +5 -3
- package/src/shared/views/CometChatEmojiKeyboard/emojis.ts +8 -8
- package/src/shared/views/CometChatEmojiKeyboard/style.ts +2 -2
- package/src/shared/views/CometChatFilesBubble/CometChatFilesBubble.tsx +5 -4
- package/src/shared/views/CometChatImageViewerModal/ImageViewerModal.tsx +7 -3
- package/src/shared/views/CometChatImagesBubble/CometChatImagesBubble.tsx +29 -23
- package/src/shared/views/CometChatInlineAudioRecorder/CometChatInlineAudioRecorder.tsx +41 -21
- package/src/shared/views/CometChatInlineAudioRecorder/useAudioRecorder.ts +7 -3
- package/src/shared/views/CometChatLinkConfirmPopup/CometChatLinkConfirmPopup.tsx +7 -3
- package/src/shared/views/CometChatList/CometChatList.tsx +10 -14
- package/src/shared/views/CometChatList/Header.tsx +5 -2
- package/src/shared/views/CometChatList/sectionLetter.ts +44 -0
- package/src/shared/views/CometChatList/styles.ts +7 -7
- package/src/shared/views/CometChatListItem/CometChatListItem.tsx +6 -4
- package/src/shared/views/CometChatMediaRecorder/CometChatAudioPlayer.tsx +16 -10
- package/src/shared/views/CometChatMediaRecorder/Timer.tsx +5 -1
- package/src/shared/views/CometChatMediaViewer/CometChatMediaViewer.tsx +16 -11
- package/src/shared/views/CometChatMessageBubble/CometChatMessageBubble.tsx +24 -4
- package/src/shared/views/CometChatMessageInput/CometChatMessageInput.tsx +18 -2
- package/src/shared/views/CometChatNewMessageIndicator/CometChatNewMessageIndicator.tsx +3 -2
- package/src/shared/views/CometChatReactionList/CometChatReactionList.tsx +3 -2
- package/src/shared/views/CometChatReactionList/ReactionListStyle.tsx +2 -2
- package/src/shared/views/CometChatReactionList/Skeleton.tsx +4 -1
- package/src/shared/views/CometChatReactions/CometChatReactions.tsx +6 -2
- package/src/shared/views/CometChatReportDialog/CometChatReportDialog.tsx +7 -3
- package/src/shared/views/CometChatReportDialog/style.ts +1 -1
- package/src/shared/views/CometChatSmartReplies/CometChatSmartReplies.tsx +1 -1
- package/src/shared/views/CometChatStreamMessageBubble/CometChatStreamMessageBubble.tsx +20 -13
- package/src/shared/views/CometChatSuggestionList/CometChatSuggestionList.tsx +3 -2
- package/src/shared/views/CometChatSuggestionList/Skeleton.tsx +1 -1
- package/src/shared/views/CometChatTextBubble/CometChatTextBubble.tsx +29 -3
- package/src/shared/views/CometChatTooltipMenu/CometChatTooltipMenu.tsx +20 -30
- package/src/shared/views/CometChatTooltipMenu/tooltipPosition.ts +43 -0
- package/src/shared/views/CometChatVideoBubble/style.ts +2 -2
- package/src/shared/views/CometChatVideoPlayer/CometChatVideoPlayer.tsx +4 -1
- package/src/shared/views/CometChatVideosBubble/CometChatVideosBubble.tsx +22 -22
- package/src/shared/views/SenderLabel.tsx +41 -0
- package/src/theme/default/default.ts +1 -1
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
package com.reactnativecometchatuikit
|
|
2
|
+
|
|
3
|
+
import android.os.Handler
|
|
4
|
+
import android.os.Looper
|
|
5
|
+
import com.facebook.react.bridge.Arguments
|
|
6
|
+
import com.facebook.react.bridge.LifecycleEventListener
|
|
7
|
+
import com.facebook.react.bridge.Promise
|
|
8
|
+
import com.facebook.react.bridge.ReactApplicationContext
|
|
9
|
+
import com.facebook.react.bridge.ReactContextBaseJavaModule
|
|
10
|
+
import com.facebook.react.bridge.ReactMethod
|
|
11
|
+
import com.facebook.react.modules.core.DeviceEventManagerModule
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* ENG-57281: the keyboard's direction for the standard composer (a React Native TextInput), which
|
|
15
|
+
* cannot see the keyboard from JS. JS listens only while the input is focused with no letter yet.
|
|
16
|
+
*/
|
|
17
|
+
class CometChatKeyboardModule(reactContext: ReactApplicationContext)
|
|
18
|
+
: ReactContextBaseJavaModule(reactContext), LifecycleEventListener {
|
|
19
|
+
|
|
20
|
+
override fun getName() = "CometChatKeyboardModule"
|
|
21
|
+
|
|
22
|
+
companion object {
|
|
23
|
+
const val EVENT = "keyboardDirectionChange"
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** The keyboard's direction ("rtl"/"ltr"/null). A seam so tests can stand in a keyboard. */
|
|
27
|
+
internal var readDirection: () -> String? = { KeyboardLanguageDirection.current(reactApplicationContext) }
|
|
28
|
+
|
|
29
|
+
private val main = Handler(Looper.getMainLooper())
|
|
30
|
+
// Touched on the main thread only.
|
|
31
|
+
private var listeners = 0
|
|
32
|
+
private var paused = false
|
|
33
|
+
|
|
34
|
+
// Polls only while JS listens (a focused standard composer with no letter yet) and the app is in
|
|
35
|
+
// the foreground.
|
|
36
|
+
private val watcher = KeyboardDirectionWatcher(
|
|
37
|
+
post = { task, delay -> main.postDelayed(task, delay) },
|
|
38
|
+
cancel = { main.removeCallbacks(it) },
|
|
39
|
+
read = { when (readDirection()) { "rtl" -> true; "ltr" -> false; else -> null } },
|
|
40
|
+
shouldPoll = { listeners > 0 && !paused },
|
|
41
|
+
onChange = { rtl -> emit(if (rtl) "rtl" else "ltr") },
|
|
42
|
+
)
|
|
43
|
+
|
|
44
|
+
// After the fields above: registering can call onHostResume straight away.
|
|
45
|
+
init {
|
|
46
|
+
reactContext.addLifecycleEventListener(this)
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
private fun emit(direction: String) {
|
|
50
|
+
if (!reactApplicationContext.hasActiveReactInstance()) return
|
|
51
|
+
val body = Arguments.createMap().apply { putString("direction", direction) }
|
|
52
|
+
reactApplicationContext
|
|
53
|
+
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter::class.java)
|
|
54
|
+
.emit(EVENT, body)
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
@ReactMethod
|
|
58
|
+
fun getKeyboardDirection(promise: Promise) {
|
|
59
|
+
try {
|
|
60
|
+
promise.resolve(readDirection())
|
|
61
|
+
} catch (e: Exception) {
|
|
62
|
+
promise.reject("ERROR", e.message)
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// NativeEventEmitter calls these when JS adds and removes listeners.
|
|
67
|
+
@ReactMethod
|
|
68
|
+
fun addListener(eventName: String) {
|
|
69
|
+
main.post {
|
|
70
|
+
if (listeners++ == 0) watcher.prime()
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
@ReactMethod
|
|
75
|
+
fun removeListeners(count: Int) {
|
|
76
|
+
main.post {
|
|
77
|
+
listeners = (listeners - count).coerceAtLeast(0)
|
|
78
|
+
watcher.schedule()
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// Backgrounded: no polling. Back: resume if JS still listens.
|
|
83
|
+
override fun onHostPause() {
|
|
84
|
+
main.post { paused = true; watcher.schedule() }
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
override fun onHostResume() {
|
|
88
|
+
main.post { paused = false; watcher.schedule() }
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
override fun onHostDestroy() {
|
|
92
|
+
main.post { paused = true; watcher.schedule() }
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
override fun invalidate() {
|
|
96
|
+
// Also stops add/resume tasks already queued with main.post from restarting the poll.
|
|
97
|
+
watcher.invalidate()
|
|
98
|
+
reactApplicationContext.removeLifecycleEventListener(this)
|
|
99
|
+
super.invalidate()
|
|
100
|
+
}
|
|
101
|
+
}
|
|
@@ -27,6 +27,7 @@ public class CometchatUiKitPackage implements ReactPackage {
|
|
|
27
27
|
modules.add(new TimeZoneCodeManager(reactContext));
|
|
28
28
|
modules.add(new ProximityModule(reactContext));
|
|
29
29
|
modules.add(new CometChatClipboardModule(reactContext));
|
|
30
|
+
modules.add(new CometChatKeyboardModule(reactContext));
|
|
30
31
|
return modules;
|
|
31
32
|
}
|
|
32
33
|
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
package com.reactnativecometchatuikit
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* ENG-57281: re-reads the keyboard language's direction on a timer, for putting an empty composer's
|
|
5
|
+
* caret on that side. Shared by RichTextEditorView (compact composer) and CometChatKeyboardModule
|
|
6
|
+
* (standard composer); each caller decides when polling is wanted ([shouldPoll]) and posts on its
|
|
7
|
+
* own thread ([post] / [cancel]).
|
|
8
|
+
*
|
|
9
|
+
* [read] gives true = right-to-left, false = left-to-right, null = no language (emoji, voice): no
|
|
10
|
+
* language keeps the last direction, so opening the emoji keyboard moves nothing.
|
|
11
|
+
*/
|
|
12
|
+
internal class KeyboardDirectionWatcher(
|
|
13
|
+
private val post: (Runnable, Long) -> Unit,
|
|
14
|
+
private val cancel: (Runnable) -> Unit,
|
|
15
|
+
private val read: () -> Boolean?,
|
|
16
|
+
private val shouldPoll: () -> Boolean,
|
|
17
|
+
private val onChange: (Boolean) -> Unit,
|
|
18
|
+
) {
|
|
19
|
+
companion object {
|
|
20
|
+
const val POLL_MS = 500L
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** The last direction seen; null until a language is reported. */
|
|
24
|
+
var last: Boolean? = null
|
|
25
|
+
private set
|
|
26
|
+
|
|
27
|
+
private var invalidated = false
|
|
28
|
+
private val tick = Runnable { refresh() }
|
|
29
|
+
|
|
30
|
+
/** Reads now; a new language calls [onChange]. Then polls on while [shouldPoll]. */
|
|
31
|
+
fun refresh() {
|
|
32
|
+
if (invalidated) return
|
|
33
|
+
val now = read()
|
|
34
|
+
if (now != null && now != last) {
|
|
35
|
+
last = now
|
|
36
|
+
onChange(now)
|
|
37
|
+
}
|
|
38
|
+
schedule()
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** Takes the current reading (null included) as the baseline without [onChange], then polls. */
|
|
42
|
+
fun prime() {
|
|
43
|
+
if (invalidated) return
|
|
44
|
+
last = read()
|
|
45
|
+
schedule()
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// ponytail: Android sends no event when the user switches keyboard languages, so this polls every
|
|
49
|
+
// POLL_MS, only while the caller's shouldPoll holds (a focused composer whose caret paragraph has
|
|
50
|
+
// no letter yet, in the foreground). Ceiling: a switch shows up to POLL_MS late; drop the poll if
|
|
51
|
+
// Android ever reports IME subtype changes to the app.
|
|
52
|
+
fun schedule() {
|
|
53
|
+
cancel(tick)
|
|
54
|
+
if (!invalidated && shouldPoll()) post(tick, POLL_MS)
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
fun stop() = cancel(tick)
|
|
58
|
+
|
|
59
|
+
/** For good: nothing reads or polls after this, even a task the caller had already queued. */
|
|
60
|
+
fun invalidate() {
|
|
61
|
+
invalidated = true
|
|
62
|
+
cancel(tick)
|
|
63
|
+
}
|
|
64
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
package com.reactnativecometchatuikit
|
|
2
|
+
|
|
3
|
+
import android.content.Context
|
|
4
|
+
import android.text.TextUtils
|
|
5
|
+
import android.view.View
|
|
6
|
+
import android.view.inputmethod.InputMethodManager
|
|
7
|
+
import java.util.Locale
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* ENG-57281: the direction of the current keyboard's language, for putting an empty composer's caret
|
|
11
|
+
* on that side. Used by RichTextEditorView (compact composer) and CometChatKeyboardModule (standard
|
|
12
|
+
* composer).
|
|
13
|
+
*/
|
|
14
|
+
internal object KeyboardLanguageDirection {
|
|
15
|
+
/**
|
|
16
|
+
* true = right-to-left, false = left-to-right, null = the keyboard gave no language (emoji,
|
|
17
|
+
* voice, a keyboard that reports none). Takes a BCP-47 tag ("ar-EG") or a legacy locale ("ar_EG").
|
|
18
|
+
*/
|
|
19
|
+
fun isRtl(language: String?): Boolean? {
|
|
20
|
+
val tag = language?.trim()?.replace('_', '-')
|
|
21
|
+
if (tag.isNullOrEmpty()) return null
|
|
22
|
+
val locale = Locale.forLanguageTag(tag)
|
|
23
|
+
if (locale.language.isNullOrEmpty()) return null // "und", or not a language tag
|
|
24
|
+
return TextUtils.getLayoutDirectionFromLocale(locale) == View.LAYOUT_DIRECTION_RTL
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* The current keyboard's language: its subtype's language tag, else the subtype's locale string.
|
|
29
|
+
* Android sends no event when the user switches keyboard languages, so callers poll this.
|
|
30
|
+
*/
|
|
31
|
+
fun currentLanguage(context: Context): String? {
|
|
32
|
+
val imm = context.getSystemService(Context.INPUT_METHOD_SERVICE) as? InputMethodManager ?: return null
|
|
33
|
+
val subtype = try { imm.currentInputMethodSubtype } catch (e: Exception) { null } ?: return null
|
|
34
|
+
return subtype.languageTag.takeUnless { it.isNullOrEmpty() }
|
|
35
|
+
?: @Suppress("DEPRECATION") subtype.locale.takeUnless { it.isNullOrEmpty() }
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** "rtl" / "ltr" / null for JS. */
|
|
39
|
+
fun current(context: Context): String? = when (isRtl(currentLanguage(context))) {
|
|
40
|
+
true -> "rtl"
|
|
41
|
+
false -> "ltr"
|
|
42
|
+
null -> null
|
|
43
|
+
}
|
|
44
|
+
}
|
|
@@ -149,6 +149,17 @@ class NoUnderlineURLSpan(url: String) : URLSpan(url) {
|
|
|
149
149
|
}
|
|
150
150
|
}
|
|
151
151
|
|
|
152
|
+
/**
|
|
153
|
+
* ENG-57281: a toolbar alignment, kept as the SCREEN side the user picked ("left"/"right"/"center")
|
|
154
|
+
* so it survives the paragraph changing language, like iOS's UserAlignmentKey. Layout only knows
|
|
155
|
+
* NORMAL/OPPOSITE, which follow the paragraph's direction, so [alignment] is that side resolved for
|
|
156
|
+
* the paragraph's current direction; RichTextEditorView.applyUserAlignment re-resolves it (one span
|
|
157
|
+
* per paragraph) on every text change.
|
|
158
|
+
*/
|
|
159
|
+
data class ScreenAlignmentSpan(val side: String, private val resolved: Layout.Alignment) : AlignmentSpan {
|
|
160
|
+
override fun getAlignment(): Layout.Alignment = resolved
|
|
161
|
+
}
|
|
162
|
+
|
|
152
163
|
class RichTextEditorView(context: Context) : androidx.appcompat.widget.AppCompatEditText(context) {
|
|
153
164
|
|
|
154
165
|
private var placeholder: String = ""
|
|
@@ -346,6 +357,81 @@ class RichTextEditorView(context: Context) : androidx.appcompat.widget.AppCompat
|
|
|
346
357
|
* IME only when the resolved value actually changed — i.e. when the caret crosses a
|
|
347
358
|
* code boundary, never mid-word in prose.
|
|
348
359
|
*/
|
|
360
|
+
/**
|
|
361
|
+
* ENG-57281: empty with no keyboard language, VIEW_START keeps the caret and hint at the kit's
|
|
362
|
+
* start edge (ENG-41384); otherwise TEXT_START aligns each paragraph by its own direction
|
|
363
|
+
* (FIRST_STRONG), so English starts on the left in an RTL composer and Arabic on the right in an
|
|
364
|
+
* LTR one, and a paragraph with no letter falls back to the keyboard's direction. A toolbar
|
|
365
|
+
* AlignmentSpan still wins over all of them.
|
|
366
|
+
*/
|
|
367
|
+
private fun applyContentAlignment(s: CharSequence?) {
|
|
368
|
+
val keyboard = keyboardRtl
|
|
369
|
+
// Setters no-op when the raw value is unchanged (the getters return resolved values).
|
|
370
|
+
textAlignment = if (s.isNullOrEmpty() && keyboard == null) View.TEXT_ALIGNMENT_VIEW_START else View.TEXT_ALIGNMENT_TEXT_START
|
|
371
|
+
// ENG-57281: a paragraph with no letter yet (the empty editor, a new empty line) falls back to
|
|
372
|
+
// the keyboard language's direction, so its caret starts on that side: English at the left of
|
|
373
|
+
// an RTL composer, Arabic at the right of an LTR one. No keyboard language, the layout's.
|
|
374
|
+
textDirection = when (keyboard) {
|
|
375
|
+
true -> View.TEXT_DIRECTION_FIRST_STRONG_RTL
|
|
376
|
+
false -> View.TEXT_DIRECTION_FIRST_STRONG_LTR
|
|
377
|
+
null -> View.TEXT_DIRECTION_FIRST_STRONG
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
// ==================== Keyboard language (ENG-57281) ====================
|
|
382
|
+
|
|
383
|
+
/** The keyboard's language. A seam so tests can stand in a keyboard. */
|
|
384
|
+
internal var keyboardLanguage: () -> String? = { KeyboardLanguageDirection.currentLanguage(context) }
|
|
385
|
+
|
|
386
|
+
/** Only while typing here: focused, in the focused window (not backgrounded, not under a dialog). */
|
|
387
|
+
private fun isTypingHere() = hasFocus() && hasWindowFocus()
|
|
388
|
+
|
|
389
|
+
/**
|
|
390
|
+
* Reads the keyboard while typing here; polls while the caret paragraph has no letter yet (the only
|
|
391
|
+
* time the keyboard decides a side). A new language re-aligns.
|
|
392
|
+
*/
|
|
393
|
+
private val keyboardWatcher = KeyboardDirectionWatcher(
|
|
394
|
+
post = { task, delay -> postDelayed(task, delay) },
|
|
395
|
+
cancel = { removeCallbacks(it) },
|
|
396
|
+
read = { if (isTypingHere()) KeyboardLanguageDirection.isRtl(keyboardLanguage()) else null },
|
|
397
|
+
shouldPoll = { isTypingHere() && firstStrongRtlAt(selectionStart) == null },
|
|
398
|
+
onChange = {
|
|
399
|
+
applyContentAlignment(text)
|
|
400
|
+
applyUserAlignment(text)
|
|
401
|
+
updatePlaceholderVisibility()
|
|
402
|
+
},
|
|
403
|
+
)
|
|
404
|
+
|
|
405
|
+
/**
|
|
406
|
+
* The keyboard language's direction, last read while focused: true RTL, false LTR, null no language
|
|
407
|
+
* seen yet. Kept on blur, and while a keyboard with no language (emoji, voice) is up, so nothing moves.
|
|
408
|
+
*/
|
|
409
|
+
private val keyboardRtl: Boolean? get() = keyboardWatcher.last
|
|
410
|
+
|
|
411
|
+
/** Re-reads the keyboard while typing here and re-aligns when it moved; keeps polling while needed. */
|
|
412
|
+
private fun refreshKeyboardDirection() = keyboardWatcher.refresh()
|
|
413
|
+
|
|
414
|
+
override fun onWindowFocusChanged(hasWindowFocus: Boolean) {
|
|
415
|
+
super.onWindowFocusChanged(hasWindowFocus)
|
|
416
|
+
// Backgrounded or covered: stop reading; back: catch a keyboard switched meanwhile.
|
|
417
|
+
if (hasWindowFocus) refreshKeyboardDirection() else keyboardWatcher.stop()
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
/**
|
|
421
|
+
* The hint keeps the kit's start edge (ENG-41384). Once the keyboard decides the empty editor's
|
|
422
|
+
* side, alignment is TEXT_START and the hint would follow its own letters, so a bidi mark of the
|
|
423
|
+
* layout's direction in front pins its paragraph direction.
|
|
424
|
+
*/
|
|
425
|
+
private fun hintText(): String = when (keyboardRtl) {
|
|
426
|
+
null -> placeholder
|
|
427
|
+
else -> (if (layoutDirection == View.LAYOUT_DIRECTION_RTL) "\u200F" else "\u200E") + placeholder
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
override fun onRtlPropertiesChanged(layoutDirection: Int) {
|
|
431
|
+
super.onRtlPropertiesChanged(layoutDirection)
|
|
432
|
+
if (isInitialized) updatePlaceholderVisibility() // the hint's mark follows the layout
|
|
433
|
+
}
|
|
434
|
+
|
|
349
435
|
private fun applyTypingTraits() {
|
|
350
436
|
var flags = EditorInfo.TYPE_CLASS_TEXT or EditorInfo.TYPE_TEXT_FLAG_MULTI_LINE
|
|
351
437
|
|
|
@@ -393,6 +479,14 @@ class RichTextEditorView(context: Context) : androidx.appcompat.widget.AppCompat
|
|
|
393
479
|
setTextColor(Color.BLACK)
|
|
394
480
|
setHintTextColor(Color.parseColor("#9E9E9E"))
|
|
395
481
|
gravity = Gravity.TOP or Gravity.START
|
|
482
|
+
// ENG-41384: START above resolves against the text's own direction, so an English hint sat
|
|
483
|
+
// at the left of an RTL composer. VIEW_START uses the view's layout direction, which Fabric
|
|
484
|
+
// sets from Yoga (the kit's layoutDirection / forceRTL). No change in LTR.
|
|
485
|
+
textAlignment = View.TEXT_ALIGNMENT_VIEW_START
|
|
486
|
+
// ENG-57281: once there is text, each paragraph aligns by its own first strong letter
|
|
487
|
+
// (applyContentAlignment); FIRST_STRONG falls back to the layout direction for a paragraph
|
|
488
|
+
// with no letters until the keyboard's language is known (refreshKeyboardDirection).
|
|
489
|
+
textDirection = View.TEXT_DIRECTION_FIRST_STRONG
|
|
396
490
|
isFocusable = true
|
|
397
491
|
isFocusableInTouchMode = true
|
|
398
492
|
applyTypingTraits()
|
|
@@ -485,6 +579,10 @@ class RichTextEditorView(context: Context) : androidx.appcompat.widget.AppCompat
|
|
|
485
579
|
}
|
|
486
580
|
|
|
487
581
|
override fun afterTextChanged(s: Editable?) {
|
|
582
|
+
applyContentAlignment(s)
|
|
583
|
+
applyUserAlignment(s)
|
|
584
|
+
// The keyboard decides only a paragraph with no letter; typing letters reads nothing.
|
|
585
|
+
if (firstStrongRtlAt(selectionStart) == null) refreshKeyboardDirection() else keyboardWatcher.stop()
|
|
488
586
|
if (!isInternalChange) {
|
|
489
587
|
// enterKeyBehavior="sendMessage": intercept Enter key press.
|
|
490
588
|
// When a single newline is inserted (not part of a paste or list continuation),
|
|
@@ -1090,8 +1188,10 @@ class RichTextEditorView(context: Context) : androidx.appcompat.widget.AppCompat
|
|
|
1090
1188
|
|
|
1091
1189
|
setOnFocusChangeListener { _, hasFocus ->
|
|
1092
1190
|
if (hasFocus) {
|
|
1191
|
+
refreshKeyboardDirection() // ENG-57281: an empty composer's caret on the keyboard's side
|
|
1093
1192
|
sendEvent("onEditorFocus", Arguments.createMap())
|
|
1094
1193
|
} else {
|
|
1194
|
+
keyboardWatcher.stop()
|
|
1095
1195
|
hideToolbar()
|
|
1096
1196
|
sendEvent("onEditorBlur", Arguments.createMap())
|
|
1097
1197
|
}
|
|
@@ -1416,6 +1516,8 @@ class RichTextEditorView(context: Context) : androidx.appcompat.widget.AppCompat
|
|
|
1416
1516
|
// Skip if not initialized yet (during construction)
|
|
1417
1517
|
if (!isInitialized) return
|
|
1418
1518
|
|
|
1519
|
+
keyboardWatcher.schedule() // ENG-57281: the caret may have moved onto an empty line
|
|
1520
|
+
|
|
1419
1521
|
// How many characters landed since the last selection change, measured against the length
|
|
1420
1522
|
// recorded here rather than previousText. A keyboard (Gboard, composing mid-word) can run
|
|
1421
1523
|
// afterTextChanged — which refreshes previousText — before this callback, so measuring
|
|
@@ -1748,38 +1850,11 @@ class RichTextEditorView(context: Context) : androidx.appcompat.widget.AppCompat
|
|
|
1748
1850
|
}
|
|
1749
1851
|
|
|
1750
1852
|
// Draw blockquote vertical bars (continuous bar for merged adjacent quote lines)
|
|
1751
|
-
// Same pattern as iOS: ▎ character is made invisible, bar is custom-drawn
|
|
1853
|
+
// Same pattern as iOS: ▎ character is made invisible, bar is custom-drawn.
|
|
1854
|
+
// RTL: each bar sits at its paragraph's start edge (right for Arabic), see quoteBarRects.
|
|
1752
1855
|
if (textLayout != null) {
|
|
1753
|
-
val
|
|
1754
|
-
|
|
1755
|
-
// Merge adjacent quote lines into regions (startOffset, endOffset)
|
|
1756
|
-
val quoteRegions = mutableListOf<Pair<Int, Int>>()
|
|
1757
|
-
var offset = 0
|
|
1758
|
-
for (line in lines) {
|
|
1759
|
-
val lineEnd = offset + line.length
|
|
1760
|
-
if (line.startsWith("▎")) {
|
|
1761
|
-
if (quoteRegions.isNotEmpty() && offset <= quoteRegions.last().second + 1) {
|
|
1762
|
-
quoteRegions[quoteRegions.lastIndex] = Pair(quoteRegions.last().first, lineEnd)
|
|
1763
|
-
} else {
|
|
1764
|
-
quoteRegions.add(Pair(offset, lineEnd))
|
|
1765
|
-
}
|
|
1766
|
-
}
|
|
1767
|
-
offset = lineEnd + 1 // +1 for the \n
|
|
1768
|
-
}
|
|
1769
|
-
|
|
1770
|
-
for ((regionStart, regionEnd) in quoteRegions) {
|
|
1771
|
-
val clampedEnd = regionEnd.coerceAtMost((text?.length ?: 0) - 1).coerceAtLeast(regionStart)
|
|
1772
|
-
val firstLine = textLayout.getLineForOffset(regionStart)
|
|
1773
|
-
val lastLine = textLayout.getLineForOffset(clampedEnd)
|
|
1774
|
-
|
|
1775
|
-
val topY = textLayout.getLineTop(firstLine).toFloat() + textBaseY
|
|
1776
|
-
val bottomY = textLayout.getLineBottom(lastLine).toFloat() + textBaseY
|
|
1777
|
-
|
|
1778
|
-
val barWidth = 3f * density
|
|
1779
|
-
val barX = paddingLeft.toFloat() + 1f
|
|
1780
|
-
val barRadius = barWidth / 2f
|
|
1781
|
-
|
|
1782
|
-
val barRect = android.graphics.RectF(barX, topY, barX + barWidth, bottomY)
|
|
1856
|
+
val barRadius = QUOTE_BAR_WIDTH_DP * density / 2f
|
|
1857
|
+
for (barRect in quoteBarRects(textLayout, textBaseY)) {
|
|
1783
1858
|
codeBlockPaint.isAntiAlias = true
|
|
1784
1859
|
codeBlockPaint.style = Paint.Style.FILL
|
|
1785
1860
|
codeBlockPaint.strokeWidth = 0f
|
|
@@ -1798,6 +1873,60 @@ class RichTextEditorView(context: Context) : androidx.appcompat.widget.AppCompat
|
|
|
1798
1873
|
}
|
|
1799
1874
|
}
|
|
1800
1875
|
|
|
1876
|
+
/**
|
|
1877
|
+
* The blockquote bars onDraw paints, in view coordinates: one per run of adjacent quote lines
|
|
1878
|
+
* of the same direction. Each paragraph's direction is its own (TEXT_DIRECTION_FIRST_STRONG,
|
|
1879
|
+
* ENG-57281), and the layout puts the hidden "▎ " and its LeadingMarginSpan at that
|
|
1880
|
+
* paragraph's start edge, so the side is read from the same layout: right for RTL.
|
|
1881
|
+
*/
|
|
1882
|
+
internal fun quoteBarRects(textLayout: Layout, textBaseY: Float): List<android.graphics.RectF> {
|
|
1883
|
+
val barWidth = QUOTE_BAR_WIDTH_DP * density
|
|
1884
|
+
val lastIndex = (text?.length ?: 0) - 1
|
|
1885
|
+
val isRtlAt = { offset: Int ->
|
|
1886
|
+
textLayout.getParagraphDirection(textLayout.getLineForOffset(offset)) == Layout.DIR_RIGHT_TO_LEFT
|
|
1887
|
+
}
|
|
1888
|
+
return quoteBarRegions(text?.toString() ?: "", isRtlAt).map { region ->
|
|
1889
|
+
val clampedEnd = region.end.coerceAtMost(lastIndex).coerceAtLeast(region.start)
|
|
1890
|
+
val topY = textLayout.getLineTop(textLayout.getLineForOffset(region.start)).toFloat() + textBaseY
|
|
1891
|
+
val bottomY = textLayout.getLineBottom(textLayout.getLineForOffset(clampedEnd)).toFloat() + textBaseY
|
|
1892
|
+
val barX = quoteBarX(region.rtl, width, paddingLeft, paddingRight, barWidth)
|
|
1893
|
+
android.graphics.RectF(barX, topY, barX + barWidth, bottomY)
|
|
1894
|
+
}
|
|
1895
|
+
}
|
|
1896
|
+
|
|
1897
|
+
internal data class QuoteBarRegion(val start: Int, val end: Int, val rtl: Boolean)
|
|
1898
|
+
|
|
1899
|
+
companion object {
|
|
1900
|
+
internal const val QUOTE_BAR_WIDTH_DP = 3f
|
|
1901
|
+
|
|
1902
|
+
/**
|
|
1903
|
+
* Runs of adjacent "▎" lines, as [start, end] offsets (end = the last line's end, before its
|
|
1904
|
+
* \n). Adjacent lines merge into one bar only when [isRtlAt] gives them the same direction.
|
|
1905
|
+
*/
|
|
1906
|
+
internal fun quoteBarRegions(text: String, isRtlAt: (Int) -> Boolean): List<QuoteBarRegion> {
|
|
1907
|
+
val regions = mutableListOf<QuoteBarRegion>()
|
|
1908
|
+
var offset = 0
|
|
1909
|
+
for (line in text.split("\n")) {
|
|
1910
|
+
val lineEnd = offset + line.length
|
|
1911
|
+
if (line.startsWith("▎")) {
|
|
1912
|
+
val rtl = isRtlAt(offset)
|
|
1913
|
+
val last = regions.lastOrNull()
|
|
1914
|
+
if (last != null && offset <= last.end + 1 && last.rtl == rtl) {
|
|
1915
|
+
regions[regions.lastIndex] = last.copy(end = lineEnd)
|
|
1916
|
+
} else {
|
|
1917
|
+
regions.add(QuoteBarRegion(offset, lineEnd, rtl))
|
|
1918
|
+
}
|
|
1919
|
+
}
|
|
1920
|
+
offset = lineEnd + 1 // +1 for the \n
|
|
1921
|
+
}
|
|
1922
|
+
return regions
|
|
1923
|
+
}
|
|
1924
|
+
|
|
1925
|
+
/** The bar's left x: just inside the paragraph's start edge (the right one for RTL). */
|
|
1926
|
+
internal fun quoteBarX(rtl: Boolean, viewWidth: Int, paddingLeft: Int, paddingRight: Int, barWidth: Float): Float =
|
|
1927
|
+
if (rtl) viewWidth - paddingRight - barWidth - 1f else paddingLeft + 1f
|
|
1928
|
+
}
|
|
1929
|
+
|
|
1801
1930
|
// Synchronous style detection - emits current active styles to JS
|
|
1802
1931
|
/** Inline formatting present at [pos] — used to let replacement text inherit it. */
|
|
1803
1932
|
private fun captureInlineStylesAt(s: CharSequence, pos: Int): InlineStyleSnapshot? {
|
|
@@ -1923,13 +2052,7 @@ class RichTextEditorView(context: Context) : androidx.appcompat.widget.AppCompat
|
|
|
1923
2052
|
}
|
|
1924
2053
|
|
|
1925
2054
|
// Check alignment
|
|
1926
|
-
|
|
1927
|
-
alignment = when (span.alignment) {
|
|
1928
|
-
Layout.Alignment.ALIGN_CENTER -> "center"
|
|
1929
|
-
Layout.Alignment.ALIGN_OPPOSITE -> "right"
|
|
1930
|
-
else -> "left"
|
|
1931
|
-
}
|
|
1932
|
-
}
|
|
2055
|
+
alignmentSideAt(checkStart, checkEnd)?.let { alignment = it }
|
|
1933
2056
|
} // end else (textLen > 0)
|
|
1934
2057
|
}
|
|
1935
2058
|
|
|
@@ -2105,13 +2228,13 @@ class RichTextEditorView(context: Context) : androidx.appcompat.widget.AppCompat
|
|
|
2105
2228
|
var alignCenter = false
|
|
2106
2229
|
var alignRight = false
|
|
2107
2230
|
|
|
2108
|
-
|
|
2109
|
-
when (
|
|
2110
|
-
|
|
2231
|
+
alignmentSideAt(start, end)?.let { side ->
|
|
2232
|
+
when (side) {
|
|
2233
|
+
"center" -> {
|
|
2111
2234
|
alignLeft = false
|
|
2112
2235
|
alignCenter = true
|
|
2113
2236
|
}
|
|
2114
|
-
|
|
2237
|
+
"right" -> {
|
|
2115
2238
|
alignLeft = false
|
|
2116
2239
|
alignRight = true
|
|
2117
2240
|
}
|
|
@@ -3565,7 +3688,8 @@ class RichTextEditorView(context: Context) : androidx.appcompat.widget.AppCompat
|
|
|
3565
3688
|
private fun updatePlaceholderVisibility() {
|
|
3566
3689
|
val empty = isEffectivelyEmpty()
|
|
3567
3690
|
// Restore or clear hint to control visibility
|
|
3568
|
-
|
|
3691
|
+
val wanted = if (empty && placeholder.isNotEmpty()) hintText() else null
|
|
3692
|
+
if (hint?.toString() != wanted) hint = wanted
|
|
3569
3693
|
}
|
|
3570
3694
|
|
|
3571
3695
|
fun setVariant(value: String) {
|
|
@@ -4575,16 +4699,112 @@ class RichTextEditorView(context: Context) : androidx.appcompat.widget.AppCompat
|
|
|
4575
4699
|
outdent()
|
|
4576
4700
|
}
|
|
4577
4701
|
|
|
4702
|
+
// ENG-57281: Left/Right are the screen's sides, kept per paragraph (ScreenAlignmentSpan).
|
|
4578
4703
|
fun onAlignLeftClick() {
|
|
4579
|
-
setAlignment(
|
|
4704
|
+
setAlignment("left")
|
|
4580
4705
|
}
|
|
4581
4706
|
|
|
4582
4707
|
fun onAlignCenterClick() {
|
|
4583
|
-
setAlignment(
|
|
4708
|
+
setAlignment("center")
|
|
4584
4709
|
}
|
|
4585
4710
|
|
|
4586
4711
|
fun onAlignRightClick() {
|
|
4587
|
-
setAlignment(
|
|
4712
|
+
setAlignment("right")
|
|
4713
|
+
}
|
|
4714
|
+
|
|
4715
|
+
/** ENG-57281: the Layout alignment that shows screen [side] in the paragraph at [pos]. */
|
|
4716
|
+
private fun resolveSide(side: String, pos: Int): Layout.Alignment = when (side) {
|
|
4717
|
+
"center" -> Layout.Alignment.ALIGN_CENTER
|
|
4718
|
+
"right" -> if (isRtlParagraphAt(pos)) Layout.Alignment.ALIGN_NORMAL else Layout.Alignment.ALIGN_OPPOSITE
|
|
4719
|
+
else -> if (isRtlParagraphAt(pos)) Layout.Alignment.ALIGN_OPPOSITE else Layout.Alignment.ALIGN_NORMAL
|
|
4720
|
+
}
|
|
4721
|
+
|
|
4722
|
+
/**
|
|
4723
|
+
* ENG-57281: one ScreenAlignmentSpan per aligned paragraph, covering it exactly and resolved for
|
|
4724
|
+
* its current direction. Typing Arabic into a Right line flips its direction, so OPPOSITE would
|
|
4725
|
+
* now show on the left; Enter inside an aligned paragraph stretches its span over two
|
|
4726
|
+
* paragraphs (both keep the choice, as before, but each needs its own resolution); a
|
|
4727
|
+
* backspace-merge leaves two spans in one paragraph (the earlier one wins, as on iOS).
|
|
4728
|
+
*/
|
|
4729
|
+
private fun applyUserAlignment(s: Editable?) {
|
|
4730
|
+
s ?: return
|
|
4731
|
+
val spans = s.getSpans(0, s.length, ScreenAlignmentSpan::class.java)
|
|
4732
|
+
if (spans.isEmpty()) return
|
|
4733
|
+
data class Placed(val start: Int, val end: Int, val span: ScreenAlignmentSpan)
|
|
4734
|
+
val current = spans.map { Placed(s.getSpanStart(it), s.getSpanEnd(it), it) }.sortedBy { it.start }
|
|
4735
|
+
val wanted = mutableListOf<Placed>()
|
|
4736
|
+
var start = 0
|
|
4737
|
+
while (true) {
|
|
4738
|
+
val nl = android.text.TextUtils.indexOf(s, '\n', start)
|
|
4739
|
+
val end = if (nl < 0) s.length else nl
|
|
4740
|
+
// An empty paragraph keeps its choice on its own "\n" (see placeAlignment).
|
|
4741
|
+
val (from, to) = if (end > start) start to end else if (nl >= 0) nl to nl + 1 else -1 to -1
|
|
4742
|
+
if (from >= 0) {
|
|
4743
|
+
current.filter { it.start < to && it.end > from }.minByOrNull { it.start }?.let {
|
|
4744
|
+
wanted.add(Placed(from, to, ScreenAlignmentSpan(it.span.side, resolveSide(it.span.side, start))))
|
|
4745
|
+
}
|
|
4746
|
+
}
|
|
4747
|
+
if (nl < 0) break
|
|
4748
|
+
start = nl + 1
|
|
4749
|
+
}
|
|
4750
|
+
// Layout reads the alignment once per span change, so a stale span is replaced, never mutated.
|
|
4751
|
+
if (wanted == current) return
|
|
4752
|
+
current.forEach { s.removeSpan(it.span) }
|
|
4753
|
+
wanted.forEach { placeAlignment(s, it.span, it.start, it.end) }
|
|
4754
|
+
}
|
|
4755
|
+
|
|
4756
|
+
/**
|
|
4757
|
+
* ENG-57281: sets an alignment span on one paragraph. A paragraph with text gets a span over its
|
|
4758
|
+
* text. An EMPTY paragraph has no text to hold a span (Android drops a zero-length
|
|
4759
|
+
* EXCLUSIVE_EXCLUSIVE one), so its span covers its own terminating "\n" with an inclusive start:
|
|
4760
|
+
* text typed into the empty line lands inside it and keeps the choice, while Enter at the end of
|
|
4761
|
+
* the line above inserts at THAT line's "\n", before this span, so the new line stays unaligned.
|
|
4762
|
+
* Layout reads a paragraph's spans from [paraStart, paraEnd incl. its "\n"), so the span shows
|
|
4763
|
+
* on the empty line only.
|
|
4764
|
+
*/
|
|
4765
|
+
private fun placeAlignment(s: Editable, span: ScreenAlignmentSpan, from: Int, to: Int) {
|
|
4766
|
+
val holdsOnlyItsNewline = to - from == 1 && s[from] == '\n'
|
|
4767
|
+
s.setSpan(span, from, to, if (holdsOnlyItsNewline) Spanned.SPAN_INCLUSIVE_EXCLUSIVE else Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
|
|
4768
|
+
}
|
|
4769
|
+
|
|
4770
|
+
/**
|
|
4771
|
+
* ENG-57281: whether the paragraph at [pos] is right-to-left, the way the editor's FIRST_STRONG
|
|
4772
|
+
* text direction resolves it: its first strong letter, else the keyboard language's direction,
|
|
4773
|
+
* else the view's layout direction (applyContentAlignment).
|
|
4774
|
+
*/
|
|
4775
|
+
private fun isRtlParagraphAt(pos: Int): Boolean =
|
|
4776
|
+
firstStrongRtlAt(pos) ?: keyboardRtl ?: (layoutDirection == View.LAYOUT_DIRECTION_RTL)
|
|
4777
|
+
|
|
4778
|
+
/** ENG-57281: the first strong letter of the paragraph at [pos]: true RTL, false LTR, null none yet. */
|
|
4779
|
+
private fun firstStrongRtlAt(pos: Int): Boolean? {
|
|
4780
|
+
val t = text ?: return null
|
|
4781
|
+
val p = pos.coerceIn(0, t.length)
|
|
4782
|
+
var start = p
|
|
4783
|
+
while (start > 0 && t[start - 1] != '\n') start--
|
|
4784
|
+
var i = start
|
|
4785
|
+
while (i < t.length && t[i] != '\n') {
|
|
4786
|
+
when (Character.getDirectionality(t[i])) {
|
|
4787
|
+
Character.DIRECTIONALITY_RIGHT_TO_LEFT, Character.DIRECTIONALITY_RIGHT_TO_LEFT_ARABIC -> return true
|
|
4788
|
+
Character.DIRECTIONALITY_LEFT_TO_RIGHT -> return false
|
|
4789
|
+
}
|
|
4790
|
+
i++
|
|
4791
|
+
}
|
|
4792
|
+
return null
|
|
4793
|
+
}
|
|
4794
|
+
|
|
4795
|
+
/** The toolbar alignment ("left"/"right"/"center") of the text in [from, to], or null if unaligned. */
|
|
4796
|
+
internal fun alignmentSideAt(from: Int, to: Int): String? {
|
|
4797
|
+
val spannable = text as? Spanned ?: return null
|
|
4798
|
+
val span = spannable.getSpans(from, to, AlignmentSpan::class.java).firstOrNull() ?: return null
|
|
4799
|
+
// The user's own choice; a plain AlignmentSpan (e.g. pasted in) is read for this paragraph.
|
|
4800
|
+
return (span as? ScreenAlignmentSpan)?.side ?: screenSide(span.alignment, from)
|
|
4801
|
+
}
|
|
4802
|
+
|
|
4803
|
+
/** ENG-57281: the screen side ("left"/"right"/"center") an AlignmentSpan shows at [pos]. */
|
|
4804
|
+
private fun screenSide(alignment: Layout.Alignment, pos: Int): String = when (alignment) {
|
|
4805
|
+
Layout.Alignment.ALIGN_CENTER -> "center"
|
|
4806
|
+
Layout.Alignment.ALIGN_OPPOSITE -> if (isRtlParagraphAt(pos)) "left" else "right"
|
|
4807
|
+
else -> if (isRtlParagraphAt(pos)) "right" else "left"
|
|
4588
4808
|
}
|
|
4589
4809
|
|
|
4590
4810
|
// ==================== Helper Methods ====================
|
|
@@ -5972,16 +6192,33 @@ class RichTextEditorView(context: Context) : androidx.appcompat.widget.AppCompat
|
|
|
5972
6192
|
sendContentChange()
|
|
5973
6193
|
}
|
|
5974
6194
|
|
|
5975
|
-
|
|
6195
|
+
/** ENG-57281: [side] is the screen side ("left"/"right"/"center"), set on each selected paragraph. */
|
|
6196
|
+
fun setAlignment(side: String) {
|
|
5976
6197
|
val (lineStart, lineEnd) = getLineRange()
|
|
5977
6198
|
val spannable = text as? Editable ?: return
|
|
5978
6199
|
|
|
5979
6200
|
isInternalChange = true
|
|
5980
6201
|
// Remove existing alignment spans
|
|
5981
|
-
spannable.getSpans(lineStart, lineEnd, AlignmentSpan
|
|
6202
|
+
spannable.getSpans(lineStart, lineEnd, AlignmentSpan::class.java)
|
|
5982
6203
|
.forEach { spannable.removeSpan(it) }
|
|
5983
6204
|
|
|
5984
|
-
|
|
6205
|
+
// One span per paragraph, each resolved for its own direction: a single span over an
|
|
6206
|
+
// English and an Arabic line would show OPPOSITE on opposite sides.
|
|
6207
|
+
var start = lineStart
|
|
6208
|
+
while (start <= lineEnd) {
|
|
6209
|
+
val nl = android.text.TextUtils.indexOf(spannable, '\n', start, lineEnd)
|
|
6210
|
+
val end = if (nl < 0) lineEnd else nl
|
|
6211
|
+
val span = ScreenAlignmentSpan(side, resolveSide(side, start))
|
|
6212
|
+
if (end > start) {
|
|
6213
|
+
placeAlignment(spannable, span, start, end)
|
|
6214
|
+
} else if (nl >= 0) {
|
|
6215
|
+
// An empty line inside the selection: typing into it later keeps the choice, as the
|
|
6216
|
+
// single span over the whole selection did before.
|
|
6217
|
+
placeAlignment(spannable, span, nl, nl + 1)
|
|
6218
|
+
}
|
|
6219
|
+
if (nl < 0) break
|
|
6220
|
+
start = nl + 1
|
|
6221
|
+
}
|
|
5985
6222
|
isInternalChange = false
|
|
5986
6223
|
sendContentChange()
|
|
5987
6224
|
updateToolbarButtonStates()
|
|
@@ -6506,6 +6743,7 @@ class RichTextEditorView(context: Context) : androidx.appcompat.widget.AppCompat
|
|
|
6506
6743
|
|
|
6507
6744
|
override fun onDetachedFromWindow() {
|
|
6508
6745
|
super.onDetachedFromWindow()
|
|
6746
|
+
keyboardWatcher.stop()
|
|
6509
6747
|
hideToolbar()
|
|
6510
6748
|
toolbarPopup = null
|
|
6511
6749
|
}
|