tw5-typed 0.0.8 → 0.0.9
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 +3 -3
- package/src/filter-operator.d.ts +110 -0
- package/src/tw.d.ts +2 -1
- package/src/widget.d.ts +22 -8
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.0.
|
|
5
|
+
"version": "0.0.9",
|
|
6
6
|
"url": "https://github.com/tiddly-gittly/tw5-typed",
|
|
7
7
|
"homepage": "https://github.com/tiddly-gittly/tw5-typed",
|
|
8
8
|
"bugs": {
|
|
@@ -20,8 +20,8 @@
|
|
|
20
20
|
"prepublishOnly": "npx tsc --noEmit"
|
|
21
21
|
},
|
|
22
22
|
"devDependencies": {
|
|
23
|
-
"@typescript-eslint/eslint-plugin": "5.
|
|
24
|
-
"@typescript-eslint/parser": "5.
|
|
23
|
+
"@typescript-eslint/eslint-plugin": "5.11.0",
|
|
24
|
+
"@typescript-eslint/parser": "5.11.0",
|
|
25
25
|
"eslint": "8.8.0",
|
|
26
26
|
"eslint-config-prettier": "8.3.0",
|
|
27
27
|
"eslint-config-standard": "17.0.0-1",
|
package/src/filter-operator.d.ts
CHANGED
|
@@ -1,4 +1,114 @@
|
|
|
1
1
|
declare module 'tiddlywiki' {
|
|
2
|
+
/**
|
|
3
|
+
* # Overview
|
|
4
|
+
|
|
5
|
+
Filter operators are modules (tiddlers of type `application/javascript`) with their `module-type` field set to `filteroperator`, exporting one or more functions implementing a filter.
|
|
6
|
+
|
|
7
|
+
Each function will be called with three arguments:
|
|
8
|
+
|
|
9
|
+
* A [tiddler iterator](#Tiddler%20Iterators) representing the results of the previous filter step (or all tiddlers, if this filter appears first in an expression), conventionally named `source`.
|
|
10
|
+
* An object, conventionally called `operator`, representing the arguments for this filter step, with the following keys:
|
|
11
|
+
|
|
12
|
+
* *operator*: the name of the filter operator specified in the [WikiText](#WikiText);
|
|
13
|
+
* *operand*: the operand for the filter step (as a string; if the filter specified it in angle brackets or braces, the text reference or variable name will have already been resolved);
|
|
14
|
+
* *prefix*: (optional) a string containing a single exclamation mark if the filter operator is to be negated;
|
|
15
|
+
* *suffix*: (optional) a string containing an additional filter argument (typically a tiddler field name) following the filter name (separated by a colon in the filter syntax);
|
|
16
|
+
* *regexp*: (optional, deprecated) used instead of *operand* if the filter operand is a regexp.
|
|
17
|
+
* An object, conventionally called `options`, with the following keys:
|
|
18
|
+
|
|
19
|
+
* *wiki*: The `$tw.Wiki` object;
|
|
20
|
+
* *widget*: (optional) a widget node.
|
|
21
|
+
|
|
22
|
+
The function should return either a new [tiddler iterator](#Tiddler%20Iterators), or else an array of tiddler titles (as strings). The underlying filter mechanism will convert back and forth between iterators and arrays as needed.
|
|
23
|
+
|
|
24
|
+
# References
|
|
25
|
+
|
|
26
|
+
There are several filter operators built into the core which can serve as a jumping off point for your own filter operators:
|
|
27
|
+
|
|
28
|
+
[https://github.com/Jermolene/TiddlyWiki5/tree/master/core/modules/filters](https://github.com/Jermolene/TiddlyWiki5/tree/master/core/modules/filters)
|
|
29
|
+
|
|
30
|
+
# Example
|
|
31
|
+
|
|
32
|
+
Suppose we want to make a filter operator that returns every other tiddler from the input list. A typical invocation might look like `[tags[interesting]everyother[]]`.
|
|
33
|
+
|
|
34
|
+
We make a new tiddler, set its `type` and `module-type` appropriately, and begin writing the code:
|
|
35
|
+
|
|
36
|
+
```js
|
|
37
|
+
(function(){
|
|
38
|
+
"use strict";
|
|
39
|
+
exports.everyother = function(source, operator, options) {
|
|
40
|
+
// TODO
|
|
41
|
+
}
|
|
42
|
+
})();
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
For the example filter syntax, our function will be called with
|
|
46
|
+
|
|
47
|
+
* source: an iterator over all the tiddlers tagged as `interesting`
|
|
48
|
+
* operator: an object `{operator: "everyother", operand: ""}`
|
|
49
|
+
* options: an object with the current Wiki object and a widget object, neither of which we need
|
|
50
|
+
|
|
51
|
+
As is usually the case, we don't care about `operator.operator` here (since that information has already been used to look up our function); we also don't care about `operator.operand`, since there is no meaningful operand for this operation.
|
|
52
|
+
|
|
53
|
+
We could implement the operator by iterating over the input tiddlers and explicitly building a result array of titles:
|
|
54
|
+
|
|
55
|
+
```js
|
|
56
|
+
(function(){
|
|
57
|
+
"use strict";
|
|
58
|
+
exports.everyother = function(source, operator, options) {
|
|
59
|
+
var result = [];
|
|
60
|
+
var include = true;
|
|
61
|
+
source(function(tiddler, title) {
|
|
62
|
+
if (include) { result.push(title); }
|
|
63
|
+
include = !include;
|
|
64
|
+
});
|
|
65
|
+
return result;
|
|
66
|
+
}
|
|
67
|
+
})();
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
That is, we supply a callback to `source` that negates `include` each time through (in order to grab every other result) and pushes the `title` of every other tiddler onto the result.
|
|
71
|
+
|
|
72
|
+
Alternatively, we can return our own iterator, by returning a function that accepts a similar callback and only calls it on every other tiddler:
|
|
73
|
+
|
|
74
|
+
```js
|
|
75
|
+
(function(){
|
|
76
|
+
"use strict";
|
|
77
|
+
exports.everyother = function(source, operator, options) {
|
|
78
|
+
return function(callback) {
|
|
79
|
+
var include = true;
|
|
80
|
+
source(function(tiddler, title) {
|
|
81
|
+
if (include) { callback(tiddler, title); }
|
|
82
|
+
include = !include;
|
|
83
|
+
});
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
})();
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
Either way, we could interpret the `!` flag on the filter, if present, to mean that we want the *other* half of the tiddlers, by using it to set the initial value of `include`: `var include = operator.prefix !== "!";`
|
|
90
|
+
|
|
91
|
+
# Filter Behaviour
|
|
92
|
+
|
|
93
|
+
As with [JavaScript Macros](#JavaScript%20Macros), filter operators should not make modifications to tiddlers, but only return a list of tiddlers or a tiddler iterator.
|
|
94
|
+
|
|
95
|
+
*/
|
|
96
|
+
export type IFilterOperator = (source: (iter: SourceIterator) => void, operator: IFilterOperatorParamOperator) => string[] | ((iter: SourceIterator) => void);
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* A [tiddler iterator](#Tiddler%20Iterators) representing the results of the previous filter step (or all tiddlers, if this filter appears first in an expression), conventionally named `source`.
|
|
100
|
+
*
|
|
101
|
+
* For Example, with an iterator over all the tiddlers tagged as `interesting`, use it like this:
|
|
102
|
+
*
|
|
103
|
+
* ```js
|
|
104
|
+
* var result = [];
|
|
105
|
+
var include = true;
|
|
106
|
+
source(function(tiddler, title) {
|
|
107
|
+
if (include) { result.push(title); }
|
|
108
|
+
include = !include;
|
|
109
|
+
});
|
|
110
|
+
```
|
|
111
|
+
*/
|
|
2
112
|
export type SourceIterator = (tiddler: Tiddler, title: string) => void;
|
|
3
113
|
export interface ISearchOptions {
|
|
4
114
|
/** If true, forces all but regexp searches to be anchored to the start of text */
|
package/src/tw.d.ts
CHANGED
|
@@ -68,9 +68,10 @@ declare module 'tiddlywiki' {
|
|
|
68
68
|
version: string;
|
|
69
69
|
|
|
70
70
|
wiki: Wiki;
|
|
71
|
+
rootWidget: Widget;
|
|
71
72
|
}
|
|
72
73
|
|
|
73
|
-
export
|
|
74
|
+
export function TiddlyWiki($tw: object): ITiddlyWiki;
|
|
74
75
|
|
|
75
76
|
global {
|
|
76
77
|
const $tw: ITiddlyWiki;
|
package/src/widget.d.ts
CHANGED
|
@@ -1,12 +1,26 @@
|
|
|
1
1
|
// eslint-disable-next-line @typescript-eslint/no-extraneous-class
|
|
2
2
|
declare class variablesConstructor {}
|
|
3
3
|
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
4
|
+
declare module 'tiddlywiki' {
|
|
5
|
+
export class Widget {
|
|
6
|
+
constructor(parseTreeNode: unknown, options: unknown);
|
|
7
|
+
initialize: (parseTreeNode: unknown, options: unknown) => void;
|
|
8
|
+
parseTreeNode: unknown;
|
|
9
|
+
wiki: unknown;
|
|
10
|
+
parentWidget?: Widget;
|
|
11
|
+
variablesConstructor: variablesConstructor;
|
|
12
|
+
variables: unknown;
|
|
13
|
+
/**
|
|
14
|
+
Add an event listener
|
|
15
|
+
*/
|
|
16
|
+
addEventListener: (type: string, handler: (args: any[]) => void) => void;
|
|
17
|
+
/**
|
|
18
|
+
Dispatch an event to a widget. If the widget doesn't handle the event then it is also dispatched to the parent widget
|
|
19
|
+
*/
|
|
20
|
+
dispatchEvent: (type: string) => void;
|
|
21
|
+
/**
|
|
22
|
+
Add a list of event listeners from an array [{type:,handler:},...]
|
|
23
|
+
*/
|
|
24
|
+
addEventListeners: (listeners: { type: string; handler: (args: any[]) => void }[]) => void;
|
|
25
|
+
}
|
|
12
26
|
}
|