@tarviks/lexical-rich-editor 1.0.9 → 1.0.10

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
@@ -5145,11 +5145,18 @@ function useDrag(onMove, onEnd, interactingRef) {
5145
5145
  );
5146
5146
  return start;
5147
5147
  }
5148
- var ColorPickerControl = ({ value, title, disabled, onChange, icon }) => {
5148
+ var ColorPickerControl = ({ value, title, disabled, onChange, icon, onOpenChange }) => {
5149
5149
  const [open, setOpen] = React9__namespace.useState(false);
5150
5150
  const btnRef = React9__namespace.useRef(null);
5151
+ const setOpenAndNotify = React9__namespace.useCallback(
5152
+ (next) => {
5153
+ setOpen(next);
5154
+ onOpenChange?.(next);
5155
+ },
5156
+ [onOpenChange]
5157
+ );
5151
5158
  const interactingRef = React9__namespace.useRef(false);
5152
- const handleDismiss = React9__namespace.useCallback(() => setOpen(false), []);
5159
+ const handleDismiss = React9__namespace.useCallback(() => setOpenAndNotify(false), [setOpenAndNotify]);
5153
5160
  const preventDismissOnEvent = React9__namespace.useCallback(
5154
5161
  (ev) => {
5155
5162
  if (interactingRef.current) return true;
@@ -5212,9 +5219,9 @@ var ColorPickerControl = ({ value, title, disabled, onChange, icon }) => {
5212
5219
  setHex(nextHex);
5213
5220
  console.log("[AO-ColorPicker]", title, "commitHsv -> onChange", { nextHex, close: !!close });
5214
5221
  onChange(nextHex);
5215
- if (close) setOpen(false);
5222
+ if (close) setOpenAndNotify(false);
5216
5223
  },
5217
- [onChange, title]
5224
+ [onChange, title, setOpenAndNotify]
5218
5225
  );
5219
5226
  const svRef = React9__namespace.useRef(null);
5220
5227
  const onSVMove = React9__namespace.useCallback(
@@ -5273,7 +5280,7 @@ var ColorPickerControl = ({ value, title, disabled, onChange, icon }) => {
5273
5280
  wasOpen: open,
5274
5281
  activeElementBeforeToggle: document.activeElement?.tagName
5275
5282
  });
5276
- setOpen((p) => !p);
5283
+ setOpenAndNotify(!open);
5277
5284
  }
5278
5285
  }
5279
5286
  ),
@@ -5368,7 +5375,7 @@ var ColorPickerControl = ({ value, title, disabled, onChange, icon }) => {
5368
5375
  }
5369
5376
  }
5370
5377
  ),
5371
- /* @__PURE__ */ jsxRuntime.jsx(react.DefaultButton, { type: "button", text: "Close", onClick: () => setOpen(false) })
5378
+ /* @__PURE__ */ jsxRuntime.jsx(react.DefaultButton, { type: "button", text: "Close", onClick: () => setOpenAndNotify(false) })
5372
5379
  ] })
5373
5380
  ] })
5374
5381
  }
@@ -5413,53 +5420,68 @@ var ColorPickerPlugin = ({ disabled }) => {
5413
5420
  )
5414
5421
  );
5415
5422
  }, [editor]);
