@drincs/pixi-vn-json 1.13.0 → 1.13.2

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.cts CHANGED
@@ -1,42 +1,11 @@
1
- import { StorageElementType, StepLabelPropsType, CanvasBaseInterface, ImageSprite, ShakeEffectProps, ShowWithDissolveTransitionProps, ShowWithFadeTransitionProps, MoveInOutProps, ZoomInOutProps, PushInOutProps, ImageContainerOptions, ImageSpriteOptions, TextOptions, ImageSpriteMemory, VideoSpriteMemory, ImageContainerMemory, TextMemory, ContainerOptions, ContainerMemory, SoundPlayOptionsWithChannel, MediaInterface, LabelRunModeType, StorageObjectType } from '@drincs/pixi-vn';
1
+ import { StorageElementType, CanvasBaseInterface, ImageSprite, ShakeEffectProps, ShowWithDissolveTransitionProps, ShowWithFadeTransitionProps, MoveInOutProps, ZoomInOutProps, PushInOutProps, ImageContainerOptions, ImageSpriteOptions, TextOptions, ImageSpriteMemory, VideoSpriteMemory, ImageContainerMemory, TextMemory, ContainerOptions, ContainerMemory, SoundPlayOptionsWithChannel, MediaInterface, LabelRunModeType, StorageObjectType, StepLabelPropsType } from '@drincs/pixi-vn';
2
+ import { ZodType } from 'zod';
2
3
  export * from '@drincs/pixi-vn-json/core';
3
4
  export * from '@drincs/pixi-vn-json/interpreter';
4
5
  import { KeyframesType, AnimationOptions, ObjectSegment, ObjectSegmentWithTransition, SequenceOptions } from '@drincs/pixi-vn/motion';
5
6
  import { UPDATE_PRIORITY } from 'pixi.js';
6
7
  export * from '@drincs/pixi-vn-json/translator';
7
8
 
8
- /**
9
- * A middleware handler that can intercept and transform a logic value before it is
10
- * returned by {@link VariableGetter.getLogichValue}.
11
- *
12
- * Handlers are called in registration order, each receiving the current value and a
13
- * `next` function to invoke the rest of the chain (including the built-in resolver).
14
- * Return `undefined` to fall through to `next`.
15
- */
16
- type VariableGetterHandler = <T = StorageElementType>(value: T, next: (value: T) => T | undefined) => T | undefined;
17
- declare namespace VariableGetter {
18
- /**
19
- * Registers a new middleware handler into the resolution chain.
20
- * Handlers are invoked in the order they were added.
21
- *
22
- * @param handler - The middleware handler to register.
23
- */
24
- function add(handler: VariableGetterHandler): void;
25
- /**
26
- * Removes all registered middleware handlers, restoring the default resolution behaviour.
27
- */
28
- function clear(): void;
29
- /**
30
- * Resolves a JSON logic value, running it through all registered middleware handlers
31
- * before falling back to the built-in storage/arithmetic/condition evaluator.
32
- *
33
- * @param value - The value or logic expression to resolve.
34
- * @param props - The current step label props passed to each handler.
35
- * @returns The resolved value, or `undefined`.
36
- */
37
- function getLogichValue<T = StorageElementType>(value: any, props?: StepLabelPropsType): T | undefined;
38
- }
39
-
40
9
  /**
41
10
  * A single element that can appear inside a step-switch, which may be a direct value,
42
11
  * a conditional statement, or a result-to-combine.
@@ -312,13 +281,14 @@ interface PixiVNJsonArithmeticOperationsArithmetic {
312
281
  * Operator of the arithmetic operation:
313
282
  * - `"*"` multiplication
314
283
  * - `"/"` division
315
- * - `"+"` addition
316
- * - `"-"` subtraction
284
+ * - `"+"` addition (concatenation for strings and arrays)
285
+ * - `"-"` subtraction (removal of elements for arrays)
317
286
  * - `"%"` modulo
318
287
  * - `"POW"` exponentiation
319
288
  * - `"RANDOM"` random integer between leftValue and rightValue (inclusive)
289
+ * - `"INTERSECTION"` intersection of two arrays (only elements present in both arrays are included in the result)
320
290
  */
321
- operator: "*" | "/" | "+" | "-" | "%" | "POW" | "RANDOM";
291
+ operator: "*" | "/" | "+" | "-" | "%" | "POW" | "RANDOM" | "INTERSECTION";
322
292
  }
323
293
  /**
324
294
  * Unary arithmetic operation applied to a single value (e.g. INT, FLOOR, FLOAT).
@@ -381,7 +351,7 @@ type PixiVNJsonUnionCondition = PixiVNJsonUnionConditionAndOr | PixiVNJsonUnionC
381
351
  * - `"<="` less than or equal
382
352
  * - `">"` greater than
383
353
  * - `">="` greater than or equal
384
- * - `"CONTAINS"` checks whether the left string/array contains the right value
354
+ * - `"CONTAINS"` checks whether the left string/array contains the right value, if left and right are arrays, checks if all items in right are included in left
385
355
  */
386
356
  type PixiVNJsonComparationOperatorsType = "==" | "!=" | "<" | "<=" | ">" | ">=" | "CONTAINS";
387
357
  /**
@@ -423,6 +393,13 @@ interface PixiVNJsonIfElse<Then> {
423
393
  type: "ifelse";
424
394
  /**
425
395
  * The list of conditions to be checked.
396
+ *
397
+ * if is a {@link StorageElementType} or a {@link PixiVNJsonValueGet}:
398
+ * - if is a array or object, the condition is true if not empty
399
+ * - if is a string, the condition is true if not empty
400
+ * - if is a number, the condition is true if not zero
401
+ * - if is a boolean, the condition is true if true
402
+ * - if is null or undefined, the condition is false
426
403
  */
427
404
  condition: PixiVNJsonConditions;
428
405
  /**
@@ -1083,6 +1060,217 @@ declare const PIXIVNJSON_SCHEMA_URL: string;
1083
1060
  */
