Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Переход курсора на следующий и предыдущий блоки при нажатии на кнопки вверх/вниз #32

Merged
merged 16 commits into from
Jun 10, 2016
Prev Previous commit
Next Next commit
block handler
  • Loading branch information
khaydarov committed Jun 6, 2016
commit 85f997a63794a4455c77da50c6f4eaab8e71c212
188 changes: 124 additions & 64 deletions codex-editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ var cEditor = (function (cEditor) {
wrapper : null,
toolbar : null,
toolbarButtons : {}, // { type : DomEl, ... }
redactor : null
redactor : null,
}

// Current editor state
Expand Down Expand Up @@ -211,13 +211,17 @@ cEditor.ui = {

/** Mouse click to radactor */
cEditor.nodes.redactor.addEventListener('click', function (event) {

cEditor.callback.redactorClicked(event);

cEditor.content.saveCaretPosition();

}, false );

/** Any redactor changes: keyboard input, mouse cut/paste, drag-n-drop text */
/**
* @deprecated;
* Any redactor changes: keyboard input, mouse cut/paste, drag-n-drop text
*/
cEditor.nodes.redactor.addEventListener('input', function (event) {

/** Saving caret in every modifications */
Expand All @@ -234,6 +238,17 @@ cEditor.ui = {
}, false);
};

},

addBlockHandlers : function(block) {

block.addEventListener('keydown', function(event) {

cEditor.content.saveCaretPosition();
cEditor.callback.blockTransition(event, block);

}, false);

}

};
Expand Down Expand Up @@ -308,63 +323,6 @@ cEditor.callback = {
cEditor.toolbar.close();
cEditor.toolbar.move();

/* Changing focused block to Previous or Next Node */
var selection = window.getSelection(),
focusedElement = selection.anchorNode.parentNode;

if (event.keyCode == cEditor.core.keys.DOWN || event.keyCode == cEditor.core.keys.RIGHT ) {

/** Stop transition when caret is not at the end of Text node
* When we click "DOWN", caret moves to the end of node.
* We should check check caret position before we transmit/switch the block.
*/
if ( cEditor.content.caretOffset != selection.anchorNode.length) {
cEditor.content.saveCaretPosition();
return ;
}

/* Get parents until we didn't find nextSibling to switch caret */
while ( focusedElement.nextSibling == null ) {
focusedElement = focusedElement.parentNode;
}

/* Setting Caret to the first child of next node */
if ( selection.focusNode.length == selection.anchorOffset ) {

cEditor.content.caretOffset = 0;
cEditor.content.focusedNodeIndex = 0;

cEditor.content.setCaret(focusedElement.nextSibling);
}

}
else if (event.keyCode == cEditor.core.keys.UP || event.keyCode == cEditor.core.keys.LEFT ) {

/** Stop transition when caret is not at the beggining of Text node
* When we click "UP", caret moves to the end of node.
* We should check check caret position before we transmit/switch the block.
*/
if ( cEditor.content.caretOffset != 0 ) {
cEditor.content.saveCaretPosition();
return ;
}

/* Get parents until we didn't find nextSibling to switch caret */
while ( focusedElement.previousSibling == null ) {
focusedElement = focusedElement.parentNode;
}

/* Setting Caret to the first child of previous node */
if ( selection.anchorOffset == 0 ) {

cEditor.content.focusedNodeIndex = focusedElement.previousSibling.childNodes.length - 1;
cEditor.content.caretOffset = focusedElement.previousSibling.childNodes[cEditor.content.focusedNodeIndex].length;

cEditor.content.setCaret(focusedElement.previousSibling);

}

}
},

redactorClicked : function (event) {
Expand Down Expand Up @@ -408,7 +366,27 @@ cEditor.callback = {

}, 500);

}
},

blockTransition : function(event, block) {

if (event.keyCode == cEditor.core.keys.DOWN || event.keyCode == cEditor.core.keys.RIGHT ) {


if ( block.nextSibling == null )
return ;

cEditor.content.setToNextSibling( block );
Copy link
Member

@neSpecc neSpecc Jun 6, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

установить в следующего брата. Что установить? Каретку? тогда setCaretToNextBlock и setCaretToPreviousBlock

}

else if (event.keyCode == cEditor.core.keys.UP || event.keyCode == cEditor.core.keys.LEFT ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

else if отвалился


if ( block.previousSibling == null )
return ;

cEditor.content.setToPreviousSibling( block );
}
},

};

Expand Down Expand Up @@ -477,11 +455,19 @@ cEditor.content = {
setCaret : function(NodeElement) {

var nodeIndex = this.focusedNodeIndex || 0,
caretOffset = this.caretOffset || 0;

var childs = NodeElement.childNodes,
caretOffset = this.caretOffset || 0,
childs = NodeElement.childNodes,
nodeChild = childs[nodeIndex];

var range = document.createRange();

if ( NodeElement.childNodes.length == 0 ) {

nodeChild = NodeElement;
caretOffset = 0;

}

var range = document.createRange(),
selection = window.getSelection();

Expand Down Expand Up @@ -598,6 +584,79 @@ cEditor.content = {
cEditor.content.workingNodeChanged(nodeCreated);

cEditor.content.setCaret(nodeCreated);
},

setToNextSibling : function(block) {

var selection = window.getSelection(),
focusedElement = selection.anchorNode.parentNode;

/** Stop transition when caret is not at the end of Text node
* When we click "DOWN", caret moves to the end of node.
* We should check check caret position before we transmit/switch the block.
*/
if ( cEditor.content.caretOffset != selection.anchorNode.length
&& typeof( selection.anchorNode.length ) != 'undefined') {

cEditor.content.saveCaretPosition();
return ;

}

/* Setting Caret to the first child of next node */
else if ( selection.focusNode.length == cEditor.content.caretOffset ) {

cEditor.content.caretOffset = 0;
cEditor.content.focusedNodeIndex = 0;

cEditor.content.setCaret(block.nextSibling);

} else {

cEditor.content.caretOffset = 0;
cEditor.content.focusedNodeIndex = 0;

cEditor.content.setCaret(block.nextSibling);
}
},

setToPreviousSibling : function(block) {

var selection = window.getSelection(),
focusedElement = selection.anchorNode.parentNode;

/** Stop transition when caret is not at the beggining of Text node
* When we click "UP", caret moves to the end of node.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

нет, не to the end

* We should check check caret position before we transmit/switch the block.
*/

cEditor.content.saveCaretPosition();

if ( cEditor.content.caretOffset != 0
&& typeof( selection.anchorNode.length ) != 'undefined') {
cEditor.content.saveCaretPosition();
return ;

}

/* Setting Caret to the first child of previous node */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

этот комментарий не относится к условию. Тут должен быть комментарий, поясняющий условие

else if ( selection.anchorOffset == 0
&& typeof( block.previousSibling.length ) != 'undefined') {

cEditor.content.focusedNodeIndex = block.previousSibling.childNodes.length - 1;
cEditor.content.caretOffset = block.previousSibling.childNodes[cEditor.content.focusedNodeIndex].length;

cEditor.content.setCaret(block.previousSibling);

}
else {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

} else {


cEditor.content.focusedNodeIndex = 0;
cEditor.content.caretOffset = 0;

cEditor.content.setCaret(block.previousSibling);

}
}

}
Expand Down Expand Up @@ -830,8 +889,9 @@ cEditor.parser = {

/** Save block to the cEditor.state array */
cEditor.state.blocks.push(block);
};

cEditor.ui.addBlockHandlers(block);
};
})

/** Log if something wrong with node */
Expand Down