mirror of
https://github.com/lightpanda-io/browser.git
synced 2025-10-29 15:13:28 +00:00
clean up history api
This commit is contained in:
@@ -27,7 +27,8 @@ const History = @This();
|
|||||||
|
|
||||||
const HistoryEntry = struct {
|
const HistoryEntry = struct {
|
||||||
url: []const u8,
|
url: []const u8,
|
||||||
// Serialized as JSON.
|
// This is serialized as JSON because
|
||||||
|
// History must survive a JsContext.
|
||||||
state: ?[]u8,
|
state: ?[]u8,
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -44,9 +45,13 @@ const ScrollRestorationMode = enum {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn toString(self: ScrollRestorationMode) []const u8 {
|
||||||
|
return @tagName(self);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
scrollRestoration: ScrollRestorationMode = .auto,
|
scroll_restoration: ScrollRestorationMode = .auto,
|
||||||
stack: std.ArrayListUnmanaged(HistoryEntry) = .empty,
|
stack: std.ArrayListUnmanaged(HistoryEntry) = .empty,
|
||||||
current: ?usize = null,
|
current: ?usize = null,
|
||||||
|
|
||||||
@@ -54,15 +59,12 @@ pub fn get_length(self: *History) u32 {
|
|||||||
return @intCast(self.stack.items.len);
|
return @intCast(self.stack.items.len);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_scrollRestoration(self: *History) []const u8 {
|
pub fn get_scrollRestoration(self: *History) ScrollRestorationMode {
|
||||||
return switch (self.scrollRestoration) {
|
return self.scroll_restoration;
|
||||||
.auto => "auto",
|
|
||||||
.manual => "manual",
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_scrollRestoration(self: *History, mode: []const u8) void {
|
pub fn set_scrollRestoration(self: *History, mode: []const u8) void {
|
||||||
self.scrollRestoration = ScrollRestorationMode.fromString(mode) orelse self.scrollRestoration;
|
self.scroll_restoration = ScrollRestorationMode.fromString(mode) orelse self.scroll_restoration;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_state(self: *History, page: *Page) !?Env.Value {
|
pub fn get_state(self: *History, page: *Page) !?Env.Value {
|
||||||
@@ -102,23 +104,20 @@ pub fn dispatchPopStateEvent(state: ?[]const u8, page: *Page) void {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
fn _dispatchPopStateEvent(
|
fn _dispatchPopStateEvent(state: ?[]const u8, page: *Page) !void {
|
||||||
state: ?[]const u8,
|
|
||||||
page: *Page,
|
|
||||||
) !void {
|
|
||||||
var evt = try PopStateEvent.constructor("popstate", .{ .state = state });
|
var evt = try PopStateEvent.constructor("popstate", .{ .state = state });
|
||||||
|
|
||||||
_ = try parser.eventTargetDispatchEvent(
|
_ = try parser.eventTargetDispatchEvent(
|
||||||
@as(*parser.EventTarget, @ptrCast(&page.window)),
|
@as(*parser.EventTarget, @ptrCast(&page.window)),
|
||||||
@as(*parser.Event, @ptrCast(&evt)),
|
&evt.proto,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn _pushState(self: *History, state: Env.JsObject, _: ?[]const u8, _url: ?[]const u8, page: *Page) !void {
|
pub fn _pushState(self: *History, state: Env.JsObject, _: ?[]const u8, _url: ?[]const u8, page: *Page) !void {
|
||||||
const arena = page.session.arena;
|
const arena = page.session.arena;
|
||||||
|
|
||||||
|
const json = try state.toJson(arena);
|
||||||
const url = if (_url) |u| try arena.dupe(u8, u) else try arena.dupe(u8, page.url.raw);
|
const url = if (_url) |u| try arena.dupe(u8, u) else try arena.dupe(u8, page.url.raw);
|
||||||
const json = try state.toJson(page.session.arena);
|
|
||||||
const entry = HistoryEntry{ .state = json, .url = url };
|
const entry = HistoryEntry{ .state = json, .url = url };
|
||||||
try self.stack.append(arena, entry);
|
try self.stack.append(arena, entry);
|
||||||
self.current = self.stack.items.len - 1;
|
self.current = self.stack.items.len - 1;
|
||||||
@@ -129,8 +128,8 @@ pub fn _replaceState(self: *History, state: Env.JsObject, _: ?[]const u8, _url:
|
|||||||
|
|
||||||
if (self.current) |curr| {
|
if (self.current) |curr| {
|
||||||
const entry = &self.stack.items[curr];
|
const entry = &self.stack.items[curr];
|
||||||
const url = if (_url) |u| try arena.dupe(u8, u) else try arena.dupe(u8, page.url.raw);
|
|
||||||
const json = try state.toJson(arena);
|
const json = try state.toJson(arena);
|
||||||
|
const url = if (_url) |u| try arena.dupe(u8, u) else try arena.dupe(u8, page.url.raw);
|
||||||
entry.* = HistoryEntry{ .state = json, .url = url };
|
entry.* = HistoryEntry{ .state = json, .url = url };
|
||||||
} else {
|
} else {
|
||||||
try self._pushState(state, "", _url, page);
|
try self._pushState(state, "", _url, page);
|
||||||
@@ -152,11 +151,7 @@ pub fn go(self: *History, delta: i32, page: *Page) !void {
|
|||||||
self.current = index;
|
self.current = index;
|
||||||
|
|
||||||
if (try page.isSameOrigin(entry.url)) {
|
if (try page.isSameOrigin(entry.url)) {
|
||||||
if (entry.state) |state| {
|
History.dispatchPopStateEvent(entry.state, page);
|
||||||
History.dispatchPopStateEvent(state, page);
|
|
||||||
} else {
|
|
||||||
History.dispatchPopStateEvent(null, page);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
try page.navigateFromWebAPI(entry.url, .{ .reason = .history });
|
try page.navigateFromWebAPI(entry.url, .{ .reason = .history });
|
||||||
|
|||||||
Reference in New Issue
Block a user