From 3c7435bfde2b12287c35cf2ea2a6b4468edc3a81 Mon Sep 17 00:00:00 2001 From: oda <934854676@qq.com> Date: Thu, 22 Aug 2024 15:12:12 +0800 Subject: [PATCH] add: notification when regex is not valid --- src/data-model/workflow-def.ts | 15 ++++++++---- src/settings/RegexTrimEditView.tsx | 32 +++++++++++++++++++++---- src/ui/react-view/react-manage-page.tsx | 1 - 3 files changed, 37 insertions(+), 11 deletions(-) diff --git a/src/data-model/workflow-def.ts b/src/data-model/workflow-def.ts index f61cbcf..d555e48 100644 --- a/src/data-model/workflow-def.ts +++ b/src/data-model/workflow-def.ts @@ -51,12 +51,17 @@ export function isTaskSummaryValid(task: STask) { export function trimTextBySettings(text: string) { const taskSummaryTrimPattern = getSettings()?.task_summary_trim_regexp_pattern as string; if (taskSummaryTrimPattern) { // undefined or empty string we do not use regex - const regex = new RegExp(taskSummaryTrimPattern, "g"); - const regexReplacedText = text.replace(regex, ""); - if (text != regexReplacedText && text.includes("UT_080")) { - devLog("Regex Trimmed", text, "->", regexReplacedText); + let regexReplacedText = text; + try { + const regex = new RegExp(taskSummaryTrimPattern, "g"); + regexReplacedText = text.replace(regex, ""); + if (text != regexReplacedText && text.includes("UT_080")) { + devLog("Regex Trimmed", text, "->", regexReplacedText); + } + } catch (e) { /* empty */ + } finally { + text = regexReplacedText; } - text = regexReplacedText; } return text; } diff --git a/src/settings/RegexTrimEditView.tsx b/src/settings/RegexTrimEditView.tsx index f6ea16e..72bfc9f 100644 --- a/src/settings/RegexTrimEditView.tsx +++ b/src/settings/RegexTrimEditView.tsx @@ -1,23 +1,39 @@ -import React from "react"; +import React, {useEffect, useState} from "react"; import {HStack} from "../ui/pure-react/view-template/h-stack"; import {centerChildren, diffGroupSpacing} from "../ui/pure-react/style-def"; import {getSettings, usePluginSettings} from "./settings"; import {ArrowMoveRight} from "../ui/pure-react/icon/ArrowBigUpDash"; import {InlineCodeView} from "../ui/common/inline-code-view"; import {devLog} from "../utils/env-util"; +import {DisappearableErrText} from "../ui/pure-react/view-template/disappearable-err-text"; export function RegexTrimEditView() { // TODO Previe use task_summary_trim_regexp_pattern_test_text const [regPattern, setRegPattern] = usePluginSettings("task_summary_trim_regexp_pattern") const [testText, setTestText] = usePluginSettings("task_summary_trim_regexp_pattern_test_text"); - const replacedText = testText.replace(new RegExp(regPattern), ""); - devLog(`$[RegexTrimEditView] oriTag: ${regPattern}, patternValid?`); + const [notiText, setNotiText] = useState("") + let regExp: RegExp | null = null; + try { + regExp = new RegExp(regPattern); + } catch (e) { + // regExp is invalid + } + useEffect(() => { + if (!regExp) + setNotiText("[Err] Invalid Regular expression.") + else setNotiText("") + }, [testText, regPattern]); + const replacedText = regExp ? testText.replace(regExp, "") : testText; + devLog(`$[RegexTrimEditView] oriTag: ${regPattern}, patternValid? ${!!regExp}`); return
Remove regex pattern from task summary
- You need to edit some file and save for this to be effective. Empty means no effect. + Empty means no effect. +
+
+ You need to edit any file and save for this to be effective.
@@ -28,10 +44,15 @@ export function RegexTrimEditView() { setRegPattern(event.target.value) }} /> +
+ +

+

- +

@@ -54,6 +75,7 @@ export function RegexTrimEditView() {
+
} diff --git a/src/ui/react-view/react-manage-page.tsx b/src/ui/react-view/react-manage-page.tsx index 95630dd..332cd03 100644 --- a/src/ui/react-view/react-manage-page.tsx +++ b/src/ui/react-view/react-manage-page.tsx @@ -245,7 +245,6 @@ export function ReactManagePage({eventCenter}: { // }) // endregion - return (