@novely/solid-renderer 0.17.0 → 0.18.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.jsx +61 -34
- package/dist/types.d.ts +4 -0
- package/dist/utils.d.ts +12 -2
- package/package.json +1 -1
package/dist/index.jsx
CHANGED
|
@@ -3734,6 +3734,17 @@ var createEmitter = () => {
|
|
|
3734
3734
|
};
|
|
3735
3735
|
|
|
3736
3736
|
// src/utils.ts
|
|
3737
|
+
var findLastIndex = (array, fn) => {
|
|
3738
|
+
for (let i = array.length - 1; i >= 0; i--) {
|
|
3739
|
+
if (fn.call(array, array[i], i, array)) {
|
|
3740
|
+
return i;
|
|
3741
|
+
}
|
|
3742
|
+
}
|
|
3743
|
+
return -1;
|
|
3744
|
+
};
|
|
3745
|
+
var findLast = (array, fn) => {
|
|
3746
|
+
return array[findLastIndex(array, fn)];
|
|
3747
|
+
};
|
|
3737
3748
|
var capitalize = (str) => {
|
|
3738
3749
|
return str[0].toUpperCase() + str.slice(1);
|
|
3739
3750
|
};
|
|
@@ -3743,7 +3754,11 @@ var isCSSImage = (str) => {
|
|
|
3743
3754
|
};
|
|
3744
3755
|
var createImage = (src) => {
|
|
3745
3756
|
const img = document.createElement("img");
|
|
3746
|
-
|
|
3757
|
+
Object.assign(img, {
|
|
3758
|
+
src,
|
|
3759
|
+
crossOrigin: "*"
|
|
3760
|
+
});
|
|
3761
|
+
return img;
|
|
3747
3762
|
};
|
|
3748
3763
|
var canvasDrawImages = (canvas = document.createElement("canvas"), ctx = canvas.getContext("2d"), images) => {
|
|
3749
3764
|
let set = false;
|
|
@@ -3785,14 +3800,6 @@ var toMedia = (media) => {
|
|
|
3785
3800
|
}
|
|
3786
3801
|
return media;
|
|
3787
3802
|
};
|
|
3788
|
-
var findLast = (array, fn) => {
|
|
3789
|
-
for (let i = array.length - 1; i >= 0; i--) {
|
|
3790
|
-
if (fn(array[i])) {
|
|
3791
|
-
return array[i];
|
|
3792
|
-
}
|
|
3793
|
-
}
|
|
3794
|
-
return;
|
|
3795
|
-
};
|
|
3796
3803
|
var simple = (fn) => {
|
|
3797
3804
|
return (...args) => {
|
|
3798
3805
|
let result;
|
|
@@ -3832,6 +3839,36 @@ var getDocumentStyles = () => {
|
|
|
3832
3839
|
}
|
|
3833
3840
|
return css;
|
|
3834
3841
|
};
|
|
3842
|
+
var useBackground = (obj, set) => {
|
|
3843
|
+
const backgrounds = Object.fromEntries(Object.entries(obj).map(([key, value]) => [toMedia(key), value]));
|
|
3844
|
+
const mediaQueries = Object.keys(backgrounds).map((media) => matchMedia(media));
|
|
3845
|
+
const allMedia = mediaQueries.find(({ media }) => media === "all");
|
|
3846
|
+
const handle = () => {
|
|
3847
|
+
const last = findLast(mediaQueries, ({ matches, media }) => matches && media !== "all");
|
|
3848
|
+
const bg = last ? backgrounds[last.media] : allMedia ? backgrounds["all"] : "";
|
|
3849
|
+
set(bg);
|
|
3850
|
+
};
|
|
3851
|
+
for (const mq of mediaQueries) {
|
|
3852
|
+
mq.onchange = handle;
|
|
3853
|
+
}
|
|
3854
|
+
let disposed = false;
|
|
3855
|
+
Promise.resolve().then(() => {
|
|
3856
|
+
if (disposed)
|
|
3857
|
+
return;
|
|
3858
|
+
handle();
|
|
3859
|
+
});
|
|
3860
|
+
return {
|
|
3861
|
+
/**
|
|
3862
|
+
* Remove all listeners
|
|
3863
|
+
*/
|
|
3864
|
+
dispose() {
|
|
3865
|
+
for (const mq of mediaQueries) {
|
|
3866
|
+
mq.onchange = null;
|
|
3867
|
+
}
|
|
3868
|
+
disposed = true;
|
|
3869
|
+
}
|
|
3870
|
+
};
|
|
3871
|
+
};
|
|
3835
3872
|
|
|
3836
3873
|
// src/hooks/useMedia.ts
|
|
3837
3874
|
var useMedia = (media) => {
|
|
@@ -4437,6 +4474,7 @@ function produce(fn) {
|
|
|
4437
4474
|
var atContextStateMap = /* @__PURE__ */ new Map();
|
|
4438
4475
|
var createContextState = () => {
|
|
4439
4476
|
const [state, setState] = createStore({
|
|
4477
|
+
disposeBackground: void 0,
|
|
4440
4478
|
background: "",
|
|
4441
4479
|
characters: {},
|
|
4442
4480
|
dialog: {
|
|
@@ -5464,7 +5502,6 @@ var createSolidRenderer = ({
|
|
|
5464
5502
|
let renderer;
|
|
5465
5503
|
let options;
|
|
5466
5504
|
let root;
|
|
5467
|
-
let currentBackground;
|
|
5468
5505
|
let unmount;
|
|
5469
5506
|
const Novely = () => {
|
|
5470
5507
|
const ctx = renderer.getContext(options.mainContextKey);
|
|
@@ -5607,33 +5644,17 @@ var createSolidRenderer = ({
|
|
|
5607
5644
|
id: name,
|
|
5608
5645
|
root,
|
|
5609
5646
|
background(background) {
|
|
5610
|
-
|
|
5647
|
+
state.disposeBackground?.();
|
|
5611
5648
|
if (typeof background === "string") {
|
|
5612
5649
|
setState("background", background);
|
|
5613
5650
|
return;
|
|
5614
5651
|
}
|
|
5615
|
-
|
|
5616
|
-
|
|
5617
|
-
|
|
5618
|
-
|
|
5619
|
-
|
|
5620
|
-
|
|
5621
|
-
if (currentBackground !== background) {
|
|
5622
|
-
for (const mq of mediaQueries) {
|
|
5623
|
-
mq.onchange = null;
|
|
5624
|
-
}
|
|
5625
|
-
return;
|
|
5626
|
-
}
|
|
5627
|
-
const last = findLast(mediaQueries, ({
|
|
5628
|
-
matches
|
|
5629
|
-
}) => matches);
|
|
5630
|
-
const bg = last ? background[last.media] : "";
|
|
5631
|
-
setState("background", bg);
|
|
5632
|
-
};
|
|
5633
|
-
for (const mq of mediaQueries) {
|
|
5634
|
-
mq.onchange = handle;
|
|
5635
|
-
}
|
|
5636
|
-
handle();
|
|
5652
|
+
const {
|
|
5653
|
+
dispose: dispose2
|
|
5654
|
+
} = useBackground(background, (value) => {
|
|
5655
|
+
setState("background", value);
|
|
5656
|
+
});
|
|
5657
|
+
setState("disposeBackground", () => dispose2);
|
|
5637
5658
|
},
|
|
5638
5659
|
character(character) {
|
|
5639
5660
|
const chars = this.store.characters;
|
|
@@ -5783,6 +5804,7 @@ var createSolidRenderer = ({
|
|
|
5783
5804
|
const onInputHandler = (event) => {
|
|
5784
5805
|
let value;
|
|
5785
5806
|
onInput({
|
|
5807
|
+
lang: options.$.get().meta[0],
|
|
5786
5808
|
input,
|
|
5787
5809
|
event,
|
|
5788
5810
|
error,
|
|
@@ -5847,7 +5869,12 @@ var createSolidRenderer = ({
|
|
|
5847
5869
|
});
|
|
5848
5870
|
return state.layers[fn.key].value;
|
|
5849
5871
|
};
|
|
5850
|
-
const result = fn(
|
|
5872
|
+
const result = fn({
|
|
5873
|
+
get,
|
|
5874
|
+
goingBack: ctx.meta.goingBack,
|
|
5875
|
+
preview: ctx.meta.preview,
|
|
5876
|
+
lang: options.$.get().meta[0]
|
|
5877
|
+
});
|
|
5851
5878
|
result ? result.then(resolve) : resolve();
|
|
5852
5879
|
return result;
|
|
5853
5880
|
},
|
package/dist/types.d.ts
CHANGED
|
@@ -117,6 +117,10 @@ type StateMeta = {
|
|
|
117
117
|
goingBack: boolean;
|
|
118
118
|
};
|
|
119
119
|
type AtContextState = {
|
|
120
|
+
disposeBackground: undefined | (() => void);
|
|
121
|
+
/**
|
|
122
|
+
* Background that should be rendered
|
|
123
|
+
*/
|
|
120
124
|
background: string;
|
|
121
125
|
characters: Record<string, StateCharacter>;
|
|
122
126
|
dialog: StateDialog;
|
package/dist/utils.d.ts
CHANGED
|
@@ -1,3 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Using this because `Array.prototype.findLast` has not enough support
|
|
3
|
+
* @see https://caniuse.com/?search=findLast
|
|
4
|
+
*/
|
|
5
|
+
declare const findLast: <T>(array: T[], fn: (this: T[], item: T, index: number, array: T[]) => boolean) => T;
|
|
1
6
|
declare const capitalize: (str: string) => string;
|
|
2
7
|
declare const isCSSImage: (str: string) => boolean;
|
|
3
8
|
declare const createImage: (src: string) => HTMLImageElement;
|
|
@@ -9,9 +14,14 @@ declare const url: <T extends string>(str: T) => `url(${T})`;
|
|
|
9
14
|
declare const escape: (str: string) => string;
|
|
10
15
|
declare const onKey: (cb: (event: KeyboardEvent) => void, ...keys: string[]) => (e: KeyboardEvent) => void;
|
|
11
16
|
declare const toMedia: (media: 'portrait' | 'landscape' | (string & Record<never, never>)) => string;
|
|
12
|
-
declare const findLast: <T>(array: T[], fn: (item: T) => boolean) => T | undefined;
|
|
13
17
|
declare const simple: <T extends unknown[], R>(fn: (...args: T) => R) => (...args: T) => R;
|
|
14
18
|
declare const vibrationPossible: () => boolean;
|
|
15
19
|
declare const vibrate: (pattern: VibratePattern) => void;
|
|
16
20
|
declare const getDocumentStyles: () => string;
|
|
17
|
-
|
|
21
|
+
declare const useBackground: (obj: Record<string, string>, set: (bg: string) => void) => {
|
|
22
|
+
/**
|
|
23
|
+
* Remove all listeners
|
|
24
|
+
*/
|
|
25
|
+
dispose(): void;
|
|
26
|
+
};
|
|
27
|
+
export { isCSSImage, canvasDrawImages, url, createImage, capitalize, escape, onKey, toMedia, findLast, simple, vibrationPossible, vibrate, getDocumentStyles, useBackground };
|