-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Changes from 7 commits
873e5d8
6134914
f0a7044
0fa465e
b1f0598
85f997a
05b88d1
1ac9c01
6cf0547
7b60218
0a29238
025c656
4fa6ba7
a203709
0ace81d
301b477
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,7 +21,7 @@ var cEditor = (function (cEditor) { | |
wrapper : null, | ||
toolbar : null, | ||
toolbarButtons : {}, // { type : DomEl, ... } | ||
redactor : null | ||
redactor : null, | ||
} | ||
|
||
// Current editor state | ||
|
@@ -211,24 +211,43 @@ cEditor.ui = { | |
|
||
/** Mouse click to radactor */ | ||
cEditor.nodes.redactor.addEventListener('click', function (event) { | ||
|
||
cEditor.callback.redactorClicked(event); | ||
|
||
cEditor.content.saveCaretPosition(); | ||
// console.log(cEditor.content.caretOffset, cEditor.content.focusedNodeIndex); | ||
cEditor.caret.save(); | ||
|
||
}, 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 */ | ||
cEditor.caret.save(); | ||
|
||
cEditor.callback.redactorInputEvent(event); | ||
|
||
}, false ); | ||
|
||
/** Bind click listeners on toolbar buttons */ | ||
for (button in cEditor.nodes.toolbarButtons){ | ||
cEditor.nodes.toolbarButtons[button].addEventListener('click', function (event) { | ||
cEditor.callback.toolbarButtonClicked(event, this); | ||
}, false); | ||
} | ||
}; | ||
|
||
}, | ||
|
||
addBlockHandlers : function(block) { | ||
|
||
block.addEventListener('keydown', function(event) { | ||
|
||
cEditor.caret.save(); | ||
cEditor.callback.blockTransition(event, block); | ||
|
||
}, false); | ||
|
||
} | ||
|
||
|
@@ -242,9 +261,9 @@ cEditor.callback = { | |
globalKeydown : function(event){ | ||
|
||
switch (event.keyCode){ | ||
case cEditor.core.keys.TAB : this.tabKeyPressed(event); break; | ||
case cEditor.core.keys.ENTER : this.enterKeyPressed(event); break; | ||
case cEditor.core.keys.ESC : this.escapeKeyPressed(event); break; | ||
case cEditor.core.keys.TAB : this.tabKeyPressed(event); break; | ||
case cEditor.core.keys.ENTER : this.enterKeyPressed(event); break; | ||
case cEditor.core.keys.ESC : this.escapeKeyPressed(event); break; | ||
} | ||
|
||
}, | ||
|
@@ -253,6 +272,8 @@ cEditor.callback = { | |
|
||
switch (event.keyCode){ | ||
case cEditor.core.keys.UP : | ||
case cEditor.core.keys.LEFT : | ||
case cEditor.core.keys.RIGHT : | ||
case cEditor.core.keys.DOWN : this.arrowKeyPressed(event); break; | ||
} | ||
|
||
|
@@ -298,6 +319,7 @@ cEditor.callback = { | |
|
||
cEditor.content.workingNodeChanged(); | ||
|
||
/* Closing toolbar */ | ||
cEditor.toolbar.close(); | ||
cEditor.toolbar.move(); | ||
|
||
|
@@ -344,7 +366,26 @@ 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.caret.setToNextBlock( block ); | ||
} | ||
|
||
else if (event.keyCode == cEditor.core.keys.UP || event.keyCode == cEditor.core.keys.LEFT ) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. else if отвалился |
||
|
||
if ( block.previousSibling == null ) | ||
return ; | ||
|
||
cEditor.caret.setToPreviousBlock( block ); | ||
} | ||
}, | ||
|
||
}; | ||
|
||
|
@@ -353,16 +394,6 @@ cEditor.content = { | |
|
||
currentNode : null, | ||
|
||
/** | ||
* @var {int} caretOffset - caret position in a text node. | ||
*/ | ||
caretOffset : null, | ||
|
||
/** | ||
* @var {int} focusedNodeIndex - we get index of child node from first-level block | ||
*/ | ||
focusedNodeIndex: null, | ||
|
||
/** | ||
* Synchronizes redactor with original textarea | ||
*/ | ||
|
@@ -382,54 +413,6 @@ cEditor.content = { | |
|
||
}, | ||
|
||
/** | ||
* We need to save caret before we change the block, | ||
* so that we could return it to original position in a new tag. | ||
* We save caret offset in a text and index of child node. | ||
*/ | ||
saveCaretPosition () { | ||
|
||
var selection = window.getSelection(); | ||
var previousElement = selection.anchorNode.previousSibling, | ||
nodeIndex = 0; | ||
|
||
while (previousElement != null) { | ||
|
||
nodeIndex ++; | ||
previousElement = previousElement.previousSibling; | ||
|
||
} | ||
|
||
this.caretOffset = selection.anchorOffset; | ||
this.focusedNodeIndex = nodeIndex; | ||
|
||
}, | ||
|
||
/** | ||
* Creates Documnt Range and sets caret to the NodeElement. | ||
* @param {Element} NodeElement - Changed Node. | ||
*/ | ||
|
||
setCaret : function(NodeElement) { | ||
|
||
var nodeIndex = this.focusedNodeIndex || 0, | ||
caretOffset = this.caretOffset || 0; | ||
|
||
var childs = NodeElement.childNodes, | ||
nodeChild = childs[nodeIndex]; | ||
|
||
console.log(caretOffset, nodeChild); | ||
|
||
var range = document.createRange(), | ||
selection = window.getSelection(); | ||
|
||
range.setStart(nodeChild, caretOffset); | ||
range.setEnd(nodeChild, caretOffset); | ||
|
||
selection.removeAllRanges(); | ||
selection.addRange(range); | ||
}, | ||
|
||
getNodeFocused : function() { | ||
|
||
var selection = window.getSelection(), | ||
|
@@ -506,7 +489,7 @@ cEditor.content = { | |
/** | ||
* Setting caret | ||
*/ | ||
cEditor.content.setCaret(nodeCreated); | ||
cEditor.caret.set(nodeCreated); | ||
|
||
return; | ||
|
||
|
@@ -535,11 +518,153 @@ cEditor.content = { | |
*/ | ||
cEditor.content.workingNodeChanged(nodeCreated); | ||
|
||
cEditor.content.setCaret(nodeCreated); | ||
} | ||
cEditor.caret.set(nodeCreated); | ||
}, | ||
|
||
} | ||
|
||
cEditor.caret = { | ||
|
||
/** | ||
* @var {int} caretOffset - caret position in a text node. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. переменная называется по-другому |
||
*/ | ||
|
||
Offset : null, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. почему с большой буквы? |
||
|
||
/** | ||
* @var {int} focusedNodeIndex - we get index of child node from first-level block | ||
*/ | ||
|
||
focusedNodeIndex: null, | ||
|
||
/** | ||
* We need to save caret before we change the block, | ||
* so that we could return it to original position in a new tag. | ||
* We save caret offset in a text and index of child node. | ||
*/ | ||
save : function() { | ||
|
||
var selection = window.getSelection(); | ||
var previousElement = selection.anchorNode.previousSibling, | ||
nodeIndex = 0; | ||
|
||
while (previousElement != null) { | ||
|
||
nodeIndex ++; | ||
previousElement = previousElement.previousSibling; | ||
|
||
} | ||
|
||
this.Offset = selection.anchorOffset; | ||
this.focusedNodeIndex = nodeIndex; | ||
|
||
}, | ||
|
||
/** | ||
* Creates Documnt Range and sets caret to the NodeElement. | ||
* @param {Element} NodeElement - Changed Node. | ||
*/ | ||
set : function(NodeElement) { | ||
|
||
var nodeIndex = this.focusedNodeIndex || 0, | ||
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(); | ||
|
||
range.setStart(nodeChild, caretOffset); | ||
range.setEnd(nodeChild, caretOffset); | ||
|
||
selection.removeAllRanges(); | ||
selection.addRange(range); | ||
}, | ||
|
||
setToNextBlock : 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.caret.Offset != selection.anchorNode.length | ||
&& typeof( selection.anchorNode.length ) != 'undefined') { | ||
cEditor.caret.save(); | ||
return ; | ||
|
||
} | ||
|
||
/* Setting Caret to the first child of next node */ | ||
else if ( selection.focusNode.length == cEditor.caret.Offset ) { | ||
|
||
/** Firstchild of block */ | ||
cEditor.caret.Offset = 0; | ||
cEditor.caret.focusedNodeIndex = 0; | ||
|
||
cEditor.caret.set(block.nextSibling); | ||
|
||
} else { | ||
|
||
/** Firstchild of block */ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. чем этот esle отличается от предыдущего else if ? |
||
cEditor.caret.Offset = 0; | ||
cEditor.caret.focusedNodeIndex = 0; | ||
cEditor.caret.set(block.nextSibling); | ||
} | ||
}, | ||
|
||
setToPreviousBlock : 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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. нет, не |
||
* We should check check caret position before we transmit/switch the block. | ||
*/ | ||
|
||
if ( cEditor.caret.Offset != 0 | ||
&& typeof( selection.anchorNode.length ) != 'undefined') { | ||
|
||
cEditor.caret.save(); | ||
return ; | ||
|
||
} | ||
|
||
/* Setting Caret to the first child of previous node */ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. этот комментарий не относится к условию. Тут должен быть комментарий, поясняющий условие |
||
else if ( selection.anchorOffset == 0 | ||
&& cEditor.caret.focusedNodeIndex == 0 | ||
&& typeof( block.previousSibling.childNodes.length ) != 'undefined') { | ||
|
||
cEditor.caret.focusedNodeIndex = (block.previousSibling.childNodes.length == 0) ? 0 : block.previousSibling.childNodes.length - 1; | ||
cEditor.caret.Offset = (block.previousSibling.childNodes.length == 0) ? 0 : block.previousSibling.childNodes[cEditor.caret.focusedNodeIndex].length; | ||
|
||
cEditor.caret.set(block.previousSibling); | ||
|
||
} | ||
else { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
cEditor.caret.Offset = 0; | ||
cEditor.caret.focusedNodeIndex = 0; | ||
|
||
cEditor.caret.set(block.previousSibling); | ||
|
||
} | ||
}, | ||
}; | ||
|
||
cEditor.toolbar = { | ||
|
||
/** | ||
|
@@ -768,9 +893,13 @@ cEditor.parser = { | |
|
||
/** Save block to the cEditor.state array */ | ||
cEditor.state.blocks.push(block); | ||
|
||
// return block; | ||
cEditor.ui.addBlockHandlers(block); | ||
}; | ||
|
||
}) | ||
// .then(cEditor.ui.addBlockHandlers(block)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ты путаешь назначение функции обработчиком и ее вызов. Сейчас ты вызываешь ее |
||
|
||
/** Log if something wrong with node */ | ||
.catch(function(error) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
надо вынести обработчик кейдауна в отдельную функцию, а в ней уже вызывать необходимую логику.
например
blockKeydownHandler