easy-email-extensions 4.0.0-alpha.8 → 4.1.0

Sign up to get free protection for your applications and to get access to all the features.
package/lib/index2.js CHANGED
@@ -55,7 +55,7 @@ var __async = (__this, __arguments, generator) => {
55
55
  };
56
56
  import * as React from "react";
57
57
  import React__default, { Children, isValidElement, cloneElement, createContext, useContext, Component, useMemo, memo, forwardRef, useEffect, useRef, useLayoutEffect, useState, createRef, useImperativeHandle, PureComponent, useCallback, useReducer, Fragment, createElement, Suspense } from "react";
58
- import { IconFont, useEditorProps, Stack as Stack$6, getShadowRoot, TextStyle, useEditorContext, useBlock, useFocusIdx, useFocusBlockLayout, getEditNode, MergeTagBadge, FIXED_CONTAINER_ID, getPluginElement, isTextBlock, scrollBlockEleIntoView, useHoverIdx, useDataTransfer, useRefState, getBlockNodeByChildEle, getDirectionPosition, DATA_ATTRIBUTE_DROP_CONTAINER, BlockAvatarWrapper, getBlockNodeByIdx, useLazyState, getEditorRoot, useActiveTab, ActiveTabKeys } from "easy-email-editor";
58
+ import { IconFont, useEditorProps, Stack as Stack$6, getShadowRoot, DATA_CONTENT_EDITABLE_TYPE, ContentEditableType, TextStyle, useEditorContext, useBlock, useFocusIdx, useFocusBlockLayout, MergeTagBadge, FIXED_CONTAINER_ID, getPluginElement, RICH_TEXT_BAR_ID, CONTENT_EDITABLE_CLASS_NAME, getEditorRoot, DATA_CONTENT_EDITABLE_IDX, scrollBlockEleIntoView, useHoverIdx, useDataTransfer, useRefState, getBlockNodeByChildEle, getDirectionPosition, DATA_ATTRIBUTE_DROP_CONTAINER, BlockAvatarWrapper, isTextBlock, getBlockNodeByIdx, useLazyState, useActiveTab, ActiveTabKeys } from "easy-email-editor";
59
59
  import { BasicType, ImageManager, EMAIL_BLOCK_CLASS_NAME, BlockManager, createBlockDataByType, AdvancedType, Operator, OperatorSymbol, isAdvancedBlock, getParentByIdx, getParentIdx, getIndexByIdx, getSiblingIdx, getNodeIdxFromClassName, getNodeIdxClassName, getPageIdx, getChildIdx, MjmlToJson, JsonToMjml, getNodeTypeFromClassName } from "easy-email-core";
60
60
  import ReactDOM, { findDOMNode, render as render$1, unmountComponentAtNode, createPortal } from "react-dom";
61
61
  import { useField, Field, useForm as useForm$1, Form as Form$3, version as version$2, useFormState } from "react-final-form";
