|
| 1 | +// Copyright (C) 2023-2024 Lightpanda (Selecy SAS) |
| 2 | +// |
| 3 | +// Francis Bouvier <francis@lightpanda.io> |
| 4 | +// Pierre Tachoire <pierre@lightpanda.io> |
| 5 | +// |
| 6 | +// This program is free software: you can redistribute it and/or modify |
| 7 | +// it under the terms of the GNU Affero General Public License as |
| 8 | +// published by the Free Software Foundation, either version 3 of the |
| 9 | +// License, or (at your option) any later version. |
| 10 | +// |
| 11 | +// This program is distributed in the hope that it will be useful, |
| 12 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | +// GNU Affero General Public License for more details. |
| 15 | +// |
| 16 | +// You should have received a copy of the GNU Affero General Public License |
| 17 | +// along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 18 | + |
| 19 | +const std = @import("std"); |
| 20 | + |
| 21 | +const Env = @import("../env.zig").Env; |
| 22 | +const Page = @import("../page.zig").Page; |
| 23 | + |
| 24 | +// https://html.spec.whatwg.org/multipage/nav-history-apis.html#the-history-interface |
| 25 | +const History = @This(); |
| 26 | + |
| 27 | +const HistoryEntry = struct { |
| 28 | + url: ?[]const u8, |
| 29 | + // Serialized Env.JsObject |
| 30 | + state: []u8, |
| 31 | +}; |
| 32 | + |
| 33 | +const ScrollRestorationMode = enum { |
| 34 | + auto, |
| 35 | + manual, |
| 36 | + |
| 37 | + pub fn fromString(str: []const u8) ?ScrollRestorationMode { |
| 38 | + for (std.enums.values(ScrollRestorationMode)) |mode| { |
| 39 | + if (std.ascii.eqlIgnoreCase(str, @tagName(mode))) { |
| 40 | + return mode; |
| 41 | + } |
| 42 | + } else { |
| 43 | + return null; |
| 44 | + } |
| 45 | + } |
| 46 | +}; |
| 47 | + |
| 48 | +scrollRestoration: ScrollRestorationMode = .auto, |
| 49 | +stack: std.ArrayListUnmanaged(HistoryEntry) = .empty, |
| 50 | +current: ?usize = null, |
| 51 | + |
| 52 | +pub fn get_length(self: *History) u32 { |
| 53 | + return @intCast(self.stack.items.len); |
| 54 | +} |
| 55 | + |
| 56 | +pub fn get_scrollRestoration(self: *History) []const u8 { |
| 57 | + return switch (self.scrollRestoration) { |
| 58 | + .auto => "auto", |
| 59 | + .manual => "manual", |
| 60 | + }; |
| 61 | +} |
| 62 | + |
| 63 | +pub fn set_scrollRestoration(self: *History, mode: []const u8) void { |
| 64 | + self.scrollRestoration = ScrollRestorationMode.fromString(mode) orelse self.scrollRestoration; |
| 65 | +} |
| 66 | + |
| 67 | +pub fn get_state(self: *History, page: *Page) !?Env.JsObject { |
| 68 | + if (self.current) |curr| { |
| 69 | + const entry = self.stack.items[curr]; |
| 70 | + const object = try Env.JsObject.fromJson(page.main_context, entry.state); |
| 71 | + return object; |
| 72 | + } else { |
| 73 | + return null; |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +pub fn _pushState(self: *History, state: Env.JsObject, _: ?[]const u8, url: ?[]const u8, page: *Page) !void { |
| 78 | + const json = try state.toJson(page.arena); |
| 79 | + const entry = HistoryEntry{ .state = json, .url = url }; |
| 80 | + try self.stack.append(page.session.arena, entry); |
| 81 | + self.current = self.stack.items.len; |
| 82 | +} |
| 83 | + |
| 84 | +// TODO implement the function |
| 85 | +// data must handle any argument. We could expect a std.json.Value but |
| 86 | +// https://github.com/lightpanda-io/zig-js-runtime/issues/267 is missing. |
| 87 | +pub fn _replaceState(self: *History, state: Env.JsObject, _: ?[]const u8, url: ?[]const u8) void { |
| 88 | + _ = self; |
| 89 | + _ = url; |
| 90 | + _ = state; |
| 91 | +} |
| 92 | + |
| 93 | +// TODO implement the function |
| 94 | +pub fn _go(self: *History, delta: ?i32) void { |
| 95 | + _ = self; |
| 96 | + _ = delta; |
| 97 | +} |
| 98 | + |
| 99 | +pub fn _back(self: *History) void { |
| 100 | + if (self.current) |curr| { |
| 101 | + if (curr > 0) { |
| 102 | + self.current = curr - 1; |
| 103 | + } |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +pub fn _forward(self: *History) void { |
| 108 | + if (self.current) |curr| { |
| 109 | + if (curr < self.stack.items.len) { |
| 110 | + self.current = curr + 1; |
| 111 | + } |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +const testing = @import("../../testing.zig"); |
| 116 | +test "Browser: HTML.History" { |
| 117 | + try testing.htmlRunner("html/history.html"); |
| 118 | +} |
0 commit comments