@charmland/gem 0.0.0-stage → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +177 -0
- package/NOTICE +13 -0
- package/README.md +24 -2
- package/assets/fonts/Hypography-Italic.woff2 +0 -0
- package/assets/fonts/Hypography-Regular.woff2 +0 -0
- package/assets/fonts/PPFraktionMono-Variable.ttf +0 -0
- package/assets/fonts/PPFraktionMonoItalic-Variable.ttf +0 -0
- package/assets/fonts/PPFraktionSans-Variable.ttf +0 -0
- package/assets/fonts/PPFraktionSansItalic-Variable.ttf +0 -0
- package/bin/gem-elm.js +3 -0
- package/dist/browser/boot.d.ts +10 -0
- package/dist/browser/boot.js +14 -0
- package/dist/browser/color-scheme.d.ts +9 -0
- package/dist/browser/color-scheme.js +76 -0
- package/dist/browser/ports.d.ts +37 -0
- package/dist/browser/ports.js +90 -0
- package/dist/browser/router.d.ts +5 -0
- package/dist/browser/router.js +35 -0
- package/dist/browser/runtime.d.ts +12 -0
- package/dist/browser/runtime.js +22 -0
- package/dist/browser/tabs.d.ts +9 -0
- package/dist/browser/tabs.js +69 -0
- package/dist/default/theme.css +4664 -0
- package/dist/default/theme.js +13815 -0
- package/dist/node/build.d.ts +16 -0
- package/dist/node/build.js +48 -0
- package/dist/node/cli.d.ts +2 -0
- package/dist/node/cli.js +21 -0
- package/elm.json +28 -0
- package/package.json +86 -4
- package/src/app/Gem/App.elm +1268 -0
- package/src/app/Gem/CharmSticker.elm +35 -0
- package/src/app/Gem/Components.elm +292 -0
- package/src/app/Gem/Main.elm +17 -0
- package/src/app/Gem/Ports.elm +32 -0
- package/src/app/Gem/Shell.elm +957 -0
- package/src/browser/theme.d.ts +1 -0
- package/src/core/Gem/Node.elm +219 -0
- package/src/core/Gem/Page.elm +560 -0
- package/src/core/Gem/Render.elm +510 -0
- package/src/core/Gem/Slots.elm +11 -0
- package/src/styles/docs.scss +1083 -0
- package/src/styles/fonts.scss +51 -0
- package/src/styles/gem.scss +362 -0
- package/src/styles/theme.scss +8 -0
- package/src/styles/ui/burger.scss +272 -0
- package/src/styles/ui/button.scss +350 -0
- package/src/styles/ui/charmtone.scss +70 -0
- package/src/styles/ui/controls-extra.scss +316 -0
- package/src/styles/ui/controls.scss +110 -0
- package/src/styles/ui/display.scss +359 -0
- package/src/styles/ui/dropdown.scss +341 -0
- package/src/styles/ui/form.scss +314 -0
- package/src/styles/ui/menu.scss +200 -0
- package/src/styles/ui/meter.scss +124 -0
- package/src/styles/ui/modal.scss +545 -0
- package/src/styles/ui/oauth.scss +50 -0
- package/src/styles/ui/page-nav.scss +63 -0
- package/src/styles/ui/reset.scss +111 -0
- package/src/styles/ui/scrollbar.scss +39 -0
- package/src/styles/ui/search.scss +164 -0
- package/src/styles/ui/sidebar.scss +229 -0
- package/src/styles/ui/squircle.scss +60 -0
- package/src/styles/ui/table.scss +97 -0
- package/src/styles/ui/theme.scss +132 -0
- package/src/styles/ui/toast.scss +587 -0
- package/src/styles/ui/toc.scss +128 -0
- package/src/styles/ui/typography.scss +21 -0
- package/src/styles/ui/ui.scss +33 -0
- package/src/ui/Ui/Badge.elm +51 -0
- package/src/ui/Ui/Banner.elm +97 -0
- package/src/ui/Ui/Burger.elm +259 -0
- package/src/ui/Ui/Button.elm +261 -0
- package/src/ui/Ui/Card.elm +32 -0
- package/src/ui/Ui/Cards.elm +15 -0
- package/src/ui/Ui/ChoiceGroup.elm +62 -0
- package/src/ui/Ui/CopyButton.elm +139 -0
- package/src/ui/Ui/Divider.elm +16 -0
- package/src/ui/Ui/Dropdown.elm +600 -0
- package/src/ui/Ui/Empty.elm +41 -0
- package/src/ui/Ui/ErrorBubble.elm +34 -0
- package/src/ui/Ui/Field.elm +45 -0
- package/src/ui/Ui/FormStatus.elm +32 -0
- package/src/ui/Ui/Icon.elm +381 -0
- package/src/ui/Ui/InlineForm.elm +51 -0
- package/src/ui/Ui/Input.elm +113 -0
- package/src/ui/Ui/KeyValue.elm +25 -0
- package/src/ui/Ui/ListRow.elm +67 -0
- package/src/ui/Ui/Loader.elm +32 -0
- package/src/ui/Ui/Logo/Charm.elm +21 -0
- package/src/ui/Ui/Logo/CharmMark.elm +32 -0
- package/src/ui/Ui/Logo/CharmSticker.elm +35 -0
- package/src/ui/Ui/Logo/Discord.elm +26 -0
- package/src/ui/Ui/Logo/GitHub.elm +21 -0
- package/src/ui/Ui/Logo/Google.elm +21 -0
- package/src/ui/Ui/Logo/Hexy.elm +29 -0
- package/src/ui/Ui/Logo/Hyper.elm +42 -0
- package/src/ui/Ui/Logo/Integration.elm +117 -0
- package/src/ui/Ui/Logo/Prism.elm +35 -0
- package/src/ui/Ui/Logos.elm +115 -0
- package/src/ui/Ui/Menu.elm +348 -0
- package/src/ui/Ui/Meter.elm +132 -0
- package/src/ui/Ui/Modal.elm +247 -0
- package/src/ui/Ui/Motion.elm +32 -0
- package/src/ui/Ui/NumberBox.elm +86 -0
- package/src/ui/Ui/OAuthButton.elm +39 -0
- package/src/ui/Ui/PageNav.elm +65 -0
- package/src/ui/Ui/Pager.elm +49 -0
- package/src/ui/Ui/Search.elm +396 -0
- package/src/ui/Ui/SearchBox.elm +78 -0
- package/src/ui/Ui/Section.elm +21 -0
- package/src/ui/Ui/Sidebar.elm +239 -0
- package/src/ui/Ui/Stat.elm +29 -0
- package/src/ui/Ui/Switch.elm +34 -0
- package/src/ui/Ui/Table.elm +89 -0
- package/src/ui/Ui/Tabs.elm +280 -0
- package/src/ui/Ui/ToC.elm +120 -0
- package/src/ui/Ui/Toast.elm +1145 -0
- package/src/ui/Ui/Toolbar.elm +31 -0
- package/src/ui/Ui/TwoStepConfirm.elm +159 -0
- package/src/ui/Ui/Typography.elm +33 -0
|
@@ -0,0 +1,1268 @@
|
|
|
1
|
+
module Gem.App exposing (Model, Msg(..), app)
|
|
2
|
+
|
|
3
|
+
{-| The default application: decodes the site document, maps a URL to a page
|
|
4
|
+
and owns the client state the shell's components need. Everything visual
|
|
5
|
+
lives in `Gem.Shell`.
|
|
6
|
+
-}
|
|
7
|
+
|
|
8
|
+
import Browser
|
|
9
|
+
import Browser.Dom as Dom
|
|
10
|
+
import Browser.Events
|
|
11
|
+
import Browser.Navigation as Nav
|
|
12
|
+
import Dict
|
|
13
|
+
import Gem.Slots exposing (Slots)
|
|
14
|
+
import Gem.Page as Page exposing (Page, Site)
|
|
15
|
+
import Gem.Ports as Ports
|
|
16
|
+
import Gem.Render as Render
|
|
17
|
+
import Gem.Shell as Shell
|
|
18
|
+
import Html exposing (Html)
|
|
19
|
+
import Html.Attributes as A
|
|
20
|
+
import Json.Decode as D
|
|
21
|
+
import Process
|
|
22
|
+
import Task
|
|
23
|
+
import Ui.Burger
|
|
24
|
+
import Ui.Button
|
|
25
|
+
import Ui.CopyButton
|
|
26
|
+
import Ui.Dropdown
|
|
27
|
+
import Ui.Search
|
|
28
|
+
import Ui.Tabs
|
|
29
|
+
import Url exposing (Url)
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
type alias Model =
|
|
33
|
+
{ site : Site
|
|
34
|
+
, page : Maybe Page
|
|
35
|
+
, url : Url
|
|
36
|
+
, query : String
|
|
37
|
+
, search : Ui.Search.Model
|
|
38
|
+
, searchOpen : Bool
|
|
39
|
+
, burger : Ui.Burger.Model
|
|
40
|
+
, copy : Ui.CopyButton.Model
|
|
41
|
+
, pageCopied : Bool
|
|
42
|
+
, collapsed : List String
|
|
43
|
+
, key : Nav.Key
|
|
44
|
+
, error : Maybe String
|
|
45
|
+
, theme : String
|
|
46
|
+
, themeTabs : Ui.Tabs.Model String
|
|
47
|
+
, menuTabs : Ui.Tabs.Model String
|
|
48
|
+
, aiOpen : Bool
|
|
49
|
+
, aiFlip : Bool
|
|
50
|
+
, scrollY : Float
|
|
51
|
+
, headerHidden : Bool
|
|
52
|
+
, tocOpen : Bool
|
|
53
|
+
, activeSection : Maybe String
|
|
54
|
+
, measurements : Maybe Measurements
|
|
55
|
+
, doc : Page.Doc
|
|
56
|
+
, docsTabs : Ui.Tabs.Model String
|
|
57
|
+
, menuDocsTabs : Ui.Tabs.Model String
|
|
58
|
+
, keepBurgerOpen : Bool
|
|
59
|
+
, index : List Hit
|
|
60
|
+
, slots : Slots Msg
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
type Msg
|
|
65
|
+
= ClickedLink Browser.UrlRequest
|
|
66
|
+
| ChangedUrl Url
|
|
67
|
+
| QueryChanged String
|
|
68
|
+
| QueryCleared
|
|
69
|
+
| SearchMsg (Ui.Search.Msg String)
|
|
70
|
+
| SearchOpened
|
|
71
|
+
| SearchClosed
|
|
72
|
+
| GroupToggled String
|
|
73
|
+
| BurgerMsg Ui.Burger.Msg
|
|
74
|
+
| CopyMsg Ui.CopyButton.Msg
|
|
75
|
+
| CopyPage
|
|
76
|
+
| PageCopyReset
|
|
77
|
+
| DocsTabsMsg (Ui.Tabs.Msg String)
|
|
78
|
+
| MenuDocsTabsMsg (Ui.Tabs.Msg String)
|
|
79
|
+
| ThemeTabsMsg (Ui.Tabs.Msg String)
|
|
80
|
+
| MenuTabsMsg (Ui.Tabs.Msg String)
|
|
81
|
+
| AiToggled
|
|
82
|
+
| AiFlipped Bool
|
|
83
|
+
| AiPicked String
|
|
84
|
+
| Scrolled Float
|
|
85
|
+
| TocToggled
|
|
86
|
+
| AnchorsMeasured Measurements
|
|
87
|
+
| SearchShortcut
|
|
88
|
+
| ScrolledTo (Result Dom.Error ())
|
|
89
|
+
| Noop
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
app : Slots Msg -> Program D.Value Model Msg
|
|
93
|
+
app slots =
|
|
94
|
+
Browser.application
|
|
95
|
+
{ init = init slots
|
|
96
|
+
, view = view
|
|
97
|
+
, update = update
|
|
98
|
+
, subscriptions = subscriptions
|
|
99
|
+
, onUrlRequest = ClickedLink
|
|
100
|
+
, onUrlChange = ChangedUrl
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
init : Slots Msg -> D.Value -> Url -> Nav.Key -> ( Model, Cmd Msg )
|
|
105
|
+
init slots flags url key =
|
|
106
|
+
case D.decodeValue Page.site flags of
|
|
107
|
+
Ok site ->
|
|
108
|
+
let
|
|
109
|
+
page =
|
|
110
|
+
Page.byRoute site (Page.routeOf site url.path)
|
|
111
|
+
|
|
112
|
+
doc =
|
|
113
|
+
Page.docFor site (Page.routeOf site url.path)
|
|
114
|
+
|
|
115
|
+
model =
|
|
116
|
+
{ site = site
|
|
117
|
+
, page = page
|
|
118
|
+
, url = url
|
|
119
|
+
, query = ""
|
|
120
|
+
, search = Ui.Search.init "gem-search"
|
|
121
|
+
, searchOpen = False
|
|
122
|
+
, burger = Ui.Burger.init
|
|
123
|
+
, copy = Ui.CopyButton.init
|
|
124
|
+
, pageCopied = False
|
|
125
|
+
, collapsed = []
|
|
126
|
+
, key = key
|
|
127
|
+
, error = Nothing
|
|
128
|
+
, theme = site.theme
|
|
129
|
+
, themeTabs =
|
|
130
|
+
Ui.Tabs.init "docs-theme-tabs" (themeIndex site.themeSystem site.theme) site.theme
|
|
131
|
+
, menuTabs =
|
|
132
|
+
Ui.Tabs.init "burger-theme-tabs" (themeIndex site.themeSystem site.theme) site.theme
|
|
133
|
+
, aiOpen = False
|
|
134
|
+
, aiFlip = False
|
|
135
|
+
, scrollY = 0
|
|
136
|
+
, headerHidden = False
|
|
137
|
+
, tocOpen = False
|
|
138
|
+
, activeSection = Nothing
|
|
139
|
+
, measurements = Nothing
|
|
140
|
+
, doc = doc
|
|
141
|
+
, docsTabs = Ui.Tabs.init "docs-tabs" (docsIndex site doc.id) doc.id
|
|
142
|
+
, menuDocsTabs =
|
|
143
|
+
Ui.Tabs.init "burger-docs-tabs" (docsIndex site doc.id) doc.id
|
|
144
|
+
, keepBurgerOpen = False
|
|
145
|
+
, index = siteIndex site
|
|
146
|
+
, slots = slots
|
|
147
|
+
}
|
|
148
|
+
in
|
|
149
|
+
( model
|
|
150
|
+
, Cmd.batch
|
|
151
|
+
[ scrollTo url
|
|
152
|
+
, measureAnchors (pageAnchors page)
|
|
153
|
+
, setMeta model
|
|
154
|
+
]
|
|
155
|
+
)
|
|
156
|
+
|
|
157
|
+
Err error ->
|
|
158
|
+
( { site = emptySite
|
|
159
|
+
, page = Nothing
|
|
160
|
+
, url = url
|
|
161
|
+
, query = ""
|
|
162
|
+
, search = Ui.Search.init "gem-search"
|
|
163
|
+
, searchOpen = False
|
|
164
|
+
, burger = Ui.Burger.init
|
|
165
|
+
, copy = Ui.CopyButton.init
|
|
166
|
+
, pageCopied = False
|
|
167
|
+
, collapsed = []
|
|
168
|
+
, key = key
|
|
169
|
+
, error = Just (D.errorToString error)
|
|
170
|
+
, theme = "dark"
|
|
171
|
+
, themeTabs = Ui.Tabs.init "docs-theme-tabs" 1 "dark"
|
|
172
|
+
, menuTabs = Ui.Tabs.init "burger-theme-tabs" 1 "dark"
|
|
173
|
+
, aiOpen = False
|
|
174
|
+
, aiFlip = False
|
|
175
|
+
, scrollY = 0
|
|
176
|
+
, headerHidden = False
|
|
177
|
+
, tocOpen = False
|
|
178
|
+
, activeSection = Nothing
|
|
179
|
+
, measurements = Nothing
|
|
180
|
+
, doc = Page.docFor emptySite "/"
|
|
181
|
+
, docsTabs = Ui.Tabs.init "docs-tabs" 0 "docs"
|
|
182
|
+
, menuDocsTabs = Ui.Tabs.init "burger-docs-tabs" 0 "docs"
|
|
183
|
+
, keepBurgerOpen = False
|
|
184
|
+
, index = []
|
|
185
|
+
, slots = slots
|
|
186
|
+
}
|
|
187
|
+
, Cmd.none
|
|
188
|
+
)
|
|
189
|
+
|
|
190
|
+
|
|
191
|
+
update : Msg -> Model -> ( Model, Cmd Msg )
|
|
192
|
+
update msg model =
|
|
193
|
+
case msg of
|
|
194
|
+
ClickedLink request ->
|
|
195
|
+
case request of
|
|
196
|
+
Browser.Internal url ->
|
|
197
|
+
( model, Nav.pushUrl model.key (Url.toString url) )
|
|
198
|
+
|
|
199
|
+
Browser.External href ->
|
|
200
|
+
( model, Nav.load href )
|
|
201
|
+
|
|
202
|
+
ChangedUrl url ->
|
|
203
|
+
let
|
|
204
|
+
page =
|
|
205
|
+
Page.byRoute model.site (Page.routeOf model.site url.path)
|
|
206
|
+
|
|
207
|
+
doc =
|
|
208
|
+
Page.docFor model.site (Page.routeOf model.site url.path)
|
|
209
|
+
|
|
210
|
+
next =
|
|
211
|
+
{ model
|
|
212
|
+
| url = url
|
|
213
|
+
, page = page
|
|
214
|
+
, doc = doc
|
|
215
|
+
, docsTabs =
|
|
216
|
+
if doc.id /= model.doc.id then
|
|
217
|
+
Ui.Tabs.init "docs-tabs" (docsIndex model.site doc.id) doc.id
|
|
218
|
+
|
|
219
|
+
else
|
|
220
|
+
model.docsTabs
|
|
221
|
+
, menuDocsTabs =
|
|
222
|
+
if doc.id /= model.doc.id then
|
|
223
|
+
Ui.Tabs.init "burger-docs-tabs" (docsIndex model.site doc.id) doc.id
|
|
224
|
+
|
|
225
|
+
else
|
|
226
|
+
model.menuDocsTabs
|
|
227
|
+
, keepBurgerOpen = False
|
|
228
|
+
, query = ""
|
|
229
|
+
, searchOpen = False
|
|
230
|
+
, burger =
|
|
231
|
+
if model.keepBurgerOpen then
|
|
232
|
+
model.burger
|
|
233
|
+
|
|
234
|
+
else
|
|
235
|
+
closeBurger model.burger
|
|
236
|
+
, tocOpen = False
|
|
237
|
+
, headerHidden = False
|
|
238
|
+
, pageCopied = False
|
|
239
|
+
, activeSection = Nothing
|
|
240
|
+
, measurements = Nothing
|
|
241
|
+
}
|
|
242
|
+
in
|
|
243
|
+
( next
|
|
244
|
+
, Cmd.batch
|
|
245
|
+
[ scrollTo url
|
|
246
|
+
, measureAnchors (pageAnchors page)
|
|
247
|
+
, setMeta next
|
|
248
|
+
]
|
|
249
|
+
)
|
|
250
|
+
|
|
251
|
+
QueryChanged query ->
|
|
252
|
+
( { model | query = query }, Cmd.none )
|
|
253
|
+
|
|
254
|
+
QueryCleared ->
|
|
255
|
+
( { model | query = "" }, Cmd.none )
|
|
256
|
+
|
|
257
|
+
SearchMsg inner ->
|
|
258
|
+
let
|
|
259
|
+
( search, command ) =
|
|
260
|
+
Ui.Search.update inner model.search
|
|
261
|
+
in
|
|
262
|
+
case inner of
|
|
263
|
+
Ui.Search.Select route ->
|
|
264
|
+
( { model | search = search, query = "", searchOpen = False }
|
|
265
|
+
, Nav.pushUrl model.key (Page.href model.site route)
|
|
266
|
+
)
|
|
267
|
+
|
|
268
|
+
_ ->
|
|
269
|
+
( { model | search = search }, Cmd.map SearchMsg command )
|
|
270
|
+
|
|
271
|
+
SearchOpened ->
|
|
272
|
+
if searchEnabled model then
|
|
273
|
+
( { model | searchOpen = True }
|
|
274
|
+
, Dom.focus "gem-search-input" |> Task.attempt (\_ -> Noop)
|
|
275
|
+
)
|
|
276
|
+
|
|
277
|
+
else
|
|
278
|
+
( model, Cmd.none )
|
|
279
|
+
|
|
280
|
+
SearchClosed ->
|
|
281
|
+
( { model
|
|
282
|
+
| searchOpen = False
|
|
283
|
+
, query = ""
|
|
284
|
+
, search = Ui.Search.init "gem-search"
|
|
285
|
+
}
|
|
286
|
+
, Cmd.none
|
|
287
|
+
)
|
|
288
|
+
|
|
289
|
+
GroupToggled id ->
|
|
290
|
+
( { model
|
|
291
|
+
| collapsed =
|
|
292
|
+
if List.member id model.collapsed then
|
|
293
|
+
List.filter (\open -> open /= id) model.collapsed
|
|
294
|
+
|
|
295
|
+
else
|
|
296
|
+
id :: model.collapsed
|
|
297
|
+
}
|
|
298
|
+
, Cmd.none
|
|
299
|
+
)
|
|
300
|
+
|
|
301
|
+
BurgerMsg inner ->
|
|
302
|
+
let
|
|
303
|
+
( burger, command ) =
|
|
304
|
+
Ui.Burger.update inner model.burger
|
|
305
|
+
in
|
|
306
|
+
( { model | burger = burger }, Cmd.map BurgerMsg command )
|
|
307
|
+
|
|
308
|
+
CopyMsg inner ->
|
|
309
|
+
let
|
|
310
|
+
( copy, maybeText, command ) =
|
|
311
|
+
Ui.CopyButton.update inner model.copy
|
|
312
|
+
in
|
|
313
|
+
( { model | copy = copy }
|
|
314
|
+
, Cmd.batch
|
|
315
|
+
[ case maybeText of
|
|
316
|
+
Just text ->
|
|
317
|
+
Ports.copyToClipboard text
|
|
318
|
+
|
|
319
|
+
Nothing ->
|
|
320
|
+
Cmd.none
|
|
321
|
+
, Cmd.map CopyMsg command
|
|
322
|
+
]
|
|
323
|
+
)
|
|
324
|
+
|
|
325
|
+
DocsTabsMsg tabsMsg ->
|
|
326
|
+
let
|
|
327
|
+
( docsTabs, cmd ) =
|
|
328
|
+
Ui.Tabs.update tabsMsg model.docsTabs
|
|
329
|
+
in
|
|
330
|
+
( { model
|
|
331
|
+
| docsTabs = docsTabs
|
|
332
|
+
-- The twin is off screen here, so re-seeding it costs no
|
|
333
|
+
-- animation; the clicked switcher keeps its model and slides.
|
|
334
|
+
, menuDocsTabs =
|
|
335
|
+
syncDocsTabs model.site model.menuDocsTabs "burger-docs-tabs" model.docsTabs.active docsTabs.active
|
|
336
|
+
, doc = docAt model.site docsTabs.active model.doc
|
|
337
|
+
}
|
|
338
|
+
, Cmd.batch
|
|
339
|
+
[ docNavigation model model.docsTabs docsTabs
|
|
340
|
+
, Cmd.map DocsTabsMsg cmd
|
|
341
|
+
]
|
|
342
|
+
)
|
|
343
|
+
|
|
344
|
+
MenuDocsTabsMsg tabsMsg ->
|
|
345
|
+
let
|
|
346
|
+
( menuDocsTabs, cmd ) =
|
|
347
|
+
Ui.Tabs.update tabsMsg model.menuDocsTabs
|
|
348
|
+
|
|
349
|
+
switching =
|
|
350
|
+
menuDocsTabs.active /= model.menuDocsTabs.active
|
|
351
|
+
in
|
|
352
|
+
( { model
|
|
353
|
+
| menuDocsTabs = menuDocsTabs
|
|
354
|
+
, docsTabs =
|
|
355
|
+
syncDocsTabs model.site model.docsTabs "docs-tabs" model.menuDocsTabs.active menuDocsTabs.active
|
|
356
|
+
, doc = docAt model.site menuDocsTabs.active model.doc
|
|
357
|
+
-- Switching collections from the drawer is not a reason to
|
|
358
|
+
-- shut the drawer: keep it open across the URL change.
|
|
359
|
+
, keepBurgerOpen = switching
|
|
360
|
+
}
|
|
361
|
+
, Cmd.batch
|
|
362
|
+
[ docNavigation model model.menuDocsTabs menuDocsTabs
|
|
363
|
+
, Cmd.map MenuDocsTabsMsg cmd
|
|
364
|
+
]
|
|
365
|
+
)
|
|
366
|
+
|
|
367
|
+
ThemeTabsMsg tabsMsg ->
|
|
368
|
+
let
|
|
369
|
+
( themeTabs, cmd ) =
|
|
370
|
+
Ui.Tabs.update tabsMsg model.themeTabs
|
|
371
|
+
|
|
372
|
+
changed =
|
|
373
|
+
themeTabs.active /= model.theme
|
|
374
|
+
in
|
|
375
|
+
( { model
|
|
376
|
+
| themeTabs = themeTabs
|
|
377
|
+
, theme = themeTabs.active
|
|
378
|
+
, menuTabs =
|
|
379
|
+
if changed then
|
|
380
|
+
resyncTabs model.site.themeSystem "burger-theme-tabs" themeTabs.active
|
|
381
|
+
|
|
382
|
+
else
|
|
383
|
+
model.menuTabs
|
|
384
|
+
}
|
|
385
|
+
, Cmd.batch
|
|
386
|
+
[ if changed then
|
|
387
|
+
Ports.saveTheme themeTabs.active
|
|
388
|
+
|
|
389
|
+
else
|
|
390
|
+
Cmd.none
|
|
391
|
+
, Cmd.map ThemeTabsMsg cmd
|
|
392
|
+
]
|
|
393
|
+
)
|
|
394
|
+
|
|
395
|
+
MenuTabsMsg tabsMsg ->
|
|
396
|
+
let
|
|
397
|
+
( menuTabs, cmd ) =
|
|
398
|
+
Ui.Tabs.update tabsMsg model.menuTabs
|
|
399
|
+
|
|
400
|
+
changed =
|
|
401
|
+
menuTabs.active /= model.theme
|
|
402
|
+
in
|
|
403
|
+
( { model
|
|
404
|
+
| menuTabs = menuTabs
|
|
405
|
+
, theme = menuTabs.active
|
|
406
|
+
, themeTabs =
|
|
407
|
+
if changed then
|
|
408
|
+
resyncTabs model.site.themeSystem "docs-theme-tabs" menuTabs.active
|
|
409
|
+
|
|
410
|
+
else
|
|
411
|
+
model.themeTabs
|
|
412
|
+
}
|
|
413
|
+
, Cmd.batch
|
|
414
|
+
[ if changed then
|
|
415
|
+
Ports.saveTheme menuTabs.active
|
|
416
|
+
|
|
417
|
+
else
|
|
418
|
+
Cmd.none
|
|
419
|
+
, Cmd.map MenuTabsMsg cmd
|
|
420
|
+
]
|
|
421
|
+
)
|
|
422
|
+
|
|
423
|
+
AiToggled ->
|
|
424
|
+
let
|
|
425
|
+
opening =
|
|
426
|
+
not model.aiOpen
|
|
427
|
+
in
|
|
428
|
+
( { model | aiOpen = opening, aiFlip = False }
|
|
429
|
+
, if opening then
|
|
430
|
+
Ui.Dropdown.measure AiFlipped "ai-dropdown"
|
|
431
|
+
|
|
432
|
+
else
|
|
433
|
+
Cmd.none
|
|
434
|
+
)
|
|
435
|
+
|
|
436
|
+
AiFlipped flip ->
|
|
437
|
+
( { model | aiFlip = flip }, Cmd.none )
|
|
438
|
+
|
|
439
|
+
AiPicked botId ->
|
|
440
|
+
( { model | aiOpen = False }
|
|
441
|
+
, Ports.openExternal (chatbotUrl botId (pageUrl model))
|
|
442
|
+
)
|
|
443
|
+
|
|
444
|
+
CopyPage ->
|
|
445
|
+
case model.page of
|
|
446
|
+
Just page ->
|
|
447
|
+
( { model | pageCopied = True }
|
|
448
|
+
, Cmd.batch
|
|
449
|
+
[ Ports.copyMarkdown (Page.markdownHref model.site page)
|
|
450
|
+
, Process.sleep 1200
|
|
451
|
+
|> Task.perform (\_ -> PageCopyReset)
|
|
452
|
+
]
|
|
453
|
+
)
|
|
454
|
+
|
|
455
|
+
Nothing ->
|
|
456
|
+
( model, Cmd.none )
|
|
457
|
+
|
|
458
|
+
PageCopyReset ->
|
|
459
|
+
( { model | pageCopied = False }, Cmd.none )
|
|
460
|
+
|
|
461
|
+
Scrolled scrollY ->
|
|
462
|
+
( { model
|
|
463
|
+
| scrollY = scrollY
|
|
464
|
+
, headerHidden =
|
|
465
|
+
if scrollY < 96 then
|
|
466
|
+
False
|
|
467
|
+
|
|
468
|
+
else if scrollY > model.scrollY + 2 then
|
|
469
|
+
True
|
|
470
|
+
|
|
471
|
+
else if scrollY < model.scrollY - 2 then
|
|
472
|
+
False
|
|
473
|
+
|
|
474
|
+
else
|
|
475
|
+
model.headerHidden
|
|
476
|
+
, activeSection =
|
|
477
|
+
case model.measurements of
|
|
478
|
+
Just measurements ->
|
|
479
|
+
activeAt scrollY measurements
|
|
480
|
+
|
|
481
|
+
Nothing ->
|
|
482
|
+
model.activeSection
|
|
483
|
+
}
|
|
484
|
+
, Cmd.none
|
|
485
|
+
)
|
|
486
|
+
|
|
487
|
+
TocToggled ->
|
|
488
|
+
( { model | tocOpen = not model.tocOpen }, Cmd.none )
|
|
489
|
+
|
|
490
|
+
AnchorsMeasured measurements ->
|
|
491
|
+
( { model
|
|
492
|
+
| measurements = Just measurements
|
|
493
|
+
, activeSection = activeAt model.scrollY measurements
|
|
494
|
+
}
|
|
495
|
+
, Cmd.none
|
|
496
|
+
)
|
|
497
|
+
|
|
498
|
+
SearchShortcut ->
|
|
499
|
+
if searchEnabled model then
|
|
500
|
+
( { model | searchOpen = True }
|
|
501
|
+
, Dom.focus "gem-search-input" |> Task.attempt (\_ -> Noop)
|
|
502
|
+
)
|
|
503
|
+
|
|
504
|
+
else
|
|
505
|
+
( model, Cmd.none )
|
|
506
|
+
|
|
507
|
+
ScrolledTo _ ->
|
|
508
|
+
( model, Cmd.none )
|
|
509
|
+
|
|
510
|
+
Noop ->
|
|
511
|
+
( model, Cmd.none )
|
|
512
|
+
|
|
513
|
+
|
|
514
|
+
subscriptions : Model -> Sub Msg
|
|
515
|
+
subscriptions model =
|
|
516
|
+
Sub.batch
|
|
517
|
+
[ Sub.map SearchMsg (Ui.Search.subscriptions model.search)
|
|
518
|
+
, Sub.map BurgerMsg (Ui.Burger.subscriptions model.burger)
|
|
519
|
+
, Sub.map DocsTabsMsg (Ui.Tabs.subscriptions True model.docsTabs)
|
|
520
|
+
, Sub.map MenuDocsTabsMsg (Ui.Tabs.subscriptions model.burger.open model.menuDocsTabs)
|
|
521
|
+
, Sub.map ThemeTabsMsg (Ui.Tabs.subscriptions True model.themeTabs)
|
|
522
|
+
, Sub.map MenuTabsMsg (Ui.Tabs.subscriptions model.burger.open model.menuTabs)
|
|
523
|
+
, Ui.Dropdown.subscriptions "ai-dropdown" model.aiOpen AiToggled
|
|
524
|
+
, Ports.onScroll Scrolled
|
|
525
|
+
, Browser.Events.onKeyDown searchShortcutKey
|
|
526
|
+
, if model.searchOpen then
|
|
527
|
+
Browser.Events.onKeyDown searchEscapeKey
|
|
528
|
+
|
|
529
|
+
else
|
|
530
|
+
Sub.none
|
|
531
|
+
]
|
|
532
|
+
|
|
533
|
+
|
|
534
|
+
view : Model -> Browser.Document Msg
|
|
535
|
+
view model =
|
|
536
|
+
{ title = documentTitle model
|
|
537
|
+
, body =
|
|
538
|
+
[ case model.error of
|
|
539
|
+
Just error ->
|
|
540
|
+
Html.pre [ A.class "gem-error" ] [ Html.text error ]
|
|
541
|
+
|
|
542
|
+
Nothing ->
|
|
543
|
+
Shell.view
|
|
544
|
+
{ site = model.site
|
|
545
|
+
, page = model.page
|
|
546
|
+
, url = model.url
|
|
547
|
+
, query = model.query
|
|
548
|
+
, results = results model
|
|
549
|
+
, collapsed = model.collapsed
|
|
550
|
+
, search = model.search
|
|
551
|
+
, searchOpen = model.searchOpen
|
|
552
|
+
, onSearchOpen = SearchOpened
|
|
553
|
+
, onSearchClose = SearchClosed
|
|
554
|
+
, burger = model.burger
|
|
555
|
+
, slots = model.slots
|
|
556
|
+
, codeView = codeView model
|
|
557
|
+
, onQuery = QueryChanged
|
|
558
|
+
, onClear = QueryCleared
|
|
559
|
+
, onSearchMsg = SearchMsg
|
|
560
|
+
, onToggleGroup = GroupToggled
|
|
561
|
+
, onBurgerMsg = BurgerMsg
|
|
562
|
+
, doc = model.doc
|
|
563
|
+
, nav = model.doc.nav
|
|
564
|
+
, docs = model.site.docs
|
|
565
|
+
, docsTabs = model.docsTabs
|
|
566
|
+
, menuDocsTabs = model.menuDocsTabs
|
|
567
|
+
, onDocsTabs = DocsTabsMsg
|
|
568
|
+
, onMenuDocsTabs = MenuDocsTabsMsg
|
|
569
|
+
, themeTabs = model.themeTabs
|
|
570
|
+
, menuTabs = model.menuTabs
|
|
571
|
+
, onThemeTabs = ThemeTabsMsg
|
|
572
|
+
, onMenuTabs = MenuTabsMsg
|
|
573
|
+
, aiOpen = model.aiOpen
|
|
574
|
+
, aiFlip = model.aiFlip
|
|
575
|
+
, onAiToggle = AiToggled
|
|
576
|
+
, onAiPick = AiPicked
|
|
577
|
+
, onCopyPage = CopyPage
|
|
578
|
+
, pageCopied = model.pageCopied
|
|
579
|
+
, copyButton = model.copy
|
|
580
|
+
, onCopyButton = CopyMsg
|
|
581
|
+
, activeSection = model.activeSection
|
|
582
|
+
, headerHidden = model.headerHidden
|
|
583
|
+
, tocOpen = model.tocOpen
|
|
584
|
+
, onTocToggle = TocToggled
|
|
585
|
+
}
|
|
586
|
+
]
|
|
587
|
+
}
|
|
588
|
+
|
|
589
|
+
|
|
590
|
+
{-| Code blocks carry the library's copy button. The clipboard write leaves
|
|
591
|
+
Elm through the `copyToClipboard` port; the button owns the check that
|
|
592
|
+
flashes in.
|
|
593
|
+
-}
|
|
594
|
+
codeView : Model -> Render.CodeView Msg
|
|
595
|
+
codeView model code =
|
|
596
|
+
Html.div [ A.class "code-frame" ]
|
|
597
|
+
[ Render.codeHead languageButton code.lang code.title
|
|
598
|
+
[ Html.map CopyMsg
|
|
599
|
+
(Ui.CopyButton.view
|
|
600
|
+
{ text = code.text
|
|
601
|
+
, label = "Copy code"
|
|
602
|
+
, attributes = [ A.class "code-copy secondary xs" ]
|
|
603
|
+
}
|
|
604
|
+
model.copy
|
|
605
|
+
)
|
|
606
|
+
]
|
|
607
|
+
, Html.div [ A.class "code-block" ]
|
|
608
|
+
[ Html.pre [] [ Html.code [] (Render.codeLines code.lines) ] ]
|
|
609
|
+
]
|
|
610
|
+
|
|
611
|
+
|
|
612
|
+
{-| The fence's language as the kit's compact secondary button. It is a
|
|
613
|
+
label, not an action: inert for pointers and keyboard, hidden from the
|
|
614
|
+
accessibility tree, and the same control family as the copy button beside
|
|
615
|
+
it.
|
|
616
|
+
-}
|
|
617
|
+
languageButton : String -> Html Msg
|
|
618
|
+
languageButton name =
|
|
619
|
+
Ui.Button.view
|
|
620
|
+
{ label = name
|
|
621
|
+
, variant = Ui.Button.Secondary
|
|
622
|
+
, size = Ui.Button.Xs
|
|
623
|
+
, wide = False
|
|
624
|
+
, disabled = False
|
|
625
|
+
, busy = Nothing
|
|
626
|
+
, icon = Nothing
|
|
627
|
+
, onClick = Noop
|
|
628
|
+
, attributes =
|
|
629
|
+
[ A.class "code-lang"
|
|
630
|
+
, A.attribute "tabindex" "-1"
|
|
631
|
+
, A.attribute "aria-hidden" "true"
|
|
632
|
+
]
|
|
633
|
+
}
|
|
634
|
+
|
|
635
|
+
|
|
636
|
+
|
|
637
|
+
-- THEME
|
|
638
|
+
|
|
639
|
+
|
|
640
|
+
{-| The tab for a preference: light, system in the middle, dark. A stored
|
|
641
|
+
"system" on a site without the middle tab lands on dark, which is what the
|
|
642
|
+
runtime resolves it to.
|
|
643
|
+
-}
|
|
644
|
+
themeIndex : Bool -> String -> Int
|
|
645
|
+
themeIndex system theme =
|
|
646
|
+
if theme == "light" then
|
|
647
|
+
0
|
|
648
|
+
|
|
649
|
+
else if theme == "system" && system then
|
|
650
|
+
1
|
|
651
|
+
|
|
652
|
+
else if system then
|
|
653
|
+
2
|
|
654
|
+
|
|
655
|
+
else
|
|
656
|
+
1
|
|
657
|
+
|
|
658
|
+
|
|
659
|
+
resyncTabs : Bool -> String -> String -> Ui.Tabs.Model String
|
|
660
|
+
resyncTabs system id theme =
|
|
661
|
+
Ui.Tabs.init id (themeIndex system theme) theme
|
|
662
|
+
|
|
663
|
+
|
|
664
|
+
|
|
665
|
+
-- OPEN IN CHATBOT
|
|
666
|
+
|
|
667
|
+
|
|
668
|
+
chatbots : List { id : String, label : String, queryUrl : String }
|
|
669
|
+
chatbots =
|
|
670
|
+
[ { id = "chatgpt", label = "Open in ChatGPT", queryUrl = "https://chatgpt.com/?q=" }
|
|
671
|
+
, { id = "claude", label = "Open in Claude", queryUrl = "https://claude.ai/new?q=" }
|
|
672
|
+
, { id = "perplexity", label = "Open in Perplexity", queryUrl = "https://www.perplexity.ai/search?q=" }
|
|
673
|
+
, { id = "copilot", label = "Open in Copilot", queryUrl = "https://copilot.microsoft.com/?q=" }
|
|
674
|
+
, { id = "you", label = "Open in You.com", queryUrl = "https://you.com/search?q=" }
|
|
675
|
+
, { id = "phind", label = "Open in Phind", queryUrl = "https://www.phind.com/search?q=" }
|
|
676
|
+
]
|
|
677
|
+
|
|
678
|
+
|
|
679
|
+
ogPath : Page -> String
|
|
680
|
+
ogPath page =
|
|
681
|
+
if page.route == "/" then
|
|
682
|
+
"/og/index.png"
|
|
683
|
+
|
|
684
|
+
else
|
|
685
|
+
"/og" ++ page.route ++ ".png"
|
|
686
|
+
|
|
687
|
+
|
|
688
|
+
origin : Model -> String
|
|
689
|
+
origin model =
|
|
690
|
+
let
|
|
691
|
+
url =
|
|
692
|
+
model.url
|
|
693
|
+
in
|
|
694
|
+
Url.toString { url | path = "/", query = Nothing, fragment = Nothing }
|
|
695
|
+
|> String.dropRight 1
|
|
696
|
+
|
|
697
|
+
|
|
698
|
+
pageUrl : Model -> String
|
|
699
|
+
pageUrl model =
|
|
700
|
+
case model.page of
|
|
701
|
+
Just page ->
|
|
702
|
+
origin model ++ Page.markdownHref model.site page
|
|
703
|
+
|
|
704
|
+
Nothing ->
|
|
705
|
+
origin model
|
|
706
|
+
|
|
707
|
+
|
|
708
|
+
chatbotUrl : String -> String -> String
|
|
709
|
+
chatbotUrl botId mdUrl =
|
|
710
|
+
let
|
|
711
|
+
prompt =
|
|
712
|
+
"Read this documentation page and explain it with examples: " ++ mdUrl
|
|
713
|
+
in
|
|
714
|
+
(chatbots
|
|
715
|
+
|> List.filter (\bot -> bot.id == botId)
|
|
716
|
+
|> List.head
|
|
717
|
+
|> Maybe.map .queryUrl
|
|
718
|
+
|> Maybe.withDefault ""
|
|
719
|
+
)
|
|
720
|
+
++ Url.percentEncode prompt
|
|
721
|
+
|
|
722
|
+
|
|
723
|
+
|
|
724
|
+
-- SCROLL SPY
|
|
725
|
+
|
|
726
|
+
|
|
727
|
+
type alias Measurements =
|
|
728
|
+
{ positions : List ( String, Float )
|
|
729
|
+
, sceneHeight : Float
|
|
730
|
+
, viewportHeight : Float
|
|
731
|
+
}
|
|
732
|
+
|
|
733
|
+
|
|
734
|
+
scrollSpyOffset : Float
|
|
735
|
+
scrollSpyOffset =
|
|
736
|
+
96
|
|
737
|
+
|
|
738
|
+
|
|
739
|
+
pageAnchors : Maybe Page -> List String
|
|
740
|
+
pageAnchors page =
|
|
741
|
+
case page of
|
|
742
|
+
Just item ->
|
|
743
|
+
List.map .id item.toc
|
|
744
|
+
|
|
745
|
+
Nothing ->
|
|
746
|
+
[]
|
|
747
|
+
|
|
748
|
+
|
|
749
|
+
measureAnchors : List String -> Cmd Msg
|
|
750
|
+
measureAnchors anchors =
|
|
751
|
+
Task.attempt toMeasured
|
|
752
|
+
(Task.map2
|
|
753
|
+
(\positions viewport ->
|
|
754
|
+
{ positions = positions
|
|
755
|
+
, sceneHeight = viewport.scene.height
|
|
756
|
+
, viewportHeight = viewport.viewport.height
|
|
757
|
+
}
|
|
758
|
+
)
|
|
759
|
+
(Task.sequence
|
|
760
|
+
(List.map
|
|
761
|
+
(\id ->
|
|
762
|
+
Dom.getElement id
|
|
763
|
+
|> Task.map (\el -> ( id, el.element.y ))
|
|
764
|
+
)
|
|
765
|
+
anchors
|
|
766
|
+
)
|
|
767
|
+
)
|
|
768
|
+
Dom.getViewport
|
|
769
|
+
)
|
|
770
|
+
|
|
771
|
+
|
|
772
|
+
toMeasured : Result Dom.Error Measurements -> Msg
|
|
773
|
+
toMeasured result =
|
|
774
|
+
case result of
|
|
775
|
+
Ok measurements ->
|
|
776
|
+
AnchorsMeasured measurements
|
|
777
|
+
|
|
778
|
+
Err _ ->
|
|
779
|
+
AnchorsMeasured { positions = [], sceneHeight = 0, viewportHeight = 0 }
|
|
780
|
+
|
|
781
|
+
|
|
782
|
+
activeAt : Float -> Measurements -> Maybe String
|
|
783
|
+
activeAt scrollY measurements =
|
|
784
|
+
let
|
|
785
|
+
positions =
|
|
786
|
+
measurements.positions
|
|
787
|
+
|
|
788
|
+
fromScroll =
|
|
789
|
+
positions
|
|
790
|
+
|> List.filter (\( _, y ) -> y - scrollSpyOffset <= scrollY)
|
|
791
|
+
|> List.reverse
|
|
792
|
+
|> List.head
|
|
793
|
+
|> Maybe.map Tuple.first
|
|
794
|
+
|
|
795
|
+
atBottom =
|
|
796
|
+
scrollY + measurements.viewportHeight >= measurements.sceneHeight - 24
|
|
797
|
+
|
|
798
|
+
lastId =
|
|
799
|
+
List.reverse positions |> List.head |> Maybe.map Tuple.first
|
|
800
|
+
in
|
|
801
|
+
if atBottom then
|
|
802
|
+
Maybe.map (\id -> "#" ++ id) lastId
|
|
803
|
+
|
|
804
|
+
else
|
|
805
|
+
case fromScroll of
|
|
806
|
+
Just id ->
|
|
807
|
+
Just ("#" ++ id)
|
|
808
|
+
|
|
809
|
+
Nothing ->
|
|
810
|
+
List.head positions |> Maybe.map (\( id, _ ) -> "#" ++ id)
|
|
811
|
+
|
|
812
|
+
|
|
813
|
+
|
|
814
|
+
-- META
|
|
815
|
+
|
|
816
|
+
|
|
817
|
+
setMeta : Model -> Cmd Msg
|
|
818
|
+
setMeta model =
|
|
819
|
+
let
|
|
820
|
+
page =
|
|
821
|
+
model.page
|
|
822
|
+
|
|
823
|
+
description =
|
|
824
|
+
Maybe.map
|
|
825
|
+
(\item ->
|
|
826
|
+
if String.isEmpty item.description then
|
|
827
|
+
item.summary
|
|
828
|
+
|
|
829
|
+
else
|
|
830
|
+
item.description
|
|
831
|
+
)
|
|
832
|
+
page
|
|
833
|
+
|> Maybe.withDefault model.site.meta.description
|
|
834
|
+
in
|
|
835
|
+
Ports.setMeta
|
|
836
|
+
{ title = documentTitle model
|
|
837
|
+
, description = description
|
|
838
|
+
, keywords =
|
|
839
|
+
page
|
|
840
|
+
|> Maybe.map (\item -> String.join ", " item.tags)
|
|
841
|
+
|> Maybe.withDefault ""
|
|
842
|
+
, url =
|
|
843
|
+
Page.href model.site
|
|
844
|
+
(page
|
|
845
|
+
|> Maybe.map .route
|
|
846
|
+
|> Maybe.withDefault "/"
|
|
847
|
+
)
|
|
848
|
+
, image =
|
|
849
|
+
Page.href model.site
|
|
850
|
+
(page
|
|
851
|
+
|> Maybe.map ogPath
|
|
852
|
+
|> Maybe.withDefault "/og/index.png"
|
|
853
|
+
)
|
|
854
|
+
}
|
|
855
|
+
|
|
856
|
+
|
|
857
|
+
documentTitle : Model -> String
|
|
858
|
+
documentTitle model =
|
|
859
|
+
case model.page of
|
|
860
|
+
Just page ->
|
|
861
|
+
if page.route == landingRoute model then
|
|
862
|
+
model.site.meta.title
|
|
863
|
+
|
|
864
|
+
else
|
|
865
|
+
page.title ++ " · " ++ model.site.meta.title
|
|
866
|
+
|
|
867
|
+
Nothing ->
|
|
868
|
+
model.site.meta.title
|
|
869
|
+
|
|
870
|
+
|
|
871
|
+
landingRoute : Model -> String
|
|
872
|
+
landingRoute model =
|
|
873
|
+
model.site.docs
|
|
874
|
+
|> List.head
|
|
875
|
+
|> Maybe.map .root
|
|
876
|
+
|> Maybe.withDefault "/"
|
|
877
|
+
|
|
878
|
+
|
|
879
|
+
{-| The twin switcher: re-seeded only when the choice actually moved, and
|
|
880
|
+
otherwise left exactly as it was. The clicked switcher keeps its own model,
|
|
881
|
+
so its thumb travels instead of being reset.
|
|
882
|
+
-}
|
|
883
|
+
syncDocsTabs :
|
|
884
|
+
Site
|
|
885
|
+
-> Ui.Tabs.Model String
|
|
886
|
+
-> String
|
|
887
|
+
-> String
|
|
888
|
+
-> String
|
|
889
|
+
-> Ui.Tabs.Model String
|
|
890
|
+
syncDocsTabs site twin id before now =
|
|
891
|
+
if before == now then
|
|
892
|
+
twin
|
|
893
|
+
|
|
894
|
+
else
|
|
895
|
+
Ui.Tabs.init id (docsIndex site now) now
|
|
896
|
+
|
|
897
|
+
|
|
898
|
+
{-| The collection the switcher points at, or the current one when the value
|
|
899
|
+
does not name a collection.
|
|
900
|
+
-}
|
|
901
|
+
docAt : Site -> String -> Page.Doc -> Page.Doc
|
|
902
|
+
docAt site id current =
|
|
903
|
+
site.docs
|
|
904
|
+
|> List.filter (\doc -> doc.id == id)
|
|
905
|
+
|> List.head
|
|
906
|
+
|> Maybe.withDefault current
|
|
907
|
+
|
|
908
|
+
|
|
909
|
+
docNavigation : Model -> Ui.Tabs.Model String -> Ui.Tabs.Model String -> Cmd Msg
|
|
910
|
+
docNavigation model before after =
|
|
911
|
+
if before.active == after.active then
|
|
912
|
+
Cmd.none
|
|
913
|
+
|
|
914
|
+
else
|
|
915
|
+
case
|
|
916
|
+
model.site.docs
|
|
917
|
+
|> List.filter (\doc -> doc.id == after.active)
|
|
918
|
+
|> List.head
|
|
919
|
+
of
|
|
920
|
+
Just doc ->
|
|
921
|
+
if doc.id /= model.doc.id then
|
|
922
|
+
Nav.pushUrl model.key (Page.href model.site doc.root)
|
|
923
|
+
|
|
924
|
+
else
|
|
925
|
+
Cmd.none
|
|
926
|
+
|
|
927
|
+
Nothing ->
|
|
928
|
+
Cmd.none
|
|
929
|
+
|
|
930
|
+
|
|
931
|
+
-- SEARCH
|
|
932
|
+
|
|
933
|
+
|
|
934
|
+
type alias Hit =
|
|
935
|
+
{ route : String
|
|
936
|
+
, label : String
|
|
937
|
+
, detail : Maybe String
|
|
938
|
+
, snippet : Maybe String
|
|
939
|
+
, text : String
|
|
940
|
+
}
|
|
941
|
+
|
|
942
|
+
|
|
943
|
+
results : Model -> List (Ui.Search.Item String)
|
|
944
|
+
results model =
|
|
945
|
+
let
|
|
946
|
+
words =
|
|
947
|
+
model.query
|
|
948
|
+
|> String.toLower
|
|
949
|
+
|> String.words
|
|
950
|
+
|> List.filter (String.isEmpty >> not)
|
|
951
|
+
|
|
952
|
+
toItem hit =
|
|
953
|
+
{ label = hit.label
|
|
954
|
+
, detail = hit.detail
|
|
955
|
+
, snippet = hit.snippet
|
|
956
|
+
, value = hit.route
|
|
957
|
+
}
|
|
958
|
+
|
|
959
|
+
scorable =
|
|
960
|
+
model.index
|
|
961
|
+
|> List.filter (\hit -> inDoc model.doc hit.route)
|
|
962
|
+
|
|
963
|
+
-- Multi-word queries are read as a phrase first: "openai re" means
|
|
964
|
+
-- the words run together (with the last one a prefix), so it lands
|
|
965
|
+
-- on "OpenAI Responses" instead of every page that happens to
|
|
966
|
+
-- contain an "re". Only when nothing carries the phrase does the
|
|
967
|
+
-- loose all-words rule take over — that keeps a reordered query
|
|
968
|
+
-- ("responses openai") from going dark.
|
|
969
|
+
phrase =
|
|
970
|
+
String.toLower (String.trim model.query)
|
|
971
|
+
|
|
972
|
+
phraseMatches =
|
|
973
|
+
List.filter (hitHasPhrase phrase) scorable
|
|
974
|
+
|
|
975
|
+
candidates =
|
|
976
|
+
if List.length words > 1 && not (List.isEmpty phraseMatches) then
|
|
977
|
+
phraseMatches
|
|
978
|
+
|
|
979
|
+
else
|
|
980
|
+
scorable
|
|
981
|
+
in
|
|
982
|
+
if List.isEmpty words then
|
|
983
|
+
-- The empty modal is a jump menu, not a dead end: the collection's
|
|
984
|
+
-- own pages lead it, so the first thing a reader sees is somewhere
|
|
985
|
+
-- to go.
|
|
986
|
+
model.site.entries
|
|
987
|
+
|> List.filter (\entry -> inDoc model.doc entry.route)
|
|
988
|
+
|> List.take 6
|
|
989
|
+
|> List.map
|
|
990
|
+
(\entry ->
|
|
991
|
+
{ label = entry.title
|
|
992
|
+
, detail = Nothing
|
|
993
|
+
, snippet = nonEmpty entry.summary
|
|
994
|
+
, value = entry.route
|
|
995
|
+
}
|
|
996
|
+
)
|
|
997
|
+
|
|
998
|
+
else
|
|
999
|
+
candidates
|
|
1000
|
+
|> List.filterMap (scoreHit model.query words)
|
|
1001
|
+
|> List.sortWith compareScored
|
|
1002
|
+
|> List.take 8
|
|
1003
|
+
|> List.map (Tuple.second >> toItem)
|
|
1004
|
+
|
|
1005
|
+
|
|
1006
|
+
{-| Whether a hit carries the whole query as one run of text. The query's
|
|
1007
|
+
last word is usually half-typed, so a literal substring check doubles as
|
|
1008
|
+
prefix matching: "openai re" is contained in "openai responses".
|
|
1009
|
+
-}
|
|
1010
|
+
hitHasPhrase : String -> Hit -> Bool
|
|
1011
|
+
hitHasPhrase phrase hit =
|
|
1012
|
+
String.contains phrase (String.toLower hit.label)
|
|
1013
|
+
|| String.contains phrase (hit.detail |> Maybe.withDefault "" |> String.toLower)
|
|
1014
|
+
|| String.contains phrase hit.text
|
|
1015
|
+
|
|
1016
|
+
|
|
1017
|
+
{-| A hit's rank: full words must appear somewhere, the label outweighs the
|
|
1018
|
+
body, a prefix beats a substring, and an exact title beats everything.
|
|
1019
|
+
Ties go to the shorter label, so the page answers before its sections.
|
|
1020
|
+
-}
|
|
1021
|
+
scoreHit : String -> List String -> Hit -> Maybe ( Int, Hit )
|
|
1022
|
+
scoreHit query words hit =
|
|
1023
|
+
let
|
|
1024
|
+
label =
|
|
1025
|
+
String.toLower hit.label
|
|
1026
|
+
|
|
1027
|
+
detail =
|
|
1028
|
+
hit.detail |> Maybe.withDefault "" |> String.toLower
|
|
1029
|
+
|
|
1030
|
+
phrase =
|
|
1031
|
+
String.toLower (String.trim query)
|
|
1032
|
+
|
|
1033
|
+
covered word =
|
|
1034
|
+
String.contains word label
|
|
1035
|
+
|| String.contains word detail
|
|
1036
|
+
|| String.contains word hit.text
|
|
1037
|
+
|
|
1038
|
+
wordScore word =
|
|
1039
|
+
(if String.contains word label then
|
|
1040
|
+
6
|
|
1041
|
+
|
|
1042
|
+
else
|
|
1043
|
+
0
|
|
1044
|
+
)
|
|
1045
|
+
+ (if List.any (String.startsWith word) (String.words label) then
|
|
1046
|
+
4
|
|
1047
|
+
|
|
1048
|
+
else
|
|
1049
|
+
0
|
|
1050
|
+
)
|
|
1051
|
+
+ (if String.contains word detail then
|
|
1052
|
+
2
|
|
1053
|
+
|
|
1054
|
+
else
|
|
1055
|
+
0
|
|
1056
|
+
)
|
|
1057
|
+
+ (if String.contains word hit.text then
|
|
1058
|
+
1
|
|
1059
|
+
|
|
1060
|
+
else
|
|
1061
|
+
0
|
|
1062
|
+
)
|
|
1063
|
+
in
|
|
1064
|
+
if List.all covered words then
|
|
1065
|
+
Just
|
|
1066
|
+
( List.sum (List.map wordScore words)
|
|
1067
|
+
+ (if label == phrase then
|
|
1068
|
+
25
|
|
1069
|
+
|
|
1070
|
+
else
|
|
1071
|
+
0
|
|
1072
|
+
)
|
|
1073
|
+
+ (if String.startsWith phrase label then
|
|
1074
|
+
12
|
|
1075
|
+
|
|
1076
|
+
else
|
|
1077
|
+
0
|
|
1078
|
+
)
|
|
1079
|
+
- (String.length label // 10)
|
|
1080
|
+
, hit
|
|
1081
|
+
)
|
|
1082
|
+
|
|
1083
|
+
else
|
|
1084
|
+
Nothing
|
|
1085
|
+
|
|
1086
|
+
|
|
1087
|
+
compareScored : ( Int, Hit ) -> ( Int, Hit ) -> Order
|
|
1088
|
+
compareScored ( left, leftHit ) ( right, rightHit ) =
|
|
1089
|
+
case compare right left of
|
|
1090
|
+
EQ ->
|
|
1091
|
+
compare (String.length leftHit.label) (String.length rightHit.label)
|
|
1092
|
+
|
|
1093
|
+
order ->
|
|
1094
|
+
order
|
|
1095
|
+
|
|
1096
|
+
|
|
1097
|
+
docsIndex : Site -> String -> Int
|
|
1098
|
+
docsIndex site docId =
|
|
1099
|
+
site.docs
|
|
1100
|
+
|> List.indexedMap Tuple.pair
|
|
1101
|
+
|> List.filter (\( _, doc ) -> doc.id == docId)
|
|
1102
|
+
|> List.head
|
|
1103
|
+
|> Maybe.map Tuple.first
|
|
1104
|
+
|> Maybe.withDefault 0
|
|
1105
|
+
|
|
1106
|
+
|
|
1107
|
+
{-| Whether the header offers search; the shortcut follows the same flag, so a
|
|
1108
|
+
site that turned it off never opens a dialog with no trigger.
|
|
1109
|
+
-}
|
|
1110
|
+
searchEnabled : Model -> Bool
|
|
1111
|
+
searchEnabled model =
|
|
1112
|
+
model.site.chrome.header.search
|
|
1113
|
+
|
|
1114
|
+
|
|
1115
|
+
{-| Whether a route belongs to the given collection: the landing itself, or
|
|
1116
|
+
anything under the collection's prefix.
|
|
1117
|
+
-}
|
|
1118
|
+
inDoc : Page.Doc -> String -> Bool
|
|
1119
|
+
inDoc doc route =
|
|
1120
|
+
route
|
|
1121
|
+
== doc.root
|
|
1122
|
+
|| String.startsWith (doc.prefix ++ "/") route
|
|
1123
|
+
|| (doc.prefix == "/" && String.startsWith "/" route)
|
|
1124
|
+
|
|
1125
|
+
|
|
1126
|
+
searchShortcutKey : D.Decoder Msg
|
|
1127
|
+
searchShortcutKey =
|
|
1128
|
+
D.map3 (\key ctrlKey metaKey -> ( key, ctrlKey || metaKey ))
|
|
1129
|
+
(D.field "key" D.string)
|
|
1130
|
+
(D.field "ctrlKey" D.bool)
|
|
1131
|
+
(D.field "metaKey" D.bool)
|
|
1132
|
+
|> D.andThen
|
|
1133
|
+
(\( key, mod ) ->
|
|
1134
|
+
if (mod && String.toLower key == "k") || key == "/" then
|
|
1135
|
+
D.succeed SearchShortcut
|
|
1136
|
+
|
|
1137
|
+
else
|
|
1138
|
+
D.fail "not a search shortcut"
|
|
1139
|
+
)
|
|
1140
|
+
|
|
1141
|
+
|
|
1142
|
+
searchEscapeKey : D.Decoder Msg
|
|
1143
|
+
searchEscapeKey =
|
|
1144
|
+
D.field "key" D.string
|
|
1145
|
+
|> D.andThen
|
|
1146
|
+
(\key ->
|
|
1147
|
+
if key == "Escape" then
|
|
1148
|
+
D.succeed SearchClosed
|
|
1149
|
+
|
|
1150
|
+
else
|
|
1151
|
+
D.fail "not escape"
|
|
1152
|
+
)
|
|
1153
|
+
|
|
1154
|
+
|
|
1155
|
+
siteIndex : Site -> List Hit
|
|
1156
|
+
siteIndex site =
|
|
1157
|
+
List.concatMap pageHits site.entries
|
|
1158
|
+
|
|
1159
|
+
|
|
1160
|
+
pageHits : Page.Entry -> List Hit
|
|
1161
|
+
pageHits entry =
|
|
1162
|
+
{ route = entry.route
|
|
1163
|
+
, label = entry.title
|
|
1164
|
+
, detail = Nothing
|
|
1165
|
+
, snippet = nonEmpty entry.summary
|
|
1166
|
+
, text = String.toLower (entry.title ++ " " ++ entry.text)
|
|
1167
|
+
}
|
|
1168
|
+
:: List.map (sectionHit entry) entry.sections
|
|
1169
|
+
|
|
1170
|
+
|
|
1171
|
+
sectionHit : Page.Entry -> Page.Section -> Hit
|
|
1172
|
+
sectionHit entry section =
|
|
1173
|
+
{ route =
|
|
1174
|
+
if String.isEmpty section.id then
|
|
1175
|
+
entry.route
|
|
1176
|
+
|
|
1177
|
+
else
|
|
1178
|
+
entry.route ++ "#" ++ section.id
|
|
1179
|
+
, label =
|
|
1180
|
+
if String.isEmpty section.label then
|
|
1181
|
+
entry.title
|
|
1182
|
+
|
|
1183
|
+
else
|
|
1184
|
+
section.label
|
|
1185
|
+
, detail = Just entry.title
|
|
1186
|
+
, snippet = snippetFor section.text
|
|
1187
|
+
, text = String.toLower (section.label ++ " " ++ entry.title ++ " " ++ section.text)
|
|
1188
|
+
}
|
|
1189
|
+
|
|
1190
|
+
|
|
1191
|
+
snippetFor : String -> Maybe String
|
|
1192
|
+
snippetFor text =
|
|
1193
|
+
let
|
|
1194
|
+
trimmed =
|
|
1195
|
+
String.trim text
|
|
1196
|
+
in
|
|
1197
|
+
if String.isEmpty trimmed then
|
|
1198
|
+
Nothing
|
|
1199
|
+
|
|
1200
|
+
else if String.length trimmed > 110 then
|
|
1201
|
+
Just (String.left 107 trimmed ++ "…")
|
|
1202
|
+
|
|
1203
|
+
else
|
|
1204
|
+
Just trimmed
|
|
1205
|
+
|
|
1206
|
+
|
|
1207
|
+
nonEmpty : String -> Maybe String
|
|
1208
|
+
nonEmpty value =
|
|
1209
|
+
if String.isEmpty (String.trim value) then
|
|
1210
|
+
Nothing
|
|
1211
|
+
|
|
1212
|
+
else
|
|
1213
|
+
Just value
|
|
1214
|
+
|
|
1215
|
+
|
|
1216
|
+
closeBurger : Ui.Burger.Model -> Ui.Burger.Model
|
|
1217
|
+
closeBurger burger =
|
|
1218
|
+
if burger.open then
|
|
1219
|
+
Tuple.first (Ui.Burger.update Ui.Burger.Closed burger)
|
|
1220
|
+
|
|
1221
|
+
else
|
|
1222
|
+
burger
|
|
1223
|
+
|
|
1224
|
+
|
|
1225
|
+
scrollTo : Url -> Cmd Msg
|
|
1226
|
+
scrollTo url =
|
|
1227
|
+
case url.fragment of
|
|
1228
|
+
Just id ->
|
|
1229
|
+
Dom.getElement id
|
|
1230
|
+
|> Task.andThen (\element -> Dom.setViewport 0 element.element.y)
|
|
1231
|
+
|> Task.attempt ScrolledTo
|
|
1232
|
+
|
|
1233
|
+
Nothing ->
|
|
1234
|
+
Dom.setViewport 0 0 |> Task.attempt ScrolledTo
|
|
1235
|
+
|
|
1236
|
+
|
|
1237
|
+
emptySite : Site
|
|
1238
|
+
emptySite =
|
|
1239
|
+
{ meta =
|
|
1240
|
+
{ title = "Gem"
|
|
1241
|
+
, description = ""
|
|
1242
|
+
, url = ""
|
|
1243
|
+
, lang = "en"
|
|
1244
|
+
, logo = "Gem"
|
|
1245
|
+
, logoImage = Nothing
|
|
1246
|
+
, footer = Nothing
|
|
1247
|
+
, editUrl = Nothing
|
|
1248
|
+
}
|
|
1249
|
+
, pages = Dict.empty
|
|
1250
|
+
, docs =
|
|
1251
|
+
[ { id = "docs"
|
|
1252
|
+
, label = "Gem"
|
|
1253
|
+
, prefix = ""
|
|
1254
|
+
, root = "/"
|
|
1255
|
+
, nav = []
|
|
1256
|
+
}
|
|
1257
|
+
]
|
|
1258
|
+
, entries = []
|
|
1259
|
+
, colors = []
|
|
1260
|
+
, theme = "dark"
|
|
1261
|
+
, themeSystem = True
|
|
1262
|
+
, chrome =
|
|
1263
|
+
{ header = { logo = True, search = True, theme = True, docs = True, actions = [] }
|
|
1264
|
+
, toolbar = { copy = True, openIn = True }
|
|
1265
|
+
, footer = { enabled = False, text = "Gem", blurb = "", chips = [], columns = [], eof = False }
|
|
1266
|
+
}
|
|
1267
|
+
, base = "/"
|
|
1268
|
+
}
|