self.view = view;
self.singleClick = false;
- let caret = this.element = $('<span id="caret"><textarea></textarea></span>');
+ let caret = this.element = $('<nobr><span id="caret"><textarea></textarea></span></nobr>');
// When user writes into caret, add it to the document.
$('textarea', caret).on('input', function() {
// On click on x-node element, set caret position.
self.view.on('click', '*[x-node]', function(e) {
- if (e.redakcja_caret_inserted) return;
- e.redakcja_caret_inserted = true;
+ if (e.redakcja_caret_ignore) return;
+ e.redakcja_caret_ignore = true;
if (self.singleClick) {
self.singleClick = false;
var selection = window.getSelection();
if (!selection.isCollapsed) return;
+ var anchorNode = selection.anchorNode;
+ if (anchorNode.nodeType != Node.TEXT_NODE) return;
+ // Is selection still inside a node?
+ if (!$(anchorNode).closest('[x-node]').length) return;
+ if ($(anchorNode).parents('[x-annotation-box]').not('.editing').length) return;
self.singleClick = true;
setTimeout(function() {
if (self.singleClick) {
self.element.insertBefore(
- selection.anchorNode.splitText(
+ anchorNode.splitText(
selection.anchorOffset
)
)
});
self.element.on('keydown', function(e) {
- console.log('KEY');
-
// TODO:
// delete selection?
}
detach() {
- console.log(this.view);
-
let p;
if (this.attached) {
p = this.element.parent()[0]
this.element.parent()[0].normalize();
}
+ insert(elem) {
+ elem.insertBefore(this.element);
+ }
+
insertChar(ch) {
- $(document.createTextNode(ch)).insertBefore(this.element);
+ this.insert(
+ $(document.createTextNode(ch))
+ );
this.normalize();
}
-
+
deleteBefore() {
let contents = this.element.parent().contents();
// Find the text before caret.
splitBlock() {
let splitter = this.element;
let parent, newParent, splitIndex, index;
-
+
while (!splitter.is('div[x-node]')) {
parent = splitter.parent();
splitIndex = parent.contents().index(splitter);
+
+ if (parent.is('[x-annotation-box]')) {
+ // We're splitting inside an inline-style annotation.
+ // Convert into a block-style annotation now.
+ let p = $('<div x-editable="true" x-node="akap">');
+ parent.contents().appendTo(p);
+ parent.empty();
+ parent.append(p);
+ parent = p;
+ }
+
newParent = parent.clone();
index = parent.contents().length - 1;
while (index >= splitIndex) {
--index;
}
while (index >= 0) {
- console.log(newParent, index);
parent.contents()[index].remove();
-- index;
}
newParent.insertBefore(parent);
- console.log('split', parent);
splitter = parent;
}
}
let target, moved, oldparent;
let parent = this.element.parent()[0];
-
+
if (opts.edge(index, contents.length)) {
// We're at the end -- what to do?
// can we go up?
target = contents[index];
moved = false;
- while (target.nodeType == 1) {
+ while (target !== undefined && target.nodeType == Node.ELEMENT_NODE) {
// we've encountered a node.
// can we go inside?
}
- if (target.nodeType == 3) {
+ if (target !== undefined && target.nodeType == Node.TEXT_NODE ) {
if (!moved) {
target = opts.splitTarget(target);
} else {