Skip to content

Commit bb7988a

Browse files
committed
fix prettier
1 parent b5822a9 commit bb7988a

File tree

5 files changed

+217
-211
lines changed

5 files changed

+217
-211
lines changed

.prettierrc.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"trailingComma": "none",
3+
"tabWidth": 2,
4+
"semi": false,
5+
"singleQuote": true
6+
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"scripts": {
1414
"clean": "rm -rf dist",
1515
"lint": "eslint './src/**' './test/**.js'",
16-
"prebuild": "npm run clean",
16+
"prebuild": "npm run clean & npm run lint",
1717
"build": "tsc --outDir dist/umd --module umd && tsc",
1818
"pretest": "npm run build",
1919
"test": "karma start test/karma.config.js",

prettier.config.js

Whitespace-only changes.

src/index.ts

Lines changed: 92 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -1,192 +1,192 @@
1-
const states = new WeakMap();
1+
const states = new WeakMap()
22

33
class RemoteInputElement extends HTMLElement {
44
constructor() {
5-
super();
6-
const fetch = fetchResults.bind(null, this, true);
5+
super()
6+
const fetch = fetchResults.bind(null, this, true)
77
const state = {
88
currentQuery: null,
99
oninput: debounce<Event>((e) => fetch(e)),
1010
fetch,
11-
controller: null,
12-
};
13-
states.set(this, state);
11+
controller: null
12+
}
13+
states.set(this, state)
1414
}
1515

1616
static get observedAttributes() {
17-
return ["src"];
17+
return ['src']
1818
}
1919

2020
attributeChangedCallback(name: string, oldValue: string) {
21-
if (oldValue && name === "src") {
22-
fetchResults(this, false);
21+
if (oldValue && name === 'src') {
22+
fetchResults(this, false)
2323
}
2424
}
2525

2626
connectedCallback() {
27-
const input = this.input;
28-
if (!input) return;
27+
const input = this.input
28+
if (!input) return
2929

30-
input.setAttribute("autocomplete", "off");
31-
input.setAttribute("spellcheck", "false");
30+
input.setAttribute('autocomplete', 'off')
31+
input.setAttribute('spellcheck', 'false')
3232

33-
const state = states.get(this);
34-
if (!state) return;
33+
const state = states.get(this)
34+
if (!state) return
3535

36-
input.addEventListener("focus", state.fetch);
37-
input.addEventListener("change", state.fetch);
38-
input.addEventListener("input", state.oninput);
36+
input.addEventListener('focus', state.fetch)
37+
input.addEventListener('change', state.fetch)
38+
input.addEventListener('input', state.oninput)
3939
}
4040

4141
disconnectedCallback() {
42-
const input = this.input;
43-
if (!input) return;
42+
const input = this.input
43+
if (!input) return
4444

45-
const state = states.get(this);
46-
if (!state) return;
45+
const state = states.get(this)
46+
if (!state) return
4747

48-
input.removeEventListener("focus", state.fetch);
49-
input.removeEventListener("change", state.fetch);
50-
input.removeEventListener("input", state.oninput);
48+
input.removeEventListener('focus', state.fetch)
49+
input.removeEventListener('change', state.fetch)
50+
input.removeEventListener('input', state.oninput)
5151
}
5252

5353
get input(): HTMLInputElement | HTMLTextAreaElement | null {
54-
const input = this.querySelector("input, textarea");
54+
const input = this.querySelector('input, textarea')
5555
return input instanceof HTMLInputElement ||
5656
input instanceof HTMLTextAreaElement
5757
? input
58-
: null;
58+
: null
5959
}
6060

6161
get src(): string {
62-
return this.getAttribute("src") || "";
62+
return this.getAttribute('src') || ''
6363
}
6464

6565
set src(url: string) {
66-
this.setAttribute("src", url);
66+
this.setAttribute('src', url)
6767
}
6868
}
6969

7070
function makeAbortController() {
71-
if ("AbortController" in window) {
72-
return new AbortController();
71+
if ('AbortController' in window) {
72+
return new AbortController()
7373
}
7474

75-
return { signal: null, abort() {} };
75+
return { signal: null, abort() {} }
7676
}
7777

7878
async function fetchResults(
7979
remoteInput: RemoteInputElement,
8080
checkCurrentQuery: boolean,
81-
event?: Event,
81+
event?: Event
8282
) {
83-
const input = remoteInput.input;
84-
if (!input) return;
83+
const input = remoteInput.input
84+
if (!input) return
8585

86-
const state = states.get(remoteInput);
87-
if (!state) return;
86+
const state = states.get(remoteInput)
87+
if (!state) return
8888

89-
const query = input.value;
90-
if (checkCurrentQuery && state.currentQuery === query) return;
89+
const query = input.value
90+
if (checkCurrentQuery && state.currentQuery === query) return
9191

92-
state.currentQuery = query;
92+
state.currentQuery = query
9393

94-
const src = remoteInput.src;
95-
if (!src) return;
94+
const src = remoteInput.src
95+
if (!src) return
9696

9797
const resultsContainer = document.getElementById(
98-
remoteInput.getAttribute("aria-owns") || "",
99-
);
100-
if (!resultsContainer) return;
98+
remoteInput.getAttribute('aria-owns') || ''
99+
)
100+
if (!resultsContainer) return
101101

102-
const url = new URL(src, window.location.href);
103-
const params = new URLSearchParams(url.search);
104-
params.append(remoteInput.getAttribute("param") || "q", query);
105-
url.search = params.toString();
102+
const url = new URL(src, window.location.href)
103+
const params = new URLSearchParams(url.search)
104+
params.append(remoteInput.getAttribute('param') || 'q', query)
105+
url.search = params.toString()
106106

107107
if (state.controller) {
108-
state.controller.abort();
108+
state.controller.abort()
109109
} else {
110-
remoteInput.dispatchEvent(new CustomEvent("loadstart"));
111-
remoteInput.setAttribute("loading", "");
110+
remoteInput.dispatchEvent(new CustomEvent('loadstart'))
111+
remoteInput.setAttribute('loading', '')
112112
}
113113

114-
state.controller = makeAbortController();
114+
state.controller = makeAbortController()
115115

116-
let response;
117-
let html = "";
116+
let response
117+
let html = ''
118118
try {
119119
response = await fetchWithNetworkEvents(remoteInput, url.toString(), {
120120
signal: state.controller.signal,
121-
credentials: "same-origin",
122-
headers: { accept: "text/fragment+html" },
123-
});
124-
html = await response.text();
125-
remoteInput.removeAttribute("loading");
126-
state.controller = null;
121+
credentials: 'same-origin',
122+
headers: { accept: 'text/fragment+html' }
123+
})
124+
html = await response.text()
125+
remoteInput.removeAttribute('loading')
126+
state.controller = null
127127
} catch (error) {
128-
if (error instanceof Error && error.name !== "AbortError") {
129-
remoteInput.removeAttribute("loading");
130-
state.controller = null;
128+
if (error instanceof Error && error.name !== 'AbortError') {
129+
remoteInput.removeAttribute('loading')
130+
state.controller = null
131131
}
132-
return;
132+
return
133133
}
134134

135135
if (response && response.ok) {
136-
resultsContainer.innerHTML = html;
136+
resultsContainer.innerHTML = html
137137
remoteInput.dispatchEvent(
138-
new CustomEvent("remote-input-success", {
138+
new CustomEvent('remote-input-success', {
139139
bubbles: true,
140-
detail: { eventType: event ? event.type : undefined },
141-
}),
142-
);
140+
detail: { eventType: event ? event.type : undefined }
141+
})
142+
)
143143
} else {
144144
remoteInput.dispatchEvent(
145-
new CustomEvent("remote-input-error", { bubbles: true }),
146-
);
145+
new CustomEvent('remote-input-error', { bubbles: true })
146+
)
147147
}
148148
}
149149

