@piying/view-angular-core 2.3.1 → 2.4.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.
@@ -1,6 +1,6 @@
1
1
  import { signal, computed, InjectionToken, effect, inject, Injector, untracked, DestroyRef, linkedSignal, isSignal } from '@angular/core';
2
2
  import * as v from 'valibot';
3
- import { isObservable, BehaviorSubject, Subject, tap, pipe, shareReplay, combineLatest, startWith, skip, map } from 'rxjs';
3
+ import { isObservable, BehaviorSubject, Subject, tap, pipe, shareReplay, map, combineLatest, startWith, skip } from 'rxjs';
4
4
  import rfdc from 'rfdc';
5
5
  import { deepEqual } from 'fast-equals';
6
6
  import { createRawConfig, defineType, BaseSchemaHandle, convertSchema, convertCore } from '@piying/valibot-visit';
@@ -1445,10 +1445,10 @@ class FormBuilder {
1445
1445
  : typeof field.type !== 'string'
1446
1446
  ? field.type
1447
1447
  : this.#findConfig.findComponentConfig(field.type);
1448
- const inputs = field.inputs;
1449
- const outputs = field.outputs;
1450
- const attributes = field.attributes;
1451
- const events = field.events;
1448
+ const inputs = asyncObjectSignal(field.inputs);
1449
+ const outputs = asyncObjectSignal(field.outputs);
1450
+ const attributes = asyncObjectSignal(field.attributes);
1451
+ const events = asyncObjectSignal(field.events);
1452
1452
  const formConfig$ = signal(field.formConfig ?? {});
1453
1453
  const renderConfig = signal(field.renderConfig ?? {});
1454
1454
  let control;
@@ -1503,7 +1503,7 @@ class FormBuilder {
1503
1503
  origin: field,
1504
1504
  renderConfig: renderConfig,
1505
1505
  formConfig: formConfig$,
1506
- props: field.props,
1506
+ props: asyncObjectSignal(field.props),
1507
1507
  context: this.#options.context,
1508
1508
  priority: field.priority,
1509
1509
  hooks: field.hooks,
@@ -1523,7 +1523,7 @@ class FormBuilder {
1523
1523
  define: define
1524
1524
  ? signal({ type: define, inputs, outputs, attributes, events })
1525
1525
  : undefined,
1526
- wrappers: field.wrappers,
1526
+ wrappers: this.#wrapperToSignal(field.wrappers, injector),
1527
1527
  injector: injector,
1528
1528
  };
1529
1529
  resolvedConfig =
@@ -1750,6 +1750,23 @@ class FormBuilder {
1750
1750
  parent.fixedChildren().push(inputField);
1751
1751
  }
1752
1752
  #findConfig = inject(FindConfigToken);
1753
+ #wrapperToSignal(list, injector) {
1754
+ return combineSignal(list.map((item) => {
1755
+ return observableSignal({
1756
+ type: item.type,
1757
+ attributes: asyncObjectSignal(item.attributes),
1758
+ inputs: asyncObjectSignal(item.inputs),
1759
+ outputs: asyncObjectSignal(item.outputs),
1760
+ events: asyncObjectSignal(item.events),
1761
+ }, {
1762
+ pipe: pipe(map((item) => {
1763
+ const defaultWrapperConfig = this.#findConfig.findWrapperComponent(item.type);
1764
+ return { ...item, type: defaultWrapperConfig };
1765
+ })),
1766
+ injector: injector,
1767
+ });
1768
+ }));
1769
+ }
1753
1770
  }
1754
1771
  _a = FormBuilder;
1755
1772
 
@@ -1829,19 +1846,15 @@ function mergeOutputFn(field, outputs) {
1829
1846
  });
1830
1847
  }
1831
1848
  const mergeOutputs = (outputs) => rawConfig((field) => {
1832
- field.outputs.update((originOutputs) => {
1833
- originOutputs = { ...originOutputs };
1834
- for (const key in outputs) {
1835
- const oldFn = originOutputs[key];
1836
- originOutputs[key] = (...args) => {
1837
- if (oldFn) {
1838
- oldFn(...args);
1839
- }
1840
- return outputs[key](...args);
1841
- };
1842
- }
1843
- return originOutputs;
1844
- });
1849
+ for (const key in outputs) {
1850
+ const oldFn = field.outputs[key];
1851
+ field.outputs[key] = (...args) => {
1852
+ if (oldFn) {
1853
+ oldFn(...args);
1854
+ }
1855
+ return outputs[key](...args);
1856
+ };
1857
+ }
1845
1858
  });
