Skip to content

Commit eb7ed40

Browse files
authored
chore: merge pull request #13 from RostyslavNihrutsa/develop
Refactoring the sidebar
2 parents 5a90a8d + f2b1658 commit eb7ed40

File tree

3 files changed

+99
-36
lines changed

3 files changed

+99
-36
lines changed

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
"@commitlint/cli": "^20.0.0",
6363
"@commitlint/config-conventional": "^20.0.0",
6464
"@release-it/conventional-changelog": "^10.0.1",
65-
"@types/chrome": "^0.1.12",
65+
"@types/chrome": "^0.1.36",
6666
"@types/jest": "^30.0.0",
6767
"husky": "^9.1.7",
6868
"jest": "^30.1.3",

src/sidebar.ts

Lines changed: 97 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ type Color = string | ColorArray;
66
type ColorArray = chrome.extensionTypes.ColorArray;
77

88
type OpenOptions = chrome.sidePanel.OpenOptions;
9+
type CloseOptions = chrome.sidePanel.CloseOptions;
910
type PanelOptions = chrome.sidePanel.PanelOptions;
1011
type PanelBehavior = chrome.sidePanel.PanelBehavior;
1112
type IconDetails = opr.sidebarAction.IconDetails;
@@ -19,6 +20,8 @@ const isAvailableOperaSidebar = (): boolean => globalThis?.opr?.sidebarAction !=
1920
// Chromium standard
2021
const sidePanel = (): typeof chrome.sidePanel | undefined => browser().sidePanel;
2122

23+
export class SidebarError extends Error {}
24+
2225
// Methods
2326
export const getSidebarOptions = (tabId?: number): Promise<PanelOptions> =>
2427
callWithPromise(cb => {
@@ -56,8 +59,22 @@ export const canOpenSidebar = (): boolean => {
5659
return false;
5760
};
5861

62+
export const canCloseSidebar = (): boolean => {
63+
if (sidePanel()) {
64+
return true;
65+
}
66+
67+
const sb = (sidebarAction() as FirefoxSidebarAction) || undefined;
68+
69+
if (sb) {
70+
return !!sb.close;
71+
}
72+
73+
return false;
74+
};
75+
5976
export const openSidebar = (options: OpenOptions): Promise<void> =>
60-
callWithPromise(cb => {
77+
callWithPromise(async cb => {
6178
const sp = sidePanel();
6279

6380
if (sp) {
@@ -67,22 +84,47 @@ export const openSidebar = (options: OpenOptions): Promise<void> =>
6784
const sb = sidebarAction() as FirefoxSidebarAction | undefined;
6885

6986
if (sb?.open) {
70-
return sb.open();
87+
const result = sb.open();
88+
89+
if (result instanceof Promise) {
90+
await result;
91+
}
92+
93+
return cb();
94+
}
95+
96+
throw new SidebarError("The sidebarAction.open API is not supported in this browser");
97+
});
98+
99+
export const closeSidebar = (options: CloseOptions): Promise<void> =>
100+
callWithPromise(async cb => {
101+
const sp = sidePanel();
102+
103+
if (sp) {
104+
return sp.close(options, cb);
71105
}
72106

73-
console.warn("The sidebar open API is not supported in this browser");
107+
const sb = sidebarAction() as FirefoxSidebarAction | undefined;
108+
109+
if (sb?.close) {
110+
const result = sb.close();
74111

75-
cb();
112+
if (result instanceof Promise) {
113+
await result;
114+
}
115+
116+
return cb();
117+
}
118+
119+
throw new SidebarError("The sidebarAction.close API is not supported in this browser");
76120
});
77121

78122
export const setSidebarOptions = (options?: PanelOptions): Promise<void> =>
79123
callWithPromise(cb => {
80124
const sp = sidePanel();
81125

82126
if (!sp) {
83-
console.warn("The chrome.sidePanel.setOptions API is not supported for this browser");
84-
85-
return cb();
127+
throw new SidebarError("The chrome.sidePanel.setOptions API is not supported for this browser");
86128
}
87129

88130
sp.setOptions(options || {}, cb);
@@ -93,12 +135,44 @@ export const setSidebarBehavior = (behavior?: PanelBehavior): Promise<void> =>
93135
const sp = sidePanel();
94136

95137
if (!sp) {
96-
console.warn("The chrome.sidePanel.setPanelBehavior API is not supported in this browser");
138+
throw new SidebarError("The chrome.sidePanel.setPanelBehavior API is not supported in this browser");
139+
}
140+
141+
sp.setPanelBehavior(behavior || {}, cb);
142+
});
143+
144+
export const isOpenSidebar = (windowId?: number): Promise<boolean> =>
145+
callWithPromise(async cb => {
146+
const sb = sidebarAction() as FirefoxSidebarAction | undefined;
147+
148+
if (sb?.isOpen) {
149+
const result = sb.isOpen({windowId});
150+
151+
if (result instanceof Promise) {
152+
return cb(await result);
153+
} else {
154+
return cb(result);
155+
}
156+
}
157+
158+
throw new SidebarError("The sidebarAction.isOpen API is not supported in this browser");
159+
});
160+
161+
export const toggleSidebar = (): Promise<void> =>
162+
callWithPromise(async cb => {
163+
const sb = sidebarAction() as FirefoxSidebarAction | undefined;
164+
165+
if (sb?.toggle) {
166+
const result = sb.toggle();
167+
168+
if (result instanceof Promise) {
169+
await result;
170+
}
97171

98172
return cb();
99173
}
100174

101-
sp.setPanelBehavior(behavior || {}, cb);
175+
throw new SidebarError("The sidebarAction.toggle API is not supported in this browser");
102176
});
103177

104178
// Customs methods
@@ -153,19 +227,16 @@ export const setSidebarTitle = (title: string | number, tabId?: number): Promise
153227
callWithPromise(async cb => {
154228
const sb = sidebarAction();
155229

156-
if (!sb) {
157-
console.warn("The sidebarAction.setTitle API is supported only in Opera or Firefox");
230+
if (sb?.setTitle) {
231+
const result = sb.setTitle({tabId, title: title.toString()});
158232

233+
if (result instanceof Promise) {
234+
await result;
235+
}
159236
return cb();
160237
}
161238

162-
const result = sb.setTitle({tabId, title: title.toString()});
163-
164-
if (result instanceof Promise) {
165-
await result;
166-
}
167-
168-
cb();
239+
throw new SidebarError("The sidebarAction.setTitle API is supported only in Opera or Firefox");
169240
});
170241

171242
export const setSidebarBadgeText = (text: string | number, tabId?: number): Promise<void> =>
@@ -178,9 +249,7 @@ export const setSidebarBadgeText = (text: string | number, tabId?: number): Prom
178249
return cb();
179250
}
180251

181-
console.warn("The opr.sidebarAction.setBadgeText API is supported only in Opera");
182-
183-
cb();
252+
throw new SidebarError("The sidebarAction.setBadgeText API is supported only in Opera");
184253
});
185254

186255
export const clearSidebarBadgeText = (tabId?: number): Promise<void> => setSidebarBadgeText("", tabId);
@@ -211,9 +280,7 @@ export const setSidebarIcon = (details: IconDetails): Promise<void> =>
211280
return cb();
212281
}
213282

214-
console.warn("The sidebarAction.setIcon API is supported only in Opera or Firefox");
215-
216-
cb();
283+
throw new SidebarError("The sidebarAction.setIcon API is supported only in Opera or Firefox");
217284
});
218285

