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

Backspace handler: Merge blocks #254

Merged
merged 33 commits into from
May 29, 2018
Merged

Backspace handler: Merge blocks #254

merged 33 commits into from
May 29, 2018

Conversation

khaydarov
Copy link
Member

No description provided.

@khaydarov khaydarov changed the base branch from master to rewriting-version2.0 May 22, 2018 13:26
/**
* Merge two blocks
* @param {Block} targetBlock - block to merge
* @param {Block} mergingBlock - block that will be merged with target block
Copy link
Member

Choose a reason for hiding this comment

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

Может лучше blockToMerge?

extractedBlock = range.extractContents();

targetBlock.pluginsContent.appendChild(extractedBlock);
targetBlock.pluginsContent.normalize();
Copy link
Member

Choose a reason for hiding this comment

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

А никак не сделать это без прямых манипуляций с DOM-деревом?

* Check passed node if it has IMG, Twitter, iframe or something that could contain media
* @param target
*/
static hasMediaContent(target) {
Copy link
Member

Choose a reason for hiding this comment

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

уже есть, см класс Block@hasMedia

@@ -185,6 +185,65 @@ export default class BlockManager extends Module {

}

/**
* Merge two blocks
* @param {Block} targetBlock - block to merge
Copy link
Member

Choose a reason for hiding this comment

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

block to merge это название второго параметра. Нужно внести ясность


}

if (!$.isEmpty(blockToMerge .html)) {
Copy link
Member

Choose a reason for hiding this comment

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

пробел перед точкой

Copy link
Member

Choose a reason for hiding this comment

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

и для проверки пустоты у класса Block есть метод isEmpty


if (!$.isEmpty(blockToMerge .html)) {

let selection = Selection.get(),
Copy link
Member

Choose a reason for hiding this comment

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

возможно тебе подойдет метод

this.Editor.Caret.extractFragmentFromCaretPosition()

this.currentNode = this._blocks[this.currentBlockIndex].html;

// set caret to the block without offset at the end
this.Editor.Caret.setToBlock(this.currentBlock, 0, true);
Copy link
Member

Choose a reason for hiding this comment

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

каретка и тулбар не относятся к removeBlock, нужно перенести их в функцию, которая обрабатывает конкретное действие, например backspace

@@ -93,11 +94,36 @@ export default class Keyboard extends Module {

}

if (this.Editor.Caret.isAtEnd) {
Copy link
Member

Choose a reason for hiding this comment

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

это точно необходимо? В центре блока тоже должен работать Сплит

this._blocks.remove(index);

// decrease current block index so that to know current actual
this.currentBlockIndex--;
Copy link
Member

Choose a reason for hiding this comment

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

не относится к этому методу

* Call plugins merge method
* @param {Object} data
*/
mergeWith(data) {
Copy link
Member

Choose a reason for hiding this comment

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

Почему не просто merge?

mergeWith(data) {

return Promise.resolve()
.then( () => {
Copy link
Member

Choose a reason for hiding this comment

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

Можно сразу в resolve() функцию передать

Copy link
Member

Choose a reason for hiding this comment

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

Или так

Promise.resolve(data).then(this.tool.merge)

if (blockToMerge.isEmpty) {

this.removeBlock(blockToMergeIndex);
return Promise.resolve();
Copy link
Member

Choose a reason for hiding this comment

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

Если сделать функцию асинхронной, можно не возвращать Promise.resolve()

}

return blockToMerge.data
.then((blockToMergeInfo) => {
Copy link
Member

Choose a reason for hiding this comment

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

Точно ли промис вернется?
В data просто вызывается save(), может там стоит промисифицировать, чтобы наверняка?

*/
getBlockIndex(block) {

for(let i = 0; i < this._blocks.length; i++) {
Copy link
Member

Choose a reason for hiding this comment

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

indexOf тоже должен работать

this.Editor.Caret.setToBlock(this.Editor.BlockManager.currentBlock, 0, true);
this.Editor.Toolbar.close();

}, 10);
Copy link
Member

Choose a reason for hiding this comment

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

Это на промисах не сделать?

.then((blockToMergeInfo) => {

targetBlock.mergeWith(blockToMergeInfo.data);
this.removeBlock(blockToMergeIndex);
Copy link
Member

Choose a reason for hiding this comment

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

можно оптимизировать дублирование remove с помощью then

@@ -201,9 +248,15 @@ export default class BlockManager extends Module {
* @todo make object in accordance with Tool
*/
let data = {
text: wrapper.innerHTML,
text: wrapper.textContent.trim() === '' ? '' : wrapper.innerHTML,
Copy link
Member

Choose a reason for hiding this comment

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

на пустоту проверяем через this.currentBlock.isEmpty

};

if (this.currentBlock.isEmpty) {

this.currentBlock.pluginsContent.innerHTML = '';
Copy link
Member

Choose a reason for hiding this comment

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

манипуляция с домом, дублирование проверки на пустоту

* @param {Block} block
* @return {Number}
*/
getBlockIndex(block) {
Copy link
Member

Choose a reason for hiding this comment

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

такой метод уже есть в классе Block

* Handle backspace keypress on block
* @param event
*/
backSpacePressed(event) {
Copy link
Member

Choose a reason for hiding this comment

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

backspacePressed

backSpacePressed(event) {

let isFirstBlock = this.Editor.BlockManager.currentBlockIndex === 0,
canMergeBlocks = !this.Editor.BlockManager.currentBlock.hasMedia && this.Editor.Caret.isAtStart && !isFirstBlock;
Copy link
Member

Choose a reason for hiding this comment

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

!hasMedia -> !isEmpty

blockToMerge = this.Editor.BlockManager.currentBlock;


if (blockToMerge.name !== targetBlock.name || !this.Editor.BlockManager.currentBlock.mergeable) {
Copy link
Member

Choose a reason for hiding this comment

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

this.Editor.BlockManager.currentBlock -> targetBlock


} else {

textNodeLength = lastNode.length;
Copy link
Member

Choose a reason for hiding this comment

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

У текстового узла тоже есть свойство textContent

let child = atLast ? 'lastChild' : 'firstChild',
sibling = atLast ? 'previousSibling' : 'nextSibling';

console.log('node is ', node);
Copy link
Member

Choose a reason for hiding this comment

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

Лог

*/
backspacePressed(event) {

let isFirstBlock = this.Editor.BlockManager.currentBlockIndex === 0,
Copy link
Member

Choose a reason for hiding this comment

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

Если в начале функции деконструировать BlockManager, можно довольно сильно сократить код.

.then( () => {

// decrease current block index so that to know current actual
BM.currentBlockIndex--;
Copy link
Member

Choose a reason for hiding this comment

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

Думаю, это не должно здесь быть

@khaydarov khaydarov merged commit d8747e5 into rewriting-version2.0 May 29, 2018
@khaydarov khaydarov deleted the daily branch May 29, 2018 18:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants