@tmagic/utils 1.5.0-beta.14 → 1.5.0-beta.16

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.
@@ -115,6 +115,8 @@ const getIdFromEl = () => dslDomRelateConfig.getIdFromEl;
115
115
  const getElById = () => dslDomRelateConfig.getElById;
116
116
  const setIdToEl = () => dslDomRelateConfig.setIdToEl;
117
117
 
118
+ let _globalThis;
119
+ const getGlobalThis = () => _globalThis || (_globalThis = typeof globalThis !== "undefined" ? globalThis : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : typeof global !== "undefined" ? global : {});
118
120
  const sleep = (ms) => new Promise((resolve) => {
119
121
  const timer = setTimeout(() => {
120
122
  clearTimeout(timer);
@@ -122,7 +124,7 @@ const sleep = (ms) => new Promise((resolve) => {
122
124
  }, ms);
123
125
  });
124
126
  const toLine = (name = "") => name.replace(/\B([A-Z])/g, "-$1").toLowerCase();
125
- const toHump = (name = "") => name.replace(/-(\w)/g, (all, letter) => letter.toUpperCase());
127
+ const toHump = (name = "") => name.replace(/-(\w)/g, (_all, letter) => letter.toUpperCase());
126
128
  const emptyFn = () => void 0;
127
129
  const getNodePath = (id, data = []) => {
128
130
  const path = [];
@@ -408,17 +410,17 @@ const convertToNumber = (value, parentValue = 0) => {
408
410
  }
409
411
  return parseFloat(value);
410
412
  };
411
- const addParamToUrl = (obj, global = globalThis, needReload = true) => {
412
- const url = new URL(global.location.href);
413
+ const addParamToUrl = (obj, global2 = globalThis, needReload = true) => {
414
+ const url = new URL(global2.location.href);
413
415
  const { searchParams } = url;
414
416
  for (const [k, v] of Object.entries(obj)) {
415
417
  searchParams.set(k, v);
416
418
  }
417
419
  const newUrl = url.toString();
418
420
  if (needReload) {
419
- global.location.href = newUrl;
421
+ global2.location.href = newUrl;
420
422
  } else {
421
- global.history.pushState({}, "", url);
423
+ global2.history.pushState({}, "", url);
422
424
  }
423
425
  };
424
426
  const dataSourceTemplateRegExp = /\$\{([\s\S]+?)\}/g;
@@ -432,5 +434,20 @@ const traverseNode = (node, cb, parents = []) => {
432
434
  });
433
435
  }
434
436
  };
437
+ const isValueIncludeDataSource = (value) => {
438
+ if (typeof value === "string" && /\$\{([\s\S]+?)\}/.test(value)) {
439
+ return true;
440
+ }
441
+ if (Array.isArray(value) && `${value[0]}`.startsWith(DATA_SOURCE_FIELDS_SELECT_VALUE_PREFIX)) {
442
+ return true;
443
+ }
444
+ if (value?.isBindDataSource && value.dataSourceId) {
445
+ return true;
446
+ }
447
+ if (value?.isBindDataSourceField && value.dataSourceId) {
448
+ return true;
449
+ }
450
+ return false;
451
+ };
435
452
 
436
- export { DATA_SOURCE_FIELDS_CHANGE_EVENT_PREFIX, DATA_SOURCE_FIELDS_SELECT_VALUE_PREFIX, DSL_NODE_KEY_COPY_PREFIX, IS_DSL_NODE_KEY, addClassName, addParamToUrl, asyncLoadCss, asyncLoadJs, calcValueByFontsize, calculatePercentage, compiledCond, compiledNode, convertToNumber, createDiv, dataSourceTemplateRegExp, emptyFn, filterXSS, getDefaultValueFromFields, getDepKeys, getDepNodeIds, getDocument, getElById, getHost, getIdFromEl, getKeys, getKeysArray, getNodeInfo, getNodePath, getNodes, getUrlParam, getValueByKeyPath, guid, injectStyle, isDslNode, isNumber, isObject, isPage, isPageFragment, isPercentage, isPop, isSameDomain, removeClassName, removeClassNameByClassName, replaceChildNode, setDslDomRelateConfig, setIdToEl, setValueByKeyPath, sleep, toHump, toLine, traverseNode };
453
+ export { DATA_SOURCE_FIELDS_CHANGE_EVENT_PREFIX, DATA_SOURCE_FIELDS_SELECT_VALUE_PREFIX, DSL_NODE_KEY_COPY_PREFIX, IS_DSL_NODE_KEY, addClassName, addParamToUrl, asyncLoadCss, asyncLoadJs, calcValueByFontsize, calculatePercentage, compiledCond, compiledNode, convertToNumber, createDiv, dataSourceTemplateRegExp, emptyFn, filterXSS, getDefaultValueFromFields, getDepKeys, getDepNodeIds, getDocument, getElById, getGlobalThis, getHost, getIdFromEl, getKeys, getKeysArray, getNodeInfo, getNodePath, getNodes, getUrlParam, getValueByKeyPath, guid, injectStyle, isDslNode, isNumber, isObject, isPage, isPageFragment, isPercentage, isPop, isSameDomain, isValueIncludeDataSource, removeClassName, removeClassNameByClassName, replaceChildNode, setDslDomRelateConfig, setIdToEl, setValueByKeyPath, sleep, toHump, toLine, traverseNode };
@@ -2724,6 +2724,8 @@
2724
2724
  const getElById = () => dslDomRelateConfig.getElById;
