fix: emoji picker set wrong selected range after insert text issue

This commit is contained in:
CMK 2021-06-30 15:30:25 +08:00
parent 9190b27163
commit 6126c15c66
5 changed files with 23 additions and 14 deletions

View File

@ -4778,7 +4778,7 @@
repositoryURL = "https://github.com/TwidereProject/MetaTextView.git";
requirement = {
kind = exactVersion;
version = 1.2.2;
version = 1.2.3;
};
};
DB0E2D2C26833FF600865C3C /* XCRemoteSwiftPackageReference "Nuke-FLAnimatedImage-Plugin" */ = {

View File

@ -12,7 +12,7 @@
<key>CoreDataStack.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>20</integer>
<integer>21</integer>
</dict>
<key>Mastodon - ASDK.xcscheme_^#shared#^_</key>
<dict>
@ -37,7 +37,7 @@
<key>NotificationService.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>21</integer>
<integer>22</integer>
</dict>
</dict>
<key>SuppressBuildableAutocreation</key>

View File

@ -114,8 +114,8 @@
"repositoryURL": "https://github.com/TwidereProject/MetaTextView.git",
"state": {
"branch": null,
"revision": "d48cf6a2479ce6fc4f836b6c4d7ba855cdbc71cc",
"version": "1.2.2"
"revision": "5b86b386464be8a6da5383aa714c458c07da6c01",
"version": "1.2.3"
}
},
{

View File

@ -58,14 +58,8 @@ extension ComposeStatusSection {
}
protocol CustomEmojiReplaceableTextInput: AnyObject {
protocol CustomEmojiReplaceableTextInput: UITextInput & UIResponder {
var inputView: UIView? { get set }
func reloadInputViews()
// UIKeyInput
func insertText(_ text: String)
// UIResponder
var isFirstResponder: Bool { get }
}
class CustomEmojiReplaceableTextInputReference {

View File

@ -46,8 +46,23 @@ extension CustomEmojiPickerInputViewModel {
removeEmptyReferences()
for reference in customEmojiReplaceableTextInputReferences {
guard reference.value?.isFirstResponder == true else { continue }
reference.value?.insertText(text)
guard let textInput = reference.value else { continue }
guard textInput.isFirstResponder == true else { continue }
let selectedTextRange = textInput.selectedTextRange
textInput.insertText(text)
// due to insert text render as attachment
// the cursor reset logic not works
// hack with hard code +2 offset
assert(text.hasSuffix(": "))
if text.hasPrefix(":") && text.hasSuffix(": "),
let selectedTextRange = selectedTextRange,
let newPosition = textInput.position(from: selectedTextRange.start, offset: 2) {
let newSelectedTextRange = textInput.textRange(from: newPosition, to: newPosition)
textInput.selectedTextRange = newSelectedTextRange
}
return reference
}