Skip to content

Commit cccbed1

Browse files
authored
Merge pull request #17 from wecode-ai/fix_history_list
fix: 修复粘贴界面item跳动的问题
2 parents 381f1a9 + 21dde11 commit cccbed1

12 files changed

Lines changed: 271 additions & 26 deletions

File tree

src/hooks/useContextMenu.tsx

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,37 +18,65 @@ import { useShortcut } from "@/contexts/ShortcutContext";
1818
import { deleteHistory, updateHistory } from "@/database/history";
1919
import { hasTag } from "@/database/tag";
2020
import { MainContext } from "@/pages/Main";
21-
import type { ItemProps } from "@/pages/Main/components/HistoryList/components/Item";
2221
import { pasteToClipboard, writeToClipboard } from "@/plugins/clipboard";
2322
import { hideWindow } from "@/plugins/window";
2423
import { clipboardStore, tagActions } from "@/stores/clipboard";
2524
import { globalStore } from "@/stores/global";
2625
import { textExpansionActions } from "@/stores/textExpansion";
26+
import type { DatabaseSchemaHistory } from "@/types/database";
27+
import { runDockAction } from "@/utils/dockAction";
2728
import { isMac } from "@/utils/is";
2829
import { join } from "@/utils/path";
2930

30-
interface UseContextMenuProps extends ItemProps {
31+
interface UseContextMenuProps {
32+
afterHide?: () => void;
33+
beforeActivate?: () => void;
34+
data: DatabaseSchemaHistory;
35+
deleteModal: HookAPI;
36+
handleNote: () => void;
3137
handleNext: () => void;
32-
handleSend: () => void;
38+
handleSend: (serviceType?: "aiChat" | "workQueue") => void;
39+
index: number;
3340
}
3441

3542
interface ContextMenuItem extends MenuItemOptions {
3643
hide?: boolean;
3744
}
3845

3946
export const useContextMenu = (props: UseContextMenuProps) => {
40-
const { data, deleteModal, handleNote, handleNext } = props;
47+
const {
48+
afterHide,
49+
beforeActivate,
50+
data,
51+
deleteModal,
52+
handleNote,
53+
handleNext,
54+
} = props;
4155
const { id, type, value, group, favorite, subtype } = data;
4256
const { t } = useTranslation();
4357
const { env } = useSnapshot(globalStore);
44-
const { rootState } = useContext(MainContext);
58+
const { rootState, touchHistoryItem } = useContext(MainContext);
4559
const { pushContext, popContext } = useShortcut();
4660

4761
const pasteAsText = async () => {
48-
await pasteToClipboard(data, true);
49-
5062
if (clipboardStore.window.style === "dock") {
51-
await hideWindow();
63+
await runDockAction({
64+
action: () => pasteToClipboard(data, true),
65+
afterHide,
66+
beforeAction: beforeActivate,
67+
hideWindow,
68+
onResult: (result) => {
69+
if (result.success) {
70+
touchHistoryItem?.(data);
71+
}
72+
},
73+
});
74+
return;
75+
}
76+
77+
const result = await pasteToClipboard(data, true);
78+
if (result.success) {
79+
touchHistoryItem?.(data);
5280
}
5381
};
5482

@@ -143,7 +171,10 @@ export const useContextMenu = (props: UseContextMenuProps) => {
143171
try {
144172
const items: ContextMenuItem[] = [
145173
{
146-
action: () => writeToClipboard(data),
174+
action: async () => {
175+
await writeToClipboard(data);
176+
touchHistoryItem?.(data);
177+
},
147178
text: t("clipboard.button.context_menu.copy"),
148179
},
149180
{

src/pages/Main/components/DockMode/components/DockCardRail/index.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import PasteCard from "../PasteCard";
99
export interface DockCardRailProps {
1010
items: DatabaseSchemaHistory[];
1111
activeId?: string;
12+
afterHide?: () => void;
13+
beforeActivate?: () => void;
1214
deleteModal: HookAPI;
1315
hasFilters?: boolean;
1416
onActiveChange: (id: string) => void;
@@ -22,6 +24,8 @@ const DockCardRail: FC<DockCardRailProps> = (props) => {
2224
const {
2325
items,
2426
activeId,
27+
afterHide,
28+
beforeActivate,
2529
deleteModal,
2630
hasFilters,
2731
onActiveChange,
@@ -156,6 +160,8 @@ const DockCardRail: FC<DockCardRailProps> = (props) => {
156160
>
157161
<PasteCard
158162
active={item.id === activeId}
163+
afterHide={afterHide}
164+
beforeActivate={beforeActivate}
159165
data={item}
160166
deleteModal={deleteModal}
161167
handleNote={() => {

src/pages/Main/components/DockMode/components/PasteCard/index.tsx

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,13 @@ import { clipboardStore } from "@/stores/clipboard";
3030
import { globalStore } from "@/stores/global";
3131
import type { DatabaseSchemaHistory } from "@/types/database";
3232
import { dayjs } from "@/utils/dayjs";
33+
import { runDockAction } from "@/utils/dockAction";
3334
import { isImage, isLinux } from "@/utils/is";
3435

3536
export interface PasteCardProps {
3637
active?: boolean;
38+
afterHide?: () => void;
39+
beforeActivate?: () => void;
3740
data: DatabaseSchemaHistory;
3841
deleteModal: HookAPI;
3942
handleNote: () => void;
@@ -72,10 +75,20 @@ const getTypeColorClass = (type: DatabaseSchemaHistory["type"]) => {
7275
};
7376

7477
const PasteCard: FC<PasteCardProps> = (props) => {
75-
const { active, data, deleteModal, handleNote, handleSend, index, onSelect } =
76-
props;
78+
const {
79+
active,
80+
afterHide,
81+
beforeActivate,
82+
data,
83+
deleteModal,
84+
handleNote,
85+
handleSend,
86+
index,
87+
onSelect,
88+
} = props;
7789
const { id, count, createTime, favorite, note, subtype, type, value } = data;
78-
const { rootState, handlePasteResult } = useContext(MainContext);
90+
const { rootState, handlePasteResult, touchHistoryItem } =
91+
useContext(MainContext);
7992
const { t, i18n } = useTranslation();
8093
const { wegent } = useSnapshot(clipboardStore);
8194
const { env, shortcut } = useSnapshot(globalStore);
@@ -333,6 +346,8 @@ const PasteCard: FC<PasteCardProps> = (props) => {
333346
};
334347

335348
const { handleContextMenu, handleDelete, handleFavorite } = useContextMenu({
349+
afterHide,
350+
beforeActivate,
336351
data,
337352
deleteModal,
338353
handleNext,
@@ -348,9 +363,18 @@ const PasteCard: FC<PasteCardProps> = (props) => {
348363
case LISTEN_KEY.CLIPBOARD_ITEM_PREVIEW:
349364
return handlePreview();
350365
case LISTEN_KEY.CLIPBOARD_ITEM_PASTE: {
351-
const result = await runActivateAction(data);
352-
handlePasteResult?.(result);
353-
await hideWindow();
366+
await runDockAction({
367+
action: () => runActivateAction(data),
368+
afterHide,
369+
beforeAction: beforeActivate,
370+
hideWindow,
371+
onResult: (result) => {
372+
handlePasteResult?.(result);
373+
if (result.success) {
374+
touchHistoryItem?.(data);
375+
}
376+
},
377+
});
354378
return;
355379
}
356380
case LISTEN_KEY.CLIPBOARD_ITEM_DELETE:
@@ -526,9 +550,18 @@ const PasteCard: FC<PasteCardProps> = (props) => {
526550

527551
const handleCardDoubleClick = async () => {
528552
rootState.activeId = id;
529-
const result = await runActivateAction(data);
530-
handlePasteResult?.(result);
531-
await hideWindow();
553+
await runDockAction({
554+
action: () => runActivateAction(data),
555+
afterHide,
556+
beforeAction: beforeActivate,
557+
hideWindow,
558+
onResult: (result) => {
559+
handlePasteResult?.(result);
560+
if (result.success) {
561+
touchHistoryItem?.(data);
562+
}
563+
},
564+
});
532565
};
533566

534567
const cardContent = (

src/pages/Main/components/DockMode/index.tsx

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { InputRef } from "antd";
22
import { Modal } from "antd";
33
import clsx from "clsx";
4-
import { useContext, useEffect, useRef, useState } from "react";
4+
import { useCallback, useContext, useEffect, useRef, useState } from "react";
55
import { useSnapshot } from "valtio";
66
import { LISTEN_KEY } from "@/constants";
77
import { useShortcut, useShortcutAction } from "@/contexts/ShortcutContext";
@@ -25,6 +25,9 @@ const DockMode = () => {
2525
const { shortcut, env } = useSnapshot(globalStore);
2626
const { wegent, aiSend } = useSnapshot(clipboardStore);
2727
const [searchActive, setSearchActive] = useState(Boolean(rootState.search));
28+
const [frozenItems, setFrozenItems] = useState<
29+
DatabaseSchemaHistory[] | null
30+
>(null);
2831

2932
// WegentChat 是否显示(由环境变量控制)
3033
const wegentChatEnabled = env.features?.wegentChat ?? false;
@@ -42,6 +45,14 @@ const DockMode = () => {
4245
rootState.activeId = firstItem.id;
4346
};
4447

48+
const freezeVisibleItems = useCallback(() => {
49+
setFrozenItems([...rootState.list]);
50+
}, [rootState]);
51+
52+
const releaseVisibleItems = useCallback(() => {
53+
setFrozenItems(null);
54+
}, []);
55+
4556
const { loadMore } = useHistoryList({
4657
scrollToTop: scrollToStart,
4758
});
@@ -421,6 +432,7 @@ const DockMode = () => {
421432
hideWindow();
422433
},
423434
onFocus() {
435+
releaseVisibleItems();
424436
if (!windowConfig.rememberActiveId) {
425437
scrollToStart();
426438
}
@@ -553,6 +565,7 @@ const DockMode = () => {
553565
}, [searchActive, rootState.search]);
554566

555567
const totalHeight = getDockContentHeight(scale);
568+
const railItems = frozenItems ?? rootState.list;
556569

557570
return (
558571
<div
@@ -579,9 +592,11 @@ const DockMode = () => {
579592

580593
<DockCardRail
581594
activeId={rootState.activeId}
595+
afterHide={releaseVisibleItems}
596+
beforeActivate={freezeVisibleItems}
582597
deleteModal={deleteModal}
583598
hasFilters={Boolean(rootState.search) || rootState.group !== "all"}
584-
items={rootState.list}
599+
items={railItems}
585600
onActiveChange={(id) => {
586601
rootState.activeId = id;
587602
}}

src/pages/Main/components/HistoryList/components/Header/index.tsx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ const Header: FC<HeaderProps> = (props) => {
4848
sourceAppIcon,
4949
sourceAppPath,
5050
} = data;
51-
const { rootState, handlePasteResult } = useContext(MainContext);
51+
const { rootState, handlePasteResult, touchHistoryItem } =
52+
useContext(MainContext);
5253
const { t, i18n } = useTranslation();
5354
const { content, wegent } = useSnapshot(clipboardStore);
5455
const { env } = useSnapshot(globalStore);
@@ -144,10 +145,15 @@ const Header: FC<HeaderProps> = (props) => {
144145

145146
switch (key) {
146147
case "copy":
147-
return writeToClipboard(data);
148+
await writeToClipboard(data);
149+
touchHistoryItem?.(data);
150+
return;
148151
case "pastePlain": {
149152
const result = await pasteToClipboard(data, true);
150153
handlePasteResult?.(result);
154+
if (result.success) {
155+
touchHistoryItem?.(data);
156+
}
151157
return;
152158
}
153159
case "note":

src/pages/Main/components/HistoryList/components/Item/index.tsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ export interface ItemProps {
3030
const Item: FC<ItemProps> = (props) => {
3131
const { index, data, handleNote, handleSend } = props;
3232
const { id, type, note, value } = data;
33-
const { rootState, handlePasteResult } = useContext(MainContext);
33+
const { rootState, handlePasteResult, touchHistoryItem } =
34+
useContext(MainContext);
3435
const { content } = useSnapshot(clipboardStore);
3536

3637
const handlePreview = () => {
@@ -64,6 +65,9 @@ const Item: FC<ItemProps> = (props) => {
6465
case LISTEN_KEY.CLIPBOARD_ITEM_PASTE: {
6566
const result = await runActivateAction(data);
6667
handlePasteResult?.(result);
68+
if (result.success) {
69+
touchHistoryItem?.(data);
70+
}
6771
return;
6872
}
6973
case LISTEN_KEY.CLIPBOARD_ITEM_DELETE:
@@ -90,6 +94,9 @@ const Item: FC<ItemProps> = (props) => {
9094

9195
const result = await runActivateAction(data);
9296
handlePasteResult?.(result);
97+
if (result.success) {
98+
touchHistoryItem?.(data);
99+
}
93100
};
94101

95102
const renderContent = () => {

0 commit comments

Comments
 (0)