flotilla/src/app/components/ChannelCompose.svelte

68 lines
1.9 KiB
Svelte
Raw Normal View History

2024-08-19 21:22:16 +00:00
<script lang="ts">
2025-02-05 18:47:56 +00:00
import {onMount} from "svelte"
2025-01-02 18:04:28 +00:00
import {writable} from "svelte/store"
2025-02-04 03:28:29 +00:00
import {isMobile, preventDefault} from "@lib/html"
2024-08-26 21:43:43 +00:00
import Icon from "@lib/components/Icon.svelte"
import Button from "@lib/components/Button.svelte"
2025-02-05 16:17:51 +00:00
import EditorContent from "@app/editor/EditorContent.svelte"
import {makeEditor} from "@app/editor"
2024-08-23 20:33:36 +00:00
2025-02-04 00:37:14 +00:00
interface Props {
onSubmit: any
content?: string
}
const {onSubmit, content = ""}: Props = $props()
2025-01-02 18:04:28 +00:00
2025-02-04 04:57:47 +00:00
const autofocus = !isMobile
2025-01-02 18:04:28 +00:00
const uploading = writable(false)
2025-01-02 18:04:28 +00:00
2025-02-04 04:57:47 +00:00
export const focus = () => editor.chain().focus().run()
const uploadFiles = () => editor.chain().selectFiles().run()
2025-01-02 18:04:28 +00:00
const submit = () => {
if ($uploading) return
2024-11-16 16:04:09 +00:00
2025-02-04 04:57:47 +00:00
const content = editor.getText({blockSeparator: "\n"}).trim()
const tags = editor.storage.nostr.getEditorTags()
2025-01-15 17:07:12 +00:00
if (!content) return
onSubmit({content, tags})
2024-08-23 20:33:36 +00:00
2025-02-04 04:57:47 +00:00
editor.chain().clearContent().run()
2024-08-22 23:16:35 +00:00
}
2025-02-04 04:57:47 +00:00
const editor = makeEditor({autofocus, submit, uploading, aggressive: true})
2025-01-02 18:04:28 +00:00
onMount(() => {
2025-02-04 04:57:47 +00:00
editor.chain().setContent(content).run()
})
2024-08-19 21:22:16 +00:00
</script>
2025-02-04 00:37:14 +00:00
<form class="relative z-feature flex gap-2 p-2" onsubmit={preventDefault(submit)}>
2024-08-26 21:43:43 +00:00
<Button
2024-09-11 22:21:25 +00:00
data-tip="Add an image"
2024-10-25 00:25:28 +00:00
class="center tooltip tooltip-right h-10 w-10 min-w-10 rounded-box bg-base-300 transition-colors hover:bg-base-200"
2025-01-02 18:04:28 +00:00
disabled={$uploading}
onclick={uploadFiles}>
2025-01-02 18:04:28 +00:00
{#if $uploading}
2024-08-22 23:16:35 +00:00
<span class="loading loading-spinner loading-xs"></span>
{:else}
<Icon icon="gallery-send" />
{/if}
</Button>
2024-09-23 20:58:01 +00:00
<div class="chat-editor flex-grow overflow-hidden">
2025-02-04 04:57:47 +00:00
<EditorContent {editor} />
2024-08-22 23:16:35 +00:00
</div>
2025-01-15 17:07:12 +00:00
<Button
data-tip="{window.navigator.platform.includes('Mac') ? 'cmd' : 'ctrl'}+enter to send"
class="center tooltip tooltip-left absolute right-4 h-10 w-10 min-w-10 rounded-full"
disabled={$uploading}
onclick={submit}>
2025-01-15 17:07:12 +00:00
<Icon icon="plain" />
</Button>
2024-11-16 16:04:09 +00:00
</form>