tw5-typed 0.2.1 → 0.2.4

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.1",
5
+ "version": "0.2.4",
6
6
  "url": "https://github.com/tiddly-gittly/tw5-typed",
7
7
  "homepage": "https://github.com/tiddly-gittly/tw5-typed",
8
8
  "bugs": {
@@ -20,17 +20,17 @@
20
20
  "prepublishOnly": "npx tsc --noEmit"
21
21
  },
22
22
  "devDependencies": {
23
- "@types/node": "^17.0.23",
24
- "@typescript-eslint/eslint-plugin": "5.17.0",
25
- "@typescript-eslint/parser": "5.17.0",
26
- "eslint": "8.12.0",
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",
27
27
  "eslint-config-prettier": "8.5.0",
28
28
  "eslint-config-standard": "16.0.3",
29
29
  "eslint-config-standard-with-typescript": "21.0.1",
30
30
  "eslint-import-resolver-alias": "1.1.2",
31
- "eslint-import-resolver-typescript": "2.7.0",
31
+ "eslint-import-resolver-typescript": "2.7.1",
32
32
  "eslint-plugin-html": "6.2.0",
33
- "eslint-plugin-import": "2.25.4",
33
+ "eslint-plugin-import": "2.26.0",
34
34
  "eslint-plugin-n": "15.1.0",
35
35
  "eslint-plugin-node": "11.1.0",
36
36
  "eslint-plugin-prettier": "4.0.0",
@@ -41,7 +41,7 @@
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
- "prettier": "2.6.1",
44
+ "prettier": "2.6.2",
45
45
  "typescript": "4.6.3"
46
46
  }
47
47
  }
package/src/Wiki.d.ts CHANGED
@@ -1,6 +1,17 @@
1
1
  /// <reference path="parser.d.ts" />
2
2
 
