1 line
5.1 KiB
JavaScript
1 line
5.1 KiB
JavaScript
function getScrollLink(){var scrollLink={extensionId:"scrollLink",extensionName:"Scroll Link",optional:true,settingsBloc:["<p>Binds together editor and preview scrollbars.</p>",'<blockquote class="muted"><b>NOTE:</b>'," The mapping between Markdown and HTML is based on the position of the title elements (h1, h2, ...) in the page."," Therefore, if your document does not contain any title, the mapping will be linear and consequently less accurate.","</bloquote>"].join("")};var msie=/msie/.test(navigator.userAgent.toLowerCase());var mdSectionList=[];var htmlSectionList=[];function pxToFloat(px){return parseFloat(px.substring(0,px.length-2))}var buildSections=_.debounce(function(){var editorElt=$("#wmd-input");mdSectionList=[];var textareaElt=$("#md-section-helper");textareaElt.width(editorElt.width());var padding=pxToFloat(editorElt.css("padding-top"));var offset=0,mdSectionOffset=0;function addMdSection(sectionText){var sectionHeight=padding;if(sectionText!==undefined){textareaElt.val(sectionText);if(msie){sectionHeight+=textareaElt.prop("scrollHeight")+6}else{sectionHeight+=textareaElt.prop("scrollHeight")}}var newSectionOffset=mdSectionOffset+sectionHeight;mdSectionList.push({startOffset:mdSectionOffset,endOffset:newSectionOffset,height:sectionHeight,length:sectionText==undefined?0:sectionText.length+1});mdSectionOffset=newSectionOffset;padding=0}var text=editorElt.val()+"\n\n";text.replace(/^```.*\n[\s\S]*?\n```|(^.+[ \t]*\n=+[ \t]*\n+|^.+[ \t]*\n-+[ \t]*\n+|^\#{1,6}[ \t]*.+?[ \t]*\#*\n+)/gm,function(match,title,matchOffset){if(title){var sectionText=undefined;if(matchOffset>offset){sectionText=text.substring(offset,matchOffset-1)}addMdSection(sectionText);offset=matchOffset}return""});padding+=pxToFloat(editorElt.css("padding-bottom"));addMdSection(text.substring(offset,text.length-2));var previewElt=$(".preview-container");htmlSectionList=[];var htmlSectionOffset=0;var previewScrollTop=previewElt.scrollTop();var wmdPreviewTop=$(".preview-container").position().top;$("#wmd-preview").children("h1,h2,h3,h4,h5,h6").each(function(){var newSectionOffset=$(this).position().top-wmdPreviewTop+previewScrollTop+pxToFloat($(this).css("margin-top"));htmlSectionList.push({startOffset:htmlSectionOffset,endOffset:newSectionOffset,height:newSectionOffset-htmlSectionOffset});htmlSectionOffset=newSectionOffset});var scrollHeight=previewElt.prop("scrollHeight");htmlSectionList.push({startOffset:htmlSectionOffset,endOffset:scrollHeight,height:scrollHeight-htmlSectionOffset});lastEditorScrollTop=-10;lastPreviewScrollTop=-10;isScrollPreview=false;runScrollLink()},500);var lastEditorScrollTop=-10;var lastPreviewScrollTop=-10;var isScrollPreview=false;var runScrollLink=_.debounce(function(){if(mdSectionList.length===0||mdSectionList.length!==htmlSectionList.length){return}var editorElt=$("#wmd-input");var editorScrollTop=editorElt.scrollTop();var previewElt=$(".preview-container");var previewScrollTop=previewElt.scrollTop();function animate(srcScrollTop,srcSectionList,destElt,destSectionList,currentDestScrollTop,callback){var sectionIndex=undefined;var srcSection=_.find(srcSectionList,function(section,index){sectionIndex=index;return srcScrollTop<section.endOffset});if(srcSection===undefined){return}var posInSection=(srcScrollTop-srcSection.startOffset)/srcSection.height;var destSection=destSectionList[sectionIndex];var destScrollTop=destSection.startOffset+destSection.height*posInSection;destScrollTop=_.min([destScrollTop,destElt.prop("scrollHeight")-destElt.outerHeight()]);if(Math.abs(destScrollTop-currentDestScrollTop)<=1){callback(currentDestScrollTop);return}destElt.animate({scrollTop:destScrollTop},500,function(){callback(destScrollTop)});setTimeout(function(){destElt.slimScroll({scrollTo:destScrollTop,onlyScrollBar:true})},500)}if(isScrollPreview===false&&Math.abs(editorScrollTop-lastEditorScrollTop)>1){lastEditorScrollTop=editorScrollTop;animate(editorScrollTop,mdSectionList,previewElt,htmlSectionList,previewScrollTop,function(destScrollTop){lastPreviewScrollTop=destScrollTop})}else if(Math.abs(previewScrollTop-lastPreviewScrollTop)>1){lastPreviewScrollTop=previewScrollTop;animate(previewScrollTop,htmlSectionList,editorElt,mdSectionList,editorScrollTop,function(destScrollTop){lastEditorScrollTop=destScrollTop})}},500);scrollLink.buildSections=function(){buildSections()};scrollLink.onLayoutCreated=function(){$(".preview-container").scroll(function(){isScrollPreview=true;runScrollLink()});$("#wmd-input").scroll(function(){isScrollPreview=false;runScrollLink()})};scrollLink.onEditorConfigure=function(editor){editor.getConverter().hooks.chain("postConversion",function(text){var previewElt=$("#wmd-preview");previewElt.height(previewElt.height());return text})};scrollLink.onPreviewFinished=function(){$("#wmd-preview").height("auto");buildSections()};scrollLink.getCursorPositionForPageDownUpInFullEditorMode=function(scrollTop){var sectionIndex=undefined;var srcSection=_.find(mdSectionList,function(section,index){sectionIndex=index;return scrollTop<section.endOffset});if(srcSection===undefined){return-1}var caretPosition=0;for(var index=0;index<=sectionIndex;index++){caretPosition=caretPosition+mdSectionList[index].length}return caretPosition};return scrollLink} |