@novely/solid-renderer 0.43.2 → 0.44.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.
@@ -1,5 +1,6 @@
1
+ import type { ContextImages } from './custom-actions/show-hide-image';
1
2
  declare const useContextState: (id: string) => import("@novely/renderer-toolkit").DeepAtom<import("@novely/renderer-toolkit").ContextStateStore<{
2
- images: Record<string, HTMLImageElement | undefined>;
3
+ images: ContextImages;
3
4
  dialogOverviewShown: boolean;
4
5
  mood: string;
5
6
  }>>, removeContextState: (id: string) => void;
@@ -1,15 +1,26 @@
1
1
  import type { ValidAction, NovelyAsset } from '@novely/core';
2
2
  type ShowImageParams = {
3
- z?: number;
4
- in?: string;
5
- class?: string;
6
- style?: string;
7
- await?: boolean;
3
+ classesBase?: string;
4
+ classesIn?: string;
5
+ wait?: boolean;
8
6
  };
9
- declare const showImage: (source: string | NovelyAsset, params?: ShowImageParams) => ValidAction;
7
+ type ShowImageData = {
8
+ classesBase?: string[];
9
+ classesIn?: string[];
10
+ classesOut?: string[];
11
+ key: string;
12
+ image: HTMLImageElement;
13
+ visible: boolean;
14
+ animate: boolean;
15
+ onIn: () => void;
16
+ onOut: () => void;
17
+ };
18
+ type ContextImages = ShowImageData[];
19
+ declare const showImage: (asset: NovelyAsset, { classesBase, classesIn, wait }?: ShowImageParams) => ValidAction;
10
20
  type HideImageParams = {
11
- out?: string;
12
- await?: boolean;
21
+ classesOut?: string;
22
+ wait?: boolean;
13
23
  };
14
- declare const hideImage: (source: string | NovelyAsset, params?: HideImageParams) => ValidAction;
24
+ declare const hideImage: (asset: NovelyAsset, { classesOut, wait }?: HideImageParams) => ValidAction;
15
25
  export { showImage, hideImage };
26
+ export type { ContextImages };
package/dist/index.jsx CHANGED
@@ -717,6 +717,61 @@ function mapArray(list, mapFn, options = {}) {
717
717
  }
718
718
  };
719
719
  }
720
+ function indexArray(list, mapFn, options = {}) {
721
+ let items = [], mapped = [], disposers = [], signals = [], len = 0, i;
722
+ onCleanup(() => dispose(disposers));
723
+ return () => {
724
+ const newItems = list() || [], newLen = newItems.length;
725
+ newItems[$TRACK];
726
+ return untrack(() => {
727
+ if (newLen === 0) {
728
+ if (len !== 0) {
729
+ dispose(disposers);
730
+ disposers = [];
731
+ items = [];
732
+ mapped = [];
733
+ len = 0;
734
+ signals = [];
735
+ }
736
+ if (options.fallback) {
737
+ items = [FALLBACK];
738
+ mapped[0] = createRoot((disposer) => {
739
+ disposers[0] = disposer;
740
+ return options.fallback();
741
+ });
742
+ len = 1;
743
+ }
744
+ return mapped;
745
+ }
746
+ if (items[0] === FALLBACK) {
747
+ disposers[0]();
748
+ disposers = [];
749
+ items = [];
750
+ mapped = [];
751
+ len = 0;
752
+ }
753
+ for (i = 0; i < newLen; i++) {
754
+ if (i < items.length && items[i] !== newItems[i]) {
755
+ signals[i](() => newItems[i]);
756
+ } else if (i >= items.length) {
757
+ mapped[i] = createRoot(mapper);
758
+ }
759
+ }
760
+ for (; i < items.length; i++) {
761
+ disposers[i]();
762
+ }
763
+ len = signals.length = disposers.length = newLen;
764
+ items = newItems.slice(0);
765
+ return mapped = mapped.slice(0, len);
766
+ });
767
+ function mapper(disposer) {
768
+ disposers[i] = disposer;
769
+ const [s, set] = createSignal(newItems[i]);
770
+ signals[i] = set;
771
+ return mapFn(s, i);
772
+ }
773
+ };
774
+ }
720
775
  var hydrationEnabled = false;
