Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix unwanted behaviour when converting text inside a list to a heading tag and vice versa #38

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 50 additions & 2 deletions js/grande.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@
prevPrevSibling = prevSibling;

while(prevPrevSibling = prevPrevSibling.previousSibling){
if (prevPrevSibling.nodeType != Node.TEXT_NODE) break;
if (prevPrevSibling.nodeType != Node.TEXT_NODE) break;
}

if (prevSibling.nodeName === "P" && !prevSibling.textContent.length && prevPrevSibling.nodeName !== "HR") {
Expand Down Expand Up @@ -249,6 +249,7 @@
subject,
insertedNode,
unwrap,
wasMerged,
node,
parent,
range;
Expand All @@ -269,17 +270,49 @@
insertedNode = insertListOnSelection(sel, textProp, "ol");
}

if (!insertedNode) {return;}

unwrap = insertedNode &&
["ul", "ol"].indexOf(insertedNode.nodeName.toLocaleLowerCase()) >= 0 &&
["p", "div"].indexOf(insertedNode.parentNode.nodeName.toLocaleLowerCase()) >= 0;
["p", "div"].indexOf(insertedNode.parentNode.nodeName.toLocaleLowerCase()) >= 0 ||
["h1", "h2", "h3"].indexOf(insertedNode.parentNode.nodeName.toLocaleLowerCase()) >= 0;

if (unwrap) {
node = sel.anchorNode;
parent = insertedNode.parentNode;
parent.parentNode.insertBefore(insertedNode, parent);
parent.parentNode.removeChild(parent);
removeDefaultFormatting(getParentWithTag(node,'li'));
moveCursorToBeginningOfSelection(sel, node);
}

wasMerged = (insertedNode.parentNode.nodeName === "ARTICLE");

if (wasMerged) {
removeDefaultFormatting(getParentWithTag(sel.focusNode,'li'));
}
}

function removeDefaultFormatting(wrappingNode) {
var children = wrappingNode.childNodes;
if (wrappingNode) {
for (var i = 0, len = children.length; i < len; i++) {
var child = children[i];
//remove styles left behind in element nodes
if (child.nodeType === 1) {
child.removeAttribute("style");
}
//remove spans we don't need
if (child.nodeName.toLowerCase() === "span") {
wrappingNode.insertBefore(child.childNodes[0],child);
wrappingNode.removeChild(child);
}
}
//remove the last linebreak if present
if (wrappingNode.lastChild.nodeName.toLowerCase() === "br") {
wrappingNode.removeChild(wrappingNode.lastChild);
}
}
}

function moveCursorToBeginningOfSelection(selection, node) {
Expand Down Expand Up @@ -364,6 +397,10 @@
if (hasParentWithTag(getFocusNode(), tag)) {
document.execCommand("formatBlock", false, "p");
document.execCommand("outdent");
} else if (hasParentWithTag(getFocusNode(),'li') && isHeadingTag(tag)) {
document.execCommand("formatBlock",false,tag);
document.execCommand("outdent");
removeDefaultFormatting( getParentHeading(window.getSelection().focusNode) );
} else {
document.execCommand("formatBlock", false, tag);
}
Expand Down Expand Up @@ -395,6 +432,13 @@
}
}

function getParentHeading(node) {
var checkNodeType = function(node) { return isHeadingTag(node.nodeName.toLowerCase()); },
returnNode = function(node) { return node; };

return getParent(node, checkNodeType, returnNode);
}

function getParentWithTag(node, nodeType) {
var checkNodeType = function(node) { return node.nodeName.toLowerCase() === nodeType; },
returnNode = function(node) { return node; };
Expand All @@ -406,6 +450,10 @@
return !!getParentWithTag(node, nodeType);
}

function isHeadingTag(tag) {
return /h1|h2|h3/.test(tag);
}

function getParentHref(node) {
var checkHref = function(node) { return typeof node.href !== "undefined"; },
returnHref = function(node) { return node.href; };
Expand Down