Skip to content

Commit 689b5c4

Browse files
Add/Fix: Add JSDoc for function and type and fix bugs (#175)
* Fix the missing initWidget See #176 * Fix the check of invalid model id See #182 --------- Co-authored-by: Mimi <[email protected]>
1 parent d1cc37a commit 689b5c4

25 files changed

+276
-49
lines changed
File renamed without changes.

build/index.js renamed to build/initWidget.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ function loadWidget(config) {
77
localStorage.removeItem('waifu-display');
88
sessionStorage.removeItem('waifu-text');
99
document.body.insertAdjacentHTML('beforeend', "<div id=\"waifu\">\n <div id=\"waifu-tips\"></div>\n <canvas id=\"live2d\" width=\"800\" height=\"800\"></canvas>\n <div id=\"waifu-tool\"></div>\n </div>");
10-
// https://stackoverflow.com/questions/24148403/trigger-css-transition-on-appended-element
1110
setTimeout(function () {
1211
document.getElementById('waifu').style.bottom = '0';
1312
}, 0);
@@ -32,7 +31,6 @@ function loadWidget(config) {
3231
})();
3332
function welcomeMessage(time) {
3433
if (location.pathname === '/') {
35-
// 如果是主页
3634
for (var _i = 0, time_1 = time; _i < time_1.length; _i++) {
3735
var _a = time_1[_i], hour = _a.hour, text_1 = _a.text;
3836
var now = new Date(), after = hour.split('-')[0], before = hour.split('-')[1] || after;
@@ -62,7 +60,6 @@ function loadWidget(config) {
6260
return text;
6361
}
6462
function registerEventListener(result) {
65-
// Detect user activity and display messages when idle
6663
var userAction = false;
6764
var userActionTimer;
6865
var messageArray = result.message.default;
@@ -137,10 +134,9 @@ function loadWidget(config) {
137134
(function initModel() {
138135
var modelId = Number(localStorage.getItem('modelId'));
139136
var modelTexturesId = Number(localStorage.getItem('modelTexturesId'));
140-
if (modelId === null) {
141-
// 首次访问加载 指定模型 的 指定材质
142-
modelId = 1; // 模型 ID
143-
modelTexturesId = 53; // 材质 ID
137+
if (!modelId) {
138+
modelId = 1;
139+
modelTexturesId = 53;
144140
}
145141
void model.loadModel(modelId, modelTexturesId, '');
146142
fetch(config.waifuPath)

build/message.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
1-
import randomSelection from "./utils.js";
1+
import randomSelection from './utils.js';
22
var messageTimer = null;
33
function showMessage(text, timeout, priority) {
4-
if (!text || (sessionStorage.getItem("waifu-text") && Number(sessionStorage.getItem("waifu-text")) > priority))
4+
if (!text ||
5+
(sessionStorage.getItem('waifu-text') &&
6+
Number(sessionStorage.getItem('waifu-text')) > priority))
57
return;
68
if (messageTimer) {
79
clearTimeout(messageTimer);
810
messageTimer = null;
911
}
1012
text = randomSelection(text);
11-
sessionStorage.setItem("waifu-text", String(priority));
12-
var tips = document.getElementById("waifu-tips");
13+
sessionStorage.setItem('waifu-text', String(priority));
14+
var tips = document.getElementById('waifu-tips');
1315
tips.innerHTML = text;
14-
tips.classList.add("waifu-tips-active");
16+
tips.classList.add('waifu-tips-active');
1517
messageTimer = setTimeout(function () {
16-
sessionStorage.removeItem("waifu-text");
17-
tips.classList.remove("waifu-tips-active");
18+
sessionStorage.removeItem('waifu-text');
19+
tips.classList.remove('waifu-tips-active');
1820
}, timeout);
1921
}
2022
export default showMessage;

build/model.js

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
3636
};
3737
import showMessage from './message.js';
3838
import randomSelection from './utils.js';
39-
var Model = /** @class */ (function () {
39+
var Model = (function () {
4040
function Model(config) {
4141
this.modelList = null;
4242
var apiPath = config.apiPath, cdnPath = config.cdnPath;
@@ -62,14 +62,14 @@ var Model = /** @class */ (function () {
6262
var response, _a;
6363
return __generator(this, function (_b) {
6464
switch (_b.label) {
65-
case 0: return [4 /*yield*/, fetch("".concat(this.cdnPath, "model_list.json"))];
65+
case 0: return [4, fetch("".concat(this.cdnPath, "model_list.json"))];
6666
case 1:
6767
response = _b.sent();
6868
_a = this;
69-
return [4 /*yield*/, response.json()];
69+
return [4, response.json()];
7070
case 2:
7171
_a.modelList = _b.sent();
72-
return [2 /*return*/];
72+
return [2];
7373
}
7474
});
7575
});
@@ -83,21 +83,21 @@ var Model = /** @class */ (function () {
8383
localStorage.setItem('modelId', modelId.toString());
8484
localStorage.setItem('modelTexturesId', modelTexturesId.toString());
8585
showMessage(message, 4000, 10);
86-
if (!(this.useCDN && this.modelList)) return [3 /*break*/, 3];
87-
if (!!this.modelList) return [3 /*break*/, 2];
88-
return [4 /*yield*/, this.loadModelList()];
86+
if (!(this.useCDN && this.modelList)) return [3, 3];
87+
if (!!this.modelList) return [3, 2];
88+
return [4, this.loadModelList()];
8989
case 1:
9090
_a.sent();
9191
_a.label = 2;
9292
case 2:
9393
target = randomSelection(this.modelList.models[modelId]);
9494
loadlive2d('live2d', "".concat(this.cdnPath, "model/").concat(target, "/index.json"));
95-
return [3 /*break*/, 4];
95+
return [3, 4];
9696
case 3:
9797
loadlive2d('live2d', "".concat(this.apiPath, "get/?id=").concat(modelId, "-").concat(modelTexturesId));
9898
console.log("Live2D Model ".concat(modelId, "-").concat(modelTexturesId, " Loaded"));
9999
_a.label = 4;
100-
case 4: return [2 /*return*/];
100+
case 4: return [2];
101101
}
102102
});
103103
});
@@ -111,19 +111,18 @@ var Model = /** @class */ (function () {
111111
case 0:
112112
modelId = Number(localStorage.getItem('modelId'));
113113
modelTexturesId = Number(localStorage.getItem('modelTexturesId'));
114-
if (!(this.useCDN && modelId && this.modelList)) return [3 /*break*/, 3];
115-
if (!!this.modelList) return [3 /*break*/, 2];
116-
return [4 /*yield*/, this.loadModelList()];
114+
if (!(this.useCDN && modelId && this.modelList)) return [3, 3];
115+
if (!!this.modelList) return [3, 2];
116+
return [4, this.loadModelList()];
117117
case 1:
118118
_a.sent();
119119
_a.label = 2;
120120
case 2:
121121
target = randomSelection(this.modelList.models[modelId]);
122122
loadlive2d('live2d', "".concat(this.cdnPath, "model/").concat(target, "/index.json"));
123123
showMessage('我的新衣服好看嘛?', 4000, 10);
124-
return [3 /*break*/, 4];
124+
return [3, 4];
125125
case 3:
126-
// Optional "rand" (Random), "switch" (Switch by order)
127126
fetch("".concat(this.apiPath, "rand_textures/?id=").concat(modelId, "-").concat(modelTexturesId))
128127
.then(function (response) { return response.json(); })
129128
.then(function (result) {
@@ -136,7 +135,7 @@ var Model = /** @class */ (function () {
136135
}
137136
});
138137
_a.label = 4;
139-
case 4: return [2 /*return*/];
138+
case 4: return [2];
140139
}
141140
});
142141
});
@@ -149,24 +148,24 @@ var Model = /** @class */ (function () {
149148
switch (_a.label) {
150149
case 0:
151150
modelId = Number(localStorage.getItem('modelId'));
152-
if (!(this.useCDN && modelId && this.modelList)) return [3 /*break*/, 3];
153-
if (!!this.modelList) return [3 /*break*/, 2];
154-
return [4 /*yield*/, this.loadModelList()];
151+
if (!(this.useCDN && modelId && this.modelList)) return [3, 3];
152+
if (!!this.modelList) return [3, 2];
153+
return [4, this.loadModelList()];
155154
case 1:
156155
_a.sent();
157156
_a.label = 2;
158157
case 2:
159158
index = ++modelId >= this.modelList.models.length ? 0 : modelId;
160159
void this.loadModel(index, 0, this.modelList.messages[index]);
161-
return [3 /*break*/, 4];
160+
return [3, 4];
162161
case 3:
163162
fetch("".concat(this.apiPath, "switch/?id=").concat(modelId))
164163
.then(function (response) { return response.json(); })
165164
.then(function (result) {
166165
_this.loadModel(result.model.id, 0, result.model.message);
167166
});
168167
_a.label = 4;
169-
case 4: return [2 /*return*/];
168+
case 4: return [2];
170169
}
171170
});
172171
});

build/tools.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import fa_info_circle from '@fortawesome/fontawesome-free/svgs/solid/circle-info
77
import fa_xmark from '@fortawesome/fontawesome-free/svgs/solid/xmark.svg';
88
import showMessage from './message.js';
99
function showHitokoto() {
10-
// 增加 hitokoto.cn 的 API
1110
fetch('https://v1.hitokoto.cn')
1211
.then(function (response) { return response.json(); })
1312
.then(function (result) {

build/waifu-tips.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
import initWidget from "./index.js";
1+
import initWidget from './initWidget.js';
22
window.initWidget = initWidget;

dist/autoload.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// live2d_path 参数建议使用绝对路径
2-
const live2d_path = "https://fastly.jsdelivr.net/gh/stevenjoezhang/live2d-widget@latest/";
3-
//const live2d_path = "/live2d-widget/";
2+
const live2d_path = "https://fastly.jsdelivr.net/npm/live2d-widgets@0/";
3+
// const live2d_path = "/live2d-widget/dist/";
44

55
// 封装异步加载资源的方法
66
function loadExternalResource(url, type) {
@@ -34,7 +34,7 @@ if (screen.width >= 768) {
3434
// 配置选项的具体用法见 README.md
3535
initWidget({
3636
waifuPath: live2d_path + "waifu-tips.json",
37-
//apiPath: "https://live2d.fghrsh.net/api/",
37+
// apiPath: "https://live2d.fghrsh.net/api/",
3838
cdnPath: "https://fastly.jsdelivr.net/gh/fghrsh/live2d_api/",
3939
tools: ["hitokoto", "asteroids", "switch-model", "switch-texture", "photo", "info", "quit"]
4040
});

0 commit comments

Comments
 (0)