@novely/core 0.22.1 → 0.22.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
@@ -18,12 +18,18 @@ var MAIN_CONTEXT_KEY = "$MAIN";
18
18
  var STACK_MAP = /* @__PURE__ */ new Map();
19
19
 
20
20
  // src/utils.ts
21
- var matchAction = (getContext, values) => {
21
+ var matchAction = ({ getContext, push, forward }, values) => {
22
22
  return (action, props, { ctx, data }) => {
23
23
  const context = typeof ctx === "string" ? getContext(ctx) : ctx;
24
24
  return values[action]({
25
25
  ctx: context,
26
- data
26
+ data,
27
+ push() {
28
+ push(context);
29
+ },
30
+ forward() {
31
+ forward(context);
32
+ }
27
33
  }, props);
28
34
  };
29
35
  };
@@ -687,11 +693,12 @@ var novely = ({
687
693
  };
688
694
  storageDelay.then(getStoredData);
689
695
  const initial = getDefaultSave(klona(defaultState));
690
- addEventListener("visibilitychange", () => {
696
+ const onVisibilityChange = () => {
691
697
  if (document.visibilityState === "hidden") {
692
698
  throttledEmergencyOnStorageDataChange();
693
699
  }
694
- });
700
+ };
701
+ addEventListener("visibilitychange", onVisibilityChange);
695
702
  addEventListener("beforeunload", throttledEmergencyOnStorageDataChange);
696
703
  const save = (override = false, type = override ? "auto" : "manual") => {
697
704
  if (!$$.get().dataLoaded)
@@ -780,7 +787,7 @@ var novely = ({
780
787
  data: latest[1]
781
788
  });
782
789
  });
783
- context.meta.restoring = context.meta.restoring = false;
790
+ context.meta.restoring = context.meta.goingBack = false;
784
791
  render(context);
785
792
  };
