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;
31 var anchorNode = selection.anchorNode;
32 // Is selection still inside a node?
33 if (!$(anchorNode).closest('[x-node]')) return;
35 self.singleClick = true;
36 setTimeout(function() {
37 if (self.singleClick) {
38 self.element.insertBefore(
40 selection.anchorOffset
45 self.singleClick = false;
50 self.element.on('keydown', function(e) {
57 // we are in <akap> (no going up)
58 // we are in <wyroznienie> (can go up)
59 // we are next to <wyroznienie> (can go inside)
100 // console.log('key', e.key, e.code);
106 return this.element.parent().length;
110 console.log(this.view);
114 p = this.element.parent()[0]
115 this.element.detach();
121 $("textarea", self.element).focus();
125 this.element.parent()[0].normalize();
129 $(document.createTextNode(ch)).insertBefore(this.element);
134 let contents = this.element.parent().contents();
135 // Find the text before caret.
136 let textBefore = contents[contents.index(this.element) - 1];
138 // Should be text, but what if not?
139 textBefore.textContent = textBefore.textContent.substr(0, textBefore.textContent.length - 1);
145 let contents = this.element.parent().contents();
146 // Find the text after caret.
147 let textAfter = contents[contents.index(this.element) + 1];
148 textAfter.textContent = textAfter.textContent.substr(1);
152 let splitter = this.element;
153 let parent, newParent, splitIndex, index;
155 while (!splitter.is('div[x-node]')) {
156 parent = splitter.parent();
157 splitIndex = parent.contents().index(splitter);
158 newParent = parent.clone();
159 index = parent.contents().length - 1;
160 while (index >= splitIndex) {
161 newParent.contents()[index].remove();
165 console.log(newParent, index);
166 parent.contents()[index].remove();
169 newParent.insertBefore(parent);
171 console.log('split', parent);
179 edge: (i, l) => {return !i;},
180 enter: (l) => {return l - 1;},
181 splitTarget: (t) => {return t.splitText(t.length - 1);},
182 noSplitTarget: (t) => {return t.splitText(t.length);},
189 edge: (i, l) => {return i == l - 1;},
190 enter: (l) => {return 0;},
191 splitTarget: (t) => {return t.splitText(1);},
192 noSplitTarget: (t) => {return t;},
197 if (!this.attached) return;
201 let contents = this.element.parent().contents();
202 let index = contents.index(this.element);
203 let target, moved, oldparent;
205 let parent = this.element.parent()[0];
207 if (opts.edge(index, contents.length)) {
208 // We're at the end -- what to do?
211 if (parent.nodeName == 'EM') {
213 parent = parent.parentNode;
214 contents = $(parent).contents();
215 index = contents.index(oldparent);
220 target = contents[index];
223 while (target.nodeType == 1) {
224 // we've encountered a node.
227 if (target.nodeName == 'EM') {
230 contents = parent.contents();
231 index = opts.enter(contents.length);
232 target = contents[index];
234 // what if it has no elements?
237 index += opts.move; // again, what if end?
238 target = contents[index];
243 // what if editable but empty?
247 if (target.nodeType == 3) {
249 target = opts.splitTarget(target);
251 target = opts.noSplitTarget(target);
254 this.element.insertBefore(target);