jedison 1.19.0 → 1.20.1

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.
@@ -1910,7 +1910,7 @@ class Instance extends EventEmitter {
1910
1910
  }
1911
1911
  }
1912
1912
  /**
1913
- * Return the last part of the instance path
1913
+ * Return the last part of the instance JSON Pointer
1914
1914
  */
1915
1915
  getKey() {
1916
1916
  return this.key;
@@ -1922,7 +1922,7 @@ class Instance extends EventEmitter {
1922
1922
  return this.schema;
1923
1923
  }
1924
1924
  /**
1925
- * Adds a child instance pointer to the instance list
1925
+ * Adds a child instance reference to the instance list
1926
1926
  */
1927
1927
  register() {
1928
1928
  this.jedison.register(this);
@@ -1936,7 +1936,7 @@ class Instance extends EventEmitter {
1936
1936
  this.children.forEach(registerChildRecursive);
1937
1937
  }
1938
1938
  /**
1939
- * Deletes a child instance pointer from the instance list
1939
+ * Deletes a child instance reference from the instance list
1940
1940
  */
1941
1941
  unregister() {
1942
1942
  this.jedison.unregister(this);
@@ -2228,9 +2228,11 @@ class Editor {
2228
2228
  this.theme = this.instance.jedison.theme;
2229
2229
  this.markdownEnabled = getSchemaXOption(this.instance.schema, "parseMarkdown") ?? this.instance.jedison.getOption("parseMarkdown");
2230
2230
  this.purifyEnabled = getSchemaXOption(this.instance.schema, "purifyHtml") ?? this.instance.jedison.getOption("purifyHtml");
2231
+ this.markdownCache = /* @__PURE__ */ new Map();
2232
+ this.purifyCache = /* @__PURE__ */ new Map();
2231
2233
  }
2232
2234
  /**
2233
- * Gets the json path level by counting how many "/" it has
2235
+ * Gets the JSON Pointer level by counting how many "/" it has
2234
2236
  */
2235
2237
  getLevel() {
2236
2238
  return (this.instance.path.match(/\//g) || []).length;
@@ -2462,7 +2464,13 @@ class Editor {
2462
2464
  */
2463
2465
  purifyContent(content, domPurifyOptions) {
2464
2466
  if (this.instance.jedison.getOption("purifyHtml") && typeof window !== "undefined" && window.DOMPurify) {
2465
- return window.DOMPurify.sanitize(content, domPurifyOptions);
2467
+ const cacheKey = JSON.stringify([content, domPurifyOptions]);
2468
+ if (this.purifyCache.has(cacheKey)) {
2469
+ return this.purifyCache.get(cacheKey);
2470
+ }
2471
+ const clean = window.DOMPurify.sanitize(content, domPurifyOptions);
2472
+ this.purifyCache.set(cacheKey, clean);
2473
+ return clean;
2466
2474
  } else {
2467
2475
  const tmp = document.createElement("div");
2468
2476
  tmp.innerHTML = content;
@@ -2470,7 +2478,12 @@ class Editor {
2470
2478
  }
2471
2479
  }
2472
2480
  getHtmlFromMarkdown(content) {
2473
- return window.marked.parse(content);
2481
+ if (this.markdownCache.has(content)) {
2482
+ return this.markdownCache.get(content);
2483
+ }
2484
+ const html = window.marked.parse(content);
2485
+ this.markdownCache.set(content, html);
2486
+ return html;
2474
2487
  }
2475
2488
  getTitle() {
2476
2489
  let titleFromSchema = false;
@@ -3883,6 +3896,10 @@ class EditorStringAwesomplete extends EditorString {
3883
3896
  this.theme.adaptForHorizontalInputControl(this.control, labelCol, inputCol);
3884
3897
  }
3885
3898
  addEventListeners() {
3899
+ const eventType = this.getValidationEventType();
3900
+ this.control.input.addEventListener(eventType, () => {
3901
+ this.instance.setValue(this.control.input.value, true, "user");
3902
+ });
3886
3903
  this.control.input.addEventListener("awesomplete-selectcomplete", () => {
3887
3904
  this.instance.setValue(this.control.input.value, true, "user");
3888
3905
  });
@@ -5672,6 +5689,121 @@ class EditorArrayChoices extends Editor {
5672
5689
  super.destroy();
5673
5690
  }
5674
5691
  }
5692
+ class EditorArrayTomSelect extends Editor {
5693
+ static resolves(schema) {
5694
+ const hasTomSelectFormat = getSchemaXOption(schema, "format") === "tom-select";
5695
+ const tomSelectInstalled = window.TomSelect;
5696
+ const schemaType = getSchemaType(schema);
5697
+ const schemaItems = getSchemaItems(schema);
5698
+ const schemaItemsType = isSet(schemaItems) && getSchemaType(schemaItems);
5699
+ const isArrayType = isSet(schemaType) && schemaType === "array";
5700
+ const isUniqueItems = getSchemaUniqueItems(schema) === true;
5701
+ const hasTypes = isSet(schemaItems) && isSet(schemaItemsType);
5702
+ const validTypes = ["string", "number", "integer"];
5703
+ const hasValidItemType = isSet(schemaItems) && isSet(schemaItemsType) && (validTypes.includes(schemaItemsType) || isArray(schemaItemsType) && schemaItemsType.some((type2) => validTypes.includes(type2)));
5704
+ return hasTomSelectFormat && tomSelectInstalled && isArrayType && isUniqueItems && hasTypes && hasValidItemType;
5705
+ }
5706
+ init() {
5707
+ super.init();
5708
+ this.setupEnumSource();
5709
+ }
5710
+ setupEnumSource() {
5711
+ const enumSourceRaw = getSchemaXOption(this.instance.schema, "enumSource");
5712
+ if (!isSet(enumSourceRaw)) return;
5713
+ const enumSource = resolveInstancePath(this.instance.path, enumSourceRaw);
5714
+ const src = this.instance.jedison.getInstance(enumSource);
5715
+ if (src) this.enumSourceValues = src.getValue();
5716
+ this.instance.jedison.watch(enumSource, () => {
5717
+ if (!this.control) return;
5718
+ const s = this.instance.jedison.getInstance(enumSource);
5719
+ if (s) {
5720
+ this.enumSourceValues = s.getValue();
5721
+ this.refreshOptions();
5722
+ }
5723
+ });
5724
+ }
5725
+ getEnumSourceValues() {
5726
+ if (this.enumSourceValues !== void 0) {
5727
+ if (isArray(this.enumSourceValues)) return this.enumSourceValues;
5728
+ if (isObject$1(this.enumSourceValues)) return Object.keys(this.enumSourceValues);
5729
+ return [];
5730
+ }
5731
+ return this.instance.schema.items && this.instance.schema.items.enum || [];
5732
+ }
5733
+ refreshOptions() {
5734
+ if (!this.tomSelectInstance) return;
5735
+ const values = this.getEnumSourceValues();
5736
+ const currentValue = this.instance.getValue();
5737
+ const itemEnumTitles = getSchemaXOption(this.instance.schema.items || {}, "enumTitles") || [];
5738
+ const options = values.map((item, index2) => ({
5739
+ value: item,
5740
+ text: itemEnumTitles[index2] || item
5741
+ }));
5742
+ this.tomSelectInstance.clearOptions();
5743
+ this.tomSelectInstance.addOptions(options);
5744
+ this.tomSelectInstance.setValue(isArray(currentValue) ? currentValue : [], true);
5745
+ }
5746
+ build() {
5747
+ this.control = this.theme.getSelectControl({
5748
+ title: this.getTitle(),
5749
+ description: this.getDescription(),
5750
+ values: [],
5751
+ titles: [],
5752
+ id: this.getIdFromPath(this.instance.path),
5753
+ titleIconClass: getSchemaXOption(this.instance.schema, "titleIconClass"),
5754
+ titleHidden: getSchemaXOption(this.instance.schema, "titleHidden"),
5755
+ info: this.getInfo()
5756
+ });
5757
+ this.control.input.setAttribute("multiple", "");
5758
+ try {
5759
+ const value = this.instance.getValue();
5760
+ const itemEnum = this.getEnumSourceValues();
5761
+ const itemEnumTitles = getSchemaXOption(this.instance.schema.items || {}, "enumTitles") || [];
5762
+ const tomSelectOptions = getSchemaXOption(this.instance.schema, "tomSelectOptions") ?? {};
5763
+ if (this.tomSelectInstance) {
5764
+ this.tomSelectInstance.destroy();
5765
+ }
5766
+ this.options = itemEnum.map((item, index2) => ({
5767
+ value: item,
5768
+ text: itemEnumTitles[index2] || item
5769
+ }));
5770
+ this.tomSelectInstance = new window.TomSelect(this.control.input, {
5771
+ plugins: ["drag_drop", "remove_button", "caret_position"],
5772
+ options: this.options,
5773
+ items: isArray(value) ? value : [],
5774
+ ...tomSelectOptions
5775
+ });
5776
+ } catch (e) {
5777
+ console.error("Tom Select is not available or not loaded correctly.", e);
5778
+ }
5779
+ }
5780
+ adaptForHorizontal(labelCol, inputCol) {
5781
+ this.theme.adaptForHorizontalSelectControl(this.control, labelCol, inputCol);
5782
+ }
5783
+ addEventListeners() {
5784
+ this.tomSelectInstance.on("change", (value) => {
5785
+ if (JSON.stringify(value) !== JSON.stringify(this.instance.getValue())) {
5786
+ this.instance.setValue(value, true, "user");
5787
+ }
5788
+ });
5789
+ }
5790
+ refreshDisabledState() {
5791
+ if (this.disabled || this.readOnly) {
5792
+ this.tomSelectInstance.disable();
5793
+ } else {
5794
+ this.tomSelectInstance.enable();
5795
+ }
5796
+ }
5797
+ refreshUI() {
5798
+ super.refreshUI();
5799
+ const value = this.instance.getValue();
5800
+ this.tomSelectInstance.setValue(isArray(value) ? value : [], true);
5801
+ }
5802
+ destroy() {
5803
+ this.tomSelectInstance.destroy();
5804
+ super.destroy();
5805
+ }
5806
+ }
5675
5807
  class EditorArrayNav extends EditorArray {
5676
5808
  static resolves(schema) {
5677
5809
  const format2 = getSchemaXOption(schema, "format");
@@ -6973,6 +7105,7 @@ class UiResolver {
6973
7105
  EditorObjectRadios,
6974
7106
  EditorObject,
6975
7107
  EditorArrayChoices,
7108
+ EditorArrayTomSelect,
6976
7109
  EditorArrayCheckboxes,
6977
7110
  EditorArrayTuple,
6978
7111
  EditorArrayTableObject,
@@ -7519,13 +7652,13 @@ class Jedison extends EventEmitter {
7519
7652
  this.hiddenInput.value = JSON.stringify(this.getValue());
7520
7653
  }
7521
7654
  /**
7522
- * Adds a child instance pointer to the instances list
7655
+ * Adds a child instance reference to the instances list
7523
7656
  */
7524
7657
  register(instance) {
7525
7658
  this.instances.set(instance.path, instance);
7526
7659
  }
7527
7660
  /**
7528
- * Deletes a child instance pointer from the instances list
7661
+ * Deletes a child instance reference from the instances list
7529
7662
  */
7530
7663
  unregister(instance) {
7531
7664
  this.instances.delete(instance.path);
@@ -7681,7 +7814,7 @@ class Jedison extends EventEmitter {
7681
7814
  this.updateInstancesWatchedData();
7682
7815
  }
7683
7816
  /**
7684
- * Returns an instance by path
7817
+ * Returns an instance by JSON Pointer
7685
7818
  * @return {*}
7686
7819
  */
7687
7820
  getInstance(path) {
@@ -7700,8 +7833,8 @@ class Jedison extends EventEmitter {
7700
7833
  return this.options[canonical];
7701
7834
  }
7702
7835
  /**
7703
- * Navigates to a specific instance by path, activating any ancestor nav/categories tabs as needed.
7704
- * @param {string} path - The instance path (e.g. '#/address/street')
7836
+ * Navigates to a specific instance by JSON Pointer, activating any ancestor nav/categories tabs as needed.
7837
+ * @param {string} path - The instance JSON Pointer (e.g. '#/address/street')
7705
7838
  */
7706
7839
  navigateTo(path) {
7707
7840
  if (!this.isEditor) return;
@@ -7798,7 +7931,9 @@ class Jedison extends EventEmitter {
7798
7931
  class RefParser {
7799
7932
  constructor(options = {}) {
7800
7933
  this.options = Object.assign({
7801
- detectRecursion: true
7934
+ detectRecursion: true,
7935
+ fetch: typeof fetch === "function" ? fetch.bind(globalThis) : void 0,
7936
+ fetchOptions: {}
7802
7937
  }, options);
7803
7938
  this.refs = {};
7804
7939
  this.data = {};
@@ -7832,7 +7967,7 @@ class RefParser {
7832
7967
  }
7833
7968
  /**
7834
7969
  * Traverses the given schema recursively and for each schema with $ref
7835
- * add a new property in the this.refs object with key being the json path to that schema.
7970
+ * add a new property in the this.refs object with key being the JSON Pointer to that schema.
7836
7971
  * If the ref has no value in data will be given a value of null. This value will be later
7837
7972
  * replaced in a future iteration. At that time the data will be available
7838
7973
  * @param schema
@@ -7938,13 +8073,15 @@ class RefParser {
7938
8073
  }
7939
8074
  }
7940
8075
  /**
7941
- * Loads a schema with a synchronous http request
8076
+ * Loads a schema over HTTP. Uses options.fetch (defaults to the global fetch) and
8077
+ * options.fetchOptions, so callers needing auth (e.g. forwarding a session cookie
8078
+ * server-side) can supply headers/credentials, or swap in a custom fetch entirely.
7942
8079
  * @param uri
7943
8080
  * @returns {any}
7944
8081
  */
7945
8082
  async load(uri) {
7946
8083
  try {
7947
- const response = await fetch(uri);
8084
+ const response = await this.options.fetch(uri, this.options.fetchOptions);
7948
8085
  if (!response.ok) {
7949
8086
  throw new Error("Network response was not ok");
7950
8087
  }
@@ -11996,6 +12133,7 @@ const index = {
11996
12133
  EditorObjectRadios,
11997
12134
  EditorObject,
11998
12135
  EditorArrayChoices,
12136
+ EditorArrayTomSelect,
11999
12137
  EditorArrayNav,
12000
12138
  EditorArray,
12001
12139
  EditorMultiple,