5423
+ const wasEditorActiveRef = React9__namespace.default.useRef(false);
5424
+ const handleOpenChange = (open) => {
5425
+ if (open) {
5426
+ const root = editor.getRootElement();
5427
+ wasEditorActiveRef.current = !!root && (document.activeElement === root || root.contains(document.activeElement));
5428
+ } else if (wasEditorActiveRef.current) {
5429
+ editor.focus();
5430
+ }
5431
+ };
5416
5432
  const applyStyle = (args) => {
5417
5433
  if (disabled) return;
5418
- const root = editor.getRootElement();
5419
- const editorIsActive = !!lastRangeSelectionRef.current && !!root && (document.activeElement === root || root.contains(document.activeElement));
5420
5434
  console.log("[AO-ColorPicker] applyStyle called", {
5421
5435
  property: args.property,
5422
5436
  color: args.color,
5423
5437
  hasSavedSelection: !!lastRangeSelectionRef.current,
5424
5438
  savedSelectionIsCollapsed: lastRangeSelectionRef.current?.isCollapsed() ?? null,
5425
- editorIsActive,
5439
+ wasEditorActiveAtOpen: wasEditorActiveRef.current,
5426
5440
  activeElementTag: document.activeElement?.tagName,
5427
- activeElementClass: document.activeElement?.className,
5428
- rootIsActiveElement: document.activeElement === root,
5429
- rootContainsActiveElement: !!root && root.contains(document.activeElement)
5441
+ activeElementClass: document.activeElement?.className
5430
5442
  });
5431
- if (editorIsActive) editor.focus();
5432
- editor.update(() => {
5433
- const saved = lastRangeSelectionRef.current;
5434
- if (saved) {
5435
- lexical.$setSelection(saved.clone());
5436
- }
5437
- const selection$1 = lexical.$getSelection();
5438
- const isRange = lexical.$isRangeSelection(selection$1);
5439
- console.log("[AO-ColorPicker] applyStyle inside editor.update", {
5440
- hadSavedSelection: !!saved,
5441
- selectionAfterRestoreIsRange: isRange,
5442
- selectionAfterRestoreIsCollapsed: isRange ? selection$1.isCollapsed() : null
5443
- });
5444
- if (isRange) {
5445
- selection.$patchStyleText(selection$1, { [args.property]: args.color });
5446
- const verify = selection.$getSelectionStyleValueForProperty(
5447
- selection$1,
5448
- args.property,
5449
- "<none>"
5450
- );
5451
- console.log("[AO-ColorPicker] applyStyle after patchStyleText, readback in same update", {
5452
- property: args.property,
5453
- appliedColor: args.color,
5454
- readBack: verify
5443
+ editor.update(
5444
+ () => {
5445
+ const saved = lastRangeSelectionRef.current;
5446
+ if (saved) {
5447
+ lexical.$setSelection(saved.clone());
5448
+ }
5449
+ const selection$1 = lexical.$getSelection();
5450
+ const isRange = lexical.$isRangeSelection(selection$1);
5451
+ console.log("[AO-ColorPicker] applyStyle inside editor.update", {
5452
+ hadSavedSelection: !!saved,
5453
+ selectionAfterRestoreIsRange: isRange,
5454
+ selectionAfterRestoreIsCollapsed: isRange ? selection$1.isCollapsed() : null
5455
5455
  });
5456
- } else {
5457
- console.warn(
5458
- "[AO-ColorPicker] applyStyle: no range selection available \u2014 style was NOT applied",
5459
- { property: args.property, color: args.color }
5460
- );
5461
- }
5462
- });
5456
+ if (isRange) {
5457
+ selection.$patchStyleText(selection$1, { [args.property]: args.color });
5458
+ const verify = selection.$getSelectionStyleValueForProperty(
5459
+ selection$1,
5460
+ args.property,
5461
+ "<none>"
5462
+ );
5463
+ console.log("[AO-ColorPicker] applyStyle after patchStyleText, readback in same update", {
5464
+ property: args.property,
5465
+ appliedColor: args.color,
5466
+ readBack: verify
5467
+ });
5468
+ } else {
5469
+ console.warn(
5470
+ "[AO-ColorPicker] applyStyle: no range selection available \u2014 style was NOT applied",
5471
+ { property: args.property, color: args.color }
5472
+ );
5473
+ }
5474
+ },
5475
+ // Without this tag, Lexical's reconciler force-focuses the editor root
5476
+ // whenever this update's selection diff finds the root isn't already
5477
+ // focused (see Lexical.dev.mjs ~8112) — which it never is while the
5478
+ // color picker's Callout legitimately holds focus. That forced focus
5479
+ // then fights Fluent's Callout for focus on every single drag-driven
5480
+ // commit, repeatedly bouncing focus (and, via FocusEventsPlugin,
5481
+ // nulling and restoring the selection) between the editor and the
5482
+ // popover until the drag's tracked color desynced from the cursor.
5483
+ { tag: lexical.SKIP_SELECTION_FOCUS_TAG }
5484
+ );
5463
5485
  };
5464
5486
  const containerStyle = disabled ? {
5465
5487
  display: "flex",
@@ -5483,7 +5505,8 @@ var ColorPickerPlugin = ({ disabled }) => {
5483
5505
  title: "Text color",
5484
5506
  disabled,
5485
5507
  icon: /* @__PURE__ */ jsxRuntime.jsx(reactIcons.TextColorRegular, { style: { color: iconColor } }),
5486
- onChange: (c) => applyStyle({ property: "color", color: c })
5508
+ onChange: (c) => applyStyle({ property: "color", color: c }),
5509
+ onOpenChange: handleOpenChange
5487
5510
  }
5488
5511
  ),
5489
5512
  /* @__PURE__ */ jsxRuntime.jsx(
@@ -5493,7 +5516,8 @@ var ColorPickerPlugin = ({ disabled }) => {
5493
5516
  title: "Background color",
5494
5517
  disabled,
5495
5518
  icon: /* @__PURE__ */ jsxRuntime.jsx(reactIcons.PaintBucket16Filled, { style: { color: iconColor } }),
5496
- onChange: (c) => applyStyle({ property: "background-color", color: c })
5519
+ onChange: (c) => applyStyle({ property: "background-color", color: c }),
5520
+ onOpenChange: handleOpenChange
5497
5521
  }
5498
5522
  )
5499
5523
  ] });