审查视图

app/components/writing/writing.tsx 8.3 KB
202304001 authored
1 2
import chatStyles from "@/app/components/chat.module.scss";
import homeStyles from "@/app/components/home.module.scss";
202304001 authored
3
import styles from "@/app/components/writing/writing.module.scss";
202304001 authored
4 5 6 7
import clsx from "clsx";

import { WriteSiderBar } from "./write-siderBar";
import { WindowContent } from "@/app/components/home";
202304001 authored
8
import { useMobileScreen } from "@/app/utils";
202304001 authored
9 10 11 12 13
import { IconButton } from "../button";
import Locale from "@/app/locales";
import { Path } from "@/app/constant";
import { useNavigate } from "react-router-dom";
import { getClientConfig } from "@/app/config/client";
202304001 authored
14
import React, { useMemo, useRef, useState } from "react";
202304001 authored
15 16 17 18 19
import { useAppConfig } from "@/app/store";
import { ChatAction } from "../chat";
import { useWindowSize } from "@/app/utils";
import { exportWord } from "@/app/utils/excelAndWordUtils/word";
import { HTMLPreview } from "../artifacts";
202304001 authored
20 21
import ReactQuill from "react-quill";
import "react-quill/dist/quill.snow.css";
202304001 authored
22 23 24 25 26 27 28 29 30 31 32 33

import ReturnIcon from "@/app/icons/return.svg";
import MinIcon from "@/app/icons/min.svg";
import MaxIcon from "@/app/icons/max.svg";
import SDIcon from "@/app/icons/sd.svg";
import LoadingIcon from "@/app/icons/loading.svg";
import BotIcon from "@/app/icons/bot.svg";
import EditIcon from "@/app/icons/rename.svg";
import ReloadIcon from "@/app/icons/reload.svg";
import CopyIcon from "@/app/icons/copy.svg";
import ExcelIcon from "@/app/icons/excel.svg";
import WordIcon from "@/app/icons/word.svg";
202304001 authored
34
import MindIcon from "@/app/icons/mind.svg";
202304001 authored
35 36 37 38 39
import PptIcon from "@/app/icons/ppt.svg";
import PdfIcon from "@/app/icons/pdf.svg";
import { message } from "antd";

