From c48d0711e87d3cafabd1c26345204ea4e354db14 Mon Sep 17 00:00:00 2001 From: SkyratBot <59378654+SkyratBot@users.noreply.github.com> Date: Tue, 28 Jun 2022 18:24:14 +0200 Subject: [PATCH] [MIRROR] TGUI Say no longer deletes your currently typed message on history [MDB IGNORE] (#14596) * TGUI Say no longer deletes your currently typed message on history (#67971) first commit * TGUI Say no longer deletes your currently typed message on history Co-authored-by: Jeremiah <42397676+jlsnow301@users.noreply.github.com> --- code/modules/tgui_input/say_modal/speech.dm | 10 +++--- tgui/packages/tgui-say/constants/index.tsx | 20 +++++------ tgui/packages/tgui-say/handlers/arrowKeys.tsx | 9 ++++- .../tgui-say/handlers/backspaceDelete.tsx | 4 +-- tgui/packages/tgui-say/handlers/keyDown.tsx | 34 ++++++++++++------- .../tgui-say/handlers/radioPrefix.tsx | 6 ++-- tgui/packages/tgui-say/handlers/reset.tsx | 1 + .../tgui-say/handlers/viewHistory.tsx | 6 ++-- tgui/packages/tgui-say/interfaces/TguiSay.tsx | 3 +- tgui/packages/tgui-say/types/index.tsx | 3 +- tgui/packages/tgui/components/TextArea.js | 7 ++-- 11 files changed, 62 insertions(+), 41 deletions(-) diff --git a/code/modules/tgui_input/say_modal/speech.dm b/code/modules/tgui_input/say_modal/speech.dm index f2ee51d381a..bf357133a7d 100644 --- a/code/modules/tgui_input/say_modal/speech.dm +++ b/code/modules/tgui_input/say_modal/speech.dm @@ -31,18 +31,18 @@ * boolean - on success or failure */ /datum/tgui_say/proc/delegate_speech(entry, channel) - if(channel == OOC_CHANNEL) - client.ooc(entry) - return TRUE switch(channel) + if(SAY_CHANNEL) + client.mob.say_verb(entry) + return TRUE if(RADIO_CHANNEL) client.mob.say_verb(";" + entry) return TRUE if(ME_CHANNEL) client.mob.me_verb(entry) return TRUE - if(SAY_CHANNEL) - client.mob.say_verb(entry) + if(OOC_CHANNEL) + client.ooc(entry) return TRUE return FALSE diff --git a/tgui/packages/tgui-say/constants/index.tsx b/tgui/packages/tgui-say/constants/index.tsx index 145d9985c36..09158ad5022 100644 --- a/tgui/packages/tgui-say/constants/index.tsx +++ b/tgui/packages/tgui-say/constants/index.tsx @@ -2,18 +2,18 @@ export const CHANNELS = ['Say', 'Radio', 'Me', 'OOC'] as const; /** Window sizes in pixels */ -export const WINDOW_SIZES = { - small: 30, - medium: 50, - large: 70, - width: 231, -} as const; +export enum WINDOW_SIZES { + small = 30, + medium = 50, + large = 70, + width = 231, +} /** Line lengths for autoexpand */ -export const LINE_LENGTHS = { - small: 20, - medium: 35, -} as const; +export enum LINE_LENGTHS { + small = 20, + medium = 35, +} /** * Radio prefixes. diff --git a/tgui/packages/tgui-say/handlers/arrowKeys.tsx b/tgui/packages/tgui-say/handlers/arrowKeys.tsx index a736c9d860e..ee399ddaf32 100644 --- a/tgui/packages/tgui-say/handlers/arrowKeys.tsx +++ b/tgui/packages/tgui-say/handlers/arrowKeys.tsx @@ -3,9 +3,16 @@ import { getHistoryLength } from '../helpers'; import { Modal } from '../types'; /** Increments the chat history counter, looping through entries */ -export const handleArrowKeys = function (this: Modal, direction: number) { +export const handleArrowKeys = function ( + this: Modal, + direction: number, + value: string +) { const { historyCounter } = this.fields; if (direction === KEY_UP && historyCounter < getHistoryLength()) { + if (!historyCounter) { + this.fields.tempHistory = value; + } this.fields.historyCounter++; this.events.onViewHistory(); } else if (direction === KEY_DOWN && historyCounter > 0) { diff --git a/tgui/packages/tgui-say/handlers/backspaceDelete.tsx b/tgui/packages/tgui-say/handlers/backspaceDelete.tsx index 340b4132b09..b008d110a01 100644 --- a/tgui/packages/tgui-say/handlers/backspaceDelete.tsx +++ b/tgui/packages/tgui-say/handlers/backspaceDelete.tsx @@ -14,9 +14,9 @@ export const handleBackspaceDelete = function (this: Modal) { this.fields.historyCounter = 0; this.setState({ buttonContent: CHANNELS[channel] }); } - if (!value.length && radioPrefix) { + if (!value?.length && radioPrefix) { this.fields.radioPrefix = ''; this.setState({ buttonContent: CHANNELS[channel] }); } - this.events.onSetSize(value.length); + this.events.onSetSize(value?.length); }; diff --git a/tgui/packages/tgui-say/handlers/keyDown.tsx b/tgui/packages/tgui-say/handlers/keyDown.tsx index 1e554f33917..5c0e473b234 100644 --- a/tgui/packages/tgui-say/handlers/keyDown.tsx +++ b/tgui/packages/tgui-say/handlers/keyDown.tsx @@ -9,27 +9,35 @@ import { Modal } from '../types'; * BKSP/DEL - Resets history counter and checks window size. * TYPING - When users key, it tells byond that it's typing. */ -export const handleKeyDown = function (this: Modal, event: KeyboardEvent) { +export const handleKeyDown = function ( + this: Modal, + event: KeyboardEvent, + value: string +) { const { channel } = this.state; const { radioPrefix } = this.fields; if (!event.keyCode) { return; // Really doubt it, but... } + if (event.keyCode === KEY_UP || event.keyCode === KEY_DOWN) { + event.preventDefault(); + if (getHistoryLength()) { + this.events.onArrowKeys(event.keyCode, value); + } + return; + } + if (event.keyCode === KEY_TAB) { + event.preventDefault(); + this.events.onIncrementChannel(); + return; + } + if (event.keyCode === KEY_DELETE || event.keyCode === KEY_BACKSPACE) { + this.events.onBackspaceDelete(); + return; + } if (isAlphanumeric(event.keyCode)) { if (channel !== 3 && radioPrefix !== ':b ') { this.timers.typingThrottle(); } } - if (event.keyCode === KEY_UP || event.keyCode === KEY_DOWN) { - if (getHistoryLength()) { - this.events.onArrowKeys(event.keyCode); - } - } - if (event.keyCode === KEY_DELETE || event.keyCode === KEY_BACKSPACE) { - this.events.onBackspaceDelete(); - } - if (event.keyCode === KEY_TAB) { - this.events.onIncrementChannel(); - event.preventDefault(); - } }; diff --git a/tgui/packages/tgui-say/handlers/radioPrefix.tsx b/tgui/packages/tgui-say/handlers/radioPrefix.tsx index 21feda26c93..bd9f1de5b1a 100644 --- a/tgui/packages/tgui-say/handlers/radioPrefix.tsx +++ b/tgui/packages/tgui-say/handlers/radioPrefix.tsx @@ -11,14 +11,14 @@ import { Modal } from '../types'; export const handleRadioPrefix = function (this: Modal) { const { channel } = this.state; const { radioPrefix, value } = this.fields; - if (channel > 1 || value.length < 3) { + if (channel > 1 || !value || value.length < 3) { return; } - const nextPrefix = value.slice(0, 3)?.toLowerCase(); + const nextPrefix = value?.slice(0, 3)?.toLowerCase(); if (!RADIO_PREFIXES[nextPrefix] || radioPrefix === nextPrefix) { return; } - this.fields.value = value.slice(3); + this.fields.value = value?.slice(3); // Binary is a "secret" channel if (nextPrefix === ':b ') { Byond.sendMessage('thinking', { mode: false }); diff --git a/tgui/packages/tgui-say/handlers/reset.tsx b/tgui/packages/tgui-say/handlers/reset.tsx index ffe5215eb43..aa5a648ac9c 100644 --- a/tgui/packages/tgui-say/handlers/reset.tsx +++ b/tgui/packages/tgui-say/handlers/reset.tsx @@ -11,6 +11,7 @@ import { Modal } from '../types'; export const handleReset = function (this: Modal, channel?: number) { this.fields.historyCounter = 0; this.fields.radioPrefix = ''; + this.fields.tempHistory = ''; this.fields.value = ''; this.setState({ buttonContent: valueExists(channel) ? CHANNELS[channel!] : '', diff --git a/tgui/packages/tgui-say/handlers/viewHistory.tsx b/tgui/packages/tgui-say/handlers/viewHistory.tsx index bfbf5aa3122..59e59259674 100644 --- a/tgui/packages/tgui-say/handlers/viewHistory.tsx +++ b/tgui/packages/tgui-say/handlers/viewHistory.tsx @@ -14,11 +14,13 @@ export const handleViewHistory = function (this: Modal) { this.setState({ buttonContent: historyCounter, edited: true }); this.events.onSetSize(0); } else { - this.fields.value = ''; + /** Restores any saved history */ + this.fields.value = this.fields.tempHistory; + this.fields.tempHistory = ''; this.setState({ buttonContent: CHANNELS[channel], edited: true, }); - this.events.onSetSize(0); } + this.events.onSetSize(this.fields.value?.length); }; diff --git a/tgui/packages/tgui-say/interfaces/TguiSay.tsx b/tgui/packages/tgui-say/interfaces/TguiSay.tsx index 413173b27c5..edfce7c17af 100644 --- a/tgui/packages/tgui-say/interfaces/TguiSay.tsx +++ b/tgui/packages/tgui-say/interfaces/TguiSay.tsx @@ -15,6 +15,7 @@ export class TguiSay extends Component<{}, State> { lightMode: false, maxLength: 1024, radioPrefix: '', + tempHistory: '', value: '', }; state: Modal['state'] = { @@ -62,7 +63,7 @@ export class TguiSay extends Component<{}, State> { onEnter={onEnter} onEscape={onEscape} onInput={onInput} - onKeyDown={onKeyDown} + onKey={onKeyDown} selfClear value={edited && value} /> diff --git a/tgui/packages/tgui-say/types/index.tsx b/tgui/packages/tgui-say/types/index.tsx index c75f7e4d31b..16e5d6a2fbe 100644 --- a/tgui/packages/tgui-say/types/index.tsx +++ b/tgui/packages/tgui-say/types/index.tsx @@ -9,7 +9,7 @@ export type Modal = { }; type Events = { - onArrowKeys: (direction: number) => void; + onArrowKeys: (direction: number, value: string) => void; onBackspaceDelete: () => void; onClick: () => void; onEscape: () => void; @@ -32,6 +32,7 @@ type Fields = { lightMode: boolean; maxLength: number; radioPrefix: string; + tempHistory: string; value: string; }; diff --git a/tgui/packages/tgui/components/TextArea.js b/tgui/packages/tgui/components/TextArea.js index 6c24cb0baed..675a18be061 100644 --- a/tgui/packages/tgui/components/TextArea.js +++ b/tgui/packages/tgui/components/TextArea.js @@ -52,7 +52,7 @@ export class TextArea extends Component { }; this.handleKeyDown = (e) => { const { editing } = this.state; - const { onChange, onInput, onEnter, onKeyDown } = this.props; + const { onChange, onInput, onEnter, onKey } = this.props; if (e.keyCode === KEY_ENTER) { this.setEditing(false); if (onChange) { @@ -86,8 +86,9 @@ export class TextArea extends Component { if (!editing) { this.setEditing(true); } - if (onKeyDown) { - onKeyDown(e, e.target.value); + // Custom key handler + if (onKey) { + onKey(e, e.target.value); } if (!dontUseTabForIndent) { const keyCode = e.keyCode || e.which;