1084
1061
  declare const PixiVNJsonComparationOperators: PixiVNJsonComparationOperatorsType[];
1085
1062
 
1063
+ /**
1064
+ * A handler function invoked for each `[key]` token found in the text that passes the
1065
+ * {@link ReplaceHandlerOptions.validation} check.
1066
+ *
1067
+ * @param key The content found inside the square brackets (without the brackets themselves).
1068
+ * For example, for the token `[john]` the key is `"john"`.
1069
+ * @returns The string to substitute in place of `[key]`, or `undefined` to leave the token unchanged.
1070
+ */
1071
+ type ReplaceHandler = (
1072
+ /**
1073
+ * The key to be replaced
1074
+ */
1075
+ key: string) => string | undefined;
1076
+ /**
1077
+ * Configuration options for a text-replacement handler registered via {@link TextReplaces.add}.
1078
+ */
1079
+ type ReplaceHandlerOptions = {
1080
+ /**
1081
+ * A unique name that identifies this handler.
1082
+ * Used for documentation and debugging purposes.
1083
+ */
1084
+ name: string;
1085
+ /**
1086
+ * An optional human-readable description of what this handler does.
1087
+ * Used for documentation purposes.
1088
+ */
1089
+ description?: string;
1090
+ /**
1091
+ * Determines whether this handler should be invoked for a given `[key]` token.
1092
+ *
1093
+ * - `"all"` – the handler is always invoked for every token found.
1094
+ * - `"characterId"` – the handler is invoked only when the key matches a registered character ID
1095
+ * (i.e. the character is present in `RegisteredCharacters`).
1096
+ * - `RegExp` – the key string is tested against the regular expression. The handler is invoked
1097
+ * only if the regex matches.
1098
+ * - `ZodType<string>` – the key string is validated with `schema.safeParse(key)`. The handler
1099
+ * is invoked only if validation succeeds.
1100
+ *
1101
+ * @example
1102
+ * ```ts
1103
+ * // RegExp: only replace keys that look like lowercase identifiers
1104
+ * validation: /^[a-z_]+$/
1105
+ *
1106
+ * // Zod: only replace keys that are one of a fixed set of values
1107
+ * import { z } from "zod"
1108
+ * validation: z.enum(["player", "npc", "enemy"])
1109
+ * ```
1110
+ */
1111
+ validation: RegExp | "characterId" | "all" | ZodType<string>;
1112
+ /**
1113
+ * When this handler should be invoked relative to the translation step.
1114
+ *
1115
+ * - `"before-translation"` – the handler runs **before** {@link onInkTranslate} is called.
1116
+ * Useful for pre-processing tokens, e.g. converting `[key]` into `{{key}}` for i18next.
1117
+ * - `"after-translation"` – the handler runs **after** {@link onInkTranslate} is called.
1118
+ * Useful for substituting values that depend on the translated text.
1119
+ *
1120
+ * @default "before-translation"
1121
+ */
1122
+ type?: "after-translation" | "before-translation";
1123
+ };
1124
+
1125
+ /**
1126
+ * A middleware handler that can intercept and transform a logic value before it is
1127
+ * returned by {@link VariableGetter.getLogichValue}.
1128
+ *
1129
+ * Handlers are called in registration order, each receiving the current value and a
1130
+ * `next` function to invoke the rest of the chain (including the built-in resolver).
1131
+ * Return `undefined` to fall through to `next`.
1132
+ */
1133
+ type VariableGetterHandler = <T = StorageElementType>(value: T, next: (value: T) => T | undefined) => T | undefined;
1134
+
1135
+ /**
1136
+ * Manages text replacement handlers that process content enclosed in square brackets (`[key]`).
1137
+ *
1138
+ * Handlers are called in the order they were added. For each handler, the current text is scanned
1139
+ * for all `[key]` patterns. If the handler's `validation` matches a key, the handler is
1140
+ * invoked with that key. If the handler returns a string, all occurrences of `[key]` are replaced
1141
+ * with the returned value. After a handler finishes processing the text, the next handler starts
1142
+ * on the updated text.
1143
+ *
1144
+ * Each handler specifies whether it runs before or after translation via the `type` field in its
1145
+ * options (defaults to `"before-translation"`).
1146
+ *
1147
+ * @example
1148
+ * ```ts
1149
+ * import { TextReplaces } from 'pixi-vn-ink'
1150
+ * import { getCharacterById } from "@drincs/pixi-vn";
1151
+ *
1152
+ * // Replace [characterId] with the character's display name
1153
+ * TextReplaces.add(
1154
+ * (key) => {
1155
+ * const character = getCharacterById(key)
1156
+ * return character?.name
1157
+ * },
1158
+ * {
1159
+ * name: "character-name",
1160
+ * description: "Replaces character IDs with their display names",
1161
+ * validation: /^[a-z_]+$/,
1162
+ * type: "after-translation",
1163
+ * }
1164
+ * )
1165
+ * // "Hello [john], meet [jane]!" -> "Hello John, meet Jane!"
1166
+ * ```
1167
+ */
1168
+ declare namespace TextReplaces {
1169
+ /**
1170
+ * Configuration options for the `TextReplaces` system.
1171
+ */
1172
+ const options: {
1173
+ /**
1174
+ * The regex used to find replacement tokens in the text (e.g. `[key]`).
1175
+ * @default /\[([^\]]+)\]/
1176
+ */
1177
+ replaceRegex: RegExp;
1178
+ };
1179
+ /**
1180
+ * Registers a new replacement handler.
1181
+ *
1182
+ * Handlers are executed in the order they are added. The first handler added runs first.
1183
+ *
1184
+ * When the first handler of a given type (`"before-translation"` or `"after-translation"`)
1185
+ * is registered, the corresponding translator hook (`translator.beforeToTranslate` or
1186
+ * `translator.afterToTranslate`) is automatically set so that `TextReplaces` takes exclusive
1187
+ * ownership of that hook. Do **not** mix `TextReplaces` with the deprecated
1188
+ * {@link onReplaceTextBeforeTranslation} / {@link onReplaceTextAfterTranslation} functions
1189
+ * for the same phase, as they will overwrite each other's hook.
1190
+ *
1191
+ * @param fn The handler function. Receives the key found inside `[...]` and should return
1192
+ * the replacement string, or `undefined` to leave that token unchanged.
1193
+ * @param handlerOptions Configuration for this handler, including its name, optional
1194
+ * description, validation regex, and execution phase.
1195
+ * @example
1196
+ * ```ts
1197
+ * import { TextReplaces } from 'pixi-vn-ink'
1198
+ *
1199
+ * TextReplaces.add(
1200
+ * (key) => key === "player" ? "Mario" : undefined,
1201
+ * {
1202
+ * name: "player-name",
1203
+ * validation: /^player$/,
1204
+ * }
1205
+ * )
1206
+ * ```
1207
+ */
1208
+ function add(fn: ReplaceHandler, handlerOptions: ReplaceHandlerOptions): void;
1209
+ /**
1210
+ * Removes a previously registered handler function.
1211
+ *
1212
+ * Only the first registration matching `fn` is removed. If the same function was added
1213
+ * multiple times, subsequent registrations remain.
1214
+ *
1215
+ * @param fn The handler function to remove.
1216
+ */
1217
+ function remove(fn: ReplaceHandler): void;
1218
+ /**
1219
+ * Returns metadata for all registered handlers, in registration order.
1220
+ *
1221
+ * @returns An array of {@link ReplaceHandlerOptions} for each registered handler.
1222
+ */
1223
+ function info(): ReplaceHandlerOptions[];
1224
+ /**
1225
+ * Applies all registered handlers of the specified type to the given text in order.
1226
+ *
1227
+ * For each handler:
1228
+ * 1. All `[key]` tokens currently present in the text are collected.
1229
+ * 2. For each unique key, the handler's `validation` is tested against the key.
1230
+ * 3. If validation passes, the handler is called with the key.
1231
+ * 4. If the handler returns a string, **all** occurrences of `[key]` in the text are
1232
+ * replaced with that string.
1233
+ * 5. After the handler has processed all tokens, the next handler starts on the updated text.
1234
+ *
1235
+ * @param text The source text to process.
1236
+ * @param replaceOptions Specifies which phase of handlers to run.
1237
+ * @returns The text after all matching handlers have been applied.
1238
+ * @example
1239
+ * ```ts
1240
+ * // Given handlers that replace [name] -> "Mario" and [surname] -> "Rossi":
1241
+ * TextReplaces.replace("Ciao [name] [surname]. [name] vai pure", { type: "before-translation" })
1242
+ * // => "Ciao Mario Rossi. Mario vai pure"
1243
+ * ```
1244
+ */
1245
+ function replace(text: string, replaceOptions: {
1246
+ /** Which phase of handlers to execute. */
1247
+ type: "after-translation" | "before-translation";
1248
+ }): string;
1249
+ }
1250
+
1251
+ declare namespace VariableGetter {
1252
+ /**
1253
+ * Registers a new middleware handler into the resolution chain.
1254
+ * Handlers are invoked in the order they were added.
1255
+ *
1256
+ * @param handler - The middleware handler to register.
1257
+ */
1258
+ function add(handler: VariableGetterHandler): void;
1259
+ /**
1260
+ * Removes all registered middleware handlers, restoring the default resolution behaviour.
1261
+ */
1262
+ function clear(): void;
1263
+ /**
1264
+ * Resolves a JSON logic value, running it through all registered middleware handlers
1265
+ * before falling back to the built-in storage/arithmetic/condition evaluator.
1266
+ *
1267
+ * @param value - The value or logic expression to resolve.
1268
+ * @param props - The current step label props passed to each handler.
1269
+ * @returns The resolved value, or `undefined`.
1270
+ */
1271
+ function getLogichValue<T = StorageElementType>(value: any, props?: StepLabelPropsType): T | undefined;
1272
+ }
1273
+
1086
1274
  /**
1087
1275
  * Initialises the pixi-vn-json system by registering all default operation handlers
1088
1276
  * (canvas, sound, assets, storage, narration, etc.) into the {@link JsonUnifier}.
@@ -1091,4 +1279,4 @@ declare const PixiVNJsonComparationOperators: PixiVNJsonComparationOperatorsType
1091
1279
  */
