@tarviks/lexical-rich-editor 1.0.11 → 1.1.0

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
@@ -5112,40 +5112,6 @@ var hsvToRgb = (h, s, v) => {
5112
5112
  b: Math.round((bb + m) * 255)
5113
5113
  };
5114
5114
  };
5115
- function useDrag(onMove, onEnd, interactingRef) {
5116
- const draggingRef = React9__namespace.useRef(false);
5117
- const start = React9__namespace.useCallback(
5118
- (e) => {
5119
- draggingRef.current = true;
5120
- if (interactingRef) interactingRef.current = true;
5121
- onMove(e.clientX, e.clientY);
5122
- const move = (ev) => {
5123
- if (!draggingRef.current) return;
5124
- onMove(ev.clientX, ev.clientY);
5125
- };
5126
- const up = () => {
5127
- draggingRef.current = false;
5128
- window.removeEventListener("mousemove", move);
5129
- window.removeEventListener("mouseup", up);
5130
- if (interactingRef) {
5131
- const clearFlag = () => {
5132
- interactingRef.current = false;
5133
- };
5134
- window.addEventListener("click", clearFlag, { once: true });
5135
- setTimeout(() => {
5136
- window.removeEventListener("click", clearFlag);
5137
- interactingRef.current = false;
5138
- }, 0);
5139
- }
5140
- onEnd?.();
5141
- };
5142
- window.addEventListener("mousemove", move);
5143
- window.addEventListener("mouseup", up);
5144
- },
5145
- [onMove, onEnd, interactingRef]
5146
- );
5147
- return start;
5148
- }
5149
5115
  var ColorPickerControl = ({ value, title, disabled, onChange, icon, onOpenChange }) => {
5150
5116
  const [open, setOpen] = React9__namespace.useState(false);
5151
5117
  const btnRef = React9__namespace.useRef(null);
@@ -5156,13 +5122,9 @@ var ColorPickerControl = ({ value, title, disabled, onChange, icon, onOpenChange
5156
5122
  },
5157
5123
  [onOpenChange]
5158
5124
  );
5159
- const interactingRef = React9__namespace.useRef(false);
5160
5125
  const handleDismiss = React9__namespace.useCallback(() => setOpenAndNotify(false), [setOpenAndNotify]);
5161
5126
  const preventDismissOnEvent = React9__namespace.useCallback(
5162
- (ev) => {
5163
- if (interactingRef.current) return true;
5164
- return ev.type !== "click";
5165
- },
5127
+ (ev) => ev.type !== "click",
5166
5128
  []
5167
5129
  );
5168
5130
  const [, forceReposition] = React9__namespace.useState(0);
@@ -5184,100 +5146,63 @@ var ColorPickerControl = ({ value, title, disabled, onChange, icon, onOpenChange
5184
5146
  window.removeEventListener("resize", reposition);
5185
5147
  };
5186
5148
  }, [open]);
5187
- const [hex, setHex] = React9__namespace.useState(normalizeHex(value || "#000000"));
5149
+ const appliedHex = React9__namespace.useMemo(() => normalizeHex(value || "#000000"), [value]);
5150
+ const [hex, setHex] = React9__namespace.useState(appliedHex);
5188
5151
  const { r, g, b } = React9__namespace.useMemo(() => hexToRgb(hex), [hex]);
5189
5152
  const hsv = React9__namespace.useMemo(() => rgbToHsv(r, g, b), [r, g, b]);
5190
5153
  const [h, setH] = React9__namespace.useState(hsv.h);
5191
5154
  const [s, setS] = React9__namespace.useState(hsv.s);
5192
5155
  const [v, setV] = React9__namespace.useState(hsv.v);
