Created
March 15, 2016 01:09
-
-
Save jools-r/65d5c36acabf7a404a75 to your computer and use it in GitHub Desktop.
Textpattern textile syntax highlighting of body and excerpt textareas using in-built prism js (v4.6) – first pass
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- paste this in the "additional js code" window of write_tab_customize or include via theme --> | |
<style> | |
#main_content .text .microcode-wrapper { | |
position: relative; | |
height:auto; | |
margin-bottom | |
} | |
#main_content .text .microcode-wrapper .code-input, | |
#main_content .text .microcode-wrapper .code-output { | |
position: absolute; | |
top: 0; | |
left: 0; | |
right: 0; | |
height: 100%; | |
padding: 1rem; | |
background: transparent; | |
white-space: pre-wrap; | |
font-family: Consolas,Menlo,Monaco,monospace; | |
font-size: 14px; | |
line-height: 1.5em; | |
word-wrap: break-word; | |
} | |
#main_content .text .microcode-wrapper .code-input { | |
opacity: 0.7; | |
margin: 0; | |
color: #999; | |
resize: none; | |
} | |
#main_content .text .microcode-wrapper .code-output { | |
pointer-events: none; | |
border-color: transparent; | |
z-index: 3; | |
margin: 0; | |
overflow-y: auto; | |
line-height: 1.5; | |
} | |
#main_content .text .microcode-wrapper .code-output code { | |
position: absolute; | |
top: 0; | |
left: 0; | |
right: 0; | |
margin: 0; | |
padding: 1rem; | |
display: block; | |
color: #666; | |
font-size: 14px; | |
font-family: Consolas,Menlo,Monaco,monospace; | |
word-wrap: break-word; | |
white-space: pre-wrap; | |
} | |
</style> | |
<script> | |
;var MicroCode = (function(){ | |
return { | |
init: function(inputSel, outputSel){ | |
this.focusInput(inputSel); | |
this.resizeOutput(outputSel, $(inputSel).outerHeight() ); | |
this.listenForInput(inputSel, outputSel); | |
this.renderOutput(outputSel, $(inputSel)[0].value); | |
this.listenerForScroll(inputSel, outputSel); | |
this.listenerForResize(inputSel, outputSel); | |
}, | |
listenForInput: function(inputSel, outputSel){ | |
var self = this; | |
$(inputSel).on('input keydown', function(key){ | |
var input = this, | |
selStartPos = input.selectionStart, | |
inputVal = input.value; | |
if(key.keyCode === 9){ | |
input.value = inputVal.substring(0, selStartPos) + " " + inputVal.substring(selStartPos, input.value.length); | |
input.selectionStart = selStartPos + 4; | |
input.selectionEnd = selStartPos + 4; | |
key.preventDefault(); | |
} | |
self.renderOutput(outputSel, this.value); | |
}); | |
Prism.highlightAll(); | |
}, | |
listenerForScroll: function(inputSel, outputSel){ | |
$(inputSel).on('scroll', function(){ | |
console.log(this.scrollTop); | |
$(outputSel)[0].scrollTop = this.scrollTop; | |
}); | |
}, | |
listenerForResize: function(inputSel, outputSel){ | |
var self = this; | |
$(inputSel).on('autosize:resized', function() { | |
self.resizeOutput(outputSel, $(inputSel).outerHeight() ); | |
}); | |
}, | |
resizeOutput: function(outputSel, value){ | |
$(outputSel).add($(outputSel).parent()).height(value); | |
}, | |
renderOutput: function(outputSel, value){ | |
$('code', outputSel) | |
.html(value | |
.replace(/&/g, "&") | |
.replace(/</g, "<") | |
.replace(/>/g, ">") + "\n"); | |
Prism.highlightAll(); | |
}, | |
focusInput: function(inputSel){ | |
var input = $(inputSel); | |
input.focus(); | |
input[0].selectionStart = input[0].value.length; | |
input[0].selectionEnd = input[0].value.length; | |
} | |
} | |
})(); | |
$(function() { | |
// change "textile" to "markdown" to use markdown syntax highlighting | |
var MicroCodeSyntax = 'textile'; | |
var $Textareas = $('#body').add('#excerpt'); | |
// add wrapper and input class | |
$Textareas.addClass('code-input').wrap('<div class="microcode-wrapper"></div>'); | |
// add styled code output container | |
$('<pre class="body-output code-output language-' + MicroCodeSyntax + '"><code class="language-' + MicroCodeSyntax + '"></code></pre>').insertAfter('#body'); | |
$('<pre class="excerpt-output code-output language-' + MicroCodeSyntax + '"><code class="language-' + MicroCodeSyntax + '"></code></pre>').insertAfter('#excerpt'); | |
// initiate MicroCode syntax highlighting | |
MicroCode.init('#body', '.body-output'); | |
MicroCode.init('#excerpt', '.excerpt-output'); | |
}); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Based on @kazzkiq's codepen.