From 44d903b6e2a3078cc82a4e4db268b2c9dafecb1c Mon Sep 17 00:00:00 2001 From: Fedor Katurov Date: Sat, 29 Apr 2023 13:50:41 +0600 Subject: [PATCH] added themes --- public/locales/en/translation.json | 9 +- public/locales/ru/translation.json | 9 +- public/manifest.json | 4 +- .../containers/SettingsRow/index.tsx | 13 +- .../containers/SettingsRow/styles.module.scss | 14 +- .../components/ModalPage/styles.module.scss | 2 +- .../settings/components/ThemeSelect/index.tsx | 46 ++++++ .../containers/ColorSettings/index.tsx | 65 ++++++-- .../settings/context/SettingsContext.ts | 5 + .../settings/hooks/usePersistSettings.ts | 13 +- src/modules/theme/constants/theme.ts | 10 ++ src/modules/theme/constants/themes.ts | 146 ++++++++++++++++++ src/modules/theme/hooks/useDefaultTheme.ts | 11 ++ src/modules/theme/hooks/useThemeColors.ts | 16 +- src/modules/theme/types/theme.ts | 7 + src/styles/_dark.scss | 5 + src/styles/main.scss | 24 ++- 17 files changed, 368 insertions(+), 31 deletions(-) create mode 100644 src/modules/settings/components/ThemeSelect/index.tsx create mode 100644 src/modules/theme/constants/themes.ts create mode 100644 src/modules/theme/hooks/useDefaultTheme.ts create mode 100644 src/modules/theme/types/theme.ts diff --git a/public/locales/en/translation.json b/public/locales/en/translation.json index 4cf03bc..a4dc99d 100644 --- a/public/locales/en/translation.json +++ b/public/locales/en/translation.json @@ -9,5 +9,12 @@ "Colors": "Colors", "Inline code": "Inline code", "Ok": "Ok", - "Settings": "Settings" + "Settings": "Settings", + "Color theme": "Color theme", + "Custom theme": "Custom theme", + "Dark theme": "Dark theme", + "Light theme": "Light theme", + "Blueberry boar": "Blueberry boar", + "User defined theme": "User defined", + "Built-in theme": "Built-in" } diff --git a/public/locales/ru/translation.json b/public/locales/ru/translation.json index d166822..956cffd 100644 --- a/public/locales/ru/translation.json +++ b/public/locales/ru/translation.json @@ -9,5 +9,12 @@ "Colors": "Цвета", "Inline code": "Код в строке", "Ok": "Ok", - "Settings": "Настройки" + "Settings": "Настройки", + "Color theme": "Цветовая схема", + "Custom theme": "Своя тема", + "Dark theme": "Тёмная", + "Light theme": "Светлая", + "Blueberry boar": "Черничный кабанчик", + "User defined theme": "Пользовательская", + "Built-in theme": "Встроенная" } diff --git a/public/manifest.json b/public/manifest.json index 1e3f75d..010b8a7 100644 --- a/public/manifest.json +++ b/public/manifest.json @@ -1,8 +1,8 @@ { "name": "Markdown Home Tab", "short_name": "Markdown New Tab", - "version": "0.0.1", - "description": "Makdown editor for your new tab", + "version": "0.0.2", + "description": "Markdown right in your home tab! Paste links, pictures, lists and more. You can also customize colors to match your needs.", "manifest_version": 2, "permissions": ["storage"], "chrome_url_overrides": { diff --git a/src/components/containers/SettingsRow/index.tsx b/src/components/containers/SettingsRow/index.tsx index bfb3bb9..7371e62 100644 --- a/src/components/containers/SettingsRow/index.tsx +++ b/src/components/containers/SettingsRow/index.tsx @@ -3,11 +3,20 @@ import styles from "./styles.module.scss"; type SettingsRowProps = PropsWithChildren & { title: string; + subTitle?: string; }; -const SettingsRow: FC = ({ title, children }) => ( +const SettingsRow: FC = ({ title, children, subTitle }) => (
-
{title}
+
+
{title}
+ {!!subTitle && ( +
+ )} +
{children}
); diff --git a/src/components/containers/SettingsRow/styles.module.scss b/src/components/containers/SettingsRow/styles.module.scss index ccdee35..afe79fc 100644 --- a/src/components/containers/SettingsRow/styles.module.scss +++ b/src/components/containers/SettingsRow/styles.module.scss @@ -6,6 +6,18 @@ gap: 20px; } -.title { +.legend { flex: 1; + min-width: 0; +} + +.title { + font-weight: bold; +} + +.subTitle { + font-size: 0.8em; + text-overflow: ellipsis; + white-space: nowrap; + overflow: hidden; } diff --git a/src/modules/modal/components/ModalPage/styles.module.scss b/src/modules/modal/components/ModalPage/styles.module.scss index 4f797e5..cc77cb7 100644 --- a/src/modules/modal/components/ModalPage/styles.module.scss +++ b/src/modules/modal/components/ModalPage/styles.module.scss @@ -13,5 +13,5 @@ .title { flex: 1; - font-size: 1.4em; + font-weight: bold; } diff --git a/src/modules/settings/components/ThemeSelect/index.tsx b/src/modules/settings/components/ThemeSelect/index.tsx new file mode 100644 index 0000000..b9229e3 --- /dev/null +++ b/src/modules/settings/components/ThemeSelect/index.tsx @@ -0,0 +1,46 @@ +import { ChangeEvent, FC, useCallback } from "react"; +import { useTranslation } from "react-i18next"; +import { CustomTheme } from "~/modules/theme/types/theme"; +import { ColorSettings } from "../../context/SettingsContext"; + +interface ThemeSelectProps { + themes: CustomTheme[]; + value?: CustomTheme; + onChange: (val: ColorSettings) => void; +} + +const ThemeSelect: FC = ({ themes, value, onChange }) => { + const { t } = useTranslation(); + + const onChangeTrigger = useCallback( + (event: ChangeEvent) => { + const themeTitle = event.target.value; + + if (!themeTitle) { + return; + } + + const theme = themes.find((it) => it.title === themeTitle); + + if (!theme) { + return; + } + + onChange(theme.colors); + }, + [themes, onChange] + ); + + return ( + + ); +}; +export { ThemeSelect }; diff --git a/src/modules/settings/containers/ColorSettings/index.tsx b/src/modules/settings/containers/ColorSettings/index.tsx index 281af32..a113394 100644 --- a/src/modules/settings/containers/ColorSettings/index.tsx +++ b/src/modules/settings/containers/ColorSettings/index.tsx @@ -1,15 +1,20 @@ -import { ChangeEvent, FC, useCallback } from "react"; +import { ChangeEvent, FC, useCallback, useMemo } from "react"; import { useTranslation } from "react-i18next"; import { RowGroup } from "~/components/containers/RowGroup"; import { SettingsRow } from "~/components/containers/SettingsRow"; import { + ColorSettings, SettingsValue, useSettings, } from "~/modules/settings/context/SettingsContext"; +import { ThemeSelect } from "../../components/ThemeSelect"; +import { useBuiltinThemes } from "~/modules/theme/constants/themes"; +import { useDefaultTheme } from "~/modules/theme/hooks/useDefaultTheme"; const ColorSettings: FC = () => { const { update, settings } = useSettings(); const { t } = useTranslation(); + const defaultColors = useDefaultTheme(); const setString = useCallback( (field: keyof SettingsValue) => (event: ChangeEvent) => { @@ -18,46 +23,88 @@ const ColorSettings: FC = () => { [update] ); + const themes = useBuiltinThemes(); + + const currentTheme = useMemo( + () => + themes.find((it) => + Object.entries({ ...defaultColors, ...it.colors }).every( + ([key, value]) => + (settings as unknown as Record)[key] === value + ) + ), + [themes, defaultColors, settings] + ); + + const setThemeColors = useCallback( + (val: ColorSettings) => { + update({ ...defaultColors, ...val }); + }, + [defaultColors, update] + ); + + const themeSubtitle = useMemo(() => { + if (!currentTheme) { + return t("User defined theme"); + } + + if (!currentTheme.url) { + return t("Built-in theme"); + } + + return `${currentTheme.url}`; + }, [currentTheme, t]); + return ( -