| | @@ -13,28 +13,35 @@ |
| 13 | 13 | This is the main entry point for the WASM rendition of fossil's |
| 14 | 14 | /pikchrshow app. It sets up the various UI bits, loads a Worker for |
| 15 | 15 | the pikchr process, and manages the communication between the UI and |
| 16 | 16 | worker. |
| 17 | 17 | |
| 18 | | - API dependencies: fossil.dom, fossil.storage |
| 18 | + API dependencies: fossil.dom, fossil.copybutton, fossil.storage |
| 19 | 19 | */ |
| 20 | 20 | (function(F/*fossil object*/){ |
| 21 | 21 | 'use strict'; |
| 22 | 22 | |
| 23 | 23 | /* Recall that the 'self' symbol, except where locally |
| 24 | 24 | overwritten, refers to the global window or worker object. */ |
| 25 | 25 | |
| 26 | + const D = F.dom; |
| 26 | 27 | /** Name of the stored copy of this app's config. */ |
| 27 | 28 | const configStorageKey = 'pikchrshow-config'; |
| 28 | 29 | |
| 29 | | - /** |
| 30 | | - The PikchrFiddle object is intended to be the primary app-level |
| 31 | | - object for the main-thread side of the fiddle application. It |
| 32 | | - uses a worker thread to load the WASM module and communicate |
| 33 | | - with it. |
| 34 | | - */ |
| 35 | | - const PS/*local convenience alias*/ = F.PikchrShow/*canonical name*/ = { |
| 30 | + /* querySelectorAll() proxy */ |
| 31 | + const EAll = function(/*[element=document,] cssSelector*/){ |
| 32 | + return (arguments.length>1 ? arguments[0] : document) |
| 33 | + .querySelectorAll(arguments[arguments.length-1]); |
| 34 | + }; |
| 35 | + /* querySelector() proxy */ |
| 36 | + const E = function(/*[element=document,] cssSelector*/){ |
| 37 | + return (arguments.length>1 ? arguments[0] : document) |
| 38 | + .querySelector(arguments[arguments.length-1]); |
| 39 | + }; |
| 40 | + |
| 41 | + /** The main application object. */ |
| 42 | + const PS = { |
| 36 | 43 | /* Config options. */ |
| 37 | 44 | config: { |
| 38 | 45 | /* If true, display input/output areas side-by-side, else stack |
| 39 | 46 | them vertically. */ |
| 40 | 47 | sideBySide: true, |
| | @@ -41,15 +48,27 @@ |
| 41 | 48 | /* If true, swap positions of the input/output areas. */ |
| 42 | 49 | swapInOut: false, |
| 43 | 50 | /* If true, the SVG is allowed to resize to fit the parent |
| 44 | 51 | content area, else the parent is resized to fit the rendered |
| 45 | 52 | SVG (as sized by pikchr). */ |
| 46 | | - renderAutoScale: false, |
| 53 | + renderAutofit: false, |
| 47 | 54 | /* If true, automatically render while the user is typing. */ |
| 48 | 55 | renderWhileTyping: false |
| 49 | 56 | }, |
| 50 | | - renderMode: 'html'/*one of: 'text','html'*/, |
| 57 | + /* Various DOM elements. */ |
| 58 | + e: { |
| 59 | + previewCopyButton: E('#preview-copy-button'), |
| 60 | + previewModeLabel: E('label[for=preview-copy-button]'), |
| 61 | + zoneOutputButtons: E('.zone-wrapper.output > legend > .button-bar'), |
| 62 | + outText: E('#pikchr-output-text'), |
| 63 | + pikOutWrapper: E('#pikchr-output-wrapper'), |
| 64 | + pikOut: E('#pikchr-output') |
| 65 | + }, |
| 66 | + renderModes: ['svg'/*SVG must be at index 0*/,'markdown', 'wiki', 'text'], |
| 67 | + renderModeLabels: { |
| 68 | + svg: 'SVG', markdown: 'Markdown', wiki: 'Fossil Wiki', text: 'Text' |
| 69 | + }, |
| 51 | 70 | _msgMap: {}, |
| 52 | 71 | /** Adds a worker message handler for messages of the given |
| 53 | 72 | type. */ |
| 54 | 73 | addMsgHandler: function f(type,callback){ |
| 55 | 74 | if(Array.isArray(type)){ |
| | @@ -85,11 +104,11 @@ |
| 85 | 104 | /** Stores this object's config in the browser's storage. */ |
| 86 | 105 | storeConfig: function(){ |
| 87 | 106 | F.storage.setJSON(configStorageKey,this.config); |
| 88 | 107 | } |
| 89 | 108 | }; |
| 90 | | - |
| 109 | + PS.renderModes.selectedIndex = 0; |
| 91 | 110 | PS._config = F.storage.getJSON(configStorageKey); |
| 92 | 111 | if(PS._config){ |
| 93 | 112 | /* Copy all properties to PS.config which are currently in |
| 94 | 113 | PS._config. We don't bother copying any other properties: those |
| 95 | 114 | would be stale/removed config entries. */ |
| | @@ -104,21 +123,10 @@ |
| 104 | 123 | PS.worker = new Worker('builtin/extsrc/pikchr-worker.js'); |
| 105 | 124 | PS.worker.onmessage = (ev)=>PS.runMsgHandlers(ev.data); |
| 106 | 125 | PS.addMsgHandler('stdout', console.log.bind(console)); |
| 107 | 126 | PS.addMsgHandler('stderr', console.error.bind(console)); |
| 108 | 127 | |
| 109 | | - /* querySelectorAll() proxy */ |
| 110 | | - const EAll = function(/*[element=document,] cssSelector*/){ |
| 111 | | - return (arguments.length>1 ? arguments[0] : document) |
| 112 | | - .querySelectorAll(arguments[arguments.length-1]); |
| 113 | | - }; |
| 114 | | - /* querySelector() proxy */ |
| 115 | | - const E = function(/*[element=document,] cssSelector*/){ |
| 116 | | - return (arguments.length>1 ? arguments[0] : document) |
| 117 | | - .querySelector(arguments[arguments.length-1]); |
| 118 | | - }; |
| 119 | | - |
| 120 | 128 | /** Handles status updates from the Module object. */ |
| 121 | 129 | PS.addMsgHandler('module', function f(ev){ |
| 122 | 130 | ev = ev.data; |
| 123 | 131 | if('status'!==ev.type){ |
| 124 | 132 | console.warn("Unexpected module-type message:",ev); |
| | @@ -156,10 +164,13 @@ |
| 156 | 164 | after the last "load" message has arrived, so |
| 157 | 165 | leave f.ui.status and message listener intact. */ |
| 158 | 166 | } |
| 159 | 167 | }); |
| 160 | 168 | |
| 169 | + PS.e.previewModeLabel.innerText = |
| 170 | + PS.renderModeLabels[PS.renderModes[PS.renderModes.selectedIndex]]; |
| 171 | + |
| 161 | 172 | /** |
| 162 | 173 | The 'pikchrshow-ready' event is fired (with no payload) when the |
| 163 | 174 | wasm module has finished loading. */ |
| 164 | 175 | PS.addMsgHandler('pikchrshow-ready', function(){ |
| 165 | 176 | PS.clearMsgHandlers('pikchrshow-ready'); |
| | @@ -287,57 +298,60 @@ |
| 287 | 298 | pikchr: txt, |
| 288 | 299 | darkMode: !!window.fossil.config.skin.isDark |
| 289 | 300 | }); |
| 290 | 301 | }; |
| 291 | 302 | |
| 292 | | - const eOut = E('#pikchr-output'); |
| 293 | | - const eOutWrapper = E('#pikchr-output-wrapper'); |
| 294 | 303 | PS.addMsgHandler('pikchr', function(ev){ |
| 295 | 304 | const m = ev.data; |
| 296 | | - eOut.classList[m.isError ? 'add' : 'remove']('error'); |
| 297 | | - eOut.dataset.pikchr = m.pikchr; |
| 298 | | - let content; |
| 299 | | - let sz; |
| 300 | | - switch(PS.renderMode){ |
| 305 | + PS.e.pikOut.classList[m.isError ? 'add' : 'remove']('error'); |
| 306 | + PS.e.pikOut.dataset.pikchr = m.pikchr; |
| 307 | + const mode = PS.renderModes[PS.renderModes.selectedIndex]; |
| 308 | + switch(mode){ |
| 301 | 309 | case 'text': |
| 302 | | - content = '<textarea>'+m.result+'</textarea>'; |
| 303 | | - eOut.classList.add('text'); |
| 304 | | - eOutWrapper.classList.add('text'); |
| 305 | | - break; |
| 306 | | - default: |
| 307 | | - content = m.result; |
| 308 | | - eOut.classList.remove('text'); |
| 309 | | - eOutWrapper.classList.remove('text'); |
| 310 | | - break; |
| 311 | | - } |
| 312 | | - eOut.innerHTML = content; |
| 310 | + case 'markdown': |
| 311 | + case 'wiki': { |
| 312 | + const body = [m.result]; |
| 313 | + if('markdown'===mode){ |
| 314 | + body.unshift('```pikchr'); |
| 315 | + body.push('```'); |
| 316 | + }else if('wiki'===mode){ |
| 317 | + body.unshift('<verbatim type="pikchr">'); |
| 318 | + body.push('</verbatim>'); |
| 319 | + } |
| 320 | + PS.e.outText.value = body.join('\n'); |
| 321 | + PS.e.outText.classList.remove('hidden'); |
| 322 | + PS.e.pikOut.classList.add('hidden'); |
| 323 | + PS.e.pikOutWrapper.classList.add('text'); |
| 324 | + break; |
| 325 | + } |
| 326 | + case 'svg': |
| 327 | + PS.e.outText.classList.add('hidden'); |
| 328 | + PS.e.pikOut.classList.remove('hidden'); |
| 329 | + PS.e.pikOutWrapper.classList.remove('text'); |
| 330 | + PS.e.pikOut.innerHTML = m.result; |
| 331 | + PS.e.outText.value = m.result/*for clipboard copy*/; |
| 332 | + break; |
| 333 | + default: throw new Error("Unhandled render mode: "+mode); |
| 334 | + } |
| 313 | 335 | let vw = null, vh = null; |
| 314 | | - if(!PS.config.renderAutoScale |
| 315 | | - && !m.isError && 'html'===PS.renderMode){ |
| 316 | | - const svg = E(eOut,':scope > svg'); |
| 317 | | - const vb = svg ? svg.getAttribute('viewBox').split(' ') : false; |
| 318 | | - if(vb && 4===vb.length){ |
| 319 | | - vw = (+vb[2] + 10)+'px'; |
| 320 | | - vh = (+vb[3] + 10)+'px'; |
| 321 | | - }else if(svg){ |
| 322 | | - console.warn("SVG element is missing viewBox attribute."); |
| 323 | | - } |
| 324 | | - } |
| 325 | | - eOut.style.width = vw; |
| 326 | | - eOut.style.height = vh; |
| 336 | + if('svg'===mode && !PS.config.renderAutofit && !m.isError){ |
| 337 | + vw = m.width; vh = m.height; |
| 338 | + } |
| 339 | + PS.e.pikOut.style.width = vw ? vw+'px' : null; |
| 340 | + PS.e.pikOut.style.height = vh ? vh+'px' : null; |
| 327 | 341 | })/*'pikchr' msg handler*/; |
| 328 | 342 | |
| 329 | 343 | E('#btn-render-mode').addEventListener('click',function(){ |
| 330 | | - let mode = PS.renderMode; |
| 331 | | - const modes = ['text','html']; |
| 332 | | - let ndx = modes.indexOf(mode) + 1; |
| 333 | | - if(ndx>=modes.length) ndx = 0; |
| 334 | | - PS.renderMode = modes[ndx]; |
| 335 | | - if(eOut.dataset.pikchr){ |
| 336 | | - PS.render(eOut.dataset.pikchr); |
| 344 | + const modes = PS.renderModes; |
| 345 | + modes.selectedIndex = (modes.selectedIndex + 1) % modes.length; |
| 346 | + PS.e.previewModeLabel.innerText = PS.renderModeLabels[modes[modes.selectedIndex]]; |
| 347 | + if(PS.e.pikOut.dataset.pikchr){ |
| 348 | + PS.render(PS.e.pikOut.dataset.pikchr); |
| 337 | 349 | } |
| 338 | 350 | }); |
| 351 | + F.copyButton(PS.e.previewCopyButton, {copyFromElement: PS.e.outText}); |
| 352 | + PS.e.previewModeLabel.addEventListener('click', ()=>PS.e.previewCopyButton.click(), false); |
| 339 | 353 | |
| 340 | 354 | PS.addMsgHandler('working',function f(ev){ |
| 341 | 355 | switch(ev.data){ |
| 342 | 356 | case 'start': /* See notes in preStartWork(). */; return; |
| 343 | 357 | case 'end': |
| | @@ -378,15 +392,15 @@ |
| 378 | 392 | e.addEventListener('change', function(){ |
| 379 | 393 | PS.config[this.dataset.config] = this.checked; |
| 380 | 394 | PS.storeConfig(); |
| 381 | 395 | }, false); |
| 382 | 396 | }); |
| 383 | | - E('#opt-cb-autoscale').addEventListener('change',function(){ |
| 384 | | - /* PS.config.renderAutoScale was set by the data-config |
| 397 | + E('#opt-cb-autofit').addEventListener('change',function(){ |
| 398 | + /* PS.config.renderAutofit was set by the data-config |
| 385 | 399 | event handler. */ |
| 386 | | - if('html'==PS.renderMode && eOut.dataset.pikchr){ |
| 387 | | - PS.render(eOut.dataset.pikchr); |
| 400 | + if(0==PS.renderModes.selectedIndex && PS.e.pikOut.dataset.pikchr){ |
| 401 | + PS.render(PS.e.pikOut.dataset.pikchr); |
| 388 | 402 | } |
| 389 | 403 | }); |
| 390 | 404 | /* For each button with data-cmd=X, map a click handler which |
| 391 | 405 | calls PS.render(X). */ |
| 392 | 406 | const cmdClick = function(){PS.render(this.dataset.cmd);}; |
| 393 | 407 | |