@@ -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,31 @@ 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+ return ;
105+ }
106+
107+ nodeToSet = node ;
108+ offsetToSet = nodeOffset ;
102109 }
103110
104- this . set ( nodeToSet as HTMLElement , offset ) ;
111+ this . set ( nodeToSet as HTMLElement , offsetToSet ) ;
105112
106113 BlockManager . setCurrentBlockByChildNode ( block . holder ) ;
107- BlockManager . currentBlock . currentInput = element ;
114+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
115+ BlockManager . currentBlock ! . currentInput = element ;
108116 }
109117
110118 /**
0 commit comments