Skip to content

Instantly share code, notes, and snippets.

@e7h4n
Created August 31, 2011 04:12
Show Gist options
  • Save e7h4n/1182812 to your computer and use it in GitHub Desktop.
Save e7h4n/1182812 to your computer and use it in GitHub Desktop.
var currentEditObj = null;
function findParent(obj, filter){
while (obj.nodeType !== Node.DOCUMENT_NODE) {
if (filter(obj)) {
return obj;
}
obj = obj.parentNode;
}
return null;
}
function edit(obj) {
currentEditObj = obj;
var content = currentEditObj.nodeValue;
console.log(content);
window.Htmlout.editContent(content);
}
function editDone(value) {
currentEditObj.nodeValue = value;
}
document.addEventListener('click', function(evt) {
var range = document.caretRangeFromPoint(evt.clientX, evt.clientY);
if (range && range.collapsed) {
var text = range.commonAncestorContainer;
if (text.nodeType === Node.TEXT_NODE && findParent(text, function(obj) {
return obj.nodeType === Node.ELEMENT_NODE && (obj.getAttribute('contenteditable') === false || obj.getAttribute('contenteditable') === 'false');
}) === null) {
edit(text);
}
}
});
/*
vim:ft=javascript
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment