Skip to content

Commit

Permalink
fix(custom text): some saved book mode texts not working correctly
Browse files Browse the repository at this point in the history
closes #5311
  • Loading branch information
Miodec committed Apr 14, 2024
1 parent 039c950 commit 9c49341
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 16 deletions.
24 changes: 15 additions & 9 deletions frontend/src/ts/modals/custom-text.ts
Expand Up @@ -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, " ");
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -422,7 +428,7 @@ async function setup(modalEl: HTMLElement): Promise<void> {
?.addEventListener("click", () => {
void SaveCustomTextPopup.show({
modalChain: modal as AnimatedModal<unknown, unknown>,
modalChainData: { text: state.textarea },
modalChainData: { text: cleanUpText() },
});
});
modalEl
Expand Down
12 changes: 5 additions & 7 deletions frontend/src/ts/modals/save-custom-text.ts
Expand Up @@ -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<IncomingData>): Promise<void> {
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);
Expand All @@ -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);
Expand Down Expand Up @@ -112,7 +110,7 @@ async function setup(modalEl: HTMLElement): Promise<void> {
}

type IncomingData = {
text: string;
text: string[];
};

const modal = new AnimatedModal<IncomingData>({
Expand Down

0 comments on commit 9c49341

Please sign in to comment.