@novely/core 0.33.3 → 0.35.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
@@ -47,6 +47,7 @@ var MAIN_CONTEXT_KEY = "$MAIN";
47
47
 
48
48
  // src/shared.ts
49
49
  var STACK_MAP = /* @__PURE__ */ new Map();
50
+ var CUSTOM_ACTION_MAP = /* @__PURE__ */ new Map();
50
51
  var PRELOADED_ASSETS = /* @__PURE__ */ new Set();
51
52
 
52
53
  // src/utils.ts
@@ -333,18 +334,31 @@ var createQueueProcessor = (queue, options) => {
333
334
  }
334
335
  keep.add(action);
335
336
  if (action === "function" || action === "custom") {
336
- if (action === "custom" && params[0].callOnlyLatest) {
337
- const notLatest = next(i).some(([, func]) => {
338
- if (!isFunction(func))
339
- return false;
340
- const c0 = func;
341
- const c1 = params[0];
342
- const isIdenticalID = Boolean(c0.id && c1.id && c0.id === c1.id);
343
- const isIdenticalByReference = c0 === c1;
344
- return isIdenticalID || isIdenticalByReference || str(c0) === str(c1);
345
- });
346
- if (notLatest)
347
- continue;
337
+ if (action === "custom") {
338
+ const fn = params[0];
339
+ if ("callOnlyLatest" in fn && fn.callOnlyLatest) {
340
+ const notLatest = next(i).some(([, func]) => {
341
+ if (!isFunction(func))
342
+ return false;
343
+ const c0 = func;
344
+ const c1 = fn;
345
+ const isIdenticalID = Boolean(c0.id && c1.id && c0.id === c1.id);
346
+ const isIdenticalByReference = c0 === c1;
347
+ return isIdenticalID || isIdenticalByReference || str(c0) === str(c1);
348
+ });
349
+ if (notLatest)
350
+ continue;
351
+ } else if ("skipOnRestore" in fn && fn.skipOnRestore) {
352
+ let getNext = () => {
353
+ const nextActions = next(i);
354
+ getNext = () => {
355
+ return nextActions;
356
+ };
357
+ return nextActions;
358
+ };
359
+ if (fn.skipOnRestore(getNext))
360
+ continue;
361
+ }
348
362
  }
349
363
  processedQueue.push(item);
350
364
  } else if (action === "showCharacter" || action === "playSound" || action === "playMusic" || action === "voice") {
@@ -738,6 +752,76 @@ var replace = (input, data, pluralization, actions, pr) => {
738
752
  });
739
753
  };
740
754
 
755
+ // src/custom-action.ts
756
+ var createCustomActionNode = (id) => {
757
+ const div = document.createElement("div");
758
+ div.setAttribute("data-id", id);
759
+ return div;
760
+ };
761
+ var getCustomActionHolder = (ctx, fn) => {
762
+ const cached = CUSTOM_ACTION_MAP.get(ctx.id + fn.key);
763
+ if (cached) {
764
+ return cached;
765
+ }
766
+ const holder = {
767
+ cleanup: noop,
768
+ node: null,
769
+ fn,
770
+ localData: {}
771
+ };
772
+ CUSTOM_ACTION_MAP.set(ctx.id + fn.key, holder);
773
+ return holder;
774
+ };
775
+ var handleCustomAction = (ctx, fn, { lang, state, setMountElement, setClear, remove: renderersRemove }) => {
776
+ const holder = getCustomActionHolder(ctx, fn);
777
+ const flags = {
778
+ ...ctx.meta
779
+ };
780
+ const getDomNodes = (insert = true) => {
781
+ if (holder.node || !insert) {
782
+ return {
783
+ element: holder.node,
784
+ root: ctx.root
785
+ };
786
+ }
787
+ holder.node = insert ? createCustomActionNode(fn.key) : null;
788
+ setMountElement(holder.node);
789
+ return {
790
+ element: holder.node,
791
+ root: ctx.root
792
+ };
793
+ };
794
+ const clear = (func) => {
795
+ setClear(holder.cleanup = () => {
796
+ func();
797
+ holder.node = null;
798
+ holder.cleanup = noop;
799
+ setMountElement(null);
800
+ setClear(noop);
801
+ });
802
+ };
803
+ const data = (updatedData) => {
804
+ if (updatedData) {
805
+ return holder.localData = updatedData;
806
+ }
807
+ return holder.localData;
808
+ };
809
+ const remove = () => {
810
+ holder.cleanup();
811
+ renderersRemove();
812
+ };
813
+ return fn({
814
+ flags,
815
+ lang,
816
+ state,
817
+ data,
818
+ clear,
819
+ remove,
820
+ rendererContext: ctx,
821
+ getDomNodes
822
+ });
823
+ };
824
+
741
825
  // src/storage.ts
742
826
  var localStorageStorage = (options) => {
743
827
  return {
@@ -1059,7 +1143,7 @@ var novely = ({
1059
1143
  }
1060
1144
  const [action2, fn] = element;
1061
1145
  if (action2 === "custom") {
1062
- context.clearCustom(fn);
1146
+ getCustomActionHolder(context, fn).cleanup();
1063
1147
  }
1064
1148
  }
1065
1149
  }
@@ -1184,6 +1268,9 @@ var novely = ({
1184
1268
  }
1185
1269
  return capitalize(language.nameOverride || getIntlLanguageDisplayName(lang));
1186
1270
  };
1271
+ const clearCustomAction = (ctx, fn) => {
1272
+ getCustomActionHolder(ctx, fn).cleanup();
1273
+ };
1187
1274
  const renderer = createRenderer({
1188
1275
  mainContextKey: MAIN_CONTEXT_KEY,
1189
1276
  characters,
@@ -1197,6 +1284,7 @@ var novely = ({
1197
1284
  preview,
1198
1285
  removeContext,
1199
1286
  getStateFunction,
1287
+ clearCustomAction,
1200
1288
  languages,
1201
1289
  storageData,
1202
1290
  coreData,
@@ -1440,19 +1528,31 @@ var novely = ({
1440
1528
  forward
1441
1529
  );
1442
1530
  },
1443
- custom({ ctx, push }, [handler]) {
1444
- if (handler.requireUserAction) {
1531
+ custom({ ctx, push }, [fn]) {
1532
+ if (fn.requireUserAction) {
1445
1533
  ctx.clearBlockingActions(void 0);
1446
1534
  }
1447
- const result = ctx.custom(handler, () => {
1448
- if (ctx.meta.restoring)
1449
- return;
1450
- if (handler.requireUserAction && !ctx.meta.preview) {
1535
+ const state = getStateFunction(ctx);
1536
+ const lang = storageData.get().meta[0];
1537
+ const result = handleCustomAction(ctx, fn, {
1538
+ ...ctx.custom(fn),
1539
+ state,
1540
+ lang
1541
+ });
1542
+ const next2 = () => {
1543
+ if (fn.requireUserAction && !ctx.meta.preview) {
1451
1544
  enmemory(ctx);
1452
1545
  interactivity(true);
1453
1546
  }
1454
1547
  push();
1455
- });
1548
+ };
1549
+ if (!ctx.meta.restoring) {
1550
+ if (isPromise(result)) {
1551
+ result.then(next2);
1552
+ } else {
1553
+ next2();
1554
+ }
1555
+ }
1456
1556
  return result;
1457
1557
  },
1458
1558
  vibrate({ ctx, push }, pattern) {