Commit 0145b453 authored by Chunchi Che's avatar Chunchi Che

Merge branch 'feature/sort-shuffle' into 'main'

feat: add sort and shuffle

See merge request mycard/Neos!301
parents c91d637a 2f42720c
Pipeline #23393 passed with stages
in 12 minutes and 43 seconds
...@@ -3,8 +3,10 @@ import { ...@@ -3,8 +3,10 @@ import {
DeleteOutlined, DeleteOutlined,
EditOutlined, EditOutlined,
FilterOutlined, FilterOutlined,
RetweetOutlined,
SearchOutlined, SearchOutlined,
SortAscendingOutlined, SortAscendingOutlined,
SwapOutlined,
UndoOutlined, UndoOutlined,
} from "@ant-design/icons"; } from "@ant-design/icons";
import { import {
...@@ -99,6 +101,14 @@ export const Component: React.FC = () => { ...@@ -99,6 +101,14 @@ export const Component: React.FC = () => {
} }
}; };
const handleDeckEditorShuffle = () => {
editDeckStore.shuffle(editDeckStore);
};
const handleDeckEditorSort = () => {
editDeckStore.sort(editDeckStore);
};
return ( return (
<DndProvider backend={HTML5Backend}> <DndProvider backend={HTML5Backend}>
<Background /> <Background />
...@@ -134,6 +144,8 @@ export const Component: React.FC = () => { ...@@ -134,6 +144,8 @@ export const Component: React.FC = () => {
onClear={editDeckStore.clear} onClear={editDeckStore.clear}
onReset={handleDeckEditorReset} onReset={handleDeckEditorReset}
onSave={handleDeckEditorSave} onSave={handleDeckEditorSave}
onShuffle={handleDeckEditorShuffle}
onSort={handleDeckEditorSort}
/> />
</div> </div>
<div className={styles.select}> <div className={styles.select}>
...@@ -155,10 +167,12 @@ Component.displayName = "Build"; ...@@ -155,10 +167,12 @@ Component.displayName = "Build";
/** 正在编辑的卡组 */ /** 正在编辑的卡组 */
export const DeckEditor: React.FC<{ export const DeckEditor: React.FC<{
deck: IDeck; deck: IDeck;
onShuffle: () => void;
onSort: () => void;
onClear: () => void; onClear: () => void;
onReset: () => void; onReset: () => void;
onSave: () => void; onSave: () => void;
}> = ({ deck, onClear, onReset, onSave }) => { }> = ({ deck, onClear, onReset, onSave, onShuffle, onSort }) => {
const snapEditDeck = useSnapshot(editDeckStore); const snapEditDeck = useSnapshot(editDeckStore);
const [deckName, setDeckName] = useState(editDeckStore.deckName); const [deckName, setDeckName] = useState(editDeckStore.deckName);
...@@ -223,11 +237,27 @@ export const DeckEditor: React.FC<{ ...@@ -223,11 +237,27 @@ export const DeckEditor: React.FC<{
placeholder="请输入卡组名字" placeholder="请输入卡组名字"
bordered={false} bordered={false}
prefix={<EditOutlined />} prefix={<EditOutlined />}
style={{ width: 400 }} style={{ width: 240 }}
onChange={(e) => setDeckName(e.target.value)} onChange={(e) => setDeckName(e.target.value)}
value={deckName} value={deckName}
/> />
<Space style={{ marginRight: 6 }}> <Space style={{ marginRight: 6 }}>
<Button
type="text"
size="small"
icon={<SwapOutlined />}
onClick={onShuffle}
>
打乱
</Button>
<Button
type="text"
size="small"
icon={<RetweetOutlined />}
onClick={onSort}
>
排序
</Button>
<Button <Button
type="text" type="text"
size="small" size="small"
......
import { shuffle } from "lodash-es";
import { proxy } from "valtio"; import { proxy } from "valtio";
import { type CardMeta } from "@/api"; import { type CardMeta } from "@/api";
...@@ -30,7 +31,7 @@ export const editDeckStore = proxy({ ...@@ -30,7 +31,7 @@ export const editDeckStore = proxy({
}, },
set(deck: EditingDeck) { set(deck: EditingDeck) {
editDeckStore.deckName = deck.deckName; editDeckStore.deckName = deck.deckName;
editDeckStore.main = deck.main.sort(compareCards); editDeckStore.main = deck.main;
editDeckStore.extra = deck.extra.sort(compareCards); editDeckStore.extra = deck.extra.sort(compareCards);
editDeckStore.side = deck.side.sort(compareCards); editDeckStore.side = deck.side.sort(compareCards);
editDeckStore.edited = false; editDeckStore.edited = false;
...@@ -41,6 +42,18 @@ export const editDeckStore = proxy({ ...@@ -41,6 +42,18 @@ export const editDeckStore = proxy({
editDeckStore.side = []; editDeckStore.side = [];
editDeckStore.edited = true; editDeckStore.edited = true;
}, },
/**
* 打乱
* @description 通常只有主卡组有打乱的需求,但这里也支持额外和副卡组
*/
shuffle(deck: EditingDeck, type: Type = "main") {
editDeckStore[type] = shuffle(deck[type]);
editDeckStore.edited = true;
},
sort(deck: EditingDeck, type: Type = "main") {
editDeckStore[type] = deck[type].sort(compareCards);
editDeckStore.edited = true;
},
getAll() { getAll() {
return [ return [
...editDeckStore.main, ...editDeckStore.main,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment