<!-- {#
@Author       : xupingmao
@email        : 578749341@qq.com
@Date         : 2022-07-31 12:15:50
@LastEditors  : xupingmao
@LastEditTime : 2023-09-03 10:29:49
@FilePath     : /xnote/handlers/note/component/sidebar/markdown_edit_sidebar.html
@Description  : 描述
 #} -->

<div class="card">
    <div class="card-title">
        <span>笔记目录</span>
        <div class="sidebar-contents">

        </div>
    </div>
</div>

<script id="sidebar-content-template" type="text/html">
    {{!each list }}
    <a class="list-item level-{{!$value.level}}" data-line="{{! $value.lineNo }}">
        <span>{{! $value.name }}</span>
    </a>
    {{!/each }}
</script>

<script>
$(function () {
    function refreshContents() {
        var content = $("#markdown-input").val();
        var mdHeading = new MarkdownHeading();
    
        mdHeading.load(content);
        console.log(mdHeading);
    
        var html = $("#sidebar-content-template").render({list: mdHeading.getHeadings()});
        $(".sidebar-contents").html(html);
    }

    refreshContents();
    xnote.on("editor.change", refreshContents);
});

$(".sidebar-contents").on("click", ".list-item", function (e) {
    var lineNo = $(this).attr("data-line");
    // xnote.codeMirror.setCursor({line:lineNo-1, ch:0});
    xnote.codeMirror.setSelection({line:lineNo-1, ch:0}, {line:lineNo});
});

</script>