Skip to content

Commit

Permalink
refactor: 数据库字段名称调整 (#523)
Browse files Browse the repository at this point in the history
  • Loading branch information
ayangweb authored and kohaiy committed Sep 20, 2024
1 parent 4d8da60 commit 86c4a03
Show file tree
Hide file tree
Showing 16 changed files with 97 additions and 78 deletions.
22 changes: 11 additions & 11 deletions src-tauri/src/plugins/clipboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ async fn has_html(manager: State<'_, ClipboardManager>) -> Result<bool, String>
}

#[command]
async fn has_rich_text(manager: State<'_, ClipboardManager>) -> Result<bool, String> {
async fn has_rtf(manager: State<'_, ClipboardManager>) -> Result<bool, String> {
Ok(manager.has(ContentFormat::Rtf))
}

Expand Down Expand Up @@ -220,7 +220,7 @@ async fn read_html(manager: State<'_, ClipboardManager>) -> Result<String, Strin
}

#[command]
async fn read_rich_text(manager: State<'_, ClipboardManager>) -> Result<String, String> {
async fn read_rtf(manager: State<'_, ClipboardManager>) -> Result<String, String> {
manager
.context
.lock()
Expand Down Expand Up @@ -283,15 +283,15 @@ async fn write_html(
}

#[command]
async fn write_rich_text(
async fn write_rtf(
manager: State<'_, ClipboardManager>,
text: String,
rich_text: String,
rtf: String,
) -> Result<(), String> {
let contents = vec![
ClipboardContent::Text(text),
ClipboardContent::Rtf(rich_text),
];
println!("text: {text}");
println!("rtf: {rtf}");

let contents = vec![ClipboardContent::Text(text), ClipboardContent::Rtf(rtf)];

manager
.context
Expand Down Expand Up @@ -324,17 +324,17 @@ pub fn init() -> TauriPlugin<Wry> {
has_files,
has_image,
has_html,
has_rich_text,
has_rtf,
has_text,
read_files,
read_image,
read_html,
read_rich_text,
read_rtf,
read_text,
write_files,
write_image,
write_html,
write_rich_text,
write_rtf,
write_text,
])
.build()
Expand Down
6 changes: 3 additions & 3 deletions src/constants/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,17 @@ export const CLIPBOARD_PLUGIN = {
HAS_FILES: "plugin:clipboard|has_files",
HAS_IMAGE: "plugin:clipboard|has_image",
HAS_HTML: "plugin:clipboard|has_html",
HAS_RICH_TEXT: "plugin:clipboard|has_rich_text",
HAS_RTF: "plugin:clipboard|has_rtf",
HAS_TEXT: "plugin:clipboard|has_text",
READ_FILES: "plugin:clipboard|read_files",
READ_IMAGE: "plugin:clipboard|read_image",
READ_HTML: "plugin:clipboard|read_html",
READ_RICH_TEXT: "plugin:clipboard|read_rich_text",
READ_RTF: "plugin:clipboard|read_rtf",
READ_TEXT: "plugin:clipboard|read_text",
WRITE_FILES: "plugin:clipboard|write_files",
WRITE_IMAGE: "plugin:clipboard|write_image",
WRITE_HTML: "plugin:clipboard|write_html",
WRITE_RICH_TEXT: "plugin:clipboard|write_rich_text",
WRITE_RTF: "plugin:clipboard|write_rtf",
WRITE_TEXT: "plugin:clipboard|write_text",
CLIPBOARD_UPDATE: "plugin:clipboard://clipboard_update",
};
Expand Down
26 changes: 21 additions & 5 deletions src/database/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type {
} from "@/types/database";
import { getName } from "@tauri-apps/api/app";
import { removeFile } from "@tauri-apps/api/fs";
import { isBoolean, isNil, map, omitBy } from "lodash-es";
import { isBoolean, isNil, map, omitBy, some } from "lodash-es";
import Database from "tauri-plugin-sql-api";

let db: Database | null;
Expand All @@ -29,14 +29,30 @@ export const initDatabase = async () => {
[group] TEXT,
value TEXT,
search TEXT,
size INTEGER,
width INTEGER,
height INTEGER,
size INTEGER,
createTime TIMESTAMP DEFAULT (DATETIME('now', 'localtime')),
isCollected INTEGER DEFAULT 0
favorite INTEGER DEFAULT 0,
createTime TIMESTAMP DEFAULT (DATETIME('now', 'localtime'))
);
`,
);

// 获取 history 表字段
const fields: any = await executeSQL("PRAGMA table_info(history)");

// `isCollected` 更名 `favorite`
if (some(fields, { name: "isCollected" })) {
await executeSQL(
"ALTER TABLE history RENAME COLUMN isCollected TO favorite;",
);
}

// 将 type 为富文本的简化为 rtf
await executeSQL("UPDATE history SET type = ? WHERE type = ?;", [
"rtf",
"rich-text",
]);
};

/**
Expand All @@ -48,7 +64,7 @@ export const executeSQL = async (query: string, values?: unknown[]) => {
await initDatabase();
}

if (query.startsWith("SELECT")) {
if (query.startsWith("SELECT") || query.startsWith("PRAGMA")) {
return await db!.select(query, values);
}

Expand Down
4 changes: 2 additions & 2 deletions src/locales/en-US.json
Original file line number Diff line number Diff line change
Expand Up @@ -204,13 +204,13 @@
"text": "Text",
"image": "Image",
"files": "File",
"collection": "Favorite"
"favorite": "Favorite"
},
"link": "Link",
"email": "Email",
"color": "Color",
"plain_text": "Plain Text",
"rich_text": "Rich Text",
"rtf": "Rich Text",
"html": "HTML",
"image": "Image",
"n_files": "{{0}} File(s)",
Expand Down
4 changes: 2 additions & 2 deletions src/locales/ja-JP.json
Original file line number Diff line number Diff line change
Expand Up @@ -204,13 +204,13 @@
"text": "テキスト",
"image": "画像",
"files": "ファイル",
"collection": "コレクション"
"favorite": "コレクション"
},
"link": "ウェブリンク",
"email": "メール",
"color": "カラー",
"plain_text": "プレーンテキスト",
"rich_text": "リッチテキスト",
"rtf": "リッチテキスト",
"html": "HTML",
"image": "画像",
"n_files": "{{0}} ファイル(フォルダ)",
Expand Down
4 changes: 2 additions & 2 deletions src/locales/zh-CN.json
Original file line number Diff line number Diff line change
Expand Up @@ -204,13 +204,13 @@
"text": "文本",
"image": "图片",
"files": "文件",
"collection": "收藏"
"favorite": "收藏"
},
"link": "链接",
"email": "邮箱",
"color": "颜色",
"plain_text": "纯文本",
"rich_text": "富文本",
"rtf": "富文本",
"html": "HTML",
"image": "图片",
"n_files": "{{0}}个文件(夹)",
Expand Down
4 changes: 2 additions & 2 deletions src/locales/zh-TW.json
Original file line number Diff line number Diff line change
Expand Up @@ -204,13 +204,13 @@
"text": "文字",
"image": "圖片",
"files": "文件",
"collection": "收藏"
"favorite": "收藏"
},
"link": "連結",
"email": "信箱",
"color": "顏色",
"plain_text": "純文字",
"rich_text": "富文字",
"rtf": "富文字",
"html": "HTML",
"image": "圖片",
"n_files": "{{0}}個檔案",
Expand Down
14 changes: 6 additions & 8 deletions src/pages/Clipboard/Panel/components/Group/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@ import { Flex, Tag } from "antd";
import clsx from "clsx";
import { ClipboardPanelContext } from "../..";

interface GroupItem {
interface GroupItem extends Partial<ClipboardItem> {
key: string;
label: string;
group?: ClipboardItem["group"];
isCollected?: boolean;
}

const Group = () => {
Expand Down Expand Up @@ -36,20 +34,20 @@ const Group = () => {
group: "files",
},
{
key: "collect",
label: t("clipboard.label.tab.collection"),
isCollected: true,
key: "favorite",
label: t("clipboard.label.tab.favorite"),
favorite: true,
},
];

const [checked, setChecked] = useState(groupList[0].key);

const handleChange = (item: GroupItem) => {
const { key, group, isCollected } = item;
const { key, group, favorite } = item;

setChecked(key);

Object.assign(state, { group, isCollected });
Object.assign(state, { group, favorite });
};

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import type { FC } from "react";

interface HeaderProps extends ClipboardItem {
copy: () => void;
collect: () => void;
toggleFavorite: () => void;
deleteItem: () => void;
}

Expand All @@ -18,9 +18,9 @@ const Header: FC<HeaderProps> = (props) => {
value,
size,
createTime,
isCollected,
favorite,
copy,
collect,
toggleFavorite,
deleteItem,
} = props;

Expand Down Expand Up @@ -50,8 +50,8 @@ const Header: FC<HeaderProps> = (props) => {
}

return t("clipboard.label.plain_text");
case "rich-text":
return t("clipboard.label.rich_text");
case "rtf":
return t("clipboard.label.rtf");
case "html":
return t("clipboard.label.html");
case "image":
Expand Down Expand Up @@ -120,9 +120,9 @@ const Header: FC<HeaderProps> = (props) => {

<Icon
hoverable
name={isCollected ? "i-iconamoon:star-fill" : "i-iconamoon:star"}
className={clsx({ "text-gold!": isCollected })}
onMouseDown={collect}
name={favorite ? "i-iconamoon:star-fill" : "i-iconamoon:star"}
className={clsx({ "text-gold!": favorite })}
onMouseDown={toggleFavorite}
/>

<Popconfirm
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ RTFJS.loggingEnabled(false);
WMFJS.loggingEnabled(false);
EMFJS.loggingEnabled(false);

const RichText: FC<ClipboardItem> = (props) => {
const RTF: FC<ClipboardItem> = (props) => {
const { value } = props;

const [parsedHTML, setParsedHTML] = useState("");
Expand Down Expand Up @@ -39,4 +39,4 @@ const RichText: FC<ClipboardItem> = (props) => {
return <HTML value={parsedHTML} />;
};

export default memo(RichText);
export default memo(RTF);
35 changes: 20 additions & 15 deletions src/pages/Clipboard/Panel/components/List/components/Item/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import Files from "./components/Files";
import HTML from "./components/HTML";
import Header from "./components/Header";
import Image from "./components/Image";
import RichText from "./components/RichText";
import RTF from "./components/RTF";
import Text from "./components/Text";

interface ItemProps extends Partial<FlexProps> {
Expand All @@ -26,7 +26,7 @@ interface MenuItem extends ContextMenu.Item {

const Item: FC<ItemProps> = (props) => {
const { index, data, className, ...rest } = props;
const { id, type, value, search, group, isCollected, createTime } = data;
const { id, type, value, search, group, favorite, createTime } = data;
const { state, getClipboardList } = useContext(ClipboardPanelContext);
const { t } = useTranslation();
const { env, appearance } = useSnapshot(globalStore);
Expand All @@ -47,8 +47,8 @@ const Item: FC<ItemProps> = (props) => {
switch (type) {
case "text":
return writeText(value);
case "rich-text":
return writeRichText(search, value);
case "rtf":
return writeRTF(search, value);
case "html":
return writeHTML(search, value);
case "image":
Expand All @@ -64,8 +64,8 @@ const Item: FC<ItemProps> = (props) => {
paste();
};

const collect = async () => {
await updateSQL("history", { id, isCollected: !isCollected });
const toggleFavorite = async () => {
await updateSQL("history", { id, favorite: !favorite });

getClipboardList?.();
};
Expand All @@ -81,7 +81,7 @@ const Item: FC<ItemProps> = (props) => {
};

const exportFile = async () => {
const ext = type === "text" ? "txt" : type === "rich-text" ? "rtf" : type;
const ext = type === "text" ? "txt" : type;
const fileName = `${env.appName}_${id}.${ext}`;
const destination = (await downloadDir()) + fileName;

Expand Down Expand Up @@ -152,8 +152,8 @@ const Item: FC<ItemProps> = (props) => {
const deleteAll = async (list: ClipboardItem[]) => {
let filteredList = list;

if (!state.isCollected) {
filteredList = list.filter((item) => !item.isCollected);
if (!state.favorite) {
filteredList = list.filter((item) => !item.favorite);
}

for await (const item of filteredList) {
Expand Down Expand Up @@ -188,14 +188,14 @@ const Item: FC<ItemProps> = (props) => {
},
{
label: t("clipboard.button.context_menu.paste_as_plain_text"),
hide: type !== "html" && type !== "rich-text",
hide: type !== "html" && type !== "rtf",
event: pastePlainText,
},
{
label: isCollected
label: favorite
? t("clipboard.button.context_menu.unfavorite")
: t("clipboard.button.context_menu.favorite"),
event: collect,
event: toggleFavorite,
},
{
label: t("clipboard.button.context_menu.open_in_browser"),
Expand Down Expand Up @@ -272,8 +272,8 @@ const Item: FC<ItemProps> = (props) => {

const renderContent = () => {
switch (type) {
case "rich-text":
return <RichText {...data} />;
case "rtf":
return <RTF {...data} />;
case "html":
return <HTML {...data} />;
case "image":
Expand Down Expand Up @@ -301,7 +301,12 @@ const Item: FC<ItemProps> = (props) => {
onClick={() => handleClick("single")}
onDoubleClick={() => handleClick("double")}
>
<Header {...data} copy={copy} collect={collect} deleteItem={deleteItem} />
<Header
{...data}
copy={copy}
toggleFavorite={toggleFavorite}
deleteItem={deleteItem}
/>

<div className="flex-1 select-auto overflow-hidden break-words">
{renderContent()}
Expand Down
Loading

0 comments on commit 86c4a03

Please sign in to comment.