Co-authored-by: Adam Bem <adam.bem@zoho.eu> Co-authored-by: Dariusz Augustyniak <augustyd@noreply.example.com> Co-authored-by: Mikolaj Widla <widlam@noreply.example.com> Reviewed-on: #177
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);
|
|
|
|
});
|
|
} |