Java小强个人技术博客站点    手机版
当前位置: 首页 >> 开源 >> 使用 CodeMirror 制作一个在线代码编辑器

使用 CodeMirror 制作一个在线代码编辑器

58261 开源 | 2021-4-5

CodeMirror 是一款“Online Source Editor”,基于Javascript,短小精悍,实时在线代码高亮显示,他不是某个富文本编辑器的附属产品,他是许多大名鼎鼎的在线代码编辑器的基础库。

QQ截图20210405115450.jpg


GIT代码

https://github.com/codemirror/CodeMirror 

官网

https://codemirror.net/ 


可以看出,CodeMirror的作者是一个十分向往自由的人。但他的CodeMirror绝对不简单,看看下面这份清单:

Google Earth KML sampler

Eloquent JavaScript's console

The qooxdoo playground

A cool tutorial about the element

An online IDE for the Orc programming language

Google's API playground

Raphaël Live

JS Bin

The RokPad plugin for Joomla

The scraperwiki editor

jsLinb UI Builder

上述的这些在线代码编辑器都是基于CodeMirror的,是不是感到惊讶,里面有你熟悉的JS Library。

CodeMirror本身的定位也很明确,短小精悍,但代码质量很高,在Google Group的群里面,人们热烈的进行着用CodeMirror做各式各样改造的讨论,可见对他的欢迎。以下有各种不同语言的Demo演示:

JavaScript

XML/HTML

CSS

SPARQL

HTML mixed-mode

HTML+PHP mixed-mode (courtesy of Yahoo!)

Python (by Timothy Farrell)

Lua (by Franciszek Wawrzak)

Ruby (by Michal Hantl, unfinished)

SQL (by John Benediktsson)

PLSQL (by Peter Raganitsch)

diff (courtesy of Liran Nuna)

Groovy (by eXo Platform)

C# (by Boris Gaber and Christopher Buchino)

OmetaJS (by Eric KEDJI)

Scheme (by Danny Yoo)


按照官网示例,我们增加对于JAVA代码的支持:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>codemirror代码编辑</title>
    <!-- codemirror代码编辑 -->
    <link rel="stylesheet" href="codemirror-5.52.0/lib/codemirror.css">
    <script src="codemirror-5.52.0/lib/codemirror.js"></script>
     <!-- html代码高亮显示 -->
     <script src="codemirror-5.52.0/mode/xml/xml.js"></script>
     <script src="codemirror-5.52.0/mode/css/css.js"></script>
     <script src="codemirror-5.52.0/mode/javascript/javascript.js"></script>
<!-- Java代码高亮显示 -->
<script src="codemirror-5.52.0/mode/clike/clike.js"></script>
     <!-- 引入css支持主题 -->
     <link rel="stylesheet" href="codemirror-5.52.0/theme/dracula.css">
<!--支持代码折叠-->
<link rel="stylesheet" href="codemirror-5.52.0/addon/fold/foldgutter.css"/>
<script src="codemirror-5.52.0/addon/fold/foldcode.js"></script>
<script src="codemirror-5.52.0/addon/fold/foldgutter.js"></script>
<script src="codemirror-5.52.0/addon/fold/brace-fold.js"></script>
<script src="codemirror-5.52.0/addon/fold/comment-fold.js"></script>
</head>
<body>
    <textarea name="code" id="code"></textarea>
    <script>
        //根据DOM元素的id构造出一个编辑器
        var editor = CodeMirror.fromTextArea(document.getElementById("code"), {
            mode: 'text/html',    //实现html代码高亮
mode: "text/x-java", //实现Java代码高亮
            lineNumbers: true,    //显示行号
            theme: "dracula",    //设置主题
            lineWrapping: true,    //代码折叠
            foldGutter: true,
            gutters: ["CodeMirror-linenumbers", "CodeMirror-foldgutter"],
            matchBrackets: true,    //括号匹配
            //readOnly: true,        //只读
        });
    </script>
</body>
</html>


效果如下:

QQ截图20210405115238 (1).jpg


END

推荐您阅读更多有关于“ js 开源 代码高亮 编辑器 CodeMirror 在线 ”的文章

上一篇:Eclipse安装lombok插件 下一篇:MyEclipse2020 定制工具栏和菜单

猜你喜欢

发表评论:

评论:

回复 Java小强 评论于 2021-04-05 11:56
如果需要查看更多属性,可以去官网查找,目前我只用到这些属性!

下面也列举些吧:

indentUnit: integer 缩进单位,值为空格数,默认为2 。

smartIndent: boolean 自动缩进,设置是否根据上下文自动缩进(和上一行相同的缩进量)。默认为true。

tabSize: integer tab字符的宽度,默认为4 。

indentWithTabs: boolean 在缩进时,是否需要把 n*tab宽度个空格替换成n个tab字符,默认为false 。

electricChars: boolean 在输入可能改变当前的缩进时,是否重新缩进,默认为true (仅在mode支持缩进时有效)。

specialChars: RegExp 需要被占位符(placeholder)替换的特殊字符的正则表达式。最常用的是非打印字符。默认为:/[\u0000-\u0019\u00ad\u200b-\u200f\u2028\u2029\ufeff]/。

