@@ -10,7 +10,8 @@ import {
1010 IndexBuilder ,
1111 type ModelEvents ,
1212 TextAddedEvent ,
13- TextRemovedEvent
13+ TextRemovedEvent ,
14+ Index
1415} from '@editorjs/model' ;
1516import type {
1617 EventBus ,
@@ -138,8 +139,6 @@ export class BlockToolAdapter implements BlockToolAdapterInterface {
138139
139140 builder . addBlockIndex ( this . #blockIndex) . addDataKey ( key ) ;
140141
141- this . #caretAdapter. attachInput ( input , builder . build ( ) ) ;
142-
143142 const value = this . #model. getText ( this . #blockIndex, key ) ;
144143 const fragments = this . #model. getFragments ( this . #blockIndex, key ) ;
145144
@@ -167,12 +166,6 @@ export class BlockToolAdapter implements BlockToolAdapterInterface {
167166 * @todo Let BlockTool handle DOM update
168167 */
169168 input . remove ( ) ;
170- this . #caretAdapter. detachInput (
171- new IndexBuilder ( )
172- . addBlockIndex ( this . #blockIndex)
173- . addDataKey ( key )
174- . build ( )
175- ) ;
176169
177170 this . #attachedInputs. delete ( key ) ;
178171
@@ -481,7 +474,21 @@ export class BlockToolAdapter implements BlockToolAdapterInterface {
481474
482475 const isInputNative = isNativeInput ( input ) ;
483476 let start : number ;
484- let end : number ;
477+
478+ /**
479+ * @todo support input merging
480+ */
481+
482+ /**
483+ * In all cases we need to handle delete selected text if range is not collapsed
484+ */
485+ if ( ! range . collapsed ) {
486+ if ( isInputNative ) {
487+ this . #handleDeleteInNativeInput( payload , input as HTMLInputElement | HTMLTextAreaElement , key , range ) ;
488+ } else {
489+ this . #handleDeleteInContentEditable( input , key , range ) ;
490+ }
491+ }
485492
486493 switch ( inputType ) {
487494 case InputType . InsertReplacementText :
@@ -523,25 +530,13 @@ export class BlockToolAdapter implements BlockToolAdapterInterface {
523530 case InputType . DeleteEntireSoftLine :
524531 case InputType . DeleteWordBackward :
525532 case InputType . DeleteWordForward : {
526- if ( isInputNative === true ) {
527- this . #handleDeleteInNativeInput( payload , input as HTMLInputElement | HTMLTextAreaElement , key , range ) ;
528- } else {
529- this . #handleDeleteInContentEditable( input , key , range ) ;
530- }
533+ /**
534+ * We already handle delete above
535+ */
531536 break ;
532537 }
533538
534539 case InputType . InsertParagraph :
535- console . log ( 'insert paragraph' , input ) ;
536-
537- if ( isInputNative ) {
538- // start = (input as HTMLInputElement | HTMLTextAreaElement).selectionStart as number;
539- this . #handleDeleteInNativeInput( payload , input as HTMLInputElement | HTMLTextAreaElement , key , range ) ;
540- } else {
541- this . #handleDeleteInContentEditable( input , key , range , true ) ;
542- // start = getAbsoluteRangeOffset(input, range.startContainer, range.startOffset);
543- }
544-
545540 /**
546541 *
547542 */
@@ -767,4 +762,29 @@ export class BlockToolAdapter implements BlockToolAdapterInterface {
767762 this . #handleModelUpdateForContentEditableElement( event , input , dataKey ! ) ;
768763 }
769764 } ;
765+
766+ /**
767+ * Public getter for block index.
768+ * Can be used to find a particular block, for example, in caret adapter
769+ */
770+ public getBlockIndex ( ) : Index {
771+ return new IndexBuilder ( )
772+ . addBlockIndex ( this . #blockIndex)
773+ . build ( ) ;
774+ }
775+
776+ /**
777+ * Public getter for all attached inputs.
778+ * Can be used to loop through all inputs to find a particular input(s)
779+ */
780+ public getAttachedInputs ( ) : Map < DataKey , HTMLElement > {
781+ return this . #attachedInputs;
782+ }
783+
784+ /**
785+ * Allows access to a particular input by key
786+ */
787+ public getInput ( key : DataKey ) : HTMLElement | undefined {
788+ return this . #attachedInputs. get ( key ) ;
789+ }
770790}
0 commit comments