tw5-typed 0.2.4 → 0.2.7

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/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "description": "Types for tiddlywiki",
3
3
  "license": "MIT",
4
4
  "name": "tw5-typed",
5
- "version": "0.2.4",
5
+ "version": "0.2.7",
6
6
  "url": "https://github.com/tiddly-gittly/tw5-typed",
7
7
  "homepage": "https://github.com/tiddly-gittly/tw5-typed",
8
8
  "bugs": {
@@ -20,28 +20,28 @@
20
20
  "prepublishOnly": "npx tsc --noEmit"
21
21
  },
22
22
  "devDependencies": {
23
- "@types/node": "^17.0.24",
24
- "@typescript-eslint/eslint-plugin": "5.19.0",
25
- "@typescript-eslint/parser": "5.19.0",
26
- "eslint": "8.13.0",
23
+ "@types/node": "^17.0.31",
24
+ "@typescript-eslint/eslint-plugin": "5.21.0",
25
+ "@typescript-eslint/parser": "5.21.0",
26
+ "eslint": "8.14.0",
27
27
  "eslint-config-prettier": "8.5.0",
28
- "eslint-config-standard": "16.0.3",
28
+ "eslint-config-standard": "17.0.0",
29
29
  "eslint-config-standard-with-typescript": "21.0.1",
30
30
  "eslint-import-resolver-alias": "1.1.2",
31
31
  "eslint-import-resolver-typescript": "2.7.1",
32
32
  "eslint-plugin-html": "6.2.0",
33
33
  "eslint-plugin-import": "2.26.0",
34
- "eslint-plugin-n": "15.1.0",
34
+ "eslint-plugin-n": "15.2.0",
35
35
  "eslint-plugin-node": "11.1.0",
36
36
  "eslint-plugin-prettier": "4.0.0",
37
37
  "eslint-plugin-promise": "6.0.0",
38
38
  "eslint-plugin-react": "7.29.4",
39
- "eslint-plugin-react-hooks": "4.4.0",
40
- "eslint-plugin-security": "1.4.0",
39
+ "eslint-plugin-react-hooks": "4.5.0",
40
+ "eslint-plugin-security": "1.5.0",
41
41
  "eslint-plugin-security-node": "1.1.1",
42
42
  "eslint-plugin-typescript-sort-keys": "2.1.0",
43
43
  "eslint-plugin-unicorn": "42.0.0",
44
44
  "prettier": "2.6.2",
45
- "typescript": "4.6.3"
45
+ "typescript": "4.6.4"
46
46
  }
47
47
  }
