优化格式化json功能

This commit is contained in:
sugood 2022-09-25 22:01:29 +08:00
parent 4e885b3869
commit 44d0a1dade
2 changed files with 30 additions and 7 deletions

View File

@ -4,7 +4,7 @@
--- DateTime: 2020/10/24 14:13 --- DateTime: 2020/10/24 14:13
--- ---
local console = require("hs.console") local console = require("hs.console")
version = "v0.1.8" version = "v0.1.9"
configPath= "~/.hammerspoon/data/config.json" configPath= "~/.hammerspoon/data/config.json"
initConfigPath= "~/.hammerspoon/data/initConfig.json" initConfigPath= "~/.hammerspoon/data/initConfig.json"
config = {} config = {}

View File

@ -5,8 +5,11 @@
----- -----
-- --
-- --
function init() function init()
sheetView = nil
end
function initWeb()
local cscreen = hs.screen.mainScreen() local cscreen = hs.screen.mainScreen()
local cres = cscreen:fullFrame() local cres = cscreen:fullFrame()
sheetView = hs.webview.newBrowser({ sheetView = hs.webview.newBrowser({
@ -15,6 +18,7 @@ function init()
w = cres.w*0.85, w = cres.w*0.85,
h = cres.h*1.0 h = cres.h*1.0
}) })
sheetView:deleteOnClose(true)
sheetView:windowTitle("JsonFormat") sheetView:windowTitle("JsonFormat")
sheetView:windowStyle("utility") sheetView:windowStyle("utility")
sheetView:titleVisibility("hidden") sheetView:titleVisibility("hidden")
@ -23,35 +27,49 @@ function init()
sheetView:allowTextEntry(true) sheetView:allowTextEntry(true)
sheetView:closeOnEscape(true) sheetView:closeOnEscape(true)
sheetView:bringToFront(false) sheetView:bringToFront(false)
sheetView:darkMode(true) sheetView:darkMode(false)
sheetView:reload(false) sheetView:reload(false)
sheetView:shadow(true) sheetView:shadow(true)
sheetView:alpha(1) sheetView:alpha(1)
sheetView:level(hs.drawing.windowLevels.mainMenu) sheetView:level(hs.drawing.windowLevels.mainMenu) --设置窗口置顶
sheetView:url("https://i.sugood.xyz/pages/jsonweb.html") sheetView:url("https://i.sugood.xyz/pages/jsonweb.html")
sheetView:windowCallback(function(action, webview)
if action == "closing" and not pickedDuration then
sheetView = nil
end
end)
end end
--- KSheet:hide() --- KSheet:hide()
--- Method --- Method
--- Hide the JsonFormat view. --- Hide the JsonFormat view.
function hide(time) function hide(time)
sheetView:hide(time) --sheetView:hide(time)
sheetView:delete()
sheetView = nil
end end
--- KSheet:show() --- KSheet:show()
--- Method --- Method
--- Show JsonFormat. --- Show JsonFormat.
function show(time) function show(time)
initWeb()
bindCopyKey() bindCopyKey()
local str = hs.pasteboard.getContents() local str = hs.pasteboard.getContents()
-- local webcontent = generateHtml() -- local webcontent = generateHtml()
-- sheetView:html(webcontent, "http://localhost") -- sheetView:html(webcontent, "http://localhost")
-- sheetView:url("https://i.sugood.xyz/pages/jsonweb.html") -- sheetView:url("https://i.sugood.xyz/pages/jsonweb.html")
--print("窗口焦点"..hs.window.focusedWindow())
sheetView:show(time) sheetView:show(time)
hs.focus()
hs.timer.delayed.new(0.7, function() bindPasteKey() end):start()
--bindPasteKey()
end end
function toggle() function toggle()
if sheetView and sheetView:hswindow() and sheetView:hswindow():isVisible() then if sheetView ~= nil and sheetView then
hide(1) hide(1)
else else
show(1) show(1)
@ -64,7 +82,12 @@ init()
function bindCopyKey() function bindCopyKey()
hs.eventtap.keyStroke({ "cmd" }, "C") hs.eventtap.keyStroke({ "cmd" }, "C")
end end
-- 添加片段(按下快捷键时做一个复制操作,并记录复制的内容到片段列表中)
function bindPasteKey()
hs.eventtap.keyStroke({ "cmd" }, "V")
end
-- json 格式化
hs.hotkey.bind(hyperCmd, "G", function () hs.hotkey.bind(hyperCmd, "G", function ()
toggle() toggle()
end) end)