1092
1280
  declare function init(): void;
1093
1281
 
1094
- export { PIXIVNJSON_PARAM_ID, PIXIVNJSON_SCHEMA_URL, type PixiVNJson, type PixiVNJsonAnimateBase, type PixiVNJsonAnimateSequence, type PixiVNJsonArithmeticOperations, type PixiVNJsonArithmeticOperationsArithmetic, type PixiVNJsonArithmeticOperationsArithmeticSingle, type PixiVNJsonCanvas, type PixiVNJsonCanvasAnimate, type PixiVNJsonCanvasEdit, type PixiVNJsonCanvasEffect, type PixiVNJsonCanvasImageContainerShow, type PixiVNJsonCanvasImageVideoShow, type PixiVNJsonCanvasRemove, type PixiVNJsonCanvasShow, type PixiVNJsonCanvasTextShow, type PixiVNJsonChoice, type PixiVNJsonChoiceGet, type PixiVNJsonChoices, type PixiVNJsonComparation, PixiVNJsonComparationOperators, type PixiVNJsonComparationOperatorsType, type PixiVNJsonConditionalOperation, type PixiVNJsonConditionalResultToCombine, type PixiVNJsonConditionalStatements, type PixiVNJsonConditions, type PixiVNJsonDialog, type PixiVNJsonDialogText, type PixiVNJsonFunction, type PixiVNJsonIfElse, type PixiVNJsonImageContainerEdit, type PixiVNJsonImageEdit, type PixiVNJsonLabelGet, type PixiVNJsonLabelStep, type PixiVNJsonLabelToOpen, type PixiVNJsonLabels, type PixiVNJsonLogicGet, type PixiVNJsonMediaTransiotions, type PixiVNJsonNarration, type PixiVNJsonOnlyStorageSet, type PixiVNJsonOperation, type PixiVNJsonOperationString, type PixiVNJsonParamGet, type PixiVNJsonSound, type PixiVNJsonStepSwitch, type PixiVNJsonStepSwitchElementType, type PixiVNJsonStepSwitchElementsType, type PixiVNJsonStorageGet, type PixiVNJsonTextEdit, type PixiVNJsonUnionCondition, type PixiVNJsonUnknownEdit, type PixiVNJsonValueGet, type PixiVNJsonValueSet, type PixiVNJsonVideoEdit, type PixiVNJsonVideoPauseResume, VariableGetter, init };
1282
+ export { PIXIVNJSON_PARAM_ID, PIXIVNJSON_SCHEMA_URL, type PixiVNJson, type PixiVNJsonAnimateBase, type PixiVNJsonAnimateSequence, type PixiVNJsonArithmeticOperations, type PixiVNJsonArithmeticOperationsArithmetic, type PixiVNJsonArithmeticOperationsArithmeticSingle, type PixiVNJsonCanvas, type PixiVNJsonCanvasAnimate, type PixiVNJsonCanvasEdit, type PixiVNJsonCanvasEffect, type PixiVNJsonCanvasImageContainerShow, type PixiVNJsonCanvasImageVideoShow, type PixiVNJsonCanvasRemove, type PixiVNJsonCanvasShow, type PixiVNJsonCanvasTextShow, type PixiVNJsonChoice, type PixiVNJsonChoiceGet, type PixiVNJsonChoices, type PixiVNJsonComparation, PixiVNJsonComparationOperators, type PixiVNJsonComparationOperatorsType, type PixiVNJsonConditionalOperation, type PixiVNJsonConditionalResultToCombine, type PixiVNJsonConditionalStatements, type PixiVNJsonConditions, type PixiVNJsonDialog, type PixiVNJsonDialogText, type PixiVNJsonDialogue, type PixiVNJsonFunction, type PixiVNJsonIfElse, type PixiVNJsonImageContainerEdit, type PixiVNJsonImageEdit, type PixiVNJsonInputRequest, type PixiVNJsonLabelGet, type PixiVNJsonLabelStep, type PixiVNJsonLabelToOpen, type PixiVNJsonLabels, type PixiVNJsonLogicGet, type PixiVNJsonMediaTransiotions, type PixiVNJsonNarration, type PixiVNJsonOnlyStorageSet, type PixiVNJsonOperation, type PixiVNJsonOperationString, type PixiVNJsonParamGet, type PixiVNJsonSound, type PixiVNJsonSoundEdit, type PixiVNJsonSoundPauseResume, type PixiVNJsonSoundPlay, type PixiVNJsonSoundRemove, type PixiVNJsonStepSwitch, type PixiVNJsonStepSwitchElementType, type PixiVNJsonStepSwitchElementsType, type PixiVNJsonStorageGet, type PixiVNJsonTextEdit, type PixiVNJsonUnionCondition, type PixiVNJsonUnknownEdit, type PixiVNJsonValueGet, type PixiVNJsonValueSet, type PixiVNJsonVideoEdit, type PixiVNJsonVideoPauseResume, type ReplaceHandler, type ReplaceHandlerOptions, TextReplaces, VariableGetter, type VariableGetterHandler, init };
package/dist/index.d.ts CHANGED
@@ -1,42 +1,11 @@
1
- import { StorageElementType, StepLabelPropsType, CanvasBaseInterface, ImageSprite, ShakeEffectProps, ShowWithDissolveTransitionProps, ShowWithFadeTransitionProps, MoveInOutProps, ZoomInOutProps, PushInOutProps, ImageContainerOptions, ImageSpriteOptions, TextOptions, ImageSpriteMemory, VideoSpriteMemory, ImageContainerMemory, TextMemory, ContainerOptions, ContainerMemory, SoundPlayOptionsWithChannel, MediaInterface, LabelRunModeType, StorageObjectType } from '@drincs/pixi-vn';
1
+ import { StorageElementType, CanvasBaseInterface, ImageSprite, ShakeEffectProps, ShowWithDissolveTransitionProps, ShowWithFadeTransitionProps, MoveInOutProps, ZoomInOutProps, PushInOutProps, ImageContainerOptions, ImageSpriteOptions, TextOptions, ImageSpriteMemory, VideoSpriteMemory, ImageContainerMemory, TextMemory, ContainerOptions, ContainerMemory, SoundPlayOptionsWithChannel, MediaInterface, LabelRunModeType, StorageObjectType, StepLabelPropsType } from '@drincs/pixi-vn';
2
+ import { ZodType } from 'zod';
2
3
  export * from '@drincs/pixi-vn-json/core';
