如何打破内容编辑器并将其附加到下一页

时间:2016-08-04 07:58:51

标签: javascript jquery

enter image description here我想在当前页面完成时将当前内容编辑器附加到下一页

这是我的小提琴Check

任何帮助都会很明显

描述

其实我正在做的是我创建了一个文本编辑器,我在其中添加了页眉和内容编辑器,但现在我想添加一个具有相同页眉和页脚的新页面,但是想要追加我当前的页面内容 - 编辑



   var margin;
        var pageSize;
        var headerHeight;
        var footerHeight;
        var marginTopBottom;
        var marginLeftRight;
        var marginArray;
        var reportHeight;
        var reportWidth;

        $(function () {
            GetPage();
            jQuery("#btnGetSize").on('click', function () {
                var h = $('#Template').height();
                var w = $('#Template').width();
                var h1 = $('#ReportPage').height();
                var w1 = $('#ReportPage').width();
                alert("Template :   h=" + (parseFloat(h) / 3.76) + " w=" + (parseFloat(w) / 3.76) + "    ReportPage :   h1=" + (parseFloat(h1) / 3.76) + " w1=" + (parseFloat(w1) / 3.76) + "");
            });

            jQuery("#ddlSize").change(function () {
                GetPage();
            });

            jQuery("#ddlMargin").change(function () {
                GetPage();
            });


            $("#btnPrint1").on('click', function () {
                var divContents = $("#ReportPage").html();

                var printWindow = window.open('', '', 'height=1000px,width=1000px');
                printWindow.document.write('<html><head>');
                printWindow.document.write('</head><body >');
                printWindow.document.write(divContents);
                printWindow.document.write('</body></html>');

                printWindow.document.close();
                printWindow.print();
            });
            //   -------------------------
            $("#btnPrint").on('click', function () {
                $('#main').addClass("page").css("display", "none");
                $('#main').addClass("print page ")
                var content_height = $('#TemplateSubPage').height();
                var page = 1;
                function buildReport() {

                    if ($('#TemplateSubPage').contents().length > 0) {

                        $page = $("#TemplatePage").clone();
                        $page.find(".subpage").empty();
                        $page.find(".footer span").append(page);
                        $("#ReportPage").append($page);
                        $('.subpage').attr('disabled', 'disabled');
                        $("#TemplateSubPage").prop('disabled', true);
                        page++;

                        $('#TemplateSubPage').columnize({
                            columns: 1,
                            target: ".page:last .subpage",
                            overflow: {
                                height: content_height,
                                id: "#TemplateSubPage",
                                doneFunc: function () {
                                    console.log("done with page");
                                    buildReport();
                                }
                            }
                        });
                    }
                }
                setTimeout(buildReport, 1000);
            });

        });

        //=------------------------


        function GetPage() {

            pageSize = $('#ddlSize option:selected').text();
            margin = $('#ddlMargin option:selected').text();
            headerHeight = $('.header').height();
            footerHeight = $('.footer').height();
            marginArray = new Array();
            marginArray = GetMargins();
            marginTopBottom = parseFloat(marginArray[0]) + parseFloat(marginArray[2]) + (parseFloat(headerHeight) / 3.7) + (parseFloat(footerHeight) / 3.7);
            marginLeftRight = parseFloat(marginArray[1]) + parseFloat(marginArray[3]);

            if (pageSize == "--Select--") {
                //$("#TemplatePage .divOuter").css("width", "210mm").css("height", "290mm");
                $(".page").css("width", "210mm").css("min-height", "290mm");
                $(".subpage").css("height", 277 + "mm").css("max-height", 290 + "mm").css("width", 187 + "mm");

                // $("div .subpage").css("height", (290) + "mm").css("max-height", (290) + "mm").css("width", (210) + "mm");

            }
            else if (pageSize == "A0") {

                $(" .page").css("width", "841mm").css("min-height", "1175mm"); /*page margins top right bottom  left  */
                $(".subpage").css("height", 1155 + "mm").css("max-height", 1175 + "mm").css("width", 820 + "mm");

                // $("div .subpage").css("height", (1175) + "mm").css("max-height", (1175) + "mm").css("width", (841) + "mm");

            }
            else if (pageSize == "A1") {
                $(".page").css("width", "594mm").css("min-height", "830mm");
                $(".subpage").css("height", 810 + "mm").css("max-height", 830 + "mm").css("width", 574 + "mm");

                //$("div .subpage").css("height", (830) + "mm").css("max-height", (830) + "mm").css("width", (594) + "mm");

            }
            else if (pageSize == "A2") {
                $(".page").css("width", "420mm").css("min-height", "585mm");
                $(".subpage").css("height", 565 + "mm").css("max-height", 585 + "mm").css("width", 400 + "mm");

                // $("div .subpage").css("height", (585) + "mm").css("max-height", (585 ) + "mm").css("width", (420 - marginLeftRight) + "mm");

            }
            else if (pageSize == "A3") {
                $("div .page").css("width", "297mm").css("min-height", "413mm");
                //  $("div .subpage").css("height", (413 - marginTopBottom) + "mm").css("max-height", (413 - marginTopBottom) + "mm").css("width", (297 - marginLeftRight) + "mm");

            }
            else if (pageSize == "A4") {
                //   $("#TemplatePage .divOuter").css("width", "210mm").css("height", "290mm");
                $(".page").css("width", "210mm").css("min-height", "297mm");
                $(".subpage").css("height", 277 + "mm").css("max-height", 297 + "mm").css("width", 187 + "mm");

                // $("div .subpage").css("height", (290 - marginTopBottom) + "mm").css("max-height", (290 - marginTopBottom) + "mm").css("width", (210 - marginLeftRight) + "mm");

            }
            else if (pageSize == "A4 LandScape") {
                // $("#TemplatePage .divOuter").css("width", "297mm").css("height", "200mm");

                $(".page").css("width", "297mm").css("min-height", "200mm");
                $(".subpage").css("height", 180 + "mm").css("max-height", 200 + "mm").css("width", 277 + "mm");

                // $("div .subpage").css("height", (200 - marginTopBottom) + "mm").css("max-height", (200 - marginTopBottom) + "mm").css("width", (299 - marginLeftRight) + "mm");
            }
            else if (pageSize == "A5") {
                $(".page").css("width", "148mm").css("min-height", "200mm");
                $(".subpage").css("height", 180 + "mm").css("max-height", 200 + "mm").css("width", 126 + "mm");

            }
            else if (pageSize == "A6") {
                $(".page").css("width", "105mm").css("min-height", "142mm");
                $(".subpage").css("height", 122 + "mm").css("max-height", 142 + "mm").css("width", 83 + "mm");

                //$("div .subpage").css("height", (142 - marginTopBottom) + "mm").css("max-height", (142 - marginTopBottom) + "mm").css("width", (105 - marginLeftRight) + "mm");

            }
            else if (pageSize == "A7") {
                $(".page").css("width", "74mm").css("min-height", "101mm");
                $(".subpage").css("height", 81 + "mm").css("max-height", 101 + "mm").css("width", 51.30 + "mm");

                //  $("div .subpage").css("height", (101 - marginTopBottom) + "mm").css("max-height", (101 - marginTopBottom) + "mm").css("width", (74 - marginLeftRight) + "mm");

            }
            else if (pageSize == "A8") {
                $(".page").css("width", "52mm").css("min-height", "71mm");
                $(".subpage").css("height", 51 + "mm").css("max-height", 71 + "mm").css("width", 29.60 + "mm");

                // $("div .subpage").css("height", (71 - marginTopBottom) + "mm").css("max-height", (71 - marginTopBottom) + "mm").css("width", (52 - marginLeftRight) + "mm");

            }
        }

        function GetMargins() {

            var marginArray = new Array();
            margin = $('#ddlMargin').val();

            if (margin == 0) {
                $(".page").css("padding", "25.4mm 25.4mm 25.4mm 25.4mm");
                marginArray = ["25.4", "25.4", "25.4", "25.4"];
                return marginArray;
            }
            else if (margin == 1) {
                $(".page").css("padding", "12.7mm 12.7mm 12.7mm 12.7mm"); /*page margins top right bottom  left  */
                marginArray = ["12.7", "12.7", "12.7", "12.7"];
                return marginArray;
            }
            else if (margin == 2) {
                $(".page").css("padding", "25.4mm 25.4mm 25.4mm 25.4mm");
                marginArray = ["25.4", "25.4", "25.4", "25.4"];
                return marginArray;
            }
            else if (margin == 3) {
                $(".page").css("padding", "25.4mm 19.05mm 25.4mm 19.05mm");
                marginArray = ["25.4", "19.05", "25.4", "19.05"];
                return marginArray;
            }
            else if (margin == 4) {
                $(".page").css("padding", "25.4mm 12.7mm 25.4mm 12.7mm");
                marginArray = ["25.4", "12.7", "25.4", "12.7"];
                return marginArray;
            }
            else if (margin == 5) {
                $(".page").css("padding", "0mm 0mm 0mm 0mm");
                marginArray = ["0", "0", "0", "0"];
                return marginArray;
            }
        }     
