我正在使用Python编辑器,并希望添加一个功能,以便在CodeMirror textarea上插入符号的位置插入文本。
有一系列可以点击的图片。单击一个时,该图片的alt
属性将被保存,然后当您再次单击textarea时,它将被复制到您的鼠标位置(演示小提琴:https://jsfiddle.net/t0k7yp7n/1/)
以下是文本插入部分的脚本:
selected = '';
$('.insert').click(function() {
console.log($(this).attr('alt'));
selected = $(this).attr('alt');
});
$('#textbox').click(function() {
insertAtCaret('textbox', selected)
// Clear the selection so it isn't copied repeatedly
selected = '';
});
function insertAtCaret(areaId, text) {
var txtarea = document.getElementById(areaId);
var scrollPos = txtarea.scrollTop;
var strPos = 0;
var br = ((txtarea.selectionStart || txtarea.selectionStart == '0') ?
"ff" : (document.selection ? "ie" : false));
if (br == "ie") {
txtarea.focus();
var range = document.selection.createRange();
range.moveStart('character', -txtarea.value.length);
strPos = range.text.length;
} else if (br == "ff") strPos = txtarea.selectionStart;
var front = (txtarea.value).substring(0, strPos);
var back = (txtarea.value).substring(strPos, txtarea.value.length);
txtarea.value = front + text + back;
strPos = strPos + text.length;
if (br == "ie") {
txtarea.focus();
var range = document.selection.createRange();
range.moveStart('character', -txtarea.value.length);
range.moveStart('character', strPos);
range.moveEnd('character', 0);
range.select();
} else if (br == "ff") {
txtarea.selectionStart = strPos;
txtarea.selectionEnd = strPos;
txtarea.focus();
}
txtarea.scrollTop = scrollPos;
}
这是CodeMirror textarea部分:
var editor;
//<![CDATA[
window.onload = function() {
editor = CodeMirror.fromTextArea(document.getElementById('textbox'), {
mode: {
name: "python",
version: 2,
singleLineStringErrors: false
},
lineNumbers: true,
indentUnit: 4
});
} //]]>
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js" type="text/javascript"></script>
<script src="http://www.skulpt.org/static/skulpt.min.js" type="text/javascript"></script>
<script src="http://www.skulpt.org/static/skulpt-stdlib.js" type="text/javascript"></script>
<script src="https://www.cs.princeton.edu/~dp6/CodeMirror/lib/codemirror.js" type="text/javascript"></script>
<script src="https://www.cs.princeton.edu/~dp6/CodeMirror/mode/python/python.js" type="text/javascript"></script>
<script src="skulpt-codemirror.js" type="text/javascript"></script>
<script src="load-save-py.js" type="text/javascript"></script>
<script src="insert.js" type="text/javascript"></script>
<link href="https://www.cs.princeton.edu/~dp6/CodeMirror/lib/codemirror.css" rel="stylesheet" type="text/css">
<title>Python Editor</title>
</head>
<body>
Filename:
<input id="inputFileNameToSaveAs">
<button onclick="saveTextAsFile()">Save</button>
<br>
<input type="file" id="fileToLoad">
<button onclick="loadFileAsText()">Open</button>
<br>
<br>
<a href="#!">
<img class="insert" alt="#1">
<img class="insert" alt="#2">
<img class="insert" alt="#3">
</a>
<br>
<br>
<textarea id="textbox" name="textbox"></textarea>
<br>
<button onclick="runit()" type="button">Run</button>
<pre id="dynamicframe"></pre>
<div id="canvas"></div>
</body>
</html>
当我将它们放在一个文件中时,当我点击图片时,alt
不会复制到textarea。为什么这样,我该如何解决?
答案 0 :(得分:1)
使用CodeMirror时,您的<textarea />
将被CodeMirror提供的编辑器直观替换,并且您的<textarea />
相对于您的大部分代码将无法按原样使用。
背景上发生的事情是,您的实际<textarea />
将首先使用样式display: none;
进行标记。未显示,<textarea />
上没有绑定的事件将实际触发。然后,CodeMirror将实际将自己的代码添加到DOM,以在<textarea />
的位置显示一个新编辑器,该编辑器现在不显示。
例如,新初始化的CodeMirror编辑器的HTML代码,其中写有字符串“Hello World”,如下所示:
<div class="CodeMirror-lines">
<div style="position: relative; outline: none;">
<div class="CodeMirror-measure">
<div style="width: 50px; height: 50px; overflow-x: scroll;"></div>
</div>
<div class="CodeMirror-measure"></div>
<div style="position: relative; z-index: 1;"></div>
<div class="CodeMirror-cursors">
<div class="CodeMirror-cursor" style="left: 74px; top: 0px; height: 13px;"> </div>
</div>
<div class="CodeMirror-code">
<div style="position: relative;">
<div class="CodeMirror-gutter-wrapper" style="position: absolute; left: -29px;">
<div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;">1</div>
</div><pre><span style="padding-right: 0.1px;"><span class="cm-variable">Hello</span> <span class="cm-variable">World</span></span></pre></div>
</div>
</div>
</div>
您的<textarea />
已不再使用。
CodeMirror本身提供了一个编程API,可用于执行您想要的操作。基本上,所需的步骤是:
selected
文字是否可用(图片的alt
)。selected
文字(图片的alt
)。与这些步骤关联的JavaScript代码如下所示:
// Listen to the editor focus events.
editor.on('focus', function () {
// Only insert if a value has been previously selected.
if (selected.length > 0) {
// Fetch the current CodeMirror document.
var doc = editor.getDoc();
// Insert the text at the cursor position.
doc.replaceSelection(selected);
// Clear the selection so it isn't copied repeatedly.
selected = '';
}
});
您可以查看this JSFiddle上的工作示例。
答案 1 :(得分:1)
简而言之,该代码将用文本替换代码镜像的当前选择(或在没有任何选择的情况下,当前光标位置):
codemirror.doc.replaceSelection('text to replace');