From 44ce597c83d1f12f4b031c9a2e129ec8caffd182 Mon Sep 17 00:00:00 2001 From: Daniel Kurowski <daniel.kurowski@grifart.cz> Date: Mon, 7 Nov 2022 16:45:46 +0100 Subject: [PATCH] rename HashTarget to Hash --- src/{HashTarget.ts => Hash.ts} | 8 ++++---- .../linkClickScroll/initializeOnLinkClickScroll.ts | 4 ++-- src/handlers/loadScroll/initializeOnLoadScroll.ts | 10 +++++----- src/index.ts | 4 ++-- src/scrollers/scrollToTarget.ts | 14 +++++++------- 5 files changed, 20 insertions(+), 20 deletions(-) rename src/{HashTarget.ts => Hash.ts} (79%) diff --git a/src/HashTarget.ts b/src/Hash.ts similarity index 79% rename from src/HashTarget.ts rename to src/Hash.ts index b21ff12..89f0b47 100644 --- a/src/HashTarget.ts +++ b/src/Hash.ts @@ -1,13 +1,13 @@ /** * Represents valid hash as per https://developer.mozilla.org/en-US/docs/Web/API/HTMLHyperlinkElementUtils/hash */ -export class HashTarget +export class Hash { private constructor( private readonly value: string, ) {} - public static fromString(value: string): HashTarget + public static fromString(value: string): Hash { if (value === '' || value === '#') { throw new Error('Hash does not contain any fragment.'); @@ -16,12 +16,12 @@ export class HashTarget return new this(value); } - public getHash(): string + public getValue(): string { return this.value; } - public getRefElement(document: HTMLDocument): HTMLElement + public findTargetElementIn(document: HTMLDocument): HTMLElement { const targetElementId = this.value.substring(1); const targetElement = document.getElementById(targetElementId); diff --git a/src/handlers/linkClickScroll/initializeOnLinkClickScroll.ts b/src/handlers/linkClickScroll/initializeOnLinkClickScroll.ts index 31806d4..441d33f 100644 --- a/src/handlers/linkClickScroll/initializeOnLinkClickScroll.ts +++ b/src/handlers/linkClickScroll/initializeOnLinkClickScroll.ts @@ -1,6 +1,6 @@ import {scrollToTarget} from '../../scrollers/scrollToTarget'; import {assert} from '../../assert'; -import {HashTarget} from '../../HashTarget'; +import {Hash} from '../../Hash'; export function initializeOnLinkClickScroll(): void { @@ -15,6 +15,6 @@ export function initializeOnLinkClickScroll(): void } event.preventDefault(); - scrollToTarget(HashTarget.fromString(element.hash)); + scrollToTarget(Hash.fromString(element.hash)); }))); } diff --git a/src/handlers/loadScroll/initializeOnLoadScroll.ts b/src/handlers/loadScroll/initializeOnLoadScroll.ts index b4a42ce..844098f 100644 --- a/src/handlers/loadScroll/initializeOnLoadScroll.ts +++ b/src/handlers/loadScroll/initializeOnLoadScroll.ts @@ -1,5 +1,5 @@ import {scrollToTarget} from '../../scrollers/scrollToTarget'; -import {HashTarget} from '../../HashTarget'; +import {Hash} from '../../Hash'; import {assert} from '../../assert'; /** @@ -17,12 +17,12 @@ export function initializeOnLoadScroll(): void return; } - let hashTarget: HashTarget|null = null; + let targetHash: Hash|null = null; const start = performance.now(); document.addEventListener('DOMContentLoaded', () => { try { - hashTarget = HashTarget.fromString(hash); + targetHash = Hash.fromString(hash); } catch (e) {} // when URL contains hash which has no corresponding DOM element, just ignore it }); @@ -31,7 +31,7 @@ export function initializeOnLoadScroll(): void * all styles are loaded and offsets are computed correctly - so the scroll will be computed correctly. */ window.addEventListener('load', () => { - if (hashTarget === null) { // if target does not exist + if (targetHash === null) { // if hash does not exist return; } @@ -46,6 +46,6 @@ export function initializeOnLoadScroll(): void window.scroll({top: 0, left: 0}); // Then, scroll down to it smoothly. - scrollToTarget(hashTarget); + scrollToTarget(targetHash); }); } diff --git a/src/index.ts b/src/index.ts index 2c03375..18e51ce 100644 --- a/src/index.ts +++ b/src/index.ts @@ -5,7 +5,7 @@ import {initializeOnLinkClickScroll} from './handlers/linkClickScroll/initialize import {scrollToElement} from './scrollers/scrollToElement'; import {scrollToOffset} from './scrollers/scrollToOffset'; import {scrollToTarget} from './scrollers/scrollToTarget'; -import {HashTarget} from './HashTarget'; +import {Hash} from './Hash'; // bind automatically on library import @@ -23,7 +23,7 @@ function handleOnLinkClickScroll(): void } export { - HashTarget, + Hash, handleOnLoadScroll, handleOnLinkClickScroll, scrollToElement, diff --git a/src/scrollers/scrollToTarget.ts b/src/scrollers/scrollToTarget.ts index 5906c2b..ea5882d 100644 --- a/src/scrollers/scrollToTarget.ts +++ b/src/scrollers/scrollToTarget.ts @@ -1,18 +1,18 @@ -import {HashTarget} from '../HashTarget'; +import {Hash} from '../Hash'; import {scrollToElement} from './scrollToElement'; import {assert} from '../assert'; -export function scrollToTarget(hashTarget: HashTarget|string, onScrollFinishedCallback?: () => void): void +export function scrollToTarget(targetHash: Hash|string, onScrollFinishedCallback?: () => void): void { - if (typeof hashTarget === 'string') { - hashTarget = HashTarget.fromString(hashTarget); + if (typeof targetHash === 'string') { + targetHash = Hash.fromString(targetHash); } scrollToElement( - hashTarget.getRefElement(document), + targetHash.findTargetElementIn(document), () => { - assert(hashTarget instanceof HashTarget); - window.location.hash = hashTarget.getHash(); + assert(targetHash instanceof Hash); + window.location.hash = targetHash.getValue(); onScrollFinishedCallback !== undefined && onScrollFinishedCallback(); }, ); -- GitLab