5 self.singleClick = false;
7 let caret = this.element = $('<nobr><span id="caret"><textarea></textarea></span></nobr>');
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_ignore) return;
20 e.redakcja_caret_ignore = 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 if (anchorNode.nodeType != Node.TEXT_NODE) return;
33 // Is selection still inside a node?
34 if (!$(anchorNode).closest('[x-node]').length) return;
35 if ($(anchorNode).parents('[x-annotation-box]').not('.editing').length) return;
37 self.singleClick = true;
38 setTimeout(function() {
39 if (self.singleClick) {
40 self.element.insertBefore(
42 selection.anchorOffset
47 self.singleClick = false;
52 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;
112 p = this.element.parent()[0]
113 this.element.detach();
119 $("textarea", self.element).focus();
123 this.element.parent()[0].normalize();
127 elem.insertBefore(this.element);
132 $(document.createTextNode(ch))
138 let contents = this.element.parent().contents();
139 // Find the text before caret.
140 let textBefore = contents[contents.index(this.element) - 1];
142 // Should be text, but what if not?
143 textBefore.textContent = textBefore.textContent.substr(0, textBefore.textContent.length - 1);
149 let contents = this.element.parent().contents();
150 // Find the text after caret.
151 let textAfter = contents[contents.index(this.element) + 1];
152 textAfter.textContent = textAfter.textContent.substr(1);
156 let splitter = this.element;
157 let parent, newParent, splitIndex, index;
159 while (!splitter.is('div[x-node]')) {
160 parent = splitter.parent();
161 splitIndex = parent.contents().index(splitter);
163 if (parent.is('[x-annotation-box]')) {
164 // We're splitting inside an inline-style annotation.
165 // Convert into a block-style annotation now.
166 let p = $('<div x-editable="true" x-node="akap">');
167 parent.contents().appendTo(p);
173 newParent = parent.clone();
174 index = parent.contents().length - 1;
175 while (index >= splitIndex) {
176 newParent.contents()[index].remove();
180 parent.contents()[index].remove();
183 newParent.insertBefore(parent);
192 edge: (i, l) => {return !i;},
193 enter: (l) => {return l - 1;},
194 splitTarget: (t) => {return t.splitText(t.length - 1);},
195 noSplitTarget: (t) => {return t.splitText(t.length);},
202 edge: (i, l) => {return i == l - 1;},
203 enter: (l) => {return 0;},
204 splitTarget: (t) => {return t.splitText(1);},
205 noSplitTarget: (t) => {return t;},
210 if (!this.attached) return;
214 let contents = this.element.parent().contents();
215 let index = contents.index(this.element);
216 let target, moved, oldparent;
218 let parent = this.element.parent()[0];
220 if (opts.edge(index, contents.length)) {
221 // We're at the end -- what to do?
224 if (parent.nodeName == 'EM') {
226 parent = parent.parentNode;
227 contents = $(parent).contents();
228 index = contents.index(oldparent);
233 target = contents[index];
236 while (target !== undefined && target.nodeType == Node.ELEMENT_NODE) {
237 // we've encountered a node.
240 if (target.nodeName == 'EM') {
243 contents = parent.contents();
244 index = opts.enter(contents.length);
245 target = contents[index];
247 // what if it has no elements?
250 index += opts.move; // again, what if end?
251 target = contents[index];
256 // what if editable but empty?
260 if (target !== undefined && target.nodeType == Node.TEXT_NODE ) {
262 target = opts.splitTarget(target);
264 target = opts.noSplitTarget(target);
267 this.element.insertBefore(target);