@johnmorrisdotca/sugoroku 1.0.2 → 1.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (67) hide show
  1. package/CHANGELOG.md +30 -0
  2. package/README.md +78 -68
  3. package/dist/board.d.ts +216 -12
  4. package/dist/board.js +205 -13
  5. package/dist/colours.d.ts +80 -8
  6. package/dist/colours.js +57 -7
  7. package/dist/computer.d.ts +139 -9
  8. package/dist/computer.js +118 -11
  9. package/dist/dice.d.ts +53 -4
  10. package/dist/dice.js +42 -3
  11. package/dist/draw-entry.d.ts +3 -3
  12. package/dist/draw-entry.js +2 -2
  13. package/dist/draw.d.ts +43 -3
  14. package/dist/draw.js +63 -13
  15. package/dist/element-define.d.ts +1 -1
  16. package/dist/element-define.js +4 -13
  17. package/dist/element.d.ts +116 -5
  18. package/dist/element.js +378 -102
  19. package/dist/elementFrame.d.ts +21 -0
  20. package/dist/elementFrame.js +110 -0
  21. package/dist/evaluate.d.ts +76 -4
  22. package/dist/evaluate.js +70 -5
  23. package/dist/game.d.ts +408 -23
  24. package/dist/game.js +344 -20
  25. package/dist/geometry.d.ts +50 -3
  26. package/dist/geometry.js +64 -28
  27. package/dist/index.d.ts +12 -11
  28. package/dist/index.js +10 -9
  29. package/dist/match.d.ts +66 -4
  30. package/dist/match.js +69 -6
  31. package/dist/mount.d.ts +77 -6
  32. package/dist/mount.js +105 -357
  33. package/dist/mountView.d.ts +58 -0
  34. package/dist/mountView.js +459 -0
  35. package/dist/moves.d.ts +48 -1
  36. package/dist/moves.js +35 -1
  37. package/dist/notation.d.ts +92 -4
  38. package/dist/notation.js +88 -5
  39. package/dist/play-entry.d.ts +4 -4
  40. package/dist/play-entry.js +1 -1
  41. package/dist/playStyle.d.ts +6 -0
  42. package/dist/playStyle.js +6 -0
  43. package/dist/plays.d.ts +108 -3
  44. package/dist/plays.js +96 -3
  45. package/dist/positionElement.d.ts +107 -0
  46. package/dist/positionElement.js +303 -0
  47. package/dist/positionId.d.ts +29 -1
  48. package/dist/positionId.js +33 -2
  49. package/dist/random.d.ts +33 -3
  50. package/dist/random.js +22 -2
  51. package/dist/record.d.ts +151 -8
  52. package/dist/record.js +121 -36
  53. package/dist/rules.d.ts +130 -10
  54. package/dist/rules.js +133 -14
  55. package/dist/side.d.ts +38 -3
  56. package/dist/side.js +30 -3
  57. package/dist/sound.d.ts +61 -3
  58. package/dist/sound.js +27 -2
  59. package/dist/strings.d.ts +42 -2
  60. package/dist/strings.js +37 -3
  61. package/dist/style.d.ts +6 -0
  62. package/dist/style.js +6 -0
  63. package/dist/variants.d.ts +113 -8
  64. package/dist/variants.js +81 -7
  65. package/dist/version.d.ts +9 -2
  66. package/dist/version.js +9 -2
  67. package/package.json +29 -22
package/dist/element.js CHANGED
@@ -1,17 +1,6 @@
1
- var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
2
- if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
3
- if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
4
- return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
5
- };
6
- var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
7
- if (kind === "m") throw new TypeError("Private method is not writable");
8
- if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
9
- if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
10
- return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
11
- };
12
- var _SugorokuBoard_instances, _SugorokuBoard_mount, _SugorokuBoard_key, _SugorokuBoard_queued, _SugorokuBoard_side, _SugorokuBoard_look, _SugorokuBoard_refresh;
1
+ import { SUGOROKU_BOARD_NAMES, SUGOROKU_CHECKER_SET_NAMES, } from "./colours.js";
13
2
  import { STRENGTHS } from "./computer.js";
