This commit is contained in:
Maciej Caderek
2022-02-20 23:36:33 +01:00
parent 5de734392c
commit 9561b0cbf6
5 changed files with 31 additions and 12 deletions

View File

@@ -193,6 +193,21 @@ class Game {
);
}
getFileName(anonymous: boolean) {
const header = this.header;
const w = anonymous ? "Anonymous" : header.WhitePretty;
const b = anonymous ? "Anonymous" : header.BlackPretty;
return (
(header.Date
? `${header.Date.replace(/\?/g, "X").replace(/\./g, "-")}_`
: "") +
`${w}_${b}` +
(header.Event ? `_${header.Event}` : "") +
(header.Round ? `_R${header.Round}` : "")
).replace(/\s+/g, "-");
}
get pgn() {
return this.game.pgn();
}

View File

@@ -84,7 +84,7 @@ const main = async () => {
const pgn = state.boardConfig.anonymous
? state.game.anonymousPGN
: state.game.pgn;
window.location.hash = `v1/pgn/${compressPGN(pgn)}`;
window.location.hash = `pgn/${compressPGN(pgn)}`;
}
},
toggleTitleScreen() {
@@ -115,7 +115,7 @@ const main = async () => {
ply: 0,
game,
});
window.location.hash = `v1/pgn/${compressPGN(game.pgn)}`;
window.location.hash = `pgn/${compressPGN(game.pgn)}`;
await player.load(game);
setState("activeTab", "game");
@@ -143,7 +143,7 @@ const main = async () => {
await player.load(game);
if (hash) {
window.location.hash = `v1/fen/${state.fen}`;
window.location.hash = `fen/${state.fen}`;
setState("activeTab", "game");
}
@@ -177,7 +177,7 @@ const main = async () => {
state.boardConfig,
state.gameConfig.picSize
);
download(data, "fen", "png");
download(data, `fen_${Date.now()}`, "png");
},
async downloadAnimation() {
await new Promise((resolve) => setTimeout(resolve, 0));
@@ -188,7 +188,10 @@ const main = async () => {
state.gameConfig.animationSize,
state.gameConfig.titleScreen
);
download(data, "game", state.gameConfig.format.toLowerCase());
const name = state.game.getFileName(state.boardConfig.anonymous);
download(data, name, state.gameConfig.format.toLowerCase());
},
};

View File

@@ -1,6 +1,6 @@
import { decompressPGN } from "../game/PGNHelpers";
const HEADER_REGEX = /^#v\d+\/(pgn|fen)\//;
const HEADER_REGEX = /^#(pgn|fen)\//;
const extractUrlData = () => {
const hash = window.location.hash;
@@ -12,13 +12,11 @@ const extractUrlData = () => {
};
}
const [rawVersion, format, ...chunks] = hash.split("/");
const [format, ...chunks] = hash.slice(1).split("/");
const version = Number((rawVersion.match(/\d+/g) ?? [])[0]);
const data = chunks.join("/");
return {
version,
pgn: format === "pgn" ? decompressPGN(data) : "",
fen: format === "fen" ? decodeURI(data) : "",
};

View File

@@ -115,7 +115,7 @@ const Share: Component<{ handlers: Handlers }> = (props) => {
<button
class="share__btn share__btn--right"
onClick={() => {
const link = `${location.origin}/#v1/fen/${encodeURI(state.fen)}`;
const link = `${location.origin}/#fen/${encodeURI(state.fen)}`;
navigator.clipboard.writeText(link);
blinkCopy("fen-link");
}}
@@ -219,7 +219,10 @@ const Share: Component<{ handlers: Handlers }> = (props) => {
const data = new Blob([state.pgn], {
type: "application/vnd.chess-pgn;charset=utf-8",
});
download(data, "pgn", "pgn");
const name = state.game.getFileName(
state.boardConfig.anonymous
);
download(data, name, "pgn");
}}
>
Export PGN

View File

@@ -3,7 +3,7 @@ const download = (data: string | Blob, name: string, ext: string) => {
const link = document.createElement("a");
link.href = url;
link.download = `${name}_${Date.now()}.${ext}`;
link.download = `${name}.${ext}`;
link.target = "_blank";
link.click();
URL.revokeObjectURL(url);