Skip to content

Commit

Permalink
Destroy observer
Browse files Browse the repository at this point in the history
  • Loading branch information
wstaeblein committed Oct 3, 2023
1 parent 942ec13 commit ed7a199
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 8 deletions.
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Since one can't style the text in a textarea, as it is not HTML, this class come
![Screenshot](screen.png)


#### [CHECK OUT THE DEMO](https://wstaeblein.github.io/texthighlighter/)
#### [CHECK OUT THE DEMO](https://wstaeblein.github.io/texthighlighter/){:target="_blank"}


## Features
Expand All @@ -26,8 +26,7 @@ Since one can't style the text in a textarea, as it is not HTML, this class come




Its use is very straightforward, instantiate the class for the textarea you need and call the search method to highlight the text you pass in the first argument. The second argument is optional and takes a boolean that if true will make the search case sensitive. The default is case insensitive.
Its use is very straightforward, instantiate the class for the textarea you need and call the search method to highlight the text you pass in the first argument. The second argument is optional and takes a boolean that if true will make the search case sensitive. The default is case insensitive. The last argument also takes a boolean that, if true, will perform a word search. Otherwise it's a free search, where any part of words can be matched.



Expand All @@ -54,7 +53,7 @@ To access how many occurrences were found, use:
let count = hilite.count();
```

You can navigate back and forth among the highlighted results using the prev and next methods. This navigation is cyclic, this means when the end is reached the next press takes you back to the begining and vice versa. Example below:
You can navigate back and forth among the highlighted results using the prev and next methods. This navigation is cyclic, this means when the end is reached the next call takes you back to the begining and vice versa. Example below:

```javascript
let btnPrev = document.getElementById('prev');
Expand Down
2 changes: 1 addition & 1 deletion texthighlighter.css
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
display: none;
}

.hlta-container > .htla-textarea {
.hlta-container > .hlta-textarea {
position: relative;
background: none transparent !important;
}
Expand Down
9 changes: 6 additions & 3 deletions texthighlighter.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class textHighlight {
scroll: this.#scrollHandler.bind(this)
}

this.ele.classList.add('htla-textarea');
this.ele.classList.add('hlta-textarea');
this.ele.addEventListener('input', this.handlers.input);
this.ele.addEventListener('scroll', this.handlers.scroll);

Expand All @@ -41,7 +41,8 @@ class textHighlight {
this.container.append(this.backdrop);
this.container.appendChild(this.ele);

let obs = new ResizeObserver(this.#resizeObs.bind(this)).observe(this.ele);
let observer = new ResizeObserver(this.#resizeObs.bind(this));
observer.observe(this.ele);
this.#inputHandler();
}

Expand Down Expand Up @@ -78,6 +79,7 @@ class textHighlight {
destroy() {
this.ele.removeEventListener('input', this.handlers.input);
this.ele.removeEventListener('scroll', this.handlers.scroll);
observer.disconnect();

this.container.parentNode.insertBefore(this.ele, this.container);
while (this.container.firstChild) {
Expand Down Expand Up @@ -112,8 +114,9 @@ class textHighlight {
}

#markText() {
let txt = this.ele.value.replace(/</g, '&lt;').replace(/>/g, '&gt;');
if (this.searchArg) { console.log(this.ele.value)
let txt = this.ele.value.replace(/</g, '&lt;').replace(/>/g, '&gt;');

let searchArg = this.searchArg.replace(/</g, '&lt;').replace(/>/g, '&gt;');
let boundary = this.word ? '\\b' : '';

Expand Down

0 comments on commit ed7a199

Please sign in to comment.