cleanup of old code which used notecase formatting

This commit is contained in:
azivner 2017-09-27 00:04:50 -04:00
parent b30bc19bd2
commit 50b61d30f0
5 changed files with 23 additions and 251 deletions

View File

@ -237,8 +237,6 @@
<!-- Note detail -->
<script src="stat/js/note.js"></script>
<script src="stat/js/notecase2html.js"></script>
<script src="stat/js/html2notecase.js"></script>
<script src="stat/js/encryption.js"></script>
<!-- dialogs -->

View File

@ -11,7 +11,6 @@ function convertNoteToHtml(noteId, failedNotes) {
return;
}
note.detail.note_text = notecase2html(note);
note.formatting = [];
for (const link of note.links) {

View File

@ -1,178 +0,0 @@
const globalHtmlEnabled = true;
function html2notecase(contents, note) {
note.formatting = [];
note.links = [];
note.images = [];
if (globalHtmlEnabled) {
note.detail.note_text = contents;
if (!note.detail.encryption) {
const linkRegexp = /<a[^>]+?href="[^"]*kapp#([A-Za-z0-9]{22})"[^>]*?>[^<]+?<\/a>/g;
let match;
while (match = linkRegexp.exec(contents)) {
console.log("adding link for " + match[1]);
note.links.push({
note_id: note.detail.note_id,
target_note_id: match[1]
});
}
}
return;
}
// remove any possible extra newlines which might be inserted - all relevant new lines should be only in <br> and <p>
contents = contents.replace(/(?:\r\n|\r|\n)/, '');
// some editors insert this font stuff which messes up parsing by doubling newlines
contents = contents.replace(/<font[^>]*>/g, '');
contents = contents.replace(/<\/font>/g, '');
contents = contents.replace(/<br><\/p>/g, '\n');
contents = contents.replace(/<p><br><\/p>/g, '\n');
contents = contents.replace(/<br \/>/g, '\n');
contents = contents.replace(/<br>/g, '\n');
contents = contents.replace(/<\/p>/g, '\n');
contents = contents.replace(/<p>/g, '');
contents = contents.replace(/&nbsp;/g, ' ');
let index = 0;
while (index < contents.length) {
let curContent = contents.substr(index);
if (contents[index] === '<') {
let found = false;
const endOfTag = curContent.indexOf('>');
if (endOfTag === -1) {
console.log("Can't find the end of the tag");
}
const curTag = curContent.substr(0, endOfTag + 1);
for (tagId in tags) {
const tag = tags[tagId];
if (contents.substr(index, tag.length) === tag) {
found = true;
note.formatting.push({
note_id: note.detail.note_id,
note_offset: index,
fmt_tag: tagId,
fmt_color: '',
fmt_font: '',
fmt_value: 100
});
contents = contents.substr(0, index) + contents.substr(index + tag.length);
break;
}
}
if (curTag.substr(0, 4) === "<img") {
//console.log("Found img tag");
let dataImagePos = curTag.indexOf('data:image/');
if (dataImagePos !== -1) {
let imageType = curTag.substr(dataImagePos + 11, 3);
//console.log("image type: " + imageType);
let dataStart = curTag.substr(dataImagePos + 22);
let endOfDataPos = dataStart.indexOf('"');
if (endOfDataPos !== -1) {
const imageData = dataStart.substr(0, endOfDataPos);
note.images.push({
note_id: note.detail.note_id,
note_offset: index,
is_png: imageType === "png",
image_data: imageData
});
contents = contents.substr(0, index) + contents.substr(index + curTag.length);
found = true;
}
}
}
const linkMatch = /^<a[^>]+?href="([^"]+?)"[^>]*?>([^<]+?)<\/a>/.exec(curContent);
if (linkMatch !== null) {
const targetUrl = linkMatch[1];
const linkText = linkMatch[2];
const newLink = {
note_id: note.detail.note_id,
note_offset: index,
lnk_text: linkText
};
const noteIdMatch = /app#([A-Za-z0-9]{22})/.exec(targetUrl);
if (noteIdMatch !== null) {
newLink.target_note_id = noteIdMatch[1];
}
else {
newLink.target_url = targetUrl;
}
note.links.push(newLink);
contents = contents.substr(0, index) + linkText + contents.substr(index + linkMatch[0].length);
// we'll skip the link text so that it's not processed twice (second time by link auto detection)
index += linkText.length;
found = true;
}
if (!found) {
contents = contents.substr(0, index) + contents.substr(index + endOfTag + 1);
}
}
else {
let linkMatch = /^(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|]/i.exec(curContent);
if (linkMatch !== null) {
const targetUrl = linkMatch[0];
const newLink = {
note_id: note.detail.note_id,
note_offset: index,
lnk_text: targetUrl
};
const noteIdMatch = /app#([A-Za-z0-9]{22})/.exec(targetUrl);
if (noteIdMatch !== null) {
newLink.target_note_id = noteIdMatch[1];
}
else {
newLink.target_url = targetUrl;
}
note.links.push(newLink);
index += targetUrl.length;
}
else {
index++;
}
}
}
contents = contents.trim();
note.detail.note_text = contents;
}

