文本编辑器如何翻译(例如)->
到latex符号rightarrow
?
看看自动编辑的好功能
public class MyDslDefaultAutoEditStrategyProvider extends DefaultAutoEditStrategyProvider {
@Inject
protected Provider<ShortCutEditStrategy> shortCut;
@Override
protected void configureStringLiteral(IEditStrategyAcceptor acceptor) {
acceptor.accept(shortCut.get().configure("->", "\rightarrow"), IDocument.DEFAULT_CONTENT_TYPE);
}
}
,不要忘记绑定
override Class<? extends AbstractEditStrategyProvider> bindAbstractEditStrategyProvider() {
MyDslDefaultAutoEditStrategyProvider
}
使用JavaScript
很容易。这个简单的脚本应该有帮助:
replaceText('->', 'rightarrow');
var replaceText = function (search, replaceWith) {
var area = document.getElementById("area");
var getCount = function (str, search) {
return str.split(search).length - 1;
};
if (area.value.indexOf(search) >= 0) {
var start = area.selectionStart;
var end = area.selectionEnd;
var textBefore = area.value.substr(0, end);
var lengthDiff = (replaceWith.length - search.length) * getCount(textBefore, search);
area.value = area.value.replace(search, replaceWith);
area.selectionStart = start + lengthDiff;
area.selectionEnd = end + lengthDiff;
}
};
这篇全文可以很容易地适应你的使用-这里有一个完整的例子。
<textarea id="area"></textarea>
<script>
function keyDownTextField() {
replaceText('->', 'u2192');
}
var replaceText = function (search, replaceWith) {
var area = document.getElementById("area");
var getCount = function (str, search) {
return str.split(search).length - 1;
};
if (area.value.indexOf(search) >= 0) {
var start = area.selectionStart;
var end = area.selectionEnd;
var textBefore = area.value.substr(0, end);
var lengthDiff = (replaceWith.length - search.length) * getCount(textBefore, search);
area.value = area.value.replace(search, replaceWith);
area.selectionStart = start + lengthDiff;
area.selectionEnd = end + lengthDiff;
}
};
document.addEventListener("keydown", keyDownTextField, false);
</script>