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

use flipper to refactor #842

Merged
merged 7 commits into from
Jul 9, 2019
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26,134 changes: 16 additions & 26,118 deletions dist/editor.js

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion dist/editor.js.map

This file was deleted.

142 changes: 135 additions & 7 deletions src/components/flipper.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,141 @@
import Dom from './dom';
import _ from './utils';

/**
* Flipper
* Flipper is a component that iterates passed items array by TAB and clicks it by ENTER
*/
export default class Flipper {
/**
* @type {{RIGHT: string; LEFT: string}}
neSpecc marked this conversation as resolved.
Show resolved Hide resolved
*/
private static LEAF_DIRECTIONS = {
RIGHT: 'right',
LEFT: 'left',
};

/**
* @type {FlipperIterator|null}
*/
private flipperIterator: FlipperIterator = null;

/**
* @type {boolean}
* @private
*/
private _activated: boolean = false;

/**
* Custom callbacks from Flippers clients
* On each event flipper can call user-provided callback
*/
private callbacks: {[key: string]: () => void};

/**
* @constructor
*
* @param {HTMLElement[]} nodeList
* @param {string} focusedCssClass
* @param {object} callbacks
*/
constructor(
nodeList: HTMLElement[],
focusedCssClass: string,
callbacks: {[key: string]: () => void} = {},
) {
this.callbacks = callbacks;
this.flipperIterator = new FlipperIterator(nodeList, focusedCssClass);

/**
* Listening all keydowns on document and react on TAB/Enter press
* TAB will leaf iterator items
* ENTER will click the focused item
*/
document.addEventListener('keydown', (event) => {
switch (event.keyCode) {
case _.keyCodes.TAB:
this.handleTabPress(event);
break;
case _.keyCodes.ENTER:
this.handleEnterPress(event);
break;
}
}, false);
}

/**
* @param value
*/
public set activated(value) {
this._activated = value;
}

/**
* @return {HTMLElement}
*/
public get currentItem(): HTMLElement {
return this.flipperIterator.currentItem;
}

/**
* When flipper is activated tab press will leaf the items
* @param event
Copy link
Member

Choose a reason for hiding this comment

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

не указан тип

*/
public handleTabPress(event) {
Copy link
Member

Choose a reason for hiding this comment

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

тип аргумента, возвращаемое значение

if (!this._activated) {
return;
}

event.preventDefault();

/** this property defines leaf direction */
const shiftKey = event.shiftKey,
direction = shiftKey ? Flipper.LEAF_DIRECTIONS.LEFT : Flipper.LEAF_DIRECTIONS.RIGHT;

switch (direction) {
case Flipper.LEAF_DIRECTIONS.RIGHT:
this.flipperIterator.next();
break;
case Flipper.LEAF_DIRECTIONS.LEFT:
this.flipperIterator.previous();
break;
}
}

/**
* Enter press will click current item if flipper is activated
* @param event
*/
public handleEnterPress(event) {
if (!this._activated) {
return;
}

if (this.flipperIterator.currentItem) {
this.flipperIterator.currentItem.click();
}

event.preventDefault();
event.stopPropagation();

if (this.callbacks && _.typeof(this.callbacks.onEnterPress) === 'function') {
this.callbacks.onEnterPress();
}
}

/**
* drops flipper iterators cursor
Copy link
Member

Choose a reason for hiding this comment

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

комментарий дублирует название метода, но не описывает что значит drop cursor

*/
public dropCursor(): void {
this.flipperIterator.dropCursor();
}
}

