@punica/form 1.1.11 → 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 -120
- 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 -1
- package/dist/utils/devtools.js +13 -43
- 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
|
@@ -37,17 +37,9 @@ class FormController extends common_1.BaseListener {
|
|
|
37
37
|
* @param data Data associated with the event
|
|
38
38
|
*/
|
|
39
39
|
this.fireEvent = (eventType, data) => {
|
|
40
|
-
var _a, _b;
|
|
41
40
|
this.trigger(eventType, data);
|
|
42
41
|
const devTools = (0, __1.getDevToolsBridge)();
|
|
43
42
|
const isEnabled = devTools.isEnabled();
|
|
44
|
-
if (typeof process !== 'undefined' &&
|
|
45
|
-
((_a = process.env) === null || _a === void 0 ? void 0 : _a.NODE_ENV) === 'development') {
|
|
46
|
-
console.log('🔥 FormController.fireEvent:', eventType, {
|
|
47
|
-
devToolsEnabled: isEnabled,
|
|
48
|
-
hasData: !!data
|
|
49
|
-
});
|
|
50
|
-
}
|
|
51
43
|
if (isEnabled) {
|
|
52
44
|
switch (eventType) {
|
|
53
45
|
case 'UPDATE_FORM':
|
|
@@ -62,16 +54,10 @@ class FormController extends common_1.BaseListener {
|
|
|
62
54
|
devTools.sendUpdateItem(data);
|
|
63
55
|
break;
|
|
64
56
|
case 'REGISTER_ITEMS':
|
|
65
|
-
devTools.sendRegisterItems(
|
|
57
|
+
devTools.sendRegisterItems(data);
|
|
66
58
|
break;
|
|
67
59
|
}
|
|
68
60
|
}
|
|
69
|
-
else {
|
|
70
|
-
if (typeof process !== 'undefined' &&
|
|
71
|
-
((_b = process.env) === null || _b === void 0 ? void 0 : _b.NODE_ENV) === 'development') {
|
|
72
|
-
console.warn('⚠️ FormController: DevTools bridge is disabled. Event not sent:', eventType);
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
61
|
};
|
|
76
62
|
/**
|
|
77
63
|
* Send entity data to DevTools
|
|
@@ -81,81 +67,62 @@ class FormController extends common_1.BaseListener {
|
|
|
81
67
|
if (!devTools.isEnabled()) {
|
|
82
68
|
return;
|
|
83
69
|
}
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
if (getValuesService && typeof getValuesService.run === 'function') {
|
|
88
|
-
getValuesService
|
|
89
|
-
.run()
|
|
90
|
-
.then((entity) => {
|
|
91
|
-
devTools.sendEntity(entity);
|
|
92
|
-
})
|
|
93
|
-
.catch(() => {
|
|
94
|
-
// Silently fail
|
|
95
|
-
});
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
catch (_a) {
|
|
99
|
-
// Silently fail
|
|
100
|
-
}
|
|
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(() => { });
|
|
101
73
|
};
|
|
102
74
|
/**
|
|
103
75
|
* Create a command service for the form.
|
|
104
76
|
* @returns A CommandService for the form
|
|
105
77
|
*/
|
|
106
78
|
this.createCommandService = () => {
|
|
107
|
-
const self = this;
|
|
108
79
|
const getItem = (property) => {
|
|
109
|
-
const
|
|
110
|
-
|
|
111
|
-
if (!Number.isInteger(itemIndex))
|
|
112
|
-
return null;
|
|
113
|
-
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;
|
|
114
82
|
};
|
|
115
|
-
const writeItems = async (
|
|
116
|
-
for (const item of
|
|
117
|
-
const
|
|
118
|
-
|
|
119
|
-
|
|
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
|
+
}
|
|
120
89
|
}
|
|
121
90
|
};
|
|
122
|
-
const fireEvent = (ev, data) => {
|
|
123
|
-
self.fireEvent(ev, data);
|
|
124
|
-
};
|
|
125
91
|
const createCommandItem = async (item) => {
|
|
126
|
-
var _a, _b;
|
|
92
|
+
var _a, _b, _c;
|
|
127
93
|
let itemCustomCommand = {};
|
|
128
|
-
for
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
itemCustomCommand = { ...itemCustomCommand, ...command };
|
|
133
|
-
}
|
|
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 };
|
|
134
98
|
}
|
|
135
99
|
return {
|
|
136
100
|
formItem: item,
|
|
137
|
-
initialEntity: __classPrivateFieldGet(
|
|
101
|
+
initialEntity: __classPrivateFieldGet(this, _FormController_initialEntity, "f"),
|
|
138
102
|
getItem,
|
|
139
103
|
writeItems,
|
|
140
104
|
...itemCustomCommand
|
|
141
105
|
};
|
|
142
106
|
};
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
initialEntity: __classPrivateFieldGet(self, _FormController_initialEntity, "f"),
|
|
146
|
-
form: __classPrivateFieldGet(self, _FormController_form, "f"),
|
|
147
|
-
itemsMap: __classPrivateFieldGet(self, _FormController_itemsMap, "f"),
|
|
107
|
+
const command = {
|
|
108
|
+
initialEntity: __classPrivateFieldGet(this, _FormController_initialEntity, "f"),
|
|
148
109
|
getItem,
|
|
149
110
|
writeItems,
|
|
150
|
-
fireEvent,
|
|
111
|
+
fireEvent: this.fireEvent,
|
|
151
112
|
createCommandItem
|
|
152
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
|
+
});
|
|
153
120
|
};
|
|
154
121
|
/**
|
|
155
122
|
* Registers form items by triggering the REGISTER_ITEMS event.
|
|
156
123
|
*/
|
|
157
124
|
this.registerFormItems = () => {
|
|
158
|
-
this.fireEvent('REGISTER_ITEMS', __1.FormItemRegister.getInstance().getItemKeys());
|
|
125
|
+
this.fireEvent('REGISTER_ITEMS', Array.from(__1.FormItemRegister.getInstance().getItemKeys()));
|
|
159
126
|
};
|
|
160
127
|
/**
|
|
161
128
|
* Maps form items to their properties.
|
|
@@ -171,14 +138,13 @@ class FormController extends common_1.BaseListener {
|
|
|
171
138
|
* Initializes the services defined in the form data.
|
|
172
139
|
*/
|
|
173
140
|
this.initializeServices = async () => {
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
}
|
|
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);
|
|
182
148
|
}
|
|
183
149
|
}
|
|
184
150
|
};
|
|
@@ -186,27 +152,26 @@ class FormController extends common_1.BaseListener {
|
|
|
186
152
|
* Executes the readers defined in the form data.
|
|
187
153
|
*/
|
|
188
154
|
this.runReaders = async () => {
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
}
|
|
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"));
|
|
193
158
|
}
|
|
194
159
|
};
|
|
195
160
|
/**
|
|
196
161
|
* Executes the starters defined in the form data.
|
|
197
162
|
*/
|
|
198
163
|
this.runStarters = async () => {
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
}
|
|
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");
|
|
203
167
|
}
|
|
204
168
|
};
|
|
205
169
|
/**
|
|
206
|
-
*
|
|
170
|
+
* Keeps a deep copy of the form items for the initial state.
|
|
171
|
+
* Services, readers and starters are shared, not copied.
|
|
207
172
|
*/
|
|
208
173
|
this.deepCopyForm = () => {
|
|
209
|
-
__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");
|
|
210
175
|
};
|
|
211
176
|
__classPrivateFieldSet(this, _FormController_serviceMap, {}, "f");
|
|
212
177
|
__classPrivateFieldSet(this, _FormController_itemsMap, {}, "f");
|
|
@@ -215,32 +180,28 @@ class FormController extends common_1.BaseListener {
|
|
|
215
180
|
* Static method to initialize the FormController with form data.
|
|
216
181
|
* @param form The initial form data
|
|
217
182
|
*/
|
|
218
|
-
static fromForm(form) {
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
resolve(instance);
|
|
224
|
-
});
|
|
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;
|
|
225
188
|
}
|
|
226
189
|
/**
|
|
227
190
|
* Static method to initialize the FormController with an entity.
|
|
228
191
|
* @param entity The entity for the form
|
|
229
192
|
*/
|
|
230
|
-
static fromEntity(entity) {
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
resolve(instance);
|
|
243
|
-
});
|
|
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;
|
|
244
205
|
}
|
|
245
206
|
getServices(serviceName, ...additionalServiceNames) {
|
|
246
207
|
if (additionalServiceNames.length > 0) {
|
|
@@ -257,27 +218,20 @@ class FormController extends common_1.BaseListener {
|
|
|
257
218
|
* @returns A promise that resolves when the form is fully initialized.
|
|
258
219
|
*/
|
|
259
220
|
async start() {
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
this.sendEntityToDevTools();
|
|
273
|
-
}
|
|
274
|
-
// Trigger an update event after the form is fully initialized
|
|
275
|
-
return __classPrivateFieldGet(this, _FormController_form, "f");
|
|
276
|
-
}
|
|
277
|
-
catch (error) {
|
|
278
|
-
console.error('Error starting the form controller:', error);
|
|
279
|
-
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();
|
|
280
233
|
}
|
|
234
|
+
return __classPrivateFieldGet(this, _FormController_form, "f");
|
|
281
235
|
}
|
|
282
236
|
}
|
|
283
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,11 +1,14 @@
|
|
|
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
|
*/
|
|
5
9
|
declare class DevToolsBridge {
|
|
6
10
|
private static instance;
|
|
7
11
|
private enabled;
|
|
8
|
-
private constructor();
|
|
9
12
|
/**
|
|
10
13
|
* Get singleton instance
|
|
11
14
|
*/
|
package/dist/utils/devtools.js
CHANGED
|
@@ -1,22 +1,16 @@
|
|
|
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
|
*/
|
|
7
11
|
class DevToolsBridge {
|
|
8
12
|
constructor() {
|
|
9
|
-
var _a;
|
|
10
13
|
this.enabled = false;
|
|
11
|
-
// Check if we're in a browser environment
|
|
12
|
-
if (typeof window !== 'undefined') {
|
|
13
|
-
// Enable by default in development
|
|
14
|
-
// Check for NODE_ENV or global flag
|
|
15
|
-
const isDevelopment = (typeof process !== 'undefined' &&
|
|
16
|
-
((_a = process.env) === null || _a === void 0 ? void 0 : _a.NODE_ENV) === 'development') ||
|
|
17
|
-
window.__PUNICA_FORM_DEVTOOLS__ === true;
|
|
18
|
-
this.enabled = isDevelopment;
|
|
19
|
-
}
|
|
20
14
|
}
|
|
21
15
|
/**
|
|
22
16
|
* Get singleton instance
|
|
@@ -49,40 +43,20 @@ class DevToolsBridge {
|
|
|
49
43
|
* Send message to DevTools extension
|
|
50
44
|
*/
|
|
51
45
|
sendMessage(type, data, formItemKey) {
|
|
52
|
-
var _a, _b, _c;
|
|
53
46
|
if (!this.isEnabled()) {
|
|
54
|
-
if (typeof process !== 'undefined' &&
|
|
55
|
-
((_a = process.env) === null || _a === void 0 ? void 0 : _a.NODE_ENV) === 'development') {
|
|
56
|
-
console.warn('⚠️ Punica Form DevTools: Bridge is DISABLED. Event not sent:', type);
|
|
57
|
-
}
|
|
58
47
|
return;
|
|
59
48
|
}
|
|
60
49
|
try {
|
|
61
50
|
const message = {
|
|
51
|
+
source: exports.DEVTOOLS_MESSAGE_SOURCE,
|
|
62
52
|
type,
|
|
63
|
-
data,
|
|
64
|
-
|
|
53
|
+
data: JSON.stringify(data),
|
|
54
|
+
formItemKey
|
|
65
55
|
};
|
|
66
|
-
window.postMessage(message,
|
|
67
|
-
// Log in development for debugging
|
|
68
|
-
if (typeof process !== 'undefined' &&
|
|
69
|
-
((_b = process.env) === null || _b === void 0 ? void 0 : _b.NODE_ENV) === 'development') {
|
|
70
|
-
console.log('📤 Punica Form DevTools: Sending event', type, {
|
|
71
|
-
hasData: !!data,
|
|
72
|
-
hasFormItemKey: !!formItemKey,
|
|
73
|
-
dataType: typeof data,
|
|
74
|
-
dataKeys: data && typeof data === 'object'
|
|
75
|
-
? Object.keys(data).slice(0, 5)
|
|
76
|
-
: null
|
|
77
|
-
});
|
|
78
|
-
}
|
|
56
|
+
window.postMessage(message, window.location.origin);
|
|
79
57
|
}
|
|
80
|
-
catch (
|
|
81
|
-
//
|
|
82
|
-
if (typeof process !== 'undefined' &&
|
|
83
|
-
((_c = process.env) === null || _c === void 0 ? void 0 : _c.NODE_ENV) === 'development') {
|
|
84
|
-
console.warn('❌ Punica Form DevTools: Failed to send message', error);
|
|
85
|
-
}
|
|
58
|
+
catch (_a) {
|
|
59
|
+
// Debugging must never break the form (e.g. non-serializable data)
|
|
86
60
|
}
|
|
87
61
|
}
|
|
88
62
|
/**
|
|
@@ -122,17 +96,13 @@ class DevToolsBridge {
|
|
|
122
96
|
* Send register items event
|
|
123
97
|
*/
|
|
124
98
|
sendRegisterItems(itemKeys) {
|
|
125
|
-
this.sendMessage('REGISTER_ITEMS',
|
|
99
|
+
this.sendMessage('REGISTER_ITEMS', itemKeys);
|
|
126
100
|
}
|
|
127
101
|
/**
|
|
128
102
|
* Send property value update event
|
|
129
103
|
*/
|
|
130
104
|
sendUpdatePropertyValue(property, value, item) {
|
|
131
|
-
this.sendMessage('UPDATE_PROPERTY_VALUE', {
|
|
132
|
-
property,
|
|
133
|
-
value,
|
|
134
|
-
item
|
|
135
|
-
}, property);
|
|
105
|
+
this.sendMessage('UPDATE_PROPERTY_VALUE', { property, value, item }, property);
|
|
136
106
|
}
|
|
137
107
|
}
|
|
138
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
|
+
}
|