786
793
  const refer = (path) => {
@@ -881,55 +888,89 @@ var novely = ({
881
888
  });
882
889
  const useStack = createUseStackFunction(renderer);
883
890
  useStack(MAIN_CONTEXT_KEY).push(initial);
884
- renderer.ui.start();
885
- const match = matchAction(renderer.getContext, {
886
- wait({ ctx }, [time]) {
887
- if (!ctx.meta.restoring)
888
- setTimeout(push, isFunction(time) ? time() : time);
891
+ const UIInstance = renderer.ui.start();
892
+ const enmemory = (ctx) => {
893
+ if (ctx.meta.restoring)
894
+ return;
895
+ const stack = useStack(ctx);
896
+ const current = klona(stack.value);
897
+ current[2][1] = "auto";
898
+ stack.push(current);
899
+ save(true, "auto");
900
+ };
901
+ const next = (ctx) => {
902
+ const stack = useStack(ctx);
903
+ const path = stack.value[0];
904
+ const last = path.at(-1);
905
+ if (last && (isNull(last[0]) || last[0] === "jump") && isNumber(last[1])) {
906
+ last[1]++;
907
+ } else {
908
+ path.push([null, 0]);
909
+ }
910
+ };
911
+ const matchActionInit = {
912
+ getContext: renderer.getContext,
913
+ push(ctx) {
914
+ if (ctx.meta.restoring)
915
+ return;
916
+ next(ctx);
917
+ render(ctx);
918
+ },
919
+ forward(ctx) {
920
+ if (!ctx.meta.preview)
921
+ enmemory(ctx);
922
+ matchActionInit.push(ctx);
923
+ if (!ctx.meta.preview)
924
+ interactivity(true);
925
+ }
926
+ };
927
+ const match = matchAction(matchActionInit, {
928
+ wait({ ctx, push }, [time]) {
929
+ if (ctx.meta.restoring)
930
+ return;
931
+ setTimeout(push, isFunction(time) ? time() : time);
889
932
  },
890
- showBackground({ ctx }, [background]) {
933
+ showBackground({ ctx, push }, [background]) {
891
934
  ctx.background(background);
892
- push(ctx);
935
+ push();
893
936
  },
894
- playMusic({ ctx }, [source]) {
937
+ playMusic({ ctx, push }, [source]) {
895
938
  ctx.audio.music(source, "music", true).play();
896
- push(ctx);
939
+ push();
897
940
  },
898
- stopMusic({ ctx }, [source]) {
941
+ stopMusic({ ctx, push }, [source]) {
899
942
  ctx.audio.music(source, "music").stop();
900
- push(ctx);
943
+ push();
901
944
  },
902
- playSound({ ctx }, [source, loop]) {
945
+ playSound({ ctx, push }, [source, loop]) {
903
946
  ctx.audio.music(source, "sound", loop || false).play();
904
- push(ctx);
947
+ push();
905
948
  },
906
- stopSound({ ctx }, [source]) {
949
+ stopSound({ ctx, push }, [source]) {
907
950
  ctx.audio.music(source, "sound").stop();
908
- push(ctx);
951
+ push();
909
952
  },
910
- voice({ ctx }, [source]) {
953
+ voice({ ctx, push }, [source]) {
911
954
  ctx.audio.voice(source);
912
- push(ctx);
955
+ push();
913
956
  },
914
- stopVoice({ ctx }) {
957
+ stopVoice({ ctx, push }) {
915
958
  ctx.audio.voiceStop();
916
- push(ctx);
959
+ push();
917
960
  },
918
- showCharacter({ ctx }, [character, emotion, className, style]) {
961
+ showCharacter({ ctx, push }, [character, emotion, className, style]) {
919
962
  if (DEV && !characters[character].emotions[emotion]) {
920
963
  throw new Error(`Attempt to show character "${character}" with unknown emotion "${emotion}"`);
921
964
  }
922
965
  const handle = ctx.character(character);
923
966
  handle.append(className, style, ctx.meta.restoring);
924
967
  handle.emotion(emotion, true);
925
- push(ctx);
968
+ push();
926
969
  },
927
- hideCharacter({ ctx }, [character, className, style, duration]) {
928
- ctx.character(character).remove(className, style, duration, ctx.meta.restoring).then(() => {
929
- push(ctx);
930
- });
970
+ hideCharacter({ ctx, push }, [character, className, style, duration]) {
971
+ ctx.character(character).remove(className, style, duration, ctx.meta.restoring).then(push);
931
972
  },
932
- dialog({ ctx, data: data2 }, [character, content, emotion]) {
973
+ dialog({ ctx, data: data2, forward }, [character, content, emotion]) {
933
974
  const name = (() => {
934
975
  const c = character;
935
976
  const cs = characters;
@@ -950,13 +991,13 @@ var novely = ({
950
991
  unwrap2(name, data2),
951
992
  character,
952
993
  emotion,
953
- () => forward(ctx)
994
+ forward
954
995
  );
955
996
  },
956
- function({ ctx }, [fn]) {
997
+ function({ ctx, push }, [fn]) {
957
998
  const result = fn(ctx.meta.restoring, ctx.meta.goingBack, ctx.meta.preview);
958
999
  if (!ctx.meta.restoring) {
959
- result ? result.then(() => push(ctx)) : push(ctx);
1000
+ result ? result.then(push) : push();
960
1001
  }
961
1002
  return result;
962
1003
  },
@@ -1006,13 +1047,13 @@ var novely = ({
1006
1047
  data: data2
1007
1048
  });
1008
1049
  },
1009
- clear({ ctx }, [keep, characters2, audio]) {
1050
+ clear({ ctx, push }, [keep, characters2, audio]) {
1010
1051
  ctx.vibrate(0);
1011
1052
  ctx.clear(
1012
1053
  keep || EMPTY_SET,
1013
1054
  characters2 || EMPTY_SET,
1014
1055
  audio || { music: EMPTY_SET, sounds: EMPTY_SET },
1015
- () => push(ctx)
1056
+ push
1016
1057
  );
1017
1058
  },
1018
1059
  condition({ ctx }, [condition, variants]) {
@@ -1041,15 +1082,15 @@ var novely = ({
1041
1082
  interactivity(false);
1042
1083
  times.clear();
1043
1084
  },
1044
- input({ ctx, data: data2 }, [question, onInput, setup]) {
1085
+ input({ ctx, data: data2, forward }, [question, onInput, setup]) {
1045
1086
  ctx.input(
1046
1087
  unwrap2(question, data2),
1047
1088
  onInput,
1048
1089
  setup || noop,
1049
- () => forward(ctx)
1090
+ forward
1050
1091
  );
1051
1092
  },
1052
- custom({ ctx }, [handler]) {
1093
+ custom({ ctx, push }, [handler]) {
1053
1094
  const result = ctx.custom(handler, () => {
1054
1095
  if (ctx.meta.restoring)
1055
1096
  return;
@@ -1057,16 +1098,16 @@ var novely = ({
1057
1098
  enmemory(ctx);
1058
1099
  interactivity(true);
1059
1100
  }
1060
- push(ctx);
1101
+ push();
1061
1102
  });
1062
1103
  return result;
1063
1104
  },
1064
- vibrate({ ctx }, pattern) {
1105
+ vibrate({ ctx, push }, pattern) {
1065
1106
  ctx.vibrate(pattern);
1066
- push(ctx);
1107
+ push();
1067
1108
  },
1068
- next({ ctx }) {
1069
- push(ctx);
1109
+ next({ ctx, push }) {
1110
+ push();
1070
1111
  },
1071
1112
  animateCharacter({ ctx, data: data2 }, [character, timeout, ...classes]) {
1072
1113
  if (DEV && classes.length === 0) {
@@ -1087,11 +1128,12 @@ var novely = ({
1087
1128
  return;
1088
1129
  const classNames = classes.filter((className) => !target.classList.contains(className));
1089
1130
  target.classList.add(...classNames);
1090
- const timeoutId = setTimeout(() => {
1131
+ const removeClassNames = () => {
1091
1132
  target.classList.remove(...classNames);
1092
- }, timeout);
1133
+ };
1134
+ const timeoutId = setTimeout(removeClassNames, timeout);
1093
1135
  clear(() => {
1094
- target.classList.remove(...classNames);
1136
+ removeClassNames();
1095
1137
  clearTimeout(timeoutId);
1096
1138
  });
1097
1139
  };
@@ -1101,12 +1143,12 @@ var novely = ({
1101
1143
  data: data2
1102
1144
  });
1103
1145
  },
1104
- text({ ctx, data: data2 }, text) {
1146
+ text({ ctx, data: data2, forward }, text) {
1105
1147
  const string = text.map((content) => unwrap2(content, data2)).join(" ");
1106
1148
  if (DEV && string.length === 0) {
1107
1149
  throw new Error(`Action Text was called with empty string or array`);
1108
1150
  }
1109
- ctx.text(string, () => forward(ctx));
1151
+ ctx.text(string, forward);
1110
1152
  },
1111
1153
  exit({ ctx, data: data2 }) {
1112
1154
  if (ctx.meta.restoring)
@@ -1155,11 +1197,11 @@ var novely = ({
1155
1197
  }
1156
1198
  render(ctx);
1157
1199
  },
1158
- preload({ ctx }, [source]) {
1200
+ preload({ ctx, push }, [source]) {
1159
1201
  if (!ctx.meta.goingBack && !ctx.meta.restoring && !PRELOADED_ASSETS.has(source)) {
1160
1202
  PRELOADED_ASSETS.add(renderer.misc.preloadImage(source));
1161
1203
  }
1162
- push(ctx);
1204
+ push();
1163
1205
  },
1164
1206
  block({ ctx }, [scene]) {
1165
1207
  if (DEV && !story[scene]) {
@@ -1175,25 +1217,6 @@ var novely = ({
1175
1217
  }
1176
1218
  }
1177
1219
  });
1178
- const enmemory = (ctx) => {
1179
- if (ctx.meta.restoring)
1180
- return;
1181
- const stack = useStack(ctx);
1182
- const current = klona(stack.value);
1183
- current[2][1] = "auto";
1184
- stack.push(current);
1185
- save(true, "auto");
1186
- };
1187
- const next = (ctx) => {
1188
- const stack = useStack(ctx);
1189
- const path = stack.value[0];
1190
- const last = path.at(-1);
1191
- if (last && (isNull(last[0]) || last[0] === "jump") && isNumber(last[1])) {
1192
- last[1]++;
1193
- } else {
1194
- path.push([null, 0]);
1195
- }
1196
- };
1197
1220
  const render = (ctx) => {
1198
1221
  const stack = useStack(ctx);
1199
1222
  const referred = refer(stack.value[0]);
@@ -1210,17 +1233,6 @@ var novely = ({
1210
1233
  });
1211
1234
  }
1212
1235
  };
1213
- const push = (ctx) => {
1214
- if (!ctx.meta.restoring)
1215
- next(ctx), render(ctx);
1216
- };
1217
- const forward = (ctx) => {
1218
- if (!ctx.meta.preview)
1219
- enmemory(ctx);
1220
- push(ctx);
1221
- if (!ctx.meta.preview)
1222
- interactivity(true);
1223
- };
1224
1236
  const interactivity = (value = false) => {
1225
1237
  interacted = value ? interacted + 1 : 0;
1226
1238
  };
@@ -1270,6 +1282,16 @@ var novely = ({
1270
1282
  */
1271
1283
  unwrap(content) {
1272
1284
  return unwrap2(content, $.get().data);
1285
+ },
1286
+ /**
1287
+ * Cancel data loading, hide UI, ignore page change events
1288
+ * Data updates still will work in case Novely already was loaded
1289
+ */
1290
+ destroy() {
1291
+ dataLoaded.cancel();
1292
+ UIInstance.unmount();
1293
+ removeEventListener("visibilitychange", onVisibilityChange);
1294
+ removeEventListener("beforeunload", throttledEmergencyOnStorageDataChange);
1273
1295
  }
1274
1296
  };
1275
1297
  };