Skip to content

Commit ff54ab1

Browse files
authored
feat: add support for passing options to the search component (#43)
支持在调用搜索弹窗的方法中传入查询参数,示例: ```ts SearchWidget.open({ includeCategoryNames:['333333'] }) ``` 此举可以方便用户或者开发者在其他地方调用搜索功能时,能够自定义查询范围等参数。 /kind feature ```release-note 支持在调用搜索弹窗的方法中传入查询参数。 ```
1 parent 36df00c commit ff54ab1

File tree

7 files changed

+63
-47
lines changed

7 files changed

+63
-47
lines changed

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"release:packages": "pnpm --filter \"./packages/**\" release"
1818
},
1919
"devDependencies": {
20+
"@halo-dev/api-client": "2.17.0",
2021
"@rushstack/eslint-patch": "^1.5.1",
2122
"@types/node": "^18.18.6",
2223
"@typescript-eslint/eslint-plugin": "^6.9.0",

packages/search-widget/package.json

-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
"prettier": "prettier \"**/*.{cjs,html,js,json,md,ts}\" --ignore-path ./.gitignore --write"
2424
},
2525
"dependencies": {
26-
"@halo-dev/api-client": "2.17.0",
2726
"lit": "^3.1.4",
2827
"lodash-es": "^4.17.21"
2928
},

packages/search-widget/src/search-form.ts

+13-13
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,12 @@ import varStyles from './styles/var';
1111

1212
@customElement('search-form')
1313
export class SearchForm extends LitElement {
14-
constructor() {
15-
super();
16-
this.addEventListener('keydown', this.handleKeydown);
17-
}
18-
1914
@property({ type: String })
2015
baseUrl = '';
2116

17+
@property({ type: Object })
18+
options = {};
19+
2220
@state()
2321
private searchResult?: SearchResult;
2422

@@ -30,6 +28,12 @@ export class SearchForm extends LitElement {
3028

3129
inputRef: Ref<HTMLInputElement> = createRef<HTMLInputElement>();
3230

31+
constructor() {
32+
super();
33+
34+
this.addEventListener('keydown', this.handleKeydown);
35+
}
36+
3337
override render() {
3438
return html`
3539
<div class="search-form__input">
@@ -117,25 +121,21 @@ export class SearchForm extends LitElement {
117121

118122
fetchHits: DebouncedFunc<(keyword: string) => Promise<void>> = debounce(
119123
async (keyword: string) => {
120-
const options: SearchOption = {
121-
annotations: {},
124+
const searchOptions: SearchOption = {
125+
...this.options,
122126
highlightPostTag: '</mark>',
123127
highlightPreTag: '<mark>',
124-
includeCategoryNames: [],
125-
includeOwnerNames: [],
126-
includeTagNames: [],
127-
includeTypes: ['post'],
128128
keyword,
129129
limit: 20,
130130
};
131131

132132
const response = await fetch(
133-
`/apis/api.halo.run/v1alpha1/indices/-/search?keyword=${keyword}`,
133+
`/apis/api.halo.run/v1alpha1/indices/-/search`,
134134
{
135135
headers: {
136136
'Content-Type': 'application/json',
137137
},
138-
body: JSON.stringify(options),
138+
body: JSON.stringify(searchOptions),
139139
method: 'post',
140140
}
141141
);

packages/search-widget/src/search-modal.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ export class SearchModal extends LitElement {
1515
@property({ type: String })
1616
baseUrl = '';
1717

18+
@property({ type: Object })
19+
options = {};
20+
1821
override render() {
1922
return html`<div
2023
class="modal__wrapper"
@@ -23,7 +26,10 @@ export class SearchModal extends LitElement {
2326
<div class="modal__layer" @click="${this.close}"></div>
2427
<div class="modal__content">
2528
${this.open
26-
? html`<search-form baseUrl=${this.baseUrl}></search-form>`
29+
? html`<search-form
30+
.baseUrl=${this.baseUrl}
31+
.options=${this.options}
32+
></search-form>`
2733
: ''}
2834
</div>
2935
</div>`;

packages/widget/src/index.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { SearchModal } from '@halo-dev/search-widget';
22
import '@halo-dev/search-widget/var.css';
3+
import type { SearchOption } from '@halo-dev/api-client';
34

45
export { SearchModal };
56

@@ -9,6 +10,7 @@ const searchModalElement = document.createElement(
910

1011
document.body.append(searchModalElement);
1112

12-
export function open() {
13+
export function open(options: SearchOption) {
14+
searchModalElement.options = options;
1315
searchModalElement.open = true;
1416
}

pnpm-lock.yaml

+8-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)