219286
export const setSidebarBadgeTextColor = (color: Color, tabId?: number): Promise<void> =>
@@ -226,9 +293,7 @@ export const setSidebarBadgeTextColor = (color: Color, tabId?: number): Promise<
226293
return cb();
227294
}
228295

229-
console.warn("The opr.sidebarAction.setBadgeTextColor API is supported only in Opera");
230-
231-
cb();
296+
throw new SidebarError("The sidebarAction.setBadgeTextColor API is supported only in Opera");
232297
});
233298

234299
export const setSidebarBadgeBgColor = (color: Color, tabId?: number): Promise<void> =>
@@ -241,9 +306,7 @@ export const setSidebarBadgeBgColor = (color: Color, tabId?: number): Promise<vo
241306
return cb();
242307
}
243308

244-
console.warn("The opr.sidebarAction.setBadgeBackgroundColor API is supported only in Opera");
245-
246-
cb();
309+
throw new SidebarError("The sidebarAction.setBadgeBackgroundColor API is supported only in Opera");
247310
});
248311

249312
export const getSidebarTitle = (tabId?: number): Promise<string> =>
@@ -258,7 +321,7 @@ export const getSidebarTitle = (tabId?: number): Promise<string> =>
258321
return (sb as any).getTitle({tabId});
259322
}
260323

261-
throw new Error("The sidebarAction.getTitle API not available");
324+
throw new SidebarError("The sidebarAction.getTitle API not available");
262325
});
263326

264327
export const getSidebarBadgeText = (tabId?: number): Promise<string> =>
@@ -269,7 +332,7 @@ export const getSidebarBadgeText = (tabId?: number): Promise<string> =>
269332
return sb.getBadgeText({tabId}, cb);
270333
}
271334

272-
throw new Error("The opr.sidebarAction.getBadgeText API is supported only in Opera");
335+
throw new SidebarError("The sidebarAction.getBadgeText API is supported only in Opera");
273336
});
274337

275338
export const getSidebarBadgeTextColor = (tabId?: number): Promise<ColorArray> =>
@@ -280,7 +343,7 @@ export const getSidebarBadgeTextColor = (tabId?: number): Promise<ColorArray> =>
280343
return sb.getBadgeTextColor({tabId}, cb);
281344
}
282345

283-
throw new Error("The opr.sidebarAction.getBadgeTextColor API is supported only in Opera");
346+
throw new SidebarError("The sidebarAction.getBadgeTextColor API is supported only in Opera");
284347
});
285348

286349
export const getSidebarBadgeBgColor = (tabId?: number): Promise<ColorArray> =>
@@ -291,5 +354,5 @@ export const getSidebarBadgeBgColor = (tabId?: number): Promise<ColorArray> =>
291354
return sb.getBadgeBackgroundColor({tabId}, cb);
292355
}
293356

294-
throw new Error("The opr.sidebarAction.getBadgeBackgroundColor API is supported only in Opera");
357+
throw new SidebarError("The sidebarAction.getBadgeBackgroundColor API is supported only in Opera");
295358
});

0 commit comments

Comments
 (0)