import { getStartPositionInLine, setStartPositionInLine } from "./position.js"; function load(textarea, div) { div.innerHTML = ''; let lines = textarea.value.split('\n'); for (let i = 0; i < lines.length; i++) { let line = lines[i]; let elem = formatLine(line); div.append(elem); } redrawTables(div, 0); } function appendData(div, data) { const selection = window.getSelection(); selection.deleteFromDocument(); let ret = getStartPositionInLine(); let line = ret[0]; let position = ret[1]; if (line == null) { return; } let lineBegin = line.innerText.substring(0, position); let lineEnd = line.innerText.substring(position, line.innerText.length); let lines = data.split('\n'); let newline; if (lines.length == 1) { newline = formatLine(lineBegin + lines[0] + lineEnd); line.parentNode.replaceChild(newline, line); position = position + lines[0].length; } else { newline = formatLine(lineBegin + lines[0]); line.parentNode.replaceChild(newline, line); let prevline = newline; for(let i=1; i\s*>\s*>(\s|$)/i)) { token = /^(\s*>\s*>\s*>(\s|$))/i; elem.classList.add('bq3'); elem.classList.add('bq'); } else if (line.match(/^\s*>\s*>(\s|$)/i)) { token = /^(\s*>\s*>(\s|$))/i; elem.classList.add('bq2'); elem.classList.add('bq'); } else if (line.match(/^\s*>(\s|$)/i)) { token = /^(\s*>(\s|$))/i; elem.classList.add('bq1'); elem.classList.add('bq'); } else { elem.classList.add('body'); } if (elem.classList.contains('bq') || elem.classList.contains('body')) { if (line.match(/^\s*((>\s*){0,3}\s)?([\*\-+]\s*){3}(\s|$)/i)) { token = /^(\s*(>\s*){0,3}([\*\-+]\s*){3}(\s|$))/i; elem.classList.add('ul3'); elem.classList.remove('body'); } else if (line.match(/^\s*((>\s*){0,3}\s)?([\*\-+]\s*){2}(\s|$)/i)) { token = /^(\s*(>\s*){0,3}([\*\-+]\s*){2}(\s|$))/i; elem.classList.add('ul2'); elem.classList.remove('body'); } else if (line.match(/^\s*((>\s*){0,3}\s)?[\*\-+](\s|$)/i)) { token = /^(\s*(>\s*){0,3}[\*\-+](\s*|$))/; elem.classList.add('ul1'); elem.classList.remove('body'); } else if (line.match(/^\s*((>\s*){0,3}\s)?([0-9]+\.){3}(\s|$)/i)) { token = /^(\s*(>\s*){0,3}(\s*|$))/; elem.classList.add('ol3'); elem.classList.remove('body'); } else if (line.match(/^\s*((>\s*){0,3}\s)?([0-9]+\.){2}(\s|$)/i)) { token = /^(\s*(>\s*){0,3}(\s*|$))/; elem.classList.add('ol2'); elem.classList.remove('body'); } else if (line.match(/^\s*((>\s*){0,3}\s)?[0-9]+\.(\s|$)/i)) { token = /^(\s*(>\s*){0,3}(\s*|$))/; elem.classList.add('ol1'); elem.classList.remove('body'); } } if (token != null) { line = line.replace(token, '$1'); } line = addLink(line); line = addBold(line); line = addItalic(line); line = addTableLine(line); line = addMono(line, listMono); elem.innerHTML = line; if (elem.getElementsByClassName('tablerow').length > 0) { if (elem.childNodes[0].childNodes.length > 0) { let child = elem.childNodes[0].childNodes[0]; while(child != null) { if (child.nodeType == 3) { let newchild = document.createElement('span'); newchild.innerText = child.nodeValue; child.parentNode.replaceChild(newchild, child); child = newchild; } child = child.nextSibling; } } elem.classList.remove('body'); elem.classList.add('tablerow'); } let links = elem.getElementsByClassName('link'); for (let i=0; i 1000) { continue; } cpt++; } matches = matches.reverse(); for(let i=0; i' let line3 = line.substring(match.index + match[0].length, line.length); line = line1 + line2 + line3; } else if (i == 0 && match.index == line.length - 1) { // dernier | let rightdp = ' data-dp="false"'; if (match[0].charAt(0) == ':') { rightdp = ' data-dp="true"'; } let line1 = line.substring(0, match.index); let line2 = '' + match[0] + '' let line3 = line.substring(match.index + match[0].length, line.length); line = line1 + line2 + line3; } else { let rightdp = ' data-dp="false"'; let leftdp = ' data-dp="false"'; if (match[0].length == 3) { rightdp = ' data-dp="true"'; leftdp = ' data-dp="true"'; } else if (match[0].length == 2) { if (match[0].charAt(0) == ':') { rightdp = ' data-dp="true"'; } else { leftdp = ' data-dp="true"'; } } let line1 = line.substring(0, match.index); let line2 = '' + match[0] + ''; let line3 = line.substring(match.index + match[0].length, line.length); line = line1 + line2 + line3; } } line = '' + line + ''; } return line; } function redrawTables(mdnotes, dpwidth = 0) { if (mdnotes.childNodes.length == 0) { return; } let child = mdnotes.childNodes[0]; let firsttableline = null; while(child != null) { if (firsttableline == null) { if (child.classList.contains('tablerow')) { firsttableline = child; } } else { if (! child.classList.contains('tablerow')) { formatTable(firsttableline, dpwidth); firsttableline = null; } } child = child.nextSibling; } if (firsttableline != null) { formatTable(firsttableline, dpwidth); } } function formatTable(line, dpwidth = 0) { if (! line.classList.contains('tablerow')) { return; } let firstline = line; let lastline = line; while (firstline.previousSibling !== null && firstline.previousSibling.classList.contains('tablerow')) { firstline = firstline.previousSibling; firstline.classList.remove('firsttablerow'); firstline.classList.remove('lasttablerow'); } while (lastline.nextSibling !== null && lastline.nextSibling.classList.contains('tablerow')) { lastline = lastline.nextSibling; lastline.classList.remove('firsttablerow'); lastline.classList.remove('lasttablerow'); } firstline.classList.add('firsttablerow'); lastline.classList.add('lasttablerow'); resizeTableCols(firstline, lastline, dpwidth) } function resizeTableCols(firstline, lastline, dpwidth) { let colsmaxwidth = []; let colsjustif = []; let currentline = firstline; while (currentline !== null && currentline.classList.contains('tablerow')) { let tablerow = currentline.getElementsByClassName('tablerow')[0]; let childNodes = tablerow.childNodes; let currentwidth = 0; let col = 0; let currentleft; for(let i=0; i 1000) { continue; } cpt++; } matches = matches.reverse(); for(let i=0; i`' + mono + '`' + line.substring(matches[i]+2, line.length); } return line; } function addLink(line) { line = line.replace(/(\[([^\]]+?)\]\(([^\)]+?)\))/ig, '[$2]($3)'); line = line.replace(/(\[([^\]]+?)\]\(\))/ig, '[$2]()'); line = line.replace(/(\[\]\(([^\)]+?)\))/ig, '[]($2)'); return line; } function addBold(line) { line = line.replace(/\*\*([^\s].*?)\*\*/ig, '**$1**'); line = line.replace(/__([^\s].*?)__/ig, '__$1__'); return line; } function addItalic(line) { line = line.replace(/(^|[^\*])\*([^\*\s].*?[^\*\s]|[^\*\s])\*([^\*]|$)/ig, '$1*$2*$3'); line = line.replace(/(^|[\W])_([^_\s].*?[^_\s]|[^_\s])_([^_]|$)/ig, '$1_$2_$3'); return line; } export { load, save, formatLine, formatTable, redrawTables, dpwidth, appendData };