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

feature: indentations by tab #40

Merged
merged 1 commit into from
Mar 12, 2021
Merged
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@editorjs/code",
"version": "2.6.0",
"version": "2.7.0",
"keywords": [
"codex editor",
"code",
Expand Down
75 changes: 71 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/**
* Build styles
*/
require('./index.css').toString();
import { getLineStartPosition } from './utils/string';
import './index.css';

/**
* CodeTool for Editor.js
Expand All @@ -17,7 +18,7 @@ require('./index.css').toString();
/**
* Code Tool for the Editor.js allows to include code examples in your articles.
*/
class CodeTool {
export default class CodeTool {

/**
* Notify core that read-only mode is supported
Expand Down Expand Up @@ -99,6 +100,17 @@ class CodeTool {

wrapper.appendChild(textarea);

/**
* Enable keydown handlers
*/
textarea.addEventListener('keydown', (event) => {
switch (event.code) {
case 'Tab':
this.tabHandler(event);
break;
}
});

this.nodes.textarea = textarea;

return wrapper;
Expand Down Expand Up @@ -209,6 +221,61 @@ class CodeTool {
code: true, // Allow HTML tags
};
}
}

module.exports = CodeTool;
/**
* Handles Tab key pressing (adds/removes indentations)
*
* @private
* @param {KeyboardEvent} event - keydown
* @returns {void}
*/
tabHandler(event) {
/**
* Prevent editor.js tab handler
*/
event.stopPropagation();

/**
* Prevent native tab behaviour
*/
event.preventDefault();

const textarea = event.target;
const isShiftPressed = event.shiftKey;
const caretPosition = textarea.selectionStart;
const value = textarea.value;
const indentation = ' ';

let newCaretPosition;

/**
* For Tab pressing, just add an indentation to the caret position
*/
if (!isShiftPressed) {
newCaretPosition = caretPosition + indentation.length;

textarea.value = value.substring(0, caretPosition) + indentation + value.substring(caretPosition);
} else {
/**
* For Shift+Tab pressing, remove an indentation from the start of line
*/
const currentLineStart = getLineStartPosition(value, caretPosition);
const firstLineChars = value.substr(currentLineStart, indentation.length);

if (firstLineChars !== indentation) {
return;
}

/**
* Trim the first two chars from the start of line
*/
textarea.value = value.substring(0, currentLineStart) + value.substring(currentLineStart + indentation.length);
newCaretPosition = caretPosition - indentation.length;
}

/**
* Restore the caret
*/
textarea.setSelectionRange(newCaretPosition, newCaretPosition);
}
}
35 changes: 35 additions & 0 deletions src/utils/string.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* Return the position of line starting from passed point
*
* ┌───────────────┐
* │1234\n │
* │2eda | dadd\n │ <-- returns 5
* └───────────────┘
*
* @param {string} string - string to process
* @param {number} position - search starting position
* @returns {number}
*/
export function getLineStartPosition(string, position) {
const charLength = 1;
let char = '';

/**
* Iterate through all the chars before the position till the
* - end of line (\n)
* - or start of string (position === 0)
*/
while (char !== '\n' && position > 0) {
position = position - charLength;
char = string.substr(position, charLength);
}

/**
* Do not count the linebreak symbol because it is related to the previous line
*/
if (char === '\n') {
position += 1;
}

return position;
}
15 changes: 8 additions & 7 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,23 @@ module.exports = {
presets: [ '@babel/preset-env' ],
},
},
]
],
},
{
test: /\.css$/,
use: [
'style-loader',
'css-loader'
]
}
]
'css-loader',
],
},
],
},
output: {
path: __dirname + '/dist',
publicPath: '/',
filename: 'bundle.js',
library: 'CodeTool',
libraryTarget: 'umd'
}
libraryTarget: 'umd',
libraryExport: 'default',
},
};