javascript - 如何使用标记保存和恢复内容?

标签 javascript codemirror

我有一个 CodeMirror 实例,其中包含一些内容/文本和一些标记来突出显示某些区域。我想将所有内容(包括标记)保存到某些后端服务。然后我想恢复内容和标记,使恢复的文档看起来像原始文档(包括标记)。

实现这一目标的最佳方法是什么?

最佳答案

你可以看看我的代码。我不久前就做过这个。

http://jsfiddle.net/aljordan82/4ewe9/

<html>
<head>
<style>
    .bg {
        background:#CCC;
    }
</style>
<script>
    $(document).ready(function() {
        var strSavedMarkers = $("#savedMarkers").val(); // get markers from textarea
        var arrMarkers = JSON.parse(strSavedMarkers)
        $("#markerStyle").html(".bg{background:#AAAAAA;}")
        var editor = CodeMirror.fromTextArea(document.getElementById("code"), {
            mode: "text/html",
            lineNumbers: true,
            onCursorActivity: function() {}
        });
        //get content from textarea 
        editor.setValue($("#savedContent").val());
        applyMarkers();
        $("#mark").click(function() {
            // Marked selected text
            var marker = editor.markText(editor.getCursor(true), editor.getCursor(false), {
                className: "bg"
            });
            // get values of marker. [ch , line , ch ,  line]
            var tempArray = [marker.find().from.ch, marker.find().from.line, marker.find().to.ch, marker.find().to.line]
            // we push this array to arrMarkers
            arrMarkers.push(tempArray)
        });

        // when done editing 
        $("#save").click(function() {
            JSON.stringify(arrMarkers)
            $("#savedMarkers").val(JSON.stringify(arrMarkers))

        });

        function applyMarkers() {
            var strSavedMarkers = $("#savedMarkers").val(); // get markers from textarea
            var arrSavedMarkers = JSON.parse(strSavedMarkers)
            for (x in arrSavedMarkers) {
                var marker = arrSavedMarkers[x]
                editor.markText({
                    'ch': marker[0],
                        'line': marker[1]
                }, {
                    'ch': marker[2],
                        'line': marker[3]
                }, {
                    className: "bg"
                });
            }
        }
    });
</script>
<head>
<body>

    <p>we need two textareas, one to store array of markers and the other is so store the content.</p>
    <p></p>
    <textarea id="savedMarkers">[[20,3,40,3],[12,8,90,8]]</textarea>
    <textarea id="savedContent">
        <title>hidden textarea, poplate from back end</title>
        <catalog>
            <book id="bk101">
                <author>Gambardella, Matthew</author>
                <title>XML Developer's Guide</title>
                <genre>Computer</genre>
                <price>44.95</price>
                <publish_date>2000-10-01</publish_date>
                <description>An in-depth look at creating applications with XML.</description>
            </book>
            <book id="bk102">
                <author>Ralls, Kim</author>
                <title>Midnight Rain</title>
                <genre>Fantasy</genre>
                <price>5.95</price>
                <publish_date>2000-12-16</publish_date>
                <description>A former architect battles corporate zombies, an evil sorceress, and her own childhood to become queen of the world.</description>
            </book>
            <book id="bk103">
                <author>Corets, Eva</author>
                <title>Maeve Ascendant</title>
                <genre>Fantasy</genre>
                <price>5.95</price>
                <publish_date>2000-11-17</publish_date>
                <description>After the collapse of a nanotechnology society in England, the young survivors lay the foundation for a new society.</description>
            </book>
            <catalog>
    </textarea>
    <form>
        <br/>
        <textarea id="code" name="code"></textarea>
    </form>
    <button id="mark">Mark Selection</button>
    <button id="save">Save Markers and Content</button>
</body>

关于javascript - 如何使用标记保存和恢复内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16182587/

相关文章:

javascript - 显示/隐藏 Javascript 问题

javascript - 在使用 <span> 按钮提交表单时重定向到新站点

javascript - 打开后拒绝错误 `.catch`

reactjs - React useEffect Hook return () => cleanup() 与 return cleanup

jquery - Codemirror对象没有方法 'getAttribute'

javascript - 在外部 Javascript 中使用 Python Flask 传递参数

javascript - 单击“确定”进行 JavaScript 确认后如何打开 Bootstrap 模式?

javascript - 在 Vim 模式下使用 Codemirror

javascript - 任何 keyup 后 codemirror 自动完成?

css - 更改 jupyter 的匹配括号颜色