5193
- const wasOpenRef = React9__namespace.useRef(open);
5194
- console.log("[AO-ColorPicker]", title, "render with incoming value prop:", JSON.stringify(value));
5195
- React9__namespace.useEffect(() => {
5196
- const justOpened = open && !wasOpenRef.current;
5197
- wasOpenRef.current = open;
5198
- console.log("[AO-ColorPicker]", title, "open-seed effect", {
5199
- open,
5200
- justOpened,
5201
- incomingValue: value
5202
- });
5203
- if (!justOpened) return;
5204
- const n = normalizeHex(value || "#000000");
5205
- console.log("[AO-ColorPicker]", title, "seeding local state from value on open", {
5206
- incomingValue: value,
5207
- normalized: n
5208
- });
5209
- setHex(n);
5210
- const rgb = hexToRgb(n);
5156
+ const setDraft = React9__namespace.useCallback((nextHex) => {
5157
+ setHex(nextHex);
5158
+ const rgb = hexToRgb(nextHex);
5211
5159
  const next = rgbToHsv(rgb.r, rgb.g, rgb.b);
5212
5160
  setH(next.h);
5213
5161
  setS(next.s);
5214
5162
  setV(next.v);
5215
- }, [value, open]);
5216
- const updateHexFromHsv = React9__namespace.useCallback((hh, ss, vv) => {
5163
+ }, []);
5164
+ const setDraftFromHsv = React9__namespace.useCallback((hh, ss, vv) => {
5217
5165
  const rgb = hsvToRgb(hh, ss, vv);
5218
- const nextHex = rgbToHex(rgb.r, rgb.g, rgb.b);
5219
- setHex(nextHex);
5220
- return nextHex;
5166
+ setHex(rgbToHex(rgb.r, rgb.g, rgb.b));
5167
+ setH(hh);
5168
+ setS(ss);
5169
+ setV(vv);
5221
5170
  }, []);
5222
- const commitHsv = React9__namespace.useCallback(
5223
- (hh, ss, vv, close) => {
5224
- const nextHex = updateHexFromHsv(hh, ss, vv);
5225
- console.log("[AO-ColorPicker]", title, "commitHsv -> onChange", { nextHex, close: !!close });
5226
- onChange(nextHex);
5227
- if (close) setOpenAndNotify(false);
5228
- },
5229
- [onChange, title, setOpenAndNotify, updateHexFromHsv]
5230
- );
5231
- const hRef = React9__namespace.useRef(h);
5232
- const sRef = React9__namespace.useRef(s);
5233
- const vRef = React9__namespace.useRef(v);
5234
- React9__namespace.useEffect(() => {
5235
- hRef.current = h;
5236
- }, [h]);
5237
- React9__namespace.useEffect(() => {
5238
- sRef.current = s;
5239
- }, [s]);
5171
+ const wasOpenRef = React9__namespace.useRef(open);
5240
5172
  React9__namespace.useEffect(() => {
5241
- vRef.current = v;
5242
- }, [v]);
5173
+ const justOpened = open && !wasOpenRef.current;
5174
+ wasOpenRef.current = open;
5175
+ if (!justOpened) return;
5176
+ setDraft(appliedHex);
5177
+ }, [appliedHex, open, setDraft]);
5243
5178
  const svRef = React9__namespace.useRef(null);
