在一些OJ项目中,常常需要用到编辑器插件。 在Github上找到了这款功能强大的编辑器插件。
这是一款开源的前端代码编辑器
github地址:ace
Ace是用JavaScript编写的独立代码编辑器。我们的目标是创建一个基于浏览器的编辑器,以匹配并扩展现有本机编辑器(如TextMate,Vim或Eclipse)的功能,可用性和性能。它可以轻松地嵌入任何网页或JavaScript应用程序中。
引入js库文件。这里使用CND资源,也可以到GitHub下载js文件,然后导入项目中。
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.2.6/ace.js" type="text/javascript" charset="utf-8"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.2.6/ext-language_tools.js" type="text/javascript"
charset="utf-8"></script>x
需要一个容器,用做代码编辑区域,并给容器设置一个i。一定要给容器设置一个高度
<pre id="code" style="min-height:400px;">pre>
生成editor对象, 并设置相应属性
//初始化对象
editor = ace.edit("code");
//设置风格和语言(更多风格和语言,请到github上相应目录查看)
theme = "xcode";
language = "c_cpp";
editor.setTheme("ace/theme/" + theme);
editor.session.setMode("ace/mode/" + language);
//字体大小
editor.setFontSize(18);
//设置只读(true时只读,用于展示代码)
editor.setReadOnly(false);
//自动换行,设置为off关闭
editor.setOption("wrap", "free")
//启用提示菜单
ace.require("ace/ext/language_tools");
editor.setOptions({
enableBasicAutocompletion: true,
enableSnippets: true,
enableLiveAutocompletion: true
});
常用方法
// editor 是一个对象
// 获取编辑器输入的内容
var code=editor.getValue();
//给编辑器设置文本
editor.setValue();
完整代码
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Editortitle>
<script src="../../static/js/jquery-3.4.1.min.js">script>
head>
<body>
<div style="width: 80em;border-color: black;">
<pre id="code" style="min-height:400px;">pre>
div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.2.6/ace.js" type="text/javascript" charset="utf-8">script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.2.6/ext-language_tools.js" type="text/javascript"
charset="utf-8">script>
<script>
$('.language').change(function () {
editor.session.setMode("ace/mode/" + $(this).val());
});
$('.theme').change(function () {
editor.setTheme("ace/theme/" + $(this).val());
});
//初始化对象
editor = ace.edit("code");
//设置风格和语言(更多风格和语言,请到github上相应目录查看)
theme = "xcode";
language = "c_cpp";
editor.setTheme("ace/theme/" + theme);
editor.session.setMode("ace/mode/" + language);
//字体大小
editor.setFontSize(18);
//设置只读(true时只读,用于展示代码)
editor.setReadOnly(false);
//自动换行,设置为off关闭
editor.setOption("wrap", "free");
//启用提示菜单
ace.require("ace/ext/language_tools");
editor.setOptions({
enableBasicAutocompletion: true,
enableSnippets: true,
enableLiveAutocompletion: true
});
script>
body>
html>
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-HS94BB62-1590672431947)(ace 前端代码编辑器的使用/1.png)]
设置语言和代码风格
ace支持多少语言的代码还有多种风格,这里列举几种常用的语言和所有代码风格。
<select class="language">
<option value="c_cpp" selected>coption>
<option value="c_cpp">c++option>
<option value="java">javaoption>
<option value="python">pythonoption>
select>
<select class="theme">
<option value="xcode">xcodeoption>
<option value="clouds">cloudsoption>
<option value="clouds_midnight">clouds_midnightoption>
<option value="cobalt">cobaltoption>
<option value="dawn">dawnoption>
<option value="idle_fingers">idle_fingersoption>
<option value="kr_theme">kr_themeoption>
<option value="merbivore">merbivoreoption>
<option value="merbivore_soft">merbivore_softoption>
<option value="mono_industrial">mono_industrialoption>
<option value="monokai">monokaioption>
<option value="nord_dark">nord_darkoption>
<option value="pastel_on_dark">pastel_on_darkoption>
<option value="solarized_dark">solarized_darkoption>
<option value="solarized_light">solarized_lightoption>
<option value="katzenmilch">katzenmilchoption>
<option value="kuroir">kuroiroption>
<option value="tomorrow">tomorrowoption>
<option value="tomorrow_night">tomorrow_nightoption>
<option value="tomorrow_night_blue">tomorrow_night_blueoption>
<option value="tomorrow_night_bright">tomorrow_night_brightoption>
<option value="tomorrow_night_eighties">tomorrow_night_eightiesoption>
<option value="twilight">twilightoption>
<option value="vibrant_ink">vibrant_inkoption>
select>
添加语言设置后的源码
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Editortitle>
<script src="../../static/js/jquery-3.4.1.min.js">script>
head>
<body>
<div style="width: 80em;border-color: black;">
<select class="language">
<option value="c_cpp" selected>coption>
<option value="c_cpp">c++option>
<option value="java">javaoption>
<option value="python">pythonoption>
select>
<select class="theme">
<option value="xcode">xcodeoption>
<option value="clouds">cloudsoption>
<option value="clouds_midnight">clouds_midnightoption>
<option value="cobalt">cobaltoption>
<option value="dawn">dawnoption>
<option value="idle_fingers">idle_fingersoption>
<option value="kr_theme">kr_themeoption>
<option value="merbivore">merbivoreoption>
<option value="merbivore_soft">merbivore_softoption>
<option value="mono_industrial">mono_industrialoption>
<option value="monokai">monokaioption>
<option value="nord_dark">nord_darkoption>
<option value="pastel_on_dark">pastel_on_darkoption>
<option value="solarized_dark">solarized_darkoption>
<option value="solarized_light">solarized_lightoption>
<option value="katzenmilch">katzenmilchoption>
<option value="kuroir">kuroiroption>
<option value="tomorrow">tomorrowoption>
<option value="tomorrow_night">tomorrow_nightoption>
<option value="tomorrow_night_blue">tomorrow_night_blueoption>
<option value="tomorrow_night_bright">tomorrow_night_brightoption>
<option value="tomorrow_night_eighties">tomorrow_night_eightiesoption>
<option value="twilight">twilightoption>
<option value="vibrant_ink">vibrant_inkoption>
select>
<pre id="code" style="min-height:400px;">pre>
div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.2.6/ace.js" type="text/javascript" charset="utf-8">script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.2.6/ext-language_tools.js" type="text/javascript"
charset="utf-8">script>
<script>
$('.language').change(function () {
editor.session.setMode("ace/mode/" + $(this).val());
});
$('.theme').change(function () {
editor.setTheme("ace/theme/" + $(this).val());
});
//初始化对象
editor = ace.edit("code");
//设置风格和语言(更多风格和语言,请到github上相应目录查看)
theme = "xcode";
language = "c_cpp";
editor.setTheme("ace/theme/" + theme);
editor.session.setMode("ace/mode/" + language);
//字体大小
editor.setFontSize(18);
//设置只读(true时只读,用于展示代码)
editor.setReadOnly(false);
//自动换行,设置为off关闭
editor.setOption("wrap", "free");
//启用提示菜单
ace.require("ace/ext/language_tools");
editor.setOptions({
enableBasicAutocompletion: true,
enableSnippets: true,
enableLiveAutocompletion: true
});
script>
body>
html>