Skip to content

Commit

Permalink
refactor: remove duplicate code in functions
Browse files Browse the repository at this point in the history
  • Loading branch information
valmisson committed Mar 4, 2024
1 parent 79451ab commit 4be3a68
Showing 1 changed file with 61 additions and 91 deletions.
152 changes: 61 additions & 91 deletions src/functions/scrolling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,26 @@ import scrolling from '../shared/scrolling'
const globalOptions = getGlobalOptions()

/**
* Scroll to the top of the page
* Scroll to a position on the page
*
* @param position - The position
* @param options - An optional param with global options
*/

export const scrollTop = async (
export const scrollPosition = async (
position: Coord | number,
options?: Partial<GlobalOptions>
): Promise<void> => {
const opts = Object.assign(globalOptions, options)
const endPosition = { x: 0, y: 0 }
if (!position) {
throw new Error('scrollPosition require a position value valid')
}

if (typeof position === 'number') {
position = { x: 0, y: position }
}

const opts = Object.assign(globalOptions, options)
const endPosition = position
const { duration, offset, onStart, onDone } = opts

onStart && onStart({ offset, duration, endPosition })
Expand All @@ -29,156 +38,117 @@ export const scrollTop = async (
}

/**
* Scroll to the end of the page
* Scroll to element
*
* @param reference - The element reference
* @param options - An optional param with global options
*/

export const scrollBottom = async (
export const scrollElement = async (
reference: string,
options?: Partial<GlobalOptions>
): Promise<void> => {
const opts = Object.assign(globalOptions, options)
if (!reference || typeof reference !== 'string') {
throw new Error('scrollElement require a reference valid')
}

const { duration, offset, onStart, onDone } = opts
const opts = Object.assign(globalOptions, options)
const ref = sanitize(reference)

const body = document.body
const html = document.documentElement
const elementsList = get(elements)
const element = getElement(elementsList, ref)

const endPosition = {
x: 0,
y: Math.max(
body.scrollHeight,
body.offsetHeight,
html.scrollHeight,
html.clientHeight,
html.offsetHeight
)
if (!element) {
throw new Error(`Element reference '${ref}' not found`)
}

onStart && onStart({ offset, duration, endPosition })

await scrolling(endPosition, opts)
const endPosition = getPosition(element)

onDone && onDone({ offset, duration, endPosition })
await scrollPosition(endPosition, opts)
}

/**
* Scroll to the end of left the page
* Scroll to the top of the page
*
* @param options - An optional param with global options
*/

export const scrollLeft = async (
export const scrollTop = async (
options?: Partial<GlobalOptions>
): Promise<void> => {
const opts = Object.assign(globalOptions, options)
const endPosition = { x: 0, y: 0 }

const { duration, offset, onStart, onDone } = opts

onStart && onStart({ offset, duration, endPosition })

await scrolling(endPosition, opts)

onDone && onDone({ offset, duration, endPosition })
await scrollPosition(endPosition, opts)
}

/**
* Scroll to the end of right the page
* Scroll to the end of the page
*
* @param options - An optional param with global options
*/

export const scrollRight = async (
export const scrollBottom = async (
options?: Partial<GlobalOptions>
): Promise<void> => {
const opts = Object.assign(globalOptions, options)

const { duration, offset, onStart, onDone } = opts

const body = document.body
const html = document.documentElement

const endPosition = {
x: Math.max(
body.scrollWidth,
body.offsetWidth,
html.scrollWidth,
html.clientWidth,
html.offsetWidth
),
y: 0
x: 0,
y: Math.max(
body.scrollHeight,
body.offsetHeight,
html.scrollHeight,
html.clientHeight,
html.offsetHeight
)
}

onStart && onStart({ offset, duration, endPosition })

await scrolling(endPosition, opts)

onDone && onDone({ offset, duration, endPosition })
await scrollPosition(endPosition, opts)
}

/**
* Scroll to element
* Scroll to the end of left the page
*
* @param reference - The element reference
* @param options - An optional param with global options
*/

export const scrollElement = async (
reference: string,
export const scrollLeft = async (
options?: Partial<GlobalOptions>
): Promise<void> => {
if (!reference || typeof reference !== 'string') {
throw new Error('scrollElement require a reference valid')
}

const opts = Object.assign(globalOptions, options)
const ref = sanitize(reference)

const { duration, offset, onStart, onDone } = opts

const elementsList = get(elements)
const element = getElement(elementsList, ref)

if (!element) {
throw new Error(`Element reference '${ref}' not found`)
}

const endPosition = getPosition(element)

onStart && onStart({ element, offset, duration, endPosition })

await scrolling(endPosition, opts)
const endPosition = { x: 0, y: 0 }

onDone && onDone({ element, offset, duration, endPosition })
await scrollPosition(endPosition, opts)
}

/**
* Scroll to a position on the page
* Scroll to the end of right the page
*
* @param position - The position
* @param options - An optional param with global options
*/

export const scrollPosition = async (
position: Coord | number,
export const scrollRight = async (
options?: Partial<GlobalOptions>
): Promise<void> => {
if (!position) {
throw new Error('scrollPosition require a position value valid')
}

if (typeof position === 'number') {
position = { x: 0, y: position }
}

const opts = Object.assign(globalOptions, options)
const endPosition = position
const { duration, offset, onStart, onDone } = opts

onStart && onStart({ offset, duration, endPosition })
const body = document.body
const html = document.documentElement

await scrolling(endPosition, opts)
const endPosition = {
x: Math.max(
body.scrollWidth,
body.offsetWidth,
html.scrollWidth,
html.clientWidth,
html.offsetWidth
),
y: 0
}

onDone && onDone({ offset, duration, endPosition })
await scrollPosition(endPosition, opts)
}

0 comments on commit 4be3a68

Please sign in to comment.