formatting

This commit is contained in:
Luck
2021-04-02 13:05:15 +01:00
parent 9701bd6529
commit 3fb11c5a9a
9 changed files with 188 additions and 115 deletions

View File

@@ -29,12 +29,12 @@ export default function EditorTextArea({ code, setCode, language, fontSize }) {
placeholder={'Paste (or type) some code...'}
padding={10}
size={fontSize}
textareaId='code-area'
textareaId="code-area"
autoFocus={true}
onKeyDown={keydown}
/>
</EditorPrismStyle>
)
);
}
const StyledReactEditor = styled(ReactEditor)`
@@ -101,29 +101,43 @@ function handleKeydown(e, editor) {
if (pair) {
e.preventDefault();
editor._applyEdits({
value: value.substring(0, selectionStart) + pair[0] + pair[1] + value.substring(selectionEnd),
value:
value.substring(0, selectionStart) +
pair[0] +
pair[1] +
value.substring(selectionEnd),
selectionStart: selectionStart + 1,
selectionEnd: selectionStart + 1
selectionEnd: selectionStart + 1,
});
}
// When pressing enter immediately after an open bracket, automatically add a newline plus extra indent
if (e.keyCode === KEYCODE_ENTER && selectionEnd !== 0 && value[selectionEnd - 1] === '{') {
if (
e.keyCode === KEYCODE_ENTER &&
selectionEnd !== 0 &&
value[selectionEnd - 1] === '{'
) {
const line = editor._getLines(value, selectionStart).pop();
const matches = line.match(/^\s+/);
const existingIndent = (matches ? matches[0] : '');
const existingIndent = matches ? matches[0] : '';
const indent = ' ';
const updatedValue = value.substring(0, selectionStart) +
'\n' + existingIndent + indent +
'\n' + existingIndent + value.substring(selectionEnd);
const updatedSelection = selectionStart + 1 /* newline */ + existingIndent.length + indent.length;
const updatedValue =
value.substring(0, selectionStart) +
'\n' +
existingIndent +
indent +
'\n' +
existingIndent +
value.substring(selectionEnd);
const updatedSelection =
selectionStart + 1 /* newline */ + existingIndent.length + indent.length;
e.preventDefault();
editor._applyEdits({
value: updatedValue,
selectionStart: updatedSelection,
selectionEnd: updatedSelection
})
selectionEnd: updatedSelection,
});
}
}