@@ -28,6 +28,41 @@ globalThis.qwebrUpdateStatusHeader = function(message) {
28
28
<span>${ message } </span>` ;
29
29
}
30
30
31
+ // Function to return true if element is found, false if not
32
+ globalThis . qwebrCheckHTMLElementExists = function ( selector ) {
33
+ const element = document . querySelector ( selector ) ;
34
+ return ! ! element ;
35
+ }
36
+
37
+ // Function that detects whether reveal.js slides are present
38
+ globalThis . qwebrIsRevealJS = function ( ) {
39
+ // If the '.reveal .slides' selector exists, RevealJS is likely present
40
+ return qwebrCheckHTMLElementExists ( '.reveal .slides' ) ;
41
+ }
42
+
43
+ // Initialize the Quarto sidebar element
44
+ function qwebrSetupQuartoSidebar ( ) {
45
+ var newSideBarDiv = document . createElement ( 'div' ) ;
46
+ newSideBarDiv . id = 'quarto-margin-sidebar' ;
47
+ newSideBarDiv . className = 'sidebar margin-sidebar' ;
48
+ newSideBarDiv . style . top = '0px' ;
49
+ newSideBarDiv . style . maxHeight = 'calc(0px + 100vh)' ;
50
+
51
+ return newSideBarDiv ;
52
+ }
53
+
54
+ // Position the sidebar in the document
55
+ function qwebrPlaceQuartoSidebar ( ) {
56
+ // Get the reference to the element with id 'quarto-document-content'
57
+ var referenceNode = document . getElementById ( 'quarto-document-content' ) ;
58
+
59
+ // Create the new div element
60
+ var newSideBarDiv = qwebrSetupQuartoSidebar ( ) ;
61
+
62
+ // Insert the new div before the 'quarto-document-content' element
63
+ referenceNode . parentNode . insertBefore ( newSideBarDiv , referenceNode ) ;
64
+ }
65
+
31
66
function qwebrPlaceMessageContents ( content , html_location = "title-block-header" , revealjs_location = "title-slide" ) {
32
67
33
68
// Get references to header elements
@@ -49,6 +84,7 @@ function qwebrPlaceMessageContents(content, html_location = "title-block-header"
49
84
}
50
85
51
86
87
+
52
88
function qwebrOffScreenCanvasSupportWarningMessage ( ) {
53
89
54
90
// Verify canvas is supported.
@@ -154,5 +190,176 @@ function displayStartupMessage(showStartupMessage, showHeaderMessage) {
154
190
qwebrPlaceMessageContents ( quartoTitleMeta ) ;
155
191
}
156
192
193
+ function qwebrAddCommandHistoryModal ( ) {
194
+ // Create the modal div
195
+ var modalDiv = document . createElement ( 'div' ) ;
196
+ modalDiv . id = 'qwebr-history-modal' ;
197
+ modalDiv . className = 'qwebr-modal' ;
198
+
199
+ // Create the modal content div
200
+ var modalContentDiv = document . createElement ( 'div' ) ;
201
+ modalContentDiv . className = 'qwebr-modal-content' ;
202
+
203
+ // Create the span for closing the modal
204
+ var closeSpan = document . createElement ( 'span' ) ;
205
+ closeSpan . id = 'qwebr-command-history-close-btn' ;
206
+ closeSpan . className = 'qwebr-modal-close' ;
207
+ closeSpan . innerHTML = '×' ;
208
+
209
+ // Create the h1 element for the modal
210
+ var modalH1 = document . createElement ( 'h1' ) ;
211
+ modalH1 . textContent = 'R History Command Contents' ;
212
+
213
+ // Create an anchor element for downloading the Rhistory file
214
+ var downloadLink = document . createElement ( 'a' ) ;
215
+ downloadLink . href = '#' ;
216
+ downloadLink . id = 'qwebr-download-history-btn' ;
217
+ downloadLink . className = 'qwebr-download-btn' ;
218
+
219
+ // Create an 'i' element for the icon
220
+ var icon = document . createElement ( 'i' ) ;
221
+ icon . className = 'bi bi-file-code' ;
222
+
223
+ // Append the icon to the anchor element
224
+ downloadLink . appendChild ( icon ) ;
225
+
226
+ // Add the text 'Download R History' to the anchor element
227
+ downloadLink . appendChild ( document . createTextNode ( ' Download R History File' ) ) ;
228
+
229
+ // Create the pre for command history contents
230
+ var commandContentsPre = document . createElement ( 'pre' ) ;
231
+ commandContentsPre . id = 'qwebr-command-history-contents' ;
232
+ commandContentsPre . className = 'qwebr-modal-content-code' ;
233
+
234
+ // Append the close span, h1, and history contents pre to the modal content div
235
+ modalContentDiv . appendChild ( closeSpan ) ;
236
+ modalContentDiv . appendChild ( modalH1 ) ;
237
+ modalContentDiv . appendChild ( downloadLink ) ;
238
+ modalContentDiv . appendChild ( commandContentsPre ) ;
239
+
240
+ // Append the modal content div to the modal div
241
+ modalDiv . appendChild ( modalContentDiv ) ;
242
+
243
+ // Append the modal div to the body
244
+ document . body . appendChild ( modalDiv ) ;
245
+ }
246
+
247
+ function qwebrRegisterRevealJSCommandHistoryModal ( ) {
248
+ // Select the <ul> element inside the <div> with data-panel="Custom0"
249
+ let ulElement = document . querySelector ( 'div[data-panel="Custom0"] > ul.slide-menu-items' ) ;
250
+
251
+ // Find the last <li> element with class slide-tool-item
252
+ let lastItem = ulElement . querySelector ( 'li.slide-tool-item:last-child' ) ;
253
+
254
+ // Calculate the next data-item value
255
+ let nextItemValue = 0 ;
256
+ if ( lastItem ) {
257
+ nextItemValue = parseInt ( lastItem . dataset . item ) + 1 ;
258
+ }
259
+
260
+ // Create a new <li> element
261
+ let newListItem = document . createElement ( 'li' ) ;
262
+ newListItem . className = 'slide-tool-item' ;
263
+ newListItem . dataset . item = nextItemValue . toString ( ) ; // Set the next available data-item value
264
+
265
+ // Create the <a> element inside the <li>
266
+ let newLink = document . createElement ( 'a' ) ;
267
+ newLink . href = '#' ;
268
+ newLink . id = 'qwebrRHistoryButton' ; // Set the ID for the new link
269
+
270
+ // Create the <kbd> element inside the <a>
271
+ let newKbd = document . createElement ( 'kbd' ) ;
272
+ newKbd . textContent = ' ' ; // Set to empty as we are not registering a keyboard shortcut
273
+
274
+ // Create text node for the link text
275
+ let newText = document . createTextNode ( ' View R History' ) ;
276
+
277
+ // Append <kbd> and text node to <a>
278
+ newLink . appendChild ( newKbd ) ;
279
+ newLink . appendChild ( newText ) ;
280
+
281
+ // Append <a> to <li>
282
+ newListItem . appendChild ( newLink ) ;
283
+
284
+ // Append <li> to <ul>
285
+ ulElement . appendChild ( newListItem ) ;
286
+ }
287
+
288
+ // Handle setting up the R history modal
289
+ function qwebrCodeLinks ( ) {
290
+
291
+ if ( qwebrIsRevealJS ( ) ) {
292
+ qwebrRegisterRevealJSCommandHistoryModal ( ) ;
293
+ return ;
294
+ }
295
+
296
+ // Create the container div
297
+ var containerDiv = document . createElement ( 'div' ) ;
298
+ containerDiv . className = 'quarto-code-links' ;
299
+
300
+ // Create the h2 element
301
+ var h2 = document . createElement ( 'h2' ) ;
302
+ h2 . textContent = 'webR Code Links' ;
303
+
304
+ // Create the ul element
305
+ var ul = document . createElement ( 'ul' ) ;
306
+
307
+ // Create the li element
308
+ var li = document . createElement ( 'li' ) ;
309
+
310
+ // Create the a_history_btn element
311
+ var a_history_btn = document . createElement ( 'a' ) ;
312
+ a_history_btn . href = 'javascript:void(0)' ;
313
+ a_history_btn . setAttribute ( 'id' , 'qwebrRHistoryButton' ) ;
314
+
315
+ // Create the i_history_btn element
316
+ var i_history_btn = document . createElement ( 'i' ) ;
317
+ i_history_btn . className = 'bi bi-file-code' ;
318
+
319
+ // Create the text node for the link text
320
+ var text_history_btn = document . createTextNode ( 'View R History' ) ;
321
+
322
+ // Append the icon element and link text to the a element
323
+ a_history_btn . appendChild ( i_history_btn ) ;
324
+ a_history_btn . appendChild ( text_history_btn ) ;
325
+
326
+ // Append the a element to the li element
327
+ li . appendChild ( a_history_btn ) ;
328
+
329
+ // Append the li element to the ul element
330
+ ul . appendChild ( li ) ;
331
+
332
+ // Append the h2 and ul elements to the container div
333
+ containerDiv . appendChild ( h2 ) ;
334
+ containerDiv . appendChild ( ul ) ;
335
+
336
+ // Append the container div to the element with the ID 'quarto-margin-sidebar'
337
+ var sidebar = document . getElementById ( 'quarto-margin-sidebar' ) ;
338
+
339
+ // If the sidebar element is not found, create it
340
+ if ( ! sidebar ) {
341
+ qwebrPlaceQuartoSidebar ( ) ;
342
+ }
343
+
344
+ // Re-select the sidebar element (if it was just created)
345
+ sidebar = document . getElementById ( 'quarto-margin-sidebar' ) ;
346
+
347
+
348
+ // If the sidebar element exists, append the container div to it
349
+ if ( sidebar ) {
350
+ // Append the container div to the sidebar
351
+ sidebar . appendChild ( containerDiv ) ;
352
+ } else {
353
+ // Get a debugger ...
354
+ console . warn ( 'Element with ID "quarto-margin-sidebar" not found.' ) ;
355
+ }
356
+ }
357
+
358
+ // Call the function to append the code links for qwebR into the right sidebar
359
+ qwebrCodeLinks ( ) ;
360
+
361
+ // Add the command history modal
362
+ qwebrAddCommandHistoryModal ( ) ;
363
+
157
364
displayStartupMessage ( qwebrShowStartupMessage , qwebrShowHeaderMessage ) ;
158
365
qwebrOffScreenCanvasSupportWarningMessage ( ) ;
0 commit comments