@stubber/form-fields 2.0.13 → 2.0.14

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.
@@ -12,8 +12,12 @@ export let dynamic_field_type = false;
12
12
  setContext("dynamic_field_type", dynamic_field_type);
13
13
  let fields = [];
14
14
  let form_errors = {};
15
- $: rebuild_fields(initial_form, form);
16
15
  const rebuild_fields = (initial_form2, form2) => {
16
+ if (last_initial_form == initial_form2 && last_form == form2) {
17
+ return;
18
+ }
19
+ last_initial_form = initial_form2;
20
+ last_form = form2;
17
21
  if (initial_form2.data) {
18
22
  form2.update((data) => {
19
23
  return { ...data, ...initial_form2.data };
@@ -21,6 +25,9 @@ const rebuild_fields = (initial_form2, form2) => {
21
25
  }
22
26
  fields = build_fields(form2, attachments, dependencies, initial_form2.spec.fields);
23
27
  };
28
+ let last_initial_form = null;
29
+ let last_form = null;
30
+ $: rebuild_fields(initial_form, form);
24
31
  $: validate_form($form);
25
32
  const validate_form = async (_) => {
26
33
  form_errors = {};
@@ -36,12 +43,7 @@ const validate_form = async (_) => {
36
43
  </script>
37
44
 
38
45
  <div class="flex flex-col gap-y-2">
39
- {#key form}
40
- {#key initial_form}
41
- <!-- {count++} -->
42
- {#each fields as fieldStore (getStoreValue(fieldStore).id)}
43
- <FormField {fieldStore} />
44
- {/each}
45
- {/key}
46
- {/key}
46
+ {#each fields as fieldStore (getStoreValue(fieldStore).id)}
47
+ <FormField {fieldStore} />
48
+ {/each}
47
49
  </div>
@@ -68,7 +68,6 @@ import { file_field_param_spec } from "./file-field.svelte";
68
68
  import { heading_field_param_spec } from "./heading-field.svelte";
69
69
  import { hidden_field_param_spec } from "./hidden-field.svelte";
70
70
  import { html_field_param_spec } from "./html-field.svelte";
71
- import JsonEditorBound from "./json-editor-bound.svelte";
72
71
  import { map_field_param_spec } from "./map-field.svelte";
73
72
  import { smart_text_field_param_spec } from "./smart-text-field.svelte";
74
73
  import { code_field_param_spec } from "./code-field.svelte";
@@ -85,6 +84,8 @@ import { max_files_param_spec } from "./screenrecorder-field.svelte";
85
84
  import { scroll_and_read_display_field_param_spec } from "./scroll-and-read-display-field.svelte";
86
85
  import { select_field_param_spec } from "./select-field.svelte";
87
86
  import { select_resource_field_param_spec } from "./selectresource-field.svelte";
87
+ import { JSONEditor } from "svelte-jsoneditor";
88
+ import { handle_editor_change_setter, handle_store_change } from "../../utils/json-editor-sync";
88
89
  export let fieldStore;
89
90
  let value = cloneDeep($fieldStore.value) || {
90
91
  details: {},
@@ -96,6 +97,8 @@ if (!value.details) {
96
97
  if (!value.initvalue) {
97
98
  value.initvalue = {};
98
99
  }
100
+ let default_initvalue_editor;
101
+ let override_initvalue_editor;
99
102
  $: update_fieldStore(value);
100
103
  function update_fieldStore(new_value) {
101
104
  const cleaned_value = omitBy(
@@ -116,6 +119,8 @@ function update_value(new_value) {
116
119
  };
117
120
  update_params_from_value(value.params);
118
121
  update_subfields_from_value(value.fields);
122
+ handle_store_change(value.initvalue.default, default_initvalue_editor);
123
+ handle_store_change(value.initvalue.override, override_initvalue_editor);
119
124
  }
120
125
  }
121
126
  const field_types = Object.keys(fields).map((key) => ({
@@ -467,14 +472,22 @@ let validations_open = false;
467
472
  <Checkbox id="has_default" bind:checked={value.initvalue.has_default} />
468
473
  <Label for="has_default">Has Default</Label>
469
474
  </div>
470
- <JsonEditorBound bind:value={value.initvalue.default} />
475
+ <JSONEditor
476
+ bind:this={default_initvalue_editor}
477
+ onChange={(content) =>
478
+ handle_editor_change_setter(content, (new_val) => (value.initvalue.default = new_val))}
479
+ />
471
480
  </div>
472
481
  <div>
473
482
  <div class="flex w-full items-center space-x-2">
474
483
  <Checkbox id="has_override" bind:checked={value.initvalue.has_override} />
475
484
  <Label for="has_override">Has Override</Label>
476
485
  </div>
477
- <JsonEditorBound bind:value={value.initvalue.override} />
486
+ <JSONEditor
487
+ bind:this={override_initvalue_editor}
488
+ onChange={(content) =>
489
+ handle_editor_change_setter(content, (new_val) => (value.initvalue.override = new_val))}
490
+ />
478
491
  </div>
479
492
  </Collapsible.Content>
480
493
  </Collapsible.Root>
@@ -11,13 +11,27 @@ export const jsoneditor_field_param_spec = {
11
11
  };
12
12
  </script>
13
13
 
14
- <script>import FieldLabel from "../FieldLabel.svelte";
14
+ <script>import {
15
+ determine_content,
16
+ handle_editor_change_setter,
17
+ handle_store_change
18
+ } from "../../utils/json-editor-sync";
19
+ import { JSONEditor } from "svelte-jsoneditor";
20
+ import FieldLabel from "../FieldLabel.svelte";
15
21
  import FieldMessage from "../FieldMessage.svelte";
16
- import JsonEditorBound from "./json-editor-bound.svelte";
17
22
  export let fieldStore;
23
+ let initial_content = determine_content($fieldStore.value);
24
+ let jsoneditor;
25
+ $: handle_store_change($fieldStore.value, jsoneditor);
18
26
  let read_only = $fieldStore.params?.readOnly ?? false;
19
27
  </script>
20
28
 
21
29
  <FieldLabel {fieldStore} />
22
- <JsonEditorBound bind:value={$fieldStore.value} readOnly={read_only} />
30
+ <JSONEditor
31
+ bind:this={jsoneditor}
32
+ content={initial_content}
33
+ readOnly={read_only}
34
+ onChange={(content) =>
35
+ handle_editor_change_setter(content, (new_val) => ($fieldStore.value = new_val))}
36
+ />
23
37
  <FieldMessage {fieldStore} />
@@ -0,0 +1,6 @@
1
+ import { JSONEditor, type Content } from "svelte-jsoneditor";
2
+ import type { Writable } from "svelte/store";
3
+ export declare const determine_content: (value: any) => Content;
4
+ export declare const handle_store_change: (value: any, jsoneditor: JSONEditor) => void;
5
+ export declare const handle_editor_change: (new_content: Content, store: Writable<any>) => void;
6
+ export declare const handle_editor_change_setter: (new_content: Content, setter: (value: any) => void) => void;
@@ -0,0 +1,77 @@
1
+ import { cloneDeep, isEqual } from "lodash-es";
2
+ import { JSONEditor } from "svelte-jsoneditor";
3
+ export const determine_content = (value) => {
4
+ if (typeof value === "string") {
5
+ return { text: value, json: undefined };
6
+ }
7
+ else if (typeof value === "object") {
8
+ return { text: undefined, json: value };
9
+ }
10
+ return { text: undefined, json: undefined };
11
+ };
12
+ export const handle_store_change = (value, jsoneditor) => {
13
+ if (jsoneditor) {
14
+ const current_content = jsoneditor.get();
15
+ let effective_content;
16
+ if ("text" in current_content && current_content.text !== undefined) {
17
+ try {
18
+ effective_content = JSON.parse(current_content.text);
19
+ }
20
+ catch (e) {
21
+ effective_content = current_content.text;
22
+ }
23
+ }
24
+ else if ("json" in current_content && current_content.json !== undefined) {
25
+ effective_content = current_content.json;
26
+ }
27
+ if (isEqual(effective_content, value)) {
28
+ // console.log("earliest return", { effective_content, value });
29
+ return;
30
+ }
31
+ if (value === undefined && effective_content === undefined) {
32
+ // console.log("early return");
33
+ return;
34
+ }
35
+ if (value === undefined) {
36
+ jsoneditor.updateProps({ content: { text: "" } });
37
+ // console.log("cleared jsoneditor");
38
+ return;
39
+ }
40
+ jsoneditor.update({ json: cloneDeep(value) });
41
+ // console.log("updated jsoneditor with value:", value);
42
+ }
43
+ };
44
+ export const handle_editor_change = (new_content, store) => {
45
+ if ("text" in new_content && new_content.text !== undefined) {
46
+ try {
47
+ const value = JSON.parse(new_content.text);
48
+ store.set(value);
49
+ }
50
+ catch (e) {
51
+ const value = new_content.text;
52
+ store.set(value);
53
+ }
54
+ }
55
+ else if ("json" in new_content && new_content.json !== undefined) {
56
+ const value = cloneDeep(new_content.json);
57
+ store.set(value);
58
+ // console.log("updated store with value:", value);
59
+ }
60
+ };
61
+ export const handle_editor_change_setter = (new_content, setter) => {
62
+ if ("text" in new_content && new_content.text !== undefined) {
63
+ try {
64
+ const value = JSON.parse(new_content.text);
65
+ setter(value);
66
+ }
67
+ catch (e) {
68
+ const value = new_content.text;
69
+ setter(value);
70
+ }
71
+ }
72
+ else if ("json" in new_content && new_content.json !== undefined) {
73
+ const value = cloneDeep(new_content.json);
74
+ setter(value);
75
+ // console.log("updated store with value:", value);
76
+ }
77
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@stubber/form-fields",
3
- "version": "2.0.13",
3
+ "version": "2.0.14",
4
4
  "description": "An automatic form builder based on field specifications",
5
5
  "keywords": [
6
6
  "components",
@@ -25,6 +25,7 @@
25
25
  "dev": "vite dev --host",
26
26
  "build": "vite build",
27
27
  "package": "svelte-kit sync && svelte-package && publint",
28
+ "package:watch": "svelte-kit sync && svelte-package --watch",
28
29
  "preview": "vite preview",
29
30
  "check": "svelte-kit sync && svelte-check --tsconfig ./jsconfig.json",
30
31
  "check:watch": "svelte-kit sync && svelte-check --tsconfig ./jsconfig.json --watch",
@@ -1,50 +0,0 @@
1
- <script>import { cloneDeep, isEqual } from "lodash-es";
2
- import { JSONEditor } from "svelte-jsoneditor";
3
- export let value;
4
- let jsoneditor;
5
- $: update_content(value);
6
- function update_content(new_value) {
7
- let new_content = null;
8
- if (new_value === void 0) {
9
- return;
10
- }
11
- if (!jsoneditor) {
12
- return;
13
- }
14
- const content = jsoneditor.get();
15
- if (typeof new_value === "string") {
16
- if ("text" in content && content.text !== new_value) {
17
- new_content = { text: new_value };
18
- } else if ("json" in content) {
19
- new_content = { text: new_value };
20
- }
21
- } else if (new_value !== void 0) {
22
- try {
23
- const current_json = "json" in content ? content.json : JSON.parse(content.text);
24
- if (!isEqual(current_json, new_value)) {
25
- new_content = { json: cloneDeep(new_value) };
26
- }
27
- } catch (e) {
28
- new_content = { json: cloneDeep(new_value) };
29
- }
30
- }
31
- if (new_content !== null) {
32
- console.log("update_content set content to", new_content);
33
- jsoneditor.set(new_content);
34
- }
35
- }
36
- function handleChange(new_content) {
37
- if ("text" in new_content && new_content.text !== void 0) {
38
- try {
39
- value = JSON.parse(new_content.text);
40
- } catch (e) {
41
- value = new_content.text;
42
- }
43
- } else if ("json" in new_content && new_content.json !== void 0) {
44
- value = cloneDeep(new_content.json);
45
- }
46
- console.log("handleChange set value to", value);
47
- }
48
- </script>
49
-
50
- <JSONEditor bind:this={jsoneditor} onChange={handleChange} {...$$restProps} />
@@ -1,66 +0,0 @@
1
- import { SvelteComponent } from "svelte";
2
- import { type Content } from "svelte-jsoneditor";
3
- declare const __propDef: {
4
- props: {
5
- content?: Content;
6
- selection?: import("svelte-jsoneditor").JSONEditorSelection | undefined;
7
- readOnly?: boolean;
8
- indentation?: number | string;
9
- tabSize?: number;
10
- mode?: import("svelte-jsoneditor").Mode;
11
- mainMenuBar?: boolean;
12
- navigationBar?: boolean;
13
- statusBar?: boolean;
14
- askToFormat?: boolean;
15
- escapeControlCharacters?: boolean;
16
- escapeUnicodeCharacters?: boolean;
17
- flattenColumns?: boolean;
18
- parser?: import("svelte-jsoneditor").JSONParser;
19
- validator?: import("svelte-jsoneditor").Validator | undefined;
20
- validationParser?: import("svelte-jsoneditor").JSONParser;
21
- pathParser?: import("svelte-jsoneditor").JSONPathParser;
22
- queryLanguages?: import("svelte-jsoneditor").QueryLanguage[];
23
- queryLanguageId?: string;
24
- onChangeQueryLanguage?: import("svelte-jsoneditor").OnChangeQueryLanguage;
25
- onChange?: import("svelte-jsoneditor").OnChange | undefined;
26
- onSelect?: import("svelte-jsoneditor").OnSelect | undefined;
27
- onRenderValue?: import("svelte-jsoneditor").OnRenderValue;
28
- onClassName?: import("svelte-jsoneditor").OnClassName;
29
- onRenderMenu?: import("svelte-jsoneditor").OnRenderMenu;
30
- onRenderContextMenu?: import("svelte-jsoneditor").OnRenderContextMenu;
31
- onChangeMode?: import("svelte-jsoneditor").OnChangeMode;
32
- onError?: import("svelte-jsoneditor").OnError;
33
- onFocus?: import("svelte-jsoneditor").OnFocus;
34
- onBlur?: import("svelte-jsoneditor").OnBlur;
35
- get?: () => Content;
36
- set?: (newContent: Content) => Promise<void>;
37
- update?: (updatedContent: Content) => Promise<void>;
38
- patch?: (operations: import("immutable-json-patch").JSONPatchDocument) => Promise<import("svelte-jsoneditor").JSONPatchResult>;
39
- select?: (newSelection: import("svelte-jsoneditor").JSONEditorSelection | undefined) => Promise<void>;
40
- expand?: (path: import("immutable-json-patch").JSONPath, callback?: import("svelte-jsoneditor").OnExpand) => Promise<void>;
41
- collapse?: (path: import("immutable-json-patch").JSONPath, recursive?: boolean) => Promise<void>;
42
- transform?: (options: import("svelte-jsoneditor").TransformModalOptions) => void;
43
- validate?: () => import("svelte-jsoneditor").ContentErrors | undefined;
44
- acceptAutoRepair?: () => Promise<Content>;
45
- scrollTo?: (path: import("immutable-json-patch").JSONPath) => Promise<void>;
46
- findElement?: (path: import("immutable-json-patch").JSONPath) => Element | undefined;
47
- focus?: () => Promise<void>;
48
- refresh?: () => Promise<void>;
49
- updateProps?: (props: import("svelte-jsoneditor").JSONEditorPropsOptional) => Promise<void>;
50
- destroy?: () => Promise<void>;
51
- } & {
52
- value: any;
53
- };
54
- events: {
55
- [evt: string]: CustomEvent<any>;
56
- };
57
- slots: {};
58
- exports?: {} | undefined;
59
- bindings?: string | undefined;
60
- };
61
- export type JsonEditorBoundProps = typeof __propDef.props;
62
- export type JsonEditorBoundEvents = typeof __propDef.events;
63
- export type JsonEditorBoundSlots = typeof __propDef.slots;
64
- export default class JsonEditorBound extends SvelteComponent<JsonEditorBoundProps, JsonEditorBoundEvents, JsonEditorBoundSlots> {
65
- }
66
- export {};