fix: use parentNode rect first

This commit is contained in:
fantasticit 2022-03-22 13:37:14 +08:00
parent c50a520529
commit b14833c79f
1 changed files with 6 additions and 16 deletions

View File

@ -5,15 +5,8 @@ import { EditorState } from 'prosemirror-state';
export type FloatMenuViewOptions = { export type FloatMenuViewOptions = {
editor: Editor; editor: Editor;
getReferenceClientRect?: (props: { getReferenceClientRect?: (props: { editor: Editor; range: Range; oldState?: EditorState }) => DOMRect;
editor: Editor; shouldShow: (props: { editor: Editor; range: Range; oldState?: EditorState }, instance: FloatMenuView) => boolean;
range: Range;
oldState?: EditorState;
}) => DOMRect;
shouldShow: (
props: { editor: Editor; range: Range; oldState?: EditorState },
instance: FloatMenuView
) => boolean;
init: (dom: HTMLElement, editor: Editor) => void; init: (dom: HTMLElement, editor: Editor) => void;
update?: ( update?: (
dom: HTMLElement, dom: HTMLElement,
@ -36,11 +29,11 @@ export class FloatMenuView {
private _update: FloatMenuViewOptions['update']; private _update: FloatMenuViewOptions['update'];
private shouldShow: FloatMenuViewOptions['shouldShow']; private shouldShow: FloatMenuViewOptions['shouldShow'];
private tippyOptions: FloatMenuViewOptions['tippyOptions']; private tippyOptions: FloatMenuViewOptions['tippyOptions'];
private getReferenceClientRect: NonNullable<FloatMenuViewOptions['getReferenceClientRect']> = ({ private getReferenceClientRect: NonNullable<FloatMenuViewOptions['getReferenceClientRect']> = ({ editor, range }) => {
editor,
range,
}) => {
const { view, state } = editor; const { view, state } = editor;
if (this.parentNode) {
return this.parentNode.getBoundingClientRect();
}
if (isNodeSelection(state.selection)) { if (isNodeSelection(state.selection)) {
const node = view.nodeDOM(range.from) as HTMLElement; const node = view.nodeDOM(range.from) as HTMLElement;
@ -48,9 +41,6 @@ export class FloatMenuView {
return node.getBoundingClientRect(); return node.getBoundingClientRect();
} }
} }
if (this.parentNode) {
return this.parentNode.getBoundingClientRect();
}
return posToDOMRect(view, range.from, range.to); return posToDOMRect(view, range.from, range.to);
}; };