@geektech/tsone 0.1.0 → 0.3.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.
package/dist/index.js CHANGED
@@ -1,608 +1,4 @@
1
- import {
2
- A,
3
- Article,
4
- Aside,
5
- Blockquote,
6
- Button,
7
- Code,
8
- Component,
9
- ComponentRenderStrategy,
10
- Div,
11
- ElementRenderStrategy,
12
- Em,
13
- Footer,
14
- Form,
15
- H1,
16
- H2,
17
- H3,
18
- H4,
19
- H5,
20
- H6,
21
- Header,
22
- Img,
23
- Input,
24
- Label,
25
- Li,
26
- Main,
27
- ModelBindingController,
28
- Nav,
29
- Ol,
30
- Option,
31
- P,
32
- Pre,
33
- ReactiveSystem,
34
- RendererContext,
35
- Router,
36
- RouterLink,
37
- RouterView,
38
- Section,
39
- Select,
40
- SlotRenderStrategy,
41
- Small,
42
- Span,
43
- Strong,
44
- Table,
45
- Tag,
46
- Tbody,
47
- Td,
48
- TemplateEngine,
49
- TextRenderStrategy,
50
- Textarea,
51
- Th,
52
- Thead,
53
- Tr,
54
- Ul,
55
- computed,
56
- createComponent,
57
- createRouter,
58
- each,
59
- effect,
60
- getModelValue,
61
- h,
62
- isComponentNode,
63
- isHTMLNode,
64
- isReactive,
65
- isReadonly,
66
- isRef,
67
- isSlotProvider,
68
- modelPath,
69
- normalizeTransitionGroupProps,
70
- reactive,
71
- readonly,
72
- ref,
73
- setModelValue,
74
- slot,
75
- stop,
76
- unref,
77
- useRouter,
78
- validateTransitionGroupChildren
79
- } from "./index-wv9gyjqt.js";
80
- import {
81
- renderStyleSheet
82
- } from "./index-vpx80nq5.js";
83
- import"./index-8wjswsye.js";
1
+ import{$ as Qe,A as Ve,Aa as xr,B as he,Ba as Tr,C as be,D as Pe,E as je,F as ke,G as ve,H as Fe,I as Me,J as Ce,K as Ge,L as Ae,M as we,N as Ee,O as Oe,P as Se,Q as qe,R as Ne,S as ze,T as Le,U as $e,V as Be,W as De,X as He,Y as Ie,Z as Je,_ as Ke,a as U,aa as Ue,b as W,ba as We,c as X,ca as Xe,d as Y,da as Ye,e as Z,ea as Ze,f as _,fa as _e,g as ee,ga as er,h as re,ha as rr,i as te,ia as tr,j as oe,ja as d,k as ne,ka as or,l as ie,la as nr,m as se,ma as ir,n as le,na as p,o as ae,oa as u,p as de,pa as sr,q as pe,qa as lr,r as ue,ra as ar,s as ce,sa as dr,t as fe,ta as pr,u as me,ua as c,v as ge,va as cr,w as xe,wa as m,x as Te,xa as fr,y as Re,ya as mr,z as ye,za as gr}from"./index-p55hs6x8.js";import{Da as ur}from"./index-939sv8vt.js";function g(e){return e instanceof Error?e.message:String(e)}function x(e,r,t){try{let o=e(r,t);if(o===!0)return;return o===!1?"Validation failed":o}catch(o){return g(o)}}function R(e="This field is required"){return(r)=>{if(r===null||r===void 0)return e;if(typeof r==="string"&&r.trim().length===0)return e;if(Array.isArray(r)&&r.length===0)return e;return!0}}function y(e,r=`Must be at least ${e} characters`){return(t)=>typeof t==="string"&&t.length>=e?!0:r}function V(e){return e}function h(e,r){let t={},o=(n)=>{let i=r[n]??[],s=d(e,n),l=i.map((a)=>x(a,s,e)).filter((a)=>a!==void 0);if(l.length===0)delete t[n];else t[n]=l;return{valid:l.length===0,errors:l}};return{model:e,errors:t,validate(){let n={};return Object.keys(r).forEach((i)=>{let s=o(i);if(!s.valid)n[i]=s.errors}),{valid:Object.keys(n).length===0,errors:n}},validateField:o,resetErrors(){Object.keys(t).forEach((n)=>delete t[n])}}}class f extends c{initState(){return{}}initStyles(){}render(){let{tag:e,type:r,duration:t}=p(this.props),o=u(this.props.children??[]);return{tag:e,props:this.props.elementProps,listeners:this.props.listeners,children:o,transitionGroup:{type:r,duration:t}}}}function D(e={}){return new m(e)}var H="0.3.0",I="@geektech/tsone";export{$e as A,Pe as Article,je as Aside,qe as Blockquote,xe as Button,Se as Code,c as Component,ar as ComponentRenderStrategy,fe as Div,ir as ElementRenderStrategy,we as Em,he as Footer,De as Form,ke as H1,ve as H2,Fe as H3,Me as H4,Ce as H5,Ge as H6,Ve as Header,Be as Img,Te as Input,He as Label,Le as Li,ye as Main,nr as ModelBindingController,be as Nav,ze as Ol,m as OneApp,Ke as Option,ge as P,Oe as Pre,Y as ReactiveSystem,sr as RendererContext,mr as Router,gr as RouterLink,xr as RouterView,Re as Section,Je as Select,dr as SlotRenderStrategy,Ee as Small,me as Span,Ae as Strong,Qe as Table,ce as Tag,We as Tbody,Ze as Td,pr as TemplateEngine,lr as TextRenderStrategy,Ie as Textarea,Ye as Th,Ue as Thead,Xe as Tr,f as TransitionGroup,Ne as Ul,re as computed,D as createApp,_e as createComponent,h as createForm,Tr as createRouter,rr as each,ee as effect,X as flushSync,d as getModelValue,ue as h,ae as isComponentNode,de as isHTMLNode,se as isReactive,le as isReadonly,oe as isRef,pe as isSlotProvider,y as minLength,tr as modelPath,I as name,W as nextTick,Z as reactive,U as reactiveScheduler,_ as readonly,te as ref,cr as renderHtmlDocument,ur as renderStyleSheet,R as required,or as setModelValue,er as slot,ie as stop,ne as unref,fr as useRouter,V as validate,H as version};
84
2
 
