@mborecki/sliding-puzzle 0.1.0 → 0.3.0

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.
@@ -1,5 +1,5 @@
1
1
  const NAMESPACE = 'sliding-puzzle';
2
- const BUILD = /* sliding-puzzle */ { hotModuleReplacement: false, hydratedSelectorName: "hydrated", propChangeCallback: false, state: true, updatable: true};
2
+ const BUILD = /* sliding-puzzle */ { hotModuleReplacement: false, hydratedSelectorName: "hydrated", lazyLoad: true, propChangeCallback: false, state: true, updatable: true};
3
3
 
4
4
  /*
5
5
  Stencil Client Platform v4.41.0 | MIT Licensed | https://stenciljs.com
@@ -2108,6 +2108,11 @@ createSupportsRuleRe(":host");
2108
2108
  createSupportsRuleRe(":host-context");
2109
2109
  var parsePropertyValue = (propValue, propType, isFormAssociated) => {
2110
2110
  if (propValue != null && !isComplexType(propValue)) {
2111
+ if (propType & 4 /* Boolean */) {
2112
+ {
2113
+ return propValue === "false" ? false : propValue === "" || !!propValue;
2114
+ }
2115
+ }
2111
2116
  return propValue;
2112
2117
  }
2113
2118
  return propValue;
@@ -2658,7 +2663,8 @@ var setValue = (ref, propName, newVal, cmpMeta) => {
2658
2663
  const flags = hostRef.$flags$;
2659
2664
  const instance = hostRef.$lazyInstance$ ;
2660
2665
  newVal = parsePropertyValue(
2661
- newVal);
2666
+ newVal,
2667
+ cmpMeta.$members$[propName][0]);
2662
2668
  const areBothNaN = Number.isNaN(oldVal) && Number.isNaN(newVal);
2663
2669
  const didValueChange = newVal !== oldVal && !areBothNaN;
2664
2670
  if ((!(flags & 8 /* isConstructingInstance */) || oldVal === void 0) && didValueChange) {
@@ -2676,7 +2682,7 @@ var setValue = (ref, propName, newVal, cmpMeta) => {
2676
2682
 
2677
2683
  // src/runtime/proxy-component.ts
2678
2684
  var proxyComponent = (Cstr, cmpMeta, flags) => {
2679
- var _a;
2685
+ var _a, _b;
2680
2686
  const prototype = Cstr.prototype;
2681
2687
  if (cmpMeta.$members$ || BUILD.propChangeCallback) {
2682
2688
  const members = Object.entries((_a = cmpMeta.$members$) != null ? _a : {});
@@ -2715,7 +2721,8 @@ var proxyComponent = (Cstr, cmpMeta, flags) => {
2715
2721
  }
2716
2722
  origSetter.apply(this, [
2717
2723
  parsePropertyValue(
2718
- newValue)
2724
+ newValue,
2725
+ memberFlags)
2719
2726
  ]);
2720
2727
  newValue = memberFlags & 32 /* State */ ? this[memberName] : ref.$hostElement$[memberName];
2721
2728
  setValue(this, memberName, newValue, cmpMeta);
@@ -2739,7 +2746,8 @@ var proxyComponent = (Cstr, cmpMeta, flags) => {
2739
2746
  ref.$instanceValues$.set(memberName, currentValue);
2740
2747
  }
2741
2748
  ref.$lazyInstance$[memberName] = parsePropertyValue(
2742
- newValue);
2749
+ newValue,
2750
+ memberFlags);
2743
2751
  setValue(this, memberName, ref.$lazyInstance$[memberName], cmpMeta);
2744
2752
  };
2745
2753
  if (ref.$lazyInstance$) {
@@ -2765,6 +2773,55 @@ var proxyComponent = (Cstr, cmpMeta, flags) => {
2765
2773
  });
2766
2774
  }
2767
2775
  });
