diff --git a/forum/qa-content/css/shCore.css b/forum/qa-content/css/shCore.css index 2e182303..01c77688 100644 --- a/forum/qa-content/css/shCore.css +++ b/forum/qa-content/css/shCore.css @@ -54,7 +54,7 @@ .syntaxhighlighter { width: 100% !important; - margin: 1em 0 1em 0 !important; + margin: /*1em*/ 0.20em 0 1em 0 !important; position: relative !important; overflow: auto !important; font-size: 1em !important; diff --git a/forum/qa-content/qa-page.js b/forum/qa-content/qa-page.js index bfad28bb..a045bc3e 100644 --- a/forum/qa-content/qa-page.js +++ b/forum/qa-content/qa-page.js @@ -186,14 +186,166 @@ function qa_ajax_error() */ ;(function(document) { + //// + // clear console (for devloping purposes - clears errors from console) + console.clear(); + //// + 'use strict'; + // boolean flag, to check if browser supports 'select()' and 'copy' command + var prepareClipboardCopying; + + if (window.getSelection && document.queryCommandSupported('copy')) + prepareClipboardCopying = true; + else + prepareClipboardCopying = false; + + + /* + * Feature: preview HTML/CSS/JavaScript code from chosen post in codepen.io / jsfiddle.net + */ + function previewFrontEnd() + { + var posts = Array.from(document.querySelectorAll('.entry-content')); + var postsWithBlocks = []; + posts.forEach(function(post) + { + var query = post.querySelectorAll('.syntaxhighlighter-parent'); + if (query.length) + { + //// postsWithBlocks.push(Array.from(query)); + + var blocksInPost = {}; + var data = {}; + var htmlCode = ''; + var cssCode = ''; + var jsCode = ''; + var parent = Array.from(query)[0].parentNode.parentNode; + + Array.from(query).forEach(function(block, index) + { + var code = ''; + Array.from(block.querySelectorAll('.code .line')).forEach(function(line) + { + code += line.textContent + '\r\n'; + }); + blocksInPost[block.firstElementChild.nextSibling.classList[1]] = code /*block.firstElementChild.nextSibling*/; + }); + ////postsWithBlocks.push(blocksInPost); + Object.keys(blocksInPost).forEach(function(language) + { + switch (language) + { + case 'css' : cssCode += blocksInPost.css; + data.css = cssCode; + break; + case 'xml' : htmlCode += blocksInPost.xml; + data.html = htmlCode; + break; + case 'jscript' : jsCode += blocksInPost.jscript; + data.js = jsCode; + break; + } + }); + + var JSONstring = + JSON.stringify(data) + // Quotes will screw up the JSON + .replace(/"/g, "&​quot;") // careful copy and pasting, I had to use a zero-width space here to get markdown to post this. + .replace(/'/g, "'"); + + var form = document.createElement('form'); + form.action = 'http://codepen.io/pen/define'; + form.method = 'POST'; + form.target= '_blank'; + form.style.backgroundColor = 'red'; + + var input1 = document.createElement('input'); + input1.type = 'hidden'; + input1.name = 'data'; + input1.value = JSONstring; + + var input2 = document.createElement('input'); + input2.type = 'image'; + input2.src = 'http://s.cdpn.io/3/cp-arrow-right.svg'; + input2.width = 40; + input2.height = 40; + input2.value = 'Create new CODEPEN'; + + form.appendChild(input1); + form.appendChild(input2); + + ////var parent = document.querySelector('.qa-q-view-content'); + var before = parent.firstElementChild; + + parent.appendChild(form); + + } + }); + } + + /* + * Feature: copy code from code-block to clipboard on button click - then user can paste it wherever he wants into + */ + function copyToClipboard(ev) + { + // prevent page refresh (or something weird) as default button action + ev.preventDefault(); + + var code = []; + var t = ev.target; + var parent = t.parentNode.parentNode; + + // get block of code content - practically all lines of code inside + Array.from(parent.querySelector('.code .container').children).forEach(function(lineOfCode) + { + code.push(lineOfCode.textContent); + }); + + /* + * In order to be able to copy the code inside block into the clipboard - so user can easily paste it wherever he wants - within single button click + * a code must be first selected (or highlighted in human meaning), so JavaScript can copy it. + * However selecting is only possible on HTML elements that are 'inputs', such as