当前位置:首页 > 科技  > 软件

强大的代码编辑器组件,你知道几个?

来源: 责编: 时间:2024-04-03 09:10:41 297观看
导读上次介绍了一个简单的示例,基于javascript如何从一段文本解析为一段代码,今天我们看下一下个功能类似,但非常强大的代码编辑器组件 CodeMirrorCodeMirror https://codemirror.net/在Web开发中,我们经常需要在网页上展示代

上次介绍了一个简单的示例,基于javascript如何从一段文本解析为一段代码,今天我们看下一下个功能类似,但非常强大的代码编辑器组件 GN228资讯网——每日最新资讯28at.com

CodeMirror

CodeMirror https://codemirror.net/GN228资讯网——每日最新资讯28at.com

在Web开发中,我们经常需要在网页上展示代码,或者让用户直接在网页上编写代码。为了提高用户体验,我们通常会使用代码编辑器来实现代码的高亮、自动补全等功能。而在众多代码编辑器中,CodeMirror无疑是一个功能强大、易用的选项。本文将带您深入了解CodeMirror的基本用法和高级特性,让您能够轻松地在网页中实现代码编辑与高亮。GN228资讯网——每日最新资讯28at.com

什么是CodeMirror?

CodeMirror是一个基于JavaScript的代码编辑器,它可以在网页中实现代码的高亮、自动补全等功能。CodeMirror支持多种编程语言,如JavaScript、HTML、CSS、Python等,并且可以通过插件扩展支持更多的语言。CodeMirror的使用非常简单,只需要引入相应的库文件,然后通过简单的配置即可实现代码编辑与高亮。GN228资讯网——每日最新资讯28at.com

如何使用CodeMirror?

基于webpack

  1. 初始化项目
npm init -y
  1. 引入库文件
npm i codemirror @codemirror/lang-javascript npm i -D webpack webpack-cli webpack-dev-server npm i -D css-loader scss scss-loader style-loader html-loader html-webpack-plugin
  1. 创建编辑器
<div id="app"></div>
  1. 初始化Codemirror