3
4
  export * from '@drincs/pixi-vn-json/interpreter';
4
5
  import { KeyframesType, AnimationOptions, ObjectSegment, ObjectSegmentWithTransition, SequenceOptions } from '@drincs/pixi-vn/motion';
5
6
  import { UPDATE_PRIORITY } from 'pixi.js';
6
7
  export * from '@drincs/pixi-vn-json/translator';
7
8
 
8
- /**
9
- * A middleware handler that can intercept and transform a logic value before it is
10
- * returned by {@link VariableGetter.getLogichValue}.
11
- *
12
- * Handlers are called in registration order, each receiving the current value and a
13
- * `next` function to invoke the rest of the chain (including the built-in resolver).
14
- * Return `undefined` to fall through to `next`.
15
- */
16
- type VariableGetterHandler = <T = StorageElementType>(value: T, next: (value: T) => T | undefined) => T | undefined;
17
- declare namespace VariableGetter {
18
- /**
19
- * Registers a new middleware handler into the resolution chain.
20
- * Handlers are invoked in the order they were added.
21
- *
22
- * @param handler - The middleware handler to register.
23
- */
24
- function add(handler: VariableGetterHandler): void;
25
- /**
26
- * Removes all registered middleware handlers, restoring the default resolution behaviour.
27
- */
28
- function clear(): void;
29
- /**
30
- * Resolves a JSON logic value, running it through all registered middleware handlers
31
- * before falling back to the built-in storage/arithmetic/condition evaluator.
32
- *
33
- * @param value - The value or logic expression to resolve.
34
- * @param props - The current step label props passed to each handler.
35
- * @returns The resolved value, or `undefined`.
36
- */
37
- function getLogichValue<T = StorageElementType>(value: any, props?: StepLabelPropsType): T | undefined;
38
- }
39
-
40
9
  /**
41
10
  * A single element that can appear inside a step-switch, which may be a direct value,
42
11
  * a conditional statement, or a result-to-combine.
@@ -312,13 +281,14 @@ interface PixiVNJsonArithmeticOperationsArithmetic {
312
281
  * Operator of the arithmetic operation:
313
282
  * - `"*"` multiplication
314
283
  * - `"/"` division
315
- * - `"+"` addition
316
- * - `"-"` subtraction
284
+ * - `"+"` addition (concatenation for strings and arrays)
285
+ * - `"-"` subtraction (removal of elements for arrays)
317
286
  * - `"%"` modulo
318
287
  * - `"POW"` exponentiation
319
288
  * - `"RANDOM"` random integer between leftValue and rightValue (inclusive)
289
+ * - `"INTERSECTION"` intersection of two arrays (only elements present in both arrays are included in the result)
320
290
  */
