feat: add coordsAtPos and posToClientRect helper methods
This commit is contained in:
@@ -17,7 +17,7 @@ function singleRect(object: Range | Element, bias: number) {
|
|||||||
: rects[bias < 0 ? 0 : rects.length - 1]
|
: rects[bias < 0 ? 0 : rects.length - 1]
|
||||||
}
|
}
|
||||||
|
|
||||||
export function coordsAtPos(view: EditorView, pos: number, end = false) {
|
export default function coordsAtPos(view: EditorView, pos: number, end = false) {
|
||||||
const { node, offset } = view.domAtPos(pos) // view.docView.domFromPos(pos);
|
const { node, offset } = view.domAtPos(pos) // view.docView.domFromPos(pos);
|
||||||
let side: DOMRectSide | null = null
|
let side: DOMRectSide | null = null
|
||||||
let rect: DOMRect | null = null
|
let rect: DOMRect | null = null
|
||||||
22
packages/core/src/helpers/posToClientRect.ts
Normal file
22
packages/core/src/helpers/posToClientRect.ts
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
import { EditorView } from 'prosemirror-view'
|
||||||
|
import coordsAtPos from './coordsAtPos'
|
||||||
|
|
||||||
|
export default function posToClientRect(view: EditorView, from: number, to: number): ClientRect {
|
||||||
|
const start = coordsAtPos(view, from)
|
||||||
|
const end = coordsAtPos(view, to, true)
|
||||||
|
const top = Math.min(start.top, end.top)
|
||||||
|
const bottom = Math.max(start.bottom, end.bottom)
|
||||||
|
const left = Math.min(start.left, end.left)
|
||||||
|
const right = Math.max(start.right, end.right)
|
||||||
|
const width = right - left
|
||||||
|
const height = bottom - top
|
||||||
|
|
||||||
|
return {
|
||||||
|
width,
|
||||||
|
height,
|
||||||
|
top,
|
||||||
|
bottom,
|
||||||
|
left,
|
||||||
|
right,
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -12,6 +12,7 @@ export { default as markPasteRule } from './pasteRules/markPasteRule'
|
|||||||
export { default as callOrReturn } from './utilities/callOrReturn'
|
export { default as callOrReturn } from './utilities/callOrReturn'
|
||||||
export { default as mergeAttributes } from './utilities/mergeAttributes'
|
export { default as mergeAttributes } from './utilities/mergeAttributes'
|
||||||
|
|
||||||
|
export { default as coordsAtPos } from './helpers/coordsAtPos'
|
||||||
export { default as getExtensionField } from './helpers/getExtensionField'
|
export { default as getExtensionField } from './helpers/getExtensionField'
|
||||||
export { default as findChildren } from './helpers/findChildren'
|
export { default as findChildren } from './helpers/findChildren'
|
||||||
export { default as findParentNode } from './helpers/findParentNode'
|
export { default as findParentNode } from './helpers/findParentNode'
|
||||||
@@ -26,6 +27,7 @@ export { default as isNodeActive } from './helpers/isNodeActive'
|
|||||||
export { default as isNodeEmpty } from './helpers/isNodeEmpty'
|
export { default as isNodeEmpty } from './helpers/isNodeEmpty'
|
||||||
export { default as isNodeSelection } from './helpers/isNodeSelection'
|
export { default as isNodeSelection } from './helpers/isNodeSelection'
|
||||||
export { default as isTextSelection } from './helpers/isTextSelection'
|
export { default as isTextSelection } from './helpers/isTextSelection'
|
||||||
|
export { default as posToClientRect } from './helpers/posToClientRect'
|
||||||
|
|
||||||
export interface Commands {}
|
export interface Commands {}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user