@iftc/yete 0.0.1-alpha.3 → 0.0.1-alpha.4

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/index.js CHANGED
@@ -67,6 +67,8 @@ let isStarted = false;
67
67
  let custom404Page = null;
68
68
  let custom502Page = null;
69
69
 
70
+ let searchParams = new URLSearchParams("");
71
+
70
72
  /**
71
73
  * 导出的对象
72
74
  */
@@ -230,7 +232,7 @@ const obj = {
230
232
  }
231
233
  document.body.innerHTML = "<center><h1>404 Not Found</h1><p>Power By Yete</p></center>";
232
234
  obj.emit('page-change', new obj.YeteEvent("page-change", { path, isNotFound: true }));
233
- throw new YeteError("The page not found.");
235
+ console.error(new YeteError("The page not found."));
234
236
  }
235
237
  },
236
238
  /**
@@ -365,8 +367,294 @@ const obj = {
365
367
  throw new YeteError("The page not found.");
366
368
  }
367
369
  },
370
+ /**
371
+ * HTTP 请求
372
+ * @description HTTP 请求封装,提供更便捷的方式进行 HTTP 请求
373
+ * @example
374
+ * const res = await HTTP.GET("https://example.com");
375
+ * const data = await res.json();
376
+ */
377
+ HTTP: class {
378
+ constructor() { }
379
+ /**
380
+ * GET 请求
381
+ * @param {String} url
382
+ * @param {Object} headers
383
+ * @returns {Promise<Response>}
384
+ * @example
385
+ * const res = await HTTP.GET("https://example.com");
386
+ * const data = await res.json();
387
+ */
388
+ static async GET(url, headers = {}) {
389
+ return await fetch(url, { headers });
390
+ }
391
+ /**
392
+ * POST 请求
393
+ * @param {String} url
394
+ * @param {Object} body
395
+ * @param {Object} headers
396
+ * @returns {Promise<Response>}
397
+ * @example
398
+ * const res = await HTTP.POST("https://example.com", { name: "Yete" });
399
+ * const data = await res.json();
400
+ */
401
+ static async POST(url, body, headers = {}) {
402
+ return await fetch(url, { method: "POST", body, headers });
403
+ }
404
+ /**
405
+ * PUT 请求
406
+ * @param {String} url
407
+ * @param {Object} body
408
+ * @param {Object} headers
409
+ * @returns {Promise<Response>}
410
+ * @example
411
+ * const res = await HTTP.PUT("https://example.com", { name: "Yete" });
412
+ * const data = await res.json();
413
+ */
414
+ static async PUT(url, body, headers = {}) {
415
+ return await fetch(url, { method: "PUT", body, headers });
416
+ }
417
+ /**
418
+ * DELETE 请求
419
+ * @param {String} url
420
+ * @param {Object} headers
421
+ * @returns {Promise<Response>}
422
+ * @example
423
+ * const res = await HTTP.DELETE("https://example.com");
424
+ * const data = await res.json();
425
+ */
426
+ static async DELETE(url, headers = {}) {
427
+ return await fetch(url, { method: "DELETE", headers });
428
+ }
429
+ /**
430
+ * PATCH 请求
431
+ * @param {String} url
432
+ * @param {Object} body
433
+ * @param {Object} headers
434
+ * @returns {Promise<Response>}
435
+ * @example
436
+ * const res = await HTTP.PATCH("https://example.com", { name: "Yete" });
437
+ * const data = await res.json();
438
+ */
439
+ static async PATCH(url, body, headers = {}) {
440
+ return await fetch(url, { method: "PATCH", body, headers });
441
+ }
442
+ /**
443
+ * HEAD 请求
444
+ * @param {String} url
445
+ * @param {Object} headers
446
+ * @returns {Promise<Response>}
447
+ * @example
448
+ * const res = await HTTP.HEAD("https://example.com");
449
+ * const data = await res.json();
450
+ */
451
+ static async HEAD(url, headers = {}) {
452
+ return await fetch(url, { method: "HEAD", headers });
453
+ }
454
+ /**
455
+ * OPTIONS 请求
456
+ * @param {String} url
457
+ * @param {Object} headers
458
+ * @returns {Promise<Response>}
459
+ * @example
460
+ * const res = await HTTP.OPTIONS("https://example.com");
461
+ * const data = await res.json();
462
+ */
463
+ static async OPTIONS(url, headers = {}) {
464
+ return await fetch(url, { method: "OPTIONS", headers });
465
+ }
466
+ /**
467
+ * TRACE 请求
468
+ * @param {String} url
469
+ * @param {Object} headers
470
+ * @returns {Promise<Response>}
471
+ * @example
472
+ * const res = await HTTP.TRACE("https://example.com");
473
+ * const data = await res.json();
474
+ */
475
+ static async TRACE(url, headers = {}) {
476
+ return await fetch(url, { method: "TRACE", headers });
477
+ }
478
+ /**
479
+ * CONNECT 请求
480
+ * @param {String} url
481
+ * @param {Object} headers
482
+ * @returns {Promise<Response>}
483
+ * @example
484
+ * const res = await HTTP.CONNECT("https://example.com");
485
+ * const data = await res.json();
486
+ */
487
+ static async CONNECT(url, headers = {}) {
488
+ return await fetch(url, { method: "CONNECT", headers });
489
+ }
490
+ /**
491
+ * 读取流
492
+ * @param {Response} response
493
+ * @param {Function} callback
494
+ * @returns {Promise<void>}
495
+ * @example
496
+ * await HTTP.readStream(res, ({ chunk, done }) => { console.log(done, chunk); });
497
+ */
498
+ static async readStream(response, callback = ({ chunk, done }) => { console.log(done, chunk); }) {
499
+ if (!response.ok) {
500
+ throw new YeteError("Response is not ok.");
501
+ }
502
+ if (!response.body) {
503
+ throw new YeteError("Response body is not available.");
504
+ }
505
+ const reader = response.body.getReader();
506
+ const decoder = new TextDecoder('utf-8');
507
+ try {
508
+ while (true) {
509
+ const { done, value } = await reader.read();
510
+ if (done) {
511
+ const finalChunk = decoder.decode();
512
+ callback({ chunk: finalChunk || null, done: true });
513
+ break;
514
+ }
515
+ const chunk = decoder.decode(value, { stream: true });
516
+ callback({ chunk, done: false });
517
+ }
518
+ } catch (error) {
519
+ throw new YeteError(`Stream reading failed: ${error.message}`);
520
+ } finally {
521
+ reader.releaseLock();
522
+ }
523
+ }
524
+ /**
525
+ * 读取响应体
526
+ * @param {Response} response
527
+ * @param {String} type
528
+ * @returns {Promise<any>}
529
+ * @example
530
+ * const data = await HTTP.readBody(res);
531
+ */
532
+ static async readBody(response, type = "text") {
533
+ if (!response.ok) {
534
+ throw new YeteError("Response is not ok.");
535
+ }
536
+ if (!response.body) {
537
+ throw new YeteError("Response body is not available.");
538
+ }
539
+ switch (type) {
540
+ case "text":
541
+ return await response.text();
542
+ case "json":
543
+ return await response.json();
544
+ case "blob":
545
+ return await response.blob();
546
+ case "arrayBuffer":
547
+ return await response.arrayBuffer();
548
+ case "formData":
549
+ return await response.formData();
550
+ default:
551
+ throw new YeteError("Invalid type.");
552
+ }
553
+ }
554
+ /**
555
+ * 读取响应头
556
+ * @param {Response} response
557
+ * @returns {Promise<Headers>}
558
+ * @example
559
+ * const headers = await HTTP.readHeaders(res);
560
+ */
561
+ static async readHeaders(response) {
562
+ return response.headers;
563
+ }
564
+ },
565
+ /**
566
+ * 获取 URL 参数
567
+ * @example
568
+ * const params = searchParams;
569
+ */
570
+ searchParams: function () {
571
+ return searchParams;
572
+ },
573
+ /**
574
+ * 加载 UI
575
+ * @description 可通过 URL 或 XML 字符串加载 UI
576
+ * @param {String} str
577
+ * @returns {Promise<HTMLElement>}
578
+ * @example
579
+ * await loadUI("https://example.com/ui.xml");
580
+ * await loadUI(`<Yete><Text>Hello World</Text></Yete>`);
581
+ */
582
+ loadUI: async function (str) {
583
+ let isURL = false;
584
+ try {
585
+ new URL(str);
586
+ isURL = true;
587
+ } catch { }
588
+ if (isURL) {
589
+ const res = await fetch(str);
590
+ const text = await res.text();
591
+ return loadUI(text);
592
+ } else {
593
+ return loadUI(str);
594
+ }
595
+ }
368
596
  };
