@@ -7,35 +7,15 @@ local utils = require 'utils'
77local json = require ' json'
88local shifter = reqscript (' internal/journal/shifter' )
99local table_of_contents = reqscript (' internal/journal/table_of_contents' )
10+ local journal_context = reqscript (' internal/journal/journal_context' )
1011
1112local RESIZE_MIN = {w = 54 , h = 20 }
1213local TOC_RESIZE_MIN = {w = 24 }
1314
14- local JOURNAL_PERSIST_KEY = ' journal'
15-
16- local JOURNAL_WELCOME_COPY = [=[
17- Welcome to gui/journal, the chronicler's tool for Dwarf Fortress!
18-
19- Here, you can carve out notes, sketch your grand designs, or record the history of your fortress.
20- The text you write here is saved together with your fort.
21-
22- For guidance on navigation and hotkeys, tap the ? button in the upper right corner.
23- Happy digging!
24- ]=]
25-
26- local TOC_WELCOME_COPY = [=[
27- Start a line with # symbols and a space to create a header. For example:
28-
29- # My section heading
30-
31- or
32-
33- ## My section subheading
34-
35- Those headers will appear here, and you can click on them to jump to them in the text.]=]
36-
3715journal_config = journal_config or json .open (' dfhack-config/journal.json' )
3816
17+ JOURNAL_CONTEXT_MODE = journal_context .JOURNAL_CONTEXT_MODE
18+
3919JournalWindow = defclass (JournalWindow , widgets .Window )
4020JournalWindow .ATTRS {
4121 frame_title = ' DF Journal' ,
@@ -47,6 +27,9 @@ JournalWindow.ATTRS {
4727 save_layout = true ,
4828 show_tutorial = false ,
4929
30+ toc_welcome_copy = DEFAULT_NIL ,
31+ journal_welcome_copy = DEFAULT_NIL ,
32+
5033 on_text_change = DEFAULT_NIL ,
5134 on_cursor_change = DEFAULT_NIL ,
5235 on_layout_change = DEFAULT_NIL
@@ -76,7 +59,7 @@ function JournalWindow:init()
7659 widgets .WrappedLabel {
7760 view_id = ' table_of_contents_tutorial' ,
7861 frame = {l = 0 ,t = 0 ,r = 0 ,b = 3 },
79- text_to_wrap = TOC_WELCOME_COPY ,
62+ text_to_wrap = self . toc_welcome_copy or ' ' ,
8063 visible = false
8164 }
8265 }
@@ -143,7 +126,7 @@ function JournalWindow:init()
143126 widgets .WrappedLabel {
144127 view_id = ' journal_tutorial' ,
145128 frame = {l = 0 ,t = 1 ,r = 0 ,b = 0 },
146- text_to_wrap = JOURNAL_WELCOME_COPY
129+ text_to_wrap = self . journal_welcome_copy or ' '
147130 }
148131 }
149132 end
@@ -285,13 +268,17 @@ end
285268JournalScreen = defclass (JournalScreen , gui .ZScreen )
286269JournalScreen .ATTRS {
287270 focus_path = ' journal' ,
288- save_on_change = true ,
271+ context_mode = DEFAULT_NIL ,
289272 save_layout = true ,
290273 save_prefix = ' '
291274}
292275
293276function JournalScreen :init ()
294- local context = self :loadContext ()
277+ self .journal_context = journal_context .journal_context_factory (
278+ self .context_mode ,
279+ self .save_prefix
280+ )
281+ local content = self .journal_context :load_content ()
295282
296283 self :addviews {
297284 JournalWindow {
@@ -300,62 +287,45 @@ function JournalScreen:init()
300287
301288 save_layout = self .save_layout ,
302289
303- init_text = context .text [1 ],
304- init_cursor = context .cursor [1 ],
305- show_tutorial = context .show_tutorial or false ,
290+ init_text = content .text [1 ],
291+ init_cursor = content .cursor [1 ],
292+ show_tutorial = content .show_tutorial or false ,
293+
294+ toc_welcome_copy = self .journal_context :tocWelcomeCopy (),
295+ journal_welcome_copy = self .journal_context :welcomeCopy (),
306296
307- on_text_change = self :callback (' saveContext ' ),
308- on_cursor_change = self :callback (' saveContext ' )
297+ on_text_change = self :callback (' onTextChange ' ),
298+ on_cursor_change = self :callback (' onTextChange ' )
309299 },
310300 }
311301end
312302
313- function JournalScreen :loadContext ()
314- local site_data = self .save_on_change and dfhack .persistent .getSiteData (
315- self .save_prefix .. JOURNAL_PERSIST_KEY
316- ) or {}
303+ function JournalScreen :onTextChange ()
304+ local text = self .subviews .journal_editor :getText ()
305+ local cursor = self .subviews .journal_editor :getCursor ()
317306
318- if not site_data .text then
319- site_data .text = {' ' }
320- site_data .show_tutorial = true
321- end
322- site_data .cursor = site_data .cursor or {# site_data .text [1 ] + 1 }
323-
324- return site_data
325- end
326-
327- function JournalScreen :onTextChange (text )
328- self :saveContext (text )
329- end
330-
331- function JournalScreen :saveContext ()
332- if self .save_on_change and dfhack .isWorldLoaded () then
333- local text = self .subviews .journal_editor :getText ()
334- local cursor = self .subviews .journal_editor :getCursor ()
335-
336- dfhack .persistent .saveSiteData (
337- self .save_prefix .. JOURNAL_PERSIST_KEY ,
338- {text = {text }, cursor = {cursor }}
339- )
340- end
307+ self .journal_context :save_content (text , cursor )
341308end
342309
343310function JournalScreen :onDismiss ()
344311 view = nil
345312end
346313
347314function main (options )
348- if not dfhack .isMapLoaded () or not dfhack .world .isFortressMode () then
349- qerror (' journal requires a fortress map to be loaded' )
315+ if not dfhack .isMapLoaded () or (not dfhack .world .isFortressMode ()
316+ and not dfhack .world .isAdventureMode ()) then
317+ qerror (' journal requires a fortress/adventure map to be loaded' )
350318 end
351319
352320 local save_layout = options and options .save_layout
353- local save_on_change = options and options .save_on_change
321+ local overrided_context_mode = options and options .context_mode
322+ local context_mode = overrided_context_mode == nil and
323+ journal_context .detect_journal_context_mode () or overrided_context_mode
354324
355325 view = view and view :raise () or JournalScreen {
356326 save_prefix = options and options .save_prefix or ' ' ,
357327 save_layout = save_layout == nil and true or save_layout ,
358- save_on_change = save_on_change == nil and true or save_on_change ,
328+ context_mode = context_mode ,
359329 }:show ()
360330end
361331
0 commit comments