1846
1859
  const asyncMergeOutputs = (outputs) => rawConfig((field) => {
1847
1860
  mergeHooksFn({
@@ -1986,7 +1999,7 @@ const createRemovePropertyFn = (key) => (list) => rawConfig((rawField, _, ...arg
1986
1999
  if (args.length > 0 &&
1987
2000
  typeof args[args.length - 1] === 'object' &&
1988
2001
  CustomDataSymbol in args[args.length - 1]) {
1989
- data$ = args[args.length - 1][CustomDataSymbol];
2002
+ data$ = args[args.length - 1][CustomDataSymbol](rawField);
1990
2003
  }
1991
2004
  else {
1992
2005
  data$ = (() => field);
@@ -2008,12 +2021,20 @@ function createPatchAsyncPropertyFn(key) {
2008
2021
  if (args.length > 0 &&
2009
2022
  typeof args[args.length - 1] === 'object' &&
2010
2023
  CustomDataSymbol in args[args.length - 1]) {
2011
- data$ = args[args.length - 1][CustomDataSymbol];
2024
+ data$ = args[args.length - 1][CustomDataSymbol](rawField);
2012
2025
  }
2013
2026
  else {
2014
- data$ = (() => rawField);
2027
+ data$ = rawField;
2015
2028
  }
2016
- const content$ = data$()[key];
2029
+ const content$ = {
2030
+ update: (fn) => {
2031
+ let value = fn(data$[key]);
2032
+ data$[key] = value;
2033
+ },
2034
+ set: (value) => {
2035
+ data$[key] = value;
2036
+ },
2037
+ };
2017
2038
  const inputList = Object.keys(dataObj);
2018
2039
  // 设置初始值
2019
2040
  content$.update((content) => ({
@@ -2025,6 +2046,16 @@ function createPatchAsyncPropertyFn(key) {
2025
2046
  }));
2026
2047
  return mergeHooksFn({
2027
2048
  allFieldsResolved: (field) => {
2049
+ let data$;
2050
+ if (args.length > 0 &&
2051
+ typeof args[args.length - 1] === 'object' &&
2052
+ CustomDataSymbol in args[args.length - 1]) {
2053
+ data$ = args[args.length - 1][CustomDataSymbol](undefined, field);
2054
+ }
2055
+ else {
2056
+ data$ = (() => field);
2057
+ }
2058
+ const content$ = data$()[key];
2028
2059
  Object.entries(dataObj).forEach(([key, value]) => {
2029
2060
  const result = value(field);
2030
2061
  content$.connect(key, result);
@@ -2039,12 +2070,20 @@ function createSetOrPatchPropertyFn(key, isPatch) {
2039
2070
  if (args.length > 0 &&
2040
2071
  typeof args[args.length - 1] === 'object' &&
2041
2072
  CustomDataSymbol in args[args.length - 1]) {
2042
- data$ = args[args.length - 1][CustomDataSymbol];
2073
+ data$ = args[args.length - 1][CustomDataSymbol](rawField);
2043
2074
  }
2044
2075
  else {
2045
- data$ = (() => rawField);
2076
+ data$ = rawField;
2046
2077
  }
2047
- const content$ = data$()[key];
2078
+ const content$ = {
2079
+ update: (fn) => {
2080
+ let value = fn(data$[key]);
2081
+ data$[key] = value;
2082
+ },
2083
+ set: (value) => {
2084
+ data$[key] = value;
2085
+ },
2086
+ };
2048
2087
  if (isPatch) {
2049
2088
  content$.update((data) => ({
2050
2089
  ...data,
@@ -2056,21 +2095,6 @@ function createSetOrPatchPropertyFn(key, isPatch) {
2056
2095
  }
2057
2096
  });
2058
2097
  }
2059
- function createMapPropertyFn(key) {
2060
- return (fn) => rawConfig((rawField, _, ...args) => {
2061
- let data$;
2062
- if (args.length > 0 &&
2063
- typeof args[args.length - 1] === 'object' &&
2064
- CustomDataSymbol in args[args.length - 1]) {
2065
- data$ = args[args.length - 1][CustomDataSymbol];
2066
- }
2067
- else {
2068
- data$ = (() => rawField);
2069
- }
2070
- const content$ = data$()[key];
2071
- content$.map(fn);
2072
- });
2073
- }
2074
2098
  function createMapAsyncPropertyFn(key) {
2075
2099
  return (fn) => rawConfig((rawField, _, ...args) => mergeHooksFn({
2076
2100
  allFieldsResolved: (field) => {
@@ -2078,7 +2102,7 @@ function createMapAsyncPropertyFn(key) {
2078
2102
  if (args.length > 0 &&
2079
2103
  typeof args[args.length - 1] === 'object' &&
2080
2104
  CustomDataSymbol in args[args.length - 1]) {
2081
- data$ = args[args.length - 1][CustomDataSymbol];
2105
+ data$ = args[args.length - 1][CustomDataSymbol](undefined, field);
2082
2106
  }
2083
2107
  else {
2084
2108
  data$ = (() => field);
@@ -2094,7 +2118,6 @@ const __actions = {
2094
2118
  set: createSetOrPatchPropertyFn('inputs'),
2095
2119
  patchAsync: createPatchAsyncPropertyFn('inputs'),
2096
2120
  remove: createRemovePropertyFn('inputs'),
2097
- map: createMapPropertyFn('inputs'),
2098
2121
  mapAsync: createMapAsyncPropertyFn('inputs'),
2099
2122
  },
2100
2123
  outputs: {
@@ -2104,7 +2127,6 @@ const __actions = {
2104
2127
  remove: createRemovePropertyFn('outputs'),
2105
2128
  merge: mergeOutputs,
2106
2129
  mergeAsync: asyncMergeOutputs,
2107
- map: createMapPropertyFn('outputs'),
2108
2130
  mapAsync: createMapAsyncPropertyFn('outputs'),
2109
2131
  },
2110
2132
  attributes: {
@@ -2112,7 +2134,6 @@ const __actions = {
2112
2134
  set: createSetOrPatchPropertyFn('attributes'),
2113
2135
  patchAsync: createPatchAsyncPropertyFn('attributes'),
2114
2136
  remove: createRemovePropertyFn('attributes'),
2115
- map: createMapPropertyFn('attributes'),
2116
2137
  mapAsync: createMapAsyncPropertyFn('attributes'),
2117
2138
  },
2118
2139
  events: {
@@ -2120,7 +2141,6 @@ const __actions = {
2120
2141
  set: createSetOrPatchPropertyFn('events'),
2121
2142
  patchAsync: createPatchAsyncPropertyFn('events'),
2122
2143
  remove: createRemovePropertyFn('events'),
2123
- map: createMapPropertyFn('events'),
2124
2144
  mapAsync: createMapAsyncPropertyFn('events'),
2125
2145
  },
2126
2146
  props: {
@@ -2128,7 +2148,6 @@ const __actions = {
2128
2148
  set: createSetOrPatchPropertyFn('props'),
2129
2149
  patchAsync: createPatchAsyncPropertyFn('props'),
2130
2150
  remove: createRemovePropertyFn('props'),
2131
- map: createMapPropertyFn('props'),
2132
2151
  mapAsync: createMapAsyncPropertyFn('props'),
2133
2152
  },
2134
2153
  };
@@ -2136,48 +2155,43 @@ const __actions = {
2136
2155
  function createSetOrPatchWrappersFn(isPatch) {
2137
2156
  return (wrappers) => rawConfig((rawField, _) => {
2138
2157
  const wrapperConfig = rawField.globalConfig.additionalData['defaultWrapperMetadataGroup'];
2139
- const injector = rawField.globalConfig.additionalData['injector'];
2140
- const OptionDefine = {
2141
- pipe: pipe(map((item) => {
2142
- if (typeof item.type === 'string') {
2143
- const type = wrapperConfig[item.type]?.type;
2144
- if (!type) {
2145
- throw new Error(`🈳wrapper:[${item.type}]❗`);
2146
- }
2147
- return { ...item, type: type };
2148
- }
2149
- return item;
2150
- })),
2151
- injector: injector,
2152
- };
2153
2158
  if (!isPatch) {
2154
- rawField.wrappers.clean();
2155
- }
2156
- wrappers.forEach((item) => {
2157
- if (typeof item === 'string') {
2158
- const defaultActions = wrapperConfig[item]?.actions ?? [];
2159
- const define = observableSignal({
2160
- type: item,
2161
- inputs: asyncObjectSignal({}),
2162
- outputs: asyncObjectSignal({}),
2163
- attributes: asyncObjectSignal({}),
2164
- events: asyncObjectSignal({}),
2165
- }, OptionDefine);
2166
- rawField.wrappers.add(define);
2159
+ rawField.wrappers = [];
2160
+ }
2161
+ wrappers.forEach((wrapperItem) => {
2162
+ if (typeof wrapperItem === 'string') {
2163
+ const defaultActions = wrapperConfig[wrapperItem]?.actions ?? [];
2164
+ const define = {
2165
+ type: wrapperItem,
2166
+ inputs: {},
2167
+ outputs: {},
2168
+ attributes: {},
2169
+ events: {},
2170
+ };
2171
+ rawField.wrappers.push(define);
2167
2172
  defaultActions.forEach((item) => {
2168
2173
  item.value(rawField, _, {
2169
- [CustomDataSymbol]: define,
2174
+ [CustomDataSymbol]: (rawField, field) => {
2175
+ if (rawField) {
2176
+ return define;
2177
+ }
2178
+ else {
2179
+ return field.wrappers.items().find((item) => {
2180
+ return (item.input().type === wrapperItem);
2181
+ });
2182
+ }
2183
+ },
2170
2184
  });
2171
2185
  });
2172
2186
  }
2173
2187
  else {
2174
- rawField.wrappers.add(observableSignal({
2175
- type: item.type,
2176
- inputs: asyncObjectSignal(item.inputs ?? {}),
2177
- outputs: asyncObjectSignal(item.outputs ?? {}),
2178
- attributes: asyncObjectSignal(item.attributes ?? {}),
2179
- events: asyncObjectSignal(item.events ?? {}),
2180
- }, OptionDefine));
2188
+ rawField.wrappers.push({
2189
+ type: wrapperItem.type,
2190
+ inputs: wrapperItem.inputs ?? {},
2191
+ outputs: wrapperItem.outputs ?? {},
2192
+ attributes: wrapperItem.attributes ?? {},
2193
+ events: wrapperItem.events ?? {},
2194
+ });
2181
2195
  }
2182
2196
  });
2183
2197
  });
@@ -2201,6 +2215,12 @@ function removeWrappers(removeList) {
2201
2215
  }, { position: 'bottom' }, field);
2202
2216
  });
2203
2217
  }
2218
+ function setSubInitValue(key, fn, initObj) {
2219
+ if (!Object.keys(initObj[key]).length) {
2220
+ return;
2221
+ }
2222
+ fn()[key].set(initObj[key]);
2223
+ }
2204
2224
  function patchAsyncWrapper(type, actions, options) {
2205
2225
  return rawConfig((rawFiled) => {
2206
2226
  // 在这里增加要处理的wrapper类型
@@ -2228,10 +2248,24 @@ function patchAsyncWrapper(type, actions, options) {
2228
2248
  }
2229
2249
  const allActions = [...defaultActions, ...(actions ?? [])];
2230
2250
  for (const item of allActions) {
2231
- const tempField = {};
2251
+ const tempField = {
2252
+ inputs: {},
2253
+ outputs: {},
2254
+ attributes: {},
2255
+ events: {},
2256
+ };
2232
2257
  item.value(tempField, undefined, {
2233
- [CustomDataSymbol]: initData,
2258
+ [CustomDataSymbol]: (rawField, field) => {
2259
+ if (rawField) {
2260
+ return tempField;
2261
+ }
2262
+ return initData;
2263
+ },
2234
2264
  });
2265
+ setSubInitValue('inputs', initData, tempField);
2266
+ setSubInitValue('outputs', initData, tempField);
2267
+ setSubInitValue('attributes', initData, tempField);
2268
+ setSubInitValue('events', initData, tempField);
2235
2269
  tempField.hooks?.allFieldsResolved?.(field);
2236
2270
  }
2237
2271
  },
@@ -2249,10 +2283,24 @@ function changeAsyncWrapper(indexFn, actions) {
2249
2283
  throw new Error(`change wrapper not found`);
2250
2284
  }
2251
2285
  for (const item of actions) {
2252
- const tempField = {};
2286
+ const tempField = {
2287
+ inputs: {},
2288
+ outputs: {},
2289
+ attributes: {},
2290
+ events: {},
2291
+ };
2253
2292
  item.value(tempField, undefined, {
2254
- [CustomDataSymbol]: initData,
2293
+ [CustomDataSymbol]: (rawField, field) => {
2294
+ if (rawField) {
2295
+ return tempField;
2296
+ }
2297
+ return initData;
2298
+ },
2255
2299
  });
2300
+ setSubInitValue('inputs', initData, tempField);
2301
+ setSubInitValue('outputs', initData, tempField);
2302
+ setSubInitValue('attributes', initData, tempField);
2303
+ setSubInitValue('events', initData, tempField);
2256
2304
  tempField.hooks?.allFieldsResolved?.(field);
2257
2305
  }
2258
2306
  },
@@ -2302,16 +2350,15 @@ const componentClass = (className, merge) => rawConfig((rawField, _, ...args) =>
2302
2350
  if (args.length > 0 &&
2303
2351
  typeof args[args.length - 1] === 'object' &&
2304
2352
  CustomDataSymbol in args[args.length - 1]) {
2305
- data$ = args[args.length - 1][CustomDataSymbol];
2353
+ data$ = args[args.length - 1][CustomDataSymbol](rawField);
2306
2354
  }
2307
2355
  else {
2308
- data$ = (() => rawField);
2356
+ data$ = rawField;
2309
2357
  }
2310
- const content$ = data$()['attributes'];
2311
- content$.update((data) => ({
2312
- ...data,
2313
- class: merge ? clsx(data?.['class'], className) : clsx(className),
2314
- }));
2358
+ const content$ = data$['attributes'];
2359
+ content$['class'] = merge
2360
+ ? clsx(content$['class'], className)
2361
+ : clsx(className);
2315
2362
  });
2316
2363
  const bottomClass = componentClass;
2317
2364
  function patchAsyncClass(fn) {
@@ -2377,12 +2424,12 @@ const actions = {
2377
2424
  };
2378
2425
 
2379
2426
  class CoreSchemaHandle extends BaseSchemaHandle {
2380
- inputs = asyncObjectSignal({});
2381
- outputs = asyncObjectSignal({});
2382
- attributes = asyncObjectSignal({});
2383
- events = asyncObjectSignal({});
2384
- wrappers = combineSignal([]);
2385
- props = asyncObjectSignal({});
2427
+ inputs = {};
2428
+ outputs = {};
2429
+ attributes = {};
2430
+ events = {};
2431
+ wrappers = [];
2432
+ props = {};
2386
2433
  alias;
2387
2434
  movePath;
2388
2435
  renderConfig;
@@ -2471,16 +2518,10 @@ class CoreSchemaHandle extends BaseSchemaHandle {
2471
2518
  this.formConfig.groupMode = 'reset';
2472
2519
  }
2473
2520
  enumSchema(schema) {
2474
- this.props.update((data) => ({
2475
- ...data,
2476
- options: data['options'] ?? schema.options,
2477
- }));
2521
+ this.props['options'] = this.props['options'] ?? schema.options;
2478
2522
  }
2479
2523
  updateProps(key, value) {
2480
- this.props.update((data) => ({
2481
- ...data,
2482
- [key]: value,
2483
- }));
2524
+ this.props[key] = value;
2484
2525
  }
2485
2526
  intersectBefore(schema) {
2486
2527
  if (this.childrenAsVirtualGroup) {
@@ -2535,7 +2576,8 @@ class CoreSchemaHandle extends BaseSchemaHandle {
2535
2576
  }
2536
2577
  end(schema) {
2537
2578
  super.end(schema);
2538
- this.formConfig.defaultValue = this.defaultValue;
2579
+ this.formConfig.defaultValue =
2580
+ this.defaultValue ?? (this.nullable ? null : undefined);
2539
2581
  }
2540
2582
  }
2541
2583