import { getStartPositionInLine, setStartPositionInLine } from './position.js'; import { formatLine, load, save } from './md.js'; function ontextarea(e) { let ta = document.getElementById('ta'); if (ta.style.display == 'none') { ta.style.display = 'inline'; } else { ta.style.display = 'none'; } } function onsave(e) { save(document.getElementById('ta'), document.getElementById('mdnotes')); } function onload(e) { load(document.getElementById('ta'), document.getElementById('mdnotes')); } function onedit(e) { let ret = getStartPositionInLine(); let line = ret[0]; let position = ret[1]; if (line.innerText == '\n') { line.className = 'mdnotes_line'; return; } let newline = formatLine(line.innerText); line.parentNode.replaceChild(newline, line); setStartPositionInLine(newline, position); } function onkeydown(e) { if (e.key != 'Tab') { return true; } e.preventDefault(); let key = ' '; let ret = getStartPositionInLine(); let line = ret[0]; let position = ret[1]; let txt = line.innerText; if (position == 0) { line.innerHTML = key + txt; // 4 espaces =>   } else { line.innerHTML = txt.substring(0, position) + key + txt.substring(position, txt.length); } /*if (line.innerText.match(RegExp(emsp()))) { alert('emsp'); }*/ setStartPositionInLine(line, position + 1); onedit(e); return false; } function onkeyup(e) { if (e.key == 'Escape') { e.preventDefault(); document.getElementById('mdnotes').blur(); return false; } } function init() { let mdnotesdiv = document.getElementById('mdnotes'); mdnotesdiv.addEventListener('input', onedit); mdnotesdiv.addEventListener('keyup', onkeyup); mdnotesdiv.addEventListener('keydown', onkeydown); let saveButton = document.getElementById('saveButton'); saveButton.addEventListener('click', onsave); loadButton.addEventListener('click', onload); taButton.addEventListener('click', ontextarea); onload(); } init();