swup 4.2.0 → 4.3.0
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/dist/Swup.cjs +1 -1
- package/dist/Swup.cjs.map +1 -1
- package/dist/Swup.modern.js +1 -1
- package/dist/Swup.modern.js.map +1 -1
- package/dist/Swup.module.js +1 -1
- package/dist/Swup.module.js.map +1 -1
- package/dist/Swup.umd.js +1 -1
- package/dist/Swup.umd.js.map +1 -1
- package/dist/types/Swup.d.ts +119 -119
- package/dist/types/__test__/index.test.d.ts +1 -1
- package/dist/types/config/version.d.ts +5 -5
- package/dist/types/helpers/Location.d.ts +24 -24
- package/dist/types/helpers/__test__/matchPath.test.d.ts +1 -1
- package/dist/types/helpers/classify.d.ts +2 -2
- package/dist/types/helpers/createHistoryRecord.d.ts +9 -2
- package/dist/types/helpers/delegateEvent.d.ts +7 -7
- package/dist/types/helpers/getCurrentUrl.d.ts +4 -4
- package/dist/types/helpers/matchPath.d.ts +4 -4
- package/dist/types/helpers/updateHistoryRecord.d.ts +2 -2
- package/dist/types/helpers.d.ts +7 -7
- package/dist/types/index.d.ts +14 -14
- package/dist/types/modules/Cache.d.ts +34 -34
- package/dist/types/modules/Classes.d.ts +13 -13
- package/dist/types/modules/Hooks.d.ts +268 -250
- package/dist/types/modules/Visit.d.ts +85 -77
- package/dist/types/modules/__test__/cache.test.d.ts +1 -1
- package/dist/types/modules/__test__/{delegateEvent.d.ts → delegateEvent.test.d.ts} +1 -1
- package/dist/types/modules/__test__/hooks.test.d.ts +1 -1
- package/dist/types/modules/__test__/plugins.test.d.ts +1 -1
- package/dist/types/modules/__test__/replaceContent.test.d.ts +1 -1
- package/dist/types/modules/animatePageIn.d.ts +6 -6
- package/dist/types/modules/animatePageOut.d.ts +6 -6
- package/dist/types/modules/awaitAnimations.d.ts +19 -19
- package/dist/types/modules/fetchPage.d.ts +27 -29
- package/dist/types/modules/getAnchorElement.d.ts +9 -9
- package/dist/types/modules/navigate.d.ts +41 -35
- package/dist/types/modules/plugins.d.ts +26 -26
- package/dist/types/modules/renderPage.d.ts +7 -7
- package/dist/types/modules/replaceContent.d.ts +13 -13
- package/dist/types/modules/resolveUrl.d.ts +14 -14
- package/dist/types/modules/scrollToContent.d.ts +6 -6
- package/dist/types/utils/index.d.ts +20 -20
- package/dist/types/utils.d.ts +1 -1
- package/package.json +9 -3
- package/src/Swup.ts +7 -6
- package/src/config/version.ts +1 -2
- package/src/helpers/createHistoryRecord.ts +9 -1
- package/src/helpers/matchPath.ts +1 -1
- package/src/helpers/updateHistoryRecord.ts +4 -2
- package/src/modules/Cache.ts +2 -2
- package/src/modules/Classes.ts +1 -1
- package/src/modules/Hooks.ts +91 -39
- package/src/modules/Visit.ts +13 -0
- package/src/modules/__test__/cache.test.ts +3 -3
- package/src/modules/__test__/hooks.test.ts +12 -13
- package/src/modules/animatePageIn.ts +1 -1
- package/src/modules/animatePageOut.ts +2 -2
- package/src/modules/awaitAnimations.ts +1 -1
- package/src/modules/fetchPage.ts +7 -5
- package/src/modules/navigate.ts +23 -4
- package/src/modules/plugins.ts +3 -3
- package/src/modules/renderPage.ts +1 -5
- package/src/modules/replaceContent.ts +13 -0
- package/src/utils/index.ts +5 -4
- /package/src/modules/__test__/{delegateEvent.ts → delegateEvent.test.ts} +0 -0
package/src/modules/plugins.ts
CHANGED
|
@@ -21,8 +21,8 @@ export type Plugin = {
|
|
|
21
21
|
};
|
|
22
22
|
|
|
23
23
|
const isSwupPlugin = (maybeInvalidPlugin: unknown): maybeInvalidPlugin is Plugin => {
|
|
24
|
-
// @ts-ignore
|
|
25
|
-
return maybeInvalidPlugin?.isSwupPlugin;
|
|
24
|
+
// @ts-ignore: this might be anything, object or no
|
|
25
|
+
return Boolean(maybeInvalidPlugin?.isSwupPlugin);
|
|
26
26
|
};
|
|
27
27
|
|
|
28
28
|
/** Install a plugin. */
|
|
@@ -72,6 +72,6 @@ export function findPlugin(this: Swup, pluginOrName: Plugin | string) {
|
|
|
72
72
|
(plugin) =>
|
|
73
73
|
plugin === pluginOrName ||
|
|
74
74
|
plugin.name === pluginOrName ||
|
|
75
|
-
plugin.name === `Swup${pluginOrName}`
|
|
75
|
+
plugin.name === `Swup${String(pluginOrName)}`
|
|
76
76
|
);
|
|
77
77
|
}
|
|
@@ -47,14 +47,10 @@ export const renderPage = async function (this: Swup, requestedUrl: string, page
|
|
|
47
47
|
});
|
|
48
48
|
|
|
49
49
|
// scroll into view: either anchor or top of page
|
|
50
|
+
// @ts-ignore: not returning a promise is intentional to allow users to pause in handler
|
|
50
51
|
await this.hooks.call('content:scroll', undefined, () => {
|
|
51
52
|
return this.scrollToContent();
|
|
52
53
|
});
|
|
53
54
|
|
|
54
55
|
await this.hooks.call('page:view', { url: this.currentPageUrl, title: document.title });
|
|
55
|
-
|
|
56
|
-
// empty cache if it's disabled (in case preload plugin filled it)
|
|
57
|
-
if (!this.options.cache) {
|
|
58
|
-
this.cache.clear();
|
|
59
|
-
}
|
|
60
56
|
};
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import Swup, { Options } from '../Swup.js';
|
|
2
|
+
import { query, queryAll } from '../utils.js';
|
|
2
3
|
import { PageData } from './fetchPage.js';
|
|
3
4
|
|
|
4
5
|
/**
|
|
@@ -20,6 +21,9 @@ export const replaceContent = function (
|
|
|
20
21
|
const title = incomingDocument.querySelector('title')?.innerText || '';
|
|
21
22
|
document.title = title;
|
|
22
23
|
|
|
24
|
+
// Save persisted elements
|
|
25
|
+
const persistedElements = queryAll('[data-swup-persist]:not([data-swup-persist=""])');
|
|
26
|
+
|
|
23
27
|
// Update content containers
|
|
24
28
|
const replaced = containers
|
|
25
29
|
.map((selector) => {
|
|
@@ -39,5 +43,14 @@ export const replaceContent = function (
|
|
|
39
43
|
})
|
|
40
44
|
.filter(Boolean);
|
|
41
45
|
|
|
46
|
+
// Restore persisted elements
|
|
47
|
+
persistedElements.forEach((existing) => {
|
|
48
|
+
const key = existing.getAttribute('data-swup-persist');
|
|
49
|
+
const replacement = query(`[data-swup-persist="${key}"]`);
|
|
50
|
+
if (replacement && replacement !== existing) {
|
|
51
|
+
replacement.replaceWith(existing);
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
|
|
42
55
|
return replaced.length === containers.length;
|
|
43
56
|
};
|
package/src/utils/index.ts
CHANGED
|
@@ -23,18 +23,19 @@ export const nextTick = (): Promise<void> => {
|
|
|
23
23
|
};
|
|
24
24
|
|
|
25
25
|
/** Check if an object is a Promise or a Thenable */
|
|
26
|
-
export function isPromise<T>(obj:
|
|
26
|
+
export function isPromise<T>(obj: unknown): obj is PromiseLike<T> {
|
|
27
27
|
return (
|
|
28
28
|
!!obj &&
|
|
29
29
|
(typeof obj === 'object' || typeof obj === 'function') &&
|
|
30
|
-
typeof obj.then === 'function'
|
|
30
|
+
typeof (obj as Record<string, unknown>).then === 'function'
|
|
31
31
|
);
|
|
32
32
|
}
|
|
33
33
|
|
|
34
34
|
/** Call a function as a Promise. Resolves with the returned Promsise or immediately. */
|
|
35
|
-
|
|
35
|
+
// eslint-disable-next-line @typescript-eslint/ban-types, @typescript-eslint/no-explicit-any
|
|
36
|
+
export function runAsPromise(func: Function, args: unknown[] = []): Promise<unknown> {
|
|
36
37
|
return new Promise((resolve, reject) => {
|
|
37
|
-
const result = func(...args);
|
|
38
|
+
const result: unknown = func(...args);
|
|
38
39
|
if (isPromise(result)) {
|
|
39
40
|
result.then(resolve, reject);
|
|
40
41
|
} else {
|
|
File without changes
|