Skip to content

Commit 31bf352

Browse files
committed
feat: add export all
1 parent ef5ea10 commit 31bf352

File tree

4 files changed

+23
-7
lines changed

4 files changed

+23
-7
lines changed

src/Components/ShowCards/ShowCards.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import ShowOneHand from "./ShowOneHand";
44
import "./index.css";
55
import { PROGRAM_POSITIONS } from "../../Utils/maps";
66
import React, { PropsWithChildren, Suspense } from "react";
7-
import exportPBN from "../../Utils/PBN";
7+
import { exportPBNs } from "../../Utils/PBN";
88
import Board from "../../models/Board";
99

1010
interface ShowCardsProps extends PropsWithChildren {
@@ -21,7 +21,7 @@ export default function ShowCards(props: ShowCardsProps) {
2121
const allHands = board.getAllHands();
2222
return (
2323
<div className="board-container">
24-
<div className="board-number"><div>{board.boardnum}</div>{dds && <button className="export" onClick={() => exportPBN(board)}>export to PBN</button>}</div>
24+
<div className="board-number"><div>{board.boardnum}</div>{dds && <button className="export" onClick={() => exportPBNs([board])}>export to PBN</button>}</div>
2525
<div className="predicted">{children}</div>
2626
{dds &&
2727
<div className="double-dummy">

src/Utils/PBN.ts

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,9 @@ export function convertAllHandsToPBN(allHands: Hand[]) {
2929
return str;
3030
}
3131

32-
export default function exportPBN(board: Board) {
32+
export function getPBN(board: Board) {
3333
const date = new Date();
34-
const pbn = `% PBN 2.1
35-
% EXPORT
36-
%Content-type: text/x-pbn; charset=ISO-8859-1
34+
const pbn = `
3735
[Event ""]
3836
[Site ""]
3937
[Date "${date.getFullYear()}.${date.getMonth() + 1}.${date.getDate()}"]
@@ -73,9 +71,24 @@ W D 0
7371
W C 0
7472
[OptimumScore ""]
7573
`
74+
board.pbn = pbn;
75+
return pbn;
76+
}
77+
78+
export function exportPBNs(boards: Board[]) {
79+
let print = `% PBN 2.1
80+
% EXPORT
81+
%Content-type: text/x-pbn; charset=ISO-8859-1
82+
`;
83+
boards.forEach((board) => {
84+
if (!board.pbn) {
85+
board.pbn = getPBN(board);
86+
}
87+
print += board.pbn;
88+
})
7689

7790
// download
78-
const blob = new Blob([pbn], { type: 'text/plain' });
91+
const blob = new Blob([print], { type: 'text/plain' });
7992
const url = URL.createObjectURL(blob);
8093
// 创建一个临时的 <a> 标签,设置 href 属性为刚刚创建的 URL
8194
const link = document.createElement('a');

src/models/Board.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export default class Board {
2727
Ehand: Hand;
2828
Whand: Hand;
2929
ddsTricks?: string | (string | number)[][];
30+
pbn?: string;
3031

3132
constructor(boardnum: number) {
3233
this.boardnum = boardnum;

src/views/Show/ShowResults.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { createContext } from "react";
22
import ShowAllBoards from "../../Components/ShowAllBoards/ShowAllBoards";
33
import Analysis from "../Analysis/Analysis";
44
import Board from "../../models/Board";
5+
import { exportPBNs } from "../../Utils/PBN";
56

67
interface ShowResultsProps {
78
all_boards: Board[];
@@ -20,6 +21,7 @@ function ShowResults(props: ShowResultsProps) {
2021
const { all_boards, dds, ...rest } = props;
2122
return (
2223
<ShowResultsContext.Provider value={{ all_boards, dds }}>
24+
{all_boards.length > 0 && <button onClick={() => exportPBNs(all_boards)}>export all in pbn format</button>}
2325
{dds && <Analysis />}
2426
<ShowAllBoards {...rest} />
2527
</ShowResultsContext.Provider>

0 commit comments

Comments
 (0)