3
3
*/
4
4
import './index.css' ;
5
5
import { IconInlineCode } from '@codexteam/icons'
6
-
6
+ import { isTextEmpty , isCaretAtLastPosition , isTextNode } from './utils'
7
7
/**
8
8
* Inline Code Tool for the Editor.js
9
9
*
@@ -21,7 +21,7 @@ export default class InlineCode {
21
21
22
22
/**
23
23
*/
24
- constructor ( { api} ) {
24
+ constructor ( { api, config } ) {
25
25
this . api = api ;
26
26
27
27
/**
@@ -47,45 +47,23 @@ export default class InlineCode {
47
47
active : this . api . styles . inlineToolButtonActive
48
48
} ;
49
49
50
- window . addEventListener ( "keydown" , this . keyListener )
50
+ document . querySelector ( config . containerSelector ) ? .addEventListener ( "keydown" , this . keyListener )
51
51
}
52
52
53
53
54
- isTextEmpty = ( textContent ) => {
55
- return / ^ \s * $ / . test ( textContent )
56
- }
57
-
58
54
keyListener = ( e ) => {
59
55
const codeWrapper = this . api . selection . findParentTag ( this . tag , InlineCode . CSS )
60
- if ( e . key === "ArrowRight" && codeWrapper ) {
61
- if ( ! this . isCaretAtLastPosition ( codeWrapper ) ) return
56
+ if ( e . key !== "ArrowRight" || ! codeWrapper ) return
57
+ if ( ! isCaretAtLastPosition ( codeWrapper ) ) return
62
58
63
- if ( codeWrapper . nextElementSibling ) return
64
-
65
- if ( ! codeWrapper . nextSibling ) {
66
- const emptyChar = document . createTextNode ( "\u00A0" )
67
- codeWrapper . parentElement . appendChild ( emptyChar )
68
-
69
- } else {
70
- if ( this . isTextEmpty ( codeWrapper . nextSibling . data ) ) {
71
- codeWrapper . nextSibling . insertData ( 0 , "\u00A0" )
72
- }
59
+ if ( ! isTextNode ( codeWrapper . nextSibling ) ) {
60
+ codeWrapper . insertAdjacentText ( "afterend" , "\u00A0" )
61
+ } else {
62
+ if ( isTextEmpty ( codeWrapper . nextSibling . data ) ) {
63
+ codeWrapper . nextSibling . replaceWith ( "\u00A0" )
73
64
}
74
65
}
75
66
}
76
-
77
- isCaretAtLastPosition ( element ) {
78
- // Get the selection object
79
- const selection = window . getSelection ( ) ;
80
-
81
- if ( selection . rangeCount === 0 ) return false ;
82
-
83
- // Get the range object
84
- const range = selection . getRangeAt ( 0 ) ;
85
-
86
- // Check if the end offset of the range is equal to the length of the text content
87
- return range . endOffset === element . textContent . length ;
88
- }
89
67
/**
90
68
* Specifies Tool as Inline Toolbar Tool
91
69
*
0 commit comments