@mhamz.01/easyflow-texteditor 0.1.89 → 0.1.91

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
@@ -7043,11 +7043,62 @@ import { jsx as jsx76, jsxs as jsxs45 } from "react/jsx-runtime";
7043
7043
  function FontFamilyDropdown() {
7044
7044
  const { editor } = useCurrentEditor3();
7045
7045
  const [open, setOpen] = useState28(false);
7046
+ const [selectedFont, setSelectedFont] = useState28(null);
7046
7047
  useEffect18(() => {
7047
7048
  loadGoogleFonts();
7048
7049
  }, []);
7050
+ useEffect18(() => {
7051
+ if (!editor || !selectedFont) return;
7052
+ const handleUpdate = () => {
7053
+ const currentFontFamily2 = editor.getAttributes("textStyle").fontFamily;
7054
+ if (!currentFontFamily2) {
7055
+ requestAnimationFrame(() => {
7056
+ editor.commands.setFontFamily(selectedFont);
7057
+ });
7058
+ }
7059
+ };
7060
+ const handleSelectionUpdate = () => {
7061
+ const currentFontFamily2 = editor.getAttributes("textStyle").fontFamily;
7062
+ if (!currentFontFamily2 && selectedFont) {
7063
+ requestAnimationFrame(() => {
7064
+ editor.commands.setFontFamily(selectedFont);
7065
+ });
7066
+ }
7067
+ };
7068
+ const handleTransaction = ({ transaction }) => {
7069
+ const isBlockChange = transaction.steps.some((step) => {
7070
+ return step.slice?.content?.content?.some(
7071
+ (node) => ["heading", "bulletList", "orderedList", "blockquote", "codeBlock"].includes(node.type?.name)
7072
+ );
7073
+ });
7074
+ if (isBlockChange && selectedFont) {
7075
+ requestAnimationFrame(() => {
7076
+ editor.commands.setFontFamily(selectedFont);
7077
+ });
7078
+ }
7079
+ };
7080
+ editor.on("update", handleUpdate);
7081
+ editor.on("selectionUpdate", handleSelectionUpdate);
7082
+ editor.on("transaction", handleTransaction);
7083
+ return () => {
7084
+ editor.off("update", handleUpdate);
7085
+ editor.off("selectionUpdate", handleSelectionUpdate);
7086
+ editor.off("transaction", handleTransaction);
7087
+ };
7088
+ }, [editor, selectedFont]);
7089
+ useEffect18(() => {
7090
+ if (!editor || !selectedFont) return;
7091
+ const { state } = editor;
7092
+ const { schema } = state;
7093
+ const textStyleMark = schema.marks.textStyle;
7094
+ if (textStyleMark) {
7095
+ const mark = textStyleMark.create({ fontFamily: selectedFont });
7096
+ const tr = state.tr.addStoredMark(mark);
7097
+ editor.view.dispatch(tr);
7098
+ }
7099
+ }, [editor, selectedFont]);
7049
7100
  if (!editor) return null;
7050
- const currentFontFamily = editor.getAttributes("textStyle").fontFamily;
7101
+ const currentFontFamily = editor.getAttributes("textStyle").fontFamily || selectedFont;
7051
7102
  const getCurrentFontLabel = () => {
7052
7103
  if (!currentFontFamily) return "Font Family";
7053
7104
  const matchedFont = FONT_OPTIONS.find(
@@ -7058,18 +7109,36 @@ function FontFamilyDropdown() {
7058
7109
  const currentFont = getCurrentFontLabel();
7059
7110
  const applyFont = (family) => {
7060
7111
  if (!editor) return;
7112
+ setSelectedFont(family);
7113
+ if (editor.state.storedMarks) {
7114
+ const textStyleMark2 = editor.schema.marks.textStyle;
7115
+ if (textStyleMark2) {
7116
+ editor.view.dispatch(editor.state.tr.removeStoredMark(textStyleMark2));
7117
+ }
7118
+ }
7119
+ editor.chain().focus().setFontFamily(family).run();
7120
+ const { state } = editor;
7121
+ const { schema } = state;
7122
+ const textStyleMark = schema.marks.textStyle;
7123
+ if (textStyleMark) {
7124
+ const mark = textStyleMark.create({ fontFamily: family });
7125
+ const tr = state.tr.addStoredMark(mark);
7126
+ editor.view.dispatch(tr);
7127
+ }
7128
+ };
7129
+ const resetFont = () => {
7130
+ if (!editor) return;
7131
+ setSelectedFont(null);
7061
7132
  if (editor.state.storedMarks) {
7062
7133
  const textStyleMark = editor.schema.marks.textStyle;
7063
7134
  if (textStyleMark) {
7064
- editor.view.dispatch(editor.state.tr.removeStoredMark(textStyleMark));
7135
+ editor.view.dispatch(
7136
+ editor.state.tr.removeStoredMark(textStyleMark)
7137
+ );
7065
7138
  }
7066
7139
  }
7067
- setTimeout(() => {
7068
- const success = editor.chain().focus().setFontFamily(family).run();
7069
- if (!success) {
7070
- console.warn("Font not applied:", family);
7071
- }
7072
- }, 0);
7140
+ editor.chain().focus().unsetFontFamily().run();
7141
+ setOpen(false);
7073
7142
  };
7074
7143
  return /* @__PURE__ */ jsxs45(Popover2, { open, onOpenChange: setOpen, children: [
7075
7144
  /* @__PURE__ */ jsx76(PopoverTrigger2, { asChild: true, children: /* @__PURE__ */ jsxs45(
@@ -7102,21 +7171,7 @@ function FontFamilyDropdown() {
7102
7171
  /* @__PURE__ */ jsx76(
7103
7172
  CommandItem,
7104
7173
  {
7105
- onSelect: () => {
7106
- if (!editor) return;
7107
- if (editor.state.storedMarks) {
7108
- const textStyleMark = editor.schema.marks.textStyle;
7109
- if (textStyleMark) {
7110
- editor.view.dispatch(
7111
- editor.state.tr.removeStoredMark(textStyleMark)
7112
- );
7113
- }
7114
- }
7115
- setTimeout(() => {
7116
- editor.chain().focus().unsetFontFamily().run();
7117
- setOpen(false);
7118
- }, 0);
7119
- },
7174
+ onSelect: resetFont,
7120
7175
  className: "font-sans",
7121
7176
  children: "Default"
7122
7177
  },