@@ -40216,17 +40216,28 @@ function InlineText({ idx, onChange, children }) {
40216
40216
  useEffect(() => {
40217
40217
  const shadowRoot = getShadowRoot();
40218
40218
  const onPaste = (e) => {
40219
- var _a;
40219
+ var _a, _b;
40220
40220
  if (!(e.target instanceof Element) || !e.target.getAttribute("contenteditable"))
40221
40221
  return;
40222
40222
  e.preventDefault();
40223
40223
  const text = ((_a = e.clipboardData) == null ? void 0 : _a.getData("text/plain")) || "";
40224
40224
  document.execCommand("insertHTML", false, text);
40225
- onChange(e.target.innerHTML || "");
40225
+ const contentEditableType = e.target.getAttribute(DATA_CONTENT_EDITABLE_TYPE);
40226
+ if (contentEditableType === ContentEditableType.RichText) {
40227
+ onChange(e.target.innerHTML || "");
40228
+ } else if (contentEditableType === ContentEditableType.Text) {
40229
+ onChange(((_b = e.target.textContent) == null ? void 0 : _b.trim()) || "");
40230
+ }
40226
40231
  };
40227
40232
  const onInput = (e) => {
40233
+ var _a;
40228
40234
  if (e.target instanceof Element && e.target.getAttribute("contenteditable")) {
40229
- onChange(e.target.innerHTML || "");
40235
+ const contentEditableType = e.target.getAttribute(DATA_CONTENT_EDITABLE_TYPE);
40236
+ if (contentEditableType === ContentEditableType.RichText) {
40237
+ onChange(e.target.innerHTML || "");
40238
+ } else if (contentEditableType === ContentEditableType.Text) {
40239
+ onChange(((_a = e.target.textContent) == null ? void 0 : _a.trim()) || "");
40240
+ }
40230
40241
  }
40231
40242
  };
40232
40243
  shadowRoot.addEventListener("paste", onPaste, true);
@@ -43324,6 +43335,7 @@ function BasicTools() {
43324
43335
  const { copyBlock, removeBlock } = useBlock();
43325
43336
  const { focusIdx: focusIdx2, setFocusIdx } = useFocusIdx();
43326
43337
  const { modal, setModalVisible } = useAddToCollection();
43338
+ const { onAddCollection } = useEditorProps();
43327
43339
  const handleAddToCollection = () => {
43328
43340
  if (document.activeElement instanceof HTMLElement) {
43329
43341
  document.activeElement.blur();
@@ -43364,7 +43376,7 @@ function BasicTools() {
43364
43376
  icon: /* @__PURE__ */ React__default.createElement(IconFont, {
43365
43377
  iconName: "icon-copy"
43366
43378
  })
43367
- }), /* @__PURE__ */ React__default.createElement(ToolItem$1, {
43379
+ }), onAddCollection && /* @__PURE__ */ React__default.createElement(ToolItem$1, {
43368
43380
  onClick: handleAddToCollection,
43369
43381
  title: "Add to collection",
43370
43382
  icon: /* @__PURE__ */ React__default.createElement(IconFont, {
@@ -43632,17 +43644,13 @@ function Tools(props) {
43632
43644
  const { mergeTags: mergeTags2, enabledMergeTagsBadge } = useEditorProps();
43633
43645
  const { focusBlockNode } = useFocusBlockLayout();
43634
43646
  const { selectionRange, restoreRange, setRangeByElement } = useSelectionRange();
43635
- const execCommand = (cmd, val) => {
43636
- const container2 = getEditNode(focusBlockNode);
43637
- if (!container2) {
43638
- console.error("No container");
43639
- return;
43640
- }
43647
+ const execCommand = useCallback((cmd, val) => {
43648
+ var _a;
43641
43649
  if (!selectionRange) {
43642
43650
  console.error("No selectionRange");
43643
43651
  return;
43644
43652
  }
43645
- if (!(container2 == null ? void 0 : container2.contains(selectionRange == null ? void 0 : selectionRange.commonAncestorContainer)) && container2 !== (selectionRange == null ? void 0 : selectionRange.commonAncestorContainer)) {
43653
+ if (!(focusBlockNode == null ? void 0 : focusBlockNode.contains(selectionRange == null ? void 0 : selectionRange.commonAncestorContainer))) {
43646
43654
  console.error("Not commonAncestorContainer");
43647
43655
  return;
43648
43656
  }
@@ -43678,19 +43686,15 @@ function Tools(props) {
43678
43686
  } else {
43679
43687
  document.execCommand(cmd, false, val);
43680
43688
  }
43681
- const html = container2.innerHTML;
43689
+ const html = ((_a = getShadowRoot().activeElement) == null ? void 0 : _a.innerHTML) || "";
43682
43690
  props.onChange(html);
43683
- };
43691
+ }, [enabledMergeTagsBadge, focusBlockNode, props, restoreRange, selectionRange, setRangeByElement]);
43684
43692
  const execCommandWithRange = useCallback((cmd, val) => {
43685
- const container2 = getEditNode(focusBlockNode);
43686
- if (!container2) {
43687
- console.error("No container");
43688
- return;
43689
- }
43693
+ var _a;
43690
43694
  document.execCommand(cmd, false, val);
43691
- const html = container2.innerHTML;
43695
+ const html = ((_a = getShadowRoot().activeElement) == null ? void 0 : _a.innerHTML) || "";
43692
43696
  props.onChange(html);
43693
- }, [focusBlockNode, props]);
43697
+ }, [props.onChange]);
43694
43698
  const getPopoverMountNode = () => document.getElementById(FIXED_CONTAINER_ID);
43695
43699
  return /* @__PURE__ */ React__default.createElement("div", {
43696
43700
  id: "Tools",
@@ -43821,6 +43825,7 @@ function RichTextToolBar(props) {
43821
43825
  return /* @__PURE__ */ React__default.createElement(React__default.Fragment, null, createPortal(/* @__PURE__ */ React__default.createElement(React__default.Fragment, null, /* @__PURE__ */ React__default.createElement("style", {
43822
43826
  dangerouslySetInnerHTML: { __html: styleText }
43823
43827
  }), /* @__PURE__ */ React__default.createElement("div", {
43828
+ id: RICH_TEXT_BAR_ID,
43824
43829
  style: {
43825
43830
  transform: "translate(0,0)",
43826
43831
  padding: "4px 8px",
@@ -43845,20 +43850,67 @@ function RichTextToolBar(props) {
43845
43850
  }))), root2));
43846
43851
  }
43847
43852
  const RichTextField = (props) => {
43848
- const { focusBlock: focusBlock2 } = useBlock();
43849
- const { focusIdx: focusIdx2 } = useFocusIdx();
43850
- if (!isTextBlock(focusBlock2 == null ? void 0 : focusBlock2.type))
43853
+ const [contentEditableName, setContentEditableName] = useState("");
43854
+ const [contentEditableType, setContentEditableType] = useState(CONTENT_EDITABLE_CLASS_NAME);
43855
+ useEffect(() => {
43856
+ const onClick = (e) => {
43857
+ var _a;
43858
+ if ((_a = getEditorRoot()) == null ? void 0 : _a.contains(e.target)) {
43859
+ return;
43860
+ }
43861
+ const fixedContainer = document.getElementById(FIXED_CONTAINER_ID);
43862
+ if (fixedContainer == null ? void 0 : fixedContainer.contains(e.target)) {
43863
+ return;
43864
+ }
43865
+ setContentEditableName("");
43866
+ };
43867
+ window.addEventListener("click", onClick);
43868
+ return () => {
43869
+ window.removeEventListener("click", onClick);
43870
+ };
43871
+ }, []);
43872
+ useEffect(() => {
43873
+ const root2 = getShadowRoot();
43874
+ if (!root2)
43875
+ return;
43876
+ const onClick = (e) => {
43877
+ const target2 = e.target;
43878
+ const fixedContainer = document.getElementById(FIXED_CONTAINER_ID);
43879
+ const richTextBar = root2.getElementById(RICH_TEXT_BAR_ID);
43880
+ if ((fixedContainer == null ? void 0 : fixedContainer.contains(target2)) || (richTextBar == null ? void 0 : richTextBar.contains(target2))) {
43881
+ return;
43882
+ }
43883
+ const activeElement = getShadowRoot().activeElement;
43884
+ if (!activeElement) {
43885
+ setContentEditableName("");
43886
+ } else {
43887
+ const idxName = activeElement.getAttribute(DATA_CONTENT_EDITABLE_IDX);
43888
+ const type = activeElement.getAttribute(DATA_CONTENT_EDITABLE_TYPE);
43889
+ setContentEditableType(type);
43890
+ if (idxName) {
43891
+ setContentEditableName(idxName);
43892
+ } else {
43893
+ setContentEditableName("");
43894
+ }
43895
+ }
43896
+ };
43897
+ root2.addEventListener("click", onClick);
43898
+ return () => {
43899
+ root2.removeEventListener("click", onClick);
43900
+ };
43901
+ }, []);
43902
+ if (!contentEditableName)
43851
43903
  return null;
43852
- const name = `${focusIdx2}.data.value.content`;
43853
43904
  return /* @__PURE__ */ React__default.createElement(React__default.Fragment, null, /* @__PURE__ */ React__default.createElement(Field, {
43854
- name,
43905
+ name: contentEditableName,
43855
43906
  parse: (v) => v
43856
43907
  }, ({ input }) => /* @__PURE__ */ React__default.createElement(FieldWrapper, __spreadProps(__spreadValues({}, props), {
43908
+ contentEditableType,
43857
43909
  input
43858
43910
  }))));
43859
43911
  };
43860
43912
  function FieldWrapper(props) {
43861
- const _a = props, { input } = _a, rest = __objRest(_a, ["input"]);
43913
+ const _a = props, { input, contentEditableType } = _a, rest = __objRest(_a, ["input", "contentEditableType"]);
43862
43914
  const { mergeTagGenerate, enabledMergeTagsBadge } = useEditorProps();
43863
43915
  const debounceCallbackChange = useCallback(lodash.exports.debounce((val) => {
43864
43916
  if (enabledMergeTagsBadge) {
@@ -43868,7 +43920,7 @@ function FieldWrapper(props) {
43868
43920
  }
43869
43921
  input.onBlur();
43870
43922
  }, 200), [input]);
43871
- return /* @__PURE__ */ React__default.createElement(React__default.Fragment, null, /* @__PURE__ */ React__default.createElement(RichTextToolBar, {
43923
+ return /* @__PURE__ */ React__default.createElement(React__default.Fragment, null, contentEditableType === ContentEditableType.RichText && /* @__PURE__ */ React__default.createElement(RichTextToolBar, {
43872
43924
  onChange: debounceCallbackChange
43873
43925
  }), /* @__PURE__ */ React__default.createElement(InlineText, __spreadProps(__spreadValues({}, rest), {
43874
43926
  onChange: debounceCallbackChange