Skip to content

Instantly share code, notes, and snippets.

@grssam
Forked from paulrouget/s.js
Created July 13, 2012 07:42
Show Gist options
  • Save grssam/3103425 to your computer and use it in GitHub Desktop.
Save grssam/3103425 to your computer and use it in GitHub Desktop.
/* ------- */
// Clean mess from previous session
Components.utils.import("resource:///modules/source-editor.jsm");
let nbox = gBrowser.getNotificationBox();
while (nbox.lastChild.id == "foobar-container" ||
nbox.lastChild.id == "foobar-splitter") {
nbox.removeChild(nbox.lastChild);
}
/* ------- */
// Build container
let container = document.createElement("vbox");
container.setAttribute("flex", "0"); // This makes it take only the required height
container.id = "foobar-container";
container.setAttribute("pack", "start");
container.setAttribute("style", "max-height:0px;overflow:hidden");
container.height = 0; // This makes its height 0 which is then adjusted later to fit one line
let splitter = document.createElement("splitter");
splitter.className = "devtools-horizontal-splitter";
splitter.id = "foobar-splitter";
nbox.appendChild(splitter);
nbox.appendChild(container);
/* ------- */
// Editor!!!
let input = new SourceEditor();
input.init(container, {
initialText: "/* input */",
}, autofitEditor);
input.editorElement.flex = 0;
function autofitEditor(e) {
let lineHeight = e._view.getLineHeight();
e.previousLineCount = 1;
// This call will make the 0 height container to be of appropriate initial height
setEditorSize(e, lineHeight);
e.addEventListener(SourceEditor.EVENTS.TEXT_CHANGED, function() {
let count = e.getLineCount();
if (count != e.previousLineCount) {
e.previousLineCount = count;
setEditorSize(e, lineHeight * count);
}
});
}
function setEditorSize(e, height) {
container.style.minHeight =
container.style.maxHeight =
container.style.height =
e.editorElement.style.minHeight =
e.editorElement.style.maxHeight =
e.editorElement.style.height = (3 + height) + "px"; // 3 being the margin
}
@grssam
Copy link
Author

grssam commented Jul 13, 2012

If the max-height and height of container are not set 0 initially (as in previous version), then the editor height jumps from around 50px to 17px. 50px is the initial height before the first setEditorSize call is made.
So I set the initial heights to 0, so that the jump is not visible.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment