@kontakto/email-template-editor 2.2.0 → 2.2.1

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
@@ -1,6 +1,6 @@
1
1
  import DOMPurify from 'dompurify';
2
2
  import { marked, Renderer } from 'marked';
3
- import React58, { createContext, forwardRef, useRef, useEffect, useImperativeHandle, useMemo, useContext, useState, useCallback, Fragment } from 'react';
3
+ import React58, { createContext, forwardRef, useRef, useEffect, useImperativeHandle, useMemo, useContext, useState, useCallback, useLayoutEffect, Fragment } from 'react';
4
4
  import { z } from 'zod';
5
5
  import { renderToStaticMarkup as renderToStaticMarkup$1 } from 'react-dom/server';
6
6
  import { createTheme, alpha, lighten, darken } from '@mui/material/styles';
@@ -6511,11 +6511,20 @@ function HeadingEditor({ style, props }) {
6511
6511
  const handleTextChange = (e) => {
6512
6512
  commitText(e.target.value);
6513
6513
  };
6514
+ const displayRef = useRef(null);
6515
+ const lastDisplayHeightRef = useRef(0);
6516
+ useLayoutEffect(() => {
6517
+ if (!isSelected && displayRef.current) {
6518
+ const h = displayRef.current.offsetHeight;
6519
+ if (h > 0) lastDisplayHeightRef.current = h;
6520
+ }
6521
+ }, [isSelected, textContent, isMarkdown, level]);
6514
6522
  const adjustTextareaHeight = (element) => {
6515
- if (element) {
6516
- element.style.height = "auto";
6517
- element.style.height = `${element.scrollHeight}px`;
6518
- }
6523
+ if (!element) return;
6524
+ element.style.height = "auto";
6525
+ const scrollH = element.scrollHeight;
6526
+ const floor = lastDisplayHeightRef.current;
6527
+ element.style.height = `${Math.max(scrollH, floor)}px`;
6519
6528
  };
6520
6529
  const { textareaRef, trackFocus, handleKeyDown, toolbarProps } = useMarkdownToolbar({
6521
6530
  text: localText,
@@ -6530,9 +6539,9 @@ function HeadingEditor({ style, props }) {
6530
6539
  });
6531
6540
  }
6532
6541
  });
6533
- useEffect(() => {
6542
+ useLayoutEffect(() => {
6534
6543
  if (textareaRef.current) adjustTextareaHeight(textareaRef.current);
6535
- }, [localText, textareaRef]);
6544
+ }, [localText, isSelected]);
6536
6545
  if (isSelected) {
6537
6546
  return /* @__PURE__ */ React58.createElement(React58.Fragment, null, /* @__PURE__ */ React58.createElement(
6538
6547
  "textarea",
@@ -6554,7 +6563,11 @@ function HeadingEditor({ style, props }) {
6554
6563
  }
6555
6564
  ), /* @__PURE__ */ React58.createElement(InlineFormattingToolbar, __spreadValues({ anchorEl: textareaRef.current }, toolbarProps)));
6556
6565
  }
6557
- const headingProps = isMarkdown ? { style: hStyle, dangerouslySetInnerHTML: { __html: renderInlineMarkdownString(textContent) } } : { style: hStyle, children: textContent };
6566
+ const headingProps = isMarkdown ? {
6567
+ ref: displayRef,
6568
+ style: hStyle,
6569
+ dangerouslySetInnerHTML: { __html: renderInlineMarkdownString(textContent) }
6570
+ } : { ref: displayRef, style: hStyle, children: textContent };
6558
6571
  switch (level) {
6559
6572
  case "h1":
6560
6573
  return /* @__PURE__ */ React58.createElement("h1", __spreadValues({}, headingProps));
@@ -6986,11 +6999,20 @@ function TextEditor({ style, props }) {
6986
6999
  const handleTextChange = (e) => {
6987
7000
  commitText(e.target.value);
6988
7001
  };
7002
+ const displayRef = useRef(null);
7003
+ const lastDisplayHeightRef = useRef(0);
7004
+ useLayoutEffect(() => {
7005
+ if (!isSelected && displayRef.current) {
7006
+ const h = displayRef.current.offsetHeight;
7007
+ if (h > 0) lastDisplayHeightRef.current = h;
7008
+ }
7009
+ }, [isSelected, textContent, isMarkdown]);
6989
7010
  const adjustTextareaHeight = (element) => {
6990
- if (element) {
6991
- element.style.height = "auto";
6992
- element.style.height = `${element.scrollHeight}px`;
6993
- }
7011
+ if (!element) return;
7012
+ element.style.height = "auto";
7013
+ const scrollH = element.scrollHeight;
7014
+ const floor = lastDisplayHeightRef.current;
7015
+ element.style.height = `${Math.max(scrollH, floor)}px`;
6994
7016
  };
6995
7017
  const { textareaRef, trackFocus, handleKeyDown, toolbarProps } = useMarkdownToolbar({
6996
7018
  text: localText,
@@ -7005,9 +7027,9 @@ function TextEditor({ style, props }) {
7005
7027
  });
7006
7028
  }
7007
7029
  });
7008
- useEffect(() => {
7030
+ useLayoutEffect(() => {
7009
7031
  if (textareaRef.current) adjustTextareaHeight(textareaRef.current);
7010
- }, [localText, textareaRef]);
7032
+ }, [localText, isSelected]);
7011
7033
  if (isSelected) {
7012
7034
  return /* @__PURE__ */ React58.createElement(React58.Fragment, null, /* @__PURE__ */ React58.createElement(
7013
7035
  "textarea",
@@ -7030,9 +7052,16 @@ function TextEditor({ style, props }) {
7030
7052
  ), /* @__PURE__ */ React58.createElement(InlineFormattingToolbar, __spreadValues({ anchorEl: textareaRef.current }, toolbarProps)));
7031
7053
  }
7032
7054
  if (isMarkdown) {
7033
- return /* @__PURE__ */ React58.createElement(EmailMarkdown, { style: wStyle, markdown: textContent });
7055
+ return /* @__PURE__ */ React58.createElement(
7056
+ "div",
7057
+ {
7058
+ ref: displayRef,
7059
+ style: wStyle,
7060
+ dangerouslySetInnerHTML: { __html: renderMarkdownString(textContent) }
7061
+ }
7062
+ );
7034
7063
  }
7035
- return /* @__PURE__ */ React58.createElement("div", { style: wStyle }, textContent);
7064
+ return /* @__PURE__ */ React58.createElement("div", { ref: displayRef, style: wStyle }, textContent);
7036
7065
  }
7037
7066
 
7038
7067
  // src/editor/core.tsx