Skip to content
This repository was archived by the owner on May 19, 2020. It is now read-only.

Commit d78f9b1

Browse files
committed
fix(SlateAsInputEditor): check isEditable for hotkeys
Signed-off-by: irmerk <[email protected]>
1 parent ac346f9 commit d78f9b1

File tree

4 files changed

+66
-78
lines changed

4 files changed

+66
-78
lines changed

package-lock.json

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -115,4 +115,4 @@
115115
"slate-plain-serializer": "^0.7.11",
116116
"style-loader": "^0.23.1"
117117
}
118-
}
118+
}

src/SlateAsInputEditor/index.js

+61-76
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import PluginManager from '../PluginManager';
3232
import FormatToolbar from '../FormattingToolbar';
3333
import ListPlugin from '../plugins/list';
3434
import BlockquotePlugin from '../plugins/blockquote';
35+
import * as CONST from '../constants';
3536
import * as action from '../FormattingToolbar/toolbarMethods';
3637

3738
import '../styles.css';
@@ -176,13 +177,13 @@ const SlateAsInputEditor = React.forwardRef((props, ref) => {
176177
const { node, attributes, children } = props;
177178

178179
switch (node.type) {
179-
case 'paragraph':
180+
case CONST.PARAGRAPH:
180181
return <p {...attributes}>{children}</p>;
181-
case 'heading_one':
182+
case CONST.H1:
182183
return <Heading as="h1" {...attributes}>{children}</Heading>;
183-
case 'heading_two':
184+
case CONST.H2:
184185
return <Heading as="h2" {...attributes}>{children}</Heading>;
185-
case 'heading_three':
186+
case CONST.H3:
186187
return <Heading as="h3" {...attributes}>{children}</Heading>;
187188
case 'heading_four':
188189
return <Heading as="h4" {...attributes}>{children}</Heading>;
@@ -209,14 +210,14 @@ const SlateAsInputEditor = React.forwardRef((props, ref) => {
209210
const { children, mark, attributes } = props;
210211

211212
switch (mark.type) {
212-
case 'bold':
213+
case CONST.FONT_BOLD:
213214
return <strong {...attributes}>{children}</strong>;
214-
case 'italic':
215+
case CONST.FONT_ITALIC:
215216
return <em {...attributes}>{children}</em>;
216217
// case 'underline':
217218
// return <u {...{ attributes }}>{children}</u>;
218219
case 'html':
219-
case 'code':
220+
case CONST.FONT_CODE:
220221
return <code {...attributes}>{children}</code>;
221222
case 'error':
222223
return <span className='error' {...attributes}>{children}</span>;
@@ -264,10 +265,10 @@ const SlateAsInputEditor = React.forwardRef((props, ref) => {
264265
if (selection.start.offset !== 0) return next();
265266

266267
const { startBlock } = value;
267-
if (startBlock.type === 'paragraph') return next();
268+
if (startBlock.type === CONST.PARAGRAPH) return next();
268269

269270
event.preventDefault();
270-
editor.setBlocks('paragraph');
271+
editor.setBlocks(CONST.PARAGRAPH);
271272

272273
return undefined;
273274
};
@@ -279,7 +280,7 @@ const SlateAsInputEditor = React.forwardRef((props, ref) => {
279280
* @return {Boolean}
280281
*/
281282

282-
const isCodespan = value => value.activeMarks.some(mark => mark.type === 'code');
283+
const isCodespan = value => value.activeMarks.some(mark => mark.type === CONST.FONT_CODE);
283284

284285
/**
285286
* 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) => {
313314
// Hitting enter while in a codespan will break out of the span
314315
if (isCodespan(value)) {
315316
event.preventDefault();
316-
editor.removeMark('code');
317-
editor.insertBlock('paragraph');
317+
editor.removeMark(CONST.FONT_CODE);
318+
editor.insertBlock(CONST.PARAGRAPH);
318319
return false;
319320
}
320321

@@ -326,47 +327,46 @@ const SlateAsInputEditor = React.forwardRef((props, ref) => {
326327

327328
// when you hit enter after a heading we insert a paragraph
328329
event.preventDefault();
329-
editor.insertBlock('paragraph');
330+
editor.insertBlock(CONST.PARAGRAPH);
330331
return next();
331332
};
332333

333334
/**
334335
* Method to handle lists
335336
* @param {*} editor
336337
* @param {*} type
337-
*/
338-
339-
let handleList = (editor, type) => {
338+
*/
339+
340+
const handleList = (editor, type) => {
340341
if (action.isSelectionList(editor.value)) {
341342
if (action.currentList(editor.value).type === type) {
342343
return action.transformListToParagraph(editor, type);
343-
} else {
344-
return action.transformListSwap(editor, type, editor.value);
345344
}
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);
351348
return action.transformParagraphToList(editor, type);
352349
}
350+
return action.transformParagraphToList(editor, type);
353351
};
354-
355-
/**
352+
353+
/**
356354
* Method to handle block quotes
357355
* @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);
368368
}
369-
}
369+
};
370370

371371
/**
372372
* Called upon a keypress
@@ -375,59 +375,44 @@ const SlateAsInputEditor = React.forwardRef((props, ref) => {
375375
* @param {*} next
376376
*/
377377
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);
386380
const isSpecialKey = async () => {
387381
switch (true) {
388-
case isHotKey("mod+z", event) && editor.props.editorProps.onUndoOrRedo:
382+
case isHotKey('mod+z', event) && editor.props.editorProps.onUndoOrRedo:
389383
await editor.undo();
390384
return editor.props.editorProps.onUndoOrRedo(editor);
391-
392385
case isHotKey('mod+shift+z', event) && editor.props.editorProps.onUndoOrRedo:
393386
await editor.redo();
394387
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:
415401
return next();
416402
}
417-
}
403+
};
418404

419405
const inputHandler = (key) => {
420406
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+
};
428413

429414
inputHandler(event.key);
430-
}
415+
};
431416

432417

433418
/**
@@ -609,7 +594,7 @@ SlateAsInputEditor.defaultProps = {
609594
data: {},
610595
nodes: [{
611596
object: 'block',
612-
type: 'paragraph',
597+
type: CONST.PARAGRAPH,
613598
data: {},
614599
nodes: [{
615600
object: 'text',

src/constants.js

+3
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,6 @@ export const PARAGRAPH = 'paragraph';
66
export const H1 = 'heading_one';
77
export const H2 = 'heading_two';
88
export const H3 = 'heading_three';
9+
export const FONT_BOLD = 'bold';
10+
export const FONT_ITALIC = 'italic';
11+
export const FONT_CODE = 'code';

0 commit comments

Comments
 (0)