@@ -195,14 +195,7 @@ open class VEditorNode: ASDisplayNode, ASTableDelegate, ASTableDataSource {
195195
196196 cellNode. rx. failed
197197 . observeOn ( MainScheduler . instance)
198- . subscribe ( onNext: { [ weak self] indexPath in
199- self ? . editorContents. remove ( at: indexPath. row)
200- self ? . tableNode. deleteRows ( at: [ indexPath] , with: . automatic)
201- self ? . mergeTextContents ( target: indexPath,
202- to: . init( row: indexPath. row - 1 ,
203- section: indexPath. section) ,
204- animated: false )
205- } )
198+ . bind ( to: self . rx. deleteContent ( animated: true ) )
206199 . disposed ( by: cellNode. disposeBag)
207200
208201 return cellNode
@@ -573,19 +566,22 @@ open class VEditorNode: ASDisplayNode, ASTableDelegate, ASTableDataSource {
573566 - to: attach text node indexPath
574567 - animated: remove node animation
575568 */
576- open func mergeTextContents( target: IndexPath ,
577- to: IndexPath ,
578- animated: Bool ) {
579-
580- guard let targetNode = tableNode. nodeForRow ( at: target) as? VEditorTextCellNode ,
581- let sourceNode = tableNode. nodeForRow ( at: to) as? VEditorTextCellNode ,
569+ open func mergeTextContents( from deleteTargetIndexPath: IndexPath ,
570+ to mergeTargetIndexPath: IndexPath ) -> Bool {
571+ guard deleteTargetIndexPath. row < self . editorContents. count,
572+ mergeTargetIndexPath. row < self . editorContents. count,
573+ deleteTargetIndexPath. row >= 0 ,
574+ mergeTargetIndexPath. row >= 0 else { return false }
575+
576+ guard let targetNode = tableNode. nodeForRow ( at: deleteTargetIndexPath) as? VEditorTextCellNode ,
577+ let sourceNode = tableNode. nodeForRow ( at: mergeTargetIndexPath) as? VEditorTextCellNode ,
582578 let targetAttributedText = targetNode. textNode
583579 . textStorage?
584580 . internalAttributedString,
585581 let sourceAttributedText = sourceNode. textNode
586582 . textStorage?
587583 . internalAttributedString else {
588- return
584+ return false
589585 }
590586
591587 // NOTE: make merged attributedText
@@ -595,12 +591,10 @@ open class VEditorNode: ASDisplayNode, ASTableDelegate, ASTableDataSource {
595591 mutableAttrText. append ( NSAttributedString . init ( string: " \n " ,
596592 attributes: newlineAttribute) )
597593 mutableAttrText. append ( targetAttributedText)
598-
599- // NOTE: update editor
600- self . editorContents. remove ( at: target. row)
601- self . tableNode. deleteRows ( at: [ target] , with: animated ? . automatic: . none)
602594 sourceNode. textNode. textStorage? . setAttributedString ( mutableAttrText)
603595 sourceNode. textNode. setNeedsLayout ( )
596+
597+ return true
604598 }
605599
606600 /**
@@ -612,31 +606,31 @@ open class VEditorNode: ASDisplayNode, ASTableDelegate, ASTableDataSource {
612606 open func deleteTargetContent( _ indexPath: IndexPath ? , animated: Bool ) {
613607 guard let indexPath = indexPath,
614608 indexPath. row < self . editorContents. count else { return }
615- self . editorContents . remove ( at : indexPath. row )
616- self . tableNode . performBatch ( animated : animated , updates : {
617- self . tableNode . deleteRows ( at : [ indexPath] , with : animated ? . automatic : . none )
618- } , completion : { fin in
619- guard fin else { return }
620- let beforeIndex : Int = indexPath . row - 1
621-
622- guard indexPath . row < self . editorContents . count ,
623- beforeIndex >= 0 else { return }
624-
625- let beforeCell = self . tableNode
626- . nodeForRow ( at : . init ( row : beforeIndex ,
627- section : indexPath . section ) ) as? VEditorTextCellNode
628- let currentCell = self . tableNode
629- . nodeForRow ( at : . init ( row : indexPath . row ,
630- section : indexPath . section ) ) as? VEditorTextCellNode
631-
632- guard let target = currentCell ? . indexPath ,
633- let to = beforeCell ? . indexPath else {
634- return
635- }
636-
637- self . mergeTextContents ( target : target ,
638- to : to ,
639- animated : animated )
609+ var deleteIndexPaths : [ IndexPath ] = [ indexPath]
610+ let beforeIndex = IndexPath ( row : indexPath . row - 1 , section : indexPath . section )
611+ let afterIndex = IndexPath ( row : indexPath. row + 1 , section : indexPath . section )
612+
613+ if self . mergeTextContents ( from : afterIndex , to : beforeIndex ) {
614+ deleteIndexPaths . append ( afterIndex )
615+ self . editorContents . removeSubrange ( indexPath . row ... afterIndex . row )
616+ } else {
617+ self . editorContents . remove ( at : indexPath . row )
618+ }
619+
620+ for deleteIndexPath in deleteIndexPaths {
621+ self . tableNode . view . cellForRow ( at : deleteIndexPath ) ? . removeFromSuperview ( )
622+ }
623+
624+ self . tableNode . performBatchUpdates ( {
625+ self . tableNode . deleteRows ( at : deleteIndexPaths ,
626+ with : animated ? . automatic : . none )
627+ } , completion : { _ in
628+ // Texture Bug Issue, ref: https://github.com/TextureGroup/Texture/issues/1407
629+ _ = self . tableNode . view . subviews
630+ . filter ( { $0 is UITableViewCell } )
631+ . filter { $0 . subviews . first ? . subviews . isEmpty ?? false ||
632+ $0 . subviews . first ? . subviews . first is UIImageView }
633+ . map { $0 . removeFromSuperview ( ) }
640634 } )
641635 }
642636
0 commit comments