@novely/core 0.34.0 → 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
@@ -751,6 +752,76 @@ var replace = (input, data, pluralization, actions, pr) => {
751
752
  });
752
753
  };
753
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
+
754
825
  // src/storage.ts
755
826
  var localStorageStorage = (options) => {
756
827
  return {
@@ -1072,7 +1143,7 @@ var novely = ({
1072
1143
  }
1073
1144
  const [action2, fn] = element;
1074
1145
  if (action2 === "custom") {
1075
- context.clearCustom(fn);
1146
+ getCustomActionHolder(context, fn).cleanup();
1076
1147
  }
1077
1148
  }
1078
1149
  }
@@ -1197,6 +1268,9 @@ var novely = ({
1197
1268
  }
1198
1269
  return capitalize(language.nameOverride || getIntlLanguageDisplayName(lang));
1199
1270
  };
1271
+ const clearCustomAction = (ctx, fn) => {
1272
+ getCustomActionHolder(ctx, fn).cleanup();
1273
+ };
1200
1274
  const renderer = createRenderer({
1201
1275
  mainContextKey: MAIN_CONTEXT_KEY,
1202
1276
  characters,
@@ -1210,6 +1284,7 @@ var novely = ({
1210
1284
  preview,
1211
1285
  removeContext,
1212
1286
  getStateFunction,
1287
+ clearCustomAction,
1213
1288
  languages,
1214
1289
  storageData,
1215
1290
  coreData,
@@ -1453,19 +1528,31 @@ var novely = ({
1453
1528
  forward
1454
1529
  );
1455
1530
  },
1456
- custom({ ctx, push }, [handler]) {
1457
- if (handler.requireUserAction) {
1531
+ custom({ ctx, push }, [fn]) {
1532
+ if (fn.requireUserAction) {
1458
1533
  ctx.clearBlockingActions(void 0);
1459
1534
  }
1460
- const result = ctx.custom(handler, () => {
1461
- if (ctx.meta.restoring)
1462
- return;
1463
- 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) {
1464
1544
  enmemory(ctx);
1465
1545
  interactivity(true);
1466
1546
  }
1467
1547
  push();
1468
- });
1548
+ };
1549
+ if (!ctx.meta.restoring) {
1550
+ if (isPromise(result)) {
1551
+ result.then(next2);
1552
+ } else {
1553
+ next2();
1554
+ }
1555
+ }
1469
1556
  return result;
1470
1557
  },
1471
1558
  vibrate({ ctx, push }, pattern) {