Skip to content

Commit

Permalink
Merge pull request #11 from FSou1/feat/highlight-complete-word
Browse files Browse the repository at this point in the history
feat(complete-words): add complete words highlighting
  • Loading branch information
FSou1 authored May 13, 2023
2 parents 8ff0dbc + 0cbbdb0 commit 0a7fcaf
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 4 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "keywords-highlighter",
"version": "0.0.3",
"version": "0.0.4",
"description": "a chrome extension that highlights keywords on web pages, making it easy to find or skip information",
"license": "MIT",
"repository": {
Expand Down
2 changes: 2 additions & 0 deletions src/pages/content/components/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export default function App() {
className: rule.id,
keywords: rule.keywords,
styles: rule.cssStyles,
highlightCompleteWords: rule.highlightCompleteWords,
};
};

Expand All @@ -27,6 +28,7 @@ export default function App() {
<Highlighter
keywords={rule.keywords}
highlightedClassName={rule.className}
highlightCompleteWords={rule.highlightCompleteWords}
debounceTimeMs={OBSERVABLE_DEBOUNCE_TIME_MS}
/>
))}
Expand Down
5 changes: 4 additions & 1 deletion src/pages/content/components/Highlighter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,24 @@ import { findAndWrap } from "@src/lib/findAndWrapHTMLNodes";
import useMutationObservable, {
DEFAULT_OPTIONS,
} from "../hooks/useMutationObservable";
import { buildFindTextRegExp } from "../utils/regExpHelper";

interface HighlighterProps {
debounceTimeMs: number;
keywords: string;
highlightedClassName: string;
highlightCompleteWords: boolean;
}

export default function Highlighter({
debounceTimeMs,
keywords,
highlightedClassName,
highlightCompleteWords,
}: HighlighterProps) {
const onMutation = useCallback(() => {
findAndWrap(document.body, {
findTextRegExp: new RegExp(keywords.replaceAll(",", "|"), "gi"),
findTextRegExp: buildFindTextRegExp({ keywords, highlightCompleteWords }),
wrapWithTag: "span",
wrapWithClassName: highlightedClassName,
wrapIf: (node: Text) => {
Expand Down
18 changes: 18 additions & 0 deletions src/pages/content/utils/regExpHelper.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
export function buildFindTextRegExp({
keywords,
highlightCompleteWords,
}): RegExp {
return new RegExp(
keywords
.split(",")
.map((word: string) => {
if (highlightCompleteWords) {
return `\\b${word}\\b`;
} else {
return word;
}
})
.join("|"),
"gi"
);
}
1 change: 1 addition & 0 deletions src/pages/options/Options.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export default function Options() {
keywords: null,
cssStyles: null,
enabledOn: null,
highlightCompleteWords: false,
};
};

Expand Down
35 changes: 33 additions & 2 deletions src/pages/options/components/KeywordRule.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import TextField from "@mui/material/TextField";
import Divider from "@mui/material/Divider";
import IconButton from "@mui/material/IconButton";
import DeleteIcon from "@mui/icons-material/Delete";
import Button from "@mui/material/Button";
import FormControlLabel from "@mui/material/FormControlLabel";
import Checkbox from "@mui/material/Checkbox";

export interface IKeywordRule {
id: string;
keywords: string;
cssStyles: string;
enabledOn: string;
highlightCompleteWords: boolean;
}

export type KeywordRuleProps = IKeywordRule & {
Expand All @@ -19,7 +21,14 @@ export type KeywordRuleProps = IKeywordRule & {
};

export default function KeywordRule(props: KeywordRuleProps) {
const { keywords, cssStyles, enabledOn, onRuleChange, onDeleteRule } = props;
const {
keywords,
cssStyles,
enabledOn,
highlightCompleteWords,
onRuleChange,
onDeleteRule,
} = props;

const handleKeywordsChange: React.ChangeEventHandler<HTMLInputElement> = (
event
Expand Down Expand Up @@ -48,6 +57,15 @@ export default function KeywordRule(props: KeywordRuleProps) {
});
};

const handleHighlightCompleteWordsChange: React.ChangeEventHandler<
HTMLInputElement
> = (event) => {
onRuleChange({
...props,
highlightCompleteWords: event.target.checked,
});
};

const handleDeleteClick = () => {
onDeleteRule({
...props,
Expand Down Expand Up @@ -103,6 +121,19 @@ export default function KeywordRule(props: KeywordRuleProps) {
/>
</Grid>

<Grid item xs={12} textAlign="left">
<FormControlLabel
control={
<Checkbox
name="highlightCompleteWords"
checked={highlightCompleteWords}
onChange={handleHighlightCompleteWordsChange}
/>
}
label="Only highlight complete words"
/>
</Grid>

<Grid item xs={12}>
<Divider />
</Grid>
Expand Down

0 comments on commit 0a7fcaf

Please sign in to comment.