Compare commits

..

2 commits

Author SHA1 Message Date
thislight
143ecf6278
ReplyEditor: supports reply 2024-09-28 14:39:20 +08:00
thislight
2c0978b572
ReplyEditor: only show toolbar when active 2024-09-28 12:43:12 +08:00
4 changed files with 163 additions and 46 deletions

View file

@ -2,7 +2,21 @@
//! It recommended to include the module by <script> tag. //! It recommended to include the module by <script> tag.
if (!document.body.animate) { if (!document.body.animate) {
// @ts-ignore: this file is polyfill, no exposed decls // @ts-ignore: this file is polyfill, no exposed decls
import("web-animations-js").then(() => { import("web-animations-js").then(() => { // all target platforms supported, prepared to remove
console.warn("web animation polyfill is included"); console.warn("web animation polyfill is included");
}); });
} }
if (!window.crypto.randomUUID) { // Chrome/Edge 92+
// https://stackoverflow.com/a/2117523/2800218
// LICENSE: https://creativecommons.org/licenses/by-sa/4.0/legalcode
window.crypto.randomUUID =
function randomUUID(): `${string}-${string}-${string}-${string}-${string}` {
return "10000000-1000-4000-8000-100000000000".replace(/[018]/g, (c) =>
(
+c ^
(crypto.getRandomValues(new Uint8Array(1))[0] & (15 >> (+c / 4)))
).toString(16),
) as `${string}-${string}-${string}-${string}-${string}`;
};
}

View file

