Skip to content

Commit e620cc6

Browse files
committed
fix(astro): don't import Astro's private modules
1 parent 9bf2156 commit e620cc6

File tree

2 files changed

+184
-1
lines changed

2 files changed

+184
-1
lines changed

packages/astro/src/default/layouts/Layout.astro

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ const baseURL = import.meta.env.BASE_URL;
3838
}
3939
</script>
4040
<script>
41-
import * as builtInSwap from 'node_modules/astro/dist/transitions/swap-functions';
41+
import * as builtInSwap from './swap-functions.js';
4242

4343
declare global {
4444
function setTutorialKitTheme(): void;
Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
// copy of built https://github.com/withastro/astro/blob/64f81e92d1ddd325f7f80508eb88590814c070a8/packages/astro/src/transitions/swap-functions.ts
2+
3+
/* eslint-disable */
4+
5+
/*
6+
MIT License
7+
8+
Copyright (c) 2021 Fred K. Schott
9+
10+
Permission is hereby granted, free of charge, to any person obtaining a copy
11+
of this software and associated documentation files (the "Software"), to deal
12+
in the Software without restriction, including without limitation the rights
13+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14+
copies of the Software, and to permit persons to whom the Software is
15+
furnished to do so, subject to the following conditions:
16+
17+
The above copyright notice and this permission notice shall be included in all
18+
copies or substantial portions of the Software.
19+
20+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26+
SOFTWARE.
27+
28+
"""
29+
This license applies to parts of the `packages/create-astro` and `packages/astro` subdirectories originating from the https://github.com/sveltejs/kit repository:
30+
31+
Copyright (c) 2020 [these people](https://github.com/sveltejs/kit/graphs/contributors)
32+
33+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
34+
35+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
36+
37+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
38+
"""
39+
40+
"""
41+
This license applies to parts of the `packages/create-astro` and `packages/astro` subdirectories originating from the https://github.com/vitejs/vite repository:
42+
43+
MIT License
44+
45+
Copyright (c) 2019-present, Yuxi (Evan) You and Vite contributors
46+
47+
Permission is hereby granted, free of charge, to any person obtaining a copy
48+
of this software and associated documentation files (the "Software"), to deal
49+
in the Software without restriction, including without limitation the rights
50+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
51+
copies of the Software, and to permit persons to whom the Software is
52+
furnished to do so, subject to the following conditions:
53+
54+
The above copyright notice and this permission notice shall be included in all
55+
copies or substantial portions of the Software.
56+
57+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
58+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
59+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
60+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
61+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
62+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
63+
SOFTWARE.
64+
"""
65+
*/
66+
67+
const PERSIST_ATTR = 'data-astro-transition-persist';
68+
69+
function deselectScripts(doc) {
70+
for (const s1 of document.scripts) {
71+
for (const s2 of doc.scripts) {
72+
if (
73+
// Check if the script should be rerun regardless of it being the same
74+
!s2.hasAttribute('data-astro-rerun') && // Inline
75+
((!s1.src && s1.textContent === s2.textContent) || // External
76+
(s1.src && s1.type === s2.type && s1.src === s2.src))
77+
) {
78+
s2.dataset.astroExec = '';
79+
break;
80+
}
81+
}
82+
}
83+
}
84+
85+
function swapRootAttributes(doc) {
86+
const html = document.documentElement;
87+
const astroAttributes = [...html.attributes].filter(
88+
({ name }) => (html.removeAttribute(name), name.startsWith('data-astro-')),
89+
);
90+
[...doc.documentElement.attributes, ...astroAttributes].forEach(({ name, value }) => html.setAttribute(name, value));
91+
}
92+
93+
function swapHeadElements(doc) {
94+
for (const el of Array.from(document.head.children)) {
95+
const newEl = persistedHeadElement(el, doc);
96+
97+
if (newEl) {
98+
newEl.remove();
99+
} else {
100+
el.remove();
101+
}
102+
}
103+
document.head.append(...doc.head.children);
104+
}
105+
106+
function swapBodyElement(newElement, oldElement) {
107+
oldElement.replaceWith(newElement);
108+
109+
for (const el of oldElement.querySelectorAll(`[${PERSIST_ATTR}]`)) {
110+
const id = el.getAttribute(PERSIST_ATTR);
111+
const newEl = newElement.querySelector(`[${PERSIST_ATTR}="${id}"]`);
112+
113+
if (newEl) {
114+
newEl.replaceWith(el);
115+
116+
if (newEl.localName === 'astro-island' && shouldCopyProps(el)) {
117+
el.setAttribute('ssr', '');
118+
el.setAttribute('props', newEl.getAttribute('props'));
119+
}
120+
}
121+
}
122+
}
123+
124+
const saveFocus = () => {
125+
const activeElement = document.activeElement;
126+
127+
if (activeElement?.closest(`[${PERSIST_ATTR}]`)) {
128+
if (activeElement instanceof HTMLInputElement || activeElement instanceof HTMLTextAreaElement) {
129+
const start = activeElement.selectionStart;
130+
const end = activeElement.selectionEnd;
131+
132+
return () => restoreFocus({ activeElement, start, end });
133+
}
134+
135+
return () => restoreFocus({ activeElement });
136+
} else {
137+
return () => restoreFocus({ activeElement: null });
138+
}
139+
};
140+
const restoreFocus = ({ activeElement, start, end }) => {
141+
if (activeElement) {
142+
activeElement.focus();
143+
144+
if (activeElement instanceof HTMLInputElement || activeElement instanceof HTMLTextAreaElement) {
145+
if (typeof start === 'number') {
146+
activeElement.selectionStart = start;
147+
}
148+
149+
if (typeof end === 'number') {
150+
activeElement.selectionEnd = end;
151+
}
152+
}
153+
}
154+
};
155+
const persistedHeadElement = (el, newDoc) => {
156+
const id = el.getAttribute(PERSIST_ATTR);
157+
const newEl = id && newDoc.head.querySelector(`[${PERSIST_ATTR}="${id}"]`);
158+
159+
if (newEl) {
160+
return newEl;
161+
}
162+
163+
if (el.matches('link[rel=stylesheet]')) {
164+
const href = el.getAttribute('href');
165+
return newDoc.head.querySelector(`link[rel=stylesheet][href="${href}"]`);
166+
}
167+
168+
return null;
169+
};
170+
const shouldCopyProps = (el) => {
171+
const persistProps = el.dataset.astroTransitionPersistProps;
172+
return persistProps == null || persistProps === 'false';
173+
};
174+
const swap = (doc) => {
175+
deselectScripts(doc);
176+
swapRootAttributes(doc);
177+
swapHeadElements(doc);
178+
179+
const restoreFocusFunction = saveFocus();
180+
swapBodyElement(doc.body, document.body);
181+
restoreFocusFunction();
182+
};
183+
export { deselectScripts, restoreFocus, saveFocus, swap, swapBodyElement, swapHeadElements, swapRootAttributes };

0 commit comments

Comments
 (0)