Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<body>
<div id='wrapper'>
<section id='text-container'>
<div id='text' contenteditable='true' spellcheck='false'></div>
<textarea id='text' spellcheck='false'></textarea>

<div class='controls-container'>
<div id='active-draft'></div>
Expand Down
2 changes: 2 additions & 0 deletions main.css
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ html, body {
}

#text {
display: block;
width: 100%;
height: 100%;
font-size: 1.7em;
max-width: 900px;
Expand Down
39 changes: 16 additions & 23 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ const sidebar = document.querySelector("#sidebar");
const clearButton = document.querySelector("#clear-button");
const newDraftButton = document.querySelector("#new-draft-button");

const welcomeMessage = `This page saves everything you type,<br>
and allows to manage auto-saved drafts.<br><br>
The data stays in your browser,<br>
no need for an account.<br><br>
Enjoy :)`
const welcomeMessage = `This page saves everything you type,
and allows to manage auto-saved drafts.

The data stays in your browser,
no need for an account.

Enjoy :)`

setTheme();
applyTheme();
Expand All @@ -20,19 +22,19 @@ window.addEventListener("load", function() {
renderSidebar();

document.addEventListener("keyup", function() {
Draft.saveCurrent(text.innerHTML);
updateThumbnail(text.innerHTML);
Draft.saveCurrent(text.value);
updateThumbnail(text.value);
});
});

clearButton.onclick = async function() {
Draft.destroyCurrent();
await renderCurrentContent();
while (text.firstChild) text.removeChild(text.firstChild);
text.value = '';
};

newDraftButton.onclick = async function() {
const draft = await Draft.init(text.innerHTML);
const draft = await Draft.init(text.value);
draft.setActive();

renderCurrentContent();
Expand All @@ -42,7 +44,7 @@ newDraftButton.onclick = async function() {
async function renderCurrentContent() {
const current = await Draft.getCurrent();

text.innerHTML = current ? current : welcomeMessage;
text.value = current ? current : welcomeMessage;

renderActiveDraft();
}
Expand Down Expand Up @@ -97,14 +99,13 @@ function clearEmptySidebar() {
function renderThumbnail(draft) {
const container = document.querySelector(".thumbnails-container");
const newDiv = document.createElement("div");
const cleanExtract = sanitizeExtract(draft.extract);

newDiv.classList.add("thumbnail");
newDiv.setAttribute("data-draft-uid", draft.uid);
newDiv.setAttribute("data-draft-position", draft.position);

renderThumbnailPosition(newDiv);
renderThumbnailContent(cleanExtract, newDiv);
renderThumbnailContent(draft.extract, newDiv);
renderThumbnailDelete(newDiv);

container.append(newDiv);
Expand Down Expand Up @@ -165,17 +166,17 @@ async function setupThumbnailsEvents() {
}

async function updateThumbnail(content) {
const cleanContent = sanitizeExtract(content.substring(0,150));
const shortenContent = content.substring(0,150);
const activeDraft = await Draft.getActive();

if (activeDraft && activeDraft != "") {
const thumbs = document.getElementsByClassName("thumbnail");
const thumb = thumbs.item(activeDraft.position - 1);
const container = thumb.querySelector(".thumbnail-content");

if (container.textContent != cleanContent) {
if (container.textContent != shortenContent) {
while (container.firstChild) container.removeChild(container.firstChild);
container.textContent = cleanContent;
container.textContent = shortenContent;
}
}
}
Expand Down Expand Up @@ -222,14 +223,6 @@ async function saveThemeToDark() {
await browser.storage.local.set({"drafter-dark-theme": true});
}

function sanitizeExtract(content) {
return content.replace(/<\/div>/g, " ")
.replace(/<\/?[^>]+(>|$)/g, "")
.replace(/&nbsp;/gi, " ")
.replace(/&gt;/g, ">")
.replace(/&lt;/g, "<");
}

function eventTargetUid(event) {
return event.currentTarget.parentNode.dataset.draftUid;
}