<!DOCTYPE html>
MVC开发Markdown编辑器(2)MVC
Markdown
实时预览
我希望实现一个在线实时预览的Markdown编辑器,左边是编辑处,右边是实时预览界面。
准备工作
引入相关js和css
- 这里我需要引入bootstrap的css 用来做基本的布局
- jquery以及之前提到过的mdd的js和css
<link href="~/Scripts/mdd_styles.css" rel="stylesheet" />
<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script src="~/Scripts/MarkdownDeepLib.min.js"></script>
由于bootstrap默认在Layout中已经引入,这里就不需要引入了
View代码
<div id="markdownDiv" class="row">
<div id="editDiv" class="mdd_modal_frame mdd_editor_wrap col-lg-6">
@Html.TextArea("OrignalText", new { @class = "mdd_modal_content mdd_editor textarea" })
</div>
<div id="renderDiv" class="mdd_modal_frame col-lg-6 ">
<div id="renderContent" class="mdd_modal_content"></div>
</div>
</div>
这里结构很简单最外层一个div 里面2个div一个显示在左,一个显示在右,
因此最外层Div我们设置Class为row
让它占满整行,里面两个div都设置为col-lg-6
,这样基本的布局就出来了,同时再添加mdd_modal_frame
到里面2个子div,这个样式是在mdd_styles.css
写好的,它是的div有基本的外框样式
JavaScript代码
function markdown(content) {
//Set required options:
// Set options
md.ExtraMode = true;
md.SafeMode = true;
md.MarkdownInHtml = true;
//Transform your text:
var output = md.Transform(content);
return output;
};
function Transform() {
var content = $("#OrignalText").val();
var html = markdown(content);
\(</span><span class="pun">(</span><span class="str">"#renderContent"</span><span class="pun">).</span><span class="pln">html</span><span class="pun">(</span><span class="pln">html</span><span class="pun">);</span></code></li><li class="L4"><code class="language-javascript"><span class="pun">}</span></code></li><li class="L5"><code class="language-javascript"></code></li></ol></pre><blockquote data-anchor-id="1yzk" class="white-blockquote"> <p>通过<code>Transform</code>函数找到需要解析的文本框,将其内容解析并添加到<code>renderContent</code>div中</p> </blockquote><div class="md-section-divider"></div><pre class="prettyprint linenums prettyprinted" data-anchor-id="cpjw"><ol class="linenums"><li class="L0"><code class="language-javascript"><span class="kwd">var</span><span class="pln"> enterOrignal </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">false</span><span class="pun">;</span><span class="com">//鼠标是否在OrigbanText的上</span></code></li><li class="L1"><code class="language-javascript"><span class="kwd">var</span><span class="pln"> enterRender </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">false</span><span class="pun">;</span><span class="com">//鼠标是否在RenderText的上</span></code></li><li class="L2"><code class="language-javascript"><span class="pln">\)(function() {
$("#OrignalText").click(function() {
this.focus();
});
//本文点击是获取焦点
$("#OrignalText").keyup(Transform);
\(</span><span class="pun">(</span><span class="str">"#OrignalText"</span><span class="pun">).</span><span class="pln">scroll</span><span class="pun">(</span><span class="kwd">function</span><span class="pln"> </span><span class="pun">()</span><span class="pln"> </span><span class="pun">{</span></code></li><li class="L1"><code class="language-javascript"><span class="pln"> </span><span class="kwd">if</span><span class="pln"> </span><span class="pun">(</span><span class="pln">enterOrignal</span><span class="pun">){</span><span class="com">//只有进入OrignalText了才滚动renderContent,</span></code></li><li class="L2"><code class="language-javascript"><span class="pln"> </span><span class="kwd">var</span><span class="pln"> height </span><span class="pun">=</span><span class="pln"> </span><span class="pun">(</span><span class="pln">\)("#renderContent").height() 1.0 / $("#OrignalText").height()) $("#OrignalText").scrollTop();
$("#renderContent").scrollTop(height);
}
});
\(</span><span class="pun">(</span><span class="str">"#renderContent"</span><span class="pun">).</span><span class="pln">scroll</span><span class="pun">(</span><span class="kwd">function</span><span class="pln"> </span><span class="pun">()</span><span class="pln"> </span><span class="pun">{</span></code></li><li class="L7"><code class="language-javascript"><span class="pln"> </span><span class="kwd">if</span><span class="pln"> </span><span class="pun">(</span><span class="pln">enterRender</span><span class="pun">){</span><span class="com">//只有进入renderContent了才滚动orignal,</span></code></li><li class="L8"><code class="language-javascript"><span class="pln"> </span><span class="kwd">var</span><span class="pln"> height </span><span class="pun">=</span><span class="pln"> </span><span class="pun">(</span><span class="pln">\)("#OrignalText").height() 1.0 / $("#renderContent").height()) $("#renderContent").scrollTop();
$("#OrignalText").scrollTop(height);
}
});
$("#OrignalText").mouseenter(function () {
enterOrignal = true;//鼠标进入则设置为true
});
$("#OrignalText").mouseleave(function () {
enterOrignal = false;//鼠标离开则设置为false
});
$("#renderContent").mouseenter(function () {
enterRender = true;
});
$("#renderContent").mouseleave(function () {
enterRender = false;
});
});
//滚动条同步滚动
});
双向滚动就完成了在页面加载时绑定事件