Commit 555b23be authored by GuiΩ's avatar GuiΩ
Browse files

fix(CodeEditor): deal correctly with Tab and Enter after last char

parent c0308658
......@@ -11,7 +11,6 @@
-->
<script lang="ts" setup>
interface ICodeEditorProps {
code?: string,
tabsize?: number
......@@ -19,10 +18,10 @@
const emit = defineEmits(['input', 'update:code'])
const props: ICodeEditorProps = defineProps<{
code?: '',
tabsize?: 2
}>()
const props = withDefaults(defineProps<ICodeEditorProps>(), {
code: '',
tabsize: 2
})
function handleInput(event: InputEvent) {
const textarea = event.target as HTMLInputElement
......@@ -49,11 +48,12 @@
if (start === end && textarea.value.charAt(start ) === "\n") {
textarea.selectionEnd = start + textarea.value.substring(start + 1).search(/[^ ]/);
}
break;
break
case 'Enter':
event.preventDefault();
let spaces = "\n" + " ".repeat(lineIndentSize);
let nextCharIndex = end + textarea.value.substring(end).search(/[^ ]/)
let nextNotSpace = textarea.value.substring(end).search(/[^ ]/)
let nextCharIndex = end + (nextNotSpace > -1 ? nextNotSpace : 0)
if (start <= lineStart + lineIndentSize) { // cursor before the first non empty char
end = lineStart + lineIndentSize + end - start + 1;
start = lineStart + lineIndentSize + 1;
......@@ -64,7 +64,7 @@
textarea.selectionStart = start + lineIndentSize + 1;
textarea.selectionEnd = textarea.selectionStart;
emit('input', textarea.value);
break;
break
case 'Tab':
event.preventDefault();
let nbspaces = props.tabsize!;
......@@ -94,7 +94,7 @@
textarea.selectionEnd = textarea.selectionStart;
}
emit('input', textarea.value);
break;
break
}
}
</script>
......@@ -106,8 +106,7 @@ textarea(
rows="30"
spellcheck="false"
@keydown="handleKeyDown"
@input="handleInput"
)
@input="handleInput")/
</template>
<style>
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment