diff options
| author | Akshay Nair <phenax5@gmail.com> | 2025-07-26 16:59:35 +0530 |
|---|---|---|
| committer | Akshay Nair <phenax5@gmail.com> | 2025-07-26 16:59:35 +0530 |
| commit | b0f598910f1f8bc8db6b65a0f01b1950ed7c3841 (patch) | |
| tree | 234758ed1c91238b9498fdc6dd4bcccc42bddc36 /lua | |
| parent | 8ab1f8d796d8f35a65e38fdd03d51824a41d1dab (diff) | |
| download | null-browser-b0f598910f1f8bc8db6b65a0f01b1950ed7c3841.tar.gz null-browser-b0f598910f1f8bc8db6b65a0f01b1950ed7c3841.zip | |
Add web.view.expose for simple lua->js interop in decorations
Diffstat (limited to '')
| -rw-r--r-- | lua/null-browser/api.lua | 21 | ||||
| -rw-r--r-- | lua/null-browser/extras/tabline.lua | 14 |
2 files changed, 32 insertions, 3 deletions
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 |
