Skip to content
This repository was archived by the owner on Mar 7, 2025. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion ios/RNTAztecView/RCTAztecView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import UIKit

class RCTAztecView: Aztec.TextView {
@objc var onBackspace: RCTBubblingEventBlock? = nil
@objc var onDelete: RCTBubblingEventBlock? = nil
@objc var onChange: RCTBubblingEventBlock? = nil
@objc var onEnter: RCTBubblingEventBlock? = nil
@objc var onFocus: RCTBubblingEventBlock? = nil
Expand Down Expand Up @@ -73,6 +74,23 @@ class RCTAztecView: Aztec.TextView {
updatePlaceholderVisibility()
}

/// The first rule of Fight Club is: You do not talk about Fight Club.
///
@objc func _deleteForwardAndNotify(_ notify: Bool) {

guard !interceptForwardDelete() else {
return
}

let selector = #selector(RCTAztecView._deleteForwardAndNotify(_:))
let imp = class_getMethodImplementation(TextView.superclass(), selector)

typealias ClosureType = @convention(c) (AnyObject, Selector, Bool) -> Void
let superMethod: ClosureType = unsafeBitCast(imp, to: ClosureType.self)

superMethod(self, selector, notify)
}

open override func deleteBackward() {
guard !interceptBackspace() else {
return
Expand All @@ -95,8 +113,19 @@ class RCTAztecView: Aztec.TextView {
return true
}

private func interceptForwardDelete() -> Bool {
guard selectedRange.location == attributedText.length && selectedRange.length == 0,
let onDelete = onDelete else {
return false
}

let caretData = packCaretDataForRN()
onDelete(caretData)
return true
}

private func interceptBackspace() -> Bool {
guard selectedRange.location == 0,
guard selectedRange.location == 0 && selectedRange.length == 0,
let onBackspace = onBackspace else {
return false
}
Expand Down
1 change: 1 addition & 0 deletions ios/RNTAztecView/RCTAztecViewManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ @interface RCT_EXTERN_MODULE(RCTAztecViewManager, NSObject)
RCT_REMAP_VIEW_PROPERTY(text, contents, NSDictionary)
RCT_EXPORT_VIEW_PROPERTY(onContentSizeChange, RCTBubblingEventBlock)
RCT_EXPORT_VIEW_PROPERTY(onBackspace, RCTBubblingEventBlock)
RCT_EXPORT_VIEW_PROPERTY(onDelete, RCTBubblingEventBlock)
RCT_EXPORT_VIEW_PROPERTY(onChange, RCTBubblingEventBlock)
RCT_EXPORT_VIEW_PROPERTY(onEnter, RCTBubblingEventBlock)
RCT_EXPORT_VIEW_PROPERTY(onFocus, RCTBubblingEventBlock)
Expand Down
11 changes: 11 additions & 0 deletions src/AztecView.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class AztecView extends React.Component {
onContentSizeChange: PropTypes.func,
onEnter: PropTypes.func,
onBackspace: PropTypes.func,
onDelete: PropTypes.func,
onScroll: PropTypes.func,
onActiveFormatsChange: PropTypes.func,
onSelectionChange: PropTypes.func,
Expand Down Expand Up @@ -79,6 +80,15 @@ class AztecView extends React.Component {
onBackspace(event);
}

_onDelete = (event) => {
if (!this.props.onDelete) {
return;
}

const { onDelete } = this.props;
onDelete(event);
}

_onHTMLContentWithCursor = (event) => {
if (!this.props.onHTMLContentWithCursor) {
return;
Expand Down Expand Up @@ -149,6 +159,7 @@ class AztecView extends React.Component {
onFocus = { () => {} }
onBlur = { this._onBlur }
onBackspace = { this._onBackspace }
onDelete = { this._onDelete }
/>
</TouchableWithoutFeedback>
);
Expand Down