-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
Split blocks #246
Split blocks #246
Conversation
src/components/modules/keyboard.js
Outdated
|
||
super({config}); | ||
|
||
document.body.addEventListener('keydown', this.keyBoardListener.bind(this)); |
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.
можно коллбэк передать так: (event) => { this.keyBoardListener(event) });
src/components/modules/keyboard.js
Outdated
|
||
switch(event.keyCode) { | ||
|
||
case (8): |
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.
в utils у нас есть константы:
_.keyCodes.ENTER, _.keyCodes.BACKSPACE ....
src/components/modules/keyboard.js
Outdated
|
||
case (13): | ||
console.log('enter pressed'); | ||
|
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.
много пустых строк
src/components/modules/keyboard.js
Outdated
|
||
event.preventDefault(); | ||
|
||
this.Editor.BlockManager.insert('text', this.getDataFromRange()); |
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.
Мне кажется лучше добавить еще одну функцию, например, enterPressed()
а в ней уже делать insert. Просто возможно при нажатии на ентер будут происходить еще другие события.
src/components/modules/keyboard.js
Outdated
} | ||
|
||
/** | ||
* Gets data from blocks |
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.
Функцию стоит нормально задокументировать. Описать что происходит и как
src/components/modules/keyboard.js
Outdated
*/ | ||
getDataFromRange() { | ||
|
||
let selection = window.getSelection(); |
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.
Для работы с селекшном у нас есть класс, добавь его перед строчкой с exports
import Selection from '../Selection'
let selection = Selection.get()
src/components/modules/keyboard.js
Outdated
getDataFromRange() { | ||
|
||
let selection = window.getSelection(); | ||
let range = new Range(); |
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.
можно через запятую объявить переменную, let selection= ..., range = new Range()
src/components/modules/keyboard.js
Outdated
let selection = window.getSelection(); | ||
let range = new Range(); | ||
|
||
let cnt = this.Editor.BlockManager.currentBlock.pluginsContent, |
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.
название переменной не очень удачное. Можно javascript let pluginsContent = ....., lastNode = $.getDeepestNode(pluginsContent, true) -> описать почему true
src/components/modules/keyboard.js
Outdated
selection.addRange(range); | ||
|
||
let fragm = range.extractContents(); | ||
let div = document.createElement('div'); |
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.
let extractedFragment = range.extractContenst(),
wrapper = $.make('div', []. {});
wrapper.append(extractedFragment.cloneNode(true));
.....```
src/components/modules/keyboard.js
Outdated
|
||
super({config}); | ||
|
||
document.body.addEventListener('keydown', this.keyBoardListener.bind(this)); |
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.
document.body.addEventListener('keydown', event => this.keyBoardListener(event));
src/components/modules/keyboard.js
Outdated
} | ||
|
||
/** | ||
* handler processes special keyboard keys |
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.
What does it mean?
src/components/modules/keyboard.js
Outdated
|
||
switch(event.keyCode) { | ||
|
||
case (8): |
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.
use Utils#keyCodes
instead of Magic numbers
src/components/modules/keyboard.js
Outdated
|
||
event.preventDefault(); | ||
|
||
this.Editor.BlockManager.insert('text', this.getDataFromRange()); |
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.
new block will be inserted and the end of the Editor or below current block?
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.
Ниже активного блока. Функция insert() сама это делает
src/components/modules/keyboard.js
Outdated
/** | ||
* Gets data from blocks | ||
*/ | ||
getDataFromRange() { |
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.
This method should be in the BlockManager class. And better name it split
А вообще, мне кажется будет правильнее сделать так: extractDataFromCaretPosition (или как-то так) () {
// сюда перенести только выделение и extractContents
return fragment; // вернуть только вырезанный фрагмент
}
// В блок-менеджере будет функция split
split() {
let extractedFragment = this.Editor.Caret.extractFromCaretPosition();
// создание data
this.insert('text', data);
} В keyboards: enterPressed() {
this.Editor.BlockManager.split();
// дальше делает что-то другое, например, переносим тулбар
} |
selection.removeAllRanges(); | ||
selection.addRange(range); | ||
|
||
let extractedFragment = range.extractContents(), |
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.
нужно в модуле Caret создать универсальную функцию, которая вырежет любой фрагмент из блока. Для начала можно просто создать extractFragmentFromCaretPosition()
и вынести код до extractContents в эту функцию (extractFragmentFromCaretPosition
)
src/components/modules/keyboard.js
Outdated
|
||
super({config}); | ||
|
||
document.body.addEventListener('keydown', event => this.keyBoardListener(event)); |
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.
javascript this.Editor.Listener.on(document.body, 'keydown', event => this.keyBoardListener(event));
src/components/modules/keyboard.js
Outdated
case _.keyCodes.ENTER: | ||
|
||
_.log('Enter key pressed'); | ||
event.preventDefault(); |
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.
preventDefault лучше занести в enterPressed(). Не всегда нужно прерывать дефолтное поведение
src/components/modules/keyboard.js
Outdated
case _.keyCodes.RIGHT: | ||
|
||
_.log('Right key pressed'); | ||
this.Editor.BlockManager.navigateNext(); |
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.
case _.keyCodes.DOWN:
case _.keyCodes.RIGHT:
_.log('Right and DOWN key pressed');
this.arrowRightAndDownPressed();
break;
}
arrowRightAndDownPressed() {
this.Editor.BlockManager.navigateNext();
}
то же самое с UP и LEFT
let block = this.composeBlock(toolName, data); | ||
|
||
this._blocks[++this.currentBlockIndex] = block; | ||
this.Editor.Caret.setToBlock(block); | ||
|
||
} | ||
|
||
/** | ||
* Insert extract content form current block to below current block |
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.
insert extracted content from current block to block that is below
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.
or next to the current
src/components/modules/caret.js
Outdated
@@ -116,4 +116,25 @@ export default class Caret extends Module { | |||
|
|||
} | |||
|
|||
/** | |||
* Extract fragment of content current block form caret position |
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.
Extract content fragment of current block from caret position
src/components/modules/caret.js
Outdated
let selection = Selection.get(), | ||
range = new Range(); | ||
|
||
let cnt = this.Editor.BlockManager.currentBlock.pluginsContent, |
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.
cnt -> pluginsContent
src/components/modules/keyboard.js
Outdated
@@ -0,0 +1,98 @@ | |||
import Selection from '../Selection'; |
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.
импорт тут значит не нужен, и это отлично
let block = this.composeBlock(toolName, data); | ||
|
||
this._blocks[++this.currentBlockIndex] = block; | ||
this.Editor.Caret.setToBlock(block); | ||
|
||
} | ||
|
||
/** | ||
* Insert extract content form current block to block that is below |
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.
extracted
и вроде можно убрать that is
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.
И коммент немного не понятный.
Вставляем контент из текущего блока в следующий. Разве этим split занимается?
let extractedFragment = this.Editor.Caret.extractFragmentFromCaretPosition(), | ||
wrapper = $.make('div'); | ||
|
||
wrapper.append(extractedFragment.cloneNode(true)); |
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.
Поясни зачем клонирование
|
||
wrapper.append(extractedFragment.cloneNode(true)); | ||
|
||
let data = { |
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.
Оставь здесь @todo
src/components/modules/caret.js
Outdated
range = new Range(); | ||
|
||
let pluginsContent = this.Editor.BlockManager.currentBlock.pluginsContent, | ||
lastNode = $.getDeepestNode(pluginsContent, true); |
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.
Коммент, что означает true
src/components/modules/keyboard.js
Outdated
} | ||
|
||
/** | ||
* Should be called after Editor.BlockManager preparation |
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.
А почем именно после менеджера?
src/components/modules/keyboard.js
Outdated
} | ||
|
||
/** | ||
* Handler on Editor for keyboard keys |
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.
Не понятно что ловишь. keyup, keydown или еще что.
src/components/modules/keyboard.js
Outdated
*/ | ||
prepare() { | ||
|
||
this.Editor.Listeners.on(document.body, 'keydown', event => { |
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.
Точно необходимо вешать на body?
src/components/modules/keyboard.js
Outdated
*/ | ||
enterPressed(event) { | ||
|
||
event.preventDefault(); |
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.
Оставь @todo, надо будет проверять настройку плагина allowLinebreaks
src/components/modules/keyboard.js
Outdated
} | ||
|
||
/** | ||
* Hand right and down keyboard keys |
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.
handle?
src/components/modules/keyboard.js
Outdated
} | ||
|
||
/** | ||
* Insert new block with data below current block |
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.
Функция же не только для этого
src/components/modules/keyboard.js
Outdated
*/ | ||
prepare() { | ||
|
||
this.Editor.Listeners.on(document.body, 'keydown', event => { |
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.
раньше это событие вешалось на блок, а не на весь документ. Это точно корректное поведение?
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.
меня это тоже смущает, потому что один keydown на весь редактор. Можно это сделать при формировании блока (в BlockManager#composeBlock
) - вешать событие здесь.
let block = this.composeBlock(toolName, data); | ||
|
||
this._blocks[++this.currentBlockIndex] = block; | ||
this.Editor.Caret.setToBlock(block); | ||
|
||
} | ||
|
||
/** | ||
* Create new block below current block and insert extracted content form current block to new block |
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.
Очень сложно. В таких случаях лучше писать прям по пунктам
Split current block
1. Extract content from caret position to block`s end
2. Insert new block below current one with extracted content
src/components/modules/caret.js
Outdated
@@ -116,4 +116,28 @@ export default class Caret extends Module { | |||
|
|||
} | |||
|
|||
/** | |||
* Extract content fragment of current block form caret position |
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.
... from caret position to the end of the block
src/components/modules/keyboard.js
Outdated
* @class Keyboard | ||
* @classdesc Сlass to handle the keystrokes | ||
* | ||
* @module Keyboard |
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.
Это можно убрать
src/components/modules/keyboard.js
Outdated
* @author CodeX Team (team@ifmo.su) | ||
* @copyright CodeX Team 2017 | ||
* @license The MIT License (MIT) | ||
* @version 2.0.0 |
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.
Это новый класс, 1 версия
src/components/modules/keyboard.js
Outdated
} | ||
|
||
/** | ||
* Handler on Editor for keyboard keys at keydown event |
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.
Что такое Editor в этом контексте?
И обработчик ты на блок же вешаешь
src/components/modules/keyboard.js
Outdated
enterPressed(event) { | ||
|
||
/** | ||
* @todo check settings of "allowLinebreaks" plugin |
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.
Что-то не понятно, что написано
check plugin`s configuration for allowLinebreaks
property
src/components/modules/keyboard.js
Outdated
*/ | ||
event.preventDefault(); | ||
/** | ||
* Insert new block with data below current block |
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.
Оставь просто split current block into two ones
break; | ||
|
||
} | ||
this.Editor.Listeners.on(block.pluginsContent, 'keydown', (event) => this.Editor.Keyboard.keyboardListener(event)); |
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.
blockKeydownsListener
let block = this.composeBlock(toolName, data); | ||
|
||
this._blocks[++this.currentBlockIndex] = block; | ||
this.Editor.Caret.setToBlock(block); | ||
|
||
} | ||
|
||
/** | ||
* Split current block | ||
* 1. Extract content from caret position to block`s end |
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.
to the
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.
Caret , Block
/** | ||
* Split current block | ||
* 1. Extract content from caret position to block`s end | ||
* 2. Insert new block below current one with extracted content |
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.
a new Block
wrapper.append(extractedFragment); | ||
|
||
/** | ||
* @todo make object in accordance with the plugin |
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.
with Tool
text: wrapper.innerHTML, | ||
}; | ||
|
||
this.insert('text', data); |
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.
inititalTool
src/components/modules/caret.js
Outdated
@@ -116,4 +116,28 @@ export default class Caret extends Module { | |||
|
|||
} | |||
|
|||
/** | |||
* Extract content fragment of current block from caret position to the end of the block |
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.
Block, Caret,
src/components/modules/keyboard.js
Outdated
@@ -0,0 +1,103 @@ | |||
/** | |||
* @class Keyboard | |||
* @classdesc Сlass to handle the keystrokes |
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.
keydowns?
src/components/modules/keyboard.js
Outdated
} | ||
|
||
/** | ||
* Handler on block for keyboard keys at keydown event |
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.
Block
src/components/modules/keyboard.js
Outdated
enterPressed(event) { | ||
|
||
/** | ||
* @todo check plugin`s configuration for allowLinebreaks property |
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.
Tool's
src/components/modules/keyboard.js
Outdated
*/ | ||
event.preventDefault(); | ||
/** | ||
* Split current block into two ones |
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.
Split the Current Block.
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.
Некорректное поведение при попытке сплита с выделенным текстом (Safari)
https://giphy.com/gifs/3oFzmnols9OJd6AKPe
closes #229
closes #247