Skip to content

Commit

Permalink
Merge pull request #4 from FSou1/feat/css-styles
Browse files Browse the repository at this point in the history
feat(highlighted-styles): allow to set CSS styles
  • Loading branch information
FSou1 authored May 7, 2023
2 parents 4d0e808 + 3a59719 commit 4b1e24d
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 5 deletions.
2 changes: 1 addition & 1 deletion public/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"src/pages/content/index.js"
],
"css": [
"assets/css/contentStyle16833527642.chunk.css"
"assets/css/contentStyle16834342064.chunk.css"
]
}
],
Expand Down
27 changes: 25 additions & 2 deletions src/pages/content/components/index.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { createRoot } from "react-dom/client";
import DebugPopup from "@src/pages/content/components/DebugPopup";
import App from "@src/pages/content/components/App";
import refreshOnUpdate from "virtual:reload-on-update-in-view";
import { get } from "@services/storage/storageService";
import { SETTINGS_DEBUG } from "@services/constants";
import {
SETTINGS_DEBUG,
SETTINGS_HIGHLIGHTED_STYLES,
SETTINGS_HIGHLIGHTED_STYLES_DEFAULT_VALUE,
} from "@services/constants";
import refreshOnUpdate from "virtual:reload-on-update-in-view";

refreshOnUpdate("pages/content");

Expand Down Expand Up @@ -32,7 +36,26 @@ const initApp = () => {
createRoot(root).render(<App />);
};

const addStyleBlock = (styles: string) => {
var sheet = document.createElement("style");
sheet.innerHTML = `.highlighted { ${styles} }`;
document.body.appendChild(sheet);
};

const initStyles = () => {
const key = SETTINGS_HIGHLIGHTED_STYLES,
defaultValue = SETTINGS_HIGHLIGHTED_STYLES_DEFAULT_VALUE;

get(key).then((result) => {
const highlightedStyles = result?.[key] || defaultValue;
if (highlightedStyles) {
addStyleBlock(highlightedStyles);
}
});
};

function init() {
initStyles();
initApp();
initDebug();
}
Expand Down
31 changes: 30 additions & 1 deletion src/pages/popup/Popup.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
import { ChangeEvent } from "react";
import "@pages/popup/Popup.css";
import { SETTINGS_DEBUG, SETTINGS_KEYWORDS } from "@services/constants";
import {
SETTINGS_DEBUG,
SETTINGS_KEYWORDS,
SETTINGS_HIGHLIGHTED_STYLES,
SETTINGS_HIGHLIGHTED_STYLES_DEFAULT_VALUE,
} from "@services/constants";
import useSettingsHook from "./hooks/useSettingsHook";

const Popup = () => {
const [debug, setDebug] = useSettingsHook<boolean>(SETTINGS_DEBUG);
const [keywords, setKeywords] = useSettingsHook<string>(SETTINGS_KEYWORDS);
const [highlightedStyles, setHighlightedStyles] = useSettingsHook<string>(
SETTINGS_HIGHLIGHTED_STYLES
);

const handleDebugChange = (e: ChangeEvent<HTMLInputElement>) => {
setDebug(e.target.checked);
Expand All @@ -15,6 +23,12 @@ const Popup = () => {
setKeywords(e.target.value);
};

const handleHighlightedStylesChange = (
e: ChangeEvent<HTMLTextAreaElement>
) => {
setHighlightedStyles(e.target.value);
};

return (
<div className="App">
<header className="App-header">
Expand Down Expand Up @@ -49,6 +63,21 @@ const Popup = () => {
onChange={handleKeywordsChange}
/>
</div>

<div>
<div>
<label htmlFor="highlightedStyles">Highlighted styles: </label>
</div>
<textarea
id="highlightedStyles"
name="highlightedStyles"
placeholder={SETTINGS_HIGHLIGHTED_STYLES_DEFAULT_VALUE}
cols={45}
rows={8}
value={highlightedStyles}
onChange={handleHighlightedStylesChange}
/>
</div>
</fieldset>
</header>
</div>
Expand Down
2 changes: 1 addition & 1 deletion src/pages/popup/index.css
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
body {
width: 400px;
height: 300px;
height: 440px;
margin: 0;
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Roboto", "Oxygen",
"Ubuntu", "Cantarell", "Fira Sans", "Droid Sans", "Helvetica Neue",
Expand Down
3 changes: 3 additions & 0 deletions src/services/constants.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
export const SETTINGS_DEBUG = "settings.debug";
export const SETTINGS_KEYWORDS = "settings.keywords";
export const SETTINGS_HIGHLIGHTED_STYLES = "settings.highlighted-styles";
export const SETTINGS_HIGHLIGHTED_STYLES_DEFAULT_VALUE =
"background-color: yellow;";
export const OBSERVABLE_DEBOUNCE_TIME_MS = 500;

0 comments on commit 4b1e24d

Please sign in to comment.