@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.js CHANGED
@@ -7069,11 +7069,62 @@ var import_jsx_runtime76 = require("react/jsx-runtime");
7069
7069
  function FontFamilyDropdown() {
7070
7070
  const { editor } = (0, import_react85.useCurrentEditor)();
7071
7071
  const [open, setOpen] = (0, import_react86.useState)(false);
7072
+ const [selectedFont, setSelectedFont] = (0, import_react86.useState)(null);
7072
7073
  (0, import_react86.useEffect)(() => {
7073
7074
  loadGoogleFonts();
7074
7075
  }, []);
7076
+ (0, import_react86.useEffect)(() => {
7077
+ if (!editor || !selectedFont) return;
7078
+ const handleUpdate = () => {
7079
+ const currentFontFamily2 = editor.getAttributes("textStyle").fontFamily;
7080
+ if (!currentFontFamily2) {
7081
+ requestAnimationFrame(() => {
7082
+ editor.commands.setFontFamily(selectedFont);
7083
+ });
7084
+ }
7085
+ };
7086
+ const handleSelectionUpdate = () => {
7087
+ const currentFontFamily2 = editor.getAttributes("textStyle").fontFamily;
7088
+ if (!currentFontFamily2 && selectedFont) {
7089
+ requestAnimationFrame(() => {
7090
+ editor.commands.setFontFamily(selectedFont);
7091
+ });
7092
+ }
7093
+ };
7094
+ const handleTransaction = ({ transaction }) => {
7095
+ const isBlockChange = transaction.steps.some((step) => {
7096
+ return step.slice?.content?.content?.some(
7097
+ (node) => ["heading", "bulletList", "orderedList", "blockquote", "codeBlock"].includes(node.type?.name)
7098
+ );
7099
+ });
7100
+ if (isBlockChange && selectedFont) {
7101
+ requestAnimationFrame(() => {
7102
+ editor.commands.setFontFamily(selectedFont);
7103
+ });
7104
+ }
7105
+ };
7106
+ editor.on("update", handleUpdate);
7107
+ editor.on("selectionUpdate", handleSelectionUpdate);
7108
+ editor.on("transaction", handleTransaction);
7109
+ return () => {
7110
+ editor.off("update", handleUpdate);
7111
+ editor.off("selectionUpdate", handleSelectionUpdate);
7112
+ editor.off("transaction", handleTransaction);
7113
+ };
7114
+ }, [editor, selectedFont]);
7115
+ (0, import_react86.useEffect)(() => {
7116
+ if (!editor || !selectedFont) return;
7117
+ const { state } = editor;
7118
+ const { schema } = state;
7119
+ const textStyleMark = schema.marks.textStyle;
7120
+ if (textStyleMark) {
7121
+ const mark = textStyleMark.create({ fontFamily: selectedFont });
7122
+ const tr = state.tr.addStoredMark(mark);
7123
+ editor.view.dispatch(tr);
7124
+ }
7125
+ }, [editor, selectedFont]);
7075
7126
  if (!editor) return null;
7076
- const currentFontFamily = editor.getAttributes("textStyle").fontFamily;
7127
+ const currentFontFamily = editor.getAttributes("textStyle").fontFamily || selectedFont;
7077
7128
  const getCurrentFontLabel = () => {
7078
7129
  if (!currentFontFamily) return "Font Family";
7079
7130
  const matchedFont = FONT_OPTIONS.find(
@@ -7084,18 +7135,36 @@ function FontFamilyDropdown() {
7084
7135
  const currentFont = getCurrentFontLabel();
7085
7136
  const applyFont = (family) => {
7086
7137
  if (!editor) return;
7138
+ setSelectedFont(family);
7139
+ if (editor.state.storedMarks) {
7140
+ const textStyleMark2 = editor.schema.marks.textStyle;
7141
+ if (textStyleMark2) {
7142
+ editor.view.dispatch(editor.state.tr.removeStoredMark(textStyleMark2));
7143
+ }
7144
+ }
7145
+ editor.chain().focus().setFontFamily(family).run();
7146
+ const { state } = editor;
7147
+ const { schema } = state;
7148
+ const textStyleMark = schema.marks.textStyle;
7149
+ if (textStyleMark) {
7150
+ const mark = textStyleMark.create({ fontFamily: family });
7151
+ const tr = state.tr.addStoredMark(mark);
7152
+ editor.view.dispatch(tr);
7153
+ }
7154
+ };
7155
+ const resetFont = () => {
7156
+ if (!editor) return;
7157
+ setSelectedFont(null);
7087
7158
  if (editor.state.storedMarks) {
7088
7159
  const textStyleMark = editor.schema.marks.textStyle;
7089
7160
  if (textStyleMark) {
7090
- editor.view.dispatch(editor.state.tr.removeStoredMark(textStyleMark));
7161
+ editor.view.dispatch(
7162
+ editor.state.tr.removeStoredMark(textStyleMark)
7163
+ );
7091
7164
  }
7092
7165
  }
7093
- setTimeout(() => {
7094
- const success = editor.chain().focus().setFontFamily(family).run();
7095
- if (!success) {
7096
- console.warn("Font not applied:", family);
7097
- }
7098
- }, 0);
7166
+ editor.chain().focus().unsetFontFamily().run();
7167
+ setOpen(false);
7099
7168
  };
7100
7169
  return /* @__PURE__ */ (0, import_jsx_runtime76.jsxs)(Popover2, { open, onOpenChange: setOpen, children: [
7101
7170
  /* @__PURE__ */ (0, import_jsx_runtime76.jsx)(PopoverTrigger2, { asChild: true, children: /* @__PURE__ */ (0, import_jsx_runtime76.jsxs)(
@@ -7128,21 +7197,7 @@ function FontFamilyDropdown() {
7128
7197
  /* @__PURE__ */ (0, import_jsx_runtime76.jsx)(
7129
7198
  CommandItem,
7130
7199
  {
7131
- onSelect: () => {
7132
- if (!editor) return;
7133
- if (editor.state.storedMarks) {
7134
- const textStyleMark = editor.schema.marks.textStyle;
7135
- if (textStyleMark) {
7136
- editor.view.dispatch(
7137
- editor.state.tr.removeStoredMark(textStyleMark)
7138
- );
7139
- }
7140
- }
7141
- setTimeout(() => {
7142
- editor.chain().focus().unsetFontFamily().run();
7143
- setOpen(false);
7144
- }, 0);
7145
- },
7200
+ onSelect: resetFont,
7146
7201
  className: "font-sans",
7147
7202
  children: "Default"
7148
7203
  },