321
- operator: "*" | "/" | "+" | "-" | "%" | "POW" | "RANDOM";
291
+ operator: "*" | "/" | "+" | "-" | "%" | "POW" | "RANDOM" | "INTERSECTION";
322
292
  }
323
293
  /**
324
294
  * Unary arithmetic operation applied to a single value (e.g. INT, FLOOR, FLOAT).
@@ -381,7 +351,7 @@ type PixiVNJsonUnionCondition = PixiVNJsonUnionConditionAndOr | PixiVNJsonUnionC
381
351
  * - `"<="` less than or equal
382
352
  * - `">"` greater than
383
353
  * - `">="` greater than or equal
384
- * - `"CONTAINS"` checks whether the left string/array contains the right value
354
+ * - `"CONTAINS"` checks whether the left string/array contains the right value, if left and right are arrays, checks if all items in right are included in left
385
355
  */
386
356
  type PixiVNJsonComparationOperatorsType = "==" | "!=" | "<" | "<=" | ">" | ">=" | "CONTAINS";
387
357
  /**
@@ -423,6 +393,13 @@ interface PixiVNJsonIfElse<Then> {
423
393
  type: "ifelse";
424
394
  /**
425
395
  * The list of conditions to be checked.
396
+ *
397
+ * if is a {@link StorageElementType} or a {@link PixiVNJsonValueGet}:
398
+ * - if is a array or object, the condition is true if not empty
399
+ * - if is a string, the condition is true if not empty
400
+ * - if is a number, the condition is true if not zero
401
+ * - if is a boolean, the condition is true if true
402
+ * - if is null or undefined, the condition is false
426
403
  */
427
404
  condition: PixiVNJsonConditions;
428
405
  /**
@@ -1083,6 +1060,217 @@ declare const PIXIVNJSON_SCHEMA_URL: string;
1083
1060
  */
1084
1061
  declare const PixiVNJsonComparationOperators: PixiVNJsonComparationOperatorsType[];
1085
1062
 
