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.
325 lines
8.7 KiB
325 lines
8.7 KiB
import { saveSelection, loadSelection, insertTextAtCaret } from './caret.js';
|
|
import { formatLine, load, save, formatTable, redrawTables, appendData, dpwidth } from './md.js';
|
|
|
|
const showtokens = [
|
|
{ fontSize: 0 },
|
|
{ fontSize: '95%' }
|
|
];
|
|
|
|
const hidetokens = [
|
|
{ fontSize: '95%' },
|
|
{ fontSize: 0 }
|
|
];
|
|
|
|
function timeoutSave() {
|
|
if (window.tos !== null) {
|
|
window.clearTimeout(window.tos);
|
|
}
|
|
window.tos = window.setTimeout(onsave, 5000);
|
|
}
|
|
|
|
function showTokens() {
|
|
let tokens = document.getElementsByClassName('token');
|
|
for (let i = 0; i < tokens.length; i++) {
|
|
tokens[i].animate(showtokens, { duration: 200, iterations: 1 });
|
|
}
|
|
}
|
|
|
|
function hideTokens() {
|
|
let tokens = document.getElementsByClassName('token');
|
|
for (let i = 0; i < tokens.length; i++) {
|
|
tokens[i].animate(hidetokens, { duration: 200, iterations: 1 });
|
|
}
|
|
}
|
|
|
|
function ontextarea(e) {
|
|
let ta = document.getElementById('ta');
|
|
let taButton = document.getElementById('taButton');
|
|
if (taButton.classList.contains('buttonoff')) {
|
|
ta.style.display = 'inline';
|
|
} else {
|
|
ta.style.display = 'none';
|
|
}
|
|
}
|
|
|
|
function onboldbutton(e) {
|
|
e.preventDefault();
|
|
let cheezenotes = document.getElementById('cheezenotes');
|
|
cheezenotes.focus();
|
|
insertTextAtCaret('**', '**');
|
|
onedit(e);
|
|
return false;
|
|
}
|
|
|
|
function onitalicbutton(e) {
|
|
e.preventDefault();
|
|
let cheezenotes = document.getElementById('cheezenotes');
|
|
cheezenotes.focus();
|
|
insertTextAtCaret('_', '_');
|
|
onedit(e);
|
|
return false;
|
|
}
|
|
|
|
function onstrikebutton(e) {
|
|
e.preventDefault();
|
|
let cheezenotes = document.getElementById('cheezenotes');
|
|
cheezenotes.focus();
|
|
insertTextAtCaret('~~', '~~');
|
|
onedit(e);
|
|
return false;
|
|
}
|
|
|
|
function onsave(e) {
|
|
let saveButton = document.getElementById('saveButton');
|
|
saveButton.disabled = true;
|
|
let text = save(document.getElementById('ta'), document.getElementById('cheezenotes'));
|
|
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('cheezenotes'));
|
|
}
|
|
|
|
function onedit(e) {
|
|
timeoutSave();
|
|
|
|
if (e.inputType == 'insertCompositionText') {
|
|
e.preventDefault();
|
|
return false;
|
|
}
|
|
|
|
saveButton.disabled = false;
|
|
let ret = saveSelection();
|
|
let line = ret[0];
|
|
let position = ret[1];
|
|
|
|
if (line.innerText == '\n') {
|
|
line.className = 'cheezenotes_line';
|
|
line.classList.add('body');
|
|
return;
|
|
}
|
|
|
|
let prevline = line.previousSibling;
|
|
if (prevline != null && prevline.innerText == '\n') {
|
|
prevline.className = 'cheezenotes_line';
|
|
prevline.classList.add('body');
|
|
}
|
|
|
|
let newline = formatLine(line.innerText);
|
|
line.parentNode.replaceChild(newline, line);
|
|
formatTable(newline, dpwidth());
|
|
ret[0] = newline;
|
|
ret[2] = newline;
|
|
loadSelection(ret);
|
|
console.log('onedit fin');
|
|
}
|
|
|
|
function onkeydown(e) {
|
|
if (e.key != 'Tab') {
|
|
return true;
|
|
}
|
|
|
|
e.preventDefault();
|
|
let key = ' ';
|
|
insertTextAtCaret(key);
|
|
onedit(e);
|
|
return false;
|
|
}
|
|
|
|
function onkeyup(e) {
|
|
if (e.key !== 'Escape') {
|
|
return true;
|
|
}
|
|
|
|
e.preventDefault();
|
|
document.getElementById('cheezenotes').blur();
|
|
if (saveButton.disabled == false) {
|
|
onsave();
|
|
}
|
|
return false;
|
|
}
|
|
|
|
function onkeypress(e) {
|
|
if (e.key !== 'Escape') {
|
|
return true;
|
|
}
|
|
|
|
e.preventDefault();
|
|
document.getElementById('cheezenotes').blur();
|
|
if (saveButton.disabled == false) {
|
|
onsave();
|
|
}
|
|
return false;
|
|
}
|
|
|
|
function onpaste(e) {
|
|
e.preventDefault();
|
|
timeoutSave();
|
|
let data = e.clipboardData.getData('text/plain');
|
|
appendData(document.getElementById('cheezenotes'), data);
|
|
saveButton.disabled = false;
|
|
return false;
|
|
}
|
|
|
|
function oncopy(e) {
|
|
//alert(e.clipboardData.getData('text/plain'));
|
|
}
|
|
|
|
function onlockbutton(e) {
|
|
let lockButton = document.getElementById('lockButton');
|
|
let lockButtonLabel = document.getElementById('lockButtonLabel');
|
|
let cheezenotes = document.getElementById('cheezenotes');
|
|
if (lockButton.checked) {
|
|
cheezenotes.contentEditable = false;
|
|
lockButtonLabel.innerText = 'visibility';
|
|
} else {
|
|
cheezenotes.contentEditable = true;
|
|
lockButtonLabel.innerText = 'visibility_off';
|
|
}
|
|
}
|
|
|
|
function ondrag(e) {
|
|
// alert('drag');
|
|
}
|
|
|
|
function ondragenter(e) {
|
|
// alert('enter');
|
|
}
|
|
|
|
function ondragleave(e) {
|
|
// alert('leave')
|
|
}
|
|
|
|
function addButton(id, icon, onclick) {
|
|
let buttons = document.getElementById('buttons');
|
|
let button = document.createElement('button');
|
|
button.id = id;
|
|
button.classList.add('button');
|
|
button.classList.add('material-icons');
|
|
button.innerText = icon;
|
|
button.addEventListener('mousedown', onclick);
|
|
buttons.appendChild(button);
|
|
return button;
|
|
}
|
|
|
|
function addOnOffButton(id, icon, alt_icon, onclick, on = false) {
|
|
let button;
|
|
if (on == true) {
|
|
button = addButton(id, icon, onclick);
|
|
button.classList.add('onoffbutton');
|
|
button.classList.add('buttonon');
|
|
} else {
|
|
button = addButton(id, alt_icon, onclick);
|
|
button.classList.add('onoffbutton');
|
|
button.classList.add('buttonoff');
|
|
}
|
|
button.addEventListener('click', function (e) {
|
|
if (button.classList.contains('buttonon')) {
|
|
button.classList.remove('buttonon');
|
|
button.classList.add('buttonoff');
|
|
button.innerText = alt_icon;
|
|
} else {
|
|
button.classList.remove('buttonoff');
|
|
button.classList.add('buttonon');
|
|
button.innerText = icon;
|
|
}
|
|
});
|
|
return button;
|
|
}
|
|
|
|
function lastButton(button) {
|
|
button.classList.add('rightborder');
|
|
return button;
|
|
}
|
|
|
|
function floatRight(button) {
|
|
button.classList.add('floatright');
|
|
return button;
|
|
}
|
|
|
|
function addSeparator() {
|
|
let separator = addButton('', 'more_vert', function () { });
|
|
separator.classList.add('separator');
|
|
separator.classList.remove('button');
|
|
}
|
|
|
|
function enableFormatButtons() {
|
|
let boldButton = document.getElementById('boldButton');
|
|
let italicButton = document.getElementById('italicButton');
|
|
let strikeButton = document.getElementById('strikeButton');
|
|
boldButton.disabled = false;
|
|
italicButton.disabled = false;
|
|
strikeButton.disabled = false;
|
|
}
|
|
|
|
function disableFormatButtons() {
|
|
let boldButton = document.getElementById('boldButton');
|
|
let italicButton = document.getElementById('italicButton');
|
|
let strikeButton = document.getElementById('strikeButton');
|
|
boldButton.disabled = true;
|
|
italicButton.disabled = true;
|
|
strikeButton.disabled = true;
|
|
}
|
|
|
|
function onblur(e) {
|
|
let cheezenotes = document.getElementById('cheezenotes');
|
|
redrawTables(cheezenotes);
|
|
onsave();
|
|
hideTokens();
|
|
disableFormatButtons();
|
|
}
|
|
|
|
function onfocus() {
|
|
redrawTables(cheezenotes, dpwidth());
|
|
showTokens();
|
|
enableFormatButtons();
|
|
}
|
|
|
|
function init(pagename = null) {
|
|
let cheezenotes = document.getElementById('cheezenotes');
|
|
|
|
dpwidth(cheezenotes);
|
|
|
|
cheezenotes.addEventListener('input', onedit);
|
|
cheezenotes.addEventListener('keyup', onkeyup);
|
|
cheezenotes.addEventListener('keypress', onkeypress);
|
|
cheezenotes.addEventListener('keydown', onkeydown);
|
|
cheezenotes.addEventListener('paste', onpaste);
|
|
cheezenotes.addEventListener('copy', oncopy);
|
|
cheezenotes.addEventListener('blur', onblur);
|
|
cheezenotes.addEventListener('focus', onfocus);
|
|
cheezenotes.addEventListener('drag', ondrag);
|
|
cheezenotes.addEventListener('dragenter', ondragenter);
|
|
cheezenotes.addEventListener('dragleave', ondragleave);
|
|
|
|
let saveButton = addButton('saveButton', 'save', onsave);
|
|
saveButton.disabled = true;
|
|
addSeparator();
|
|
addButton('boldButton', 'format_bold', onboldbutton);
|
|
addButton('italicButton', 'format_italic', onitalicbutton);
|
|
lastButton(addButton('strikeButton', 'format_strikethrough', onstrikebutton));
|
|
|
|
floatRight(lastButton(addOnOffButton('taButton', 'notes', 'notes', ontextarea)));
|
|
floatRight(addOnOffButton('editModeButton', 'edit_note', 'visibility',
|
|
function (e) {
|
|
if (editModeButton.classList.contains('buttonon')) {
|
|
cheezenotes.contentEditable = false;
|
|
} else {
|
|
cheezenotes.contentEditable = true;
|
|
}
|
|
}, true));
|
|
|
|
disableFormatButtons();
|
|
|
|
onload();
|
|
}
|
|
|
|
export { init }; |