@novely/core 0.44.2 → 0.45.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, visible: boolean, image: string][], resolve: (selected: number) => void) => void;
104
+ choices: (question: string, choices: [name: string, active: Stored<boolean>, visible: Stored<boolean>, onselect: ChoiceOnSelectFunction, 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>;
@@ -707,7 +707,7 @@ interface ActionInputOnInputMeta<L extends string, S extends State> {
707
707
  */
708
708
  state: StateFunction<S>;
709
709
  }
710
- type FunctionActionProps<L extends string, S extends State> = {
710
+ type FunctionActionProps<L extends Lang, S extends State> = {
711
711
  restoring: boolean;
712
712
  goingBack: boolean;
713
713
  preview: boolean;
@@ -720,7 +720,7 @@ type FunctionActionProps<L extends string, S extends State> = {
720
720
  */
721
721
  state: StateFunction<S>;
722
722
  };
723
- type ChoiceCheckFunctionProps<L extends string, S extends State> = {
723
+ type ChoiceCheckFunctionProps<L extends Lang, S extends State> = {
724
724
  /**
725
725
  * Language
726
726
  */
@@ -730,25 +730,28 @@ type ChoiceCheckFunctionProps<L extends string, S extends State> = {
730
730
  */
731
731
  state: S;
732
732
  };
733
- type ChoiceCheckFunction<L extends string, S extends State> = (props: ChoiceCheckFunctionProps<L, S>) => boolean;
733
+ type ChoiceCheckFunction<L extends Lang, S extends State> = (props: ChoiceCheckFunctionProps<L, S>) => boolean;
734
+ type ChoiceOnSelectFunction = () => Thenable<void>;
734
735
  type ConditionCheckFunction<S extends State, R extends string | true | false> = (state: S) => R;
735
736
  type FunctionAction<L extends string, S extends State> = (props: FunctionActionProps<L, S>) => Thenable<void>;
736
737
  type ActionInputSetupCleanup = () => void;
737
738
  type ActionInputSetup = (input: HTMLInputElement) => ActionInputSetupCleanup | void;
738
739
  type BackgroundImage = Record<string, string | NovelyAsset>;
739
740
  type VoiceAction<L extends Lang> = (params: string | NovelyAsset | Partial<Record<L, string | NovelyAsset>>) => ValidAction;
740
- type ActionChoiceChoiceObject<Languages extends Lang, S extends State> = {
741
- title: TextContent<Languages, S>;
741
+ type ActionChoiceChoiceObject<L extends Lang, S extends State> = {
742
+ title: TextContent<L, S>;
742
743
  children: ValidAction[];
743
- active?: ChoiceCheckFunction<Languages, S>;
744
- visible?: ChoiceCheckFunction<Languages, S>;
744
+ active?: ChoiceCheckFunction<L, S>;
745
+ visible?: ChoiceCheckFunction<L, S>;
746
+ onSelect?: ChoiceOnSelectFunction;
745
747
  image?: string | NovelyAsset;
746
748
  };
747
- type ActionChoiceChoice<Languages extends Lang, S extends State> = [
748
- title: TextContent<Languages, S>,
749
+ type ActionChoiceChoice<L extends Lang, S extends State> = [
750
+ title: TextContent<L, S>,
749
751
  actions: ValidAction[],
750
- active?: ChoiceCheckFunction<Languages, S>,
751
- visible?: ChoiceCheckFunction<Languages, S>,
752
+ active?: ChoiceCheckFunction<L, S>,
753
+ visible?: ChoiceCheckFunction<L, S>,
754
+ onSelect?: ChoiceOnSelectFunction,
752
755
  image?: string | NovelyAsset
753
756
  ];
754
757
  type ActionProxy<Characters extends Record<string, Character>, Languages extends Lang, S extends State> = {
@@ -974,4 +977,4 @@ declare const extendAction: <Part0 extends Record<string, (...args: any[]) => Va
974
977
  */
975
978
  declare const asset: (...variants: string[]) => NovelyAsset;
976
979
 
977
- export { type ActionChoiceChoice, type ActionChoiceChoiceObject, type ActionInputOnInputMeta, type ActionInputSetup, type ActionInputSetupCleanup, type ActionProxy, type AllowedContent, type AudioHandle, type BackgroundImage, type BaseTranslationStrings, type Character, type CharacterAssetSizes, type CharacterHandle, type CharactersData, type ChoiceCheckFunction, type ChoiceParams, type ConditionParams, type Context, type CoreData, type CustomActionHandle, type CustomHandler, type CustomHandlerFunctionGetFn, type CustomHandlerFunctionParameters, type CustomHandlerGetResult, type CustomHandlerGetResultDataFunction, type Data, type DeepPartial, type DefaultActionProxy, EN, type Emotions, type FunctionParams, type FunctionableValue, type GetActionParameters, type InputHandler, JP, KK, type Lang, type NovelyInit, type NovelyScreen, type NovelyStorage, type Path, type PluralType, type Pluralization, RU, type Renderer, type RendererInit, type RendererInitPreviewReturn, type Save, type Stack, type StackHolder, type State, type StateFunction, type StorageData, type StorageMeta, type Stored, type Story, type TextContent, type Thenable, type TranslationActions, type TypeEssentials, type TypewriterSpeed, type ValidAction, asset, extendAction, localStorageStorage, novely };
980
+ export { type ActionChoiceChoice, type ActionChoiceChoiceObject, type ActionInputOnInputMeta, type ActionInputSetup, type ActionInputSetupCleanup, type ActionProxy, type AllowedContent, type AudioHandle, type BackgroundImage, type BaseTranslationStrings, type Character, type CharacterAssetSizes, type CharacterHandle, type CharactersData, type ChoiceCheckFunction, type ChoiceCheckFunctionProps, type ChoiceOnSelectFunction, type ChoiceParams, type ConditionCheckFunction, type ConditionParams, type Context, type CoreData, type CustomActionHandle, type CustomHandler, type CustomHandlerFunction, type CustomHandlerFunctionGetFn, type CustomHandlerFunctionParameters, type CustomHandlerGetResult, type CustomHandlerGetResultDataFunction, type CustomHandlerInfo, type Data, type DeepPartial, type DefaultActionProxy, EN, type Emotions, type FunctionParams, type FunctionableValue, type GetActionParameters, type InputHandler, JP, KK, type Lang, type NovelyInit, type NovelyScreen, type NovelyStorage, type Path, type PluralType, type Pluralization, RU, type Renderer, type RendererInit, type RendererInitPreviewReturn, type Save, type Stack, type StackHolder, type State, type StateFunction, type StorageData, type StorageMeta, type Stored, type Story, type TextContent, type Thenable, type TranslationActions, type TypeEssentials, type TypewriterSpeed, type ValidAction, asset, extendAction, localStorageStorage, novely };
@@ -364,16 +364,16 @@ var Novely = (() => {
364
364
  var isAction = (element) => {
365
365
  return Array.isArray(element) && isString(element[0]);
366
366
  };
367
+ var flatActions = (item) => {
368
+ return item.flatMap((data) => {
369
+ const type = data[0];
370
+ if (Array.isArray(type)) return flatActions(data);
371
+ return [data];
372
+ });
373
+ };
367
374
  var flattenStory = (story) => {
368
375
  const entries = Object.entries(story).map(([name, items]) => {
369
- const flat = (item) => {
370
- return item.flatMap((data) => {
371
- const type = data[0];
372
- if (Array.isArray(type)) return flat(data);
373
- return [data];
374
- });
375
- };
376
- return [name, flat(items)];
376
+ return [name, flatActions(items)];
377
377
  });
378
378
  return Object.fromEntries(entries);
379
379
  };
@@ -1441,9 +1441,10 @@ var Novely = (() => {
1441
1441
  const choice = props[i];
1442
1442
  props[i] = [
1443
1443
  choice.title,
1444
- choice.children,
1444
+ flatActions(choice.children),
1445
1445
  choice.active,
1446
1446
  choice.visible,
1447
+ choice.onSelect,
1447
1448
  choice.image
1448
1449
  ];
1449
1450
  }
@@ -1978,20 +1979,29 @@ var Novely = (() => {
1978
1979
  choices.unshift(question);
1979
1980
  question = "";
1980
1981
  }
1981
- const transformedChoices = choices.map(([content, action2, active, visible, image]) => {
1982
- const activeValue = !active || active({
1983
- lang: getLanguageFromStore(storageData),
1984
- state: getStateAtCtx(ctx)
1985
- });
1986
- const visibleValue = !visible || visible({
1987
- lang: getLanguageFromStore(storageData),
1988
- state: getStateAtCtx(ctx)
1989
- });
1982
+ const transformedChoices = choices.map(([content, _children, active, visible, onSelect, image]) => {
1983
+ const active$ = store(false);
1984
+ const visible$ = store(false);
1985
+ const update = () => {
1986
+ const activeValue = !active || active({
1987
+ lang: getLanguageFromStore(storageData),
1988
+ state: getStateAtCtx(ctx)
1989
+ });
1990
+ const visibleValue = !visible || visible({
1991
+ lang: getLanguageFromStore(storageData),
1992
+ state: getStateAtCtx(ctx)
1993
+ });
1994
+ active$.set(activeValue);
1995
+ visible$.set(visibleValue);
1996
+ };
1997
+ update();
1998
+ const onSelectGuarded = onSelect || noop;
1999
+ const onSelectWrapped = async () => {
2000
+ await onSelectGuarded();
2001
+ update();
2002
+ };
1990
2003
  const imageValue = image ? handleImageAsset(image) : "";
1991
- if (DEV && action2.length === 0 && (!activeValue && !visibleValue)) {
1992
- console.warn(`Choice children should not be empty, either add content there or make item not selectable`);
1993
- }
1994
- return [templateReplace(content, data2), activeValue, visibleValue, imageValue];
2004
+ return [templateReplace(content, data2), active$, visible$, onSelectWrapped, imageValue];
1995
2005
  });
1996
2006
  if (DEV && transformedChoices.length === 0) {
1997
2007
  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]`);