-
-
Notifications
You must be signed in to change notification settings - Fork 214
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ref(calendar): refactored view outside of calendar module (#815)
Co-authored-by: Andrea <andrea@DESKTOP-RPJDAO0.localdomain>
- Loading branch information
Showing
2 changed files
with
204 additions
and
152 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
-- NOTICE: Consider this whole module a demo for now | ||
-- A lot of stuff is hardcoded in order to prove that it can work | ||
|
||
local module = neorg.modules.create("core.ui.calendar") | ||
|
||
module.setup = function() | ||
return { | ||
requires = { | ||
"core.ui", | ||
"core.ui.calendar.views.monthly" | ||
}, | ||
} | ||
end | ||
|
||
module.private = { | ||
|
||
modes = {}, | ||
views = {}, | ||
|
||
get_mode = function(name, callback) | ||
if module.private.modes[name] ~= nil then | ||
local cur_mode = module.private.modes[name](callback) | ||
cur_mode.name = name | ||
return cur_mode | ||
end | ||
|
||
print("Error: mode not set or not available") | ||
end, | ||
|
||
get_view = function(name) | ||
if module.private.views[name] ~= nil then | ||
return module.private.views[name] | ||
end | ||
|
||
print("Error: view not set or not available") | ||
end, | ||
|
||
extract_ui_info = function (buffer, window) | ||
local width = vim.api.nvim_win_get_width(window) | ||
local height = vim.api.nvim_win_get_height(window) | ||
|
||
local half_width = math.floor(width / 2) | ||
local half_height = math.floor(height / 2) | ||
|
||
return { | ||
window = window, | ||
buffer = buffer, | ||
width = width, | ||
height = height, | ||
half_width = half_width, | ||
half_height = half_height, | ||
} | ||
end, | ||
|
||
open_window = function (options) | ||
local buffer, window = module.required["core.ui"].create_split( | ||
"calendar", | ||
{}, | ||
options.height or math.floor(vim.opt.lines:get() * 0.3) | ||
) | ||
|
||
return buffer, window | ||
end | ||
} | ||
|
||
module.public = { | ||
add_mode = function(name, factory) | ||
module.private.modes[name] = factory | ||
end, | ||
|
||
add_view = function(name, details) | ||
module.private.views[name] = details | ||
end, | ||
|
||
create_calendar = function(buffer, window, options) | ||
local callback_and_close = function (result) | ||
if options.callback ~= nil then | ||
options.callback(result) | ||
end | ||
|
||
module.required['core.ui'].delete_window(buffer) | ||
end | ||
|
||
local mode = module.private.get_mode(options.mode, callback_and_close) | ||
if mode == nil then | ||
return | ||
end | ||
|
||
local ui_info = module.private.extract_ui_info(buffer, window) | ||
|
||
local view = module.private.get_view(options.view or "monthly") | ||
|
||
view.setup(ui_info, mode, options) | ||
end, | ||
|
||
open = function(options) | ||
local buffer, window = module.private.open_window(options) | ||
|
||
options.mode = "standalone" | ||
|
||
return module.public.create_calendar(buffer, window, options) | ||
end, | ||
|
||
select_date = function(options) | ||
local buffer, window = module.private.open_window(options) | ||
|
||
options.mode = "select_date" | ||
|
||
return module.public.create_calendar(buffer, window, options) | ||
end, | ||
|
||
select_date_range = function(options) | ||
local buffer, window = module.private.open_window(options) | ||
|
||
options.mode = "select_range" | ||
|
||
return module.public.create_calendar(buffer, window, options) | ||
end, | ||
} | ||
|
||
module.load = function() | ||
-- Add default calendar modes | ||
module.public.add_mode("standalone", function(_) | ||
return {} | ||
end) | ||
|
||
module.public.add_mode("select_date", function(callback) | ||
return { | ||
on_select = function(_, date) | ||
if callback then | ||
callback(date) | ||
end | ||
return false | ||
end, | ||
} | ||
end) | ||
|
||
module.public.add_mode("select_range", function(callback) | ||
return { | ||
range_start = nil, | ||
range_end = nil, | ||
|
||
on_select = function(self, date) | ||
if not self.range_start then | ||
self.range_start = date | ||
return true | ||
else | ||
if os.time(date) <= os.time(self.range_start) then | ||
print("Error: you should choose a date that is after the starting day.") | ||
return false | ||
end | ||
|
||
self.range_end = date | ||
callback({ self.range_start, self.range_end }) | ||
return false | ||
end | ||
end, | ||
|
||
get_day_highlight = function(self, date, default_highlight) | ||
if self.range_start ~= nil then | ||
if os.time(date) < os.time(self.range_start) then | ||
return "@comment" | ||
end | ||
end | ||
return default_highlight | ||
end, | ||
} | ||
end) | ||
end | ||
|
||
return module |
Oops, something went wrong.