可以即时进行语法突出显示的文本区域?

发布于 2024-08-08 08:37:57 字数 203 浏览 10 评论 0原文

出于更容易维护的原因,我在 CMS 中存储了许多 HTML 块。它们由

有谁知道某种 JavaScript Widget 可以在 textarea 或类似内容中对 HTML 进行语法突出显示,同时仍然保留纯文本编辑器(没有所见即所得或高级功能)?

I am storing a number of HTML blocks inside a CMS for reasons of easier maintenance. They are represented by <textarea>s.

Does anybody know a JavaScript Widget of some sort that can do syntax highlighting for HTML within a textarea or similar, while still staying a plain text editor (no WYSIWYG or advanced functions)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

左岸枫 2024-08-15 08:37:57

无法在常规 中实现对演示文稿的所需控制级别

如果您同意,请尝试 CodeMirrorAceMonaco (在 VS Code 中使用) 。

请参阅 Wikipedia 上的基于 JavaScript 的源代码编辑器的比较

It's not possible to achieve the required level of control over presentation in a regular <textarea>.

If you're OK with that, try CodeMirror or Ace or Monaco (used in VS Code).

See Comparison of JavaScript-based source code editors on Wikipedia.

蓝咒 2024-08-15 08:37:57

这是我对类似问题的回答 (在线代码编辑器)对于程序员

首先,你可以看一下到这篇文章:
维基百科 ― 基于 JavaScript 的源代码编辑器的比较

