@novely/core 0.40.2 → 0.41.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.
package/dist/index.js CHANGED
@@ -53,35 +53,6 @@ var ASSETS_TO_PRELOAD = /* @__PURE__ */ new Set();
53
53
 
54
54
  // src/utils.ts
55
55
  import { DEV as DEV2 } from "esm-env";
56
-
57
- // ../../node_modules/.pnpm/klona@2.0.6/node_modules/klona/json/index.mjs
58
- function klona(val) {
59
- var k, out, tmp;
60
- if (Array.isArray(val)) {
61
- out = Array(k = val.length);
62
- while (k--) out[k] = (tmp = val[k]) && typeof tmp === "object" ? klona(tmp) : tmp;
63
- return out;
64
- }
65
- if (Object.prototype.toString.call(val) === "[object Object]") {
66
- out = {};
67
- for (k in val) {
68
- if (k === "__proto__") {
69
- Object.defineProperty(out, k, {
70
- value: klona(val[k]),
71
- configurable: true,
72
- enumerable: true,
73
- writable: true
74
- });
75
- } else {
76
- out[k] = (tmp = val[k]) && typeof tmp === "object" ? klona(tmp) : tmp;
77
- }
78
- }
79
- return out;
80
- }
81
- return val;
82
- }
83
-
84
- // src/utils.ts
85
56
  import { memoize as memoize2 } from "es-toolkit/function";
86
57
 
87
58
  // src/asset.ts
@@ -560,8 +531,8 @@ var createUseStackFunction = (renderer) => {
560
531
  };
561
532
  return useStack;
562
533
  };
