Syntax highlight now should work on all tools apart from Mock Services. Co-authored-by: Adam Bem <adam.bem@zoho.eu> Reviewed-on: #156 Reviewed-by: Mikolaj Widla <widlam@noreply.example.com>
		
			
				
	
	
		
			64 lines
		
	
	
		
			1.7 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
			
		
		
	
	
			64 lines
		
	
	
		
			1.7 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
/**
 | 
						|
 * This file contains scripts needed for syntax highlight to work.
 | 
						|
 */
 | 
						|
 | 
						|
 | 
						|
/**
 | 
						|
 * This functions highlight element with provided ID.
 | 
						|
 * 
 | 
						|
 * @function
 | 
						|
 * @name highlightSyntax
 | 
						|
 * @kind function
 | 
						|
 * @param {any} elementId
 | 
						|
 * @returns {void}
 | 
						|
 */
 | 
						|
function highlightSyntax(elementId) {
 | 
						|
    const element = document.getElementById(elementId);
 | 
						|
    element.innerHTML = hljs.highlightAuto(element.innerText).value
 | 
						|
}
 | 
						|
 | 
						|
/**
 | 
						|
 * Converts pasted data to plain text
 | 
						|
 * 
 | 
						|
 * @function
 | 
						|
 * @name configurePastingInElement
 | 
						|
 * @kind function
 | 
						|
 * @param {any} elementId
 | 
						|
 * @returns {void}
 | 
						|
 */
 | 
						|
function configurePastingInElement(elementId) {
 | 
						|
  const editorEle = document.getElementById(elementId);
 | 
						|
 | 
						|
  // Handle the `paste` event
 | 
						|
  editorEle.addEventListener('paste', function (e) {
 | 
						|
      // Prevent the default action
 | 
						|
      e.preventDefault();
 | 
						|
 | 
						|
      // Get the copied text from the clipboard
 | 
						|
      const text = e.clipboardData
 | 
						|
          ? (e.originalEvent || e).clipboardData.getData('text/plain')
 | 
						|
          : // For IE
 | 
						|
          window.clipboardData
 | 
						|
              ? window.clipboardData.getData('Text')
 | 
						|
              : '';
 | 
						|
 | 
						|
      if (document.queryCommandSupported('insertText')) {
 | 
						|
          document.execCommand('insertText', false, text);
 | 
						|
      } else {
 | 
						|
          // Insert text at the current position of caret
 | 
						|
          const range = document.getSelection().getRangeAt(0);
 | 
						|
          range.deleteContents();
 | 
						|
 | 
						|
          const textNode = document.createTextNode(text);
 | 
						|
          range.insertNode(textNode);
 | 
						|
          range.selectNodeContents(textNode);
 | 
						|
          range.collapse(false);
 | 
						|
 | 
						|
          const selection = window.getSelection();
 | 
						|
          selection.removeAllRanges();
 | 
						|
          selection.addRange(range);
 | 
						|
      }
 | 
						|
      highlightSyntax(editorEle.id);
 | 
						|
      
 | 
						|
  });
 | 
						|
} |