react-tailwind-email-editor 0.0.16 → 0.0.18

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.mjs CHANGED
@@ -4000,6 +4000,160 @@ var TabsContent = React9.forwardRef(({ className, ...props }, ref) => /* @__PURE
4000
4000
  }
4001
4001
  ));
4002
4002
  TabsContent.displayName = TabsPrimitive.Content.displayName;
4003
+
4004
+ // src/lib/htmlImporter.ts
4005
+ var nodeCounter = 0;
4006
+ var generateNodeId = () => {
4007
+ nodeCounter++;
4008
+ return `imported_${nodeCounter}_${Math.random().toString(36).slice(2, 8)}`;
4009
+ };
4010
+ var decodeProps = (encoded) => {
4011
+ try {
4012
+ const json = atob(encoded);
4013
+ const parsed = JSON.parse(json);
4014
+ const { _type, ...props } = parsed;
4015
+ return props;
4016
+ } catch (e) {
4017
+ return null;
4018
+ }
4019
+ };
4020
+ var importHtmlToState = (html) => {
4021
+ nodeCounter = 0;
4022
+ const parser = new DOMParser();
4023
+ const doc = parser.parseFromString(html, "text/html");
4024
+ const componentEls = doc.querySelectorAll("[data-component]");
4025
+ const nodes = {};
4026
+ const childNodeIds = [];
4027
+ const topLevelComponents = [];
4028
+ componentEls.forEach((el) => {
4029
+ let parent = el.parentElement;
4030
+ let isNested = false;
4031
+ while (parent) {
4032
+ if (parent.hasAttribute("data-component")) {
4033
+ isNested = true;
4034
+ break;
4035
+ }
4036
+ parent = parent.parentElement;
4037
+ }
4038
+ if (!isNested) {
4039
+ topLevelComponents.push(el);
4040
+ }
4041
+ });
4042
+ if (topLevelComponents.length === 0) {
4043
+ const rawNodeId = generateNodeId();
4044
+ nodes[rawNodeId] = {
4045
+ type: { resolvedName: "RawHtml" },
4046
+ isCanvas: false,
4047
+ props: { html, padding: 0 },
4048
+ displayName: "RawHtml",
4049
+ custom: {},
4050
+ hidden: false,
4051
+ nodes: [],
4052
+ linkedNodes: {},
4053
+ parent: "ROOT"
4054
+ };
4055
+ childNodeIds.push(rawNodeId);
4056
+ } else {
4057
+ for (const el of topLevelComponents) {
4058
+ const typeName = el.getAttribute("data-component") || "RawHtml";
4059
+ const encodedProps = el.getAttribute("data-props") || "";
4060
+ const props = decodeProps(encodedProps);
4061
+ if (!props) continue;
4062
+ const nodeId = generateNodeId();
4063
+ const isCanvas = typeName === "Container" || typeName === "Paper";
4064
+ const nestedChildren = [];
4065
+ const nestedLinkedNodes = {};
4066
+ if (typeName === "Container") {
4067
+ const nestedEls = el.querySelectorAll(":scope > div > [data-component], :scope [data-component]");
4068
+ nestedEls.forEach((nestedEl) => {
4069
+ let nestedParent = nestedEl.parentElement;
4070
+ let belongsToThis = false;
4071
+ while (nestedParent && nestedParent !== el) {
4072
+ if (nestedParent.hasAttribute("data-component") && nestedParent !== el) {
4073
+ break;
4074
+ }
4075
+ if (nestedParent === el) {
4076
+ belongsToThis = true;
4077
+ }
4078
+ nestedParent = nestedParent.parentElement;
4079
+ }
4080
+ if (nestedParent === el) belongsToThis = true;
4081
+ if (belongsToThis) {
4082
+ const nestedType = nestedEl.getAttribute("data-component") || "RawHtml";
4083
+ const nestedEncoded = nestedEl.getAttribute("data-props") || "";
4084
+ const nestedProps = decodeProps(nestedEncoded);
4085
+ if (nestedProps) {
4086
+ const childId = generateNodeId();
4087
+ nodes[childId] = {
4088
+ type: { resolvedName: nestedType },
4089
+ isCanvas: false,
4090
+ props: nestedProps,
4091
+ displayName: nestedType,
4092
+ custom: {},
4093
+ hidden: false,
4094
+ nodes: [],
4095
+ linkedNodes: {},
4096
+ parent: nodeId
4097
+ };
4098
+ nestedChildren.push(childId);
4099
+ }
4100
+ }
4101
+ });
4102
+ }
4103
+ if (typeName === "TwoColumn") {
4104
+ const leftId = generateNodeId();
4105
+ const rightId = generateNodeId();
4106
+ nodes[leftId] = {
4107
+ type: { resolvedName: "Container" },
4108
+ isCanvas: true,
4109
+ props: { background: "#f9f9f9", padding: 10 },
4110
+ displayName: "Container",
4111
+ custom: {},
4112
+ hidden: false,
4113
+ nodes: [],
4114
+ linkedNodes: {},
4115
+ parent: nodeId
4116
+ };
4117
+ nodes[rightId] = {
4118
+ type: { resolvedName: "Container" },
4119
+ isCanvas: true,
4120
+ props: { background: "#f9f9f9", padding: 10 },
4121
+ displayName: "Container",
4122
+ custom: {},
4123
+ hidden: false,
4124
+ nodes: [],
4125
+ linkedNodes: {},
4126
+ parent: nodeId
4127
+ };
4128
+ nestedLinkedNodes["left-column"] = leftId;
4129
+ nestedLinkedNodes["right-column"] = rightId;
4130
+ }
4131
+ nodes[nodeId] = {
4132
+ type: { resolvedName: typeName },
4133
+ isCanvas,
4134
+ props,
4135
+ displayName: typeName,
4136
+ custom: {},
4137
+ hidden: false,
4138
+ nodes: nestedChildren,
4139
+ linkedNodes: nestedLinkedNodes,
4140
+ parent: "ROOT"
4141
+ };
4142
+ childNodeIds.push(nodeId);
4143
+ }
4144
+ }
4145
+ nodes["ROOT"] = {
4146
+ type: { resolvedName: "Paper" },
4147
+ isCanvas: true,
4148
+ props: { background: "#ffffff" },
4149
+ displayName: "Paper",
4150
+ custom: {},
4151
+ hidden: false,
4152
+ nodes: childNodeIds,
4153
+ linkedNodes: {}
4154
+ };
4155
+ return JSON.stringify(nodes);
4156
+ };
4003
4157
  var Modal = ({
4004
4158
  open,
4005
4159
  onClose,
@@ -4701,7 +4855,8 @@ var EmailEditor = ({
4701
4855
  showToolbox = true,
4702
4856
  darkMode = true,
4703
4857
  className,
4704
- style
4858
+ style,
4859
+ initialHtml
4705
4860
  }) => {
4706
4861
  var _a, _b, _c, _d;
4707
4862
  const mergedComponents = useMemo(() => {
@@ -4819,7 +4974,7 @@ var EmailEditor = ({
4819
4974
  onClick: () => {
4820
4975
  setActiveTab("properties");
4821
4976
  },
4822
- children: /* @__PURE__ */ jsx(Frame, { data: initialState, children: defaultEmailContent })
4977
+ children: /* @__PURE__ */ jsx("div", { className: "p-8", children: initialState ? /* @__PURE__ */ jsx(Frame, { data: initialState, children: defaultEmailContent }) : initialHtml ? /* @__PURE__ */ jsx(Frame, { data: importHtmlToState(initialHtml), children: defaultEmailContent }) : /* @__PURE__ */ jsx(Frame, { children: defaultContent || defaultEmailContent }) })
4823
4978
  }
4824
4979
  )
4825
4980
  }