@gallerist/plugin-hash 0.0.0-stage → 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Shafayet Ahmmed
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,29 @@
1
- # Temporary Holding Version
1
+ # @gallerist/plugin-hash
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Gallerist plugin — deep-link the cursor to the URL hash (#g=&i=), with back/forward.
4
+
5
+ Part of [Gallerist](https://github.com/shafayet9780/gallerist#readme) — a headless, framework-agnostic media viewer for grouped / nested galleries with image-space overlays and virtualized rendering.
6
+
7
+ ## Install
8
+
9
+ ```bash
10
+ npm i @gallerist/plugin-hash
11
+ ```
12
+
13
+ ## Usage
14
+
15
+ Tree-shakeable Gallerist plugin. Register it when you create the viewer:
16
+
17
+ ```ts
18
+ import { createViewer } from '@gallerist/core'
19
+ // import the plugin's factory from this package and pass it to `plugins`
20
+ const viewer = createViewer({ items, plugins: [/* plugin() */] })
21
+ ```
22
+
23
+ See [Plugin authoring](https://github.com/shafayet9780/gallerist/blob/main/docs/PLUGIN-AUTHORING.md).
24
+
25
+ Full docs: the [monorepo README](https://github.com/shafayet9780/gallerist#readme) and [`docs/`](https://github.com/shafayet9780/gallerist/tree/main/docs).
26
+
27
+ ## License
28
+
29
+ MIT © Shafayet Ahmmed
package/dist/index.cjs ADDED
@@ -0,0 +1,83 @@
1
+ 'use strict';
2
+
3
+ // src/index.ts
4
+ function parseHash(hash2) {
5
+ const out = {};
6
+ const raw = hash2.replace(/^#/, "");
7
+ const dec = (s) => {
8
+ try {
9
+ return decodeURIComponent(s);
10
+ } catch {
11
+ return s;
12
+ }
13
+ };
14
+ for (const part of raw.split("&")) {
15
+ const eq = part.indexOf("=");
16
+ if (eq < 0) continue;
17
+ const k = part.slice(0, eq);
18
+ const v = part.slice(eq + 1);
19
+ if (!v) continue;
20
+ if (k === "g") out.g = dec(v);
21
+ else if (k === "i") out.i = dec(v);
22
+ }
23
+ return out;
24
+ }
25
+ function formatHash(groupId, itemId) {
26
+ const g = `g=${encodeURIComponent(groupId)}`;
27
+ return itemId !== void 0 ? `${g}&i=${encodeURIComponent(itemId)}` : g;
28
+ }
29
+ function hash(opts = {}) {
30
+ return (viewer) => {
31
+ if (typeof window === "undefined") return { name: "hash" };
32
+ let lastWritten = null;
33
+ const offs = [];
34
+ const applyFromHash = () => {
35
+ const { g, i } = parseHash(window.location.hash);
36
+ if (!g) return false;
37
+ viewer.goToId(g, i);
38
+ return true;
39
+ };
40
+ const writeHash = () => {
41
+ const col = viewer.getCollection();
42
+ const cur = viewer.getState().cursor;
43
+ const group = col.groups[cur.g];
44
+ if (!group) return;
45
+ const item = group.items[cur.i];
46
+ lastWritten = formatHash(group.id, item?.id);
47
+ try {
48
+ window.history.replaceState(window.history.state, "", `#${lastWritten}`);
49
+ } catch {
50
+ window.location.hash = lastWritten;
51
+ }
52
+ };
53
+ const onHashChange = () => {
54
+ if (window.location.hash.replace(/^#/, "") === lastWritten) return;
55
+ applyFromHash();
56
+ };
57
+ return {
58
+ name: "hash",
59
+ init() {
60
+ window.addEventListener("hashchange", onHashChange);
61
+ offs.push(
62
+ viewer.on("slide", writeHash),
63
+ viewer.on("item", writeHash),
64
+ viewer.on("opened", writeHash)
65
+ );
66
+ if (parseHash(window.location.hash).g) {
67
+ applyFromHash();
68
+ if (opts.open !== false) viewer.open();
69
+ }
70
+ },
71
+ destroy() {
72
+ window.removeEventListener("hashchange", onHashChange);
73
+ for (const off of offs) off();
74
+ }
75
+ };
76
+ };
77
+ }
78
+
79
+ exports.formatHash = formatHash;
80
+ exports.hash = hash;
81
+ exports.parseHash = parseHash;
82
+ //# sourceMappingURL=index.cjs.map
83
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.ts"],"names":["hash"],"mappings":";;;AAWO,SAAS,UAAUA,KAAAA,EAA0C;AAClE,EAAA,MAAM,MAAkC,EAAC;AACzC,EAAA,MAAM,GAAA,GAAMA,KAAAA,CAAK,OAAA,CAAQ,IAAA,EAAM,EAAE,CAAA;AAGjC,EAAA,MAAM,GAAA,GAAM,CAAC,CAAA,KAAc;AACzB,IAAA,IAAI;AACF,MAAA,OAAO,mBAAmB,CAAC,CAAA;AAAA,IAC7B,CAAA,CAAA,MAAQ;AACN,MAAA,OAAO,CAAA;AAAA,IACT;AAAA,EACF,CAAA;AACA,EAAA,KAAA,MAAW,IAAA,IAAQ,GAAA,CAAI,KAAA,CAAM,GAAG,CAAA,EAAG;AAEjC,IAAA,MAAM,EAAA,GAAK,IAAA,CAAK,OAAA,CAAQ,GAAG,CAAA;AAC3B,IAAA,IAAI,KAAK,CAAA,EAAG;AACZ,IAAA,MAAM,CAAA,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,MAAA,CAAO,IAAA,EAAK;AAAA,QACvC;AAAA,MACF,CAAA;AAAA,MACA,OAAA,GAAU;AACR,QAAA,MAAA,CAAO,mBAAA,CAAoB,cAAc,YAAY,CAAA;AACrD,QAAA,KAAA,MAAW,GAAA,IAAO,MAAM,GAAA,EAAI;AAAA,MAC9B;AAAA,KACF;AAAA,EACF,CAAA;AACF","file":"index.cjs","sourcesContent":["// @gallerist/plugin-hash — deep-link the cursor to the URL hash `#g=<groupId>&i=<itemId>` (spec §14, §16).\n// Client-only (SSR-guarded); reflects nav into the URL and applies the URL on load + back/forward.\n\nimport type { Plugin } from '@gallerist/core'\n\nexport interface HashOptions {\n /** open the viewer on load if a hash is present (default true). */\n open?: boolean\n}\n\n/** Parse a location.hash like \"#g=mon-2&i=s3\" → { g, i } (values URL-decoded). */\nexport function parseHash(hash: string): { g?: string; i?: string } {\n const out: { g?: string; i?: string } = {}\n const raw = hash.replace(/^#/, '')\n // The hash is untrusted (any user can edit the address bar): tolerate a malformed percent\n // escape instead of throwing URIError (I-PL1).\n const dec = (s: string) => {\n try {\n return decodeURIComponent(s)\n } catch {\n return s\n }\n }\n for (const part of raw.split('&')) {\n // Split on the FIRST '=' only so ids may legitimately contain '=' (e.g. base64 padding) (I-PL2).\n const eq = part.indexOf('=')\n if (eq < 0) continue\n const k = part.slice(0, eq)\n const v = part.slice(eq + 1)\n if (!v) continue\n if (k === 'g') out.g = dec(v)\n else if (k === 'i') out.i = dec(v)\n }\n return out\n}\n\n/** Format a group/item id pair into a hash body \"g=<id>&i=<id>\" (no leading #). */\nexport function formatHash(groupId: string, itemId?: string): string {\n const g = `g=${encodeURIComponent(groupId)}`\n return itemId !== undefined ? `${g}&i=${encodeURIComponent(itemId)}` : g\n}\n\nexport function hash(opts: HashOptions = {}): Plugin {\n return (viewer) => {\n if (typeof window === 'undefined') return { name: 'hash' } // SSR no-op\n\n // Track the last value we wrote and ignore matching hashchange events. Robust to the browser\n // coalescing / firing hashchange asynchronously (a boolean latch can get stuck and then swallow\n // a genuine back/forward); comparing the value can't (review T9).\n let lastWritten: string | null = null\n const offs: (() => void)[] = []\n\n const applyFromHash = (): boolean => {\n const { g, i } = parseHash(window.location.hash)\n if (!g) return false\n viewer.goToId(g, i)\n return true\n }\n\n const writeHash = () => {\n const col = viewer.getCollection()\n const cur = viewer.getState().cursor\n const group = col.groups[cur.g]\n if (!group) return\n const item = group.items[cur.i]\n lastWritten = formatHash(group.id, item?.id)\n // Replace, don't push: assigning location.hash stacks a new history entry on every nav,\n // trapping the Back button (press Back N times to leave). replaceState keeps the URL\n // shareable without polluting history (I-PL3). Falls back where history is restricted.\n try {\n window.history.replaceState(window.history.state, '', `#${lastWritten}`)\n } catch {\n window.location.hash = lastWritten\n }\n }\n\n const onHashChange = () => {\n if (window.location.hash.replace(/^#/, '') === lastWritten) return // our own write\n applyFromHash()\n }\n\n return {\n name: 'hash',\n init() {\n window.addEventListener('hashchange', onHashChange)\n offs.push(\n viewer.on('slide', writeHash),\n viewer.on('item', writeHash),\n viewer.on('opened', writeHash),\n )\n if (parseHash(window.location.hash).g) {\n applyFromHash()\n if (opts.open !== false) viewer.open()\n }\n },\n destroy() {\n window.removeEventListener('hashchange', onHashChange)\n for (const off of offs) off()\n },\n }\n }\n}\n"]}
@@ -0,0 +1,16 @@
1
+ import { Plugin } from '@gallerist/core';
2
+
3
+ interface HashOptions {
4
+ /** open the viewer on load if a hash is present (default true). */
5
+ open?: boolean;
6
+ }
7
+ /** Parse a location.hash like "#g=mon-2&i=s3" → { g, i } (values URL-decoded). */
8
+ declare function parseHash(hash: string): {
9
+ g?: string;
10
+ i?: string;
11
+ };
12
+ /** Format a group/item id pair into a hash body "g=<id>&i=<id>" (no leading #). */
13
+ declare function formatHash(groupId: string, itemId?: string): string;
14
+ declare function hash(opts?: HashOptions): Plugin;
15
+
16
+ export { type HashOptions, formatHash, hash, parseHash };
@@ -0,0 +1,16 @@
1
+ import { Plugin } from '@gallerist/core';
2
+
3
+ interface HashOptions {
4
+ /** open the viewer on load if a hash is present (default true). */
5
+ open?: boolean;
6
+ }
7
+ /** Parse a location.hash like "#g=mon-2&i=s3" → { g, i } (values URL-decoded). */
8
+ declare function parseHash(hash: string): {
9
+ g?: string;
10
+ i?: string;
11
+ };
12
+ /** Format a group/item id pair into a hash body "g=<id>&i=<id>" (no leading #). */
13
+ declare function formatHash(groupId: string, itemId?: string): string;
14
+ declare function hash(opts?: HashOptions): Plugin;
15
+
16
+ export { type HashOptions, formatHash, hash, parseHash };
package/dist/index.js ADDED
@@ -0,0 +1,79 @@
1
+ // src/index.ts
2
+ function parseHash(hash2) {
3
+ const out = {};
4
+ const raw = hash2.replace(/^#/, "");
5
+ const dec = (s) => {
6
+ try {
7
+ return decodeURIComponent(s);
8
+ } catch {
9
+ return s;
10
+ }
11
+ };
12
+ for (const part of raw.split("&")) {
13
+ const eq = part.indexOf("=");
14
+ if (eq < 0) continue;
15
+ const k = part.slice(0, eq);
16
+ const v = part.slice(eq + 1);
17
+ if (!v) continue;
18
+ if (k === "g") out.g = dec(v);
19
+ else if (k === "i") out.i = dec(v);
20
+ }
21
+ return out;
22
+ }
23
+ function formatHash(groupId, itemId) {
24
+ const g = `g=${encodeURIComponent(groupId)}`;
25
+ return itemId !== void 0 ? `${g}&i=${encodeURIComponent(itemId)}` : g;
26
+ }
27
+ function hash(opts = {}) {
28
+ return (viewer) => {
29
+ if (typeof window === "undefined") return { name: "hash" };
30
+ let lastWritten = null;
31
+ const offs = [];
32
+ const applyFromHash = () => {
33
+ const { g, i } = parseHash(window.location.hash);
34
+ if (!g) return false;
35
+ viewer.goToId(g, i);
36
+ return true;
37
+ };
38
+ const writeHash = () => {
39
+ const col = viewer.getCollection();
40
+ const cur = viewer.getState().cursor;
41
+ const group = col.groups[cur.g];
42
+ if (!group) return;
43
+ const item = group.items[cur.i];
44
+ lastWritten = formatHash(group.id, item?.id);
45
+ try {
46
+ window.history.replaceState(window.history.state, "", `#${lastWritten}`);
47
+ } catch {
48
+ window.location.hash = lastWritten;
49
+ }
50
+ };
51
+ const onHashChange = () => {
52
+ if (window.location.hash.replace(/^#/, "") === lastWritten) return;
53
+ applyFromHash();
54
+ };
55
+ return {
56
+ name: "hash",
57
+ init() {
58
+ window.addEventListener("hashchange", onHashChange);
59
+ offs.push(
60
+ viewer.on("slide", writeHash),
61
+ viewer.on("item", writeHash),
62
+ viewer.on("opened", writeHash)
63
+ );
64
+ if (parseHash(window.location.hash).g) {
65
+ applyFromHash();
66
+ if (opts.open !== false) viewer.open();
67
+ }
68
+ },
69
+ destroy() {
70
+ window.removeEventListener("hashchange", onHashChange);
71
+ for (const off of offs) off();
72
+ }
73
+ };
74
+ };
75
+ }
76
+
77
+ export { formatHash, hash, parseHash };
78
+ //# sourceMappingURL=index.js.map
79
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.ts"],"names":["hash"],"mappings":";AAWO,SAAS,UAAUA,KAAAA,EAA0C;AAClE,EAAA,MAAM,MAAkC,EAAC;AACzC,EAAA,MAAM,GAAA,GAAMA,KAAAA,CAAK,OAAA,CAAQ,IAAA,EAAM,EAAE,CAAA;AAGjC,EAAA,MAAM,GAAA,GAAM,CAAC,CAAA,KAAc;AACzB,IAAA,IAAI;AACF,MAAA,OAAO,mBAAmB,CAAC,CAAA;AAAA,IAC7B,CAAA,CAAA,MAAQ;AACN,MAAA,OAAO,CAAA;AAAA,IACT;AAAA,EACF,CAAA;AACA,EAAA,KAAA,MAAW,IAAA,IAAQ,GAAA,CAAI,KAAA,CAAM,GAAG,CAAA,EAAG;AAEjC,IAAA,MAAM,EAAA,GAAK,IAAA,CAAK,OAAA,CAAQ,GAAG,CAAA;AAC3B,IAAA,IAAI,KAAK,CAAA,EAAG;AACZ,IAAA,MAAM,CAAA,GAAI,IAAA,CAAK,KAAA,CAAM,CAAA,EAAG,EAAE,CAAA;AAC1B,IAAA,MAAM,CAAA,GAAI,IAAA,CAAK,KAAA,CAAM,EAAA,GAAK,CAAC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CAAA;AACrD,QAAA,KAAA,MAAW,GAAA,IAAO,MAAM,GAAA,EAAI;AAAA,MAC9B;AAAA,KACF;AAAA,EACF,CAAA;AACF","file":"index.js","sourcesContent":["// @gallerist/plugin-hash — deep-link the cursor to the URL hash `#g=<groupId>&i=<itemId>` (spec §14, §16).\n// Client-only (SSR-guarded); reflects nav into the URL and applies the URL on load + back/forward.\n\nimport type { Plugin } from '@gallerist/core'\n\nexport interface HashOptions {\n /** open the viewer on load if a hash is present (default true). */\n open?: boolean\n}\n\n/** Parse a location.hash like \"#g=mon-2&i=s3\" → { g, i } (values URL-decoded). */\nexport function parseHash(hash: string): { g?: string; i?: string } {\n const out: { g?: string; i?: string } = {}\n const raw = hash.replace(/^#/, '')\n // The hash is untrusted (any user can edit the address bar): tolerate a malformed percent\n // escape instead of throwing URIError (I-PL1).\n const dec = (s: string) => {\n try {\n return decodeURIComponent(s)\n } catch {\n return s\n }\n }\n for (const part of raw.split('&')) {\n // Split on the FIRST '=' only so ids may legitimately contain '=' (e.g. base64 padding) (I-PL2).\n const eq = part.indexOf('=')\n if (eq < 0) continue\n const k = part.slice(0, eq)\n const v = part.slice(eq + 1)\n if (!v) continue\n if (k === 'g') out.g = dec(v)\n else if (k === 'i') out.i = dec(v)\n }\n return out\n}\n\n/** Format a group/item id pair into a hash body \"g=<id>&i=<id>\" (no leading #). */\nexport function formatHash(groupId: string, itemId?: string): string {\n const g = `g=${encodeURIComponent(groupId)}`\n return itemId !== undefined ? `${g}&i=${encodeURIComponent(itemId)}` : g\n}\n\nexport function hash(opts: HashOptions = {}): Plugin {\n return (viewer) => {\n if (typeof window === 'undefined') return { name: 'hash' } // SSR no-op\n\n // Track the last value we wrote and ignore matching hashchange events. Robust to the browser\n // coalescing / firing hashchange asynchronously (a boolean latch can get stuck and then swallow\n // a genuine back/forward); comparing the value can't (review T9).\n let lastWritten: string | null = null\n const offs: (() => void)[] = []\n\n const applyFromHash = (): boolean => {\n const { g, i } = parseHash(window.location.hash)\n if (!g) return false\n viewer.goToId(g, i)\n return true\n }\n\n const writeHash = () => {\n const col = viewer.getCollection()\n const cur = viewer.getState().cursor\n const group = col.groups[cur.g]\n if (!group) return\n const item = group.items[cur.i]\n lastWritten = formatHash(group.id, item?.id)\n // Replace, don't push: assigning location.hash stacks a new history entry on every nav,\n // trapping the Back button (press Back N times to leave). replaceState keeps the URL\n // shareable without polluting history (I-PL3). Falls back where history is restricted.\n try {\n window.history.replaceState(window.history.state, '', `#${lastWritten}`)\n } catch {\n window.location.hash = lastWritten\n }\n }\n\n const onHashChange = () => {\n if (window.location.hash.replace(/^#/, '') === lastWritten) return // our own write\n applyFromHash()\n }\n\n return {\n name: 'hash',\n init() {\n window.addEventListener('hashchange', onHashChange)\n offs.push(\n viewer.on('slide', writeHash),\n viewer.on('item', writeHash),\n viewer.on('opened', writeHash),\n )\n if (parseHash(window.location.hash).g) {\n applyFromHash()\n if (opts.open !== false) viewer.open()\n }\n },\n destroy() {\n window.removeEventListener('hashchange', onHashChange)\n for (const off of offs) off()\n },\n }\n }\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,68 @@
1
1
  {
2
2
  "name": "@gallerist/plugin-hash",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "0.3.1",
4
+ "description": "Gallerist plugin — deep-link the cursor to the URL hash (#g=&i=), with back/forward.",
5
+ "license": "MIT",
6
+ "type": "module",
7
+ "sideEffects": false,
8
+ "files": [
9
+ "dist",
10
+ "LICENSE",
11
+ "README.md"
12
+ ],
13
+ "main": "./dist/index.cjs",
14
+ "module": "./dist/index.js",
15
+ "types": "./dist/index.d.ts",
16
+ "exports": {
17
+ ".": {
18
+ "import": {
19
+ "types": "./dist/index.d.ts",
20
+ "default": "./dist/index.js"
21
+ },
22
+ "require": {
23
+ "types": "./dist/index.d.cts",
24
+ "default": "./dist/index.cjs"
25
+ }
26
+ }
27
+ },
28
+ "peerDependencies": {
29
+ "@gallerist/core": ">=0.3.0 <1"
30
+ },
31
+ "devDependencies": {
32
+ "tsup": "^8.3.5",
33
+ "typescript": "^5.7.2",
34
+ "@gallerist/core": "0.3.1"
35
+ },
36
+ "repository": {
37
+ "type": "git",
38
+ "url": "git+https://github.com/shafayet9780/gallerist.git",
39
+ "directory": "packages/plugin-hash"
40
+ },
41
+ "homepage": "https://github.com/shafayet9780/gallerist#readme",
42
+ "bugs": {
43
+ "url": "https://github.com/shafayet9780/gallerist/issues"
44
+ },
45
+ "author": "Shafayet Ahmmed",
46
+ "keywords": [
47
+ "lightbox",
48
+ "gallery",
49
+ "image-viewer",
50
+ "media-viewer",
51
+ "photo-gallery",
52
+ "carousel",
53
+ "zoom",
54
+ "pan",
55
+ "overlays",
56
+ "headless",
57
+ "framework-agnostic",
58
+ "virtualized",
59
+ "nested-gallery"
60
+ ],
61
+ "publishConfig": {
62
+ "access": "public"
63
+ },
64
+ "scripts": {
65
+ "build": "tsup",
66
+ "typecheck": "tsc --noEmit"
67
+ }
6
68
  }