2024-08-19 21:22:16 +00:00
|
|
|
<script lang="ts">
|
2025-10-06 18:23:19 +00:00
|
|
|
import type {Instance} from "tippy.js"
|
2025-01-02 18:04:28 +00:00
|
|
|
import {writable} from "svelte/store"
|
2026-02-02 22:06:36 +00:00
|
|
|
import {once} from "@welshman/lib"
|
2025-04-15 18:03:27 +00:00
|
|
|
import type {EventContent} from "@welshman/util"
|
2025-02-04 03:28:29 +00:00
|
|
|
import {isMobile, preventDefault} from "@lib/html"
|
2025-09-10 22:26:39 +00:00
|
|
|
import GallerySend from "@assets/icons/gallery-send.svg?dataurl"
|
2025-10-06 18:23:19 +00:00
|
|
|
import WidgetAdd from "@assets/icons/widget-add.svg?dataurl"
|
2025-09-10 22:26:39 +00:00
|
|
|
import Plane from "@assets/icons/plane-2.svg?dataurl"
|
2024-08-26 21:43:43 +00:00
|
|
|
import Icon from "@lib/components/Icon.svelte"
|
|
|
|
|
import Button from "@lib/components/Button.svelte"
|
2025-10-06 18:23:19 +00:00
|
|
|
import Tippy from "@lib/components/Tippy.svelte"
|
|
|
|
|
import ComposeMenu from "@app/components/ComposeMenu.svelte"
|
2025-02-05 16:17:51 +00:00
|
|
|
import EditorContent from "@app/editor/EditorContent.svelte"
|
2025-01-28 17:20:34 +00:00
|
|
|
import {makeEditor} from "@app/editor"
|
2025-10-15 14:00:23 +00:00
|
|
|
import {onDestroy, onMount} from "svelte"
|
2024-08-23 20:33:36 +00:00
|
|
|
|
2025-04-15 18:03:27 +00:00
|
|
|
type Props = {
|
|
|
|
|
url?: string
|
2025-10-30 22:33:34 +00:00
|
|
|
h?: string
|
2025-10-15 14:00:23 +00:00
|
|
|
content?: string
|
2025-11-05 00:59:17 +00:00
|
|
|
onEscape?: () => void
|
2025-10-15 14:00:23 +00:00
|
|
|
onEditPrevious?: () => void
|
2025-04-15 18:03:27 +00:00
|
|
|
onSubmit: (event: EventContent) => void
|
2025-02-04 00:37:14 +00:00
|
|
|
}
|
|
|
|
|
|
2025-11-05 00:59:17 +00:00
|
|
|
const {url, h, content, onEscape, onEditPrevious, onSubmit}: 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
|
|
|
|
2025-01-28 17:20:34 +00:00
|
|
|
const uploading = writable(false)
|
2025-01-02 18:04:28 +00:00
|
|
|
|
2025-04-29 19:24:20 +00:00
|
|
|
export const focus = () => editor.then(ed => ed.chain().focus().run())
|
2025-02-04 04:57:47 +00:00
|
|
|
|
2025-10-15 14:00:23 +00:00
|
|
|
export const canEnterEditPrevious = () =>
|
|
|
|
|
editor.then(ed => ed.getText({blockSeparator: "\n"}) === "")
|
|
|
|
|
|
|
|
|
|
const handleKeyDown = async (event: KeyboardEvent) => {
|
2025-11-05 00:59:17 +00:00
|
|
|
if (event.key === "Escape") {
|
|
|
|
|
onEscape?.()
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-15 14:00:23 +00:00
|
|
|
if (event.key === "ArrowUp" && (await canEnterEditPrevious())) {
|
|
|
|
|
onEditPrevious?.()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-02 22:06:36 +00:00
|
|
|
const uploadFiles = () =>
|
|
|
|
|
// TODO: go back to the following command after PR is released
|
|
|
|
|
// https://github.com/cesardeazevedo/nostr-editor/pull/37
|
|
|
|
|
// editor.then(ed => ed.chain().selectFiles().run())
|
|
|
|
|
editor.then(ed => {
|
|
|
|
|
const input = document.createElement("input")
|
|
|
|
|
|
|
|
|
|
input.type = "file"
|
|
|
|
|
input.multiple = true
|
|
|
|
|
input.accept = "image/jpeg,image/png,image/gif,video/mp4,video/mpeg,video/webm"
|
|
|
|
|
input.style.display = "none"
|
|
|
|
|
|
|
|
|
|
const cleanup = () => {
|
|
|
|
|
input.removeEventListener("change", handleFiles)
|
|
|
|
|
input.removeEventListener("input", handleFiles)
|
|
|
|
|
input.removeEventListener("cancel", cleanup)
|
|
|
|
|
|
|
|
|
|
if (input.parentNode) {
|
|
|
|
|
input.parentNode.removeChild(input)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const handleFiles = once((event: Event) => {
|
|
|
|
|
const files = (event.target as HTMLInputElement).files
|
|
|
|
|
|
|
|
|
|
if (files) {
|
|
|
|
|
Array.from(files).forEach(file => {
|
|
|
|
|
if (file) {
|
|
|
|
|
ed.commands.addFile(file, ed.view.state.selection.from + 1)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cleanup()
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// Add both change and input listeners for iOS compatibility
|
|
|
|
|
input.addEventListener("change", handleFiles)
|
|
|
|
|
input.addEventListener("input", handleFiles)
|
|
|
|
|
input.addEventListener("cancel", cleanup)
|
|
|
|
|
|
|
|
|
|
// Attach to DOM for iOS compatibility
|
|
|
|
|
document.body.appendChild(input)
|
|
|
|
|
|
|
|
|
|
// Small delay for iOS WebView
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
input.click()
|
|
|
|
|
}, 100)
|
|
|
|
|
})
|
2025-01-02 18:04:28 +00:00
|
|
|
|
2025-10-06 18:23:19 +00:00
|
|
|
const showPopover = () => popover?.show()
|
|
|
|
|
|
|
|
|
|
const hidePopover = () => popover?.hide()
|
|
|
|
|
|
2025-04-29 19:24:20 +00:00
|
|
|
const submit = async () => {
|
2025-01-02 18:04:28 +00:00
|
|
|
if ($uploading) return
|
2024-11-16 16:04:09 +00:00
|
|
|
|
2025-04-29 19:24:20 +00:00
|
|
|
const ed = await editor
|
|
|
|
|
const content = ed.getText({blockSeparator: "\n"}).trim()
|
|
|
|
|
const tags = ed.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-04-29 19:24:20 +00:00
|
|
|
ed.chain().clearContent().run()
|
2024-08-22 23:16:35 +00:00
|
|
|
}
|
|
|
|
|
|
2025-10-06 18:23:19 +00:00
|
|
|
const editor = makeEditor({url, content, autofocus, submit, uploading, aggressive: true})
|
|
|
|
|
|
|
|
|
|
let popover: Instance | undefined = $state()
|
2025-10-15 14:00:23 +00:00
|
|
|
|
|
|
|
|
onMount(async () => {
|
|
|
|
|
const ed = await editor
|
|
|
|
|
ed.view.dom.addEventListener("keydown", handleKeyDown)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
onDestroy(async () => {
|
|
|
|
|
const ed = await editor
|
|
|
|
|
ed?.view?.dom.removeEventListener("keydown", handleKeyDown)
|
|
|
|
|
})
|
2024-08-19 21:22:16 +00:00
|
|
|
</script>
|
|
|
|
|
|
2025-11-05 01:14:02 +00:00
|
|
|
<form class="relative flex gap-2 p-2" onsubmit={preventDefault(submit)}>
|
2025-10-06 18:23:19 +00:00
|
|
|
<div class="join">
|
|
|
|
|
<Button
|
2026-02-03 19:00:13 +00:00
|
|
|
class="center join-item h-10 w-10 min-w-10 rounded-full border border-solid border-base-200 bg-base-300"
|
2025-10-06 18:23:19 +00:00
|
|
|
disabled={$uploading}
|
|
|
|
|
onclick={uploadFiles}>
|
|
|
|
|
{#if $uploading}
|
|
|
|
|
<span class="loading loading-spinner loading-xs"></span>
|
|
|
|
|
{:else}
|
|
|
|
|
<Icon icon={GallerySend} />
|
|
|
|
|
{/if}
|
|
|
|
|
</Button>
|
|
|
|
|
<Tippy
|
|
|
|
|
bind:popover
|
|
|
|
|
component={ComposeMenu}
|
2025-10-30 22:33:34 +00:00
|
|
|
props={{url, h, onClick: hidePopover}}
|
2025-10-06 18:23:19 +00:00
|
|
|
params={{trigger: "manual", interactive: true}}>
|
|
|
|
|
<Button
|
2026-02-03 19:00:13 +00:00
|
|
|
class="center join-item h-10 w-10 min-w-10 rounded-full border border-solid border-base-200 bg-base-300"
|
2025-10-06 18:23:19 +00:00
|
|
|
disabled={$uploading}
|
|
|
|
|
onclick={showPopover}>
|
|
|
|
|
<Icon icon={WidgetAdd} />
|
|
|
|
|
</Button>
|
|
|
|
|
</Tippy>
|
|
|
|
|
</div>
|
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}
|
2025-02-04 01:21:46 +00:00
|
|
|
onclick={submit}>
|
2025-09-10 22:26:39 +00:00
|
|
|
<Icon icon={Plane} />
|
2025-01-15 17:07:12 +00:00
|
|
|
</Button>
|
2024-11-16 16:04:09 +00:00
|
|
|
</form>
|