150150
async function fetchWithNetworkEvents(
151151
el: Element,
152152
url: string,
153-
options: RequestInit,
153+
options: RequestInit
154154
): Promise<Response> {
155155
try {
156-
const response = await fetch(url, options);
157-
el.dispatchEvent(new CustomEvent("load"));
158-
el.dispatchEvent(new CustomEvent("loadend"));
159-
return response;
156+
const response = await fetch(url, options)
157+
el.dispatchEvent(new CustomEvent('load'))
158+
el.dispatchEvent(new CustomEvent('loadend'))
159+
return response
160160
} catch (error) {
161-
if (error instanceof Error && error?.name !== "AbortError") {
162-
el.dispatchEvent(new CustomEvent("error"));
163-
el.dispatchEvent(new CustomEvent("loadend"));
161+
if (error instanceof Error && error?.name !== 'AbortError') {
162+
el.dispatchEvent(new CustomEvent('error'))
163+
el.dispatchEvent(new CustomEvent('loadend'))
164164
}
165-
throw error;
165+
throw error
166166
}
167167
}
168168

169169
function debounce<T>(callback: (_args: T) => void) {
170-
let timeout: ReturnType<typeof setTimeout>;
170+
let timeout: ReturnType<typeof setTimeout>
171171
return function (args: T) {
172-
clearTimeout(timeout);
172+
clearTimeout(timeout)
173173
timeout = setTimeout(() => {
174-
clearTimeout(timeout);
175-
callback(args);
176-
}, 300);
177-
};
174+
clearTimeout(timeout)
175+
callback(args)
176+
}, 300)
177+
}
178178
}
179179

180-
export default RemoteInputElement;
180+
export default RemoteInputElement
181181

182182
declare global {
183183
// eslint-disable-next-line no-unused-vars
184184
interface Window {
185-
RemoteInputElement: typeof RemoteInputElement;
185+
RemoteInputElement: typeof RemoteInputElement
186186
}
187187
}
188188

189-
if (!window.customElements.get("remote-input")) {
190-
window.RemoteInputElement = RemoteInputElement;
191-
window.customElements.define("remote-input", RemoteInputElement);
189+
if (!window.customElements.get('remote-input')) {
190+
window.RemoteInputElement = RemoteInputElement
191+
window.customElements.define('remote-input', RemoteInputElement)
192192
}

0 commit comments

Comments
 (0)