Skip to content

Commit

Permalink
refactor: drag and drop options (#59)
Browse files Browse the repository at this point in the history
* feat: allow drag and drop to override any opts

* docs: update config options

* chore: update options

* fix(tests): remove redundant checks
  • Loading branch information
HakonHarnes authored Apr 1, 2024
1 parent ba847e5 commit 2e581be
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 25 deletions.
9 changes: 3 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,12 @@ The plugin comes with the following defaults:
embed_image_as_base64 = false, -- paste image as base64 string instead of saving to file
max_base64_size = 10, -- max size of base64 string in KB
template = "$FILE_PATH", -- default template
copy_images = false, -- copy images instead of using the original file
download_images = true, -- download images and save them to dir_path instead of using the URL

drag_and_drop = {
enabled = true, -- enable drag and drop mode
insert_mode = false, -- enable drag and drop in insert mode
copy_images = false, -- copy images instead of using the original file
download_images = true, -- download images and save them to dir_path instead of using the URL
},
},

Expand All @@ -111,10 +111,7 @@ The plugin comes with the following defaults:
markdown = {
url_encode_path = true,
template = "![$CURSOR]($FILE_PATH)",

drag_and_drop = {
download_images = false,
},
download_images = false,
},

html = {
Expand Down
19 changes: 13 additions & 6 deletions lua/img-clip/config.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
local M = {}

M.drag_and_drop = false
M.config_file = "Default"
M.sorted_files = {}
M.sorted_dirs = {}
Expand All @@ -23,12 +24,12 @@ local defaults = {
embed_image_as_base64 = false, -- paste image as base64 string instead of saving to file
max_base64_size = 10, -- max size of base64 string in KB
template = "$FILE_PATH", -- default template
copy_images = false, -- copy images instead of using the original file
download_images = true, -- download images and save them to dir_path instead of using the URL

drag_and_drop = {
enabled = true, -- enable drag and drop mode
insert_mode = false, -- enable drag and drop in insert mode
copy_images = false, -- copy images instead of using the original file
download_images = true, -- download images and save them to dir_path instead of using the URL
},
},

Expand All @@ -41,10 +42,7 @@ local defaults = {
markdown = {
url_encode_path = true,
template = "![$CURSOR]($FILE_PATH)",

drag_and_drop = {
download_images = false,
},
download_images = false,
},

html = {
Expand Down Expand Up @@ -292,6 +290,15 @@ M.get_opt = function(key, args, opts)
return M.get_opt(key, args, M.get_config())
end

-- if we're in drag and drop mode, try to get drag and drop options
-- and then fall back to the regular options if they're not found
if M.drag_and_drop and not key:find("drag_and_drop") then
local val = M.get_opt("drag_and_drop." .. key, args, M.opts)
if val ~= nil then
return val
end
end

local val = get_custom_opt(key, opts, args)
if val == nil then
val = get_file_opt(key, opts, args, vim.fn.expand("%:p"))
Expand Down
4 changes: 2 additions & 2 deletions lua/img-clip/paste.lua
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ end

---@param url string
M.paste_image_from_url = function(url)
if not config.get_opt("drag_and_drop.download_images") then
if not config.get_opt("download_images") then
if not markup.insert_markup(url) then
util.error("Could not insert markup code.")
return false
Expand Down Expand Up @@ -103,7 +103,7 @@ M.paste_image_from_path = function(src_path)
end
end

if not config.get_opt("drag_and_drop.copy_images") then
if not config.get_opt("copy_images") then
if not markup.insert_markup(src_path, true) then
util.error("Could not insert markup code.")
return false
Expand Down
10 changes: 8 additions & 2 deletions plugin/img-clip.lua
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,15 @@ vim.paste = (function(original)
end

util.verbose = false
if not plugin.pasteImage({}, line) then
config.drag_and_drop = true

local ok = plugin.pasteImage({}, line)

config.drag_and_drop = false
util.verbose = true

if not ok then
debug.log("Did not handle paste, calling original vim.paste")
util.verbose = true
return original(lines, phase) -- if we did not handle the paste, call the original vim.paste function
end
end
Expand Down
3 changes: 0 additions & 3 deletions tests/config_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,10 @@ describe("config", function()

assert.is_true(config.get_opt("drag_and_drop.enabled"))
assert.is_false(config.get_opt("drag_and_drop.insert_mode"))
assert.is_false(config.get_opt("drag_and_drop.copy_images"))
assert.is_true(config.get_opt("drag_and_drop.download_images"))

vim.bo.filetype = "markdown"
assert.is_true(config.get_opt("url_encode_path"))
assert.equals("![$CURSOR]($FILE_PATH)", config.get_opt("template"))
assert.is_false(config.get_opt("drag_and_drop.download_images"))
end)

it("should allow overriding default values", function()
Expand Down
9 changes: 3 additions & 6 deletions vimdoc.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,12 @@ The plugin comes with the following defaults:
embed_image_as_base64 = false, -- paste image as base64 string instead of saving to file
max_base64_size = 10, -- max size of base64 string in KB
template = "$FILE_PATH", -- default template
copy_images = false, -- copy images instead of using the original file
download_images = true, -- download images and save them to dir_path instead of using the URL

drag_and_drop = {
enabled = true, -- enable drag and drop mode
insert_mode = false, -- enable drag and drop in insert mode
copy_images = false, -- copy images instead of using the original file
download_images = true, -- download images and save them to dir_path instead of using the URL
},
},

Expand All @@ -102,10 +102,7 @@ The plugin comes with the following defaults:
markdown = {
url_encode_path = true,
template = "![$CURSOR]($FILE_PATH)",

drag_and_drop = {
download_images = false,
},
download_images = false,
},

html = {
Expand Down

0 comments on commit 2e581be

Please sign in to comment.