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

Ensure chat loses focus on board click #5323

Merged
merged 1 commit into from
Jul 30, 2019
Merged
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
27 changes: 27 additions & 0 deletions ui/chat/src/discussion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ function renderInput(ctrl: Ctrl): VNode | undefined {
});
}

let mouchListener: EventListener;

const setupHooks = (ctrl: Ctrl, chatEl: HTMLElement) => {
chatEl.addEventListener('keypress',
(e: KeyboardEvent) => setTimeout(() => {
Expand Down Expand Up @@ -106,6 +108,31 @@ const setupHooks = (ctrl: Ctrl, chatEl: HTMLElement) => {
});

window.Mousetrap(chatEl).bind('esc', () => chatEl.blur());


// Ensure clicks remove chat focus.
// See ornicar/chessground#109

const mouchEvents = ['touchstart', 'mousedown'];

if (mouchListener) mouchEvents.forEach(event =>
document.body.removeEventListener(event, mouchListener)
);

mouchListener = (e: MouseEvent) => {
if (!e.shiftKey && e.buttons !== 2 && e.button !== 2) chatEl.blur();
};

chatEl.onfocus = () =>
mouchEvents.forEach(event =>
document.body.addEventListener(event, mouchListener,
{passive: true, capture: true}
));

chatEl.onblur = () =>
mouchEvents.forEach(event =>
document.body.removeEventListener(event, mouchListener, {capture: true})
);
};

function sameLines(l1: Line, l2: Line) {
Expand Down