-
-
Notifications
You must be signed in to change notification settings - Fork 82
/
plugin.ts
210 lines (192 loc) · 5.57 KB
/
plugin.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import {InputBindingApi} from '../blade/binding/api/input-binding.js';
import {InputBindingController} from '../blade/binding/controller/input-binding.js';
import {createBlade} from '../blade/common/model/blade.js';
import {BindingReader, BindingWriter} from '../common/binding/binding.js';
import {ReadWriteBinding} from '../common/binding/read-write.js';
import {BindingTarget} from '../common/binding/target.js';
import {InputBindingValue} from '../common/binding/value/input-binding.js';
import {Constraint} from '../common/constraint/constraint.js';
import {ValueController} from '../common/controller/value.js';
import {LabelPropsObject} from '../common/label/view/label.js';
import {parseRecord} from '../common/micro-parsers.js';
import {Value} from '../common/model/value.js';
import {ValueMap} from '../common/model/value-map.js';
import {createValue} from '../common/model/values.js';
import {ViewProps} from '../common/model/view-props.js';
import {BaseInputParams} from '../common/params.js';
import {isEmpty} from '../misc/type-util.js';
import {BasePlugin} from '../plugin/plugin.js';
interface Acceptance<T, P extends BaseInputParams> {
initialValue: T;
params: P;
}
interface BindingArguments<Ex, P extends BaseInputParams> {
initialValue: Ex;
params: P;
target: BindingTarget;
}
interface ControllerArguments<In, Ex, P extends BaseInputParams> {
constraint: Constraint<In> | undefined;
document: Document;
initialValue: Ex;
params: P;
value: Value<In>;
viewProps: ViewProps;
}
interface ApiArguments {
controller: InputBindingController<unknown>;
}
/**
* An input binding plugin interface.
* @template In The type of the internal value.
* @template Ex The type of the external value. It will be provided by users.
* @template P The type of the parameters.
*/
export interface InputBindingPlugin<In, Ex, P extends BaseInputParams>
extends BasePlugin {
type: 'input';
/**
* Decides whether the plugin accepts the provided value and the parameters.
*/
accept: {
/**
* @param exValue The value input by users.
* @param params The additional parameters specified by users.
* @return A typed value if the plugin accepts the input, or null if the plugin sees them off and pass them to the next plugin.
*/
(exValue: unknown, params: Record<string, unknown>): Acceptance<
Ex,
P
> | null;
};
/**
* Configurations of the binding.
*/
binding: {
/**
* Creates a value reader from the user input.
*/
reader: {
/**
* @param args The arguments for binding.
* @return A value reader.
*/
(args: BindingArguments<Ex, P>): BindingReader<In>;
};
/**
* Creates a value constraint from the user input.
*/
constraint?: {
/**
* @param args The arguments for binding.
* @return A value constraint.
*/
(args: BindingArguments<Ex, P>): Constraint<In>;
};
/**
* Compares the equality of two internal values.
* Use `===` for primitive values, or a custom comparator for complex objects.
*/
equals?: {
/**
* @param v1 The value.
* @param v2 The another value.
* @return true if equal, false otherwise.
*/
(v1: In, v2: In): boolean;
};
/**
* Creates a value writer from the user input.
*/
writer: {
/**
* @param args The arguments for binding.
* @return A value writer.
*/
(args: BindingArguments<Ex, P>): BindingWriter<In>;
};
};
/**
* Creates a custom controller for the plugin.
*/
controller: {
/**
* @param args The arguments for creating a controller.
* @return A custom controller that contains a custom view.
*/
(args: ControllerArguments<In, Ex, P>): ValueController<In>;
};
/**
* Creates a custom API for the plugin if available.
*/
api?: {
/**
* @param args The arguments for creating an API.
* @return A custom API for the specified controller, or null if there is no suitable API.
*/
(args: ApiArguments): InputBindingApi<In, Ex> | null;
};
}
export function createInputBindingController<In, Ex, P extends BaseInputParams>(
plugin: InputBindingPlugin<In, Ex, P>,
args: {
document: Document;
params: Record<string, unknown>;
target: BindingTarget;
},
): InputBindingController<In> | null {
const result = plugin.accept(args.target.read(), args.params);
if (isEmpty(result)) {
return null;
}
const valueArgs = {
target: args.target,
initialValue: result.initialValue,
params: result.params,
};
const params = parseRecord(args.params, (p) => ({
disabled: p.optional.boolean,
hidden: p.optional.boolean,
label: p.optional.string,
tag: p.optional.string,
}));
// Binding and value
const reader = plugin.binding.reader(valueArgs);
const constraint = plugin.binding.constraint
? plugin.binding.constraint(valueArgs)
: undefined;
const binding = new ReadWriteBinding({
reader: reader,
target: args.target,
writer: plugin.binding.writer(valueArgs),
});
const value = new InputBindingValue(
createValue(reader(result.initialValue), {
constraint: constraint,
equals: plugin.binding.equals,
}),
binding,
);
// Value controller
const controller = plugin.controller({
constraint: constraint,
document: args.document,
initialValue: result.initialValue,
params: result.params,
value: value,
viewProps: ViewProps.create({
disabled: params?.disabled,
hidden: params?.hidden,
}),
});
// Input binding controller
return new InputBindingController(args.document, {
blade: createBlade(),
props: ValueMap.fromObject<LabelPropsObject>({
label: 'label' in args.params ? params?.label ?? null : args.target.key,
}),
tag: params?.tag,
value: value,
valueController: controller,
});
}