You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
177 lines
4.8 KiB
177 lines
4.8 KiB
import { getStartPositionInLine, setStartPositionInLine } from './position.js';
|
|
import { formatLine, load, save, formatTable, redrawTables, appendData, dpwidth } from './md.js';
|
|
|
|
function timeoutSave() {
|
|
if (window.tos !== null) {
|
|
window.clearTimeout(window.tos);
|
|
}
|
|
window.tos = window.setTimeout(onsave, 5000);
|
|
}
|
|
|
|
function ontextarea(e) {
|
|
let ta = document.getElementById('ta');
|
|
if (ta.style.display != 'inline') {
|
|
ta.style.display = 'inline';
|
|
} else {
|
|
ta.style.display = 'none';
|
|
}
|
|
}
|
|
|
|
function onsave(e) {
|
|
let saveButton = document.getElementById('saveButton');
|
|
saveButton.disabled = true;
|
|
let text = save(document.getElementById('ta'), document.getElementById('mdnotes'));
|
|
var xhttp = new XMLHttpRequest();
|
|
xhttp.onreadystatechange = function() {
|
|
if (this.readyState == 4 && this.status != 200) {
|
|
saveButton.disabled = false;
|
|
alert(xhttp.responseText);
|
|
}
|
|
}
|
|
xhttp.open("PUT", document.location.href, true);
|
|
xhttp.send(text);
|
|
}
|
|
|
|
function onload(e) {
|
|
load(document.getElementById('ta'), document.getElementById('mdnotes'));
|
|
}
|
|
|
|
function onedit(e) {
|
|
console.log('onedit');
|
|
timeoutSave();
|
|
saveButton.disabled = false;
|
|
let ret = getStartPositionInLine();
|
|
let line = ret[0];
|
|
let position = ret[1];
|
|
|
|
if (line.innerText == '\n') {
|
|
line.className = 'mdnotes_line';
|
|
line.classList.add('body');
|
|
return;
|
|
}
|
|
|
|
let prevline = line.previousSibling;
|
|
if (prevline != null && prevline.innerText == '\n') {
|
|
prevline.className = 'mdnotes_line';
|
|
prevline.classList.add('body');
|
|
}
|
|
|
|
let newline = formatLine(line.innerText);
|
|
line.parentNode.replaceChild(newline, line);
|
|
formatTable(newline, dpwidth());
|
|
setStartPositionInLine(newline, position);
|
|
console.log('onedit fin');
|
|
}
|
|
|
|
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);
|
|
}
|
|
setStartPositionInLine(line, position + 1);
|
|
onedit(e);
|
|
return false;
|
|
}
|
|
|
|
function onkeypress(e) {
|
|
if (e.key == 'Escape') {
|
|
e.preventDefault();
|
|
document.getElementById('mdnotes').blur();
|
|
if (saveButton.disabled == false) {
|
|
onsave();
|
|
}
|
|
return false;
|
|
}
|
|
if (e.key == 'Enter') {
|
|
|
|
}
|
|
}
|
|
|
|
function onkeyup(e) {
|
|
if (e.key == 'Escape') {
|
|
e.preventDefault();
|
|
document.getElementById('mdnotes').blur();
|
|
if (saveButton.disabled == false) {
|
|
onsave();
|
|
}
|
|
return false;
|
|
}
|
|
}
|
|
|
|
function onpaste(e) {
|
|
e.preventDefault();
|
|
timeoutSave();
|
|
let data = e.clipboardData.getData('text/plain');
|
|
appendData(document.getElementById('mdnotes'), data);
|
|
saveButton.disabled = false;
|
|
return false;
|
|
}
|
|
|
|
function oncopy(e) {
|
|
//alert(e.clipboardData.getData('text/plain'));
|
|
}
|
|
|
|
function onlockbutton(e) {
|
|
let lockButton = document.getElementById('lockButton');
|
|
let mdnotes = document.getElementById('mdnotes');
|
|
if (lockButton.checked) {
|
|
mdnotes.contentEditable = false;
|
|
} else {
|
|
mdnotes.contentEditable = true;
|
|
}
|
|
}
|
|
|
|
function ondrag(e) {
|
|
// alert('drag');
|
|
}
|
|
|
|
function ondragenter(e) {
|
|
// alert('enter');
|
|
}
|
|
|
|
function ondragleave(e) {
|
|
// alert('leave')
|
|
}
|
|
|
|
function init(pagename = null) {
|
|
/*if (pagename != null) {
|
|
window.history.replaceState(null, '', '/page/' + pagename);
|
|
}*/
|
|
let mdnotesdiv = document.getElementById('mdnotes');
|
|
|
|
dpwidth(mdnotesdiv);
|
|
|
|
mdnotesdiv.addEventListener('input', onedit);
|
|
mdnotesdiv.addEventListener('keyup', onkeyup);
|
|
mdnotesdiv.addEventListener('keypress', onkeypress);
|
|
mdnotesdiv.addEventListener('keydown', onkeydown);
|
|
mdnotesdiv.addEventListener('paste', onpaste);
|
|
mdnotesdiv.addEventListener('copy', oncopy);
|
|
mdnotesdiv.addEventListener('blur', () => {redrawTables(mdnotesdiv); onsave()});
|
|
mdnotesdiv.addEventListener('focus', () => {redrawTables(mdnotesdiv, dpwidth());});
|
|
mdnotesdiv.addEventListener('drag', ondrag);
|
|
mdnotesdiv.addEventListener('dragenter', ondragenter);
|
|
mdnotesdiv.addEventListener('dragleave', ondragleave);
|
|
|
|
let saveButton = document.getElementById('saveButton');
|
|
saveButton.disabled = true;
|
|
saveButton.addEventListener('click', onsave);
|
|
let taButton = document.getElementById('taButton');
|
|
taButton.addEventListener('click', ontextarea);
|
|
let lockButton = document.getElementById('lockButton');
|
|
lockButton.addEventListener('click', onlockbutton);
|
|
onload();
|
|
}
|
|
|
|
export { init }; |