Update slate plugin to drop data

This commit is contained in:
Damien Duhamel 2017-11-30 23:26:18 +01:00 committed by Shawn Erquhart
parent 4ddccc0d24
commit 9e5f42e772
3 changed files with 22 additions and 20 deletions

View File

@ -129,6 +129,7 @@
"gray-matter": "^3.0.6", "gray-matter": "^3.0.6",
"history": "^4.7.2", "history": "^4.7.2",
"immutable": "^3.7.6", "immutable": "^3.7.6",
"is-hotkey": "^0.1.1",
"js-base64": "^2.1.9", "js-base64": "^2.1.9",
"js-yaml": "^3.10.0", "js-yaml": "^3.10.0",
"jwt-decode": "^2.1.0", "jwt-decode": "^2.1.0",

View File

@ -1,4 +1,5 @@
import { Block, Text } from 'slate'; import { Block, Text } from 'slate';
import { isHotkey } from 'is-hotkey';
export default onKeyDown; export default onKeyDown;
@ -20,10 +21,9 @@ function changeHistory(change, type) {
*/ */
const next = historyOfType.first(); const next = historyOfType.first();
const historyOfTypeIsValid = historyOfType.size > 1 const historyOfTypeIsValid = historyOfType.size > 1
|| next.length > 1 || ( next && next.length > 1 && next[0].type !== 'set_selection' );
|| next[0].type !== 'set_selection';
if (next && historyOfTypeIsValid) { if (historyOfTypeIsValid) {
change[type](); change[type]();
} }
@ -33,14 +33,15 @@ function changeHistory(change, type) {
return change.focus(); return change.focus();
} }
function onKeyDown(e, data, change) { function onKeyDown(event, change) {
const createDefaultBlock = () => { const createDefaultBlock = () => {
return Block.create({ return Block.create({
type: 'paragraph', type: 'paragraph',
nodes: [Text.create('')], nodes: [Text.create('')],
}); });
}; };
if (data.key === 'enter') {
if (event.key === 'Enter') {
/** /**
* If "Enter" is pressed while a single void block is selected, a new * If "Enter" is pressed while a single void block is selected, a new
* paragraph should be added above or below it, and the current selection * paragraph should be added above or below it, and the current selection
@ -53,7 +54,7 @@ function onKeyDown(e, data, change) {
const singleBlockSelected = anchorBlock === focusBlock; const singleBlockSelected = anchorBlock === focusBlock;
if (!singleBlockSelected || !focusBlock.isVoid) return; if (!singleBlockSelected || !focusBlock.isVoid) return;
e.preventDefault(); event.preventDefault();
const focusBlockParent = doc.getParent(focusBlock.key); const focusBlockParent = doc.getParent(focusBlock.key);
const focusBlockIndex = focusBlockParent.nodes.indexOf(focusBlock); const focusBlockIndex = focusBlockParent.nodes.indexOf(focusBlock);
@ -67,35 +68,35 @@ function onKeyDown(e, data, change) {
.collapseToStartOf(newBlock); .collapseToStartOf(newBlock);
} }
if (data.isMod) { if (isHotkey(`mod+${event.key}`, event)) {
/** /**
* Undo and redo work automatically with Slate, but focus is lost in certain * Undo and redo work automatically with Slate, but focus is lost in certain
* actions. We override Slate's built in undo/redo here and force focus * actions. We override Slate's built in undo/redo here and force focus
* back to the editor each time. * back to the editor each time.
*/ */
if (data.key === 'y') { if (event.key === 'y') {
e.preventDefault(); event.preventDefault();
return changeHistory(change, 'redo'); return changeHistory(change, 'redo');
} }
if (data.key === 'z') { if (event.key === 'z') {
e.preventDefault(); event.preventDefault();
return changeHistory(change, data.isShift ? 'redo' : 'undo'); return changeHistory(change, event.isShift ? 'redo' : 'undo');
} }
const marks = { const marks = {
b: 'bold', b: 'bold',
i: 'italic', i: 'italic',
u: 'underlined', u: 'underline',
s: 'strikethrough', s: 'strikethrough',
'`': 'code', '`': 'code',
}; };
const mark = marks[data.key]; const mark = marks[event.key];
if (mark) { if (mark) {
e.preventDefault(); event.preventDefault();
return change.toggleMark(mark); return change.toggleMark(mark);
} }
} }

View File

@ -4,9 +4,9 @@ import EditList from 'slate-edit-list';
import EditTable from 'slate-edit-table'; import EditTable from 'slate-edit-table';
const SoftBreak = (options = {}) => ({ const SoftBreak = (options = {}) => ({
onKeyDown(e, data, change) { onKeyDown(event, change) {
if (data.key != 'enter') return; if (event.key != 'Enter') return;
if (options.shift && e.shiftKey == false) return; if (options.shift && event.shiftKey == false) return;
const { onlyIn, ignoreIn, defaultBlock = 'paragraph' } = options; const { onlyIn, ignoreIn, defaultBlock = 'paragraph' } = options;
const { type, text } = change.value.startBlock; const { type, text } = change.value.startBlock;
@ -38,9 +38,9 @@ export const SoftBreakConfigured = SoftBreak(SoftBreakOpts);
export const ParagraphSoftBreakConfigured = SoftBreak({ onlyIn: ['paragraph'], shift: true }); export const ParagraphSoftBreakConfigured = SoftBreak({ onlyIn: ['paragraph'], shift: true });
const BreakToDefaultBlock = ({ onlyIn = [], defaultBlock = 'paragraph' }) => ({ const BreakToDefaultBlock = ({ onlyIn = [], defaultBlock = 'paragraph' }) => ({
onKeyDown(e, data, change) { onKeyDown(event, change) {
const { value } = change; const { value } = change;
if (data.key != 'enter' || e.shiftKey == true || value.isExpanded) return; if (event.key != 'Enter' || event.shiftKey == true || value.isExpanded) return;
if (onlyIn.includes(value.startBlock.type)) { if (onlyIn.includes(value.startBlock.type)) {
return change.insertBlock(defaultBlock); return change.insertBlock(defaultBlock);
} }