From baee0143a02cccddf2e9193decfe7f38067384d6 Mon Sep 17 00:00:00 2001 From: Ben Rush Date: Sun, 29 Jan 2023 13:22:09 -0800 Subject: [PATCH 1/3] Add support for startingPosition yaml key --- src/Chesser.ts | 18 +++++++++++++++++- src/menu.ts | 8 ++++---- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/src/Chesser.ts b/src/Chesser.ts index d7d71cc..b7b4dd6 100644 --- a/src/Chesser.ts +++ b/src/Chesser.ts @@ -59,6 +59,9 @@ import "../assets/board-css/green.css"; import "../assets/board-css/purple.css"; import "../assets/board-css/ic.css"; import debug from "./debug"; +import startingPositions from "./startingPositions" +import { readFileSync } from "fs"; +import { start } from "repl"; export function draw_chessboard(app: App, settings: ChesserSettings) { return (source: string, el: HTMLElement, ctx: MarkdownPostProcessorContext) => { @@ -126,7 +129,20 @@ export class Chesser extends MarkdownRenderChild { if (config.pgn) { debug(() => console.debug("loading from pgn", config.pgn)); this.chess.load_pgn(config.pgn); - } else if (config.fen) { + } else if (config.startingPosition) { + debug(() => console.debug("loading from starting position ", config.startingPosition)); + let positions = startingPositions.flatMap((cat) => cat.items) + const startingPosition = positions.find((item) => item.name === config.startingPosition) ?? + positions.find((item) => item.eco === config.startingPosition); + if (startingPosition) { + this.loadFen(startingPosition.fen, startingPosition.moves) + } + else { + new Notice("Chesser: Unable to find starting position " + config.startingPosition) + } + + } + else if (config.fen) { debug(() => console.debug("loading from fen", config.fen)); this.chess.load(config.fen); } diff --git a/src/menu.ts b/src/menu.ts index b0842e1..d601a01 100644 --- a/src/menu.ts +++ b/src/menu.ts @@ -1,6 +1,6 @@ import { setIcon, Setting } from "obsidian"; import { Chesser } from "./Chesser"; -import startingPositons from "./startingPositions"; +import startingPositions from "./startingPositions"; export default class ChesserMenu { private chesser: Chesser; @@ -29,7 +29,7 @@ export default class ChesserMenu { }); el.createEl("optgroup", {}, (optgroup) => { optgroup.label = "Popular Openings"; - startingPositons.forEach((category) => { + startingPositions.forEach((category) => { category.items.forEach((item) => { optgroup.createEl("option", { value: item.eco, @@ -58,7 +58,7 @@ export default class ChesserMenu { return; } - const startingPosition = startingPositons + const startingPosition = startingPositions .flatMap((cat) => cat.items) .find((item) => item.eco === value); @@ -83,7 +83,7 @@ export default class ChesserMenu { } getStartingPositionFromFen(fen: string) { - return startingPositons.flatMap((cat) => cat.items).find((item) => item.eco === fen); + return startingPositions.flatMap((cat) => cat.items).find((item) => item.eco === fen); } createToolbar() { From e8823e115746fe1e2b7630c02935c9f41ac41004 Mon Sep 17 00:00:00 2001 From: Ben Rush Date: Mon, 30 Jan 2023 08:36:06 -0800 Subject: [PATCH 2/3] Update Settings table --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index d6bb107..2e82f50 100644 --- a/README.md +++ b/README.md @@ -29,6 +29,7 @@ Here are the available settings for a `chesr` code block: | `drawable` | `true`/`false` | Controls the ability to draw annotations (arrows, circles) on the board. | | `viewOnly` | `true`/`false` | If enabled, displays a static chess board (no moves, annotations, ...). | | `free` | `true`/`false` | If enabled, disables the chess logic, all moves are valid. | +| `startingPosition`| The name of a starting position (e.g., `Queen's Gambit`) or ECO string (e.g., `D06`) | Starts the chess board with a particular opening | You can permanently set some settings in [Chesser](https://github.com/SilentVoid13/Chesser)'s obsidian plugin settings. From 2acce254055fbd75cd2699cf32680806fae14355 Mon Sep 17 00:00:00 2001 From: Ben Rush Date: Mon, 30 Jan 2023 08:41:42 -0800 Subject: [PATCH 3/3] Fix bug with CG init --- src/Chesser.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Chesser.ts b/src/Chesser.ts index b7b4dd6..4ff18f4 100644 --- a/src/Chesser.ts +++ b/src/Chesser.ts @@ -125,6 +125,8 @@ export class Chesser extends MarkdownRenderChild { }); }); } + + this.cg = Chessground(containerEl.createDiv()) if (config.pgn) { debug(() => console.debug("loading from pgn", config.pgn)); @@ -159,7 +161,7 @@ export class Chesser extends MarkdownRenderChild { // Setup UI this.set_style(containerEl, config.pieceStyle, config.boardStyle); try { - this.cg = Chessground(containerEl.createDiv(), { + this.cg.set({ fen: this.chess.fen(), addDimensionsCssVars: true, lastMove,