@coorax/core 0.0.0-canary.4 → 0.0.0-canary.5

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
@@ -566,95 +566,119 @@ function getIframeSrc(value) {
566
566
  return null;
567
567
  return `https://www.youtube.com/embed/${videoId}?enablejsapi=1`;
568
568
  }
569
+ function mergeFieldProps(defaults, overrides) {
570
+ if (!overrides)
571
+ return defaults;
572
+ const merged = { ...defaults, ...overrides };
573
+ if (defaults.style || overrides.style) {
574
+ merged.style = {
575
+ ...defaults.style,
576
+ ...overrides.style
577
+ };
578
+ }
579
+ if (defaults.pseudoStyle || overrides.pseudoStyle) {
580
+ merged.pseudoStyle = {
581
+ ...defaults.pseudoStyle,
582
+ ...overrides.pseudoStyle
583
+ };
584
+ }
585
+ return merged;
586
+ }
569
587
  function NewTextField(defaultValue) {
588
+ const { props: defaultProps, ...restDefault } = defaultValue ?? {};
570
589
  return new FieldClass({
571
590
  id: UTILS.generateFieldId(),
572
591
  name: "Text field",
573
592
  type: "text",
574
593
  tag: "p",
575
594
  variant: "field",
576
- props: {
595
+ props: mergeFieldProps({
577
596
  children: { text: "", marks: [] },
578
597
  "data-text-path": "props.children",
579
598
  contentEditable: false,
580
599
  ...DEFAULT_STYLES["text"]
581
- },
582
- ...defaultValue
600
+ }, defaultProps),
601
+ ...restDefault
583
602
  });
584
603
  }
585
604
  function NewHeadingField(defaultValue) {
605
+ const { props: defaultProps, ...restDefault } = defaultValue ?? {};
586
606
  return new FieldClass({
587
607
  id: UTILS.generateFieldId(),
588
608
  name: "Heading field",
589
609
  type: "heading",
590
610
  tag: "h1",
591
611
  variant: "field",
592
- props: {
612
+ props: mergeFieldProps({
593
613
  children: { text: "", marks: [] },
594
614
  "data-text-path": "props.children",
595
615
  contentEditable: false,
596
616
  ...DEFAULT_STYLES["heading"]
597
- },
598
- ...defaultValue
617
+ }, defaultProps),
618
+ ...restDefault
599
619
  });
600
620
  }
601
621
  function NewSubHeadingField(defaultValue) {
622
+ const { props: defaultProps, ...restDefault } = defaultValue ?? {};
602
623
  return new FieldClass({
603
624
  id: UTILS.generateFieldId(),
604
625
  name: "Sub-heading field",
605
626
  type: "sub-heading",
606
627
  tag: "h2",
607
628
  variant: "field",
608
- props: {
629
+ props: mergeFieldProps({
609
630
  children: { text: "", marks: [] },
610
631
  "data-text-path": "props.children",
611
632
  contentEditable: false,
612
633
  ...DEFAULT_STYLES["sub-heading"]
613
- },
614
- ...defaultValue
634
+ }, defaultProps),
635
+ ...restDefault
615
636
  });
616
637
  }
617
638
  function NewImageField(defaultValue) {
639
+ const { props: defaultProps, ...restDefault } = defaultValue ?? {};
618
640
  return new FieldClass({
619
641
  id: UTILS.generateFieldId(),
620
642
  name: "Image field",
621
643
  type: "image",
622
644
  tag: "img",
623
645
  variant: "field",
624
- props: {
646
+ props: mergeFieldProps({
625
647
  src: "",
626
648
  alt: "image",
627
649
  ...DEFAULT_STYLES["image"]
628
- },
629
- ...defaultValue
650
+ }, defaultProps),
651
+ ...restDefault
630
652
  });
631
653
  }
