@farmzone/fz-react-ui 1.1.17 → 1.1.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.cjs CHANGED
@@ -10434,7 +10434,7 @@ function buildExtensions(config) {
10434
10434
  accentTextColor,
10435
10435
  zIndex
10436
10436
  } = config;
10437
- const exts = [extensionTextStyle.TextStyleKit, buildMarkResetOnEnter()];
10437
+ const exts = [extensionTextStyle.TextStyleKit, buildMarkResetOnEnter(), buildRedundantListMarkerStrip()];
10438
10438
  const headingLevels = toolbar.heading === false ? [] : Array.isArray(toolbar.heading) ? [...toolbar.heading] : [1, 2, 3, 4];
10439
10439
  exts.push(
10440
10440
  StarterKit__default.default.configure({
@@ -10610,6 +10610,36 @@ function buildMarkResetOnEnter() {
10610
10610
  }
10611
10611
  });
10612
10612
  }
10613
+ function buildRedundantListMarkerStrip() {
10614
+ const bulletMarkerRegex = /^\s*([-+*])\s$/;
10615
+ const orderedMarkerRegex = /^(\d+)\.\s$/;
10616
+ function makeRule(listTypeName, regex) {
10617
+ return new core.InputRule({
10618
+ find: regex,
10619
+ handler: ({ state, range, chain }) => {
10620
+ const { $from } = state.selection;
10621
+ if ($from.depth < 2) return null;
10622
+ const parent = $from.node($from.depth - 1);
10623
+ const grandparent = $from.node($from.depth - 2);
10624
+ if (parent.type.name !== "listItem" || grandparent.type.name !== listTypeName) {
10625
+ return null;
10626
+ }
10627
+ chain().deleteRange(range).run();
10628
+ return void 0;
10629
+ }
10630
+ });
10631
+ }
10632
+ return core.Extension.create({
10633
+ name: "redundantListMarkerStrip",
10634
+ priority: 200,
10635
+ addInputRules() {
10636
+ return [
10637
+ makeRule("bulletList", bulletMarkerRegex),
10638
+ makeRule("orderedList", orderedMarkerRegex)
10639
+ ];
10640
+ }
10641
+ });
10642
+ }
10613
10643
 
10614
10644
  // src/components/RichEditor/config/keyboardHandlers.ts
