Skip to content

Commit

Permalink
Add keyboard navigation to journal table of contents
Browse files Browse the repository at this point in the history
Jump to previous/next section by ctrl+up/ctrl+down, working only
in beta 51.01+
  • Loading branch information
wiktor-obrebski committed Aug 1, 2024
1 parent 59c54ff commit e7001f4
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 12 deletions.
1 change: 1 addition & 0 deletions docs/gui/journal.rst
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ Supported Features
- If no text is selected, paste text in the cursor position
- Scrolling behaviour for long text build-in
- Table of contents (:kbd:`Ctrl` + :kbd:`O`), with headers line prefixed by '#', e.g. '# Fort history', '## Year 1'
- Table of contents navigation: jump to previous/next section by :kbd:`Ctrl` + :kbd:`Up` / :kbd:`Ctrl` + :kbd:`Down`

Usage
-----
Expand Down
36 changes: 28 additions & 8 deletions gui/journal.lua
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,31 @@ end

function JournalWindow:onInput(keys)
if (keys.A_MOVE_N_DOWN) then
print('works N')
local curr_cursor = self.subviews.journal_editor:getCursor()
local section_index, section = self.subviews.table_of_contents_panel:cursorSection(
curr_cursor
)

if section.line_cursor == curr_cursor then
self.subviews.table_of_contents_panel:setSelectedSection(
section_index - 1
)
self.subviews.table_of_contents_panel:submit()
else
self:onTableOfContentsSubmit(section_index, section)
end

return false
elseif (keys.A_MOVE_S_DOWN) then
local section_index = self.subviews.table_of_contents_panel:cursorSection(
self.subviews.journal_editor:getCursor()
)
self.subviews.table_of_contents_panel:setSelectedSection(
section_index + 1
)
self.subviews.table_of_contents_panel:submit()

return false
end
if (keys.A_MOVE_S_DOWN) then
print('works S')
end

return JournalWindow.super.onInput(self, keys)
Expand Down Expand Up @@ -223,7 +243,7 @@ function JournalWindow:postUpdateLayout()
end

function JournalWindow:onCursorChange(cursor)
local section_index, cursor_section = self.subviews.table_of_contents_panel:cursorSection(
local section_index = self.subviews.table_of_contents_panel:cursorSection(
cursor
)
self.subviews.table_of_contents_panel:setSelectedSection(section_index)
Expand All @@ -241,9 +261,9 @@ function JournalWindow:onTextChange(text)
end
end

function JournalWindow:onTableOfContentsSubmit(ind, choice)
self.subviews.journal_editor:setCursor(choice.line_cursor)
self.subviews.journal_editor:scrollToCursor(choice.line_cursor)
function JournalWindow:onTableOfContentsSubmit(ind, section)
self.subviews.journal_editor:setCursor(section.line_cursor)
self.subviews.journal_editor:scrollToCursor(section.line_cursor)
end

JournalScreen = defclass(JournalScreen, gui.ZScreen)
Expand Down
9 changes: 8 additions & 1 deletion internal/journal/table_of_contents.lua
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,14 @@ function TableOfContents:cursorSection(cursor)
end

function TableOfContents:setSelectedSection(section_index)
self.subviews.table_of_contents:setSelected(section_index)
local curr_sel = self.subviews.table_of_contents:getSelected()
if curr_sel ~= section_index then
self.subviews.table_of_contents:setSelected(section_index)
end
end

function TableOfContents:submit()
return self.subviews.table_of_contents:submit()
end

function TableOfContents:reload(text)
Expand Down
72 changes: 69 additions & 3 deletions test/gui/journal.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2954,17 +2954,83 @@ function test.table_of_contents_selection_follows_cursor()
text_area:setCursor(300)
gui_journal.view:onRender()

print(read_rendered_text(text_area))

expect.eq(toc:getSelected(), 3)


text_area:setCursor(646)
gui_journal.view:onRender()

print(read_rendered_text(text_area))
expect.eq(toc:getSelected(), 6)

journal:dismiss()
end

function test.table_of_contents_keyboard_navigation()
local journal, text_area = arrange_empty_journal({
w=100,
h=50,
allow_layout_restore=false
})

local text = table.concat({
'# Header 1',
'Lorem ipsum dolor sit amet, consectetur adipiscing elit.',
'Nulla ut lacus ut tortor semper consectetur.',
'# Header 2',
'Ut eu orci non nibh hendrerit posuere.',
'Sed euismod odio eu fringilla bibendum.',
'## Subheader 1',
'Etiam dignissim diam nec aliquet facilisis.',
'Integer tristique purus at tellus luctus, vel aliquet sapien sollicitudin.',
'## Subheader 2',
'Fusce ornare est vitae urna feugiat, vel interdum quam vestibulum.',
'10: Vivamus id felis scelerisque, lobortis diam ut, mollis nisi.',
'### Subsubheader 1',
'# Header 3',
'Donec quis lectus ac erat placerat eleifend.',
'Aenean non orci id erat malesuada pharetra.',
'Nunc in lectus et metus finibus venenatis.',
}, '\n')

simulate_input_text(text)

simulate_input_keys('CUSTOM_CTRL_O')

local toc = journal.subviews.table_of_contents

text_area:setCursor(5)
gui_journal.view:onRender()

simulate_input_keys('A_MOVE_N_DOWN')

expect.eq(toc:getSelected(), 1)

simulate_input_keys('A_MOVE_N_DOWN')

expect.eq(toc:getSelected(), 6)

simulate_input_keys('A_MOVE_N_DOWN')
simulate_input_keys('A_MOVE_N_DOWN')

expect.eq(toc:getSelected(), 4)

simulate_input_keys('A_MOVE_S_DOWN')

expect.eq(toc:getSelected(), 5)

simulate_input_keys('A_MOVE_S_DOWN')
simulate_input_keys('A_MOVE_S_DOWN')
simulate_input_keys('A_MOVE_S_DOWN')

expect.eq(toc:getSelected(), 2)


text_area:setCursor(250)
gui_journal.view:onRender()

simulate_input_keys('A_MOVE_N_DOWN')

expect.eq(toc:getSelected(), 3)

journal:dismiss()
end

0 comments on commit e7001f4

Please sign in to comment.