From 2e1bbdc93cbd6aef30df693654060a8cfd40aa90 Mon Sep 17 00:00:00 2001 From: Okiki Date: Fri, 24 Sep 2021 06:53:48 +0000 Subject: [PATCH] feat: push updates --- docs/animate/index.md | 2 +- packages/animate/src/custom-easing.ts | 389 +++++++++++++++----------- src/markdown-plugins.js | 81 ++++-- src/pages/[...page].astro | 1 + 4 files changed, 286 insertions(+), 187 deletions(-) diff --git a/docs/animate/index.md b/docs/animate/index.md index b5c7ad7..b4577aa 100644 --- a/docs/animate/index.md +++ b/docs/animate/index.md @@ -211,4 +211,4 @@ Read through the [contributing documentation](/docs/animate/contributing.md) for ## Licence -See the [LICENSE](/LICENSE) file for license rights and limitations (MIT). \ No newline at end of file +See the [LICENSE](/LICENSE) file for license rights and limitations (MIT). diff --git a/packages/animate/src/custom-easing.ts b/packages/animate/src/custom-easing.ts index 139aa42..1496953 100644 --- a/packages/animate/src/custom-easing.ts +++ b/packages/animate/src/custom-easing.ts @@ -1,30 +1,46 @@ -import { isValid, transpose, toStr, convertToDash, mapObject, getUnit, trim } from "./utils"; +import { + isValid, + transpose, + toStr, + convertToDash, + mapObject, + getUnit, + trim, +} from "./utils"; import { toRGBAArr } from "./unit-conversion"; import bezier from "./bezier-easing"; -export const limit = (value: number, min: number, max: number) => Math.min(Math.max(value, min), max); +export const limit = (value: number, min: number, max: number) => + Math.min(Math.max(value, min), max); /** * The format to use when defining custom easing functions */ -export type TypeEasingFunction = (t: number, params?: (string | number)[], duration?: number) => number; +export type TypeEasingFunction = ( + t: number, + params?: (string | number)[], + duration?: number +) => number; /** Easing Functions from anime.js, they are tried and true, so, its better to use them instead of other alternatives */ -export const Quad: TypeEasingFunction = t => Math.pow(t, 2); -export const Cubic: TypeEasingFunction = t => Math.pow(t, 3); -export const Quart: TypeEasingFunction = t => Math.pow(t, 4); -export const Quint: TypeEasingFunction = t => Math.pow(t, 5); -export const Expo: TypeEasingFunction = t => Math.pow(t, 6); -export const Sine: TypeEasingFunction = t => 1 - Math.cos((t * Math.PI) / 2); -export const Circ: TypeEasingFunction = t => 1 - Math.sqrt(1 - t * t); -export const Back: TypeEasingFunction = t => t * t * (3 * t - 2); - -export const Bounce: TypeEasingFunction = t => { - let pow2: number, b = 4; - while (t < ((pow2 = Math.pow(2, --b)) - 1) / 11) { } - return 1 / Math.pow(4, 3 - b) - 7.5625 * Math.pow((pow2 * 3 - 2) / 22 - t, 2); +export const Quad: TypeEasingFunction = (t) => Math.pow(t, 2); +export const Cubic: TypeEasingFunction = (t) => Math.pow(t, 3); +export const Quart: TypeEasingFunction = (t) => Math.pow(t, 4); +export const Quint: TypeEasingFunction = (t) => Math.pow(t, 5); +export const Expo: TypeEasingFunction = (t) => Math.pow(t, 6); +export const Sine: TypeEasingFunction = (t) => 1 - Math.cos((t * Math.PI) / 2); +export const Circ: TypeEasingFunction = (t) => 1 - Math.sqrt(1 - t * t); +export const Back: TypeEasingFunction = (t) => t * t * (3 * t - 2); + +export const Bounce: TypeEasingFunction = (t) => { + let pow2: number, + b = 4; + while (t < ((pow2 = Math.pow(2, --b)) - 1) / 11) {} + return ( + 1 / Math.pow(4, 3 - b) - 7.5625 * Math.pow((pow2 * 3 - 2) / 22 - t, 2) + ); }; export const Elastic: TypeEasingFunction = (t, params: number[] = []) => { @@ -32,11 +48,14 @@ export const Elastic: TypeEasingFunction = (t, params: number[] = []) => { const a = limit(amplitude, 1, 10); const p = limit(period, 0.1, 2); if (t === 0 || t === 1) return t; - return -a * + return ( + -a * Math.pow(2, 10 * (t - 1)) * Math.sin( - ((t - 1 - (p / (Math.PI * 2)) * Math.asin(1 / a)) * (Math.PI * 2)) / p - ); + ((t - 1 - (p / (Math.PI * 2)) * Math.asin(1 / a)) * (Math.PI * 2)) / + p + ) + ); }; export const Spring: TypeEasingFunction = ( @@ -44,12 +63,7 @@ export const Spring: TypeEasingFunction = ( params: number[] = [], duration?: number ) => { - let [ - mass = 1, - stiffness = 100, - damping = 10, - velocity = 0 - ] = params; + let [mass = 1, stiffness = 100, damping = 10, velocity = 0] = params; mass = limit(mass, 0.1, 1000); stiffness = limit(stiffness, 0.1, 1000); @@ -78,7 +92,10 @@ export const Spring: TypeEasingFunction = ( /** * Cache the durations at set easing parameters */ -export const EasingDurationCache: Map = new Map(); +export const EasingDurationCache: Map< + string | TypeEasingFunction, + number +> = new Map(); /** * The threshold for an infinite loop @@ -86,19 +103,24 @@ export const EasingDurationCache: Map = new export const INTINITE_LOOP_LIMIT = 10000; /** - * The spring easing function will only look smooth at certain durations, with certain parameters. - * This functions returns the optimal duration to create a smooth springy animation based on physics - * + * The spring easing function will only look smooth at certain durations, with certain parameters. + * This functions returns the optimal duration to create a smooth springy animation based on physics + * * Note: it can also be used to determine the optimal duration of other types of easing function, but be careful of "in-" * easing functions, because of the nature of the function it can sometimes create an infinite loop, I suggest only using * `getEasingDuration` for `spring`, specifically "out-spring" and "spring" */ -export const getEasingDuration = (easing: string | TypeEasingFunction = "spring") => { - if (EasingDurationCache.has(easing)) - return EasingDurationCache.get(easing); - - const easingFunction = typeof easing == "function" ? easing : GetEasingFunction(easing as string); - const params = typeof easing == "function" ? [] : parseEasingParameters(easing); +export const getEasingDuration = ( + easing: string | TypeEasingFunction = "spring" +) => { + if (EasingDurationCache.has(easing)) return EasingDurationCache.get(easing); + + const easingFunction = + typeof easing == "function" + ? easing + : GetEasingFunction(easing as string); + const params = + typeof easing == "function" ? [] : parseEasingParameters(easing); const frame = 1 / 6; let elapsed = 0; @@ -118,7 +140,7 @@ export const getEasingDuration = (easing: string | TypeEasingFunction = "spring" const duration = elapsed * frame * 1000; EasingDurationCache.set(easing, duration); return duration; -} +}; /** These Easing Functions are based off of the Sozi Project's easing functions @@ -130,7 +152,10 @@ export const Steps: TypeEasingFunction = (t: number, params = []) => { return trunc(limit(t, 0, 1) * steps) / steps; }; -export const Bezier: TypeEasingFunction = (t: number, params: number[] = []) => { +export const Bezier: TypeEasingFunction = ( + t: number, + params: number[] = [] +) => { let [mX1, mY1, mX2, mY2] = params; return bezier(mX1, mY1, mX2, mY2)(t); }; @@ -140,14 +165,17 @@ export const easein: TypeEasingFunction = bezier(0.42, 0.0, 1.0, 1.0); /** Converts easing functions to their `out`counter parts */ export const EaseOut = (ease: TypeEasingFunction): TypeEasingFunction => { - return (t, params = [], duration?: number) => 1 - ease(1 - t, params, duration); -} + return (t, params = [], duration?: number) => + 1 - ease(1 - t, params, duration); +}; /** Converts easing functions to their `in-out` counter parts */ export const EaseInOut = (ease: TypeEasingFunction): TypeEasingFunction => { return (t, params = [], duration?: number) => - t < 0.5 ? ease(t * 2, params, duration) / 2 : 1 - ease(t * -2 + 2, params, duration) / 2; -} + t < 0.5 + ? ease(t * 2, params, duration) / 2 + : 1 - ease(t * -2 + 2, params, duration) / 2; +}; /** Converts easing functions to their `out-in` counter parts */ export const EaseOutIn = (ease: TypeEasingFunction): TypeEasingFunction => { @@ -156,19 +184,19 @@ export const EaseOutIn = (ease: TypeEasingFunction): TypeEasingFunction => { ? (1 - ease(1 - t * 2, params, duration)) / 2 : (ease(t * 2 - 1, params, duration) + 1) / 2; }; -} +}; /** * The default list of easing functions, do note this is different from {@link EASING} */ export const EasingFunctions: { [key: string]: TypeEasingFunction } = { steps: Steps, - "step-start": t => Steps(t, [1, "start"]), - "step-end": t => Steps(t, [1, "end"]), + "step-start": (t) => Steps(t, [1, "start"]), + "step-end": (t) => Steps(t, [1, "end"]), - linear: t => t, + linear: (t) => t, "cubic-bezier": Bezier, - ease: t => Bezier(t, [0.25, 0.1, 0.25, 1.0]), + ease: (t) => Bezier(t, [0.25, 0.1, 0.25, 1.0]), in: easein, out: EaseOut(easein), @@ -229,32 +257,34 @@ export const EasingFunctions: { [key: string]: TypeEasingFunction } = { "spring-in": Spring, "spring-out": EaseOut(Spring), "spring-in-out": EaseInOut(Spring), - "spring-out-in": EaseOutIn(Spring) + "spring-out-in": EaseOutIn(Spring), }; export let EasingFunctionKeys = Object.keys(EasingFunctions); /** - * Allows you to register new easing functions + * Allows you to register new easing functions */ export const registerEasingFunction = (key: string, fn: TypeEasingFunction) => { Object.assign(EasingFunctions, { - [key]: fn + [key]: fn, }); EasingFunctionKeys = Object.keys(EasingFunctions); -} +}; /** - * Allows you to register multiple new easing functions + * Allows you to register multiple new easing functions */ -export const registerEasingFunctions = (...obj: Array) => { +export const registerEasingFunctions = ( + ...obj: Array +) => { Object.assign(EasingFunctions, ...obj); EasingFunctionKeys = Object.keys(EasingFunctions); -} +}; /** - * Convert string easing to their proper form + * Convert string easing to their proper form */ export const ComplexEasingSyntax = (ease: string) => convertToDash(ease) @@ -267,22 +297,24 @@ export const ComplexEasingSyntax = (ease: string) => export const GetEasingFunction = (ease: string) => { let search = ComplexEasingSyntax(toStr(ease)); - if (EasingFunctionKeys.includes(search)) - return EasingFunctions[search]; + if (EasingFunctionKeys.includes(search)) return EasingFunctions[search]; return null; }; /** Convert easing parameters to Array of numbers, e.g. "spring(2, 500)" to [2, 500] */ export const parseEasingParameters = (str: string) => { const match = /(\(|\s)([^)]+)\)?/.exec(toStr(str)); - return match ? match[2].split(",").map(value => { - let num = parseFloat(value); - return !Number.isNaN(num) ? num : value.trim(); - }) : []; + return match + ? match[2].split(",").map((value) => { + let num = parseFloat(value); + return !Number.isNaN(num) ? num : value.trim(); + }) + : []; }; /** map `t` from 0 to 1, to `start` to `end` */ -export const scale = (t: number, start: number, end: number) => start + (end - start) * t; +export const scale = (t: number, start: number, end: number) => + start + (end - start) * t; /** Rounds numbers to a fixed decimal place */ export const toFixed = (value: number, decimal: number) => @@ -326,7 +358,10 @@ export const isNumberLike = (num: string | number) => { Given an Array of values, find a value using `t` (`t` goes from 0 to 1), by using `t` to estimate the index of said value in the array of `values` */ -export const interpolateUsingIndex = (t: number, values: (string | number)[]) => { +export const interpolateUsingIndex = ( + t: number, + values: (string | number)[] +) => { // limit `t`, to a min of 0 and a max of 1 t = limit(t, 0, 1); @@ -346,14 +381,23 @@ export const interpolateUsingIndex = (t: number, values: (string | number)[]) => Make sure to read {@link interpolateNumber}. */ -export const interpolateString = (t: number, values: (string | number)[], decimal = 3) => { +export const interpolateString = ( + t: number, + values: (string | number)[], + decimal = 3 +) => { let units = ""; // If the first value looks like a number with a unit - if (isNumberLike(values[0])) - units = getUnit(values[0]); - - return interpolateNumber(t, values.map(v => typeof v == "number" ? v : parseFloat(v)), decimal) + units; + if (isNumberLike(values[0])) units = getUnit(values[0]); + + return ( + interpolateNumber( + t, + values.map((v) => (typeof v == "number" ? v : parseFloat(v))), + decimal + ) + units + ); }; /** @@ -366,10 +410,12 @@ export const interpolateString = (t: number, values: (string | number)[], decima Make sure to read {@link interpolateNumber}. */ export const interpolateColor = (t: number, values: string[], decimal = 3) => { - return transpose(...values.map(v => toRGBAArr(v))).map((colors: number[], i) => { - let result = interpolateNumber(t, colors); - return i < 3 ? Math.round(result) : toFixed(result, decimal); - }); + return transpose(...values.map((v) => toRGBAArr(v))).map( + (colors: number[], i) => { + let result = interpolateNumber(t, colors); + return i < 3 ? Math.round(result) : toFixed(result, decimal); + } + ); }; /** @@ -394,22 +440,27 @@ export const ComplexStrtoArr = (str: string) => { Make sure to read {@link interpolateNumber}, {@link interpolateString}, {@link interpolateColor}, and {@link interpolateUsingIndex}. */ -export const interpolateComplex = (t: number, values: (string | number)[], decimal = 3) => { +export const interpolateComplex = ( + t: number, + values: (string | number)[], + decimal = 3 +) => { // Interpolate numbers - let isNumber = values.every(v => typeof v == "number"); - if (isNumber) - return interpolateNumber(t, values as number[], decimal); + let isNumber = values.every((v) => typeof v == "number"); + if (isNumber) return interpolateNumber(t, values as number[], decimal); // Interpolate colors - let isColor = values.every(v => CSS.supports("color", toStr(v))); + let isColor = values.every((v) => CSS.supports("color", toStr(v))); if (isColor) return `rgba(${interpolateColor(t, values as string[], decimal)})`; // Interpolate complex values and strings - let isString = values.some(v => typeof v == "string"); + let isString = values.some((v) => typeof v == "string"); if (isString) { // Interpolate complex values like "10px solid red" - let isComplex = values.some(v => /(\d|\)|\w)\s/.test(trim(v as string))); + let isComplex = values.some((v) => + /(\d|\)|\w)\s/.test(trim(v as string)) + ); if (isComplex) { return transpose(...values.map(ComplexStrtoArr)) .map((value) => interpolateComplex(t, value, decimal)) @@ -417,10 +468,9 @@ export const interpolateComplex = (t: number, values: (string | number)[], decim } // Interpolate strings with numbers, e.g. "5px" - let isLikeNumber = values.every(v => isNumberLike(v as string)); + let isLikeNumber = values.every((v) => isNumberLike(v as string)); if (isLikeNumber) return interpolateString(t, values as (number | string)[], decimal); - // Interpolate pure strings, e.g. "inherit", "solid", etc... else return interpolateUsingIndex(t, values as string[]); } @@ -428,7 +478,7 @@ export const interpolateComplex = (t: number, values: (string | number)[], decim /** * Custom Easing has 3 properties they are `easing` (all the easings from [#easing](#easing) are supported on top of custom easing functions, like spring, bounce, etc...), `numPoints` (the size of the Array the Custom Easing function should create), and `decimal` (the number of decimal places of the values within said Array). - * + * * | Properties | Default Value | * | ----------- | ----------------------- | * | `easing` | `spring(1, 100, 10, 0)` | @@ -436,10 +486,10 @@ export const interpolateComplex = (t: number, values: (string | number)[], decim * | `decimal` | `3` | */ export type TypeCustomEasingOptions = { - /** - * + /** + * * By default, Custom Easing support easing functions, in the form, - * + * * | constant | accelerate | decelerate | accelerate-decelerate | decelerate-accelerate | * | :--------- | :----------------- | :------------- | :-------------------- | :-------------------- | * | linear | ease-in / in | ease-out / out | ease-in-out / in-out | ease-out-in / out-in | @@ -454,59 +504,61 @@ export type TypeCustomEasingOptions = { * | | in-bounce | out-bounce | in-out-bounce | out-in-bounce | * | | in-elastic | out-elastic | in-out-elastic | out-in-elastic | * | | spring / spring-in | spring-out | spring-in-out | spring-out-in | - * + * * All **Elastic** easing's can be configured using theses parameters, - * + * * `*-elastic(amplitude, period)` - * + * * Each parameter comes with these defaults - * + * * | Parameter | Default Value | * | --------- | ------------- | * | amplitude | `1` | * | period | `0.5` | - * + * * *** - * + * * All **Spring** easing's can be configured using theses parameters, - * + * * `spring-*(mass, stiffness, damping, velocity)` - * + * * Each parameter comes with these defaults - * + * * | Parameter | Default Value | * | --------- | ------------- | * | mass | `1` | * | stiffness | `100` | * | damping | `10` | * | velocity | `0` | - * + * * You can create your own custom cubic-bezier easing curves. Similar to css you type `cubic-bezier(...)` with 4 numbers representing the shape of the bezier curve, for example, `cubic-bezier(0.47, 0, 0.745, 0.715)` this is the bezier curve for `in-sine`. - * + * * _**Note**: the `easing` property supports the original values and functions for easing as well, for example, `steps(1)`, and etc... are supported._ - * + * * _**Note**: you can also use camelCase when defining easing functions, e.g. `inOutCubic` to represent `in-out-cubic`_ - * - */ - easing?: string | TypeEasingFunction, - numPoints?: number, - decimal?: number, - duration?: number + * + */ + easing?: string | TypeEasingFunction; + numPoints?: number; + decimal?: number; + duration?: number; }; /** * returns a CustomEasingOptions object from a easing "string", or function */ -export const CustomEasingOptions = (options: TypeCustomEasingOptions | string | TypeEasingFunction = {}) => { +export const CustomEasingOptions = ( + options: TypeCustomEasingOptions | string | TypeEasingFunction = {} +) => { let isEasing = typeof options == "string" || typeof options == "function"; let { easing = "spring(1, 100, 10, 0)", numPoints = 100, decimal = 3, - duration + duration, } = (isEasing ? { easing: options } : options) as TypeCustomEasingOptions; return { easing, numPoints, decimal, duration }; -} +}; /** Cache calculated tween points for commonly used easing functions @@ -527,13 +579,21 @@ export const TweenCache = new Map(); Based on https://github.com/w3c/csswg-drafts/issues/229#issuecomment-861415901 */ -export const EasingPts = ({ easing, numPoints, duration }: TypeCustomEasingOptions = {}): number[] => { +export const EasingPts = ({ + easing, + numPoints, + duration, +}: TypeCustomEasingOptions = {}): number[] => { const pts = []; const key = `${easing}${numPoints}`; if (TweenCache.has(key)) return TweenCache.get(key); - const easingFunction = typeof easing == "function" ? easing : GetEasingFunction(easing as string); - const params = typeof easing == "function" ? [] : parseEasingParameters(easing); + const easingFunction = + typeof easing == "function" + ? easing + : GetEasingFunction(easing as string); + const params = + typeof easing == "function" ? [] : parseEasingParameters(easing); for (let i = 0; i < numPoints; i++) { pts[i] = easingFunction(i / (numPoints - 1), params, duration); @@ -553,23 +613,23 @@ const updateDuration = (optionsObj: TypeCustomEasingOptions = {}) => { optionsObj.duration = getEasingDuration(optionsObj.easing); } } -} +}; -/** +/** * Generates an Array of values using easing functions which in turn create the effect of custom easing. * To use this properly make sure to set the easing animation option to "linear". * Check out a demo of CustomEasing at - * + * * Custom Easing has 3 properties they are `easing` (all the easings from {@link EasingFunctions} are supported on top of custom easing functions like spring, bounce, etc...), `numPoints` (the size of the Array the Custom Easing function should create), and `decimal` (the number of decimal places of the values within said Array). - * + * * | Properties | Default Value | * | ----------- | ----------------------- | * | `easing` | `spring(1, 100, 10, 0)` | * | `numPoints` | `50` | * | `decimal` | `3` | - * + * * By default, Custom Easing support easing functions, in the form, - * + * * | constant | accelerate | decelerate | accelerate-decelerate | decelerate-accelerate | * | :--------- | :----------------- | :------------- | :-------------------- | :-------------------- | * | linear | ease-in / in | ease-out / out | ease-in-out / in-out | ease-out-in / out-in | @@ -584,47 +644,47 @@ const updateDuration = (optionsObj: TypeCustomEasingOptions = {}) => { * | | in-bounce | out-bounce | in-out-bounce | out-in-bounce | * | | in-elastic | out-elastic | in-out-elastic | out-in-elastic | * | | spring / spring-in | spring-out | spring-in-out | spring-out-in | - * + * * All **Elastic** easing's can be configured using theses parameters, - * + * * `*-elastic(amplitude, period)` - * + * * Each parameter comes with these defaults - * + * * | Parameter | Default Value | * | --------- | ------------- | * | amplitude | `1` | * | period | `0.5` | - * + * * *** - * + * * All **Spring** easing's can be configured using theses parameters, - * + * * `spring-*(mass, stiffness, damping, velocity)` - * + * * Each parameter comes with these defaults - * + * * | Parameter | Default Value | * | --------- | ------------- | * | mass | `1` | * | stiffness | `100` | * | damping | `10` | * | velocity | `0` | - * + * * You can create your own custom cubic-bezier easing curves. Similar to css you type `cubic-bezier(...)` with 4 numbers representing the shape of the bezier curve, for example, `cubic-bezier(0.47, 0, 0.745, 0.715)` this is the bezier curve for `in-sine`. - * + * * _**Note**: the `easing` property supports the original values and functions for easing as well, for example, `steps(1)`, and etc... are supported._ - * + * * _**Note**: you can also use camelCase when defining easing functions, e.g. `inOutCubic` to represent `in-out-cubic`_ - * + * * _**Suggestion**: if you decide to use CustomEasing on one CSS property, I suggest using CustomEasing or {@link ApplyCustomEasing} on the rest (this is no longer necessary, but for the sake of readability it's better to do)_ * - * e.g. + * e.g. * ```ts - * import { animate, CustomEasing, EaseOut, Quad } from "@okikio/animate"; + * import { animate, CustomEasing, EaseOut, Quad } from "@okikio/animate"; * animate({ * target: "div", - * + * * // Notice how only, the first value in the Array uses the "px" unit * border: CustomEasing(["1px solid red", "3 dashed green", "2 solid black"], { * // This is a custom easing function @@ -634,7 +694,7 @@ const updateDuration = (optionsObj: TypeCustomEasingOptions = {}) => { * translateX: CustomEasing([0, 250], { * easing: "linear", * - * // You can change the size of Array for the CustomEasing function to generate + * // You can change the size of Array for the CustomEasing function to generate * numPoints: 200, * * // The number of decimal places to round, final values in the generated Array @@ -642,10 +702,10 @@ const updateDuration = (optionsObj: TypeCustomEasingOptions = {}) => { * }), * * // You can set the easing without an object - * // Also, if units are detected in the values Array, + * // Also, if units are detected in the values Array, * // the unit of the first value in the values Array are * // applied to other values in the values Array, even if they - * // have prior units + * // have prior units * rotate: CustomEasing(["0turn", 1, 0, 0.5], "out"), * "background-color": CustomEasing(["#616aff", "white"]), * easing: "linear" @@ -655,8 +715,11 @@ const updateDuration = (optionsObj: TypeCustomEasingOptions = {}) => { * easing: "linear" * }) * ``` -*/ -export const CustomEasing = (values: (string | number)[], options: TypeCustomEasingOptions | string | TypeEasingFunction = {}) => { + */ +export const CustomEasing = ( + values: (string | number)[], + options: TypeCustomEasingOptions | string | TypeEasingFunction = {} +) => { let optionsObj = CustomEasingOptions(options); updateDuration(optionsObj); @@ -668,26 +731,26 @@ export const CustomEasing = (values: (string | number)[], options: TypeCustomEas /** * Returns an array containing `[easing pts, duration]`, it's meant to be a self enclosed way to create spring easing. * Springs have an optimal duration; using `getEasingDuration()` we are able to have the determine the optimal duration for a spring with given parameters. - * + * * By default it will only give the optimal duration for `spring` or `spring-in` easing, this is to avoid infinite loops caused by the `getEasingDuration()` function. - * + * * Internally the `SpringEasing` uses {@link CustomEasing}, read more on it, to understand how the `SpringEasing` function works. - * + * * e.g. * ```ts * import { animate, SpringEasing } from "@okikio/animate"; - * + * * // `duration` is the optimal duration for the spring with the set parameters * let [translateX, duration] = SpringEasing([0, 250], "spring(5, 100, 10, 1)"); * // or * // `duration` is 5000 here - * let [translateX, duration] = SpringEasing([0, 250], { + * let [translateX, duration] = SpringEasing([0, 250], { * easing: "spring(5, 100, 10, 1)", * numPoints: 50, * duration: 5000, * decimal: 3 * }); - * + * * animate({ * target: "div", * translateX, @@ -695,7 +758,10 @@ export const CustomEasing = (values: (string | number)[], options: TypeCustomEas * }); * ``` */ -export const SpringEasing = (values: (string | number)[], options: TypeCustomEasingOptions = {}) => { +export const SpringEasing = ( + values: (string | number)[], + options: TypeCustomEasingOptions = {} +) => { let optionsObj = CustomEasingOptions(options); let { duration } = optionsObj; @@ -703,39 +769,39 @@ export const SpringEasing = (values: (string | number)[], options: TypeCustomEas return [ CustomEasing(values, optionsObj), - isValid(duration) ? duration : optionsObj.duration - ] + isValid(duration) ? duration : optionsObj.duration, + ]; }; /** * Applies the same custom easings to all properties of the object and returns an object with each property having an array of custom eased values - * - * If you use the `spring` or `spring-in` easings it will also return the optimal duration as a key in the object it returns. + * + * If you use the `spring` or `spring-in` easings it will also return the optimal duration as a key in the object it returns. * If you set `duration` to a number, it will prioritize that `duration` over optimal duration given by the spring easings. - * + * * Read more about {@link CustomEasing} - * + * * e.g. * ```ts * import { animate, ApplyCustomEasing } from "@okikio/animate"; * animate({ * target: "div", - * + * * ...ApplyCustomEasing({ * border: ["1px solid red", "3 dashed green", "2 solid black"], * translateX: [0, 250], * rotate: ["0turn", 1, 0, 0.5], * "background-color": ["#616aff", "white"], - * + * * // You don't need to enter any parameters, you can just use the default values * easing: "spring", * - * // You can change the size of Array for the CustomEasing function to generate + * // You can change the size of Array for the CustomEasing function to generate * numPoints: 200, * * // The number of decimal places to round, final values in the generated Array * decimal: 5, - * + * * // You can also set the duration from here. * // When using spring animations, the duration you set here is not nesscary, * // since by default springs will try to determine the most appropriate duration for the spring animation. @@ -745,16 +811,27 @@ export const SpringEasing = (values: (string | number)[], options: TypeCustomEas * }) * ``` */ -export const ApplyCustomEasing = (options: TypeCustomEasingOptions & { [key: string]: number | string | TypeEasingFunction | (number | string)[] } = {}) => { +export const ApplyCustomEasing = ( + options: TypeCustomEasingOptions & { + [key: string]: + | number + | string + | TypeEasingFunction + | (number | string)[]; + } = {} +) => { let { easing, numPoints, decimal, duration, ...rest } = options; let optionsObj = { easing, numPoints, decimal, duration }; updateDuration(optionsObj); - let properties = mapObject(rest, (value: (string | number)[]) => CustomEasing(value, optionsObj)); + let properties = mapObject(rest, (value: (string | number)[]) => + CustomEasing(value, optionsObj) + ); let durationObj = {}; if (isValid(duration)) durationObj = { duration }; - else if (isValid(optionsObj.duration)) durationObj = { duration: optionsObj.duration }; + else if (isValid(optionsObj.duration)) + durationObj = { duration: optionsObj.duration }; return Object.assign({}, properties, durationObj); -} \ No newline at end of file +}; diff --git a/src/markdown-plugins.js b/src/markdown-plugins.js index 2f51a5c..a8cf7ce 100644 --- a/src/markdown-plugins.js +++ b/src/markdown-plugins.js @@ -1,46 +1,67 @@ import { h } from "hastscript"; +import fs from "fs"; +import path from "path"; export default { - remarkPlugins: ['remark-code-titles', 'remark-gfm', 'remark-footnotes', '@silvenon/remark-smartypants'], + remarkPlugins: [ + "remark-code-titles", + "remark-gfm", + "remark-footnotes", + "@silvenon/remark-smartypants", + ], rehypePlugins: [ - ['rehype-slug'], - ['rehype-wrap', {wrapper: 'div.markdown-body'}], - ['rehype-toc', { - headings: ['h2', 'h3'], - position: "beforeend", - customizeTOC(toc) { - let div = h("div.toc-sticky", [ - h("h4.toc-title", "Table of Contents"), - ...toc.children - ]) - toc.children = [div]; - return toc; - } - }], - ['rehype-autolink-headings', { - behavior: 'append', - content: [ - h('span.icon', "link") - ] - }], - ['rehype-add-classes', { 'h1,h2,h3': 'title' }], - ['rehype-urls', redirectDocsURL], - ['rehype-highlight'], - ['rehype-external-links', { target: "_blank", rel: ['noopener'] }] + ["rehype-slug"], + ["rehype-wrap", { wrapper: "div.markdown-body" }], + [ + "rehype-toc", + { + headings: ["h2", "h3"], + position: "beforeend", + customizeTOC(toc) { + let div = h("div.toc-sticky", [ + h("h4.toc-title", "Table of Contents"), + ...toc.children, + ]); + toc.children = [div]; + return toc; + }, + }, + ], + [ + "rehype-autolink-headings", + { + behavior: "append", + content: [h("span.icon", "link")], + }, + ], + ["rehype-add-classes", { "h1,h2,h3": "title" }], + ["rehype-urls", redirectDocsURL], + ["rehype-highlight"], + ["rehype-external-links", { target: "_blank", rel: ["noopener"] }], // ['rehype-sanitize'] ], -} +}; +const __dirname = path.resolve(path.dirname("")); +// console.log(fs.existsSync(path.join(__dirname, `/docs/animate`))); function redirectDocsURL(url) { if (/^\/docs/.test(url.path)) { - return url.path.replace(/^\/docs\//, "/").replace(/\.md$/, ""); + if (fs.existsSync(path.join(__dirname, url.path))) { + return url.path.replace(/^\/docs\//, "/").replace(/\.md$/, ""); + } else { + return `https://github.com/okikio/native/tree/beta` + url.path; + } } else if (/\.md$/.test(url.path)) { return url.path.replace(/\.md$/, ""); } else if (/^\/packages/.test(url.path)) { if (/LICENSE$/i.test(url.path)) { - return "https://github.com/okikio/native/tree/master/LICENSE"; - + return "https://github.com/okikio/native/tree/beta/LICENSE"; } else if (!/\.(jpg|svg|png|webp)$/.test(url.path)) { - return url.path.replace(/^\/packages/, "https://github.com/okikio/native/tree/beta/packages"); + return url.path.replace( + /^\/packages/, + "https://github.com/okikio/native/tree/beta/packages" + ); } } + + return `https://github.com/okikio/native/tree/beta` + url.path; } diff --git a/src/pages/[...page].astro b/src/pages/[...page].astro index 619f7fc..288430a 100644 --- a/src/pages/[...page].astro +++ b/src/pages/[...page].astro @@ -38,6 +38,7 @@ let pathArr = Astro.request.params.page.split("/");
+

Home / {pathArr.map((path, i) => { -- knotmirror 0.0.1