Skip to content

Commit

Permalink
enhance table paste process & add T/C - fixed #493 (#618)
Browse files Browse the repository at this point in the history
* change condition statement priority
* add condition for non collapsed range check
* change condition for multi TD paste & add removeTableContents()
* change isInTable() to public & extract pasting branch logic from WwPasteContentHelper
* modify condition for table removal
* solve TD paste & wrap TDs into TR and TRs into TBODY
* extract _completeIncompleteTable() method related
* refactor: _getTableDataFromTable
* change table completion stage
  • Loading branch information
junghwan-park authored and seonim-ryu committed Jan 2, 2020
1 parent 3e42a73 commit 994062c
Show file tree
Hide file tree
Showing 8 changed files with 837 additions and 20 deletions.
56 changes: 55 additions & 1 deletion apps/core/src/js/domUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,58 @@ var getPath = function(node, root) {
return paths;
};

/**
* Find next TR's TD element by given TD and it's offset
* @param {HTMLElement} node TD element
* @param {boolean} [needFirstTd] Boolean value for find first TD in next line
* @returns {HTMLElement|null}
*/
var nextLineTableCell = function(node, needFirstTd) {
var index = 0;
var nextLineTrElement, nextLineTdElement, theadElement;

if (node) {
if (!needFirstTd) {
while (node.previousElementSibling) {
node = node.previousElementSibling;
index += 1;
}
}

nextLineTrElement = node.parentNode.nextSibling;
theadElement = $(node).parents('thead')[0];

if (nextLineTrElement) {
nextLineTdElement = nextLineTrElement.childNodes[index];
} else if (theadElement && theadElement.nextElementSibling.tagName === 'TBODY') {
nextLineTdElement = $(theadElement.nextElementSibling).find('td')[index];
}

if (nextLineTdElement && nextLineTdElement.tagName === 'TD') {
return nextLineTdElement;
}
}

return null;
};

/**
* Find next TD or TH element by given TE element
* @param {HTMLElement} node TD element
* @returns {HTMLElement|null}
*/
var nextTableCell = function(node) {
var nextElement;

nextElement = node.nextElementSibling;

if (nextElement && (nextElement.nodeName === 'TD' || nextElement.nodeName === 'TH')) {
return nextElement;
}

return null;
};

module.exports = {
getNodeName: getNodeName,
isTextNode: isTextNode,
Expand All @@ -399,5 +451,7 @@ module.exports = {
getPrevTextNode: getPrevTextNode,
findOffsetNode: findOffsetNode,
getPath: getPath,
getNodInfo: getNodeInfo
getNodInfo: getNodeInfo,
nextLineTableCell: nextLineTableCell,
nextTableCell: nextTableCell
};
8 changes: 6 additions & 2 deletions apps/core/src/js/wwClipboardManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ WwClipboardManager.prototype._initSquireEvent = function() {

self._pch.preparePaste(pasteData);
self.wwe.eventManager.emit('pasteBefore', {source: 'wysiwyg', data: pasteData});
self._refineCursorWithPasteContents(pasteData.fragment);
self._refineCursorWithPasteContentsIfNeed(pasteData.fragment);
self.wwe.postProcessForChange();
});
};
Expand All @@ -84,10 +84,14 @@ WwClipboardManager.prototype._initSquireEvent = function() {
* @param {DocumentFragment} fragment Copied contents
* @private
*/
WwClipboardManager.prototype._refineCursorWithPasteContents = function(fragment) {
WwClipboardManager.prototype._refineCursorWithPasteContentsIfNeed = function(fragment) {
var node = fragment;
var range = this.wwe.getEditor().getSelection().cloneRange();

if (fragment.childNodes.length === 0) {
return;
}

while (node.lastChild) {
node = node.lastChild;
}
Expand Down
39 changes: 34 additions & 5 deletions apps/core/src/js/wwPasteContentHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ WwPasteContentHelper.prototype.preparePaste = function(pasteData) {
var range = this.wwe.getEditor().getSelection().cloneRange();
var newFragment = this.wwe.getEditor().getDocument().createDocumentFragment();
var firstBlockIsTaken = false;
var nodeName, node, childNodes;
var codeblockManager = this.wwe.getManager('codeblock');
var tableManager = this.wwe.getManager('table');
var nodeName, node, childNodes, isPastingList;

pasteData.fragment = this._pasteFirstAid(pasteData.fragment);

Expand All @@ -47,10 +49,14 @@ WwPasteContentHelper.prototype.preparePaste = function(pasteData) {
while (childNodes.length) {
node = childNodes[0];
nodeName = domUtils.getNodeName(node);

if (this.wwe.getManager('codeblock').isInCodeBlock(range)) {
newFragment.appendChild(this.wwe.getManager('codeblock').prepareToPasteOnCodeblock(childNodes));
} else if (nodeName === 'LI' || nodeName === 'UL' || nodeName === 'OL') {
isPastingList = nodeName === 'LI' || nodeName === 'UL' || nodeName === 'OL';

if (codeblockManager.isInCodeBlock(range)) {
newFragment.appendChild(codeblockManager.prepareToPasteOnCodeblock(childNodes));
} else if (tableManager.isInTable(range)) {
newFragment = tableManager.prepareToPasteOnTable(pasteData, node);
childNodes.shift();
} else if (isPastingList) {
newFragment.appendChild(this._prepareToPasteList(childNodes, pasteData.rangeInfo, firstBlockIsTaken));
//첫번째 현재위치와 병합될 가능성이있는 컨텐츠가 만들어진경우는 이후 위치에 대한 정보가 필요없다
firstBlockIsTaken = true;
Expand Down Expand Up @@ -104,6 +110,8 @@ WwPasteContentHelper.prototype._pasteFirstAid = function(fragment) {

this._preElementAid(fragment);

this._tableElementAid(fragment);

//br은 preElemnetAid에서 필요해서 처리후 불필요한 br은 삭제한다.
$(fragment).find('br').remove();

Expand Down Expand Up @@ -333,4 +341,25 @@ WwPasteContentHelper.prototype._makeNodeAndAppend = function(pathInfo, content)
return node[0];
};

/**
* Pasting table element pre-process
* @param {DocumentFragment} fragment pasteData's fragment
* @memberOf WwPasteContentHelper
* @private
*/
WwPasteContentHelper.prototype._tableElementAid = function(fragment) {
var tableManager = this.wwe.getManager('table');
var wrapperTable = tableManager.wrapTheadAndTbodyIntoTableIfNeed(fragment);
var wrapperTr = tableManager.wrapDanglingTableCellsIntoTrIfNeed(fragment);
var wrapperTbody = tableManager.wrapTrsIntoTbodyIfNeed(fragment);

if (wrapperTr) {
$(fragment).append(wrapperTr);
} else if (wrapperTbody) {
$(fragment).append(wrapperTbody);
} else if (wrapperTable) {
$(fragment).append(wrapperTable);
}
};

module.exports = WwPasteContentHelper;
Loading

0 comments on commit 994062c

Please sign in to comment.