forked from XiaoMo/ChatGPT-Next-Web
Merge pull request #2269 from Yidadaa/bugfix-0705
feat: improve image preview & model switcher model ux
This commit is contained in:
commit
81771568be
@ -61,7 +61,14 @@ import Locale from "../locales";
|
|||||||
import { IconButton } from "./button";
|
import { IconButton } from "./button";
|
||||||
import styles from "./chat.module.scss";
|
import styles from "./chat.module.scss";
|
||||||
|
|
||||||
import { ListItem, Modal, showConfirm, showPrompt, showToast } from "./ui-lib";
|
import {
|
||||||
|
ListItem,
|
||||||
|
Modal,
|
||||||
|
Selector,
|
||||||
|
showConfirm,
|
||||||
|
showPrompt,
|
||||||
|
showToast,
|
||||||
|
} from "./ui-lib";
|
||||||
import { useLocation, useNavigate } from "react-router-dom";
|
import { useLocation, useNavigate } from "react-router-dom";
|
||||||
import { LAST_INPUT_KEY, Path, REQUEST_TIMEOUT_MS } from "../constant";
|
import { LAST_INPUT_KEY, Path, REQUEST_TIMEOUT_MS } from "../constant";
|
||||||
import { Avatar } from "./emoji";
|
import { Avatar } from "./emoji";
|
||||||
@ -404,16 +411,11 @@ export function ChatActions(props: {
|
|||||||
|
|
||||||
// switch model
|
// switch model
|
||||||
const currentModel = chatStore.currentSession().mask.modelConfig.model;
|
const currentModel = chatStore.currentSession().mask.modelConfig.model;
|
||||||
function nextModel() {
|
const models = useMemo(
|
||||||
const models = config.models.filter((m) => m.available).map((m) => m.name);
|
() => config.models.filter((m) => m.available).map((m) => m.name),
|
||||||
const modelIndex = models.indexOf(currentModel);
|
[config.models],
|
||||||
const nextIndex = (modelIndex + 1) % models.length;
|
);
|
||||||
const nextModel = models[nextIndex];
|
const [showModelSelector, setShowModelSelector] = useState(false);
|
||||||
chatStore.updateCurrentSession((session) => {
|
|
||||||
session.mask.modelConfig.model = nextModel as ModelType;
|
|
||||||
session.mask.syncGlobalConfig = false;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className={styles["chat-input-actions"]}>
|
<div className={styles["chat-input-actions"]}>
|
||||||
@ -485,10 +487,28 @@ export function ChatActions(props: {
|
|||||||
/>
|
/>
|
||||||
|
|
||||||
<ChatAction
|
<ChatAction
|
||||||
onClick={nextModel}
|
onClick={() => setShowModelSelector(true)}
|
||||||
text={currentModel}
|
text={currentModel}
|
||||||
icon={<RobotIcon />}
|
icon={<RobotIcon />}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
|
{showModelSelector && (
|
||||||
|
<Selector
|
||||||
|
items={models.map((m) => ({
|
||||||
|
title: m,
|
||||||
|
value: m,
|
||||||
|
}))}
|
||||||
|
onClose={() => setShowModelSelector(false)}
|
||||||
|
onSelection={(s) => {
|
||||||
|
if (s.length === 0) return;
|
||||||
|
chatStore.updateCurrentSession((session) => {
|
||||||
|
session.mask.modelConfig.model = s[0] as ModelType;
|
||||||
|
session.mask.syncGlobalConfig = false;
|
||||||
|
});
|
||||||
|
showToast(s[0]);
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -186,7 +186,7 @@
|
|||||||
box-shadow: var(--card-shadow);
|
box-shadow: var(--card-shadow);
|
||||||
border: var(--border-in-light);
|
border: var(--border-in-light);
|
||||||
|
|
||||||
* {
|
*:not(li) {
|
||||||
overflow: hidden;
|
overflow: hidden;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,16 @@
|
|||||||
|
/* eslint-disable @next/next/no-img-element */
|
||||||
import { ChatMessage, useAppConfig, useChatStore } from "../store";
|
import { ChatMessage, useAppConfig, useChatStore } from "../store";
|
||||||
import Locale from "../locales";
|
import Locale from "../locales";
|
||||||
import styles from "./exporter.module.scss";
|
import styles from "./exporter.module.scss";
|
||||||
import { List, ListItem, Modal, Select, showToast } from "./ui-lib";
|
import {
|
||||||
|
List,
|
||||||
|
ListItem,
|
||||||
|
Modal,
|
||||||
|
Select,
|
||||||
|
showImageModal,
|
||||||
|
showModal,
|
||||||
|
showToast,
|
||||||
|
} from "./ui-lib";
|
||||||
import { IconButton } from "./button";
|
import { IconButton } from "./button";
|
||||||
import { copyToClipboard, downloadAs, useMobileScreen } from "../utils";
|
import { copyToClipboard, downloadAs, useMobileScreen } from "../utils";
|
||||||
|
|
||||||
@ -23,6 +32,7 @@ import { DEFAULT_MASK_AVATAR } from "../store/mask";
|
|||||||
import { api } from "../client/api";
|
import { api } from "../client/api";
|
||||||
import { prettyObject } from "../utils/format";
|
import { prettyObject } from "../utils/format";
|
||||||
import { EXPORT_MESSAGE_CLASS_NAME } from "../constant";
|
import { EXPORT_MESSAGE_CLASS_NAME } from "../constant";
|
||||||
|
import { getClientConfig } from "../config/client";
|
||||||
|
|
||||||
const Markdown = dynamic(async () => (await import("./markdown")).Markdown, {
|
const Markdown = dynamic(async () => (await import("./markdown")).Markdown, {
|
||||||
loading: () => <LoadingIcon />,
|
loading: () => <LoadingIcon />,
|
||||||
@ -369,6 +379,7 @@ export function ImagePreviewer(props: {
|
|||||||
const previewRef = useRef<HTMLDivElement>(null);
|
const previewRef = useRef<HTMLDivElement>(null);
|
||||||
|
|
||||||
const copy = () => {
|
const copy = () => {
|
||||||
|
showToast(Locale.Export.Image.Toast);
|
||||||
const dom = previewRef.current;
|
const dom = previewRef.current;
|
||||||
if (!dom) return;
|
if (!dom) return;
|
||||||
toBlob(dom).then((blob) => {
|
toBlob(dom).then((blob) => {
|
||||||
@ -393,17 +404,15 @@ export function ImagePreviewer(props: {
|
|||||||
const isMobile = useMobileScreen();
|
const isMobile = useMobileScreen();
|
||||||
|
|
||||||
const download = () => {
|
const download = () => {
|
||||||
|
showToast(Locale.Export.Image.Toast);
|
||||||
const dom = previewRef.current;
|
const dom = previewRef.current;
|
||||||
if (!dom) return;
|
if (!dom) return;
|
||||||
toPng(dom)
|
toPng(dom)
|
||||||
.then((blob) => {
|
.then((blob) => {
|
||||||
if (!blob) return;
|
if (!blob) return;
|
||||||
|
|
||||||
if (isMobile) {
|
if (isMobile || getClientConfig()?.isApp) {
|
||||||
const image = new Image();
|
showImageModal(blob);
|
||||||
image.src = blob;
|
|
||||||
const win = window.open("");
|
|
||||||
win?.document.write(image.outerHTML);
|
|
||||||
} else {
|
} else {
|
||||||
const link = document.createElement("a");
|
const link = document.createElement("a");
|
||||||
link.download = `${props.topic}.png`;
|
link.download = `${props.topic}.png`;
|
||||||
|
@ -12,6 +12,7 @@ import mermaid from "mermaid";
|
|||||||
import LoadingIcon from "../icons/three-dots.svg";
|
import LoadingIcon from "../icons/three-dots.svg";
|
||||||
import React from "react";
|
import React from "react";
|
||||||
import { useDebouncedCallback, useThrottledCallback } from "use-debounce";
|
import { useDebouncedCallback, useThrottledCallback } from "use-debounce";
|
||||||
|
import { showImageModal } from "./ui-lib";
|
||||||
|
|
||||||
export function Mermaid(props: { code: string }) {
|
export function Mermaid(props: { code: string }) {
|
||||||
const ref = useRef<HTMLDivElement>(null);
|
const ref = useRef<HTMLDivElement>(null);
|
||||||
@ -37,11 +38,13 @@ export function Mermaid(props: { code: string }) {
|
|||||||
if (!svg) return;
|
if (!svg) return;
|
||||||
const text = new XMLSerializer().serializeToString(svg);
|
const text = new XMLSerializer().serializeToString(svg);
|
||||||
const blob = new Blob([text], { type: "image/svg+xml" });
|
const blob = new Blob([text], { type: "image/svg+xml" });
|
||||||
const url = URL.createObjectURL(blob);
|
console.log(blob);
|
||||||
const win = window.open(url);
|
// const url = URL.createObjectURL(blob);
|
||||||
if (win) {
|
// const win = window.open(url);
|
||||||
win.onload = () => URL.revokeObjectURL(url);
|
// if (win) {
|
||||||
}
|
// win.onload = () => URL.revokeObjectURL(url);
|
||||||
|
// }
|
||||||
|
showImageModal(URL.createObjectURL(blob));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (hasError) {
|
if (hasError) {
|
||||||
|
@ -62,6 +62,7 @@
|
|||||||
box-shadow: var(--card-shadow);
|
box-shadow: var(--card-shadow);
|
||||||
margin-bottom: 20px;
|
margin-bottom: 20px;
|
||||||
animation: slide-in ease 0.3s;
|
animation: slide-in ease 0.3s;
|
||||||
|
background: var(--white);
|
||||||
}
|
}
|
||||||
|
|
||||||
.list .list-item:last-child {
|
.list .list-item:last-child {
|
||||||
@ -79,6 +80,19 @@
|
|||||||
|
|
||||||
--modal-padding: 20px;
|
--modal-padding: 20px;
|
||||||
|
|
||||||
|
&-max {
|
||||||
|
width: 95vw;
|
||||||
|
max-width: unset;
|
||||||
|
height: 95vh;
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
|
||||||
|
.modal-content {
|
||||||
|
max-height: unset !important;
|
||||||
|
flex-grow: 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
.modal-header {
|
.modal-header {
|
||||||
padding: var(--modal-padding);
|
padding: var(--modal-padding);
|
||||||
display: flex;
|
display: flex;
|
||||||
@ -91,11 +105,19 @@
|
|||||||
font-size: 16px;
|
font-size: 16px;
|
||||||
}
|
}
|
||||||
|
|
||||||
.modal-close-btn {
|
.modal-header-actions {
|
||||||
cursor: pointer;
|
display: flex;
|
||||||
|
|
||||||
&:hover {
|
.modal-header-action {
|
||||||
filter: brightness(1.2);
|
cursor: pointer;
|
||||||
|
|
||||||
|
&:not(:last-child) {
|
||||||
|
margin-right: 20px;
|
||||||
|
}
|
||||||
|
|
||||||
|
&:hover {
|
||||||
|
filter: brightness(1.2);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -249,3 +271,34 @@
|
|||||||
border: 1px solid var(--primary);
|
border: 1px solid var(--primary);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.selector {
|
||||||
|
position: fixed;
|
||||||
|
top: 0;
|
||||||
|
left: 0;
|
||||||
|
height: 100vh;
|
||||||
|
width: 100vw;
|
||||||
|
background-color: rgba(0, 0, 0, 0.5);
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
justify-content: center;
|
||||||
|
|
||||||
|
&-content {
|
||||||
|
.list {
|
||||||
|
overflow: hidden;
|
||||||
|
|
||||||
|
.list-item {
|
||||||
|
cursor: pointer;
|
||||||
|
background-color: var(--white);
|
||||||
|
|
||||||
|
&:hover {
|
||||||
|
filter: brightness(0.95);
|
||||||
|
}
|
||||||
|
|
||||||
|
&:active {
|
||||||
|
filter: brightness(0.9);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
/* eslint-disable @next/next/no-img-element */
|
||||||
import styles from "./ui-lib.module.scss";
|
import styles from "./ui-lib.module.scss";
|
||||||
import LoadingIcon from "../icons/three-dots.svg";
|
import LoadingIcon from "../icons/three-dots.svg";
|
||||||
import CloseIcon from "../icons/close.svg";
|
import CloseIcon from "../icons/close.svg";
|
||||||
@ -6,6 +7,8 @@ import EyeOffIcon from "../icons/eye-off.svg";
|
|||||||
import DownIcon from "../icons/down.svg";
|
import DownIcon from "../icons/down.svg";
|
||||||
import ConfirmIcon from "../icons/confirm.svg";
|
import ConfirmIcon from "../icons/confirm.svg";
|
||||||
import CancelIcon from "../icons/cancel.svg";
|
import CancelIcon from "../icons/cancel.svg";
|
||||||
|
import MaxIcon from "../icons/max.svg";
|
||||||
|
import MinIcon from "../icons/min.svg";
|
||||||
|
|
||||||
import Locale from "../locales";
|
import Locale from "../locales";
|
||||||
|
|
||||||
@ -44,9 +47,13 @@ export function ListItem(props: {
|
|||||||
children?: JSX.Element | JSX.Element[];
|
children?: JSX.Element | JSX.Element[];
|
||||||
icon?: JSX.Element;
|
icon?: JSX.Element;
|
||||||
className?: string;
|
className?: string;
|
||||||
|
onClick?: () => void;
|
||||||
}) {
|
}) {
|
||||||
return (
|
return (
|
||||||
<div className={styles["list-item"] + ` ${props.className || ""}`}>
|
<div
|
||||||
|
className={styles["list-item"] + ` ${props.className || ""}`}
|
||||||
|
onClick={props.onClick}
|
||||||
|
>
|
||||||
<div className={styles["list-header"]}>
|
<div className={styles["list-header"]}>
|
||||||
{props.icon && <div className={styles["list-icon"]}>{props.icon}</div>}
|
{props.icon && <div className={styles["list-icon"]}>{props.icon}</div>}
|
||||||
<div className={styles["list-item-title"]}>
|
<div className={styles["list-item-title"]}>
|
||||||
@ -93,6 +100,7 @@ interface ModalProps {
|
|||||||
title: string;
|
title: string;
|
||||||
children?: any;
|
children?: any;
|
||||||
actions?: JSX.Element[];
|
actions?: JSX.Element[];
|
||||||
|
defaultMax?: boolean;
|
||||||
onClose?: () => void;
|
onClose?: () => void;
|
||||||
}
|
}
|
||||||
export function Modal(props: ModalProps) {
|
export function Modal(props: ModalProps) {
|
||||||
@ -111,13 +119,30 @@ export function Modal(props: ModalProps) {
|
|||||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
|
const [isMax, setMax] = useState(!!props.defaultMax);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className={styles["modal-container"]}>
|
<div
|
||||||
|
className={
|
||||||
|
styles["modal-container"] + ` ${isMax && styles["modal-container-max"]}`
|
||||||
|
}
|
||||||
|
>
|
||||||
<div className={styles["modal-header"]}>
|
<div className={styles["modal-header"]}>
|
||||||
<div className={styles["modal-title"]}>{props.title}</div>
|
<div className={styles["modal-title"]}>{props.title}</div>
|
||||||
|
|
||||||
<div className={styles["modal-close-btn"]} onClick={props.onClose}>
|
<div className={styles["modal-header-actions"]}>
|
||||||
<CloseIcon />
|
<div
|
||||||
|
className={styles["modal-header-action"]}
|
||||||
|
onClick={() => setMax(!isMax)}
|
||||||
|
>
|
||||||
|
{isMax ? <MinIcon /> : <MaxIcon />}
|
||||||
|
</div>
|
||||||
|
<div
|
||||||
|
className={styles["modal-header-action"]}
|
||||||
|
onClick={props.onClose}
|
||||||
|
>
|
||||||
|
<CloseIcon />
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
@ -394,3 +419,54 @@ export function showPrompt(content: any, value = "", rows = 3) {
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function showImageModal(img: string) {
|
||||||
|
showModal({
|
||||||
|
title: Locale.Export.Image.Modal,
|
||||||
|
children: (
|
||||||
|
<div>
|
||||||
|
<img
|
||||||
|
src={img}
|
||||||
|
alt="preview"
|
||||||
|
style={{
|
||||||
|
maxWidth: "100%",
|
||||||
|
}}
|
||||||
|
></img>
|
||||||
|
</div>
|
||||||
|
),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
export function Selector<T>(props: {
|
||||||
|
items: Array<{
|
||||||
|
title: string;
|
||||||
|
subTitle?: string;
|
||||||
|
value: T;
|
||||||
|
}>;
|
||||||
|
onSelection?: (selection: T[]) => void;
|
||||||
|
onClose?: () => void;
|
||||||
|
multiple?: boolean;
|
||||||
|
}) {
|
||||||
|
return (
|
||||||
|
<div className={styles["selector"]} onClick={() => props.onClose?.()}>
|
||||||
|
<div className={styles["selector-content"]}>
|
||||||
|
<List>
|
||||||
|
{props.items.map((item, i) => {
|
||||||
|
return (
|
||||||
|
<ListItem
|
||||||
|
className={styles["selector-item"]}
|
||||||
|
key={i}
|
||||||
|
title={item.title}
|
||||||
|
subTitle={item.subTitle}
|
||||||
|
onClick={() => {
|
||||||
|
props.onSelection?.([item.value]);
|
||||||
|
props.onClose?.();
|
||||||
|
}}
|
||||||
|
></ListItem>
|
||||||
|
);
|
||||||
|
})}
|
||||||
|
</List>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
@ -86,6 +86,10 @@ const cn = {
|
|||||||
Select: "选取",
|
Select: "选取",
|
||||||
Preview: "预览",
|
Preview: "预览",
|
||||||
},
|
},
|
||||||
|
Image: {
|
||||||
|
Toast: "正在生成截图",
|
||||||
|
Modal: "长按或右键保存图片",
|
||||||
|
},
|
||||||
},
|
},
|
||||||
Select: {
|
Select: {
|
||||||
Search: "搜索消息",
|
Search: "搜索消息",
|
||||||
|
@ -87,6 +87,10 @@ const en: LocaleType = {
|
|||||||
Select: "Select",
|
Select: "Select",
|
||||||
Preview: "Preview",
|
Preview: "Preview",
|
||||||
},
|
},
|
||||||
|
Image: {
|
||||||
|
Toast: "Capturing Image...",
|
||||||
|
Modal: "Long press or right click to save image",
|
||||||
|
},
|
||||||
},
|
},
|
||||||
Select: {
|
Select: {
|
||||||
Search: "Search",
|
Search: "Search",
|
||||||
|
Loading…
Reference in New Issue
Block a user