Skip to content

Add: Add JSDoc for function and type #175

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions build/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,11 @@
/**
* @file 包含初始化看板娘小部件的函数。
* @module index
*/
/**
* 初始化看板娘小部件。
* @param {string | Config} config - 看板娘配置或配置路径。
* @param {string} [apiPath] - API 路径,如果 config 是字符串。
*/
declare function initWidget(config: string | Config, apiPath?: string): void;
export default initWidget;
24 changes: 24 additions & 0 deletions build/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
/**
* @file 包含初始化看板娘小部件的函数。
* @module index
*/
import Model from './model.js';
import showMessage from './message.js';
import randomSelection from './utils.js';
import tools from './tools.js';
/**
* 加载看板娘小部件。
* @param {Config} config - 看板娘配置。
*/
function loadWidget(config) {
var model = new Model(config);
localStorage.removeItem('waifu-display');
Expand Down Expand Up @@ -30,6 +38,11 @@ function loadWidget(config) {
}
}
})();
/**
* 根据时间显示欢迎消息。
* @param {Time} time - 时间消息配置。
* @returns {string} 欢迎消息。
*/
function welcomeMessage(time) {
if (location.pathname === '/') {
// 如果是主页
Expand Down Expand Up @@ -61,6 +74,10 @@ function loadWidget(config) {
}
return text;
}
/**
* 注册事件监听器。
* @param {Result} result - 结果配置。
*/
function registerEventListener(result) {
// Detect user activity and display messages when idle
var userAction = false;
Expand All @@ -84,6 +101,7 @@ function loadWidget(config) {
showMessage(welcomeMessage(result.time), 7000, 11);
window.addEventListener('mouseover', function (event) {
var _a;
// eslint-disable-next-line prefer-const
for (var _i = 0, _b = result.mouseover; _i < _b.length; _i++) {
var _c = _b[_i], selector = _c.selector, text = _c.text;
if (!((_a = event.target) === null || _a === void 0 ? void 0 : _a.closest(selector)))
Expand All @@ -99,6 +117,7 @@ function loadWidget(config) {
});
window.addEventListener('click', function (event) {
var _a;
// eslint-disable-next-line prefer-const
for (var _i = 0, _b = result.click; _i < _b.length; _i++) {
var _c = _b[_i], selector = _c.selector, text = _c.text;
if (!((_a = event.target) === null || _a === void 0 ? void 0 : _a.closest(selector)))
Expand Down Expand Up @@ -148,6 +167,11 @@ function loadWidget(config) {
.then(registerEventListener);
})();
}
/**
* 初始化看板娘小部件。
* @param {string | Config} config - 看板娘配置或配置路径。
* @param {string} [apiPath] - API 路径,如果 config 是字符串。
*/
function initWidget(config, apiPath) {
if (typeof config === 'string') {
config = {
Expand Down
10 changes: 10 additions & 0 deletions build/message.d.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,12 @@
/**
* @file 包含显示看板娘消息的函数。
* @module message
*/
/**
* 显示看板娘消息。
* @param {string | string[]} text - 消息文本或文本数组。
* @param {number} timeout - 消息显示的超时时间(毫秒)。
* @param {number} priority - 消息的优先级。
*/
declare function showMessage(text: string | string[], timeout: number, priority: number): void;
export default showMessage;
26 changes: 19 additions & 7 deletions build/message.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,32 @@
import randomSelection from "./utils.js";
/**
* @file 包含显示看板娘消息的函数。
* @module message
*/
import randomSelection from './utils.js';
var messageTimer = null;
/**
* 显示看板娘消息。
* @param {string | string[]} text - 消息文本或文本数组。
* @param {number} timeout - 消息显示的超时时间(毫秒)。
* @param {number} priority - 消息的优先级。
*/
function showMessage(text, timeout, priority) {
if (!text || (sessionStorage.getItem("waifu-text") && Number(sessionStorage.getItem("waifu-text")) > priority))
if (!text ||
(sessionStorage.getItem('waifu-text') &&
Number(sessionStorage.getItem('waifu-text')) > priority))
return;
if (messageTimer) {
clearTimeout(messageTimer);
messageTimer = null;
}
text = randomSelection(text);
sessionStorage.setItem("waifu-text", String(priority));
var tips = document.getElementById("waifu-tips");
sessionStorage.setItem('waifu-text', String(priority));
var tips = document.getElementById('waifu-tips');
tips.innerHTML = text;
tips.classList.add("waifu-tips-active");
tips.classList.add('waifu-tips-active');
messageTimer = setTimeout(function () {
sessionStorage.removeItem("waifu-text");
tips.classList.remove("waifu-tips-active");
sessionStorage.removeItem('waifu-text');
tips.classList.remove('waifu-tips-active');
}, timeout);
}
export default showMessage;
28 changes: 28 additions & 0 deletions build/model.d.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,43 @@
/**
* @file 包含看板娘模型加载和管理相关的类。
* @module model
*/
/**
* 看板娘模型类,负责加载和管理模型。
*/
declare class Model {
private readonly useCDN;
private readonly apiPath;
private readonly cdnPath;
private modelList;
/**
* 创建一个 Model 实例。
* @param {Object} config - 配置选项。
* @param {string} [config.apiPath] - API 路径。
* @param {string} [config.cdnPath] - CDN 路径。
*/
constructor(config: {
apiPath?: string;
cdnPath?: string;
});
/**
* 加载模型列表。
*/
loadModelList(): Promise<void>;
/**
* 加载指定模型。
* @param {number} modelId - 模型 ID。
* @param {number} modelTexturesId - 模型材质 ID。
* @param {string} message - 加载消息。
*/
loadModel(modelId: number, modelTexturesId: number, message: string): Promise<void>;
/**
* 加载随机材质的模型。
*/
loadRandModel(): Promise<void>;
/**
* 加载其他模型。
*/
loadOtherModel(): Promise<void>;
}
export default Model;
28 changes: 28 additions & 0 deletions build/model.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
/**
* @file 包含看板娘模型加载和管理相关的类。
* @module model
*/
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
Expand Down Expand Up @@ -36,7 +40,16 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
};
import showMessage from './message.js';
import randomSelection from './utils.js';
/**
* 看板娘模型类,负责加载和管理模型。
*/
var Model = /** @class */ (function () {
/**
* 创建一个 Model 实例。
* @param {Object} config - 配置选项。
* @param {string} [config.apiPath] - API 路径。
* @param {string} [config.cdnPath] - CDN 路径。
*/
function Model(config) {
this.modelList = null;
var apiPath = config.apiPath, cdnPath = config.cdnPath;
Expand All @@ -57,6 +70,9 @@ var Model = /** @class */ (function () {
this.apiPath = apiPath || '';
this.cdnPath = cdnPath || '';
}
/**
* 加载模型列表。
*/
Model.prototype.loadModelList = function () {
return __awaiter(this, void 0, void 0, function () {
var response, _a;
Expand All @@ -74,6 +90,12 @@ var Model = /** @class */ (function () {
});
});
};
/**
* 加载指定模型。
* @param {number} modelId - 模型 ID。
* @param {number} modelTexturesId - 模型材质 ID。
* @param {string} message - 加载消息。
*/
Model.prototype.loadModel = function (modelId, modelTexturesId, message) {
return __awaiter(this, void 0, void 0, function () {
var target;
Expand Down Expand Up @@ -102,6 +124,9 @@ var Model = /** @class */ (function () {
});
});
};
/**
* 加载随机材质的模型。
*/
Model.prototype.loadRandModel = function () {
return __awaiter(this, void 0, void 0, function () {
var modelId, modelTexturesId, target;
Expand Down Expand Up @@ -141,6 +166,9 @@ var Model = /** @class */ (function () {
});
});
};
/**
* 加载其他模型。
*/
Model.prototype.loadOtherModel = function () {
return __awaiter(this, void 0, void 0, function () {
var modelId, index;
Expand Down
11 changes: 11 additions & 0 deletions build/tools.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
/**
* @file 包含看板娘工具的配置和函数。
* @module tools
*/
/**
* 显示一句一言。
*/
declare function showHitokoto(): void;
/**
* 看板娘工具配置。
* @type {Object}
*/
declare const tools: {
hitokoto: {
icon: string;
Expand Down
11 changes: 11 additions & 0 deletions build/tools.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
/**
* @file 包含看板娘工具的配置和函数。
* @module tools
*/
import fa_comment from '@fortawesome/fontawesome-free/svgs/solid/comment.svg';
import fa_paper_plane from '@fortawesome/fontawesome-free/svgs/solid/paper-plane.svg';
import fa_user_circle from '@fortawesome/fontawesome-free/svgs/solid/circle-user.svg';
Expand All @@ -6,6 +10,9 @@ import fa_camera_retro from '@fortawesome/fontawesome-free/svgs/solid/camera-ret
import fa_info_circle from '@fortawesome/fontawesome-free/svgs/solid/circle-info.svg';
import fa_xmark from '@fortawesome/fontawesome-free/svgs/solid/xmark.svg';
import showMessage from './message.js';
/**
* 显示一句一言。
*/
function showHitokoto() {
// 增加 hitokoto.cn 的 API
fetch('https://v1.hitokoto.cn')
Expand All @@ -18,6 +25,10 @@ function showHitokoto() {
}, 6000);
});
}
/**
* 看板娘工具配置。
* @type {Object}
*/
var tools = {
hitokoto: {
icon: fa_comment,
Expand Down
9 changes: 9 additions & 0 deletions build/utils.d.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,11 @@
/**
* @file 包含实用工具函数。
* @module utils
*/
/**
* 从数组中随机选择一个元素,或返回原始值(如果不是数组)。
* @param {any} obj - 要选择的对象或数组。
* @returns {any} 随机选择的元素或原始值。
*/
declare function randomSelection(obj: any): any;
export default randomSelection;
9 changes: 9 additions & 0 deletions build/utils.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
/**
* @file 包含实用工具函数。
* @module utils
*/
/**
* 从数组中随机选择一个元素,或返回原始值(如果不是数组)。
* @param {any} obj - 要选择的对象或数组。
* @returns {any} 随机选择的元素或原始值。
*/
function randomSelection(obj) {
return Array.isArray(obj) ? obj[Math.floor(Math.random() * obj.length)] : obj;
}
Expand Down
4 changes: 4 additions & 0 deletions build/waifu-tips.d.ts
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
/**
* @file 包含初始化看板娘小部件的入口。
* @module waifu-tips
*/
export {};
6 changes: 5 additions & 1 deletion build/waifu-tips.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
import initWidget from "./index.js";
/**
* @file 包含初始化看板娘小部件的入口。
* @module waifu-tips
*/
import initWidget from './index.js';
window.initWidget = initWidget;
25 changes: 24 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
/**
* @file 包含初始化看板娘小部件的函数。
* @module index
*/

import Model from './model.js';
import showMessage from './message.js';
import randomSelection from './utils.js';
import tools from './tools.js';

/**
* 加载看板娘小部件。
* @param {Config} config - 看板娘配置。
*/
function loadWidget(config: Config) {
const model = new Model(config);
localStorage.removeItem('waifu-display');
Expand Down Expand Up @@ -42,7 +51,12 @@ function loadWidget(config: Config) {
}
})();

function welcomeMessage(time: Time) {
/**
* 根据时间显示欢迎消息。
* @param {Time} time - 时间消息配置。
* @returns {string} 欢迎消息。
*/
function welcomeMessage(time: Time): string {
if (location.pathname === '/') {
// 如果是主页
for (const { hour, text } of time) {
Expand Down Expand Up @@ -76,6 +90,10 @@ function loadWidget(config: Config) {
return text;
}

/**
* 注册事件监听器。
* @param {Result} result - 结果配置。
*/
function registerEventListener(result: Result) {
// Detect user activity and display messages when idle
let userAction = false;
Expand Down Expand Up @@ -171,6 +189,11 @@ function loadWidget(config: Config) {
})();
}

/**
* 初始化看板娘小部件。
* @param {string | Config} config - 看板娘配置或配置路径。
* @param {string} [apiPath] - API 路径,如果 config 是字符串。
*/
function initWidget(config: string | Config, apiPath?: string) {
if (typeof config === 'string') {
config = {
Expand Down
Loading
Loading