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
Changes from 1 commit
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
23 changes: 21 additions & 2 deletions lib/utils/removeIndent.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
var Draft = require('draft-js');
var endsWith = require('ends-with');
var detectIndent = require('detect-indent');

var getNewLine = require('./getNewLine');
var getIndentation = require('./getIndentation');
var getLines = require('./getLines');
var getLineAnchorForOffset = require('./getLineAnchorForOffset');

Expand All @@ -26,8 +26,27 @@ function removeIndent(editorState) {
var blockText = currentBlock.getText();

// Detect newline separator and indentation
var indent = detectIndent(blockText);

// 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 (indent.amount < 1) {
var lastBlockBefore = contentState
.getBlockMap()
.takeUntil((value, key) => {
return key.match(startKey);
})
.last();

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

var newLine = getNewLine(blockText);
var indent = getIndentation(blockText);

// Get current line
var lines = getLines(blockText, newLine);
Expand Down