@mhamz.01/easyflow-texteditor 0.1.90 → 0.1.92

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
@@ -7070,37 +7070,49 @@ function FontFamilyDropdown() {
7070
7070
  const { editor } = (0, import_react85.useCurrentEditor)();
7071
7071
  const [open, setOpen] = (0, import_react86.useState)(false);
7072
7072
  const [selectedFont, setSelectedFont] = (0, import_react86.useState)(null);
7073
+ const isApplyingFont = (0, import_react86.useRef)(false);
7073
7074
  (0, import_react86.useEffect)(() => {
7074
7075
  loadGoogleFonts();
7075
7076
  }, []);
7076
7077
  (0, import_react86.useEffect)(() => {
7077
- if (!editor) return;
7078
- const updateSelectedFont = () => {
7079
- const currentFontFamily2 = editor.getAttributes("textStyle").fontFamily;
7080
- if (currentFontFamily2) {
7081
- setSelectedFont(currentFontFamily2);
7078
+ if (!editor || !selectedFont) return;
7079
+ const handleTransaction = ({ transaction }) => {
7080
+ if (isApplyingFont.current) return;
7081
+ const hasTextChanges = transaction.steps.some(
7082
+ (step) => step.slice || step.from !== void 0
7083
+ );
7084
+ if (!hasTextChanges) return;
7085
+ const { $from } = transaction.selection;
7086
+ const marks = $from.marks();
7087
+ const hasFontFamily = marks.some(
7088
+ (mark) => mark.type.name === "textStyle" && mark.attrs.fontFamily
7089
+ );
7090
+ if (!hasFontFamily) {
7091
+ const { state } = editor;
7092
+ const textStyleMark = state.schema.marks.textStyle;
7093
+ if (textStyleMark) {
7094
+ isApplyingFont.current = true;
7095
+ const mark = textStyleMark.create({ fontFamily: selectedFont });
7096
+ state.storedMarks = [mark];
7097
+ setTimeout(() => {
7098
+ isApplyingFont.current = false;
7099
+ }, 50);
7100
+ }
7082
7101
  }
7083
7102
  };
7084
- editor.on("selectionUpdate", updateSelectedFont);
7085
- editor.on("transaction", updateSelectedFont);
7103
+ editor.on("transaction", handleTransaction);
7086
7104
  return () => {
7087
- editor.off("selectionUpdate", updateSelectedFont);
7088
- editor.off("transaction", updateSelectedFont);
7105
+ editor.off("transaction", handleTransaction);
7089
7106
  };
7090
- }, [editor]);
7107
+ }, [editor, selectedFont]);
7091
7108
  (0, import_react86.useEffect)(() => {
7092
7109
  if (!editor || !selectedFont) return;
7093
- const handleUpdate = () => {
7094
- const { from, to } = editor.state.selection;
7095
- const currentFontFamily2 = editor.getAttributes("textStyle").fontFamily;
7096
- if (!currentFontFamily2 && selectedFont) {
7097
- editor.chain().focus().setFontFamily(selectedFont).run();
7098
- }
7099
- };
7100
- editor.on("update", handleUpdate);
7101
- return () => {
7102
- editor.off("update", handleUpdate);
7103
- };
7110
+ const { state } = editor;
7111
+ const textStyleMark = state.schema.marks.textStyle;
7112
+ if (textStyleMark) {
7113
+ const mark = textStyleMark.create({ fontFamily: selectedFont });
7114
+ state.storedMarks = [mark];
7115
+ }
7104
7116
  }, [editor, selectedFont]);
7105
7117
  if (!editor) return null;
7106
7118
  const currentFontFamily = editor.getAttributes("textStyle").fontFamily || selectedFont;
@@ -7114,35 +7126,45 @@ function FontFamilyDropdown() {
7114
7126
  const currentFont = getCurrentFontLabel();
7115
7127
  const applyFont = (family) => {
7116
7128
  if (!editor) return;
7129
+ isApplyingFont.current = true;
7117
7130
  setSelectedFont(family);
7118
- if (editor.state.storedMarks) {
7119
- const textStyleMark = editor.schema.marks.textStyle;
7120
- if (textStyleMark) {
7121
- editor.view.dispatch(editor.state.tr.removeStoredMark(textStyleMark));
7122
- }
7131
+ const { state, view } = editor;
7132
+ const { from, to } = state.selection;
7133
+ const textStyleMark = state.schema.marks.textStyle;
7134
+ if (!textStyleMark) {
7135
+ isApplyingFont.current = false;
7136
+ return;
7123
7137
  }
7138
+ const mark = textStyleMark.create({ fontFamily: family });
7139
+ if (from !== to) {
7140
+ const tr = state.tr.addMark(from, to, mark);
7141
+ view.dispatch(tr);
7142
+ }
7143
+ state.storedMarks = [mark];
7144
+ editor.commands.focus(void 0, { scrollIntoView: false });
7124
7145
  setTimeout(() => {
7125
- const success = editor.chain().focus().setFontFamily(family).run();
7126
- if (!success) {
7127
- console.warn("Font not applied:", family);
7128
- }
7129
- }, 0);
7146
+ isApplyingFont.current = false;
7147
+ }, 50);
7130
7148
  };
7131
7149
  const resetFont = () => {
7132
7150
  if (!editor) return;
7151
+ isApplyingFont.current = true;
7133
7152
  setSelectedFont(null);
7134
- if (editor.state.storedMarks) {
7135
- const textStyleMark = editor.schema.marks.textStyle;
7153
+ const { state, view } = editor;
7154
+ const { from, to } = state.selection;
7155
+ if (from !== to) {
7156
+ const textStyleMark = state.schema.marks.textStyle;
7136
7157
  if (textStyleMark) {
7137
- editor.view.dispatch(
7138
- editor.state.tr.removeStoredMark(textStyleMark)
7139
- );
7158
+ const tr = state.tr.removeMark(from, to, textStyleMark);
7159
+ view.dispatch(tr);
7140
7160
  }
7141
7161
  }
7162
+ state.storedMarks = null;
7163
+ editor.commands.focus(void 0, { scrollIntoView: false });
7164
+ setOpen(false);
7142
7165
  setTimeout(() => {
7143
- editor.chain().focus().unsetFontFamily().run();
7144
- setOpen(false);
7145
- }, 0);
7166
+ isApplyingFont.current = false;
7167
+ }, 50);
7146
7168
  };
7147
7169
  return /* @__PURE__ */ (0, import_jsx_runtime76.jsxs)(Popover2, { open, onOpenChange: setOpen, children: [
7148
7170
  /* @__PURE__ */ (0, import_jsx_runtime76.jsx)(PopoverTrigger2, { asChild: true, children: /* @__PURE__ */ (0, import_jsx_runtime76.jsxs)(