5244
- const onSVMove = React9__namespace.useCallback(
5245
- (clientX, clientY) => {
5179
+ const handleSVClick = React9__namespace.useCallback(
5180
+ (e) => {
5246
5181
  if (!svRef.current) return;
5247
5182
  const rect = svRef.current.getBoundingClientRect();
5248
- const x = clamp3(clientX - rect.left, 0, rect.width);
5249
- const y = clamp3(clientY - rect.top, 0, rect.height);
5183
+ const x = clamp3(e.clientX - rect.left, 0, rect.width);
5184
+ const y = clamp3(e.clientY - rect.top, 0, rect.height);
5250
5185
  const ss = rect.width === 0 ? 0 : x / rect.width;
5251
5186
  const vv = rect.height === 0 ? 0 : 1 - y / rect.height;
5252
- setS(ss);
5253
- setV(vv);
5254
- sRef.current = ss;
5255
- vRef.current = vv;
5256
- updateHexFromHsv(hRef.current, ss, vv);
5187
+ setDraftFromHsv(h, ss, vv);
5257
5188
  },
5258
- [updateHexFromHsv]
5189
+ [h, setDraftFromHsv]
5259
5190
  );
5260
- const commitSV = React9__namespace.useCallback(() => {
5261
- commitHsv(hRef.current, sRef.current, vRef.current);
5262
- }, [commitHsv]);
5263
- const startSV = useDrag(onSVMove, commitSV, interactingRef);
5264
5191
  const hueRef = React9__namespace.useRef(null);
5265
- const onHueMove = React9__namespace.useCallback(
5266
- (clientX) => {
5192
+ const handleHueClick = React9__namespace.useCallback(
5193
+ (e) => {
5267
5194
  if (!hueRef.current) return;
5268
5195
  const rect = hueRef.current.getBoundingClientRect();
5269
- const x = clamp3(clientX - rect.left, 0, rect.width);
5196
+ const x = clamp3(e.clientX - rect.left, 0, rect.width);
5270
5197
  const hh = rect.width === 0 ? 0 : x / rect.width * 360;
5271
- setH(hh);
5272
- hRef.current = hh;
5273
- updateHexFromHsv(hh, sRef.current, vRef.current);
5198
+ setDraftFromHsv(hh, s, v);
5274
5199
  },
5275
- [updateHexFromHsv]
5200
+ [s, v, setDraftFromHsv]
5276
5201
  );
5277
- const commitHue = React9__namespace.useCallback(() => {
5278
- commitHsv(hRef.current, sRef.current, vRef.current);
5279
- }, [commitHsv]);
5280
- const startHue = useDrag((x) => onHueMove(x), commitHue, interactingRef);
5202
+ const handleApply = React9__namespace.useCallback(() => {
5203
+ onChange(hex);
5204
+ setOpenAndNotify(false);
5205
+ }, [onChange, hex, setOpenAndNotify]);
5281
5206
  const svThumb = React9__namespace.useMemo(() => ({ left: `${s * 100}%`, top: `${(1 - v) * 100}%` }), [s, v]);
5282
5207
  const hueThumb = React9__namespace.useMemo(() => ({ left: `${h / 360 * 100}%` }), [h]);
5283
5208
  const hueColor = React9__namespace.useMemo(() => {
@@ -5302,10 +5227,6 @@ var ColorPickerControl = ({ value, title, disabled, onChange, icon, onOpenChange
5302
5227
  },
5303
5228
  onClick: () => {
5304
5229
  if (disabled) return;
5305
- console.log("[AO-ColorPicker]", title, "trigger button clicked", {
5306
- wasOpen: open,
5307
- activeElementBeforeToggle: document.activeElement?.tagName
5308
- });
5309
5230
  setOpenAndNotify(!open);
5310
5231
  }
5311
5232
  }
@@ -5320,7 +5241,7 @@ var ColorPickerControl = ({ value, title, disabled, onChange, icon, onOpenChange
5320
5241
  transform: "translateX(-50%)",
5321
5242
  width: 14,
5322
5243
  height: 3,
5323
- background: hex,
5244
+ background: appliedHex,
5324
5245
  borderRadius: 1,
5325
5246
  border: "0.5px solid rgba(0,0,0,0.18)",
5326
5247
  pointerEvents: "none"
@@ -5348,17 +5269,7 @@ var ColorPickerControl = ({ value, title, disabled, onChange, icon, onOpenChange
5348
5269
  {
5349
5270
  value: hex,
5350
5271
  onChange: (_, val) => setHex(normalizeHex(val || "")),
5351
- onBlur: () => {
5352
- const n = normalizeHex(hex);
5353
- setHex(n);
5354
- const rgb = hexToRgb(n);
5355
- const next = rgbToHsv(rgb.r, rgb.g, rgb.b);
5356
- setH(next.h);
5357
- setS(next.s);
5358
- setV(next.v);
5359
- console.log("[AO-ColorPicker]", title, "hex field blur -> onChange", { raw: hex, normalized: n });
5360
- onChange(n);
5361
- }
5272
+ onBlur: () => setDraft(normalizeHex(hex))
5362
5273
  }
5363
5274
  )
5364
5275
  ] }),
@@ -5368,39 +5279,21 @@ var ColorPickerControl = ({ value, title, disabled, onChange, icon, onOpenChange
5368
5279
  type: "button",
5369
5280
  className: "aoLexSwatchBtn",
5370
5281
  style: { background: c },
5371
- onClick: () => {
5372
- setHex(c);
5373
- const rgb = hexToRgb(c);
5374
- const next = rgbToHsv(rgb.r, rgb.g, rgb.b);
5375
- setH(next.h);
5376
- setS(next.s);
5377
- setV(next.v);
5378
- console.log("[AO-ColorPicker]", title, "preset swatch click -> onChange", { color: c });
5379
- onChange(c);
5380
- },
5282
+ onClick: () => setDraft(c),
5381
5283
  title: c
5382
5284
  },
5383
5285
  c
5384
5286
  )) }),
5385
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "aoLexSV", ref: svRef, onMouseDown: startSV, children: [
5287
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "aoLexSV", ref: svRef, onClick: handleSVClick, children: [
5386
5288
  /* @__PURE__ */ jsxRuntime.jsx("div", { className: "aoLexSVHue", style: { background: hueColor } }),
5387
5289
  /* @__PURE__ */ jsxRuntime.jsx("div", { className: "aoLexSVWhite" }),
5388
5290
  /* @__PURE__ */ jsxRuntime.jsx("div", { className: "aoLexSVBlack" }),
5389
5291
  /* @__PURE__ */ jsxRuntime.jsx("div", { className: "aoLexSVThumb", style: svThumb })
5390
5292
  ] }),
5391
- /* @__PURE__ */ jsxRuntime.jsx("div", { className: "aoLexHue", ref: hueRef, onMouseDown: startHue, children: /* @__PURE__ */ jsxRuntime.jsx("div", { className: "aoLexHueThumb", style: hueThumb }) }),
5293
+ /* @__PURE__ */ jsxRuntime.jsx("div", { className: "aoLexHue", ref: hueRef, onClick: handleHueClick, children: /* @__PURE__ */ jsxRuntime.jsx("div", { className: "aoLexHueThumb", style: hueThumb }) }),
5392
5294
  /* @__PURE__ */ jsxRuntime.jsx("div", { className: "aoLexPreview", style: { background: hex } }),
5393
5295
  /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "aoLexActions", children: [
5394
- /* @__PURE__ */ jsxRuntime.jsx(
5395
- react.DefaultButton,
5396
- {
5397
- type: "button",
5398
- text: "Apply",
5399
- onClick: () => {
5400
- commitHsv(h, s, v, true);
5401
- }
5402
- }
5403
- ),
5296
+ /* @__PURE__ */ jsxRuntime.jsx(react.DefaultButton, { type: "button", text: "Apply", onClick: handleApply }),
5404
5297
  /* @__PURE__ */ jsxRuntime.jsx(react.DefaultButton, { type: "button", text: "Close", onClick: () => setOpenAndNotify(false) })
5405
5298
  ] })
