amateras 0.5.0 → 0.6.0

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.
Files changed (81) hide show
  1. package/README.md +23 -26
  2. package/ext/html/node/$Anchor.ts +2 -2
  3. package/ext/html/node/$Canvas.ts +2 -2
  4. package/ext/html/node/$Dialog.ts +2 -2
  5. package/ext/html/node/$Form.ts +2 -2
  6. package/ext/html/node/$Image.ts +2 -2
  7. package/ext/html/node/$Input.ts +2 -2
  8. package/ext/html/node/$Label.ts +2 -2
  9. package/ext/html/node/$Media.ts +2 -2
  10. package/ext/html/node/$OptGroup.ts +2 -2
  11. package/ext/html/node/$Option.ts +2 -2
  12. package/ext/html/node/$Select.ts +2 -2
  13. package/ext/html/node/$TextArea.ts +2 -2
  14. package/ext/i18n/README.md +20 -0
  15. package/ext/i18n/src/index.ts +106 -12
  16. package/ext/i18n/src/structure/I18n.ts +12 -8
  17. package/ext/i18n/src/structure/I18nTranslation.ts +35 -0
  18. package/ext/idb/src/structure/builder/$IDBBuilder.ts +8 -8
  19. package/ext/markdown/README.md +53 -0
  20. package/ext/markdown/package.json +7 -0
  21. package/ext/markdown/src/index.ts +3 -0
  22. package/ext/markdown/src/lib/type.ts +26 -0
  23. package/ext/markdown/src/lib/util.ts +21 -0
  24. package/ext/markdown/src/structure/Markdown.ts +54 -0
  25. package/ext/markdown/src/structure/MarkdownLexer.ts +111 -0
  26. package/ext/markdown/src/structure/MarkdownParser.ts +33 -0
  27. package/ext/markdown/src/syntax/alert.ts +46 -0
  28. package/ext/markdown/src/syntax/blockquote.ts +35 -0
  29. package/ext/markdown/src/syntax/bold.ts +11 -0
  30. package/ext/markdown/src/syntax/code.ts +11 -0
  31. package/ext/markdown/src/syntax/codeblock.ts +44 -0
  32. package/ext/markdown/src/syntax/heading.ts +14 -0
  33. package/ext/markdown/src/syntax/horizontalRule.ts +11 -0
  34. package/ext/markdown/src/syntax/image.ts +23 -0
  35. package/ext/markdown/src/syntax/italic.ts +11 -0
  36. package/ext/markdown/src/syntax/link.ts +46 -0
  37. package/ext/markdown/src/syntax/list.ts +121 -0
  38. package/ext/markdown/src/syntax/table.ts +67 -0
  39. package/ext/markdown/src/syntax/text.ts +19 -0
  40. package/ext/router/README.md +111 -17
  41. package/ext/router/package.json +10 -0
  42. package/ext/router/src/index.ts +69 -0
  43. package/ext/router/src/node/Page.ts +34 -0
  44. package/ext/router/src/node/Router.ts +191 -0
  45. package/ext/router/{node → src/node}/RouterAnchor.ts +13 -2
  46. package/ext/router/src/structure/PageBuilder.ts +24 -0
  47. package/ext/router/src/structure/Route.ts +105 -0
  48. package/ext/signal/README.md +93 -0
  49. package/ext/signal/package.json +9 -0
  50. package/ext/signal/src/index.ts +128 -0
  51. package/{src → ext/signal/src}/structure/Signal.ts +6 -10
  52. package/ext/ssr/index.ts +4 -4
  53. package/ext/ui/lib/VirtualScroll.ts +25 -0
  54. package/ext/ui/node/Accordian.ts +97 -0
  55. package/ext/ui/node/Form.ts +53 -0
  56. package/ext/ui/node/Grid.ts +0 -0
  57. package/ext/ui/node/Table.ts +43 -0
  58. package/ext/ui/node/Tabs.ts +114 -0
  59. package/ext/ui/node/Toast.ts +16 -0
  60. package/ext/ui/node/Waterfall.ts +72 -0
  61. package/ext/ui/package.json +11 -0
  62. package/package.json +6 -3
  63. package/src/core.ts +30 -60
  64. package/src/global.ts +9 -2
  65. package/src/index.ts +1 -2
  66. package/src/lib/assignProperties.ts +57 -0
  67. package/src/lib/native.ts +25 -8
  68. package/src/lib/uppercase.ts +3 -0
  69. package/src/node/$Element.ts +7 -41
  70. package/src/node/$EventTarget.ts +45 -0
  71. package/src/node/$Node.ts +60 -65
  72. package/src/node/$Virtual.ts +65 -0
  73. package/src/node.ts +7 -6
  74. package/ext/i18n/src/node/I18nText.ts +0 -35
  75. package/ext/markdown/index.ts +0 -121
  76. package/ext/router/index.ts +0 -73
  77. package/ext/router/node/Page.ts +0 -27
  78. package/ext/router/node/Route.ts +0 -54
  79. package/ext/router/node/Router.ts +0 -149
  80. package/src/lib/assign.ts +0 -38
  81. package/src/lib/assignHelper.ts +0 -18
