Skip to content

add optional indentWidth for tab key #175

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

Merged
merged 1 commit into from
Oct 16, 2024
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
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