Skip to content

Commit

Permalink
Autofocus the comment editor, submit comment on ctrl/cmd+enter
Browse files Browse the repository at this point in the history
  • Loading branch information
sheodox committed Aug 15, 2023
1 parent 673cabc commit 2bcc7cb
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 4 deletions.
3 changes: 3 additions & 0 deletions src/lib/MarkdownEditor.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<Stack dir="c">
<Stack dir="c">
<label for={textareaId} class="fw-bold p-2">{label}</label>
<!-- svelte-ignore a11y-autofocus -->
<textarea
id={textareaId}
rows="6"
Expand All @@ -26,6 +27,7 @@
bind:this={textarea}
{required}
on:keydown={keydown}
{autofocus}
/>
<Stack dir="r" gap={1}>
<IconButton icon="bold" text="Bold" type="button" on:click={format.bold} />
Expand Down Expand Up @@ -76,6 +78,7 @@
export let name: string;
export let disabled = false;
export let required = true;
export let autofocus = false;
let busy = false;
Expand Down
14 changes: 11 additions & 3 deletions src/lib/comments/Comment.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@
</Stack>
{/if}
{#if $buffer[bk.showEditComposer] && !postLocked}
<form bind:this={editForm} class="reply-editor">
<form bind:this={editForm} class="reply-editor" use:submitOnHardEnter>
<input type="hidden" name="commentId" value={comment.id} />
<CommentEditor
bind:value={$buffer[bk.editText]}
Expand All @@ -184,18 +184,20 @@
on:cancel={() => ($buffer[bk.showEditComposer] = false)}
submitting={$editState.busy}
community={contentView.view.community}
autofocus
/>
</form>
{/if}
{#if $buffer[bk.showReplyComposer] && $profile.loggedIn && !postLocked}
<form bind:this={replyForm} class="reply-editor">
<form bind:this={replyForm} class="reply-editor" use:submitOnHardEnter>
<input type="hidden" name="parentId" value={comment.id} />
<CommentEditor
cancellable
bind:value={$buffer[bk.replyText]}
on:cancel={() => ($buffer[bk.showReplyComposer] = false)}
submitting={$replyState.busy}
community={contentView.view.community}
autofocus
/>
</form>
{/if}
Expand Down Expand Up @@ -228,7 +230,13 @@
import CommunityLink from '../CommunityLink.svelte';
import EllipsisText from '$lib/EllipsisText.svelte';
import { getCommentContextId, nameAtInstance } from '../nav-utils';
import { createStatefulForm, type ActionFn, createStatefulAction, type ExtraAction } from '../utils';
import {
createStatefulForm,
type ActionFn,
createStatefulAction,
type ExtraAction,
submitOnHardEnter
} from '../utils';
import { getVirtualFeedBuffer } from '../virtual-feed';
import {
commentViewToContentView,
Expand Down
3 changes: 2 additions & 1 deletion src/lib/comments/CommentEditor.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<div class:submitting>
<Stack dir="c" gap={2}>
<Stack dir="c">
<MarkdownEditor name="content" {label} bind:value />
<MarkdownEditor name="content" {label} bind:value {autofocus} />
</Stack>

<Stack dir="r" justify="between" align="center">
Expand Down Expand Up @@ -54,6 +54,7 @@
export let showSubmit = true;
export let useLanguage = true;
export let community: Community | undefined = undefined;
export let autofocus = false;
const initialValue = value;
const communityContext = getCommunityContext();
Expand Down
28 changes: 28 additions & 0 deletions src/lib/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { goto } from '$app/navigation';
import { readable, writable } from 'svelte/store';
import { getCtrlBasedHotkeys } from './app-context';

// a wrapper for things in a MenuButton, used by ExtraActions.svelte
export interface ExtraAction {
Expand Down Expand Up @@ -263,3 +264,30 @@ export function isElementEditable(el: HTMLElement) {

return ['input', 'textarea', 'select'].includes(nodeName);
}

export function submitOnHardEnter(formEl: HTMLFormElement) {
// make a button that can trigger a proper form submit.
// we need a button that would trigger submitting a form if
// clicked, so formEl.requestSubmit works. and
// formEL.submit() is unusable because it doesn't run handlers
// and does an unwanted full page navigation
const submitEl = document.createElement('button');
submitEl.style.display = 'none';
submitEl.classList.add('hard-enter-submit-button');

formEl.appendChild(submitEl);
function onKeydown(e: KeyboardEvent) {
const ctrl = getCtrlBasedHotkeys() ? e.ctrlKey : e.metaKey;

if (e.key === 'Enter' && ctrl) {
formEl.requestSubmit(submitEl);
}
}

formEl.addEventListener('keydown', onKeydown);
return {
destroy: () => {
formEl.addEventListener('keydown', onKeydown);
}
};
}

0 comments on commit 2bcc7cb

Please sign in to comment.