@@ -43,7 +43,7 @@ export default class Caret extends Module {
4343 * @param {Block } block - Block class
4444 * @param {string } position - position where to set caret.
4545 * If default - leave default behaviour and apply offset if it's passed
46- * @param {number } offset - caret offset regarding to the text node
46+ * @param {number } offset - caret offset regarding to the block content
4747 */
4848 public setToBlock ( block : Block , position : string = this . positions . DEFAULT , offset = 0 ) : void {
4949 const { BlockManager, BlockSelection } = this . Editor ;
@@ -88,23 +88,32 @@ export default class Caret extends Module {
8888 return ;
8989 }
9090
91- const nodeToSet = $ . getDeepestNode ( element , position === this . positions . END ) ;
92- const contentLength = $ . getContentLength ( nodeToSet ) ;
91+ let nodeToSet : Node ;
92+ let offsetToSet = offset ;
9393
94- switch ( true ) {
95- case position === this . positions . START :
96- offset = 0 ;
97- break ;
98- case position === this . positions . END :
99- case offset > contentLength :
100- offset = contentLength ;
101- break ;
94+ if ( position === this . positions . START ) {
95+ nodeToSet = $ . getDeepestNode ( element , false ) as Node ;
96+ offsetToSet = 0 ;
97+ } else if ( position === this . positions . END ) {
98+ nodeToSet = $ . getDeepestNode ( element , true ) as Node ;
99+ offsetToSet = $ . getContentLength ( nodeToSet ) ;
100+ } else {
101+ const { node, offset : nodeOffset } = $ . getNodeByOffset ( element , offset ) ;
102+
103+ if ( node ) {
104+ nodeToSet = node ;
105+ offsetToSet = nodeOffset ;
106+ } else { // case for empty block's input
107+ nodeToSet = $ . getDeepestNode ( element , false ) as Node ;
108+ offsetToSet = 0 ;
109+ }
102110 }
103111
104- this . set ( nodeToSet as HTMLElement , offset ) ;
112+ this . set ( nodeToSet as HTMLElement , offsetToSet ) ;
105113
106114 BlockManager . setCurrentBlockByChildNode ( block . holder ) ;
107- BlockManager . currentBlock . currentInput = element ;
115+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
116+ BlockManager . currentBlock ! . currentInput = element ;
108117 }
109118
110119 /**
0 commit comments