10615
10645
  function createKeyboardHandler(codeBlockEmptyEnterRef, onSubmitRef) {
@@ -10730,12 +10760,15 @@ function buildAcceptAttribute(fileFormat) {
10730
10760
  return Array.from(expandFormats(fileFormat)).map((ext) => `.${ext}`).join(",");
10731
10761
  }
10732
10762
  function useRichEditorFiles(options = {}) {
10733
- const { fileFormat, onFileUpload } = options;
10734
- const [attachments, setAttachments] = React6.useState([]);
10763
+ const { fileFormat, onFileUpload, initialAttachments } = options;
10764
+ const [attachments, setAttachments] = React6.useState(() => initialAttachments ?? []);
10735
10765
  const [isUploading, setIsUploading] = React6.useState(false);
10736
10766
  const removeAttachment = React6.useCallback((id) => {
10737
10767
  setAttachments((prev) => prev.filter((a) => a.id !== id));
10738
10768
  }, []);
10769
+ const hydrateAttachments = React6.useCallback((loaded) => {
10770
+ setAttachments(loaded);
10771
+ }, []);
10739
10772
  const processFiles = React6.useCallback(
10740
10773
  async (files, editor) => {
10741
10774
  const allowedExts = fileFormat ? expandFormats(fileFormat) : null;
@@ -10744,7 +10777,12 @@ function useRichEditorFiles(options = {}) {
10744
10777
  setIsUploading(true);
10745
10778
  try {
10746
10779
  for (const file of accepted) {
10747
- const url = await (onFileUpload ? onFileUpload(file) : URL.createObjectURL(file));
10780
+ let url;
10781
+ try {
10782
+ url = await (onFileUpload ? onFileUpload(file) : URL.createObjectURL(file));
10783
+ } catch {
10784
+ continue;
10785
+ }
10748
10786
  if (file.type.startsWith("image/")) {
10749
10787
  if (!editor) continue;
10750
10788
  editor.chain().focus().insertContent({ type: "image", attrs: { src: url } }).run();
@@ -10769,7 +10807,58 @@ function useRichEditorFiles(options = {}) {
10769
10807
  },
10770
10808
  [fileFormat, onFileUpload]
10771
10809
  );
10772
- return { attachments, processFiles, removeAttachment, isUploading };
10810
+ return { attachments, processFiles, removeAttachment, hydrateAttachments, isUploading };
10811
+ }
10812
+
10813
+ // src/components/RichEditor/config/attachmentSerialization.ts
10814
+ var ATTACHMENTS_CONTAINER_ATTR = "data-rich-editor-attachments";
10815
+ var ATTACHMENT_ITEM_ATTR = "data-rich-editor-attachment";
10816
+ function escapeHtmlAttr(value) {
10817
+ return value.replace(/&/g, "&amp;").replace(/"/g, "&quot;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
10818
+ }
10819
+ function escapeHtmlText(value) {
10820
+ return value.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
10821
+ }
10822
+ function serializeAttachmentsBlock(attachments) {
10823
+ if (attachments.length === 0) return "";
10824
+ const items = attachments.map((attachment) => {
10825
+ const href = escapeHtmlAttr(attachment.url);
10826
+ const name = escapeHtmlAttr(attachment.name);
10827
+ return `<a ${ATTACHMENT_ITEM_ATTR}="true" href="${href}" data-name="${name}" data-size="${attachment.size}" data-mime="${escapeHtmlAttr(attachment.mimeType)}">${escapeHtmlText(attachment.name)}</a>`;
10828
+ }).join("");
10829
+ return `<div ${ATTACHMENTS_CONTAINER_ATTR}="true">${items}</div>`;
10830
+ }
10831
+ function splitAttachmentsFromHtml(html) {
10832
+ if (!html || !html.includes(ATTACHMENTS_CONTAINER_ATTR) || typeof DOMParser === "undefined") {
10833
+ return { html, attachments: [] };
10834
+ }
10835
+ const doc = new DOMParser().parseFromString(html, "text/html");
10836
+ const container2 = doc.body.querySelector(`[${ATTACHMENTS_CONTAINER_ATTR}]`);
10837
+ if (!container2) {
10838
+ return { html, attachments: [] };
10839
+ }
10840
+ const attachments = Array.from(
10841
+ container2.querySelectorAll(`[${ATTACHMENT_ITEM_ATTR}]`)
10842
+ ).map((el, index) => {
10843
+ const url = el.getAttribute("href") ?? "";
10844
+ const name = el.getAttribute("data-name") ?? el.textContent ?? `file-${index}`;
10845
+ const mimeType = el.getAttribute("data-mime") ?? "";
10846
+ const size2 = Number(el.getAttribute("data-size") ?? "0") || 0;
10847
+ return {
10848
+ id: `loaded-${index}-${Math.random().toString(36).slice(2)}`,
10849
+ name,
10850
+ size: size2,
10851
+ mimeType,
10852
+ file: new File([], name, { type: mimeType }),
10853
+ url
10854
+ };
10855
+ });
10856
+ container2.remove();
10857
+ return { html: doc.body.innerHTML, attachments };
10858
+ }
10859
+ function composeRichEditorHtml(bodyHtml, attachments) {
10860
+ const block = serializeAttachmentsBlock(attachments);
10861
+ return block ? `${bodyHtml}${block}` : bodyHtml;
10773
10862
  }
10774
10863
 
10775
10864
  // src/components/RichEditor/hooks/useRichEditor.ts
@@ -10800,10 +10889,18 @@ function useRichEditor(options = {}) {
10800
10889
  onChangeRef.current = onChange;
10801
10890
  const onSubmitRef = React6.useRef(onSubmit);
10802
10891
  onSubmitRef.current = onSubmit;
10803
- const { attachments, processFiles, removeAttachment, isUploading } = useRichEditorFiles({
10892
+ const initialSplitRef = React6.useRef(null);
10893
+ if (initialSplitRef.current === null) {
10894
+ initialSplitRef.current = splitAttachmentsFromHtml(defaultValue ?? "");
10895
+ }
10896
+ const initialSplit = initialSplitRef.current;
10897
+ const { attachments, processFiles, removeAttachment, hydrateAttachments, isUploading } = useRichEditorFiles({
10804
10898
  fileFormat,
10805
- onFileUpload
10899
+ onFileUpload,
10900
+ initialAttachments: initialSplit.attachments
10806
10901
  });
10902
+ const attachmentsRef = React6.useRef(attachments);
10903
+ attachmentsRef.current = attachments;
10807
10904
  const editorRef = React6.useRef(null);
10808
10905
  const processFilesRef = React6.useRef(() => {
10809
10906
  });
@@ -10837,21 +10934,27 @@ function useRichEditor(options = {}) {
10837
10934
  handleDrop: createDropHandler(processFilesRef)
10838
10935
  } : {}
10839
10936
  },
10840
- content: defaultValue ?? "",
10937
+ content: initialSplit.html,
10841
10938
  onUpdate: ({ editor: editor2 }) => {
10842
- onChangeRef.current?.(editor2.getHTML());
10939
+ onChangeRef.current?.(composeRichEditorHtml(editor2.getHTML(), attachmentsRef.current));
10843
10940
  }
10844
10941
  });
10845
10942
  editorRef.current = editor;
10943
+ React6.useEffect(() => {
10944
+ if (!editor) return;
10945
+ onChangeRef.current?.(composeRichEditorHtml(editor.getHTML(), attachments));
10946
+ }, [attachments]);
10846
10947
  React6.useEffect(() => {
10847
10948
  if (editor) editor.setEditable(editable && !isUploading);
10848
10949
  }, [editor, editable, isUploading]);
10849
10950
  return {
10850
10951
  editor,
10851
- getHTML: () => editor?.getHTML() ?? "",
10952
+ getHTML: () => composeRichEditorHtml(editor?.getHTML() ?? "", attachments),
10852
10953
  getJSON: () => editor?.getJSON() ?? {},
10853
10954
  setContent: (html) => {
10854
- editor?.commands.setContent(html);
10955
+ const split = splitAttachmentsFromHtml(html);
10956
+ editor?.commands.setContent(split.html);
10957
+ hydrateAttachments(split.attachments);
10855
10958
  },
10856
10959
  clearContent: () => {
10857
10960
  editor?.commands.clearContent();
@@ -10862,6 +10965,7 @@ function useRichEditor(options = {}) {
10862
10965
  isEmpty: editor?.isEmpty ?? true,
10863
10966
  attachments,
10864
10967
  removeAttachment,
10968
+ hydrateAttachments,
10865
10969
  processFiles: processFilesRef.current,
10866
10970
  isUploading
10867
10971
  };
@@ -11654,7 +11758,16 @@ var RichEditor = React6.forwardRef(function RichEditor2({
11654
11758
  },
11655
11759
  [onChange]
11656
11760
  );
11657
- const { editor, attachments, removeAttachment, processFiles, isUploading } = useRichEditor({
11761
+ const {
11762
+ editor,
11763
+ attachments,
11764
+ removeAttachment,
11765
+ hydrateAttachments,
11766
+ processFiles,
11767
+ isUploading,
11768
+ getHTML,
11769
+ setContent
11770
+ } = useRichEditor({
11658
11771
  defaultValue: value ?? defaultValue,
11659
11772
  toolbar: activeToolbar,
11660
11773
  mentionUsers,
@@ -11681,18 +11794,18 @@ var RichEditor = React6.forwardRef(function RichEditor2({
11681
11794
  isInternalChangeRef.current = false;
11682
11795
  return;
11683
11796
  }
11684
- if (editor.getHTML() !== value) {
11685
- editor.commands.setContent(value, { emitUpdate: false });
11797
+ if (getHTML() !== value) {
11798
+ const split = splitAttachmentsFromHtml(value);
11799
+ editor.commands.setContent(split.html, { emitUpdate: false });
11800
+ hydrateAttachments(split.attachments);
11686
11801
  }
11687
11802
  }, [value, editor]);
11688
11803
  React6.useImperativeHandle(
11689
11804
  ref,
11690
11805
  () => ({
11691
- getHTML: () => editor?.getHTML() ?? "",
11806
+ getHTML,
11692
11807
  getJSON: () => editor?.getJSON() ?? {},
11693
- setContent: (html) => {
11694
- editor?.commands.setContent(html);
11695
- },
11808
+ setContent,
11696
11809
  clearContent: () => {
11697
11810
  editor?.commands.clearContent();
11698
11811
  },
@@ -11705,7 +11818,7 @@ var RichEditor = React6.forwardRef(function RichEditor2({
11705
11818
  attachments,
11706
11819
  editor
11707
11820
  }),
11708
- [editor, attachments]
11821
+ [editor, attachments, getHTML, setContent]
11709
11822
  );
11710
11823
  const isToolbarVisible = showToolbar && editable;
11711
11824
  const imageFeatureEnabled = activeToolbar.image !== false;