@@ -3,10 +3,10 @@ import type {
3
3
InlineTool ,
4
4
InlineToolConstructorOptions ,
5
5
} from '@editorjs/editorjs' ;
6
- import EditorJSStyleElement from './EditorJSStyleElement' ;
7
- import EditorJSStyleError from './EditorJSStyleError' ;
6
+ import { EditorJSStyleElement } from './EditorJSStyleElement' ;
7
+ import { EditorJSStyleError } from './EditorJSStyleError' ;
8
8
9
- class EditorJSStyle implements InlineTool {
9
+ class StyleInlineTool implements InlineTool {
10
10
static get isInline ( ) {
11
11
return true ;
12
12
}
@@ -25,18 +25,20 @@ class EditorJSStyle implements InlineTool {
25
25
return 'Style' ;
26
26
}
27
27
28
+ static prepare ( ) {
29
+ if ( customElements . get ( 'editorjs-style' ) ) {
30
+ return ;
31
+ }
32
+
33
+ customElements . define ( 'editorjs-style' , EditorJSStyleElement ) ;
34
+ }
35
+
28
36
#actions: HTMLDivElement ;
29
37
#api: API ;
30
38
31
39
constructor ( { api } : InlineToolConstructorOptions ) {
32
40
this . #actions = document . createElement ( 'div' ) ;
33
41
this . #api = api ;
34
-
35
- if ( customElements . get ( 'editorjs-style' ) ) {
36
- return ;
37
- }
38
-
39
- customElements . define ( 'editorjs-style' , EditorJSStyleElement ) ;
40
42
}
41
43
42
44
get shortcut ( ) {
@@ -46,14 +48,16 @@ class EditorJSStyle implements InlineTool {
46
48
checkState ( ) {
47
49
this . #actions. innerHTML = '' ;
48
50
49
- const editorjsStyle = this . #api. selection . findParentTag ( 'EDITORJS-STYLE' ) ;
51
+ const editorJSStyleElement = this . #api. selection . findParentTag (
52
+ 'EDITORJS-STYLE'
53
+ ) ;
50
54
51
- if ( ! editorjsStyle ) {
55
+ if ( ! editorJSStyleElement ) {
52
56
return false ;
53
57
}
54
58
55
59
this . #actions. innerHTML = `
56
- <div style="margin-left: 0.5rem ; ">
60
+ <div style="margin-bottom: 16px; margin- left: 16px; margin-right: 16px ; ">
57
61
<div style="display: flex; align-items: center; justify-content: space-between; ">
58
62
<div>Style settings</div>
59
63
@@ -72,15 +76,15 @@ class EditorJSStyle implements InlineTool {
72
76
73
77
<input class="id-input ${
74
78
this . #api. styles . input
75
- } " placeholder="exciting" style="width: 80%; ">
79
+ } " style="width: 80%; ">
76
80
</label>
77
81
78
82
<label style="display: flex; align-items: center; justify-content: space-between; ">
79
83
<span>Class</span>
80
84
81
85
<input class="class-input ${
82
86
this . #api. styles . input
83
- } " placeholder="note editorial" style="width: 80%; ">
87
+ } " style="width: 80%; ">
84
88
</label>
85
89
86
90
<label style="display: flex; align-items: center; justify-content: space-between; ">
@@ -116,14 +120,18 @@ class EditorJSStyle implements InlineTool {
116
120
}
117
121
118
122
deleteButton . addEventListener ( 'click' , ( ) => {
119
- const clonedNodes = Array . from ( editorjsStyle . childNodes ) . map ( ( node ) =>
120
- node . cloneNode ( true )
121
- ) ;
123
+ const clonedNodes = Array . from (
124
+ editorJSStyleElement . childNodes
125
+ ) . map ( ( node ) => node . cloneNode ( true ) ) ;
122
126
123
127
clonedNodes . forEach ( ( node ) =>
124
- editorjsStyle . parentNode ?. insertBefore ( node , editorjsStyle )
128
+ editorJSStyleElement . parentNode ?. insertBefore (
129
+ node ,
130
+ editorJSStyleElement
131
+ )
125
132
) ;
126
- editorjsStyle . remove ( ) ;
133
+
134
+ editorJSStyleElement . remove ( ) ;
127
135
128
136
if ( clonedNodes . length === 0 ) {
129
137
return ;
@@ -143,31 +151,36 @@ class EditorJSStyle implements InlineTool {
143
151
range . setEndAfter ( clonedNodes [ clonedNodes . length - 1 ] ) ;
144
152
145
153
selection . addRange ( range ) ;
154
+ this . #actions. innerHTML = '' ;
155
+ this . #api. tooltip . hide ( ) ;
146
156
} ) ;
147
157
148
158
this . #api. tooltip . onHover ( deleteButton , 'Delete style' , {
149
159
placement : 'top' ,
150
160
} ) ;
151
161
152
- classInput . value = editorjsStyle . className ;
162
+ classInput . value = editorJSStyleElement . className ;
153
163
154
164
classInput . addEventListener ( 'input' , ( ) =>
155
- editorjsStyle . setAttribute ( 'class' , classInput . value )
165
+ editorJSStyleElement . setAttribute ( 'class' , classInput . value )
156
166
) ;
157
167
158
- idInput . value = editorjsStyle . id ;
168
+ idInput . value = editorJSStyleElement . id ;
159
169
160
- idInput . addEventListener ( 'input' , ( ) => ( editorjsStyle . id = idInput . value ) ) ;
170
+ idInput . addEventListener (
171
+ 'input' ,
172
+ ( ) => ( editorJSStyleElement . id = idInput . value )
173
+ ) ;
161
174
162
- styleTextarea . value = editorjsStyle . getAttribute ( 'style' ) ?? '' ;
175
+ styleTextarea . value = editorJSStyleElement . getAttribute ( 'style' ) ?? '' ;
163
176
164
177
// To input line breaks
165
178
styleTextarea . addEventListener ( 'keydown' , ( event ) =>
166
179
event . stopPropagation ( )
167
180
) ;
168
181
169
182
styleTextarea . addEventListener ( 'input' , ( ) =>
170
- editorjsStyle . setAttribute ( 'style' , styleTextarea . value )
183
+ editorJSStyleElement . setAttribute ( 'style' , styleTextarea . value )
171
184
) ;
172
185
173
186
return true ;
@@ -198,17 +211,13 @@ class EditorJSStyle implements InlineTool {
198
211
}
199
212
200
213
surround ( range : Range ) {
201
- const editorjsStyle = new EditorJSStyleElement ( ) ;
214
+ const editorjsStyleElement = new EditorJSStyleElement ( ) ;
202
215
203
- editorjsStyle . append (
204
- range . collapsed ? 'new style' : range . extractContents ( )
205
- ) ;
216
+ editorjsStyleElement . append ( range . extractContents ( ) ) ;
206
217
207
- setTimeout ( ( ) => {
208
- range . insertNode ( editorjsStyle ) ;
209
- this . #api. selection . expandToTag ( editorjsStyle ) ;
210
- } ) ;
218
+ range . insertNode ( editorjsStyleElement ) ;
219
+ this . #api. selection . expandToTag ( editorjsStyleElement ) ;
211
220
}
212
221
}
213
222
214
- export default EditorJSStyle ;
223
+ export { StyleInlineTool } ;
0 commit comments