5 self.singleClick = false;
7 let caret = this.element = $('<span id="caret"><textarea></textarea></span>');
9 // When user writes into caret, add it to the document.
10 $('textarea', caret).on('input', function() {
11 let v = $(this).val();
17 // On click on x-node element, set caret position.
18 self.view.on('click', '*[x-node]', function(e) {
19 if (e.redakcja_caret_inserted) return;
20 e.redakcja_caret_inserted = true;
22 if (self.singleClick) {
23 self.singleClick = false;
29 var selection = window.getSelection();
30 if (!selection.isCollapsed) return;
32 self.singleClick = true;
33 setTimeout(function() {
34 if (self.singleClick) {
35 self.element.insertBefore(
36 selection.anchorNode.splitText(
37 selection.anchorOffset
42 self.singleClick = false;
47 self.element.on('keydown', function(e) {
54 // we are in <akap> (no going up)
55 // we are in <wyroznienie> (can go up)
56 // we are next to <wyroznienie> (can go inside)
97 // console.log('key', e.key, e.code);
103 return this.element.parent().length;
107 console.log(this.view);
111 p = this.element.parent()[0]
112 this.element.detach();
118 $("textarea", self.element).focus();
122 this.element.parent()[0].normalize();
126 $(document.createTextNode(ch)).insertBefore(this.element);
131 let contents = this.element.parent().contents();
132 // Find the text before caret.
133 let textBefore = contents[contents.index(this.element) - 1];
135 // Should be text, but what if not?
136 textBefore.textContent = textBefore.textContent.substr(0, textBefore.textContent.length - 1);
142 let contents = this.element.parent().contents();
143 // Find the text after caret.
144 let textAfter = contents[contents.index(this.element) + 1];
145 textAfter.textContent = textAfter.textContent.substr(1);
149 let splitter = this.element;
150 let parent, newParent, splitIndex, index;
152 while (!splitter.is('div[x-node]')) {
153 parent = splitter.parent();
154 splitIndex = parent.contents().index(splitter);
155 newParent = parent.clone();
156 index = parent.contents().length - 1;
157 while (index >= splitIndex) {
158 newParent.contents()[index].remove();
162 console.log(newParent, index);
163 parent.contents()[index].remove();
166 newParent.insertBefore(parent);
168 console.log('split', parent);
176 edge: (i, l) => {return !i;},
177 enter: (l) => {return l - 1;},
178 splitTarget: (t) => {return t.splitText(t.length - 1);},
179 noSplitTarget: (t) => {return t.splitText(t.length);},
186 edge: (i, l) => {return i == l - 1;},
187 enter: (l) => {return 0;},
188 splitTarget: (t) => {return t.splitText(1);},
189 noSplitTarget: (t) => {return t;},
194 if (!this.attached) return;
198 let contents = this.element.parent().contents();
199 let index = contents.index(this.element);
200 let target, moved, oldparent;
202 let parent = this.element.parent()[0];
204 if (opts.edge(index, contents.length)) {
205 // We're at the end -- what to do?
208 if (parent.nodeName == 'EM') {
210 parent = parent.parentNode;
211 contents = $(parent).contents();
212 index = contents.index(oldparent);
217 target = contents[index];
220 while (target.nodeType == 1) {
221 // we've encountered a node.
224 if (target.nodeName == 'EM') {
227 contents = parent.contents();
228 index = opts.enter(contents.length);
229 target = contents[index];
231 // what if it has no elements?
234 index += opts.move; // again, what if end?
235 target = contents[index];
240 // what if editable but empty?
244 if (target.nodeType == 3) {
246 target = opts.splitTarget(target);
248 target = opts.noSplitTarget(target);
251 this.element.insertBefore(target);