From b0f598910f1f8bc8db6b65a0f01b1950ed7c3841 Mon Sep 17 00:00:00 2001 From: Akshay Nair Date: Sat, 26 Jul 2025 16:59:35 +0530 Subject: Add web.view.expose for simple lua->js interop in decorations --- lua/null-browser/api.lua | 21 +++++++++++++++++++++ lua/null-browser/extras/tabline.lua | 14 +++++++++++--- 2 files changed, 32 insertions(+), 3 deletions(-) (limited to 'lua') diff --git a/lua/null-browser/api.lua b/lua/null-browser/api.lua index 598594e..07b7072 100644 --- a/lua/null-browser/api.lua +++ b/lua/null-browser/api.lua @@ -129,6 +129,27 @@ function web.view.set_url(url, view_id) return __internals.view_set_url(url, vie --- ``` function web.view.set_html(html, opts) return __internals.view_set_html(html, (opts or {}).view) end +--- @class ExposeOpts +--- @field view? number View id + +--- Expose a lua function inside a view (Only works with decorations) +--- +--- @param name string Func name +--- @param action fun(table):nil Action to call when function is invoked in view +--- @param opts? ExposeOpts Options +--- +--- @example +--- ```lua +--- web.view.expose('tab_select', function(args) +--- web.view.select(tonumber(args.view)) +--- end) +--- ``` +--- then in js +--- ```js +--- __nullbrowser.tab_select({ view: 5 }) +--- ``` +function web.view.expose(name, action, opts) return __internals.view_expose(name, action, (opts or {}).view) end + --- Open devtools window for the view --- --- @param view_id? number Id of the view diff --git a/lua/null-browser/extras/tabline.lua b/lua/null-browser/extras/tabline.lua index e8edbae..b0d7761 100644 --- a/lua/null-browser/extras/tabline.lua +++ b/lua/null-browser/extras/tabline.lua @@ -27,9 +27,17 @@ function tabline.show_tabs_in_window(win_id, decoration) callback = function() if not decoration.is_enabled({ win = win_id }) then return end - web.view.set_html(tabline.tabs_html(), { - view = decoration.view({ win = win_id }), - }) + local view = decoration.view({ win = win_id }) + + web.view.expose('tab_select', function(args) + print(web.inspect(args)) + local view_id = args and args.view and tonumber(args.view) + if view_id then + web.view.select(view_id) + end + end, { view = view }) + + web.view.set_html(tabline.tabs_html(), { view = view }) end, }) end -- cgit v1.3.1