Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
juliaroldi committed Apr 30, 2024
1 parent 73f2362 commit ca86446
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
isNodeOfType,
parseTableCells,
toArray,
wrap,
} from 'roosterjs-content-model-dom';
import type {
ParsedTable,
Expand Down Expand Up @@ -44,14 +45,12 @@ export const setDOMSelection: SetDOMSelection = (core, selection, skipSelectionC
try {
switch (selection?.type) {
case 'image':
const image = selection.image;
const image = ensureImageHasSpanParent(selection.image);

const spanImage = ensureImageHasSpanParent(image);
if (!spanImage) {
return;
}

core.selection.selection = selection;
core.selection.selection = {
type: 'image',
image,
};
core.api.setEditorStyle(
core,
DOM_SELECTION_CSS_KEY,
Expand Down Expand Up @@ -242,22 +241,19 @@ function setRangeSelection(doc: Document, element: HTMLElement | undefined, coll
}
}

function ensureImageHasSpanParent(image: HTMLImageElement) {
function ensureImageHasSpanParent(image: HTMLImageElement): HTMLImageElement {
const parent = image.parentElement;

if (
parent &&
isNodeOfType(parent, 'ELEMENT_NODE') &&
isElementOfType(parent, 'span') &&
parent.firstElementChild == image &&
parent.lastElementChild == image &&
!parent.textContent
parent.firstChild == image &&
parent.lastChild == image
) {
return parent;
return image;
}

const span = image.ownerDocument.createElement('span');
span.appendChild(image);
parent?.appendChild(span);
return parent;
wrap(image.ownerDocument, image, 'span');
return image;
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ export function removeUnnecessarySpan(root: Node) {

const isImageSpan = (child: HTMLElement) => {
return (
child?.firstElementChild?.tagName == 'IMG' &&
child.firstElementChild == child.lastElementChild
isNodeOfType(child.firstChild, 'ELEMENT_NODE') &&
child.firstChild.tagName == 'IMG' &&
child.firstChild == child.lastChild
);
};

0 comments on commit ca86446

Please sign in to comment.