package/src/Wiki.d.ts CHANGED
@@ -25,12 +25,40 @@ declare module 'tiddlywiki' {
25
25
  /**
26
26
  * Get full list of tiddler titles in the wiki
27
27
  */
28
- getTiddlers: () => string[];
28
+ getTiddlers(): string[];
29
+ /**
30
+ * Return a named global cache object. Global cache objects are cleared whenever a tiddler change.
31
+ * You can put anything into the cache.
32
+ * @param cacheName key of the cache
33
+ * @param initializer when cache miss, this will be called to get initial value
34
+ */
35
+ getGlobalCache<T>(cacheName: string, initializer: () => T): T;
36
+ /**
37
+ * Return the named cache object for a tiddler. If the cache doesn't exist then the initializer function is invoked to create it
38
+ * @param cacheName key of the cache
39
+ * @param initializer when cache miss, this will be called to get initial value
40
+ */
41
+ getCacheForTiddler<T>(title: string, cacheName: string, initializer: () => T): T;
42
+ /**
43
+ * clear all cache, will be called when a tiddler is changed
44
+ */
45
+ clearGlobalCache(): void;
29
46
  /**
30
47
  * Compile filter string to be a function that execute the filter in the wiki.
31
48
  * You can pass an optional iterator that provide the input to the returned function. If no iterator is provided, filter will use first operator to get input.
49
+ *
50
+ * @returns a function with the signature fn(source,widget) where:
51
+ source: an iterator function for the source tiddlers, called source(iterator), where iterator is called as iterator(tiddler,title)
52
+ widget: an optional widget node for retrieving the current tiddler etc.
32
53
  */
33
- compileFilter: (filterString: string) => (iterator?: SourceIterator) => string[];
54
+ compileFilter(filterString: string): (source?: SourceIterator, widget?: Widget) => string[];
55
+ /**
56
+ *
57
+ * @param filterString
58
+ * @param widget an optional widget node for retrieving the current tiddler etc.
59
+ * @param source an iterator function for the source tiddlers, called source(iterator), where iterator is called as iterator(tiddler,title)
60
+ */
61
+ filterTiddlers(filterString: string, widget?: Widget, source?: SourceIterator): string[];
34
62
  /**
35
63
  * Set JSON tiddler, Object in data field will be JSON.stringify and put into the text.
36
64
  * This will make tiddler to be JSON data tiddler `"type":"application/json"`, so if you just want to modify existed tiddler's data, use `addTiddler` instead.
@@ -106,6 +134,16 @@ declare module 'tiddlywiki' {
106
134
  parentWidget: optional parent widget for the root node
107
135
  */
108
136
  renderTiddler(outputType: OutputMimeTypes, title: string, options?: IRenderOptions): string;
137
+ /**
138
+ Parse text in a specified format and render it into another format
139
+ @param outputType content type for the output
140
+ @param textType content type of the input text
141
+ @param text input text
142
+ @param options see below, Options includes:
143
+ - variables: hashmap of variables to set
144
+ - parentWidget: optional parent widget for the root node
145
+ */
146
+ renderText(outputType: OutputMimeTypes, textType: TextMimeTypes, text: string, options?: Partial<IMakeWidgetOptions> & IParserOptions): string;
109
147
  /**
110
148
  Make a widget tree for a parse tree
111
149
  @params parser: parser object
@@ -115,17 +153,6 @@ declare module 'tiddlywiki' {
115
153
  variables: hashmap of variables to set
116
154
  parentWidget: optional parent widget for the root node
117
155
  */
118
- /**
119
- Parse text in a specified format and render it into another format
120
- outputType: content type for the output
121
- textType: content type of the input text
122
- text: input text
123
- options: see below
124
- Options include:
125
- variables: hashmap of variables to set
126
- parentWidget: optional parent widget for the root node
127
- */
128
- renderText(outputType: OutputMimeTypes, textType: TextMimeTypes, text: string, options?: IRenderOptions): string;
129
156
  makeWidget(parser: WikiParser, options?: IMakeWidgetOptions): Widget;
130
157
  /**
131
158
  Make a widget tree for transclusion
package/src/ast.d.ts CHANGED
@@ -22,9 +22,18 @@ declare module 'tiddlywiki' {
22
22
  type: 'text';
23
23
  }
24
24
  export interface ILinkParseTreeNode extends IWikiASTNode {
25
- text: string;
25
+ text?: string;
26
26
  type: 'link';
27
27
  }
28
+ export interface IImageParseTreeNode extends IWikiASTNode {
29
+ type: 'image';
30
+ }
31
+ export interface ITranscludeParseTreeNode extends IWikiASTNode {
32
+ type: 'transclude';
33
+ }
34
+ export interface ITiddlerParseTreeNode extends IWikiASTNode {
35
+ type: 'tiddler';
36
+ }
28
37
  export type HTMLTags = keyof HTMLElementTagNameMap | 'strike';
29
38
 
30
39
  export interface IDomParseTreeNode extends IWikiASTNode {
@@ -64,5 +73,13 @@ declare module 'tiddlywiki' {
64
73
  text?: string;
65
74
  type: string;
66
75
  }
67
- export type IParseTreeNode = IDomParseTreeNode | IMacroParameterCallParseTreeNode | IMacroCallParseTreeNode | ITextParseTreeNode | ICustomParseTreeNode;
76
+ export type IParseTreeNode =
77
+ | IDomParseTreeNode
78
+ | IMacroParameterCallParseTreeNode
79
+ | IMacroCallParseTreeNode
80
+ | ITextParseTreeNode
81
+ | IImageParseTreeNode
82
+ | ITranscludeParseTreeNode
83
+ | ITiddlerParseTreeNode
84
+ | ICustomParseTreeNode;
68
85
  }
package/src/tw.d.ts CHANGED
@@ -30,6 +30,10 @@ declare module 'tiddlywiki' {
30
30
  type: string;
31
31
  }
32
32
 
33
+ export interface ILanguage {
34
+ getString(key: string, options: { variables: { title: string } }): string;
35
+ }
36
+
33
37
  export interface IContentTypeInfo {
34
38
  deserializerType: string;
35
39
  encoding: string;
@@ -71,9 +75,10 @@ declare module 'tiddlywiki' {
71
75
  /**
72
76
  Invoke the hook by key
73
77
  */
74
- invokeHook(hookName: string): void;
78
+ invokeHook(hookName: string, event: IWidgetEvent): undefined | IWidgetEvent;
75
79
  };
76
80
 
81
+ language: ILanguage;
77
82
  modules: ITWModules;
78
83
  /** NodeJS features, if tw isn't running on a NodeJS environment, the value will be `null` */
79
84
  node: null | object;