2725
2725
  const setIdToEl = () => dslDomRelateConfig.setIdToEl;
2726
2726
 
2727
+ let _globalThis;
2728
+ const getGlobalThis = () => _globalThis || (_globalThis = typeof globalThis !== "undefined" ? globalThis : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : typeof global !== "undefined" ? global : {});
2727
2729
  const sleep = (ms) => new Promise((resolve) => {
2728
2730
  const timer = setTimeout(() => {
2729
2731
  clearTimeout(timer);
@@ -2731,7 +2733,7 @@
2731
2733
  }, ms);
2732
2734
  });
2733
2735
  const toLine = (name = "") => name.replace(/\B([A-Z])/g, "-$1").toLowerCase();
2734
- const toHump = (name = "") => name.replace(/-(\w)/g, (all, letter) => letter.toUpperCase());
2736
+ const toHump = (name = "") => name.replace(/-(\w)/g, (_all, letter) => letter.toUpperCase());
2735
2737
  const emptyFn = () => void 0;
2736
2738
  const getNodePath = (id, data = []) => {
2737
2739
  const path = [];
@@ -3017,17 +3019,17 @@
3017
3019
  }
3018
3020
  return parseFloat(value);
3019
3021
  };
3020
- const addParamToUrl = (obj, global = globalThis, needReload = true) => {
3021
- const url = new URL(global.location.href);
3022
+ const addParamToUrl = (obj, global2 = globalThis, needReload = true) => {
3023
+ const url = new URL(global2.location.href);
3022
3024
  const { searchParams } = url;
3023
3025
  for (const [k, v] of Object.entries(obj)) {
3024
3026
  searchParams.set(k, v);
3025
3027
  }
3026
3028
  const newUrl = url.toString();
3027
3029
  if (needReload) {
3028
- global.location.href = newUrl;
3030
+ global2.location.href = newUrl;
3029
3031
  } else {
3030
- global.history.pushState({}, "", url);
3032
+ global2.history.pushState({}, "", url);
3031
3033
  }
3032
3034
  };
3033
3035
  const dataSourceTemplateRegExp = /\$\{([\s\S]+?)\}/g;
@@ -3041,6 +3043,21 @@
3041
3043
  });
3042
3044
  }
3043
3045
  };
3046
+ const isValueIncludeDataSource = (value) => {
3047
+ if (typeof value === "string" && /\$\{([\s\S]+?)\}/.test(value)) {
3048
+ return true;
3049
+ }
3050
+ if (Array.isArray(value) && `${value[0]}`.startsWith(DATA_SOURCE_FIELDS_SELECT_VALUE_PREFIX)) {
3051
+ return true;
3052
+ }
3053
+ if (value?.isBindDataSource && value.dataSourceId) {
3054
+ return true;
3055
+ }
3056
+ if (value?.isBindDataSourceField && value.dataSourceId) {
3057
+ return true;
3058
+ }
3059
+ return false;
3060
+ };
3044
3061
 
3045
3062
  exports.DATA_SOURCE_FIELDS_CHANGE_EVENT_PREFIX = DATA_SOURCE_FIELDS_CHANGE_EVENT_PREFIX;
3046
3063
  exports.DATA_SOURCE_FIELDS_SELECT_VALUE_PREFIX = DATA_SOURCE_FIELDS_SELECT_VALUE_PREFIX;
@@ -3064,6 +3081,7 @@
3064
3081
  exports.getDepNodeIds = getDepNodeIds;
