blowater/UI/editor.tsx

282 lines
11 KiB
TypeScript
Raw Normal View History

2023-06-30 14:05:57 +00:00
/** @jsx h */
import { createRef, h } from "https://esm.sh/preact@10.17.1";
import { CenterClass, LinearGradientsClass, NoOutlineClass } from "./components/tw.ts";
import { emitFunc } from "../event-bus.ts";
2023-06-30 14:05:57 +00:00
2023-08-28 17:58:05 +00:00
import { PublicKey } from "../lib/nostr-ts/key.ts";
2023-11-11 11:19:21 +00:00
import { ImageIcon } from "./icons/image-icon.tsx";
2023-12-18 10:23:15 +00:00
import { DividerBackgroundColor, PrimaryBackgroundColor, PrimaryTextColor } from "./style/colors.ts";
2023-11-11 11:19:21 +00:00
import { SendIcon } from "./icons/send-icon.tsx";
import { Component } from "https://esm.sh/preact@10.17.1";
2023-11-11 11:19:21 +00:00
import { RemoveIcon } from "./icons/remove-icon.tsx";
import { isMobile } from "./_helper.ts";
2023-06-30 14:05:57 +00:00
export type EditorModel = {
readonly pubkey: PublicKey;
2023-06-30 14:05:57 +00:00
text: string;
files: Blob[];
};
export function new_DM_EditorModel(
pubkey: PublicKey,
): EditorModel {
2023-06-30 14:05:57 +00:00
return {
pubkey: pubkey,
2023-06-30 14:05:57 +00:00
text: "",
files: [],
};
}
export type EditorEvent = SendMessage | UpdateEditorText | UpdateMessageFiles;
2023-06-30 14:05:57 +00:00
export type SendMessage = {
readonly type: "SendMessage";
readonly pubkey: PublicKey;
2023-06-30 14:05:57 +00:00
text: string;
files: Blob[];
isGroupChat: boolean;
2023-06-30 14:05:57 +00:00
};
export type UpdateEditorText = {
readonly type: "UpdateEditorText";
readonly pubkey: PublicKey;
readonly isGroupChat: boolean;
2023-06-30 14:05:57 +00:00
readonly text: string;
};
export type UpdateMessageFiles = {
readonly type: "UpdateMessageFiles";
readonly pubkey: PublicKey;
readonly isGroupChat: boolean;
2023-06-30 14:05:57 +00:00
readonly files: Blob[];
};
type EditorProps = {
2023-06-30 14:05:57 +00:00
// UI
readonly placeholder: string;
readonly maxHeight: string;
// Logic
readonly targetNpub: PublicKey;
readonly text: string;
files: Blob[];
2023-06-30 14:05:57 +00:00
//
readonly emit: emitFunc<EditorEvent>;
readonly isGroupChat: boolean;
};
2023-06-30 14:05:57 +00:00
export class Editor extends Component<EditorProps> {
componentWillReceiveProps(nextProps: Readonly<EditorProps>, nextContext: any): void {
if (!isMobile()) {
this.textareaElement.current.focus();
}
}
textareaElement = createRef();
render(props: EditorProps) {
const uploadFileInput = createRef();
2023-06-30 14:05:57 +00:00
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,
});
};
2023-06-30 14:05:57 +00:00
const sendMessage = async () => {
props.emit({
type: "SendMessage",
pubkey: props.targetNpub,
2023-10-21 11:29:47 +00:00
files: props.files,
text: props.text,
isGroupChat: props.isGroupChat,
});
this.textareaElement.current.setAttribute(
"rows",
"1",
);
};
return (
2023-12-18 10:23:15 +00:00
<div class={`flex mb-4 mx-5 mobile:mx-2 mobile:mb-2 items-center`}>
<button
2023-12-18 10:23:15 +00:00
class={`min-w-[3rem] mobile:min-w-[2rem] w-[3rem] mobile:w-8 h-[3rem] mobile:h-8 hover:bg-[${DividerBackgroundColor}] group ${CenterClass} rounded-[50%] ${NoOutlineClass}`}
onClick={() => {
if (uploadFileInput.current) {
uploadFileInput.current.click();
2023-06-30 14:05:57 +00:00
}
}}
>
<ImageIcon
2023-12-18 10:23:15 +00:00
class={`h-[2rem] w-[2rem] mobile:w-6 mobile:h-6 stroke-current text-[${PrimaryTextColor}4D] group-hover:text-[${PrimaryTextColor}]`}
style={{
fill: "none",
}}
/>
</button>
<input
ref={uploadFileInput}
type="file"
accept="image/*"
multiple
onChange={async (e) => {
let propsfiles = props.files;
const files = e.currentTarget.files;
if (!files) {
2023-06-30 14:05:57 +00:00
return;
}
for (let i = 0; i < files.length; i++) {
const file = files.item(i);
if (!file) {
continue;
2023-06-30 14:05:57 +00:00
}
propsfiles = propsfiles.concat([file]);
2023-06-30 14:05:57 +00:00
}
props.emit({
type: "UpdateMessageFiles",
files: propsfiles,
pubkey: props.targetNpub,
isGroupChat: props.isGroupChat,
});
2023-06-30 14:05:57 +00:00
}}
2023-12-18 10:23:15 +00:00
class={`hidden`}
/>
<div
2023-12-18 10:23:15 +00:00
class={`mx-2 p-[0.75rem] mobile:p-2 mobile:text-sm bg-[${DividerBackgroundColor}] rounded-lg flex flex-col flex-1 overflow-hidden`}
2023-06-30 14:05:57 +00:00
>
{props.files.length > 0
? (
<ul
2023-12-18 10:23:15 +00:00
class={`flex overflow-auto list-none py-2 w-full border-b border-[#52525B] mb-[1rem]`}
>
{props.files.map((file, index) => {
return (
<li
2023-12-18 10:23:15 +00:00
class={`relative mx-2 min-w-[10rem] w-[10rem] h-[10rem] p-2 bg-[${PrimaryBackgroundColor}] rounded ${CenterClass}`}
>
<button
2023-12-18 10:23:15 +00:00
class={`w-[2rem] h-[2rem] absolute top-1 right-1 rounded-[50%] hover:bg-[${DividerBackgroundColor}] ${CenterClass} ${NoOutlineClass}`}
onClick={() => {
removeFile(index);
}}
>
<RemoveIcon
2023-12-18 10:23:15 +00:00
class={`w-[1.3rem] h-[1.3rem]`}
style={{
fill: "none",
stroke: PrimaryTextColor,
}}
/>
</button>
<img
2023-12-18 10:23:15 +00:00
class={`max-w-full max-h-full`}
src={URL.createObjectURL(file)}
alt=""
/>
</li>
);
})}
</ul>
)
: undefined}
2023-06-30 14:05:57 +00:00
<textarea
ref={this.textareaElement}
2023-07-03 13:42:11 +00:00
style={{
maxHeight: props.maxHeight,
2023-07-03 13:42:11 +00:00
}}
value={props.text}
rows={1}
2023-12-18 10:23:15 +00:00
class={`flex-1 bg-transparent focus-visible:outline-none placeholder-[${PrimaryTextColor}4D] text-[0.8rem] text-[#D2D3D5] whitespace-nowrap resize-none overflow-x-hidden overflow-y-auto`}
placeholder={props.placeholder}
onInput={(e) => {
props.emit({
type: "UpdateEditorText",
pubkey: props.targetNpub,
text: e.currentTarget.value,
isGroupChat: props.isGroupChat,
});
const lines = e.currentTarget.value.split("\n");
e.currentTarget.setAttribute(
"rows",
`${lines.length}`,
);
}}
onKeyDown={async (e) => {
// https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/metaKey
if (e.code === "Enter" && (e.ctrlKey || e.metaKey)) {
await sendMessage();
}
}}
onPaste={async (_) => {
let clipboardData: ClipboardItems = [];
try {
clipboardData = await window.navigator.clipboard.read();
} catch (e) {
2023-10-21 11:29:47 +00:00
console.error(e.message);
return;
}
for (const item of clipboardData) {
try {
const image = await item.getType(
"image/png",
);
2023-10-21 11:29:47 +00:00
props.emit({
type: "UpdateMessageFiles",
isGroupChat: props.isGroupChat,
pubkey: props.targetNpub,
files: props.files.concat([image]),
});
} catch (e) {
console.error(e);
}
}
}}
>
</textarea>
</div>
2023-11-13 06:20:13 +00:00
<div
2023-12-18 10:23:15 +00:00
class={`w-[5rem] h-[2.5rem] rounded-lg mobile:hidden ${LinearGradientsClass} ${CenterClass}`}
2023-11-13 06:20:13 +00:00
>
<button
2023-12-18 10:23:15 +00:00
class={`w-[4.8rem] h-[2.3rem] text-[${PrimaryTextColor}] rounded-lg ${CenterClass} bg-[#36393F] hover:bg-transparent font-bold`}
onClick={async () => {
await sendMessage();
this.textareaElement.current?.focus();
}}
>
<SendIcon
2023-12-18 10:23:15 +00:00
class={`h-[1.25rem] w-[1.25rem] mr-[0.1rem]`}
style={{
stroke: PrimaryTextColor,
fill: "none",
}}
/>
Send
</button>
</div>
2023-11-15 11:35:02 +00:00
<button
2023-12-18 10:23:15 +00:00
class={`desktop:hidden w-12 h-8 ${CenterClass} ${LinearGradientsClass} rounded`}
2023-11-15 11:35:02 +00:00
onClick={async () => {
await sendMessage();
this.textareaElement.current?.focus();
2023-11-15 11:35:02 +00:00
}}
>
<SendIcon
2023-12-18 10:23:15 +00:00
class={`h-4 w-4`}
2023-11-15 11:35:02 +00:00
style={{
stroke: PrimaryTextColor,
fill: "none",
}}
/>
</button>
2023-07-03 13:42:11 +00:00
</div>
);
}
}