Skip to content
This repository has been archived by the owner on May 2, 2024. It is now read-only.

Commit

Permalink
Fix tab key in the editor
Browse files Browse the repository at this point in the history
  • Loading branch information
cosmicproc committed Mar 30, 2024
1 parent 7d62ccc commit c4f61cc
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/lib/app.css
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
}

.input {
@apply inline flex-grow rounded-full border-2 border-neutral-300 bg-neutral-200 px-6 py-3 text-inherit focus:shadow-lg dark:border-neutral-700 dark:bg-neutral-800;
@apply inline flex-grow rounded-full border-2 border-neutral-300 bg-neutral-200 px-6 py-3 text-inherit focus:shadow dark:border-neutral-700 dark:bg-neutral-800;
}
}

Expand Down
33 changes: 24 additions & 9 deletions src/lib/components/Editor.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import hljs from 'highlight.js';
import 'highlight.js/styles/monokai.css';
import markdownit from 'markdown-it';
import { afterUpdate, beforeUpdate } from 'svelte';
const md = markdownit({
linkify: true,
Expand All @@ -40,7 +41,10 @@
},
});
let source: HTMLElement;
let selectionStart: number;
let selectionEnd: number;
let source: HTMLTextAreaElement;
let preview: HTMLElement;
let sourceContent: string;
let previewContent: string = '';
Expand All @@ -60,8 +64,8 @@
let autoSynced = false;
function syncScroll(this: HTMLElement) {
let first = preview;
let second = source;
let first: HTMLElement | HTMLTextAreaElement = preview;
let second: HTMLElement | HTMLTextAreaElement = source;
if (this === source) {
first = source;
second = preview;
Expand All @@ -81,12 +85,12 @@
function handleKeydown(this: HTMLTextAreaElement, event: KeyboardEvent) {
if (event.key !== 'Tab') return;
event.preventDefault();
const selStart = this?.selectionStart;
const selEnd = this?.selectionEnd;
this.value =
this.value.substring(0, selStart) +
({ selectionStart, selectionEnd } = source);
sourceContent =
sourceContent.substring(0, selectionStart) +
'\t' +
this.value.substring(selEnd);
sourceContent.substring(selectionEnd);
source.setSelectionRange(selectionStart + 1, selectionEnd + 1);
}
function handleInput() {
Expand All @@ -97,6 +101,17 @@
updated[index].content = sourceContent;
notes.set(updated);
}
beforeUpdate(() => {
if (source) {
({ selectionStart, selectionEnd } = source);
}
});
afterUpdate(() => {
source.setSelectionRange(selectionStart, selectionEnd);
source.focus();
});
</script>

<div class="flex flex-grow basis-0 flex-col overflow-hidden p-10 lg:flex-row">
Expand Down Expand Up @@ -190,7 +205,7 @@
>
<article
data-testid="result-text"
class="break-word prose mx-auto pb-8 dark:prose-invert dark:prose-pre:bg-gray-900"
class="prose mx-auto break-words pb-8 dark:prose-invert dark:prose-pre:bg-gray-900"
>
<!-- eslint-disable-next-line svelte/no-at-html-tags -->
{@html previewContent}
Expand Down

0 comments on commit c4f61cc

Please sign in to comment.