| 
8 | 8 | (function() {  | 
9 | 9 |   'use strict';  | 
10 | 10 | 
 
  | 
 | 11 | +  // HELPER  | 
 | 12 | +  // -----------------------------------  | 
 | 13 | +  function insertAtCursor(el, newText) {  | 
 | 14 | +    const sel = window.getSelection();  | 
 | 15 | + | 
 | 16 | +    const start = sel.anchorOffset;  | 
 | 17 | +    const end = sel.focusOffset;  | 
 | 18 | +    const text = el.innerText;  | 
 | 19 | + | 
 | 20 | +    const textBefore = text.substring(0, start);  | 
 | 21 | +    const textAfter = text.substring(end, text.length);  | 
 | 22 | + | 
 | 23 | +    const completeTextAfterInsert = (textBefore + newText + textAfter).trim();  | 
 | 24 | + | 
 | 25 | +    el.innerText = completeTextAfterInsert;  | 
 | 26 | + | 
 | 27 | +    // reset caret to proper offset  | 
 | 28 | +    const range = document.createRange();  | 
 | 29 | +    range.setStart(el.childNodes[0], start + newText.length);  | 
 | 30 | +    range.collapse(true);  | 
 | 31 | +    const sel2 = window.getSelection();  | 
 | 32 | + | 
 | 33 | +    sel2.removeAllRanges();  | 
 | 34 | +    sel2.addRange(range);  | 
 | 35 | +  }  | 
 | 36 | + | 
 | 37 | +  function removeTags(str) {  | 
 | 38 | +    return str.replace(/<\/?[^`]+?\/?>/gmi, '\n') //replace all tags  | 
 | 39 | +      .replace(/\n/gmi, '') // replace line breaks  | 
 | 40 | +      .replace(/ /gmi, '') // replace line breaks  | 
 | 41 | +      .trim();  | 
 | 42 | +  }  | 
 | 43 | + | 
 | 44 | +  function getCaretPosition(editableDiv) {  | 
 | 45 | +    let caretPos = 0;  | 
 | 46 | +    let sel;  | 
 | 47 | +    let range;  | 
 | 48 | + | 
 | 49 | +    if (window.getSelection) {  | 
 | 50 | +      sel = window.getSelection();  | 
 | 51 | +      if (sel.rangeCount) {  | 
 | 52 | +        range = sel.getRangeAt(0);  | 
 | 53 | +        if (range.commonAncestorContainer.parentNode == editableDiv) {  | 
 | 54 | +          caretPos = range.endOffset;  | 
 | 55 | +        }  | 
 | 56 | +      }  | 
 | 57 | +      return caretPos;  | 
 | 58 | +    } else {  | 
 | 59 | +      throw new Error('no window.getSelection :(')  | 
 | 60 | +    }  | 
 | 61 | +  }  | 
 | 62 | + | 
11 | 63 |   angular  | 
12 | 64 |     .module('superProductivity')  | 
13 | 65 |     .constant('EDIT_ON_CLICK_TOGGLE_EV', 'EDIT_ON_CLICK_TOGGLE_EV')  | 
 | 
29 | 81 |     };  | 
30 | 82 |   }  | 
31 | 83 | 
 
  | 
32 |  | - | 
33 | 84 |   function linkFn(scope, el, attrs, ngModel) {  | 
34 | 85 |     let lastVal;  | 
35 | 86 |     // to do this better  | 
36 | 87 |     setTimeout(() => {  | 
37 |  | -      lastVal = el.html().replace(/<\S[^><]*>/g, '');  | 
 | 88 | +      lastVal = removeTags(el.html());  | 
38 | 89 |     });  | 
39 | 90 | 
 
  | 
40 | 91 |     el[0].setAttribute('contenteditable', true);  | 
41 | 92 | 
 
  | 
42 | 93 |     function execCb(event) {  | 
43 | 94 |       // deselect all text  | 
44 |  | -      //if (window.getSelection) {  | 
45 |  | -      //  window.getSelection().removeAllRanges();  | 
46 |  | -      //}  | 
47 |  | -      //else if (document.selection) {  | 
48 |  | -      //  document.selection.empty();  | 
49 |  | -      //}  | 
 | 95 | +      if (window.getSelection) {  | 
 | 96 | +        window.getSelection().removeAllRanges();  | 
 | 97 | +      }  | 
 | 98 | +      else if (document.selection) {  | 
 | 99 | +        document.selection.empty();  | 
 | 100 | +      }  | 
50 | 101 | 
 
  | 
51 | 102 |       const curVal = el.html();  | 
52 | 103 |       const isChanged = lastVal !== curVal;  | 
 | 
63 | 114 |     function read() {  | 
64 | 115 |       let curVal = el.html();  | 
65 | 116 |       // strip tags  | 
66 |  | -      curVal = curVal.replace(/<\S[^><]*>/g, '');  | 
 | 117 | +      curVal = removeTags(curVal);  | 
67 | 118 | 
 
  | 
68 | 119 |       const isChanged = lastVal !== curVal;  | 
69 |  | -      console.log(curVal, lastVal, isChanged);  | 
70 | 120 | 
 
  | 
71 | 121 |       if (isChanged) {  | 
72 | 122 |         ngModel.$setViewValue(curVal);  | 
 | 
77 | 127 |     ngModel.$render = () => {  | 
78 | 128 |       let html = ngModel.$viewValue || '';  | 
79 | 129 |       // strip tags  | 
80 |  | -      html = html.replace(/<\S[^><]*>/g, '');  | 
 | 130 | +      html = removeTags(html);  | 
81 | 131 |       el.html(html);  | 
82 | 132 |     };  | 
83 | 133 | 
 
  | 
 | 
103 | 153 |       }  | 
104 | 154 |     });  | 
105 | 155 | 
 
  | 
 | 156 | +    el[0].onpaste = (ev) => {  | 
 | 157 | +      ev.stopPropagation();  | 
 | 158 | +      ev.preventDefault();  | 
 | 159 | +      const text = ev.clipboardData.getData('text/plain')  | 
 | 160 | +        .trim();  | 
 | 161 | +      insertAtCursor(el[0], text);  | 
 | 162 | +      scope.$apply(read);  | 
 | 163 | +    };  | 
 | 164 | + | 
106 | 165 |     function clickToggleEvHandler(ev, eventId) {  | 
107 | 166 |       if (eventId === scope.editOnClickEvId) {  | 
108 | 167 |         el.focus();  | 
 | 
0 commit comments