Skip to content

Commit

Permalink
update: #217 nested list
Browse files Browse the repository at this point in the history
  • Loading branch information
JiHong88 committed Feb 24, 2020
1 parent 2579674 commit cfab3a2
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 21 deletions.
2 changes: 1 addition & 1 deletion src/lib/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -1147,8 +1147,8 @@ export default function (context, pluginCallButtons, plugins, lang, _options) {

if (selectedFormats && selectedFormats.indexOf(insNode) === -1) {
if (!rangeEl) rangeEl = rangeElement.cloneNode(false);
insNode = insNode.cloneNode(true);
rangeEl.appendChild(insNode);
i--, len--;
}
else {
if (rangeEl && rangeEl.children.length > 0) {
Expand Down
61 changes: 43 additions & 18 deletions src/plugins/submenu/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ export default {
}
},

editList: function (command) {
const selectedFormsts = this.getSelectedElementsAndComponents();
editList: function (command, selectedCells) {
const selectedFormsts = !selectedCells ? this.getSelectedElementsAndComponents() : selectedCells;
if (!selectedFormsts || selectedFormsts.length === 0) return;

let isRemove = true;
Expand All @@ -126,7 +126,7 @@ export default {
}
}

if (isRemove && (!topEl || command !== topEl.tagName.toUpperCase()) && (!bottomEl || command !== bottomEl.tagName.toUpperCase())) {
if (isRemove && (!topEl || (firstSel.tagName !== topEl.tagName || command !== topEl.tagName.toUpperCase())) && (!bottomEl || (lastSel.tagName !== bottomEl.tagName || command !== bottomEl.tagName.toUpperCase()))) {
const currentFormat = this.util.getRangeFormatElement(this.getSelectionNode());
const cancel = currentFormat && currentFormat.tagName === command;
let rangeArr, tempList;
Expand Down Expand Up @@ -251,41 +251,66 @@ export default {
editInsideList: function (remove) {
const selectedCells = this.getSelectedElements().filter(function (el) { return this.isListCell(el); }.bind(this.util));
const cellsLen = selectedCells.length;
if ((!remove && !selectedCells[0].previousElementSibling) || cellsLen === 0) return;
if (cellsLen === 0 || (!remove && (!selectedCells[0].previousElementSibling || !selectedCells[cellsLen - 1].nextElementSibling))) return;

let originList = selectedCells[0].parentNode;
let sc, so, ec, eo;

if (remove) {
this.plugins.list.editList.call(this, originList.nodeName.toUpperCase());
this.plugins.list.editList.call(this, originList.nodeName.toUpperCase(), selectedCells);
} else {
sc = selectedCells[0], eo = 1;
const sc = selectedCells[0], so = cellsLen > 1 ? 0 : 1, ec = selectedCells[cellsLen - 1], eo = 1;
let innerList = this.util.createElement(originList.nodeName);
let prev = sc.previousElementSibling;
let next = sc.nextElementSibling;

for (let i = 0, len = cellsLen, c; i < len; i++) {
c = selectedCells[i];
if (c.parentNode !== originList) {
originList.insertBefore(innerList, next);
this.plugins.list._insiedList(originList, innerList, prev, next);
originList = c.parentNode;
innerList = this.util.createElement(originList.nodeName);
}

prev = c.previousElementSibling;
next = c.nextElementSibling;
innerList.appendChild(c);
if (i === len - 1) {
originList.insertBefore(innerList, next);
}
}

innerList = this.plugins.list._insiedList(originList, innerList, prev, next);
this.setRange(sc, so, ec, eo);

if (cellsLen > 1) {
so = 0, ec = selectedCells[cellsLen - 1];
} else {
so = 1, ec = sc;
// history stack
this.history.push(false);
}
},

_insiedList: function (originList, innerList, prev, next) {
let insertPrev = false;

if (prev && innerList.tagName === prev.tagName) {
const children = innerList.children;
while (children[0]) {
prev.appendChild(children[0]);
}

innerList = prev;
insertPrev = true;
}

this.setRange(sc, so, ec, eo);

if (next && innerList.tagName === next.tagName) {
const children = next.children;
while (children[0]) {
innerList.appendChild(children[0]);
}

const temp = next.nextElementSibling;
next.parentNode.removeChild(next);
next = temp;
}

if (!insertPrev) originList.insertBefore(innerList, next);

return innerList;
},

pickup: function (e) {
Expand All @@ -302,6 +327,6 @@ export default {

if (!command) return;

this.plugins.list.editList.call(this, command);
this.plugins.list.editList.call(this, command, null);
}
};
6 changes: 4 additions & 2 deletions test/dev/suneditor_build_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ let s1 = suneditor.create('editor', {
'preview', 'print', 'save', 'template']
],
width: '100%',
pasteTagsWhitelist: 'p|h[1-6]',
height: 'auto',
// pasteTagsWhitelist: 'p|h[1-6]',
formats: [
{
tag: 'div', // Tag name
Expand Down Expand Up @@ -100,7 +101,8 @@ window.sun_create1 = function () {
s1 = suneditor.create('editor', {
plugins: [align, plugins.link],
buttonList: [['align', 'link', 'bold', 'underline', 'italic', 'strike', 'removeFormat', 'codeView']],
width: '100%'
width: '100%',
height: 'auto'
})
}

Expand Down

0 comments on commit cfab3a2

Please sign in to comment.