macro.d.ts 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. export interface MacroTags {
  2. /**
  3. * Return the named macro tag's parents array (includes the names of all macros who have registered the tag as a
  4. * child), or null on failure.
  5. * @param name Name of the macro tag whose parents array should be returned.
  6. * @since 2.0.0
  7. * @example
  8. * Macro.tags.get("else") // For the standard library, returns: ["if"]
  9. */
  10. get(name: string): string[];
  11. /**
  12. * Returns whether the named macro tag exists.
  13. * @param name Name of the macro tag to search for.
  14. * @since 2.0.0
  15. */
  16. has(name: string): boolean;
  17. }
  18. export interface MacroArgsArray extends Array<any> {
  19. /**
  20. * The current tag's argument string after converting all TwineScript syntax elements into their
  21. * native JavaScript counterparts. Equivalent in function to <MacroContext>.args.full.
  22. */
  23. full: string;
  24. /**
  25. * The current tag's unprocessed argument string. Equivalent in function to <MacroContext>.args.raw.
  26. */
  27. raw: string;
  28. }
  29. export interface MacroContextObject {
  30. /**
  31. * Name of the current tag.
  32. */
  33. name: string;
  34. /**
  35. * The current tag's argument string parsed into an array of discrete arguments.
  36. * Equivalent in function to <MacroContext>.args.
  37. */
  38. args: MacroArgsArray;
  39. /**
  40. * The current tag's contents — i.e. the text between the current tag and the next.
  41. */
  42. contents: string;
  43. }
  44. type ShadowWrapperCallback<T extends any[]> = (this: MacroContext, ...args: T) => void;
  45. export interface MacroContext {
  46. /**
  47. * The argument string parsed into an array of discrete arguments.
  48. * @since 2.0.0
  49. */
  50. args: MacroArgsArray;
  51. /**
  52. * The name of the macro.
  53. * @since 2.0.0
  54. */
  55. name: string;
  56. /**
  57. * The current output element.
  58. * @since 2.0.0
  59. */
  60. output: DocumentFragment | HTMLElement;
  61. /**
  62. * The (execution) context object of the macro's parent, or null if the macro has no parent.
  63. * @since 2.0.0
  64. */
  65. parent: null | object;
  66. /**
  67. * The parser instanced that generated the macro call.
  68. * @since 2.0.0
  69. */
  70. parser: unknown;
  71. /**
  72. * The text of a container macro parsed into discrete payload objects by tag.
  73. * @since 2.0.0
  74. */
  75. payload: MacroContextObject[];
  76. /**
  77. * The macro's definition — created via @see Macro.add()
  78. * @since 2.0.0
  79. */
  80. self: object;
  81. /**
  82. * Returns whether any of the macro's ancestors passed the test implemented by the given
  83. * filter function.
  84. * @param filter he function used to test each ancestor execution context object, which
  85. * is passed in as its sole parameter.
  86. * @since 2.0.0
  87. */
  88. contextHas(filter: (context: MacroContextObject) => boolean): boolean;
  89. /**
  90. * Returns the first of the macro's ancestors which passed the test implemented by the given
  91. * filter function or null, if no members pass.
  92. * @param filter The function used to test each ancestor execution context object, which is
  93. * passed in as its sole parameter.
  94. * @since 2.0.0
  95. */
  96. contextSelect(filter: (context: MacroContextObject) => boolean): object;
  97. /**
  98. * Returns a new array containing all of the macro's ancestors which passed the test implemented
  99. * by the given filter function or an empty array, if no members pass.
  100. * @since 2.0.0
  101. * @param filter
  102. */
  103. contextSelectAll(filter: (context: MacroContextObject) => boolean): object[];
  104. /**
  105. * Returns a callback function that wraps the given callbacks to provide access to the variable
  106. * shadowing system.
  107. * This is only useful if you have an asynchronous callback (such as a button being pressed)
  108. * that invokes code/content that needs to access variables shadowed by `<<capture>>`.
  109. * @param callback Executed when the wrapper is invoked. Receives access to variable shadows.
  110. * @param doneCallback Executed after the main callback returns. Does not have access.
  111. * @param startCallback Executed before the main callback is invoked. Does not have access.
  112. * @since 2.14.0
  113. */
  114. createShadowWrapper<T extends any[]>(
  115. callback: ShadowWrapperCallback<T>,
  116. doneCallback?: ShadowWrapperCallback<T>,
  117. startCallback?: ShadowWrapperCallback<T>,
  118. ): (...args: T) => void;
  119. /**
  120. * Renders the message prefixed with the name of the macro and returns false.
  121. * @param message The error message to output.
  122. * @since 2.0.0
  123. */
  124. error(message: string): false;
  125. }
  126. export interface MacroDefinition {
  127. handler: (this: MacroContext) => void;
  128. /**
  129. * Having this property signifies that this is a container macro
  130. * This should be an array of child tag names or `null`
  131. * @since 2.0.0
  132. */
  133. tags?: string[] | null | undefined;
  134. /**
  135. * Disables parsing argument strings into discrete arguments.
  136. * This is used by macros that only use the raw/full argument strings.
  137. * `true` to affect all tags
  138. * Or Array of tags to affect
  139. * @since 2.0.0
  140. */
  141. skipArgs?: boolean | undefined | string[];
  142. }
  143. export interface MacroAPI {
  144. /**
  145. * Add new macro(s).
  146. * @param name Name, or array of names, of the macro(s) to add.
  147. * @param definition Definition of the macro(s) or the name of an existing macro whose definition to copy.
  148. * Definition object:
  149. * A macro definition object should have some of the following properties (only handler is absolutely required):
  150. * skipArgs: (optional, boolean) Disables parsing argument strings into discrete arguments. Used by macros which
  151. * only use the raw/full argument strings.
  152. * tags: (optional, null | string array) Signifies that the macro is a container macro—i.e. not self-closing. An
  153. * array of the names of the child tags, or null if there are no child tags.
  154. * handler: (function) The macro's main function. It will be called without arguments, but with its this set to a
  155. * macro context object.
  156. * @param deep Enables deep cloning of the definition. Used to give macros separate instances of the same
  157. * definition.
  158. * @since 2.0.0
  159. * @example
  160. * // Example of a very simple/naive <<if>>/<<elseif>>/<<else>> macro implementation.
  161. * Macro.add('if', {
  162. * skipArgs: true,
  163. * tags: ['elseif', 'else'],
  164. * handler: function () {
  165. * try {
  166. * for (var i = 0, len = this.payload.length; i < len; ++i) {
  167. * if (
  168. * this.payload[i].name === 'else' ||
  169. * !!Scripting.evalJavaScript(this.payload[i].args.full)
  170. * ) {
  171. * jQuery(this.output).wiki(this.payload[i].contents);
  172. * break;
  173. * }
  174. * }
  175. * }
  176. * catch (ex) {
  177. * return this.error('bad conditional expression: ' + ex.message);
  178. * }
  179. * }
  180. * });
  181. */
  182. add(name: string | string[], definition: MacroDefinition, deep?: boolean): void;
  183. /**
  184. * Remove existing macro(s).
  185. * @param name Name, or array of names, of the macro(s) to remove.
  186. * @since 2.0.0
  187. */
  188. delete(name: string | string[]): void;
  189. /**
  190. * Return the named macro definition, or null on failure.
  191. * @param name Name of the macro whose definition should be returned.
  192. * @since 2.0.0
  193. */
  194. get(name: string): MacroDefinition;
  195. /**
  196. * Returns whether the named macro exists.
  197. * @param name Name of the macro to search for.
  198. * @since 2.0.0
  199. */
  200. has(name: string): boolean;
  201. /**
  202. * @since 2.0.0
  203. */
  204. tags: MacroTags;
  205. }
  206. export {};