Skip to content

Commit

Permalink
Reorganize chat hooks
Browse files Browse the repository at this point in the history
Consoladate the chat eventListeners into a mithril hook
to avoid dom query.
  • Loading branch information
isaacl committed Jul 12, 2019
1 parent 733a228 commit b00a28d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
22 changes: 19 additions & 3 deletions ui/chat/src/discussion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import * as spam from './spam'
import enhance from './enhance';
import { presetView } from './preset';
import { lineAction } from './moderation';
import { userLink, bind } from './util';
import { userLink } from './util';

const whisperRegex = /^\/w(?:hisper)?\s/;

Expand Down Expand Up @@ -69,7 +69,17 @@ function renderInput(ctrl: Ctrl): VNode | undefined {
maxlength: 140,
disabled: ctrl.vm.timeout || !ctrl.vm.writeable
},
hook: bind('keypress', (e: KeyboardEvent) => setTimeout(() => {
hook: {
insert(vnode) {
setupHooks(ctrl, vnode.elm as HTMLElement);
}
}
});
}

const setupHooks = (ctrl: Ctrl, chatEl: HTMLElement) => {
chatEl.addEventListener('keypress',
(e: KeyboardEvent) => setTimeout(() => {
const el = e.target as HTMLInputElement,
txt = el.value,
pub = ctrl.opts.public;
Expand All @@ -88,8 +98,14 @@ function renderInput(ctrl: Ctrl): VNode | undefined {
if (!pub) el.classList.toggle('whisper', !!txt.match(whisperRegex));
}
}))

window.Mousetrap.bind('c', () => {
chatEl.focus();
return false;
});
}

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

function sameLines(l1: Line, l2: Line) {
return l1.d && l2.d && l1.u === l2.u;
Expand Down
11 changes: 0 additions & 11 deletions ui/chat/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ export default function LichessChat(element: Element, opts: ChatOpts): {
} {
const patch = init([klass, attributes]);

const container = element.parentNode as HTMLElement;

let vnode: VNode, ctrl: Ctrl

function redraw() {
Expand All @@ -30,14 +28,5 @@ export default function LichessChat(element: Element, opts: ChatOpts): {
element.innerHTML = '';
vnode = patch(element, blueprint);

window.Mousetrap.bind('c', () => {
(container.querySelector('.mchat__say') as HTMLElement).focus();
return false;
});

window.Mousetrap(container).bind('esc', () => {
(container.querySelector('.mchat__say') as HTMLElement).blur();
});

return ctrl;
};

0 comments on commit b00a28d

Please sign in to comment.