14
- import { SUGOROKU_BOARD_NAMES, SUGOROKU_CHECKER_SET_NAMES } from "./colours.js";
3
+ import { GAME_ATTRIBUTES, isOn, oneOf, SugorokuFrame } from "./elementFrame.js";
15
4
  import { mountSugoroku } from "./mount.js";
16
5
  import { isVariantKey } from "./variants.js";
17
6
  /**
@@ -42,133 +31,420 @@ import { isVariantKey } from "./variants.js";
42
31
  * `nextGame`, `newMatch`, `record`) and the properties `game` and `match`. Nothing in it can be selected, and its box stays
43
32
  * one steady shape whatever is on the board.
44
33
  */
45
- const ElementBase = typeof HTMLElement === "undefined" ? class {
46
- } : HTMLElement;
47
- const isOn = (value) => value !== null && !["false", "off", "0", "no"].includes(value.toLowerCase());
48
- const oneOf = (value, allowed) => (allowed.includes(value) ? value : undefined);
49
- /** The attributes that name the game: changing one starts a new match. */
50
- const GAME_ATTRIBUTES = ["variant", "preset", "points", "cube", "crawford", "jacoby", "beaver", "gammons", "seed", "dice", "controls", "drag", "auto-end", "sound", "computer-delay"];
51
- export class SugorokuBoard extends ElementBase {
52
- constructor() {
53
- super(...arguments);
54
- _SugorokuBoard_instances.add(this);
55
- _SugorokuBoard_mount.set(this, null);
56
- _SugorokuBoard_key.set(this, "");
57
- _SugorokuBoard_queued.set(this, false);
34
+ /** The real mounted options, checked alongside builder coverage.
35
+ * @example
36
+ * ```ts
37
+ * import { SUGOROKU_EMBED_OPTION_KEYS } from "@johnmorrisdotca/sugoroku/element";
38
+ * console.log(typeof SUGOROKU_EMBED_OPTION_KEYS); // object
39
+ * ```
40
+ */
41
+ export const SUGOROKU_EMBED_OPTION_KEYS = Object.keys({
42
+ board: true,
43
+ checkers: true,
44
+ colours: true,
45
+ theme: true,
46
+ home: true,
47
+ view: true,
48
+ numbers: true,
49
+ orientation: true,
50
+ variant: true,
51
+ rules: true,
52
+ preset: true,
53
+ seed: true,
54
+ dice: true,
55
+ computer: true,
56
+ computerDelay: true,
57
+ controls: true,
58
+ drag: true,
59
+ autoEnd: true,
60
+ language: true,
61
+ names: true,
62
+ sound: true,
63
+ match: true,
64
+ onRoll: true,
65
+ onMove: true,
66
+ onTurn: true,
67
+ onCube: true,
68
+ onEnd: true,
69
+ });
70
+ /** Native custom element with reflected setup and presentation.
71
+ * @example
72
+ * ```ts
73
+ * import { SugorokuBoard } from "@johnmorrisdotca/sugoroku/element";
74
+ * console.log(SugorokuBoard.observedAttributes.includes("lang")); // true
75
+ * ```
76
+ */
77
+ export class SugorokuBoard extends SugorokuFrame {
78
+ static observedAttributes = [
79
+ ...GAME_ATTRIBUTES,
80
+ "white",
81
+ "black",
82
+ "board",
83
+ "checkers",
84
+ "theme",
85
+ "home",
86
+ "view",
87
+ "numbers",
88
+ "orientation",
89
+ "lang",
90
+ "size",
91
+ "width",
92
+ "label",
93
+ ];
94
+ #mount = null;
95
+ #key = "";
96
+ #queued = false;
97
+ #options = {};
98
+ /** Host mount options; callback and look changes preserve the current game. */
99
+ get options() {
100
+ return this.#options;
101
+ }
102
+ set options(value) {
103
+ this.#options = value ?? {};
104
+ if (this.isConnected)
105
+ this.#refresh();
58
106
  }
59
107
  connectedCallback() {
60
- __classPrivateFieldGet(this, _SugorokuBoard_instances, "m", _SugorokuBoard_refresh).call(this);
108
+ this.#refresh();
61
109
  }
62
110
  disconnectedCallback() {
63
- __classPrivateFieldGet(this, _SugorokuBoard_mount, "f")?.destroy();
64
- __classPrivateFieldSet(this, _SugorokuBoard_mount, null, "f");
65
- __classPrivateFieldSet(this, _SugorokuBoard_key, "", "f");
111
+ this.#mount?.destroy();
112
+ this.#mount = null;
113
+ this.#key = "";
66
114
  }
67
115
  attributeChangedCallback() {
68
- if (!this.isConnected || __classPrivateFieldGet(this, _SugorokuBoard_queued, "f"))
116
+ if (!this.isConnected || this.#queued)
69
117
  return;
70
- __classPrivateFieldSet(this, _SugorokuBoard_queued, true, "f");
118
+ this.#queued = true;
71
119
  queueMicrotask(() => {
72
- __classPrivateFieldSet(this, _SugorokuBoard_queued, false, "f");
73
- __classPrivateFieldGet(this, _SugorokuBoard_instances, "m", _SugorokuBoard_refresh).call(this);
120
+ this.#queued = false;
121
+ if (this.isConnected)
122
+ this.#refresh();
74
123
  });
75
124
  }
76
125
  /** The mounted board's handle (`mountSugoroku`), or null while it is not on a page. */
77
126
  get mount() {
78
- return __classPrivateFieldGet(this, _SugorokuBoard_mount, "f");
127
+ return this.#mount;
79
128
  }
80
129
  get game() {
81
- return __classPrivateFieldGet(this, _SugorokuBoard_mount, "f")?.game() ?? null;
130
+ return this.#mount?.game() ?? null;
82
131
  }
83
132
  get match() {
84
- return __classPrivateFieldGet(this, _SugorokuBoard_mount, "f")?.match() ?? null;
133
+ return this.#mount?.match() ?? null;
85
134
  }
86
135
  record() {
87
- return __classPrivateFieldGet(this, _SugorokuBoard_mount, "f")?.record() ?? "";
136
+ return this.#mount?.record() ?? "";
88
137
  }
89
138
  roll(dice) {
90
- __classPrivateFieldGet(this, _SugorokuBoard_mount, "f")?.roll(dice);
139
+ this.#mount?.roll(dice);
91
140
  }
92
141
  move(from, to) {
93
- return __classPrivateFieldGet(this, _SugorokuBoard_mount, "f")?.move(from, to) ?? false;
142
+ return this.#mount?.move(from, to) ?? false;
94
143
  }
95
144
  undo() {
96
- __classPrivateFieldGet(this, _SugorokuBoard_mount, "f")?.undo();
145
+ this.#mount?.undo();
97
146
  }
98
147
  done() {
99
- __classPrivateFieldGet(this, _SugorokuBoard_mount, "f")?.done();
148
+ this.#mount?.done();
100
149
  }
101
150
  double() {
102
- __classPrivateFieldGet(this, _SugorokuBoard_mount, "f")?.double();
151
+ this.#mount?.double();
103
152
  }
104
153
  take() {
105
- __classPrivateFieldGet(this, _SugorokuBoard_mount, "f")?.take();
154
+ this.#mount?.take();
106
155
  }
107
156
  drop() {
108
- __classPrivateFieldGet(this, _SugorokuBoard_mount, "f")?.drop();
157
+ this.#mount?.drop();
158
+ }
159
+ /** Calling beaver() keeps the native action; assignment reflects the rule attribute. */
160
+ get beaver() {
161
+ return () => this.#mount?.beaver();
109
162
  }
110
- beaver() {
111
- __classPrivateFieldGet(this, _SugorokuBoard_mount, "f")?.beaver();
163
+ set beaver(value) {
164
+ if (typeof value !== "function")
165
+ this.write("beaver", value);
112
166
  }
113
167
  resign() {
114
- __classPrivateFieldGet(this, _SugorokuBoard_mount, "f")?.resign();
168
+ this.#mount?.resign();
115
169
  }
116
170
  nextGame() {
117
- __classPrivateFieldGet(this, _SugorokuBoard_mount, "f")?.nextGame();
171
+ this.#mount?.nextGame();
118
172
  }
119
173
  newMatch() {
120
- __classPrivateFieldGet(this, _SugorokuBoard_mount, "f")?.newMatch();
174
+ this.#mount?.newMatch();
121
175
  }
122
- }
123
- _SugorokuBoard_mount = new WeakMap(), _SugorokuBoard_key = new WeakMap(), _SugorokuBoard_queued = new WeakMap(), _SugorokuBoard_instances = new WeakSet(), _SugorokuBoard_side = function _SugorokuBoard_side(name) {
124
- return oneOf(this.getAttribute(name), STRENGTHS) ?? null;
125
- }, _SugorokuBoard_look = function _SugorokuBoard_look() {
126
- const view = this.getAttribute("view");
127
- const language = oneOf(this.getAttribute("lang"), ["en", "ja"]);
128
- return {
129
- board: oneOf(this.getAttribute("board"), SUGOROKU_BOARD_NAMES),
130
- checkers: oneOf(this.getAttribute("checkers"), SUGOROKU_CHECKER_SET_NAMES),
131
- theme: oneOf(this.getAttribute("theme"), ["auto", "light", "dark"]),
132
- home: oneOf(this.getAttribute("home"), ["left", "right"]),
133
- view: view === "white" || view === "black" || view === "auto" ? view : undefined,
134
- numbers: isOn(this.getAttribute("numbers")),
135
- orientation: oneOf(this.getAttribute("orientation"), ["auto", "landscape", "portrait"]),
136
- computer: { white: __classPrivateFieldGet(this, _SugorokuBoard_instances, "m", _SugorokuBoard_side).call(this, "white"), black: __classPrivateFieldGet(this, _SugorokuBoard_instances, "m", _SugorokuBoard_side).call(this, "black") },
137
- language,
138
- };
139
- }, _SugorokuBoard_refresh = function _SugorokuBoard_refresh() {
140
- const key = JSON.stringify(GAME_ATTRIBUTES.map((name) => this.getAttribute(name)));
141
- if (key === __classPrivateFieldGet(this, _SugorokuBoard_key, "f") && __classPrivateFieldGet(this, _SugorokuBoard_mount, "f") !== null) {
142
- __classPrivateFieldGet(this, _SugorokuBoard_mount, "f").set(__classPrivateFieldGet(this, _SugorokuBoard_instances, "m", _SugorokuBoard_look).call(this));
143
- return;
144
- }
145
- __classPrivateFieldGet(this, _SugorokuBoard_mount, "f")?.destroy();
146
- __classPrivateFieldSet(this, _SugorokuBoard_key, key, "f");
147
- const variant = this.getAttribute("variant");
148
- const rules = {};
149
- const points = this.getAttribute("points");
150
- if (points !== null && /^\d+$/.test(points))
151
- rules.points = Number(points);
152
- for (const name of ["cube", "crawford", "jacoby", "beaver", "gammons"]) {
176
+ /** The `variant` attribute, reflected. */
177
+ get variant() {
178
+ return this.getAttribute("variant");
179
+ }
180
+ set variant(value) {
181
+ this.write("variant", value);
182
+ }
183
+ /** The `preset` attribute, reflected. */
184
+ get preset() {
185
+ return this.getAttribute("preset");
186
+ }
187
+ set preset(value) {
188
+ this.write("preset", value);
189
+ }
190
+ /** The `points` attribute, reflected. */
191
+ get points() {
192
+ return this.getAttribute("points");
193
+ }
194
+ set points(value) {
195
+ this.write("points", value);
196
+ }
197
+ /** The `cube` attribute, reflected. */
198
+ get cube() {
199
+ return this.getAttribute("cube");
200
+ }
201
+ set cube(value) {
202
+ this.write("cube", value);
203
+ }
204
+ /** The `crawford` attribute, reflected. */
205
+ get crawford() {
206
+ return this.getAttribute("crawford");
207
+ }
208
+ set crawford(value) {
209
+ this.write("crawford", value);
210
+ }
211
+ /** The `jacoby` attribute, reflected. */
212
+ get jacoby() {
213
+ return this.getAttribute("jacoby");
214
+ }
215
+ set jacoby(value) {
216
+ this.write("jacoby", value);
217
+ }
218
+ /** The `gammons` attribute, reflected. */
219
+ get gammons() {
220
+ return this.getAttribute("gammons");
221
+ }
222
+ set gammons(value) {
223
+ this.write("gammons", value);
224
+ }
225
+ /** The `seed` attribute, reflected. */
226
+ get seed() {
227
+ return this.getAttribute("seed");
228
+ }
229
+ set seed(value) {
230
+ this.write("seed", value);
231
+ }
232
+ /** The `dice` attribute, reflected. */
233
+ get dice() {
234
+ return this.getAttribute("dice");
235
+ }
236
+ set dice(value) {
237
+ this.write("dice", value);
238
+ }
239
+ /** The `controls` attribute, reflected. */
240
+ get controls() {
241
+ return this.getAttribute("controls");
242
+ }
243
+ set controls(value) {
244
+ this.write("controls", value);
245
+ }
246
+ /** The `drag` attribute, reflected. */
247
+ get drag() {
248
+ return this.getAttribute("drag");
249
+ }
250
+ set drag(value) {
251
+ this.write("drag", value);
252
+ }
253
+ /** The `auto-end` attribute, reflected. */
254
+ get autoEnd() {
255
+ return this.getAttribute("auto-end");
256
+ }
257
+ set autoEnd(value) {
258
+ this.write("auto-end", value);
259
+ }
260
+ /** The `sound` attribute, reflected. */
261
+ get sound() {
262
+ return this.getAttribute("sound");
263
+ }
264
+ set sound(value) {
265
+ this.write("sound", value);
266
+ }
267
+ /** The `computer-delay` attribute, reflected. */
268
+ get computerDelay() {
269
+ return this.getAttribute("computer-delay");
270
+ }
271
+ set computerDelay(value) {
272
+ this.write("computer-delay", value);
273
+ }
274
+ /** The `white` attribute, reflected. */
275
+ get white() {
276
+ return this.getAttribute("white");
277
+ }
278
+ set white(value) {
279
+ this.write("white", value);
280
+ }
281
+ /** The `black` attribute, reflected. */
282
+ get black() {
283
+ return this.getAttribute("black");
284
+ }
285
+ set black(value) {
286
+ this.write("black", value);
287
+ }
288
+ /** The `board` attribute, reflected. */
289
+ get board() {
290
+ return this.getAttribute("board");
291
+ }
292
+ set board(value) {
293
+ this.write("board", value);
294
+ }
295
+ /** The `checkers` attribute, reflected. */
296
+ get checkers() {
297
+ return this.getAttribute("checkers");
298
+ }
299
+ set checkers(value) {
300
+ this.write("checkers", value);
301
+ }
302
+ /** The `theme` attribute, reflected. */
303
+ get theme() {
304
+ return this.getAttribute("theme");
305
+ }
306
+ set theme(value) {
307
+ this.write("theme", value);
308
+ }
309
+ /** The `home` attribute, reflected. */
310
+ get home() {
311
+ return this.getAttribute("home");
312
+ }
313
+ set home(value) {
314
+ this.write("home", value);
315
+ }
316
+ /** The `view` attribute, reflected. */
317
+ get view() {
318
+ return this.getAttribute("view");
319
+ }
320
+ set view(value) {
321
+ this.write("view", value);
322
+ }
323
+ /** The `numbers` attribute, reflected. */
324
+ get numbers() {
325
+ return this.getAttribute("numbers");
326
+ }
327
+ set numbers(value) {
328
+ this.write("numbers", value);
329
+ }
330
+ /** The `orientation` attribute, reflected. */
331
+ get orientation() {
332
+ return this.getAttribute("orientation");
333
+ }
334
+ set orientation(value) {
335
+ this.write("orientation", value);
336
+ }
337
+ #side(name) {
153
338
  const value = this.getAttribute(name);
154
- if (value !== null)
155
- rules[name] = isOn(value);
156
- }
157
- const seed = this.getAttribute("seed");
158
- const delay = this.getAttribute("computer-delay");
159
- const sound = this.getAttribute("sound");
160
- __classPrivateFieldSet(this, _SugorokuBoard_mount, mountSugoroku(this, {
161
- variant: isVariantKey(variant) ? variant : undefined,
162
- rules,
163
- preset: this.getAttribute("preset") ?? undefined,
164
- seed: seed === null ? undefined : /^\d+$/.test(seed) ? Number(seed) : seed,
165
- dice: this.getAttribute("dice") === "host" ? "host" : "button",
166
- controls: isOn(this.getAttribute("controls") ?? "on"),
167
- drag: isOn(this.getAttribute("drag") ?? "on"),
168
- autoEnd: isOn(this.getAttribute("auto-end")),
169
- sound: sound !== null && isOn(sound === "" ? "on" : sound),
170
- computerDelay: delay !== null && /^\d+$/.test(delay) ? Number(delay) : undefined,
171
- ...__classPrivateFieldGet(this, _SugorokuBoard_instances, "m", _SugorokuBoard_look).call(this),
172
- }), "f");
173
- };
174
- SugorokuBoard.observedAttributes = [...GAME_ATTRIBUTES, "white", "black", "board", "checkers", "theme", "home", "view", "numbers", "orientation", "lang"];
339
+ return value === null ? (this.#options.computer?.[name] ?? null) : (oneOf(value, STRENGTHS) ?? null);
340
+ }
341
+ #look() {
342
+ const view = this.getAttribute("view");
343
+ const language = oneOf(this.getAttribute("lang"), ["en", "ja"]) ?? this.#options.language;
344
+ return {
345
+ board: oneOf(this.getAttribute("board"), SUGOROKU_BOARD_NAMES) ?? this.#options.board,
346
+ checkers: oneOf(this.getAttribute("checkers"), SUGOROKU_CHECKER_SET_NAMES) ??
347
+ this.#options.checkers,
348
+ theme: oneOf(this.getAttribute("theme"), ["auto", "light", "dark"]) ?? this.#options.theme,
349
+ home: oneOf(this.getAttribute("home"), ["left", "right"]) ?? this.#options.home,
350
+ view: view === "white" || view === "black" || view === "auto" ? view : this.#options.view,
351
+ numbers: isOn(this.getAttribute("numbers"), this.#options.numbers ?? false),
352
+ orientation: oneOf(this.getAttribute("orientation"), ["auto", "landscape", "portrait"]) ??
353
+ this.#options.orientation,
354
+ computer: { white: this.#side("white"), black: this.#side("black") },
355
+ language,
356
+ colours: this.#options.colours,
357
+ };
358
+ }
359
+ #refresh() {
360
+ this.frame();
361
+ const key = JSON.stringify([
362
+ GAME_ATTRIBUTES.map((name) => this.getAttribute(name)),
363
+ ...["variant", "rules", "preset", "seed", "dice", "controls", "drag", "autoEnd", "computerDelay", "match"].map((name) => this.#options[name]),
364
+ ]);
365
+ if (key === this.#key && this.#mount !== null) {
366
+ this.#mount.set({
367
+ ...this.#look(),
368
+ names: this.#options.names,
369
+ sound: this.hasAttribute("sound") ? isOn(this.getAttribute("sound")) : this.#options.sound,
370
+ });
371
+ return;
372
+ }
373
+ this.#mount?.destroy();
374
+ this.#mount = null;
375
+ this.#key = "";
376
+ try {
377
+ const variant = this.getAttribute("variant");
378
+ const rules = { ...this.#options.rules };
379
+ const points = this.getAttribute("points");
380
+ if (points !== null && /^\d+$/.test(points))
381
+ rules.points = Number(points);
382
+ for (const name of ["cube", "crawford", "jacoby", "beaver", "gammons"]) {
383
+ const value = this.getAttribute(name);
384
+ if (value !== null)
385
+ rules[name] = isOn(value);
386
+ }
387
+ const seed = this.getAttribute("seed");
388
+ const delay = this.getAttribute("computer-delay");
389
+ const sound = this.getAttribute("sound");
390
+ this.#mount = mountSugoroku(this, {
391
+ ...this.#options,
392
+ variant: isVariantKey(variant) ? variant : this.#options.variant,
393
+ rules,
394
+ preset: this.getAttribute("preset") ?? this.#options.preset,
395
+ seed: seed === null ? this.#options.seed : /^\d+$/.test(seed) ? Number(seed) : seed,
396
+ dice: this.getAttribute("dice") === "host"
397
+ ? "host"
398
+ : this.getAttribute("dice") === "button"
399
+ ? "button"
400
+ : (this.#options.dice ?? "button"),
401
+ controls: isOn(this.getAttribute("controls"), this.#options.controls ?? true),
402
+ drag: isOn(this.getAttribute("drag"), this.#options.drag ?? true),
403
+ autoEnd: isOn(this.getAttribute("auto-end"), this.#options.autoEnd ?? false),
404
+ sound: sound === null ? this.#options.sound : isOn(sound === "" ? "on" : sound),
405
+ computerDelay: delay !== null && /^\d+$/.test(delay) ? Number(delay) : this.#options.computerDelay,
406
+ ...this.#look(),
407
+ onRoll: (detail) => this.#options.onRoll?.(detail),
408
+ onMove: (detail) => this.#options.onMove?.(detail),
409
+ onTurn: (detail) => this.#options.onTurn?.(detail),
410
+ onCube: (detail) => this.#options.onCube?.(detail),
411
+ onEnd: (detail) => this.#options.onEnd?.(detail),
412
+ });
413
+ this.#key = key;
414
+ this.frame();
415
+ this.dispatchEvent(new CustomEvent("sugoroku-load", {
416
+ bubbles: true,
417
+ composed: true,
418
+ detail: JSON.parse(JSON.stringify({ game: this.game, match: this.match })),
419
+ }));
420
+ }
421
+ catch (error) {
422
+ this.#mount?.destroy();
423
+ this.#mount = null;
424
+ this.#key = "";
425
+ const message = error instanceof Error ? error.message : String(error);
426
+ const status = this.ownerDocument.createElement("p");
427
+ status.setAttribute("role", "status");
428
+ status.textContent = message;
429
+ this.replaceChildren(status);
430
+ this.dispatchEvent(new CustomEvent("sugoroku-error", { bubbles: true, composed: true, detail: { message } }));
431
+ }
432
+ }
433
+ }
434
+ /** Defines the default playable board, or a subclass under a custom tag. Server-safe and idempotent.
435
+ * @param tagName - The custom tag name; omitted to use the package default.
436
+ * @param registry - The custom-element registry; omitted outside a browser.
437
+ * @returns Nothing; existing definitions and server imports remain safe.
438
+ * @example
439
+ * ```ts
440
+ * import { defineBoard } from "@johnmorrisdotca/sugoroku/element";
441
+ * defineBoard();
442
+ * console.log(typeof defineBoard); // function
443
+ * ```
444
+ */
445
+ export function defineBoard(tagName = "sugoroku-board", registry = typeof customElements === "undefined" ? undefined : customElements) {
446
+ if (registry !== undefined && registry.get(tagName) === undefined)
447
+ registry.define(tagName, tagName === "sugoroku-board" ? SugorokuBoard : class extends SugorokuBoard {
448
+ });
449
+ }
450
+ export { definePosition, readEmbedPosition, SUGOROKU_DRAW_OPTION_KEYS, SUGOROKU_POSITION_OPTION_KEYS, SugorokuPosition, } from "./positionElement.js";
@@ -0,0 +1,21 @@
1
+ /** The shared frame of `<sugoroku-board>` and `<sugoroku-position>`: size, width, label and the dark-theme rule. */
2
+ declare const ElementBase: typeof HTMLElement;
3
+ export declare const isOn: (value: string | null, fallback?: boolean) => boolean;
4
+ export declare const oneOf: <T extends string>(value: string | null, allowed: readonly T[]) => T | undefined;
5
+ /** The attributes that name the game: changing one starts a new match. */
6
+ export declare const GAME_ATTRIBUTES: readonly ["variant", "preset", "points", "cube", "crawford", "jacoby", "beaver", "gammons", "seed", "dice", "controls", "drag", "auto-end", "sound", "computer-delay"];
7
+ export declare class SugorokuFrame extends ElementBase {
8
+ #private;
9
+ protected frame(): void;
10
+ protected write(name: string, value: unknown): void;
11
+ /** Symbolic display size, reflected. */
12
+ get size(): string | null;
13
+ set size(value: string | null);
14
+ /** Pixel display width, reflected. */
15
+ get width(): string | null;
16
+ set width(value: string | number | null);
17
+ /** Spoken group label, reflected. */
18
+ get label(): string | null;
19
+ set label(value: string | null);
20
+ }
21
+ export {};
@@ -0,0 +1,110 @@
1
+ import { SUGOROKU_STYLE } from "./style.js";
2
+ /** The shared frame of `<sugoroku-board>` and `<sugoroku-position>`: size, width, label and the dark-theme rule. */
3
+ const ElementBase = typeof HTMLElement === "undefined" ? class {
4
+ } : HTMLElement;
5
+ export const isOn = (value, fallback = false) => value === null ? fallback : !["false", "off", "0", "no"].includes(value.trim().toLowerCase());
6
+ export const oneOf = (value, allowed) => allowed.includes(value) ? value : undefined;
7
+ /** The attributes that name the game: changing one starts a new match. */
8
+ export const GAME_ATTRIBUTES = [
9
+ "variant",
10
+ "preset",
11
+ "points",
12
+ "cube",
13
+ "crawford",
14
+ "jacoby",
15
+ "beaver",
16
+ "gammons",
17
+ "seed",
18
+ "dice",
19
+ "controls",
20
+ "drag",
21
+ "auto-end",
22
+ "sound",
23
+ "computer-delay",
24
+ ];
25
+ export class SugorokuFrame extends ElementBase {
26
+ #widthBefore = null;
27
+ #widthOwned = false;
28
+ #labelBefore = null;
29
+ frame() {
30
+ this.style.display = "block";
31
+ this.style.maxWidth = "100%";
32
+ const width = Number(this.getAttribute("width"));
33
+ const size = this.getAttribute("size");
34
+ const pixels = this.hasAttribute("width") && width > 0 && Number.isFinite(width)
35
+ ? Math.min(width, 2000)
36
+ : size === "small"
37
+ ? 320
38
+ : size === "medium"
39
+ ? 480
40
+ : size === "large"
41
+ ? 640
42
+ : null;
43
+ if (pixels !== null) {
44
+ if (!this.#widthOwned)
45
+ this.#widthBefore = this.style.width;
46
+ this.#widthOwned = true;
47
+ this.style.width = `${pixels}px`;
48
+ }
49
+ else if (this.#widthOwned) {
50
+ this.style.width = this.#widthBefore ?? "";
51
+ this.#widthOwned = false;
52
+ }
53
+ const theme = this.getAttribute("theme");
54
+ this.style.colorScheme = theme === "light" || theme === "dark" ? theme : "light dark";
55
+ this.setAttribute("data-sugoroku-embed", "");
56
+ const root = this.getRootNode();
57
+ const target = typeof ShadowRoot !== "undefined" && root instanceof ShadowRoot ? root : this.ownerDocument.head;
58
+ if (target.querySelector("style[data-sugoroku-embed-style]") === null) {
59
+ const style = this.ownerDocument.createElement("style");
60
+ style.setAttribute("data-sugoroku-embed-style", "");
61
+ const palette = SUGOROKU_STYLE.match(/\.sugoroku\[data-theme="dark"\] \{([^}]+)\}/)?.[1] ?? "";
62
+ style.textContent = `@media(prefers-color-scheme:dark){[data-sugoroku-embed] .sugoroku:not([data-theme="light"]){${palette}}}`;
63
+ target.append(style);
64
+ }
65
+ if (this.hasAttribute("label")) {
66
+ this.#labelBefore ??= { role: this.getAttribute("role"), label: this.getAttribute("aria-label") };
67
+ this.setAttribute("role", "group");
68
+ this.setAttribute("aria-label", this.getAttribute("label"));
69
+ }
70
+ else if (this.#labelBefore !== null) {
71
+ for (const [name, value] of [
72
+ ["role", this.#labelBefore.role],
73
+ ["aria-label", this.#labelBefore.label],
74
+ ]) {
75
+ if (value === null)
76
+ this.removeAttribute(name);
77
+ else
78
+ this.setAttribute(name, value);
79
+ }
80
+ this.#labelBefore = null;
81
+ }
82
+ }
83
+ write(name, value) {
84
+ if (value === null || value === undefined)
85
+ this.removeAttribute(name);
86
+ else
87
+ this.setAttribute(name, String(value));
88
+ }
89
+ /** Symbolic display size, reflected. */
90
+ get size() {
91
+ return this.getAttribute("size");
92
+ }
93
+ set size(value) {
94
+ this.write("size", value);
95
+ }
96
+ /** Pixel display width, reflected. */
97
+ get width() {
98
+ return this.getAttribute("width");
99
+ }
100
+ set width(value) {
101
+ this.write("width", value);
102
+ }
103
+ /** Spoken group label, reflected. */
104
+ get label() {
105
+ return this.getAttribute("label");
106
+ }
107
+ set label(value) {
108
+ this.write("label", value);
109
+ }
110
+ }