From 44d0a1dadefbd7365a95befc735f37ffb2fb00f2 Mon Sep 17 00:00:00 2001 From: sugood <15820258199@163.com> Date: Sun, 25 Sep 2022 22:01:29 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=A0=BC=E5=BC=8F=E5=8C=96js?= =?UTF-8?q?on=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- modules/commons.lua | 2 +- modules/jsonFormat.lua | 35 +++++++++++++++++++++++++++++------ 2 files changed, 30 insertions(+), 7 deletions(-) diff --git a/modules/commons.lua b/modules/commons.lua index 49a0531..99bb824 100644 --- a/modules/commons.lua +++ b/modules/commons.lua @@ -4,7 +4,7 @@ --- DateTime: 2020/10/24 14:13 --- local console = require("hs.console") -version = "v0.1.8" +version = "v0.1.9" configPath= "~/.hammerspoon/data/config.json" initConfigPath= "~/.hammerspoon/data/initConfig.json" config = {} diff --git a/modules/jsonFormat.lua b/modules/jsonFormat.lua index 0375097..7cbfc2b 100644 --- a/modules/jsonFormat.lua +++ b/modules/jsonFormat.lua @@ -5,8 +5,11 @@ ----- -- -- - function init() + sheetView = nil +end + +function initWeb() local cscreen = hs.screen.mainScreen() local cres = cscreen:fullFrame() sheetView = hs.webview.newBrowser({ @@ -15,6 +18,7 @@ function init() w = cres.w*0.85, h = cres.h*1.0 }) + sheetView:deleteOnClose(true) sheetView:windowTitle("JsonFormat") sheetView:windowStyle("utility") sheetView:titleVisibility("hidden") @@ -23,35 +27,49 @@ function init() sheetView:allowTextEntry(true) sheetView:closeOnEscape(true) sheetView:bringToFront(false) - sheetView:darkMode(true) + sheetView:darkMode(false) sheetView:reload(false) sheetView:shadow(true) 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:windowCallback(function(action, webview) + if action == "closing" and not pickedDuration then + sheetView = nil + end + end) end --- KSheet:hide() --- Method --- Hide the JsonFormat view. function hide(time) - sheetView:hide(time) + --sheetView:hide(time) + sheetView:delete() + + sheetView = nil end --- KSheet:show() --- Method --- Show JsonFormat. function show(time) + initWeb() bindCopyKey() local str = hs.pasteboard.getContents() -- local webcontent = generateHtml() -- sheetView:html(webcontent, "http://localhost") -- sheetView:url("https://i.sugood.xyz/pages/jsonweb.html") + + --print("窗口焦点"..hs.window.focusedWindow()) sheetView:show(time) + hs.focus() + hs.timer.delayed.new(0.7, function() bindPasteKey() end):start() + --bindPasteKey() end function toggle() - if sheetView and sheetView:hswindow() and sheetView:hswindow():isVisible() then + if sheetView ~= nil and sheetView then hide(1) else show(1) @@ -64,7 +82,12 @@ init() function bindCopyKey() hs.eventtap.keyStroke({ "cmd" }, "C") end --- 添加片段(按下快捷键时做一个复制操作,并记录复制的内容到片段列表中) + +function bindPasteKey() + hs.eventtap.keyStroke({ "cmd" }, "V") +end + +-- json 格式化 hs.hotkey.bind(hyperCmd, "G", function () toggle() end)