Skip to content

Commit e19eb4a

Browse files
fix: fix inline toolbar position
1 parent 7b48330 commit e19eb4a

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

src/components/modules/toolbar/inline.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { ModuleConfig } from '../../../types-internal/module-config';
1212
import InlineTool from '../../tools/inline';
1313
import { CommonInternalSettings } from '../../tools/base';
1414
import { IconChevronDown } from '@codexteam/icons';
15+
import UI from '../ui';
1516

1617
/**
1718
* Inline Toolbar elements
@@ -321,6 +322,7 @@ export default class InlineToolbar extends Module<InlineToolbarNodes> {
321322
private move(): void {
322323
const selectionRect = SelectionUtils.rect as DOMRect;
323324
const wrapperOffset = this.Editor.UI.nodes.wrapper.getBoundingClientRect();
325+
const isNestedEditor = !!this.Editor.UI.nodes.holder.closest(`.${this.Editor.UI.CSS.editorWrapper}`)
324326
const newCoords = {
325327
x: selectionRect.x - wrapperOffset.x,
326328
y: selectionRect.y +
@@ -330,13 +332,13 @@ export default class InlineToolbar extends Module<InlineToolbarNodes> {
330332
this.toolbarVerticalMargin,
331333
};
332334

333-
const realRightCoord = newCoords.x + this.width;
335+
const realRightCoord = isNestedEditor ? newCoords.x + this.width : newCoords.x + this.width + wrapperOffset.x;
334336

335337
/**
336338
* Prevent InlineToolbar from overflowing the content zone on the right side
337339
*/
338340
if (realRightCoord > this.Editor.UI.contentRect.right) {
339-
newCoords.x = this.Editor.UI.contentRect.right - this.width;
341+
newCoords.x = isNestedEditor ? this.Editor.UI.contentRect.right - this.width : this.Editor.UI.contentRect.right - this.width - wrapperOffset.x;
340342
}
341343

342344
this.nodes.wrapper.style.left = Math.floor(newCoords.x) + 'px';

src/components/modules/ui.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ export default class UI extends Module<UINodes> {
4747
*/
4848
private prevSelectionAnchorElement: Element | null = null;
4949

50+
/**
51+
* The actual cached selection.
52+
*/
53+
private prevSelection: any = null;
54+
5055
/**
5156
* Editor.js UI CSS class names
5257
*
@@ -801,12 +806,17 @@ export default class UI extends Module<UINodes> {
801806
private selectionChanged(): void {
802807
const { CrossBlockSelection, BlockSelection } = this.Editor;
803808
const focusedElement = Selection.anchorElement;
809+
const currentSelection = window.getSelection();
810+
811+
const sameSelection = currentSelection == this.prevSelection && currentSelection;
812+
const sameFocus = this.prevSelectionAnchorElement == focusedElement && focusedElement;
804813

805-
if (this.prevSelectionAnchorElement == focusedElement) {
814+
if (sameSelection && sameFocus) {
806815
return;
807816
}
808817

809818
this.prevSelectionAnchorElement = focusedElement;
819+
this.prevSelection = currentSelection;
810820

811821
if (CrossBlockSelection.isCrossBlockSelectionStarted) {
812822
// Removes all ranges when any Block is selected

0 commit comments

Comments
 (0)