gpt4 book ai didi

ace-editor - 想要在 Ace Editor 中突出显示/更改某些单词的颜色?

转载 作者:行者123 更新时间:2023-12-03 16:15:08 26 4
gpt4 key购买 nike

我正在使用以下内容设置文本:

var someString = 'Mark has solved the problem';
editor.getSession().setValue(someString);

在上述情况下,只需要“标记”以蓝色显示。
有没有办法在 ace 编辑器中加载 html 标签或操纵某些单词的样式?

最佳答案

您可以创建自定义突出显示规则来执行此操作

<!DOCTYPE html>
<html lang="en">
<head>
<title>ACE in Action</title>
<meta charset="utf-8">
<style type="text/css" media="screen">
#editor {
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
}
.ace_customTokenName {color: darkred}
</style>
</head>
<body>
<div id="editor" style="height: 500px; width: 800px"></div>

<script src="https://ajaxorg.github.io/ace-builds/src/ace.js"></script>
<script>
define('ace/mode/custom', [], function(require, exports, module) {
var oop = require("ace/lib/oop");
var TextMode = require("ace/mode/text").Mode;
var Tokenizer = require("ace/tokenizer").Tokenizer;
var CustomHighlightRules = require("ace/mode/custom_highlight_rules").CustomHighlightRules;

var Mode = function() {
this.HighlightRules = CustomHighlightRules;
};
oop.inherits(Mode, TextMode);

(function() {

}).call(Mode.prototype);

exports.Mode = Mode;
});

define('ace/mode/custom_highlight_rules', [], function(require, exports, module) {
var oop = require("ace/lib/oop");
var TextHighlightRules = require("ace/mode/text_highlight_rules").TextHighlightRules;

var CustomHighlightRules = function() {

var keywordMapper = this.createKeywordMapper({
"variable.language": "this",
"keyword": "Mark|Ben|Bill",
"constant.language": "true|false|null",
// it is also possible to use css, but that may conflict with themes
"customTokenName": "problem"
}, "text", true);

this.$rules = {
"start": [
{
regex: "\\w+\\b",
token: keywordMapper
},
]
};
this.normalizeRules()
};

oop.inherits(CustomHighlightRules, TextHighlightRules);

exports.CustomHighlightRules = CustomHighlightRules;
});


var editor = ace.edit("editor");

editor.session.setMode("ace/mode/custom");
var someString = 'Mark has solved the problem';
editor.session.setValue(someString);

</script>
</body>
</html>

关于ace-editor - 想要在 Ace Editor 中突出显示/更改某些单词的颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43735711/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com