2776
+ if ((flags & 1 /* isElementConstructor */)) {
2777
+ const attrNameToPropName = /* @__PURE__ */ new Map();
2778
+ prototype.attributeChangedCallback = function(attrName, oldValue, newValue) {
2779
+ plt.jmp(() => {
2780
+ var _a2;
2781
+ const propName = attrNameToPropName.get(attrName);
2782
+ const hostRef = getHostRef(this);
2783
+ if (this.hasOwnProperty(propName) && BUILD.lazyLoad) {
2784
+ newValue = this[propName];
2785
+ delete this[propName];
2786
+ }
2787
+ if (prototype.hasOwnProperty(propName) && typeof this[propName] === "number" && // cast type to number to avoid TS compiler issues
2788
+ this[propName] == newValue) {
2789
+ return;
2790
+ } else if (propName == null) {
2791
+ const flags2 = hostRef == null ? void 0 : hostRef.$flags$;
2792
+ if (hostRef && flags2 && !(flags2 & 8 /* isConstructingInstance */) && newValue !== oldValue) {
2793
+ const instance = hostRef.$lazyInstance$ ;
2794
+ const entry = (_a2 = cmpMeta.$watchers$) == null ? void 0 : _a2[attrName];
2795
+ entry == null ? void 0 : entry.forEach((watcher) => {
2796
+ const [[watchMethodName, watcherFlags]] = Object.entries(watcher);
2797
+ if (instance[watchMethodName] != null && (flags2 & 128 /* isWatchReady */ || watcherFlags & 1 /* Immediate */)) {
2798
+ instance[watchMethodName].call(instance, newValue, oldValue, attrName);
2799
+ }
2800
+ });
2801
+ }
2802
+ return;
2803
+ }
2804
+ const propFlags = members.find(([m]) => m === propName);
2805
+ if (propFlags && propFlags[1][0] & 4 /* Boolean */) {
2806
+ newValue = newValue === null || newValue === "false" ? false : true;
2807
+ }
2808
+ const propDesc = Object.getOwnPropertyDescriptor(prototype, propName);
2809
+ if (newValue != this[propName] && (!propDesc.get || !!propDesc.set)) {
2810
+ this[propName] = newValue;
2811
+ }
2812
+ });
2813
+ };
2814
+ Cstr.observedAttributes = Array.from(
2815
+ /* @__PURE__ */ new Set([
2816
+ ...Object.keys((_b = cmpMeta.$watchers$) != null ? _b : {}),
2817
+ ...members.filter(([_, m]) => m[0] & 31 /* HasAttribute */).map(([propName, m]) => {
2818
+ const attrName = m[1] || propName;
2819
+ attrNameToPropName.set(attrName, propName);
2820
+ return attrName;
2821
+ })
2822
+ ])
2823
+ );
2824
+ }
2768
2825
  }
2769
2826
  return Cstr;
2770
2827
  };
@@ -1,11 +1,11 @@
1
- import { b as bootstrapLazy } from './index-CoGw-MK9.js';
2
- export { s as setNonce } from './index-CoGw-MK9.js';
1
+ import { b as bootstrapLazy } from './index-D-FhYfR4.js';
2
+ export { s as setNonce } from './index-D-FhYfR4.js';
3
3
  import { g as globalScripts } from './app-globals-DQuL1Twl.js';
4
4
 
5
5
  const defineCustomElements = async (win, options) => {
6
6
  if (typeof window === 'undefined') return undefined;
7
7
  await globalScripts();
8
- return bootstrapLazy([["mb-sliding-puzzle",[[513,"mb-sliding-puzzle",{"tiles":[1040],"size":[32],"checkAnswer":[64]}]]]], options);
8
+ return bootstrapLazy([["mb-sliding-puzzle",[[513,"mb-sliding-puzzle",{"tiles":[1040],"freeMove":[4,"free-move"],"size":[32],"checkAnswer":[64]}]]]], options);
9
9
  };
10
10
 
11
11
  export { defineCustomElements };
@@ -1,4 +1,4 @@
1
- import { r as registerInstance, h } from './index-CoGw-MK9.js';
1
+ import { r as registerInstance, h } from './index-D-FhYfR4.js';
2
2
 
