@mborecki/crossword 0.1.0 → 0.2.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.
- package/dist/cjs/loader.cjs.js +1 -1
- package/dist/cjs/mb-crossword.cjs.entry.js +33 -13
- package/dist/cjs/mb-crossword.cjs.js +1 -1
- package/dist/collection/components/crossword/clue.js +2 -2
- package/dist/collection/components/crossword/mb-crossword.css +48 -16
- package/dist/collection/components/crossword/mb-crossword.js +68 -10
- package/dist/components/mb-crossword.js +1 -1
- package/dist/esm/loader.js +1 -1
- package/dist/esm/mb-crossword.entry.js +33 -13
- package/dist/esm/mb-crossword.js +1 -1
- package/dist/mb-crossword/mb-crossword.esm.js +1 -1
- package/dist/mb-crossword/p-b4b191b9.entry.js +1 -0
- package/dist/types/components/crossword/clue.d.ts +2 -1
- package/dist/types/components/crossword/mb-crossword.d.ts +4 -1
- package/dist/types/components/crossword/types.d.ts +8 -0
- package/dist/types/components.d.ts +9 -0
- package/package.json +1 -1
- package/dist/mb-crossword/p-162bd00d.entry.js +0 -1
package/dist/cjs/loader.cjs.js
CHANGED
|
@@ -6,7 +6,7 @@ var appGlobals = require('./app-globals-V2Kpy_OQ.js');
|
|
|
6
6
|
const defineCustomElements = async (win, options) => {
|
|
7
7
|
if (typeof window === 'undefined') return undefined;
|
|
8
8
|
await appGlobals.globalScripts();
|
|
9
|
-
return index.bootstrapLazy([["mb-crossword.cjs",[[529,"mb-crossword",{"init":[4],"data":[16],"hWords":[32],"vWords":[32],"currentClueId":[32],"currentClue":[32],"isFocused":[32],"selectedCell":[32],"specialBorders":[32],"specialCells":[32],"boardWidth":[32],"boardHeight":[32],"cells":[32],"initGame":[64]},null,{"data":[{"watchData":0}]}]]]], options);
|
|
9
|
+
return index.bootstrapLazy([["mb-crossword.cjs",[[529,"mb-crossword",{"init":[4],"data":[16],"showCluePreview":[4,"show-clue-preview"],"hWords":[32],"vWords":[32],"currentClueId":[32],"currentClue":[32],"isFocused":[32],"selectedCell":[32],"specialBorders":[32],"specialCells":[32],"labels":[32],"boardWidth":[32],"boardHeight":[32],"cells":[32],"initGame":[64],"checkAnswer":[64]},null,{"data":[{"watchData":0}]}]]]], options);
|
|
10
10
|
};
|
|
11
11
|
|
|
12
12
|
exports.setNonce = index.setNonce;
|
|
@@ -3,10 +3,10 @@
|
|
|
3
3
|
var index = require('./index-BlTGwvVC.js');
|
|
4
4
|
var index$1 = require('./index-CEGyCfpa.js');
|
|
5
5
|
|
|
6
|
-
function Clue({ isCurrent, word, onPointerDown, preview }) {
|
|
6
|
+
function Clue({ isCurrent, word, onPointerDown, preview, showPreview }) {
|
|
7
7
|
return index.h("p", { onPointerDown: onPointerDown, part: "clue", class: {
|
|
8
8
|
"--current": isCurrent
|
|
9
|
-
} }, word.clue, index.h("span", { class: "preview" }, preview));
|
|
9
|
+
} }, word.clue, showPreview && index.h("span", { class: "preview" }, preview));
|
|
10
10
|
}
|
|
11
11
|
|
|
12
12
|
function Cell({ data, isInCurrentClue, isSelected, isSpecial, onPointerDown }) {
|
|
@@ -22,7 +22,7 @@ function Cell({ data, isInCurrentClue, isSelected, isSpecial, onPointerDown }) {
|
|
|
22
22
|
} }, index.h("div", { class: "_inner", style: { position: 'absolute', width: '100%' } }, data.value));
|
|
23
23
|
}
|
|
24
24
|
|
|
25
|
-
const mbCrosswordCss = () => `*[part=main]{position:relative;display:grid;grid-template-areas:"board" "clues";grid-template-columns:1fr}@media (min-width: 768px){*[part=main]{grid-template-areas:"clues board" "clues .";grid-template-columns:1fr 1fr}}*[part=main].--focused{background:lightgrey}*[part=main] input.dummy{pointer-events:none;opacity:0.2;z-index:100;grid-area:board}[part=clues]{grid-area:clues;display:grid;grid-template-columns:1fr 1fr}[part=clue].--current{background:lightcyan;position:sticky;bottom:0;left:0;right:0}[part=clue] .preview{font-weight:bold;letter-spacing:0.2em;text-align:center;display:block;text-transform:uppercase}[part=board]{grid-area:board;display:block;position:relative;container-type:size;background:darkgoldenrod;--border:
|
|
25
|
+
const mbCrosswordCss = () => `*[part=main]{position:relative;display:grid;grid-template-areas:"board" "clues";grid-template-columns:1fr}@media (min-width: 768px){*[part=main]{grid-template-areas:"clues board" "clues .";grid-template-columns:1fr 1fr}}*[part=main].--focused{background:lightgrey}*[part=main] input.dummy{pointer-events:none;opacity:0.2;z-index:100;grid-area:board;border:none;outline:none}[part=clues]{grid-area:clues;display:grid;grid-template-columns:1fr 1fr;gap:16px}[part=clues].oneList{grid-template-columns:1fr}[part=clue].--current{background:var(--clue-current-background, lightcyan);position:sticky;bottom:0;left:0;right:0}[part=clue] .preview{font-weight:bold;letter-spacing:0.2em;text-align:center;display:block;text-transform:uppercase}[part=board]{grid-area:board;display:block;position:relative;container-type:size;background:var(--board-background, darkgoldenrod);--cell-border-width:2px;--cell-width-temp:calc((100cqw - var(--cell-border-width)) / var(--board-width, 10));--cell-width:var(--cell-width-temp);--cell-width:round(var(--cell-width-temp), 1px)}[part=cell]{display:block;color:var(--cell-color, #11138d);width:calc(var(--cell-width) - var(--cell-border-width));aspect-ratio:1;background:var(--cell-background, #d9d9d9);position:absolute;container-type:size;top:calc(var(--y, 0) * var(--cell-width));left:calc(var(--x, 0) * var(--cell-width));border:var(--cell-border-width) solid var(--cell-border-color, red)}[part=cell].--alt{background:var(--cell-background-alt, #999)}[part=cell].--in-current-clue{box-shadow:inset 0 0 2px 2px var(--cell-selected-background, lightblue)}[part=cell].--selected{background:var(--cell-selected-background, lightblue)}[part=cell].--special{background:var(--cell-special-background, lightgreen)}[part=cell] ._inner{position:absolute;top:0;left:0;right:0;bottom:0;display:grid;place-content:center;text-transform:uppercase;font-size:80cqh}[part=preview]{display:none}[part=special-border]{display:block;background:black;position:absolute;z-index:100;--s-border:max(2px, var(--cell-border-width))}[part=special-border].left,[part=special-border].right{width:var(--s-border);height:calc(var(--cell-width) - var(--cell-border-width));top:calc(var(--y, 0) * var(--cell-width) + var(--cell-border-width))}[part=special-border].left{left:calc(var(--x, 0) * var(--cell-width) - var(--s-border) / 2)}[part=special-border].right{left:calc((var(--x, 0) + 1) * var(--cell-width) - var(--s-border) / 2)}[part=special-border].top,[part=special-border].bottom{width:calc(var(--cell-width) - var(--cell-border-width));height:var(--s-border);left:calc(var(--x, 0) * var(--cell-width) + var(--cell-border-width))}[part=special-border].top{top:calc(var(--y, 0) * var(--cell-width))}[part=special-border].bottom{top:calc((var(--y, 0) + 1) * var(--cell-width))}[part=label]{display:grid;position:absolute;container-type:size;top:calc(var(--y, 0) * var(--cell-width));left:calc(var(--x, 0) * var(--cell-width));width:calc(var(--cell-width) - var(--cell-border-width));aspect-ratio:1;border:var(--cell-border-width) solid transparent;--label-place-content:center end}[part=label] ._inner{position:absolute;top:var(--label-padding, 10cqh);left:var(--label-padding, 10cqh);right:var(--label-padding, 10cqh);bottom:var(--label-padding, 10cqh);display:grid;place-content:var(--label-place-content, center);text-transform:uppercase;font-size:var(--label-font-size, 60cqh)}`;
|
|
26
26
|
|
|
27
27
|
const MBCrossword = class {
|
|
28
28
|
constructor(hostRef) {
|
|
@@ -30,6 +30,7 @@ const MBCrossword = class {
|
|
|
30
30
|
}
|
|
31
31
|
init = true;
|
|
32
32
|
data;
|
|
33
|
+
showCluePreview = true;
|
|
33
34
|
textInput;
|
|
34
35
|
hWords = [];
|
|
35
36
|
vWords = [];
|
|
@@ -75,10 +76,20 @@ const MBCrossword = class {
|
|
|
75
76
|
});
|
|
76
77
|
this.specialBorders = data.specialBorders ?? [];
|
|
77
78
|
this.specialCells = data.specialCells ?? [];
|
|
79
|
+
this.labels = data.labels ?? [];
|
|
78
80
|
this.buildBoard();
|
|
79
81
|
}
|
|
82
|
+
async checkAnswer() {
|
|
83
|
+
const cells = this.cells.filter(c => !c.isBlocked);
|
|
84
|
+
console.log(cells);
|
|
85
|
+
cells.forEach(c => {
|
|
86
|
+
console.log(`${c.value} === ${c.answer}`, `${c.x},${c.y}`, c.value === c.answer);
|
|
87
|
+
});
|
|
88
|
+
return cells.every(c => c.value === c.answer);
|
|
89
|
+
}
|
|
80
90
|
specialBorders = [];
|
|
81
91
|
specialCells = [];
|
|
92
|
+
labels = [];
|
|
82
93
|
boardWidth = 0;
|
|
83
94
|
boardHeight = 0;
|
|
84
95
|
cells = [];
|
|
@@ -113,6 +124,7 @@ const MBCrossword = class {
|
|
|
113
124
|
const v = index$1.Vec2.from(w);
|
|
114
125
|
const cellIndex = index$1.indexFromXY(new index$1.Vec2(v.x + i, v.y), boardWidth);
|
|
115
126
|
cells[cellIndex].isBlocked = false;
|
|
127
|
+
cells[cellIndex].answer = w.word[i];
|
|
116
128
|
}
|
|
117
129
|
}
|
|
118
130
|
if (w.orientation === 'vertical') {
|
|
@@ -120,6 +132,7 @@ const MBCrossword = class {
|
|
|
120
132
|
const v = index$1.Vec2.from(w);
|
|
121
133
|
const cellIndex = index$1.indexFromXY(new index$1.Vec2(v.x, v.y + i), boardWidth);
|
|
122
134
|
cells[cellIndex].isBlocked = false;
|
|
135
|
+
cells[cellIndex].answer = w.word[i];
|
|
123
136
|
}
|
|
124
137
|
}
|
|
125
138
|
});
|
|
@@ -340,9 +353,6 @@ const MBCrossword = class {
|
|
|
340
353
|
this.selectClue(nextClue);
|
|
341
354
|
}
|
|
342
355
|
}
|
|
343
|
-
// if (isKeyboardEventLetter(event)) {
|
|
344
|
-
// this.inputLetter(event.key.toLowerCase()[0]);
|
|
345
|
-
// }
|
|
346
356
|
if (this.selectedCell) {
|
|
347
357
|
switch (event.key) {
|
|
348
358
|
case 'ArrowLeft':
|
|
@@ -374,15 +384,20 @@ const MBCrossword = class {
|
|
|
374
384
|
this.textInput.value = '';
|
|
375
385
|
}
|
|
376
386
|
render() {
|
|
377
|
-
|
|
387
|
+
const hasVWords = Boolean(this.vWords.length);
|
|
388
|
+
const hasHWords = Boolean(this.hWords.length);
|
|
389
|
+
const twoLists = hasHWords && hasVWords;
|
|
390
|
+
return index.h("div", { key: '87d88806305c6102eeb76c3984d4b9c2ad2d9759', part: "main", class: {
|
|
378
391
|
"--focused": this.isFocused
|
|
379
|
-
}, onFocusin: this.onFocusin.bind(this), onFocusout: this.onFocusOut.bind(this) }, index.h("input", { key: '
|
|
392
|
+
}, onFocusin: this.onFocusin.bind(this), onFocusout: this.onFocusOut.bind(this) }, index.h("input", { key: '49cdb053a4d2c4ad2c7eb881488b18c68e31fe65', type: "text", class: "dummy", ref: (el) => this.textInput = el, onKeyDown: this.onKeyPress.bind(this), onInput: this.onInput.bind(this) }), index.h("div", { key: '0ca71a907f70e17a6d2d10bff9462bd0b2284007', part: "clues", class: {
|
|
393
|
+
twoLists, oneList: !twoLists
|
|
394
|
+
} }, hasHWords ? index.h("div", { part: "clue-list" }, this.hWords.map(w => {
|
|
380
395
|
const isCurrent = w.id === this.currentClueId;
|
|
381
|
-
return index.h(Clue, { preview: this.getCluePreview(w), word: w, isCurrent: isCurrent, onPointerDown: () => this.selectClue(w.id) });
|
|
382
|
-
})), index.h("div", {
|
|
396
|
+
return index.h(Clue, { showPreview: this.showCluePreview, preview: this.getCluePreview(w), word: w, isCurrent: isCurrent, onPointerDown: () => this.selectClue(w.id) });
|
|
397
|
+
})) : '', hasVWords ? index.h("div", { part: "clue-list" }, this.vWords.map(w => {
|
|
383
398
|
const isCurrent = w.id === this.currentClueId;
|
|
384
|
-
return index.h(Clue, { preview: this.getCluePreview(w), word: w, isCurrent: isCurrent, onPointerDown: () => this.selectClue(w.id) });
|
|
385
|
-
}))), index.h("div", { key: '
|
|
399
|
+
return index.h(Clue, { showPreview: this.showCluePreview, preview: this.getCluePreview(w), word: w, isCurrent: isCurrent, onPointerDown: () => this.selectClue(w.id) });
|
|
400
|
+
})) : ''), index.h("div", { key: 'ea16dca25fe7884d25f9748f98740ad83e25293e', part: "board", style: {
|
|
386
401
|
'--board-width': `${this.boardWidth}`,
|
|
387
402
|
'grid-template-columns': `repeat(${this.boardWidth}, 1fr)`,
|
|
388
403
|
aspectRatio: `${this.boardWidth / this.boardHeight}`
|
|
@@ -401,7 +416,12 @@ const MBCrossword = class {
|
|
|
401
416
|
"--x": `${b.x}`,
|
|
402
417
|
"--y": `${b.y}`
|
|
403
418
|
} });
|
|
404
|
-
})
|
|
419
|
+
}), this.labels.map((label) => {
|
|
420
|
+
return index.h("div", { part: "label", style: {
|
|
421
|
+
"--x": `${label.x}`,
|
|
422
|
+
"--y": `${label.y}`
|
|
423
|
+
}, onPointerDown: () => this.selectClue(label.clueId) }, index.h("div", { class: "_inner" }, label.text));
|
|
424
|
+
})), index.h("div", { key: 'd391413e7be3dee148f2f5e6d8fbf22742278a0c', part: "preview" }, "Tu bedzie podgl\u0105d: ", this.currentClueId));
|
|
405
425
|
}
|
|
406
426
|
static get delegatesFocus() { return true; }
|
|
407
427
|
static get watchers() { return {
|
|
@@ -19,7 +19,7 @@ var patchBrowser = () => {
|
|
|
19
19
|
|
|
20
20
|
patchBrowser().then(async (options) => {
|
|
21
21
|
await appGlobals.globalScripts();
|
|
22
|
-
return index.bootstrapLazy([["mb-crossword.cjs",[[529,"mb-crossword",{"init":[4],"data":[16],"hWords":[32],"vWords":[32],"currentClueId":[32],"currentClue":[32],"isFocused":[32],"selectedCell":[32],"specialBorders":[32],"specialCells":[32],"boardWidth":[32],"boardHeight":[32],"cells":[32],"initGame":[64]},null,{"data":[{"watchData":0}]}]]]], options);
|
|
22
|
+
return index.bootstrapLazy([["mb-crossword.cjs",[[529,"mb-crossword",{"init":[4],"data":[16],"showCluePreview":[4,"show-clue-preview"],"hWords":[32],"vWords":[32],"currentClueId":[32],"currentClue":[32],"isFocused":[32],"selectedCell":[32],"specialBorders":[32],"specialCells":[32],"labels":[32],"boardWidth":[32],"boardHeight":[32],"cells":[32],"initGame":[64],"checkAnswer":[64]},null,{"data":[{"watchData":0}]}]]]], options);
|
|
23
23
|
});
|
|
24
24
|
|
|
25
25
|
exports.setNonce = index.setNonce;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { h } from "@stencil/core";
|
|
2
|
-
export function Clue({ isCurrent, word, onPointerDown, preview }) {
|
|
2
|
+
export function Clue({ isCurrent, word, onPointerDown, preview, showPreview }) {
|
|
3
3
|
return h("p", { onPointerDown: onPointerDown, part: "clue", class: {
|
|
4
4
|
"--current": isCurrent
|
|
5
|
-
} }, word.clue, h("span", { class: "preview" }, preview));
|
|
5
|
+
} }, word.clue, showPreview && h("span", { class: "preview" }, preview));
|
|
6
6
|
}
|
|
@@ -18,16 +18,22 @@
|
|
|
18
18
|
opacity: 0.2;
|
|
19
19
|
z-index: 100;
|
|
20
20
|
grid-area: board;
|
|
21
|
+
border: none;
|
|
22
|
+
outline: none;
|
|
21
23
|
}
|
|
22
24
|
|
|
23
25
|
[part=clues] {
|
|
24
26
|
grid-area: clues;
|
|
25
27
|
display: grid;
|
|
26
28
|
grid-template-columns: 1fr 1fr;
|
|
29
|
+
gap: 16px;
|
|
30
|
+
}
|
|
31
|
+
[part=clues].oneList {
|
|
32
|
+
grid-template-columns: 1fr;
|
|
27
33
|
}
|
|
28
34
|
|
|
29
35
|
[part=clue].--current {
|
|
30
|
-
background: lightcyan;
|
|
36
|
+
background: var(--clue-current-background, lightcyan);
|
|
31
37
|
position: sticky;
|
|
32
38
|
bottom: 0;
|
|
33
39
|
left: 0;
|
|
@@ -46,33 +52,36 @@
|
|
|
46
52
|
display: block;
|
|
47
53
|
position: relative;
|
|
48
54
|
container-type: size;
|
|
49
|
-
background: darkgoldenrod;
|
|
50
|
-
--border:
|
|
51
|
-
--cell-width: calc((100cqw - var(--border)) / var(--board-width, 10));
|
|
55
|
+
background: var(--board-background, darkgoldenrod);
|
|
56
|
+
--cell-border-width: 2px;
|
|
57
|
+
--cell-width-temp: calc((100cqw - var(--cell-border-width)) / var(--board-width, 10));
|
|
58
|
+
--cell-width: var(--cell-width-temp);
|
|
59
|
+
--cell-width: round(var(--cell-width-temp), 1px);
|
|
52
60
|
}
|
|
53
61
|
|
|
54
62
|
[part=cell] {
|
|
55
63
|
display: block;
|
|
56
|
-
|
|
64
|
+
color: var(--cell-color, #11138d);
|
|
65
|
+
width: calc(var(--cell-width) - var(--cell-border-width));
|
|
57
66
|
aspect-ratio: 1;
|
|
58
|
-
background: #d9d9d9;
|
|
67
|
+
background: var(--cell-background, #d9d9d9);
|
|
59
68
|
position: absolute;
|
|
60
69
|
container-type: size;
|
|
61
70
|
top: calc(var(--y, 0) * var(--cell-width));
|
|
62
71
|
left: calc(var(--x, 0) * var(--cell-width));
|
|
63
|
-
border: var(--border) solid red;
|
|
72
|
+
border: var(--cell-border-width) solid var(--cell-border-color, red);
|
|
64
73
|
}
|
|
65
74
|
[part=cell].--alt {
|
|
66
|
-
background: #999;
|
|
75
|
+
background: var(--cell-background-alt, #999);
|
|
67
76
|
}
|
|
68
77
|
[part=cell].--in-current-clue {
|
|
69
|
-
box-shadow: inset 0 0 2px 2px lightblue;
|
|
78
|
+
box-shadow: inset 0 0 2px 2px var(--cell-selected-background, lightblue);
|
|
70
79
|
}
|
|
71
80
|
[part=cell].--selected {
|
|
72
|
-
background: lightblue;
|
|
81
|
+
background: var(--cell-selected-background, lightblue);
|
|
73
82
|
}
|
|
74
83
|
[part=cell].--special {
|
|
75
|
-
background: lightgreen;
|
|
84
|
+
background: var(--cell-special-background, lightgreen);
|
|
76
85
|
}
|
|
77
86
|
[part=cell] ._inner {
|
|
78
87
|
position: absolute;
|
|
@@ -95,12 +104,12 @@
|
|
|
95
104
|
background: black;
|
|
96
105
|
position: absolute;
|
|
97
106
|
z-index: 100;
|
|
98
|
-
--s-border: max(2px, var(--border));
|
|
107
|
+
--s-border: max(2px, var(--cell-border-width));
|
|
99
108
|
}
|
|
100
109
|
[part=special-border].left, [part=special-border].right {
|
|
101
110
|
width: var(--s-border);
|
|
102
|
-
height: calc(var(--cell-width) - var(--border));
|
|
103
|
-
top: calc(var(--y, 0) * var(--cell-width) + var(--border));
|
|
111
|
+
height: calc(var(--cell-width) - var(--cell-border-width));
|
|
112
|
+
top: calc(var(--y, 0) * var(--cell-width) + var(--cell-border-width));
|
|
104
113
|
}
|
|
105
114
|
[part=special-border].left {
|
|
106
115
|
left: calc(var(--x, 0) * var(--cell-width) - var(--s-border) / 2);
|
|
@@ -109,13 +118,36 @@
|
|
|
109
118
|
left: calc((var(--x, 0) + 1) * var(--cell-width) - var(--s-border) / 2);
|
|
110
119
|
}
|
|
111
120
|
[part=special-border].top, [part=special-border].bottom {
|
|
112
|
-
width: calc(var(--cell-width) - var(--border));
|
|
121
|
+
width: calc(var(--cell-width) - var(--cell-border-width));
|
|
113
122
|
height: var(--s-border);
|
|
114
|
-
left: calc(var(--x, 0) * var(--cell-width) + var(--border));
|
|
123
|
+
left: calc(var(--x, 0) * var(--cell-width) + var(--cell-border-width));
|
|
115
124
|
}
|
|
116
125
|
[part=special-border].top {
|
|
117
126
|
top: calc(var(--y, 0) * var(--cell-width));
|
|
118
127
|
}
|
|
119
128
|
[part=special-border].bottom {
|
|
120
129
|
top: calc((var(--y, 0) + 1) * var(--cell-width));
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
[part=label] {
|
|
133
|
+
display: grid;
|
|
134
|
+
position: absolute;
|
|
135
|
+
container-type: size;
|
|
136
|
+
top: calc(var(--y, 0) * var(--cell-width));
|
|
137
|
+
left: calc(var(--x, 0) * var(--cell-width));
|
|
138
|
+
width: calc(var(--cell-width) - var(--cell-border-width));
|
|
139
|
+
aspect-ratio: 1;
|
|
140
|
+
border: var(--cell-border-width) solid transparent;
|
|
141
|
+
--label-place-content: center end;
|
|
142
|
+
}
|
|
143
|
+
[part=label] ._inner {
|
|
144
|
+
position: absolute;
|
|
145
|
+
top: var(--label-padding, 10cqh);
|
|
146
|
+
left: var(--label-padding, 10cqh);
|
|
147
|
+
right: var(--label-padding, 10cqh);
|
|
148
|
+
bottom: var(--label-padding, 10cqh);
|
|
149
|
+
display: grid;
|
|
150
|
+
place-content: var(--label-place-content, center);
|
|
151
|
+
text-transform: uppercase;
|
|
152
|
+
font-size: var(--label-font-size, 60cqh);
|
|
121
153
|
}
|
|
@@ -6,6 +6,7 @@ import { Vec2 } from "@mb-puzzle/vec2";
|
|
|
6
6
|
export class MBCrossword {
|
|
7
7
|
init = true;
|
|
8
8
|
data;
|
|
9
|
+
showCluePreview = true;
|
|
9
10
|
textInput;
|
|
10
11
|
hWords = [];
|
|
11
12
|
vWords = [];
|
|
@@ -51,10 +52,20 @@ export class MBCrossword {
|
|
|
51
52
|
});
|
|
52
53
|
this.specialBorders = data.specialBorders ?? [];
|
|
53
54
|
this.specialCells = data.specialCells ?? [];
|
|
55
|
+
this.labels = data.labels ?? [];
|
|
54
56
|
this.buildBoard();
|
|
55
57
|
}
|
|
58
|
+
async checkAnswer() {
|
|
59
|
+
const cells = this.cells.filter(c => !c.isBlocked);
|
|
60
|
+
console.log(cells);
|
|
61
|
+
cells.forEach(c => {
|
|
62
|
+
console.log(`${c.value} === ${c.answer}`, `${c.x},${c.y}`, c.value === c.answer);
|
|
63
|
+
});
|
|
64
|
+
return cells.every(c => c.value === c.answer);
|
|
65
|
+
}
|
|
56
66
|
specialBorders = [];
|
|
57
67
|
specialCells = [];
|
|
68
|
+
labels = [];
|
|
58
69
|
boardWidth = 0;
|
|
59
70
|
boardHeight = 0;
|
|
60
71
|
cells = [];
|
|
@@ -89,6 +100,7 @@ export class MBCrossword {
|
|
|
89
100
|
const v = Vec2.from(w);
|
|
90
101
|
const cellIndex = indexFromXY(new Vec2(v.x + i, v.y), boardWidth);
|
|
91
102
|
cells[cellIndex].isBlocked = false;
|
|
103
|
+
cells[cellIndex].answer = w.word[i];
|
|
92
104
|
}
|
|
93
105
|
}
|
|
94
106
|
if (w.orientation === 'vertical') {
|
|
@@ -96,6 +108,7 @@ export class MBCrossword {
|
|
|
96
108
|
const v = Vec2.from(w);
|
|
97
109
|
const cellIndex = indexFromXY(new Vec2(v.x, v.y + i), boardWidth);
|
|
98
110
|
cells[cellIndex].isBlocked = false;
|
|
111
|
+
cells[cellIndex].answer = w.word[i];
|
|
99
112
|
}
|
|
100
113
|
}
|
|
101
114
|
});
|
|
@@ -316,9 +329,6 @@ export class MBCrossword {
|
|
|
316
329
|
this.selectClue(nextClue);
|
|
317
330
|
}
|
|
318
331
|
}
|
|
319
|
-
// if (isKeyboardEventLetter(event)) {
|
|
320
|
-
// this.inputLetter(event.key.toLowerCase()[0]);
|
|
321
|
-
// }
|
|
322
332
|
if (this.selectedCell) {
|
|
323
333
|
switch (event.key) {
|
|
324
334
|
case 'ArrowLeft':
|
|
@@ -350,15 +360,20 @@ export class MBCrossword {
|
|
|
350
360
|
this.textInput.value = '';
|
|
351
361
|
}
|
|
352
362
|
render() {
|
|
353
|
-
|
|
363
|
+
const hasVWords = Boolean(this.vWords.length);
|
|
364
|
+
const hasHWords = Boolean(this.hWords.length);
|
|
365
|
+
const twoLists = hasHWords && hasVWords;
|
|
366
|
+
return h("div", { key: '87d88806305c6102eeb76c3984d4b9c2ad2d9759', part: "main", class: {
|
|
354
367
|
"--focused": this.isFocused
|
|
355
|
-
}, onFocusin: this.onFocusin.bind(this), onFocusout: this.onFocusOut.bind(this) }, h("input", { key: '
|
|
368
|
+
}, onFocusin: this.onFocusin.bind(this), onFocusout: this.onFocusOut.bind(this) }, h("input", { key: '49cdb053a4d2c4ad2c7eb881488b18c68e31fe65', type: "text", class: "dummy", ref: (el) => this.textInput = el, onKeyDown: this.onKeyPress.bind(this), onInput: this.onInput.bind(this) }), h("div", { key: '0ca71a907f70e17a6d2d10bff9462bd0b2284007', part: "clues", class: {
|
|
369
|
+
twoLists, oneList: !twoLists
|
|
370
|
+
} }, hasHWords ? h("div", { part: "clue-list" }, this.hWords.map(w => {
|
|
356
371
|
const isCurrent = w.id === this.currentClueId;
|
|
357
|
-
return h(Clue, { preview: this.getCluePreview(w), word: w, isCurrent: isCurrent, onPointerDown: () => this.selectClue(w.id) });
|
|
358
|
-
})), h("div", {
|
|
372
|
+
return h(Clue, { showPreview: this.showCluePreview, preview: this.getCluePreview(w), word: w, isCurrent: isCurrent, onPointerDown: () => this.selectClue(w.id) });
|
|
373
|
+
})) : '', hasVWords ? h("div", { part: "clue-list" }, this.vWords.map(w => {
|
|
359
374
|
const isCurrent = w.id === this.currentClueId;
|
|
360
|
-
return h(Clue, { preview: this.getCluePreview(w), word: w, isCurrent: isCurrent, onPointerDown: () => this.selectClue(w.id) });
|
|
361
|
-
}))), h("div", { key: '
|
|
375
|
+
return h(Clue, { showPreview: this.showCluePreview, preview: this.getCluePreview(w), word: w, isCurrent: isCurrent, onPointerDown: () => this.selectClue(w.id) });
|
|
376
|
+
})) : ''), h("div", { key: 'ea16dca25fe7884d25f9748f98740ad83e25293e', part: "board", style: {
|
|
362
377
|
'--board-width': `${this.boardWidth}`,
|
|
363
378
|
'grid-template-columns': `repeat(${this.boardWidth}, 1fr)`,
|
|
364
379
|
aspectRatio: `${this.boardWidth / this.boardHeight}`
|
|
@@ -377,7 +392,12 @@ export class MBCrossword {
|
|
|
377
392
|
"--x": `${b.x}`,
|
|
378
393
|
"--y": `${b.y}`
|
|
379
394
|
} });
|
|
380
|
-
})
|
|
395
|
+
}), this.labels.map((label) => {
|
|
396
|
+
return h("div", { part: "label", style: {
|
|
397
|
+
"--x": `${label.x}`,
|
|
398
|
+
"--y": `${label.y}`
|
|
399
|
+
}, onPointerDown: () => this.selectClue(label.clueId) }, h("div", { class: "_inner" }, label.text));
|
|
400
|
+
})), h("div", { key: 'd391413e7be3dee148f2f5e6d8fbf22742278a0c', part: "preview" }, "Tu bedzie podgl\u0105d: ", this.currentClueId));
|
|
381
401
|
}
|
|
382
402
|
static get is() { return "mb-crossword"; }
|
|
383
403
|
static get encapsulation() { return "shadow"; }
|
|
@@ -437,6 +457,26 @@ export class MBCrossword {
|
|
|
437
457
|
},
|
|
438
458
|
"getter": false,
|
|
439
459
|
"setter": false
|
|
460
|
+
},
|
|
461
|
+
"showCluePreview": {
|
|
462
|
+
"type": "boolean",
|
|
463
|
+
"mutable": false,
|
|
464
|
+
"complexType": {
|
|
465
|
+
"original": "boolean",
|
|
466
|
+
"resolved": "boolean",
|
|
467
|
+
"references": {}
|
|
468
|
+
},
|
|
469
|
+
"required": false,
|
|
470
|
+
"optional": false,
|
|
471
|
+
"docs": {
|
|
472
|
+
"tags": [],
|
|
473
|
+
"text": ""
|
|
474
|
+
},
|
|
475
|
+
"getter": false,
|
|
476
|
+
"setter": false,
|
|
477
|
+
"reflect": false,
|
|
478
|
+
"attribute": "show-clue-preview",
|
|
479
|
+
"defaultValue": "true"
|
|
440
480
|
}
|
|
441
481
|
};
|
|
442
482
|
}
|
|
@@ -450,6 +490,7 @@ export class MBCrossword {
|
|
|
450
490
|
"selectedCell": {},
|
|
451
491
|
"specialBorders": {},
|
|
452
492
|
"specialCells": {},
|
|
493
|
+
"labels": {},
|
|
453
494
|
"boardWidth": {},
|
|
454
495
|
"boardHeight": {},
|
|
455
496
|
"cells": {}
|
|
@@ -483,6 +524,23 @@ export class MBCrossword {
|
|
|
483
524
|
"text": "",
|
|
484
525
|
"tags": []
|
|
485
526
|
}
|
|
527
|
+
},
|
|
528
|
+
"checkAnswer": {
|
|
529
|
+
"complexType": {
|
|
530
|
+
"signature": "() => Promise<boolean>",
|
|
531
|
+
"parameters": [],
|
|
532
|
+
"references": {
|
|
533
|
+
"Promise": {
|
|
534
|
+
"location": "global",
|
|
535
|
+
"id": "global::Promise"
|
|
536
|
+
}
|
|
537
|
+
},
|
|
538
|
+
"return": "Promise<boolean>"
|
|
539
|
+
},
|
|
540
|
+
"docs": {
|
|
541
|
+
"text": "",
|
|
542
|
+
"tags": []
|
|
543
|
+
}
|
|
486
544
|
}
|
|
487
545
|
};
|
|
488
546
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import{h as t,t as e,p as i,H as s,Vec2fromIndex as r,V as l,f as o,isInputEventLetter as a,indexFromXY as n}from"./index.js";function c({isCurrent:e,word:i,onPointerDown:s,preview:r}){return t("p",{onPointerDown:s,part:"clue",class:{"--current":e}},i.clue,t("span",{class:"preview"},r))}function h({data:e,isInCurrentClue:i,isSelected:s,isSpecial:r,onPointerDown:l}){return t("div",{onPointerDown:l,part:"cell",class:{"--blocked":e.isBlocked,"--in-current-clue":i,"--selected":s,"--alt":!((e.x+e.y)%2),"--special":r},style:{"--x":""+e.x,"--y":""+e.y}},t("div",{class:"_inner",style:{position:"absolute",width:"100%"}},e.value))}const d=i(class extends s{constructor(t){super(),!1!==t&&this.__registerHost(),this.__attachShadow()}init=!0;data;textInput;hWords=[];vWords=[];currentClueId=null;currentClue=null;isFocused=!1;selectedCell=null;componentWillLoad(){console.log("componentWillLoad",this.init,this.data),this.init&&this.data&&this.initGame()}watchData(){this.init&&this.data&&this.initGame()}get allWords(){return[...this.hWords,...this.vWords]}async initGame(t=this.data){if(console.log("initGame"),!t.words)throw Error("Words definition missing");this.hWords=[],this.vWords=[],t.words.forEach(((t,e)=>{const i=Math.random();void 0===t.id&&(t.id=`${i}_${e}`),"horizontal"===t.orientation&&(this.hWords=[...this.hWords,t]),"vertical"===t.orientation&&(this.vWords=[...this.vWords,t])})),this.specialBorders=t.specialBorders??[],this.specialCells=t.specialCells??[],this.buildBoard()}specialBorders=[];specialCells=[];boardWidth=0;boardHeight=0;cells=[];buildBoard(){let t=0,e=0;this.allWords.forEach((i=>{"horizontal"===i.orientation&&(t=Math.max(t,i.x+i.word.length),e=Math.max(e,i.y+1)),"vertical"===i.orientation&&(t=Math.max(t,i.x+1),e=Math.max(e,i.y+i.word.length))}));const i=Array(e*t).fill(null).map(((e,i)=>{const[s,l]=r(i,t);return{index:i,x:s,y:l,value:null,isCurrent:!1,isInCurrentWord:!1,isBlocked:!0}}));this.allWords.forEach((e=>{if("horizontal"===e.orientation)for(let s=0;s<e.word.length;s++){const r=l.from(e),o=n(new l(r.x+s,r.y),t);i[o].isBlocked=!1}if("vertical"===e.orientation)for(let s=0;s<e.word.length;s++){const r=l.from(e),o=n(new l(r.x,r.y+s),t);i[o].isBlocked=!1}})),this.cells=i,this.boardWidth=t,this.boardHeight=e}getClueById(t){return this.allWords.find((e=>e.id===t))??null}isInClue(t,e=this.currentClue){return null!==e&&(t.x===e.x||t.y===e.y)&&("horizontal"===e.orientation?!(t.y!==e.y||t.x<e.x||t.x>=e.x+e.word.length):"vertical"===e.orientation?!(t.x!==e.x||t.y<e.y||t.y>=e.y+e.word.length):void 0)}isSelectedCell(t){return!!this.selectedCell&&t.x===this.selectedCell.x&&t.y===this.selectedCell.y}getNextClueId(){if(!this.currentClueId)return this.allWords[0]?.id??null;const t=this.allWords.findIndex((t=>t.id===this.currentClueId));return t<0?this.allWords[0]?.id??null:this.allWords[t+1]?.id??null}getPrevClueId(){if(!this.currentClueId)return this.allWords[0]?.id??null;const t=this.allWords.findIndex((t=>t.id===this.currentClueId));return t<0?this.allWords[0]?.id??null:this.allWords[t-1]?.id??null}inputLetter(t){this.selectedCell&&(this.selectedCell.value=t,this.selectNextCell(),o(this))}backspace(){this.selectedCell&&(this.selectedCell.value?(this.selectedCell.value="",this.selectPrevCell()):(this.selectPrevCell(),this.selectedCell.value=""),o(this))}selectNextCell(){if(!this.selectedCell||!this.currentClue)return;const t=this.currentClue.orientation;if(function(t,e){return u(t).eq(l.from(e))}(this.currentClue,this.selectedCell)){const t=this.getNextClueId(),e=this.getClueById(t);return e?void this.selectClueByXY(l.from(e)):void 0}"horizontal"===t&&this.selectClueByXY(new l(this.selectedCell.x+1,this.selectedCell.y)),"vertical"===t&&this.selectClueByXY(new l(this.selectedCell.x,this.selectedCell.y+1))}selectPrevCell(){if(!this.selectedCell||!this.currentClue)return;const t=this.currentClue.orientation;if(function(t,e){return l.from(t).eq(l.from(e))}(this.currentClue,this.selectedCell)){const t=this.getPrevClueId(),e=this.getClueById(t);if(e){const t=u(e);return console.log(t),void this.selectClueByXY(t)}}"horizontal"===t&&this.selectClueByXY(new l(this.selectedCell.x-1,this.selectedCell.y)),"vertical"===t&&this.selectClueByXY(new l(this.selectedCell.x,this.selectedCell.y-1))}onFocusin(){this.isFocused=!0,this.currentClueId||(this.currentClueId=this.allWords[0]?.id??null),this.textInput.focus()}onFocusOut(){this.isFocused=!1}selectClue(t,{row:e,col:i}={}){if(!t)return;this.currentClueId=t;const s=this.allWords.find((e=>e.id===t));s||(this.currentClueId=null,this.currentClue=null),this.currentClue=s;const r=[s.x,s.y];void 0!==e&&"vertical"===s.orientation&&e>=s.y&&e<s.y+s.word.length&&(r[1]=e),void 0!==i&&"horizontal"===s.orientation&&i>=s.x&&i<s.x+s.word.length&&(r[0]=i),this.selectCellByXY(l.from(r))}selectCellByXY(t){const e=n(t,this.boardWidth);this.selectedCell=this.cells[e]??null}findNonblockedCell(t,e){const i=new l(t[0]+e[0],t[1]+e[1]),s=this.boardWidth*this.boardHeight;let r=0;for(;this.isCellBlocked(i)&&r<s;)r++,i[0]+=e[0],i[1]+=e[1],i[0]<0&&(i[0]=this.boardWidth-1),i[0]>=this.boardWidth&&(i[0]=0),i[1]<0&&(i[1]=this.boardHeight-1),i[1]>=this.boardHeight&&(i[1]=0);return i}isCellBlocked(t){const e=this.cells[n(t,this.boardWidth)];return e?.isBlocked??!0}selectClueByXY(t){const e=this.allWords.filter((e=>this.isInClue(t,e)));if(e.length)if(this.selectedCell&&2===e.length&&t.eq(l.from(this.selectedCell))){const i=e.find((t=>t.orientation!==this.currentClue?.orientation));this.selectClue(i?.id??e[0].id,{row:t.y,col:t.x})}else{const i=e.find((t=>t.orientation===this.currentClue?.orientation));this.selectClue(i?.id??e[0].id,{row:t.y,col:t.x})}}getCluePreview(t){if(!t)return"_";const e=Array(t.word.length).fill("_");for(let i=0;i<t.word.length;i++){const s=new l(t.x,t.y).add("vertical"===t.orientation?new l(0,1*i):new l(1*i,0)),r=this.cells[n(s,this.boardWidth)];r.value&&(e[i]=r.value)}return e.join("")}onKeyPress(t){if(console.log(t),"Tab"===t.key){const e=t.shiftKey?this.getPrevClueId():this.getNextClueId();e&&(t.preventDefault(),this.selectClue(e))}if(this.selectedCell)switch(t.key){case"ArrowLeft":return this.selectClueByXY(this.findNonblockedCell(new l(this.selectedCell.x,this.selectedCell.y),new l(-1,0))),void t.preventDefault();case"ArrowRight":return this.selectClueByXY(this.findNonblockedCell(new l(this.selectedCell.x,this.selectedCell.y),new l(1,0))),void t.preventDefault();case"ArrowUp":return this.selectClueByXY(this.findNonblockedCell(new l(this.selectedCell.x,this.selectedCell.y),new l(0,-1))),void t.preventDefault();case"ArrowDown":return this.selectClueByXY(this.findNonblockedCell(new l(this.selectedCell.x,this.selectedCell.y),new l(0,1))),void t.preventDefault();case"Backspace":console.log("BACKSPACE!"),this.backspace()}}onInput(t){a(t)&&this.inputLetter(t.data.toLowerCase()[0]),this.textInput.value=""}render(){return t("div",{key:"1dec8e0d58c347be7a44c19c416d3940c37ccf0c",part:"main",class:{"--focused":this.isFocused},onFocusin:this.onFocusin.bind(this),onFocusout:this.onFocusOut.bind(this)},t("input",{key:"40c9f176b21072bcd084871b9caff7add03c9680",type:"text",class:"dummy",ref:t=>this.textInput=t,onKeyDown:this.onKeyPress.bind(this),onInput:this.onInput.bind(this)}),t("div",{key:"14eb06bad93227192ea299ecd27f12cf58304cea",part:"clues"},t("div",{key:"e58b0150ff780acd6f770b009d3936a3a7195138",part:"clue-list"},this.hWords.map((e=>{const i=e.id===this.currentClueId;return t(c,{preview:this.getCluePreview(e),word:e,isCurrent:i,onPointerDown:()=>this.selectClue(e.id)})}))),t("div",{key:"13c5ca874d95561654096aa09d5b715576d4392d",part:"clue-list"},this.vWords.map((e=>{const i=e.id===this.currentClueId;return t(c,{preview:this.getCluePreview(e),word:e,isCurrent:i,onPointerDown:()=>this.selectClue(e.id)})})))),t("div",{key:"02b43a7a46253d36ad8975d8ab9426af208e82f4",part:"board",style:{"--board-width":""+this.boardWidth,"grid-template-columns":`repeat(${this.boardWidth}, 1fr)`,aspectRatio:""+this.boardWidth/this.boardHeight}},this.cells.filter((t=>!t.isBlocked)).map((e=>{const i=this.isInClue(e),s=this.isSelectedCell(e),r=this.specialCells.some((t=>t.x===e.x&&t.y===e.y));return t(h,{isSpecial:r,onPointerDown:()=>this.selectClueByXY(l.from(e)),isInCurrentClue:i,isSelected:s,data:e})})),this.specialBorders.map(((e,i)=>t("div",{key:i,part:"special-border",class:{top:!!(4096&e.border),right:!!(256&e.border),bottom:!!(16&e.border),left:!!(1&e.border)},style:{"--x":""+e.x,"--y":""+e.y}})))),t("div",{key:"24818d75c9ecf6df2f4e1044ab8d74e1e413f29e",part:"preview"},"Tu bedzie podgląd: ",this.currentClueId))}static get delegatesFocus(){return!0}static get watchers(){return{data:[{watchData:0}]}}static get style(){return'*[part=main]{position:relative;display:grid;grid-template-areas:"board" "clues";grid-template-columns:1fr}@media (min-width: 768px){*[part=main]{grid-template-areas:"clues board" "clues .";grid-template-columns:1fr 1fr}}*[part=main].--focused{background:lightgrey}*[part=main] input.dummy{pointer-events:none;opacity:0.2;z-index:100;grid-area:board}[part=clues]{grid-area:clues;display:grid;grid-template-columns:1fr 1fr}[part=clue].--current{background:lightcyan;position:sticky;bottom:0;left:0;right:0}[part=clue] .preview{font-weight:bold;letter-spacing:0.2em;text-align:center;display:block;text-transform:uppercase}[part=board]{grid-area:board;display:block;position:relative;container-type:size;background:darkgoldenrod;--border:1px;--cell-width:calc((100cqw - var(--border)) / var(--board-width, 10))}[part=cell]{display:block;width:calc(var(--cell-width) - var(--border));aspect-ratio:1;background:#d9d9d9;position:absolute;container-type:size;top:calc(var(--y, 0) * var(--cell-width));left:calc(var(--x, 0) * var(--cell-width));border:var(--border) solid red}[part=cell].--alt{background:#999}[part=cell].--in-current-clue{box-shadow:inset 0 0 2px 2px lightblue}[part=cell].--selected{background:lightblue}[part=cell].--special{background:lightgreen}[part=cell] ._inner{position:absolute;top:0;left:0;right:0;bottom:0;display:grid;place-content:center;text-transform:uppercase;font-size:80cqh}[part=preview]{display:none}[part=special-border]{display:block;background:black;position:absolute;z-index:100;--s-border:max(2px, var(--border))}[part=special-border].left,[part=special-border].right{width:var(--s-border);height:calc(var(--cell-width) - var(--border));top:calc(var(--y, 0) * var(--cell-width) + var(--border))}[part=special-border].left{left:calc(var(--x, 0) * var(--cell-width) - var(--s-border) / 2)}[part=special-border].right{left:calc((var(--x, 0) + 1) * var(--cell-width) - var(--s-border) / 2)}[part=special-border].top,[part=special-border].bottom{width:calc(var(--cell-width) - var(--border));height:var(--s-border);left:calc(var(--x, 0) * var(--cell-width) + var(--border))}[part=special-border].top{top:calc(var(--y, 0) * var(--cell-width))}[part=special-border].bottom{top:calc((var(--y, 0) + 1) * var(--cell-width))}'}},[529,"mb-crossword",{init:[4],data:[16],hWords:[32],vWords:[32],currentClueId:[32],currentClue:[32],isFocused:[32],selectedCell:[32],specialBorders:[32],specialCells:[32],boardWidth:[32],boardHeight:[32],cells:[32],initGame:[64]},void 0,{data:[{watchData:0}]}]);function u(t){return new l(t.x+("horizontal"===t.orientation?t.word.length-1:0),t.y+("vertical"===t.orientation?t.word.length-1:0))}function p(){"undefined"!=typeof customElements&&["mb-crossword"].forEach((t=>{"mb-crossword"===t&&(customElements.get(e(t))||customElements.define(e(t),d))}))}p();const b=d,v=p;export{b as MbCrossword,v as defineCustomElement}
|
|
1
|
+
import{h as t,t as e,p as i,H as r,Vec2fromIndex as s,V as l,f as a,isInputEventLetter as o,indexFromXY as n}from"./index.js";function c({isCurrent:e,word:i,onPointerDown:r,preview:s,showPreview:l}){return t("p",{onPointerDown:r,part:"clue",class:{"--current":e}},i.clue,l&&t("span",{class:"preview"},s))}function h({data:e,isInCurrentClue:i,isSelected:r,isSpecial:s,onPointerDown:l}){return t("div",{onPointerDown:l,part:"cell",class:{"--blocked":e.isBlocked,"--in-current-clue":i,"--selected":r,"--alt":!((e.x+e.y)%2),"--special":s},style:{"--x":""+e.x,"--y":""+e.y}},t("div",{class:"_inner",style:{position:"absolute",width:"100%"}},e.value))}const d=i(class extends r{constructor(t){super(),!1!==t&&this.__registerHost(),this.__attachShadow()}init=!0;data;showCluePreview=!0;textInput;hWords=[];vWords=[];currentClueId=null;currentClue=null;isFocused=!1;selectedCell=null;componentWillLoad(){console.log("componentWillLoad",this.init,this.data),this.init&&this.data&&this.initGame()}watchData(){this.init&&this.data&&this.initGame()}get allWords(){return[...this.hWords,...this.vWords]}async initGame(t=this.data){if(console.log("initGame"),!t.words)throw Error("Words definition missing");this.hWords=[],this.vWords=[],t.words.forEach(((t,e)=>{const i=Math.random();void 0===t.id&&(t.id=`${i}_${e}`),"horizontal"===t.orientation&&(this.hWords=[...this.hWords,t]),"vertical"===t.orientation&&(this.vWords=[...this.vWords,t])})),this.specialBorders=t.specialBorders??[],this.specialCells=t.specialCells??[],this.labels=t.labels??[],this.buildBoard()}async checkAnswer(){const t=this.cells.filter((t=>!t.isBlocked));return console.log(t),t.forEach((t=>{console.log(`${t.value} === ${t.answer}`,`${t.x},${t.y}`,t.value===t.answer)})),t.every((t=>t.value===t.answer))}specialBorders=[];specialCells=[];labels=[];boardWidth=0;boardHeight=0;cells=[];buildBoard(){let t=0,e=0;this.allWords.forEach((i=>{"horizontal"===i.orientation&&(t=Math.max(t,i.x+i.word.length),e=Math.max(e,i.y+1)),"vertical"===i.orientation&&(t=Math.max(t,i.x+1),e=Math.max(e,i.y+i.word.length))}));const i=Array(e*t).fill(null).map(((e,i)=>{const[r,l]=s(i,t);return{index:i,x:r,y:l,value:null,isCurrent:!1,isInCurrentWord:!1,isBlocked:!0}}));this.allWords.forEach((e=>{if("horizontal"===e.orientation)for(let r=0;r<e.word.length;r++){const s=l.from(e),a=n(new l(s.x+r,s.y),t);i[a].isBlocked=!1,i[a].answer=e.word[r]}if("vertical"===e.orientation)for(let r=0;r<e.word.length;r++){const s=l.from(e),a=n(new l(s.x,s.y+r),t);i[a].isBlocked=!1,i[a].answer=e.word[r]}})),this.cells=i,this.boardWidth=t,this.boardHeight=e}getClueById(t){return this.allWords.find((e=>e.id===t))??null}isInClue(t,e=this.currentClue){return null!==e&&(t.x===e.x||t.y===e.y)&&("horizontal"===e.orientation?!(t.y!==e.y||t.x<e.x||t.x>=e.x+e.word.length):"vertical"===e.orientation?!(t.x!==e.x||t.y<e.y||t.y>=e.y+e.word.length):void 0)}isSelectedCell(t){return!!this.selectedCell&&t.x===this.selectedCell.x&&t.y===this.selectedCell.y}getNextClueId(){if(!this.currentClueId)return this.allWords[0]?.id??null;const t=this.allWords.findIndex((t=>t.id===this.currentClueId));return t<0?this.allWords[0]?.id??null:this.allWords[t+1]?.id??null}getPrevClueId(){if(!this.currentClueId)return this.allWords[0]?.id??null;const t=this.allWords.findIndex((t=>t.id===this.currentClueId));return t<0?this.allWords[0]?.id??null:this.allWords[t-1]?.id??null}inputLetter(t){this.selectedCell&&(this.selectedCell.value=t,this.selectNextCell(),a(this))}backspace(){this.selectedCell&&(this.selectedCell.value?(this.selectedCell.value="",this.selectPrevCell()):(this.selectPrevCell(),this.selectedCell.value=""),a(this))}selectNextCell(){if(!this.selectedCell||!this.currentClue)return;const t=this.currentClue.orientation;if(function(t,e){return u(t).eq(l.from(e))}(this.currentClue,this.selectedCell)){const t=this.getNextClueId(),e=this.getClueById(t);return e?void this.selectClueByXY(l.from(e)):void 0}"horizontal"===t&&this.selectClueByXY(new l(this.selectedCell.x+1,this.selectedCell.y)),"vertical"===t&&this.selectClueByXY(new l(this.selectedCell.x,this.selectedCell.y+1))}selectPrevCell(){if(!this.selectedCell||!this.currentClue)return;const t=this.currentClue.orientation;if(function(t,e){return l.from(t).eq(l.from(e))}(this.currentClue,this.selectedCell)){const t=this.getPrevClueId(),e=this.getClueById(t);if(e){const t=u(e);return console.log(t),void this.selectClueByXY(t)}}"horizontal"===t&&this.selectClueByXY(new l(this.selectedCell.x-1,this.selectedCell.y)),"vertical"===t&&this.selectClueByXY(new l(this.selectedCell.x,this.selectedCell.y-1))}onFocusin(){this.isFocused=!0,this.currentClueId||(this.currentClueId=this.allWords[0]?.id??null),this.textInput.focus()}onFocusOut(){this.isFocused=!1}selectClue(t,{row:e,col:i}={}){if(!t)return;this.currentClueId=t;const r=this.allWords.find((e=>e.id===t));r||(this.currentClueId=null,this.currentClue=null),this.currentClue=r;const s=[r.x,r.y];void 0!==e&&"vertical"===r.orientation&&e>=r.y&&e<r.y+r.word.length&&(s[1]=e),void 0!==i&&"horizontal"===r.orientation&&i>=r.x&&i<r.x+r.word.length&&(s[0]=i),this.selectCellByXY(l.from(s))}selectCellByXY(t){const e=n(t,this.boardWidth);this.selectedCell=this.cells[e]??null}findNonblockedCell(t,e){const i=new l(t[0]+e[0],t[1]+e[1]),r=this.boardWidth*this.boardHeight;let s=0;for(;this.isCellBlocked(i)&&s<r;)s++,i[0]+=e[0],i[1]+=e[1],i[0]<0&&(i[0]=this.boardWidth-1),i[0]>=this.boardWidth&&(i[0]=0),i[1]<0&&(i[1]=this.boardHeight-1),i[1]>=this.boardHeight&&(i[1]=0);return i}isCellBlocked(t){const e=this.cells[n(t,this.boardWidth)];return e?.isBlocked??!0}selectClueByXY(t){const e=this.allWords.filter((e=>this.isInClue(t,e)));if(e.length)if(this.selectedCell&&2===e.length&&t.eq(l.from(this.selectedCell))){const i=e.find((t=>t.orientation!==this.currentClue?.orientation));this.selectClue(i?.id??e[0].id,{row:t.y,col:t.x})}else{const i=e.find((t=>t.orientation===this.currentClue?.orientation));this.selectClue(i?.id??e[0].id,{row:t.y,col:t.x})}}getCluePreview(t){if(!t)return"_";const e=Array(t.word.length).fill("_");for(let i=0;i<t.word.length;i++){const r=new l(t.x,t.y).add("vertical"===t.orientation?new l(0,1*i):new l(1*i,0)),s=this.cells[n(r,this.boardWidth)];s.value&&(e[i]=s.value)}return e.join("")}onKeyPress(t){if(console.log(t),"Tab"===t.key){const e=t.shiftKey?this.getPrevClueId():this.getNextClueId();e&&(t.preventDefault(),this.selectClue(e))}if(this.selectedCell)switch(t.key){case"ArrowLeft":return this.selectClueByXY(this.findNonblockedCell(new l(this.selectedCell.x,this.selectedCell.y),new l(-1,0))),void t.preventDefault();case"ArrowRight":return this.selectClueByXY(this.findNonblockedCell(new l(this.selectedCell.x,this.selectedCell.y),new l(1,0))),void t.preventDefault();case"ArrowUp":return this.selectClueByXY(this.findNonblockedCell(new l(this.selectedCell.x,this.selectedCell.y),new l(0,-1))),void t.preventDefault();case"ArrowDown":return this.selectClueByXY(this.findNonblockedCell(new l(this.selectedCell.x,this.selectedCell.y),new l(0,1))),void t.preventDefault();case"Backspace":console.log("BACKSPACE!"),this.backspace()}}onInput(t){o(t)&&this.inputLetter(t.data.toLowerCase()[0]),this.textInput.value=""}render(){const e=!!this.vWords.length,i=!!this.hWords.length,r=i&&e;return t("div",{key:"87d88806305c6102eeb76c3984d4b9c2ad2d9759",part:"main",class:{"--focused":this.isFocused},onFocusin:this.onFocusin.bind(this),onFocusout:this.onFocusOut.bind(this)},t("input",{key:"49cdb053a4d2c4ad2c7eb881488b18c68e31fe65",type:"text",class:"dummy",ref:t=>this.textInput=t,onKeyDown:this.onKeyPress.bind(this),onInput:this.onInput.bind(this)}),t("div",{key:"0ca71a907f70e17a6d2d10bff9462bd0b2284007",part:"clues",class:{twoLists:r,oneList:!r}},i?t("div",{part:"clue-list"},this.hWords.map((e=>{const i=e.id===this.currentClueId;return t(c,{showPreview:this.showCluePreview,preview:this.getCluePreview(e),word:e,isCurrent:i,onPointerDown:()=>this.selectClue(e.id)})}))):"",e?t("div",{part:"clue-list"},this.vWords.map((e=>{const i=e.id===this.currentClueId;return t(c,{showPreview:this.showCluePreview,preview:this.getCluePreview(e),word:e,isCurrent:i,onPointerDown:()=>this.selectClue(e.id)})}))):""),t("div",{key:"ea16dca25fe7884d25f9748f98740ad83e25293e",part:"board",style:{"--board-width":""+this.boardWidth,"grid-template-columns":`repeat(${this.boardWidth}, 1fr)`,aspectRatio:""+this.boardWidth/this.boardHeight}},this.cells.filter((t=>!t.isBlocked)).map((e=>{const i=this.isInClue(e),r=this.isSelectedCell(e),s=this.specialCells.some((t=>t.x===e.x&&t.y===e.y));return t(h,{isSpecial:s,onPointerDown:()=>this.selectClueByXY(l.from(e)),isInCurrentClue:i,isSelected:r,data:e})})),this.specialBorders.map(((e,i)=>t("div",{key:i,part:"special-border",class:{top:!!(4096&e.border),right:!!(256&e.border),bottom:!!(16&e.border),left:!!(1&e.border)},style:{"--x":""+e.x,"--y":""+e.y}}))),this.labels.map((e=>t("div",{part:"label",style:{"--x":""+e.x,"--y":""+e.y},onPointerDown:()=>this.selectClue(e.clueId)},t("div",{class:"_inner"},e.text))))),t("div",{key:"d391413e7be3dee148f2f5e6d8fbf22742278a0c",part:"preview"},"Tu bedzie podgląd: ",this.currentClueId))}static get delegatesFocus(){return!0}static get watchers(){return{data:[{watchData:0}]}}static get style(){return'*[part=main]{position:relative;display:grid;grid-template-areas:"board" "clues";grid-template-columns:1fr}@media (min-width: 768px){*[part=main]{grid-template-areas:"clues board" "clues .";grid-template-columns:1fr 1fr}}*[part=main].--focused{background:lightgrey}*[part=main] input.dummy{pointer-events:none;opacity:0.2;z-index:100;grid-area:board;border:none;outline:none}[part=clues]{grid-area:clues;display:grid;grid-template-columns:1fr 1fr;gap:16px}[part=clues].oneList{grid-template-columns:1fr}[part=clue].--current{background:var(--clue-current-background, lightcyan);position:sticky;bottom:0;left:0;right:0}[part=clue] .preview{font-weight:bold;letter-spacing:0.2em;text-align:center;display:block;text-transform:uppercase}[part=board]{grid-area:board;display:block;position:relative;container-type:size;background:var(--board-background, darkgoldenrod);--cell-border-width:2px;--cell-width-temp:calc((100cqw - var(--cell-border-width)) / var(--board-width, 10));--cell-width:var(--cell-width-temp);--cell-width:round(var(--cell-width-temp), 1px)}[part=cell]{display:block;color:var(--cell-color, #11138d);width:calc(var(--cell-width) - var(--cell-border-width));aspect-ratio:1;background:var(--cell-background, #d9d9d9);position:absolute;container-type:size;top:calc(var(--y, 0) * var(--cell-width));left:calc(var(--x, 0) * var(--cell-width));border:var(--cell-border-width) solid var(--cell-border-color, red)}[part=cell].--alt{background:var(--cell-background-alt, #999)}[part=cell].--in-current-clue{box-shadow:inset 0 0 2px 2px var(--cell-selected-background, lightblue)}[part=cell].--selected{background:var(--cell-selected-background, lightblue)}[part=cell].--special{background:var(--cell-special-background, lightgreen)}[part=cell] ._inner{position:absolute;top:0;left:0;right:0;bottom:0;display:grid;place-content:center;text-transform:uppercase;font-size:80cqh}[part=preview]{display:none}[part=special-border]{display:block;background:black;position:absolute;z-index:100;--s-border:max(2px, var(--cell-border-width))}[part=special-border].left,[part=special-border].right{width:var(--s-border);height:calc(var(--cell-width) - var(--cell-border-width));top:calc(var(--y, 0) * var(--cell-width) + var(--cell-border-width))}[part=special-border].left{left:calc(var(--x, 0) * var(--cell-width) - var(--s-border) / 2)}[part=special-border].right{left:calc((var(--x, 0) + 1) * var(--cell-width) - var(--s-border) / 2)}[part=special-border].top,[part=special-border].bottom{width:calc(var(--cell-width) - var(--cell-border-width));height:var(--s-border);left:calc(var(--x, 0) * var(--cell-width) + var(--cell-border-width))}[part=special-border].top{top:calc(var(--y, 0) * var(--cell-width))}[part=special-border].bottom{top:calc((var(--y, 0) + 1) * var(--cell-width))}[part=label]{display:grid;position:absolute;container-type:size;top:calc(var(--y, 0) * var(--cell-width));left:calc(var(--x, 0) * var(--cell-width));width:calc(var(--cell-width) - var(--cell-border-width));aspect-ratio:1;border:var(--cell-border-width) solid transparent;--label-place-content:center end}[part=label] ._inner{position:absolute;top:var(--label-padding, 10cqh);left:var(--label-padding, 10cqh);right:var(--label-padding, 10cqh);bottom:var(--label-padding, 10cqh);display:grid;place-content:var(--label-place-content, center);text-transform:uppercase;font-size:var(--label-font-size, 60cqh)}'}},[529,"mb-crossword",{init:[4],data:[16],showCluePreview:[4,"show-clue-preview"],hWords:[32],vWords:[32],currentClueId:[32],currentClue:[32],isFocused:[32],selectedCell:[32],specialBorders:[32],specialCells:[32],labels:[32],boardWidth:[32],boardHeight:[32],cells:[32],initGame:[64],checkAnswer:[64]},void 0,{data:[{watchData:0}]}]);function u(t){return new l(t.x+("horizontal"===t.orientation?t.word.length-1:0),t.y+("vertical"===t.orientation?t.word.length-1:0))}function p(){"undefined"!=typeof customElements&&["mb-crossword"].forEach((t=>{"mb-crossword"===t&&(customElements.get(e(t))||customElements.define(e(t),d))}))}p();const w=d,b=p;export{w as MbCrossword,b as defineCustomElement}
|
package/dist/esm/loader.js
CHANGED
|
@@ -5,7 +5,7 @@ import { g as globalScripts } from './app-globals-DQuL1Twl.js';
|
|
|
5
5
|
const defineCustomElements = async (win, options) => {
|
|
6
6
|
if (typeof window === 'undefined') return undefined;
|
|
7
7
|
await globalScripts();
|
|
8
|
-
return bootstrapLazy([["mb-crossword",[[529,"mb-crossword",{"init":[4],"data":[16],"hWords":[32],"vWords":[32],"currentClueId":[32],"currentClue":[32],"isFocused":[32],"selectedCell":[32],"specialBorders":[32],"specialCells":[32],"boardWidth":[32],"boardHeight":[32],"cells":[32],"initGame":[64]},null,{"data":[{"watchData":0}]}]]]], options);
|
|
8
|
+
return bootstrapLazy([["mb-crossword",[[529,"mb-crossword",{"init":[4],"data":[16],"showCluePreview":[4,"show-clue-preview"],"hWords":[32],"vWords":[32],"currentClueId":[32],"currentClue":[32],"isFocused":[32],"selectedCell":[32],"specialBorders":[32],"specialCells":[32],"labels":[32],"boardWidth":[32],"boardHeight":[32],"cells":[32],"initGame":[64],"checkAnswer":[64]},null,{"data":[{"watchData":0}]}]]]], options);
|
|
9
9
|
};
|
|
10
10
|
|
|
11
11
|
export { defineCustomElements };
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { h, r as registerInstance, f as forceUpdate } from './index-BGHKtxML.js';
|
|
2
2
|
import { V as Vec2fromIndex, a as Vec2, i as isInputEventLetter, b as indexFromXY } from './index-Dn3GSx6U.js';
|
|
3
3
|
|
|
4
|
-
function Clue({ isCurrent, word, onPointerDown, preview }) {
|
|
4
|
+
function Clue({ isCurrent, word, onPointerDown, preview, showPreview }) {
|
|
5
5
|
return h("p", { onPointerDown: onPointerDown, part: "clue", class: {
|
|
6
6
|
"--current": isCurrent
|
|
7
|
-
} }, word.clue, h("span", { class: "preview" }, preview));
|
|
7
|
+
} }, word.clue, showPreview && h("span", { class: "preview" }, preview));
|
|
8
8
|
}
|
|
9
9
|
|
|
10
10
|
function Cell({ data, isInCurrentClue, isSelected, isSpecial, onPointerDown }) {
|
|
@@ -20,7 +20,7 @@ function Cell({ data, isInCurrentClue, isSelected, isSpecial, onPointerDown }) {
|
|
|
20
20
|
} }, h("div", { class: "_inner", style: { position: 'absolute', width: '100%' } }, data.value));
|
|
21
21
|
}
|
|
22
22
|
|
|
23
|
-
const mbCrosswordCss = () => `*[part=main]{position:relative;display:grid;grid-template-areas:"board" "clues";grid-template-columns:1fr}@media (min-width: 768px){*[part=main]{grid-template-areas:"clues board" "clues .";grid-template-columns:1fr 1fr}}*[part=main].--focused{background:lightgrey}*[part=main] input.dummy{pointer-events:none;opacity:0.2;z-index:100;grid-area:board}[part=clues]{grid-area:clues;display:grid;grid-template-columns:1fr 1fr}[part=clue].--current{background:lightcyan;position:sticky;bottom:0;left:0;right:0}[part=clue] .preview{font-weight:bold;letter-spacing:0.2em;text-align:center;display:block;text-transform:uppercase}[part=board]{grid-area:board;display:block;position:relative;container-type:size;background:darkgoldenrod;--border:
|
|
23
|
+
const mbCrosswordCss = () => `*[part=main]{position:relative;display:grid;grid-template-areas:"board" "clues";grid-template-columns:1fr}@media (min-width: 768px){*[part=main]{grid-template-areas:"clues board" "clues .";grid-template-columns:1fr 1fr}}*[part=main].--focused{background:lightgrey}*[part=main] input.dummy{pointer-events:none;opacity:0.2;z-index:100;grid-area:board;border:none;outline:none}[part=clues]{grid-area:clues;display:grid;grid-template-columns:1fr 1fr;gap:16px}[part=clues].oneList{grid-template-columns:1fr}[part=clue].--current{background:var(--clue-current-background, lightcyan);position:sticky;bottom:0;left:0;right:0}[part=clue] .preview{font-weight:bold;letter-spacing:0.2em;text-align:center;display:block;text-transform:uppercase}[part=board]{grid-area:board;display:block;position:relative;container-type:size;background:var(--board-background, darkgoldenrod);--cell-border-width:2px;--cell-width-temp:calc((100cqw - var(--cell-border-width)) / var(--board-width, 10));--cell-width:var(--cell-width-temp);--cell-width:round(var(--cell-width-temp), 1px)}[part=cell]{display:block;color:var(--cell-color, #11138d);width:calc(var(--cell-width) - var(--cell-border-width));aspect-ratio:1;background:var(--cell-background, #d9d9d9);position:absolute;container-type:size;top:calc(var(--y, 0) * var(--cell-width));left:calc(var(--x, 0) * var(--cell-width));border:var(--cell-border-width) solid var(--cell-border-color, red)}[part=cell].--alt{background:var(--cell-background-alt, #999)}[part=cell].--in-current-clue{box-shadow:inset 0 0 2px 2px var(--cell-selected-background, lightblue)}[part=cell].--selected{background:var(--cell-selected-background, lightblue)}[part=cell].--special{background:var(--cell-special-background, lightgreen)}[part=cell] ._inner{position:absolute;top:0;left:0;right:0;bottom:0;display:grid;place-content:center;text-transform:uppercase;font-size:80cqh}[part=preview]{display:none}[part=special-border]{display:block;background:black;position:absolute;z-index:100;--s-border:max(2px, var(--cell-border-width))}[part=special-border].left,[part=special-border].right{width:var(--s-border);height:calc(var(--cell-width) - var(--cell-border-width));top:calc(var(--y, 0) * var(--cell-width) + var(--cell-border-width))}[part=special-border].left{left:calc(var(--x, 0) * var(--cell-width) - var(--s-border) / 2)}[part=special-border].right{left:calc((var(--x, 0) + 1) * var(--cell-width) - var(--s-border) / 2)}[part=special-border].top,[part=special-border].bottom{width:calc(var(--cell-width) - var(--cell-border-width));height:var(--s-border);left:calc(var(--x, 0) * var(--cell-width) + var(--cell-border-width))}[part=special-border].top{top:calc(var(--y, 0) * var(--cell-width))}[part=special-border].bottom{top:calc((var(--y, 0) + 1) * var(--cell-width))}[part=label]{display:grid;position:absolute;container-type:size;top:calc(var(--y, 0) * var(--cell-width));left:calc(var(--x, 0) * var(--cell-width));width:calc(var(--cell-width) - var(--cell-border-width));aspect-ratio:1;border:var(--cell-border-width) solid transparent;--label-place-content:center end}[part=label] ._inner{position:absolute;top:var(--label-padding, 10cqh);left:var(--label-padding, 10cqh);right:var(--label-padding, 10cqh);bottom:var(--label-padding, 10cqh);display:grid;place-content:var(--label-place-content, center);text-transform:uppercase;font-size:var(--label-font-size, 60cqh)}`;
|
|
24
24
|
|
|
25
25
|
const MBCrossword = class {
|
|
26
26
|
constructor(hostRef) {
|
|
@@ -28,6 +28,7 @@ const MBCrossword = class {
|
|
|
28
28
|
}
|
|
29
29
|
init = true;
|
|
30
30
|
data;
|
|
31
|
+
showCluePreview = true;
|
|
31
32
|
textInput;
|
|
32
33
|
hWords = [];
|
|
33
34
|
vWords = [];
|
|
@@ -73,10 +74,20 @@ const MBCrossword = class {
|
|
|
73
74
|
});
|
|
74
75
|
this.specialBorders = data.specialBorders ?? [];
|
|
75
76
|
this.specialCells = data.specialCells ?? [];
|
|
77
|
+
this.labels = data.labels ?? [];
|
|
76
78
|
this.buildBoard();
|
|
77
79
|
}
|
|
80
|
+
async checkAnswer() {
|
|
81
|
+
const cells = this.cells.filter(c => !c.isBlocked);
|
|
82
|
+
console.log(cells);
|
|
83
|
+
cells.forEach(c => {
|
|
84
|
+
console.log(`${c.value} === ${c.answer}`, `${c.x},${c.y}`, c.value === c.answer);
|
|
85
|
+
});
|
|
86
|
+
return cells.every(c => c.value === c.answer);
|
|
87
|
+
}
|
|
78
88
|
specialBorders = [];
|
|
79
89
|
specialCells = [];
|
|
90
|
+
labels = [];
|
|
80
91
|
boardWidth = 0;
|
|
81
92
|
boardHeight = 0;
|
|
82
93
|
cells = [];
|
|
@@ -111,6 +122,7 @@ const MBCrossword = class {
|
|
|
111
122
|
const v = Vec2.from(w);
|
|
112
123
|
const cellIndex = indexFromXY(new Vec2(v.x + i, v.y), boardWidth);
|
|
113
124
|
cells[cellIndex].isBlocked = false;
|
|
125
|
+
cells[cellIndex].answer = w.word[i];
|
|
114
126
|
}
|
|
115
127
|
}
|
|
116
128
|
if (w.orientation === 'vertical') {
|
|
@@ -118,6 +130,7 @@ const MBCrossword = class {
|
|
|
118
130
|
const v = Vec2.from(w);
|
|
119
131
|
const cellIndex = indexFromXY(new Vec2(v.x, v.y + i), boardWidth);
|
|
120
132
|
cells[cellIndex].isBlocked = false;
|
|
133
|
+
cells[cellIndex].answer = w.word[i];
|
|
121
134
|
}
|
|
122
135
|
}
|
|
123
136
|
});
|
|
@@ -338,9 +351,6 @@ const MBCrossword = class {
|
|
|
338
351
|
this.selectClue(nextClue);
|
|
339
352
|
}
|
|
340
353
|
}
|
|
341
|
-
// if (isKeyboardEventLetter(event)) {
|
|
342
|
-
// this.inputLetter(event.key.toLowerCase()[0]);
|
|
343
|
-
// }
|
|
344
354
|
if (this.selectedCell) {
|
|
345
355
|
switch (event.key) {
|
|
346
356
|
case 'ArrowLeft':
|
|
@@ -372,15 +382,20 @@ const MBCrossword = class {
|
|
|
372
382
|
this.textInput.value = '';
|
|
373
383
|
}
|
|
374
384
|
render() {
|
|
375
|
-
|
|
385
|
+
const hasVWords = Boolean(this.vWords.length);
|
|
386
|
+
const hasHWords = Boolean(this.hWords.length);
|
|
387
|
+
const twoLists = hasHWords && hasVWords;
|
|
388
|
+
return h("div", { key: '87d88806305c6102eeb76c3984d4b9c2ad2d9759', part: "main", class: {
|
|
376
389
|
"--focused": this.isFocused
|
|
377
|
-
}, onFocusin: this.onFocusin.bind(this), onFocusout: this.onFocusOut.bind(this) }, h("input", { key: '
|
|
390
|
+
}, onFocusin: this.onFocusin.bind(this), onFocusout: this.onFocusOut.bind(this) }, h("input", { key: '49cdb053a4d2c4ad2c7eb881488b18c68e31fe65', type: "text", class: "dummy", ref: (el) => this.textInput = el, onKeyDown: this.onKeyPress.bind(this), onInput: this.onInput.bind(this) }), h("div", { key: '0ca71a907f70e17a6d2d10bff9462bd0b2284007', part: "clues", class: {
|
|
391
|
+
twoLists, oneList: !twoLists
|
|
392
|
+
} }, hasHWords ? h("div", { part: "clue-list" }, this.hWords.map(w => {
|
|
378
393
|
const isCurrent = w.id === this.currentClueId;
|
|
379
|
-
return h(Clue, { preview: this.getCluePreview(w), word: w, isCurrent: isCurrent, onPointerDown: () => this.selectClue(w.id) });
|
|
380
|
-
})), h("div", {
|
|
394
|
+
return h(Clue, { showPreview: this.showCluePreview, preview: this.getCluePreview(w), word: w, isCurrent: isCurrent, onPointerDown: () => this.selectClue(w.id) });
|
|
395
|
+
})) : '', hasVWords ? h("div", { part: "clue-list" }, this.vWords.map(w => {
|
|
381
396
|
const isCurrent = w.id === this.currentClueId;
|
|
382
|
-
return h(Clue, { preview: this.getCluePreview(w), word: w, isCurrent: isCurrent, onPointerDown: () => this.selectClue(w.id) });
|
|
383
|
-
}))), h("div", { key: '
|
|
397
|
+
return h(Clue, { showPreview: this.showCluePreview, preview: this.getCluePreview(w), word: w, isCurrent: isCurrent, onPointerDown: () => this.selectClue(w.id) });
|
|
398
|
+
})) : ''), h("div", { key: 'ea16dca25fe7884d25f9748f98740ad83e25293e', part: "board", style: {
|
|
384
399
|
'--board-width': `${this.boardWidth}`,
|
|
385
400
|
'grid-template-columns': `repeat(${this.boardWidth}, 1fr)`,
|
|
386
401
|
aspectRatio: `${this.boardWidth / this.boardHeight}`
|
|
@@ -399,7 +414,12 @@ const MBCrossword = class {
|
|
|
399
414
|
"--x": `${b.x}`,
|
|
400
415
|
"--y": `${b.y}`
|
|
401
416
|
} });
|
|
402
|
-
})
|
|
417
|
+
}), this.labels.map((label) => {
|
|
418
|
+
return h("div", { part: "label", style: {
|
|
419
|
+
"--x": `${label.x}`,
|
|
420
|
+
"--y": `${label.y}`
|
|
421
|
+
}, onPointerDown: () => this.selectClue(label.clueId) }, h("div", { class: "_inner" }, label.text));
|
|
422
|
+
})), h("div", { key: 'd391413e7be3dee148f2f5e6d8fbf22742278a0c', part: "preview" }, "Tu bedzie podgl\u0105d: ", this.currentClueId));
|
|
403
423
|
}
|
|
404
424
|
static get delegatesFocus() { return true; }
|
|
405
425
|
static get watchers() { return {
|
package/dist/esm/mb-crossword.js
CHANGED
|
@@ -17,5 +17,5 @@ var patchBrowser = () => {
|
|
|
17
17
|
|
|
18
18
|
patchBrowser().then(async (options) => {
|
|
19
19
|
await globalScripts();
|
|
20
|
-
return bootstrapLazy([["mb-crossword",[[529,"mb-crossword",{"init":[4],"data":[16],"hWords":[32],"vWords":[32],"currentClueId":[32],"currentClue":[32],"isFocused":[32],"selectedCell":[32],"specialBorders":[32],"specialCells":[32],"boardWidth":[32],"boardHeight":[32],"cells":[32],"initGame":[64]},null,{"data":[{"watchData":0}]}]]]], options);
|
|
20
|
+
return bootstrapLazy([["mb-crossword",[[529,"mb-crossword",{"init":[4],"data":[16],"showCluePreview":[4,"show-clue-preview"],"hWords":[32],"vWords":[32],"currentClueId":[32],"currentClue":[32],"isFocused":[32],"selectedCell":[32],"specialBorders":[32],"specialCells":[32],"labels":[32],"boardWidth":[32],"boardHeight":[32],"cells":[32],"initGame":[64],"checkAnswer":[64]},null,{"data":[{"watchData":0}]}]]]], options);
|
|
21
21
|
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import{p as e,b as
|
|
1
|
+
import{p as e,b as r}from"./p-BGHKtxML.js";export{s as setNonce}from"./p-BGHKtxML.js";import{g as t}from"./p-DQuL1Twl.js";(()=>{const s=import.meta.url,r={};return""!==s&&(r.resourcesUrl=new URL(".",s).href),e(r)})().then((async e=>(await t(),r([["p-b4b191b9",[[529,"mb-crossword",{init:[4],data:[16],showCluePreview:[4,"show-clue-preview"],hWords:[32],vWords:[32],currentClueId:[32],currentClue:[32],isFocused:[32],selectedCell:[32],specialBorders:[32],specialCells:[32],labels:[32],boardWidth:[32],boardHeight:[32],cells:[32],initGame:[64],checkAnswer:[64]},null,{data:[{watchData:0}]}]]]],e))));
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{h as t,r as e,f as i}from"./p-BGHKtxML.js";import{V as r,a as s,i as l,b as a}from"./p-Dn3GSx6U.js";function o({isCurrent:e,word:i,onPointerDown:r,preview:s,showPreview:l}){return t("p",{onPointerDown:r,part:"clue",class:{"--current":e}},i.clue,l&&t("span",{class:"preview"},s))}function n({data:e,isInCurrentClue:i,isSelected:r,isSpecial:s,onPointerDown:l}){return t("div",{onPointerDown:l,part:"cell",class:{"--blocked":e.isBlocked,"--in-current-clue":i,"--selected":r,"--alt":!((e.x+e.y)%2),"--special":s},style:{"--x":`${e.x}`,"--y":`${e.y}`}},t("div",{class:"_inner",style:{position:"absolute",width:"100%"}},e.value))}const c=class{constructor(t){e(this,t)}init=!0;data;showCluePreview=!0;textInput;hWords=[];vWords=[];currentClueId=null;currentClue=null;isFocused=!1;selectedCell=null;componentWillLoad(){console.log("componentWillLoad",this.init,this.data),this.init&&this.data&&this.initGame()}watchData(){this.init&&this.data&&this.initGame()}get allWords(){return[...this.hWords,...this.vWords]}async initGame(t=this.data){if(console.log("initGame"),!t.words)throw new Error("Words definition missing");this.hWords=[],this.vWords=[],t.words.forEach(((t,e)=>{const i=Math.random();void 0===t.id&&(t.id=`${i}_${e}`),"horizontal"===t.orientation&&(this.hWords=[...this.hWords,t]),"vertical"===t.orientation&&(this.vWords=[...this.vWords,t])})),this.specialBorders=t.specialBorders??[],this.specialCells=t.specialCells??[],this.labels=t.labels??[],this.buildBoard()}async checkAnswer(){const t=this.cells.filter((t=>!t.isBlocked));return console.log(t),t.forEach((t=>{console.log(`${t.value} === ${t.answer}`,`${t.x},${t.y}`,t.value===t.answer)})),t.every((t=>t.value===t.answer))}specialBorders=[];specialCells=[];labels=[];boardWidth=0;boardHeight=0;cells=[];buildBoard(){let t=0,e=0;this.allWords.forEach((i=>{"horizontal"===i.orientation&&(t=Math.max(t,i.x+i.word.length),e=Math.max(e,i.y+1)),"vertical"===i.orientation&&(t=Math.max(t,i.x+1),e=Math.max(e,i.y+i.word.length))}));const i=Array(e*t).fill(null).map(((e,i)=>{const[s,l]=r(i,t);return{index:i,x:s,y:l,value:null,isCurrent:!1,isInCurrentWord:!1,isBlocked:!0}}));this.allWords.forEach((e=>{if("horizontal"===e.orientation)for(let r=0;r<e.word.length;r++){const l=s.from(e),o=a(new s(l.x+r,l.y),t);i[o].isBlocked=!1,i[o].answer=e.word[r]}if("vertical"===e.orientation)for(let r=0;r<e.word.length;r++){const l=s.from(e),o=a(new s(l.x,l.y+r),t);i[o].isBlocked=!1,i[o].answer=e.word[r]}})),this.cells=i,this.boardWidth=t,this.boardHeight=e}getClueById(t){return this.allWords.find((e=>e.id===t))??null}isInClue(t,e=this.currentClue){return null!==e&&(t.x===e.x||t.y===e.y)&&("horizontal"===e.orientation?!(t.y!==e.y||t.x<e.x||t.x>=e.x+e.word.length):"vertical"===e.orientation?!(t.x!==e.x||t.y<e.y||t.y>=e.y+e.word.length):void 0)}isSelectedCell(t){return!!this.selectedCell&&t.x===this.selectedCell.x&&t.y===this.selectedCell.y}getNextClueId(){if(!this.currentClueId)return this.allWords[0]?.id??null;const t=this.allWords.findIndex((t=>t.id===this.currentClueId));return t<0?this.allWords[0]?.id??null:this.allWords[t+1]?.id??null}getPrevClueId(){if(!this.currentClueId)return this.allWords[0]?.id??null;const t=this.allWords.findIndex((t=>t.id===this.currentClueId));return t<0?this.allWords[0]?.id??null:this.allWords[t-1]?.id??null}inputLetter(t){this.selectedCell&&(this.selectedCell.value=t,this.selectNextCell(),i(this))}backspace(){this.selectedCell&&(this.selectedCell.value?(this.selectedCell.value="",this.selectPrevCell()):(this.selectPrevCell(),this.selectedCell.value=""),i(this))}selectNextCell(){if(!this.selectedCell||!this.currentClue)return;const t=this.currentClue.orientation;if(e=this.selectedCell,h(this.currentClue).eq(s.from(e))){const t=this.getNextClueId(),e=this.getClueById(t);return e?void this.selectClueByXY(s.from(e)):void 0}var e;"horizontal"===t&&this.selectClueByXY(new s(this.selectedCell.x+1,this.selectedCell.y)),"vertical"===t&&this.selectClueByXY(new s(this.selectedCell.x,this.selectedCell.y+1))}selectPrevCell(){if(!this.selectedCell||!this.currentClue)return;const t=this.currentClue.orientation;if(e=this.selectedCell,s.from(this.currentClue).eq(s.from(e))){const t=this.getPrevClueId(),e=this.getClueById(t);if(e){const t=h(e);return console.log(t),void this.selectClueByXY(t)}}var e;"horizontal"===t&&this.selectClueByXY(new s(this.selectedCell.x-1,this.selectedCell.y)),"vertical"===t&&this.selectClueByXY(new s(this.selectedCell.x,this.selectedCell.y-1))}onFocusin(){this.isFocused=!0,this.currentClueId||(this.currentClueId=this.allWords[0]?.id??null),this.textInput.focus()}onFocusOut(){this.isFocused=!1}selectClue(t,{row:e,col:i}={}){if(!t)return;this.currentClueId=t;const r=this.allWords.find((e=>e.id===t));r||(this.currentClueId=null,this.currentClue=null),this.currentClue=r;const l=[r.x,r.y];void 0!==e&&"vertical"===r.orientation&&e>=r.y&&e<r.y+r.word.length&&(l[1]=e),void 0!==i&&"horizontal"===r.orientation&&i>=r.x&&i<r.x+r.word.length&&(l[0]=i),this.selectCellByXY(s.from(l))}selectCellByXY(t){const e=a(t,this.boardWidth);this.selectedCell=this.cells[e]??null}findNonblockedCell(t,e){const i=new s(t[0]+e[0],t[1]+e[1]),r=this.boardWidth*this.boardHeight;let l=0;for(;this.isCellBlocked(i)&&l<r;)l++,i[0]+=e[0],i[1]+=e[1],i[0]<0&&(i[0]=this.boardWidth-1),i[0]>=this.boardWidth&&(i[0]=0),i[1]<0&&(i[1]=this.boardHeight-1),i[1]>=this.boardHeight&&(i[1]=0);return i}isCellBlocked(t){const e=this.cells[a(t,this.boardWidth)];return e?.isBlocked??!0}selectClueByXY(t){const e=this.allWords.filter((e=>this.isInClue(t,e)));if(e.length)if(this.selectedCell&&2===e.length&&t.eq(s.from(this.selectedCell))){const i=e.find((t=>t.orientation!==this.currentClue?.orientation));this.selectClue(i?.id??e[0].id,{row:t.y,col:t.x})}else{const i=e.find((t=>t.orientation===this.currentClue?.orientation));this.selectClue(i?.id??e[0].id,{row:t.y,col:t.x})}}getCluePreview(t){if(!t)return"_";const e=Array(t.word.length).fill("_");for(let i=0;i<t.word.length;i++){const r=new s(t.x,t.y).add("vertical"===t.orientation?new s(0,1*i):new s(1*i,0)),l=this.cells[a(r,this.boardWidth)];l.value&&(e[i]=l.value)}return e.join("")}onKeyPress(t){if(console.log(t),"Tab"===t.key){const e=t.shiftKey?this.getPrevClueId():this.getNextClueId();e&&(t.preventDefault(),this.selectClue(e))}if(this.selectedCell)switch(t.key){case"ArrowLeft":return this.selectClueByXY(this.findNonblockedCell(new s(this.selectedCell.x,this.selectedCell.y),new s(-1,0))),void t.preventDefault();case"ArrowRight":return this.selectClueByXY(this.findNonblockedCell(new s(this.selectedCell.x,this.selectedCell.y),new s(1,0))),void t.preventDefault();case"ArrowUp":return this.selectClueByXY(this.findNonblockedCell(new s(this.selectedCell.x,this.selectedCell.y),new s(0,-1))),void t.preventDefault();case"ArrowDown":return this.selectClueByXY(this.findNonblockedCell(new s(this.selectedCell.x,this.selectedCell.y),new s(0,1))),void t.preventDefault();case"Backspace":console.log("BACKSPACE!"),this.backspace()}}onInput(t){l(t)&&this.inputLetter(t.data.toLowerCase()[0]),this.textInput.value=""}render(){const e=Boolean(this.vWords.length),i=Boolean(this.hWords.length),r=i&&e;return t("div",{key:"87d88806305c6102eeb76c3984d4b9c2ad2d9759",part:"main",class:{"--focused":this.isFocused},onFocusin:this.onFocusin.bind(this),onFocusout:this.onFocusOut.bind(this)},t("input",{key:"49cdb053a4d2c4ad2c7eb881488b18c68e31fe65",type:"text",class:"dummy",ref:t=>this.textInput=t,onKeyDown:this.onKeyPress.bind(this),onInput:this.onInput.bind(this)}),t("div",{key:"0ca71a907f70e17a6d2d10bff9462bd0b2284007",part:"clues",class:{twoLists:r,oneList:!r}},i?t("div",{part:"clue-list"},this.hWords.map((e=>{const i=e.id===this.currentClueId;return t(o,{showPreview:this.showCluePreview,preview:this.getCluePreview(e),word:e,isCurrent:i,onPointerDown:()=>this.selectClue(e.id)})}))):"",e?t("div",{part:"clue-list"},this.vWords.map((e=>{const i=e.id===this.currentClueId;return t(o,{showPreview:this.showCluePreview,preview:this.getCluePreview(e),word:e,isCurrent:i,onPointerDown:()=>this.selectClue(e.id)})}))):""),t("div",{key:"ea16dca25fe7884d25f9748f98740ad83e25293e",part:"board",style:{"--board-width":`${this.boardWidth}`,"grid-template-columns":`repeat(${this.boardWidth}, 1fr)`,aspectRatio:""+this.boardWidth/this.boardHeight}},this.cells.filter((t=>!t.isBlocked)).map((e=>{const i=this.isInClue(e),r=this.isSelectedCell(e),l=this.specialCells.some((t=>t.x===e.x&&t.y===e.y));return t(n,{isSpecial:l,onPointerDown:()=>this.selectClueByXY(s.from(e)),isInCurrentClue:i,isSelected:r,data:e})})),this.specialBorders.map(((e,i)=>t("div",{key:i,part:"special-border",class:{top:Boolean(4096&e.border),right:Boolean(256&e.border),bottom:Boolean(16&e.border),left:Boolean(1&e.border)},style:{"--x":`${e.x}`,"--y":`${e.y}`}}))),this.labels.map((e=>t("div",{part:"label",style:{"--x":`${e.x}`,"--y":`${e.y}`},onPointerDown:()=>this.selectClue(e.clueId)},t("div",{class:"_inner"},e.text))))),t("div",{key:"d391413e7be3dee148f2f5e6d8fbf22742278a0c",part:"preview"},"Tu bedzie podgląd: ",this.currentClueId))}static get delegatesFocus(){return!0}static get watchers(){return{data:[{watchData:0}]}}};function h(t){return new s(t.x+("horizontal"===t.orientation?t.word.length-1:0),t.y+("vertical"===t.orientation?t.word.length-1:0))}c.style='*[part=main]{position:relative;display:grid;grid-template-areas:"board" "clues";grid-template-columns:1fr}@media (min-width: 768px){*[part=main]{grid-template-areas:"clues board" "clues .";grid-template-columns:1fr 1fr}}*[part=main].--focused{background:lightgrey}*[part=main] input.dummy{pointer-events:none;opacity:0.2;z-index:100;grid-area:board;border:none;outline:none}[part=clues]{grid-area:clues;display:grid;grid-template-columns:1fr 1fr;gap:16px}[part=clues].oneList{grid-template-columns:1fr}[part=clue].--current{background:var(--clue-current-background, lightcyan);position:sticky;bottom:0;left:0;right:0}[part=clue] .preview{font-weight:bold;letter-spacing:0.2em;text-align:center;display:block;text-transform:uppercase}[part=board]{grid-area:board;display:block;position:relative;container-type:size;background:var(--board-background, darkgoldenrod);--cell-border-width:2px;--cell-width-temp:calc((100cqw - var(--cell-border-width)) / var(--board-width, 10));--cell-width:var(--cell-width-temp);--cell-width:round(var(--cell-width-temp), 1px)}[part=cell]{display:block;color:var(--cell-color, #11138d);width:calc(var(--cell-width) - var(--cell-border-width));aspect-ratio:1;background:var(--cell-background, #d9d9d9);position:absolute;container-type:size;top:calc(var(--y, 0) * var(--cell-width));left:calc(var(--x, 0) * var(--cell-width));border:var(--cell-border-width) solid var(--cell-border-color, red)}[part=cell].--alt{background:var(--cell-background-alt, #999)}[part=cell].--in-current-clue{box-shadow:inset 0 0 2px 2px var(--cell-selected-background, lightblue)}[part=cell].--selected{background:var(--cell-selected-background, lightblue)}[part=cell].--special{background:var(--cell-special-background, lightgreen)}[part=cell] ._inner{position:absolute;top:0;left:0;right:0;bottom:0;display:grid;place-content:center;text-transform:uppercase;font-size:80cqh}[part=preview]{display:none}[part=special-border]{display:block;background:black;position:absolute;z-index:100;--s-border:max(2px, var(--cell-border-width))}[part=special-border].left,[part=special-border].right{width:var(--s-border);height:calc(var(--cell-width) - var(--cell-border-width));top:calc(var(--y, 0) * var(--cell-width) + var(--cell-border-width))}[part=special-border].left{left:calc(var(--x, 0) * var(--cell-width) - var(--s-border) / 2)}[part=special-border].right{left:calc((var(--x, 0) + 1) * var(--cell-width) - var(--s-border) / 2)}[part=special-border].top,[part=special-border].bottom{width:calc(var(--cell-width) - var(--cell-border-width));height:var(--s-border);left:calc(var(--x, 0) * var(--cell-width) + var(--cell-border-width))}[part=special-border].top{top:calc(var(--y, 0) * var(--cell-width))}[part=special-border].bottom{top:calc((var(--y, 0) + 1) * var(--cell-width))}[part=label]{display:grid;position:absolute;container-type:size;top:calc(var(--y, 0) * var(--cell-width));left:calc(var(--x, 0) * var(--cell-width));width:calc(var(--cell-width) - var(--cell-border-width));aspect-ratio:1;border:var(--cell-border-width) solid transparent;--label-place-content:center end}[part=label] ._inner{position:absolute;top:var(--label-padding, 10cqh);left:var(--label-padding, 10cqh);right:var(--label-padding, 10cqh);bottom:var(--label-padding, 10cqh);display:grid;place-content:var(--label-place-content, center);text-transform:uppercase;font-size:var(--label-font-size, 60cqh)}';export{c as mb_crossword}
|
|
@@ -4,5 +4,6 @@ export interface Props {
|
|
|
4
4
|
word: PuzzleClue;
|
|
5
5
|
onPointerDown(event: PointerEvent): void;
|
|
6
6
|
preview: string;
|
|
7
|
+
showPreview: boolean;
|
|
7
8
|
}
|
|
8
|
-
export declare function Clue({ isCurrent, word, onPointerDown, preview }: Props): any;
|
|
9
|
+
export declare function Clue({ isCurrent, word, onPointerDown, preview, showPreview }: Props): any;
|
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
import { CellData, PuzzleData, PuzzleClue, Border, SpecialCell } from './types';
|
|
1
|
+
import { CellData, PuzzleData, PuzzleClue, Border, SpecialCell, PuzzleLabel } from './types';
|
|
2
2
|
import { Vec2 } from '@mb-puzzle/vec2';
|
|
3
3
|
export declare class MBCrossword {
|
|
4
4
|
init: boolean;
|
|
5
5
|
data: PuzzleData;
|
|
6
|
+
showCluePreview: boolean;
|
|
6
7
|
textInput: HTMLInputElement;
|
|
7
8
|
hWords: PuzzleClue[];
|
|
8
9
|
vWords: PuzzleClue[];
|
|
@@ -14,8 +15,10 @@ export declare class MBCrossword {
|
|
|
14
15
|
watchData(): void;
|
|
15
16
|
get allWords(): PuzzleClue[];
|
|
16
17
|
initGame(data?: PuzzleData): Promise<void>;
|
|
18
|
+
checkAnswer(): Promise<boolean>;
|
|
17
19
|
specialBorders: Border[];
|
|
18
20
|
specialCells: SpecialCell[];
|
|
21
|
+
labels: PuzzleLabel[];
|
|
19
22
|
boardWidth: number;
|
|
20
23
|
boardHeight: number;
|
|
21
24
|
cells: CellData[];
|
|
@@ -2,6 +2,7 @@ export interface PuzzleData {
|
|
|
2
2
|
words: PuzzleClue[];
|
|
3
3
|
specialBorders?: Border[];
|
|
4
4
|
specialCells?: SpecialCell[];
|
|
5
|
+
labels?: PuzzleLabel[];
|
|
5
6
|
}
|
|
6
7
|
export interface Border {
|
|
7
8
|
x: number;
|
|
@@ -20,6 +21,12 @@ export interface PuzzleClue {
|
|
|
20
21
|
orientation: 'horizontal' | 'vertical';
|
|
21
22
|
clue: string;
|
|
22
23
|
}
|
|
24
|
+
export interface PuzzleLabel {
|
|
25
|
+
text: string;
|
|
26
|
+
x: number;
|
|
27
|
+
y: number;
|
|
28
|
+
clueId: string;
|
|
29
|
+
}
|
|
23
30
|
export interface CellData {
|
|
24
31
|
index: number;
|
|
25
32
|
x: number;
|
|
@@ -28,4 +35,5 @@ export interface CellData {
|
|
|
28
35
|
isCurrent: boolean;
|
|
29
36
|
isInCurrentWord: boolean;
|
|
30
37
|
isBlocked: boolean;
|
|
38
|
+
answer?: string;
|
|
31
39
|
}
|
|
@@ -9,12 +9,17 @@ import { PuzzleData } from "./components/crossword/types";
|
|
|
9
9
|
export { PuzzleData } from "./components/crossword/types";
|
|
10
10
|
export namespace Components {
|
|
11
11
|
interface MbCrossword {
|
|
12
|
+
"checkAnswer": () => Promise<boolean>;
|
|
12
13
|
"data": PuzzleData;
|
|
13
14
|
/**
|
|
14
15
|
* @default true
|
|
15
16
|
*/
|
|
16
17
|
"init": boolean;
|
|
17
18
|
"initGame": (data?: PuzzleData) => Promise<void>;
|
|
19
|
+
/**
|
|
20
|
+
* @default true
|
|
21
|
+
*/
|
|
22
|
+
"showCluePreview": boolean;
|
|
18
23
|
}
|
|
19
24
|
}
|
|
20
25
|
declare global {
|
|
@@ -35,6 +40,10 @@ declare namespace LocalJSX {
|
|
|
35
40
|
* @default true
|
|
36
41
|
*/
|
|
37
42
|
"init"?: boolean;
|
|
43
|
+
/**
|
|
44
|
+
* @default true
|
|
45
|
+
*/
|
|
46
|
+
"showCluePreview"?: boolean;
|
|
38
47
|
}
|
|
39
48
|
interface IntrinsicElements {
|
|
40
49
|
"mb-crossword": MbCrossword;
|
package/package.json
CHANGED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
import{h as t,r as i,f as e}from"./p-BGHKtxML.js";import{V as r,a as s,i as a,b as l}from"./p-Dn3GSx6U.js";function o({isCurrent:i,word:e,onPointerDown:r,preview:s}){return t("p",{onPointerDown:r,part:"clue",class:{"--current":i}},e.clue,t("span",{class:"preview"},s))}function n({data:i,isInCurrentClue:e,isSelected:r,isSpecial:s,onPointerDown:a}){return t("div",{onPointerDown:a,part:"cell",class:{"--blocked":i.isBlocked,"--in-current-clue":e,"--selected":r,"--alt":!((i.x+i.y)%2),"--special":s},style:{"--x":`${i.x}`,"--y":`${i.y}`}},t("div",{class:"_inner",style:{position:"absolute",width:"100%"}},i.value))}const c=class{constructor(t){i(this,t)}init=!0;data;textInput;hWords=[];vWords=[];currentClueId=null;currentClue=null;isFocused=!1;selectedCell=null;componentWillLoad(){console.log("componentWillLoad",this.init,this.data),this.init&&this.data&&this.initGame()}watchData(){this.init&&this.data&&this.initGame()}get allWords(){return[...this.hWords,...this.vWords]}async initGame(t=this.data){if(console.log("initGame"),!t.words)throw new Error("Words definition missing");this.hWords=[],this.vWords=[],t.words.forEach(((t,i)=>{const e=Math.random();void 0===t.id&&(t.id=`${e}_${i}`),"horizontal"===t.orientation&&(this.hWords=[...this.hWords,t]),"vertical"===t.orientation&&(this.vWords=[...this.vWords,t])})),this.specialBorders=t.specialBorders??[],this.specialCells=t.specialCells??[],this.buildBoard()}specialBorders=[];specialCells=[];boardWidth=0;boardHeight=0;cells=[];buildBoard(){let t=0,i=0;this.allWords.forEach((e=>{"horizontal"===e.orientation&&(t=Math.max(t,e.x+e.word.length),i=Math.max(i,e.y+1)),"vertical"===e.orientation&&(t=Math.max(t,e.x+1),i=Math.max(i,e.y+e.word.length))}));const e=Array(i*t).fill(null).map(((i,e)=>{const[s,a]=r(e,t);return{index:e,x:s,y:a,value:null,isCurrent:!1,isInCurrentWord:!1,isBlocked:!0}}));this.allWords.forEach((i=>{if("horizontal"===i.orientation)for(let r=0;r<i.word.length;r++){const a=s.from(i),o=l(new s(a.x+r,a.y),t);e[o].isBlocked=!1}if("vertical"===i.orientation)for(let r=0;r<i.word.length;r++){const a=s.from(i),o=l(new s(a.x,a.y+r),t);e[o].isBlocked=!1}})),this.cells=e,this.boardWidth=t,this.boardHeight=i}getClueById(t){return this.allWords.find((i=>i.id===t))??null}isInClue(t,i=this.currentClue){return null!==i&&(t.x===i.x||t.y===i.y)&&("horizontal"===i.orientation?!(t.y!==i.y||t.x<i.x||t.x>=i.x+i.word.length):"vertical"===i.orientation?!(t.x!==i.x||t.y<i.y||t.y>=i.y+i.word.length):void 0)}isSelectedCell(t){return!!this.selectedCell&&t.x===this.selectedCell.x&&t.y===this.selectedCell.y}getNextClueId(){if(!this.currentClueId)return this.allWords[0]?.id??null;const t=this.allWords.findIndex((t=>t.id===this.currentClueId));return t<0?this.allWords[0]?.id??null:this.allWords[t+1]?.id??null}getPrevClueId(){if(!this.currentClueId)return this.allWords[0]?.id??null;const t=this.allWords.findIndex((t=>t.id===this.currentClueId));return t<0?this.allWords[0]?.id??null:this.allWords[t-1]?.id??null}inputLetter(t){this.selectedCell&&(this.selectedCell.value=t,this.selectNextCell(),e(this))}backspace(){this.selectedCell&&(this.selectedCell.value?(this.selectedCell.value="",this.selectPrevCell()):(this.selectPrevCell(),this.selectedCell.value=""),e(this))}selectNextCell(){if(!this.selectedCell||!this.currentClue)return;const t=this.currentClue.orientation;if(i=this.selectedCell,h(this.currentClue).eq(s.from(i))){const t=this.getNextClueId(),i=this.getClueById(t);return i?void this.selectClueByXY(s.from(i)):void 0}var i;"horizontal"===t&&this.selectClueByXY(new s(this.selectedCell.x+1,this.selectedCell.y)),"vertical"===t&&this.selectClueByXY(new s(this.selectedCell.x,this.selectedCell.y+1))}selectPrevCell(){if(!this.selectedCell||!this.currentClue)return;const t=this.currentClue.orientation;if(i=this.selectedCell,s.from(this.currentClue).eq(s.from(i))){const t=this.getPrevClueId(),i=this.getClueById(t);if(i){const t=h(i);return console.log(t),void this.selectClueByXY(t)}}var i;"horizontal"===t&&this.selectClueByXY(new s(this.selectedCell.x-1,this.selectedCell.y)),"vertical"===t&&this.selectClueByXY(new s(this.selectedCell.x,this.selectedCell.y-1))}onFocusin(){this.isFocused=!0,this.currentClueId||(this.currentClueId=this.allWords[0]?.id??null),this.textInput.focus()}onFocusOut(){this.isFocused=!1}selectClue(t,{row:i,col:e}={}){if(!t)return;this.currentClueId=t;const r=this.allWords.find((i=>i.id===t));r||(this.currentClueId=null,this.currentClue=null),this.currentClue=r;const a=[r.x,r.y];void 0!==i&&"vertical"===r.orientation&&i>=r.y&&i<r.y+r.word.length&&(a[1]=i),void 0!==e&&"horizontal"===r.orientation&&e>=r.x&&e<r.x+r.word.length&&(a[0]=e),this.selectCellByXY(s.from(a))}selectCellByXY(t){const i=l(t,this.boardWidth);this.selectedCell=this.cells[i]??null}findNonblockedCell(t,i){const e=new s(t[0]+i[0],t[1]+i[1]),r=this.boardWidth*this.boardHeight;let a=0;for(;this.isCellBlocked(e)&&a<r;)a++,e[0]+=i[0],e[1]+=i[1],e[0]<0&&(e[0]=this.boardWidth-1),e[0]>=this.boardWidth&&(e[0]=0),e[1]<0&&(e[1]=this.boardHeight-1),e[1]>=this.boardHeight&&(e[1]=0);return e}isCellBlocked(t){const i=this.cells[l(t,this.boardWidth)];return i?.isBlocked??!0}selectClueByXY(t){const i=this.allWords.filter((i=>this.isInClue(t,i)));if(i.length)if(this.selectedCell&&2===i.length&&t.eq(s.from(this.selectedCell))){const e=i.find((t=>t.orientation!==this.currentClue?.orientation));this.selectClue(e?.id??i[0].id,{row:t.y,col:t.x})}else{const e=i.find((t=>t.orientation===this.currentClue?.orientation));this.selectClue(e?.id??i[0].id,{row:t.y,col:t.x})}}getCluePreview(t){if(!t)return"_";const i=Array(t.word.length).fill("_");for(let e=0;e<t.word.length;e++){const r=new s(t.x,t.y).add("vertical"===t.orientation?new s(0,1*e):new s(1*e,0)),a=this.cells[l(r,this.boardWidth)];a.value&&(i[e]=a.value)}return i.join("")}onKeyPress(t){if(console.log(t),"Tab"===t.key){const i=t.shiftKey?this.getPrevClueId():this.getNextClueId();i&&(t.preventDefault(),this.selectClue(i))}if(this.selectedCell)switch(t.key){case"ArrowLeft":return this.selectClueByXY(this.findNonblockedCell(new s(this.selectedCell.x,this.selectedCell.y),new s(-1,0))),void t.preventDefault();case"ArrowRight":return this.selectClueByXY(this.findNonblockedCell(new s(this.selectedCell.x,this.selectedCell.y),new s(1,0))),void t.preventDefault();case"ArrowUp":return this.selectClueByXY(this.findNonblockedCell(new s(this.selectedCell.x,this.selectedCell.y),new s(0,-1))),void t.preventDefault();case"ArrowDown":return this.selectClueByXY(this.findNonblockedCell(new s(this.selectedCell.x,this.selectedCell.y),new s(0,1))),void t.preventDefault();case"Backspace":console.log("BACKSPACE!"),this.backspace()}}onInput(t){a(t)&&this.inputLetter(t.data.toLowerCase()[0]),this.textInput.value=""}render(){return t("div",{key:"1dec8e0d58c347be7a44c19c416d3940c37ccf0c",part:"main",class:{"--focused":this.isFocused},onFocusin:this.onFocusin.bind(this),onFocusout:this.onFocusOut.bind(this)},t("input",{key:"40c9f176b21072bcd084871b9caff7add03c9680",type:"text",class:"dummy",ref:t=>this.textInput=t,onKeyDown:this.onKeyPress.bind(this),onInput:this.onInput.bind(this)}),t("div",{key:"14eb06bad93227192ea299ecd27f12cf58304cea",part:"clues"},t("div",{key:"e58b0150ff780acd6f770b009d3936a3a7195138",part:"clue-list"},this.hWords.map((i=>{const e=i.id===this.currentClueId;return t(o,{preview:this.getCluePreview(i),word:i,isCurrent:e,onPointerDown:()=>this.selectClue(i.id)})}))),t("div",{key:"13c5ca874d95561654096aa09d5b715576d4392d",part:"clue-list"},this.vWords.map((i=>{const e=i.id===this.currentClueId;return t(o,{preview:this.getCluePreview(i),word:i,isCurrent:e,onPointerDown:()=>this.selectClue(i.id)})})))),t("div",{key:"02b43a7a46253d36ad8975d8ab9426af208e82f4",part:"board",style:{"--board-width":`${this.boardWidth}`,"grid-template-columns":`repeat(${this.boardWidth}, 1fr)`,aspectRatio:""+this.boardWidth/this.boardHeight}},this.cells.filter((t=>!t.isBlocked)).map((i=>{const e=this.isInClue(i),r=this.isSelectedCell(i),a=this.specialCells.some((t=>t.x===i.x&&t.y===i.y));return t(n,{isSpecial:a,onPointerDown:()=>this.selectClueByXY(s.from(i)),isInCurrentClue:e,isSelected:r,data:i})})),this.specialBorders.map(((i,e)=>t("div",{key:e,part:"special-border",class:{top:Boolean(4096&i.border),right:Boolean(256&i.border),bottom:Boolean(16&i.border),left:Boolean(1&i.border)},style:{"--x":`${i.x}`,"--y":`${i.y}`}})))),t("div",{key:"24818d75c9ecf6df2f4e1044ab8d74e1e413f29e",part:"preview"},"Tu bedzie podgląd: ",this.currentClueId))}static get delegatesFocus(){return!0}static get watchers(){return{data:[{watchData:0}]}}};function h(t){return new s(t.x+("horizontal"===t.orientation?t.word.length-1:0),t.y+("vertical"===t.orientation?t.word.length-1:0))}c.style='*[part=main]{position:relative;display:grid;grid-template-areas:"board" "clues";grid-template-columns:1fr}@media (min-width: 768px){*[part=main]{grid-template-areas:"clues board" "clues .";grid-template-columns:1fr 1fr}}*[part=main].--focused{background:lightgrey}*[part=main] input.dummy{pointer-events:none;opacity:0.2;z-index:100;grid-area:board}[part=clues]{grid-area:clues;display:grid;grid-template-columns:1fr 1fr}[part=clue].--current{background:lightcyan;position:sticky;bottom:0;left:0;right:0}[part=clue] .preview{font-weight:bold;letter-spacing:0.2em;text-align:center;display:block;text-transform:uppercase}[part=board]{grid-area:board;display:block;position:relative;container-type:size;background:darkgoldenrod;--border:1px;--cell-width:calc((100cqw - var(--border)) / var(--board-width, 10))}[part=cell]{display:block;width:calc(var(--cell-width) - var(--border));aspect-ratio:1;background:#d9d9d9;position:absolute;container-type:size;top:calc(var(--y, 0) * var(--cell-width));left:calc(var(--x, 0) * var(--cell-width));border:var(--border) solid red}[part=cell].--alt{background:#999}[part=cell].--in-current-clue{box-shadow:inset 0 0 2px 2px lightblue}[part=cell].--selected{background:lightblue}[part=cell].--special{background:lightgreen}[part=cell] ._inner{position:absolute;top:0;left:0;right:0;bottom:0;display:grid;place-content:center;text-transform:uppercase;font-size:80cqh}[part=preview]{display:none}[part=special-border]{display:block;background:black;position:absolute;z-index:100;--s-border:max(2px, var(--border))}[part=special-border].left,[part=special-border].right{width:var(--s-border);height:calc(var(--cell-width) - var(--border));top:calc(var(--y, 0) * var(--cell-width) + var(--border))}[part=special-border].left{left:calc(var(--x, 0) * var(--cell-width) - var(--s-border) / 2)}[part=special-border].right{left:calc((var(--x, 0) + 1) * var(--cell-width) - var(--s-border) / 2)}[part=special-border].top,[part=special-border].bottom{width:calc(var(--cell-width) - var(--border));height:var(--s-border);left:calc(var(--x, 0) * var(--cell-width) + var(--border))}[part=special-border].top{top:calc(var(--y, 0) * var(--cell-width))}[part=special-border].bottom{top:calc((var(--y, 0) + 1) * var(--cell-width))}';export{c as mb_crossword}
|