563
- var mapSet = (set, fn) => {
564
- return [...set].map(fn);
534
+ var mapSet = (set2, fn) => {
535
+ return [...set2].map(fn);
565
536
  };
566
537
  var isImageAsset = (asset2) => {
567
538
  return isString(asset2) && isCSSImage(asset2);
@@ -713,7 +684,7 @@ var nextPath = (path) => {
713
684
  var isBlockingAction = (action) => {
714
685
  return isUserRequiredAction(action) || isSkippedDuringRestore(action[0]) && action[0] !== "vibrate";
715
686
  };
716
- var collectActionsBeforeBlockingAction = ({ path, refer }) => {
687
+ var collectActionsBeforeBlockingAction = ({ path, refer, clone }) => {
717
688
  const collection = [];
718
689
  let action = refer(path);
719
690
  while (true) {
@@ -736,11 +707,12 @@ var collectActionsBeforeBlockingAction = ({ path, refer }) => {
736
707
  for (let i = 0; i < choiceProps.length; i++) {
737
708
  const branchContent = choiceProps[i];
738
709
  if (!Array.isArray(branchContent)) continue;
739
- const virtualPath = klona(path);
710
+ const virtualPath = clone(path);
740
711
  virtualPath.push(["choice", i], [null, 0]);
741
712
  const innerActions = collectActionsBeforeBlockingAction({
742
713
  path: virtualPath,
743
- refer
714
+ refer,
715
+ clone
744
716
  });
745
717
  collection.push(...innerActions);
746
718
  }
@@ -748,11 +720,12 @@ var collectActionsBeforeBlockingAction = ({ path, refer }) => {
748
720
  const conditionProps = props;
749
721
  const conditions = Object.keys(conditionProps[1]);
750
722
  for (const condition of conditions) {
751
- const virtualPath = klona(path);
723
+ const virtualPath = clone(path);
752
724
  virtualPath.push(["condition", condition], [null, 0]);
753
725
  const innerActions = collectActionsBeforeBlockingAction({
754
726
  path: virtualPath,
755
- refer
727
+ refer,
728
+ clone
756
729
  });
757
730
  collection.push(...innerActions);
758
731
  }
@@ -825,15 +798,62 @@ var store = (current, subscribers = /* @__PURE__ */ new Set()) => {
825
798
  const update = (fn) => {
826
799
  push(current = fn(current));
827
800
  };
828
- const set = (val) => {
801
+ const set2 = (val) => {
829
802
  update(() => val);
830
803
  };
831
804
  const get = () => {
832
805
  return current;
833
806
  };
834
- return { subscribe, update, set, get };
807
+ return { subscribe, update, set: set2, get };
835
808
  };
836
809
 
810
+ // ../../node_modules/.pnpm/klona@2.0.6/node_modules/klona/full/index.mjs
811
+ function set(obj, key, val) {
812
+ if (typeof val.value === "object") val.value = klona(val.value);
813
+ if (!val.enumerable || val.get || val.set || !val.configurable || !val.writable || key === "__proto__") {
814
+ Object.defineProperty(obj, key, val);
815
+ } else obj[key] = val.value;
816
+ }
817
+ function klona(x) {
818
+ if (typeof x !== "object") return x;
819
+ var i = 0, k, list, tmp, str2 = Object.prototype.toString.call(x);
820
+ if (str2 === "[object Object]") {
821
+ tmp = Object.create(x.__proto__ || null);
822
+ } else if (str2 === "[object Array]") {
823
+ tmp = Array(x.length);
824
+ } else if (str2 === "[object Set]") {
825
+ tmp = /* @__PURE__ */ new Set();
826
+ x.forEach(function(val) {
827
+ tmp.add(klona(val));
828
+ });
829
+ } else if (str2 === "[object Map]") {
830
+ tmp = /* @__PURE__ */ new Map();
831
+ x.forEach(function(val, key) {
832
+ tmp.set(klona(key), klona(val));
833
+ });
834
+ } else if (str2 === "[object Date]") {
835
+ tmp = /* @__PURE__ */ new Date(+x);
836
+ } else if (str2 === "[object RegExp]") {
837
+ tmp = new RegExp(x.source, x.flags);
838
+ } else if (str2 === "[object DataView]") {
839
+ tmp = new x.constructor(klona(x.buffer));
840
+ } else if (str2 === "[object ArrayBuffer]") {
841
+ tmp = x.slice(0);
842
+ } else if (str2.slice(-6) === "Array]") {
843
+ tmp = new x.constructor(x);
844
+ }
845
+ if (tmp) {
846
+ for (list = Object.getOwnPropertySymbols(x); i < list.length; i++) {
847
+ set(tmp, list[i], Object.getOwnPropertyDescriptor(x, list[i]));
848
+ }
849
+ for (i = 0, list = Object.getOwnPropertyNames(x); i < list.length; i++) {
850
+ if (Object.hasOwnProperty.call(tmp, k = list[i]) && tmp[k] === x[k]) continue;
851
+ set(tmp, k, Object.getOwnPropertyDescriptor(x, k));
852
+ }
853
+ }
854
+ return tmp || x;
855
+ }
856
+
837
857
  // src/translation.ts
838
858
  var RGX = /{{(.*?)}}/g;
839
859
  var split = (input, delimeters) => {
@@ -1106,6 +1126,7 @@ var novely = ({
1106
1126
  preloadAssets = "lazy",
1107
1127
  parallelAssetsDownloadLimit = 15,
1108
1128
  fetch: request = fetch,
1129
+ cloneFunction: clone = klona,
1109
1130
  saveOnUnload = true,
1110
1131
  startKey = "start"
1111
1132
  }) => {
@@ -1173,7 +1194,7 @@ var novely = ({
1173
1194
  };
1174
1195
  }
1175
1196
  });
1176
- const getDefaultSave = (state = {}) => {
1197
+ const getDefaultSave = (state) => {
1177
1198
  return [
1178
1199
  [
1179
1200
  ["jump", startKey],
@@ -1195,7 +1216,7 @@ var novely = ({
1195
1216
  };
1196
1217
  const initialData = {
1197
1218
  saves: [],
1198
- data: klona(defaultData),
1219
+ data: clone(defaultData),
1199
1220
  meta: [getLanguageWithoutParameters(), DEFAULT_TYPEWRITER_SPEED, 1, 1, 1]
1200
1221
  };
1201
1222
  const storageData = store(initialData);
@@ -1245,7 +1266,7 @@ var novely = ({
1245
1266
  storageData.set(stored);
1246
1267
  };
1247
1268
  storageDelay.then(getStoredData);
1248
- const initial = getDefaultSave(klona(defaultState));
1269
+ const initial = getDefaultSave(clone(defaultState));
1249
1270
  const unsubscribeFromBrowserVisibilityChange = setupBrowserVisibilityChangeListeners({
1250
1271
  onChange: throttledEmergencyOnStorageDataChange
1251
1272
  });
@@ -1253,7 +1274,7 @@ var novely = ({
1253
1274
  if (!coreData.get().dataLoaded) return;
1254
1275
  if (!autosaves && type === "auto") return;
1255
1276
  const stack = useStack(MAIN_CONTEXT_KEY);
1256
- const current = klona(stack.value);
1277
+ const current = clone(stack.value);
1257
1278
  storageData.update((prev) => {
1258
1279
  const replace2 = () => {
1259
1280
  prev.saves[prev.saves.length - 1] = current;
@@ -1283,7 +1304,7 @@ var novely = ({
1283
1304
  };
1284
1305
  const newGame = () => {
1285
1306
  if (!coreData.get().dataLoaded) return;
1286
- const save2 = getDefaultSave(klona(defaultState));
1307
+ const save2 = getDefaultSave(clone(defaultState));
1287
1308
  if (autosaves) {
1288
1309
  storageData.update((prev) => {
1289
1310
  return prev.saves.push(save2), prev;
@@ -1296,7 +1317,7 @@ var novely = ({
1296
1317
  renderer.ui.showScreen("game");
1297
1318
  render(context);
1298
1319
  };
1299
- const set = (save2, ctx) => {
1320
+ const set2 = (save2, ctx) => {
1300
1321
  const stack = useStack(ctx || MAIN_CONTEXT_KEY);
1301
1322
  stack.value = save2;
1302
1323
  return restore(save2);
@@ -1312,7 +1333,7 @@ var novely = ({
1312
1333
  if (!coreData.get().dataLoaded) return;
1313
1334
  let latest = save2 || storageData.get().saves.at(-1);
1314
1335
  if (!latest) {
1315
- latest = klona(initial);
1336
+ latest = clone(initial);
1316
1337
  storageData.update((prev) => {
1317
1338
  prev.saves.push(latest);
1318
1339
  return prev;
@@ -1424,7 +1445,7 @@ var novely = ({
1424
1445
  const processor = createQueueProcessor(queue, {
1425
1446
  skip: EMPTY_SET
1426
1447
  });
1427
- useStack(ctx).push(klona(save2));
1448
+ useStack(ctx).push(clone(save2));
1428
1449
  const assets = [];
1429
1450
  await processor.run(([action2, ...props]) => {
1430
1451
  if (isAudioAction(action2)) return;
@@ -1494,7 +1515,7 @@ var novely = ({
1494
1515
  mainContextKey: MAIN_CONTEXT_KEY,
1495
1516
  characters: getCharactersData(characters),
1496
1517
  characterAssetSizes,
1497
- set,
1518
+ set: set2,
1498
1519
  restore,
1499
1520
  save,
1500
1521
  newGame,
@@ -1519,7 +1540,7 @@ var novely = ({
1519
1540
  const enmemory = (ctx) => {
1520
1541
  if (ctx.meta.restoring) return;
1521
1542
  const stack = useStack(ctx);
1522
- const current = klona(stack.value);
1543
+ const current = clone(stack.value);
1523
1544
  current[2][1] = "auto";
1524
1545
  stack.push(current);
1525
1546
  save("auto");
@@ -1547,8 +1568,9 @@ var novely = ({
1547
1568
  if (!isBlockingAction([action2, ...props])) return;
1548
1569
  try {
1549
1570
  const collection = collectActionsBeforeBlockingAction({
1550
- path: nextPath(klona(useStack(ctx).value[0])),
1551
- refer
1571
+ path: nextPath(clone(useStack(ctx).value[0])),
1572
+ refer,
1573
+ clone
1552
1574
  });
1553
1575
  for (const [action3, ...props2] of collection) {
1554
1576
  huntAssets({
@@ -1930,7 +1952,7 @@ var novely = ({
1930
1952
  c: null
1931
1953
  };
1932
1954
  const getCurrentStorageData = () => {
1933
- return coreData.get().dataLoaded ? klona(storageData.get()) : null;
1955
+ return coreData.get().dataLoaded ? clone(storageData.get()) : null;
1934
1956
  };
1935
1957
  const setStorageData = (data2) => {
1936
1958
  if (destroyed) {
@@ -2003,7 +2025,7 @@ var novely = ({
2003
2025
  */
2004
2026
  typeEssentials,
2005
2027
  /**
2006
- * Replaces content inside {{braces}} with using global data
2028
+ * Replaces content inside {{braces}} using global data
2007
2029
  * @example
2008
2030
  * ```ts
2009
2031
  * data({ name: 'Alexei' })
@@ -2017,6 +2039,12 @@ var novely = ({
2017
2039
  templateReplace(content) {
2018
2040
  return templateReplace(content);
2019
2041
  },
2042
+ /**
2043
+ * Same as `templateReplace` but uses state and requires explicitly providing it
2044
+ */
2045
+ templateReplaceState(content, state) {
2046
+ return templateReplace(content, state);
2047
+ },
2020
2048
  /**
2021
2049
  * Cancel data loading, hide UI, ignore page change events
2022
2050
  * Data updates still will work in case Novely already was loaded