Skip to content

Commit

Permalink
Correct several issues with range selections.
Browse files Browse the repository at this point in the history
  • Loading branch information
tellsworth committed Apr 20, 2023
1 parent 1328c1a commit f08fec1
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
6 changes: 5 additions & 1 deletion src/lib/plugins/ImageElement.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@
editor: T,
options: ImagesOptions = {}
): T {
const { insertData, insertBreak } = editor;
const { insertData, insertBreak, isVoid } = editor;
editor.isVoid = element => {
return element.type === 'image' ? true : isVoid(element)
}
editor.insertBreak = () => {
const [match] = Editor.nodes(editor, {
Expand Down
19 changes: 16 additions & 3 deletions src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -360,13 +360,26 @@ export function toSlatePoint<T extends boolean>(
): T extends true ? Point | null : Point {
const { exactMatch, suppressThrow } = options;
const [nearestNode, nearestOffset] = exactMatch ? domPoint : normalizeDOMPoint(domPoint);
const parentNode = nearestNode.parentNode as DOMElement;
// Handle blockquotes and other elements whose direct parent is the editor itself.
const nearestNodeIsDOMElement =
nearestNode instanceof DOMElement && nearestNode.getAttribute('data-slate-node') === 'element';
const parentNode = nearestNodeIsDOMElement ? nearestNode : nearestNode.parentNode as DOMElement;
let textNode: DOMElement | null = null;
let offset = 0;

if (parentNode) {
const voidNode = parentNode.closest('[data-slate-void="true"]');
let leafNode = parentNode.closest('[data-slate-leaf]');
// Sometimes the selected node is the wrapping element, sometimes it is the leaf contents.
let voidNode, leafNode;
if (parentNode.getAttribute('data-slate-node') === 'element') {
leafNode = parentNode.querySelector('[data-slate-leaf]');
if (parentNode.getAttribute('data-slate-void')) {
voidNode = parentNode;
}
} else {
leafNode = parentNode.closest('[data-slate-leaf]');
voidNode = parentNode.closest('[data-slate-void="true"]');
}

let domNode: DOMElement | null = null;

if (leafNode) {
Expand Down

0 comments on commit f08fec1

Please sign in to comment.