@iftc/yete 0.0.1-alpha.2 → 0.0.1-alpha.3
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/README.md +5 -3
- package/index.js +182 -6
- package/libs/yete-ui/Text.js +4 -0
- package/libs/yete-ui/yete.json +4 -1
- package/package.json +1 -1
- package/src/loader.js +35 -1
package/README.md
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
# Yete
|
|
2
2
|
|
|
3
|
-
Yete 是一个轻量级、可扩展的 Web 框架。使用 Dexie 库操作 IndexedDB
|
|
4
|
-
|
|
3
|
+
Yete 是一个轻量级、可扩展的 Web 框架。使用 Dexie 库操作 IndexedDB,将资源文件缓存到浏览器中,能够减少加载时间和带宽消耗。<br>
|
|
4
|
+
每次构建后,会重新下载一次资源文件,并缓存到浏览器中。<br>
|
|
5
|
+
`libs` 目录下的文件不会视为资源文件,如需使用,请使用 `yete add <libdirpath>` 命令添加。<br>
|
|
6
|
+
开发文档:https://iftc.koyeb.app/docs/yete
|
|
5
7
|
|
|
6
8
|
## 快速开始
|
|
7
9
|
|
|
@@ -28,7 +30,7 @@ yete build && yete serve
|
|
|
28
30
|
|
|
29
31
|
## 许可证
|
|
30
32
|
|
|
31
|
-
[MIT](https://github.com/IFTC-XLKJ/Yete
|
|
33
|
+
[MIT](https://github.com/IFTC-XLKJ/Yete?tab=MIT-1-ov-file)
|
|
32
34
|
|
|
33
35
|
## 联系作者
|
|
34
36
|
|
package/index.js
CHANGED
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
const { add } = require("dexie");
|
|
2
|
-
|
|
3
1
|
/**
|
|
4
2
|
* 自定义错误类
|
|
5
3
|
*/
|
|
@@ -7,6 +5,7 @@ class YeteError extends Error {
|
|
|
7
5
|
/**
|
|
8
6
|
* 构造函数
|
|
9
7
|
* @param {String} message 错误信息
|
|
8
|
+
* @example new YeteError("Something went wrong.");
|
|
10
9
|
*/
|
|
11
10
|
constructor(message) {
|
|
12
11
|
super(message);
|
|
@@ -15,11 +14,22 @@ class YeteError extends Error {
|
|
|
15
14
|
}
|
|
16
15
|
/**
|
|
17
16
|
* 页面基类
|
|
17
|
+
* @example
|
|
18
|
+
* class MyPage extends Page {
|
|
19
|
+
* constructor() {
|
|
20
|
+
* super("my-page");
|
|
21
|
+
* }
|
|
22
|
+
* render() {
|
|
23
|
+
* return document.createElement('div', { className: 'page' });
|
|
24
|
+
* }
|
|
25
|
+
* }
|
|
18
26
|
*/
|
|
19
27
|
class Page {
|
|
20
28
|
/**
|
|
21
29
|
* 构造函数
|
|
22
30
|
* @param {String} name
|
|
31
|
+
* @example
|
|
32
|
+
* new Page("my-page");
|
|
23
33
|
*/
|
|
24
34
|
constructor(name) {
|
|
25
35
|
this.name = name;
|
|
@@ -27,21 +37,47 @@ class Page {
|
|
|
27
37
|
/**
|
|
28
38
|
* 渲染页面
|
|
29
39
|
* @returns {HTMLElement}
|
|
40
|
+
* @example
|
|
41
|
+
* return document.createElement('div', { className: 'page' });
|
|
30
42
|
*/
|
|
31
43
|
render() {
|
|
32
|
-
return document.createElement('div', { className: 'page' }
|
|
44
|
+
return document.createElement('div', { className: 'page' });
|
|
33
45
|
}
|
|
34
46
|
}
|
|
35
47
|
|
|
48
|
+
/**
|
|
49
|
+
* 页面数组
|
|
50
|
+
* @property {Page} page 页面对象
|
|
51
|
+
*/
|
|
36
52
|
const pages = [];
|
|
53
|
+
/**
|
|
54
|
+
* 路由数组
|
|
55
|
+
* @property {String} path 路径
|
|
56
|
+
* @property {HTMLElement} page 页面
|
|
57
|
+
*/
|
|
37
58
|
const routes = [];
|
|
59
|
+
/**
|
|
60
|
+
* 事件数组
|
|
61
|
+
* @property {String} name 事件名称
|
|
62
|
+
* @property {Function} callback 回调函数
|
|
63
|
+
*/
|
|
38
64
|
const events = [];
|
|
65
|
+
let isStarted = false;
|
|
66
|
+
|
|
67
|
+
let custom404Page = null;
|
|
68
|
+
let custom502Page = null;
|
|
39
69
|
|
|
70
|
+
/**
|
|
71
|
+
* 导出的对象
|
|
72
|
+
*/
|
|
40
73
|
const obj = {
|
|
41
74
|
/**
|
|
42
75
|
* 设置页面图标
|
|
43
76
|
* @param {String} iconPath
|
|
44
77
|
* @returns
|
|
78
|
+
* @throws {YeteError}
|
|
79
|
+
* @example
|
|
80
|
+
* setIcon("icon.png");
|
|
45
81
|
*/
|
|
46
82
|
setIcon: function (iconPath) {
|
|
47
83
|
const fileType = getFileType(iconPath);
|
|
@@ -63,6 +99,9 @@ const obj = {
|
|
|
63
99
|
/**
|
|
64
100
|
* 设置页面标题
|
|
65
101
|
* @param {String} title
|
|
102
|
+
* @throws {YeteError}
|
|
103
|
+
* @example
|
|
104
|
+
* setTitle("Yete");
|
|
66
105
|
*/
|
|
67
106
|
setTitle: function (title) {
|
|
68
107
|
document.title = title;
|
|
@@ -70,6 +109,8 @@ const obj = {
|
|
|
70
109
|
/**
|
|
71
110
|
* 配置路由
|
|
72
111
|
* @param {Array<Object>} options
|
|
112
|
+
* @property {String} path 路径
|
|
113
|
+
* @property {HTMLElement} page 页面
|
|
73
114
|
* @example
|
|
74
115
|
* options: [
|
|
75
116
|
* { path: '/', page: () => loadPage(HomePage) },
|
|
@@ -80,10 +121,26 @@ const obj = {
|
|
|
80
121
|
console.log(options);
|
|
81
122
|
routes.push(...options);
|
|
82
123
|
},
|
|
124
|
+
/**
|
|
125
|
+
* 页面基类
|
|
126
|
+
* @example
|
|
127
|
+
* class MyPage extends Page {
|
|
128
|
+
* constructor() {
|
|
129
|
+
* super("my-page");
|
|
130
|
+
* }
|
|
131
|
+
* render() {
|
|
132
|
+
* return document.createElement('div', { className: 'page' });
|
|
133
|
+
* }
|
|
134
|
+
* }
|
|
135
|
+
*/
|
|
83
136
|
Page: Page,
|
|
84
137
|
/**
|
|
85
138
|
* 加载组件
|
|
86
139
|
* @param {Page} page
|
|
140
|
+
* @returns {HTMLElement}
|
|
141
|
+
* @throws {YeteError}
|
|
142
|
+
* @example
|
|
143
|
+
* loadPage(MyPage);
|
|
87
144
|
*/
|
|
88
145
|
loadPage: function (page) {
|
|
89
146
|
console.log(page);
|
|
@@ -96,14 +153,28 @@ const obj = {
|
|
|
96
153
|
},
|
|
97
154
|
/**
|
|
98
155
|
* 启动程序
|
|
156
|
+
* @description 该方法只能调用一次
|
|
157
|
+
* @throws {YeteError}
|
|
158
|
+
* @example
|
|
159
|
+
* start();
|
|
99
160
|
*/
|
|
100
161
|
start: function () {
|
|
162
|
+
if (isStarted) {
|
|
163
|
+
throw new YeteError("The program has already started.");
|
|
164
|
+
}
|
|
165
|
+
if (!routes.some(r => r.path === '/')) {
|
|
166
|
+
throw new YeteError("The route must contain a path '/'.");
|
|
167
|
+
}
|
|
168
|
+
isStarted = true;
|
|
101
169
|
const path = location.hash.slice(1) || '/';
|
|
102
170
|
obj.toPage(path);
|
|
103
171
|
},
|
|
104
172
|
/**
|
|
105
173
|
* 加载 CSS 文件
|
|
106
174
|
* @param {String} cssPath
|
|
175
|
+
* @returns {Promise<void>}
|
|
176
|
+
* @example
|
|
177
|
+
* await loadCSS("index.css");
|
|
107
178
|
*/
|
|
108
179
|
loadCSS: async function (cssPath) {
|
|
109
180
|
return new Promise(async (resolve, reject) => {
|
|
@@ -119,6 +190,9 @@ const obj = {
|
|
|
119
190
|
/**
|
|
120
191
|
* 加载 JS 文件
|
|
121
192
|
* @param {String} scriptPath
|
|
193
|
+
* @returns {Promise<void>}
|
|
194
|
+
* @example
|
|
195
|
+
* await loadScript("index.js");
|
|
122
196
|
*/
|
|
123
197
|
loadScript: async function (scriptPath) {
|
|
124
198
|
return new Promise(async (resolve, reject) => {
|
|
@@ -134,16 +208,26 @@ const obj = {
|
|
|
134
208
|
/**
|
|
135
209
|
* 跳转到指定页面
|
|
136
210
|
* @param {String} path
|
|
211
|
+
* @param {String} message
|
|
212
|
+
* @throws {YeteError}
|
|
213
|
+
* @example
|
|
214
|
+
* toPage('/about');
|
|
137
215
|
*/
|
|
138
|
-
toPage: function (path) {
|
|
216
|
+
toPage: function (path, message = null) {
|
|
139
217
|
location.hash = path;
|
|
140
218
|
const page = routes.find(r => r.path === path);
|
|
141
219
|
console.log(pages, page);
|
|
142
220
|
if (page) {
|
|
143
221
|
document.body.innerHTML = "";
|
|
144
222
|
document.body.appendChild(page.page);
|
|
145
|
-
obj.emit('page-change', new obj.YeteEvent("page-change", { path, isNotFound: false }));
|
|
223
|
+
obj.emit('page-change', new obj.YeteEvent("page-change", { path, isNotFound: false, message }));
|
|
146
224
|
} else {
|
|
225
|
+
if (custom404Page) {
|
|
226
|
+
document.body.innerHTML = "";
|
|
227
|
+
document.body.appendChild(custom404Page);
|
|
228
|
+
obj.emit('page-change', new obj.YeteEvent("page-change", { path, isNotFound: true }));
|
|
229
|
+
return;
|
|
230
|
+
}
|
|
147
231
|
document.body.innerHTML = "<center><h1>404 Not Found</h1><p>Power By Yete</p></center>";
|
|
148
232
|
obj.emit('page-change', new obj.YeteEvent("page-change", { path, isNotFound: true }));
|
|
149
233
|
throw new YeteError("The page not found.");
|
|
@@ -154,6 +238,8 @@ const obj = {
|
|
|
154
238
|
* @param {String} tag
|
|
155
239
|
* @param {Object} options
|
|
156
240
|
* @returns {HTMLElement}
|
|
241
|
+
* @example
|
|
242
|
+
* const button = newElement('Text', { className: 'btn', textContent: 'Click Me' });
|
|
157
243
|
*/
|
|
158
244
|
newElement: function (tag, options = {}) {
|
|
159
245
|
if (!UIElements[tag]) {
|
|
@@ -175,6 +261,8 @@ const obj = {
|
|
|
175
261
|
* 添加事件监听器
|
|
176
262
|
* @param {String} type
|
|
177
263
|
* @param {Function} listener
|
|
264
|
+
* @example
|
|
265
|
+
* addEventListener('click', () => console.log('Clicked'));
|
|
178
266
|
*/
|
|
179
267
|
addEventListener: function (type, listener) {
|
|
180
268
|
events.push({ type, listener });
|
|
@@ -183,6 +271,8 @@ const obj = {
|
|
|
183
271
|
* 移除事件监听器
|
|
184
272
|
* @param {String} type
|
|
185
273
|
* @param {Function} listener
|
|
274
|
+
* @example
|
|
275
|
+
* removeEventListener('click', () => console.log('Clicked'));
|
|
186
276
|
*/
|
|
187
277
|
removeEventListener: function (type, listener) {
|
|
188
278
|
const index = events.findIndex(e => e.type === type && e.listener === listener);
|
|
@@ -194,6 +284,8 @@ const obj = {
|
|
|
194
284
|
* 派发事件
|
|
195
285
|
* @param {String} type
|
|
196
286
|
* @param {Object} event
|
|
287
|
+
* @example
|
|
288
|
+
* dispatchEvent('click', new YeteEvent("click", { bubbles: true, cancelable: true }));
|
|
197
289
|
*/
|
|
198
290
|
dispatchEvent: function (type, event) {
|
|
199
291
|
events.forEach(e => {
|
|
@@ -204,12 +296,19 @@ const obj = {
|
|
|
204
296
|
},
|
|
205
297
|
/**
|
|
206
298
|
* 创建事件
|
|
299
|
+
* @param {String} type
|
|
300
|
+
* @param {Object} options
|
|
301
|
+
* @returns {YeteEvent}
|
|
302
|
+
* @example
|
|
303
|
+
* const event = new YeteEvent("click", { bubbles: true, cancelable: true });
|
|
207
304
|
*/
|
|
208
305
|
YeteEvent: class extends Event {
|
|
209
306
|
/**
|
|
210
307
|
* 构造函数
|
|
211
308
|
* @param {String} type
|
|
212
309
|
* @param {Object} options
|
|
310
|
+
* @example
|
|
311
|
+
* const event = new YeteEvent("click", { bubbles: true, cancelable: true });
|
|
213
312
|
*/
|
|
214
313
|
constructor(type, options) {
|
|
215
314
|
super(type, { bubbles: true, cancelable: true });
|
|
@@ -218,33 +317,110 @@ const obj = {
|
|
|
218
317
|
}
|
|
219
318
|
}
|
|
220
319
|
},
|
|
320
|
+
/**
|
|
321
|
+
* 获取绑定的元素
|
|
322
|
+
* @returns {NodeList}
|
|
323
|
+
* @example
|
|
324
|
+
* const elements = getBinding();
|
|
325
|
+
*/
|
|
326
|
+
getBinding: function () {
|
|
327
|
+
const elementsWithId = document.querySelectorAll('[yete-id]');
|
|
328
|
+
return elementsWithId;
|
|
329
|
+
},
|
|
330
|
+
/**
|
|
331
|
+
* 自定义 404 页面
|
|
332
|
+
* @description 当页面不存在时,会显示此页面
|
|
333
|
+
* @param {Page} page
|
|
334
|
+
* @example
|
|
335
|
+
* custom404(Custom404Page);
|
|
336
|
+
*/
|
|
337
|
+
custom404: function (page = null) {
|
|
338
|
+
if (!page) {
|
|
339
|
+
custom404Page = null;
|
|
340
|
+
return;
|
|
341
|
+
}
|
|
342
|
+
const p = new page();
|
|
343
|
+
if (p instanceof Page) {
|
|
344
|
+
custom404Page = p.page;
|
|
345
|
+
} else {
|
|
346
|
+
throw new YeteError("The page not found.");
|
|
347
|
+
}
|
|
348
|
+
},
|
|
349
|
+
/**
|
|
350
|
+
* 自定义 502 错误页面
|
|
351
|
+
* @description 当页面有未捕获的错误被抛出时,会显示此页面
|
|
352
|
+
* @param {Page} page
|
|
353
|
+
* @example
|
|
354
|
+
* custom502(Custom502Page);
|
|
355
|
+
*/
|
|
356
|
+
custom502: function (page = null) {
|
|
357
|
+
if (!page) {
|
|
358
|
+
custom502Page = null;
|
|
359
|
+
return;
|
|
360
|
+
}
|
|
361
|
+
const p = new page();
|
|
362
|
+
if (p instanceof Page) {
|
|
363
|
+
custom502Page = p.page;
|
|
364
|
+
} else {
|
|
365
|
+
throw new YeteError("The page not found.");
|
|
366
|
+
}
|
|
367
|
+
},
|
|
221
368
|
};
|
|
222
369
|
|
|
223
370
|
/**
|
|
224
371
|
* 添加事件监听器
|
|
225
372
|
* @param {String} type
|
|
226
373
|
* @param {Function} listener
|
|
374
|
+
* @example
|
|
375
|
+
* on('click', () => console.log('Clicked'));
|
|
227
376
|
*/
|
|
228
377
|
obj.on = obj.addEventListener;
|
|
229
378
|
/**
|
|
230
379
|
* 移除事件监听器
|
|
231
380
|
* @param {String} type
|
|
232
381
|
* @param {Function} listener
|
|
382
|
+
* @example
|
|
383
|
+
* off('click', () => console.log('Clicked'));
|
|
233
384
|
*/
|
|
234
385
|
obj.off = obj.removeEventListener;
|
|
235
386
|
/**
|
|
236
387
|
* 派发事件
|
|
237
388
|
* @param {String} type
|
|
238
389
|
* @param {Object} event
|
|
390
|
+
* @example
|
|
391
|
+
* emit('click', new YeteEvent("click", { bubbles: true, cancelable: true }));
|
|
239
392
|
*/
|
|
240
393
|
obj.emit = obj.dispatchEvent;
|
|
241
394
|
|
|
242
|
-
|
|
395
|
+
window.addEventListener("error", function (event) {
|
|
396
|
+
const { message, filename, lineno, colno, error } = event;
|
|
397
|
+
if (custom502Page) {
|
|
398
|
+
document.body.innerHTML = "";
|
|
399
|
+
document.body.appendChild(custom502Page);
|
|
400
|
+
const errorMsgEl = document.getElementById("error-message");
|
|
401
|
+
const errorFilenameEl = document.getElementById("error-filename");
|
|
402
|
+
const errorLinenoEl = document.getElementById("error-lineno");
|
|
403
|
+
const errorColnoEl = document.getElementById("error-colno");
|
|
404
|
+
const errorErrorEl = document.getElementById("error-error");
|
|
405
|
+
if (errorMsgEl) errorMsgEl.innerText = message;
|
|
406
|
+
if (errorFilenameEl) errorFilenameEl.innerText = filename;
|
|
407
|
+
if (errorLinenoEl) errorLinenoEl.innerText = lineno;
|
|
408
|
+
if (errorColnoEl) errorColnoEl.innerText = colno;
|
|
409
|
+
if (errorErrorEl) errorErrorEl.innerText = error;
|
|
410
|
+
obj.emit('page-change', new obj.YeteEvent("page-change", { path, isError: true }));
|
|
411
|
+
} else {
|
|
412
|
+
document.body.innerHTML = `<center><h1>Have an error: "${message}" in "${filename}" at ${lineno}:${colno}</h1><p>Powsered by Yete.</p></center>`;
|
|
413
|
+
obj.emit('page-change', new obj.YeteEvent("page-change", { path, isError: true }));
|
|
414
|
+
}
|
|
415
|
+
});
|
|
416
|
+
|
|
243
417
|
window.addEventListener('hashchange', () => {
|
|
244
418
|
const path = location.hash.slice(1) || '/';
|
|
245
419
|
obj.toPage(path);
|
|
246
420
|
});
|
|
247
421
|
|
|
422
|
+
console.log("%cYete v" + YeteVersion, "border-radius: 16px; font-size: 64px; color: #fff; background-color: #007bff; padding: 5px;margin: 5px;");
|
|
423
|
+
|
|
248
424
|
/**
|
|
249
425
|
* 获取文件类型
|
|
250
426
|
* @param {String} filePath
|
package/libs/yete-ui/yete.json
CHANGED
package/package.json
CHANGED
package/src/loader.js
CHANGED
|
@@ -24,7 +24,10 @@ async function loadIndex() {
|
|
|
24
24
|
script.src = url;
|
|
25
25
|
document.body.appendChild(script);
|
|
26
26
|
} else {
|
|
27
|
+
document.body.innerHTML = `<center><h1>index.js not found in the database. Reseting...</h1><p>Powered By Yete</p></center>`;
|
|
27
28
|
console.error("Error: index.js not found in the database.");
|
|
29
|
+
localStorage.clear();
|
|
30
|
+
location.reload();
|
|
28
31
|
}
|
|
29
32
|
}
|
|
30
33
|
|
|
@@ -38,12 +41,42 @@ async function updateFiles() {
|
|
|
38
41
|
path: file,
|
|
39
42
|
size: blob.size,
|
|
40
43
|
type: blob.type,
|
|
41
|
-
lastModified:
|
|
44
|
+
lastModified: new Date(),
|
|
42
45
|
blob: blob
|
|
43
46
|
});
|
|
44
47
|
}
|
|
45
48
|
}
|
|
46
49
|
|
|
50
|
+
async function loadLibs() {
|
|
51
|
+
const files = yete.files;
|
|
52
|
+
const libFiles = files.filter(f => f.startsWith("libs/"));
|
|
53
|
+
console.log(libFiles);
|
|
54
|
+
for (const file of libFiles) {
|
|
55
|
+
const f = await db.files.get({ path: file });
|
|
56
|
+
console.log(file, f)
|
|
57
|
+
const url = URL.createObjectURL(f.blob);
|
|
58
|
+
if (file.endsWith(".js")) {
|
|
59
|
+
const script = document.createElement('script');
|
|
60
|
+
script.src = url;
|
|
61
|
+
document.body.appendChild(script);
|
|
62
|
+
} else if (file.endsWith(".css")) {
|
|
63
|
+
const style = document.createElement('style');
|
|
64
|
+
style.src = url;
|
|
65
|
+
document.head.appendChild(style);
|
|
66
|
+
} else if (file.endsWith(".ttf") || file.endsWith(".otf") || file.endsWith(".woff") || file.endsWith(".woff2")) {
|
|
67
|
+
const font = document.createElement("style");
|
|
68
|
+
style.textContent = `@font-face {
|
|
69
|
+
font-family: '${file.split("/").pop().split(".").slice(0, -1).join(".")}';
|
|
70
|
+
src: url('${url}') format('${file.split(".").pop()}'),
|
|
71
|
+
font-weight: normal;
|
|
72
|
+
font-style: normal;
|
|
73
|
+
font-display: swap;
|
|
74
|
+
}`;
|
|
75
|
+
document.head.appendChild(font);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
47
80
|
async function main() {
|
|
48
81
|
const version = yete.version;
|
|
49
82
|
if (localStorage.getItem('yete-version') != version) {
|
|
@@ -53,6 +86,7 @@ async function main() {
|
|
|
53
86
|
} else {
|
|
54
87
|
await loadFiles();
|
|
55
88
|
}
|
|
89
|
+
await loadLibs();
|
|
56
90
|
}
|
|
57
91
|
|
|
58
92
|
main();
|