3
3
  class Vec2 {
4
4
  x;
@@ -75,6 +75,12 @@ class Grid {
75
75
  getVecFromIndex(index) {
76
76
  return Grid.getVecFromIndex(index, this.size.width);
77
77
  }
78
+ getAllIndexes() {
79
+ return Grid.getAllIndexes(this.size);
80
+ }
81
+ getAllCells() {
82
+ return Grid.getAllCells(this.size);
83
+ }
78
84
  isBlack(v) {
79
85
  return Boolean((v.x + v.y) % 2);
80
86
  }
@@ -84,6 +90,12 @@ class Grid {
84
90
  isInGrid(v) {
85
91
  return Grid.isInGrid(v, this.size);
86
92
  }
93
+ static getAllIndexes(v) {
94
+ return Array(v.width * v.height).fill(true).map((_, i) => i);
95
+ }
96
+ static getAllCells(v) {
97
+ return Array(v.width * v.height).fill(true).map((_, i) => Grid.getVecFromIndex(i, v.width));
98
+ }
87
99
  static getVecFromIndex(index, width) {
88
100
  const _w = width instanceof Vec2 ? width.width : width;
89
101
  const x = index % _w;
@@ -109,7 +121,7 @@ class Grid {
109
121
  }
110
122
  }
111
123
 
112
- const slidingPuzzleCss = () => `.board{width:100%;height:100%;display:inline-block;aspect-ratio:calc(var(--width, 1) / var(--height, 1));border:1px solid black;position:relative;container-type:size}.tile{position:absolute;width:calc(100cqw / var(--width, 1));height:calc(100cqh / var(--height, 1));padding:0;border:none;transform:translate(calc(100% * var(--x)), calc(100% * var(--y)));transition:transform 0.5s}.tile img{display:block;width:100%;height:100%;pointer-events:none}`;
124
+ const slidingPuzzleCss = () => `.board{width:100%;height:100%;display:inline-block;aspect-ratio:calc(var(--width, 1) / var(--height, 1));border:1px solid black;position:relative}.tile{position:absolute;width:calc(100% / var(--width, 1));height:calc(100% / var(--height, 1));padding:0;border:none;transform:translate(calc(100% * var(--x)), calc(100% * var(--y)));transition:transform 0.5s}.tile img{display:block;width:100%;height:100%;pointer-events:none}`;
113
125
 
114
126
  const MbSlidingPuzzle = class {
115
127
  constructor(hostRef) {
@@ -117,6 +129,7 @@ const MbSlidingPuzzle = class {
117
129
  }
118
130
  size = new Vec2(3, 3);
119
131
  tiles = [];
132
+ freeMove = false;
120
133
  async checkAnswer() {
121
134
  return this.tiles.every(t => t.position === t.correctPosition);
122
135
  }
@@ -129,14 +142,30 @@ const MbSlidingPuzzle = class {
129
142
  }
130
143
  onTileClicked(id) {
131
144
  const tile = this.getTileById(id);
145
+ if (tile.blocked)
146
+ return;
132
147
  const v = Grid.getVecFromIndex(tile.position, this.size.width);
133
- const siblings = Grid.getOrtoSiblings(v, this.size);
134
- const emptySibling = siblings.find(v => {
135
- const index = Grid.getIndexFromVec(v, this.size.width);
136
- return this.tiles.every(t => t.position !== index);
137
- });
138
- if (emptySibling) {
139
- this.setTilePosition(tile, Grid.getIndexFromVec(emptySibling, this.size));
148
+ const target = this.getMoveTarget(v);
149
+ if (target) {
150
+ this.setTilePosition(tile, Grid.getIndexFromVec(target, this.size));
151
+ }
152
+ }
153
+ getMoveTarget(source) {
154
+ if (this.freeMove) {
155
+ const cells = Grid.getAllCells(this.size);
156
+ const emptyCell = cells.find(c => {
157
+ const index = Grid.getIndexFromVec(c, this.size.width);
158
+ return this.tiles.every(t => t.position !== index);
159
+ });
160
+ return emptyCell ?? null;
161
+ }
162
+ else {
163
+ const siblings = Grid.getOrtoSiblings(source, this.size);
164
+ const emptySibling = siblings.find(v => {
165
+ const index = Grid.getIndexFromVec(v, this.size.width);
166
+ return this.tiles.every(t => t.position !== index);
167
+ });
168
+ return emptySibling ?? null;
140
169
  }
141
170
  }
142
171
  setTilePosition(tile, position) {
@@ -151,7 +180,7 @@ const MbSlidingPuzzle = class {
151
180
  });
152
181
  }
153
182
  render() {
154
- return h("div", { key: 'b22497e26d2ce6f7aeadc788f9475bb9fcd92e76', class: "board", style: {
183
+ return h("div", { key: 'd6042863922edef06b51cf3897c4cdad5b39b3c7', class: "board", style: {
155
184
  '--width': `${this.size.width}`,
156
185
  '--height': `${this.size.height}`,
157
186
  } }, this.tiles.map(tile => {
@@ -1,5 +1,5 @@
1
- import { p as promiseResolve, b as bootstrapLazy } from './index-CoGw-MK9.js';
2
- export { s as setNonce } from './index-CoGw-MK9.js';
1
+ import { p as promiseResolve, b as bootstrapLazy } from './index-D-FhYfR4.js';
2
+ export { s as setNonce } from './index-D-FhYfR4.js';
3
3
  import { g as globalScripts } from './app-globals-DQuL1Twl.js';
4
4
 
5
5
  /*
@@ -17,5 +17,5 @@ var patchBrowser = () => {
17
17
 
18
18
  patchBrowser().then(async (options) => {
19
19
  await globalScripts();
20
- return bootstrapLazy([["mb-sliding-puzzle",[[513,"mb-sliding-puzzle",{"tiles":[1040],"size":[32],"checkAnswer":[64]}]]]], options);
20
+ return bootstrapLazy([["mb-sliding-puzzle",[[513,"mb-sliding-puzzle",{"tiles":[1040],"freeMove":[4,"free-move"],"size":[32],"checkAnswer":[64]}]]]], options);
21
21
  });