3
3
  declare module 'tiddlywiki' {
4
+ export interface IMakeWidgetOptions extends IRenderOptions {
5
+ document: typeof document | IFakeDocument;
6
+ }
7
+
8
+ export type OutputMimeTypes = 'text/html' | 'text/plain-formatted' | 'text/plain';
9
+ export type TextMimeTypes = 'text/html' | 'text/vnd.tiddlywiki' | 'text/plain';
10
+ export interface IRenderOptions {
11
+ parentWidget?: Widget;
12
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
13
+ variables?: Record<string, any>;
14
+ }
4
15
  export class Wiki {
5
16
  /**
6
17
  * Wiki constructor. State is stored in private members that only a small number of privileged accessor methods have direct access. Methods added via the prototype have to use these accessors and cannot access the state data directly.
@@ -31,9 +42,9 @@ declare module 'tiddlywiki' {
31
42
  */
32
43
  addTiddler: (tiddler: Tiddler | Partial<ITiddlerFields>) => void;
33
44
  /**
34
- * Call `addTiddler` for each iton of the list
45
+ * Call `addTiddler` for each iton of the list, but should passing `tiddler.fields`, directly passing tiddler object may failed to add in some cases.
35
46
  */
36
- addTiddlers: (tiddler: Array<Tiddler | Partial<ITiddlerFields>>) => void;
47
+ addTiddlers: (tiddler: Array<Partial<ITiddlerFields>>) => void;
37
48
  /**
38
49
  * Get tiddler's text field, with an optional default text.
39
50
  * If have _is_skinny field, will just return null (this is a rare case, so not put in the return type for now).
@@ -43,6 +54,36 @@ declare module 'tiddlywiki' {
43
54
  */
44
55
  getTiddlerText(title: string, fallbackText: string): string;
45
56
  getTiddlerText(title: string, fallbackText?: string): string | undefined;
57
+ /**
58
+ Get the content of a tiddler as a JavaScript object. How this is done depends on the type of the tiddler:
59
+
60
+ application/json: the tiddler JSON is parsed into an object
61
+ application/x-tiddler-dictionary: the tiddler is parsed as sequence of name:value pairs
62
+
63
+ Other types currently just return undefined or as same as fallbackData.
64
+
65
+ titleOrTiddler: string tiddler title or a tiddler object
66
+ defaultData: default data to be returned if the tiddler is missing or doesn't contain data
67
+
68
+ Alternative, uncached version of getTiddlerDataCached(). The return value can be mutated freely and reused
69
+ */
70
+ getTiddlerData<D extends Record<string, unknown> | undefined>(titleOrTiddler: string, fallbackData?: D): D;
71
+ getTiddlerData<D extends Record<string, unknown> | undefined>(titleOrTiddler: Tiddler, fallbackData?: D): D;
72
+ /**
73
+ Get the content of a tiddler as a JavaScript object. How this is done depends on the type of the tiddler:
74
+
75
+ application/json: the tiddler JSON is parsed into an object
76
+ application/x-tiddler-dictionary: the tiddler is parsed as sequence of name:value pairs
77
+
78
+ Other types currently just return undefined or as same as fallbackData.
79
+
80
+ titleOrTiddler: string tiddler title or a tiddler object
81
+ defaultData: default data to be returned if the tiddler is missing or doesn't contain data
82
+
83
+ Note that the same value is returned for repeated calls for the same tiddler data. The value is frozen to prevent modification; otherwise modifications would be visible to all callers
84
+ */
85
+ getTiddlerDataCached<D extends Record<string, unknown> | undefined>(titleOrTiddler: string, fallbackData?: D): D;
86
+ getTiddlerDataCached<D extends Record<string, unknown> | undefined>(titleOrTiddler: Tiddler, fallbackData?: D): D;
46
87
  /**
47
88
  * Set tiddler text of any field.
48
89
  *
@@ -52,8 +93,68 @@ declare module 'tiddlywiki' {
52
93
  * @param {string} value text content to set
53
94
  * @param {object} options options, see tiddlywiki dev doc for details
54
95
  */
55
- setText: (title: string, field?: string, index?: string | undefined, value?: string, options?: any) => void;
96
+ setText: (title: string, field?: string, index?: string | undefined, value?: string, options?: { suppressTimestamp?: boolean }) => void;
56
97
  parseTiddler(title: string, options?: IParserOptions): WikiParser;
57
98
  parseText(type: string, text: string, options?: IParserOptions): WikiParser;
99
+ /**
100
+ Parse text from a tiddler and render it into another format
101
+ outputType: content type for the output
102
+ title: title of the tiddler to be rendered
103
+ options: see below
104
+ Options include:
105
+ variables: hashmap of variables to set
106
+ parentWidget: optional parent widget for the root node
107
+ */
108
+ renderTiddler(outputType: OutputMimeTypes, title: string, options?: IRenderOptions): string;
109
+ /**
110
+ Make a widget tree for a parse tree
111
+ @params parser: parser object
112
+ @params options: see below
113
+ Options include:
114
+ document: optional document to use
115
+ variables: hashmap of variables to set
116
+ parentWidget: optional parent widget for the root node
117
+ */
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
+ makeWidget(parser: WikiParser, options?: IMakeWidgetOptions): Widget;
130
+ /**
131
+ Make a widget tree for transclusion
132
+ @params title: target tiddler title
133
+ @params options: as for wiki.makeWidget() plus:
134
+
135
+ - options.field: optional field to transclude (defaults to "text")
136
+ - options.mode: transclusion mode "inline" or "block"
137
+ - options.recursionMarker : optional flag to set a recursion marker, defaults to "yes"
138
+ - options.children: optional array of children for the transclude widget
139
+ - options.importVariables: optional importvariables filter string for macros to be included
140
+ - options.importPageMacros: optional boolean; if true, equivalent to passing "[[$:/core/ui/PageMacros]] [all[shadows+tiddlers]tag[$:/tags/Macro]!has[draft.of]]" to options.importVariables
141
+ */
142
+ makeTranscludeWidget(
143
+ title: string,
144
+ options: {
145
+ /** optional array of children for the transclude widget */
146
+ children?: Widget[];
147
+ /** optional field to transclude (defaults to "text") */
148
+ field?: string;
149
+ /** optional boolean; if true, equivalent to passing "[[$:/core/ui/PageMacros]] [all[shadows+tiddlers]tag[$:/tags/Macro]!has[draft.of]]" to options.importVariables */
150
+ importPageMacros?: boolean;
151
+ /** optional importvariables filter string for macros to be included */
152
+ importVariables?: string;
153
+ /** transclusion mode "inline" or "block" */
154
+ mode?: 'inline' | 'block';
155
+ /** optional flag to set a recursion marker, defaults to "yes" */
156
+ recursionMarker?: 'yes' | 'no';
157
+ } & IMakeWidgetOptions,
158
+ ): Widget;
58
159
  }
59
160
  }