package/src/utils.d.ts CHANGED
@@ -4,7 +4,10 @@
4
4
  declare module 'tiddlywiki' {
5
5
  export type TWDocument = Document;
6
6
  export type TWDOMElement = Element;
7
- export type TWEachCallback<T> = (element?: unknown, index?: string | number, object?: T) => boolean | undefined;
7
+ /** Callback is invoked with (element, index, object), if callback returns false, then the each loop will be terminated. */
8
+ export type TWEachCallback<O, I> =
9
+ | ((element?: I, indexOrKey?: string | number, object?: O) => boolean | undefined)
10
+ | ((element?: I, indexOrKey?: string | number, object?: O) => void);
8
11
  export interface ITWUtils {
9
12
  Crypto: typeof Crypto;
10
13
  PasswordPrompt: typeof PasswordPrompt;
@@ -85,7 +88,7 @@ declare module 'tiddlywiki' {
85
88
  * Iterate through all the own properties of an object or array.
86
89
  * Callback is invoked with (element, index, object), if callback returns false, then the each loop will be terminated.
87
90
  */
88
- each<T = object | unknown[]>(object: T, callback: TWEachCallback<T>): void;
91
+ each<I = any>(object: Record<string, I> | I[], callback: TWEachCallback<Record<string, I> | I[], I>): void;
89
92
  /** Display an error and exit */
90
93
  error(error: Event | string): void;
91
94
  /** Run code globally with specified context variables in scope */
package/src/widget.d.ts CHANGED
@@ -9,8 +9,9 @@ declare module 'tiddlywiki' {
9
9
  }
10
10
 
11
11
  export interface IWidgetEvent {
12
+ [extraKeys: string]: unknown;
12
13
  /** maybe a DOM click event, if trigger by button click */
13
- event: Event;
14
+ event: UIEvent | Event;
14
15
  navigateFromTitle?: string;
15
16
  /**
16
17
  * Get `$param`
@@ -23,6 +24,7 @@ declare module 'tiddlywiki' {
23
24
  paramObject?: {
24
25
  [othersParamKeys: string]: unknown;
25
26
  };
27
+ tiddlerTitle?: string;
26
28
  /** the first parameter of addEventListener
27
29
  *
28
30
  * For example, the `'open-command-palette'` in `$tw.rootWidget.addEventListener('open-command-palette', (e: IWidgetEvent) => this.openPalette(e));`
@@ -39,21 +41,30 @@ declare module 'tiddlywiki' {
39
41
  */
40
42
  export class Widget {
41
43
  constructor(parseTreeNode: IParseTreeNode, options?: unknown);
42
- initialize: (parseTreeNode: IParseTreeNode, options?: unknown) => void;
44
+ initialize(parseTreeNode: IParseTreeNode, options?: unknown): void;
43
45
  parseTreeNode: IParseTreeNode;
44
46
  wiki: ITiddlyWiki;
45
47
  parentWidget?: Widget;
48
+ /** we can use $tw.rootWidget.widgetClasses.widget to new a widget */
49
+ widgetClasses: Widget;
50
+ /** we can use $tw.rootWidget.widgetClasses.widget to new a widget
51
+ *
52
+ * Like `new widget.widget(widgetNode,{` in `$tw.wiki.makeWidget`
53
+ */
54
+ widget: new (parseTreeNode: IParseTreeNode, options?: unknown) => Widget;
46
55
  children: Widget[];
47
- /*
48
- Make child widgets correspondng to specified parseTreeNodes
49
- */
50
- makeChildWidgets(parseTreeNodes: IParseTreeNode[], options?: { variables?: unknown }): void;
51
56
  /**
52
- Construct the widget object for a parse tree node
57
+ Make child widgets correspondng to specified parseTreeNodes
58
+ And push them to `this.children`
59
+ @param parseTreeNodes default to `this.parseTreeNode.children`, can be undefined
60
+ */
61
+ makeChildWidgets(parseTreeNodes?: IParseTreeNode[], options?: { variables?: unknown }): void;
62
+ /**
63
+ Construct the widget object for a parse tree node, and return the new widget
53
64
  options include:
54
65
  variables: optional hashmap of variables to wrap around the widget
55
66
  */
56
- makeChildWidget(parseTreeNode: IParseTreeNode, options?: { variables?: unknown }): void;
67
+ makeChildWidget(parseTreeNode: IParseTreeNode, options?: { variables?: unknown }): Widget;
57
68
  variablesConstructor: variablesConstructor;
58
69
  variables: unknown;
59
70
  domNodes: Node[];
@@ -71,12 +82,23 @@ Make child widgets correspondng to specified parseTreeNodes
71
82
  addEventListeners(listeners: Array<{ handler: (event: IWidgetEvent) => void | Promise<void>; type: string }>): void;
72
83
 
73
84
  parentDomNode: Node;
74
- execute: () => void;
85
+ /**
86
+ Compute the internal state of the widget.
87
+ This will be automatically called in the `render` method.
88
+
89
+ For example, `getAttribute` and set them to class members.
90
+
91
+ */
92
+ execute(): void;
75
93
 
76
94
  /**
77
95
  * Lifecycle method: Render this widget into the DOM
78
96
  */
79
97
  render(parent: Node, nextSibling: Node | null): void;
98
+ /**
99
+ * Render the children of this widget into the DOM
100
+ */
101
+ renderChildren(parent: Node, nextSibling: Node | null): void;
80
102
  /**
81
103
  * Selectively refreshes the widget if needed. Returns true if the widget or any of its children needed re-rendering.
82
104
  * You can do some cleanup or buildup before return true.
@@ -93,7 +115,7 @@ Make child widgets correspondng to specified parseTreeNodes
93
115
  Rebuild a previously rendered widget
94
116
  */
95
117
  refreshSelf(): boolean;
96
- computeAttributes(): void;
118
+ computeAttributes(): Record<string, IParseTreeAttribute>;
97
119
  /**
98
120
  * Get parameters that user set in the widget
99
121
  * @param name attribute name, for example, `actions` in the button widget