Skip to content

Commit 46e28f5

Browse files
committed
feat: make copy & paste work for edit on click
1 parent d796855 commit 46e28f5

File tree

1 file changed

+70
-11
lines changed

1 file changed

+70
-11
lines changed

app-src/scripts/edit-on-click/edit-on-click-d.js

Lines changed: 70 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,58 @@
88
(function() {
99
'use strict';
1010

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(/&nbsp;/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+
1163
angular
1264
.module('superProductivity')
1365
.constant('EDIT_ON_CLICK_TOGGLE_EV', 'EDIT_ON_CLICK_TOGGLE_EV')
@@ -29,24 +81,23 @@
2981
};
3082
}
3183

32-
3384
function linkFn(scope, el, attrs, ngModel) {
3485
let lastVal;
3586
// to do this better
3687
setTimeout(() => {
37-
lastVal = el.html().replace(/<\S[^><]*>/g, '');
88+
lastVal = removeTags(el.html());
3889
});
3990

4091
el[0].setAttribute('contenteditable', true);
4192

4293
function execCb(event) {
4394
// 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+
}
50101

51102
const curVal = el.html();
52103
const isChanged = lastVal !== curVal;
@@ -63,10 +114,9 @@
63114
function read() {
64115
let curVal = el.html();
65116
// strip tags
66-
curVal = curVal.replace(/<\S[^><]*>/g, '');
117+
curVal = removeTags(curVal);
67118

68119
const isChanged = lastVal !== curVal;
69-
console.log(curVal, lastVal, isChanged);
70120

71121
if (isChanged) {
72122
ngModel.$setViewValue(curVal);
@@ -77,7 +127,7 @@
77127
ngModel.$render = () => {
78128
let html = ngModel.$viewValue || '';
79129
// strip tags
80-
html = html.replace(/<\S[^><]*>/g, '');
130+
html = removeTags(html);
81131
el.html(html);
82132
};
83133

@@ -103,6 +153,15 @@
103153
}
104154
});
105155

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+
106165
function clickToggleEvHandler(ev, eventId) {
107166
if (eventId === scope.editOnClickEvId) {
108167
el.focus();

0 commit comments

Comments
 (0)