From bb682a4422bc8489add4d6f5a2624da296b4b020 Mon Sep 17 00:00:00 2001 From: newsiberian Date: Thu, 7 Dec 2017 11:00:00 +0700 Subject: [PATCH 1/2] - fixed indentation behaviour; - added new argument `indent depth` for onTabs, handleReturn; --- README.md | 4 ++-- lib/__tests__/buildIndentation.test.js | 10 ++++++++++ lib/handleKeyCommand.js | 5 +++-- lib/onTab.js | 6 ++++-- lib/utils/buildIndentation.js | 20 ++++++++++++++++++++ lib/utils/getIndentation.js | 4 +--- lib/utils/removeIndent.js | 7 ++++--- package.json | 2 +- 8 files changed, 45 insertions(+), 13 deletions(-) create mode 100644 lib/__tests__/buildIndentation.test.js create mode 100644 lib/utils/buildIndentation.js diff --git a/README.md b/README.md index c04d8ed..bbb880d 100644 --- a/README.md +++ b/README.md @@ -70,7 +70,7 @@ class Editor extends React.Component { let newState; if (CodeUtils.hasSelectionInBlock(editorState)) { - newState = CodeUtils.handleKeyCommand(editorState, command); + newState = CodeUtils.handleKeyCommand(editorState, command, 2); } if (!newState) { @@ -105,7 +105,7 @@ class Editor extends React.Component { const { editorState } = this.state; if (!CodeUtils.hasSelectionInBlock(editorState)) return 'not-handled'; - this.onChange(CodeUtils.onTab(evt, editorState)); + this.onChange(CodeUtils.onTab(evt, editorState, 2)); return 'handled'; } diff --git a/lib/__tests__/buildIndentation.test.js b/lib/__tests__/buildIndentation.test.js new file mode 100644 index 0000000..839d509 --- /dev/null +++ b/lib/__tests__/buildIndentation.test.js @@ -0,0 +1,10 @@ +var buildIndentation = require('../utils/buildIndentation'); + +describe('buildIndentation', function() { + it('should correctly translate numbers to spaces', function() { + var spaces = buildIndentation(2); + expect(spaces).toBe(' '); + var spacesAgain = buildIndentation(3); + expect(spacesAgain).toBe(' '); + }); +}); diff --git a/lib/handleKeyCommand.js b/lib/handleKeyCommand.js index 8465fe7..05ff06a 100644 --- a/lib/handleKeyCommand.js +++ b/lib/handleKeyCommand.js @@ -5,11 +5,12 @@ var removeIndent = require('./utils/removeIndent'); * * @param {Draft.EditorState} editorState * @param {String} command + * @param {Number} indent - Indent depth * @return {Boolean} */ -function handleKeyCommand(editorState, command) { +function handleKeyCommand(editorState, command, indent) { if (command === 'backspace') { - return removeIndent(editorState); + return removeIndent(editorState, indent); } } diff --git a/lib/onTab.js b/lib/onTab.js index cd068f3..b5329ed 100644 --- a/lib/onTab.js +++ b/lib/onTab.js @@ -1,5 +1,6 @@ var Draft = require('draft-js'); var getIndentation = require('./utils/getIndentation'); +var buildIndentation = require('./utils/buildIndentation'); // TODO: tab should complete indentation instead of just inserting one @@ -8,9 +9,10 @@ var getIndentation = require('./utils/getIndentation'); * * @param {SyntheticKeyboardEvent} event * @param {Draft.EditorState} editorState + * @param {Number} indent - Indent depth * @return {Draft.EditorState} */ -function onTab(e, editorState) { +function onTab(e, editorState, indent) { e.preventDefault(); var contentState = editorState.getCurrentContent(); @@ -18,7 +20,7 @@ function onTab(e, editorState) { var startKey = selection.getStartKey(); var currentBlock = contentState.getBlockForKey(startKey); - var indentation = getIndentation(currentBlock.getText()); + var indentation = buildIndentation(indent); var newContentState; if (selection.isCollapsed()) { diff --git a/lib/utils/buildIndentation.js b/lib/utils/buildIndentation.js new file mode 100644 index 0000000..4be500e --- /dev/null +++ b/lib/utils/buildIndentation.js @@ -0,0 +1,20 @@ +var DEFAULT_INDENTATION = ' '; + +/** + * Build indent from given number of spaces + * @param indent + * @return {string} + */ +function buildIndentation(indent) { + if (typeof indent !== 'number') { + return DEFAULT_INDENTATION; + } + + var spaces = ''; + for (var i = 0, j = indent; i < j; i++) { + spaces += ' '; + } + return spaces; +} + +module.exports = buildIndentation; diff --git a/lib/utils/getIndentation.js b/lib/utils/getIndentation.js index 7ba0394..3e3cc9e 100644 --- a/lib/utils/getIndentation.js +++ b/lib/utils/getIndentation.js @@ -1,7 +1,5 @@ var detectIndent = require('detect-indent'); -var DEFAULT_INDENTATION = ' '; - /** * Detect indentation in a text * @param {String} text @@ -9,7 +7,7 @@ var DEFAULT_INDENTATION = ' '; */ function getIndentation(text) { var result = detectIndent(text); - return result.indent || DEFAULT_INDENTATION; + return result.indent; } module.exports = getIndentation; diff --git a/lib/utils/removeIndent.js b/lib/utils/removeIndent.js index 43bf101..1af9351 100644 --- a/lib/utils/removeIndent.js +++ b/lib/utils/removeIndent.js @@ -2,7 +2,7 @@ var Draft = require('draft-js'); var endsWith = require('ends-with'); var getNewLine = require('./getNewLine'); -var getIndentation = require('./getIndentation'); +var buildIndentation = require('./buildIndentation'); var getLines = require('./getLines'); var getLineAnchorForOffset = require('./getLineAnchorForOffset'); @@ -10,9 +10,10 @@ var getLineAnchorForOffset = require('./getLineAnchorForOffset'); * Remove last indentation before cursor, return undefined if no modification is done * * @param {Draft.EditorState} editorState + * @param {Number} indentLength * @return {Draft.EditorState|undefined} */ -function removeIndent(editorState) { +function removeIndent(editorState, indentLength) { var contentState = editorState.getCurrentContent(); var selection = editorState.getSelection(); @@ -27,7 +28,7 @@ function removeIndent(editorState) { // Detect newline separator and indentation var newLine = getNewLine(blockText); - var indent = getIndentation(blockText); + var indent = buildIndentation(indentLength); // Get current line var lines = getLines(blockText, newLine); diff --git a/package.json b/package.json index 89065c5..5b06a54 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "draft-js-code", - "version": "0.3.0", + "version": "0.4.0", "description": "Collection of utilities to make code blocks edition easy in DraftJS", "main": "./lib/index.js", "scripts": { From 9928552e382b10c071347f189809d3e27b706f9a Mon Sep 17 00:00:00 2001 From: newsiberian Date: Thu, 7 Dec 2017 22:22:13 +0700 Subject: [PATCH 2/2] - revert version changing; --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 5b06a54..89065c5 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "draft-js-code", - "version": "0.4.0", + "version": "0.3.0", "description": "Collection of utilities to make code blocks edition easy in DraftJS", "main": "./lib/index.js", "scripts": {