@ -64,7 +64,7 @@ const MediaAttachmentGrid: Component<{
> >
<For each={props.attachments}> <For each={props.attachments}>
{(item, index) => { {(item, index) => {
const [loaded, setLoaded] = createSignal(false) const [loaded, setLoaded] = createSignal(false);
const width = item.meta?.small?.width; const width = item.meta?.small?.width;
const height = item.meta?.small?.height; const height = item.meta?.small?.height;
const aspectRatio = item.meta?.small?.aspect; const aspectRatio = item.meta?.small?.aspect;
@ -74,10 +74,13 @@ const MediaAttachmentGrid: Component<{
width && height && height > maxHeight width && height && height > maxHeight
? maxHeight / (aspectRatio ?? 1) ? maxHeight / (aspectRatio ?? 1)
: width; : width;
const style = () => loaded() ? undefined : { const style = () =>
width: realWidth ? `${realWidth}px` : undefined, loaded()
height: realHeight ? `${realHeight}px` : undefined, ? undefined
}; : {
width: realWidth ? `${realWidth}px` : undefined,
height: realHeight ? `${realHeight}px` : undefined,
};
switch (item.type) { switch (item.type) {
case "image": case "image":
return ( return (
@ -100,7 +103,17 @@ const MediaAttachmentGrid: Component<{
controls controls
/> />
); );
case "gifv": case "gifv": // Later we can handle the preview
return (
<video
src={item.url || undefined}
style={style()}
onLoadedMetadata={[setLoaded, true]}
autoplay={true}
controls
/>
);
case "audio": case "audio":
case "unknown": case "unknown":
return <div></div>; return <div></div>;

View file

@ -1,6 +1,7 @@
import { import {
createEffect, createEffect,
createSignal, createSignal,
createUniqueId,
onMount, onMount,
Show, Show,
type Component, type Component,
@ -18,6 +19,7 @@ import {
Radio, Radio,
Switch, Switch,
Divider, Divider,
CircularProgress,
} from "@suid/material"; } from "@suid/material";
import { import {
ArrowDropDown, ArrowDropDown,
@ -36,6 +38,7 @@ import BottomSheet from "../material/BottomSheet";
import { useLanguage } from "../platform/i18n"; import { useLanguage } from "../platform/i18n";
import iso639_1 from "iso-639-1"; import iso639_1 from "iso-639-1";
import ChooseTootLang from "./ChooseTootLang"; import ChooseTootLang from "./ChooseTootLang";
import type { mastodon } from "masto";
type TootVisibility = "public" | "unlisted" | "private" | "direct"; type TootVisibility = "public" | "unlisted" | "private" | "direct";
@ -177,13 +180,19 @@ const TootLanguagePickerDialog: Component<{
const ReplyEditor: Component<{ const ReplyEditor: Component<{
profile: Account; profile: Account;
replyToDisplayName: string; replyToDisplayName: string;
mentions?: readonly string[];
isTyping?: boolean; isTyping?: boolean;
onTypingChange: (value: boolean) => void; onTypingChange: (value: boolean) => void;
client?: mastodon.rest.Client;
inReplyToId?: string;
onSent?: (status: mastodon.v1.Status) => void;
}> = (props) => { }> = (props) => {
let inputRef: HTMLTextAreaElement; let inputRef: HTMLTextAreaElement;
let sendKey: string | undefined;
const typing = () => props.isTyping; const typing = () => props.isTyping;
const setTyping = (v: boolean) => props.onTypingChange(v); const setTyping = (v: boolean) => props.onTypingChange(v);
const [sending, setSending] = createSignal(false);
const [visibility, setVisibility] = createSignal<TootVisibility>("public"); const [visibility, setVisibility] = createSignal<TootVisibility>("public");
const [permPicker, setPermPicker] = createSignal(false); const [permPicker, setPermPicker] = createSignal(false);
const [language, setLanguage] = createSignal("en"); const [language, setLanguage] = createSignal("en");
@ -199,6 +208,14 @@ const ReplyEditor: Component<{
makeEventListener(inputRef, "focus", () => setTyping(true)); makeEventListener(inputRef, "focus", () => setTyping(true));
}); });
createEffect(() => {
if (inputRef.value !== "") return;
if (props.mentions) {
const prepText = props.mentions.join(" ") + " ";
inputRef.value = prepText;
}
});
const containerStyle = () => const containerStyle = () =>
typing() || permPicker() typing() || permPicker()
? { ? {
@ -222,6 +239,39 @@ const ReplyEditor: Component<{
} }
}; };
const getOrGenSendKey = () => {
if (sendKey === undefined) {
sendKey = window.crypto.randomUUID();
}
return sendKey;
};
const send = async () => {
setSending(true);
try {
const status = await props.client!.v1.statuses.create(
{
status: inputRef.value,
language: language(),
visibility: visibility(),
inReplyToId: props.inReplyToId,
},
{
requestInit: {
headers: {
["Idempotency-Key"]: getOrGenSendKey(),
},
},
},
);
props.onSent?.(status);
inputRef.value = "";
} finally {
setSending(false);
}
};
return ( return (
<div <div
class={tootComposers.composer} class={tootComposers.composer}
@ -229,51 +279,74 @@ const ReplyEditor: Component<{
onClick={(e) => inputRef.focus()} onClick={(e) => inputRef.focus()}
> >
<div class={tootComposers.replyInput}> <div class={tootComposers.replyInput}>
<Avatar src={props.profile.inf?.avatar} sx={{marginLeft: '-0.25em'}} /> <Avatar
src={props.profile.inf?.avatar}
sx={{ marginLeft: "-0.25em" }}
/>
<textarea <textarea
ref={inputRef!} ref={inputRef!}
placeholder={`Reply to ${props.replyToDisplayName}...`} placeholder={`Reply to ${props.replyToDisplayName}...`}
style={{ width: "100%", border: "none" }} style={{ width: "100%", border: "none" }}
disabled={sending()}
></textarea> ></textarea>
<IconButton sx={{marginRight: '-0.5em'}}> <Show when={props.client}>
<Send /> <Show
</IconButton> when={!sending()}
fallback={
<div style={{ padding: "8px" }}>
<CircularProgress
sx={{
marginRight: "-0.5em",
width: "1.5rem",
height: "1.5rem",
}}
/>
</div>
}
>
<IconButton sx={{ marginRight: "-0.5em" }} onClick={send}>
<Send />
</IconButton>
</Show>
</Show>
</div> </div>
<div <Show when={typing()}>
style={{ <div
display: "flex", style={{
"justify-content": "flex-end", display: "flex",
"margin-top": "8px", "justify-content": "flex-end",
gap: "16px", "margin-top": "8px",
"flex-flow": "row wrap", gap: "16px",
}} "flex-flow": "row wrap",
> }}
<Button onClick={[setLangPickerOpen, true]}> >
<Translate sx={{ marginTop: "-0.25em", marginRight: "0.25em" }} /> <Button onClick={[setLangPickerOpen, true]} disabled={sending()}>
{iso639_1.getNativeName(language())} <Translate sx={{ marginTop: "-0.25em", marginRight: "0.25em" }} />
<ArrowDropDown sx={{ marginTop: "-0.25em" }} /> {iso639_1.getNativeName(language())}
</Button> <ArrowDropDown sx={{ marginTop: "-0.25em" }} />
<Button onClick={[setPermPicker, true]}> </Button>
<Visibility sx={{ marginTop: "-0.15em", marginRight: "0.25em" }} /> <Button onClick={[setPermPicker, true]} disabled={sending()}>
{visibilityText()} <Visibility sx={{ marginTop: "-0.15em", marginRight: "0.25em" }} />
<ArrowDropDown sx={{ marginTop: "-0.25em" }} /> {visibilityText()}
</Button> <ArrowDropDown sx={{ marginTop: "-0.25em" }} />
</div> </Button>
</div>
<TootVisibilityPickerDialog <TootVisibilityPickerDialog
open={permPicker()} open={permPicker()}
onClose={() => setPermPicker(false)} onClose={() => setPermPicker(false)}
visibility={visibility()} visibility={visibility()}
onVisibilityChange={setVisibility} onVisibilityChange={setVisibility}
/> />
<TootLanguagePickerDialog <TootLanguagePickerDialog
open={langPickerOpen()} open={langPickerOpen()}
onClose={() => setLangPickerOpen(false)} onClose={() => setLangPickerOpen(false)}
code={language()} code={language()}
onCodeChange={setLanguage} onCodeChange={setLanguage}
/> />
</Show>
</div> </div>
); );
}; };

View file

@ -84,7 +84,7 @@ const TootBottomSheet: Component = (props) => {
return tootId; return tootId;
}); });
const [tootContext] = createResource( const [tootContext, {refetch: refetchContext}] = createResource(
() => [session().client, params.id] as const, () => [session().client, params.id] as const,
async ([client, id]) => { async ([client, id]) => {
return await client.v1.statuses.$select(id).context.fetch(); return await client.v1.statuses.$select(id).context.fetch();
@ -171,6 +171,18 @@ const TootBottomSheet: Component = (props) => {
}); });
}; };
const defaultMentions = () => {
const tootAcct = remoteToot()?.reblog?.account ?? remoteToot()?.account
if (!tootAcct) {
return
}
const others = ancestors().map(x => x.account)
const values = [tootAcct, ...others].map(x => `@${x.acct}`)
return Array.from(new Set(values).keys())
}
css` css`
.name :global(img) { .name :global(img) {
max-height: 1em; max-height: 1em;
@ -237,12 +249,16 @@ const TootBottomSheet: Component = (props) => {
</Show> </Show>
</article> </article>
<Show when={profile()}> <Show when={session()!.account}>
<ReplyEditor <ReplyEditor
isTyping={isInTyping()} isTyping={isInTyping()}
onTypingChange={setInTyping} onTypingChange={setInTyping}
profile={profile()!} mentions={defaultMentions()}
profile={session().account!}
replyToDisplayName={toot()?.account?.displayName || ""} replyToDisplayName={toot()?.account?.displayName || ""}
client={session().client}
onSent={() => refetchContext()}
inReplyToId={remoteToot()?.reblog?.id ?? remoteToot()?.id}
/> />
</Show> </Show>
@ -270,6 +286,7 @@ const TootBottomSheet: Component = (props) => {
)} )}
</For> </For>
</TimeSourceProvider> </TimeSourceProvider>
<div style={{height: "var(--safe-area-inset-bottom, 0)"}}></div>
</Scaffold> </Scaffold>
); );
}; };