Skip to content

Instantly share code, notes, and snippets.

@marijnh
Created March 21, 2014 10:02
Show Gist options
  • Save marijnh/9683027 to your computer and use it in GitHub Desktop.
Save marijnh/9683027 to your computer and use it in GitHub Desktop.
CodeMirror merge view resize to fit content
function mergeViewHeight(mergeView) {
function editorHeight(editor) {
if (!editor) return 0;
return editor.getScrollInfo().height;
}
return Math.max(editorHeight(mergeView.leftOriginal()),
editorHeight(mergeView.editor()),
editorHeight(mergeView.rightOriginal()));
}
function resize(mergeView) {
var height = mergeViewHeight(mergeView);
for(;;) {
if (mergeView.leftOriginal())
mergeView.leftOriginal().setSize(null, height);
mergeView.editor().setSize(null, height);
if (mergeView.rightOriginal())
mergeView.rightOriginal().setSize(null, height);
var newHeight = mergeViewHeight(mergeView);
if (newHeight >= height) break;
else height = newHeight;
}
mergeView.wrap.style.height = height + "px";
}
@marianopeck
Copy link

Hi, I cannot seem to make it work this script. I am trying ti fit MergeView to its contents. I have added both functions but still cannot make it work. Any idea how can use it? Let's say I created code mirror this way: dv = CodeMirror.MergeView( target , { ....

@kulakowka
Copy link

kulakowka commented Oct 5, 2016

@marianopeck i found solution.

var mergeView = CodeMirror.MergeView(target, {
    value: value,
    orig: origLeft,
    lineNumbers: false,
    mode: 'gfm',
    lineWrapping: true,
    viewportMargin: Infinity,
    highlightDifferences: true,
    connect: true,
    collapseIdentical: false,
    revertButtons: true
  })

  resize(mergeView)

  $(window).on('resize', function () {
    resize(mergeView)
  })

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