3065
3082
  exports.getDocument = getDocument;
3066
3083
  exports.getElById = getElById;
3084
+ exports.getGlobalThis = getGlobalThis;
3067
3085
  exports.getHost = getHost;
3068
3086
  exports.getIdFromEl = getIdFromEl;
3069
3087
  exports.getKeys = getKeys;
@@ -3083,6 +3101,7 @@
3083
3101
  exports.isPercentage = isPercentage;
3084
3102
  exports.isPop = isPop;
3085
3103
  exports.isSameDomain = isSameDomain;
3104
+ exports.isValueIncludeDataSource = isValueIncludeDataSource;
3086
3105
  exports.removeClassName = removeClassName;
3087
3106
  exports.removeClassNameByClassName = removeClassNameByClassName;
3088
3107
  exports.replaceChildNode = replaceChildNode;
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "1.5.0-beta.14",
2
+ "version": "1.5.0-beta.16",
3
3
  "name": "@tmagic/utils",
4
4
  "type": "module",
5
5
  "main": "dist/tmagic-utils.umd.cjs",
@@ -22,6 +22,7 @@
22
22
  "node": ">=18"
23
23
  },
24
24
  "repository": {
25
+ "directory": "packages/utils",
25
26
  "type": "git",
26
27
  "url": "https://github.com/Tencent/tmagic-editor.git"
27
28
  },
@@ -32,11 +33,11 @@
32
33
  "@types/lodash-es": "^4.17.4",
33
34
  "@types/node": "^18.19.0",
34
35
  "rimraf": "^3.0.2",
35
- "vite": "^5.4.3"
36
+ "vite": "^5.4.10"
36
37
  },
37
38
  "peerDependencies": {
38
39
  "typescript": "*",
39
- "@tmagic/schema": "1.5.0-beta.14"
40
+ "@tmagic/schema": "1.5.0-beta.16"
40
41
  },
