@punica/form 1.1.12 → 1.2.0
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/dist/controller/index.d.ts +2 -1
- package/dist/controller/index.js +74 -106
- package/dist/model/item/index.d.ts +1 -1
- package/dist/reader/decorators/base/index.js +5 -10
- package/dist/reader/decorators/readers/ItemDataReader.js +1 -1
- package/dist/reader/decorators/readers/ItemInitializerReader.js +3 -3
- package/dist/reader/reader/Reader.d.ts +1 -6
- package/dist/reader/reader/Reader.js +5 -16
- package/dist/utils/deepCopy.d.ts +6 -16
- package/dist/utils/deepCopy.js +32 -69
- package/dist/utils/devtools.d.ts +4 -0
- package/dist/utils/devtools.js +13 -21
- package/package.json +8 -13
|
@@ -55,7 +55,8 @@ export declare class FormController<E, F extends FormItem<E>> extends BaseListen
|
|
|
55
55
|
*/
|
|
56
56
|
private runStarters;
|
|
57
57
|
/**
|
|
58
|
-
*
|
|
58
|
+
* Keeps a deep copy of the form items for the initial state.
|
|
59
|
+
* Services, readers and starters are shared, not copied.
|
|
59
60
|
*/
|
|
60
61
|
private deepCopyForm;
|
|
61
62
|
/**
|
package/dist/controller/index.js
CHANGED
|
@@ -54,7 +54,7 @@ class FormController extends common_1.BaseListener {
|
|
|
54
54
|
devTools.sendUpdateItem(data);
|
|
55
55
|
break;
|
|
56
56
|
case 'REGISTER_ITEMS':
|
|
57
|
-
devTools.sendRegisterItems(
|
|
57
|
+
devTools.sendRegisterItems(data);
|
|
58
58
|
break;
|
|
59
59
|
}
|
|
60
60
|
}
|
|
@@ -67,81 +67,62 @@ class FormController extends common_1.BaseListener {
|
|
|
67
67
|
if (!devTools.isEnabled()) {
|
|
68
68
|
return;
|
|
69
69
|
}
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
if (getValuesService && typeof getValuesService.run === 'function') {
|
|
74
|
-
getValuesService
|
|
75
|
-
.run()
|
|
76
|
-
.then((entity) => {
|
|
77
|
-
devTools.sendEntity(entity);
|
|
78
|
-
})
|
|
79
|
-
.catch(() => {
|
|
80
|
-
// Silently fail
|
|
81
|
-
});
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
catch (_a) {
|
|
85
|
-
// Silently fail
|
|
86
|
-
}
|
|
70
|
+
const getValues = this.getServices('getValues');
|
|
71
|
+
// Debugging must never break the form
|
|
72
|
+
getValues === null || getValues === void 0 ? void 0 : getValues.run().then((entity) => devTools.sendEntity(entity)).catch(() => { });
|
|
87
73
|
};
|
|
88
74
|
/**
|
|
89
75
|
* Create a command service for the form.
|
|
90
76
|
* @returns A CommandService for the form
|
|
91
77
|
*/
|
|
92
78
|
this.createCommandService = () => {
|
|
93
|
-
const self = this;
|
|
94
79
|
const getItem = (property) => {
|
|
95
|
-
const
|
|
96
|
-
|
|
97
|
-
if (!Number.isInteger(itemIndex))
|
|
98
|
-
return null;
|
|
99
|
-
return items[itemIndex];
|
|
80
|
+
const index = __classPrivateFieldGet(this, _FormController_itemsMap, "f")[property];
|
|
81
|
+
return Number.isInteger(index) ? __classPrivateFieldGet(this, _FormController_form, "f").items[index] : null;
|
|
100
82
|
};
|
|
101
|
-
const writeItems = async (
|
|
102
|
-
for (const item of
|
|
103
|
-
const
|
|
104
|
-
|
|
105
|
-
|
|
83
|
+
const writeItems = async (items) => {
|
|
84
|
+
for (const item of items) {
|
|
85
|
+
const index = __classPrivateFieldGet(this, _FormController_itemsMap, "f")[item.property];
|
|
86
|
+
if (Number.isInteger(index)) {
|
|
87
|
+
__classPrivateFieldGet(this, _FormController_form, "f").items[index] = item;
|
|
88
|
+
}
|
|
106
89
|
}
|
|
107
90
|
};
|
|
108
|
-
const fireEvent = (ev, data) => {
|
|
109
|
-
self.fireEvent(ev, data);
|
|
110
|
-
};
|
|
111
91
|
const createCommandItem = async (item) => {
|
|
112
|
-
var _a, _b;
|
|
92
|
+
var _a, _b, _c;
|
|
113
93
|
let itemCustomCommand = {};
|
|
114
|
-
for
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
itemCustomCommand = { ...itemCustomCommand, ...command };
|
|
119
|
-
}
|
|
94
|
+
for (const service of (_a = __classPrivateFieldGet(this, _FormController_form, "f").services) !== null && _a !== void 0 ? _a : []) {
|
|
95
|
+
// Called on the service so that its own state stays reachable
|
|
96
|
+
const command = (_c = (_b = service).addCustomFeaturesForCommandItem) === null || _c === void 0 ? void 0 : _c.call(_b);
|
|
97
|
+
itemCustomCommand = { ...itemCustomCommand, ...command };
|
|
120
98
|
}
|
|
121
99
|
return {
|
|
122
100
|
formItem: item,
|
|
123
|
-
initialEntity: __classPrivateFieldGet(
|
|
101
|
+
initialEntity: __classPrivateFieldGet(this, _FormController_initialEntity, "f"),
|
|
124
102
|
getItem,
|
|
125
103
|
writeItems,
|
|
126
104
|
...itemCustomCommand
|
|
127
105
|
};
|
|
128
106
|
};
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
initialEntity: __classPrivateFieldGet(self, _FormController_initialEntity, "f"),
|
|
132
|
-
form: __classPrivateFieldGet(self, _FormController_form, "f"),
|
|
133
|
-
itemsMap: __classPrivateFieldGet(self, _FormController_itemsMap, "f"),
|
|
107
|
+
const command = {
|
|
108
|
+
initialEntity: __classPrivateFieldGet(this, _FormController_initialEntity, "f"),
|
|
134
109
|
getItem,
|
|
135
110
|
writeItems,
|
|
136
|
-
fireEvent,
|
|
111
|
+
fireEvent: this.fireEvent,
|
|
137
112
|
createCommandItem
|
|
138
113
|
};
|
|
114
|
+
// Getters keep services in sync with the current state of the controller
|
|
115
|
+
return Object.defineProperties(command, {
|
|
116
|
+
form: { enumerable: true, get: () => __classPrivateFieldGet(this, _FormController_form, "f") },
|
|
117
|
+
initialForm: { enumerable: true, get: () => __classPrivateFieldGet(this, _FormController_initialForm, "f") },
|
|
118
|
+
itemsMap: { enumerable: true, get: () => __classPrivateFieldGet(this, _FormController_itemsMap, "f") }
|
|
119
|
+
});
|
|
139
120
|
};
|
|
140
121
|
/**
|
|
141
122
|
* Registers form items by triggering the REGISTER_ITEMS event.
|
|
142
123
|
*/
|
|
143
124
|
this.registerFormItems = () => {
|
|
144
|
-
this.fireEvent('REGISTER_ITEMS', __1.FormItemRegister.getInstance().getItemKeys());
|
|
125
|
+
this.fireEvent('REGISTER_ITEMS', Array.from(__1.FormItemRegister.getInstance().getItemKeys()));
|
|
145
126
|
};
|
|
146
127
|
/**
|
|
147
128
|
* Maps form items to their properties.
|
|
@@ -157,14 +138,13 @@ class FormController extends common_1.BaseListener {
|
|
|
157
138
|
* Initializes the services defined in the form data.
|
|
158
139
|
*/
|
|
159
140
|
this.initializeServices = async () => {
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
}
|
|
141
|
+
var _a;
|
|
142
|
+
for (const service of (_a = __classPrivateFieldGet(this, _FormController_form, "f").services) !== null && _a !== void 0 ? _a : []) {
|
|
143
|
+
const command = this.createCommandService();
|
|
144
|
+
const serviceControl = service;
|
|
145
|
+
__classPrivateFieldGet(this, _FormController_serviceMap, "f")[service.name] = service;
|
|
146
|
+
if (serviceControl.initialize) {
|
|
147
|
+
await serviceControl.initialize(command);
|
|
168
148
|
}
|
|
169
149
|
}
|
|
170
150
|
};
|
|
@@ -172,27 +152,26 @@ class FormController extends common_1.BaseListener {
|
|
|
172
152
|
* Executes the readers defined in the form data.
|
|
173
153
|
*/
|
|
174
154
|
this.runReaders = async () => {
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
}
|
|
155
|
+
var _a;
|
|
156
|
+
for (const reader of (_a = __classPrivateFieldGet(this, _FormController_form, "f").readers) !== null && _a !== void 0 ? _a : []) {
|
|
157
|
+
await reader.read(__classPrivateFieldGet(this, _FormController_initialEntity, "f"), __classPrivateFieldGet(this, _FormController_form, "f"), __classPrivateFieldGet(this, _FormController_itemsMap, "f"));
|
|
179
158
|
}
|
|
180
159
|
};
|
|
181
160
|
/**
|
|
182
161
|
* Executes the starters defined in the form data.
|
|
183
162
|
*/
|
|
184
163
|
this.runStarters = async () => {
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
}
|
|
164
|
+
var _a;
|
|
165
|
+
for (const starter of (_a = __classPrivateFieldGet(this, _FormController_form, "f").starters) !== null && _a !== void 0 ? _a : []) {
|
|
166
|
+
__classPrivateFieldSet(this, _FormController_form, await starter.run(__classPrivateFieldGet(this, _FormController_form, "f"), __classPrivateFieldGet(this, _FormController_initialEntity, "f")), "f");
|
|
189
167
|
}
|
|
190
168
|
};
|
|
191
169
|
/**
|
|
192
|
-
*
|
|
170
|
+
* Keeps a deep copy of the form items for the initial state.
|
|
171
|
+
* Services, readers and starters are shared, not copied.
|
|
193
172
|
*/
|
|
194
173
|
this.deepCopyForm = () => {
|
|
195
|
-
__classPrivateFieldSet(this, _FormController_initialForm, (0, __1.deepCopy)(__classPrivateFieldGet(this, _FormController_form, "f")), "f");
|
|
174
|
+
__classPrivateFieldSet(this, _FormController_initialForm, { ...__classPrivateFieldGet(this, _FormController_form, "f"), items: (0, __1.deepCopy)(__classPrivateFieldGet(this, _FormController_form, "f").items) }, "f");
|
|
196
175
|
};
|
|
197
176
|
__classPrivateFieldSet(this, _FormController_serviceMap, {}, "f");
|
|
198
177
|
__classPrivateFieldSet(this, _FormController_itemsMap, {}, "f");
|
|
@@ -201,32 +180,28 @@ class FormController extends common_1.BaseListener {
|
|
|
201
180
|
* Static method to initialize the FormController with form data.
|
|
202
181
|
* @param form The initial form data
|
|
203
182
|
*/
|
|
204
|
-
static fromForm(form) {
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
resolve(instance);
|
|
210
|
-
});
|
|
183
|
+
static async fromForm(form) {
|
|
184
|
+
const instance = new FormController();
|
|
185
|
+
__classPrivateFieldSet(instance, _FormController_form, form, "f");
|
|
186
|
+
__classPrivateFieldSet(instance, _FormController_initialEntity, {}, "f");
|
|
187
|
+
return instance;
|
|
211
188
|
}
|
|
212
189
|
/**
|
|
213
190
|
* Static method to initialize the FormController with an entity.
|
|
214
191
|
* @param entity The entity for the form
|
|
215
192
|
*/
|
|
216
|
-
static fromEntity(entity) {
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
resolve(instance);
|
|
229
|
-
});
|
|
193
|
+
static async fromEntity(entity) {
|
|
194
|
+
const instance = new FormController();
|
|
195
|
+
const initialReader = (0, __1.createInitialReader)();
|
|
196
|
+
__classPrivateFieldSet(instance, _FormController_initialEntity, entity, "f");
|
|
197
|
+
__classPrivateFieldSet(instance, _FormController_form, {
|
|
198
|
+
items: null,
|
|
199
|
+
services: null,
|
|
200
|
+
starters: null,
|
|
201
|
+
readers: null
|
|
202
|
+
}, "f");
|
|
203
|
+
await initialReader.read(__classPrivateFieldGet(instance, _FormController_initialEntity, "f"), __classPrivateFieldGet(instance, _FormController_form, "f"));
|
|
204
|
+
return instance;
|
|
230
205
|
}
|
|
231
206
|
getServices(serviceName, ...additionalServiceNames) {
|
|
232
207
|
if (additionalServiceNames.length > 0) {
|
|
@@ -243,27 +218,20 @@ class FormController extends common_1.BaseListener {
|
|
|
243
218
|
* @returns A promise that resolves when the form is fully initialized.
|
|
244
219
|
*/
|
|
245
220
|
async start() {
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
this.sendEntityToDevTools();
|
|
259
|
-
}
|
|
260
|
-
// Trigger an update event after the form is fully initialized
|
|
261
|
-
return __classPrivateFieldGet(this, _FormController_form, "f");
|
|
262
|
-
}
|
|
263
|
-
catch (error) {
|
|
264
|
-
console.error('Error starting the form controller:', error);
|
|
265
|
-
throw error;
|
|
221
|
+
this.registerFormItems();
|
|
222
|
+
this.mapFormItems();
|
|
223
|
+
await this.runReaders();
|
|
224
|
+
await this.runStarters();
|
|
225
|
+
// Services are initialized against the final form produced by starters
|
|
226
|
+
this.mapFormItems();
|
|
227
|
+
await this.initializeServices();
|
|
228
|
+
this.deepCopyForm();
|
|
229
|
+
const devTools = (0, __1.getDevToolsBridge)();
|
|
230
|
+
if (devTools.isEnabled()) {
|
|
231
|
+
devTools.sendInit(__classPrivateFieldGet(this, _FormController_form, "f"));
|
|
232
|
+
this.sendEntityToDevTools();
|
|
266
233
|
}
|
|
234
|
+
return __classPrivateFieldGet(this, _FormController_form, "f");
|
|
267
235
|
}
|
|
268
236
|
}
|
|
269
237
|
exports.FormController = FormController;
|
|
@@ -11,7 +11,7 @@ export type FormItem<E> = {
|
|
|
11
11
|
disabled?: boolean;
|
|
12
12
|
hidden?: boolean;
|
|
13
13
|
getData?: (params?: any) => Promise<any>;
|
|
14
|
-
setValue?: (property: keyof E, value: any) => void
|
|
14
|
+
setValue?: (property: keyof E, value: any) => Promise<void>;
|
|
15
15
|
control?: (command: CommandItem<E, any>) => Promise<Array<FormItem<E>>>;
|
|
16
16
|
submitted?: (command: CommandItem<E, any>) => Promise<void>;
|
|
17
17
|
initialize?: (command: CommandItem<E, any>) => Promise<Array<FormItem<E>>>;
|
|
@@ -47,17 +47,10 @@ class BaseReader {
|
|
|
47
47
|
* @returns {CommandItem<E, F>} - The command item.
|
|
48
48
|
*/
|
|
49
49
|
getCommand(item) {
|
|
50
|
-
const self = this;
|
|
51
|
-
const getItem = (property) => {
|
|
52
|
-
const { items } = __classPrivateFieldGet(self, _BaseReader_form, "f");
|
|
53
|
-
const index = __classPrivateFieldGet(self, _BaseReader_itemsMap, "f")[property];
|
|
54
|
-
const item = items[index];
|
|
55
|
-
return item;
|
|
56
|
-
};
|
|
57
50
|
return {
|
|
58
51
|
formItem: item,
|
|
59
|
-
initialEntity: __classPrivateFieldGet(
|
|
60
|
-
getItem:
|
|
52
|
+
initialEntity: __classPrivateFieldGet(this, _BaseReader_entity, "f"),
|
|
53
|
+
getItem: (property) => __classPrivateFieldGet(this, _BaseReader_form, "f").items[__classPrivateFieldGet(this, _BaseReader_itemsMap, "f")[property]]
|
|
61
54
|
};
|
|
62
55
|
}
|
|
63
56
|
/**
|
|
@@ -79,7 +72,9 @@ class BaseReader {
|
|
|
79
72
|
for (const item of items) {
|
|
80
73
|
const { property } = item;
|
|
81
74
|
const index = __classPrivateFieldGet(this, _BaseReader_itemsMap, "f")[property];
|
|
82
|
-
|
|
75
|
+
if (Number.isInteger(index)) {
|
|
76
|
+
__classPrivateFieldGet(this, _BaseReader_form, "f").items[index] = item;
|
|
77
|
+
}
|
|
83
78
|
}
|
|
84
79
|
}
|
|
85
80
|
}
|
|
@@ -24,7 +24,7 @@ class Reader extends base_1.default {
|
|
|
24
24
|
* @param items - The array of form items to update.
|
|
25
25
|
*/
|
|
26
26
|
async readItemsData(entity, items) {
|
|
27
|
-
for
|
|
27
|
+
for (const item of items) {
|
|
28
28
|
const data = this.getPropertyData(entity, item);
|
|
29
29
|
item.value = data;
|
|
30
30
|
}
|
|
@@ -10,13 +10,13 @@ class Reader extends base_1.default {
|
|
|
10
10
|
* @param items - Array of form items.
|
|
11
11
|
*/
|
|
12
12
|
async readItemsData(items) {
|
|
13
|
-
for
|
|
13
|
+
for (const formItem of items) {
|
|
14
14
|
const { initialize } = formItem;
|
|
15
15
|
if (initialize) {
|
|
16
16
|
// Call the 'initialize' function of the form item with a command item.
|
|
17
17
|
const items = await initialize(super.getCommand(formItem));
|
|
18
18
|
// Write the updated items back to the form.
|
|
19
|
-
this.writeItems(items);
|
|
19
|
+
await this.writeItems(items);
|
|
20
20
|
}
|
|
21
21
|
}
|
|
22
22
|
}
|
|
@@ -28,7 +28,7 @@ class Reader extends base_1.default {
|
|
|
28
28
|
async read(entity, form) {
|
|
29
29
|
await super.read(entity, form);
|
|
30
30
|
// Read and initialize form items.
|
|
31
|
-
this.readItemsData(form.items);
|
|
31
|
+
await this.readItemsData(form.items);
|
|
32
32
|
}
|
|
33
33
|
}
|
|
34
34
|
exports.default = Reader;
|
|
@@ -3,15 +3,10 @@ import { FormItem, Form, IReader } from '../..';
|
|
|
3
3
|
* Class for reading form data.
|
|
4
4
|
*/
|
|
5
5
|
declare class Reader<E, F extends FormItem<E>> implements IReader<E, F> {
|
|
6
|
-
/**
|
|
7
|
-
*
|
|
8
|
-
*/
|
|
9
|
-
constructor();
|
|
10
6
|
/**
|
|
11
7
|
* Reads the form data from the provided entity.
|
|
12
8
|
* @param {E} entity - The entity to read form data from.
|
|
13
|
-
* @
|
|
14
|
-
* @throws {Error} - If the entity is not provided.
|
|
9
|
+
* @param {Form<E, F>} form - The form to write the metadata to.
|
|
15
10
|
*/
|
|
16
11
|
read(entity: E, form: Form<E, F>): Promise<void>;
|
|
17
12
|
}
|
|
@@ -5,26 +5,15 @@ const __1 = require("../..");
|
|
|
5
5
|
* Class for reading form data.
|
|
6
6
|
*/
|
|
7
7
|
class Reader {
|
|
8
|
-
/**
|
|
9
|
-
*
|
|
10
|
-
*/
|
|
11
|
-
constructor() { }
|
|
12
8
|
/**
|
|
13
9
|
* Reads the form data from the provided entity.
|
|
14
10
|
* @param {E} entity - The entity to read form data from.
|
|
15
|
-
* @
|
|
16
|
-
* @throws {Error} - If the entity is not provided.
|
|
11
|
+
* @param {Form<E, F>} form - The form to write the metadata to.
|
|
17
12
|
*/
|
|
18
|
-
read(entity, form) {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
// Retrieves starters metadata from the entity using reflection
|
|
23
|
-
form.starters = Reflect.getMetadata(__1.DECORATOR_STARTERS, entity);
|
|
24
|
-
// Retrieves starters metadata from the entity using reflection
|
|
25
|
-
form.readers = Reflect.getMetadata(__1.DECORATOR_READERS, entity);
|
|
26
|
-
resolve();
|
|
27
|
-
});
|
|
13
|
+
async read(entity, form) {
|
|
14
|
+
form.services = Reflect.getMetadata(__1.DECORATOR_SERVICES, entity);
|
|
15
|
+
form.starters = Reflect.getMetadata(__1.DECORATOR_STARTERS, entity);
|
|
16
|
+
form.readers = Reflect.getMetadata(__1.DECORATOR_READERS, entity);
|
|
28
17
|
}
|
|
29
18
|
}
|
|
30
19
|
exports.default = Reader;
|
package/dist/utils/deepCopy.d.ts
CHANGED
|
@@ -1,18 +1,8 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
4
|
-
*
|
|
2
|
+
* Creates a deep copy of the given value.
|
|
3
|
+
* Functions are kept by reference, prototypes are preserved and
|
|
4
|
+
* circular references are supported.
|
|
5
|
+
* @param value - The value to copy
|
|
6
|
+
* @returns The copied value
|
|
5
7
|
*/
|
|
6
|
-
export declare const
|
|
7
|
-
/**
|
|
8
|
-
* Performs a deep copy of a function.
|
|
9
|
-
* @param func - The function to be copied.
|
|
10
|
-
* @returns The deep copy of the function.
|
|
11
|
-
*/
|
|
12
|
-
export declare const deepCopyFunction: (func: Function) => any;
|
|
13
|
-
/**
|
|
14
|
-
* Performs a deep copy of an object.
|
|
15
|
-
* @param obj - The object to be copied.
|
|
16
|
-
* @returns The deep copy of the object.
|
|
17
|
-
*/
|
|
18
|
-
export declare const deepCopy: (obj: any) => any;
|
|
8
|
+
export declare const deepCopy: <T>(value: T, seen?: WeakMap<object, any>) => T;
|
package/dist/utils/deepCopy.js
CHANGED
|
@@ -1,82 +1,45 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.deepCopy =
|
|
3
|
+
exports.deepCopy = void 0;
|
|
4
4
|
/**
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
5
|
+
* Creates a deep copy of the given value.
|
|
6
|
+
* Functions are kept by reference, prototypes are preserved and
|
|
7
|
+
* circular references are supported.
|
|
8
|
+
* @param value - The value to copy
|
|
9
|
+
* @returns The copied value
|
|
8
10
|
*/
|
|
9
|
-
const
|
|
10
|
-
if (
|
|
11
|
-
return
|
|
11
|
+
const deepCopy = (value, seen = new WeakMap()) => {
|
|
12
|
+
if (typeof value !== 'object' || value === null) {
|
|
13
|
+
return value;
|
|
12
14
|
}
|
|
13
|
-
if (
|
|
14
|
-
|
|
15
|
-
const flags = pattern.substring(pattern.lastIndexOf('/') + 1);
|
|
16
|
-
const realPattern = pattern.substring(1, pattern.lastIndexOf('/'));
|
|
17
|
-
return new RegExp(realPattern, flags);
|
|
15
|
+
if (seen.has(value)) {
|
|
16
|
+
return seen.get(value);
|
|
18
17
|
}
|
|
19
|
-
if (
|
|
20
|
-
|
|
21
|
-
obj.forEach((v, k) => {
|
|
22
|
-
mapCopy.set(k, v);
|
|
23
|
-
});
|
|
24
|
-
return mapCopy;
|
|
18
|
+
if (value instanceof Date) {
|
|
19
|
+
return new Date(value.getTime());
|
|
25
20
|
}
|
|
26
|
-
if (
|
|
27
|
-
|
|
28
|
-
obj.forEach((v) => {
|
|
29
|
-
setCopy.add(v);
|
|
30
|
-
});
|
|
31
|
-
return setCopy;
|
|
21
|
+
if (value instanceof RegExp) {
|
|
22
|
+
return new RegExp(value.source, value.flags);
|
|
32
23
|
}
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
* @returns The deep copy of the function.
|
|
39
|
-
*/
|
|
40
|
-
const deepCopyFunction = (func) => {
|
|
41
|
-
const funcStr = func.toString();
|
|
42
|
-
if (func.prototype) {
|
|
43
|
-
const paramArr = funcStr.match(/\((.*?)\)/)[1].split(', ');
|
|
44
|
-
const funcBody = funcStr.match(/\{([\s\S]*)\}/)[1];
|
|
45
|
-
const funcCopy = new Function(...paramArr, funcBody);
|
|
46
|
-
funcCopy.prototype = Object.assign({}, func.prototype);
|
|
47
|
-
return funcCopy;
|
|
48
|
-
}
|
|
49
|
-
else {
|
|
50
|
-
return eval(funcStr);
|
|
51
|
-
}
|
|
52
|
-
};
|
|
53
|
-
exports.deepCopyFunction = deepCopyFunction;
|
|
54
|
-
/**
|
|
55
|
-
* Performs a deep copy of an object.
|
|
56
|
-
* @param obj - The object to be copied.
|
|
57
|
-
* @returns The deep copy of the object.
|
|
58
|
-
*/
|
|
59
|
-
const deepCopy = (obj) => {
|
|
60
|
-
//if (obj instanceof Function) {
|
|
61
|
-
// return deepCopyFunction(obj);
|
|
62
|
-
//}
|
|
63
|
-
if (obj instanceof Date ||
|
|
64
|
-
obj instanceof RegExp ||
|
|
65
|
-
obj instanceof Map ||
|
|
66
|
-
obj instanceof Set) {
|
|
67
|
-
return (0, exports.deepCopySpecialTypes)(obj);
|
|
68
|
-
}
|
|
69
|
-
if (obj instanceof Array) {
|
|
70
|
-
return obj.map(exports.deepCopy);
|
|
24
|
+
if (value instanceof Map) {
|
|
25
|
+
const copy = new Map();
|
|
26
|
+
seen.set(value, copy);
|
|
27
|
+
value.forEach((v, k) => copy.set((0, exports.deepCopy)(k, seen), (0, exports.deepCopy)(v, seen)));
|
|
28
|
+
return copy;
|
|
71
29
|
}
|
|
72
|
-
if (
|
|
73
|
-
const copy =
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
copy[key] = (0, exports.deepCopy)(obj[key]);
|
|
77
|
-
});
|
|
30
|
+
if (value instanceof Set) {
|
|
31
|
+
const copy = new Set();
|
|
32
|
+
seen.set(value, copy);
|
|
33
|
+
value.forEach((v) => copy.add((0, exports.deepCopy)(v, seen)));
|
|
78
34
|
return copy;
|
|
79
35
|
}
|
|
80
|
-
|
|
36
|
+
const copy = Array.isArray(value)
|
|
37
|
+
? []
|
|
38
|
+
: Object.create(Object.getPrototypeOf(value));
|
|
39
|
+
seen.set(value, copy);
|
|
40
|
+
for (const key of Object.keys(value)) {
|
|
41
|
+
copy[key] = (0, exports.deepCopy)(value[key], seen);
|
|
42
|
+
}
|
|
43
|
+
return copy;
|
|
81
44
|
};
|
|
82
45
|
exports.deepCopy = deepCopy;
|
package/dist/utils/devtools.d.ts
CHANGED
|
@@ -1,4 +1,8 @@
|
|
|
1
1
|
import { Form, FormItem } from '../model';
|
|
2
|
+
/**
|
|
3
|
+
* Marker used by the DevTools extension to recognize bridge messages
|
|
4
|
+
*/
|
|
5
|
+
export declare const DEVTOOLS_MESSAGE_SOURCE = "punica-form-devtools";
|
|
2
6
|
/**
|
|
3
7
|
* DevTools bridge for sending form events to Chrome DevTools extension
|
|
4
8
|
*/
|
package/dist/utils/devtools.js
CHANGED
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.isDevToolsEnabled = exports.disableDevTools = exports.enableDevTools = exports.getDevToolsBridge = void 0;
|
|
3
|
+
exports.isDevToolsEnabled = exports.disableDevTools = exports.enableDevTools = exports.getDevToolsBridge = exports.DEVTOOLS_MESSAGE_SOURCE = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Marker used by the DevTools extension to recognize bridge messages
|
|
6
|
+
*/
|
|
7
|
+
exports.DEVTOOLS_MESSAGE_SOURCE = 'punica-form-devtools';
|
|
4
8
|
/**
|
|
5
9
|
* DevTools bridge for sending form events to Chrome DevTools extension
|
|
6
10
|
*/
|
|
@@ -42,25 +46,17 @@ class DevToolsBridge {
|
|
|
42
46
|
if (!this.isEnabled()) {
|
|
43
47
|
return;
|
|
44
48
|
}
|
|
45
|
-
console.log('🔥 DevToolsBridge.sendMessage:', type, data, formItemKey);
|
|
46
49
|
try {
|
|
47
50
|
const message = {
|
|
51
|
+
source: exports.DEVTOOLS_MESSAGE_SOURCE,
|
|
48
52
|
type,
|
|
49
|
-
data: JSON.stringify(data)
|
|
53
|
+
data: JSON.stringify(data),
|
|
54
|
+
formItemKey
|
|
50
55
|
};
|
|
51
|
-
|
|
52
|
-
try {
|
|
53
|
-
window.postMessage(message, '*');
|
|
54
|
-
}
|
|
55
|
-
catch (e) {
|
|
56
|
-
// Fallback: try with current window
|
|
57
|
-
if (typeof window !== 'undefined') {
|
|
58
|
-
window.postMessage(message, '*');
|
|
59
|
-
}
|
|
60
|
-
}
|
|
56
|
+
window.postMessage(message, window.location.origin);
|
|
61
57
|
}
|
|
62
|
-
catch (
|
|
63
|
-
//
|
|
58
|
+
catch (_a) {
|
|
59
|
+
// Debugging must never break the form (e.g. non-serializable data)
|
|
64
60
|
}
|
|
65
61
|
}
|
|
66
62
|
/**
|
|
@@ -100,17 +96,13 @@ class DevToolsBridge {
|
|
|
100
96
|
* Send register items event
|
|
101
97
|
*/
|
|
102
98
|
sendRegisterItems(itemKeys) {
|
|
103
|
-
this.sendMessage('REGISTER_ITEMS',
|
|
99
|
+
this.sendMessage('REGISTER_ITEMS', itemKeys);
|
|
104
100
|
}
|
|
105
101
|
/**
|
|
106
102
|
* Send property value update event
|
|
107
103
|
*/
|
|
108
104
|
sendUpdatePropertyValue(property, value, item) {
|
|
109
|
-
this.sendMessage('UPDATE_PROPERTY_VALUE', {
|
|
110
|
-
property,
|
|
111
|
-
value,
|
|
112
|
-
item
|
|
113
|
-
}, property);
|
|
105
|
+
this.sendMessage('UPDATE_PROPERTY_VALUE', { property, value, item }, property);
|
|
114
106
|
}
|
|
115
107
|
}
|
|
116
108
|
DevToolsBridge.instance = null;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@punica/form",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"description": "Form",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "Uryan Sezgin TEKE",
|
|
@@ -18,23 +18,18 @@
|
|
|
18
18
|
"access": "public"
|
|
19
19
|
},
|
|
20
20
|
"repository": {
|
|
21
|
-
"url": "git
|
|
21
|
+
"url": "git+https://github.com/punicalab/punica-form.git"
|
|
22
22
|
},
|
|
23
23
|
"files": [
|
|
24
24
|
"dist"
|
|
25
25
|
],
|
|
26
|
-
"scripts": {
|
|
27
|
-
"build": "scripty",
|
|
28
|
-
"clean": "scripty",
|
|
29
|
-
"lint": "scripty",
|
|
30
|
-
"compile": "scripty"
|
|
31
|
-
},
|
|
32
|
-
"scripty": {
|
|
33
|
-
"path": "../../scripts/packages"
|
|
34
|
-
},
|
|
35
26
|
"dependencies": {
|
|
36
27
|
"@punica/common": "^1.0.1",
|
|
37
28
|
"@punica/util": "^1.0.2"
|
|
38
29
|
},
|
|
39
|
-
"
|
|
40
|
-
|
|
30
|
+
"scripts": {
|
|
31
|
+
"build": "tsc -p tsconfig.build.json",
|
|
32
|
+
"clean": "rimraf dist",
|
|
33
|
+
"lint": "eslint lib"
|
|
34
|
+
}
|
|
35
|
+
}
|