1063
+ /**
1064
+ * A handler function invoked for each `[key]` token found in the text that passes the
1065
+ * {@link ReplaceHandlerOptions.validation} check.
1066
+ *
1067
+ * @param key The content found inside the square brackets (without the brackets themselves).
1068
+ * For example, for the token `[john]` the key is `"john"`.
1069
+ * @returns The string to substitute in place of `[key]`, or `undefined` to leave the token unchanged.
1070
+ */
1071
+ type ReplaceHandler = (
1072
+ /**
1073
+ * The key to be replaced
1074
+ */
1075
+ key: string) => string | undefined;
1076
+ /**
1077
+ * Configuration options for a text-replacement handler registered via {@link TextReplaces.add}.
1078
+ */
1079
+ type ReplaceHandlerOptions = {
1080
+ /**
1081
+ * A unique name that identifies this handler.
1082
+ * Used for documentation and debugging purposes.
1083
+ */
1084
+ name: string;
1085
+ /**
1086
+ * An optional human-readable description of what this handler does.
1087
+ * Used for documentation purposes.
1088
+ */
1089
+ description?: string;
1090
+ /**
1091
+ * Determines whether this handler should be invoked for a given `[key]` token.
1092
+ *
1093
+ * - `"all"` – the handler is always invoked for every token found.
1094
+ * - `"characterId"` – the handler is invoked only when the key matches a registered character ID
1095
+ * (i.e. the character is present in `RegisteredCharacters`).
1096
+ * - `RegExp` – the key string is tested against the regular expression. The handler is invoked
1097
+ * only if the regex matches.
1098
+ * - `ZodType<string>` – the key string is validated with `schema.safeParse(key)`. The handler
1099
+ * is invoked only if validation succeeds.
1100
+ *
1101
+ * @example
1102
+ * ```ts
1103
+ * // RegExp: only replace keys that look like lowercase identifiers
1104
+ * validation: /^[a-z_]+$/
1105
+ *
1106
+ * // Zod: only replace keys that are one of a fixed set of values
1107
+ * import { z } from "zod"
1108
+ * validation: z.enum(["player", "npc", "enemy"])
1109
+ * ```
1110
+ */
1111
+ validation: RegExp | "characterId" | "all" | ZodType<string>;
1112
+ /**
1113
+ * When this handler should be invoked relative to the translation step.
1114
+ *
1115
+ * - `"before-translation"` – the handler runs **before** {@link onInkTranslate} is called.
1116
+ * Useful for pre-processing tokens, e.g. converting `[key]` into `{{key}}` for i18next.
1117
+ * - `"after-translation"` – the handler runs **after** {@link onInkTranslate} is called.
1118
+ * Useful for substituting values that depend on the translated text.
1119
+ *
1120
+ * @default "before-translation"
1121
+ */
1122
+ type?: "after-translation" | "before-translation";
1123
+ };
1124
+
1125
+ /**
1126
+ * A middleware handler that can intercept and transform a logic value before it is
1127
+ * returned by {@link VariableGetter.getLogichValue}.
1128
+ *
1129
+ * Handlers are called in registration order, each receiving the current value and a
1130
+ * `next` function to invoke the rest of the chain (including the built-in resolver).
1131
+ * Return `undefined` to fall through to `next`.
1132
+ */
1133
+ type VariableGetterHandler = <T = StorageElementType>(value: T, next: (value: T) => T | undefined) => T | undefined;
1134
+
1135
+ /**
1136
+ * Manages text replacement handlers that process content enclosed in square brackets (`[key]`).
1137
+ *
1138
+ * Handlers are called in the order they were added. For each handler, the current text is scanned
1139
+ * for all `[key]` patterns. If the handler's `validation` matches a key, the handler is
1140
+ * invoked with that key. If the handler returns a string, all occurrences of `[key]` are replaced
1141
+ * with the returned value. After a handler finishes processing the text, the next handler starts
1142
+ * on the updated text.
1143
+ *
1144
+ * Each handler specifies whether it runs before or after translation via the `type` field in its
1145
+ * options (defaults to `"before-translation"`).
1146
+ *
1147
+ * @example
1148
+ * ```ts
1149
+ * import { TextReplaces } from 'pixi-vn-ink'
1150
+ * import { getCharacterById } from "@drincs/pixi-vn";
1151
+ *
1152
+ * // Replace [characterId] with the character's display name
1153
+ * TextReplaces.add(
1154
+ * (key) => {
1155
+ * const character = getCharacterById(key)
1156
+ * return character?.name
1157
+ * },
1158
+ * {
1159
+ * name: "character-name",
1160
+ * description: "Replaces character IDs with their display names",
1161
+ * validation: /^[a-z_]+$/,
1162
+ * type: "after-translation",
1163
+ * }
1164
+ * )
1165
+ * // "Hello [john], meet [jane]!" -> "Hello John, meet Jane!"
1166
+ * ```
1167
+ */
1168
+ declare namespace TextReplaces {
1169
+ /**
1170
+ * Configuration options for the `TextReplaces` system.
1171
+ */
1172
+ const options: {
1173
+ /**
1174
+ * The regex used to find replacement tokens in the text (e.g. `[key]`).
1175
+ * @default /\[([^\]]+)\]/
1176
+ */
1177
+ replaceRegex: RegExp;
1178
+ };
1179
+ /**
1180
+ * Registers a new replacement handler.
1181
+ *
1182
+ * Handlers are executed in the order they are added. The first handler added runs first.
1183
+ *
1184
+ * When the first handler of a given type (`"before-translation"` or `"after-translation"`)
1185
+ * is registered, the corresponding translator hook (`translator.beforeToTranslate` or
1186
+ * `translator.afterToTranslate`) is automatically set so that `TextReplaces` takes exclusive
1187
+ * ownership of that hook. Do **not** mix `TextReplaces` with the deprecated
1188
+ * {@link onReplaceTextBeforeTranslation} / {@link onReplaceTextAfterTranslation} functions
1189
+ * for the same phase, as they will overwrite each other's hook.
1190
+ *
1191
+ * @param fn The handler function. Receives the key found inside `[...]` and should return
1192
+ * the replacement string, or `undefined` to leave that token unchanged.
1193
+ * @param handlerOptions Configuration for this handler, including its name, optional
1194
+ * description, validation regex, and execution phase.
1195
+ * @example
1196
+ * ```ts
1197
+ * import { TextReplaces } from 'pixi-vn-ink'
1198
+ *
1199
+ * TextReplaces.add(
1200
+ * (key) => key === "player" ? "Mario" : undefined,
1201
+ * {
1202
+ * name: "player-name",
1203
+ * validation: /^player$/,
1204
+ * }
1205
+ * )
1206
+ * ```
1207
+ */
1208
+ function add(fn: ReplaceHandler, handlerOptions: ReplaceHandlerOptions): void;
1209
+ /**
1210
+ * Removes a previously registered handler function.
1211
+ *
1212
+ * Only the first registration matching `fn` is removed. If the same function was added
1213
+ * multiple times, subsequent registrations remain.
1214
+ *
1215
+ * @param fn The handler function to remove.
1216
+ */
1217
+ function remove(fn: ReplaceHandler): void;
1218
+ /**
1219
+ * Returns metadata for all registered handlers, in registration order.
1220
+ *
1221
+ * @returns An array of {@link ReplaceHandlerOptions} for each registered handler.
1222
+ */
1223
+ function info(): ReplaceHandlerOptions[];
1224
+ /**
1225
+ * Applies all registered handlers of the specified type to the given text in order.
1226
+ *
1227
+ * For each handler:
1228
+ * 1. All `[key]` tokens currently present in the text are collected.
1229
+ * 2. For each unique key, the handler's `validation` is tested against the key.
1230
+ * 3. If validation passes, the handler is called with the key.
1231
+ * 4. If the handler returns a string, **all** occurrences of `[key]` in the text are
1232
+ * replaced with that string.
1233
+ * 5. After the handler has processed all tokens, the next handler starts on the updated text.
1234
+ *
1235
+ * @param text The source text to process.
1236
+ * @param replaceOptions Specifies which phase of handlers to run.
1237
+ * @returns The text after all matching handlers have been applied.
1238
+ * @example
1239
+ * ```ts
1240
+ * // Given handlers that replace [name] -> "Mario" and [surname] -> "Rossi":
1241
+ * TextReplaces.replace("Ciao [name] [surname]. [name] vai pure", { type: "before-translation" })
1242
+ * // => "Ciao Mario Rossi. Mario vai pure"
1243
+ * ```
1244
+ */
1245
+ function replace(text: string, replaceOptions: {
1246
+ /** Which phase of handlers to execute. */
1247
+ type: "after-translation" | "before-translation";
1248
+ }): string;
1249
+ }
1250
+
1251
+ declare namespace VariableGetter {
1252
+ /**
1253
+ * Registers a new middleware handler into the resolution chain.
1254
+ * Handlers are invoked in the order they were added.
1255
+ *
1256
+ * @param handler - The middleware handler to register.
1257
+ */
1258
+ function add(handler: VariableGetterHandler): void;
1259
+ /**
1260
+ * Removes all registered middleware handlers, restoring the default resolution behaviour.
1261
+ */
1262
+ function clear(): void;
1263
+ /**
1264
+ * Resolves a JSON logic value, running it through all registered middleware handlers
1265
+ * before falling back to the built-in storage/arithmetic/condition evaluator.
1266
+ *
1267
+ * @param value - The value or logic expression to resolve.
1268
+ * @param props - The current step label props passed to each handler.
1269
+ * @returns The resolved value, or `undefined`.
1270
+ */
1271
+ function getLogichValue<T = StorageElementType>(value: any, props?: StepLabelPropsType): T | undefined;
1272
+ }
1273
+
1086
1274
  /**
1087
1275
  * Initialises the pixi-vn-json system by registering all default operation handlers
1088
1276
  * (canvas, sound, assets, storage, narration, etc.) into the {@link JsonUnifier}.
@@ -1091,4 +1279,4 @@ declare const PixiVNJsonComparationOperators: PixiVNJsonComparationOperatorsType
1091
1279
  */
