From b1cf1849f5bfa8f297f78a5ca58d797f466b3086 Mon Sep 17 00:00:00 2001 From: Vijay A Date: Sat, 21 Aug 2021 08:07:32 +0530 Subject: chore: typescript conversion --- src/config.js | 182 ------------------------------------------- src/config.ts | 183 ++++++++++++++++++++++++++++++++++++++++++++ src/prop-types.js | 14 ---- src/prop-types.ts | 14 ++++ src/sentry.js | 5 -- src/sentry.ts | 5 ++ src/stores/index.js | 42 ---------- src/stores/index.ts | 45 +++++++++++ src/webview/badge.js | 36 --------- src/webview/badge.ts | 40 ++++++++++ src/webview/darkmode.js | 38 --------- src/webview/darkmode.ts | 38 +++++++++ src/webview/spellchecker.js | 39 ---------- src/webview/spellchecker.ts | 35 +++++++++ src/webview/zoom.js | 35 --------- src/webview/zoom.ts | 35 +++++++++ 16 files changed, 395 insertions(+), 391 deletions(-) delete mode 100644 src/config.js create mode 100644 src/config.ts delete mode 100644 src/prop-types.js create mode 100644 src/prop-types.ts delete mode 100644 src/sentry.js create mode 100644 src/sentry.ts delete mode 100644 src/stores/index.js create mode 100644 src/stores/index.ts delete mode 100644 src/webview/badge.js create mode 100644 src/webview/badge.ts delete mode 100644 src/webview/darkmode.js create mode 100644 src/webview/darkmode.ts delete mode 100644 src/webview/spellchecker.js create mode 100644 src/webview/spellchecker.ts delete mode 100644 src/webview/zoom.js create mode 100644 src/webview/zoom.ts (limited to 'src') diff --git a/src/config.js b/src/config.js deleted file mode 100644 index aeaec1089..000000000 --- a/src/config.js +++ /dev/null @@ -1,182 +0,0 @@ -// Note: This file has now become devoid of all references to values deduced from the runtime process - all those now live in the `environment.js` file - -import ms from 'ms'; - -export const CHECK_INTERVAL = ms('1h'); // How often should we perform checks - -export const LOCAL_HOSTNAME = 'localhost'; -export const LOCAL_PORT = 45569; -export const LOCAL_API = 'http://localhost:3000'; -export const DEV_FRANZ_API = 'https://dev.franzinfra.com'; - -export const LIVE_FERDI_API = 'https://api.getferdi.com'; -export const LIVE_FRANZ_API = 'https://api.franzinfra.com'; - -// URL used to submit debugger information, see https://github.com/getferdi/debugger -export const DEBUG_API = 'https://debug.getferdi.com'; - -export const LOCAL_WS_API = 'ws://localhost:3000'; -export const DEV_WS_API = 'wss://dev.franzinfra.com'; -export const LIVE_WS_API = 'wss://api.franzinfra.com'; - -export const LOCAL_API_WEBSITE = 'http://localhost:3333'; -export const DEV_API_FRANZ_WEBSITE = 'https://meetfranz.com'; -export const LIVE_API_FERDI_WEBSITE = 'https://getferdi.com'; - -export const STATS_API = 'https://stats.franzinfra.com'; - -export const LOCAL_TODOS_FRONTEND_URL = 'http://localhost:4000'; -export const PRODUCTION_TODOS_FRONTEND_URL = 'https://app.franztodos.com'; -export const DEVELOPMENT_TODOS_FRONTEND_URL = 'https://development--franz-todos.netlify.com'; - -export const CDN_URL = 'https://cdn.franzinfra.com'; - -export const KEEP_WS_LOADED_USID = '0a0aa000-0a0a-49a0-a000-a0a0a0a0a0a0'; - -export const HIBERNATION_STRATEGIES = { - 10: 'Extremely Fast Hibernation (10sec)', - 30: 'Very Fast Hibernation (30sec)', - 60: 'Fast Hibernation (1min)', - 300: 'Normal Hibernation (5min)', - 600: 'Slow Hibernation (10min)', - 1800: 'Very Slow Hibernation (30min)', - 3600: 'Extremely Slow Hibernation (1hour)', -}; - -export const WAKE_UP_STRATEGIES = { - 0: 'Never wake up', - 10: 'Wake up after 10sec', - 30: 'Wake up after 30sec', - 60: 'Wake up after 1min', - 300: 'Wake up after 5min', - 600: 'Wake up after 10min', - 1800: 'Wake up after 30min', - 3600: 'Wake up after 1hour', -}; - -export const NAVIGATION_BAR_BEHAVIOURS = { - custom: 'Show navigation bar on custom websites only', - always: 'Show navigation bar on all services', - never: 'Never show navigation bar', -}; - -export const SEARCH_ENGINE_GOOGLE = 'google'; -export const SEARCH_ENGINE_DDG = 'duckDuckGo'; -export const SEARCH_ENGINE_NAMES = { - [SEARCH_ENGINE_GOOGLE]: 'Google', - [SEARCH_ENGINE_DDG]: 'DuckDuckGo', -}; - -export const SEARCH_ENGINE_URLS = { - [SEARCH_ENGINE_GOOGLE]: ({ searchTerm }) => `https://www.google.com/search?q=${searchTerm}`, - [SEARCH_ENGINE_DDG]: ({ searchTerm }) => `https://duckduckgo.com/?q=${searchTerm}`, -}; - -export const CUSTOM_TODO_SERVICE = 'isUsingCustomTodoService'; - -const TODO_TODOIST_URL = 'https://todoist.com/app'; -const TODO_FRANZ_TODOS_URL = 'https://app.franztodos.com'; -const TODO_TICKTICK_URL = 'https://ticktick.com/signin'; -const TODO_MSTODO_URL = 'https://todo.microsoft.com/?app#'; -const TODO_HABITICA_URL = 'https://habitica.com/login'; -const TODO_NOZBE_URL = 'https://app.nozbe.com/#login'; -const TODO_RTM_URL = 'https://www.rememberthemilk.com/login/'; -const TODO_ANYDO_URL = 'https://desktop.any.do/'; -const TODO_GOOGLETASKS_URL = 'https://tasks.google.com/embed/?origin=https%3A%2F%2Fcalendar.google.com&fullWidth=1'; - -export const TODO_SERVICE_RECIPE_IDS = { - [TODO_TODOIST_URL]: 'todoist', - [TODO_FRANZ_TODOS_URL]: 'franz-todos', - [TODO_TICKTICK_URL]: 'TickTick', - [TODO_MSTODO_URL]: 'mstodo', - [TODO_HABITICA_URL]: 'habitica', - [TODO_ANYDO_URL]: 'anydo', -}; - -export const TODO_APPS = { - [TODO_TODOIST_URL]: 'Todoist', - [TODO_FRANZ_TODOS_URL]: 'Franz Todo', - [TODO_TICKTICK_URL]: 'TickTick', - [TODO_MSTODO_URL]: 'Microsoft To Do', - [TODO_HABITICA_URL]: 'Habitica', - [TODO_NOZBE_URL]: 'Nozbe', - [TODO_RTM_URL]: 'Remember The Milk', - [TODO_ANYDO_URL]: 'Any.do', - [TODO_GOOGLETASKS_URL]: 'Google Tasks', - [CUSTOM_TODO_SERVICE]: 'Other service', -}; - -export const DEFAULT_TODO_SERVICE = TODO_FRANZ_TODOS_URL; -export const DEFAULT_TODO_RECIPE_ID = TODO_SERVICE_RECIPE_IDS[DEFAULT_TODO_SERVICE]; -export const DEFAULT_TODO_SERVICE_NAME = TODO_APPS[DEFAULT_TODO_SERVICE]; - -export const SIDEBAR_WIDTH = { - 35: 'Extremely slim sidebar', - 45: 'Very slim sidebar', - 55: 'Slim sidebar', - 68: 'Normal sidebar', - 80: 'Wide sidebar', - 90: 'Very wide sidebar', - 100: 'Extremely wide sidebar', -}; - -export const ICON_SIZES = { - 0: 'Very small icons', - 10: 'Small icons', - 20: 'Normal icons', - 30: 'Large icons', - 40: 'Very large icons', -}; -// We need a bias to push all icon sizes into positive numbers -// otherwise the settings screen won't sort the sizes correctly. -// The bias should always be the "Normal icons" value -export const iconSizeBias = 20; - -export const DEFAULT_FEATURES_CONFIG = { - isServiceProxyEnabled: false, - isAnnouncementsEnabled: true, - isWorkspaceEnabled: false, - isTodosEnabled: true, -}; - -export const DEFAULT_WINDOW_OPTIONS = { - width: 800, - height: 600, - x: 0, - y: 0, -}; - -export const GITHUB_FRANZ_URL = 'https://github.com/meetfranz'; -export const GITHUB_FERDI_URL = 'https://github.com/getferdi'; -export const FRANZ_SERVICE_REQUEST = `${GITHUB_FERDI_URL}/recipes/issues`; -export const FRANZ_TRANSLATION = 'https://crowdin.com/project/getferdi'; -export const FRANZ_DEV_DOCS = 'http://bit.ly/franz-dev-hub'; - -export const GITHUB_ORG_NAME = 'getferdi'; -export const GITHUB_FERDI_REPO_NAME = 'ferdi'; -export const GITHUB_NIGHTLIES_REPO_NAME = 'nightlies'; - -export const FILE_SYSTEM_SETTINGS_TYPES = [ - 'app', - 'proxy', -]; - -export const LOCAL_SERVER = 'You are using Ferdi without a server'; -export const SERVER_NOT_LOADED = 'Ferdi::SERVER_NOT_LOADED'; - -export const ALLOWED_PROTOCOLS = [ - 'https:', - 'http:', - 'ftp:', - 'ferdi:', -]; - -export const DEFAULT_SETTING_KEEP_ALL_WORKSPACES_LOADED = false; - -export const DEFAULT_TODOS_WIDTH = 300; -export const TODOS_MIN_WIDTH = 200; -export const DEFAULT_TODOS_VISIBLE = true; -export const DEFAULT_IS_FEATURE_ENABLED_BY_USER = true; -export const TODOS_PARTITION_ID = 'persist:todos'; - -export const CUSTOM_WEBSITE_RECIPE_ID = 'franz-custom-website'; diff --git a/src/config.ts b/src/config.ts new file mode 100644 index 000000000..005dd3601 --- /dev/null +++ b/src/config.ts @@ -0,0 +1,183 @@ +// Note: This file has now become devoid of all references to values deduced from the runtime process - all those now live in the `environment.js` file + +import ms from 'ms'; + +export const CHECK_INTERVAL = ms('1h'); // How often should we perform checks + +export const LOCAL_HOSTNAME = 'localhost'; +export const LOCAL_PORT = 45569; +export const LOCAL_API = 'http://localhost:3000'; +export const DEV_FRANZ_API = 'https://dev.franzinfra.com'; + +export const LIVE_FERDI_API = 'https://api.getferdi.com'; +export const LIVE_FRANZ_API = 'https://api.franzinfra.com'; + +// URL used to submit debugger information, see https://github.com/getferdi/debugger +export const DEBUG_API = 'https://debug.getferdi.com'; + +export const LOCAL_WS_API = 'ws://localhost:3000'; +export const DEV_WS_API = 'wss://dev.franzinfra.com'; +export const LIVE_WS_API = 'wss://api.franzinfra.com'; + +export const LOCAL_API_WEBSITE = 'http://localhost:3333'; +export const DEV_API_FRANZ_WEBSITE = 'https://meetfranz.com'; +export const LIVE_API_FERDI_WEBSITE = 'https://getferdi.com'; + +export const STATS_API = 'https://stats.franzinfra.com'; + +export const LOCAL_TODOS_FRONTEND_URL = 'http://localhost:4000'; +export const PRODUCTION_TODOS_FRONTEND_URL = 'https://app.franztodos.com'; +export const DEVELOPMENT_TODOS_FRONTEND_URL = 'https://development--franz-todos.netlify.com'; + +export const CDN_URL = 'https://cdn.franzinfra.com'; + +export const KEEP_WS_LOADED_USID = '0a0aa000-0a0a-49a0-a000-a0a0a0a0a0a0'; + +// TODO: Need to convert many of these to i18n +export const HIBERNATION_STRATEGIES = { + 10: 'Extremely Fast Hibernation (10sec)', + 30: 'Very Fast Hibernation (30sec)', + 60: 'Fast Hibernation (1min)', + 300: 'Normal Hibernation (5min)', + 600: 'Slow Hibernation (10min)', + 1800: 'Very Slow Hibernation (30min)', + 3600: 'Extremely Slow Hibernation (1hour)', +}; + +export const WAKE_UP_STRATEGIES = { + 0: 'Never wake up', + 10: 'Wake up after 10sec', + 30: 'Wake up after 30sec', + 60: 'Wake up after 1min', + 300: 'Wake up after 5min', + 600: 'Wake up after 10min', + 1800: 'Wake up after 30min', + 3600: 'Wake up after 1hour', +}; + +export const NAVIGATION_BAR_BEHAVIOURS = { + custom: 'Show navigation bar on custom websites only', + always: 'Show navigation bar on all services', + never: 'Never show navigation bar', +}; + +export const SEARCH_ENGINE_GOOGLE = 'google'; +export const SEARCH_ENGINE_DDG = 'duckDuckGo'; +export const SEARCH_ENGINE_NAMES = { + [SEARCH_ENGINE_GOOGLE]: 'Google', + [SEARCH_ENGINE_DDG]: 'DuckDuckGo', +}; + +export const SEARCH_ENGINE_URLS = { + [SEARCH_ENGINE_GOOGLE]: ({ searchTerm }) => `https://www.google.com/search?q=${searchTerm}`, + [SEARCH_ENGINE_DDG]: ({ searchTerm }) => `https://duckduckgo.com/?q=${searchTerm}`, +}; + +export const CUSTOM_TODO_SERVICE = 'isUsingCustomTodoService'; + +const TODO_TODOIST_URL = 'https://todoist.com/app'; +const TODO_FRANZ_TODOS_URL = 'https://app.franztodos.com'; +const TODO_TICKTICK_URL = 'https://ticktick.com/signin'; +const TODO_MSTODO_URL = 'https://todo.microsoft.com/?app#'; +const TODO_HABITICA_URL = 'https://habitica.com/login'; +const TODO_NOZBE_URL = 'https://app.nozbe.com/#login'; +const TODO_RTM_URL = 'https://www.rememberthemilk.com/login/'; +const TODO_ANYDO_URL = 'https://desktop.any.do/'; +const TODO_GOOGLETASKS_URL = 'https://tasks.google.com/embed/?origin=https%3A%2F%2Fcalendar.google.com&fullWidth=1'; + +export const TODO_SERVICE_RECIPE_IDS = { + [TODO_TODOIST_URL]: 'todoist', + [TODO_FRANZ_TODOS_URL]: 'franz-todos', + [TODO_TICKTICK_URL]: 'TickTick', + [TODO_MSTODO_URL]: 'mstodo', + [TODO_HABITICA_URL]: 'habitica', + [TODO_ANYDO_URL]: 'anydo', +}; + +export const TODO_APPS = { + [TODO_TODOIST_URL]: 'Todoist', + [TODO_FRANZ_TODOS_URL]: 'Franz Todo', + [TODO_TICKTICK_URL]: 'TickTick', + [TODO_MSTODO_URL]: 'Microsoft To Do', + [TODO_HABITICA_URL]: 'Habitica', + [TODO_NOZBE_URL]: 'Nozbe', + [TODO_RTM_URL]: 'Remember The Milk', + [TODO_ANYDO_URL]: 'Any.do', + [TODO_GOOGLETASKS_URL]: 'Google Tasks', + [CUSTOM_TODO_SERVICE]: 'Other service', +}; + +export const DEFAULT_TODO_SERVICE = TODO_FRANZ_TODOS_URL; +export const DEFAULT_TODO_RECIPE_ID = TODO_SERVICE_RECIPE_IDS[DEFAULT_TODO_SERVICE]; +export const DEFAULT_TODO_SERVICE_NAME = TODO_APPS[DEFAULT_TODO_SERVICE]; + +export const SIDEBAR_WIDTH = { + 35: 'Extremely slim sidebar', + 45: 'Very slim sidebar', + 55: 'Slim sidebar', + 68: 'Normal sidebar', + 80: 'Wide sidebar', + 90: 'Very wide sidebar', + 100: 'Extremely wide sidebar', +}; + +export const ICON_SIZES = { + 0: 'Very small icons', + 10: 'Small icons', + 20: 'Normal icons', + 30: 'Large icons', + 40: 'Very large icons', +}; +// We need a bias to push all icon sizes into positive numbers +// otherwise the settings screen won't sort the sizes correctly. +// The bias should always be the "Normal icons" value +export const iconSizeBias = 20; + +export const DEFAULT_FEATURES_CONFIG = { + isServiceProxyEnabled: false, + isAnnouncementsEnabled: true, + isWorkspaceEnabled: false, + isTodosEnabled: true, +}; + +export const DEFAULT_WINDOW_OPTIONS = { + width: 800, + height: 600, + x: 0, + y: 0, +}; + +export const GITHUB_FRANZ_URL = 'https://github.com/meetfranz'; +export const GITHUB_FERDI_URL = 'https://github.com/getferdi'; +export const FRANZ_SERVICE_REQUEST = `${GITHUB_FERDI_URL}/recipes/issues`; +export const FRANZ_TRANSLATION = 'https://crowdin.com/project/getferdi'; +export const FRANZ_DEV_DOCS = 'http://bit.ly/franz-dev-hub'; + +export const GITHUB_ORG_NAME = 'getferdi'; +export const GITHUB_FERDI_REPO_NAME = 'ferdi'; +export const GITHUB_NIGHTLIES_REPO_NAME = 'nightlies'; + +export const FILE_SYSTEM_SETTINGS_TYPES = [ + 'app', + 'proxy', +]; + +export const LOCAL_SERVER = 'You are using Ferdi without a server'; +export const SERVER_NOT_LOADED = 'Ferdi::SERVER_NOT_LOADED'; + +export const ALLOWED_PROTOCOLS = [ + 'https:', + 'http:', + 'ftp:', + 'ferdi:', +]; + +export const DEFAULT_SETTING_KEEP_ALL_WORKSPACES_LOADED = false; + +export const DEFAULT_TODOS_WIDTH = 300; +export const TODOS_MIN_WIDTH = 200; +export const DEFAULT_TODOS_VISIBLE = true; +export const DEFAULT_IS_FEATURE_ENABLED_BY_USER = true; +export const TODOS_PARTITION_ID = 'persist:todos'; + +export const CUSTOM_WEBSITE_RECIPE_ID = 'franz-custom-website'; diff --git a/src/prop-types.js b/src/prop-types.js deleted file mode 100644 index 459b9a7b9..000000000 --- a/src/prop-types.js +++ /dev/null @@ -1,14 +0,0 @@ -import PropTypes from 'prop-types'; - -// eslint-disable-next-line -export const oneOrManyChildElements = PropTypes.oneOfType([ - PropTypes.arrayOf(PropTypes.element), - PropTypes.element, - PropTypes.array, -]); - -export const globalError = PropTypes.shape({ - status: PropTypes.number, - message: PropTypes.string, - code: PropTypes.string, -}); diff --git a/src/prop-types.ts b/src/prop-types.ts new file mode 100644 index 000000000..459b9a7b9 --- /dev/null +++ b/src/prop-types.ts @@ -0,0 +1,14 @@ +import PropTypes from 'prop-types'; + +// eslint-disable-next-line +export const oneOrManyChildElements = PropTypes.oneOfType([ + PropTypes.arrayOf(PropTypes.element), + PropTypes.element, + PropTypes.array, +]); + +export const globalError = PropTypes.shape({ + status: PropTypes.number, + message: PropTypes.string, + code: PropTypes.string, +}); diff --git a/src/sentry.js b/src/sentry.js deleted file mode 100644 index 4f6d7205d..000000000 --- a/src/sentry.js +++ /dev/null @@ -1,5 +0,0 @@ -import * as Sentry from '@sentry/electron'; - -Sentry.init({ - dsn: 'https://f3d022fbcf6243a68a58d681c2122ee4@sentry.io/1803053', -}); diff --git a/src/sentry.ts b/src/sentry.ts new file mode 100644 index 000000000..4f6d7205d --- /dev/null +++ b/src/sentry.ts @@ -0,0 +1,5 @@ +import * as Sentry from '@sentry/electron'; + +Sentry.init({ + dsn: 'https://f3d022fbcf6243a68a58d681c2122ee4@sentry.io/1803053', +}); diff --git a/src/stores/index.js b/src/stores/index.js deleted file mode 100644 index b6e481e8a..000000000 --- a/src/stores/index.js +++ /dev/null @@ -1,42 +0,0 @@ -import AppStore from './AppStore'; -import UserStore from './UserStore'; -import FeaturesStore from './FeaturesStore'; -import SettingsStore from './SettingsStore'; -import ServicesStore from './ServicesStore'; -import RecipesStore from './RecipesStore'; -import RecipePreviewsStore from './RecipePreviewsStore'; -import UIStore from './UIStore'; -import NewsStore from './NewsStore'; -import RequestStore from './RequestStore'; -import GlobalErrorStore from './GlobalErrorStore'; -import { workspaceStore } from '../features/workspaces'; -import { announcementsStore } from '../features/announcements'; -import { communityRecipesStore } from '../features/communityRecipes'; -import { todosStore } from '../features/todos'; - -export default (api, actions, router) => { - const stores = {}; - Object.assign(stores, { - router, - app: new AppStore(stores, api, actions), - user: new UserStore(stores, api, actions), - features: new FeaturesStore(stores, api, actions), - settings: new SettingsStore(stores, api, actions), - services: new ServicesStore(stores, api, actions), - recipes: new RecipesStore(stores, api, actions), - recipePreviews: new RecipePreviewsStore(stores, api, actions), - ui: new UIStore(stores, api, actions), - news: new NewsStore(stores, api, actions), - requests: new RequestStore(stores, api, actions), - globalError: new GlobalErrorStore(stores, api, actions), - workspaces: workspaceStore, - announcements: announcementsStore, - communityRecipes: communityRecipesStore, - todos: todosStore, - }); - // Initialize all stores - Object.keys(stores).forEach((name) => { - if (stores[name] && stores[name].initialize) stores[name].initialize(); - }); - return stores; -}; diff --git a/src/stores/index.ts b/src/stores/index.ts new file mode 100644 index 000000000..e980f2c5b --- /dev/null +++ b/src/stores/index.ts @@ -0,0 +1,45 @@ +import AppStore from './AppStore'; +import UserStore from './UserStore'; +import FeaturesStore from './FeaturesStore'; +import SettingsStore from './SettingsStore'; +import ServicesStore from './ServicesStore'; +import RecipesStore from './RecipesStore'; +import RecipePreviewsStore from './RecipePreviewsStore'; +import UIStore from './UIStore'; +import NewsStore from './NewsStore'; +import RequestStore from './RequestStore'; +import GlobalErrorStore from './GlobalErrorStore'; +import { workspaceStore } from '../features/workspaces'; +import { announcementsStore } from '../features/announcements'; +import { communityRecipesStore } from '../features/communityRecipes'; +import { todosStore } from '../features/todos'; + +export default (api, actions, router) => { + const stores = {}; + Object.assign(stores, { + router, + app: new AppStore(stores, api, actions), + user: new UserStore(stores, api, actions), + features: new FeaturesStore(stores, api, actions), + settings: new SettingsStore(stores, api, actions), + services: new ServicesStore(stores, api, actions), + recipes: new RecipesStore(stores, api, actions), + recipePreviews: new RecipePreviewsStore(stores, api, actions), + ui: new UIStore(stores, api, actions), + news: new NewsStore(stores, api, actions), + requests: new RequestStore(stores, api, actions), + globalError: new GlobalErrorStore(stores, api, actions), + workspaces: workspaceStore, + announcements: announcementsStore, + communityRecipes: communityRecipesStore, + todos: todosStore, + }); + + // Initialize all stores + Object.keys(stores).forEach((name) => { + if (stores[name] && stores[name].initialize) { + stores[name].initialize(); + } + }); + return stores; +}; diff --git a/src/webview/badge.js b/src/webview/badge.js deleted file mode 100644 index 6be4cf609..000000000 --- a/src/webview/badge.js +++ /dev/null @@ -1,36 +0,0 @@ -const { ipcRenderer } = require('electron'); - -const debug = require('debug')('Ferdi:Plugin:BadgeHandler'); - -export class BadgeHandler { - constructor() { - this.countCache = { - direct: 0, - indirect: 0, - }; - } - - _normalizeNumber(count) { - // Parse number to integer - // This will correct errors that recipes may introduce, e.g. - // by sending a String instead of an integer - const parsedNumber = parseInt(count, 10); - const adjustedNumber = Number.isNaN(parsedNumber) ? 0 : parsedNumber; - return Math.max(adjustedNumber, 0); - } - - setBadge(direct, indirect) { - if (this.countCache.direct === direct - && this.countCache.indirect === indirect) return; - - const count = { - direct: this._normalizeNumber(direct), - indirect: this._normalizeNumber(indirect), - }; - - ipcRenderer.sendToHost('message-counts', count); - Object.assign(this.countCache, count); - - debug('Sending badge count to host', count); - } -} diff --git a/src/webview/badge.ts b/src/webview/badge.ts new file mode 100644 index 000000000..fc420b903 --- /dev/null +++ b/src/webview/badge.ts @@ -0,0 +1,40 @@ +import { ipcRenderer } from 'electron'; + +const debug = require('debug')('Ferdi:Plugin:BadgeHandler'); + +export class BadgeHandler { + countCache: { direct: number; indirect: number; }; + + constructor() { + this.countCache = { + direct: 0, + indirect: 0, + }; + } + + _normalizeNumber(count: string | number) { + // Parse number to integer + // This will correct errors that recipes may introduce, e.g. + // by sending a String instead of an integer + const parsedNumber = parseInt(count.toString(), 10); + const adjustedNumber = Number.isNaN(parsedNumber) ? 0 : parsedNumber; + return Math.max(adjustedNumber, 0); + } + + setBadge(direct: number, indirect: number) { + if (this.countCache.direct === direct + && this.countCache.indirect === indirect) { + return; + } + + const count = { + direct: this._normalizeNumber(direct), + indirect: this._normalizeNumber(indirect), + }; + + debug('Sending badge count to host', count); + ipcRenderer.sendToHost('message-counts', count); + + Object.assign(this.countCache, count); + } +} diff --git a/src/webview/darkmode.js b/src/webview/darkmode.js deleted file mode 100644 index 7435d6404..000000000 --- a/src/webview/darkmode.js +++ /dev/null @@ -1,38 +0,0 @@ -/* eslint no-bitwise: ["error", { "int32Hint": true }] */ - -import { join } from 'path'; -import { pathExistsSync, readFileSync } from 'fs-extra'; - -const debug = require('debug')('Ferdi:DarkMode'); - -const chars = [...'abcdefghijklmnopqrstuvwxyz']; - -const ID = [...Array(20)].map(() => chars[Math.random() * chars.length | 0]).join``; - -export function injectDarkModeStyle(recipePath) { - const darkModeStyle = join(recipePath, 'darkmode.css'); - if (pathExistsSync(darkModeStyle)) { - const data = readFileSync(darkModeStyle); - const styles = document.createElement('style'); - styles.id = ID; - styles.innerHTML = data.toString(); - - document.querySelector('head').appendChild(styles); - - debug('Injected Dark Mode style with ID', ID); - } -} - -export function removeDarkModeStyle() { - const style = document.querySelector(`#${ID}`); - - if (style) { - style.remove(); - - debug('Removed Dark Mode Style with ID', ID); - } -} - -export function isDarkModeStyleInjected() { - return !!document.querySelector(`#${ID}`); -} diff --git a/src/webview/darkmode.ts b/src/webview/darkmode.ts new file mode 100644 index 000000000..e06c22f11 --- /dev/null +++ b/src/webview/darkmode.ts @@ -0,0 +1,38 @@ +/* eslint no-bitwise: ["error", { "int32Hint": true }] */ + +import { join } from 'path'; +import { pathExistsSync, readFileSync } from 'fs-extra'; + +const debug = require('debug')('Ferdi:DarkMode'); + +const chars = [...'abcdefghijklmnopqrstuvwxyz']; + +const ID = [...Array(20)].map(() => chars[Math.random() * chars.length | 0]).join(''); + +export function injectDarkModeStyle(recipePath: string) { + const darkModeStyle = join(recipePath, 'darkmode.css'); + if (pathExistsSync(darkModeStyle)) { + const data = readFileSync(darkModeStyle); + const styles = document.createElement('style'); + styles.id = ID; + styles.innerHTML = data.toString(); + + document.querySelector('head')?.appendChild(styles); + + debug('Injected Dark Mode style with ID', ID); + } +} + +export function removeDarkModeStyle() { + const style = document.querySelector(`#${ID}`); + + if (style) { + style.remove(); + + debug('Removed Dark Mode Style with ID', ID); + } +} + +export function isDarkModeStyleInjected() { + return !!document.querySelector(`#${ID}`); +} diff --git a/src/webview/spellchecker.js b/src/webview/spellchecker.js deleted file mode 100644 index 0f4715207..000000000 --- a/src/webview/spellchecker.js +++ /dev/null @@ -1,39 +0,0 @@ -import { getCurrentWebContents } from '@electron/remote'; -import { SPELLCHECKER_LOCALES } from '../i18n/languages'; -import { DEFAULT_APP_SETTINGS, isMac } from '../environment'; - -const debug = require('debug')('Ferdi:spellchecker'); - -const webContents = getCurrentWebContents(); -const [defaultLocale] = webContents.session.getSpellCheckerLanguages(); -debug('Spellchecker default locale is', defaultLocale); - -export function getSpellcheckerLocaleByFuzzyIdentifier(identifier) { - const locales = Object.keys(SPELLCHECKER_LOCALES).filter((key) => key.toLocaleLowerCase() === identifier.toLowerCase() || key.split('-')[0] === identifier.toLowerCase()); - - if (locales.length >= 1) { - return locales[0]; - } - - return null; -} - -export function switchDict(locale) { - if (isMac) { - debug('Ignoring dictionary changes on macOS'); - return; - } - - debug('Setting spellchecker locale to', locale); - - const locales = []; - const foundLocale = getSpellcheckerLocaleByFuzzyIdentifier(locale); - - if (foundLocale) { - locales.push(foundLocale); - } - - locales.push(defaultLocale, DEFAULT_APP_SETTINGS.fallbackLocale); - - webContents.session.setSpellCheckerLanguages(locales); -} diff --git a/src/webview/spellchecker.ts b/src/webview/spellchecker.ts new file mode 100644 index 000000000..aa9a2350f --- /dev/null +++ b/src/webview/spellchecker.ts @@ -0,0 +1,35 @@ +import { getCurrentWebContents } from '@electron/remote'; +import { SPELLCHECKER_LOCALES } from '../i18n/languages'; +import { DEFAULT_APP_SETTINGS, isMac } from '../environment'; + +const debug = require('debug')('Ferdi:spellchecker'); + +const webContents = getCurrentWebContents(); +const [defaultLocale] = webContents.session.getSpellCheckerLanguages(); +debug('Spellchecker default locale is', defaultLocale); + +export function getSpellcheckerLocaleByFuzzyIdentifier(identifier: string) { + const locales = Object.keys(SPELLCHECKER_LOCALES).filter((key) => key.toLocaleLowerCase() === identifier.toLowerCase() || key.split('-')[0] === identifier.toLowerCase()); + + return locales.length >= 1 ? locales[0] : null; +} + +export function switchDict(locale: string) { + if (isMac) { + debug('Ignoring dictionary changes on macOS'); + return; + } + + debug('Setting spellchecker locale to', locale); + + const locales: string[] = []; + + const foundLocale = getSpellcheckerLocaleByFuzzyIdentifier(locale); + if (foundLocale) { + locales.push(foundLocale); + } + + locales.push(defaultLocale, DEFAULT_APP_SETTINGS.fallbackLocale); + + webContents.session.setSpellCheckerLanguages(locales); +} diff --git a/src/webview/zoom.js b/src/webview/zoom.js deleted file mode 100644 index ec2d672b6..000000000 --- a/src/webview/zoom.js +++ /dev/null @@ -1,35 +0,0 @@ -import { ipcRenderer, webFrame } from 'electron'; - -const maxZoomLevel = 9; -const minZoomLevel = -8; -let zoomLevel = 0; - -ipcRenderer.on('zoomIn', () => { - if (maxZoomLevel > zoomLevel) { - zoomLevel += 1; - } - webFrame.setZoomLevel(zoomLevel); - - ipcRenderer.sendToHost('zoomLevel', { zoom: zoomLevel }); -}); - -ipcRenderer.on('zoomOut', () => { - if (minZoomLevel < zoomLevel) { - zoomLevel -= 1; - } - webFrame.setZoomLevel(zoomLevel); - - ipcRenderer.sendToHost('zoomLevel', { zoom: zoomLevel }); -}); - -ipcRenderer.on('zoomReset', () => { - zoomLevel = 0; - webFrame.setZoomLevel(zoomLevel); - - ipcRenderer.sendToHost('zoomLevel', { zoom: zoomLevel }); -}); - -ipcRenderer.on('setZoom', (e, arg) => { - zoomLevel = arg; - webFrame.setZoomLevel(zoomLevel); -}); diff --git a/src/webview/zoom.ts b/src/webview/zoom.ts new file mode 100644 index 000000000..53ddf46c6 --- /dev/null +++ b/src/webview/zoom.ts @@ -0,0 +1,35 @@ +import { ipcRenderer, webFrame } from 'electron'; + +const maxZoomLevel = 9; +const minZoomLevel = -8; +let zoomLevel = 0; + +ipcRenderer.on('zoomIn', () => { + if (maxZoomLevel > zoomLevel) { + zoomLevel += 1; + } + webFrame.setZoomLevel(zoomLevel); + + ipcRenderer.sendToHost('zoomLevel', { zoom: zoomLevel }); +}); + +ipcRenderer.on('zoomOut', () => { + if (minZoomLevel < zoomLevel) { + zoomLevel -= 1; + } + webFrame.setZoomLevel(zoomLevel); + + ipcRenderer.sendToHost('zoomLevel', { zoom: zoomLevel }); +}); + +ipcRenderer.on('zoomReset', () => { + zoomLevel = 0; + webFrame.setZoomLevel(zoomLevel); + + ipcRenderer.sendToHost('zoomLevel', { zoom: zoomLevel }); +}); + +ipcRenderer.on('setZoom', (_e, arg) => { + zoomLevel = arg; + webFrame.setZoomLevel(zoomLevel); +}); -- cgit v1.2.3-70-g09d2