Skip to content
Open
Show file tree
Hide file tree
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
Prev Previous commit
Next Next commit
Extend check stripNewlines option
In utils condenseBlock and before proccessing text
  • Loading branch information
exah committed Jun 16, 2016
commit b00667241ecc4a7719e557e733343022c3a8f278
12 changes: 9 additions & 3 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,14 @@ var _immutable = require('immutable');

var _draftJs = require('draft-js');

var _isSoftNewlineEvent = require('draft-js/lib/isSoftNewlineEvent');

var _isSoftNewlineEvent2 = _interopRequireDefault(_isSoftNewlineEvent);

var _utils = require('./utils');

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

/**
* Default options
* @type {Object}
Expand Down Expand Up @@ -64,11 +70,12 @@ function singleLinePlugin() {
var contentBlock = blocks[0];
var text = contentBlock.getText();
var characterList = contentBlock.getCharacterList();
var isNewLine = options.stripNewlines && _utils.NEWLINE_REGEX.test(text);

if (_utils.NEWLINE_REGEX.test(text) || (0, _utils.characterListhasEntities)(characterList)) {
if (isNewLine || (0, _utils.characterListhasEntities)(characterList)) {
// Replace the text stripped of its newlines. Note that we replace
// one '\n' with one ' ' so we don't need to modify the characterList
if (options.stripNewlines === true) {
if (options.stripNewlines) {
text = (0, _utils.replaceNewlines)(text);
}

Expand All @@ -87,7 +94,6 @@ function singleLinePlugin() {
});

// Update the editor state with the compressed version
// const selection = editorState.getSelection()
var newContentState = _draftJs.ContentState.createFromBlockArray([contentBlock]);

// Create the new state as an undoable action
Expand Down
6 changes: 5 additions & 1 deletion lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,11 @@ function condenseBlocks(editorState, blocks, options) {
blocks.forEach(function (block) {
// Atomic blocks should be ignored (stripped)
if (block.getType() !== 'atomic') {
text = text.push(replaceNewlines(block.getText()));
if (options.stripNewlines) {
text = text.push(replaceNewlines(block.getText()));
} else {
text = text.push(block.getText());
}
characterList = characterList.concat(block.getCharacterList());
}
});
Expand Down
8 changes: 5 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import {
genKey,
} from 'draft-js'

import isSoftNewlineEvent from 'draft-js/lib/isSoftNewlineEvent'

import {
NEWLINE_REGEX,
replaceNewlines,
Expand Down Expand Up @@ -66,11 +68,12 @@ function singleLinePlugin (options = {}) {
let contentBlock = blocks[0]
let text = contentBlock.getText()
let characterList = contentBlock.getCharacterList()
const isNewLine = (options.stripNewlines && NEWLINE_REGEX.test(text));

if (NEWLINE_REGEX.test(text) || characterListhasEntities(characterList)) {
if ( isNewLine || characterListhasEntities(characterList) ) {
// Replace the text stripped of its newlines. Note that we replace
// one '\n' with one ' ' so we don't need to modify the characterList
if (options.stripNewlines === true) {
if (options.stripNewlines) {
text = replaceNewlines(text)
}

Expand All @@ -89,7 +92,6 @@ function singleLinePlugin (options = {}) {
})

// Update the editor state with the compressed version
// const selection = editorState.getSelection()
const newContentState = ContentState.createFromBlockArray([contentBlock])

// Create the new state as an undoable action
Expand Down
6 changes: 5 additions & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@ export function condenseBlocks (editorState, blocks, options) {
blocks.forEach((block) => {
// Atomic blocks should be ignored (stripped)
if (block.getType() !== 'atomic') {
text = text.push(replaceNewlines(block.getText()))
if (options.stripNewlines) {
text = text.push(replaceNewlines(block.getText()))
} else {
text = text.push(block.getText());
}
characterList = characterList.concat(block.getCharacterList())
}
})
Expand Down