/** * @module plugins/enter */ import type { IJodit, Nullable } from 'jodit/types'; import { Dom } from 'jodit/core/dom/dom'; import { $$ } from 'jodit/core/helpers/utils/selector'; import { insertParagraph } from './insert-paragraph'; /** * Handles pressing the Enter key inside an empty LI inside a list * @private */ export function processEmptyLILeaf(jodit: IJodit, li: HTMLElement): void { const list: Nullable = Dom.closest( li, ['ol', 'ul'], jodit.editor ); if (!list) { return; } const parentLi = list.parentElement, listInsideLeaf = Dom.isTag(parentLi, 'li'); const container = listInsideLeaf ? parentLi : list; // Empty element in the middle of the list const leftRange = jodit.s.createRange(); leftRange.setStartAfter(li); leftRange.setEndAfter(list); const rightPart = leftRange.extractContents(); const fakeTextNode = jodit.createInside.fake(); Dom.after(container, fakeTextNode); Dom.safeRemove(li); if (!$$('li', list).length) { Dom.safeRemove(list); } const newLi = insertParagraph( jodit, fakeTextNode, listInsideLeaf ? 'li' : jodit.o.enter ); if (!rightPart.querySelector('li')) { return; } if (listInsideLeaf) { newLi.appendChild(rightPart); } else { Dom.after(newLi, rightPart); } }