From 4d0410538650e18769a49fd9153b4c80412ffa66 Mon Sep 17 00:00:00 2001 From: lin onetwo Date: Sun, 1 Dec 2024 16:56:13 +0800 Subject: [PATCH] feat: add tag for shared content --- .../localization/locales/en/translation.json | 5 ++- .../locales/zh_CN/translation.json | 6 ++- src/pages/Config/Shared.tsx | 38 +++++++++++++++++++ src/pages/Config/index.tsx | 2 + src/services/NativeService/hooks.tsx | 7 +++- src/store/config.ts | 5 ++- 6 files changed, 58 insertions(+), 5 deletions(-) create mode 100644 src/pages/Config/Shared.tsx diff --git a/src/i18n/localization/locales/en/translation.json b/src/i18n/localization/locales/en/translation.json index 907b78e..bd31aae 100644 --- a/src/i18n/localization/locales/en/translation.json +++ b/src/i18n/localization/locales/en/translation.json @@ -310,6 +310,7 @@ "Title": "Preference", "ClearBrowsingData": "Clear Browsing Data (git isn't affected)", "General": "UI & Interact", + "Shared": "Shared", "Sync": "Sync & Backup", "SyncInterval": "Sync/Backup Interval", "SyncIntervalDescription": "After this length of time, it will automatically start backing up to Github, if is a local workspace it will create a local git backup (take effect after restart app)", @@ -517,7 +518,9 @@ "Share": { "SharedContent": "Shared Content", "TidGiMobileShare": "Shared from TidGi Mobile", - "ImportSuccess": "Import Success" + "ImportSuccess": "Import Success", + "TagForSharedContent": "Tag for shared content", + "Clipped": "Clipped" }, "Description": "Description", "Tags": "Tags", diff --git a/src/i18n/localization/locales/zh_CN/translation.json b/src/i18n/localization/locales/zh_CN/translation.json index a21d17d..8a784ae 100644 --- a/src/i18n/localization/locales/zh_CN/translation.json +++ b/src/i18n/localization/locales/zh_CN/translation.json @@ -330,6 +330,7 @@ "DefaultUserName": "默认编辑者名", "Token": "Git身份凭证", "TokenDescription": "用于向Git服务器验证身份并同步内容的凭证,可通过登录在线存储服务(如Github)来取得,也可以手动获取「Personal Access Token」后填到这里。", + "Shared": "分享", "Sync": "同步和备份", "SyncInterval": "同步/备份间隔", "SyncIntervalDescription": "每经过这段长度的时间后,就会自动开始备份到 Github,如果工作区是本地工作区则会创建本地备份(重启后生效)", @@ -502,7 +503,10 @@ "Share": { "SharedContent": "分享内容", "TidGiMobileShare": "太记移动版分享", - "ImportSuccess": "导入成功" + "ImportSuccess": "导入成功", + "TagForSharedContent": "分享内容标签", + "TagForSharedContentDescription": "分享给太记移动端的内容,会自动加上这个标签。留空时表示不加任何标签。", + "Clipped": "剪藏" }, "Import": { "ImportWiki": "导入Wiki" diff --git a/src/pages/Config/Shared.tsx b/src/pages/Config/Shared.tsx new file mode 100644 index 0000000..2d3267b --- /dev/null +++ b/src/pages/Config/Shared.tsx @@ -0,0 +1,38 @@ +import useDebouncedCallback from 'beautiful-react-hooks/useDebouncedCallback'; +import React, { useState } from 'react'; +import { useTranslation } from 'react-i18next'; +import { styled } from 'styled-components/native'; + +import { TextInput, Text } from 'react-native-paper'; +import { useConfigStore } from '../../store/config'; + +const StyledTextInput = styled(TextInput)` + margin-top: 10px; +`; + +export function Shared(): JSX.Element { + const { t } = useTranslation(); + + const [initialTagForSharedContent] = useConfigStore(state => [state.tagForSharedContent]); + const [tagForSharedContent, tagForSharedContentSetter] = useState(initialTagForSharedContent); + const setConfig = useConfigStore(state => state.set); + + const tagForSharedContentOnChange = useDebouncedCallback((newText: string) => { + setConfig({ tagForSharedContent: newText }); + }, []); + + return ( + <> + { + tagForSharedContentSetter(newText); + tagForSharedContentOnChange(newText); + }} + /> + {t('Share.TagForSharedContentDescription')} + + ); +} diff --git a/src/pages/Config/index.tsx b/src/pages/Config/index.tsx index f84676b..0da5eb1 100644 --- a/src/pages/Config/index.tsx +++ b/src/pages/Config/index.tsx @@ -11,6 +11,7 @@ import { General } from './General'; import { Language } from './Language'; import { Performance } from './Performance'; import { ServerAndSync } from './ServerAndSync'; +import { Shared } from './Shared'; import { TiddlyWiki } from './TiddlyWiki'; const PreferencesList = styled.SectionList` @@ -32,6 +33,7 @@ export const Config: FC> = () { title: t('Preference.Performance'), data: [Performance] }, { title: t('Preference.TiddlyWiki'), data: [TiddlyWiki] }, { title: t('Preference.Sync'), data: [ServerAndSync] }, + { title: t('Preference.Shared'), data: [Shared] }, { title: t('Preference.Languages'), data: [Language] }, { title: t('ContextMenu.About'), data: [About] }, { title: t('Preference.DeveloperTools'), data: [Developer] }, diff --git a/src/services/NativeService/hooks.tsx b/src/services/NativeService/hooks.tsx index 4f74365..c645578 100644 --- a/src/services/NativeService/hooks.tsx +++ b/src/services/NativeService/hooks.tsx @@ -7,6 +7,7 @@ import { useEffect, useState } from 'react'; import { Snackbar } from 'react-native-paper'; import { useRegisterProxy } from 'react-native-postmessage-cat'; import i18n from '../../i18n'; +import { useConfigStore } from '../../store/config'; import { IWikiWorkspace, useWorkspaceStore } from '../../store/workspace'; import { WikiStorageService } from '../WikiStorageService'; import { nativeService } from '.'; @@ -38,6 +39,7 @@ export function useRegisterReceivingShareIntent() { {i18n.t('Share.ImportSuccess')} ); + const [tagForSharedContent] = useConfigStore(state => [state.tagForSharedContent]); /** If you get error on development: * ``` @@ -75,11 +77,14 @@ export function useRegisterReceivingShareIntent() { // put into default workspace's database, with random title const storageOfDefaultWorkspace = new WikiStorageService(defaultWiki); const randomTitle = `${i18n.t('Share.SharedContent')}-${Date.now()}`; + const created = format(new Date(), 'yyyyMMddHHmmssSSS'); await storageOfDefaultWorkspace.saveTiddler(shareIntent.meta?.title ?? randomTitle, { text: shareIntent.text, url: shareIntent.webUrl, - created: format(new Date(), 'yyyyMMddHHmmssSSS'), + created, + modified: created, creator: i18n.t('Share.TidGiMobileShare'), + tags: [tagForSharedContent ?? i18n.t('Share.Clipped')], }); setImportSuccessSnackBarVisible(true); } diff --git a/src/store/config.ts b/src/store/config.ts index cea49c8..0f819bc 100644 --- a/src/store/config.ts +++ b/src/store/config.ts @@ -15,9 +15,10 @@ export interface ConfigState { syncInBackground: boolean; syncInterval: number; syncIntervalBackground: number; + /** Undefined means unset, use default value. If is empty string, then means user deleted the default value, then don't set the tag. */ + tagForSharedContent?: string; theme: ColorSchemeName | 'default'; translucentStatusBar?: boolean; - userName: string; } const defaultConfig: ConfigState = { autoOpenDefaultWiki: undefined, @@ -30,7 +31,7 @@ const defaultConfig: ConfigState = { syncIntervalBackground: 60 * 30 * 1000, theme: 'default', translucentStatusBar: true, - userName: '', + tagForSharedContent: undefined, }; interface ConfigActions { set: (newConfig: Partial) => void;