@@ -1,149 +0,0 @@
1
- import type { AnchorTarget } from "#html/$Anchor";
2
- import { _document } from "#lib/env";
3
- import { _Array_from, _instanceof, _JSON_parse, _JSON_stringify, _Object_entries, _Object_fromEntries, forEach, startsWith } from "#lib/native";
4
- import { Page } from "./Page";
5
- import { BaseRouteNode, Route } from "./Route";
6
-
7
- // history index
8
- let index = 0;
9
- const _addEventListener = addEventListener;
10
- const _location = location;
11
- const {origin} = _location;
12
- const _history = history;
13
- const _sessionStorage = sessionStorage;
14
- const documentElement = _document.documentElement;
15
- const [PUSH, REPLACE] = [1, 2] as const;
16
- const [FORWARD, BACK] = ['forward', 'back'] as const;
17
- const scrollStorageKey = '__scroll__';
18
- /** convert path string to URL object */
19
- const toURL = (path: string | URL) =>
20
- _instanceof(path, URL) ? path : startsWith(path, 'http') ? new URL(path) : new URL(startsWith(path, origin) ? path : origin + path);
21
-
22
- type ScrollData = {[key: number]: {x: number, y: number}};
23
- const scrollRecord = (e?: Event) => {
24
- const data = _JSON_parse(_sessionStorage.getItem(scrollStorageKey) ?? '{}') as ScrollData;
25
- data[index] = { x: documentElement.scrollLeft, y: documentElement.scrollTop };
26
- // e is Event when called from scroll or beforeload
27
- if (!e) forEach(_Object_entries(data), ([i]) => +i > index && delete data[+i])
28
- _sessionStorage.setItem(scrollStorageKey, _JSON_stringify(data));
29
- }
30
- /** handle history state with push and replace state. */
31
- const historyHandler = async (path: string | URL | Nullish, mode: 1 | 2, target?: AnchorTarget) => {
32
- if (!path) return;
33
- const url = toURL(path);
34
- if (url.href === _location.href) return;
35
- if (target && target !== '_self') return open(url, target);
36
- if (url.origin !== origin) return open(url, target);
37
- scrollRecord();
38
- if (mode === PUSH) index += 1;
39
- Router.direction = FORWARD;
40
- _history[mode === PUSH ? 'pushState' : 'replaceState']({index}, '' , url);
41
- for (let router of Router.routers) router.routes.size && await router.resolve(path);
42
- }
43
- // disable browser scroll restoration
44
- _history.scrollRestoration = 'manual';
45
-
46
- export class Router extends BaseRouteNode<''> {
47
- static pageRouters = new Map<Page, Router>();
48
- static routers = new Set<Router>();
49
- pageMap = new Map<string, Page>();
50
- static direction: 'back' | 'forward' = FORWARD;
51
- constructor(page?: Page) {
52
- super('', () => [], 'router')
53
- Router.routers.add(this);
54
- if (page) Router.pageRouters.set(page, this);
55
- }
56
-
57
- static open(path: string | URL | Nullish, target?: AnchorTarget) {
58
- historyHandler(path, PUSH, target);
59
- return this;
60
- }
61
-
62
- static back() {
63
- _history.back();
64
- return this;
65
- }
66
-
67
- static forward() {
68
- _history.forward();
69
- return this;
70
- }
71
-
72
- static replace(path: string | URL | Nullish) {
73
- historyHandler(path, REPLACE);
74
- return this;
75
- }
76
-
77
- async resolve(path: string | URL) {
78
- const {pathname, searchParams, hash, href} = toURL(path);
79
- const routeData = { params: {} as {[key: string]: string}, query: _Object_fromEntries(searchParams) }
80
- const split = (p: string) => p.replaceAll(/\/+/g, '/').split('/').map(path => `/${path}`);
81
-
82
- function determineRoute(parentRoute: BaseRouteNode<any>, path: string, hash: string | undefined): [route: Route | null, pathId: string][] {
83
- const targetPathSplit = split(path);
84
- hash && targetPathSplit.push(hash);
85
- if (!parentRoute.routes.size) return [];
86
- routeLoop: for (const route of _Array_from(parentRoute.routes.values()).sort((a, b) => b.path.length - a.path.length)) {
87
- const routePathSplit = split(route.path);
88
- let pathId = '';
89
- splitLoop: for (let i = 0; i < routePathSplit.length; i++) {
90
- const pass = () => pathId += targetSnippet;
91
- const [routeSnippet, targetSnippet] = [routePathSplit[i], targetPathSplit[i]];
92
- if (!routeSnippet || !targetSnippet) continue routeLoop;
93
- // process params in path
94
- if (routeSnippet.includes(':')) {
95
- if (targetSnippet === '/') continue routeLoop;
96
- const [prefix, paramName] = routeSnippet.split(':') as [string, string];
97
- if (!startsWith(targetSnippet, prefix)) continue routeLoop;
98
- routeData.params[paramName] = targetSnippet.replace(`${prefix}`, '');
99
- pass();
100
- continue splitLoop;
101
- }
102
- if (routeSnippet === '/' && route.routes.size) continue splitLoop;
103
- if (routeSnippet !== targetSnippet) continue routeLoop;
104
- pass()
105
- }
106
- return [[route, pathId], ...determineRoute(route, path, hash)];
107
- }
108
- return [[null, parentRoute.path + '$$NOT_FOUND$$']];
109
- }
110
- // analytics
111
- const targetRoutes = determineRoute(this, pathname + '/', hash);
112
- // build pages
113
- let prevPage: null | Page = null, prevRoute: BaseRouteNode<any> = this;
114
- const appendPage = (prevRouter: Router | undefined, page: Page) => page.parentNode !== prevRouter?.node && prevRouter?.content(page);
115
-
116
- for (const [route, pathId] of targetRoutes) {
117
- const page = this.pageMap.get(pathId) ?? new Page(route ?? prevRoute.routes.get('404') ?? new Route('404', () => null), routeData);
118
- if (!page.initial) await route?.build(routeData, page);
119
- _document && (_document.title = page.pageTitle() ?? _document.title);
120
- this.pageMap.set(pathId, page);
121
- if (href === _location.href) appendPage(prevPage ? Router.pageRouters.get(prevPage) : this, page);
122
- prevPage = page;
123
- if (route) prevRoute = route;
124
- }
125
- let { x, y } = Router.scroll ?? {x: 0, y: 0};
126
- scrollTo(x, y);
127
- this.dispatchEvent(new Event('routeopen', {bubbles: true}));
128
- return this;
129
- }
130
-
131
- listen() {
132
- const resolve = () => {
133
- const stateIndex = _history.state?.index ?? 0;
134
- if (index > stateIndex) Router.direction = BACK;
135
- if (index < stateIndex) Router.direction = FORWARD;
136
- index = stateIndex;
137
- this.resolve(_location.href);
138
- }
139
- _addEventListener('popstate', resolve);
140
- _addEventListener('beforeunload', scrollRecord);
141
- _addEventListener('scroll', scrollRecord, false);
142
- resolve();
143
- return this;
144
- }
145
-
146
- static get scroll(): ScrollData[number] {
147
- return _JSON_parse(_sessionStorage.getItem(scrollStorageKey) ?? '{}')[index] ?? {x: 0, y: 0}
148
- }
149
- }
package/src/lib/assign.ts DELETED
@@ -1,38 +0,0 @@
1
- import { Signal } from "../structure/Signal";
2
- import { _instanceof, _Object_defineProperty, forEach, isUndefined } from "./native";
3
-
4
- export const assign = (target: any, {set, get, fn}: {
5
- set?: string[],
6
- get?: string[],
7
- fn?: string[]
8
- }) => {
9
- const [GET, SET, FN] = ['get', 'set', 'fn'] as const;
10
- const filterAndMap = (type: 'get' | 'set' | 'fn', arr: string[] | undefined) => arr?.map(prop => [type, prop]) ?? []
11
- const list = [...filterAndMap(GET, get), ...filterAndMap(SET, set), ...filterAndMap(FN, fn)] as [string, string][];
12
- forEach(list, ([type, prop]) =>
13
- _Object_defineProperty(target.prototype, prop, {
14
- ...(type === GET ? {
15
- get() { return this.node[prop as any] }
16
- } : {
17
- writable: true,
18
- ...(type === SET ? {
19
- // set
20
- value(this, args: any) {
21
- if (!arguments.length) return this.node[prop];
22
- let set = (value: any) => !isUndefined(value) && (this.node[prop] = value);
23
- if (_instanceof(args, Signal)) args = args.subscribe(set).value();
24
- set(args)
25
- return this;
26
- }
27
- } : {
28
- // fn
29
- value(this, ...args : any[]) {
30
- let result = this.node[prop](...args)
31
- return isUndefined(result) ? this : result;
32
- }
33
- })
34
- }),
35
-
36
- })
37
- )
38
- }
@@ -1,18 +0,0 @@
1
- import type { $Node } from "#node/$Node";
2
- import { assign } from "./assign";
3
- import { _Object_entries, _Object_getOwnPropertyDescriptors, forEach } from "./native";
4
-
5
- export const assignHelper = (object: Constructor<EventTarget>, target: Constructor<$Node>, tagname?: string) => {
6
- const [set, get, fn] = [[], [], []] as [string[], string[], string[]]
7
- // assign native object properties to target
8
- forEach(_Object_entries(_Object_getOwnPropertyDescriptors(object.prototype)), ([prop, value]) => {
9
- if (!(prop in target.prototype)) {
10
- if (value.get && !value.set) get.push(prop);
11
- else if (value.value) fn.push(prop);
12
- else if (value.get && value.set) set.push(prop);
13
- }
14
- })
15
- assign(target, {set, get, fn})
16
- // register tagname
17
- if (tagname) $.assign(tagname, target)
18
- }