5406
5299
  ] })
@@ -5414,20 +5307,10 @@ var ColorPickerPlugin = ({ disabled }) => {
5414
5307
  const lastRangeSelectionRef = React9__namespace.default.useRef(null);
5415
5308
  const updateToolbar = () => {
5416
5309
  const selection$1 = lexical.$getSelection();
5417
- const isRange = lexical.$isRangeSelection(selection$1);
5418
- console.log("[AO-ColorPicker] updateToolbar", {
5419
- isRangeSelection: isRange,
5420
- isCollapsed: isRange ? selection$1.isCollapsed() : null
5421
- });
5422
- if (isRange) {
5310
+ if (lexical.$isRangeSelection(selection$1)) {
5423
5311
  lastRangeSelectionRef.current = selection$1.clone();
5424
5312
  const c = selection.$getSelectionStyleValueForProperty(selection$1, "color", "#000000");
5425
- const bg = selection.$getSelectionStyleValueForProperty(
5426
- selection$1,
5427
- "background-color",
5428
- "#ffffff"
5429
- );
5430
- console.log("[AO-ColorPicker] updateToolbar readback", { color: c, bgColor: bg });
5313
+ const bg = selection.$getSelectionStyleValueForProperty(selection$1, "background-color", "#ffffff");
5431
5314
  setColors({ color: c, bgColor: bg });
5432
5315
  }
5433
5316
  };
@@ -5457,15 +5340,6 @@ var ColorPickerPlugin = ({ disabled }) => {
5457
5340
  };
5458
5341
  const applyStyle = (args) => {
5459
5342
  if (disabled) return;
5460
- console.log("[AO-ColorPicker] applyStyle called", {
5461
- property: args.property,
5462
- color: args.color,
5463
- hasSavedSelection: !!lastRangeSelectionRef.current,
5464
- savedSelectionIsCollapsed: lastRangeSelectionRef.current?.isCollapsed() ?? null,
5465
- wasEditorActiveAtOpen: wasEditorActiveRef.current,
5466
- activeElementTag: document.activeElement?.tagName,
5467
- activeElementClass: document.activeElement?.className
5468
- });
5469
5343
  editor.update(
5470
5344
  () => {
5471
5345
  const saved = lastRangeSelectionRef.current;
@@ -5473,39 +5347,17 @@ var ColorPickerPlugin = ({ disabled }) => {
5473
5347
  lexical.$setSelection(saved.clone());
5474
5348
  }
5475
5349
  const selection$1 = lexical.$getSelection();
5476
- const isRange = lexical.$isRangeSelection(selection$1);
5477
- console.log("[AO-ColorPicker] applyStyle inside editor.update", {
5478
- hadSavedSelection: !!saved,
5479
- selectionAfterRestoreIsRange: isRange,
5480
- selectionAfterRestoreIsCollapsed: isRange ? selection$1.isCollapsed() : null
5481
- });
5482
- if (isRange) {
5350
+ if (lexical.$isRangeSelection(selection$1)) {
5483
5351
  selection.$patchStyleText(selection$1, { [args.property]: args.color });
5484
- const verify = selection.$getSelectionStyleValueForProperty(
5485
- selection$1,
5486
- args.property,
5487
- "<none>"
5488
- );
5489
- console.log("[AO-ColorPicker] applyStyle after patchStyleText, readback in same update", {
5490
- property: args.property,
5491
- appliedColor: args.color,
5492
- readBack: verify
5493
- });
5494
- } else {
5495
- console.warn(
5496
- "[AO-ColorPicker] applyStyle: no range selection available \u2014 style was NOT applied",
5497
- { property: args.property, color: args.color }
5498
- );
5499
5352
  }
5500
5353
  },
5501
5354
  // Without this tag, Lexical's reconciler force-focuses the editor root
5502
5355
  // whenever this update's selection diff finds the root isn't already
5503
- // focused (see Lexical.dev.mjs ~8112) — which it never is while the
5504
- // color picker's Callout legitimately holds focus. That forced focus
5505
- // then fights Fluent's Callout for focus on every single drag-driven
5506
- // commit, repeatedly bouncing focus (and, via FocusEventsPlugin,
5507
- // nulling and restoring the selection) between the editor and the
5508
- // popover until the drag's tracked color desynced from the cursor.
5356
+ // focused — which it isn't while the color picker's Callout holds
5357
+ // focus. The picker now only calls applyStyle once, on Apply, so this
5358
+ // is no longer fighting for focus on every drag pixel, but there's no
5359
+ // reason to force a focus change here at all — handleOpenChange above
5360
+ // already does that deliberately, once, on close.
5509
5361
  { tag: lexical.SKIP_SELECTION_FOCUS_TAG }
5510
5362
  );
5511
5363
  };
@@ -6851,12 +6703,6 @@ function FocusEventsPlugin({
6851
6703
  const next = e.relatedTarget;
6852
6704
  const container = containerRef.current;
6853
6705
  const stillInside = !!next && (container ? container.contains(next) : root.contains(next));
6854
- console.log("[AO-ColorPicker] FocusEventsPlugin focusout", {
6855
- relatedTargetTag: next ? next.tagName : null,
6856
- relatedTargetClass: next ? next.className : null,
6857
- stillInside,
6858
- willClearSelection: !stillInside
6859
- });
6860
6706
  if (stillInside) return;
6861
6707
  editor.update(() => {
6862
6708
  lexical.$setSelection(null);