0

I am creating an application where I have created a MS-word type application in Jquery. Now I have a problem where I have an editor and I want to place a div 'footer' on it at some position which I can do the actual problem is that when I am placing it,the place occupied by the footer div from content-editable should be disabled. Check this fiddle Demo

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div style="padding:40px 40px 40px 40px; background-color:#eaedf1">
<div id="wrapper" style="border:1px solid #eaedf1; width:602px; background-color:white;">

<div id="header" style="height:96px; width:600px;">
<h2 style="text-align:center;">This is header
</h2>
</div>
<div contenteditable="true" style="height:1400px; width:600px; border:1px solid #eaedf1; padding:2px 2px 2px 2px;"> 
</div>
<br>
<div style="position:absolute; top:750px; width:600px; height:200px; border:1px solid #eaedf1;">

<div id="footer" style="height:50px; width:600px;">
<h2 style="text-align:center;">This is footer
</h2>
</div>
<div style="background-color:#eaedf1;height:10px; width:600px;">

</div>
<div id="header" style="height:96px; width:600px;">
<h2 style="text-align:center;">This is header
</h2>
</div>
</div>

<div id="footer" style="height:50px; width:600px;">
<h2 style="text-align:center;">This is footer
</h2>
</div>
</div>
  <div>

enter image description here

Nikhil Ghuse
  • 1,258
  • 14
  • 31

3 Answers3

2

I'm not sure this is easily possible with one contenteditable element. I setup a basic example using multiple contenteditable elements, one for each page.

Example: https://fiddle.jshell.net/d68ew9qc/

The following snippet adds a new page if the maximum rows per page is reached and the key used was an "enter":

$(".pageEdit").on('keypress', function(e) {
  var length = $(this).children().length + 1;
  /* only add a new page if the next sibling is not available */
  // enter
  if (e.which == 13 && length >= this.getAttribute("max_rows")) {
    if ($(this).parent('.page').next().length === 0) {
      var newPage = $('.page').clone(true);
      $('.pageEdit', newPage).html('');
      newPage.appendTo('#editor');
      $('.pageEdit', newPage).focus();
    }
    e.preventDefault();
    return false;
  }
});

The keypress duplicates an element with the class=".page" and emptys the contenteditable element on the newly created one.

The keyup handles the backspace deletion of empty pages.

$(".pageEdit").on('keyup', function(e) {
    var length = $(this).children().length;
  var total_pages = $(this).parent('.page').parent().children().length;
  // backspace is not working with keypress, this portion needs some work!!!
  if (e.which == 8 && $(this).parent('.page').prev().length > 0) {
    if (length === 0 && total_pages > 1) {
        // found via: http://stackoverflow.com/a/4238971/3298029
        placeCaretAtEnd($('.pageEdit>div:last', $(this).parent('.page').prev())[0]);
        $(this).closest('.page').remove();
    }
  }
});

The basic html for that looks like the following:

<style>
.editor {
  background-color: transparent;
  border: 1px solid #eaedf1;
}

.page {
  width: 600px;
  position: relative;
  margin-top: 15px;
  background-color: #fff;
}

.header {
  height: 100px;
  width: 100%;
  margin: 0;
}

.footer {
  height: 100px;
  bottom: 0;
  width: 100%;
  margin: 0;
}

.pageEdit {
  min-height: 1400px;
  border: 1px solid #eaedf1;
  overflow: hidden;
  padding: 0;
}


/* found: http://stackoverflow.com/a/4407335/3298029 */

.noselect {
  -webkit-touch-callout: none;
  /* iOS Safari */
  -webkit-user-select: none;
  /* Chrome/Safari/Opera */
  -khtml-user-select: none;
  /* Konqueror */
  -moz-user-select: none;
  /* Firefox */
  -ms-user-select: none;
  /* Internet Explorer/Edge */
  user-select: none;
  /* Non-prefixed version, currently
                                  not supported by any browser */
  pointer-events: none;
}

</style>    
<div style="padding:40px 40px 40px 40px; background-color:#eaedf1">
      <div id="editor">
        <div class="page">
          <div class="header noselect">
            <h2 style="text-align:center;">This is header</h2>
          </div>
          <div contenteditable="true" class="pageEdit" max_rows="77">
          </div>
          <div class="footer noselect">
            <h2 style="text-align:center;">This is footer
        </h2>
          </div>
        </div>
      </div>
    </div>