632
654
  function NewVideoField(defaultValue) {
655
+ const { props: defaultProps, ...restDefault } = defaultValue ?? {};
633
656
  return new FieldClass({
634
657
  id: UTILS.generateFieldId(),
635
658
  name: "Video field",
636
659
  type: "video",
637
660
  tag: "video",
638
661
  variant: "field",
639
- props: {
662
+ props: mergeFieldProps({
640
663
  src: "",
641
664
  alt: "video",
642
665
  controlsList: "nodownload nofullscreen noremoteplayback noplaybackrate",
643
666
  preload: "metadata",
644
667
  controls: true,
645
668
  ...DEFAULT_STYLES["video"]
646
- },
647
- ...defaultValue
669
+ }, defaultProps),
670
+ ...restDefault
648
671
  });
649
672
  }
650
673
  function NewIFrameField(defaultValue) {
674
+ const { props: defaultProps, ...restDefault } = defaultValue ?? {};
651
675
  return new FieldClass({
652
676
  id: UTILS.generateFieldId(),
653
677
  name: "IFrame field",
654
678
  type: "iframe",
655
679
  tag: "iframe",
656
680
  variant: "field",
657
- props: {
681
+ props: mergeFieldProps({
658
682
  src: "",
659
683
  alt: "image",
660
684
  allow: "accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share",
@@ -662,86 +686,91 @@ function NewIFrameField(defaultValue) {
662
686
  allowFullScreen: false,
663
687
  frameBorder: 0,
664
688
  ...DEFAULT_STYLES["iframe"]
665
- },
666
- ...defaultValue
689
+ }, defaultProps),
690
+ ...restDefault
667
691
  });
668
692
  }
669
693
  function NewLinkField(defaultValue) {
694
+ const { props: defaultProps, ...restDefault } = defaultValue ?? {};
670
695
  return new FieldClass({
671
696
  id: UTILS.generateFieldId(),
672
697
  name: "Link field",
673
698
  type: "link",
674
699
  tag: "a",
675
700
  variant: "field",
676
- props: {
701
+ props: mergeFieldProps({
677
702
  href: "#",
678
703
  alt: "Preview new link",
679
704
  "data-text-path": "props.children",
680
705
  children: { text: "Preview new link", marks: [] },
681
706
  ...DEFAULT_STYLES["link"]
682
- },
683
- ...defaultValue
707
+ }, defaultProps),
708
+ ...restDefault
684
709
  });
685
710
  }
686
711
  function NewBlockquoteField(defaultValue) {
712
+ const { props: defaultProps, ...restDefault } = defaultValue ?? {};
687
713
  return new FieldClass({
688
714
  id: UTILS.generateFieldId(),
689
715
  name: "Blockquote field",
690
716
  type: "blockquote",
691
717
  tag: "blockquote",
692
718
  variant: "field",
693
- props: {
719
+ props: mergeFieldProps({
694
720
  "data-text-path": "props.children",
695
721
  children: { text: "", marks: [] },
696
722
  ...DEFAULT_STYLES["blockquote"]
697
- },
698
- ...defaultValue
723
+ }, defaultProps),
724
+ ...restDefault
699
725
  });
700
726
  }
701
727
  function NewDividerField(defaultValue) {
728
+ const { props: defaultProps, ...restDefault } = defaultValue ?? {};
702
729
  return new FieldClass({
703
730
  id: UTILS.generateFieldId(),
704
731
  name: "Divider field",
705
732
  type: "divider",
706
733
  tag: "hr",
707
734
  variant: "field",
708
- props: {
735
+ props: mergeFieldProps({
709
736
  ...DEFAULT_STYLES["divider"]
710
- },
711
- ...defaultValue
737
+ }, defaultProps),
738
+ ...restDefault
712
739
  });
713
740
  }
714
741
  function NewCodeField(defaultValue) {
742
+ const { props: defaultProps, ...restDefault } = defaultValue ?? {};
715
743
  return new FieldClass({
716
744
  id: UTILS.generateFieldId(),
717
745
  name: "Code field",
718
746
  type: "code",
719
747
  tag: "pre",
720
748
  variant: "field",
721
- props: {
749
+ props: mergeFieldProps({
722
750
  "data-text-path": "props.children",
723
751
  children: { text: "", marks: [] },
724
752
  theme: "dark",
725
753
  contentEditable: false,
726
754
  ...DEFAULT_STYLES["code"]
727
- },
728
- ...defaultValue
755
+ }, defaultProps),
756
+ ...restDefault
729
757
  });
730
758
  }
731
759
  function NewCaptionField(defaultValue) {
760
+ const { props: defaultProps, ...restDefault } = defaultValue ?? {};
732
761
  return new FieldClass({
733
762
  id: UTILS.generateFieldId(),
734
763
  name: "Caption field",
735
764
  type: "caption",
736
765
  tag: "figcaption",
737
766
  variant: "field",
738
- props: {
767
+ props: mergeFieldProps({
739
768
  "data-text-path": "props.children",
740
769
  children: { text: "", marks: [] },
741
770
  contentEditable: false,
742
771
  ...DEFAULT_STYLES["caption"]
743
- },
744
- ...defaultValue
772
+ }, defaultProps),
773
+ ...restDefault
745
774
  });
746
775
  }
747
776
  function NewField(type, defaultValue) {
@@ -773,6 +802,15 @@ function NewField(type, defaultValue) {
773
802
  }
774
803
  }
775
804
  function DeserializeField(field4) {
805
+ const defaultStyle = DEFAULT_STYLES[field4.type];
806
+ if (defaultStyle) {
807
+ return new FieldClass({
808
+ ...field4,
809
+ props: mergeFieldProps({
810
+ ...defaultStyle
811
+ }, field4.props)
812
+ });
813
+ }
776
814
  return new FieldClass(field4);
777
815
  }
778
816
  var FieldsUtils = {
@@ -1875,18 +1913,28 @@ class MutationsService {
1875
1913
  if (!(field instanceof FieldClass)) {
1876
1914
  throw new MutationPrototypeError(field.name);
1877
1915
  }
1878
- if (!isEditableField(field) || field.type === type) {
1879
- return tree;
1880
- }
1881
- const hasContent = !!field.props.children.text.length;
1882
- if (hasContent) {
1916
+ if (field.type === type) {
1883
1917
  return tree;
1884
1918
  }
1885
1919
  const order = new Set(tree.order);
1886
1920
  const pos = [...order].indexOf(fieldId);
1887
- return this.insertField(this.deleteField(tree, fieldId), FieldsUtils.NewField(type, { id: field.id }), pos);
1921
+ const isCurrentEditable = isEditableField(field);
1922
+ const isTargetEditable = isEditableType(type);
1923
+ let newField;
1924
+ if (isCurrentEditable && isTargetEditable) {
1925
+ const currentChildren = field.props.children;
1926
+ newField = FieldsUtils.NewField(type, {
1927
+ id: field.id,
1928
+ props: {
1929
+ children: currentChildren ?? { text: "", marks: [] }
1930
+ }
1931
+ });
1932
+ } else {
1933
+ newField = FieldsUtils.NewField(type, { id: field.id });
1934
+ }
1935
+ return this.insertField(this.deleteField(tree, fieldId), newField, pos);
1888
1936
  }
1889
- mutateField(tree, { id, keyValuePairs, autoMerge }) {
1937
+ mutateField(tree, { id, keyValuePairs, autoMerge = true }) {
1890
1938
  const hasFieldInOrder = tree.order.has(id);
1891
1939
  if (!hasFieldInOrder) {
1892
1940
  throw new MutationError(`Field ${id} doesn't exist on the tree`);
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@coorax/core",
3
- "version": "0.0.0-canary.4",
3
+ "version": "0.0.0-canary.5",
4
4
  "private": false,
5
5
  "files": [
6
6
  "dist",