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 $.wiki.activePerspective().flush();
41 self.element.insertBefore(
43 selection.anchorOffset
48 self.singleClick = false;
53 self.element.on('keydown', function(e) {
58 // we are in <akap> (no going up)
59 // we are in <wyroznienie> (can go up)
60 // we are next to <wyroznienie> (can go inside)
101 // console.log('key', e.key, e.code);
107 return this.element.parent().length;
113 p = this.element.parent()[0]
114 this.element.detach();
120 $("textarea", self.element).focus();
124 this.element.parent()[0].normalize();
128 elem.insertBefore(this.element);
133 $(document.createTextNode(ch))
139 let contents = this.element.parent().contents();
140 // Find the text before caret.
141 let textBefore = contents[contents.index(this.element) - 1];
143 // Should be text, but what if not?
144 textBefore.textContent = textBefore.textContent.substr(0, textBefore.textContent.length - 1);
150 let contents = this.element.parent().contents();
151 // Find the text after caret.
152 let textAfter = contents[contents.index(this.element) + 1];
153 textAfter.textContent = textAfter.textContent.substr(1);
157 let splitter = this.element;
158 let parent, newParent, splitIndex, index;
160 while (!splitter.is('div[x-node]')) {
161 parent = splitter.parent();
162 splitIndex = parent.contents().index(splitter);
164 if (parent.is('[x-annotation-box]')) {
165 // We're splitting inside an inline-style annotation.
166 // Convert into a block-style annotation now.
167 let p = $('<div x-editable="true" x-node="akap">');
168 parent.contents().appendTo(p);
174 newParent = parent.clone();
175 index = parent.contents().length - 1;
176 while (index >= splitIndex) {
177 newParent.contents()[index].remove();
181 parent.contents()[index].remove();
184 newParent.insertBefore(parent);
193 edge: (i, l) => {return !i;},
194 enter: (l) => {return l - 1;},
195 splitTarget: (t) => {return t.splitText(t.length - 1);},
196 noSplitTarget: (t) => {return t.splitText(t.length);},
203 edge: (i, l) => {return i == l - 1;},
204 enter: (l) => {return 0;},
205 splitTarget: (t) => {return t.splitText(1);},
206 noSplitTarget: (t) => {return t;},
211 if (!this.attached) return;
213 $.wiki.activePerspective().flush();
217 let contents = this.element.parent().contents();
218 let index = contents.index(this.element);
219 let target, moved, oldparent;
221 let parent = this.element.parent()[0];
223 if (opts.edge(index, contents.length)) {
224 // We're at the end -- what to do?
227 if (parent.nodeName == 'EM') {
229 parent = parent.parentNode;
230 contents = $(parent).contents();
231 index = contents.index(oldparent);
236 target = contents[index];
239 while (target !== undefined && target.nodeType == Node.ELEMENT_NODE) {
240 // we've encountered a node.
243 if (target.nodeName == 'EM') {
246 contents = parent.contents();
247 index = opts.enter(contents.length);
248 target = contents[index];
250 // what if it has no elements?
253 index += opts.move; // again, what if end?
254 target = contents[index];
259 // what if editable but empty?
263 if (target !== undefined && target.nodeType == Node.TEXT_NODE ) {
265 target = opts.splitTarget(target);
267 target = opts.noSplitTarget(target);
270 this.element.insertBefore(target);