@iftc/yete 0.0.1-alpha.1 → 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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) IFTC 2020-2026
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,41 @@
1
+ # Yete
2
+
3
+ &nbsp;&nbsp;&nbsp;&nbsp;Yete 是一个轻量级、可扩展的 Web 框架。使用 Dexie 库操作 IndexedDB,将资源文件缓存到浏览器中,能够减少加载时间和带宽消耗。<br>
4
+ &nbsp;&nbsp;&nbsp;&nbsp;每次构建后,会重新下载一次资源文件,并缓存到浏览器中。<br>
5
+ &nbsp;&nbsp;&nbsp;&nbsp;`libs` 目录下的文件不会视为资源文件,如需使用,请使用 `yete add <libdirpath>` 命令添加。<br>
6
+ &nbsp;&nbsp;&nbsp;&nbsp;开发文档:https://iftc.koyeb.app/docs/yete
7
+
8
+ ## 快速开始
9
+
10
+ 1. 下载 Yete CLI:
11
+ - [Windows x64](https://dbmp-xbgmorqeur6oh81z.database.nocode.cn/storage/v1/object/public/files/yete.exe "yete.exe")
12
+ - [Linux arm64](https://dbmp-xbgmorqeur6oh81z.database.nocode.cn/storage/v1/object/public/files/yete "yete")
13
+ - [CLI源文件(其他操作系统请自行编译或直接使用 Txiki.js 运行)](https://dbmp-xbgmorqeur6oh81z.database.nocode.cn/storage/v1/object/public/files/yete.zip "yete.zip")
14
+
15
+ 2. 创建并构建项目:
16
+
17
+ ```bash
18
+ yete init <project-name>
19
+ cd <project-name>
20
+ npm i
21
+ yete build && yete serve
22
+ ```
23
+
24
+ 3. 访问项目:
25
+ 浏览器访问 http://localhost:12345
26
+
27
+ ## 贡献
28
+
29
+ 欢迎任何形式的贡献,如提交 PR、提交 Issue、提交文档、提交翻译、提交代码片段。
30
+
31
+ ## 许可证
32
+
33
+ [MIT](https://github.com/IFTC-XLKJ/Yete?tab=MIT-1-ov-file)
34
+
35
+ ## 联系作者
36
+
37
+ - 邮箱:iftcceo@139.com 或 iftcceo@gmail.com
38
+ - 社区:https://discord.gg/dWWaUjVWv6
39
+ - 官网:https://iftc.koyeb.app/
40
+ - 仓库:https://github.com/IFTC-XLKJ/yete
41
+ - 官方文档:https://iftc.koyeb.app/docs/yete
package/index.js CHANGED
@@ -1,27 +1,83 @@
1
+ /**
2
+ * 自定义错误类
3
+ */
1
4
  class YeteError extends Error {
5
+ /**
6
+ * 构造函数
7
+ * @param {String} message 错误信息
8
+ * @example new YeteError("Something went wrong.");
9
+ */
2
10
  constructor(message) {
3
11
  super(message);
4
12
  this.name = 'YeteError';
5
13
  }
6
14
  }
7
-
15
+ /**
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
+ * }
26
+ */
8
27
  class Page {
28
+ /**
29
+ * 构造函数
30
+ * @param {String} name
31
+ * @example
32
+ * new Page("my-page");
33
+ */
9
34
  constructor(name) {
10
35
  this.name = name;
11
36
  }
37
+ /**
38
+ * 渲染页面
39
+ * @returns {HTMLElement}
40
+ * @example
41
+ * return document.createElement('div', { className: 'page' });
42
+ */
12
43
  render() {
13
- return document.createElement('div', { className: 'page' }, this.name);
44
+ return document.createElement('div', { className: 'page' });
14
45
  }
15
46
  }
16
47
 
48
+ /**
49
+ * 页面数组
50
+ * @property {Page} page 页面对象
51
+ */
17
52
  const pages = [];
53
+ /**
54
+ * 路由数组
55
+ * @property {String} path 路径
56
+ * @property {HTMLElement} page 页面
57
+ */
18
58
  const routes = [];
59
+ /**
60
+ * 事件数组
61
+ * @property {String} name 事件名称
62
+ * @property {Function} callback 回调函数
63
+ */
64
+ const events = [];
65
+ let isStarted = false;
19
66
 
67
+ let custom404Page = null;
68
+ let custom502Page = null;
69
+
70
+ /**
71
+ * 导出的对象
72
+ */
20
73
  const obj = {
21
74
  /**
22
75
  * 设置页面图标
23
76
  * @param {String} iconPath
24
77
  * @returns
78
+ * @throws {YeteError}
79
+ * @example
80
+ * setIcon("icon.png");
25
81
  */
26
82
  setIcon: function (iconPath) {
27
83
  const fileType = getFileType(iconPath);
@@ -43,6 +99,9 @@ const obj = {
43
99
  /**
44
100
  * 设置页面标题
45
101
  * @param {String} title
102
+ * @throws {YeteError}
103
+ * @example
104
+ * setTitle("Yete");
46
105
  */
47
106
  setTitle: function (title) {
48
107
  document.title = title;
@@ -50,6 +109,8 @@ const obj = {
50
109
  /**
51
110
  * 配置路由
52
111
  * @param {Array<Object>} options
112
+ * @property {String} path 路径
113
+ * @property {HTMLElement} page 页面
53
114
  * @example
54
115
  * options: [
55
116
  * { path: '/', page: () => loadPage(HomePage) },
@@ -60,10 +121,26 @@ const obj = {
60
121
  console.log(options);
61
122
  routes.push(...options);
62
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
+ */
63
136
  Page: Page,
64
137
  /**
65
138
  * 加载组件
66
139
  * @param {Page} page
140
+ * @returns {HTMLElement}
141
+ * @throws {YeteError}
142
+ * @example
143
+ * loadPage(MyPage);
67
144
  */
68
145
  loadPage: function (page) {
69
146
  console.log(page);
@@ -76,14 +153,28 @@ const obj = {
76
153
  },
77
154
  /**
78
155
  * 启动程序
156
+ * @description 该方法只能调用一次
157
+ * @throws {YeteError}
158
+ * @example
159
+ * start();
79
160
  */
80
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;
81
169
  const path = location.hash.slice(1) || '/';
82
170
  obj.toPage(path);
83
171
  },
84
172
  /**
85
173
  * 加载 CSS 文件
86
174
  * @param {String} cssPath
175
+ * @returns {Promise<void>}
176
+ * @example
177
+ * await loadCSS("index.css");
87
178
  */
88
179
  loadCSS: async function (cssPath) {
89
180
  return new Promise(async (resolve, reject) => {
@@ -96,31 +187,245 @@ const obj = {
96
187
  document.head.appendChild(link);
97
188
  });
98
189
  },
190
+ /**
191
+ * 加载 JS 文件
192
+ * @param {String} scriptPath
193
+ * @returns {Promise<void>}
194
+ * @example
195
+ * await loadScript("index.js");
196
+ */
197
+ loadScript: async function (scriptPath) {
198
+ return new Promise(async (resolve, reject) => {
199
+ const script = document.createElement('script');
200
+ script.type = 'text/javascript';
201
+ const scriptFile = await YeteDB.files.get({ name: scriptPath });
202
+ script.src = URL.createObjectURL(scriptFile.blob);
203
+ script.onload = resolve;
204
+ script.onerror = reject;
205
+ document.body.appendChild(script);
206
+ });
207
+ },
99
208
  /**
100
209
  * 跳转到指定页面
101
210
  * @param {String} path
211
+ * @param {String} message
212
+ * @throws {YeteError}
213
+ * @example
214
+ * toPage('/about');
102
215
  */
103
- toPage: function (path) {
216
+ toPage: function (path, message = null) {
104
217
  location.hash = path;
105
218
  const page = routes.find(r => r.path === path);
106
219
  console.log(pages, page);
107
220
  if (page) {
108
221
  document.body.innerHTML = "";
109
222
  document.body.appendChild(page.page);
223
+ obj.emit('page-change', new obj.YeteEvent("page-change", { path, isNotFound: false, message }));
110
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
+ }
111
231
  document.body.innerHTML = "<center><h1>404 Not Found</h1><p>Power By Yete</p></center>";
232
+ obj.emit('page-change', new obj.YeteEvent("page-change", { path, isNotFound: true }));
112
233
  throw new YeteError("The page not found.");
113
234
  }
114
- }
235
+ },
236
+ /**
237
+ * 创建 UI 元素
238
+ * @param {String} tag
239
+ * @param {Object} options
240
+ * @returns {HTMLElement}
241
+ * @example
242
+ * const button = newElement('Text', { className: 'btn', textContent: 'Click Me' });
243
+ */
244
+ newElement: function (tag, options = {}) {
245
+ if (!UIElements[tag]) {
246
+ throw new YeteError(`The element ${tag} not found.`);
247
+ }
248
+ const element = document.createElement(UIElements[tag].html);
249
+ const style = UIElements[tag].style;
250
+ if (style) {
251
+ for (const key in style) {
252
+ element.style[key] = style[key];
253
+ }
254
+ }
255
+ for (const key in options) {
256
+ element[key] = options[key];
257
+ }
258
+ return element;
259
+ },
260
+ /**
261
+ * 添加事件监听器
262
+ * @param {String} type
263
+ * @param {Function} listener
264
+ * @example
265
+ * addEventListener('click', () => console.log('Clicked'));
266
+ */
267
+ addEventListener: function (type, listener) {
268
+ events.push({ type, listener });
269
+ },
270
+ /**
271
+ * 移除事件监听器
272
+ * @param {String} type
273
+ * @param {Function} listener
274
+ * @example
275
+ * removeEventListener('click', () => console.log('Clicked'));
276
+ */
277
+ removeEventListener: function (type, listener) {
278
+ const index = events.findIndex(e => e.type === type && e.listener === listener);
279
+ if (index !== -1) {
280
+ events.splice(index, 1);
281
+ }
282
+ },
283
+ /**
284
+ * 派发事件
285
+ * @param {String} type
286
+ * @param {Object} event
287
+ * @example
288
+ * dispatchEvent('click', new YeteEvent("click", { bubbles: true, cancelable: true }));
289
+ */
290
+ dispatchEvent: function (type, event) {
291
+ events.forEach(e => {
292
+ if (e.type === type) {
293
+ e.listener(event);
294
+ }
295
+ });
296
+ },
297
+ /**
298
+ * 创建事件
299
+ * @param {String} type
300
+ * @param {Object} options
301
+ * @returns {YeteEvent}
302
+ * @example
303
+ * const event = new YeteEvent("click", { bubbles: true, cancelable: true });
304
+ */
305
+ YeteEvent: class extends Event {
306
+ /**
307
+ * 构造函数
308
+ * @param {String} type
309
+ * @param {Object} options
310
+ * @example
311
+ * const event = new YeteEvent("click", { bubbles: true, cancelable: true });
312
+ */
313
+ constructor(type, options) {
314
+ super(type, { bubbles: true, cancelable: true });
315
+ for (const key in options) {
316
+ this[key] = options[key];
317
+ }
318
+ }
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
+ },
115
368
  };
116
369
 
117
- // 监听 hashchange 事件,实现页面跳转
370
+ /**
371
+ * 添加事件监听器
372
+ * @param {String} type
373
+ * @param {Function} listener
374
+ * @example
375
+ * on('click', () => console.log('Clicked'));
376
+ */
377
+ obj.on = obj.addEventListener;
378
+ /**
379
+ * 移除事件监听器
380
+ * @param {String} type
381
+ * @param {Function} listener
382
+ * @example
383
+ * off('click', () => console.log('Clicked'));
384
+ */
385
+ obj.off = obj.removeEventListener;
386
+ /**
387
+ * 派发事件
388
+ * @param {String} type
389
+ * @param {Object} event
390
+ * @example
391
+ * emit('click', new YeteEvent("click", { bubbles: true, cancelable: true }));
392
+ */
393
+ obj.emit = obj.dispatchEvent;
394
+
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
+
118
417
  window.addEventListener('hashchange', () => {
119
418
  const path = location.hash.slice(1) || '/';
120
419
  obj.toPage(path);
121
420
  });
122
421
 
422
+ console.log("%cYete v" + YeteVersion, "border-radius: 16px; font-size: 64px; color: #fff; background-color: #007bff; padding: 5px;margin: 5px;");
123
423
 
424
+ /**
425
+ * 获取文件类型
426
+ * @param {String} filePath
427
+ * @returns {String}
428
+ */
124
429
  function getFileType(filePath) {
125
430
  const ext = path.extname(filePath).toLowerCase();
126
431
  switch (ext) {
@@ -0,0 +1,4 @@
1
+ class Text extends HTMLElement {
2
+ }
3
+
4
+ customElements.define('yete-text', Text);
@@ -0,0 +1,15 @@
1
+ {
2
+ "name": "yete-ui",
3
+ "type": "ui",
4
+ "version": "0.0.1-alpha.1",
5
+ "ui": {
6
+ "Text": {
7
+ "html": "yete-text",
8
+ "style": null,
9
+ "attr": ["id", "class"]
10
+ }
11
+ },
12
+ "files": [
13
+ "Text.js"
14
+ ]
15
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@iftc/yete",
3
- "version": "0.0.1-alpha.1",
3
+ "version": "0.0.1-alpha.3",
4
4
  "description": "",
5
5
  "keywords": [
6
6
  "IFTC",
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: blob.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();