const state = EditorState.create({    doc: 'console.log("hello codemirror")',    extensions: [        basicSetup,        javascript(),        // 其他扩展,包括主题、语法高亮...    ],});const editor = new EditorView({    state,    parent: document.getElementById("editor")})
  1. 配置webpack
const path = require('path');const webpack = require('webpack');const HtmlWebpackPlugin = require('html-webpack-plugin');module.exports = {  entry: './src/js/index.js',  output: {    filename: 'bundle.js',    path: path.resolve(__dirname, 'dist'),  },  devServer: {    static: {      directory: path.join(__dirname, 'dist'),      watch: true,    },    compress: true,    port: 9000,    hot: true, // 启用HMR  },  plugins: [    new HtmlWebpackPlugin({      template: './src/index.html',    }),    new webpack.HotModuleReplacementPlugin(),  ],  optimization: {    minimize: false, // 关闭代码压缩  },  module: {    rules: [      {        test: //.scss$/, // 正则表达式,匹配所有 .scss 文件        use: [          'style-loader', // 将 JS 字符串生成为 style 节点          'css-loader', // 将 CSS 转化成 CommonJS 模块          'sass-loader' // 将 Sass 编译成 CSS,需要 npm 安装 sass-loader 和 sass        ]      },      {        test: //.css$/i,        use: ['style-loader', 'css-loader'],      },      {        test: //.(png|svg|jpg|jpeg|gif)$/i,        type: 'asset/resource',      },      {        test: //.html$/i,        loader: 'html-loader',      },    ],  },  mode: 'development',};
  1. 启动
{    "scripts": {      "start": "webpack serve --open",      "build": "webpack --mode=dependencies"    },}
npm start

CodeMirror的高级特性

  1. 自定义主题

CodeMirror允许我们自定义主题,以实现个性化的代码高亮效果。可以通过以下方式设置自定义主题:GN228资讯网——每日最新资讯28at.com

import {EditorView} from "@codemirror/view"let myTheme = EditorView.theme({  "&": {    color: "white",    backgroundColor: "#034"  },  ".cm-content": {    caretColor: "#0e9"  },  "&.cm-focused .cm-cursor": {    borderLeftColor: "#0e9"  },  "&.cm-focused .cm-selectionBackground, ::selection": {    backgroundColor: "#074"  },  ".cm-gutters": {    backgroundColor: "#045",    color: "#ddd",    border: "none"  }}, {dark: true})
  1. 自定义语言模式

除了内置的语言模式外,我们还可以自定义语言模式。首先需要引入相应的模式文件,然后通过以下方式设置自定义语言模式:模板是基于lezer解析,syntax.grammar,基于lezer的语法解析器,可以自定义语法规则,实现自定义语言模式,像之前那样逐个对字符处理。GN228资讯网——每日最新资讯28at.com

@top Program { expression* }@skip { space | LineComment }expression {  Identifier |  String |  Boolean |  Application { "(" expression* ")" }}@tokens {  Identifier { $[a-zA-Z_/-0-9]+ }  String { '"' (!["//] | "//" _)* '"' }  Boolean { "#t" | "#f" }  LineComment { ";" ![/n]* }  space { $[ /t/n/r]+ }  "(" ")"}@detectDelim
import {parser} from "./syntax.grammar"import {LRLanguage, LanguageSupport, indentNodeProp, foldNodeProp, foldInside, delimitedIndent} from "@codemirror/language"import {styleTags, tags as t} from "@lezer/highlight"export const EXAMPLELanguage = LRLanguage.define({  parser: parser.configure({    props: [      indentNodeProp.add({        Application: delimitedIndent({closing: ")", align: false})      }),      foldNodeProp.add({        Application: foldInside      }),      styleTags({        Identifier: t.variableName,        Boolean: t.bool,        String: t.string,        LineComment: t.lineComment,        "( )": t.paren      })    ]  }),  languageData: {    commentTokens: {line: ";"}  }})export function EXAMPLE() {  return new LanguageSupport(EXAMPLELanguage)}
  1. 语法高亮

可以根据标签类型设置不同标签的颜色与其他样式,可以根据内置的tag类型定义不同的显示风格:GN228资讯网——每日最新资讯28at.com

import {tags} from "@lezer/highlight"import {HighlightStyle} from "@codemirror/language"const myHighlightStyle = HighlightStyle.define([  {tag: tags.keyword, color: "#fc6"},  {tag: tags.comment, color: "#f5d", fontStyle: "italic"}])

codemirror的扩展点很多,类似于Visual Studio Code的Web版本都可以基于该框架试下,在官网上罗列了具体的:GN228资讯网——每日最新资讯28at.com

Accessibility

Works well with screen readers and keyboard-only users.GN228资讯网——每日最新资讯28at.com

Mobile Support

Use the platform's native selection and editing features on phones.GN228资讯网——每日最新资讯28at.com

Bidirectional Text

Support mixing of right-to-left and left-to-right text.GN228资讯网——每日最新资讯28at.com

Syntax Highlighting

Color code to reflect syntactic structure.GN228资讯网——每日最新资讯28at.com

Line Numbers

Display gutters with line numbers or other information next to the code.GN228资讯网——每日最新资讯28at.com

Autocompletion

Provide language-specific completion hints in the editor.GN228资讯网——每日最新资讯28at.com

Code Folding

Temporarily hide parts of the document.GN228资讯网——每日最新资讯28at.com

Search/Replace

Editor-specific search, regexp search, and replace functionality.GN228资讯网——每日最新资讯28at.com

Full Parsing

Detailed parse trees allow many types of language integration.GN228资讯网——每日最新资讯28at.com

Extension Interface

Robustly implement demanding editor extensions.GN228资讯网——每日最新资讯28at.com

Modularity

Most features are implemented on top of a generic public API.GN228资讯网——每日最新资讯28at.com

Speed

Remains responsive even on huge documents and long lines.GN228资讯网——每日最新资讯28at.com

Bracket Closing

Automatically insert matching brackets during typing.GN228资讯网——每日最新资讯28at.com

Linting

Show error and warning messages in the editor.GN228资讯网——每日最新资讯28at.com

Flexible Styling

Mix font styles and sizes, add widgets in the content.GN228资讯网——每日最新资讯28at.com

Theming

Import or create custom visual editor styles.GN228资讯网——每日最新资讯28at.com

Collaborative Editing

Allow multiple users to edit the same document.GN228资讯网——每日最新资讯28at.com

Undo History

Undo and redo functionality with collab editing support.GN228资讯网——每日最新资讯28at.com

Multiple Selections

Select and edit multiple ranges of the document at once.GN228资讯网——每日最新资讯28at.com

Internationalization

Provide custom text to display or announce to the user.GN228资讯网——每日最新资讯28at.com

...and more

Find a full description of the library's features in the docs.GN228资讯网——每日最新资讯28at.com

结束语

以上就是关于CodeMirror的简单介绍和基本用法。通过本文,您应该已经了解了如何在网页中使用CodeMirror实现代码编辑与高亮。当然,CodeMirror还有很多高级特性等待您去发掘。希望本文能帮助您更好地使用CodeMirror,为您的Web开发带来更多便利。GN228资讯网——每日最新资讯28at.com

本文链接:http://www.28at.com/showinfo-26-81062-0.html强大的代码编辑器组件,你知道几个?

声明:本网页内容旨在传播知识,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。邮件:2376512515@qq.com

上一篇: C#中Dictionary与ConcurrentDictionary解锁多线程操作安全之道

下一篇: 使用Kafka构建实时音乐排行榜系统,你学会了吗?

标签:
  • 热门焦点
  • 6月iOS设备性能榜:M2稳居榜首 A系列只能等一手3nm来救

    没有新品发布,自然iOS设备性能榜的上榜设备就没有什么更替,仅仅只有跑分变化而产生的排名变动,毕竟苹果新品的发布节奏就是这样的,一年下来也就几个移动端新品,不会像安卓厂商,一
  • 2023 年的 Node.js 生态系统

    随着技术的不断演进和创新,Node.js 在 2023 年达到了一个新的高度。Node.js 拥有一个庞大的生态系统,可以帮助开发人员更快地实现复杂的应用。本文就来看看 Node.js 最新的生
  • 企业采用CRM系统的11个好处

    客户关系管理(CRM)软件可以为企业提供很多的好处,从客户保留到提高生产力。  CRM软件用于企业收集客户互动,以改善客户体验和满意度。  CRM软件市场规模如今超过580
  • 深度探索 Elasticsearch 8.X:function_score 参数解读与实战案例分析

    在 Elasticsearch 中,function_score 可以让我们在查询的同时对搜索结果进行自定义评分。function_score 提供了一系列的参数和函数让我们可以根据需求灵活地进行设置。近期
  • 虚拟键盘 API 的妙用

    你是否在遇到过这样的问题:移动设备上有一个固定元素,当激活虚拟键盘时,该元素被隐藏在了键盘下方?多年来,这一直是 Web 上的默认行为,在本文中,我们将探讨这个问题、为什么会发生
  • 一条抖音4亿人围观 ! 这家MCN比无忧传媒还野

    作者:Hiu 来源:互联网品牌官01 擦边少女空降热搜,幕后推手曝光被网友誉为&ldquo;纯欲天花板&rdquo;的女网红井川里予,近期因为一组哥特风照片登上热搜,引发了一场互联网世界关于
  • 华为发布HarmonyOS 4:更好玩、更流畅、更安全

    在8月4日的华为开发者大会2023(HDC.Together)大会上,HarmonyOS 4正式发布。自2019年发布以来,HarmonyOS一直以用户为中心,经历四年多的发展HarmonyOS已
  • 三星显示已开始为AR设备研发硅基LED微显示屏

    7月18日消息,据外媒报道,随着苹果首款头显产品Vision Pro在6月份正式推出,AR/VR/MR等头显产品也就将成为各大公司下一个重要的竞争领域,对显示屏这一关
  • iQOO Neo8系列或定档5月23日:首发天玑9200+ 安卓跑分王者

    去年10月,iQOO推出了iQOO Neo7系列机型,不仅搭载了天玑9000+,而且是同价位唯一一款天玑9000+直屏旗舰,一经上市便受到了用户的广泛关注。在时隔半年后,
Top