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 // This selection is not from this click. Ignore it.
34 if (anchorNode.parentNode != e.target) return;
35 // Is selection still inside a node?
36 if (!$(anchorNode).closest('[x-node]').length) return;
37 if ($(anchorNode).parents('[x-annotation-box]').not('.editing').length) return;
39 self.singleClick = true;
40 setTimeout(function() {
41 if (self.singleClick) {
42 $.wiki.activePerspective().flush();
43 self.element.insertBefore(
45 selection.anchorOffset
50 self.singleClick = false;
55 self.element.on('keydown', function(e) {
60 // we are in <akap> (no going up)
61 // we are in <wyroznienie> (can go up)
62 // we are next to <wyroznienie> (can go inside)
103 // console.log('key', e.key, e.code);
109 return this.element.parent().length;
115 p = this.element.parent()[0]
116 this.element.detach();
122 $("textarea", self.element).focus();
126 this.element.parent()[0].normalize();
130 elem.insertBefore(this.element);
135 $(document.createTextNode(ch))
141 let contents = this.element.parent().contents();
142 // Find the text before caret.
143 let textBefore = contents[contents.index(this.element) - 1];
145 // Should be text, but what if not?
146 textBefore.textContent = textBefore.textContent.substr(0, textBefore.textContent.length - 1);
152 let contents = this.element.parent().contents();
153 // Find the text after caret.
154 let textAfter = contents[contents.index(this.element) + 1];
155 textAfter.textContent = textAfter.textContent.substr(1);
159 let splitter = this.element;
160 let parent, newParent, splitIndex, index;
162 while (!splitter.is('div[x-node]')) {
163 parent = splitter.parent();
164 splitIndex = parent.contents().index(splitter);
166 if (parent.is('[x-annotation-box]')) {
167 // We're splitting inside an inline-style annotation.
168 // Convert into a block-style annotation now.
169 let p = $('<div x-editable="true" x-node="akap">');
170 parent.contents().appendTo(p);
176 newParent = parent.clone();
177 index = parent.contents().length - 1;
178 while (index >= splitIndex) {
179 newParent.contents()[index].remove();
183 parent.contents()[index].remove();
186 newParent.insertBefore(parent);
195 edge: (i, l) => {return !i;},
196 enter: (l) => {return l - 1;},
197 splitTarget: (t) => {return t.splitText(t.length - 1);},
198 noSplitTarget: (t) => {return t.splitText(t.length);},
205 edge: (i, l) => {return i == l - 1;},
206 enter: (l) => {return 0;},
207 splitTarget: (t) => {return t.splitText(1);},
208 noSplitTarget: (t) => {return t;},
213 if (!this.attached) return;
215 $.wiki.activePerspective().flush();
219 let contents = this.element.parent().contents();
220 let index = contents.index(this.element);
221 let target, moved, oldparent;
223 let parent = this.element.parent()[0];
225 if (opts.edge(index, contents.length)) {
226 // We're at the end -- what to do?
229 if (parent.nodeName == 'EM') {
231 parent = parent.parentNode;
232 contents = $(parent).contents();
233 index = contents.index(oldparent);
238 target = contents[index];
241 while (target !== undefined && target.nodeType == Node.ELEMENT_NODE) {
242 // we've encountered a node.
245 if (target.nodeName == 'EM') {
248 contents = parent.contents();
249 index = opts.enter(contents.length);
250 target = contents[index];
252 // what if it has no elements?
255 index += opts.move; // again, what if end?
256 target = contents[index];
261 // what if editable but empty?
265 if (target !== undefined && target.nodeType == Node.TEXT_NODE ) {
267 target = opts.splitTarget(target);
269 target = opts.noSplitTarget(target);
272 this.element.insertBefore(target);