From b9b1226e60ed6971bec0008e618c66f8e0a6eaf3 Mon Sep 17 00:00:00 2001 From: Mute Date: Fri, 6 Sep 2024 21:34:29 +0000 Subject: [PATCH] history improvements: backspace passthrough, hint rearranged --- ambient.d.ts | 6 ++++-- mbchc.mjs | 26 ++++++++++++++------------ 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/ambient.d.ts b/ambient.d.ts index b0545e2..da320fa 100644 --- a/ambient.d.ts +++ b/ambient.d.ts @@ -350,6 +350,8 @@ interface Utils { complete_do(this: Optional): undefined replace_me(_match: string, _offset: number, whole: string): string scroll(): true + get scrolled(): boolean + rescroll(func: (_: undefined) => unknown): true } interface Complete { @@ -448,7 +450,7 @@ interface InputHistory { /** * Specific key handlers. */ - key: Record true> + key: Record true> /** * Set or unset the readonly mode on the input. @@ -463,7 +465,7 @@ interface InputHistory { /** * Exit the history mode and proc the key event. */ - exit(textarea: HTMLTextAreaElement, key: KeyboardEvent['key']): true + exit(textarea: HTMLTextAreaElement, e: KeyboardEvent): true } // FIXME spread around readonly where appropriate diff --git a/mbchc.mjs b/mbchc.mjs index 0595c6a..4747ec2 100644 --- a/mbchc.mjs +++ b/mbchc.mjs @@ -159,6 +159,8 @@ const/**@type {Utils}*/U = { remove_loader_hook: $, RGB: {Polly: '#81b1e7', Mute })}, replace_me: (_, __, whole) => cur(whole.slice(1), t => `${U.ACT}<${U.cid(W.Player)}:>SourceCharacter${t.startsWith('\'') || t.startsWith(' ') ? $S : ' '}`), scroll: () => yes(void W.ElementScrollToEnd('TextAreaChatLog')), + get scrolled() {return W.ElementIsScrolledToEnd('TextAreaChatLog')}, + rescroll: f => cur(U.scrolled, s => yes(f, s && U.scroll())), } const/**@type {SUBCOMMANDS}*/SUBCOMMANDS_MBCHC = { @@ -178,7 +180,7 @@ const/**@type {Complete}*/C = { S_OPTS: {behavior: 'instant'}, hint: cs => m_t(cs) || yes(_ => { yes(C.e.style.display = 'block') && C.div?.replaceChildren(...[...cs].sort().reverse().map(U.mkdiv)) W.ElementSetDataAttribute(C.e.id, 'colortheme', W.Player.ChatSettings?.ColorTheme ?? 'Light') - cur(W.ElementIsScrolledToEnd('TextAreaChatLog'), r => yes(void W.ChatRoomResize(false)) && r && U.scroll()) + U.rescroll(_ => void W.ChatRoomResize(false)) C.div?.lastElementChild?.scrollIntoView(C.S_OPTS) }), get hidden() {return C.e.parentElement === null || C.e.style.display === 'none'}, @@ -204,12 +206,12 @@ const/**@type {Complete}*/C = { S_OPTS: {behavior: 'instant'}, const/**@type {InputHistory}*/H = { input: undefined, ids: undefined, bottom: undefined, // FIXME ids don't need to be a set, but I'm too tired right now key: { - Escape: ic => yes(val(H.input, i => ic.value = i)), - ArrowLeft: ic => yes(void ic.setSelectionRange(0, 0)), + Escape: (_, ic) => yes(val(H.input, i => ic.value = i)), + ArrowLeft: (_, ic) => yes(void ic.setSelectionRange(0, 0)), }, icro: (ic, ro) => yes(ic.readOnly = ro, val(ic.parentElement?.parentElement?.dataset, d => ro ? d['mbchcMode'] = 'h' : del(d, 'mbchcMode'))), enter: (ic, i, b, is) => yes(H.input = i, H.bottom = b, H.ids = is, H.icro(ic, true), b && U.scroll()), - exit: (ic, k) => yes(H.icro(ic, false), H.key[k]?.(ic), val(H.bottom, b => b && U.scroll()), W.ChatRoomLastMessageIndex = W.ChatRoomLastMessage.length) + exit: (ic, e) => yes(H.icro(ic, false), H.key[e.key]?.(e, ic), val(H.bottom, b => b && U.scroll()), W.ChatRoomLastMessageIndex = W.ChatRoomLastMessage.length) } ass('MBCHC found, aborting loading', W.MBCHC === $) @@ -478,7 +480,7 @@ const/**@type {SDK.Hook}*/after = (name, f) => mod.hookFunction(name, 0, (na, n) // this.COMP_HINT.innerHTML = '
' + options.sort().reverse().map(s => `
${s}
`).join($S) + '
' // this.COMP_HINT.style.display = 'block' // W.ElementSetDataAttribute(this.COMP_HINT.id, 'colortheme', (W.Player.ChatSettings?.ColorTheme || 'Light')) - // const rescroll = W.ElementIsScrolledToEnd('TextAreaChatLog') + // const rescroll = U.scrolled // W.ChatRoomResize(false) // if (rescroll) U.scroll() // this.COMP_HINT.firstChild?.lastChild?.scrollIntoView({behaviour: 'instant'}) @@ -582,7 +584,7 @@ const/**@type {SDK.Hook}*/after = (name, f) => mod.hookFunction(name, 0, (na, n) {Tag: 'activity', Description: '[Message]: Send a custom activity (or "@@Message", or "@Message" as yourself)', Action: this.command_activity}, {Tag: 'do', Description: ': Do an activity, as if clicked on its button ("/do" for help)', Action: this.command_do, AutoComplete: U.complete_do}, ] - mut(D.createElement('style'), c => void D.head.append(c), c => c.textContent = ` + U.rescroll(_ => mut(D.createElement('style'), c => void D.head.append(c), c => c.textContent = ` #TextAreaChatLog .mbchc { background-color: ${U.RGB.Polly}; margin-left: -0.4em; padding-left: 0.4em; } #TextAreaChatLog[data-colortheme^="dark"] .mbchc { background-color: ${U.RGB.Mute}; } #${C.e.id} { display: none; text-align: right; } @@ -591,12 +593,12 @@ const/**@type {SDK.Hook}*/after = (name, f) => mod.hookFunction(name, 0, (na, n) #${C.e.id} > div div { margin: 0.25ex 0; } #chat-room-div #TextAreaChatLog::before { content: ''; display: block; height: 100%; } #chat-room-div[data-mbchc-mode="h"] #TextAreaChatLog::after { - content: 'π—΅π—Άπ˜€π˜π—Όπ—Ώπ˜† βŸ¨π˜—π˜¨π˜œπ˜±/π˜‹π˜―/β†•βŸ© π—Œπ–Όπ—‹π—ˆπ—…π—… β‡… ⟨𝘌𝘯𝘡𝘦𝘳⟩ π—Œπ–Ύπ—‡π–½ ↡ βŸ¨π˜›π˜’π˜£/β†”βŸ© 𝖾𝖽𝗂𝗍 ⌨ ⟨𝘌𝘴𝘀⟩ π–Ίπ–»π—ˆπ—‹π— ⟲\\A' attr(data-mbchc-h-h); whitespace: pre; + content: 'βŸ¨π˜—π˜¨π˜œπ˜±/π˜‹π˜―/β†•βŸ© π—Œπ–Όπ—‹π—ˆπ—…π—… β‡… ⟨𝘌𝘯𝘡𝘦𝘳⟩ π—Œπ–Ύπ—‡π–½ ↡ βŸ¨π˜›π˜’π˜£/↔/⌫⟩ 𝖾𝖽𝗂𝗍 ⌨ ⟨𝘌𝘴𝘀⟩ π–Ίπ–»π—ˆπ—‹π— ⟲\\A' attr(data-mbchc-h-h); whitespace: pre; display: block; position: sticky; z-index: 1; bottom: 0; background: black; color: orange; padding: 0 0.2ex; animation: 0.2s cubic-bezier(0.19, 1, 0.22, 1) mbchc_hh_show; } #InputChat:read-only { background: black; color: orange; } @keyframes mbchc_hh_show { from {transform: translateX(-100%);} to {transform: translateX(0);} } - `) + `)) // Actions this.calculate_maps() @@ -637,12 +639,12 @@ const/**@type {SDK.Hook}*/after = (name, f) => mod.hookFunction(name, 0, (na, n) const/**@type {(this: HTMLTextAreaElement, e: KeyboardEvent) => void}*/ickd = function(e) { C.hide() const ic = this // eslint-disable-line unicorn/no-this-assignment,@typescript-eslint/no-this-alias - if (ic.readOnly) switch (e.key) { + if (ic.readOnly && !e.repeat) switch (e.key) { // FIXME maybe deal with modifiers case 'ArrowUp': case 'ArrowDown': W.ChatRoomScrollHistory(e.key === 'ArrowUp'); break case 'Escape': case 'Tab': case 'ArrowRight': case 'ArrowLeft': e.stopImmediatePropagation() e.preventDefault() // falls through - case 'Enter': H.exit(ic, e.key) // no default + case 'Enter': case 'Backspace': H.exit(ic, e) // no default } } after('ChatRoomCreateElement', () => { // This thing runs on every frame actually. @@ -675,8 +677,8 @@ const/**@type {SDK.Hook}*/after = (name, f) => mod.hookFunction(name, 0, (na, n) const/**@type {Map}*/map = new Map() const/**@type {(l: string, i: number, I: string) => boolean}*/ cond = m_t(input) ? (_, i, __) => i > 0 : (l, _, i) => l !== i && l.startsWith(i) if (m_t(history.reduce((ax, l, i) => cond(l, i, input) ? ax.set(l, i) : ax, map))) return U.bell() - val(asa(HTMLDivElement, D.querySelector('#TextAreaChatLog')), t => t.dataset['mbchcHH'] = m_t(input) ? 'All history' : `Prefix: ${input}`) - H.enter(ic, input, W.ElementIsScrolledToEnd('TextAreaChatLog'), new Set(map.values())) + val(asa(HTMLDivElement, D.querySelector('#TextAreaChatLog')), t => t.dataset['mbchcHH'] = `π—΅π—Άπ˜€π˜π—Όπ—Ώπ˜†: ${m_t(input) ? 'Everything' : `Prefix: ${input}`}`) + H.enter(ic, input, U.scrolled, new Set(map.values())) } if (ic.readOnly) { // this can't be an else, because we mutate state above. To be honest, this will always be true, but I want to make sure. if (H.ids === $) return U.bell() // shouldn't happen?