Skip to content

Commit

Permalink
don't persist changes on every key stroke
Browse files Browse the repository at this point in the history
Instead only persist when:
- changing pages
- hiding the Scratchpad
- removing focus from the textarea
  • Loading branch information
rkusa committed Oct 15, 2021
1 parent 158c9dc commit 22dea08
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions Scripts/Hooks/scratchpad-hook.lua
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ local function loadScratchpad()
end

local function savePage(path, content, override)
if path == nil then
return
end

log("saving page " .. path)
lfs.mkdir(lfs.writedir() .. [[Scratchpad\]])
local mode = "a"
Expand All @@ -88,6 +92,9 @@ local function loadScratchpad()
return
end

-- make sure current changes are persisted
savePage(currentPage, textarea:getText(), true)

local lastPage = nil
for _, page in pairs(pages) do
if currentPage == nil or (lastPage ~= nil and lastPage.path == currentPage) then
Expand All @@ -104,6 +111,13 @@ local function loadScratchpad()
end

local function prevPage()
if pagesCount == 0 then
return
end

-- make sure current changes are persisted
savePage(currentPage, textarea:getText(), true)

local lastPage = nil
for i, page in pairs(pages) do
if currentPage == nil or (page.path == currentPage and i ~= 1) then
Expand Down Expand Up @@ -294,9 +308,6 @@ local function loadScratchpad()
local lineCountAdded = textarea:getLineCount() - lineCountBefore
local line = lineEnd + lineCountAdded - 1
textarea:setSelectionNew(line, 0, line, 0)

-- persist text changes to disk
savePage(currentPage, textarea:getText(), true)
end

local function setVisible(b)
Expand Down Expand Up @@ -418,17 +429,13 @@ local function loadScratchpad()
skin.skinData.states.released[1].text.fontSize = config.fontSize
textarea:setSkin(skin)

textarea:addChangeCallback(
function(self)
savePage(currentPage, self:getText(), true)
end
)
textarea:addFocusCallback(
function(self)
if self:getFocused() then
lockKeyboardInput()
else
unlockKeyboardInput(true)
savePage(currentPage, self:getText(), true)
end
end
)
Expand All @@ -437,6 +444,7 @@ local function loadScratchpad()
if keyName == "escape" then
self:setFocused(false)
unlockKeyboardInput(true)
savePage(currentPage, self:getText(), true)
end
end
)
Expand Down

0 comments on commit 22dea08

Please sign in to comment.