@tmagic/utils 1.0.0-beta.6 → 1.0.0-rc.2

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.
@@ -2,7 +2,7 @@ import { MNode } from '@tmagic/schema';
2
2
  export declare const sleep: (ms: number) => Promise<void>;
3
3
  export declare const datetimeFormatter: (v: string | Date, defaultValue?: string, f?: string) => string | number;
4
4
  export declare const asyncLoadJs: (url: string, crossOrigin?: string | undefined, document?: Document) => any;
5
- export declare const asyncLoadCss: (url: string) => Promise<unknown>;
5
+ export declare const asyncLoadCss: (url: string, document?: Document) => any;
6
6
  export declare const toLine: (name?: string) => string;
7
7
  export declare const toHump: (name?: string) => string;
8
8
  export declare const emptyFn: () => any;
@@ -16,3 +16,5 @@ export declare const getNodePath: (id: number | string, data?: MNode[]) => MNode
16
16
  export declare const filterXSS: (str: string) => string;
17
17
  export declare const getUrlParam: (param: string, url?: string | undefined) => string;
18
18
  export declare const isPop: (node: MNode) => boolean;
19
+ export declare const isPage: (node: MNode) => boolean;
20
+ export declare const isNumber: (value: string) => boolean;
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "1.0.0-beta.6",
2
+ "version": "1.0.0-rc.2",
3
3
  "name": "@tmagic/utils",
4
4
  "main": "dist/tmagic-utils.umd.js",
5
5
  "module": "dist/tmagic-utils.es.js",
@@ -10,8 +10,11 @@
10
10
  "require": "./dist/tmagic-utils.umd.js"
11
11
  }
12
12
  },
13
+ "license": "Apache-2.0",
13
14
  "scripts": {
14
- "build": "vite build"
15
+ "build": "vite build",
16
+ "test": "jest --maxWorkers=8",
17
+ "test:coverage": "jest --maxWorkers=16 --coverage"
15
18
  },
16
19
  "engines": {
17
20
  "node": ">=14"
@@ -20,10 +23,20 @@
20
23
  "type": "git",
21
24
  "url": "https://github.com/Tencent/tmagic-editor.git"
22
25
  },
26
+ "dependencies": {
27
+ "@tmagic/schema": "^1.0.0-rc.2",
28
+ "moment": "^2.29.2"
29
+ },
23
30
  "devDependencies": {
31
+ "@types/jest": "^27.5.1",
24
32
  "@types/node": "^15.12.4",
33
+ "jest": "^27.5.1",
34
+ "jest-serializer-vue": "^2.0.2",
35
+ "jest-transform-stub": "^2.0.0",
36
+ "ts-jest": "^27.1.4",
25
37
  "typescript": "^4.3.4",
26
38
  "vite": "^2.3.7",
27
39
  "vite-plugin-dts": "^0.9.6"
28
- }
40
+ },
41
+ "gitHead": "3e309780ed33640ab3fc986a93dd0e7b15c167e5"
29
42
  }
package/src/index.ts CHANGED
@@ -18,7 +18,7 @@
18
18
 
19
19
  import moment from 'moment';
20
20
 
21
- import { MNode } from '@tmagic/schema';
21
+ import { MNode, NodeType } from '@tmagic/schema';
22
22
 
23
23
  export const sleep = (ms: number): Promise<void> =>