This is a fairly simple demo and many things won't work. If a line breaks to a new one, this should be handled as well.

This was only tested in Chrome which adds <div><br /></div> for a new line. Firefox only adds <br /><br />. So with that in mind the total number of rows should be calculated dynamically and not set to a fixed value because it defers from one browser to another.

Hope this gets you started.

d.h.
  • 1,206
  • 1
  • 18
  • 33
  • but what about delete and backspace..?? – Nikhil Ghuse Sep 20 '16 at 09:36
  • code provided by you is already implemented by me....but issue is in on delete and backspace – Nikhil Ghuse Sep 20 '16 at 09:39
  • I already mentioned backspace. Delete would do the same as backspace if on the first line and the page is empty. I didn't implemented this to the final state. This is just a start for you. – d.h. Sep 20 '16 at 09:39
  • I will have a look on how backspace and delete might work. – d.h. Sep 20 '16 at 09:41
  • Just to be sure... In that case you want to delete the empty page and focus on the last line of the previous page? Right? – d.h. Sep 20 '16 at 09:43
  • yes that would be needed and if some content is delete from first page then some content fro first line should replace in first page – Nikhil Ghuse Sep 20 '16 at 09:46
  • Same is true if you backspace from the next Page to the previous and the Page is not empty. Those two scenarios are not that Easy. – d.h. Sep 20 '16 at 09:55
  • I updated the answer and backspace should now delete empty pages and jump to the last line on the previous page. I am not sure if I got the time to work on the other problems. – d.h. Sep 20 '16 at 10:25
  • yes but major issue is when some content is delete from first page then some content fro first line should replace in first page – Nikhil Ghuse Sep 20 '16 at 10:34
  • I am sorry this is nothing that is easy done. I better might stop here and leave it to you. – d.h. Sep 20 '16 at 10:58
  • Hey just wanted to let you know that I tried to use :after pseudo elements to fake the header and footer. https://fiddle.jshell.net/55c85w2h/ Not really working because with that other problems arrive. I'm really asking myself if a simple textarea would be better suited. – d.h. Sep 21 '16 at 06:43
  • nope but you have really helped me a lot and you need to be rewarded thanks I got a better solution. – Nikhil Ghuse Sep 21 '16 at 06:56
  • Can you share your solution? I'm interested in it too. – d.h. Sep 21 '16 at 06:59
  • Actually It is privacy related that's why I cant but u should find a jQuery library named collumizer which i have used it might not get header footer on writing time but got page breaks and on preview got accepted output – Nikhil Ghuse Sep 21 '16 at 07:01
0

Try css pointer-events: none; or select: none; or jQuery

$("#div").click(function(e){e.preventDefault();});

or maybe

$("#div").click(function(e){e.stopPropagation();});

(not 100% clear on your intent).

-1

Is there any particular reason why would you want to use 'position:absolute'? I think if I change the code to work with 'relative' position, it display the results you want. For example:

<div id="wrapper" style="border:1px solid #eaedf1; width:602px;background-color:white;">
  <div id="header" style="height:96px; width:600px;">
<h2 style="text-align:center;">This is header
</h2>
  </div>
  <div contenteditable="true" style="overflow:hidden;height:1400px; width:600px; border:1px solid #eaedf1;"> 
  </div>
  <div id="footer" style="height:50px; width:600px;">
<h2 style="text-align:center;">This is footer
</h2>
  </div>
  <div style="background-color:#eaedf1;height:10px; width:600px;">
  </div>

  <div id="header" style="height:96px; width:600px;">
<h2 style="text-align:center;">This is header
</h2>
  </div>
  <div contenteditable="true" style="height:1400px; width:600px; border:1px solid #eaedf1;"> 
  </div>
  <div id="footer" style="height:50px; width:600px;">
<h2 style="text-align:center;">This is footer
</h2>
  </div>
</div>

See: https://jsfiddle.net/alejuliet/qn4k7csu/

AleJuliet
  • 75
  • 1
  • 9