Skip to content
This repository was archived by the owner on Jan 11, 2023. It is now read-only.

Commit cd95d2c

Browse files
committed
Move the first few functions
1 parent 2135b2f commit cd95d2c

File tree

3 files changed

+57
-53
lines changed

3 files changed

+57
-53
lines changed

runtime/src/app/app.ts

Lines changed: 1 addition & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -71,16 +71,6 @@ export function set_target(element) {
7171
target = element;
7272
}
7373

74-
export let uid = 1;
75-
export function set_uid(n) {
76-
uid = n;
77-
}
78-
79-
export let cid: number;
80-
export function set_cid(n) {
81-
cid = n;
82-
}
83-
8474
const _history = typeof history !== 'undefined' ? history : {
8575
pushState: (state: any, title: string, href: string) => {},
8676
replaceState: (state: any, title: string, href: string) => {},
@@ -169,22 +159,7 @@ export function scroll_state() {
169159
};
170160
}
171161

172-
export async function navigate(dest: Target, id: number, noscroll?: boolean, hash?: string): Promise<any> {
173-
if (id) {
174-
// popstate or initial navigation
175-
cid = id;
176-
} else {
177-
const current_scroll = scroll_state();
178-
179-
// clicked on a link. preserve scroll state
180-
scroll_history[cid] = current_scroll;
181-
182-
id = cid = ++uid;
183-
scroll_history[cid] = noscroll ? current_scroll : { x: 0, y: 0 };
184-
}
185-
186-
cid = id;
187-
162+
export async function handle_target(dest: Target) {
188163
if (root_component) stores.preloading.set(true);
189164

190165
const loaded = prefetching && prefetching.href === dest.href ?
@@ -204,26 +179,6 @@ export async function navigate(dest: Target, id: number, noscroll?: boolean, has
204179
const { props, branch } = loaded_result;
205180
await render(branch, props, dest.page);
206181
}
207-
if (document.activeElement && (document.activeElement instanceof HTMLElement)) document.activeElement.blur();
208-
209-
if (!noscroll) {
210-
let scroll = scroll_history[id];
211-
212-
if (hash) {
213-
// scroll is an element id (from a hash), we need to compute y.
214-
const deep_linked = document.getElementById(hash.slice(1));
215-
216-
if (deep_linked) {
217-
scroll = {
218-
x: 0,
219-
y: deep_linked.getBoundingClientRect().top + scrollY
220-
};
221-
}
222-
}
223-
224-
scroll_history[cid] = scroll;
225-
if (scroll) scrollTo(scroll.x, scroll.y);
226-
}
227182
}
228183

229184
async function render(branch: any[], props: any, page: Page) {

runtime/src/app/goto/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { history, select_target, navigate, cid } from '../app';
1+
import { history, select_target } from '../app';
2+
import { navigate, cid } from '../start';
23

34
export default function goto(
45
href: string,

runtime/src/app/start/index.ts

Lines changed: 54 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,28 @@
11
import {
2-
cid,
2+
handle_target,
33
history,
44
initial_data,
5-
navigate,
65
scroll_history,
76
scroll_state,
87
select_target,
98
handle_error,
10-
set_target,
11-
uid,
12-
set_uid,
13-
set_cid
9+
set_target
1410
} from '../app';
11+
import {
12+
Target
13+
} from '../types';
1514
import prefetch from '../prefetch/index';
1615

16+
export let uid = 1;
17+
export function set_uid(n: number) {
18+
uid = n;
19+
}
20+
21+
export let cid: number;
22+
export function set_cid(n: number) {
23+
cid = n;
24+
}
25+
1726
export default function start(opts: {
1827
target: Node
1928
}): Promise<void> {
@@ -145,3 +154,42 @@ function handle_popstate(event: PopStateEvent) {
145154
history.replaceState({ id: cid }, '', location.href);
146155
}
147156
}
157+
158+
export async function navigate(dest: Target, id: number, noscroll?: boolean, hash?: string): Promise<any> {
159+
if (id) {
160+
// popstate or initial navigation
161+
cid = id;
162+
} else {
163+
const current_scroll = scroll_state();
164+
165+
// clicked on a link. preserve scroll state
166+
scroll_history[cid] = current_scroll;
167+
168+
id = cid = ++uid;
169+
scroll_history[cid] = noscroll ? current_scroll : { x: 0, y: 0 };
170+
}
171+
172+
cid = id;
173+
174+
await handle_target(dest);
175+
if (document.activeElement && (document.activeElement instanceof HTMLElement)) document.activeElement.blur();
176+
177+
if (!noscroll) {
178+
let scroll = scroll_history[id];
179+
180+
if (hash) {
181+
// scroll is an element id (from a hash), we need to compute y.
182+
const deep_linked = document.getElementById(hash.slice(1));
183+
184+
if (deep_linked) {
185+
scroll = {
186+
x: 0,
187+
y: deep_linked.getBoundingClientRect().top + scrollY
188+
};
189+
}
190+
}
191+
192+
scroll_history[cid] = scroll;
193+
if (scroll) scrollTo(scroll.x, scroll.y);
194+
}
195+
}

0 commit comments

Comments
 (0)