Skip to content

Commit

Permalink
helpers.js refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
jakewhiteley committed May 15, 2024
1 parent bec8e7e commit f31bd69
Show file tree
Hide file tree
Showing 16 changed files with 427 additions and 483 deletions.
2 changes: 1 addition & 1 deletion dist/taxi.esm.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/taxi.esm.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/taxi.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/taxi.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/taxi.modern.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/taxi.modern.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/taxi.umd.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/taxi.umd.js.map

Large diffs are not rendered by default.

456 changes: 228 additions & 228 deletions src/Core.d.ts

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions src/Core.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import E from '@unseenco/e'
import { appendScript, parseDom, processUrl, reloadScript, reloadInlineStyle, appendInlineStyle } from './helpers'
import { appendElement, parseDom, processUrl, reloadElement } from './helpers'
import Transition from './Transition'
import Renderer from './Renderer'
import RouteStore from './RouteStore'
Expand Down Expand Up @@ -342,15 +342,15 @@ export default class Core {
for (let i = 0; i < currentScripts.length; i++) {
for (let n = 0; n < newScripts.length; n++) {
if (currentScripts[i].outerHTML === newScripts[n].outerHTML) {
reloadScript(currentScripts[i])
reloadElement(currentScripts[i], 'SCRIPT')
newScripts.splice(n, 1)
break
}
}
}

for (const script of newScripts) {
appendScript(script)
appendElement(script, 'SCRIPT')
}
}

Expand All @@ -377,15 +377,15 @@ export default class Core {
for (let i = 0; i < currentInlineStyles.length; i++) {
for (let n = 0; n < newInlineStyles.length; n++) {
if (currentInlineStyles[i].outerHTML === newInlineStyles[n].outerHTML) {
reloadInlineStyle(currentInlineStyles[i])
reloadElement(currentInlineStyles[i], 'STYLE')
newInlineStyles.splice(n, 1)
break
}
}
}

for (const style of newInlineStyles) {
appendInlineStyle(style)
appendElement(style, 'STYLE')
}
}

