Skip to content

Commit 660146c

Browse files
committed
improvement on initial data
1 parent 18ea05d commit 660146c

File tree

8 files changed

+81
-19
lines changed

8 files changed

+81
-19
lines changed

src/assets/css/github.scss

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
.item:hover {
5858
background: #f6f8fa;
5959

60-
svg path {
60+
.item-close svg path {
6161
stroke: var(--search-js-text-color) !important;
6262
}
6363

src/assets/css/index.scss

+21-1
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,10 @@
114114
}
115115
}
116116

117+
#search-js-histories {
118+
overflow: unset;
119+
}
120+
117121
#search-js-result {
118122
display: none;
119123
}
@@ -155,6 +159,7 @@
155159

156160
.item {
157161
display: flex;
162+
position: relative;
158163
cursor: pointer;
159164
flex-direction: row;
160165
background: var(--search-js-item-bg);
@@ -165,9 +170,24 @@
165170
box-shadow: var(--search-js-item-box-shadow);
166171

167172
.item-icon {
173+
position: relative;
168174
margin-right: 8px;
169175
}
170176

177+
.floating-history-icon {
178+
position: absolute;
179+
left: 50%;
180+
top: 46%;
181+
width: 20px;
182+
height: 20px;
183+
padding: 0px;
184+
border-radius: 250px;
185+
transform: translate(-50%, -50%);
186+
display: flex;
187+
align-items: center;
188+
justify-content: center;
189+
}
190+
171191
.item-title {
172192
color: var(--search-js-text-color);
173193
font-size: 15px;
@@ -183,7 +203,7 @@
183203
background: var(--search-js-theme);
184204
color: white;
185205

186-
svg path {
206+
.item-close svg path {
187207
stroke: white !important;
188208
}
189209

src/components/Item.ts

+17-3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ interface ItemComponentPayload {
77
item: SearchJSItem
88
icon: string
99
hideRemoveButton: boolean
10+
isHistoryIcon?: boolean
1011
}
1112

1213
export interface ListRenderPayload {
@@ -15,6 +16,7 @@ export interface ListRenderPayload {
1516
icon: string
1617
hideRemoveButton: boolean
1718
notFoundLabel: string
19+
isHistoryIcon?: boolean
1820
}
1921

2022
export class Item {
@@ -24,7 +26,14 @@ export class Item {
2426
* @param {Array<SearchJSItem>} items
2527
* @returns {void}
2628
*/
27-
public renderList({ id, items, hideRemoveButton, notFoundLabel, icon }: ListRenderPayload): void {
29+
public renderList({
30+
id,
31+
items,
32+
hideRemoveButton,
33+
notFoundLabel,
34+
icon,
35+
isHistoryIcon,
36+
}: ListRenderPayload): void {
2837
const element = document.getElementById(id)
2938
element.innerHTML = ``
3039

@@ -39,6 +48,7 @@ export class Item {
3948
item,
4049
icon,
4150
hideRemoveButton,
51+
isHistoryIcon,
4252
})
4353
})
4454

@@ -52,10 +62,14 @@ export class Item {
5262
* @param {ItemComponentPayload} props
5363
* @returns {string}
5464
*/
55-
render({ item, icon, hideRemoveButton = false }: ItemComponentPayload): string {
65+
render({ item, icon, hideRemoveButton = false, isHistoryIcon }: ItemComponentPayload): string {
5666
const dataPayload = Encoder.encode(item)
5767
return `<div class="item" ${ATTR_DATA_PAYLOAD}='${dataPayload}'>
58-
<div class="item-icon">${item.icon ?? icon}</div>
68+
<div class="item-icon">
69+
${item.icon ?? icon}
70+
${item.icon && isHistoryIcon ? `<div class="floating-history-icon">${icon}</div>` : ``}
71+
</div>
72+
5973
<div style="flex: 1">
6074
<div class="item-title">${item.title}</div>
6175
${item.description ? `<div class="item-description">${item.description}</div>` : ``}

src/index.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,12 @@ export class SearchJSApp {
2727
* @param {SearchJSConfig} config
2828
*/
2929
constructor(public config: SearchJSConfig) {
30-
this.component = new SearchComponent(this, new DomListener(), new SearchHistory(), new Theme())
30+
this.component = new SearchComponent(
31+
this,
32+
new DomListener(),
33+
new SearchHistory(this.config.maxHistoryLength ?? 4),
34+
new Theme(),
35+
)
3136
this.listenKeyboardKeyPress()
3237
}
3338

src/themes/AvailableThemes.ts

+5
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export const CssWidth = '--search-js-width'
1616
export const CssHeight = '--search-js-height'
1717
export const CssFontFamily = '--search-js-font-family'
1818
export const CssPositionTop = '--search-js-top'
19+
export const CssBorderColor = '--search-js-border-color'
1920

2021
export const AvailableThemes: any = {
2122
[SearchJSTheme.ThemeDark]: {
@@ -32,6 +33,7 @@ export const AvailableThemes: any = {
3233
[CssItemBackground]: '#1c1e21',
3334
[CssItemBoxShadow]: 'none',
3435
[CssTextColor]: '#b3b3b3',
36+
[CssBorderColor]: '#32354778',
3537
},
3638
[SearchJSTheme.ThemeLight]: {
3739
[CssBackdropBackground]: 'rgba(101, 108, 133, 0.8)',
@@ -46,6 +48,7 @@ export const AvailableThemes: any = {
4648
[CssItemBackground]: 'white',
4749
[CssItemBoxShadow]: '0 1px 3px 0 #d4d9e1',
4850
[CssTextColor]: '#969faf',
51+
[CssBorderColor]: '#cfcfcf4f',
4952
},
5053
[SearchJSTheme.ThemeGithubDark]: {
5154
[CssBackdropBackground]: 'rgba(1,4,9,0.8)',
@@ -60,6 +63,7 @@ export const AvailableThemes: any = {
6063
[CssItemBoxShadow]: 'none',
6164
[CssTextColor]: '#C5CED6',
6265
[CssTheme]: 'transparent',
66+
[CssBorderColor]: '#30363d',
6367
},
6468
[SearchJSTheme.ThemeGithubLight]: {
6569
[CssBackdropBackground]: 'rgba(27,31,36,0.5)',
@@ -74,5 +78,6 @@ export const AvailableThemes: any = {
7478
[CssItemBoxShadow]: 'none',
7579
[CssTextColor]: '#1F2329',
7680
[CssTheme]: 'transparent',
81+
[CssBorderColor]: '#d0d7de',
7782
},
7883
}

src/types/index.ts

+6
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ export interface SearchJSConfig {
4444
*/
4545
positionTop?: string
4646

47+
/**
48+
* max history length
49+
* default: 4
50+
*/
51+
maxHistoryLength?: number
52+
4753
/**
4854
* data to show on the list
4955
*/

src/utils/SearchComponent.ts

+17-4
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ export class SearchComponent {
5757
// render initial data list
5858
this.showHistory(this.searchHistory.getList())
5959

60+
// show initial data list
61+
if (this.app.config.data?.length > 0) this.showSearchResult(this.app.config.data)
62+
6063
this.domListener.onBackDropClick(() => {
6164
if (!this.app.config.persistent) {
6265
this.app.close()
@@ -73,15 +76,16 @@ export class SearchComponent {
7376
*/
7477
private handleOnSearch(): void {
7578
this.domListener.onSearch(async (keyword: string) => {
79+
console.log(keyword)
7680
if (!keyword) {
7781
clearTimeout(this.searchTimer)
7882
this.hideLoading()
7983
this.showHistory(this.searchHistory.getList())
80-
this.hideSearchResult()
84+
this.hideSearchResult(keyword)
8185
return
8286
}
8387
this.hideHistories()
84-
this.hideSearchResult()
88+
this.hideSearchResult(keyword)
8589
if (this.app.config.onSearch) {
8690
this.showLoading()
8791
clearTimeout(this.searchTimer)
@@ -167,8 +171,11 @@ export class SearchComponent {
167171
*
168172
* @returns {void}
169173
*/
170-
private hideSearchResult(): void {
174+
private hideSearchResult(keyword: string): void {
171175
document.getElementById(ID_RESULTS).style.display = 'none'
176+
if (!keyword && this.app.config.data?.length > 0) {
177+
this.showSearchResult(this.app.config.data)
178+
}
172179
}
173180

174181
/**
@@ -178,13 +185,17 @@ export class SearchComponent {
178185
* @returns {void}
179186
*/
180187
private showHistory(items: Array<SearchJSItem>): void {
188+
if (items.length === 0 && this.app.config.data?.length > 0) {
189+
return this.hideHistories()
190+
}
181191
const itemInstance = new Item()
182192
itemInstance.renderList({
183193
id: ID_HISTORIES,
184194
items: items,
185195
hideRemoveButton: false,
186196
notFoundLabel: 'No recent data',
187197
icon: historyIcon(),
198+
isHistoryIcon: true,
188199
})
189200
this.handleItemClickListener()
190201
}
@@ -211,7 +222,9 @@ export class SearchComponent {
211222
},
212223
(data: any) => {
213224
this.searchHistory.remove(data)
214-
this.showHistory(this.searchHistory.getList())
225+
setTimeout(() => {
226+
this.showHistory(this.searchHistory.getList())
227+
}, 100)
215228
},
216229
)
217230
}

src/utils/SearchHistory.ts

+8-9
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,18 @@ export class SearchHistory {
88
*/
99
private db: Storage
1010

11-
/**
12-
* max items to store in history
13-
*
14-
* @var {number} maxItems
15-
*/
16-
private maxItems = 4
17-
1811
/**
1912
* local storage key
2013
*
2114
* @var {string} storageKey
2215
*/
2316
private storageKey = 'search-js-histories'
2417

25-
constructor() {
18+
/**
19+
* constructor
20+
* @param {number} maxItems - max items to store in history
21+
*/
22+
constructor(private maxItems: number) {
2623
this.db = window.localStorage
2724
}
2825

@@ -36,7 +33,7 @@ export class SearchHistory {
3633
if (!data) {
3734
data = '[]'
3835
}
39-
return JSON.parse(data).reverse()
36+
return JSON.parse(data).reverse().slice(0, this.maxItems)
4037
}
4138

4239
/**
@@ -64,6 +61,8 @@ export class SearchHistory {
6461
return JSON.stringify(d) == JSON.stringify(item)
6562
})
6663

64+
console.log(index)
65+
6766
if (index != -1) {
6867
arrayItems.splice(index, 1)
6968
}

0 commit comments

Comments
 (0)