@glyphjs/runtime 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.cjs CHANGED
@@ -400,11 +400,13 @@ var RuntimeContext = react.createContext(null);
400
400
  function RuntimeProvider({
401
401
  registry,
402
402
  references,
403
+ documentId,
403
404
  theme,
404
405
  className,
405
406
  style: consumerStyle,
406
407
  onDiagnostic,
407
408
  onNavigate,
409
+ onInteraction,
408
410
  children
409
411
  }) {
410
412
  const resolvedThemeObject = react.useMemo(() => resolveTheme(theme), [theme]);
@@ -420,11 +422,13 @@ function RuntimeProvider({
420
422
  () => ({
421
423
  registry,
422
424
  references,
425
+ documentId,
423
426
  theme: resolvedTheme,
424
427
  onDiagnostic: onDiagnostic ?? noop,
425
- onNavigate: onNavigate ?? noop
428
+ onNavigate: onNavigate ?? noop,
429
+ onInteraction
426
430
  }),
427
- [registry, references, resolvedTheme, onDiagnostic, onNavigate]
431
+ [registry, references, documentId, resolvedTheme, onDiagnostic, onNavigate, onInteraction]
428
432
  );
429
433
  const style = react.useMemo(
430
434
  () => ({ ...resolvedThemeObject.variables, ...consumerStyle }),
@@ -781,6 +785,12 @@ function GlyphRawHtml({ block }) {
781
785
  const clean = DOMPurify__default.default.sanitize(data.value);
782
786
  return /* @__PURE__ */ jsxRuntime.jsx("div", { dangerouslySetInnerHTML: { __html: clean } });
783
787
  }
788
+ function RichText({ content, style, className }) {
789
+ if (Array.isArray(content)) {
790
+ return /* @__PURE__ */ jsxRuntime.jsx("span", { style, className, children: /* @__PURE__ */ jsxRuntime.jsx(InlineRenderer, { nodes: content }) });
791
+ }
792
+ return /* @__PURE__ */ jsxRuntime.jsx("span", { style, className, children: content });
793
+ }
784
794
 
785
795
  // src/renderers/index.ts
786
796
  var builtInRenderers = {
@@ -795,7 +805,7 @@ var builtInRenderers = {
795
805
  };
796
806
 
797
807
  // src/plugins/resolve-props.ts
798
- function resolveComponentProps(block, definition, references, onNavigate, themeContext, layoutHints, containerContext) {
808
+ function resolveComponentProps(block, definition, references, onNavigate, themeContext, layoutHints, containerContext, onInteraction) {
799
809
  const parseResult = definition.schema.safeParse(block.data);
800
810
  const data = parseResult.success ? parseResult.data : (() => {
801
811
  const err = parseResult.error;
@@ -825,7 +835,7 @@ function resolveComponentProps(block, definition, references, onNavigate, themeC
825
835
  }
826
836
  }
827
837
  }
828
- return {
838
+ const props = {
829
839
  data,
830
840
  block,
831
841
  outgoingRefs,
@@ -835,6 +845,10 @@ function resolveComponentProps(block, definition, references, onNavigate, themeC
835
845
  layout: layoutHints,
836
846
  container: containerContext
837
847
  };
848
+ if (onInteraction) {
849
+ props.onInteraction = onInteraction;
850
+ }
851
+ return props;
838
852
  }
839
853
  var defaultConfig = {
840
854
  enabled: true,
@@ -1024,7 +1038,7 @@ function ReferenceIndicator({
1024
1038
  );
1025
1039
  }
1026
1040
  function BlockDispatch({ block, layout, container }) {
1027
- const { registry, references, theme, onNavigate } = useRuntime();
1041
+ const { registry, references, theme, documentId, onNavigate, onInteraction } = useRuntime();
1028
1042
  const { incomingRefs, outgoingRefs } = useReferences(block.id);
1029
1043
  const hasRefs = incomingRefs.length > 0 || outgoingRefs.length > 0;
1030
1044
  if (block.type.startsWith("ui:")) {
@@ -1040,6 +1054,9 @@ function BlockDispatch({ block, layout, container }) {
1040
1054
  }
1041
1055
  }
1042
1056
  }
1057
+ const handleInteraction = onInteraction && block.metadata?.interactive === true ? (event) => {
1058
+ onInteraction({ ...event, documentId });
1059
+ } : void 0;
1043
1060
  let content;
1044
1061
  const overrideDef = registry.getOverride(block.type);
1045
1062
  if (overrideDef) {
@@ -1058,7 +1075,8 @@ function BlockDispatch({ block, layout, container }) {
1058
1075
  handleNavigate,
1059
1076
  theme,
1060
1077
  layout,
1061
- container
1078
+ container,
1079
+ handleInteraction
1062
1080
  );
1063
1081
  const Renderer = definition.render;
1064
1082
  content = /* @__PURE__ */ jsxRuntime.jsx(Renderer, { ...props });
@@ -1650,7 +1668,7 @@ function createGlyphRuntime(config) {
1650
1668
  setThemeState(currentTheme);
1651
1669
  setVersion(registryVersion);
1652
1670
  }, []);
1653
- react.useMemo(() => {
1671
+ react.useEffect(() => {
1654
1672
  listeners.add(forceUpdate);
1655
1673
  return () => {
1656
1674
  listeners.delete(forceUpdate);
@@ -1661,9 +1679,11 @@ function createGlyphRuntime(config) {
1661
1679
  {
1662
1680
  registry,
1663
1681
  references: ir.references,
1682
+ documentId: ir.id,
1664
1683
  theme,
1665
1684
  onDiagnostic: config.onDiagnostic,
1666
1685
  onNavigate: config.onNavigate,
1686
+ onInteraction: config.onInteraction,
1667
1687
  children: /* @__PURE__ */ jsxRuntime.jsx(GlyphDocument, { ir, className, animation: config.animation })
1668
1688
  }
1669
1689
  );
@@ -1715,6 +1735,25 @@ function SSRPlaceholder({
1715
1735
  return /* @__PURE__ */ jsxRuntime.jsx(jsxRuntime.Fragment, { children });
1716
1736
  }
1717
1737
 
1738
+ // src/debounce.ts
1739
+ function debounceInteractions(callback, delay = 300) {
1740
+ const timers = /* @__PURE__ */ new Map();
1741
+ return (event) => {
1742
+ const key = `${event.blockId}:${event.kind}`;
1743
+ const existing = timers.get(key);
1744
+ if (existing !== void 0) {
1745
+ clearTimeout(existing);
1746
+ }
1747
+ timers.set(
1748
+ key,
1749
+ setTimeout(() => {
1750
+ timers.delete(key);
1751
+ callback(event);
1752
+ }, delay)
1753
+ );
1754
+ };
1755
+ }
1756
+
1718
1757
  exports.AnimationContext = AnimationContext;
1719
1758
  exports.AnimationProvider = AnimationProvider;
1720
1759
  exports.BlockDiagnosticIndicator = BlockDiagnosticIndicator;
@@ -1740,6 +1779,7 @@ exports.LayoutProvider = LayoutProvider;
1740
1779
  exports.PluginRegistry = PluginRegistry;
1741
1780
  exports.PresentationLayout = PresentationLayout;
1742
1781
  exports.ReferenceIndicator = ReferenceIndicator;
1782
+ exports.RichText = RichText;
1743
1783
  exports.RuntimeProvider = RuntimeProvider;
1744
1784
  exports.SSRPlaceholder = SSRPlaceholder;
1745
1785
  exports.ThemeProvider = ThemeProvider;
@@ -1747,6 +1787,7 @@ exports.builtInRenderers = builtInRenderers;
1747
1787
  exports.createGlyphRuntime = createGlyphRuntime;
1748
1788
  exports.createResolveVar = createResolveVar;
1749
1789
  exports.darkTheme = darkTheme;
1790
+ exports.debounceInteractions = debounceInteractions;
1750
1791
  exports.isDarkTheme = isDarkTheme;
1751
1792
  exports.lightTheme = lightTheme;
1752
1793
  exports.mergeThemeDefaults = mergeThemeDefaults;