Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolve Text Overflow in Sidebar #4 #30

Closed
wants to merge 3 commits into from
Closed
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: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ Note Generator is a note-taking app for the web.

It is a static site without a database, notes are persisted temporarily in local storage



## Pre Installation

- [ ] Install the live - server extension in your favorite text editor( ...really helpful)
Expand Down
4 changes: 4 additions & 0 deletions css/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ body {
.notes__small-title,
.notes__small-updated {
padding: 10px;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;

}

.notes__small-title {
Expand Down
16 changes: 12 additions & 4 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,17 @@
</head>
<body>
<div class="notes" id="app">
<!-- <div class="notes__sidebar">
<div class="notes__sidebar">
<button class="notes__add" type="button">Add Note</button>
<div class="notes__list">
<div class="notes__list-item notes__list-item--selected">
<div class="notes__list containers">
<div class="notes__list-item notes__list-item--selected draggable" draggable="true">
<div class="notes__small-title">Lecture Notes</div>
<div class="notes__small-body">I learnt nothing today.</div>
<div class="notes__small-updated">Thursday 3:30pm</div>
</div>
</div>
<div class="notes__list_starred containers">
<div class="notes__list-item notes__list-item--selected draggable" draggable="true">
<div class="notes__small-title">Lecture Notes</div>
<div class="notes__small-body">I learnt nothing today.</div>
<div class="notes__small-updated">Thursday 3:30pm</div>
Expand All @@ -22,8 +29,9 @@
<div class="notes__preview">
<input class="notes__title" type="text" placeholder="Enter a title...">
<textarea class="notes__body">I am the notes body...</textarea>
</div> -->
</div>
</div>

<script src="./js/main.js" type="module"></script>
</body>
</html>
50 changes: 48 additions & 2 deletions js/NotesView.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ export default class NotesView {
this.root.innerHTML = `
<div class="notes__sidebar">
<button class="notes__add" type="button">Add Note</button>
<div class="notes__list"></div>
<div class="notes__list_starred containers"></div>
<div class="notes__list containers"></div>
</div>
<div class="notes__preview">
<input class="notes__title" type="text" placeholder="New Note...">
Expand All @@ -19,6 +20,8 @@ export default class NotesView {
const btnAddNote = this.root.querySelector(".notes__add");
const inpTitle = this.root.querySelector(".notes__title");
const inpBody = this.root.querySelector(".notes__body");
const draggables = document.querySelectorAll('.draggable')
const containers = document.querySelectorAll('.container')

btnAddNote.addEventListener("click", () => {
this.onNoteAdd();
Expand All @@ -34,13 +37,37 @@ export default class NotesView {
});

this.updateNotePreviewVisibility(false);

draggables.forEach(draggable => {
draggable.addEventListener('dragstart', () => {
draggable.classList.add('dragging')
})

draggable.addEventListener('dragend', () => {
draggable.classList.remove('dragging')
})
})

containers.forEach(container => {
container.addEventListener('dragover', e => {
e.preventDefault()
document.head.title = "dragging"
const afterElement = getDragAfterElement(container, e.clientY)
const draggable = document.querySelector('.dragging')
if (afterElement == null) {
container.appendChild(draggable)
} else {
container.insertBefore(draggable, afterElement)
}
})
})
}

_createListItemHTML(id, title, body, updated) {
const MAX_BODY_LENGTH = 60;

return `
<div class="notes__list-item" data-note-id="${id}">
<div class="notes__list-item draggable" draggable="true" data-note-id="${id}">
<div class="notes__small-title">${title}</div>
<div class="notes__small-body">
${body.substring(0, MAX_BODY_LENGTH)}
Expand Down Expand Up @@ -95,4 +122,23 @@ export default class NotesView {
updateNotePreviewVisibility(visible) {
this.root.querySelector(".notes__preview").style.visibility = visible ? "visible" : "hidden";
}





getDragAfterElement(container, y) {
const draggableElements = [...container.querySelectorAll('.draggable:not(.dragging)')]

return draggableElements.reduce((closest, child) => {
const box = child.getBoundingClientRect()
const offset = y - box.top - box.height / 2
if (offset < 0 && offset > closest.offset) {
return { offset: offset, element: child }
} else {
return closest
}
}, { offset: Number.NEGATIVE_INFINITY }).element
}
}

1 change: 1 addition & 0 deletions js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ import App from "./App.js";

const root = document.getElementById("app");
const app = new App(root);