revojs 0.0.26 → 0.0.28

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.d.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  export * from "./app";
2
2
  export * from "./html";
3
3
  export * from "./http";
4
+ export * from "./locale";
4
5
  export * from "./markdown";
5
6
  export * from "./radix";
6
7
  export * from "./router";
package/dist/index.js CHANGED
@@ -260,9 +260,9 @@ const renderToString = async (scope, slot) => {
260
260
  const children = await renderToString(scope, slot.children);
261
261
  if (customElement) {
262
262
  const element = new customElement(slot.attributes, scope);
263
- const template = await renderToString(scope, await element.setup());
263
+ const template = await renderToString(element.scope, await element.setup());
264
264
  if (element.shadowRoot) {
265
- const shadow = await renderToString(scope, {
265
+ const shadow = await renderToString(element.scope, {
266
266
  tag: "template",
267
267
  attributes: { shadowRootMode: element.shadowRoot.mode },
268
268
  children: [template]
@@ -377,7 +377,7 @@ const defineComponent = (options) => {
377
377
  return findParent(node.parentNode);
378
378
  }
379
379
  };
380
- const parentNode = findParent(this.host);
380
+ const parentNode = findParent(this.host?.parentNode);
381
381
  if (parentNode) this.scope.parentScope = parentNode.component.scope;
382
382
  return options.setup(this);
383
383
  };
@@ -548,99 +548,6 @@ const mimeTypes = {
548
548
  txt: "text/plain"
549
549
  };
550
550
 
551
- //#endregion
552
- //#region src/markdown/index.ts
553
- const charWhile = (buffer, start, ...chars) => {
554
- let depth = 0;
555
- let current = buffer.at(start + depth);
556
- while (current && chars.includes(current)) {
557
- depth += 1;
558
- current = buffer.at(start + depth);
559
- }
560
- return depth;
561
- };
562
- const charUntil = (buffer, start, ...chars) => {
563
- let depth = 0;
564
- let current = buffer.at(start + depth);
565
- while (current && !chars.includes(current)) {
566
- depth += 1;
567
- current = buffer.at(start + depth);
568
- }
569
- return depth;
570
- };
571
- const inlineText = (buffer, options) => {
572
- const nodes = new Array();
573
- let index = 0;
574
- while (index < buffer.length) {
575
- const char = buffer.charAt(index);
576
- const text = charUntil(buffer, index, "*", "_", "\n");
577
- if (text > 0) {
578
- nodes.push(buffer.slice(index, index + text));
579
- index += text;
580
- continue;
581
- }
582
- if (char === "*" || char === "_") {
583
- const start = charWhile(buffer, index, char);
584
- const between = charUntil(buffer, index + start, char);
585
- const end = charWhile(buffer, index + start + between, char);
586
- const min = Math.min(start, end, 2);
587
- const leading = start - min;
588
- const trailing = end - min;
589
- const slice = buffer.slice(index + leading + min, index + start + between + end - trailing - min);
590
- if (slice.length > 0) {
591
- const inline = inlineText(char.repeat(leading) + slice + char.repeat(trailing), options);
592
- const tag = min === 2 ? "strong" : "em";
593
- nodes.push(defu(options?.[tag], {
594
- tag,
595
- attributes: {},
596
- children: inline
597
- }));
598
- }
599
- index += start + between + end;
600
- continue;
601
- }
602
- if (char === "\n") {
603
- nodes.push(defu(options?.["br"], {
604
- tag: "br",
605
- attributes: {},
606
- children: []
607
- }));
608
- index += 1;
609
- continue;
610
- }
611
- }
612
- return nodes;
613
- };
614
- const markdownToSlot = (input, options) => {
615
- const nodes = new Array();
616
- const buffer = input.replace(/[\r]+/g, "").trim();
617
- let index = 0;
618
- while (index < buffer.length) {
619
- const start = index;
620
- let lines = charWhile(buffer, index, "\n");
621
- while (lines < 2 && index < buffer.length) {
622
- index += lines + charUntil(buffer, index + lines, "\n");
623
- lines = charWhile(buffer, index, "\n");
624
- }
625
- const block = buffer.slice(start, index);
626
- if (block.startsWith("#")) {
627
- const depth = charWhile(block, 0, "#");
628
- const tag = "h" + depth;
629
- nodes.push(defu(options?.[tag], {
630
- tag,
631
- attributes: {},
632
- children: inlineText(block.slice(depth))
633
- }));
634
- } else nodes.push(defu(options?.["p"], {
635
- tag: "p",
636
- attributes: {},
637
- children: inlineText(block)
638
- }));
639
- index += lines;
640
- }
641
- return nodes;
642
- };
643
-
644
551
  //#endregion
645
552
  //#region src/radix/index.ts
646
553
  var Radix = class Radix {
@@ -762,6 +669,18 @@ const RUNTIME_CONTEXT = defineContext("RUNTIME_CONTEXT");
762
669
 
763
670
  //#endregion
764
671
  //#region src/router/index.tsx
672
+ const ROUTE_CONTEXT = defineContext("ROUTE_CONTEXT");
673
+ const navigate = (url) => {
674
+ if (isClient()) {
675
+ const state = window.history.state;
676
+ window.history.pushState(state, "", url);
677
+ window.dispatchEvent(new PopStateEvent("popstate", { state }));
678
+ }
679
+ };
680
+ const anchorNavigate = (event) => {
681
+ event.preventDefault();
682
+ navigate(event.currentTarget?.getAttribute("href") ?? "/");
683
+ };
765
684
  const Outlet = defineComponent({
766
685
  name: "x-outlet",
767
686
  shadowRoot: false,
@@ -778,21 +697,139 @@ const Outlet = defineComponent({
778
697
  return async () => {
779
698
  const { value, inputs } = radix.match(url.value.pathname);
780
699
  const Page = await value?.();
781
- if (Page) return /* @__PURE__ */ h(Page, inputs);
700
+ if (Page) {
701
+ scope.setContext(ROUTE_CONTEXT, { inputs });
702
+ return /* @__PURE__ */ h(Page, inputs);
703
+ }
782
704
  };
783
705
  }
784
706
  });
785
- const navigate = (url) => {
786
- if (isClient()) {
787
- const state = window.history.state;
788
- window.history.pushState(state, "", url);
789
- window.dispatchEvent(new PopStateEvent("popstate", { state }));
707
+
708
+ //#endregion
709
+ //#region src/locale/index.ts
710
+ const createLocaleContext = (options) => {
711
+ const LOCALE_CONTEXT = defineContext("LOCALE_CONTEXT");
712
+ const registerLocaleContext = (scope) => {
713
+ scope.setContext(LOCALE_CONTEXT, options);
714
+ };
715
+ return {
716
+ LOCALE_CONTEXT,
717
+ registerLocaleContext
718
+ };
719
+ };
720
+ const useLocaleContext = async (scope, context) => {
721
+ const { event } = scope.getContext(RUNTIME_CONTEXT);
722
+ const { inputs } = scope.getContext(ROUTE_CONTEXT);
723
+ const { locales, defaultLocale, cookie, input } = scope.getContext(context);
724
+ const entries = await import("#virtual/locales").then((module) => module.index);
725
+ let locale = defaultLocale;
726
+ let set;
727
+ if (cookie) {
728
+ if (event) set = getCookies(event)[cookie];
790
729
  }
730
+ if (input) if (event) set = event.context.variables[input];
731
+ else set = inputs[input];
732
+ if (set) locale = set;
733
+ if (locale) locales[locale] = await entries[locale]?.() ?? {};
734
+ const $ = (key) => {
735
+ if (locale) return locales[locale]?.[key] ?? key;
736
+ return key;
737
+ };
738
+ return { $ };
791
739
  };
792
- const anchorNavigate = (event) => {
793
- event.preventDefault();
794
- navigate(event.currentTarget?.getAttribute("href") ?? "/");
740
+
741
+ //#endregion
742
+ //#region src/markdown/index.ts
743
+ const charWhile = (buffer, start, ...chars) => {
744
+ let depth = 0;
745
+ let current = buffer.at(start + depth);
746
+ while (current && chars.includes(current)) {
747
+ depth += 1;
748
+ current = buffer.at(start + depth);
749
+ }
750
+ return depth;
751
+ };
752
+ const charUntil = (buffer, start, ...chars) => {
753
+ let depth = 0;
754
+ let current = buffer.at(start + depth);
755
+ while (current && !chars.includes(current)) {
756
+ depth += 1;
757
+ current = buffer.at(start + depth);
758
+ }
759
+ return depth;
760
+ };
761
+ const inlineText = (buffer, options) => {
762
+ const nodes = new Array();
763
+ let index = 0;
764
+ while (index < buffer.length) {
765
+ const char = buffer.charAt(index);
766
+ const text = charUntil(buffer, index, "*", "_", "\n");
767
+ if (text > 0) {
768
+ nodes.push(buffer.slice(index, index + text));
769
+ index += text;
770
+ continue;
771
+ }
772
+ if (char === "*" || char === "_") {
773
+ const start = charWhile(buffer, index, char);
774
+ const between = charUntil(buffer, index + start, char);
775
+ const end = charWhile(buffer, index + start + between, char);
776
+ const min = Math.min(start, end, 2);
777
+ const leading = start - min;
778
+ const trailing = end - min;
779
+ const slice = buffer.slice(index + leading + min, index + start + between + end - trailing - min);
780
+ if (slice.length > 0) {
781
+ const inline = inlineText(char.repeat(leading) + slice + char.repeat(trailing), options);
782
+ const tag = min === 2 ? "strong" : "em";
783
+ nodes.push(defu(options?.[tag], {
784
+ tag,
785
+ attributes: {},
786
+ children: inline
787
+ }));
788
+ }
789
+ index += start + between + end;
790
+ continue;
791
+ }
792
+ if (char === "\n") {
793
+ nodes.push(defu(options?.["br"], {
794
+ tag: "br",
795
+ attributes: {},
796
+ children: []
797
+ }));
798
+ index += 1;
799
+ continue;
800
+ }
801
+ }
802
+ return nodes;
803
+ };
804
+ const markdownToSlot = (input, options) => {
805
+ const nodes = new Array();
806
+ const buffer = input.replace(/[\r]+/g, "").trim();
807
+ let index = 0;
808
+ while (index < buffer.length) {
809
+ const start = index;
810
+ let lines = charWhile(buffer, index, "\n");
811
+ while (lines < 2 && index < buffer.length) {
812
+ index += lines + charUntil(buffer, index + lines, "\n");
813
+ lines = charWhile(buffer, index, "\n");
814
+ }
815
+ const block = buffer.slice(start, index);
816
+ if (block.startsWith("#")) {
817
+ const depth = charWhile(block, 0, "#");
818
+ const tag = "h" + depth;
819
+ nodes.push(defu(options?.[tag], {
820
+ tag,
821
+ attributes: {},
822
+ children: inlineText(block.slice(depth))
823
+ }));
824
+ } else nodes.push(defu(options?.["p"], {
825
+ tag: "p",
826
+ attributes: {},
827
+ children: inlineText(block)
828
+ }));
829
+ index += lines;
830
+ }
831
+ return nodes;
795
832
  };
796
833
 
797
834
  //#endregion
798
- export { $fetch, Compute, Handler, MountedEvent, Outlet, RUNTIME_CONTEXT, Radix, Scope, StopEvent, activeCompute, anchorNavigate, components, createApp, createCompute, createElement, createEvent, createMemo, createRuntime, createState, defineComponent, defineContext, defineRoute, fileName, fromValue, getAssets, getCookies, getCustomElement, getGlobalStyles, getMimeType, getRequestUrl, getRoutes, getSetCookies, getVariables, isClient, isServer, isTemplate, markdownToSlot, navigate, preventDefault, registerComponent, renderToNode, renderToString, sendBadRequest, sendHtml, sendJson, sendRedirect, sendText, sendUnauthorized, setCookie, stopImmediatePropagation, stopPropagation, targets, toCustomElement, toFragment, toPath, toString, useEvent };
835
+ export { $fetch, Compute, Handler, MountedEvent, Outlet, ROUTE_CONTEXT, RUNTIME_CONTEXT, Radix, Scope, StopEvent, activeCompute, anchorNavigate, components, createApp, createCompute, createElement, createEvent, createLocaleContext, createMemo, createRuntime, createState, defineComponent, defineContext, defineRoute, fileName, fromValue, getAssets, getCookies, getCustomElement, getGlobalStyles, getMimeType, getRequestUrl, getRoutes, getSetCookies, getVariables, isClient, isServer, isTemplate, markdownToSlot, navigate, preventDefault, registerComponent, renderToNode, renderToString, sendBadRequest, sendHtml, sendJson, sendRedirect, sendText, sendUnauthorized, setCookie, stopImmediatePropagation, stopPropagation, targets, toCustomElement, toFragment, toPath, toString, useEvent, useLocaleContext };
@@ -0,0 +1,15 @@
1
+ import { type Descriptor, Scope } from "../signals";
2
+ export type Locales = Record<string, Record<string, string>>;
3
+ export type LocaleOptions<T extends Locales> = {
4
+ locales: T;
5
+ defaultLocale?: keyof T;
6
+ cookie?: string;
7
+ input?: string;
8
+ };
9
+ export declare const createLocaleContext: <T extends LocaleOptions<Locales>>(options: T) => {
10
+ LOCALE_CONTEXT: Descriptor<T>;
11
+ registerLocaleContext: (scope: Scope) => void;
12
+ };
13
+ export declare const useLocaleContext: <T extends LocaleOptions<Locales>>(scope: Scope, context: Descriptor<T>) => Promise<{
14
+ $: (key: keyof T["locales"][keyof T["locales"]]) => string | number | symbol | keyof T["locales"][keyof T["locales"]];
15
+ }>;
@@ -1,4 +1,7 @@
1
1
  import { type ComponentConstructor } from "../html";
2
- export declare const Outlet: ComponentConstructor<{}, {}>;
2
+ export declare const ROUTE_CONTEXT: import("..").Descriptor<{
3
+ inputs: Record<string, string>;
4
+ }>;
3
5
  export declare const navigate: (url: string) => void;
4
6
  export declare const anchorNavigate: (event: Event) => void;
7
+ export declare const Outlet: ComponentConstructor<{}, {}>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "revojs",
3
- "version": "0.0.26",
3
+ "version": "0.0.28",
4
4
  "type": "module",
5
5
  "repository": "coverbase/revojs",
6
6
  "license": "MIT",