&#13;
body
{
    margin: 0;
    padding: 0;
    font: 12pt "Tahoma";
}
.page
{
    padding: 20mm;
    border: 1px solid black;
    border-radius: 5px;
    background: white;
    margin-top: 0px;
    margin-bottom: 0px;
}
.subpage
{
    min-width: 10mm;
    word-break: break-all;
    cursor: text;
    overflow: hidden;
    top: 0px;
    padding: 1cm;
    border: 1px solid red;
}

@page
{
    margin: 0;
}
@media print
{
    /*   html, body
                {
                    width: 210mm;
                    height: 297mm;
                }
                .page
                {
                    margin: 0;
                    border: initial;
                    border-radius: initial;
                    width: initial;
                    min-height: initial;
                    box-shadow: initial;
                    background: initial;
                    page-break-after: always;
                }
            */
}
&#13;
<script src="https://dl.dropboxusercontent.com/u/285608/Miscellaneous/JS%20Libraries/columnizer/jquery.columnizer.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>

  <div id="controls">
        <div style="text-align: center; float: left; font-size: medium">
            <select id="ddlSize">
                <option id="A" value="0">--Select--</option>
                <option id="A0" value="841mm X 1189mm">A0</option>
                <option id="A1" value="594mm X 841mm">A1</option>
                <option id="A2" value="420mm X 594mm">A2</option>
                <option id="A3" value="297mm X 420mm">A3</option>
                <option id="A4" value="210mm X 297mm">A4</option>
                <option id="A4Landscape" value="297mm X 210mm">A4 LandScape</option>
                <option id="A5" value="148mm X 210mm">A5</option>
                <option id="A6" value="105mm X 148mm">A6</option>
                <option id="A7" value="74mm X 105mm">A7</option>
                <option id="A8" value="52mm X 74mm">A8</option>
            </select>
        </div>
        &nbsp;&nbsp;&nbsp;&nbsp;
        <button id="btnPrint">
            Print preview</button>&nbsp;&nbsp;&nbsp;&nbsp;
        <button id="btnPrint1">
            Print</button>
        <button id="btnGetSize">
            Get divSize</button>
        <div style="text-align: center; float: left; padding-bottom: 50px; font-size: medium;
            width: 300px">
            <select id="ddlMargin">
                <option id="No" value="0">--Select--</option>
                <option id="Narrow" value="1">0.5" 0.5" 0.5" 0.5" </option>
                <option id="Normal" value="2">1" 1" 1" 1"</option>
                <option id="Moderate" value="3">1" 0.75" 1" 0.75" </option>
                <option id="Other" value="4">1" 0.5" 1" 0.5" </option>
                <option id="NoMargin" value="5">0" 0" 0" 0"</option>
            </select>&nbsp;&nbsp;&nbsp;&nbsp;
        </div>
    </div>
    <!--tyle="border: 1px solid black;"-->
    <div id="main">
        <div class="page" id="TemplatePage" style="page-break-after: always;">
            <div id="header">
                <center>
                    This is header</center>
                <br />
            </div>
            <div class="subpage" contenteditable="true" id="TemplateSubPage">
                <br />
            </div>
            <div id="footer">
                <br />
                <center>
                    This is Footer</center>
            </div>
        </div>
    </div>
    <div id="ReportPage" class="divOuter">
    </div>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:8)