41
42
  "peerDependenciesMeta": {
42
43
  "typescript": {
package/src/index.ts CHANGED
@@ -1,3 +1,4 @@
1
+ /* eslint-disable no-nested-ternary */
1
2
  /*
2
3
  * Tencent is pleased to support the open source community by making TMagicEditor available.
3
4
  *
@@ -36,6 +37,23 @@ import type { EditorNodeInfo } from '@editor/type';
36
37
 
37
38
  export * from './dom';
38
39
 
40
+ // for typeof global checks without @types/node
41
+ declare let global: {};
42
+
43
+ let _globalThis: any;
44
+ export const getGlobalThis = (): any =>
45
+ _globalThis ||
46
+ (_globalThis =
47
+ typeof globalThis !== 'undefined'
48
+ ? globalThis
49
+ : typeof self !== 'undefined'
50
+ ? self
51
+ : typeof window !== 'undefined'
52
+ ? window
53
+ : typeof global !== 'undefined'
54
+ ? global
55
+ : {});
56
+
39
57
  export const sleep = (ms: number): Promise<void> =>
40
58
  new Promise((resolve) => {
41
59
  const timer = setTimeout(() => {
@@ -47,7 +65,7 @@ export const sleep = (ms: number): Promise<void> =>
47
65
  // 驼峰转换横线
48
66
  export const toLine = (name = '') => name.replace(/\B([A-Z])/g, '-$1').toLowerCase();
49
67
 
50
- export const toHump = (name = ''): string => name.replace(/-(\w)/g, (all, letter) => letter.toUpperCase());
68
+ export const toHump = (name = ''): string => name.replace(/-(\w)/g, (_all, letter) => letter.toUpperCase());
51
69
 
52
70
  export const emptyFn = (): any => undefined;
53
71
 
@@ -91,7 +109,7 @@ export const getNodePath = (id: Id, data: MNode[] = []): MNode[] => {
91
109
  return path;
92
110
  };
93
111
 
94
- export const getNodeInfo = (id: Id, root: MApp | null) => {
112
+ export const getNodeInfo = (id: Id, root: Pick<MApp, 'id' | 'items'> | null) => {
95
113
  const info: EditorNodeInfo = {
96
114
  node: null,
97
115
  parent: null,
@@ -491,3 +509,19 @@ export const traverseNode = <T extends NodeItem = NodeItem>(
491
509
  });
492
510
  }
493
511
  };
512
+
513
+ export const isValueIncludeDataSource = (value: any) => {
514
+ if (typeof value === 'string' && /\$\{([\s\S]+?)\}/.test(value)) {
515
+ return true;
516
+ }
517
+ if (Array.isArray(value) && `${value[0]}`.startsWith(DATA_SOURCE_FIELDS_SELECT_VALUE_PREFIX)) {
518
+ return true;
519
+ }
520
+ if (value?.isBindDataSource && value.dataSourceId) {
521
+ return true;
522
+ }
523
+ if (value?.isBindDataSourceField && value.dataSourceId) {
524
+ return true;
525
+ }
526
+ return false;
527
+ };
package/types/index.d.ts CHANGED
@@ -29,6 +29,7 @@ declare const getIdFromEl: () => (el?: HTMLElement | SVGElement | null) => strin
29
29
  declare const getElById: () => (doc?: Document, id?: string | number) => HTMLElement;
30
30
  declare const setIdToEl: () => (el: HTMLElement | SVGElement, id: string | number) => void;
31
31
 
32
+ declare const getGlobalThis: () => any;
32
33
  declare const sleep: (ms: number) => Promise<void>;
33
34
  declare const toLine: (name?: string) => string;
34
35
  declare const toHump: (name?: string) => string;
@@ -40,7 +41,7 @@ declare const emptyFn: () => any;
40
41
  * @return {Array} 组件在data中的子孙路径
41
42
  */
42
43
  declare const getNodePath: (id: Id, data?: MNode$1[]) => MNode$1[];
43
- declare const getNodeInfo: (id: Id, root: MApp | null) => EditorNodeInfo;
44
+ declare const getNodeInfo: (id: Id, root: Pick<MApp, "id" | "items"> | null) => EditorNodeInfo;
44
45
  declare const filterXSS: (str: string) => string;
45
46
  declare const getUrlParam: (param: string, url?: string) => string;
46
47
  declare const isObject: (obj: any) => boolean;
@@ -94,5 +95,6 @@ interface NodeItem {
94
95
  [key: string]: any;
95
96
  }
96
97
  declare const traverseNode: <T extends NodeItem = NodeItem>(node: T, cb: (node: T, parents: T[]) => void, parents?: T[]) => void;
98
+ declare const isValueIncludeDataSource: (value: any) => boolean;
97
99
 
98
- export { DATA_SOURCE_FIELDS_CHANGE_EVENT_PREFIX, DATA_SOURCE_FIELDS_SELECT_VALUE_PREFIX, DSL_NODE_KEY_COPY_PREFIX, IS_DSL_NODE_KEY, type NodeItem, addClassName, addParamToUrl, asyncLoadCss, asyncLoadJs, calcValueByFontsize, calculatePercentage, compiledCond, compiledNode, convertToNumber, createDiv, dataSourceTemplateRegExp, emptyFn, filterXSS, getDefaultValueFromFields, getDepKeys, getDepNodeIds, getDocument, getElById, getHost, getIdFromEl, getKeys, getKeysArray, getNodeInfo, getNodePath, getNodes, getUrlParam, getValueByKeyPath, guid, injectStyle, isDslNode, isNumber, isObject, isPage, isPageFragment, isPercentage, isPop, isSameDomain, removeClassName, removeClassNameByClassName, replaceChildNode, setDslDomRelateConfig, setIdToEl, setValueByKeyPath, sleep, toHump, toLine, traverseNode };
100
+ export { DATA_SOURCE_FIELDS_CHANGE_EVENT_PREFIX, DATA_SOURCE_FIELDS_SELECT_VALUE_PREFIX, DSL_NODE_KEY_COPY_PREFIX, IS_DSL_NODE_KEY, type NodeItem, addClassName, addParamToUrl, asyncLoadCss, asyncLoadJs, calcValueByFontsize, calculatePercentage, compiledCond, compiledNode, convertToNumber, createDiv, dataSourceTemplateRegExp, emptyFn, filterXSS, getDefaultValueFromFields, getDepKeys, getDepNodeIds, getDocument, getElById, getGlobalThis, getHost, getIdFromEl, getKeys, getKeysArray, getNodeInfo, getNodePath, getNodes, getUrlParam, getValueByKeyPath, guid, injectStyle, isDslNode, isNumber, isObject, isPage, isPageFragment, isPercentage, isPop, isSameDomain, isValueIncludeDataSource, removeClassName, removeClassNameByClassName, replaceChildNode, setDslDomRelateConfig, setIdToEl, setValueByKeyPath, sleep, toHump, toLine, traverseNode };