24
24
  new Promise((resolve) => {
@@ -97,22 +97,44 @@ export const asyncLoadJs = (() => {
97
97
  };
98
98
  })();
99
99
 
100
- export const asyncLoadCss = function (url: string) {
101
- return new Promise((resolve, reject) => {
102
- const hasLoaded = globalThis.document.querySelector(`link[href="${url}"]`);
103
- if (hasLoaded) {
104
- resolve(undefined);
105
- return;
100
+ export const asyncLoadCss = (() => {
101
+ // 正在加载或加载成功的存入此Map中
102
+ const documentMap = new Map();
103
+
104
+ return (url: string, document = globalThis.document) => {
105
+ let loaded = documentMap.get(document);
106
+ if (!loaded) {
107
+ loaded = new Map();
108
+ documentMap.set(document, loaded);
106
109
  }
107
110
 
108
- const node = document.createElement('link');
109
- node.rel = 'stylesheet';
110
- node.href = url;
111
- document.getElementsByTagName('head')[0].appendChild(node);
112
- node.onload = resolve;
113
- node.onerror = reject;
114
- });
115
- };
111
+ // 正在加载或已经加载成功的,直接返回
112
+ if (loaded.get(url)) return loaded.get(url);
113
+
114
+ const load = new Promise<void>((resolve, reject) => {
115
+ const node = document.createElement('link');
116
+ node.rel = 'stylesheet';
117
+ node.href = url;
118
+ document.head.appendChild(node);
119
+ node.onload = () => {
120
+ resolve();
121
+ };
122
+ node.onerror = () => {
123
+ reject(new Error('加载失败'));
124
+ };
125
+ setTimeout(() => {
126
+ reject(new Error('timeout'));
127
+ }, 60 * 1000);
128
+ }).catch((err) => {
129
+ // 加载失败的,从map中移除,第二次加载时,可以再次执行加载
130
+ loaded.delete(url);
131
+ throw err;
132
+ });
133
+
134
+ loaded.set(url, load);
135
+ return loaded.get(url);
136
+ };
137
+ })();
116
138
 
117
139
  // 驼峰转换横线
118
140
  export const toLine = (name = '') => name.replace(/\B([A-Z])/g, '-$1').toLowerCase();
@@ -181,4 +203,8 @@ export const getUrlParam = (param: string, url?: string) => {
181
203
  return '';
182
204
  };
183
205
 
184
- export const isPop = (node: MNode): boolean => node.type.toLowerCase().endsWith('pop');
206
+ export const isPop = (node: MNode): boolean => Boolean(node.type?.toLowerCase().endsWith('pop'));
207
+
208
+ export const isPage = (node: MNode): boolean => Boolean(node.type?.toLowerCase() === NodeType.PAGE);
209
+
210
+ export const isNumber = (value: string) => /^(-?\d+)(\.\d+)?$/.test(value);
package/tsconfig.json DELETED
@@ -1,9 +0,0 @@
1
- {
2
- "extends": "../../tsconfig.json",
3
- "compilerOptions": {
4
- "baseUrl": "../..",
5
- },
6
- "exclude": [
7
- "**/dist/**/*"
8
- ],
9
- }
package/vite.config.ts DELETED
@@ -1,45 +0,0 @@
1
- /*
2
- * Tencent is pleased to support the open source community by making TMagicEditor available.
3
- *
4
- * Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved.
5
- *
6
- * Licensed under the Apache License, Version 2.0 (the "License");
7
- * you may not use this file except in compliance with the License.
8
- * You may obtain a copy of the License at
9
- *
10
- * http://www.apache.org/licenses/LICENSE-2.0
11
- *
12
- * Unless required by applicable law or agreed to in writing, software
13
- * distributed under the License is distributed on an "AS IS" BASIS,
14
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
- * See the License for the specific language governing permissions and
16
- * limitations under the License.
17
- */
18
-
19
- import { defineConfig } from 'vite';
20
- import dts from 'vite-plugin-dts';
21
-
22
- export default defineConfig({
23
- plugins: [
24
- dts({
25
- outputDir: 'dist/types',
26
- include: ['src/**/*'],
27
- staticImport: true,
28
- insertTypesEntry: true,
29
- logDiagnostics: true,
30
- }),
31
- ],
32
-
33
- build: {
34
- cssCodeSplit: false,
35
- sourcemap: true,
36
- minify: false,
37
- target: 'esnext',
38
-
39
- lib: {
40
- entry: 'src/index.ts',
41
- name: 'TMagicUtils',
42
- fileName: 'tmagic-utils',
43
- },
44
- },
45
- });