如需了解更多信息,这里有一些似乎符合您要求的工具:

  • EditArea作为 FileEditor 演示 谁是一个 Yii 扩展 ― (Apache 软件许可证、BSD、LGPL

    <块引用>

    这是 EditArea,一个免费的 JavaScript 源代码编辑器。它允许编写格式良好的源代码,包括行编号、选项卡支持、搜索和搜索。替换(使用正则表达式)和实时语法突出显示(可自定义)。

  • CodePressJoomla 演示! CodePress 插件 ― (LGPL) ― 它在 Chrome 中不起作用,看起来开发已经停止。

    <块引用>

    CodePress 是基于 Web 的源代码编辑器,具有用 JavaScript 编写的语法突出显示功能,可在浏览器中键入文本时实时为其着色。

  • CodeMirror众多演示之一 ― (MIT 风格< /a> 许可证 + 可选商业支持

    <块引用>

    CodeMirror 是一个 JavaScript 库,可用于为类似代码的内容(计算机程序、HTML 标记等)创建相对令人愉悦的编辑器界面。如果已为您正在编辑的语言编写了模式,则代码将被着色,并且编辑器将选择性地帮助您缩进

  • Ace Ajax.org Cloud9 编辑器演示 ― (Mozilla 三重许可证 (MPL/GPL/LGPL))

    <块引用>

    Ace 是一个用 JavaScript 编写的独立代码编辑器。我们的目标是创建一个基于 Web 的代码编辑器,匹配并扩展现有本机编辑器(例如 TextMate、Vim 或 Eclipse)的功能、可用性和性能。它可以轻松嵌入到任何网页和 JavaScript 应用程序中。 Ace 是作为 Cloud9 IDE 的主要编辑器开发的,也是 Mozilla Skywriter (Bespin) 项目的后继者。

Here is the response I've done to a similar question (Online Code Editor) on programmers:

First, you can take a look to this article:
Wikipedia ― Comparison of JavaScript-based source code editors.

For more, here is some tools that seem to fit with your request:

  • EditAreaDemo as FileEditor who is a Yii Extension ― (Apache Software License, BSD, LGPL)

    Here is EditArea, a free javascript editor for source code. It allow to write well formated source code with line numerotation, tab support, search & replace (with regexp) and live syntax highlighting (customizable).

  • CodePressDemo of Joomla! CodePress Plugin ― (LGPL) ― It doesn't work in Chrome and it looks like development has ceased.

    CodePress is web-based source code editor with syntax highlighting written in JavaScript that colors text in real time while it's being typed in the browser.

  • CodeMirrorOne of the many demo ― (MIT-style license + optional commercial support)

    CodeMirror is a JavaScript library that can be used to create a relatively pleasant editor interface for code-like content ― computer programs, HTML markup, and similar. If a mode has been written for the language you are editing, the code will be coloured, and the editor will optionally help you with indentation

  • Ace Ajax.org Cloud9 EditorDemo ― (Mozilla tri-license (MPL/GPL/LGPL))

    Ace is a standalone code editor written in JavaScript. Our goal is to create a web based code editor that matches and extends the features, usability and performance of existing native editors such as TextMate, Vim or Eclipse. It can be easily embedded in any web page and JavaScript application. Ace is developed as the primary editor for Cloud9 IDE and the successor of the Mozilla Skywriter (Bespin) Project.

小傻瓜 2024-08-15 08:37:57

CodePress 执行此操作,编辑区域。两者都是开源的。

CodePress does this, as does EditArea. Both are open source.

勿挽旧人 2024-08-15 08:37:57

您可以使用小心放置在

查看突出显示文本区域内的文本

You can Highlight text in a <textarea>, using a <div> carefully placed behind it.

check out Highlight Text Inside a Textarea.

ま昔日黯然 2024-08-15 08:37:57

我建议使用 EditArea 来实时编辑语法突出显示的文本区域。

I would recommend EditArea for live editing of a syntax hightlighted textarea.

香草可樂 2024-08-15 08:37:57

您实际上无法在文本区域内呈现标记。

但是,您可以通过小心地将 div 放置在文本区域后面并在其中添加突出显示标记来伪造它。

JavaScript 负责同步内容和滚动位置。

var $container = $('.container');
var $backdrop = $('.backdrop');
var $highlights = $('.highlights');
var $textarea = $('textarea');
var $toggle = $('button');


var ua = window.navigator.userAgent.toLowerCase();
var isIE = !!ua.match(/msie|trident\/7|edge/);
var isWinPhone = ua.indexOf('windows phone') !== -1;
var isIOS = !isWinPhone && !!ua.match(/ipad|iphone|ipod/);

function applyHighlights(text) {
  text = text
    .replace(/\n$/g, '\n\n')
    .replace(/[A-Z].*?\b/g, '<mark>
amp;</mark>');

  if (isIE) {
    // IE wraps whitespace differently in a div vs textarea, this fixes it
    text = text.replace(/ /g, ' <wbr>');
  }

  return text;
}

function handleInput() {
  var text = $textarea.val();
  var highlightedText = applyHighlights(text);
  $highlights.html(highlightedText);
}

function handleScroll() {
  var scrollTop = $textarea.scrollTop();
  $backdrop.scrollTop(scrollTop);

  var scrollLeft = $textarea.scrollLeft();
  $backdrop.scrollLeft(scrollLeft);
}

function fixIOS() {
  $highlights.css({
    'padding-left': '+=3px',
    'padding-right': '+=3px'
  });
}

function bindEvents() {
  $textarea.on({
    'input': handleInput,
    'scroll': handleScroll
  });
}

if (isIOS) {
  fixIOS();
}

bindEvents();
handleInput();
@import url(https://fonts.googleapis.com/css?family=Open+Sans);
*,
*::before,
*::after {
  box-sizing: border-box;
}

body {
  margin: 30px;
  background-color: #fff;
  caret-color: #000;
}

.container,
.backdrop,
textarea {
  width: 460px;
  height: 180px;
}

.highlights,
textarea {
  padding: 10px;
  font: 20px/28px 'Open Sans', sans-serif;
  letter-spacing: 1px;
}

.container {
  display: block;
  margin: 0 auto;
  transform: translateZ(0);
  -webkit-text-size-adjust: none;
}

.backdrop {
  position: absolute;
  z-index: 1;
  border: 2px solid #685972;
  background-color: #fff;
  overflow: auto;
  pointer-events: none;
  transition: transform 1s;
}

.highlights {
  white-space: pre-wrap;
  word-wrap: break-word;
  color: #000;
}

textarea {
  display: block;
  position: absolute;
  z-index: 2;
  margin: 0;
  border: 2px solid #74637f;
  border-radius: 0;
  color: transparent;
  background-color: transparent;
  overflow: auto;
  resize: none;
  transition: transform 1s;
}

mark {
  border-radius: 3px;
  color: red;
  background-color: transparent;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="container">
  <div class="backdrop">
    <div class="highlights"></div>
  </div>
  <textarea>All capitalized Words will be highlighted. Try Typing to see how it Works</textarea>
</div>

原始笔:https://codepen.io/lonekorean/pen/gaLEMR

You can't actually render markup inside a textarea.

But, you can fake it by carefully positioning a div behind the textarea and adding your highlight markup there.

JavaScript takes care of syncing the content and scroll position.

var $container = $('.container');
var $backdrop = $('.backdrop');
var $highlights = $('.highlights');
var $textarea = $('textarea');
var $toggle = $('button');


var ua = window.navigator.userAgent.toLowerCase();
var isIE = !!ua.match(/msie|trident\/7|edge/);
var isWinPhone = ua.indexOf('windows phone') !== -1;
var isIOS = !isWinPhone && !!ua.match(/ipad|iphone|ipod/);

function applyHighlights(text) {
  text = text
    .replace(/\n$/g, '\n\n')
    .replace(/[A-Z].*?\b/g, '<mark>
amp;</mark>');

  if (isIE) {
    // IE wraps whitespace differently in a div vs textarea, this fixes it
    text = text.replace(/ /g, ' <wbr>');
  }

  return text;
}

function handleInput() {
  var text = $textarea.val();
  var highlightedText = applyHighlights(text);
  $highlights.html(highlightedText);
}

function handleScroll() {
  var scrollTop = $textarea.scrollTop();
  $backdrop.scrollTop(scrollTop);

  var scrollLeft = $textarea.scrollLeft();
  $backdrop.scrollLeft(scrollLeft);
}

function fixIOS() {
  $highlights.css({
    'padding-left': '+=3px',
    'padding-right': '+=3px'
  });
}

function bindEvents() {
  $textarea.on({
    'input': handleInput,
    'scroll': handleScroll
  });
}

if (isIOS) {
  fixIOS();
}

bindEvents();
handleInput();
@import url(https://fonts.googleapis.com/css?family=Open+Sans);
*,
*::before,
*::after {
  box-sizing: border-box;
}

body {
  margin: 30px;
  background-color: #fff;
  caret-color: #000;
}

.container,
.backdrop,
textarea {
  width: 460px;
  height: 180px;
}

.highlights,
textarea {
  padding: 10px;
  font: 20px/28px 'Open Sans', sans-serif;
  letter-spacing: 1px;
}

.container {
  display: block;
  margin: 0 auto;
  transform: translateZ(0);
  -webkit-text-size-adjust: none;
}

.backdrop {
  position: absolute;
  z-index: 1;
  border: 2px solid #685972;
  background-color: #fff;
  overflow: auto;
  pointer-events: none;
  transition: transform 1s;
}

.highlights {
  white-space: pre-wrap;
  word-wrap: break-word;
  color: #000;
}

textarea {
  display: block;
  position: absolute;
  z-index: 2;
  margin: 0;
  border: 2px solid #74637f;
  border-radius: 0;
  color: transparent;
  background-color: transparent;
  overflow: auto;
  resize: none;
  transition: transform 1s;
}

mark {
  border-radius: 3px;
  color: red;
  background-color: transparent;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="container">
  <div class="backdrop">
    <div class="highlights"></div>
  </div>
  <textarea>All capitalized Words will be highlighted. Try Typing to see how it Works</textarea>
</div>

Original Pen: https://codepen.io/lonekorean/pen/gaLEMR

桃扇骨 2024-08-15 08:37:57

更新: Bespin 现在是 ACE,此处评分最高的答案提到了这一点。请改用 ACE。

必须使用 Mozilla 的 Bespin。它是使用 HTML5 功能构建的(因此速度很快,但不支持旧版浏览器),但使用起来绝对令人惊叹,并且击败了我遇到的所有内容 - 可能是因为它有 Mozilla 支持,而且他们开发了 Firefox,所以是的。 .. 还有一个 jQuery 插件,其中包含一个扩展使其更容易与 jQuery 一起使用。

Update: Bespin is now ACE, which is mentioned by the highest rated answer here. Use ACE instead.

Gotta go with Bespin by Mozilla. It's built using HTML5 features (so it's quick and fast, but doesn't support legacy browsers though), but definitely amazing to use and beats everything I've come across - probably beacause it's Mozilla backing it, and they develop Firefox so yeah... There's also a jQuery Plugin which contains a extension for it to make it a bit easier to use with jQuery.

泪眸﹌ 2024-08-15 08:37:57

总之,我们可以使用这些:

为了保存您的时间和精力,在这个范围内进一步研究是有好处的。

In summary, these are the one we can go with:

In order to save your time and energy, it is good to investigate further within these scope.

伴梦长久 2024-08-15 08:37:57

据我所知,唯一具有语法突出显示和文本区域回退功能的编辑器是 Mozilla Bespin。谷歌搜索嵌入 Bespin 以了解如何嵌入编辑器。据我所知,目前唯一使用此功能的网站是 alpha Mozilla Jetpack Gallery(位于提交 Jetpack 页面),您可能想看看他们如何包含它。

还有一篇关于嵌入和重用 Bespin 编辑器的博客文章 这可能对你有帮助。

The only editor I know of that has syntax highlighting and a fallback to a textarea is Mozilla Bespin. Google around for embedding Bespin to see how to embed the editor. The only site I know of that uses this right now is the very alpha Mozilla Jetpack Gallery (in the submit a Jetpack page) and you may want to see how they include it.

There's also a blog post on embedding and reusing the Bespin editor that may help you.

美人骨 2024-08-15 08:37:57

为什么将它们表示为文本区域?这是我最喜欢的:

http://alexgorbatchev.com/wiki/SyntaxHighlighter

但是如果您使用的是CMS,可能有更好的插件。例如,wordpress 有一个进化版本:

http://www.viper007bond.com/wordpress-插件/syntaxhighlighter/

Why are you representing them as textareas? This is my favorite:

http://alexgorbatchev.com/wiki/SyntaxHighlighter

But if you are using a CMS, there's probably a better plugin. For example, wordpress has an evolved version:

http://www.viper007bond.com/wordpress-plugins/syntaxhighlighter/

Spring初心 2024-08-15 08:37:57

这可以通过在文本区域前面添加一个覆盖 code 块来实现,并使用它来应用语法突出显示,正如其他几个人提到的那样。

然而,有一些边缘情况需要处理。
本文详细介绍了它们:
https://css-tricks.com/ create-an-editable-textarea-that-supports-syntax-highlighted-code

值得庆幸的是,作者已将其创建为自定义元素
https://github.com/WebCoder49/code-input

使用示例
https://codepen.io/WebCoder49/pen/jOypJOx

可编辑内容演示

This is doable by adding an overlay code block infront of textarea, and use that to apply syntax highlighting as mentioned by couple of others.

However there are some edge cases to take care of.
This article goes in detail about them:
https://css-tricks.com/creating-an-editable-textarea-that-supports-syntax-highlighted-code

Thankfully, author has created it as a custom element
https://github.com/WebCoder49/code-input

Usage example
https://codepen.io/WebCoder49/pen/jOypJOx

demo of content editable

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文