Skip to content

Fix for indentation behaviour #32

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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This probably shouldn't be hard coded? Rather than being provided as an argument it should ideally be detected—that's what the getIndentation.js utils is for afaict.

Can we somehow infer this instead of hardcoding it? Why do we need this argument here?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm. it's not hardcoded. You can set it with props if you want, or you mean we should show something like CodeUtils.onTab(evt, editorState, indentDepth) here?

And again. I don't how getIndentation can help here. The case I see for it is to detect current line indentation and set next line similar indentation on that data, like insertNewLine does

return 'handled';
}

Expand Down
10 changes: 10 additions & 0 deletions lib/__tests__/buildIndentation.test.js
Original file line number Diff line number Diff line change
@@ -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(' ');
});
});
5 changes: 3 additions & 2 deletions lib/handleKeyCommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
6 changes: 4 additions & 2 deletions lib/onTab.js
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -8,17 +9,18 @@ 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();
var selection = editorState.getSelection();
var startKey = selection.getStartKey();
var currentBlock = contentState.getBlockForKey(startKey);

var indentation = getIndentation(currentBlock.getText());
var indentation = buildIndentation(indent);
var newContentState;

if (selection.isCollapsed()) {
Expand Down
20 changes: 20 additions & 0 deletions lib/utils/buildIndentation.js
Original file line number Diff line number Diff line change
@@ -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;
4 changes: 1 addition & 3 deletions lib/utils/getIndentation.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
var detectIndent = require('detect-indent');

var DEFAULT_INDENTATION = ' ';

/**
* Detect indentation in a text
* @param {String} text
* @return {String}
*/
function getIndentation(text) {
var result = detectIndent(text);
return result.indent || DEFAULT_INDENTATION;
return result.indent;
}

module.exports = getIndentation;
7 changes: 4 additions & 3 deletions lib/utils/removeIndent.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@ 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');

/**
* 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();

Expand All @@ -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);
Expand Down