Skip to content
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

Added option to disable stripping newline character #1

Open
wants to merge 6 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
14 changes: 7 additions & 7 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
{
extends: [
"extends": [
"standard",
],
rules: {
"rules": {
"comma-dangle": [2, "always-multiline"]
},
parserOptions: {
ecmaVersion: 6,
sourceType: "module",
"parserOptions": {
"ecmaVersion": 6,
"sourceType": "module"
},
plugins: [
"standard",
"plugins": [
"standard"
]
}

1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules
npm-debug.log*
dist
20 changes: 18 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,28 @@ You can pass options to the plugin as you call it:

```js
const options = {
stripEntities: false
stripEntities: false,
stripNewlines: false,
}
const singleLinePlugin = createSingleLinePlugin(options)
```

There’s only one option so far: `stripEntities: true/false`.
### stripEntities

Strip Entities from text.

Type: `boolean` <br>
Default: `true` <br>
Options: `true` | `false`

### stripNewlines

Strip newline characters `\n` from text.

Type: `boolean` <br>
Default: `true` <br>
Options: `true` | `false`


## Developing

Expand Down
112 changes: 0 additions & 112 deletions lib/index.js

This file was deleted.

99 changes: 0 additions & 99 deletions lib/utils.js

This file was deleted.

9 changes: 4 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
{
"name": "draft-js-single-line-plugin",
"version": "0.0.1",
"version": "0.0.2",
"description": "Restrict a draft-js editor to a single line of input.",
"main": "lib/index.js",
"main": "dist/index.js",
"scripts": {
"build": "babel src --out-dir lib",
"build": "babel src --out-dir dist",
"compile": "npm run build",
"precompile": "npm run clean",
"prepublish": "npm run compile",
"test": "NODE_ENV=test babel-node test | faucet",
"posttest": "npm run lint",
"clean": "rm -rf ./lib/*",
"clean": "rm -rf ./dist/*",
"lint": "eslint 'src/*.js' 'src/**/*.js'; exit 0"
},
"repository": {
Expand Down
9 changes: 6 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
*/
const defaultOptions = {
stripEntities: true,
stripNewlines: true,
}

/**
Expand Down Expand Up @@ -65,11 +66,14 @@ 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
text = replaceNewlines(text)
if (options.stripNewlines) {
text = replaceNewlines(text)
}

// Strip entities?
if (options.stripEntities) {
Expand All @@ -86,7 +90,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
Loading