From 4ed987229b1eef6e14d41046b0149c6a9180199a Mon Sep 17 00:00:00 2001 From: Zhou Yunliang Date: Thu, 3 Nov 2022 21:06:17 +0800 Subject: [PATCH] feat: text filter regex support (#406) --- web/src/components/CreateShortcutDialog.tsx | 1 + web/src/helpers/filter.ts | 13 +++++++++---- web/src/locales/en.json | 3 ++- web/src/locales/zh.json | 3 ++- 4 files changed, 14 insertions(+), 6 deletions(-) diff --git a/web/src/components/CreateShortcutDialog.tsx b/web/src/components/CreateShortcutDialog.tsx index d6a32c5c..c6d3d3f7 100644 --- a/web/src/components/CreateShortcutDialog.tsx +++ b/web/src/components/CreateShortcutDialog.tsx @@ -258,6 +258,7 @@ const MemoFilterInputer: React.FC = (props: MemoFilterIn onChange={(event) => { handleValueChange(event.target.value); }} + placeholder={t("filter.text-placeholder")} /> ) : ( diff --git a/web/src/helpers/filter.ts b/web/src/helpers/filter.ts index c0100269..57dd1880 100644 --- a/web/src/helpers/filter.ts +++ b/web/src/helpers/filter.ts @@ -142,11 +142,16 @@ export const checkShouldShowMemo = (memo: Memo, filter: Filter) => { } shouldShow = matched; } else if (type === "TEXT") { - let contained = memo.content.toLowerCase().includes(value.toLowerCase()); - if (operator === "NOT_CONTAIN") { - contained = !contained; + if (value.startsWith("^")) { + const reg = new RegExp(value.slice(1)); + shouldShow = operator === "NOT_CONTAIN" ? !reg.test(memo.content) : reg.test(memo.content); + } else { + let contained = memo.content.toLowerCase().includes(value.toLowerCase()); + if (operator === "NOT_CONTAIN") { + contained = !contained; + } + shouldShow = contained; } - shouldShow = contained; } return shouldShow; diff --git a/web/src/locales/en.json b/web/src/locales/en.json index 87f98d8d..67e96810 100644 --- a/web/src/locales/en.json +++ b/web/src/locales/en.json @@ -116,7 +116,8 @@ "value": { "not-tagged": "No tags", "linked": "Has links" - } + }, + "text-placeholder": "Starts with ^ to use regex" }, "tag-list": { "tip-text": "Enter `#tag ` to create" diff --git a/web/src/locales/zh.json b/web/src/locales/zh.json index 3073eba7..6d750034 100644 --- a/web/src/locales/zh.json +++ b/web/src/locales/zh.json @@ -116,7 +116,8 @@ "value": { "not-tagged": "无标签", "linked": "包含链接" - } + }, + "text-placeholder": "以 ^ 开头使用正则表达式" }, "tag-list": { "tip-text": "输入`#tag `来创建标签"