Skip to content
This repository has been archived by the owner on Oct 25, 2022. It is now read-only.

Commit

Permalink
Move deleteBackward behaviour to default editor
Browse files Browse the repository at this point in the history
  • Loading branch information
tiberiuichim committed May 20, 2020
1 parent 180e4ab commit daf07d0
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 39 deletions.
1 change: 1 addition & 0 deletions src/TextBlock/TextBlockEdit.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ const TextBlockEdit = (props) => {
},

Backspace: ({ editor, event, selection, onDeleteBlock, id, data }) => {
console.log('backspace event');
const { start, end } = selection;
const { value } = data;

Expand Down
4 changes: 2 additions & 2 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import * as slateConfig from './editor/config';

import installLinkPlugin from './editor/plugins/Link';
import installCalloutPlugin from './editor/plugins/Callout';
import installMarkdown from './editor/plugins/Markdown';
import installVoltoProposals from './futurevolto/config';
// import installMarkdown from './editor/plugins/Markdown';

export function applyConfig(config) {
config.blocks.blocksConfig.slate = {
Expand All @@ -30,7 +30,7 @@ export function applyConfig(config) {
};

installLinkPlugin(config);
installMarkdown(config);
// installMarkdown(config);
installCalloutPlugin(config);
installVoltoProposals(config);

Expand Down
46 changes: 44 additions & 2 deletions src/editor/components/SlateEditor.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,49 @@ import Toolbar from './Toolbar';
import ExpandedToolbar from './ExpandedToolbar';
import { toggleMark } from '../utils';
import { settings } from '~/config';
// import { initialValue } from '../constants';
import { Editor, Transforms, Range, Point } from 'slate';

const withDelete = (editor) => {
const { deleteBackward } = editor;

editor.deleteBackward = (...args) => {
const { selection } = editor;

if (selection && Range.isCollapsed(selection)) {
console.log('second case');
const match = Editor.above(editor, {
match: (n) => Editor.isBlock(editor, n),
});

if (match) {
const [block, path] = match;
const start = Editor.start(editor, path);

if (
block.type !== 'paragraph' &&
Point.equals(selection.anchor, start)
) {
Transforms.setNodes(editor, { type: 'paragraph' });

if (block.type === 'list-item') {
Transforms.unwrapNodes(editor, {
match: (n) => n.type === 'bulleted-list',
split: true,
});
}

return;
}
}
deleteBackward(...args);
} else {
console.log('delete', args);
deleteBackward(1);
}
};

return editor;
};

const SlateEditor = ({
selected,
Expand Down Expand Up @@ -49,7 +91,7 @@ const SlateEditor = ({
() =>
(slate.decorators || []).reduce(
(acc, apply) => apply(acc),
withHistory(withReact(createEditor())),
withDelete(withHistory(withReact(createEditor()))),
),
[slate.decorators],
);
Expand Down
37 changes: 2 additions & 35 deletions src/editor/plugins/Markdown/decorators.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { SHORTCUTS } from './constants';

import { Editor, Transforms, Range, Point } from 'slate';
import { Editor, Transforms, Range } from 'slate';

export const withShortcuts = (editor) => {
const { deleteBackward, insertText } = editor;
const { insertText } = editor;

editor.insertText = (text) => {
const { selection } = editor;
Expand Down Expand Up @@ -42,38 +42,5 @@ export const withShortcuts = (editor) => {
insertText(text);
};

editor.deleteBackward = (...args) => {
const { selection } = editor;

if (selection && Range.isCollapsed(selection)) {
const match = Editor.above(editor, {
match: (n) => Editor.isBlock(editor, n),
});

if (match) {
const [block, path] = match;
const start = Editor.start(editor, path);

if (
block.type !== 'paragraph' &&
Point.equals(selection.anchor, start)
) {
Transforms.setNodes(editor, { type: 'paragraph' });

if (block.type === 'list-item') {
Transforms.unwrapNodes(editor, {
match: (n) => n.type === 'bulleted-list',
split: true,
});
}

return;
}
}

deleteBackward(...args);
}
};

return editor;
};

0 comments on commit daf07d0

Please sign in to comment.