-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ Store zoom state across browser starts
- Loading branch information
Showing
7 changed files
with
167 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
// @ts-check | ||
/// <reference types="@browser/link" /> | ||
import mitt from 'resource://app/modules/mitt.sys.mjs' | ||
|
||
const ZOOM_STORE_FILE = PathUtils.join(PathUtils.profileDir, 'zoomstore.json') | ||
|
||
/** @typedef {import("resource://app/modules/ZoomStore.sys.mjs").ZoomStoreInterface} ZoomStoreInterface */ | ||
|
||
/** @implements {ZoomStoreInterface} */ | ||
class ZoomStoreImpl { | ||
/** | ||
* @private | ||
* @type {Map<string, number> | null} | ||
*/ | ||
zoomPages = null | ||
|
||
/** @type {import('resource://app/modules/mitt.sys.mjs').Emitter<import('resource://app/modules/ZoomStore.sys.mjs').ZoomStoreEvents>} */ | ||
events = mitt() | ||
|
||
constructor() { | ||
this.init() | ||
} | ||
|
||
/** @protected */ | ||
async init() { | ||
if (this.zoomPages) { | ||
return | ||
} | ||
|
||
if (!(await IOUtils.exists(ZOOM_STORE_FILE))) { | ||
this.zoomPages = new Map() | ||
return | ||
} | ||
|
||
try { | ||
const pages = await IOUtils.readJSON(ZOOM_STORE_FILE) | ||
if (this.zoomPages) { | ||
return | ||
} | ||
|
||
this.zoomPages = new Map(pages) | ||
} catch (e) { | ||
console.error('Failed to load zoomStore from file: ', e) | ||
return | ||
} | ||
} | ||
|
||
/** @private */ | ||
async save() { | ||
if (!this.zoomPages) { | ||
return | ||
} | ||
|
||
const toWrite = Array.from(this.zoomPages.entries()) | ||
try { | ||
await IOUtils.writeJSON(ZOOM_STORE_FILE, toWrite) | ||
} catch (e) { | ||
console.error('Failed to write zoomStore to file:', e) | ||
return | ||
} | ||
} | ||
|
||
/** | ||
* @param {nsIURIType} uri The uri to check zoom for | ||
* @returns {number} | ||
*/ | ||
getZoomForUri(uri) { | ||
try { | ||
return this.zoomPages?.get(uri.host) || 1 | ||
} catch { | ||
return 1 | ||
} | ||
} | ||
|
||
/** | ||
* @param {nsIURIType} uri | ||
* @param {number} zoom The zoom to store. If set to 1, will delete any stored values | ||
*/ | ||
setZoomForUri(uri, zoom) { | ||
try { | ||
uri.host | ||
} catch { | ||
return | ||
} | ||
|
||
if (zoom === 1) { | ||
this.zoomPages?.delete(uri.host) | ||
return | ||
} | ||
|
||
this.zoomPages?.set(uri.host, Math.round(zoom * 100) / 100) | ||
|
||
// @ts-ignore | ||
Services.tm.dispatchToMainThread(() => { | ||
this.events.emit('setZoom', { host: uri.host, zoom }) | ||
this.save() | ||
}) | ||
} | ||
} | ||
|
||
export const ZoomStore = new ZoomStoreImpl() |
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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
declare module 'resource://app/modules/ZoomStore.sys.mjs' { | ||
import { Emitter } from 'mitt' | ||
|
||
export type ZoomStoreEvents = { | ||
setZoom: { host: string; zoom: number } | ||
} | ||
|
||
export const ZoomStore: { | ||
events: Emitter<ZoomStoreEvents> | ||
|
||
getZoomForUri(uri: nsIURIType): number | ||
setZoomForUri(uri: nsIURIType, zoom: number): void | ||
} | ||
|
||
export type ZoomStoreInterface = typeof ZoomStore | ||
} | ||
|
||
declare interface MozESMExportFile { | ||
ZoomStore: 'resource://app/modules/ZoomStore.sys.mjs' | ||
} | ||
|
||
declare interface MozESMExportType { | ||
ZoomStore: typeof import('resource://app/modules/ZoomStore.sys.mjs').ZoomStore | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.