Skip to content

Commit

Permalink
feat: add export all
Browse files Browse the repository at this point in the history
  • Loading branch information
S-N-O-R-L-A-X committed Nov 6, 2024
1 parent ef5ea10 commit 31bf352
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/Components/ShowCards/ShowCards.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import ShowOneHand from "./ShowOneHand";
import "./index.css";
import { PROGRAM_POSITIONS } from "../../Utils/maps";
import React, { PropsWithChildren, Suspense } from "react";
import exportPBN from "../../Utils/PBN";
import { exportPBNs } from "../../Utils/PBN";
import Board from "../../models/Board";

interface ShowCardsProps extends PropsWithChildren {
Expand All @@ -21,7 +21,7 @@ export default function ShowCards(props: ShowCardsProps) {
const allHands = board.getAllHands();
return (
<div className="board-container">
<div className="board-number"><div>{board.boardnum}</div>{dds && <button className="export" onClick={() => exportPBN(board)}>export to PBN</button>}</div>
<div className="board-number"><div>{board.boardnum}</div>{dds && <button className="export" onClick={() => exportPBNs([board])}>export to PBN</button>}</div>
<div className="predicted">{children}</div>
{dds &&
<div className="double-dummy">
Expand Down
23 changes: 18 additions & 5 deletions src/Utils/PBN.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,9 @@ export function convertAllHandsToPBN(allHands: Hand[]) {
return str;
}

export default function exportPBN(board: Board) {
export function getPBN(board: Board) {
const date = new Date();
const pbn = `% PBN 2.1
% EXPORT
%Content-type: text/x-pbn; charset=ISO-8859-1
const pbn = `
[Event ""]
[Site ""]
[Date "${date.getFullYear()}.${date.getMonth() + 1}.${date.getDate()}"]
Expand Down Expand Up @@ -73,9 +71,24 @@ W D 0
W C 0
[OptimumScore ""]
`
board.pbn = pbn;
return pbn;
}

export function exportPBNs(boards: Board[]) {
let print = `% PBN 2.1
% EXPORT
%Content-type: text/x-pbn; charset=ISO-8859-1
`;
boards.forEach((board) => {
if (!board.pbn) {
board.pbn = getPBN(board);
}
print += board.pbn;
})

// download
const blob = new Blob([pbn], { type: 'text/plain' });
const blob = new Blob([print], { type: 'text/plain' });
const url = URL.createObjectURL(blob);
// 创建一个临时的 <a> 标签,设置 href 属性为刚刚创建的 URL
const link = document.createElement('a');
Expand Down
1 change: 1 addition & 0 deletions src/models/Board.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export default class Board {
Ehand: Hand;
Whand: Hand;
ddsTricks?: string | (string | number)[][];
pbn?: string;

constructor(boardnum: number) {
this.boardnum = boardnum;
Expand Down
2 changes: 2 additions & 0 deletions src/views/Show/ShowResults.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { createContext } from "react";
import ShowAllBoards from "../../Components/ShowAllBoards/ShowAllBoards";
import Analysis from "../Analysis/Analysis";
import Board from "../../models/Board";
import { exportPBNs } from "../../Utils/PBN";

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

0 comments on commit 31bf352

Please sign in to comment.