Skip to content

Vertical toolbox fixes #2017

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

Merged
merged 5 commits into from
Apr 14, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
48 changes: 16 additions & 32 deletions src/components/flipper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,22 @@ export interface FlipperOptions {
items?: HTMLElement[];

/**
* Defines arrows usage. By default Flipper leafs items also via RIGHT/LEFT.
*
* true by default
*
* Pass 'false' if you don't need this behaviour
* (for example, Inline Toolbar should be closed by arrows,
* because it means caret moving with selection clearing)
* Optional callback for button click
*/
allowArrows?: boolean;
activateCallback?: (item: HTMLElement) => void;

/**
* Optional callback for button click
* List of keys allowed for handling.
* Can include codes of the following keys:
* - Tab
* - Enter
* - Arrow up
* - Arrow down
* - Arrow right
* - Arrow left
* If not specified all keys are enabled
*/
activateCallback?: (item: HTMLElement) => void;
allowedKeys?: number[];
}

/**
Expand All @@ -53,11 +55,9 @@ export default class Flipper {
private activated = false;

/**
* Flag that allows arrows usage to flip items
*
* @type {boolean}
* List codes of the keys allowed for handling
*/
private readonly allowArrows: boolean = true;
private readonly allowedKeys: number[];

/**
* Call back for button click/enter
Expand All @@ -68,9 +68,9 @@ export default class Flipper {
* @param {FlipperOptions} options - different constructing settings
*/
constructor(options: FlipperOptions) {
this.allowArrows = _.isBoolean(options.allowArrows) ? options.allowArrows : true;
this.iterator = new DomIterator(options.items, options.focusedItemClass);
this.activateCallback = options.activateCallback;
this.allowedKeys = options.allowedKeys || Flipper.usedKeys;
}

/**
Expand Down Expand Up @@ -206,23 +206,7 @@ export default class Flipper {
* @returns {boolean}
*/
private isEventReadyForHandling(event: KeyboardEvent): boolean {
const handlingKeyCodeList = [
_.keyCodes.TAB,
_.keyCodes.ENTER,
];

const isCurrentItemIsFocusedInput = this.iterator.currentItem == document.activeElement;

if (this.allowArrows && !isCurrentItemIsFocusedInput) {
handlingKeyCodeList.push(
_.keyCodes.LEFT,
_.keyCodes.RIGHT,
_.keyCodes.UP,
_.keyCodes.DOWN
);
}

return this.activated && handlingKeyCodeList.indexOf(event.keyCode) !== -1;
return this.activated && this.allowedKeys.indexOf(event.keyCode) !== -1;
}

/**
Expand Down
5 changes: 4 additions & 1 deletion src/components/modules/toolbar/inline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -698,7 +698,10 @@ export default class InlineToolbar extends Module<InlineToolbarNodes> {
private enableFlipper(): void {
this.flipper = new Flipper({
focusedItemClass: this.CSS.focusedButton,
allowArrows: false,
allowedKeys: [
_.keyCodes.ENTER,
_.keyCodes.TAB,
],
});
}
}
8 changes: 7 additions & 1 deletion src/components/utils/popover.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import Listeners from './listeners';
import Flipper from '../flipper';
import SearchInput from './search-input';
import EventsDispatcher from './events';
import { isMobile } from '../utils';
import { isMobile, keyCodes } from '../utils';

/**
* Describe parameters for rendering the single item of Popover
Expand Down Expand Up @@ -347,6 +347,12 @@ export default class Popover extends EventsDispatcher<PopoverEvent> {
this.flipper = new Flipper({
items: tools,
focusedItemClass: Popover.CSS.itemFocused,
allowedKeys: [
keyCodes.TAB,
keyCodes.UP,
keyCodes.DOWN,
keyCodes.ENTER,
],
});
}
}
5 changes: 2 additions & 3 deletions src/styles/popover.css
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
.ce-popover {
position: absolute;
opacity: 0;
visibility: hidden;
will-change: opacity, transform;
display: flex;
flex-direction: column;
padding: 4px;
min-width: 200px;
max-height: 284px;
overflow: hidden;
box-sizing: border-box;
flex-shrink: 0;
max-height: 0;

@apply --overlay-pane;

Expand All @@ -19,7 +18,7 @@

&--opened {
opacity: 1;
visibility: visible;
max-height: 284px;
animation: panelShowing 100ms ease;

@media (--mobile) {
Expand Down