Expand Down
82 changes: 41 additions & 41 deletions src/Renderer.d.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,41 @@
export default class Renderer {
/**
* @param {{content: HTMLElement|Element, page: Document|Node, title: string, wrapper: Element}} props
*/
constructor({ content, page, title, wrapper }: {
content: HTMLElement | Element;
page: Document | Node;
title: string;
wrapper: Element;
});
_contentString: string;
_DOM: HTMLDivElement;
page: Node | Document;
title: string;
wrapper: Element;
content: Element;
onEnter(): void;
onEnterCompleted(): void;
onLeave(): void;
onLeaveCompleted(): void;
initialLoad(): void;
update(): void;
createDom(): void;
remove(): void;
/**
* Called when transitioning into the current page.
* @param {Transition} transition
* @param {string|HTMLElement|false} trigger
* @return {Promise<null>}
*/
enter(transition: Transition, trigger: string | HTMLElement | false): Promise<null>;
/**
* Called when transitioning away from the current page.
* @param {Transition} transition
* @param {string|HTMLElement|false} trigger
* @param {boolean} removeOldContent
* @return {Promise<null>}
*/
leave(transition: Transition, trigger: string | HTMLElement | false, removeOldContent: boolean): Promise<null>;
}
import Transition from "./Transition";
export default class Renderer {
/**
* @param {{content: HTMLElement|Element, page: Document|Node, title: string, wrapper: Element}} props
*/
constructor({ content, page, title, wrapper }: {
content: HTMLElement | Element;
page: Document | Node;
title: string;
wrapper: Element;
});
_contentString: string;
_DOM: HTMLDivElement;
page: Node | Document;
title: string;
wrapper: Element;
content: Element;
onEnter(): void;
onEnterCompleted(): void;
onLeave(): void;
onLeaveCompleted(): void;
initialLoad(): void;
update(): void;
createDom(): void;
remove(): void;
/**
* Called when transitioning into the current page.
* @param {Transition} transition
* @param {string|HTMLElement|false} trigger
* @return {Promise<null>}
*/
enter(transition: Transition, trigger: string | HTMLElement | false): Promise<null>;
/**
* Called when transitioning away from the current page.
* @param {Transition} transition
* @param {string|HTMLElement|false} trigger
* @param {boolean} removeOldContent
* @return {Promise<null>}
*/
leave(transition: Transition, trigger: string | HTMLElement | false, removeOldContent: boolean): Promise<null>;
}
import Transition from "./Transition";
68 changes: 34 additions & 34 deletions src/RouteStore.d.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
export default class RouteStore {
/**
* @type {Map<string, Map<string, string>>}
*/
data: Map<string, Map<string, string>>;
/**
* @type {Map<string, RegExp>}
*/
regexCache: Map<string, RegExp>;
/**
*
* @param {string} fromPattern
* @param {string} toPattern
* @param {string} transition
*/
add(fromPattern: string, toPattern: string, transition: string): void;
/**
*
* @param {{ raw: string, href: string, hasHash: boolean, pathname: string }} currentUrl
* @param {{ raw: string, href: string, hasHash: boolean, pathname: string }} nextUrl
* @return {string|null}
*/
findMatch(currentUrl: {
raw: string;
href: string;
hasHash: boolean;
pathname: string;
}, nextUrl: {
raw: string;
href: string;
hasHash: boolean;
pathname: string;
}): string | null;
}
export default class RouteStore {
/**
* @type {Map<string, Map<string, string>>}
*/
data: Map<string, Map<string, string>>;
/**
* @type {Map<string, RegExp>}
*/
regexCache: Map<string, RegExp>;
/**
*
* @param {string} fromPattern
* @param {string} toPattern
* @param {string} transition
*/
add(fromPattern: string, toPattern: string, transition: string): void;
/**
*
* @param {{ raw: string, href: string, hasHash: boolean, pathname: string }} currentUrl
* @param {{ raw: string, href: string, hasHash: boolean, pathname: string }} nextUrl
* @return {string|null}
*/
findMatch(currentUrl: {
raw: string;
href: string;
hasHash: boolean;
pathname: string;
}, nextUrl: {
raw: string;
href: string;
hasHash: boolean;
pathname: string;
}): string | null;
}
86 changes: 43 additions & 43 deletions src/Transition.d.ts
Original file line number Diff line number Diff line change
@@ -1,43 +1,43 @@
export default class Transition {
/**
* @param {{wrapper: HTMLElement}} props
*/
constructor({ wrapper }: {
wrapper: HTMLElement;
});
wrapper: HTMLElement;
/**
* @param {{ from: HTMLElement|Element, trigger: string|HTMLElement|false }} props
* @return {Promise<void>}
*/
leave(props: {
from: HTMLElement | Element;
trigger: string | HTMLElement | false;
}): Promise<void>;
/**
* @param {{ to: HTMLElement|Element, trigger: string|HTMLElement|false }} props
* @return {Promise<void>}
*/
enter(props: {
to: HTMLElement | Element;
trigger: string | HTMLElement | false;
}): Promise<void>;
/**
* Handle the transition leaving the previous page.
* @param {{from: HTMLElement|Element, trigger: string|HTMLElement|false, done: function}} props
*/
onLeave({ from, trigger, done }: {
from: HTMLElement | Element;
trigger: string | HTMLElement | false;
done: Function;
}): void;
/**
* Handle the transition entering the next page.
* @param {{to: HTMLElement|Element, trigger: string|HTMLElement|false, done: function}} props
*/
onEnter({ to, trigger, done }: {
to: HTMLElement | Element;
trigger: string | HTMLElement | false;
done: Function;
}): void;
}
export default class Transition {
/**
* @param {{wrapper: HTMLElement}} props
*/
constructor({ wrapper }: {
wrapper: HTMLElement;
});
wrapper: HTMLElement;
/**
* @param {{ from: HTMLElement|Element, trigger: string|HTMLElement|false }} props
* @return {Promise<void>}
*/
leave(props: {
from: HTMLElement | Element;
trigger: string | HTMLElement | false;
}): Promise<void>;
/**
* @param {{ to: HTMLElement|Element, trigger: string|HTMLElement|false }} props
* @return {Promise<void>}
*/
enter(props: {
to: HTMLElement | Element;
trigger: string | HTMLElement | false;
}): Promise<void>;
/**
* Handle the transition leaving the previous page.
* @param {{from: HTMLElement|Element, trigger: string|HTMLElement|false, done: function}} props
*/
onLeave({ from, trigger, done }: {
from: HTMLElement | Element;
trigger: string | HTMLElement | false;
done: Function;
}): void;
/**
* Handle the transition entering the next page.
* @param {{to: HTMLElement|Element, trigger: string|HTMLElement|false, done: function}} props
*/
onEnter({ to, trigger, done }: {
to: HTMLElement | Element;
trigger: string | HTMLElement | false;
done: Function;
}): void;
}
92 changes: 42 additions & 50 deletions src/helpers.d.ts
Original file line number Diff line number Diff line change
@@ -1,50 +1,42 @@
/**
* Parse a HTML string into a proper Document.
* @param {string|Document} html
* @return {Document|*}
*/
export function parseDom(html: string | Document): Document | any;
/**
* Extract details from a given URL string. Assumed to be on the current TLD.
* @param {string} url
* @return {{raw: string, href: string, host: string, hasHash: boolean, pathname: string}}
*/
export function processUrl(url: string): {
raw: string;
href: string;
host: string;
hasHash: boolean;
pathname: string;
};
/**
* Reloads a provided script/stylesheet by replacing with itself.
* @param {HTMLElement|HTMLScriptElement} node
*/
export function reloadScript(node: HTMLElement | HTMLScriptElement): void;
/**
* Reloads a provided inline-stylesheet by replacing with itself.
* @param {HTMLStyleElement} node
*/
export function reloadInlineStyle(node: HTMLStyleElement): void;
/**
* Loads a provided script/stylesheet by appending a clone to the current document.
* @param {HTMLElement} node
*/
export function appendScript(node: HTMLElement): void;
/**
* Loads a provided inline-stylesheet by appending a clone to the current document.
* @param {HTMLStyleElement} node
*/
export function appendInlineStyle(node: HTMLStyleElement): void;
/**
* Creates a clone of a given HTMLElement
* @param {HTMLElement} node
* @return {HTMLElement}
*/
export function duplicateScript(node: HTMLElement): HTMLElement;
/**
* Creates a clone of a given HTMLStyleElement
* @param {HTMLStyleElement} node
* @return {HTMLStyleElement}
*/
export function duplicateInlineStyle(node: HTMLStyleElement): HTMLStyleElement;
/**
* Parse a HTML string into a proper Document.
*
* @param {string|Document} html
* @return {Document|*}
*/
export function parseDom(html: string | Document): Document | any;
/**
* Extract details from a given URL string. Assumed to be on the current TLD.
*
* @param {string} url
* @return {{raw: string, href: string, host: string, hasHash: boolean, pathname: string}}
*/
export function processUrl(url: string): {
raw: string;
href: string;
host: string;
hasHash: boolean;
pathname: string;
};
/**
* Reloads a provided script/stylesheet by replacing with itself.
*
* @param {HTMLElement|HTMLScriptElement|HTMLStyleElement} node
* @param {string} elementType - 'SCRIPT' or 'STYLE'
*/
export function reloadElement(node: HTMLElement | HTMLScriptElement | HTMLStyleElement, elementType: string): void;
/**
* Loads a provided script/stylesheet by appending a clone to the current document.
*
* @param {HTMLElement|HTMLStyleElement} node
* @param {string} elementType - 'SCRIPT' or 'STYLE'
*/
export function appendElement(node: HTMLElement | HTMLStyleElement, elementType: string): void;
/**
* Creates a clone of a given HTMLElement or HTMLStyleElement
*
* @param {HTMLElement|HTMLStyleElement} node
* @param {string} elementType - 'SCRIPT' or 'STYLE'
* @return {HTMLElement|HTMLStyleElement}
*/
export function duplicateElement(node: HTMLElement | HTMLStyleElement, elementType: string): HTMLElement | HTMLStyleElement;
Loading

0 comments on commit f31bd69

Please sign in to comment.