Skip to content

Commit

Permalink
Merge pull request #9 from revolter/feature/note-title-as-tab-title
Browse files Browse the repository at this point in the history
  • Loading branch information
revolter authored Jun 11, 2024
2 parents d383fab + 4e7eef2 commit 6d16f3b
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

{%- endcapture -%}

<textarea id="{{ textarea_id }}" placeholder="{{ textarea_placeholder}}" autofocus></textarea>
<textarea id="{{ textarea_id }}" placeholder="{{ textarea_placeholder }}" autofocus></textarea>

<script>
function main() {
Expand All @@ -47,14 +47,17 @@
const value = retrieveValue();
const oldValue = textarea.value;
textarea.value = value;
updateTitle(value);
if (oldValue === '') {
textarea.selectionStart = value.length;
}
}

function addValueListener(textarea) {
textarea.addEventListener('input', (event) => {
storeValue(event.target.value);
const value = event.target.value;
storeValue(value);
updateTitle(value);
}, false);
}

Expand All @@ -74,6 +77,18 @@
window.location.hash = '#' + serialize(value);
}

function updateTitle(value) {
const rawTitle = value.split('\n', 1)[0];
if (rawTitle === '') { return; }
const truncatedTitle = truncate(rawTitle, 30);
document.title = truncatedTitle + ' | ' + '{{ site.tagline }}';
}

function truncate(string, limit) {
if (string.length < limit) { return string; }
return string.slice(0, limit - 1) + "…";
}

function serialize(value) {
if (value === '') { return ''; }
const data = new TextEncoder().encode(value);
Expand Down

0 comments on commit 6d16f3b

Please sign in to comment.