specialCharPlaceholder: function(char) → Element 这是一个接收由specialChars选项指定的字符作为参数的函数,此函数会产生一个用来显示指定字符的DOM节点。默认情况下,显示一个红点(•),这个红点有一个带有前面特殊字符编码的提示框。

rtlMoveVisually: boolean Determines whether horizontal cursor movement through right-to-left (Arabic, Hebrew) text is visual (pressing the left arrow moves the cursor left) or logical (pressing the left arrow moves to the next lower index in the string, which is visually right in right-to-left text). The default is false on Windows, and true on other platforms.(这段完全不晓得搞啥子鬼)

keyMap: string 配置快捷键。默认值为default,即 codemorrir.js 内部定义。其它在key map目录下。

extraKeys: object 给编辑器绑定与前面keyMap配置不同的快捷键。

lineWrapping: boolean 在长行时文字是换行(wrap)还是滚动(scroll),默认为滚动(scroll)。

lineNumbers: boolean 是否在编辑器左侧显示行号。

firstLineNumber: integer 行号从哪个数开始计数,默认为1 。

lineNumberFormatter: function(line: integer) → string 使用一个函数设置行号。

gutters: array 用来添加额外的gutter(在行号gutter前或代替行号gutter)。值应该是CSS名称数组,每一项定义了用于绘制gutter背景的宽度(还有可选的背景)。为了能明确设置行号gutter的位置(默认在所有其它gutter的右边),也可以包含CodeMirror-linenumbers类。类名是用于传给setGutterMarker的键名(keys)。

fixedGutter: boolean 设置gutter跟随编辑器内容水平滚动(false)还是固定在左侧(true或默认)。

scrollbarStyle: string 设置滚动条。默认为”native”,显示原生的滚动条。核心库还提供了”null”样式,此样式会完全隐藏滚动条。Addons可以设置更多的滚动条模式。

coverGutterNextToScrollbar: boolean 当fixedGutter启用,并且存在水平滚动条时,在滚动条最左侧默认会显示gutter,当此项设置为true时,gutter会被带有CodeMirror-gutter-filler类的元素遮挡。 inputStyle: string 选择CodeMirror处理输入和焦点的方式。核心库定义了textarea和contenteditable输入模式。在移动浏览器上,默认是contenteditable,在桌面浏览器上,默认是textarea。在contenteditable模式下对IME和屏幕阅读器支持更好。

readOnly: boolean|string 编辑器是否只读。如果设置为预设的值 “nocursor”,那么除了设置只读外,编辑区域还不能获得焦点。

showCursorWhenSelecting: boolean 在选择时是否显示光标,默认为false。

lineWiseCopyCut: boolean 启用时,如果在复制或剪切时没有选择文本,那么就会自动操作光标所在的整行。

undoDepth: integer 最大撤消次数,默认为200(包括选中内容改变事件) 。

historyEventDelay: integer 在输入或删除时引发历史事件前的毫秒数。

tabindex: integer 编辑器的tabindex。

autofocus: boolean 是否在初始化时自动获取焦点。默认情况是关闭的。但是,在使用textarea并且没有明确指定值的时候会被自动设置为true。

dragDrop: boolean 是否允许拖放,默认为true。

allowDropFileTypes: array 默认为null。当设置此项时,只接收包含在此数组内的文件类型拖入编辑器。文件类型为MIME名称。

cursorBlinkRate: number 光标闪动的间隔,单位为毫秒。默认为530。当设置为0时,会禁用光标闪动。负数会隐藏光标。

cursorScrollMargin: number 当光标靠近可视区域边界时,光标距离上方和下方的距离。默认为0 。

cursorHeight: number 光标高度。默认为1,也就是撑满行高。对一些字体,设置0.85看起来会更好。

resetSelectionOnContextMenu: boolean 设置在选择文本外点击打开上下文菜单时,是否将光标移动到点击处。默认为true。

workTime, workDelay: number 通过一个假的后台线程高亮 workTime 时长,然后使用 timeout 休息 workDelay 时长。默认为200和300 。(完全不懂这个功能是在说啥)

pollInterval: number 指明CodeMirror向对应的textarea滚动(写数据)的速度(获得焦点时)。大多数的输入都是通过事件捕获,但是有的输入法(如IME)在某些浏览器上并不会生成事件,所以使用数据滚动。默认为100毫秒。

flattenSpans: boolean 默认情况下,CodeMirror会将使用相同class的两个span合并成一个。通过设置此项为false禁用此功能。

addModeClass: boolean 当启用时(默认禁用),会给每个标记添加额外的表示生成标记的mode的以cm-m开头的CSS样式类。例如,XML mode产生的标记,会添加cm-m-xml类。

maxHighlightLength: number 当需要高亮很长的行时,为了保持响应性能,当到达某些位置时,编辑器会直接将其他行设置为纯文本(plain text)。默认为10000,可以设置为Infinity来关闭此功能。

viewportMargin: integer 指定当前滚动到视图中内容上方和下方要渲染的行数。这会影响到滚动时要更新的行数。通常情况下应该使用默认值10。可以设置值为Infinity始终渲染整个文档。注意:这样设置在处理大文档时会影响性能。