Skip to content

Protection against current code-block removing #33

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 39 additions & 1 deletion lib/__tests__/handleKeyCommand.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
const { EditorState, ContentState, SelectionState } = require('draft-js');
const {
EditorState,
ContentState,
ContentBlock,
SelectionState
} = require('draft-js');
const handleKeyCommand = require('../handleKeyCommand');

const toPlainText = editorState =>
Expand Down Expand Up @@ -119,6 +124,39 @@ it('should not do anything on backspace if something is selected', () => {
expect(handleKeyCommand(editorState, 'backspace')).toEqual(undefined);
});

it('should skip handling when text beginning from the content-block beginning', () => {
const firstBlock = new ContentBlock({
key: 'a1',
text: 'const a = 1;',
type: 'unstyled'
});
const secondBlock = new ContentBlock({
key: 'a2',
text: 'function () {',
type: 'code-block'
});
const currentContent = ContentState.createFromBlockArray([
firstBlock,
secondBlock
]);
const selectSecondBlock = SelectionState.createEmpty(
currentContent
.getBlockMap()
.last()
.getKey()
)
.set('anchorOffset', 0)
.set('focusOffset', 0);
const editorState = EditorState.create({
allowUndo: true,
currentContent,
selection: selectSecondBlock
});

const after = handleKeyCommand(editorState, 'backspace');
expect(toPlainText(after)).toEqual(toPlainText(editorState));
});

it('should not do anything for any other command', () => {
const editorState = createWithText('');
expect(handleKeyCommand(editorState, 'enter')).toEqual(undefined);
Expand Down
2 changes: 1 addition & 1 deletion lib/utils/getLineAnchorForOffset.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ function getLineAnchorForOffset(text, offset, sep) {
}

return new LineAnchor({
line: lineIndex - 1,
line: lineIndex === 0 ? 0 : lineIndex - 1,
offset: offset - lastLineIndex
});
}
Expand Down
27 changes: 26 additions & 1 deletion lib/utils/removeIndent.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var Draft = require('draft-js');
var endsWith = require('ends-with');
var detectIndent = require('detect-indent');

var getNewLine = require('./getNewLine');
var getIndentation = require('./getIndentation');
Expand All @@ -25,7 +26,6 @@ function removeIndent(editorState) {
var currentBlock = contentState.getBlockForKey(startKey);
var blockText = currentBlock.getText();

// Detect newline separator and indentation
var newLine = getNewLine(blockText);
var indent = getIndentation(blockText);

Expand All @@ -36,6 +36,31 @@ function removeIndent(editorState) {
var currentLine = lines.get(lineAnchor.getLine());
var beforeSelection = currentLine.slice(0, lineAnchor.getOffset());

// Detect newline separator and indentation
var currentIndent = detectIndent(currentLine);

// if previous block was not `code-block` and we are at the beginning of line,
// we don't do any action to prevent current `code-block` removing
if (
currentIndent.amount < 1 &&
lineAnchor.getLine() === 0 &&
startOffset === 0
) {
var lastBlockBefore = contentState
.getBlockMap()
.takeUntil((value, key) => {
return key.match(startKey);
})
.last();

if (
!(lastBlockBefore instanceof Draft.ContentBlock) ||
lastBlockBefore.getType() !== 'code-block'
) {
return editorState;
}
}

// If the line before selection ending with the indentation?
if (!endsWith(beforeSelection, indent)) {
return;
Expand Down