@iftc/yete 0.0.1-alpha.0 → 0.0.1-alpha.2

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,39 @@
1
+ # Yete
2
+
3
+ Yete 是一个轻量级、可扩展的 Web 框架。使用 Dexie 库操作 IndexedDB,将资源文件缓存到浏览器中,并支持离线访问。
4
+ 开发文档:https://iftc.koyeb.app/docs/yete
5
+
6
+ ## 快速开始
7
+
8
+ 1. 下载 Yete CLI:
9
+ - [Windows x64](https://dbmp-xbgmorqeur6oh81z.database.nocode.cn/storage/v1/object/public/files/yete.exe "yete.exe")
10
+ - [Linux arm64](https://dbmp-xbgmorqeur6oh81z.database.nocode.cn/storage/v1/object/public/files/yete "yete")
11
+ - [CLI源文件(其他操作系统请自行编译或直接使用 Txiki.js 运行)](https://dbmp-xbgmorqeur6oh81z.database.nocode.cn/storage/v1/object/public/files/yete.zip "yete.zip")
12
+
13
+ 2. 创建并构建项目:
14
+
15
+ ```bash
16
+ yete init <project-name>
17
+ cd <project-name>
18
+ npm i
19
+ yete build && yete serve
20
+ ```
21
+
22
+ 3. 访问项目:
23
+ 浏览器访问 http://localhost:12345
24
+
25
+ ## 贡献
26
+
27
+ 欢迎任何形式的贡献,如提交 PR、提交 Issue、提交文档、提交翻译、提交代码片段。
28
+
29
+ ## 许可证
30
+
31
+ [MIT](https://github.com/IFTC-XLKJ/Yete/blob/main/LICENSE)
32
+
33
+ ## 联系作者
34
+
35
+ - 邮箱:iftcceo@139.com 或 iftcceo@gmail.com
36
+ - 社区:https://discord.gg/dWWaUjVWv6
37
+ - 官网:https://iftc.koyeb.app/
38
+ - 仓库:https://github.com/IFTC-XLKJ/yete
39
+ - 官方文档:https://iftc.koyeb.app/docs/yete
package/index.js CHANGED
@@ -1 +1,279 @@
1
- module.exports = {};
1
+ const { add } = require("dexie");
2
+
3
+ /**
4
+ * 自定义错误类
5
+ */
6
+ class YeteError extends Error {
7
+ /**
8
+ * 构造函数
9
+ * @param {String} message 错误信息
10
+ */
11
+ constructor(message) {
12
+ super(message);
13
+ this.name = 'YeteError';
14
+ }
15
+ }
16
+ /**
17
+ * 页面基类
18
+ */
19
+ class Page {
20
+ /**
21
+ * 构造函数
22
+ * @param {String} name
23
+ */
24
+ constructor(name) {
25
+ this.name = name;
26
+ }
27
+ /**
28
+ * 渲染页面
29
+ * @returns {HTMLElement}
30
+ */
31
+ render() {
32
+ return document.createElement('div', { className: 'page' }, this.name);
33
+ }
34
+ }
35
+
36
+ const pages = [];
37
+ const routes = [];
38
+ const events = [];
39
+
40
+ const obj = {
41
+ /**
42
+ * 设置页面图标
43
+ * @param {String} iconPath
44
+ * @returns
45
+ */
46
+ setIcon: function (iconPath) {
47
+ const fileType = getFileType(iconPath);
48
+ if (!fileType.startsWith('image/')) {
49
+ throw new YeteError("The icon path must be a valid image file.");
50
+ }
51
+ const iconLink = document.querySelector('link[rel="icon"]');
52
+ if (iconLink) {
53
+ iconLink.href = iconPath;
54
+ iconLink.type = fileType;
55
+ } else {
56
+ const newIconLink = document.createElement('link');
57
+ newIconLink.rel = 'icon';
58
+ newIconLink.href = iconPath;
59
+ newIconLink.type = fileType;
60
+ document.head.appendChild(newIconLink);
61
+ }
62
+ },
63
+ /**
64
+ * 设置页面标题
65
+ * @param {String} title
66
+ */
67
+ setTitle: function (title) {
68
+ document.title = title;
69
+ },
70
+ /**
71
+ * 配置路由
72
+ * @param {Array<Object>} options
73
+ * @example
74
+ * options: [
75
+ * { path: '/', page: () => loadPage(HomePage) },
76
+ * { path: '/about', page: () => loadPage(AboutPage) }
77
+ * ]
78
+ */
79
+ route: function (options) {
80
+ console.log(options);
81
+ routes.push(...options);
82
+ },
83
+ Page: Page,
84
+ /**
85
+ * 加载组件
86
+ * @param {Page} page
87
+ */
88
+ loadPage: function (page) {
89
+ console.log(page);
90
+ const p = new page();
91
+ if (p instanceof Page) {
92
+ pages.push(p);
93
+ return p.render();
94
+ }
95
+ throw new YeteError("The page must be an instance of Page.");
96
+ },
97
+ /**
98
+ * 启动程序
99
+ */
100
+ start: function () {
101
+ const path = location.hash.slice(1) || '/';
102
+ obj.toPage(path);
103
+ },
104
+ /**
105
+ * 加载 CSS 文件
106
+ * @param {String} cssPath
107
+ */
108
+ loadCSS: async function (cssPath) {
109
+ return new Promise(async (resolve, reject) => {
110
+ const link = document.createElement('link');
111
+ link.rel = 'stylesheet';
112
+ const cssFile = await YeteDB.files.get({ name: cssPath });
113
+ link.href = URL.createObjectURL(cssFile.blob);
114
+ link.onload = resolve;
115
+ link.onerror = reject;
116
+ document.head.appendChild(link);
117
+ });
118
+ },
119
+ /**
120
+ * 加载 JS 文件
121
+ * @param {String} scriptPath
122
+ */
123
+ loadScript: async function (scriptPath) {
124
+ return new Promise(async (resolve, reject) => {
125
+ const script = document.createElement('script');
126
+ script.type = 'text/javascript';
127
+ const scriptFile = await YeteDB.files.get({ name: scriptPath });
128
+ script.src = URL.createObjectURL(scriptFile.blob);
129
+ script.onload = resolve;
130
+ script.onerror = reject;
131
+ document.body.appendChild(script);
132
+ });
133
+ },
134
+ /**
135
+ * 跳转到指定页面
136
+ * @param {String} path
137
+ */
138
+ toPage: function (path) {
139
+ location.hash = path;
140
+ const page = routes.find(r => r.path === path);
141
+ console.log(pages, page);
142
+ if (page) {
143
+ document.body.innerHTML = "";
144
+ document.body.appendChild(page.page);
145
+ obj.emit('page-change', new obj.YeteEvent("page-change", { path, isNotFound: false }));
146
+ } else {
147
+ document.body.innerHTML = "<center><h1>404 Not Found</h1><p>Power By Yete</p></center>";
148
+ obj.emit('page-change', new obj.YeteEvent("page-change", { path, isNotFound: true }));
149
+ throw new YeteError("The page not found.");
150
+ }
151
+ },
152
+ /**
153
+ * 创建 UI 元素
154
+ * @param {String} tag
155
+ * @param {Object} options
156
+ * @returns {HTMLElement}
157
+ */
158
+ newElement: function (tag, options = {}) {
159
+ if (!UIElements[tag]) {
160
+ throw new YeteError(`The element ${tag} not found.`);
161
+ }
162
+ const element = document.createElement(UIElements[tag].html);
163
+ const style = UIElements[tag].style;
164
+ if (style) {
165
+ for (const key in style) {
166
+ element.style[key] = style[key];
167
+ }
168
+ }
169
+ for (const key in options) {
170
+ element[key] = options[key];
171
+ }
172
+ return element;
173
+ },
174
+ /**
175
+ * 添加事件监听器
176
+ * @param {String} type
177
+ * @param {Function} listener
178
+ */
179
+ addEventListener: function (type, listener) {
180
+ events.push({ type, listener });
181
+ },
182
+ /**
183
+ * 移除事件监听器
184
+ * @param {String} type
185
+ * @param {Function} listener
186
+ */
187
+ removeEventListener: function (type, listener) {
188
+ const index = events.findIndex(e => e.type === type && e.listener === listener);
189
+ if (index !== -1) {
190
+ events.splice(index, 1);
191
+ }
192
+ },
193
+ /**
194
+ * 派发事件
195
+ * @param {String} type
196
+ * @param {Object} event
197
+ */
198
+ dispatchEvent: function (type, event) {
199
+ events.forEach(e => {
200
+ if (e.type === type) {
201
+ e.listener(event);
202
+ }
203
+ });
204
+ },
205
+ /**
206
+ * 创建事件
207
+ */
208
+ YeteEvent: class extends Event {
209
+ /**
210
+ * 构造函数
211
+ * @param {String} type
212
+ * @param {Object} options
213
+ */
214
+ constructor(type, options) {
215
+ super(type, { bubbles: true, cancelable: true });
216
+ for (const key in options) {
217
+ this[key] = options[key];
218
+ }
219
+ }
220
+ },
221
+ };
222
+
223
+ /**
224
+ * 添加事件监听器
225
+ * @param {String} type
226
+ * @param {Function} listener
227
+ */
228
+ obj.on = obj.addEventListener;
229
+ /**
230
+ * 移除事件监听器
231
+ * @param {String} type
232
+ * @param {Function} listener
233
+ */
234
+ obj.off = obj.removeEventListener;
235
+ /**
236
+ * 派发事件
237
+ * @param {String} type
238
+ * @param {Object} event
239
+ */
240
+ obj.emit = obj.dispatchEvent;
241
+
242
+ // 监听 hashchange 事件,实现页面跳转
243
+ window.addEventListener('hashchange', () => {
244
+ const path = location.hash.slice(1) || '/';
245
+ obj.toPage(path);
246
+ });
247
+
248
+ /**
249
+ * 获取文件类型
250
+ * @param {String} filePath
251
+ * @returns {String}
252
+ */
253
+ function getFileType(filePath) {
254
+ const ext = path.extname(filePath).toLowerCase();
255
+ switch (ext) {
256
+ case '.js':
257
+ return 'application/javascript';
258
+ case '.css':
259
+ return 'text/css';
260
+ case '.html':
261
+ return 'text/html';
262
+ case '.svg':
263
+ return 'image/svg+xml';
264
+ case '.png':
265
+ return 'image/png';
266
+ case '.jpg':
267
+ return 'image/jpeg';
268
+ case '.jpeg':
269
+ return 'image/jpeg';
270
+ case '.gif':
271
+ return 'image/gif';
272
+ case '.ico':
273
+ return 'image/x-icon';
274
+ default:
275
+ return 'application/octet-stream';
276
+ }
277
+ }
278
+
279
+ module.exports = obj;
@@ -0,0 +1,12 @@
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
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@iftc/yete",
3
- "version": "0.0.1-alpha.0",
3
+ "version": "0.0.1-alpha.2",
4
4
  "description": "",
5
5
  "keywords": [
6
6
  "IFTC",
package/src/loader.js ADDED
@@ -0,0 +1,58 @@
1
+ const { Dexie } = require('dexie');
2
+
3
+ console.log(Dexie);
4
+ const db = new Dexie('YeteDB');
5
+ db.version(1).stores({
6
+ files: "path, name, size, type, lastModified"
7
+ });
8
+ db.open();
9
+ globalThis.YeteDB = db;
10
+
11
+ globalThis.files = [];
12
+ async function loadFiles() {
13
+ const files = await db.files.toArray();
14
+ globalThis.files = files;
15
+ console.log(files);
16
+ await loadIndex();
17
+ }
18
+
19
+ async function loadIndex() {
20
+ const index = await db.files.get({ name: 'index.js' });
21
+ if (index) {
22
+ const url = URL.createObjectURL(index.blob);
23
+ const script = document.createElement('script');
24
+ script.src = url;
25
+ document.body.appendChild(script);
26
+ } else {
27
+ console.error("Error: index.js not found in the database.");
28
+ }
29
+ }
30
+
31
+ async function updateFiles() {
32
+ const files = yete.files;
33
+ for (const file of files) {
34
+ const r = await fetch(file);
35
+ const blob = await r.blob();
36
+ await db.files.put({
37
+ name: file.split('/').pop(),
38
+ path: file,
39
+ size: blob.size,
40
+ type: blob.type,
41
+ lastModified: blob.lastModified,
42
+ blob: blob
43
+ });
44
+ }
45
+ }
46
+
47
+ async function main() {
48
+ const version = yete.version;
49
+ if (localStorage.getItem('yete-version') != version) {
50
+ await updateFiles();
51
+ await loadFiles();
52
+ localStorage.setItem('yete-version', version);
53
+ } else {
54
+ await loadFiles();
55
+ }
56
+ }
57
+
58
+ main();