@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.d.cts CHANGED
@@ -1449,6 +1449,7 @@ interface UseRichEditorReturn {
1449
1449
  isEmpty: boolean;
1450
1450
  attachments: FileAttachment[];
1451
1451
  removeAttachment: (id: string) => void;
1452
+ hydrateAttachments: (loaded: FileAttachment[]) => void;
1452
1453
  processFiles: (files: File[]) => void;
1453
1454
  isUploading: boolean;
1454
1455
  }
package/dist/index.d.ts CHANGED
@@ -1449,6 +1449,7 @@ interface UseRichEditorReturn {
1449
1449
  isEmpty: boolean;
1450
1450
  attachments: FileAttachment[];
1451
1451
  removeAttachment: (id: string) => void;
1452
+ hydrateAttachments: (loaded: FileAttachment[]) => void;
1452
1453
  processFiles: (files: File[]) => void;
1453
1454
  isUploading: boolean;
1454
1455
  }
package/dist/index.js CHANGED
@@ -30,7 +30,7 @@ import { createJSONStorage, persist } from 'zustand/middleware';
30
30
  import { v4 } from 'uuid';
31
31
  import heic2any from 'heic2any';
32
32
  import { useEditor, useEditorState, EditorContent, ReactRenderer } from '@tiptap/react';
33
- import { Extension } from '@tiptap/core';
33
+ import { Extension, InputRule } from '@tiptap/core';
34
34
  import StarterKit from '@tiptap/starter-kit';
35
35
  import TextAlign from '@tiptap/extension-text-align';
36
36
  import { Image as Image$1 } from '@tiptap/extension-image';
@@ -10397,7 +10397,7 @@ function buildExtensions(config) {
10397
10397
  accentTextColor,
10398
10398
  zIndex
10399
10399
  } = config;
10400
- const exts = [TextStyleKit, buildMarkResetOnEnter()];
10400
+ const exts = [TextStyleKit, buildMarkResetOnEnter(), buildRedundantListMarkerStrip()];
10401
10401
  const headingLevels = toolbar.heading === false ? [] : Array.isArray(toolbar.heading) ? [...toolbar.heading] : [1, 2, 3, 4];
10402
10402
  exts.push(
10403
10403
  StarterKit.configure({
@@ -10573,6 +10573,36 @@ function buildMarkResetOnEnter() {
10573
10573
  }
10574
10574
  });
10575
10575
  }
10576
+ function buildRedundantListMarkerStrip() {
10577
+ const bulletMarkerRegex = /^\s*([-+*])\s$/;
10578
+ const orderedMarkerRegex = /^(\d+)\.\s$/;
10579
+ function makeRule(listTypeName, regex) {
10580
+ return new InputRule({
10581
+ find: regex,
10582
+ handler: ({ state, range, chain }) => {
10583
+ const { $from } = state.selection;
10584
+ if ($from.depth < 2) return null;
10585
+ const parent = $from.node($from.depth - 1);
10586
+ const grandparent = $from.node($from.depth - 2);
10587
+ if (parent.type.name !== "listItem" || grandparent.type.name !== listTypeName) {
10588
+ return null;
10589
+ }
10590
+ chain().deleteRange(range).run();
10591
+ return void 0;
10592
+ }
10593
+ });
10594
+ }
10595
+ return Extension.create({
10596
+ name: "redundantListMarkerStrip",
10597
+ priority: 200,
10598
+ addInputRules() {
10599
+ return [
10600
+ makeRule("bulletList", bulletMarkerRegex),
10601
+ makeRule("orderedList", orderedMarkerRegex)
10602
+ ];
10603
+ }
10604
+ });
10605
+ }
10576
10606
 
10577
10607
  // src/components/RichEditor/config/keyboardHandlers.ts