View File

@ -58,6 +58,28 @@ $(document).ready(function() {
$(".note-editable").attr("tabindex", 2);
});
function html2notecase(contents, note) {
note.formatting = [];
note.links = [];
note.images = [];
note.detail.note_text = contents;
if (!note.detail.encryption) {
const linkRegexp = /<a[^>]+?href="[^"]*kapp#([A-Za-z0-9]{22})"[^>]*?>[^<]+?<\/a>/g;
let match;
while (match = linkRegexp.exec(contents)) {
console.log("adding link for " + match[1]);
note.links.push({
note_id: note.detail.note_id,
target_note_id: match[1]
});
}
}
}
function updateNoteFromInputs(note) {
let contents = $('#noteDetail').summernote('code');
@ -194,14 +216,12 @@ function loadNote(noteId) {
$("#noteTitle").val(note.detail.note_title);
let noteText = notecase2html(note);
noteChangeDisabled = true;
// Clear contents and remove all stored history. This is to prevent undo from going across notes
$('#noteDetail').summernote('reset');
$('#noteDetail').summernote('code', noteText);
$('#noteDetail').summernote('code', note.detail.note_text);
document.location.hash = noteId;

View File

@ -1,67 +0,0 @@
function notecase2html(note) {
if (globalHtmlEnabled) {
return note.detail.note_text;
}
let noteText = note.detail.note_text;
note.formatting.forEach(el => el.type = 'formatting');
note.links.forEach(el => el.type = 'link');
note.images.forEach(el => el.type = 'image');
const allTags = note.formatting.concat(note.links).concat(note.images);
allTags.sort(function compare(a, b) {
return a.note_offset - b.note_offset;
});
let offset = 0;
function inject(target, injected, position) {
offset += injected.length;
return noteText.substr(0, position) + injected + noteText.substr(position);
}
for (const el of allTags) {
if (el.type === 'formatting') {
if (tags[el.fmt_tag]) {
noteText = inject(noteText, tags[el.fmt_tag], el.note_offset + offset);
}
}
else if (el.type === 'link') {
let targetUrl;
if (el.target_url) {
targetUrl = el.target_url;
}
else {
targetUrl = "app#" + el.target_note_id;
}
const linkHtml = '<a href="' + targetUrl + '">' + el.lnk_text + '</a>';
noteText = noteText.substr(0, el.note_offset + offset) + noteText.substr(el.note_offset + offset + el.lnk_text.length);
noteText = inject(noteText, linkHtml, el.note_offset + offset);
offset -= el.lnk_text.length;
}
else if (el.type === 'image') {
const type = el.is_png ? "png" : "jpg";
const imgHtml = '<img alt="Embedded Image" src="data:image/' + type + ';base64,' + el.image_data + '" />';
noteText = inject(noteText, imgHtml, el.note_offset + offset);
}
}
noteText = noteText.replace(/(?:\r\n|\r)/g, '\n');
noteText = noteText.replace(/(.+)\n/g, '<p>$1</p>');
noteText = noteText.replace(/\n/g, '<p><br></p>');
noteText = noteText.replace(/ /g, '&nbsp;&nbsp;');
return noteText;
}