export function WritingPage() {
202304001 authored
40 41 42 43 44 45 46 47 48 49 50 51
  const isMobileScreen = useMobileScreen();
  const navigate = useNavigate();
  const clientConfig = useMemo(() => getClientConfig(), []);
  const showMaxIcon = !isMobileScreen && !clientConfig?.isApp;
  const config = useAppConfig();
  const scrollRef = useRef<HTMLDivElement>(null);
  const isWriting = location.pathname === Path.Writing;
  const { height } = useWindowSize();
  const [isEdit, setIsEdit] = useState(false);
  const [loading, setLoading] = useState(false);
  const quillRef = useRef<ReactQuill | null>(null);
  const [htmlCode, setHtmlCode] = useState("");
202304001 authored
52
202304001 authored
53 54 55 56 57 58 59 60 61
  //编辑器
  const toolbarOptions = [
    [{ font: [] }, { size: ["small", false, "large", "huge"] }],
    [{ header: [1, 2, 3, 4, 5, 6, false] }],
    ["bold", "italic", "underline", "strike"],
    [{ list: "ordered" }, { list: "bullet" }, { align: [] }],
    [{ color: [] }, { background: [] }],
    ["link", "image"],
  ];
202304001 authored
62
202304001 authored
63 64 65 66 67 68
  const copyToClipboard = () => {
    // 检查quillRef.current是否为null
    if (quillRef.current) {
      const editor = quillRef.current.getEditor(); // 获取编辑器实例
      const range = editor.getSelection(); // 获取当前选择的范围
      const text = editor.getText(); // 获取编辑器中的文本内容
202304001 authored
69
202304001 authored
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
      if (range && range.length > 0) {
        // 如果有选中的文本,复制选中的内容
        const selectedText = text.substring(
          range.index,
          range.index + range.length,
        );
        navigator.clipboard
          .writeText(selectedText)
          .then(() => {
            message.success("复制成功");
          })
          .catch((err) => {
            message.error("复制失败:");
          });
      } else {
        // 如果没有选择文本,就复制全部内容
        navigator.clipboard
          .writeText(text)
          .then(() => {
            message.success("复制成功");
          })
          .catch((err) => {
            message.error("复制失败:");
          });
      }
    } else {
      console.error("Quill编辑器尚未初始化");
    }
  };
202304001 authored
99
202304001 authored
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
  return (
    <>
      <WriteSiderBar
        className={clsx({ [homeStyles["sidebar-show"]]: isWriting })}
        htmlCode={htmlCode}
        setHtmlCode={setHtmlCode}
        loading={loading}
        setLoading={setLoading}
      />
      <WindowContent>
        <div className={chatStyles.chat} key={"1"}>
          <div className="window-header" data-tauri-drag-region>
            {isMobileScreen && (
              <div className="window-actions">
                <div className={"window-action-button"}>
                  <IconButton
                    icon={<ReturnIcon />}
                    bordered
                    title={Locale.Chat.Actions.ChatList}
                    onClick={() => navigate(Path.BgRemoval)}
                  />
                </div>
              </div>
            )}
            <div
              className={clsx(
                "window-header-title",
                chatStyles["chat-body-title"],
              )}
            >
              <div className={`window-header-main-title`}>AI-Writing</div>
            </div>
            <div className={chatStyles["chat-message-actions"]}>
              <div className={chatStyles["chat-input-actions"]}>
                <ChatAction
                  text={Locale.Chat.Actions.ReWrite}
                  icon={<ReloadIcon />}
                  onClick={() => {}}
                />
                <ChatAction
                  text={Locale.Chat.Actions.Copy}
                  icon={<CopyIcon />}
                  onClick={copyToClipboard}
                />
                {!isEdit ? (
                  <ChatAction
                    text={Locale.Chat.Actions.Edit}
                    icon={<EditIcon />}
                    onClick={() => {
                      setIsEdit(true);
                    }}
                  />
                ) : (
                  <ChatAction
                    text="取消编辑"
                    icon={<EditIcon />}
                    onClick={() => {
                      setIsEdit(false);
                    }}
                  />
                )}
202304001 authored
161
202304001 authored
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
                <ChatAction
                  text={Locale.Chat.Actions.Pdf}
                  icon={<PdfIcon />}
                  onClick={() => {}}
                />
                {htmlCode && (
                  <ChatAction
                    text={Locale.Chat.Actions.Word}
                    icon={<WordIcon />}
                    onClick={() => {
                      exportWord(htmlCode);
                    }}
                  />
                )}
                <ChatAction
                  text={Locale.Chat.Actions.Excel}
                  icon={<ExcelIcon />}
                  onClick={() => {}}
                />
                <ChatAction
                  text={Locale.Chat.Actions.Ppt}
                  icon={<PptIcon />}
                  onClick={() => {}}
                />
                <ChatAction
                  text={Locale.Chat.Actions.Mind}
                  icon={<MindIcon />}
                  onClick={() => {}}
                />
              </div>
            </div>
            <div className="window-actions">
              {showMaxIcon && (
                <div className="window-action-button">
                  <IconButton
                    aria={Locale.Chat.Actions.FullScreen}
                    icon={config.tightBorder ? <MinIcon /> : <MaxIcon />}
                    bordered
                    onClick={() => {
                      config.update(
                        (config) => (config.tightBorder = !config.tightBorder),
                      );
                    }}
                  />
202304001 authored
206
                </div>
202304001 authored
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
              )}
              {isMobileScreen && <SDIcon width={50} height={50} />}
            </div>
          </div>
          <div className={chatStyles["chat-body"]} ref={scrollRef}>
            {loading ? (
              <div className={clsx("no-dark", styles["loading-content"])}>
                <BotIcon />
                <LoadingIcon />
              </div>
            ) : (
              htmlCode &&
              (isEdit ? (
                <ReactQuill
                  ref={quillRef}
                  theme="snow"
                  value={htmlCode}
                  onChange={setHtmlCode}
                  modules={{
                    toolbar: toolbarOptions,
                  }}
                />
              ) : (
                <HTMLPreview
                  code={htmlCode}
                  autoHeight={!document.fullscreenElement}
                  height={!document.fullscreenElement ? 600 : height}
                />
              ))
            )}
          </div>
        </div>
      </WindowContent>
    </>
  );
}