@@ -32,6 +32,7 @@ import PluginManager from '../PluginManager';
32
32
import FormatToolbar from '../FormattingToolbar' ;
33
33
import ListPlugin from '../plugins/list' ;
34
34
import BlockquotePlugin from '../plugins/blockquote' ;
35
+ import * as CONST from '../constants' ;
35
36
import * as action from '../FormattingToolbar/toolbarMethods' ;
36
37
37
38
import '../styles.css' ;
@@ -176,13 +177,13 @@ const SlateAsInputEditor = React.forwardRef((props, ref) => {
176
177
const { node, attributes, children } = props ;
177
178
178
179
switch ( node . type ) {
179
- case 'paragraph' :
180
+ case CONST . PARAGRAPH :
180
181
return < p { ...attributes } > { children } </ p > ;
181
- case 'heading_one' :
182
+ case CONST . H1 :
182
183
return < Heading as = "h1" { ...attributes } > { children } </ Heading > ;
183
- case 'heading_two' :
184
+ case CONST . H2 :
184
185
return < Heading as = "h2" { ...attributes } > { children } </ Heading > ;
185
- case 'heading_three' :
186
+ case CONST . H3 :
186
187
return < Heading as = "h3" { ...attributes } > { children } </ Heading > ;
187
188
case 'heading_four' :
188
189
return < Heading as = "h4" { ...attributes } > { children } </ Heading > ;
@@ -209,14 +210,14 @@ const SlateAsInputEditor = React.forwardRef((props, ref) => {
209
210
const { children, mark, attributes } = props ;
210
211
211
212
switch ( mark . type ) {
212
- case 'bold' :
213
+ case CONST . FONT_BOLD :
213
214
return < strong { ...attributes } > { children } </ strong > ;
214
- case 'italic' :
215
+ case CONST . FONT_ITALIC :
215
216
return < em { ...attributes } > { children } </ em > ;
216
217
// case 'underline':
217
218
// return <u {...{ attributes }}>{children}</u>;
218
219
case 'html' :
219
- case 'code' :
220
+ case CONST . FONT_CODE :
220
221
return < code { ...attributes } > { children } </ code > ;
221
222
case 'error' :
222
223
return < span className = 'error' { ...attributes } > { children } </ span > ;
@@ -264,10 +265,10 @@ const SlateAsInputEditor = React.forwardRef((props, ref) => {
264
265
if ( selection . start . offset !== 0 ) return next ( ) ;
265
266
266
267
const { startBlock } = value ;
267
- if ( startBlock . type === 'paragraph' ) return next ( ) ;
268
+ if ( startBlock . type === CONST . PARAGRAPH ) return next ( ) ;
268
269
269
270
event . preventDefault ( ) ;
270
- editor . setBlocks ( 'paragraph' ) ;
271
+ editor . setBlocks ( CONST . PARAGRAPH ) ;
271
272
272
273
return undefined ;
273
274
} ;
@@ -279,7 +280,7 @@ const SlateAsInputEditor = React.forwardRef((props, ref) => {
279
280
* @return {Boolean }
280
281
*/
281
282
282
- const isCodespan = value => value . activeMarks . some ( mark => mark . type === 'code' ) ;
283
+ const isCodespan = value => value . activeMarks . some ( mark => mark . type === CONST . FONT_CODE ) ;
283
284
284
285
/**
285
286
* On return, if at the end of a node type that should not be extended,
@@ -313,8 +314,8 @@ const SlateAsInputEditor = React.forwardRef((props, ref) => {
313
314
// Hitting enter while in a codespan will break out of the span
314
315
if ( isCodespan ( value ) ) {
315
316
event . preventDefault ( ) ;
316
- editor . removeMark ( 'code' ) ;
317
- editor . insertBlock ( 'paragraph' ) ;
317
+ editor . removeMark ( CONST . FONT_CODE ) ;
318
+ editor . insertBlock ( CONST . PARAGRAPH ) ;
318
319
return false ;
319
320
}
320
321
@@ -326,47 +327,46 @@ const SlateAsInputEditor = React.forwardRef((props, ref) => {
326
327
327
328
// when you hit enter after a heading we insert a paragraph
328
329
event . preventDefault ( ) ;
329
- editor . insertBlock ( 'paragraph' ) ;
330
+ editor . insertBlock ( CONST . PARAGRAPH ) ;
330
331
return next ( ) ;
331
332
} ;
332
333
333
334
/**
334
335
* Method to handle lists
335
336
* @param {* } editor
336
337
* @param {* } type
337
- */
338
-
339
- let handleList = ( editor , type ) => {
338
+ */
339
+
340
+ const handleList = ( editor , type ) => {
340
341
if ( action . isSelectionList ( editor . value ) ) {
341
342
if ( action . currentList ( editor . value ) . type === type ) {
342
343
return action . transformListToParagraph ( editor , type ) ;
343
- } else {
344
- return action . transformListSwap ( editor , type , editor . value ) ;
345
344
}
346
- } else if ( action . isSelectionInput ( editor . value , "block_quote" ) ) {
347
- editor . unwrapBlock ( "block_quote" ) ;
348
- return action . transformParagraphToList ( editor , type ) ;
349
-
350
- } else {
345
+ return action . transformListSwap ( editor , type , editor . value ) ;
346
+ } if ( action . isSelectionInput ( editor . value , CONST . BLOCK_QUOTE ) ) {
347
+ editor . unwrapBlock ( CONST . BLOCK_QUOTE ) ;
351
348
return action . transformParagraphToList ( editor , type ) ;
352
349
}
350
+ return action . transformParagraphToList ( editor , type ) ;
353
351
} ;
354
-
355
- /**
352
+
353
+ /**
356
354
* Method to handle block quotes
357
355
* @param {* } editor
358
- */
359
-
360
- let handleBlockQuotes = ( editor ) => {
361
- if ( action . isSelectionInput ( editor . value , "block_quote" ) ) {
362
- editor . unwrapBlock ( "block_quote" ) ;
363
- } else if ( action . isSelectionList ( editor . value ) ) {
364
- action . isSelectionInput ( editor . value , "ol_list" ) ?action . transformListToParagraph ( editor , 'ol_list' ) :action . transformListToParagraph ( editor , 'ul_list' )
365
- editor . wrapBlock ( "block_quote" ) ;
366
- } else {
367
- editor . wrapBlock ( "block_quote" ) ;
356
+ */
357
+
358
+ const handleBlockQuotes = ( editor ) => {
359
+ if ( action . isSelectionInput ( editor . value , CONST . BLOCK_QUOTE ) ) {
360
+ editor . unwrapBlock ( CONST . BLOCK_QUOTE ) ;
361
+ } else if ( action . isSelectionList ( editor . value ) ) {
362
+ if ( action . isSelectionInput ( editor . value , CONST . OL_LIST ) ) {
363
+ action . transformListToParagraph ( editor , CONST . OL_LIST ) ;
364
+ } else { action . transformListToParagraph ( editor , CONST . UL_LIST ) ; }
365
+ editor . wrapBlock ( CONST . BLOCK_QUOTE ) ;
366
+ } else {
367
+ editor . wrapBlock ( CONST . BLOCK_QUOTE ) ;
368
368
}
369
- }
369
+ } ;
370
370
371
371
/**
372
372
* Called upon a keypress
@@ -375,59 +375,44 @@ const SlateAsInputEditor = React.forwardRef((props, ref) => {
375
375
* @param {* } next
376
376
*/
377
377
const onKeyDown = async ( event , editor , next ) => {
378
- const isEnter = ( ) => {
379
- return handleEnter ( event , editor , next ) ;
380
- }
381
-
382
- const isBackSpace = ( ) => {
383
- return handleBackspace ( event , editor , next ) ;
384
- }
385
-
378
+ const isEnter = ( ) => handleEnter ( event , editor , next ) ;
379
+ const isBackSpace = ( ) => handleBackspace ( event , editor , next ) ;
386
380
const isSpecialKey = async ( ) => {
387
381
switch ( true ) {
388
- case isHotKey ( " mod+z" , event ) && editor . props . editorProps . onUndoOrRedo :
382
+ case isHotKey ( ' mod+z' , event ) && editor . props . editorProps . onUndoOrRedo :
389
383
await editor . undo ( ) ;
390
384
return editor . props . editorProps . onUndoOrRedo ( editor ) ;
391
-
392
385
case isHotKey ( 'mod+shift+z' , event ) && editor . props . editorProps . onUndoOrRedo :
393
386
await editor . redo ( ) ;
394
387
return editor . props . editorProps . onUndoOrRedo ( editor ) ;
395
-
396
- case isHotKey ( "mod+b" , event ) :
397
- return editor . toggleMark ( "bold" ) ;
398
-
399
- case isHotKey ( "mod+i" , event ) :
400
- return editor . toggleMark ( "italic" ) ;
401
-
402
- case isHotKey ( "mod+alt+c" , event ) :
403
- return editor . toggleMark ( "code" ) ;
404
-
405
- case isHotKey ( "mod+q" , event ) :
406
- return handleBlockQuotes ( editor )
407
-
408
- case isHotKey ( "mod+shift+7" , event ) :
409
- return handleList ( editor , "ol_list" ) ;
410
-
411
- case isHotKey ( "mod+shift+8" , event ) :
412
- return handleList ( editor , "ul_list" ) ;
413
-
414
- default :
388
+ case isHotKey ( 'mod+b' , event ) && isEditable ( editor , CONST . FONT_BOLD ) :
389
+ return editor . toggleMark ( CONST . FONT_BOLD ) ;
390
+ case isHotKey ( 'mod+i' , event ) && isEditable ( editor , CONST . FONT_ITALIC ) :
391
+ return editor . toggleMark ( CONST . FONT_ITALIC ) ;
392
+ case isHotKey ( 'mod+alt+c' , event ) && isEditable ( editor , CONST . FONT_CODE ) :
393
+ return editor . toggleMark ( CONST . FONT_CODE ) ;
394
+ case isHotKey ( 'mod+q' , event ) && isEditable ( editor , CONST . BLOCK_QUOTE ) :
395
+ return handleBlockQuotes ( editor ) ;
396
+ case isHotKey ( 'mod+shift+7' , event ) && isEditable ( editor , CONST . OL_LIST ) :
397
+ return handleList ( editor , CONST . OL_LIST ) ;
398
+ case isHotKey ( 'mod+shift+8' , event ) && isEditable ( editor , CONST . UL_LIST ) :
399
+ return handleList ( editor , CONST . UL_LIST ) ;
400
+ default :
415
401
return next ( ) ;
416
402
}
417
- }
403
+ } ;
418
404
419
405
const inputHandler = ( key ) => {
420
406
const cases = {
421
- 'Enter' : isEnter ,
422
- 'Backspace' : isBackSpace ,
423
- 'default' : isSpecialKey ,
424
- }
425
-
426
- return ( cases [ key ] || cases [ 'default' ] ) ( ) ;
427
- }
407
+ Enter : isEnter ,
408
+ Backspace : isBackSpace ,
409
+ default : isSpecialKey ,
410
+ } ;
411
+ return ( cases [ key ] || cases . default ) ( ) ;
412
+ } ;
428
413
429
414
inputHandler ( event . key ) ;
430
- }
415
+ } ;
431
416
432
417
433
418
/**
@@ -609,7 +594,7 @@ SlateAsInputEditor.defaultProps = {
609
594
data : { } ,
610
595
nodes : [ {
611
596
object : 'block' ,
612
- type : 'paragraph' ,
597
+ type : CONST . PARAGRAPH ,
613
598
data : { } ,
614
599
nodes : [ {
615
600
object : 'text' ,
0 commit comments