@tmagic/utils 1.4.4 → 1.4.6

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.
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "1.4.4",
2
+ "version": "1.4.6",
3
3
  "name": "@tmagic/utils",
4
4
  "type": "module",
5
5
  "main": "dist/tmagic-utils.umd.cjs",
@@ -25,23 +25,28 @@
25
25
  "url": "https://github.com/Tencent/tmagic-editor.git"
26
26
  },
27
27
  "dependencies": {
28
- "@tmagic/schema": "1.4.4",
29
- "dayjs": "^1.11.4",
28
+ "dayjs": "^1.11.11",
30
29
  "lodash-es": "^4.17.21"
31
30
  },
32
- "peerDependencies": {
33
- "dayjs": "^1.11.4"
34
- },
35
31
  "devDependencies": {
36
32
  "@types/lodash-es": "^4.17.4",
37
33
  "@types/node": "^18.19.0",
38
34
  "rimraf": "^3.0.2",
39
- "typescript": "^5.4.2",
40
- "vite": "^5.1.6"
35
+ "vite": "^5.2.11"
36
+ },
37
+ "peerDependencies": {
38
+ "typescript": "*",
39
+ "@tmagic/schema": "1.4.6"
40
+ },
41
+ "peerDependenciesMeta": {
42
+ "typescript": {
43
+ "optional": true
44
+ }
41
45
  },
42
46
  "scripts": {
43
47
  "build": "rimraf ./dist && npm run build:type && vite build --mode=es && vite build --mode=umd",
44
48
  "build:type": "npm run clear:type && tsc --declaration --emitDeclarationOnly --project tsconfig.build.json",
45
- "clear:type": "rimraf ./types"
49
+ "clear:type": "rimraf ./types",
50
+ "check:type": "tsc --noEmit --project tsconfig.build.json"
46
51
  }
47
52
  }
package/src/dom.ts CHANGED
@@ -110,3 +110,15 @@ export const createDiv = ({ className, cssText }: { className: string; cssText:
110
110
  };
111
111
 
112
112
  export const getDocument = () => globalThis.document;
113
+
114
+ export const calcValueByFontsize = (doc: Document | undefined, value: number) => {
115
+ if (!doc) return value;
116
+ const { fontSize } = doc.documentElement.style;
117
+
118
+ if (fontSize) {
119
+ const times = globalThis.parseFloat(fontSize) / 100;
120
+ return Number((value / times).toFixed(2));
121
+ }
122
+
123
+ return value;
124
+ };
package/src/index.ts CHANGED
@@ -25,8 +25,6 @@ import { NodeType } from '@tmagic/schema';
25
25
 
26
26
  export * from './dom';
27
27
 
28
- dayjs.extend(utc);
29
-
30
28
  export const sleep = (ms: number): Promise<void> =>
31
29
  new Promise((resolve) => {
32
30
  const timer = setTimeout(() => {
@@ -45,6 +43,7 @@ export const datetimeFormatter = (
45
43
  if (['x', 'timestamp'].includes(format)) {
46
44
  time = dayjs(v).valueOf();
47
45
  } else if ((typeof v === 'string' && v.includes('Z')) || v.constructor === Date) {
46
+ dayjs.extend(utc);
48
47
  // UTC字符串时间或Date对象格式化为北京时间
49
48
  time = dayjs(v).utcOffset(8).format(format);
50
49
  } else {
@@ -165,9 +164,12 @@ export const guid = (digit = 8): string =>
165
164
  return v.toString(16);
166
165
  });
167
166
 
168
- export const getValueByKeyPath: any = (keys: string | string[] = '', data: Record<string | number, any> = {}) => {
167
+ export const getValueByKeyPath = (
168
+ keys: number | string | string[] = '',
169
+ data: Record<string | number, any> = {},
170
+ ): any => {
169
171
  // 将 array[0] 转成 array.0
170
- const keyArray = Array.isArray(keys) ? keys : keys.replaceAll(/\[(\d+)\]/g, '.$1').split('.');
172
+ const keyArray = Array.isArray(keys) ? keys : `${keys}`.replaceAll(/\[(\d+)\]/g, '.$1').split('.');
171
173
  return keyArray.reduce((accumulator, currentValue: any) => {
172
174
  if (isObject(accumulator) || Array.isArray(accumulator)) {
173
175
  return accumulator[currentValue];
@@ -177,7 +179,7 @@ export const getValueByKeyPath: any = (keys: string | string[] = '', data: Recor
177
179
  }, data);
178
180
  };
179
181
 
180
- export const setValueByKeyPath: any = (keys: string, value: any, data: Record<string | number, any> = {}) =>
182
+ export const setValueByKeyPath = (keys: string | number, value: any, data: Record<string | number, any> = {}): any =>
181
183
  objectSet(data, keys, value);
182
184
 
183
185
  export const getNodes = (ids: Id[], data: MNode[] = []): MNode[] => {
@@ -390,6 +392,8 @@ export const getDefaultValueFromFields = (fields: DataSchema[]) => {
390
392
 
391
393
  export const DATA_SOURCE_FIELDS_SELECT_VALUE_PREFIX = 'ds-field::';
392
394
 
395
+ export const DATA_SOURCE_FIELDS_CHANGE_EVENT_PREFIX = 'ds-field-changed';
396
+
393
397
  export const getKeys = Object.keys as <T extends object>(obj: T) => Array<keyof T>;
394
398
 
395
399
  export const calculatePercentage = (value: number, percentageStr: string) => {
package/types/dom.d.ts CHANGED
@@ -9,3 +9,4 @@ export declare const createDiv: ({ className, cssText }: {
9
9
  cssText: string;
10
10
  }) => HTMLDivElement;
11
11
  export declare const getDocument: () => Document;
12
+ export declare const calcValueByFontsize: (doc: Document | undefined, value: number) => number;
package/types/index.d.ts CHANGED
@@ -27,8 +27,8 @@ export declare const isSameDomain: (targetUrl?: string, source?: string) => bool
27
27
  * @returns
28
28
  */
29
29
  export declare const guid: (digit?: number) => string;
30
- export declare const getValueByKeyPath: any;
31
- export declare const setValueByKeyPath: any;
30
+ export declare const getValueByKeyPath: (keys?: number | string | string[], data?: Record<string | number, any>) => any;
31
+ export declare const setValueByKeyPath: (keys: string | number, value: any, data?: Record<string | number, any>) => any;
32
32
  export declare const getNodes: (ids: Id[], data?: MNode[]) => MNode[];
33
33
  export declare const getDepKeys: (dataSourceDeps: DataSourceDeps | undefined, nodeId: Id) => Id[];
34
34
  export declare const getDepNodeIds: (dataSourceDeps?: DataSourceDeps) => string[];
@@ -44,6 +44,7 @@ export declare const compiledNode: (compile: (value: any) => any, node: MNode, d
44
44
  export declare const compiledCond: (op: string, fieldValue: any, inputValue: any, range?: number[]) => boolean;
45
45
  export declare const getDefaultValueFromFields: (fields: DataSchema[]) => Record<string, any>;
46
46
  export declare const DATA_SOURCE_FIELDS_SELECT_VALUE_PREFIX = "ds-field::";
47
+ export declare const DATA_SOURCE_FIELDS_CHANGE_EVENT_PREFIX = "ds-field-changed";
47
48
  export declare const getKeys: <T extends object>(obj: T) => Array<keyof T>;
48
49
  export declare const calculatePercentage: (value: number, percentageStr: string) => number;
49
50
  export declare const isPercentage: (value: number | string) => boolean;