测试了我编译的 codemirror 脚本包,它独立工作,没有别的,但是当我尝试使用gentelella主题中的元素时,它加载时没有错误,但是在隐藏 textarea 时,codemirror 本身是隐藏的。如果我使用 Chrome 调试器,我可以让 codemirror 可见的唯一方法是通过添加来编辑 textareastyle="display: block !important;"
以下是我用于gentelella主题的代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<!-- Meta, title, CSS, favicons, etc. -->
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Dashboard</title>
<!-- Bootstrap core CSS -->
<link href="./templates/gentelella/css/bootstrap.min.css" rel="stylesheet">
<link href="./templates/gentelella/fonts/css/font-awesome.min.css" rel="stylesheet">
<link href="./templates/gentelella/css/animate.min.css" rel="stylesheet">
<!-- Custom styling plus plugins -->
<link href="./templates/gentelella/css/custom.css" rel="stylesheet">
<link href="./templates/gentelella/css/maps/jquery-jvectormap-2.0.3.css" rel="stylesheet" type="text/css">
<link href="./templates/gentelella/css/icheck/flat/green.css" rel="stylesheet">
<link href="./templates/gentelella/css/floatexamples.css" rel="stylesheet" type="text/css">
<script src="./templates/gentelella/js/jquery.min.js"></script>
<script src="./templates/gentelella/js/nprogress.js"></script>
<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
<!--[if lt IE 9]>
<script src="./templates/gentelella/js/ie8-responsive-file-warning.js"></script>
<script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
<!-- CodeMirror -->
<link rel="stylesheet" href="./templates/gentelella/js/codemirror/lib/codemirror.css">
<link rel="stylesheet" href="./templates/gentelella/js/codemirror/addon/hint/show-hint.css">
<link rel="stylesheet" href="./templates/gentelella/js/codemirror/theme/cobalt.css">
<link rel="stylesheet" href="./templates/gentelella/js/codemirror/addon/dialog/dialog.css" />
<link rel="stylesheet" href="./templates/gentelella/js/codemirror/addon/hint/show-hint.css" />
<link rel="stylesheet" href="./templates/gentelella/js/codemirror/addon/fold/foldgutter.css" />
<link rel="stylesheet" href="./templates/gentelella/js/codemirror/addon/tern/tern.css" />
</head>
<body class="nav-md">
<div class="container body">
<div class="main_container">
<div class="col-md-3 left_col">
<div class="left_col scroll-view">
<div class="navbar nav_title" style="border: 0; height: 1px; overflow: hidden;">
<a href="/app/" class="site_title"><i class="fa fa-edit"></i> <span>Dashboard</span></a>
<br>
</div>
<div class="clearfix"></div>
<!-- menu prile quick info -->
<div class="profile">
<div class="profile_pic">
<img src="https://secure.gravatar.com/avatar/99fda7ae65e2d02e452f731d56ebe108" alt="Administrator" class="img-circle profile_img">
</div>
<div class="profile_info">
<span>Welcome,</span>
<h2>Administrator</h2>
</div>
<br>
</div>
<!-- /menu prile quick info -->
<!-- sidebar menu -->
<div id="sidebar-menu" class="main_menu_side hidden-print main_menu">
<div class="menu_section">
<h3> </h3>
<ul class="nav side-menu">
<li><a><i class="fa fa-home"></i> Home <span class="fa fa-chevron-down"></span></a>
<ul class="nav child_menu" style="display: none">
<li><a href="/app/">Dashboard</a></li>
</ul>
</li>
</ul>
</div>
</div>
<!-- /sidebar menu -->
<!-- /menu footer buttons -->
<div class="sidebar-footer hidden-small">
<a data-toggle="tooltip" data-placement="top" title="Settings" href="/app/settings">
<span class="glyphicon glyphicon-cog" aria-hidden="true"></span>
</a>
<a data-toggle="tooltip" data-placement="top" title="FullScreen" onclick="fullscreen()" href="javascript:void(0);">
<span class="glyphicon glyphicon-fullscreen" aria-hidden="true"></span>
</a>
<a data-toggle="tooltip" data-placement="top" title="Foo" href="#">
<span class="glyphicon glyphicon-lock" aria-hidden="true"></span>
</a>
<a data-toggle="tooltip" data-placement="top" title="Logout" href="/app/logout">
<span class="glyphicon glyphicon-off" aria-hidden="true"></span>
</a>
</div>
<!-- /menu footer buttons -->
</div>
</div>
<!-- top navigation -->
<div class="top_nav">
<div class="nav_menu">
<nav class="" role="navigation">
<div class="nav toggle">
<a id="menu_toggle"><i class="fa fa-bars"></i></a>
</div>
<ul class="nav navbar-nav navbar-right">
<li class="">
<a href="javascript:;" class="user-profile dropdown-toggle" data-toggle="dropdown" aria-expanded="false">
<img src="https://secure.gravatar.com/avatar/99fda7ae65e2d02e452f731d56ebe108" alt="Administrator">Administrator
<span class=" fa fa-angle-down"></span>
</a>
<ul class="dropdown-menu dropdown-usermenu pull-right">
<li><a href="/app/logout"><i class="fa fa-sign-out pull-right"></i> Log Out</a></li>
</ul>
</li>
<li role="presentation">
<a href="/app/notifications" alt="See All Notifications">
<i class="fa fa-envelope-o"></i>
</a>
</li>
</ul>
</nav>
</div>
</div>
<!-- /top navigation -->
<!-- page content -->
<div class="right_col" role="main">
<div class="row">
<textarea id="template" name="code"></textarea>
</div>
</div>
<!-- /page content -->
</div>
</div>
<div id="custom_notifications" class="custom-notifications dsp_none">
<ul class="list-unstyled notifications clearfix" data-tabbed_notifications="notif-group">
</ul>
<div class="clearfix"></div>
<div id="notif-group" class="tabbed_notifications"></div>
</div>
<script src="./templates/gentelella/js/bootstrap.min.js"></script>
<script src="./templates/gentelella/js/codemirror/lib/codemirror.js"></script>
<script src="./templates/gentelella/js/codemirror/lib/codemirror-extras.js"></script>
<script src="./templates/gentelella/js/codemirror/init.js"></script>
<!-- gauge js -->
<script type="text/javascript" src="./templates/gentelella/js/gauge/gauge.min.js"></script>
<!-- bootstrap progress js -->
<script src="./templates/gentelella/js/progressbar/bootstrap-progressbar.min.js"></script>
<!-- icheck -->
<script src="./templates/gentelella/js/icheck/icheck.min.js"></script>
<!-- daterangepicker -->
<script type="text/javascript" src="./templates/gentelella/js/moment/moment.min.js"></script>
<script type="text/javascript" src="./templates/gentelella/js/datepicker/daterangepicker.js"></script>
<!-- chart js -->
<script src="./templates/gentelella/js/chartjs/chart.min.js"></script>
<script src="./templates/gentelella/js/custom.js"></script>
<!-- flot js -->
<!--[if lte IE 8]><script type="text/javascript" src="./templates/gentelella/js/excanvas.min.js"></script><![endif]-->
<script type="text/javascript" src="./templates/gentelella/js/flot/jquery.flot.js"></script>
<script type="text/javascript" src="./templates/gentelella/js/flot/jquery.flot.pie.js"></script>
<script type="text/javascript" src="./templates/gentelella/js/flot/jquery.flot.orderBars.js"></script>
<script type="text/javascript" src="./templates/gentelella/js/flot/jquery.flot.time.min.js"></script>
<script type="text/javascript" src="./templates/gentelella/js/flot/date.js"></script>
<script type="text/javascript" src="./templates/gentelella/js/flot/jquery.flot.spline.js"></script>
<script type="text/javascript" src="./templates/gentelella/js/flot/jquery.flot.stack.js"></script>
<script type="text/javascript" src="./templates/gentelella/js/flot/curvedLines.js"></script>
<!-- <script type="text/javascript" src="./templates/gentelella/js/flot/jquery.flot.resize.js"></script>//-->
<!-- worldmap -->
<script type="text/javascript" src="./templates/gentelella/js/maps/jquery-jvectormap-2.0.3.min.js"></script>
<script type="text/javascript" src="./templates/gentelella/js/maps/gdp-data.js"></script>
<script type="text/javascript" src="./templates/gentelella/js/maps/jquery-jvectormap-world-mill-en.js"></script>
<script type="text/javascript" src="./templates/gentelella/js/maps/jquery-jvectormap-us-aea-en.js"></script>
<!-- pace -->
<script src="./templates/gentelella/js/pace/pace.min.js"></script>
<!-- skycons -->
<script src="./templates/gentelella/js/skycons/skycons.min.js"></script>
<!-- PNotify -->
<script type="text/javascript" src="./templates/gentelella/js/notify/pnotify.core.js"></script>
<script type="text/javascript" src="./templates/gentelella/js/notify/pnotify.buttons.js"></script>
<script type="text/javascript" src="./templates/gentelella/js/notify/pnotify.nonblock.js"></script>
<script>
function fullscreen() {
var isInFullScreen = (document.fullscreenElement && document.fullscreenElement !== null) ||
(document.webkitFullscreenElement && document.webkitFullscreenElement !== null) ||
(document.mozFullScreenElement && document.mozFullScreenElement !== null) ||
(document.msFullscreenElement && document.msFullscreenElement !== null);
if (!isInFullScreen) {
if (document.documentElement.requestFullscreen) {
document.documentElement.requestFullscreen();
} else if (document.documentElement.mozRequestFullScreen) {
document.documentElement.mozRequestFullScreen();
} else if (document.documentElement.webkitRequestFullScreen) {
document.documentElement.webkitRequestFullScreen();
} else if (document.documentElement.msRequestFullscreen) {
document.documentElement.msRequestFullscreen();
}
} else {
if (document.exitFullscreen) {
document.exitFullscreen();
} else if (document.webkitExitFullscreen) {
document.webkitExitFullscreen();
} else if (document.mozCancelFullScreen) {
document.mozCancelFullScreen();
} else if (document.msExitFullscreen) {
document.msExitFullscreen();
}
}
}
NProgress.done();
</script>
<!-- /datepicker -->
<!-- /footer content -->
</body>
</html>
这是我在查看页面时得到的:
如果我改变textarea
如上所述的风格:
一切正常,除了我无法摆脱文本区域(见下图)。
即使我将样式应用于 textarea(与 chrome live 中的方式相同)以将其重新定位到屏幕外,它实际上最终会重新定位 CodeMirror 块,并且 textarea 仍然存在。(示例样式 position: absolute; top: -1000;
:)
唯一不属于gentelella主题的自定义脚本如下:
codemirror-extras.js
这是使用CodeMirror Builder 构建的,并选择了以下选项:
Version: HEAD
CodeMirror Library:
- codemirror.js
Modes:
- css.js
- htmlembedded.js
- htmlmixed.js
- javascript.js
- markdown.js
- php.js
- sql.js
- vb.js
- vbscript.js
- xml.js
Add-ons:
- active-line.js
- brace-fold.js
- closebrackets.js
- closetag.js
- comment-fold.js
- css-hint.js
- dialog.js
- foldcode.js
- foldgutter.js
- html-hint.js
- javascript-hint.js
- jump-to-line.js
- markdown-fold.js
- match-highlighter.js
- matchbrackets.js
- matchtags.js
- overlay.js
- search.js
- searchcursor.js
- show-hint.js
- sql-hint.js
- xml-fold.js
- xml-hint.js
初始化.js
这只是使用一些选项初始化 CodeMirror。
InitializeCodeMirror = true;
function completeAfter(cm, pred) {
var cur = cm.getCursor();
if (!pred || pred()) setTimeout(function() {
if (!cm.state.completionActive)
var doc = cm.getDoc();
var POS = doc.getCursor();
var mode = CodeMirror.innerMode(cm.getMode(), cm.getTokenAt(POS).state).mode.name;
console.log(mode);
if (mode == 'xml') { //html depends on xml
CodeMirror.showHint(cm, CodeMirror.hint.html);
} else if (mode == 'javascript') {
CodeMirror.showHint(cm, CodeMirror.hint.javascript);
} else if (mode == 'css') {
CodeMirror.showHint(cm, CodeMirror.hint.css);
}
// cm.showHint({completeSingle: false});
}, 100);
return CodeMirror.Pass;
}
function completeIfAfterLt(cm) {
return completeAfter(cm, function() {
var cur = cm.getCursor();
return cm.getRange(CodeMirror.Pos(cur.line, cur.ch - 1), cur) == "<";
});
}
function completeIfInTag(cm) {
return completeAfter(cm, function() {
var tok = cm.getTokenAt(cm.getCursor());
if (tok.type == "string" && (!/['"]/.test(tok.string.charAt(tok.string.length - 1)) || tok.string.length == 1)) return false;
var inner = CodeMirror.innerMode(cm.getMode(), tok.state).state;
return inner.tagName;
});
}
function CodeMirrorInit(elementName) {
cmInstance = CodeMirror.fromTextArea(document.getElementById(elementName), {
mode: "htmlmixed",
lineNumbers: true,
autoCloseTags: true,
autoCloseBrackets: true,
foldGutter: true,
styleActiveLine: true,
theme: 'cobalt',
gutters: ["CodeMirror-foldgutter", "CodeMirror-linenumbers"],
autocomplete: true,
extraKeys: {
"'<'": completeAfter,
"'/'": completeIfAfterLt,
"' '": completeIfInTag,
"'='": completeIfInTag,
"'.'": completeAfter,
"'{'": completeAfter,
"':'": completeAfter,
"Ctrl-Space": "autocomplete"
}
} );
}
function registerHotKeys() {
$(window).bind('keydown', function(event) {
if (event.ctrlKey || event.metaKey) {
switch (String.fromCharCode(event.which).toLowerCase()) {
case 's':
event.preventDefault();
// jQuery.gritter.add({
// title: 'This is a regular notice!',
// text: 'This will fade out after a certain amount of time.',
// class_name: 'growl-success',
// image: '/se/template/images/screen.png',
// sticky: false,
// time: ''
// });
//alert('ctrl-s');
break;
// case 'f':
// event.preventDefault();
// alert('ctrl-f');
// break;
// case 'g':
// event.preventDefault();
// alert('ctrl-g');
// break;
}
}
});
}
if(InitializeCodeMirror === true) {
CodeMirrorInit('template');
cmInstance.focus();
cmInstance.setCursor(1);
registerHotKeys();
}