-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
config.ts
304 lines (267 loc) · 9.41 KB
/
config.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/**
* @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
*/
/**
* @module utils/config
*/
import { isPlainObject, isElement, cloneDeepWith } from 'lodash-es';
/**
* Handles a configuration dictionary.
*
* @typeParam Cfg A type of the configuration dictionary.
*/
export default class Config<Cfg> {
/**
* Store for the whole configuration.
*/
private readonly _config: Record<string, any>;
/**
* Creates an instance of the {@link ~Config} class.
*
* @param configurations The initial configurations to be set. Usually, provided by the user.
* @param defaultConfigurations The default configurations. Usually, provided by the system.
*/
constructor( configurations?: Partial<Cfg>, defaultConfigurations?: Partial<Cfg> ) {
this._config = {};
// Set default configuration.
if ( defaultConfigurations ) {
// Clone the configuration to make sure that the properties will not be shared
// between editors and make the watchdog feature work correctly.
this.define( cloneConfig( defaultConfigurations ) );
}
// Set initial configuration.
if ( configurations ) {
this._setObjectToTarget( this._config, configurations );
}
}
/**
* Set configuration values.
*
* It also accepts setting a "deep configuration" by using dots in the name. For example, `'resize.width'` sets
* the value for the `width` configuration in the `resize` subset.
*
* ```ts
* config.set( 'resize.width', 500 );
* ```
*
* It accepts both a name/value pair or an object, which properties and values will be used to set
* configurations. See {@link #set:CONFIG_OBJECT}.
*
* @label KEY_VALUE
* @param name The configuration name. Configuration names are case-sensitive.
* @param value The configuration value.
*/
public set<K extends string>( name: K, value: GetSubConfig<Cfg, K> ): void;
/**
* Set configuration values.
*
* It accepts an object, which properties and values will be used to set configurations.
*
* ```ts
* config.set( {
* width: 500
* toolbar: {
* collapsed: true
* }
* } );
*
* // Equivalent to:
* config.set( 'width', 500 );
* config.set( 'toolbar.collapsed', true );
* ```
*
* Passing an object as the value will amend the configuration, not replace it.
*
* ```ts
* config.set( 'toolbar', {
* collapsed: true,
* } );
*
* config.set( 'toolbar', {
* color: 'red',
* } );
*
* config.get( 'toolbar.collapsed' ); // true
* config.get( 'toolbar.color' ); // 'red'
* ```
*
* It accepts both a name/value pair or an object, which properties and values will be used to set
* configurations. See {@link #set:KEY_VALUE}.
*
* @label CONFIG_OBJECT
* @param config The configuration object from which take properties as
* configuration entries. Configuration names are case-sensitive.
*/
public set( config: Partial<Cfg> ): void;
public set( name: string | Record<string, any>, value?: any ): void {
this._setToTarget( this._config, name, value );
}
/**
* Does exactly the same as {@link #set:KEY_VALUE} with one exception – passed configuration extends
* existing one, but does not overwrite already defined values.
*
* This method is supposed to be called by plugin developers to setup plugin's configurations. It would be
* rarely used for other needs.
*
* @label KEY_VALUE
* @param name The configuration name. Configuration names are case-sensitive.
* @param value The configuration value.
*/
public define<K extends string>( name: K, value: GetSubConfig<Cfg, K> ): void;
/**
* Does exactly the same as {@link #set:CONFIG_OBJECT} with one exception – passed configuration extends
* existing one, but does not overwrite already defined values.
*
* This method is supposed to be called by plugin developers to setup plugin's configurations. It would be
* rarely used for other needs.
*
* @label CONFIG_OBJECT
* @param config The configuration object from which take properties as
* configuration entries. Configuration names are case-sensitive.
*/
public define( config: Partial<Cfg> ): void;
public define( name: string | Record<string, any>, value?: any ): void {
const isDefine = true;
this._setToTarget( this._config, name, value, isDefine );
}
/**
* Gets the value for a configuration entry.
*
* ```ts
* config.get( 'name' );
* ```
*
* Deep configurations can be retrieved by separating each part with a dot.
*
* ```ts
* config.get( 'toolbar.collapsed' );
* ```
*
* @param name The configuration name. Configuration names are case-sensitive.
* @returns The configuration value or `undefined` if the configuration entry was not found.
*/
public get<K extends string>( name: K ): GetSubConfig<Cfg, K> | undefined {
return this._getFromSource( this._config, name );
}
/**
* Iterates over all top level configuration names.
*/
public* names(): Iterable<string> {
for ( const name of Object.keys( this._config ) ) {
yield name;
}
}
/**
* Saves passed configuration to the specified target (nested object).
*
* @param target Nested config object.
* @param name The configuration name or an object from which take properties as
* configuration entries. Configuration names are case-sensitive.
* @param value The configuration value. Used if a name is passed.
* @param isDefine Define if passed configuration should overwrite existing one.
*/
private _setToTarget( target: any, name: any, value: any, isDefine: boolean = false ): void {
// In case of an object, iterate through it and call `_setToTarget` again for each property.
if ( isPlainObject( name ) ) {
this._setObjectToTarget( target, name, isDefine );
return;
}
// The configuration name should be split into parts if it has dots. E.g. `resize.width` -> [`resize`, `width`].
const parts = name.split( '.' );
// Take the name of the configuration out of the parts. E.g. `resize.width` -> `width`.
name = parts.pop();
// Iterate over parts to check if currently stored configuration has proper structure.
for ( const part of parts ) {
// If there is no object for specified part then create one.
if ( !isPlainObject( target[ part ] ) ) {
target[ part ] = {};
}
// Nested object becomes a target.
target = target[ part ];
}
// In case of value is an object.
if ( isPlainObject( value ) ) {
// We take care of proper config structure.
if ( !isPlainObject( target[ name ] ) ) {
target[ name ] = {};
}
target = target[ name ];
// And iterate through this object calling `_setToTarget` again for each property.
this._setObjectToTarget( target, value, isDefine );
return;
}
// Do nothing if we are defining configuration for non empty name.
if ( isDefine && typeof target[ name ] != 'undefined' ) {
return;
}
target[ name ] = value;
}
/**
* Get specified configuration from specified source (nested object).
*
* @param source level of nested object.
* @param name The configuration name. Configuration names are case-sensitive.
* @returns The configuration value or `undefined` if the configuration entry was not found.
*/
private _getFromSource( source: any, name: string ): any {
// The configuration name should be split into parts if it has dots. E.g. `resize.width` -> [`resize`, `width`].
const parts = name.split( '.' );
// Take the name of the configuration out of the parts. E.g. `resize.width` -> `width`.
name = parts.pop()!;
// Iterate over parts to check if currently stored configuration has proper structure.
for ( const part of parts ) {
if ( !isPlainObject( source[ part ] ) ) {
source = null;
break;
}
// Nested object becomes a source.
source = source[ part ];
}
// Always returns undefined for non existing configuration.
return source ? cloneConfig( source[ name ] ) : undefined;
}
/**
* Iterates through passed object and calls {@link #_setToTarget} method with object key and value for each property.
*
* @param target Nested config object.
* @param configuration Configuration data set
* @param isDefine Defines if passed configuration is default configuration or not.
*/
private _setObjectToTarget( target: any, configuration: any, isDefine?: boolean ): void {
Object.keys( configuration ).forEach( key => {
this._setToTarget( target, key, configuration[ key ], isDefine );
} );
}
}
/**
* Clones configuration object or value.
*/
function cloneConfig<T>( source: T ): T {
return cloneDeepWith( source, leaveItemReferences );
}
/**
* A customized function for cloneDeepWith.
* In case if it's a DOM Element it will leave references to DOM Elements instead of cloning them.
* If it's a function it will leave reference to actuall function.
*/
function leaveItemReferences( value: unknown ): unknown {
return isElement( value ) || typeof value === 'function' ? value : undefined;
}
/**
* An utility type excluding primitive values and arrays from the union.
*/
export type OnlyObject<T> = Exclude<T, undefined | null | string | number | boolean | Array<any>>;
/**
* An utility type extracting configuration value from the given name.
*
* @typeParam T The type of a configuration dictionary.
* @typeParam K The literal type of configuration name (dot-separated path).
*/
export type GetSubConfig<T, K> = K extends keyof T ?
T[ K ] :
K extends `${ infer K1 }.${ infer K2 }` ?
K1 extends keyof T ?
GetSubConfig<OnlyObject<T[ K1 ]>, K2> :
unknown :
unknown;