leksy-editor 2.2.1 → 2.2.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/utilities.js +48 -14
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "leksy-editor",
3
- "version": "2.2.1",
3
+ "version": "2.2.3",
4
4
  "description": "Leksy Editor is an alternative to traditional WYSIWYG editors, designed primarily for creating mail templates, blogs, and documents without any content manipulation.",
5
5
  "main": "index.js",
6
6
  "directories": {
package/utilities.js CHANGED
@@ -3933,42 +3933,76 @@ const handleBackspaceInList = (event, core) => {
3933
3933
  if (!range.collapsed) return;
3934
3934
 
3935
3935
  let element = range.startContainer;
3936
- if (element.nodeType === Node.TEXT_NODE) element = element.parentElement;
3936
+ if (element.nodeType === Node.TEXT_NODE) {
3937
+ element = element.parentElement;
3938
+ }
3937
3939
 
3938
3940
  const li = element.closest('li');
3939
- if (!li) return;
3940
-
3941
- if (!core.elements.editor.contains(li)) return;
3941
+ if (!li || !core.elements.editor.contains(li)) return;
3942
3942
 
3943
3943
  const nestedList = li.querySelector('ul, ol');
3944
3944
  if (!nestedList) return;
3945
3945
 
3946
+
3947
+ if (!isCaretAtStart(range, li)) return;
3948
+
3949
+ event.preventDefault();
3950
+
3951
+ const parentList = li.parentElement;
3952
+ const prevLi = li.previousElementSibling;
3953
+
3946
3954
  const clone = li.cloneNode(true);
3947
3955
  const nestedInClone = clone.querySelector('ul, ol');
3948
3956
  if (nestedInClone) nestedInClone.remove();
3949
- const textContent = clone.textContent.replace(/\u200B/g, '').trim();
3950
-
3951
- if (textContent.length > 0 && !isCaretAtStart(range, li)) return;
3952
3957
 
3953
- event.preventDefault();
3958
+ const parentText = clone.textContent.replace(/\u200B/g, '').trim();
3954
3959
 
3955
- const parentList = li.parentElement;
3956
3960
  const fragment = document.createDocumentFragment();
3957
3961
  const children = Array.from(nestedList.children);
3958
3962
 
3959
3963
  children.forEach(child => fragment.appendChild(child));
3960
3964
 
3961
- if (children.length > 0) {
3962
- parentList.insertBefore(fragment, li);
3963
- const firstChild = children[0];
3965
+ // Case 1: previous list item exists
3966
+ if (prevLi) {
3967
+
3968
+ if (parentText.length > 0) {
3969
+ prevLi.appendChild(document.createTextNode(parentText));
3970
+ }
3971
+
3972
+ if (children.length > 0) {
3973
+ parentList.insertBefore(fragment, li);
3974
+ }
3975
+
3976
+ li.remove();
3977
+
3964
3978
  const newRange = document.createRange();
3965
- newRange.setStart(firstChild, 0);
3979
+ newRange.selectNodeContents(prevLi);
3980
+ newRange.collapse(false);
3981
+
3982
+ selection.removeAllRanges();
3983
+ selection.addRange(newRange);
3984
+ }
3985
+ // Case 2: first list item
3986
+ else {
3987
+ const span = document.createElement('span');
3988
+ span.textContent = parentText || '';
3989
+
3990
+ parentList.parentElement.insertBefore(span, parentList);
3991
+
3992
+ if (children.length > 0) {
3993
+ parentList.insertBefore(fragment, li);
3994
+ }
3995
+
3996
+ li.remove();
3997
+
3998
+ const newRange = document.createRange();
3999
+ newRange.selectNodeContents(span);
3966
4000
  newRange.collapse(true);
4001
+
3967
4002
  selection.removeAllRanges();
3968
4003
  selection.addRange(newRange);
3969
4004
  }
3970
4005
 
3971
- li.remove();
3972
4006
  core.updateCaretPosition();
3973
4007
  };
3974
4008