处理键盘长按删除 撤销出现的bug

This commit is contained in:
2025-12-23 18:05:01 +08:00
parent 73d6ec933a
commit 6a539dc3c5
6 changed files with 141 additions and 126 deletions

View File

@@ -770,18 +770,21 @@ static void KBULDarwinCallback(CFNotificationCenterRef center, void *observer, C
- (void)kb_fullAccessChanged {
dispatch_async(dispatch_get_main_queue(), ^{ [self kb_refreshPasteboardMonitor]; });
}
- (void)onTapDelete {
- (void)onTapDelete {
NSLog(@"点击:删除");
[[KBBackspaceUndoManager shared] registerNonClearAction];
UIInputViewController *ivc = KBFindInputViewController(self);
id<UITextDocumentProxy> proxy = ivc.textDocumentProxy;
[[KBBackspaceUndoManager shared] recordDeletionSnapshotBefore:proxy.documentContextBeforeInput
after:proxy.documentContextAfterInput];
[proxy deleteBackward];
}
- (void)onTapClear {
NSLog(@"点击:清空");
[self.backspaceHandler performClearAction];
}
- (void)onTapSend {
- (void)onTapSend {
NSLog(@"点击:发送");
[[KBBackspaceUndoManager shared] registerNonClearAction];
// App

View File

@@ -14,6 +14,7 @@
#import "KBSuggestionBarView.h"
#import "Masonry.h"
#import "KBSkinManager.h"
#import "KBBackspaceUndoManager.h"
@interface KBKeyBoardMainView ()<KBToolBarDelegate, KBKeyboardViewDelegate, KBEmojiPanelViewDelegate, KBSuggestionBarViewDelegate>
@property (nonatomic, strong) KBToolBar *topBar;
@@ -87,10 +88,18 @@
make.top.equalTo(self.topBar.mas_bottom).offset(barSpacing);
}];
// /
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(kb_undoStateChanged)
name:KBBackspaceUndoStateDidChangeNotification
object:nil];
}
return self;
}
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)setEmojiPanelVisible:(BOOL)visible animated:(BOOL)animated {
if (self.emojiPanelVisible == visible) return;
self.emojiPanelVisible = visible;
@@ -108,7 +117,7 @@
self.emojiView.alpha = visible ? 1.0 : 0.0;
self.keyboardView.alpha = visible ? 0.0 : 1.0;
self.topBar.alpha = visible ? 0.0 : 1.0;
self.suggestionBar.alpha = visible ? 0.0 : (self.suggestionBarHasItems ? 1.0 : 0.0);
self.suggestionBar.alpha = visible ? 0.0 : ([self kb_shouldShowSuggestions] ? 1.0 : 0.0);
};
void (^completion)(BOOL) = ^(BOOL finished) {
self.emojiView.hidden = !visible;
@@ -117,7 +126,7 @@
if (visible) {
self.suggestionBar.hidden = YES;
} else {
self.suggestionBar.hidden = !self.suggestionBarHasItems;
self.suggestionBar.hidden = ![self kb_shouldShowSuggestions];
}
};
@@ -258,14 +267,7 @@
- (void)kb_setSuggestions:(NSArray<NSString *> *)suggestions {
self.suggestionBarHasItems = (suggestions.count > 0);
[self.suggestionBar updateSuggestions:suggestions];
if (self.emojiPanelVisible) {
self.suggestionBar.hidden = YES;
self.suggestionBar.alpha = 0.0;
} else {
self.suggestionBar.hidden = !self.suggestionBarHasItems;
self.suggestionBar.alpha = self.suggestionBarHasItems ? 1.0 : 0.0;
}
[self kb_applySuggestionVisibility];
}
#pragma mark - KBSuggestionBarViewDelegate
@@ -276,4 +278,22 @@
}
}
- (void)kb_undoStateChanged {
[self kb_applySuggestionVisibility];
}
- (BOOL)kb_shouldShowSuggestions {
if (self.emojiPanelVisible) { return NO; }
if (![KBBackspaceUndoManager shared].hasUndo && self.suggestionBarHasItems) {
return YES;
}
return NO;
}
- (void)kb_applySuggestionVisibility {
BOOL shouldShow = [self kb_shouldShowSuggestions];
self.suggestionBar.hidden = !shouldShow;
self.suggestionBar.alpha = shouldShow ? 1.0 : 0.0;
}
@end