@novely/core 0.41.0 → 0.43.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.d.ts CHANGED
@@ -101,7 +101,7 @@ type Context = {
101
101
  character: (character: string) => CharacterHandle;
102
102
  background: (background: Record<string, string>) => void;
103
103
  dialog: (content: string, name: string, character: string | undefined, emotion: string | undefined, resolve: () => void) => void;
104
- choices: (question: string, choices: [name: string, active: boolean][], resolve: (selected: number) => void) => void;
104
+ choices: (question: string, choices: [name: string, active: boolean, visible: boolean, image: string][], resolve: (selected: number) => void) => void;
105
105
  input: (question: string, onInput: (meta: ActionInputOnInputMeta<Lang, State>) => void, setup: ActionInputSetup, resolve: () => void) => void;
106
106
  clear: (keep: Set<keyof DefaultActionProxy>, keepCharacters: Set<string>, keepAudio: {
107
107
  music: Set<string>;
@@ -560,10 +560,10 @@ type Character<$Lang extends Lang = string> = {
560
560
  emotions: Emotions;
561
561
  };
562
562
 
563
- type ValidAction = ['choice', number] | ['clear', Set<keyof DefaultActionProxy>?, Set<string>?, {
563
+ type ValidAction = ['choice', string | undefined, ...[string, unknown[], (() => boolean)?, (() => boolean)?, string?][]] | ['clear', Set<keyof DefaultActionProxy>?, Set<string>?, {
564
564
  music: Set<string>;
565
565
  sounds: Set<string>;
566
- }?] | ['condition', (state: State) => boolean, Record<string, ValidAction[]>] | ['dialog', string | undefined, TextContent<string, State>, string | undefined] | ['say', string, TextContent<string, State>] | ['end'] | ['showBackground', string | NovelyAsset | BackgroundImage] | ['playMusic', string | NovelyAsset] | ['stopMusic', string | NovelyAsset] | ['pauseMusic', string | NovelyAsset] | ['playSound', audio: string | NovelyAsset, loop?: boolean] | ['pauseSound', string | NovelyAsset] | ['stopSound', string | NovelyAsset] | ['voice', string | NovelyAsset | Record<string, string | NovelyAsset>] | ['stopVoice'] | ['jump', string] | ['showCharacter', string, keyof Character['emotions'], string?, string?] | ['hideCharacter', string, string?, string?, number?] | ['animateCharacter', string, number, ...string[]] | ['wait', (number | ((state: State) => number))] | ['function', FunctionAction<string, State>] | ['input', string, (meta: ActionInputOnInputMeta<string, State>) => void, ActionInputSetup?] | ['custom', CustomHandler<string, State>] | ['vibrate', ...number[]] | ['next'] | ['text', ...TextContent<string, State>[]] | ['exit'] | ['preload', string] | ['block', string] | ValidAction[];
566
+ }?] | ['condition', (state: State) => boolean, Record<string, ValidAction[]>] | ['dialog', string | undefined, TextContent<string, State>, string | undefined] | ['end'] | ['showBackground', string | NovelyAsset | BackgroundImage] | ['playMusic', string | NovelyAsset] | ['stopMusic', string | NovelyAsset] | ['pauseMusic', string | NovelyAsset] | ['playSound', audio: string | NovelyAsset, loop?: boolean] | ['pauseSound', string | NovelyAsset] | ['stopSound', string | NovelyAsset] | ['voice', string | NovelyAsset | Record<string, string | NovelyAsset>] | ['stopVoice'] | ['jump', string] | ['showCharacter', string, keyof Character['emotions'], string?, string?] | ['hideCharacter', string, string?, string?, number?] | ['animateCharacter', string, number, ...string[]] | ['wait', (number | ((state: State) => number))] | ['function', FunctionAction<string, State>] | ['input', string, (meta: ActionInputOnInputMeta<string, State>) => void, ActionInputSetup?] | ['custom', CustomHandler<string, State>] | ['vibrate', ...number[]] | ['next'] | ['text', ...TextContent<string, State>[]] | ['exit'] | ['preload', string] | ['block', string] | ValidAction[];
567
567
  type Story = Record<string, ValidAction[]>;
568
568
  type TextContent<L extends string, S extends State> = string | ((state: S) => string) | Record<L, string | ((state: S) => string)>;
569
569
  type FunctionableValue<T> = T | (() => T);
@@ -733,10 +733,24 @@ type ActionInputSetupCleanup = () => void;
733
733
  type ActionInputSetup = (input: HTMLInputElement) => ActionInputSetupCleanup | void;
734
734
  type BackgroundImage = Record<string, string | NovelyAsset>;
735
735
  type VoiceAction<L extends Lang> = (params: string | NovelyAsset | Partial<Record<L, string | NovelyAsset>>) => ValidAction;
736
+ type ActionChoiceExtendedChoice<Languages extends Lang, S extends State> = {
737
+ title: TextContent<Languages, S>;
738
+ children: ValidAction[];
739
+ active?: ChoiceCheckFunction<Languages, S>;
740
+ visible?: ChoiceCheckFunction<Languages, S>;
741
+ image?: string | NovelyAsset;
742
+ };
743
+ type ActionChoiceChoice<Languages extends Lang, S extends State> = [
744
+ title: TextContent<Languages, S>,
745
+ actions: ValidAction[],
746
+ active?: ChoiceCheckFunction<Languages, S>,
747
+ visible?: ChoiceCheckFunction<Languages, S>,
748
+ image?: string | NovelyAsset
749
+ ];
736
750
  type ActionProxy<Characters extends Record<string, Character>, Languages extends Lang, S extends State> = {
737
751
  choice: {
738
- (...choices: [name: TextContent<Languages, S>, actions: ValidAction[], active?: ChoiceCheckFunction<Languages, S>][]): ValidAction;
739
- (question: TextContent<Languages, S>, ...choices: [name: TextContent<Languages, S>, actions: ValidAction[], active?: ChoiceCheckFunction<Languages, S>][]): ValidAction;
752
+ (...choices: ActionChoiceChoice<Languages, S>[]): ValidAction;
753
+ (question: TextContent<Languages, S>, ...choices: ActionChoiceChoice<Languages, S>[]): ValidAction;
740
754
  };
741
755
  clear: (keep?: Set<keyof DefaultActionProxy>, keepCharacters?: Set<string>, keepAudio?: {
742
756
  music: Set<string>;
@@ -749,7 +763,6 @@ type ActionProxy<Characters extends Record<string, Character>, Languages extends
749
763
  (character: undefined, content: TextContent<Languages, S>, emotion?: undefined): ValidAction;
750
764
  (character: string, content: TextContent<Languages, S>, emotion?: undefined): ValidAction;
751
765
  };
752
- say: (character: keyof Characters, content: TextContent<Languages, S>) => ValidAction;
753
766
  end: () => ValidAction;
754
767
  showBackground: {
755
768
  (background: string | NovelyAsset): ValidAction;
@@ -795,6 +808,10 @@ type ActionProxy<Characters extends Record<string, Character>, Languages extends
795
808
  };
796
809
  type DefaultActionProxy = ActionProxy<Record<string, Character>, Lang, State>;
797
810
  type GetActionParameters<T extends Capitalize<keyof DefaultActionProxy>> = Parameters<DefaultActionProxy[Uncapitalize<T>]>;
811
+ type VirtualActions<Characters extends Record<string, Character>, Languages extends Lang, S extends State> = {
812
+ choice: (question: TextContent<Languages, S>, ...choices: ActionChoiceExtendedChoice<Languages, State>[]) => ValidAction;
813
+ say: (character: keyof Characters, content: TextContent<Languages, S>) => ValidAction;
814
+ };
798
815
 
799
816
  type ConditionParams<T> = T extends TypeEssentials<any, infer $State, any, any> ? $State : never;
800
817
  type ChoiceParams<T> = T extends TypeEssentials<infer $Lang, infer $State, any, any> ? ChoiceCheckFunctionProps<$Lang, $State> : never;
@@ -827,7 +844,7 @@ declare const novely: <$Language extends string, $Characters extends Record<stri
827
844
  * })
828
845
  * ```
829
846
  */
830
- action: $Actions & ActionProxy<$Characters, $Language, $State>;
847
+ action: $Actions & ActionProxy<$Characters, $Language, $State> & VirtualActions<$Characters, $Language, $State>;
831
848
  /**
832
849
  * @deprecated Will be removed BUT replaced with state passed into actions as a parameter
833
850
  */
@@ -32,7 +32,7 @@ var Novely = (() => {
32
32
  });
33
33
 
34
34
  // src/constants.ts
35
- var SKIPPED_DURING_RESTORE = /* @__PURE__ */ new Set(["dialog", "say", "choice", "input", "vibrate", "text"]);
35
+ var SKIPPED_DURING_RESTORE = /* @__PURE__ */ new Set(["dialog", "choice", "input", "vibrate", "text"]);
36
36
  var BLOCK_EXIT_STATEMENTS = /* @__PURE__ */ new Set(["choice:exit", "condition:exit", "block:exit"]);
37
37
  var BLOCK_STATEMENTS = /* @__PURE__ */ new Set(["choice", "condition", "block"]);
38
38
  var AUDIO_ACTIONS = /* @__PURE__ */ new Set([
@@ -262,13 +262,11 @@ var Novely = (() => {
262
262
  var matchAction = ({ getContext, onBeforeActionCall, push, forward }, values) => {
263
263
  return (action, props, { ctx, data }) => {
264
264
  const context = typeof ctx === "string" ? getContext(ctx) : ctx;
265
- if (action !== "say") {
266
- onBeforeActionCall({
267
- action,
268
- props,
269
- ctx: context
270
- });
271
- }
265
+ onBeforeActionCall({
266
+ action,
267
+ props,
268
+ ctx: context
269
+ });
272
270
  return values[action]({
273
271
  ctx: context,
274
272
  data,
@@ -1346,6 +1344,14 @@ var Novely = (() => {
1346
1344
  }
1347
1345
  return;
1348
1346
  }
1347
+ if (action === "choice") {
1348
+ for (let i = 1; i < props.length; i++) {
1349
+ const data = props[i];
1350
+ if (Array.isArray(data)) {
1351
+ handle(handleImageAsset(data[4]));
1352
+ }
1353
+ }
1354
+ }
1349
1355
  };
1350
1356
 
1351
1357
  // src/novely.ts
@@ -1422,6 +1428,26 @@ var Novely = (() => {
1422
1428
  return renderer.actions[action2];
1423
1429
  }
1424
1430
  return (...props) => {
1431
+ if (action2 === "say") {
1432
+ action2 = "dialog";
1433
+ const [character] = props;
1434
+ if (DEV && !characters[character]) {
1435
+ throw new Error(`Attempt to call Say action with unknown character "${character}"`);
1436
+ }
1437
+ } else if (action2 === "choice") {
1438
+ if (props.slice(1).every((choice) => !Array.isArray(choice))) {
1439
+ for (let i = 1; i < props.length; i++) {
1440
+ const choice = props[i];
1441
+ props[i] = [
1442
+ choice.title,
1443
+ choice.children,
1444
+ choice.active,
1445
+ choice.visible,
1446
+ choice.image
1447
+ ];
1448
+ }
1449
+ }
1450
+ }
1425
1451
  if (preloadAssets === "blocking") {
1426
1452
  huntAssets({
1427
1453
  action: action2,
@@ -1931,15 +1957,6 @@ var Novely = (() => {
1931
1957
  forward
1932
1958
  );
1933
1959
  },
1934
- say({ ctx, data: data2 }, [character, content]) {
1935
- if (DEV && !characters[character]) {
1936
- throw new Error(`Attempt to call Say action with unknown character "${character}"`);
1937
- }
1938
- match("dialog", [character, content], {
1939
- ctx,
1940
- data: data2
1941
- });
1942
- },
1943
1960
  function({ ctx, push }, [fn]) {
1944
1961
  const { restoring, goingBack, preview: preview2 } = ctx.meta;
1945
1962
  const result = fn({
@@ -1960,15 +1977,20 @@ var Novely = (() => {
1960
1977
  choices.unshift(question);
1961
1978
  question = "";
1962
1979
  }
1963
- const transformedChoices = choices.map(([content, action2, visible]) => {
1964
- const shown = !visible || visible({
1980
+ const transformedChoices = choices.map(([content, action2, active, visible, image]) => {
1981
+ const activeValue = !active || active({
1982
+ lang: getLanguageFromStore(storageData),
1983
+ state: getStateAtCtx(ctx)
1984
+ });
1985
+ const visibleValue = !visible || visible({
1965
1986
  lang: getLanguageFromStore(storageData),
1966
1987
  state: getStateAtCtx(ctx)
1967
1988
  });
1968
- if (DEV && action2.length === 0 && !shown) {
1989
+ const imageValue = image ? handleImageAsset(image) : "";
1990
+ if (DEV && action2.length === 0 && (!activeValue && !visibleValue)) {
1969
1991
  console.warn(`Choice children should not be empty, either add content there or make item not selectable`);
1970
1992
  }
1971
- return [templateReplace(content, data2), shown];
1993
+ return [templateReplace(content, data2), activeValue, visibleValue, imageValue];
1972
1994
  });
1973
1995
  if (DEV && transformedChoices.length === 0) {
1974
1996
  throw new Error(`Running choice without variants to choose from, look at how to use Choice action properly [https://novely.pages.dev/guide/actions/choice#usage]`);