This repository has been archived by the owner on Sep 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(highlight): use helper from is.js instead of custom implementati…
…on (#888)
- Loading branch information
Showing
8 changed files
with
93 additions
and
151 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,59 +1,36 @@ | ||
import { Component, Input } from '@angular/core'; | ||
import { bem, getPropertyByPath } from '../utils'; | ||
import { ChangeDetectionStrategy, Component, Input } from '@angular/core'; | ||
|
||
import { Hit } from 'instantsearch.js'; | ||
|
||
import { highlight } from 'instantsearch.js/es/helpers'; | ||
import { getPropertyByPath } from 'instantsearch.js/es/lib/utils'; | ||
|
||
@Component({ | ||
selector: 'ais-highlight', | ||
template: `<span [class]="cx()" [innerHtml]="content"></span>`, | ||
changeDetection: ChangeDetectionStrategy.OnPush, | ||
template: `<span class="ais-Highlight" [innerHtml]="content"></span>`, | ||
}) | ||
export class NgAisHighlight { | ||
@Input() attribute: string; | ||
@Input() hit: { _highlightResult?: {}; label?: string; highlighted?: string }; | ||
@Input() tagName: string = 'em'; | ||
|
||
cx = bem('Highlight'); | ||
@Input() hit: Partial<Hit>; | ||
@Input() tagName: string = 'mark'; | ||
|
||
get content() { | ||
if (this.attribute === 'highlighted') { | ||
return this.hit.highlighted | ||
? this.replaceWithTagName(this.hit.highlighted) | ||
: this.hit.label; | ||
} | ||
|
||
if (this.hit.hasOwnProperty('_highlightResult')) { | ||
const attributeHighlighted = getPropertyByPath( | ||
this.hit._highlightResult, | ||
this.attribute | ||
); | ||
|
||
// check that the attributeHighlighted is a string | ||
if ( | ||
attributeHighlighted !== undefined && | ||
typeof attributeHighlighted.value === 'string' | ||
) { | ||
return this.replaceWithTagName(attributeHighlighted.value); | ||
} | ||
} | ||
|
||
const highlightAttributeResult = getPropertyByPath( | ||
this.hit._highlightResult, | ||
this.attribute | ||
); | ||
const fallback = getPropertyByPath(this.hit, this.attribute); | ||
if (!fallback) { | ||
console.warn( | ||
`Could not find attribute [${ | ||
this.attribute | ||
}] into hit object, will display an empty string.` | ||
); | ||
|
||
return ''; | ||
// @MAJOR drop this custom fallback once it is implemented directly in instantsearch.js v5 | ||
if (!highlightAttributeResult && fallback) { | ||
return fallback; | ||
} | ||
|
||
return fallback; | ||
} | ||
|
||
replaceWithTagName(value: string) { | ||
return value | ||
.replace( | ||
new RegExp('<em>', 'g'), | ||
`<${this.tagName} class="${this.cx('highlighted')}">` | ||
) | ||
.replace(new RegExp('</em>', 'g'), `</${this.tagName}>`); | ||
return highlight({ | ||
attribute: this.attribute, | ||
highlightedTagName: this.tagName, | ||
hit: this.hit, | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.