package/src/server.d.ts CHANGED
@@ -74,14 +74,14 @@ declare module 'tiddlywiki' {
74
74
  variables: Record<string, any>;
75
75
  get(variableName: string): any;
76
76
  requestHandler: ServerEndpointHandler;
77
- /*
77
+ /**
78
78
  Listen for requests
79
79
  port: optional port number (falls back to value of "port" variable)
80
80
  host: optional host address (falls back to value of "host" variable)
81
81
  prefix: optional prefix (falls back to value of "path-prefix" variable)
82
82
  */
83
83
  listen(port?: string, host?: string, prefix?: string): void;
84
- /*
84
+ /**
85
85
  Check whether a given user is authorized for the specified authorizationType ("readers" or "writers"). Pass null or undefined as the username to check for anonymous access
86
86
  */
87
87
  isAuthorized(authorizationType: 'readers' | 'writers', username?: string | undefined): boolean;
package/src/tw.d.ts CHANGED
@@ -59,9 +59,19 @@ declare module 'tiddlywiki' {
59
59
 
60
60
  config: ITWConfig;
61
61
 
62
+ /**
63
+ Global Hooks mechanism which allows plugins to modify default functionality
64
+ */
62
65
  hooks: {
66
+ /**
67
+ Add hooks to the hashmap
68
+ */
63
69
  addHook(hookName: 'th-server-command-post-start', callback: (listenCommand: unknown, server: Server) => void): void;
64
70
  addHook(hookName: string, callback: (...arguments_: unknown[]) => unknown): void;
71
+ /**
72
+ Invoke the hook by key
73
+ */
74
+ invokeHook(hookName: string): void;
65
75
  };
66
76
 
67
77
  modules: ITWModules;
@@ -70,17 +80,18 @@ declare module 'tiddlywiki' {
70
80
  /** Broswer features, if tw isn't running on a browser environment, the value will be `null` */
71
81
  nodeWebKit: null | object;
72
82
 
83
+ notifier: Notifier;
73
84
  /** Convenience function for pushing a tiddler onto the preloading array */
74
85
  preloadTiddler(fields: Record<string, unknown>): void;
75
86
  /** Convenience function for pushing an array of tiddlers onto the preloading array */
76
87
  preloadTiddlerArray(fieldsArray: Array<Record<string, unknown>>): void;
88
+
77
89
  /** External JavaScript can populate this array before calling boot.js in order to preload tiddlers */
78
90
  preloadTiddlers: Record<string, Record<string, unknown>>;
79
91
 
80
92
  rootWidget: Widget;
81
93
 
82
94
  utils: ITWUtils;
83
-
84
95
  version: string;
85
96
  wiki: Wiki;
86
97
  }
package/src/utils.d.ts CHANGED
@@ -15,6 +15,20 @@ declare module 'tiddlywiki' {
15
15
  * @param className
16
16
  */
17
17
  addClass(element: Element, className: string): void;
18
+ /**
19
+ Attach specified event handlers to a DOM node
20
+ @param domNode: where to attach the event handlers
21
+ @param events: array of event handlers to be added (see below)
22
+ Each entry in the events array is an object with these properties:
23
+ - name: event name of `addEventListener`
24
+ - handlerFunction: optional event handler function
25
+ - handlerObject: optional event handler object
26
+ - handlerMethod: optionally specifies object handler method name (defaults to `handleEvent`)
27
+ */
28
+ addEventListeners(
29
+ domNode: Node,
30
+ events: Array<{ handlerFunction?: (event: MouseEvent) => void; handlerMethod?: string; handlerObject?: Widget; name: string }>,
31
+ ): void;
18
32
  /** Returns true if the version string A is greater than the version string B. Returns true if the versions are the same */
19
33
  checkVersions(versionStringA: string, versionStringB: string): boolean;
20
34
  /**
@@ -26,8 +40,6 @@ declare module 'tiddlywiki' {
26
40
  decodeURIComponentSafe(uri: string): string;
27
41
  /** Convert a URI encoded string to a string safely */
28
42
  decodeURISafe(uri: string): string;
29
- /** the function behind `<<now "format">> */
30
- formatDateString(date: Date, format: string): string;
31
43
  /** Fill in any null or undefined properties of an object with the properties from a list of source objects. Each property that is an object is called recursively */
32
44
  deepDefaults(object: object, ...sourceObjectList: object[]): object;
33
45
  /**
@@ -82,6 +94,8 @@ declare module 'tiddlywiki' {
82
94
  evalSandboxed(code: string, context: IModuleSandbox, filename: string): unknown;
83
95
  /** Extend an object with the properties from a list of source objects */
84
96
  extend(object: object, ...sourceObjectList: object[]): object;
97
+ /** the function behind `<<now "format">> */
98
+ formatDateString(date: Date, format: string): string;
85
99
  /** Given an extension, always access the $tw.config.fileExtensionInfo using a lowercase extension only. */
86
100
  getFileExtensionInfo(extension: string): IFileExtensionInfo | null;
87
101
  /** Get the browser location.hash. We don't use location.hash because of the way that Firefox auto-urldecodes it (see http://stackoverflow.com/questions/1703552/encoding-of-window-location-hash) */
@@ -154,4 +168,16 @@ declare module 'tiddlywiki' {
154
168
  /** Stringify an array of tiddler titles into a list string */
155
169
  stringifyList(value: string[]): string;
156
170
  }
171
+ /**
172
+ * Notifier mechanism
173
+ */
174
+ export class Notifier {
175
+ /*
176
+ Display a notification
177
+ title: Title of tiddler containing the notification text
178
+ options: see below
179
+ Options include:
180
+ */
181
+ display(title: string, options?: Record<string, unknown>): void;
182
+ }
157
183
  }
package/src/widget.d.ts CHANGED
@@ -9,26 +9,26 @@ declare module 'tiddlywiki' {
9
9
  }
10
10
 
11
11
  export interface IWidgetEvent {
12
- /** widget event can carry any other parameters
12
+ /** maybe a DOM click event, if trigger by button click */
13
+ event: Event;
14
+ navigateFromTitle?: string;
15
+ /**
16
+ * Get `$param`
17
+ */
18
+ param?: string | undefined;
19
+ /** Optional hashmap of additional tiddler fields. Widget event can carry any other parameters
13
20
  *
14
21
  * For example, `<$action-sendmessage $message="tw-mobile-sync-set-active-server-and-sync" title={{!!title}} />` will produce `paramObject: { title: "xxxx" }`
15
22
  */
16
23
  paramObject?: {
17
24
  [othersParamKeys: string]: unknown;
18
25
  };
19
- /**
20
- * Get `$param`
21
- */
22
- param?: string | undefined;
23
26
  /** the first parameter of addEventListener
24
27
  *
25
28
  * For example, the `'open-command-palette'` in `$tw.rootWidget.addEventListener('open-command-palette', (e: IWidgetEvent) => this.openPalette(e));`
26
29
  */
27
30
  type: string;
28
31
  widget: Widget;
29
- /** maybe a DOM click event, if trigger by button click */
30
- event: Event;
31
- navigateFromTitle?: string;
32
32
  }
33
33
 
34
34
  // eslint-disable-next-line @typescript-eslint/no-extraneous-class
@@ -38,11 +38,22 @@ declare module 'tiddlywiki' {
38
38
  * @link https://tiddlywiki.com/dev/#Widgets
39
39
  */
40
40
  export class Widget {
41
- constructor(parseTreeNode: unknown, options: unknown);
42
- initialize: (parseTreeNode: unknown, options: unknown) => void;
43
- parseTreeNode: unknown;
44
- wiki: unknown;
41
+ constructor(parseTreeNode: IParseTreeNode, options?: unknown);
42
+ initialize: (parseTreeNode: IParseTreeNode, options?: unknown) => void;
43
+ parseTreeNode: IParseTreeNode;
44
+ wiki: ITiddlyWiki;
45
45
  parentWidget?: Widget;
46
+ children: Widget[];
47
+ /*
48
+ Make child widgets correspondng to specified parseTreeNodes
49
+ */
50
+ makeChildWidgets(parseTreeNodes: IParseTreeNode[], options?: { variables?: unknown }): void;
51
+ /**
52
+ Construct the widget object for a parse tree node
53
+ options include:
54
+ variables: optional hashmap of variables to wrap around the widget
55
+ */
56
+ makeChildWidget(parseTreeNode: IParseTreeNode, options?: { variables?: unknown }): void;
46
57
  variablesConstructor: variablesConstructor;
47
58
  variables: unknown;
48
59
  domNodes: Node[];
@@ -65,7 +76,7 @@ declare module 'tiddlywiki' {
65
76
  /**
66
77
  * Lifecycle method: Render this widget into the DOM
67
78
  */
68
- render(parent: Node, nextSibling: Node): void;
79
+ render(parent: Node, nextSibling: Node | null): void;
69
80
  /**
70
81
  * Selectively refreshes the widget if needed. Returns true if the widget or any of its children needed re-rendering.
71
82
  * You can do some cleanup or buildup before return true.
@@ -73,6 +84,15 @@ declare module 'tiddlywiki' {
73
84
  * @link https://tiddlywiki.com/dev/#Selective%20Update
74
85
  */
75
86
  refresh(changedTiddlers: IChangedTiddlers): boolean;
87
+ /**
88
+ Refresh all the children of a widget
89
+ will call `this.render`
90
+ */
91
+ refreshChildren(changedTiddlers: IChangedTiddlers): boolean;
92
+ /**
93
+ Rebuild a previously rendered widget
94
+ */
95
+ refreshSelf(): boolean;
76
96
  computeAttributes(): void;
77
97
  /**
78
98
  * Get parameters that user set in the widget
@@ -97,4 +117,25 @@ declare module 'tiddlywiki' {
97
117
  */
98
118
  getVariable(name: string, options?: object): string;
99
119
  }
120
+
121
+ export interface IFakeDocument {
122
+ compatMode: string;
123
+ createElement: (tag: string) => TW_Element;
124
+ createElementNS: (namespace: string, tag: string) => TW_Element;
125
+ createTextNode: (text: string) => TW_TextNode;
126
+ isTiddlyWikiFakeDom: boolean;
127
+ setSequenceNumber: (value: any) => void;
128
+ }
129
+ export class TW_Element {
130
+ isTiddlyWikiFakeDom: boolean;
131
+ tag: string;
132
+ attributes: Record<string, unknown>;
133
+ isRaw: boolean;
134
+ children: Array<TW_Element | TW_TextNode>;
135
+ _style: Record<string, unknown>;
136
+ namespaceURI: string;
137
+ }
138
+ export class TW_TextNode {
139
+ textContent: string;
140
+ }
100
141
  }