721
776
  function createComponent(Comp, props) {
722
777
  if (hydrationEnabled) {
@@ -903,6 +958,12 @@ function For(props) {
903
958
  };
904
959
  return createMemo(mapArray(() => props.each, props.children, fallback || void 0));
905
960
  }
961
+ function Index(props) {
962
+ const fallback = "fallback" in props && {
963
+ fallback: () => props.fallback
964
+ };
965
+ return createMemo(indexArray(() => props.each, props.children, fallback || void 0));
966
+ }
906
967
  function Show(props) {
907
968
  const keyed = props.keyed;
908
969
  const condition = createMemo(() => props.when, void 0, {
@@ -3461,7 +3522,7 @@ var Transition2 = (props) => {
3461
3522
  // src/context-state.ts
3462
3523
  var { useContextState, removeContextState } = createContextStateRoot(() => {
3463
3524
  return {
3464
- images: {},
3525
+ images: [],
3465
3526
  dialogOverviewShown: false,
3466
3527
  mood: ""
3467
3528
  };
@@ -4128,6 +4189,7 @@ var _tmpl$102 = /* @__PURE__ */ template(`<div class="control-panel-container-ba
4128
4189
  var _tmpl$112 = /* @__PURE__ */ template(`<div class="control-panel"><div role="menubar" class="control-panel-container">`);
4129
4190
  var _tmpl$122 = /* @__PURE__ */ template(`<div><div class="characters"></div><div class="action-dialog"><div class="action-dialog-container" role="button" tabindex="0"><div class="action-dialog-content"></div></div></div><div></div><div class="action-text" role="button" tabindex="0"></div><div class="action-image">`);
4130
4191
  var _tmpl$132 = /* @__PURE__ */ template(`<button type="button" class="button choice-dialog__choice"><span>`);
4192
+ var _tmpl$14 = /* @__PURE__ */ template(`<div class="action-image__image-container">`);
4131
4193
  var Game = (props) => {
4132
4194
  const data = useData();
4133
4195
  const {
@@ -4539,11 +4601,52 @@ var Game = (props) => {
4539
4601
  return _el$18;
4540
4602
  }
4541
4603
  }), _el$23);
4542
- insert(_el$23, createComponent(For, {
4604
+ insert(_el$23, createComponent(Index, {
4543
4605
  get each() {
4544
- return Object.values(images());
4606
+ return images();
4545
4607
  },
4546
- children: (image) => image
4608
+ children: (image) => createComponent(Transition2, {
4609
+ onEnter: (el, done) => {
4610
+ const classesIn = image().classesIn;
4611
+ const onCompleted = () => {
4612
+ done();
4613
+ image().onIn();
4614
+ };
4615
+ if (!image().animate || !classesIn || classesIn.length === 0) {
4616
+ return onCompleted();
4617
+ }
4618
+ el.classList.add(...classesIn);
4619
+ el.addEventListener("animationend", onCompleted, {
4620
+ once: true
4621
+ });
4622
+ },
4623
+ onExit: (el, done) => {
4624
+ const classesOut = image().classesOut;
4625
+ const onCompleted = () => {
4626
+ done();
4627
+ image().onOut();
4628
+ };
4629
+ if (!image().animate || !classesOut || classesOut.length === 0) {
4630
+ return onCompleted();
4631
+ }
4632
+ el.classList.add(...classesOut);
4633
+ el.addEventListener("animationend", onCompleted, {
4634
+ once: true
4635
+ });
4636
+ },
4637
+ get children() {
4638
+ return createComponent(Show, {
4639
+ get when() {
4640
+ return image().visible;
4641
+ },
4642
+ get children() {
4643
+ const _el$26 = _tmpl$14();
4644
+ insert(_el$26, () => image().image);
4645
+ return _el$26;
4646
+ }
4647
+ });
4648
+ }
4649
+ })
4547
4650
  }));
4548
4651
  insert(_el$, createComponent(DialogOverview, {
4549
4652
  context,
@@ -4584,17 +4687,17 @@ var Game = (props) => {
4584
4687
  delegateEvents(["click", "keydown"]);
4585
4688
 
4586
4689
  // src/screens/loading.tsx
4587
- var _tmpl$14 = /* @__PURE__ */ template(`<div class="loading"><div class="loading__animation"><div></div><div></div><div></div><div>`);
4690
+ var _tmpl$15 = /* @__PURE__ */ template(`<div class="loading"><div class="loading__animation"><div></div><div></div><div></div><div>`);
4588
4691
  var Loading = (props) => {
4589
4692
  return (() => {
4590
- const _el$ = _tmpl$14();
4693
+ const _el$ = _tmpl$15();
4591
4694
  createRenderEffect(() => _el$.classList.toggle("overlay", !!props.overlay));
4592
4695
  return _el$;
4593
4696
  })();
4594
4697
  };
4595
4698
 
4596
4699
  // src/screens/mainmenu.tsx
4597
- var _tmpl$15 = /* @__PURE__ */ template(`<div class="main-menu"><div class="main-menu__controls"><button type="button" class="button main-menu__button"><span class="main-menu__button__text"></span></button><button type="button" class="button main-menu__button"><span class="main-menu__button__text"></span></button><button type="button" class="button main-menu__button"><span class="main-menu__button__text"></span></button><button type="button" class="button main-menu__button"><span class="main-menu__button__text">`);
4700
+ var _tmpl$16 = /* @__PURE__ */ template(`<div class="main-menu"><div class="main-menu__controls"><button type="button" class="button main-menu__button"><span class="main-menu__button__text"></span></button><button type="button" class="button main-menu__button"><span class="main-menu__button__text"></span></button><button type="button" class="button main-menu__button"><span class="main-menu__button__text"></span></button><button type="button" class="button main-menu__button"><span class="main-menu__button__text">`);
4598
4701
  var _tmpl$27 = /* @__PURE__ */ template(`<button>`);
4599
4702
  var MainMenu = () => {
4600
4703
  const {
@@ -4611,7 +4714,7 @@ var MainMenu = () => {
4611
4714
  $rendererState.mutate((s) => s.screen, screen);
4612
4715
  });
4613
4716
  return (() => {
4614
- const _el$ = _tmpl$15(), _el$2 = _el$.firstChild, _el$3 = _el$2.firstChild, _el$4 = _el$3.firstChild, _el$5 = _el$3.nextSibling, _el$6 = _el$5.firstChild, _el$7 = _el$5.nextSibling, _el$8 = _el$7.firstChild, _el$9 = _el$7.nextSibling, _el$10 = _el$9.firstChild;
4717
+ const _el$ = _tmpl$16(), _el$2 = _el$.firstChild, _el$3 = _el$2.firstChild, _el$4 = _el$3.firstChild, _el$5 = _el$3.nextSibling, _el$6 = _el$5.firstChild, _el$7 = _el$5.nextSibling, _el$8 = _el$7.firstChild, _el$9 = _el$7.nextSibling, _el$10 = _el$9.firstChild;
4615
4718
  _el$3.$$click = () => {
4616
4719
  options.newGame();
4617
4720
  };
@@ -4662,7 +4765,7 @@ var MainMenu = () => {
4662
4765
  delegateEvents(["click"]);
4663
4766
 
4664
4767
  // src/screens/saves.tsx
4665
- var _tmpl$16 = /* @__PURE__ */ template(`<ol class="saves__list">`);
4768
+ var _tmpl$17 = /* @__PURE__ */ template(`<ol class="saves__list">`);
4666
4769
  var _tmpl$28 = /* @__PURE__ */ template(`<div class="saves"><div class="saves__controls"><button type="button" class="button saves__button"></button></div><div class="saves__list-container">`);
4667
4770
  var _tmpl$36 = /* @__PURE__ */ template(`<div class="saves__list saves__list--empty">`);
4668
4771
  var Saves = () => {
@@ -4748,7 +4851,7 @@ var Saves = () => {
4748
4851
  })();
4749
4852
  },
4750
4853
  get children() {
4751
- const _el$5 = _tmpl$16();
4854
+ const _el$5 = _tmpl$17();
4752
4855
  use(setList, _el$5);
4753
4856
  insert(_el$5, createComponent(For, {
4754
4857
  get each() {
@@ -4779,7 +4882,7 @@ var Saves = () => {
4779
4882
  delegateEvents(["click"]);
4780
4883
 
4781
4884
  // src/screens/settings.tsx
4782
- var _tmpl$17 = /* @__PURE__ */ template(`<div class="root settings"><div class="settings__controls"><button type="button" class="button settings__button"></button><button type="button" class="button settings__button"></button></div><div class="settings__options"><div></div><div>`);
4885
+ var _tmpl$18 = /* @__PURE__ */ template(`<div class="root settings"><div class="settings__controls"><button type="button" class="button settings__button"></button><button type="button" class="button settings__button"></button></div><div class="settings__options"><div></div><div>`);
4783
4886
  var _tmpl$29 = /* @__PURE__ */ template(`<option>`);
4784
4887
  var Settings = (props) => {
4785
4888
  const {
@@ -4826,7 +4929,7 @@ var Settings = (props) => {
4826
4929
  return fn;
4827
4930
  };
4828
4931
  return (() => {
4829
- const _el$ = _tmpl$17(), _el$2 = _el$.firstChild, _el$3 = _el$2.firstChild, _el$4 = _el$3.nextSibling, _el$5 = _el$2.nextSibling, _el$6 = _el$5.firstChild, _el$7 = _el$6.nextSibling;
4932
+ const _el$ = _tmpl$18(), _el$2 = _el$.firstChild, _el$3 = _el$2.firstChild, _el$4 = _el$3.nextSibling, _el$5 = _el$2.nextSibling, _el$6 = _el$5.firstChild, _el$7 = _el$6.nextSibling;
4830
4933
  _el$3.$$click = () => {
4831
4934
  $rendererState.setKey("screen", "mainmenu");
4832
4935
  };
@@ -4927,7 +5030,7 @@ var Settings = (props) => {
4927
5030
  delegateEvents(["click"]);
4928
5031
 
4929
5032
  // src/components/Root.tsx
4930
- var _tmpl$18 = /* @__PURE__ */ template(`<div class="root">`);
5033
+ var _tmpl$19 = /* @__PURE__ */ template(`<div class="root">`);
4931
5034
  var createRootComponent = ({
4932
5035
  $rendererState,
4933
5036
  $contextState,
@@ -4961,7 +5064,7 @@ var createRootComponent = ({
4961
5064
  emitter.emit("screen:change", currentScreen);
4962
5065
  });
4963
5066
  return (() => {
4964
- const _el$ = _tmpl$18();
5067
+ const _el$ = _tmpl$19();
4965
5068
  const _ref$ = setRoot;
4966
5069
  typeof _ref$ === "function" ? use(_ref$, _el$) : setRoot = _el$;
4967
5070
  insert(_el$, createComponent(Provider, {
@@ -5048,89 +5151,101 @@ var createRootComponent = ({
5048
5151
  };
5049
5152
 
5050
5153
  // src/custom-actions/show-hide-image.ts
5051
- var SHOW_IMAGE = Symbol();
5052
- var showImage = (source, params = {}) => {
5053
- const handler = ({ contextKey, clear, flags, data, rendererContext }) => {
5054
- const ctx = useContextState(contextKey);
5154
+ var getIndex = (ctx, key) => {
5155
+ const images = ctx.get().images;
5156
+ const index = images.findIndex((value) => value.key === key);
5157
+ if (index === -1) {
5158
+ return images.length;
5159
+ }
5160
+ return index;
5161
+ };
5162
+ var SHOW_HIDE_IMAGE = Symbol();
5163
+ var showImage = (asset, { classesBase, classesIn, wait } = {}) => {
5164
+ const key = `show-image--${asset.id}`;
5165
+ const handler = ({ contextKey, clear, flags, rendererContext }) => {
5055
5166
  const { promise, resolve } = Promise.withResolvers();
5056
- const image = document.createElement("img");
5057
- const src = typeof source === "object" ? source.source : source;
5058
- {
5059
- image.src = src;
5060
- image.className = "action-image__image " + (params.class || "");
5061
- image.style.cssText = params.style || "";
5062
- image.style.setProperty("--z-index", String(params.z || 1));
5063
- }
5064
- ctx.mutate((s) => s.images[src], image);
5065
- const inClasses = params.in ? params.in.split(" ") : [];
5066
- data({
5067
- image,
5068
- inClasses
5069
- });
5070
- let clearAnimation = noop;
5071
- if (params.in && !flags.preview) {
5072
- image.classList.add(...inClasses);
5073
- if (params.await) {
5074
- rendererContext.clearBlockingActions(void 0);
5075
- }
5076
- clearAnimation = () => {
5077
- image.classList.remove(...inClasses);
5078
- resolve();
5079
- };
5080
- image.addEventListener("animationend", clearAnimation, { once: true });
5167
+ const ctx = useContextState(contextKey);
5168
+ const index = getIndex(ctx, key);
5169
+ const image = imagePreloadWithCachingNotComplete(asset.source).cloneNode(true);
5170
+ const classesBaseSplitted = classesBase?.split(" ") || [];
5171
+ const classesInSplitted = classesIn?.split(" ") || [];
5172
+ image.classList.add(...classesBaseSplitted);
5173
+ image.classList.add("action-image__image");
5174
+ if (wait) {
5175
+ rendererContext.clearBlockingActions(void 0);
5081
5176
  } else {
5082
5177
  resolve();
5083
5178
  }
5179
+ const onIn = () => {
5180
+ if (wait) {
5181
+ resolve();
5182
+ }
5183
+ };
5184
+ ctx.mutate((s) => s.images[index], {
5185
+ classesBase: classesBaseSplitted,
5186
+ classesIn: classesInSplitted,
5187
+ classesOut: void 0,
5188
+ key,
5189
+ image,
5190
+ visible: false,
5191
+ animate: !flags.preview,
5192
+ onIn,
5193
+ onOut: noop
5194
+ });
5195
+ setTimeout(() => ctx.mutate((s) => s.images[index].visible, true));
5084
5196
  clear(() => {
5085
- data({});
5086
- clearAnimation();
5197
+ ctx.mutate((s) => s.images[index], {
5198
+ classesIn: void 0,
5199
+ classesOut: void 0,
5200
+ key,
5201
+ image,
5202
+ visible: false,
5203
+ animate: !flags.preview,
5204
+ onIn: noop,
5205
+ onOut: noop
5206
+ });
5207
+ image.classList.remove(...classesBaseSplitted);
5087
5208
  });
5088
- if (!params.await) {
5089
- resolve();
5090
- }
5091
5209
  return promise;
5092
5210
  };
5093
- handler.id = SHOW_IMAGE;
5094
- handler.key = typeof source === "object" ? source.id : source;
5095
- handler.assets = [source];
5211
+ handler.id = SHOW_HIDE_IMAGE;
5212
+ handler.key = key;
5213
+ handler.assets = [asset];
5096
5214
  handler.callOnlyLatest = true;
5097
5215
  return ["custom", handler];
5098
5216
  };
5099
- var hideImage = (source, params = {}) => {
5100
- const handler = async ({ data, rendererContext, contextKey, flags }) => {
5101
- const ctx = useContextState(contextKey);
5217
+ var hideImage = (asset, { classesOut, wait } = {}) => {
5218
+ const key = `show-image--${asset.id}`;
5219
+ const handler = ({ contextKey, flags, rendererContext }) => {
5102
5220
  const { promise, resolve } = Promise.withResolvers();
5103
- const done = () => {
5104
- ctx.mutate((s) => s.images[typeof source === "object" ? source.source : source], void 0);
5105
- resolve();
5106
- };
5107
- const { image, inClasses } = data();
5108
- if (!image) {
5109
- resolve();
5110
- return promise;
5111
- }
5112
- if (inClasses) {
5113
- image.classList.remove(...inClasses);
5114
- }
5115
- if (params.out && !flags.preview) {
5116
- const classes = params.out.split(" ");
5117
- image.classList.add(...classes);
5221
+ const ctx = useContextState(contextKey);
5222
+ const index = getIndex(ctx, key);
5223
+ const { image, classesBase } = ctx.get().images[index];
5224
+ const classesBaseSplitted = classesBase || [];
5225
+ const classesOutSplitted = classesOut?.split(" ") || [];
5226
+ if (wait) {
5118
5227
  rendererContext.clearBlockingActions(void 0);
5119
- const onAnimationEnd = () => {
5120
- image.classList.remove(...classes);
5121
- done();
5122
- };
5123
- image.addEventListener("animationend", onAnimationEnd, { once: true });
5124
- if (!params.await) {
5125
- done();
5126
- }
5127
5228
  } else {
5128
- done();
5229
+ image.classList.remove(...classesBaseSplitted);
5230
+ resolve();
5129
5231
  }
5232
+ const onOut = () => {
5233
+ if (wait) {
5234
+ image.classList.remove(...classesBaseSplitted);
5235
+ resolve();
5236
+ }
5237
+ };
5238
+ ctx.mutate(
5239
+ (s) => s.images[index].onOut,
5240
+ () => onOut
5241
+ );
5242
+ ctx.mutate((s) => s.images[index].animate, !flags.preview);
5243
+ ctx.mutate((s) => s.images[index].classesOut, classesOutSplitted);
5244
+ setTimeout(() => ctx.mutate((s) => s.images[index].visible, false));
5130
5245
  return promise;
5131
5246
  };
5132
- handler.id = SHOW_IMAGE;
5133
- handler.key = typeof source === "object" ? source.id : source;
5247
+ handler.id = SHOW_HIDE_IMAGE;
5248
+ handler.key = key;
5134
5249
  handler.assets = [];
5135
5250
  handler.callOnlyLatest = true;
5136
5251
  return ["custom", handler];
@@ -5188,7 +5303,7 @@ var createEmitter = () => {
5188
5303
  };
5189
5304
 
5190
5305
  // src/renderer.tsx
5191
- var _tmpl$19 = /* @__PURE__ */ template(`<canvas>`);
5306
+ var _tmpl$20 = /* @__PURE__ */ template(`<canvas>`);
5192
5307
  var {
5193
5308
  preloadAudioBlocking
5194
5309
  } = createAudioMisc();
@@ -5247,7 +5362,7 @@ var createSolidRenderer = ({
5247
5362
  return chars[character];
5248
5363
  }
5249
5364
  const canvas = (() => {
5250
- const _el$ = _tmpl$19();
5365
+ const _el$ = _tmpl$20();
5251
5366
  setAttribute(_el$, "data-character", character);
5252
5367
  return _el$;
5253
5368
  })();
@@ -21,16 +21,14 @@ declare const createSolidRenderer: ({ fullscreen, controls, skipTypewriterWhenGo
21
21
  preloadAudioBlocking: (source: string) => Promise<void>;
22
22
  };
23
23
  actions: {
24
- showImage: (source: string | import("@novely/core").NovelyAsset, params?: {
25
- z?: number;
26
- in?: string;
27
- class?: string;
28
- style?: string;
29
- await?: boolean;
24
+ showImage: (asset: import("@novely/core").NovelyAsset, { classesBase, classesIn, wait }?: {
25
+ classesBase?: string;
26
+ classesIn?: string;
27
+ wait?: boolean;
30
28
  }) => import("@novely/core").ValidAction;
31
- hideImage: (source: string | import("@novely/core").NovelyAsset, params?: {
32
- out?: string;
33
- await?: boolean;
29
+ hideImage: (asset: import("@novely/core").NovelyAsset, { classesOut, wait }?: {
30
+ classesOut?: string;
31
+ wait?: boolean;
34
32
  }) => import("@novely/core").ValidAction;
35
33
  setMood: (mood: string | import("@novely/core").TextContent<Lang, import("@novely/core").State>) => import("@novely/core").ValidAction;
36
34
  };
@@ -579,13 +579,18 @@
579
579
  left: 0;
580
580
  right: 0;
581
581
  }
582
- .action-image__image {
582
+ .action-image__image-container {
583
583
  z-index: var(--z-index, 1);
584
- object-fit: cover;
585
- object-position: 50% 50%;
584
+ position: absolute;
585
+ }
586
+ .action-image__image-container,
587
+ .action-image__image {
586
588
  width: 100vw;
587
589
  height: 100vh;
588
- position: absolute;
590
+ }
591
+ .action-image__image {
592
+ object-fit: cover;
593
+ object-position: 50% 50%;
589
594
  }
590
595
 
591
596
  /* src/styles/custom-actions/image/index.css */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@novely/solid-renderer",
3
- "version": "0.43.2",
3
+ "version": "0.44.0",
4
4
  "type": "module",
5
5
  "publishConfig": {
6
6
  "access": "public"