class FlipperIterator {
neSpecc marked this conversation as resolved.
Show resolved Hide resolved
/**
* CSS class
*/
private focusedCssClass: string;

/**
* Focused button index.
* Default is -1 which means nothing is active
Expand All @@ -17,12 +149,8 @@ export default class Flipper {
private items: HTMLElement[] = [];

/**
* CSS class
*/
private focusedCssClass: string;

/**
* @constructor
* @param {HTMLElement[]} nodeList
* @param {string} focusedCssClass
*/
constructor(
nodeList: HTMLElement[],
Expand Down
94 changes: 20 additions & 74 deletions src/components/modules/blockEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export default class BlockEvents extends Module {
return;
}

const { InlineToolbar, ConversionToolbar, UI, BlockManager } = this.Editor;
const { InlineToolbar, ConversionToolbar, UI, BlockManager, BlockSettings } = this.Editor;
const block = BlockManager.getBlock(event.target);

/**
Expand All @@ -110,6 +110,7 @@ export default class BlockEvents extends Module {
*/
if (SelectionUtils.almostAllSelected(block.pluginsContent.textContent)) {
InlineToolbar.close();
BlockSettings.close();
ConversionToolbar.tryToShow(block);
} else {
ConversionToolbar.close();
Expand Down Expand Up @@ -178,36 +179,24 @@ export default class BlockEvents extends Module {
*/
this.Editor.BlockSelection.clearSelection(event);

const { BlockManager, Tools, ConversionToolbar, InlineToolbar } = this.Editor;
const { BlockManager, Tools, InlineToolbar, ConversionToolbar } = this.Editor;
const currentBlock = BlockManager.currentBlock;

if (!currentBlock) {
return;
}

/** Prevent Default behaviour */
event.preventDefault();
event.stopPropagation();

/** this property defines leaf direction */
const shiftKey = event.shiftKey,
direction = shiftKey ? 'left' : 'right';

const canLeafToolbox = Tools.isInitial(currentBlock.tool) && currentBlock.isEmpty;
const canLeafInlineToolbar = !currentBlock.isEmpty && !SelectionUtils.isCollapsed && InlineToolbar.opened;
const canLeafConversionToolbar = !currentBlock.isEmpty && ConversionToolbar.opened;
const canOpenToolbox = Tools.isInitial(currentBlock.tool) && currentBlock.isEmpty;
const conversionToolbarOpened = !currentBlock.isEmpty && ConversionToolbar.opened;
const inlineToolbarOpened = !currentBlock.isEmpty && !SelectionUtils.isCollapsed && InlineToolbar.opened;

/**
* For empty Blocks we show Plus button via Toobox only for initial Blocks
* For empty Blocks we show Plus button via Toolbox only for initial Blocks
*/
if (canLeafToolbox) {
this.leafToolboxTools(direction);
} else if (canLeafInlineToolbar) {
this.leafInlineToolbarTools(direction);
} else if (canLeafConversionToolbar) {
this.leafConversionToolbarTools(direction);
} else {
this.leafBlockSettingsTools(direction);
if (canOpenToolbox) {
this.activateToolbox();
} else if (!conversionToolbarOpened && !inlineToolbarOpened) {
this.activateBlockSettings();
}
}

Expand All @@ -229,6 +218,8 @@ export default class BlockEvents extends Module {
this.Editor.BlockSettings.close();
} else if (this.Editor.InlineToolbar.opened) {
this.Editor.InlineToolbar.close();
} else if (this.Editor.ConversionToolbar.opened) {
this.Editor.ConversionToolbar.close();
} else {
this.Editor.Toolbar.close();
}
Expand Down Expand Up @@ -320,29 +311,14 @@ export default class BlockEvents extends Module {
* Don't handle Enter keydowns when Tool sets enableLineBreaks to true.
* Uses for Tools like <code> where line breaks should be handled by default behaviour.
*/
if (tool
&& tool[Tools.apiSettings.IS_ENABLED_LINE_BREAKS]
&& !BlockSettings.opened
&& !InlineToolbar.opened
&& !ConversionToolbar.opened) {
return;
}

if (Toolbox.opened && Toolbox.getActiveTool) {
event.preventDefault();
event.stopPropagation();
event.stopImmediatePropagation();

Toolbox.toolButtonActivate(event, Toolbox.getActiveTool);
if (tool && tool[Tools.apiSettings.IS_ENABLED_LINE_BREAKS]) {
return;
}

if (InlineToolbar.opened && InlineToolbar.focusedButton) {
event.preventDefault();
event.stopPropagation();
event.stopImmediatePropagation();

InlineToolbar.focusedButton.click();
/**
* This modules uses flipper
*/
if (BlockSettings.opened || InlineToolbar.opened || ConversionToolbar.opened || Toolbox.opened) {
return;
}

Expand Down Expand Up @@ -386,8 +362,6 @@ export default class BlockEvents extends Module {
}

event.preventDefault();
event.stopPropagation();
event.stopImmediatePropagation();
}

/**
Expand Down Expand Up @@ -596,46 +570,20 @@ export default class BlockEvents extends Module {

/**
* If Toolbox is not open, then just open it and show plus button
* Next Tab press will leaf Toolbox Tools
*
* @param {string} direction
*/
private leafToolboxTools(direction: string): void {
private activateToolbox(): void {
if (!this.Editor.Toolbar.opened) {
this.Editor.Toolbar.open(false , false);
this.Editor.Toolbar.plusButton.show();
} else {
this.Editor.Toolbox.leaf(direction);
}

this.Editor.Toolbox.open();
}

/**
* If InlineToolbar is not open, just open it and focus first button
* Next Tab press will leaf InlineToolbar Tools
*
* @param {string} direction
*/
private leafInlineToolbarTools(direction: string): void {
if (this.Editor.InlineToolbar.opened) {
this.Editor.InlineToolbar.leaf(direction);
}
}

/**
* Leaf Conversion Toolbar Tools
* @param {string} direction
*/
private leafConversionToolbarTools(direction: string): void {
this.Editor.ConversionToolbar.leaf(direction);
}

/**
* Open Toolbar and show BlockSettings before flipping Tools
* @param {string} direction
*/
private leafBlockSettingsTools(direction: string): void {
private activateBlockSettings(): void {
if (!this.Editor.Toolbar.opened) {
this.Editor.BlockManager.currentBlock.focused = true;
this.Editor.Toolbar.open(true, false);
Expand All @@ -649,7 +597,5 @@ export default class BlockEvents extends Module {
if (!this.Editor.BlockSettings.opened) {
this.Editor.BlockSettings.open();
}

this.Editor.BlockSettings.leaf(direction);
}
}
Loading