1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-06-25 06:43:15 +00:00

Allows text areas to be auto-height while being wrapped in an

element that has scrollbars. The wrapper element with the
scroll bar does not need to be a direct parent of the text area.

**update:** fixed a bug that came up in the discussion

This is fixed now: https://github.com/Jermolene/TiddlyWiki5/pull/1933#issuecomment-141774881

The problem was the check in getScrollContainer()
This commit is contained in:
Felix Hayashi 2015-10-26 21:22:10 +01:00
parent a204784c0c
commit d3ab4144d4

View File

@ -211,18 +211,31 @@ EditTextWidget.prototype.updateEditorDomNode = function(text) {
} }
}; };
/*
Get the first parent element that has scrollbars or use the body as fallback.
*/
EditTextWidget.prototype.getScrollContainer = function(el) {
while(el.parentNode) {
el = el.parentNode;
if(el.scrollTop) {
return el;
}
}
return this.document.body;
};
/* /*
Fix the height of textareas to fit their content Fix the height of textareas to fit their content
*/ */
EditTextWidget.prototype.fixHeight = function() { EditTextWidget.prototype.fixHeight = function() {
var self = this, var domNode = this.domNodes[0];
domNode = this.domNodes[0];
if(this.editAutoHeight && domNode && !domNode.isTiddlyWikiFakeDom && this.editTag === "textarea") { if(this.editAutoHeight && domNode && !domNode.isTiddlyWikiFakeDom && this.editTag === "textarea") {
// Resize the textarea to fit its content, preserving scroll position // Resize the textarea to fit its content, preserving scroll position
var scrollPosition = $tw.utils.getScrollPosition(), // Get the scroll container and register the current scroll position
scrollTop = scrollPosition.y; var container = this.getScrollContainer(domNode),
// Measure the specified minimum height scrollTop = container.scrollTop;
domNode.style.height = self.editMinHeight; // Measure the specified minimum height
domNode.style.height = this.editMinHeight;
var minHeight = domNode.offsetHeight; var minHeight = domNode.offsetHeight;
// Set its height to auto so that it snaps to the correct height // Set its height to auto so that it snaps to the correct height
domNode.style.height = "auto"; domNode.style.height = "auto";
@ -230,12 +243,11 @@ EditTextWidget.prototype.fixHeight = function() {
var newHeight = Math.max(domNode.scrollHeight + domNode.offsetHeight - domNode.clientHeight,minHeight); var newHeight = Math.max(domNode.scrollHeight + domNode.offsetHeight - domNode.clientHeight,minHeight);
// Only try to change the height if it has changed // Only try to change the height if it has changed
if(newHeight !== domNode.offsetHeight) { if(newHeight !== domNode.offsetHeight) {
domNode.style.height = newHeight + "px"; domNode.style.height = newHeight + "px";
// Make sure that the dimensions of the textarea are recalculated // Make sure that the dimensions of the textarea are recalculated
$tw.utils.forceLayout(domNode); $tw.utils.forceLayout(domNode);
// Check that the scroll position is still visible before trying to scroll back to it // Set the container to the position we registered at the beginning
scrollTop = Math.min(scrollTop,self.document.body.scrollHeight - window.innerHeight); container.scrollTop = scrollTop;
window.scrollTo(scrollPosition.x,scrollTop);
} }
} }
}; };