@uniformdev/canvas-react 19.135.0 → 19.135.1-alpha.11

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.esm.js CHANGED
@@ -233,6 +233,50 @@ var PureContextualEditingComponentWrapper = ({
233
233
  // src/components/UniformComposition.tsx
234
234
  import React4, { createContext, useContext } from "react";
235
235
 
236
+ // src/hooks/useClientConditionsComposition.ts
237
+ import {
238
+ evaluateWalkTreeNodeVisibility,
239
+ evaluateWalkTreePropertyCriteria,
240
+ walkNodeTree
241
+ } from "@uniformdev/canvas";
242
+ import { produce } from "immer";
243
+ import { useMemo as useMemo2 } from "react";
244
+
245
+ // src/hooks/useClientVisibilityRules.ts
246
+ import { createQuirksVisibilityRule } from "@uniformdev/canvas";
247
+ import { useQuirks } from "@uniformdev/context-react";
248
+ import { useMemo } from "react";
249
+ function useClientVisibilityRules() {
250
+ const quirks = useQuirks({ throwOnMissingProvider: false });
251
+ return useMemo(() => {
252
+ return {
253
+ ...createQuirksVisibilityRule(quirks)
254
+ };
255
+ }, [quirks]);
256
+ }
257
+
258
+ // src/hooks/useClientConditionsComposition.ts
259
+ function useClientConditionsComposition(data) {
260
+ const rules = useClientVisibilityRules();
261
+ const preprocessedValue = useMemo2(() => {
262
+ if (!data) {
263
+ return data;
264
+ }
265
+ try {
266
+ return produce(data, (draft) => {
267
+ walkNodeTree(draft, (context) => {
268
+ evaluateWalkTreeNodeVisibility({ context, rules, showIndeterminate: false });
269
+ evaluateWalkTreePropertyCriteria({ node: context.node, rules, keepIndeterminate: false });
270
+ });
271
+ });
272
+ } catch (e) {
273
+ console.error("Error evaluating visibility rules:", e.stack);
274
+ return data;
275
+ }
276
+ }, [data, rules]);
277
+ return preprocessedValue;
278
+ }
279
+
236
280
  // src/hooks/useUniformContextualEditing.ts
237
281
  import {
238
282
  createCanvasChannel,
@@ -243,14 +287,15 @@ import {
243
287
  isAllowedReferrer,
244
288
  isUpdateCompositionInternalMessage
245
289
  } from "@uniformdev/canvas";
246
- import { useEffect, useMemo, useState } from "react";
290
+ import { useEffect, useMemo as useMemo3, useRef, useState } from "react";
247
291
  var registeredCompositionIds = /* @__PURE__ */ new Set();
248
292
  var useUniformContextualEditing = ({
249
293
  initialCompositionValue,
250
294
  enhance = (message) => message.composition
251
295
  }) => {
252
296
  const [contextualComposition, setContextualComposition] = useState();
253
- const channel = useMemo(() => {
297
+ const latestEventTimeStamp = useRef();
298
+ const channel = useMemo3(() => {
254
299
  var _a;
255
300
  if (!isInContextEditingMode()) {
256
301
  return;
@@ -273,8 +318,12 @@ var useUniformContextualEditing = ({
273
318
  if (!isUpdateCompositionInternalMessage(message)) {
274
319
  return;
275
320
  }
321
+ if (latestEventTimeStamp.current && message.eventTimestamp && message.eventTimestamp <= latestEventTimeStamp.current) {
322
+ return;
323
+ }
276
324
  const enhancedComposition = await enhance(message);
277
325
  setContextualComposition(enhancedComposition);
326
+ latestEventTimeStamp.current = message.eventTimestamp;
278
327
  });
279
328
  registeredCompositionIds.add(initialCompositionValue == null ? void 0 : initialCompositionValue._id);
280
329
  return () => {
@@ -336,8 +385,9 @@ function UniformComposition({
336
385
  contextualEditingEnhancer,
337
386
  contextualEditingDefaultPlaceholder
338
387
  }) {
388
+ const ruledComposition = useClientConditionsComposition(data);
339
389
  const { composition, isContextualEditing } = useUniformContextualEditing({
340
- initialCompositionValue: data,
390
+ initialCompositionValue: ruledComposition,
341
391
  enhance: contextualEditingEnhancer
342
392
  });
343
393
  return /* @__PURE__ */ React4.createElement(
@@ -600,20 +650,19 @@ function resolveChildren({
600
650
 
601
651
  // src/components/UniformPlayground.tsx
602
652
  import { EMPTY_COMPOSITION as EMPTY_COMPOSITION4 } from "@uniformdev/canvas";
603
- import React8, { useMemo as useMemo2 } from "react";
653
+ import React8, { useMemo as useMemo4 } from "react";
604
654
  var UniformPlayground = ({
605
655
  resolveRenderer,
606
656
  decorators = [],
607
657
  contextualEditingEnhancer,
608
658
  behaviorTracking,
609
- contextualEditingDefaultPlaceholder,
610
- children
659
+ contextualEditingDefaultPlaceholder
611
660
  }) => {
612
661
  const { composition, isContextualEditing } = useUniformContextualEditing({
613
662
  initialCompositionValue: EMPTY_COMPOSITION4,
614
663
  enhance: contextualEditingEnhancer
615
664
  });
616
- const renderedComponent = useMemo2(() => {
665
+ const renderedComponent = useMemo4(() => {
617
666
  if (!composition) {
618
667
  return null;
619
668
  }
@@ -624,21 +673,13 @@ var UniformPlayground = ({
624
673
  behaviorTracking,
625
674
  resolveRenderer,
626
675
  contextualEditingDefaultPlaceholder
627
- },
628
- children
676
+ }
629
677
  ));
630
678
  decorators.forEach((Decorator) => {
631
679
  component = /* @__PURE__ */ React8.createElement(Decorator, { data: composition }, component);
632
680
  });
633
681
  return component;
634
- }, [
635
- children,
636
- resolveRenderer,
637
- decorators,
638
- composition,
639
- behaviorTracking,
640
- contextualEditingDefaultPlaceholder
641
- ]);
682
+ }, [resolveRenderer, decorators, composition, behaviorTracking, contextualEditingDefaultPlaceholder]);
642
683
  return /* @__PURE__ */ React8.createElement(
643
684
  UniformCompositionContext.Provider,
644
685
  {
@@ -654,15 +695,12 @@ import {
654
695
  ATTRIBUTE_PARAMETER_ID,
655
696
  ATTRIBUTE_PARAMETER_TYPE
656
697
  } from "@uniformdev/canvas";
657
- import {
658
- isRichTextValue,
659
- isRichTextValueConsideredEmpty
660
- } from "@uniformdev/richtext";
661
- import React18, { useMemo as useMemo3 } from "react";
698
+ import { isRichTextValue, isRichTextValueConsideredEmpty } from "@uniformdev/richtext";
699
+ import React19 from "react";
662
700
 
663
701
  // src/components/UniformRichText/UniformRichTextNode.tsx
664
702
  import { isRichTextNode } from "@uniformdev/richtext";
665
- import React17 from "react";
703
+ import React18 from "react";
666
704
 
667
705
  // src/components/UniformRichText/nodes/HeadingRichTextNode.tsx
668
706
  import React9 from "react";
@@ -716,26 +754,34 @@ var ParagraphRichTextNode = ({ children, node }) => {
716
754
  );
717
755
  };
718
756
 
719
- // src/components/UniformRichText/nodes/TabRichTextNode.tsx
757
+ // src/components/UniformRichText/nodes/TableCellRichTextNode.tsx
758
+ import { getRichTextTagFromTableCellHeaderState } from "@uniformdev/richtext";
720
759
  import React15 from "react";
760
+ var TableCellRichTextNode = ({ children, node }) => {
761
+ const { headerState } = node;
762
+ const TableCellTag = getRichTextTagFromTableCellHeaderState(headerState);
763
+ return /* @__PURE__ */ React15.createElement(TableCellTag, null, children);
764
+ };
765
+
766
+ // src/components/UniformRichText/nodes/TabRichTextNode.tsx
767
+ import React16 from "react";
721
768
  var TabRichTextNode = () => {
722
- return /* @__PURE__ */ React15.createElement(React15.Fragment, null, " ");
769
+ return /* @__PURE__ */ React16.createElement(React16.Fragment, null, " ");
723
770
  };
724
771
 
725
772
  // src/components/UniformRichText/nodes/TextRichTextNode.tsx
726
773
  import { getRichTextTagsFromTextFormat } from "@uniformdev/richtext";
727
- import React16 from "react";
774
+ import React17 from "react";
728
775
  var TextRichTextNode = ({ node }) => {
729
776
  const { text, format } = node;
730
777
  const tags = getRichTextTagsFromTextFormat(format);
731
- return /* @__PURE__ */ React16.createElement(React16.Fragment, null, tags.length > 0 ? tags.reduceRight((children, Tag) => /* @__PURE__ */ React16.createElement(Tag, null, children), text) : text);
778
+ return /* @__PURE__ */ React17.createElement(React17.Fragment, null, tags.length > 0 ? tags.reduceRight((children, Tag) => /* @__PURE__ */ React17.createElement(Tag, null, children), text) : text);
732
779
  };
733
780
 
734
781
  // src/components/UniformRichText/UniformRichTextNode.tsx
735
782
  function UniformRichTextNode({ node, ...props }) {
736
783
  var _a;
737
- if (!isRichTextNode(node))
738
- return null;
784
+ if (!isRichTextNode(node)) return null;
739
785
  let NodeRenderer = (_a = props.resolveRichTextRenderer) == null ? void 0 : _a.call(props, node);
740
786
  if (typeof NodeRenderer === "undefined") {
741
787
  NodeRenderer = resolveRichTextDefaultRenderer(node);
@@ -743,8 +789,8 @@ function UniformRichTextNode({ node, ...props }) {
743
789
  if (!NodeRenderer) {
744
790
  return null;
745
791
  }
746
- const children = node.children ? node.children.map((childNode, i) => /* @__PURE__ */ React17.createElement(UniformRichTextNode, { ...props, key: i, node: childNode })) : null;
747
- return /* @__PURE__ */ React17.createElement(NodeRenderer, { node }, children);
792
+ const children = node.children ? node.children.map((childNode, i) => /* @__PURE__ */ React18.createElement(UniformRichTextNode, { ...props, key: i, node: childNode })) : null;
793
+ return /* @__PURE__ */ React18.createElement(NodeRenderer, { node }, children);
748
794
  }
749
795
  var rendererMap = /* @__PURE__ */ new Map([
750
796
  ["heading", HeadingRichTextNode],
@@ -753,49 +799,59 @@ var rendererMap = /* @__PURE__ */ new Map([
753
799
  ["list", ListRichTextNode],
754
800
  ["listitem", ListItemRichTextNode],
755
801
  ["paragraph", ParagraphRichTextNode],
756
- ["quote", ({ children }) => /* @__PURE__ */ React17.createElement("blockquote", null, children)],
802
+ ["quote", ({ children }) => /* @__PURE__ */ React18.createElement("blockquote", null, children)],
757
803
  [
758
804
  "code",
759
- ({ children }) => /* @__PURE__ */ React17.createElement("pre", null, /* @__PURE__ */ React17.createElement("code", null, children))
805
+ ({ children }) => /* @__PURE__ */ React18.createElement("pre", null, /* @__PURE__ */ React18.createElement("code", null, children))
760
806
  ],
761
- ["root", ({ children }) => /* @__PURE__ */ React17.createElement(React17.Fragment, null, children)],
807
+ ["root", ({ children }) => /* @__PURE__ */ React18.createElement(React18.Fragment, null, children)],
762
808
  ["text", TextRichTextNode],
763
- ["tab", TabRichTextNode]
809
+ ["tab", TabRichTextNode],
810
+ [
811
+ "table",
812
+ ({ children }) => /* @__PURE__ */ React18.createElement("table", null, /* @__PURE__ */ React18.createElement("tbody", null, children))
813
+ ],
814
+ ["tablerow", ({ children }) => /* @__PURE__ */ React18.createElement("tr", null, children)],
815
+ ["tablecell", TableCellRichTextNode]
764
816
  ]);
765
817
  var resolveRichTextDefaultRenderer = (node) => {
766
818
  return rendererMap.get(node.type);
767
819
  };
768
820
 
769
821
  // src/components/UniformRichText/UniformRichText.tsx
770
- var UniformRichText = React18.forwardRef(function UniformRichText2({ parameterId, resolveRichTextRenderer, as: Tag = "div", ...props }, ref) {
771
- const { data: componentData } = useUniformCurrentComponent();
772
- const parameter = useMemo3(() => {
773
- var _a;
774
- return (_a = componentData == null ? void 0 : componentData.parameters) == null ? void 0 : _a[parameterId];
775
- }, [componentData, parameterId]);
776
- const value = useMemo3(() => parameter == null ? void 0 : parameter.value, [parameter]);
777
- if (!value || !isRichTextValue(value) || isRichTextValueConsideredEmpty(value))
822
+ var UniformRichText = React19.forwardRef(function UniformRichText2({ parameterId, resolveRichTextRenderer, as: Tag = "div", placeholder, ...props }, ref) {
823
+ var _a;
824
+ const { data: componentData, contextualEditingDefaultPlaceholder } = useUniformCurrentComponent();
825
+ const { isContextualEditing } = useUniformCurrentComposition();
826
+ const parameter = (_a = componentData == null ? void 0 : componentData.parameters) == null ? void 0 : _a[parameterId];
827
+ if (!parameter) {
778
828
  return null;
779
- return Tag === null ? /* @__PURE__ */ React18.createElement(UniformRichTextNode, { node: value.root, resolveRichTextRenderer }) : /* @__PURE__ */ React18.createElement(
829
+ }
830
+ const placeholderProp = placeholder != null ? placeholder : contextualEditingDefaultPlaceholder;
831
+ const computedPlaceholder = typeof placeholderProp === "function" ? placeholderProp({ id: parameterId }) : placeholderProp;
832
+ const value = parameter.value;
833
+ if (!value || !isRichTextValue(value)) return null;
834
+ if (!isContextualEditing && isRichTextValueConsideredEmpty(value)) return null;
835
+ return Tag === null ? /* @__PURE__ */ React19.createElement(UniformRichTextNode, { node: value.root, resolveRichTextRenderer }) : /* @__PURE__ */ React19.createElement(
780
836
  Tag,
781
837
  {
782
838
  ref,
783
839
  ...props,
784
- ...{
840
+ ...isContextualEditing ? {
785
841
  [ATTRIBUTE_COMPONENT_ID]: componentData == null ? void 0 : componentData._id,
786
842
  [ATTRIBUTE_PARAMETER_ID]: parameterId,
787
843
  [ATTRIBUTE_PARAMETER_TYPE]: "richText"
788
- }
844
+ } : {}
789
845
  },
790
- /* @__PURE__ */ React18.createElement(UniformRichTextNode, { node: value.root, resolveRichTextRenderer })
846
+ isRichTextValueConsideredEmpty(value) ? /* @__PURE__ */ React19.createElement("p", null, /* @__PURE__ */ React19.createElement("i", null, computedPlaceholder)) : /* @__PURE__ */ React19.createElement(UniformRichTextNode, { node: value.root, resolveRichTextRenderer })
791
847
  );
792
848
  });
793
849
 
794
850
  // src/components/UniformText.tsx
795
- import React20, { useState as useState2 } from "react";
851
+ import React21, { useState as useState2 } from "react";
796
852
 
797
853
  // src/components/PureUniformText.tsx
798
- import React19 from "react";
854
+ import React20 from "react";
799
855
  var PureUniformText = ({
800
856
  as: Tag = "span",
801
857
  parameterId,
@@ -814,10 +870,10 @@ var PureUniformText = ({
814
870
  return null;
815
871
  }
816
872
  if (!isContextualEditing) {
817
- return /* @__PURE__ */ React19.createElement(Tag, { style: isMultiline ? { whiteSpace: "pre-wrap" } : {}, ...props }, render(value));
873
+ return /* @__PURE__ */ React20.createElement(Tag, { style: isMultiline ? { whiteSpace: "pre-wrap" } : {}, ...props }, render(value));
818
874
  }
819
875
  const computedPlaceholder = typeof placeholder === "function" ? placeholder({ id: parameterId }) : placeholder;
820
- return /* @__PURE__ */ React19.createElement(
876
+ return /* @__PURE__ */ React20.createElement(
821
877
  Tag,
822
878
  {
823
879
  ...props,
@@ -855,7 +911,7 @@ var UniformText = ({
855
911
  }
856
912
  const placeholderProp = placeholder != null ? placeholder : contextualEditingDefaultPlaceholder;
857
913
  const computedPlaceholder = typeof placeholderProp === "function" ? placeholderProp({ id: parameterId }) : placeholderProp;
858
- return /* @__PURE__ */ React20.createElement(
914
+ return /* @__PURE__ */ React21.createElement(
859
915
  PureUniformText,
860
916
  {
861
917
  ...props,
@@ -933,14 +989,17 @@ import {
933
989
  createCanvasChannel as createCanvasChannel2,
934
990
  isUpdateContextualEditingStateInternalMessage
935
991
  } from "@uniformdev/canvas";
936
- import { useEffect as useEffect3, useMemo as useMemo4, useState as useState3 } from "react";
992
+ import { useEffect as useEffect3, useMemo as useMemo5, useState as useState3 } from "react";
937
993
  var useUniformContextualEditingState = ({
938
994
  global = false
939
995
  } = {}) => {
940
996
  const { isContextualEditing } = useUniformCurrentComposition();
941
997
  const { data: componentData } = useUniformCurrentComponent();
942
998
  const [selectedComponentReference, setSelectedComponentReference] = useState3();
943
- const channel = useMemo4(() => {
999
+ const [previewMode, setPreviewMode] = useState3(
1000
+ isContextualEditing ? "editor" : void 0
1001
+ );
1002
+ const channel = useMemo5(() => {
944
1003
  if (!isContextualEditing) {
945
1004
  return;
946
1005
  }
@@ -951,15 +1010,20 @@ var useUniformContextualEditingState = ({
951
1010
  return channel2;
952
1011
  }, [isContextualEditing]);
953
1012
  useEffect3(() => {
1013
+ var _a, _b;
954
1014
  if (!channel) {
955
1015
  return;
956
1016
  }
1017
+ if (typeof window !== "undefined") {
1018
+ setPreviewMode((_b = (_a = window.__UNIFORM_CONTEXTUAL_EDITING__) == null ? void 0 : _a.state) == null ? void 0 : _b.previewMode);
1019
+ }
957
1020
  const unsubscribe = channel.on("update-contextual-editing-state-internal", async (message) => {
958
- var _a;
1021
+ var _a2;
959
1022
  if (!isUpdateContextualEditingStateInternalMessage(message)) {
960
1023
  return;
961
1024
  }
962
- if (!global && (componentData == null ? void 0 : componentData._id) !== ((_a = message.state.selectedComponentReference) == null ? void 0 : _a.parentId)) {
1025
+ setPreviewMode(message.state.previewMode);
1026
+ if (!global && (componentData == null ? void 0 : componentData._id) !== ((_a2 = message.state.selectedComponentReference) == null ? void 0 : _a2.parentId)) {
963
1027
  setSelectedComponentReference(void 0);
964
1028
  return;
965
1029
  }
@@ -968,13 +1032,14 @@ var useUniformContextualEditingState = ({
968
1032
  return () => {
969
1033
  unsubscribe();
970
1034
  };
971
- }, [global, channel, componentData == null ? void 0 : componentData._id, setSelectedComponentReference]);
972
- return useMemo4(
1035
+ }, [global, channel, componentData == null ? void 0 : componentData._id, setSelectedComponentReference, setPreviewMode]);
1036
+ return useMemo5(
973
1037
  () => ({
974
1038
  isContextualEditing,
975
- selectedComponentReference
1039
+ selectedComponentReference,
1040
+ previewMode
976
1041
  }),
977
- [isContextualEditing, selectedComponentReference]
1042
+ [isContextualEditing, selectedComponentReference, previewMode]
978
1043
  );
979
1044
  };
980
1045
  export {