优化懒加载触发机制

This commit is contained in:
captain 2026-05-07 17:04:26 +08:00
parent ee08df6765
commit 1cb5843449
11 changed files with 94 additions and 63 deletions

View file

@ -8,30 +8,38 @@ local P = {
PackUtils.setup_listener(P.name, P.build_cmd)
-- app = { "zen", "-private-window" },
-- app = { "firefox-esr", "-private-window" },
-- 在插件未加载时,这些命令就存在了。一旦被调用,它们会先加载插件,再执行真正的功能。
vim.api.nvim_create_user_command("PeekToggle", function()
local peek = require("peek")
if not peek.is_open() and vim.bo[vim.api.nvim_get_current_buf()].filetype == 'markdown' then
PackUtils.load(P, function()
local app = { "chromium", "--app=http://localhost:9000/?theme=dark", "--incognito" }
if IS_ARM then
app = { "chromium", "--no-sandbox", "--app=http://localhost:9000/?theme=dark", "--incognito", "--test-type",
"--force-device-scale-factor=1.75" }
end
if vim.fn.has("wsl") == 1 then
app = { "/mnt/d/my_program/chrome-win/chrome.exe", "--no-sandbox", "--app=http://localhost:9000/?theme=dark", "--incognito", "--test-type" }
end
require("peek").setup({
port = 9000,
-- app = { "zen", "-private-window" },
-- app = { "firefox-esr", "-private-window" },
app = app
})
end)
peek.open()
else -- 只有加载了插件的情况下执行关闭这样可以跳过非markdown文件
if PackUtils.plugin_loaded[P.name] then
local is_markdown = vim.bo[vim.api.nvim_get_current_buf()].filetype == 'markdown'
if not PackUtils.plugin_loaded[P.name] then
if is_markdown then
PackUtils.load(P, function()
-- 动态决定浏览器配置
local app = { "chromium", "--app=http://localhost:9000/?theme=dark", "--incognito" }
if IS_ARM then
app = { "chromium", "--no-sandbox", "--app=http://localhost:9000/?theme=dark", "--incognito", "--test-type",
"--force-device-scale-factor=1.75" }
elseif vim.fn.has("wsl") == 1 then
app = { "/mnt/d/my_program/chrome-win/chrome.exe", "--no-sandbox", "--app=http://localhost:9000/?theme=dark",
"--incognito", "--test-type" }
end
local peek = require("peek")
peek.setup({
port = 9000,
app = app
})
peek.open()
end)
end
else
local peek = require("peek")
if peek.is_open() then
peek.close()
elseif is_markdown then
peek.open()
end
end
end, { desc = "Lazy load and open Peek" })