From 9c49341c16feae6de787fe363c5d784080022842 Mon Sep 17 00:00:00 2001 From: Miodec Date: Sun, 14 Apr 2024 17:18:45 +0200 Subject: [PATCH] fix(custom text): some saved book mode texts not working correctly closes #5311 --- frontend/src/ts/modals/custom-text.ts | 24 ++++++++++++++-------- frontend/src/ts/modals/save-custom-text.ts | 12 +++++------ 2 files changed, 20 insertions(+), 16 deletions(-) diff --git a/frontend/src/ts/modals/custom-text.ts b/frontend/src/ts/modals/custom-text.ts index 6ac3b0135cfb..4179d690090b 100644 --- a/frontend/src/ts/modals/custom-text.ts +++ b/frontend/src/ts/modals/custom-text.ts @@ -179,15 +179,10 @@ function handleFileOpen(): void { } } -function apply(): void { +function cleanUpText(): string[] { let text = state.textarea; - if (text === "") { - Notifications.add("Text cannot be empty", 0); - return; - } - - state.lastSavedTextareaState = state.textarea; + if (text === "") return []; text = text.normalize().trim(); // text = text.replace(/[\r]/gm, " "); @@ -233,7 +228,18 @@ function apply(): void { } const words = text.split(CustomText.delimiter).filter((word) => word !== ""); - CustomText.setText(words); + return words; +} + +function apply(): void { + if (state.textarea === "") { + Notifications.add("Text cannot be empty", 0); + return; + } + + state.lastSavedTextareaState = state.textarea; + + CustomText.setText(cleanUpText()); CustomText.setWord( parseInt(($(`${popup} .wordcount input`).val() as string) || "-1") @@ -422,7 +428,7 @@ async function setup(modalEl: HTMLElement): Promise { ?.addEventListener("click", () => { void SaveCustomTextPopup.show({ modalChain: modal as AnimatedModal, - modalChainData: { text: state.textarea }, + modalChainData: { text: cleanUpText() }, }); }); modalEl diff --git a/frontend/src/ts/modals/save-custom-text.ts b/frontend/src/ts/modals/save-custom-text.ts index e2c530ad2e5b..728327537503 100644 --- a/frontend/src/ts/modals/save-custom-text.ts +++ b/frontend/src/ts/modals/save-custom-text.ts @@ -8,19 +8,19 @@ import AnimatedModal, { ShowOptions } from "../utils/animated-modal"; let indicator: InputIndicator | undefined; type State = { - textToSave: string; + textToSave: string[]; }; const state: State = { - textToSave: "", + textToSave: [], }; export async function show(options: ShowOptions): Promise { - state.textToSave = ""; + state.textToSave = []; void modal.show({ ...options, beforeAnimation: async (modalEl, modalChainData) => { - state.textToSave = modalChainData?.text ?? ""; + state.textToSave = modalChainData?.text ?? []; $("#saveCustomTextModal .textName").val(""); $("#saveCustomTextModal .isLongText").prop("checked", false); $("#saveCustomTextModal button.save").prop("disabled", true); @@ -46,8 +46,6 @@ function save(): boolean { return false; } - state.textToSave = state.textToSave.replace(/( *(\r\n|\r|\n) *)/g, "\n "); - CustomText.setCustomText(name, state.textToSave, checkbox); CustomTextState.setCustomTextName(name, checkbox); Notifications.add("Custom text saved", 1); @@ -112,7 +110,7 @@ async function setup(modalEl: HTMLElement): Promise { } type IncomingData = { - text: string; + text: string[]; }; const modal = new AnimatedModal({