tw5-typed 0.2.0 → 0.2.3

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.0",
5
+ "version": "0.2.3",
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/Tiddler.d.ts CHANGED
@@ -1,6 +1,10 @@
1
1
  declare module 'tiddlywiki' {
2
2
  export class Tiddler {
3
- constructor(...fields: Array<Record<string, unknown> | Tiddler>);
3
+ /**
4
+ *
5
+ * @param tiddlers multiple tiddler fields or instances, will merge them to create a new one
6
+ */
7
+ constructor(...tiddlers: Array<Record<string, unknown> | Tiddler>);
4
8
  readonly cache: ITiddlerCache;
5
9
  readonly fields: ITiddlerFields;
6
10
  static fieldModules: Record<string, IModuleInfo>;
@@ -10,9 +14,9 @@ declare module 'tiddlywiki' {
10
14
 
11
15
  export interface ITiddlerFields {
12
16
  readonly [anyKey: string]: unknown;
13
- readonly color: string;
17
+ readonly color?: string;
14
18
  readonly created: Date;
15
- readonly list: string[];
19
+ readonly list?: string[];
16
20
  readonly modified: Date;
17
21
  readonly tags: string[];
18
22
  readonly text: string;
package/src/Wiki.d.ts CHANGED
@@ -1,6 +1,11 @@
1
1
  /// <reference path="parser.d.ts" />
2
2
 
3
3
  declare module 'tiddlywiki' {
4
+ export interface IMakeWidgetOptions {
5
+ document: typeof document | IFakeDocument;
6
+ parentWidget?: Widget;
7
+ variables?: Record<string, any>;
8
+ }
4
9
  export class Wiki {
5
10
  /**
6
11
  * 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.
@@ -10,7 +15,7 @@ declare module 'tiddlywiki' {
10
15
  */
11
16
  constructor(options: { enableIndexers: unknown[] });
12
17
  addIndexer(indexer: unknown, name: string): void;
13
- getTiddler: (title: string) => Tiddler | undefined;
18
+ getTiddler: <T extends Tiddler>(title: string) => T | undefined;
14
19
  /**
15
20
  * Get full list of tiddler titles in the wiki
16
21
  */
@@ -22,17 +27,18 @@ declare module 'tiddlywiki' {
22
27
  compileFilter: (filterString: string) => (iterator?: SourceIterator) => string[];
23
28
  /**
24
29
  * Set JSON tiddler, Object in data field will be JSON.stringify and put into the text.
30
+ * 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.
25
31
  */
26
32
  setTiddlerData: (title: string, data?: object, fields?: ITiddlerFields, options?: any) => void;
27
33
  /**
28
34
  * Create or update tiddler.
29
35
  * Update existed tiddler based on the title field.
30
36
  */
31
- addTiddler: (tiddler: Tiddler | ITiddlerFields) => void;
37
+ addTiddler: (tiddler: Tiddler | Partial<ITiddlerFields>) => void;
32
38
  /**
33
- * Call `addTiddler` for each iton of the list
39
+ * Call `addTiddler` for each iton of the list, but should passing `tiddler.fields`, directly passing tiddler object may failed to add in some cases.
34
40
  */
35
- addTiddlers: (tiddler: Array<Tiddler | ITiddlerFields>) => void;
41
+ addTiddlers: (tiddler: Array<Partial<ITiddlerFields>>) => void;
36
42
  /**
37
43
  * Get tiddler's text field, with an optional default text.
38
44
  * If have _is_skinny field, will just return null (this is a rare case, so not put in the return type for now).
@@ -42,6 +48,36 @@ declare module 'tiddlywiki' {
42
48
  */
43
49
  getTiddlerText(title: string, fallbackText: string): string;
44
50
  getTiddlerText(title: string, fallbackText?: string): string | undefined;
51
+ /**
52
+ Get the content of a tiddler as a JavaScript object. How this is done depends on the type of the tiddler:
53
+
54
+ application/json: the tiddler JSON is parsed into an object
55
+ application/x-tiddler-dictionary: the tiddler is parsed as sequence of name:value pairs
56
+
57
+ Other types currently just return undefined or as same as fallbackData.
58
+
59
+ titleOrTiddler: string tiddler title or a tiddler object
60
+ defaultData: default data to be returned if the tiddler is missing or doesn't contain data
61
+
62
+ Alternative, uncached version of getTiddlerDataCached(). The return value can be mutated freely and reused
63
+ */
64
+ getTiddlerData<D extends Record<string, unknown> | undefined>(titleOrTiddler: string, fallbackData?: D): D;
65
+ getTiddlerData<D extends Record<string, unknown> | undefined>(titleOrTiddler: Tiddler, fallbackData?: D): D;
66
+ /**
67
+ Get the content of a tiddler as a JavaScript object. How this is done depends on the type of the tiddler:
68
+
69
+ application/json: the tiddler JSON is parsed into an object
70
+ application/x-tiddler-dictionary: the tiddler is parsed as sequence of name:value pairs
71
+
72
+ Other types currently just return undefined or as same as fallbackData.
73
+
74
+ titleOrTiddler: string tiddler title or a tiddler object
75
+ defaultData: default data to be returned if the tiddler is missing or doesn't contain data
76
+
77
+ 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
78
+ */
79
+ getTiddlerDataCached<D extends Record<string, unknown> | undefined>(titleOrTiddler: string, fallbackData?: D): D;
80
+ getTiddlerDataCached<D extends Record<string, unknown> | undefined>(titleOrTiddler: Tiddler, fallbackData?: D): D;
45
81
  /**
46
82
  * Set tiddler text of any field.
47
83
  *
@@ -54,5 +90,58 @@ declare module 'tiddlywiki' {
54
90
  setText: (title: string, field?: string, index?: string | undefined, value?: string, options?: any) => void;
55
91
  parseTiddler(title: string, options?: IParserOptions): WikiParser;
56
92
  parseText(type: string, text: string, options?: IParserOptions): WikiParser;
93
+ /**
94
+ Parse text from a tiddler and render it into another format
95
+ outputType: content type for the output
96
+ title: title of the tiddler to be rendered
97
+ options: see below
98
+ Options include:
99
+ variables: hashmap of variables to set
100
+ parentWidget: optional parent widget for the root node
101
+ */
102
+ renderTiddler(
103
+ outputType: 'text/html' | 'text/plain-formatted' | 'text/plain',
104
+ title: string,
105
+ options?: { parentWidget?: Widget; variables?: Record<string, any> },
106
+ ): string;
107
+ /**
108
+ Make a widget tree for a parse tree
109
+ @params parser: parser object
110
+ @params options: see below
111
+ Options include:
112
+ document: optional document to use
113
+ variables: hashmap of variables to set
114
+ parentWidget: optional parent widget for the root node
115
+ */
116
+ makeWidget(parser: WikiParser, options?: IMakeWidgetOptions): Widget;
117
+ /**
118
+ Make a widget tree for transclusion
119
+ @params title: target tiddler title
120
+ @params options: as for wiki.makeWidget() plus:
121
+
122
+ - options.field: optional field to transclude (defaults to "text")
123
+ - options.mode: transclusion mode "inline" or "block"
124
+ - options.recursionMarker : optional flag to set a recursion marker, defaults to "yes"
125
+ - options.children: optional array of children for the transclude widget
126
+ - options.importVariables: optional importvariables filter string for macros to be included
127
+ - options.importPageMacros: optional boolean; if true, equivalent to passing "[[$:/core/ui/PageMacros]] [all[shadows+tiddlers]tag[$:/tags/Macro]!has[draft.of]]" to options.importVariables
128
+ */
129
+ makeTranscludeWidget(
130
+ title: string,
131
+ options: {
132
+ /** optional array of children for the transclude widget */
133
+ children?: Widget[];
134
+ /** optional field to transclude (defaults to "text") */
135
+ field?: string;
136
+ /** optional boolean; if true, equivalent to passing "[[$:/core/ui/PageMacros]] [all[shadows+tiddlers]tag[$:/tags/Macro]!has[draft.of]]" to options.importVariables */
137
+ importPageMacros?: boolean;
138
+ /** optional importvariables filter string for macros to be included */
139
+ importVariables?: string;
140
+ /** transclusion mode "inline" or "block" */
141
+ mode?: 'inline' | 'block';
142
+ /** optional flag to set a recursion marker, defaults to "yes" */
143
+ recursionMarker?: 'yes' | 'no';
144
+ } & IMakeWidgetOptions,
145
+ ): Widget;
57
146
  }
58
147
  }
package/src/server.d.ts CHANGED
@@ -13,14 +13,91 @@ declare module 'tiddlywiki' {
13
13
  username: string;
14
14
  }
15
15
 
16
+ export interface IRoute {
17
+ handler: ServerEndpointHandler;
18
+ method: 'GET' | 'POST' | 'DELETE' | 'PUT' | 'OPTIONS' | 'HEAD';
19
+ path: RegExp;
20
+ }
21
+
22
+ export interface IServerOptions {
23
+ routes: IRoute[];
24
+ variables?: { [key: string]: string };
25
+ wiki: Wiki;
26
+ }
27
+
28
+ /**
29
+ A simple HTTP server with regexp-based routes
30
+
31
+ options: variables - optional hashmap of variables to set (a misnomer - they are really constant parameters)
32
+ routes - optional array of routes to use
33
+ wiki - reference to wiki object
34
+ */
35
+ export class Server {
36
+ constructor(options: IServerOptions);
37
+ routes: IRoute[];
38
+ addRoute(route: IRoute): void;
39
+ /**
40
+ * ```json
41
+ * {
42
+ port: "8080",
43
+ host: "127.0.0.1",
44
+ "required-plugins": "$:/plugins/tiddlywiki/filesystem,$:/plugins/tiddlywiki/tiddlyweb",
45
+ "root-tiddler": "$:/core/save/all",
46
+ "root-render-type": "text/plain",
47
+ "root-serve-type": "text/html",
48
+ "tiddler-render-type": "text/html",
49
+ "tiddler-render-template": "$:/core/templates/server/static.tiddler.html",
50
+ "system-tiddler-render-type": "text/plain",
51
+ "system-tiddler-render-template": "$:/core/templates/wikified-tiddler",
52
+ "debug-level": "none",
53
+ "gzip": "no",
54
+ "use-browser-cache": "no"
55
+ }
56
+ ```
57
+ */
58
+ defaultVariables: {
59
+ 'debug-level': string;
60
+ gzip: string;
61
+ host: string;
62
+ port: string;
63
+ 'required-plugins': string;
64
+ 'root-render-type': string;
65
+ 'root-serve-type': string;
66
+ 'root-tiddler': string;
67
+ 'system-tiddler-render-template': string;
68
+ 'system-tiddler-render-type': string;
69
+ 'tiddler-render-template': string;
70
+ 'tiddler-render-type': string;
71
+ 'use-browser-cache': string;
72
+ };
73
+
74
+ variables: Record<string, any>;
75
+ get(variableName: string): any;
76
+ requestHandler: ServerEndpointHandler;
77
+ /**
78
+ Listen for requests
79
+ port: optional port number (falls back to value of "port" variable)
80
+ host: optional host address (falls back to value of "host" variable)
81
+ prefix: optional prefix (falls back to value of "path-prefix" variable)
82
+ */
83
+ listen(port?: string, host?: string, prefix?: string): void;
84
+ /**
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
+ */
87
+ isAuthorized(authorizationType: 'readers' | 'writers', username?: string | undefined): boolean;
88
+ close(): void;
89
+ }
90
+
16
91
  export interface ServerEndpointContext {
92
+ authenticatedUsername: string | undefined;
17
93
  data: string;
94
+ server: Server;
18
95
  wiki: Wiki;
19
96
  }
20
97
  /**
21
98
  * @link https://talk.tiddlywiki.org/t/what-is-the-state-in-server-route-handler/2877
22
99
  */
23
- export type ServerEndpointHandler<T = Record<string, any>> = (
100
+ export type ServerEndpointHandler<T = Record<string, unknown>> = (
24
101
  request: Http.ClientRequest,
25
102
  response: Http.ServerResponse,
26
103
  context: ServerEndpointContext & T,
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
  /**
@@ -80,6 +94,8 @@ declare module 'tiddlywiki' {
80
94
  evalSandboxed(code: string, context: IModuleSandbox, filename: string): unknown;
81
95
  /** Extend an object with the properties from a list of source objects */
82
96
  extend(object: object, ...sourceObjectList: object[]): object;
97
+ /** the function behind `<<now "format">> */
98
+ formatDateString(date: Date, format: string): string;
83
99
  /** Given an extension, always access the $tw.config.fileExtensionInfo using a lowercase extension only. */
84
100
  getFileExtensionInfo(extension: string): IFileExtensionInfo | null;
85
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) */
@@ -152,4 +168,16 @@ declare module 'tiddlywiki' {
152
168
  /** Stringify an array of tiddler titles into a list string */
153
169
  stringifyList(value: string[]): string;
154
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
+ }
155
183
  }
package/src/widget.d.ts CHANGED
@@ -8,6 +8,29 @@ declare module 'tiddlywiki' {
8
8
  modified: boolean;
9
9
  }
10
10
 
11
+ export interface IWidgetEvent {
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
20
+ *
21
+ * For example, `<$action-sendmessage $message="tw-mobile-sync-set-active-server-and-sync" title={{!!title}} />` will produce `paramObject: { title: "xxxx" }`
22
+ */
23
+ paramObject?: {
24
+ [othersParamKeys: string]: unknown;
25
+ };
26
+ /** the first parameter of addEventListener
27
+ *
28
+ * For example, the `'open-command-palette'` in `$tw.rootWidget.addEventListener('open-command-palette', (e: IWidgetEvent) => this.openPalette(e));`
29
+ */
30
+ type: string;
31
+ widget: Widget;
32
+ }
33
+
11
34
  // eslint-disable-next-line @typescript-eslint/no-extraneous-class
12
35
  class variablesConstructor {}
13
36
 
@@ -15,26 +38,37 @@ declare module 'tiddlywiki' {
15
38
  * @link https://tiddlywiki.com/dev/#Widgets
16
39
  */
17
40
  export class Widget {
18
- constructor(parseTreeNode: unknown, options: unknown);
19
- initialize: (parseTreeNode: unknown, options: unknown) => void;
20
- parseTreeNode: unknown;
21
- wiki: unknown;
41
+ constructor(parseTreeNode: IParseTreeNode, options?: unknown);
42
+ initialize: (parseTreeNode: IParseTreeNode, options?: unknown) => void;
43
+ parseTreeNode: IParseTreeNode;
44
+ wiki: ITiddlyWiki;
22
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;
23
57
  variablesConstructor: variablesConstructor;
24
58
  variables: unknown;
25
59
  domNodes: Node[];
26
60
  /**
27
- Add an event listener
28
- */
29
- addEventListener(type: string, handler: (arguments_: unknown[]) => void): void;
61
+ Add an event listener
62
+ */
63
+ addEventListener(type: string, handler: (event: IWidgetEvent) => void | Promise<void>): void;
30
64
  /**
31
- Dispatch an event to a widget. If the widget doesn't handle the event then it is also dispatched to the parent widget
32
- */
33
- dispatchEvent(type: string): void;
65
+ Dispatch an event to a widget. If the widget doesn't handle the event then it is also dispatched to the parent widget
66
+ */
67
+ dispatchEvent(typeOrEvent: string | Omit<IWidgetEvent, 'widget'>): void;
34
68
  /**
35
- Add a list of event listeners from an array [{type:,handler:},...]
36
- */
37
- addEventListeners(listeners: Array<{ handler: (arguments_: unknown[]) => void; type: string }>): void;
69
+ Add a list of event listeners from an array [{type:,handler:},...]
70
+ */
71
+ addEventListeners(listeners: Array<{ handler: (event: IWidgetEvent) => void | Promise<void>; type: string }>): void;
38
72
 
39
73
  parentDomNode: Node;
40
74
  execute: () => void;
@@ -42,7 +76,7 @@ declare module 'tiddlywiki' {
42
76
  /**
43
77
  * Lifecycle method: Render this widget into the DOM
44
78
  */
45
- render(parent: Node, nextSibling: Node): void;
79
+ render(parent: Node, nextSibling: Node | null): void;
46
80
  /**
47
81
  * Selectively refreshes the widget if needed. Returns true if the widget or any of its children needed re-rendering.
48
82
  * You can do some cleanup or buildup before return true.
@@ -50,6 +84,15 @@ declare module 'tiddlywiki' {
50
84
  * @link https://tiddlywiki.com/dev/#Selective%20Update
51
85
  */
52
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;
53
96
  computeAttributes(): void;
54
97
  /**
55
98
  * Get parameters that user set in the widget
@@ -74,4 +117,25 @@ declare module 'tiddlywiki' {
74
117
  */
75
118
  getVariable(name: string, options?: object): string;
76
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
+ }
77
141
  }