From 659f53c8c61e1ce516307842e6dcdff0c2c29d06 Mon Sep 17 00:00:00 2001 From: Nikita Date: Wed, 14 Oct 2020 19:58:11 +0300 Subject: [PATCH] refactor --- public/manifest.json | 2 +- rollup.config.js | 4 +- src/__tests__/time.test.ts | 2 +- src/{utils => common}/keyboard-keys.ts | 0 src/{ => common}/storage.ts | 2 +- src/components/Hotkey.svelte | 20 ------ src/content_script.ts | 71 -------------------- src/content_scripts.ts | 24 +++++++ src/{utils => content_scripts}/dom.ts | 0 src/content_scripts/hotkey.ts | 56 +++++++++++++++ src/{utils => content_scripts}/time.ts | 0 src/options.ts | 2 +- src/{ => options}/components/Fieldset.svelte | 0 src/options/components/HotkeyCols.svelte | 24 +++++++ src/{ => options}/components/Options.svelte | 56 +++++++++------ 15 files changed, 147 insertions(+), 116 deletions(-) rename src/{utils => common}/keyboard-keys.ts (100%) rename src/{ => common}/storage.ts (88%) delete mode 100644 src/components/Hotkey.svelte delete mode 100644 src/content_script.ts create mode 100644 src/content_scripts.ts rename src/{utils => content_scripts}/dom.ts (100%) create mode 100644 src/content_scripts/hotkey.ts rename src/{utils => content_scripts}/time.ts (100%) rename src/{ => options}/components/Fieldset.svelte (100%) create mode 100644 src/options/components/HotkeyCols.svelte rename src/{ => options}/components/Options.svelte (72%) diff --git a/public/manifest.json b/public/manifest.json index 18daff4..5af4a80 100644 --- a/public/manifest.json +++ b/public/manifest.json @@ -15,7 +15,7 @@ "content_scripts": [ { "matches": ["*://www.workflowy.com/*", "*://workflowy.com/*"], - "js": ["build/content_script.js"] + "js": ["build/content_scripts.js"] } ], "permissions": ["storage"] diff --git a/rollup.config.js b/rollup.config.js index 1734338..da3a462 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -21,7 +21,7 @@ function createConfig(filename, useSvelte = false) { dev: !production, // we'll extract any component CSS out into // a separate file - better for performance - css: css => { + css: (css) => { css.write(`${filename}.css`, false); }, preprocess: sveltePreprocess(), @@ -49,4 +49,4 @@ function createConfig(filename, useSvelte = false) { }; } -export default [createConfig("options", true), createConfig("content_script")]; +export default [createConfig("options", true), createConfig("content_scripts")]; diff --git a/src/__tests__/time.test.ts b/src/__tests__/time.test.ts index a8bef07..becb9f8 100644 --- a/src/__tests__/time.test.ts +++ b/src/__tests__/time.test.ts @@ -1,4 +1,4 @@ -import { getTagSeconds } from "../utils/time"; +import { getTagSeconds } from "../content_scripts/time"; describe("time tests", () => { test("getTagSeconds test", () => { diff --git a/src/utils/keyboard-keys.ts b/src/common/keyboard-keys.ts similarity index 100% rename from src/utils/keyboard-keys.ts rename to src/common/keyboard-keys.ts diff --git a/src/storage.ts b/src/common/storage.ts similarity index 88% rename from src/storage.ts rename to src/common/storage.ts index 88dc404..6a35adf 100644 --- a/src/storage.ts +++ b/src/common/storage.ts @@ -1,4 +1,4 @@ -import type { IHotkey } from "./utils/keyboard-keys"; +import type { IHotkey } from "./keyboard-keys"; export type IStorage = { /** Filter by hashtags on hotkey */ diff --git a/src/components/Hotkey.svelte b/src/components/Hotkey.svelte deleted file mode 100644 index 8318314..0000000 --- a/src/components/Hotkey.svelte +++ /dev/null @@ -1,20 +0,0 @@ - - - - - - diff --git a/src/content_script.ts b/src/content_script.ts deleted file mode 100644 index 3803823..0000000 --- a/src/content_script.ts +++ /dev/null @@ -1,71 +0,0 @@ -import setHotkey from "hotkeys-js"; -import { defaultStorage, IStorage } from "./storage"; -import { createHashtag } from "./utils/dom"; -import { createObserver, highlight, renderTotalTime } from "./utils/time"; - -// By default hotkeys are not enabled for INPUT, SELECT, TEXTAREA elements. -setHotkey.filter = function (event) { - return true; -}; - -chrome.storage.sync.get(defaultStorage, ({ filters, calcTotalTime, swapHashtags }: IStorage) => { - // Start calculate total time - if (calcTotalTime) { - highlight(); - renderTotalTime(); - createObserver(); - } - - // Start filters by hotkey - for (const filter of filters) { - setHotkey(`${filter.specialKey}+${filter.key}`, function (event, handler) { - // Prevent the default refresh event under WINDOWS system - event.preventDefault(); - - if (filter.hashtags) { - location.hash = - "?q=" + filter.hashtags.split(" ").reduce((acc, val) => `${acc}#${val} `, ""); - } else { - location.hash = ""; - } - }); - } - - // Start swap hashtags on hotkey - if (swapHashtags) { - setHotkey(`${swapHashtags.specialKey}+${swapHashtags.key}`, function (event, handler) { - // Prevent the default refresh event under WINDOWS system - event.preventDefault(); - - const activeElement = document.activeElement; - const innerContentContainer = activeElement.querySelector(".innerContentContainer"); - - if (innerContentContainer) { - const deleteTags = swapHashtags.delete ? swapHashtags.delete.split(" ") : []; - const insertTags = swapHashtags.insert ? swapHashtags.insert.split(" ") : []; - - for (const tag of document.activeElement.querySelectorAll(".contentTag")) { - const contentTagText = tag.querySelector(".contentTagText"); - - if (deleteTags.includes(contentTagText.textContent)) { - tag.remove(); - } - } - - for (const tag of insertTags) { - innerContentContainer.append(" "); - innerContentContainer.append(createHashtag(tag)); - // remove possible resulting double spaces - innerContentContainer.innerHTML = innerContentContainer.innerHTML.replace( - /\s{2,}\ { + // Start calculate total time + if (calcTotalTime) { + highlight(); + renderTotalTime(); + createObserver(); + } + + startFiltersOnHotkey(filters); + + if (swapHashtags) { + startSwapHashtagsOnHotkey(swapHashtags); + } +}); diff --git a/src/utils/dom.ts b/src/content_scripts/dom.ts similarity index 100% rename from src/utils/dom.ts rename to src/content_scripts/dom.ts diff --git a/src/content_scripts/hotkey.ts b/src/content_scripts/hotkey.ts new file mode 100644 index 0000000..510ad3a --- /dev/null +++ b/src/content_scripts/hotkey.ts @@ -0,0 +1,56 @@ +import setHotkey from "hotkeys-js"; +import type { IStorage } from "../common/storage"; +import { createHashtag } from "./dom"; + +export function startFiltersOnHotkey(filters: IStorage["filters"]) { + for (const filter of filters) { + setHotkey(`${filter.specialKey}+${filter.key}`, function (event, handler) { + // Prevent the default refresh event under WINDOWS system + event.preventDefault(); + + if (filter.hashtags) { + location.hash = + "?q=" + filter.hashtags.split(" ").reduce((acc, val) => `${acc}#${val} `, ""); + } else { + location.hash = ""; + } + }); + } +} + +export function startSwapHashtagsOnHotkey(swapHashtags: IStorage["swapHashtags"]) { + setHotkey(`${swapHashtags.specialKey}+${swapHashtags.key}`, function (event, handler) { + // Prevent the default refresh event under WINDOWS system + event.preventDefault(); + + const activeElement = document.activeElement; + const innerContentContainer = activeElement.querySelector(".innerContentContainer"); + + if (innerContentContainer) { + const deleteTags = swapHashtags.delete ? swapHashtags.delete.split(" ") : []; + const insertTags = swapHashtags.insert ? swapHashtags.insert.split(" ") : []; + + for (const tag of document.activeElement.querySelectorAll(".contentTag")) { + const contentTagText = tag.querySelector(".contentTagText"); + + if (deleteTags.includes(contentTagText.textContent)) { + tag.remove(); + } + } + + for (const tag of insertTags) { + innerContentContainer.append(" "); + innerContentContainer.append(createHashtag(tag)); + // remove possible resulting double spaces + innerContentContainer.innerHTML = innerContentContainer.innerHTML.replace( + /\s{2,}\ + import { keys, specialKeys } from "../../common/keyboard-keys"; + + export let key = ""; + export let specialKey = ""; + + + + + + + + + + diff --git a/src/components/Options.svelte b/src/options/components/Options.svelte similarity index 72% rename from src/components/Options.svelte rename to src/options/components/Options.svelte index 07e76e6..420a1e7 100644 --- a/src/components/Options.svelte +++ b/src/options/components/Options.svelte @@ -1,9 +1,9 @@