@novely/core 0.38.1 → 0.38.2

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
@@ -59,8 +59,7 @@ function klona(val) {
59
59
  var k, out, tmp;
60
60
  if (Array.isArray(val)) {
61
61
  out = Array(k = val.length);
62
- while (k--)
63
- out[k] = (tmp = val[k]) && typeof tmp === "object" ? klona(tmp) : tmp;
62
+ while (k--) out[k] = (tmp = val[k]) && typeof tmp === "object" ? klona(tmp) : tmp;
64
63
  return out;
65
64
  }
66
65
  if (Object.prototype.toString.call(val) === "[object Object]") {
@@ -97,13 +96,11 @@ var matchAction = ({ getContext, onBeforeActionCall, push, forward }, values) =>
97
96
  ctx: context,
98
97
  data,
99
98
  push() {
100
- if (context.meta.preview)
101
- return;
99
+ if (context.meta.preview) return;
102
100
  push(context);
103
101
  },
104
102
  forward() {
105
- if (context.meta.preview)
106
- return;
103
+ if (context.meta.preview) return;
107
104
  forward(context);
108
105
  }
109
106
  }, props);
@@ -225,8 +222,7 @@ var flattenStory = (story) => {
225
222
  const flat = (item) => {
226
223
  return item.flatMap((data) => {
227
224
  const type = data[0];
228
- if (Array.isArray(type))
229
- return flat(data);
225
+ if (Array.isArray(type)) return flat(data);
230
226
  return [data];
231
227
  });
232
228
  };
@@ -287,8 +283,7 @@ var getActionsFromPath = (story, path, filter) => {
287
283
  }
288
284
  for (let i = startIndex; i <= val; i++) {
289
285
  const item = current[i];
290
- if (!isAction(item))
291
- continue;
286
+ if (!isAction(item)) continue;
292
287
  const [action] = item;
293
288
  const last = index === max && i === val;
294
289
  const shouldSkip = isSkippedDuringRestore(action) || isUserRequiredAction(item);
@@ -346,16 +341,14 @@ var createQueueProcessor = (queue, options) => {
346
341
  const fn = params[0];
347
342
  if ("callOnlyLatest" in fn && fn.callOnlyLatest) {
348
343
  const notLatest = next(i).some(([, func]) => {
349
- if (!isFunction(func))
350
- return false;
344
+ if (!isFunction(func)) return false;
351
345
  const c0 = func;
352
346
  const c1 = fn;
353
347
  const isIdenticalID = Boolean(c0.id && c1.id && c0.id === c1.id);
354
348
  const isIdenticalByReference = c0 === c1;
355
349
  return isIdenticalID || isIdenticalByReference || str(c0) === str(c1);
356
350
  });
357
- if (notLatest)
358
- continue;
351
+ if (notLatest) continue;
359
352
  } else if ("skipOnRestore" in fn && fn.skipOnRestore) {
360
353
  let getNext = () => {
361
354
  const nextActions = next(i);
@@ -364,23 +357,21 @@ var createQueueProcessor = (queue, options) => {
364
357
  };
365
358
  return nextActions;
366
359
  };
367
- if (fn.skipOnRestore(getNext))
368
- continue;
360
+ if (fn.skipOnRestore(getNext)) continue;
369
361
  }
370
362
  }
371
363
  processedQueue.push(item);
372
364
  } else if (action === "showCharacter" || action === "playSound" || action === "playMusic" || action === "voice") {
373
365
  const closing = getOppositeAction(action);
374
366
  const skip = next(i).some(([_action, target]) => {
375
- if (target !== params[0]) {
367
+ if (target !== params[0] && action !== "voice") {
376
368
  return false;
377
369
  }
378
370
  const musicGonnaBePaused = action === "playMusic" && _action === "pauseMusic";
379
371
  const soundGonnaBePaused = action === "playSound" && _action === "pauseSound";
380
372
  return musicGonnaBePaused || soundGonnaBePaused || _action === closing || _action === action;
381
373
  });
382
- if (skip)
383
- continue;
374
+ if (skip) continue;
384
375
  if (action === "showCharacter") {
385
376
  characters.add(params[0]);
386
377
  } else if (action === "playMusic") {
@@ -391,8 +382,7 @@ var createQueueProcessor = (queue, options) => {
391
382
  processedQueue.push(item);
392
383
  } else if (action === "showBackground" || action === "preload") {
393
384
  const skip = next(i).some(([_action]) => action === _action);
394
- if (skip)
395
- continue;
385
+ if (skip) continue;
396
386
  processedQueue.push(item);
397
387
  } else if (action === "animateCharacter") {
398
388
  const skip = next(i).some(([_action, character], j, array) => {
@@ -404,8 +394,7 @@ var createQueueProcessor = (queue, options) => {
404
394
  const hasBlockingActions = next2.some((item2) => options.skip.has(item2));
405
395
  return characterWillAnimate && hasBlockingActions;
406
396
  });
407
- if (skip)
408
- continue;
397
+ if (skip) continue;
409
398
  processedQueue.push(item);
410
399
  } else {
411
400
  processedQueue.push(item);
@@ -432,8 +421,7 @@ var createQueueProcessor = (queue, options) => {
432
421
  var getStack = (ctx) => {
433
422
  const { id } = ctx;
434
423
  const cached = STACK_MAP.get(id);
435
- if (cached)
436
- return cached;
424
+ if (cached) return cached;
437
425
  const stack = [];
438
426
  STACK_MAP.set(id, stack);
439
427
  return stack;
@@ -595,16 +583,14 @@ var exitPath = ({ path, refer, onExitImpossible }) => {
595
583
  if (isBlockExitStatement(name)) {
596
584
  ignore.push(name);
597
585
  }
598
- if (!isBlockStatement(name))
599
- continue;
586
+ if (!isBlockStatement(name)) continue;
600
587
  if (ignore.at(-1)?.startsWith(name)) {
601
588
  ignore.pop();
602
589
  continue;
603
590
  }
604
591
  path.push([`${name}:exit`]);
605
592
  const prev = findLastPathItemBeforeItemOfType(path.slice(0, i + 1), name);
606
- if (prev)
607
- path.push([null, prev[1] + 1]);
593
+ if (prev) path.push([null, prev[1] + 1]);
608
594
  if (!isAction(refer(path))) {
609
595
  path.pop();
610
596
  continue;
@@ -649,8 +635,7 @@ var collectActionsBeforeBlockingAction = ({ path, refer }) => {
649
635
  const choiceProps = props;
650
636
  for (let i = 0; i < choiceProps.length; i++) {
651
637
  const branchContent = choiceProps[i];
652
- if (!Array.isArray(branchContent))
653
- continue;
638
+ if (!Array.isArray(branchContent)) continue;
654
639
  const virtualPath = klona(path);
655
640
  virtualPath.push(["choice", i], [null, 0]);
656
641
  const innerActions = collectActionsBeforeBlockingAction({
@@ -699,8 +684,7 @@ var store = (current, subscribers = /* @__PURE__ */ new Set()) => {
699
684
  };
700
685
  };
701
686
  const push = (value) => {
702
- for (const cb of subscribers)
703
- cb(value);
687
+ for (const cb of subscribers) cb(value);
704
688
  };
705
689
  const update = (fn) => {
706
690
  push(current = fn(current));
@@ -730,8 +714,7 @@ var propertyIsUnsafe = (target, key) => {
730
714
  propertyIsEnumerable.call(target, key));
731
715
  };
732
716
  var getEnumerableOwnPropertySymbols = (target) => {
733
- if (!getOwnPropertySymbols)
734
- return [];
717
+ if (!getOwnPropertySymbols) return [];
735
718
  return getOwnPropertySymbols(target).filter((symbol) => propertyIsEnumerable.call(target, symbol));
736
719
  };
737
720
  var keys = (target) => {
@@ -793,8 +776,7 @@ var RGX = /{{(.*?)}}/g;
793
776
  var split = (input, delimeters) => {
794
777
  const output = [];
795
778
  for (const delimeter of delimeters) {
796
- if (!input)
797
- break;
779
+ if (!input) break;
798
780
  const [start, end] = input.split(delimeter, 2);
799
781
  output.push(start);
800
782
  input = end;
@@ -820,14 +802,12 @@ var replace = (input, data, pluralization, actions, pr) => {
820
802
  return "";
821
803
  }
822
804
  const path = pathstr.split(".");
823
- while (y && x < path.length)
824
- y = y[path[x++]];
805
+ while (y && x < path.length) y = y[path[x++]];
825
806
  if (plural && pluralization && y && pr) {
826
807
  y = pluralization[plural][pr.select(y)];
827
808
  }
828
809
  const actionHandler = actions && action ? actions[action] : void 0;
829
- if (actionHandler)
830
- y = actionHandler(y);
810
+ if (actionHandler) y = actionHandler(y);
831
811
  return y == null ? "" : y;
832
812
  });
833
813
  };
@@ -925,8 +905,7 @@ var localStorageStorage = (options) => {
925
905
 
926
906
  // src/browser.ts
927
907
  var setupBrowserVisibilityChangeListeners = ({ onChange }) => {
928
- if (typeof document === "undefined")
929
- return noop;
908
+ if (typeof document === "undefined") return noop;
930
909
  const onVisibilityChange = () => {
931
910
  if (document.visibilityState === "hidden") {
932
911
  onChange();
@@ -1004,8 +983,7 @@ var novely = ({
1004
983
  }
1005
984
  };
1006
985
  const scriptBase = async (part) => {
1007
- if (destroyed)
1008
- return;
986
+ if (destroyed) return;
1009
987
  Object.assign(story, flattenStory(part));
1010
988
  let loadingIsShown = false;
1011
989
  if (!initialScreenWasShown) {
@@ -1039,8 +1017,7 @@ var novely = ({
1039
1017
  }
1040
1018
  if (props[0] && typeof props[0] === "object") {
1041
1019
  for (const value of Object.values(props[0])) {
1042
- if (!isImageAsset(value))
1043
- continue;
1020
+ if (!isImageAsset(value)) continue;
1044
1021
  doAction(value);
1045
1022
  }
1046
1023
  }
@@ -1121,8 +1098,7 @@ var novely = ({
1121
1098
  };
1122
1099
  dataLoaded.promise.then(onDataLoadedPromise);
1123
1100
  const onStorageDataChange = (value) => {
1124
- if (coreData.get().dataLoaded)
1125
- storage.set(value);
1101
+ if (coreData.get().dataLoaded) storage.set(value);
1126
1102
  };
1127
1103
  const throttledOnStorageDataChange = throttle(onStorageDataChange, throttleTimeout);
1128
1104
  const throttledEmergencyOnStorageDataChange = throttle(() => {
@@ -1158,10 +1134,8 @@ var novely = ({
1158
1134
  onChange: throttledEmergencyOnStorageDataChange
1159
1135
  });
1160
1136
  const save = (type) => {
1161
- if (!coreData.get().dataLoaded)
1162
- return;
1163
- if (!autosaves && type === "auto")
1164
- return;
1137
+ if (!coreData.get().dataLoaded) return;
1138
+ if (!autosaves && type === "auto") return;
1165
1139
  const stack = useStack(MAIN_CONTEXT_KEY);
1166
1140
  const current = klona(stack.value);
1167
1141
  storageData.update((prev) => {
@@ -1174,8 +1148,7 @@ var novely = ({
1174
1148
  return prev;
1175
1149
  };
1176
1150
  const last = prev.saves.at(-1);
1177
- if (!last)
1178
- return add();
1151
+ if (!last) return add();
1179
1152
  current[2][0] = intime(Date.now());
1180
1153
  current[2][1] = type;
1181
1154
  const isIdentical = dequal(last[0], current[0]) && dequal(last[1], current[1]);
@@ -1193,8 +1166,7 @@ var novely = ({
1193
1166
  });
1194
1167
  };
1195
1168
  const newGame = () => {
1196
- if (!coreData.get().dataLoaded)
1197
- return;
1169
+ if (!coreData.get().dataLoaded) return;
1198
1170
  const save2 = getDefaultSave(klona(defaultState));
1199
1171
  if (autosaves) {
1200
1172
  storageData.update((prev) => {
@@ -1221,8 +1193,7 @@ var novely = ({
1221
1193
  }
1222
1194
  return;
1223
1195
  }
1224
- if (!coreData.get().dataLoaded)
1225
- return;
1196
+ if (!coreData.get().dataLoaded) return;
1226
1197
  let latest = save2 || storageData.get().saves.at(-1);
1227
1198
  if (!latest) {
1228
1199
  latest = klona(initial);
@@ -1264,8 +1235,7 @@ var novely = ({
1264
1235
  const lastQueueItem = queue.at(-1);
1265
1236
  const lastQueueItemRequiresUserAction = lastQueueItem && isBlockingAction(lastQueueItem);
1266
1237
  await run((item) => {
1267
- if (!latest)
1268
- return;
1238
+ if (!latest) return;
1269
1239
  if (lastQueueItem === item && lastQueueItemRequiresUserAction) {
1270
1240
  return;
1271
1241
  }
@@ -1341,12 +1311,9 @@ var novely = ({
1341
1311
  useStack(ctx).push(klona(save2));
1342
1312
  const assets = [];
1343
1313
  await processor.run(([action2, ...props]) => {
1344
- if (isAudioAction(action2))
1345
- return;
1346
- if (action2 === "vibrate")
1347
- return;
1348
- if (action2 === "end")
1349
- return;
1314
+ if (isAudioAction(action2)) return;
1315
+ if (action2 === "vibrate") return;
1316
+ if (action2 === "end") return;
1350
1317
  assetPreloadingCheck(action2, props, assets.push.bind(assets));
1351
1318
  return match(action2, props, {
1352
1319
  ctx,
@@ -1415,8 +1382,7 @@ var novely = ({
1415
1382
  useStack(MAIN_CONTEXT_KEY).push(initial);
1416
1383
  const UIInstance = renderer.ui.start();
1417
1384
  const enmemory = (ctx) => {
1418
- if (ctx.meta.restoring)
1419
- return;
1385
+ if (ctx.meta.restoring) return;
1420
1386
  const stack = useStack(ctx);
1421
1387
  const current = klona(stack.value);
1422
1388
  current[2][1] = "auto";
@@ -1431,25 +1397,19 @@ var novely = ({
1431
1397
  const matchActionOptions = {
1432
1398
  getContext: renderer.getContext,
1433
1399
  push(ctx) {
1434
- if (ctx.meta.restoring)
1435
- return;
1400
+ if (ctx.meta.restoring) return;
1436
1401
  next(ctx);
1437
1402
  render(ctx);
1438
1403
  },
1439
1404
  forward(ctx) {
1440
- if (!ctx.meta.preview)
1441
- enmemory(ctx);
1405
+ if (!ctx.meta.preview) enmemory(ctx);
1442
1406
  matchActionOptions.push(ctx);
1443
- if (!ctx.meta.preview)
1444
- interactivity(true);
1407
+ if (!ctx.meta.preview) interactivity(true);
1445
1408
  },
1446
1409
  onBeforeActionCall({ action: action2, props, ctx }) {
1447
- if (preloadAssets !== "automatic")
1448
- return;
1449
- if (ctx.meta.preview || ctx.meta.restoring)
1450
- return;
1451
- if (!isBlockingAction([action2, ...props]))
1452
- return;
1410
+ if (preloadAssets !== "automatic") return;
1411
+ if (ctx.meta.preview || ctx.meta.restoring) return;
1412
+ if (!isBlockingAction([action2, ...props])) return;
1453
1413
  try {
1454
1414
  const collection = collectActionsBeforeBlockingAction({
1455
1415
  path: nextPath(klona(useStack(ctx).value[0])),
@@ -1484,8 +1444,7 @@ var novely = ({
1484
1444
  };
1485
1445
  const match = matchAction(matchActionOptions, {
1486
1446
  wait({ ctx, push }, [time]) {
1487
- if (ctx.meta.restoring)
1488
- return;
1447
+ if (ctx.meta.restoring) return;
1489
1448
  setTimeout(push, isFunction(time) ? time(getStateAtCtx(ctx)) : time);
1490
1449
  },
1491
1450
  showBackground({ ctx, push }, [background]) {
@@ -1535,8 +1494,7 @@ var novely = ({
1535
1494
  if (DEV2 && !emotion) {
1536
1495
  throw new Error(`Attemp to show character "${character}" without emotion provided.`);
1537
1496
  }
1538
- if (!emotion)
1539
- return;
1497
+ if (!emotion) return;
1540
1498
  if (DEV2 && !characters[character].emotions[emotion]) {
1541
1499
  throw new Error(`Attempt to show character "${character}" with unknown emotion "${emotion}"`);
1542
1500
  }
@@ -1674,8 +1632,7 @@ var novely = ({
1674
1632
  }
1675
1633
  },
1676
1634
  end({ ctx }) {
1677
- if (ctx.meta.preview)
1678
- return;
1635
+ if (ctx.meta.preview) return;
1679
1636
  exit(true, false);
1680
1637
  },
1681
1638
  input({ ctx, data: data2, forward }, [question, onInput, setup]) {
@@ -1726,8 +1683,7 @@ var novely = ({
1726
1683
  if (DEV2 && classes.length === 0) {
1727
1684
  throw new Error("Attempt to use AnimateCharacter without classes. Classes should be provided [https://novely.pages.dev/guide/actions/animateCharacter.html]");
1728
1685
  }
1729
- if (ctx.meta.preview)
1730
- return;
1686
+ if (ctx.meta.preview) return;
1731
1687
  ctx.character(character).animate(classes);
1732
1688
  push();
1733
1689
  },
@@ -1740,8 +1696,7 @@ var novely = ({
1740
1696
  ctx.text(string, forward);
1741
1697
  },
1742
1698
  exit({ ctx, data: data2 }) {
1743
- if (ctx.meta.restoring)
1744
- return;
1699
+ if (ctx.meta.restoring) return;
1745
1700
  const { exitImpossible } = exitPath({
1746
1701
  path: useStack(ctx).value[0],
1747
1702
  refer,
@@ -1824,8 +1779,7 @@ var novely = ({
1824
1779
  };
1825
1780
  const data = (value) => {
1826
1781
  const _data = storageData.get().data;
1827
- if (!value)
1828
- return _data;
1782
+ if (!value) return _data;
1829
1783
  const val = isFunction(value) ? value(_data) : deepmerge(_data, value);
1830
1784
  storageData.update((prev) => {
1831
1785
  prev.data = val;