10578
10608
  function createKeyboardHandler(codeBlockEmptyEnterRef, onSubmitRef) {
@@ -10693,12 +10723,15 @@ function buildAcceptAttribute(fileFormat) {
10693
10723
  return Array.from(expandFormats(fileFormat)).map((ext) => `.${ext}`).join(",");
10694
10724
  }
10695
10725
  function useRichEditorFiles(options = {}) {
10696
- const { fileFormat, onFileUpload } = options;
10697
- const [attachments, setAttachments] = useState([]);
10726
+ const { fileFormat, onFileUpload, initialAttachments } = options;
10727
+ const [attachments, setAttachments] = useState(() => initialAttachments ?? []);
10698
10728
  const [isUploading, setIsUploading] = useState(false);
10699
10729
  const removeAttachment = useCallback((id) => {
10700
10730
  setAttachments((prev) => prev.filter((a) => a.id !== id));
10701
10731
  }, []);
10732
+ const hydrateAttachments = useCallback((loaded) => {
10733
+ setAttachments(loaded);
10734
+ }, []);
10702
10735
  const processFiles = useCallback(
10703
10736
  async (files, editor) => {
10704
10737
  const allowedExts = fileFormat ? expandFormats(fileFormat) : null;
@@ -10707,7 +10740,12 @@ function useRichEditorFiles(options = {}) {
10707
10740
  setIsUploading(true);
10708
10741
  try {
10709
10742
  for (const file of accepted) {
10710
- const url = await (onFileUpload ? onFileUpload(file) : URL.createObjectURL(file));
10743
+ let url;
10744
+ try {
10745
+ url = await (onFileUpload ? onFileUpload(file) : URL.createObjectURL(file));
10746
+ } catch {
10747
+ continue;
10748
+ }
10711
10749
  if (file.type.startsWith("image/")) {
10712
10750
  if (!editor) continue;
10713
10751
  editor.chain().focus().insertContent({ type: "image", attrs: { src: url } }).run();
@@ -10732,7 +10770,58 @@ function useRichEditorFiles(options = {}) {
10732
10770
  },
10733
10771
  [fileFormat, onFileUpload]
10734
10772
  );
10735
- return { attachments, processFiles, removeAttachment, isUploading };
10773
+ return { attachments, processFiles, removeAttachment, hydrateAttachments, isUploading };
10774
+ }
10775
+
10776
+ // src/components/RichEditor/config/attachmentSerialization.ts
10777
+ var ATTACHMENTS_CONTAINER_ATTR = "data-rich-editor-attachments";
10778
+ var ATTACHMENT_ITEM_ATTR = "data-rich-editor-attachment";
10779
+ function escapeHtmlAttr(value) {
10780
+ return value.replace(/&/g, "&amp;").replace(/"/g, "&quot;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
10781
+ }
10782
+ function escapeHtmlText(value) {
10783
+ return value.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
10784
+ }
10785
+ function serializeAttachmentsBlock(attachments) {
10786
+ if (attachments.length === 0) return "";
10787
+ const items = attachments.map((attachment) => {
10788
+ const href = escapeHtmlAttr(attachment.url);
10789
+ const name = escapeHtmlAttr(attachment.name);
10790
+ return `<a ${ATTACHMENT_ITEM_ATTR}="true" href="${href}" data-name="${name}" data-size="${attachment.size}" data-mime="${escapeHtmlAttr(attachment.mimeType)}">${escapeHtmlText(attachment.name)}</a>`;
10791
+ }).join("");
10792
+ return `<div ${ATTACHMENTS_CONTAINER_ATTR}="true">${items}</div>`;
10793
+ }
10794
+ function splitAttachmentsFromHtml(html) {
10795
+ if (!html || !html.includes(ATTACHMENTS_CONTAINER_ATTR) || typeof DOMParser === "undefined") {
10796
+ return { html, attachments: [] };
10797
+ }
10798
+ const doc = new DOMParser().parseFromString(html, "text/html");
10799
+ const container2 = doc.body.querySelector(`[${ATTACHMENTS_CONTAINER_ATTR}]`);
10800
+ if (!container2) {
10801
+ return { html, attachments: [] };
10802
+ }
10803
+ const attachments = Array.from(
10804
+ container2.querySelectorAll(`[${ATTACHMENT_ITEM_ATTR}]`)
10805
+ ).map((el, index) => {
10806
+ const url = el.getAttribute("href") ?? "";
10807
+ const name = el.getAttribute("data-name") ?? el.textContent ?? `file-${index}`;
10808
+ const mimeType = el.getAttribute("data-mime") ?? "";
10809
+ const size2 = Number(el.getAttribute("data-size") ?? "0") || 0;
10810
+ return {
10811
+ id: `loaded-${index}-${Math.random().toString(36).slice(2)}`,
10812
+ name,
10813
+ size: size2,
10814
+ mimeType,
10815
+ file: new File([], name, { type: mimeType }),
10816
+ url
10817
+ };
10818
+ });
10819
+ container2.remove();
10820
+ return { html: doc.body.innerHTML, attachments };
10821
+ }
10822
+ function composeRichEditorHtml(bodyHtml, attachments) {
10823
+ const block = serializeAttachmentsBlock(attachments);
10824
+ return block ? `${bodyHtml}${block}` : bodyHtml;
10736
10825
  }
10737
10826
 
10738
10827
  // src/components/RichEditor/hooks/useRichEditor.ts
@@ -10763,10 +10852,18 @@ function useRichEditor(options = {}) {
10763
10852
  onChangeRef.current = onChange;
10764
10853
  const onSubmitRef = useRef(onSubmit);
10765
10854
  onSubmitRef.current = onSubmit;
10766
- const { attachments, processFiles, removeAttachment, isUploading } = useRichEditorFiles({
10855
+ const initialSplitRef = useRef(null);
10856
+ if (initialSplitRef.current === null) {
10857
+ initialSplitRef.current = splitAttachmentsFromHtml(defaultValue ?? "");
10858
+ }
10859
+ const initialSplit = initialSplitRef.current;
10860
+ const { attachments, processFiles, removeAttachment, hydrateAttachments, isUploading } = useRichEditorFiles({
10767
10861
  fileFormat,
10768
- onFileUpload
10862
+ onFileUpload,
10863
+ initialAttachments: initialSplit.attachments
10769
10864
  });
10865
+ const attachmentsRef = useRef(attachments);
10866
+ attachmentsRef.current = attachments;
10770
10867
  const editorRef = useRef(null);
10771
10868
  const processFilesRef = useRef(() => {
10772
10869
  });
@@ -10800,21 +10897,27 @@ function useRichEditor(options = {}) {
10800
10897
  handleDrop: createDropHandler(processFilesRef)
10801
10898
  } : {}
10802
10899
  },
10803
- content: defaultValue ?? "",
10900
+ content: initialSplit.html,
10804
10901
  onUpdate: ({ editor: editor2 }) => {
10805
- onChangeRef.current?.(editor2.getHTML());
10902
+ onChangeRef.current?.(composeRichEditorHtml(editor2.getHTML(), attachmentsRef.current));
10806
10903
  }
10807
10904
  });
10808
10905
  editorRef.current = editor;
10906
+ useEffect(() => {
10907
+ if (!editor) return;
10908
+ onChangeRef.current?.(composeRichEditorHtml(editor.getHTML(), attachments));
10909
+ }, [attachments]);
10809
10910
  useEffect(() => {
10810
10911
  if (editor) editor.setEditable(editable && !isUploading);
10811
10912
  }, [editor, editable, isUploading]);
10812
10913
  return {
10813
10914
  editor,
10814
- getHTML: () => editor?.getHTML() ?? "",
10915
+ getHTML: () => composeRichEditorHtml(editor?.getHTML() ?? "", attachments),
10815
10916
  getJSON: () => editor?.getJSON() ?? {},
10816
10917
  setContent: (html) => {
10817
- editor?.commands.setContent(html);
10918
+ const split = splitAttachmentsFromHtml(html);
10919
+ editor?.commands.setContent(split.html);
10920
+ hydrateAttachments(split.attachments);
10818
10921
  },
10819
10922
  clearContent: () => {
10820
10923
  editor?.commands.clearContent();
@@ -10825,6 +10928,7 @@ function useRichEditor(options = {}) {
10825
10928
  isEmpty: editor?.isEmpty ?? true,
10826
10929
  attachments,
10827
10930
  removeAttachment,
10931
+ hydrateAttachments,
10828
10932
  processFiles: processFilesRef.current,
10829
10933
  isUploading
10830
10934
  };
@@ -11617,7 +11721,16 @@ var RichEditor = forwardRef(function RichEditor2({
11617
11721
  },
11618
11722
  [onChange]
11619
11723
  );
11620
- const { editor, attachments, removeAttachment, processFiles, isUploading } = useRichEditor({
11724
+ const {
11725
+ editor,
11726
+ attachments,
11727
+ removeAttachment,
11728
+ hydrateAttachments,
11729
+ processFiles,
11730
+ isUploading,
11731
+ getHTML,
11732
+ setContent
11733
+ } = useRichEditor({
11621
11734
  defaultValue: value ?? defaultValue,
11622
11735
  toolbar: activeToolbar,
11623
11736
  mentionUsers,
@@ -11644,18 +11757,18 @@ var RichEditor = forwardRef(function RichEditor2({
11644
11757
  isInternalChangeRef.current = false;
11645
11758
  return;
11646
11759
  }
11647
- if (editor.getHTML() !== value) {
11648
- editor.commands.setContent(value, { emitUpdate: false });
11760
+ if (getHTML() !== value) {
11761
+ const split = splitAttachmentsFromHtml(value);
11762
+ editor.commands.setContent(split.html, { emitUpdate: false });
11763
+ hydrateAttachments(split.attachments);
11649
11764
  }
11650
11765
  }, [value, editor]);
11651
11766
  useImperativeHandle(
11652
11767
  ref,
11653
11768
  () => ({
11654
- getHTML: () => editor?.getHTML() ?? "",
11769
+ getHTML,
11655
11770
  getJSON: () => editor?.getJSON() ?? {},
11656
- setContent: (html) => {
11657
- editor?.commands.setContent(html);
11658
- },
11771
+ setContent,
11659
11772
  clearContent: () => {
11660
11773
  editor?.commands.clearContent();
11661
11774
  },
@@ -11668,7 +11781,7 @@ var RichEditor = forwardRef(function RichEditor2({
11668
11781
  attachments,
11669
11782
  editor
11670
11783
  }),
11671
- [editor, attachments]
11784
+ [editor, attachments, getHTML, setContent]
11672
11785
  );
11673
11786
  const isToolbarVisible = showToolbar && editable;
11674
11787
  const imageFeatureEnabled = activeToolbar.image !== false;