369
597
 
598
+ /**
599
+ * 加载 UI
600
+ * @param {String} xmlstring
601
+ * @returns {HTMLElement}
602
+ */
603
+ function loadUI(xmlstring) {
604
+ const parser = new DOMParser();
605
+ try {
606
+ const xmlDoc = parser.parseFromString(xmlstring, "text/xml");
607
+ const root = xmlDoc.documentElement;
608
+ if (root.tagName === "Yete") {
609
+ let html = "<div yete-root>";
610
+ const rootNodes = root.childNodes;
611
+ for (let i = 0; i < rootNodes.length; i++) {
612
+ const node = rootNodes[i];
613
+ html += parseNode(node);
614
+ }
615
+ html += "</div>";
616
+ return new DOMParser().parseFromString(html, "text/html").body.firstElementChild;
617
+ } else {
618
+ throw new YeteError("Invalid XML string.");
619
+ }
620
+ } catch (error) {
621
+ throw new YeteError(`Failed to LoadUI : ${error.message}`);
622
+ }
623
+ function parseNode(node) {
624
+ const tagName = node.tagName;
625
+ if (node.nodeName === "#text") {
626
+ return node.textContent;
627
+ }
628
+ if (node instanceof NodeList) {
629
+ let html = "";
630
+ for (let i = 0; i < node.length; i++) {
631
+ html += parseNode(node[i]);
632
+ }
633
+ return html;
634
+ }
635
+ if (UIElements[tagName]) {
636
+ const htmlTagName = UIElements[tagName].html;
637
+ let attrs = "";
638
+ for (const i in UIElements[tagName].attr) {
639
+ const attr = UIElements[tagName].attr[i];
640
+ console.log(node.hasAttribute(attr), attr);
641
+ if (node.hasAttribute(attr)) {
642
+ const value = node.getAttribute(attr);
643
+ console.log(attr, value);
644
+ if (value == "true") {
645
+ attrs += ` ${attr == "id" ? `${"yete-id"}` : attr}`;
646
+ } else {
647
+ attrs += ` ${attr == "id" ? `${"yete-id"}` : attr}="${value}"`;
648
+ }
649
+ }
650
+ }
651
+ return `<${htmlTagName} ${attrs} style="${UIElements[tagName].style || ""}">${parseNode(node.childNodes)}</${htmlTagName}>`;
652
+ } else {
653
+ throw new YeteError(`Invalid tag name: ${tagName}`);
654
+ }
655
+ }
656
+ }
657
+
370
658
  /**
371
659
  * 添加事件监听器
372
660
  * @param {String} type
@@ -392,6 +680,7 @@ obj.off = obj.removeEventListener;
392
680
  */