1092
1280
  declare function init(): void;
1093
1281
 
1094
- export { PIXIVNJSON_PARAM_ID, PIXIVNJSON_SCHEMA_URL, type PixiVNJson, type PixiVNJsonAnimateBase, type PixiVNJsonAnimateSequence, type PixiVNJsonArithmeticOperations, type PixiVNJsonArithmeticOperationsArithmetic, type PixiVNJsonArithmeticOperationsArithmeticSingle, type PixiVNJsonCanvas, type PixiVNJsonCanvasAnimate, type PixiVNJsonCanvasEdit, type PixiVNJsonCanvasEffect, type PixiVNJsonCanvasImageContainerShow, type PixiVNJsonCanvasImageVideoShow, type PixiVNJsonCanvasRemove, type PixiVNJsonCanvasShow, type PixiVNJsonCanvasTextShow, type PixiVNJsonChoice, type PixiVNJsonChoiceGet, type PixiVNJsonChoices, type PixiVNJsonComparation, PixiVNJsonComparationOperators, type PixiVNJsonComparationOperatorsType, type PixiVNJsonConditionalOperation, type PixiVNJsonConditionalResultToCombine, type PixiVNJsonConditionalStatements, type PixiVNJsonConditions, type PixiVNJsonDialog, type PixiVNJsonDialogText, type PixiVNJsonFunction, type PixiVNJsonIfElse, type PixiVNJsonImageContainerEdit, type PixiVNJsonImageEdit, type PixiVNJsonLabelGet, type PixiVNJsonLabelStep, type PixiVNJsonLabelToOpen, type PixiVNJsonLabels, type PixiVNJsonLogicGet, type PixiVNJsonMediaTransiotions, type PixiVNJsonNarration, type PixiVNJsonOnlyStorageSet, type PixiVNJsonOperation, type PixiVNJsonOperationString, type PixiVNJsonParamGet, type PixiVNJsonSound, type PixiVNJsonStepSwitch, type PixiVNJsonStepSwitchElementType, type PixiVNJsonStepSwitchElementsType, type PixiVNJsonStorageGet, type PixiVNJsonTextEdit, type PixiVNJsonUnionCondition, type PixiVNJsonUnknownEdit, type PixiVNJsonValueGet, type PixiVNJsonValueSet, type PixiVNJsonVideoEdit, type PixiVNJsonVideoPauseResume, VariableGetter, init };
1282
+ export { PIXIVNJSON_PARAM_ID, PIXIVNJSON_SCHEMA_URL, type PixiVNJson, type PixiVNJsonAnimateBase, type PixiVNJsonAnimateSequence, type PixiVNJsonArithmeticOperations, type PixiVNJsonArithmeticOperationsArithmetic, type PixiVNJsonArithmeticOperationsArithmeticSingle, type PixiVNJsonCanvas, type PixiVNJsonCanvasAnimate, type PixiVNJsonCanvasEdit, type PixiVNJsonCanvasEffect, type PixiVNJsonCanvasImageContainerShow, type PixiVNJsonCanvasImageVideoShow, type PixiVNJsonCanvasRemove, type PixiVNJsonCanvasShow, type PixiVNJsonCanvasTextShow, type PixiVNJsonChoice, type PixiVNJsonChoiceGet, type PixiVNJsonChoices, type PixiVNJsonComparation, PixiVNJsonComparationOperators, type PixiVNJsonComparationOperatorsType, type PixiVNJsonConditionalOperation, type PixiVNJsonConditionalResultToCombine, type PixiVNJsonConditionalStatements, type PixiVNJsonConditions, type PixiVNJsonDialog, type PixiVNJsonDialogText, type PixiVNJsonDialogue, type PixiVNJsonFunction, type PixiVNJsonIfElse, type PixiVNJsonImageContainerEdit, type PixiVNJsonImageEdit, type PixiVNJsonInputRequest, type PixiVNJsonLabelGet, type PixiVNJsonLabelStep, type PixiVNJsonLabelToOpen, type PixiVNJsonLabels, type PixiVNJsonLogicGet, type PixiVNJsonMediaTransiotions, type PixiVNJsonNarration, type PixiVNJsonOnlyStorageSet, type PixiVNJsonOperation, type PixiVNJsonOperationString, type PixiVNJsonParamGet, type PixiVNJsonSound, type PixiVNJsonSoundEdit, type PixiVNJsonSoundPauseResume, type PixiVNJsonSoundPlay, type PixiVNJsonSoundRemove, type PixiVNJsonStepSwitch, type PixiVNJsonStepSwitchElementType, type PixiVNJsonStepSwitchElementsType, type PixiVNJsonStorageGet, type PixiVNJsonTextEdit, type PixiVNJsonUnionCondition, type PixiVNJsonUnknownEdit, type PixiVNJsonValueGet, type PixiVNJsonValueSet, type PixiVNJsonVideoEdit, type PixiVNJsonVideoPauseResume, type ReplaceHandler, type ReplaceHandlerOptions, TextReplaces, VariableGetter, type VariableGetterHandler, init };
package/dist/index.mjs CHANGED
@@ -1,2 +1,2 @@
1
- import {getValueFromConditionalStatements,getLogichValue,getConditionalStep,setInitialStorageValue,setStorageValue,videoOperation,soundOperation,loadAssets,narrationOperation,textOperation,imageOperation,imageContainerOperation,effectOperation,canvasElementOperation,animateOperation}from'@drincs/pixi-vn-json/actions';import {JsonUnifier}from'@drincs/pixi-vn-json/core';export*from'@drincs/pixi-vn-json/core';export*from'@drincs/pixi-vn-json/interpreter';export*from'@drincs/pixi-vn-json/translator';var t;(A=>{let e=new Set;function L(i){e.add(i);}A.add=L;function J(){e.clear();}A.clear=J;function w(i,o={}){let n=r=>getLogichValue(r,o)??void 0,a=getValueFromConditionalStatements(i,o);if(e.size===0)return n(a);let s=n;for(let r of e){let d=s;s=c=>r(c,d)??void 0;}return s(a)}A.getLogichValue=w;})(t||(t={}));var p={version:"1.13.0"};var f="___param___",x=`https://pixi-vn.web.app/schemas/${p.version}/schema.json`,h=["==","!=","<","<=",">",">=","CONTAINS"];function X(){JsonUnifier.init({animateOperation:animateOperation,canvasElementOperation:canvasElementOperation,effectOperation:effectOperation,imageContainerOperation:imageContainerOperation,imageOperation:imageOperation,textOperation:textOperation,narrationOperation:narrationOperation,loadAssets:loadAssets,soundOperation:soundOperation,videoOperation:videoOperation,setStorageValue:setStorageValue,setInitialStorageValue:setInitialStorageValue,getLogichValue:t.getLogichValue,getConditionalStep:getConditionalStep});}
2
- export{f as PIXIVNJSON_PARAM_ID,x as PIXIVNJSON_SCHEMA_URL,h as PixiVNJsonComparationOperators,t as VariableGetter,X as init};
1
+ import {getValueFromConditionalStatements,getLogichValue,getConditionalStep,setInitialStorageValue,setStorageValue,videoOperation,soundOperation,loadAssets,narrationOperation,textOperation,imageOperation,imageContainerOperation,effectOperation,canvasElementOperation,animateOperation}from'@drincs/pixi-vn-json/actions';import {JsonUnifier}from'@drincs/pixi-vn-json/core';export*from'@drincs/pixi-vn-json/core';import {translator as translator$1}from'@drincs/pixi-vn-json/translator';export*from'@drincs/pixi-vn-json/translator';import {RegisteredCharacters}from'@drincs/pixi-vn/characters';import {ZodType}from'zod';export*from'@drincs/pixi-vn-json/interpreter';var l;(v=>{let o=new Set;function n(d){o.add(d);}v.add=n;function m(){o.clear();}v.clear=m;function f(d,u={}){let p=e=>getLogichValue(e,u)??void 0,c=getValueFromConditionalStatements(d,u);if(o.size===0)return p(c);let a=p;for(let e of o){let r=a;a=t=>e(t,r)??void 0;}return a(c)}v.getLogichValue=f;})(l||(l={}));var y={version:"1.13.2"};var P="___param___",S=`https://pixi-vn.web.app/schemas/${y.version}/schema.json`,_=["==","!=","<","<=",">",">=","CONTAINS"];var b;(a=>{a.options={replaceRegex:/\[([^\]]+)\]/};let n=[],m=false,f=false;function v(e,r){n.push({fn:e,opts:r});let t=r.type??"before-translation";t==="before-translation"&&!m?(translator$1.beforeToTranslate=s=>p(s,{type:"before-translation"}),m=true):t==="after-translation"&&!f&&(translator$1.afterToTranslate=s=>p(s,{type:"after-translation"}),f=true);}a.add=v;function d(e){let r=n.findIndex(t=>t.fn===e);r!==-1&&n.splice(r,1);}a.remove=d;function u(){return n.map(e=>e.opts)}a.info=u;function p(e,r){let t=n.filter(s=>(s.opts.type??"before-translation")===r.type);for(let s of t)e=c(e,s.fn,s.opts.validation);return e}a.replace=p;function c(e,r,t){let s=new RegExp(a.options.replaceRegex.source,"g"),V=[...e.matchAll(s)],h=new Set,g=[];for(let i of V)h.has(i[1])||(h.add(i[1]),g.push(i[1]));for(let i of g){if(t==="characterId"){if(!RegisteredCharacters.has(i))continue}else if(t!=="all"){if(t instanceof RegExp){if(!t.test(i))continue}else if(t instanceof ZodType&&!t.safeParse(i).success)continue}let x=r(i);x!==void 0&&(e=e.replaceAll(`[${i}]`,x));}return e}})(b||(b={}));function ae(){JsonUnifier.init({animateOperation:animateOperation,canvasElementOperation:canvasElementOperation,effectOperation:effectOperation,imageContainerOperation:imageContainerOperation,imageOperation:imageOperation,textOperation:textOperation,narrationOperation:narrationOperation,loadAssets:loadAssets,soundOperation:soundOperation,videoOperation:videoOperation,setStorageValue:setStorageValue,setInitialStorageValue:setInitialStorageValue,getLogichValue:l.getLogichValue,getConditionalStep:getConditionalStep});}
2
+ export{P as PIXIVNJSON_PARAM_ID,S as PIXIVNJSON_SCHEMA_URL,_ as PixiVNJsonComparationOperators,b as TextReplaces,l as VariableGetter,ae as init};