一个可能的解决方案是在textArea输入元素的情况下计算换行符的数量(\r\n\n),然后创建一个新的&#34;页面&#34;元素,编辑旧文本区域和新文本区域的值,并将焦点更改为新文本区域。这是一个psuedo-JS事件处理程序:

function onPageInput(event){
  //count lines in original page
  var lines = page.value.split(/\r*\n/);

  if (lines.length > linesPerPage){
    var newPage = addPage(); //returns the new page
    page.insertAfter(newPage);


    //reset original textArea value
    page.value = "";

    //populate old textarea and new textarea
    page.value = lines.slice(0, linesPerPage).join("\n");
    newPage.value = lines.slice(linesPerPage, lines.length).join("\n");

    newPage.focus();
  }
}

这是一个有效的example implementation

请注意,我使用textAreas进行简化。如果要使用ContentEditable div,则需要一种方法来计算内容行,并根据所需的换行符将内容分成两个单独的块。一些现有的WYSIWYG库可能已经提供了用于行计数/拆分内容的API,而无需您使用pitfalls of ContentEditable

当然 - 您仍然需要处理特殊情况,例如当用户在空白页面上按退格键时删除页面,或者您想要的任何其他行为。

答案 1 :(得分:2)

<强> fiddle proof of concept... 这不仅仅是一个答案,一个建议尝试的路径:

$(document).ready(function(){
        var page = {
              newLines : 0,
              newLinesCheck : function(){
              this.newLines = $("#TemplateSubPage").val().split('\n').length - 1;
              if(this.newLines < 0){this.newLines = 0;}
            console.warn($("#TemplateSubPage").val().split('\n'));
              },
              newLine : function(){
                this.newLines++;
              }
            };

         $(document).keydown(function(k){
            //console.log(k.which);
            if(k.which == 13){ 
              page.newLine();
              console.log(page.newLines);
            }
            if(k.which == 46 || k.which == 8){// DEL, BKSPS
                page.newLinesCheck();
            }
          });
    });

我必须将可编辑的div更改为textarea以允许找到\n字符但是在你的小提琴中它起作用...正如我所说的是一个建议并且不能完全起作用,但可能会引导你或给你一些想法。