From 2c5c64a35f63aa649622ef3cfdce473e58f2a0f6 Mon Sep 17 00:00:00 2001 From: Tessa Kelly Date: Tue, 13 Sep 2022 13:45:10 -0600 Subject: [PATCH] Support sentence level segments in addition to word-level segments --- styleguide-app/Examples/Highlighter.elm | 45 +++++++++++++++++++++---- 1 file changed, 39 insertions(+), 6 deletions(-) diff --git a/styleguide-app/Examples/Highlighter.elm b/styleguide-app/Examples/Highlighter.elm index 30920f9f..4da06b84 100644 --- a/styleguide-app/Examples/Highlighter.elm +++ b/styleguide-app/Examples/Highlighter.elm @@ -215,29 +215,62 @@ init = controlSettings in { settings = settings - , highlighter = - initHighlighter (Control.currentValue settings) - (Highlightable.initFragments Nothing CommonControls.romeoAndJulietQuotation) + , highlighter = initHighlighter (Control.currentValue settings) [] } initHighlighter : Settings -> List (Highlightable ()) -> Highlighter.Model () -initHighlighter settings highlightables = +initHighlighter settings previousHighlightables = + let + highlightables = + if settings.splitOnSentences then + let + segments = + String.split "." CommonControls.romeoAndJulietQuotation + + segmentCount = + List.length segments + in + List.indexedMap + (\index sentence -> + Highlightable.init Highlightable.Interactive + Nothing + index + ( [] + , if (index + 1) == segmentCount then + sentence + + else + sentence ++ "." + ) + ) + segments + + else + Highlightable.initFragments Nothing CommonControls.romeoAndJulietQuotation + in Highlighter.init { id = "example-romeo-and-juliet" - , highlightables = highlightables + , highlightables = + if List.map .text previousHighlightables == List.map .text highlightables then + previousHighlightables + + else + highlightables , marker = settings.tool } type alias Settings = - { tool : Tool.Tool () + { splitOnSentences : Bool + , tool : Tool.Tool () } controlSettings : Control Settings controlSettings = Control.record Settings + |> Control.field "splitOnSentences" (Control.bool False) |> Control.field "tool" (Control.choice [ ( "Marker", Control.map Tool.Marker controlMarker )