Skip to content

Commit 885c895

Browse files
Move loadExternalResource to utils.ts
1 parent aae5527 commit 885c895

File tree

4 files changed

+32
-31
lines changed

4 files changed

+32
-31
lines changed

src/message.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* @module message
44
*/
55

6-
import randomSelection from './utils.js';
6+
import { randomSelection } from './utils.js';
77

88
let messageTimer: NodeJS.Timeout | null = null;
99

src/model.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*/
55

66
import showMessage from './message.js';
7-
import randomSelection from './utils.js';
7+
import { randomSelection } from './utils.js';
88
import Model from './live2d/index.js';
99
import logger from './logger.js';
1010

src/utils.ts

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,30 @@ function randomSelection(obj: any) {
1212
return Array.isArray(obj) ? obj[Math.floor(Math.random() * obj.length)] : obj;
1313
}
1414

15-
export default randomSelection;
15+
/**
16+
* 异步加载外部资源。
17+
* @param {string} url - 资源路径。
18+
* @param {string} type - 资源类型。
19+
*/
20+
function loadExternalResource(url: string, type: string): Promise<string> {
21+
return new Promise((resolve: any, reject: any) => {
22+
let tag;
23+
24+
if (type === 'css') {
25+
tag = document.createElement('link');
26+
tag.rel = 'stylesheet';
27+
tag.href = url;
28+
}
29+
else if (type === 'js') {
30+
tag = document.createElement('script');
31+
tag.src = url;
32+
}
33+
if (tag) {
34+
tag.onload = () => resolve(url);
35+
tag.onerror = () => reject(url);
36+
document.head.appendChild(tag);
37+
}
38+
});
39+
}
40+
41+
export { randomSelection, loadExternalResource };

src/widget.ts

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import ModelManager from './model.js';
77
import showMessage from './message.js';
8-
import randomSelection from './utils.js';
8+
import { randomSelection } from './utils.js';
99
import tools from './tools.js';
1010
import logger from './logger.js';
1111
import registerDrag from './drag.js';
@@ -237,30 +237,5 @@ function initWidget(config: string | Config, apiPath?: string) {
237237
}
238238
}
239239

240-
/**
241-
* 异步加载外部资源。
242-
* @param {string} url - 资源路径。
243-
* @param {string} type - 资源类型。
244-
*/
245-
function loadExternalResource(url: string, type: string): Promise<string> {
246-
return new Promise((resolve: any, reject: any) => {
247-
let tag;
248-
249-
if (type === 'css') {
250-
tag = document.createElement('link');
251-
tag.rel = 'stylesheet';
252-
tag.href = url;
253-
}
254-
else if (type === 'js') {
255-
tag = document.createElement('script');
256-
tag.src = url;
257-
}
258-
if (tag) {
259-
tag.onload = () => resolve(url);
260-
tag.onerror = () => reject(url);
261-
document.head.appendChild(tag);
262-
}
263-
});
264-
}
265-
266-
export { initWidget, loadExternalResource };
240+
export { initWidget };
241+
export { loadExternalResource } from './utils.js';

0 commit comments

Comments
 (0)