将 CodeMirror 添加到 Skulpt 文本区域

Add CodeMirror to Skulpt Textarea

我正在制作在线 python 编辑器。我正在使用 Skulpt 来 运行 用户输入到 texarea 的代码。这是我的代码:

HTML:

<script src="skulpt.min.js" type="text/javascript"></script> 
<script src="skulpt-stdlib.js" type="text/javascript"></script> 
<textarea id="textbox" cols="50" rows="10"></textarea>
<br /> 
<button type="button" onclick="runit()">Run</button> 
<pre id="dynamicframe"></pre>
<div id="canvas"></div>

Javascript:

function builtinRead(x) {
    if (Sk.builtinFiles === undefined || Sk.builtinFiles["files"][x] === undefined)
        throw "File not found: '" + x + "'";
    return Sk.builtinFiles["files"][x];
}

function runit() {
    var prog = document.getElementById("textbox").value;
    var mypre = document.getElementById("dynamicframe");
    mypre.innerHTML = '';
    Sk.pre = "output";
    Sk.configure({
        output: outf,
        read: builtinRead
    });
    (Sk.TurtleGraphics || (Sk.TurtleGraphics = {})).target = 'canvas';
    var myPromise = Sk.misceval.asyncToPromise(function() {
        return Sk.importMainWithBody("<stdin>", false, prog, true);
    });
    myPromise.then(function(mod) {
            console.log('success');
        },
        function(err) {
            console.log(err.toString());
        });
}

我是 CodeMirror 的新手,我想使用他们的 python 版本来为我的 textarea 添加代码着色、行号和缩进,但我对如何去做有点困惑更改我的代码以添加这些功能,因为我发现他们的网站在将他们的工具连接到文本区域时有点含糊。

如何在我现有的 textarea 旁边使用 CodeMirror?

这就是我添加 codemirror 以突出显示文本区域中匹配括号的方式:

var editor = CodeMirror.fromTextArea(document.getElementByid('textbox'), {
                        mode: 'none',
                        matchBrackets: true,
                        dragDrop: false,
                        styleSelectedText: false,
                        showCursorWhenSelecting: true,
                        lineWrapping: true
                    });
var textAreaContents = editor.getDoc().getValue(); 

这就是我必须做的。您可以调整配置以对其进行定制,例如设置 mode 将突出显示并可选择智能缩进特定编程语言的文本。

这里是 fiddle,它显示了 python 模式下的代码镜像设置,带有行号和缩进:https://jsfiddle.net/gw0shwok/2/

记录了 Python 的配置选项 here

额外的功能可以通过几个 addons