393
681
  obj.emit = obj.dispatchEvent;
394
682
 
683
+
395
684
  window.addEventListener("error", function (event) {
396
685
  const { message, filename, lineno, colno, error } = event;
397
686
  if (custom502Page) {
@@ -409,13 +698,20 @@ window.addEventListener("error", function (event) {
409
698
  if (errorErrorEl) errorErrorEl.innerText = error;
410
699
  obj.emit('page-change', new obj.YeteEvent("page-change", { path, isError: true }));
411
700
  } else {
412
- document.body.innerHTML = `<center><h1>Have an error: "${message}" in "${filename}" at ${lineno}:${colno}</h1><p>Powsered by Yete.</p></center>`;
701
+ document.body.innerHTML = `<center><h1>Have an error: "${message}" in "${filename}" at ${lineno}:${colno}</h1><p>Power by Yete.</p></center>`;
413
702
  obj.emit('page-change', new obj.YeteEvent("page-change", { path, isError: true }));
414
703
  }
415
704
  });
416
705
 
417
706
  window.addEventListener('hashchange', () => {
418
- const path = location.hash.slice(1) || '/';
707
+ const fullpath = location.hash.slice(1) || '/';
708
+ console.log("[Yete]", "Full path", fullpath);
709
+ const SearchParams = fullpath.split("?").slice(1).join("?") || '';
710
+ const path = fullpath.split("?")[0];
711
+ const query = new URLSearchParams(SearchParams);
712
+ searchParams = query;
713
+ console.log("[Yete]", "Query", query);
714
+ console.log("[Yete]", "Navigate to", path);
419
715
  obj.toPage(path);
420
716
  });
421
717
 
@@ -0,0 +1,156 @@
1
+ class Button extends HTMLElement {
2
+ static get observedAttributes() {
3
+ return ['variant', 'size', 'disabled', 'loading', 'icon', 'icon-position'];
4
+ }
5
+
6
+ constructor() {
7
+ super();
8
+ this.attachShadow({ mode: "open" });
9
+ this._rippleElement = null;
10
+ }
11
+
12
+ connectedCallback() {
13
+ this._render();
14
+ this._addEventListeners();
15
+ }
16
+
17
+ attributeChangedCallback(name, oldValue, newValue) {
18
+ if (oldValue !== newValue) {
19
+ this._render();
20
+ }
21
+ }
22
+
23
+ _render() {
24
+ const variant = this.getAttribute('variant') || 'contained';
25
+ const size = this.getAttribute('size') || 'medium';
26
+ const disabled = this.hasAttribute('disabled');
27
+ const loading = this.hasAttribute('loading');
28
+ const icon = this.getAttribute('icon');
29
+ const iconPosition = this.getAttribute('icon-position') || 'start';
30
+
31
+ this.shadowRoot.innerHTML = `
32
+ <style>
33
+ :host {
34
+ display: inline-block;
35
+ user-select: none;
36
+ }
37
+ .button {
38
+ font-family: Roboto, sans-serif;
39
+ font-size: 14px;
40
+ font-weight: 500;
41
+ letter-spacing: 0.089em;
42
+ border-radius: 20px; /* Android风格圆角 */
43
+ border: none;
44
+ cursor: pointer;
45
+ position: relative;
46
+ overflow: hidden;
47
+ transition: all 0.2s ease;
48
+ display: inline-flex;
49
+ align-items: center;
50
+ justify-content: center;
51
+ gap: 8px;
52
+ box-shadow: 0 1px 3px rgba(0,0,0,0.12), 0 1px 2px rgba(0,0,0,0.24); /* Android阴影 */
53
+ min-height: 36px;
54
+ padding: 8px 16px;
55
+ }
56
+ .button:hover {
57
+ box-shadow: 0 3px 6px rgba(0,0,0,0.16), 0 3px 6px rgba(0,0,0,0.23);
58
+ }
59
+ .button:active {
60
+ box-shadow: 0 1px 3px rgba(0,0,0,0.12), 0 1px 2px rgba(0,0,0,0.24);
61
+ transform: translateY(1px);
62
+ }
63
+ }
64
+ .button:disabled {
65
+ cursor: not-allowed;
66
+ opacity: 0.6;
67
+ }
68
+ /* 变体样式 */
69
+ .button.contained {
70
+ background: #1976d2; /* Android蓝色 */
71
+ color: white;
72
+ }
73
+ .button.contained:hover {
74
+ background: #1565c0;
75
+ }
76
+ .button.contained:active {
77
+ background: #0d47a1;
78
+ }
79
+ .button.outlined {
80
+ background: transparent;
81
+ color: #1976d2;
82
+ border: 1px solid #1976d2;
83
+ }
84
+ .button.outlined:hover {
85
+ background: rgba(25, 118, 210, 0.04);
86
+ }
87
+ .button.text {
88
+ background: transparent;
89
+ color: #1976d2;
90
+ box-shadow: none;
91
+ }
92
+ .button.text:hover {
93
+ background: rgba(25, 118, 210, 0.04);
94
+ }
95
+ /* 尺寸样式 */
96
+ .button.small { padding: 6px 12px; min-height: 32px; font-size: 12px; }
97
+ .button.medium { padding: 8px 16px; min-height: 36px; font-size: 14px; }
98
+ .button.large { padding: 10px 20px; min-height: 44px; font-size: 16px; }
99
+ /* 波纹效果 */
100
+ .ripple {
101
+ position: absolute;
102
+ border-radius: 50%;
103
+ background: rgba(255, 255, 255, 0.5);
104
+ transform: scale(0);
105
+ animation: ripple-animation 0.6s linear;
106
+ pointer-events: none;
107
+ }
108
+ @keyframes ripple-animation {
109
+ to { transform: scale(4); opacity: 0; }
110
+ }
111
+ /* 加载状态 */
112
+ .spinner {
113
+ width: 18px;
114
+ height: 18px;
115
+ border: 2px solid currentColor;
116
+ border-top-color: transparent;
117
+ border-radius: 50%;
118
+ animation: spin 0.8s linear infinite;
119
+ }
120
+ @keyframes spin {
121
+ to { transform: rotate(360deg); }
122
+ }
123
+ </style>
124
+ <button class="button ${variant} ${size}" ?disabled="${disabled}">
125
+ ${loading ? '<span class="spinner"></span>' : ''}
126
+ ${!loading && icon && iconPosition === 'start' ? `<span class="icon">${icon}</span>` : ''}
127
+ <slot></slot>
128
+ ${!loading && icon && iconPosition === 'end' ? `<span class="icon">${icon}</span>` : ''}
129
+ </button>
130
+ `;
131
+ }
132
+
133
+ _addEventListeners() {
134
+ const button = this.shadowRoot.querySelector('.button');
135
+ button?.addEventListener('click', (e) => this._handleRipple(e));
136
+ }
137
+
138
+ _handleRipple(event) {
139
+ if (this.hasAttribute('disabled')) return;
140
+
141
+ const button = event.currentTarget;
142
+ const rect = button.getBoundingClientRect();
143
+ const size = Math.max(rect.width, rect.height);
144
+
145
+ this._rippleElement = document.createElement('span');
146
+ this._rippleElement.classList.add('ripple');
147
+ this._rippleElement.style.width = this._rippleElement.style.height = `${size}px`;
148
+ this._rippleElement.style.left = `${event.clientX - rect.left - size / 2}px`;
149
+ this._rippleElement.style.top = `${event.clientY - rect.top - size / 2}px`;
150
+
151
+ button.appendChild(this._rippleElement);
152
+ setTimeout(() => this._rippleElement?.remove(), 600);
153
+ }
154
+ }
155
+
156
+ customElements.define("yete-button", Button);
@@ -1,4 +1,21 @@
1
1
  class Text extends HTMLElement {
2
+ constructor() {
3
+ super();
4
+ this.attachShadow({ mode: 'open' });
5
+ }
6
+
7
+ connectedCallback() {
8
+ this.shadowRoot.innerHTML = `
9
+ <style>
10
+ :host {
11
+ display: block;
12
+ color: var(--text-color, #757575);
13
+ user-select: none;
14
+ }
15
+ </style>
16
+ <slot></slot>
17
+ `;
18
+ }
2
19
  }
3
20
 
4
21
  customElements.define('yete-text', Text);
@@ -7,9 +7,15 @@
7
7
  "html": "yete-text",
8
8
  "style": null,
9
9
  "attr": ["id", "class"]
10
+ },
11
+ "Button": {
12
+ "html": "yete-button",
13
+ "style": null,
14
+ "attr": ["id", "class"]
10
15
  }
11
16
  },
12
17
  "files": [
13
- "Text.js"
18
+ "Text.js",
19
+ "Button.js"
14
20
  ]
15
21
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@iftc/yete",
3
- "version": "0.0.1-alpha.3",
3
+ "version": "0.0.1-alpha.4",
4
4
  "description": "",
5
5
  "keywords": [
6
6
  "IFTC",