-
-
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
Fix behaviour of inputs editing in block settings #1123
Changes from 1 commit
60eb7bf
b8f64bc
9b55695
dc27e15
07a6fad
bbad9d8
3234c18
e4ce19c
4f168e9
a1ebf26
86c13f8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,7 +31,7 @@ export interface FlipperOptions { | |
/** | ||
* Optional callback for button click | ||
*/ | ||
activateCallback?: () => void; | ||
activateCallback?: (item: HTMLElement) => void; | ||
} | ||
|
||
/** | ||
|
@@ -62,7 +62,7 @@ export default class Flipper { | |
/** | ||
* Call back for button click/enter | ||
*/ | ||
private readonly activateCallback: () => void; | ||
private readonly activateCallback: (item: HTMLElement) => void; | ||
|
||
/** | ||
* @class | ||
|
@@ -73,44 +73,6 @@ export default class Flipper { | |
this.allowArrows = typeof options.allowArrows === 'boolean' ? options.allowArrows : true; | ||
this.iterator = new DomIterator(options.items, options.focusedItemClass); | ||
this.activateCallback = options.activateCallback; | ||
|
||
/** | ||
* 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) => { | ||
const isReady = this.isEventReadyForHandling(event); | ||
|
||
if (!isReady) { | ||
return; | ||
} | ||
|
||
/** | ||
* Prevent only used keys default behaviour | ||
* (allows to navigate by ARROW DOWN, for example) | ||
*/ | ||
if (Flipper.usedKeys.includes(event.keyCode)) { | ||
event.preventDefault(); | ||
} | ||
|
||
switch (event.keyCode) { | ||
case _.keyCodes.TAB: | ||
this.handleTabPress(event); | ||
break; | ||
case _.keyCodes.LEFT: | ||
case _.keyCodes.UP: | ||
this.flipLeft(); | ||
break; | ||
case _.keyCodes.RIGHT: | ||
case _.keyCodes.DOWN: | ||
this.flipRight(); | ||
break; | ||
case _.keyCodes.ENTER: | ||
this.handleEnterPress(event); | ||
break; | ||
} | ||
}, false); | ||
} | ||
|
||
/** | ||
|
@@ -141,6 +103,13 @@ export default class Flipper { | |
if (items) { | ||
this.iterator.setItems(items); | ||
} | ||
|
||
/** | ||
* 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', this.onKeyDown); | ||
} | ||
|
||
/** | ||
|
@@ -149,6 +118,8 @@ export default class Flipper { | |
public deactivate(): void { | ||
this.activated = false; | ||
this.dropCursor(); | ||
|
||
document.removeEventListener('keydown', this.onKeyDown); | ||
} | ||
|
||
/** | ||
|
@@ -168,6 +139,20 @@ export default class Flipper { | |
this.flipRight(); | ||
} | ||
|
||
/** | ||
* Focuses previous flipper iterator item | ||
*/ | ||
public flipLeft(): void { | ||
this.iterator.previous(); | ||
} | ||
|
||
/** | ||
* Focuses next flipper iterator item | ||
*/ | ||
public flipRight(): void { | ||
this.iterator.next(); | ||
} | ||
|
||
/** | ||
* Drops flipper's iterator cursor | ||
* | ||
|
@@ -177,6 +162,44 @@ export default class Flipper { | |
this.iterator.dropCursor(); | ||
} | ||
|
||
/** | ||
* KeyDown event handler | ||
* | ||
* @param event - keydown event | ||
*/ | ||
private onKeyDown = (event): void => { | ||
const isReady = this.isEventReadyForHandling(event); | ||
|
||
if (!isReady) { | ||
return; | ||
} | ||
|
||
/** | ||
* Prevent only used keys default behaviour | ||
* (allows to navigate by ARROW DOWN, for example) | ||
*/ | ||
if (Flipper.usedKeys.includes(event.keyCode)) { | ||
event.preventDefault(); | ||
} | ||
|
||
switch (event.keyCode) { | ||
case _.keyCodes.TAB: | ||
this.handleTabPress(event); | ||
break; | ||
case _.keyCodes.LEFT: | ||
case _.keyCodes.UP: | ||
this.flipLeft(); | ||
break; | ||
case _.keyCodes.RIGHT: | ||
case _.keyCodes.DOWN: | ||
this.flipRight(); | ||
break; | ||
case _.keyCodes.ENTER: | ||
this.handleEnterPress(event); | ||
break; | ||
} | ||
}; | ||
|
||
/** | ||
* This function is fired before handling flipper keycodes | ||
* The result of this function defines if it is need to be handled or not | ||
|
@@ -190,7 +213,7 @@ export default class Flipper { | |
_.keyCodes.ENTER, | ||
]; | ||
|
||
if (this.allowArrows) { | ||
if (this.allowArrows && this.iterator.currentItem !== document.activeElement) { | ||
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. need to describe this case or move the second condition to the variable |
||
handlingKeyCodeList.push( | ||
_.keyCodes.LEFT, | ||
_.keyCodes.RIGHT, | ||
|
@@ -199,11 +222,7 @@ export default class Flipper { | |
); | ||
} | ||
|
||
if (!this.activated || handlingKeyCodeList.indexOf(event.keyCode) === -1) { | ||
return false; | ||
} | ||
|
||
return true; | ||
return (this.activated && handlingKeyCodeList.indexOf(event.keyCode) !== -1); | ||
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. extra parentheses |
||
} | ||
|
||
/** | ||
|
@@ -226,20 +245,6 @@ export default class Flipper { | |
} | ||
} | ||
|
||
/** | ||
* Focuses previous flipper iterator item | ||
*/ | ||
private flipLeft(): void { | ||
this.iterator.previous(); | ||
} | ||
|
||
/** | ||
* Focuses next flipper iterator item | ||
*/ | ||
private flipRight(): void { | ||
this.iterator.next(); | ||
} | ||
|
||
/** | ||
* Enter press will click current item if flipper is activated | ||
* | ||
|
@@ -255,7 +260,7 @@ export default class Flipper { | |
} | ||
|
||
if (typeof this.activateCallback === 'function') { | ||
this.activateCallback(); | ||
this.activateCallback(this.iterator.currentItem); | ||
} | ||
|
||
event.preventDefault(); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -188,17 +188,20 @@ export default class BlockSelection extends Module { | |
this.nativeInputSelected = false; | ||
this.readyToBlockSelection = false; | ||
|
||
const isKeyboard = reason && (reason instanceof KeyboardEvent); | ||
const isPrintableKey = isKeyboard && _.isPrintableKey((reason as KeyboardEvent).keyCode); | ||
|
||
/** | ||
* If reason caused clear of the selection was printable key and any block is selected, | ||
* remove selected blocks and insert pressed key | ||
*/ | ||
if (this.anyBlockSelected && reason && reason instanceof KeyboardEvent && _.isPrintableKey(reason.keyCode)) { | ||
if (this.anyBlockSelected && isKeyboard && isPrintableKey && !SelectionUtils.exists) { | ||
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.
|
||
const indexToInsert = BlockManager.removeSelectedBlocks(); | ||
|
||
BlockManager.insertInitialBlockAtIndex(indexToInsert, true); | ||
Caret.setToBlock(BlockManager.currentBlock); | ||
_.delay(() => { | ||
Caret.insertContentAtCaretPosition(reason.key); | ||
Caret.insertContentAtCaretPosition((reason as KeyboardEvent).key); | ||
}, 20)(); | ||
} | ||
|
||
|
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 can be contenteditable element, so you need to use custom
focus()
method