818
Du
dustymarkdown / files
dustymarkdown / node_modules / node-markdown / lib / vendor / showdown / example / showdown-gui.js
8.9 KB
1// 2// showdown-gui.js 3// 4// A sample application for Showdown, a javascript port 5// of Markdown. 6// 7// Copyright (c) 2007 John Fraser. 8// 9// Redistributable under a BSD-style open source license. 10// See license.txt for more information. 11// 12// The full source distribution is at: 13// 14// A A L 15// T C A 16// T K B 17// 18// <http://www.attacklab.net/> 19// 20 21// 22// The Showdown converter itself is in showdown.js, which must be 23// included by the HTML before this file is. 24// 25// showdown-gui.js assumes the id and class definitions in 26// showdown.html. It isn't dependent on the CSS, but it does 27// manually hide, display, and resize the individual panes -- 28// overriding the stylesheets. 29// 30// This sample application only interacts with showdown.js in 31// two places: 32// 33// In startGui(): 34// 35// converter = new Showdown.converter(); 36// 37// In convertText(): 38// 39// text = converter.makeHtml(text); 40// 41// The rest of this file is user interface stuff. 42// 43 44 45// 46// Register for onload 47// 48window.onload = startGui; 49 50 51// 52// Globals 53// 54 55var converter; 56var convertTextTimer,processingTime; 57var lastText,lastOutput,lastRoomLeft; 58var convertTextSetting, convertTextButton, paneSetting; 59var inputPane,previewPane,outputPane,syntaxPane; 60var maxDelay = 3000; // longest update pause (in ms) 61 62 63// 64// Initialization 65// 66 67function startGui() { 68 // find elements 69 convertTextSetting = document.getElementById("convertTextSetting"); 70 convertTextButton = document.getElementById("convertTextButton"); 71 paneSetting = document.getElementById("paneSetting"); 72 73 inputPane = document.getElementById("inputPane"); 74 previewPane = document.getElementById("previewPane"); 75 outputPane = document.getElementById("outputPane"); 76 syntaxPane = document.getElementById("syntaxPane"); 77 78 // set event handlers 79 convertTextSetting.onchange = onConvertTextSettingChanged; 80 convertTextButton.onclick = onConvertTextButtonClicked; 81 paneSetting.onchange = onPaneSettingChanged; 82 window.onresize = setPaneHeights; 83 84 // First, try registering for keyup events 85 // (There's no harm in calling onInput() repeatedly) 86 window.onkeyup = inputPane.onkeyup = onInput; 87 88 // In case we can't capture paste events, poll for them 89 var pollingFallback = window.setInterval(function(){ 90 if(inputPane.value != lastText) 91 onInput(); 92 },1000); 93 94 // Try registering for paste events 95 inputPane.onpaste = function() { 96 // It worked! Cancel paste polling. 97 if (pollingFallback!=undefined) { 98 window.clearInterval(pollingFallback); 99 pollingFallback = undefined; 100 } 101 onInput(); 102 } 103 104 // Try registering for input events (the best solution) 105 if (inputPane.addEventListener) { 106 // Let's assume input also fires on paste. 107 // No need to cancel our keyup handlers; 108 // they're basically free. 109 inputPane.addEventListener("input",inputPane.onpaste,false); 110 } 111 112 // poll for changes in font size 113 // this is cheap; do it often 114 window.setInterval(setPaneHeights,250); 115 116 // start with blank page? 117 if (top.document.location.href.match(/\?blank=1$/)) 118 inputPane.value = ""; 119 120 // refresh panes to avoid a hiccup 121 onPaneSettingChanged(); 122 123 // build the converter 124 converter = new Showdown.converter(); 125 126 // do an initial conversion to avoid a hiccup 127 convertText(); 128 129 // give the input pane focus 130 inputPane.focus(); 131 132 // start the other panes at the top 133 // (our smart scrolling moved them to the bottom) 134 previewPane.scrollTop = 0; 135 outputPane.scrollTop = 0; 136} 137 138 139// 140// Conversion 141// 142 143function convertText() { 144 // get input text 145 var text = inputPane.value; 146 147 // if there's no change to input, cancel conversion 148 if (text && text == lastText) { 149 return; 150 } else { 151 lastText = text; 152 } 153 154 var startTime = new Date().getTime(); 155 156 // Do the conversion 157 text = converter.makeHtml(text); 158 159 // display processing time 160 var endTime = new Date().getTime(); 161 processingTime = endTime - startTime; 162 document.getElementById("processingTime").innerHTML = processingTime+" ms"; 163 164 // save proportional scroll positions 165 saveScrollPositions(); 166 167 // update right pane 168 if (paneSetting.value == "outputPane") { 169 // the output pane is selected 170 outputPane.value = text; 171 } else if (paneSetting.value == "previewPane") { 172 // the preview pane is selected 173 previewPane.innerHTML = text; 174 } 175 176 lastOutput = text; 177 178 // restore proportional scroll positions 179 restoreScrollPositions(); 180}; 181 182 183// 184// Event handlers 185// 186 187function onConvertTextSettingChanged() { 188 // If the user just enabled automatic 189 // updates, we'll do one now. 190 onInput(); 191} 192 193function onConvertTextButtonClicked() { 194 // hack: force the converter to run 195 lastText = ""; 196 197 convertText(); 198 inputPane.focus(); 199} 200 201function onPaneSettingChanged() { 202 previewPane.style.display = "none"; 203 outputPane.style.display = "none"; 204 syntaxPane.style.display = "none"; 205 206 // now make the selected one visible 207 top[paneSetting.value].style.display = "block"; 208 209 lastRoomLeft = 0; // hack: force resize of new pane 210 setPaneHeights(); 211 212 if (paneSetting.value == "outputPane") { 213 // Update output pane 214 outputPane.value = lastOutput; 215 } else if (paneSetting.value == "previewPane") { 216 // Update preview pane 217 previewPane.innerHTML = lastOutput; 218 } 219} 220 221function onInput() { 222// In "delayed" mode, we do the conversion at pauses in input. 223// The pause is equal to the last runtime, so that slow 224// updates happen less frequently. 225// 226// Use a timer to schedule updates. Each keystroke 227// resets the timer. 228 229 // if we already have convertText scheduled, cancel it 230 if (convertTextTimer) { 231 window.clearTimeout(convertTextTimer); 232 convertTextTimer = undefined; 233 } 234 235 if (convertTextSetting.value != "manual") { 236 var timeUntilConvertText = 0; 237 if (convertTextSetting.value == "delayed") { 238 // make timer adaptive 239 timeUntilConvertText = processingTime; 240 } 241 242 if (timeUntilConvertText > maxDelay) 243 timeUntilConvertText = maxDelay; 244 245 // Schedule convertText(). 246 // Even if we're updating every keystroke, use a timer at 0. 247 // This gives the browser time to handle other events. 248 convertTextTimer = window.setTimeout(convertText,timeUntilConvertText); 249 } 250} 251 252 253// 254// Smart scrollbar adjustment 255// 256// We need to make sure the user can't type off the bottom 257// of the preview and output pages. We'll do this by saving 258// the proportional scroll positions before the update, and 259// restoring them afterwards. 260// 261 262var previewScrollPos; 263var outputScrollPos; 264 265function getScrollPos(element) { 266 // favor the bottom when the text first overflows the window 267 if (element.scrollHeight <= element.clientHeight) 268 return 1.0; 269 return element.scrollTop/(element.scrollHeight-element.clientHeight); 270} 271 272function setScrollPos(element,pos) { 273 element.scrollTop = (element.scrollHeight - element.clientHeight) * pos; 274} 275 276function saveScrollPositions() { 277 previewScrollPos = getScrollPos(previewPane); 278 outputScrollPos = getScrollPos(outputPane); 279} 280 281function restoreScrollPositions() { 282 // hack for IE: setting scrollTop ensures scrollHeight 283 // has been updated after a change in contents 284 previewPane.scrollTop = previewPane.scrollTop; 285 286 setScrollPos(previewPane,previewScrollPos); 287 setScrollPos(outputPane,outputScrollPos); 288} 289 290// 291// Textarea resizing 292// 293// Some browsers (i.e. IE) refuse to set textarea 294// percentage heights in standards mode. (But other units? 295// No problem. Percentage widths? No problem.) 296// 297// So we'll do it in javascript. If IE's behavior ever 298// changes, we should remove this crap and do 100% textarea 299// heights in CSS, because it makes resizing much smoother 300// on other browsers. 301// 302 303function getTop(element) { 304 var sum = element.offsetTop; 305 while(element = element.offsetParent) 306 sum += element.offsetTop; 307 return sum; 308} 309 310function getElementHeight(element) { 311 var height = element.clientHeight; 312 if (!height) height = element.scrollHeight; 313 return height; 314} 315 316function getWindowHeight(element) { 317 if (window.innerHeight) 318 return window.innerHeight; 319 else if (document.documentElement && document.documentElement.clientHeight) 320 return document.documentElement.clientHeight; 321 else if (document.body) 322 return document.body.clientHeight; 323} 324 325function setPaneHeights() { 326 var textarea = inputPane; 327 var footer = document.getElementById("footer"); 328 329 var windowHeight = getWindowHeight(); 330 var footerHeight = getElementHeight(footer); 331 var textareaTop = getTop(textarea); 332 333 // figure out how much room the panes should fill 334 var roomLeft = windowHeight - footerHeight - textareaTop; 335 336 if (roomLeft < 0) roomLeft = 0; 337 338 // if it hasn't changed, return 339 if (roomLeft == lastRoomLeft) { 340 return; 341 } 342 lastRoomLeft = roomLeft; 343 344 // resize all panes 345 inputPane.style.height = roomLeft + "px"; 346 previewPane.style.height = roomLeft + "px"; 347 outputPane.style.height = roomLeft + "px"; 348 syntaxPane.style.height = roomLeft + "px"; 349}