/** @jsx h */ import { createRef, h } from "https://esm.sh/preact@10.17.1"; import { tw } from "https://esm.sh/twind@0.16.16"; import { CenterClass, LinearGradientsClass, NoOutlineClass } from "./components/tw.ts"; import { emitFunc } from "../event-bus.ts"; import { PublicKey } from "../lib/nostr-ts/key.ts"; import { ImageIcon } from "./icons/image-icon.tsx"; import { DividerBackgroundColor, PrimaryBackgroundColor, PrimaryTextColor, SecondaryBackgroundColor, } from "./style/colors.ts"; import { SendIcon } from "./icons/send-icon.tsx"; import { Component } from "https://esm.sh/preact@10.17.1"; import { RemoveIcon } from "./icons/remove-icon.tsx"; import { isMobile } from "./_helper.ts"; export type EditorModel = { readonly pubkey: PublicKey; text: string; files: Blob[]; }; export function new_DM_EditorModel( pubkey: PublicKey, ): EditorModel { return { pubkey: pubkey, text: "", files: [], }; } export type EditorEvent = SendMessage | UpdateEditorText | UpdateMessageFiles; export type SendMessage = { readonly type: "SendMessage"; readonly pubkey: PublicKey; text: string; files: Blob[]; isGroupChat: boolean; }; export type UpdateEditorText = { readonly type: "UpdateEditorText"; readonly pubkey: PublicKey; readonly isGroupChat: boolean; readonly text: string; }; export type UpdateMessageFiles = { readonly type: "UpdateMessageFiles"; readonly pubkey: PublicKey; readonly isGroupChat: boolean; readonly files: Blob[]; }; type EditorProps = { // UI readonly placeholder: string; readonly maxHeight: string; // Logic readonly targetNpub: PublicKey; readonly text: string; files: Blob[]; // readonly emit: emitFunc; readonly isGroupChat: boolean; }; export class Editor extends Component { componentWillReceiveProps(nextProps: Readonly, nextContext: any): void { if (!isMobile()) { this.textareaElement.current.focus(); } } textareaElement = createRef(); render(props: EditorProps) { const uploadFileInput = createRef(); const removeFile = (index: number) => { props.emit({ type: "UpdateMessageFiles", files: props.files.slice(0, index).concat( props.files.slice(index + 1), ), pubkey: props.targetNpub, isGroupChat: props.isGroupChat, }); }; const sendMessage = async () => { props.emit({ type: "SendMessage", pubkey: props.targetNpub, files: props.files, text: props.text, isGroupChat: props.isGroupChat, }); this.textareaElement.current.setAttribute( "rows", "1", ); }; return (
{ let propsfiles = props.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.emit({ type: "UpdateMessageFiles", files: propsfiles, pubkey: props.targetNpub, isGroupChat: props.isGroupChat, }); }} class={tw`hidden`} />
{props.files.length > 0 ? (
    {props.files.map((file, index) => { return (
  • ); })}
) : undefined}
); } }