/** @jsx h */ import { createRef, h } from "https://esm.sh/preact@10.11.3"; import { tw } from "https://esm.sh/twind@0.16.16"; import { CenterClass, IconButtonClass, KeyboradClass, LinearGradientsClass, NoOutlineClass, } from "./components/tw.ts"; import { EventEmitter } from "../event-bus.ts"; import { NostrKind } from "https://raw.githubusercontent.com/BlowaterNostr/nostr.ts/main/nostr.ts"; import { PublicKey } from "https://raw.githubusercontent.com/BlowaterNostr/nostr.ts/main/key.ts"; import { Tag } from "../nostr.ts"; import { ImageIcon } from "./icons2/image-icon.tsx"; import { DividerBackgroundColor, HoverButtonBackgroudColor, PrimaryBackgroundColor, PrimaryTextColor, } from "./style/colors.ts"; import { SendIcon } from "./icons2/send-icon.tsx"; import { RemoveIcon } from "./icons2/remove-icon.tsx"; export type EditorModel = DM_EditorModel | Social_EditorModel; export type DM_EditorModel = { id: string; text: string; files: Blob[]; tags: Tag[]; readonly target: DM_Target; }; export type Social_EditorModel = { id: string; text: string; files: Blob[]; tags: Tag[]; readonly target: Social_Target; }; export type EditorSubmissionTarget = DM_Target | Social_Target; export type DM_Target = { kind: NostrKind.DIRECT_MESSAGE; receiver: { pubkey: PublicKey; name?: string; picture?: string; }; }; export type Social_Target = { kind: NostrKind.TEXT_NOTE; }; export function new_DM_EditorModel(receiver: { pubkey: PublicKey; name?: string; picture?: string; }): DM_EditorModel { return { id: receiver.pubkey.hex, text: "", files: [], tags: [], target: { kind: NostrKind.DIRECT_MESSAGE, receiver, }, }; } export function new_Social_EditorModel(): Social_EditorModel { return { id: "social", text: "", files: [], tags: [], target: { kind: NostrKind.TEXT_NOTE, }, }; } export type EditorEvent = SendMessage | UpdateMessageText | UpdateMessageFiles; export type SendMessage = { readonly type: "SendMessage"; readonly id: string; readonly target: EditorSubmissionTarget; text: string; files: Blob[]; tags: Tag[]; }; export type UpdateMessageText = { readonly type: "UpdateMessageText"; readonly id: string; readonly target: EditorSubmissionTarget; readonly text: string; }; export type UpdateMessageFiles = { readonly type: "UpdateMessageFiles"; readonly id: string; readonly target: EditorSubmissionTarget; readonly files: Blob[]; }; export function Editor(props: { // UI readonly placeholder: string; readonly maxHeight: string; // Logic readonly model: EditorModel; // readonly eventEmitter: EventEmitter; }) { const textareaElement = createRef(); const uploadFileInput = createRef(); const removeFile = (index: number) => { props.eventEmitter.emit({ type: "UpdateMessageFiles", id: props.model.id, target: props.model.target, files: props.model.files.slice(0, index).concat( props.model.files.slice(index + 1), ), }); }; const sendMessage = async () => { props.eventEmitter.emit({ type: "SendMessage", id: props.model.id, tags: props.model.tags, target: props.model.target, files: props.model.files, text: props.model.text, }); textareaElement.current.setAttribute( "rows", "1", ); }; return (
{ let propsfiles = props.model.files; const files = e.currentTarget.files; if (!files) { return; } for (let i = 0; i < files.length; i++) { const file = files.item(i); if (!file) { continue; } propsfiles = propsfiles.concat([file]); } props.eventEmitter.emit({ type: "UpdateMessageFiles", id: props.model.id, target: props.model.target, files: propsfiles, }); }} class={tw`hidden`} />
{props.model.files.length > 0 ? (
    {props.model.files.map((file, index) => { return (
  • ); })}
) : undefined}
); }