Skip to content

Commit

Permalink
feat: add optional indentWidth (#175)
Browse files Browse the repository at this point in the history
  • Loading branch information
l9c authored Oct 16, 2024
1 parent 624e0f1 commit 30a6878
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
4 changes: 4 additions & 0 deletions core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,10 @@ interface TextareaCodeEditorProps extends React.TextareaHTMLAttributes<HTMLTextA
*/
minHeight?: number;
onKeyDown?: (event: React.KeyboardEvent<HTMLTextAreaElement>) => void | boolean;
/**
* The number of spaces for indentation when pressing tab key. Default: `2`.
*/
indentWidth?: number
}
```
Expand Down
7 changes: 6 additions & 1 deletion core/src/Editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ export interface TextareaCodeEditorProps extends React.TextareaHTMLAttributes<HT
*/
minHeight?: number;
onKeyDown?: (event: React.KeyboardEvent<HTMLTextAreaElement>) => void | boolean;
/**
* The number of spaces for indentation when pressing tab key. Default: `2`.
*/
indentWidth?: number;
}

export default React.forwardRef<HTMLTextAreaElement, TextareaCodeEditorProps>((props, ref) => {
Expand All @@ -46,6 +50,7 @@ export default React.forwardRef<HTMLTextAreaElement, TextareaCodeEditorProps>((p
style,
rehypePlugins,
onChange,
indentWidth = 2,
...other
} = props;

Expand Down Expand Up @@ -93,7 +98,7 @@ export default React.forwardRef<HTMLTextAreaElement, TextareaCodeEditorProps>((p
const keyDown = (evn: React.KeyboardEvent<HTMLTextAreaElement>) => {
if (other.readOnly) return;
if (!other.onKeyDown || other.onKeyDown(evn) !== false) {
shortcuts(evn);
shortcuts(evn, indentWidth);
}
};

Expand Down
14 changes: 8 additions & 6 deletions core/src/shortcuts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,30 @@ import React from 'react';
import { stopPropagation } from './utils';
import { SelectionText } from './SelectionText';

export default function shortcuts(e: React.KeyboardEvent<HTMLTextAreaElement>) {
export default function shortcuts(e: React.KeyboardEvent<HTMLTextAreaElement>, indentWidth: number = 2) {
const api = new SelectionText(e.target as HTMLTextAreaElement);
/**
* Support of shortcuts for React v16
* https://github.com/uiwjs/react-textarea-code-editor/issues/128
* https://blog.saeloun.com/2021/04/23/react-keyboard-event-code.html
*/
const code = (e.code || e.nativeEvent.code).toLocaleLowerCase();
const indent = ' '.repeat(indentWidth);

if (code === 'tab') {
stopPropagation(e);
if (api.start === api.end) {
if (e.shiftKey) {
api.lineStarRemove(' ');
api.lineStarRemove(indent);
} else {
api.insertText(' ').position(api.start + 2, api.end + 2);
api.insertText(indent).position(api.start + indentWidth, api.end + indentWidth);
}
} else if (api.getSelectedValue().indexOf('\n') > -1 && e.shiftKey) {
api.lineStarRemove(' ');
api.lineStarRemove(indent);
} else if (api.getSelectedValue().indexOf('\n') > -1) {
api.lineStarInstert(' ');
api.lineStarInstert(indent);
} else {
api.insertText(' ').position(api.start + 2, api.end);
api.insertText(indent).position(api.start + indentWidth, api.end);
}
api.notifyChange();
} else if (code === 'enter') {
Expand Down

0 comments on commit 30a6878

Please sign in to comment.