85
- // lib/core/document.ts
86
- var VOID_HEAD_TAGS = new Set(["base", "link", "meta"]);
87
- function renderHtmlDocument(options) {
88
- const lang = options.lang ?? "en";
89
- const charset = options.charset ?? "utf-8";
90
- const viewport = options.viewport ?? "width=device-width, initial-scale=1";
91
- const htmlAttributes = renderAttributes({
92
- lang,
93
- ...options.htmlAttributes ?? {}
94
- });
95
- const bodyAttributes = renderAttributes(options.bodyAttributes);
96
- const bodyHtml = renderDocumentBody(options.body);
97
- return [
98
- "<!doctype html>",
99
- `<html${htmlAttributes}>`,
100
- "<head>",
101
- ` <meta charset="${escapeHtml(charset)}">`,
102
- ` <meta name="viewport" content="${escapeHtml(viewport)}">`,
103
- ` <title>${escapeHtml(options.title)}</title>`,
104
- options.description ? ` <meta name="description" content="${escapeHtml(options.description)}">` : "",
105
- ...(options.head ?? []).map((element) => ` ${renderHeadElement(element)}`),
106
- options.styles && options.styles.length > 0 ? ` <style>${renderStyleSheet(options.styles)}</style>` : "",
107
- "</head>",
108
- `<body${bodyAttributes}>`,
109
- bodyHtml,
110
- ...(options.scripts ?? []).map((script) => ` ${renderScript(script)}`),
111
- "</body>",
112
- "</html>"
113
- ].filter((line) => line !== "").join(`
114
- `);
115
- }
116
- function renderDocumentBody(body) {
117
- if (typeof document === "undefined") {
118
- throw new Error("renderHtmlDocument requires a DOM-like document");
119
- }
120
- const container = document.createElement("div");
121
- const renderer = new RendererContext;
122
- const mountedComponents = new Set;
123
- const renderables = Array.isArray(body) ? body : [body];
124
- const context = {
125
- templateEngine: new TemplateEngine({}),
126
- renderer,
127
- slots: { default: [] },
128
- registerChild: (component) => {
129
- mountedComponents.add(component);
130
- },
131
- unregisterChild: (component) => {
132
- mountedComponents.delete(component);
133
- }
134
- };
135
- renderables.forEach((renderable) => {
136
- container.appendChild(renderer.mount(renderable, context));
137
- });
138
- const html = container.innerHTML;
139
- mountedComponents.forEach((component) => {
140
- component.unmount();
141
- });
142
- return html;
143
- }
144
- function renderHeadElement(element) {
145
- const attributes = renderAttributes(element.attributes);
146
- if (VOID_HEAD_TAGS.has(element.tag) && !element.text) {
147
- return `<${element.tag}${attributes}>`;
148
- }
149
- return `<${element.tag}${attributes}>${escapeHtml(element.text ?? "")}</${element.tag}>`;
150
- }
151
- function renderScript(script) {
152
- const attributes = renderAttributes({
153
- type: script.type,
154
- src: script.src,
155
- async: script.async,
156
- defer: script.defer,
157
- ...script.attributes ?? {}
158
- });
159
- return `<script${attributes}></script>`;
160
- }
161
- function renderAttributes(attributes = {}) {
162
- const rendered = Object.entries(attributes).flatMap(([name, value]) => {
163
- if (value === false || value === null || value === undefined) {
164
- return [];
165
- }
166
- return value === true ? [name] : [`${name}="${escapeHtml(String(value))}"`];
167
- }).join(" ");
168
- return rendered ? ` ${rendered}` : "";
169
- }
170
- function escapeHtml(value) {
171
- return value.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;");
172
- }
173
-
174
- // lib/core/app.ts
175
- var DEFAULT_ROOT_ELEMENT = "#app";
176
-
177
- class OneApp {
178
- options;
179
- container = null;
180
- rootInstance = null;
181
- mounted = false;
182
- templateEngine = null;
183
- appContext;
184
- providers = new Map;
185
- plugins = [];
186
- unmountedCallback;
187
- router;
188
- constructor(options = {}) {
189
- this.options = options;
190
- this.appContext = {
191
- app: this,
192
- version: "0.0.2",
193
- config: options.config || {}
194
- };
195
- }
196
- handleError(error) {
197
- console.error("应用错误:", error);
198
- this.renderErrorUI(error);
199
- }
200
- renderErrorUI(error) {
201
- if (!this.container) {
202
- return;
203
- }
204
- this.container.innerHTML = `
205
- <div style="padding: 20px; background-color: #ffebee; color: #c62828; font-family: Arial, sans-serif;">
206
- <h3>应用错误</h3>
207
- <p>${error.message}</p>
208
- <pre style="background-color: #fff; padding: 10px; border-radius: 4px; overflow: auto;">${error.stack}</pre>
209
- </div>
210
- `;
211
- }
212
- use(plugin, ...args) {
213
- if (typeof plugin.install !== "function") {
214
- throw new Error("插件必须提供 install 方法");
215
- }
216
- plugin.install(this, ...args);
217
- this.plugins.push({ plugin, args });
218
- return this;
219
- }
220
- mount() {
221
- if (this.mounted) {
222
- console.warn("应用已经处于运行状态");
223
- return;
224
- }
225
- const mountContainer = this.resolveMountContainer();
226
- if (!mountContainer) {
227
- return;
228
- }
229
- try {
230
- this.container = mountContainer;
231
- globalThis.__APP__ = this;
232
- if (this.options.root) {
233
- this.rootInstance = new this.options.root(this.options.rootProps);
234
- if ("setAppContext" in this.rootInstance) {
235
- this.rootInstance.setAppContext(this.appContext);
236
- }
237
- if (this.options.state && "setState" in this.rootInstance) {
238
- this.rootInstance.setState(this.options.state);
239
- }
240
- this.rootInstance.mount(this.container);
241
- this.templateEngine = new TemplateEngine(this.options.state || {});
242
- }
243
- this.mounted = true;
244
- this.onMounted();
245
- } catch (error) {
246
- this.handleError(error);
247
- }
248
- }
249
- unmount() {
250
- if (!this.mounted) {
251
- console.warn("应用未处于运行状态");
252
- return;
253
- }
254
- try {
255
- this.onBeforeUnmount();
256
- if (this.rootInstance) {
257
- if ("unmount" in this.rootInstance) {
258
- this.rootInstance.unmount();
259
- }
260
- this.rootInstance = null;
261
- this.mounted = false;
262
- delete globalThis.__APP__;
263
- }
264
- if (this.templateEngine) {
265
- this.templateEngine.clearBindings();
266
- this.templateEngine = null;
267
- }
268
- if (this.container) {
269
- this.container.innerHTML = "";
270
- }
271
- if (this.unmountedCallback) {
272
- this.unmountedCallback();
273
- }
274
- } catch (error) {
275
- console.error("Failed to unmount app:", error);
276
- }
277
- }
278
- isRunning() {
279
- return this.mounted;
280
- }
281
- updateRootComponent(component) {
282
- if (this.mounted) {
283
- this.unmount();
284
- }
285
- this.options.root = component;
286
- this.mount();
287
- }
288
- update(state) {
289
- if (!this.mounted) {
290
- console.warn("Cannot update unmounted app");
291
- return this;
292
- }
293
- try {
294
- if (state && this.options.state) {
295
- this.options.state = { ...this.options.state, ...state };
296
- if (this.rootInstance && "setState" in this.rootInstance) {
297
- this.rootInstance.setState(state);
298
- }
299
- if (this.templateEngine) {
300
- this.templateEngine.state = this.options.state;
301
- }
302
- }
303
- this.onUpdated();
304
- } catch (error) {
305
- console.error("Failed to update app:", error);
306
- }
307
- return this;
308
- }
309
- getContext() {
310
- return this.appContext;
311
- }
312
- provide(key, value) {
313
- this.providers.set(key, value);
314
- return this;
315
- }
316
- inject(key, fallback) {
317
- const result = this.resolveInjection(key);
318
- return result.found ? result.value : fallback;
319
- }
320
- resolveInjection(key) {
321
- if (!this.providers.has(key)) {
322
- return { found: false, value: undefined };
323
- }
324
- return { found: true, value: this.providers.get(key) };
325
- }
326
- getState() {
327
- return this.options.state;
328
- }
329
- setState(newState) {
330
- this.options.state = newState;
331
- if (this.mounted) {
332
- this.update();
333
- }
334
- return this;
335
- }
336
- onUnmounted(callback) {
337
- this.unmountedCallback = callback;
338
- return this;
339
- }
340
- renderHtmlDocument(options = {}) {
341
- const appDocument = this.options.document ?? {};
342
- const scripts = this.mergeDocumentScripts(appDocument.scripts, options.scripts);
343
- return renderHtmlDocument({
344
- ...appDocument,
345
- ...options,
346
- title: options.title ?? appDocument.title ?? "TSone App",
347
- body: options.body ?? appDocument.body ?? this.createMountDocumentBody(),
348
- scripts
349
- });
350
- }
351
- resolveRootElement(selector) {
352
- if (!selector) {
353
- return null;
354
- }
355
- if (typeof selector === "string") {
356
- if (typeof document === "undefined") {
357
- return null;
358
- }
359
- return document.querySelector(selector);
360
- }
361
- return typeof Element !== "undefined" && selector instanceof Element ? selector : null;
362
- }
363
- resolveMountContainer() {
364
- if (typeof document === "undefined") {
365
- return null;
366
- }
367
- const rootElement = this.resolveRootElement(this.options.rootElement ?? DEFAULT_ROOT_ELEMENT);
368
- return rootElement instanceof HTMLElement ? rootElement : null;
369
- }
370
- createMountDocumentBody() {
371
- const rootElement = this.options.rootElement ?? DEFAULT_ROOT_ELEMENT;
372
- if (typeof rootElement === "string") {
373
- return this.createMountElementFromSelector(rootElement);
374
- }
375
- if (typeof Element !== "undefined" && rootElement instanceof Element) {
376
- const props = {};
377
- if (rootElement.id) {
378
- props.id = rootElement.id;
379
- }
380
- if (rootElement.className) {
381
- props.className = rootElement.className;
382
- }
383
- return { tag: rootElement.tagName.toLowerCase(), props };
384
- }
385
- return Div({ props: { id: "app" } });
386
- }
387
- createMountElementFromSelector(selector) {
388
- if (selector.startsWith("#") && selector.length > 1) {
389
- return Div({ props: { id: selector.slice(1) } });
390
- }
391
- if (selector.startsWith(".") && selector.length > 1) {
392
- return Div({ props: { className: selector.slice(1) } });
393
- }
394
- return Div({ props: { "data-tsone-root": selector } });
395
- }
396
- mergeDocumentScripts(baseScripts, extraScripts) {
397
- if (!baseScripts && !extraScripts) {
398
- return;
399
- }
400
- return [...baseScripts ?? [], ...extraScripts ?? []];
401
- }
402
- onMounted() {
403
- this.plugins.forEach(({ plugin: pluginObj }) => {
404
- if (pluginObj && typeof pluginObj.onMounted === "function") {
405
- pluginObj.onMounted(this);
406
- }
407
- });
408
- }
409
- onUpdated() {
410
- this.plugins.forEach(({ plugin: pluginObj }) => {
411
- if (pluginObj && typeof pluginObj.onUpdated === "function") {
412
- pluginObj.onUpdated(this);
413
- }
414
- });
415
- }
416
- onBeforeUnmount() {
417
- this.plugins.forEach(({ plugin: pluginObj }) => {
418
- if (pluginObj && typeof pluginObj.onBeforeUnmount === "function") {
419
- pluginObj.onBeforeUnmount(this);
420
- }
421
- });
422
- }
423
- }
424
- // lib/core/form.ts
425
- function errorMessage(error) {
426
- return error instanceof Error ? error.message : String(error);
427
- }
428
- function runRule(rule, value, model) {
429
- try {
430
- const result = rule(value, model);
431
- if (result === true) {
432
- return;
433
- }
434
- return result === false ? "Validation failed" : result;
435
- } catch (error) {
436
- return errorMessage(error);
437
- }
438
- }
439
- function required(message = "This field is required") {
440
- return (value) => {
441
- if (value === null || value === undefined) {
442
- return message;
443
- }
444
- if (typeof value === "string" && value.trim().length === 0) {
445
- return message;
446
- }
447
- if (Array.isArray(value) && value.length === 0) {
448
- return message;
449
- }
450
- return true;
451
- };
452
- }
453
- function minLength(length, message = `Must be at least ${length} characters`) {
454
- return (value) => typeof value === "string" && value.length >= length ? true : message;
455
- }
456
- function validate(rule) {
457
- return rule;
458
- }
459
- function createForm(model, rules) {
460
- const errors = {};
461
- const validateField = (path) => {
462
- const fieldRules = rules[path] ?? [];
463
- const value = getModelValue(model, path);
464
- const fieldErrors = fieldRules.map((rule) => runRule(rule, value, model)).filter((message) => message !== undefined);
465
- if (fieldErrors.length === 0) {
466
- delete errors[path];
467
- } else {
468
- errors[path] = fieldErrors;
469
- }
470
- return { valid: fieldErrors.length === 0, errors: fieldErrors };
471
- };
472
- return {
473
- model,
474
- errors,
475
- validate() {
476
- const allErrors = {};
477
- Object.keys(rules).forEach((path) => {
478
- const result = validateField(path);
479
- if (!result.valid) {
480
- allErrors[path] = result.errors;
481
- }
482
- });
483
- return { valid: Object.keys(allErrors).length === 0, errors: allErrors };
484
- },
485
- validateField,
486
- resetErrors() {
487
- Object.keys(errors).forEach((path) => delete errors[path]);
488
- }
489
- };
490
- }
491
- // lib/core/animation/TransitionGroup.ts
492
- class TransitionGroup extends Component {
493
- initState() {
494
- return {};
495
- }
496
- initStyles() {}
497
- render() {
498
- const { tag, type, duration } = normalizeTransitionGroupProps(this.props);
499
- const children = validateTransitionGroupChildren(this.props.children ?? []);
500
- return {
501
- tag,
502
- props: this.props.elementProps,
503
- listeners: this.props.listeners,
504
- children,
505
- transitionGroup: {
506
- type,
507
- duration
508
- }
509
- };
510
- }
511
- }
512
- // lib/index.ts
513
- function createApp(options = {}) {
514
- return new OneApp(options);
515
- }
516
- var version = "0.0.2";
517
- var name = "@geektech/tsone";
518
- export {
519
- version,
520
- validate,
521
- useRouter,
522
- unref,
523
- stop,
524
- slot,
525
- setModelValue,
526
- required,
527
- renderStyleSheet,
528
- renderHtmlDocument,
529
- ref,
530
- readonly,
531
- reactive,
532
- name,
533
- modelPath,
534
- minLength,
535
- isSlotProvider,
536
- isRef,
537
- isReadonly,
538
- isReactive,
539
- isHTMLNode,
540
- isComponentNode,
541
- h,
542
- getModelValue,
543
- effect,
544
- each,
545
- createRouter,
546
- createForm,
547
- createComponent,
548
- createApp,
549
- computed,
550
- Ul,
551
- TransitionGroup,
552
- Tr,
553
- Thead,
554
- Th,
555
- Textarea,
556
- TextRenderStrategy,
557
- TemplateEngine,
558
- Td,
559
- Tbody,
560
- Tag,
561
- Table,
562
- Strong,
563
- Span,
564
- Small,
565
- SlotRenderStrategy,
566
- Select,
567
- Section,
568
- RouterView,
569
- RouterLink,
570
- Router,
571
- RendererContext,
572
- ReactiveSystem,
573
- Pre,
574
- P,
575
- Option,
576
- OneApp,
577
- Ol,
578
- Nav,
579
- ModelBindingController,
580
- Main,
581
- Li,
582
- Label,
583
- Input,
584
- Img,
585
- Header,
586
- H6,
587
- H5,
588
- H4,
589
- H3,
590
- H2,
591
- H1,
592
- Form,
593
- Footer,
594
- Em,
595
- ElementRenderStrategy,
596
- Div,
597
- ComponentRenderStrategy,
598
- Component,
599
- Code,
600
- Button,
601
- Blockquote,
602
- Aside,
603
- Article,
604
- A
605
- };
606
-
607
- //# debugId=7471E8A79DBDD62164756E2164756E21
3
+ //# debugId=8BE5D72DEC3451F664756E2164756E21
608
4
  //# sourceMappingURL=index.js.map