@piying/view-angular-core 1.6.0 → 1.6.2

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.
@@ -1,226 +1,12 @@
1
- import { effect, inject, Injector, untracked, DestroyRef, isSignal, signal, computed, InjectionToken, linkedSignal, EnvironmentInjector } from '@angular/core';
1
+ import { signal, computed, InjectionToken, effect, inject, Injector, untracked, DestroyRef, linkedSignal, isSignal, EnvironmentInjector } from '@angular/core';
2
2
  import * as v from 'valibot';
3
- import { deepEqual } from 'fast-equals';
4
- import { BehaviorSubject, isObservable, Subject, combineLatest, startWith, skip, map } from 'rxjs';
3
+ import { isObservable, BehaviorSubject, Subject, combineLatest, startWith, skip, map } from 'rxjs';
5
4
  import rfdc from 'rfdc';
5
+ import { deepEqual } from 'fast-equals';
6
6
  import { createRawConfig, BaseSchemaHandle, convertSchema, convertCore } from '@piying/valibot-visit';
7
7
  export { asControl, asVirtualGroup, changeObject, condition, getDefaults, getSchemaByIssuePath, getSchemaMetadata } from '@piying/valibot-visit';
8
8
  import clsx from 'clsx';
9
9
 
10
- function arrayStartsWith(list, parts) {
11
- return (Array.isArray(parts) ? parts : [parts]).every((item, index) => item === list[index]);
12
- }
13
-
14
- /** 跳过第一个值,假如发射时和初始值不同 */
15
- function effectListen(listen, fn, options) {
16
- let first = true;
17
- const oldValue = listen();
18
- return effect(() => {
19
- const currentValue = listen();
20
- if (first) {
21
- first = false;
22
- if (!deepEqual(oldValue, currentValue)) {
23
- fn();
24
- }
25
- return;
26
- }
27
- return fn();
28
- }, options);
29
- }
30
-
31
- /**
32
- * Exposes the value of an Angular `Signal` as an RxJS `Observable`.
33
- *
34
- * The signal's value will be propagated into the `Observable`'s subscribers using an `effect`.
35
- *
36
- * `toObservable` must be called in an injection context unless an injector is provided via options.
37
- *
38
- * @publicApi 20.0
39
- */
40
- function toObservable(listen, source, options) {
41
- const injector = options?.injector ?? inject(Injector);
42
- const subject = new BehaviorSubject(source());
43
- const watcher = effectListen(listen, () => {
44
- let value;
45
- try {
46
- value = source();
47
- }
48
- catch (err) {
49
- untracked(() => subject.error(err));
50
- return;
51
- }
52
- untracked(() => subject.next(value));
53
- }, { injector, manualCleanup: true });
54
- injector.get(DestroyRef).onDestroy(() => {
55
- watcher.destroy();
56
- subject.complete();
57
- });
58
- return subject.asObservable();
59
- }
60
-
61
- const clone = rfdc({ proto: false, circles: false });
62
-
63
- function unWrapSignal$1(value) {
64
- return isSignal(value) ? value() : value;
65
- }
66
-
67
- function toArray(input) {
68
- return input === undefined
69
- ? input
70
- : Array.isArray(input)
71
- ? input
72
- : [input];
73
- }
74
-
75
- class SortedArray extends Array {
76
- #compareFn;
77
- constructor(compareFn) {
78
- super();
79
- this.#compareFn = compareFn;
80
- }
81
- push(...items) {
82
- const result = super.push(...items);
83
- this.sort(this.#compareFn);
84
- return result;
85
- }
86
- }
87
-
88
- const markSymbol = Symbol();
89
- function lazyMark(fn) {
90
- return { [markSymbol]: fn };
91
- }
92
- function isLazyMark(input) {
93
- return markSymbol in input;
94
- }
95
- function getLazyImport(input) {
96
- return (input[markSymbol] ??
97
- (typeof input === 'function' ? input : undefined));
98
- }
99
-
100
- function createViewControlLink(fieldControl, cva, injector) {
101
- cva.registerOnChange((value) => {
102
- if (fieldControl().disabled$$()) {
103
- return;
104
- }
105
- fieldControl().pendingStatus.update((status) => ({
106
- ...status,
107
- value: value,
108
- change: true,
109
- }));
110
- if (fieldControl().updateOn$$() === 'change') {
111
- fieldControl().viewValueChange(value);
112
- }
113
- });
114
- cva.registerOnTouched(() => {
115
- if (fieldControl().disabled$$()) {
116
- return;
117
- }
118
- const updateOn = fieldControl().updateOn$$();
119
- const pendingStatus = fieldControl().pendingStatus();
120
- if (updateOn === 'submit') {
121
- fieldControl().pendingStatus.update((status) => ({
122
- ...status,
123
- touched: true,
124
- }));
125
- }
126
- else {
127
- fieldControl().markAsTouched();
128
- if (pendingStatus.change && updateOn === 'blur') {
129
- fieldControl().viewValueChange(pendingStatus.value);
130
- fieldControl().pendingStatus.update((status) => ({
131
- ...status,
132
- change: false,
133
- value: undefined,
134
- }));
135
- }
136
- }
137
- });
138
- const disposeList = [];
139
- const ref = effect(() => {
140
- const value = fieldControl().modelValueToViewValue$$();
141
- untracked(() => {
142
- cva.writeValue(value);
143
- });
144
- }, { injector: injector });
145
- cva.writeValue(fieldControl().modelValueToViewValue$$());
146
- disposeList.push(() => ref.destroy());
147
- if (cva.setDisabledState) {
148
- const disabledRef = effect(() => {
149
- const value = fieldControl().disabled$$();
150
- untracked(() => {
151
- cva.setDisabledState(value);
152
- });
153
- }, { injector: injector });
154
- cva.setDisabledState(fieldControl().disabled$$());
155
- disposeList.push(() => disabledRef.destroy());
156
- }
157
- return (destroy) => {
158
- disposeList.forEach((fn) => fn());
159
- if (!destroy) {
160
- untracked(() => {
161
- fieldControl().updateValue(fieldControl().value$$(), true);
162
- });
163
- }
164
- };
165
- }
166
-
167
- function controlStatusList(fieldControl, skipDisabled) {
168
- if (!fieldControl) {
169
- return [];
170
- }
171
- const statusList = [];
172
- if (!skipDisabled && fieldControl.disabled$$()) {
173
- statusList.push('disabled');
174
- }
175
- if (fieldControl.touched$$()) {
176
- statusList.push('touched');
177
- }
178
- else {
179
- statusList.push('untouched');
180
- }
181
- if (fieldControl.dirty$$()) {
182
- statusList.push('dirty');
183
- }
184
- else {
185
- statusList.push('pristine');
186
- }
187
- switch (fieldControl.status$$()) {
188
- case 'VALID':
189
- statusList.push('valid');
190
- break;
191
- case 'INVALID':
192
- statusList.push('invalid');
193
- break;
194
- case 'PENDING':
195
- statusList.push('pending');
196
- break;
197
- default:
198
- break;
199
- }
200
- return statusList;
201
- }
202
- function fieldControlStatusClass(fieldControl, skipDisabled) {
203
- return controlStatusList(fieldControl, skipDisabled)
204
- .map((item) => `pi-${item}`)
205
- .join(' ');
206
- }
207
-
208
- function initListen(input, control, injector, fn) {
209
- let init = true;
210
- return effect(() => {
211
- const modelValue = control.value$$();
212
- if (init) {
213
- if (!deepEqual(modelValue, input)) {
214
- fn(modelValue);
215
- }
216
- init = false;
217
- }
218
- else {
219
- fn(modelValue);
220
- }
221
- }, { injector });
222
- }
223
-
224
10
  function isPromise(obj) {
225
11
  return !!obj && typeof obj.then === 'function';
226
12
  }
@@ -275,6 +61,55 @@ const PI_FORM_BUILDER_ALIAS_MAP = new InjectionToken('PI_FORM_BUILDER_ALIAS_MAP'
275
61
  /** 上下文注入 */
276
62
  const PI_CONTEXT_TOKEN = new InjectionToken('PI_CONTEXT');
277
63
 
64
+ const clone = rfdc({ proto: false, circles: false });
65
+
66
+ /** 跳过第一个值,假如发射时和初始值不同 */
67
+ function effectListen(listen, fn, options) {
68
+ let first = true;
69
+ const oldValue = listen();
70
+ return effect(() => {
71
+ const currentValue = listen();
72
+ if (first) {
73
+ first = false;
74
+ if (!deepEqual(oldValue, currentValue)) {
75
+ fn();
76
+ }
77
+ return;
78
+ }
79
+ return fn();
80
+ }, options);
81
+ }
82
+
83
+ /**
84
+ * Exposes the value of an Angular `Signal` as an RxJS `Observable`.
85
+ *
86
+ * The signal's value will be propagated into the `Observable`'s subscribers using an `effect`.
87
+ *
88
+ * `toObservable` must be called in an injection context unless an injector is provided via options.
89
+ *
90
+ * @publicApi 20.0
91
+ */
92
+ function toObservable(listen, source, options) {
93
+ const injector = options?.injector ?? inject(Injector);
94
+ const subject = new BehaviorSubject(source());
95
+ const watcher = effectListen(listen, () => {
96
+ let value;
97
+ try {
98
+ value = source();
99
+ }
100
+ catch (err) {
101
+ untracked(() => subject.error(err));
102
+ return;
103
+ }
104
+ untracked(() => subject.next(value));
105
+ }, { injector, manualCleanup: true });
106
+ injector.get(DestroyRef).onDestroy(() => {
107
+ watcher.destroy();
108
+ subject.complete();
109
+ });
110
+ return subject.asObservable();
111
+ }
112
+
278
113
  function shortCircuitTrue(value) {
279
114
  return value;
280
115
  }
@@ -1082,7 +917,7 @@ key, formConfig$$, isRoot, injector) {
1082
917
  return control;
1083
918
  }
1084
919
 
1085
- function unWrapSignal(value) {
920
+ function unWrapSignal$1(value) {
1086
921
  return isSignal(value) ? value() : value;
1087
922
  }
1088
923
 
@@ -1111,6 +946,192 @@ class ParentMap extends Map {
1111
946
  }
1112
947
  }
1113
948
 
949
+ function arrayStartsWith(list, parts) {
950
+ return (Array.isArray(parts) ? parts : [parts]).every((item, index) => item === list[index]);
951
+ }
952
+
953
+ function unWrapSignal(value) {
954
+ return isSignal(value) ? value() : value;
955
+ }
956
+
957
+ function toArray(input) {
958
+ return input === undefined
959
+ ? input
960
+ : Array.isArray(input)
961
+ ? input
962
+ : [input];
963
+ }
964
+
965
+ class SortedArray extends Array {
966
+ #compareFn;
967
+ constructor(compareFn) {
968
+ super();
969
+ this.#compareFn = compareFn;
970
+ }
971
+ push(...items) {
972
+ const result = super.push(...items);
973
+ this.sort(this.#compareFn);
974
+ return result;
975
+ }
976
+ }
977
+
978
+ const markSymbol = Symbol();
979
+ function lazyMark(fn) {
980
+ return { [markSymbol]: fn };
981
+ }
982
+ function isLazyMark(input) {
983
+ return markSymbol in input;
984
+ }
985
+ function getLazyImport(input) {
986
+ return (input[markSymbol] ??
987
+ (typeof input === 'function' ? input : undefined));
988
+ }
989
+
990
+ function createViewControlLink(fieldControl, cva, injector) {
991
+ cva.registerOnChange((value) => {
992
+ if (fieldControl().disabled$$()) {
993
+ return;
994
+ }
995
+ fieldControl().pendingStatus.update((status) => ({
996
+ ...status,
997
+ value: value,
998
+ change: true,
999
+ }));
1000
+ if (fieldControl().updateOn$$() === 'change') {
1001
+ fieldControl().viewValueChange(value);
1002
+ }
1003
+ });
1004
+ cva.registerOnTouched(() => {
1005
+ if (fieldControl().disabled$$()) {
1006
+ return;
1007
+ }
1008
+ const updateOn = fieldControl().updateOn$$();
1009
+ const pendingStatus = fieldControl().pendingStatus();
1010
+ if (updateOn === 'submit') {
1011
+ fieldControl().pendingStatus.update((status) => ({
1012
+ ...status,
1013
+ touched: true,
1014
+ }));
1015
+ }
1016
+ else {
1017
+ fieldControl().markAsTouched();
1018
+ if (pendingStatus.change && updateOn === 'blur') {
1019
+ fieldControl().viewValueChange(pendingStatus.value);
1020
+ fieldControl().pendingStatus.update((status) => ({
1021
+ ...status,
1022
+ change: false,
1023
+ value: undefined,
1024
+ }));
1025
+ }
1026
+ }
1027
+ });
1028
+ const disposeList = [];
1029
+ const ref = effect(() => {
1030
+ const value = fieldControl().modelValueToViewValue$$();
1031
+ untracked(() => {
1032
+ cva.writeValue(value);
1033
+ });
1034
+ }, { injector: injector });
1035
+ cva.writeValue(fieldControl().modelValueToViewValue$$());
1036
+ disposeList.push(() => ref.destroy());
1037
+ if (cva.setDisabledState) {
1038
+ const disabledRef = effect(() => {
1039
+ const value = fieldControl().disabled$$();
1040
+ untracked(() => {
1041
+ cva.setDisabledState(value);
1042
+ });
1043
+ }, { injector: injector });
1044
+ cva.setDisabledState(fieldControl().disabled$$());
1045
+ disposeList.push(() => disabledRef.destroy());
1046
+ }
1047
+ return (destroy) => {
1048
+ disposeList.forEach((fn) => fn());
1049
+ if (!destroy) {
1050
+ untracked(() => {
1051
+ fieldControl().updateValue(fieldControl().value$$(), true);
1052
+ });
1053
+ }
1054
+ };
1055
+ }
1056
+
1057
+ function controlStatusList(fieldControl, skipDisabled) {
1058
+ if (!fieldControl) {
1059
+ return [];
1060
+ }
1061
+ const statusList = [];
1062
+ if (!skipDisabled && fieldControl.disabled$$()) {
1063
+ statusList.push('disabled');
1064
+ }
1065
+ if (fieldControl.touched$$()) {
1066
+ statusList.push('touched');
1067
+ }
1068
+ else {
1069
+ statusList.push('untouched');
1070
+ }
1071
+ if (fieldControl.dirty$$()) {
1072
+ statusList.push('dirty');
1073
+ }
1074
+ else {
1075
+ statusList.push('pristine');
1076
+ }
1077
+ switch (fieldControl.status$$()) {
1078
+ case 'VALID':
1079
+ statusList.push('valid');
1080
+ break;
1081
+ case 'INVALID':
1082
+ statusList.push('invalid');
1083
+ break;
1084
+ case 'PENDING':
1085
+ statusList.push('pending');
1086
+ break;
1087
+ default:
1088
+ break;
1089
+ }
1090
+ return statusList;
1091
+ }
1092
+ function fieldControlStatusClass(fieldControl, skipDisabled) {
1093
+ return controlStatusList(fieldControl, skipDisabled)
1094
+ .map((item) => `pi-${item}`)
1095
+ .join(' ');
1096
+ }
1097
+
1098
+ function initListen(input, control, injector, fn) {
1099
+ let init = true;
1100
+ return effect(() => {
1101
+ const modelValue = control.value$$();
1102
+ if (init) {
1103
+ if (!deepEqual(modelValue, input)) {
1104
+ fn(modelValue);
1105
+ }
1106
+ init = false;
1107
+ }
1108
+ else {
1109
+ fn(modelValue);
1110
+ }
1111
+ }, { injector });
1112
+ }
1113
+
1114
+ function getDeepError(control) {
1115
+ if (!control) {
1116
+ return [];
1117
+ }
1118
+ const list = [];
1119
+ const controlList = [control];
1120
+ while (controlList.length) {
1121
+ const control = controlList.shift();
1122
+ if (control.valid) {
1123
+ continue;
1124
+ }
1125
+ list.push({ control, errors: control.errors });
1126
+ if (isFieldArray(control) || isFieldGroup(control)) {
1127
+ (control.activatedChildren$$ ?? control.children$$)().forEach((control) => {
1128
+ controlList.push(control);
1129
+ });
1130
+ }
1131
+ }
1132
+ return list;
1133
+ }
1134
+
1114
1135
  function* groupGenerator(list) {
1115
1136
  for (const item of list) {
1116
1137
  if (!item.keyPath?.length && item.fixedChildren?.().length) {
@@ -1577,21 +1598,21 @@ class FormBuilder {
1577
1598
  value = list
1578
1599
  .filter(Boolean)
1579
1600
  .flat()
1580
- .map((item) => unWrapSignal(item));
1601
+ .map((item) => unWrapSignal$1(item));
1581
1602
  }
1582
1603
  else {
1583
- value = list.reduce((data, item) => unWrapSignal(item) ?? data, []);
1604
+ value = list.reduce((data, item) => unWrapSignal$1(item) ?? data, []);
1584
1605
  }
1585
1606
  }
1586
1607
  else {
1587
1608
  if (strategy === 'merge') {
1588
1609
  value = list.reduce((obj, item) => ({
1589
1610
  ...obj,
1590
- ...unWrapSignal(item),
1611
+ ...unWrapSignal$1(item),
1591
1612
  }), {});
1592
1613
  }
1593
1614
  else {
1594
- value = list.reduce((data, item) => unWrapSignal(item) ?? data, {});
1615
+ value = list.reduce((data, item) => unWrapSignal$1(item) ?? data, {});
1595
1616
  }
1596
1617
  }
1597
1618
  return clone(value);
@@ -1714,7 +1735,7 @@ function patchInputs(inputs) {
1714
1735
  }
1715
1736
  function removeInputs(list) {
1716
1737
  return rawConfig((field) => {
1717
- const oldValue = unWrapSignal$1(field.inputs);
1738
+ const oldValue = unWrapSignal(field.inputs);
1718
1739
  if (!oldValue) {
1719
1740
  return;
1720
1741
  }
@@ -1816,7 +1837,7 @@ function patchAttributes(attributes) {
1816
1837
  }
1817
1838
  function removeAttributes(list) {
1818
1839
  return rawConfig((field) => {
1819
- const oldValue = unWrapSignal$1(field.attributes);
1840
+ const oldValue = unWrapSignal(field.attributes);
1820
1841
  if (!oldValue) {
1821
1842
  return;
1822
1843
  }
@@ -1843,7 +1864,7 @@ function patchProps(props) {
1843
1864
  }
1844
1865
  function removeProps(list) {
1845
1866
  return rawConfig((field) => {
1846
- const oldValue = unWrapSignal$1(field.props);
1867
+ const oldValue = unWrapSignal(field.props);
1847
1868
  if (!oldValue) {
1848
1869
  return;
1849
1870
  }
@@ -2361,11 +2382,12 @@ function convert(obj, options) {
2361
2382
  });
2362
2383
  }
2363
2384
 
2385
+ /** NonFieldControl */
2364
2386
  const NFCSchema = v.optional(v.void());
2365
2387
 
2366
2388
  /**
2367
2389
  * Generated bundle index. Do not edit.
2368
2390
  */
2369
2391
 
2370
- export { AbstractControl, CoreSchemaHandle, FieldArray, FieldControl, FieldGroup, FieldLogicGroup, FormBuilder, INVALID, NFCSchema, PENDING, PI_CONTEXT_TOKEN, PI_FORM_BUILDER_ALIAS_MAP, PI_FORM_BUILDER_OPTIONS_TOKEN, PI_VIEW_CONFIG_TOKEN, SortedArray, UpdateType, VALID, arrayStartsWith, asyncInputMerge, clone, componentClass, controlStatusList, convert, createViewControlLink, disableWhen, effectListen, fieldControlStatusClass, formConfig, getLazyImport, hideWhen, initListen, isArray, isFieldArray, isFieldControl, isFieldGroup, isFieldLogicGroup, isGroup, isLazyMark, layout, lazyMark, mergeHooks, mergeHooksFn, mergeOutputFn, mergeOutputs, nonFieldControl, outputChange, outputChangeFn, patchAsyncAttributes, patchAsyncClass, patchAsyncFn, patchAsyncInputs, patchAsyncProps, patchAsyncWrapper, patchAttributes, patchHooks, patchInputs, patchOutputs, patchProps, patchWrappers, rawConfig, removeAttributes, removeHooks, removeInputs, removeOutputs, removeProps, removeWrappers, renderConfig, setAlias, setAttributes, setComponent, setHooks, setInputs, setOutputs, setProps, setWrappers, toArray, toObservable, topClass, unWrapSignal$1 as unWrapSignal, valueChange, valueChangeFn };
2392
+ export { AbstractControl, CoreSchemaHandle, FieldArray, FieldControl, FieldGroup, FieldLogicGroup, FormBuilder, INVALID, NFCSchema, PENDING, PI_CONTEXT_TOKEN, PI_FORM_BUILDER_ALIAS_MAP, PI_FORM_BUILDER_OPTIONS_TOKEN, PI_VIEW_CONFIG_TOKEN, SortedArray, UpdateType, VALID, arrayStartsWith, asyncInputMerge, clone, componentClass, controlStatusList, convert, createViewControlLink, disableWhen, effectListen, fieldControlStatusClass, formConfig, getDeepError, getLazyImport, hideWhen, initListen, isArray, isFieldArray, isFieldControl, isFieldGroup, isFieldLogicGroup, isGroup, isLazyMark, layout, lazyMark, mergeHooks, mergeHooksFn, mergeOutputFn, mergeOutputs, nonFieldControl, outputChange, outputChangeFn, patchAsyncAttributes, patchAsyncClass, patchAsyncFn, patchAsyncInputs, patchAsyncProps, patchAsyncWrapper, patchAttributes, patchHooks, patchInputs, patchOutputs, patchProps, patchWrappers, rawConfig, removeAttributes, removeHooks, removeInputs, removeOutputs, removeProps, removeWrappers, renderConfig, setAlias, setAttributes, setComponent, setHooks, setInputs, setOutputs, setProps, setWrappers, toArray, toObservable, topClass, unWrapSignal, valueChange, valueChangeFn };
2371
2393
  //# sourceMappingURL=piying-view-angular-core.mjs.map