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

Code changes (05/08) #1519

Merged
merged 2 commits into from
Aug 4, 2023
Merged
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
68 changes: 39 additions & 29 deletions web/_js/main/draw.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ const periodClipboard = {
const drawBackButton = document.createElement("a")
drawBackButton.className = "btn btn-outline-primary"
drawBackButton.id = "drawBackButton"
drawBackButton.textContent = "Exit Draw Mode"
drawBackButton.textContent = "Exit Drawing"

const baseInputAddon = document.createElement("span")
baseInputAddon.className = "input-group-text"
Expand Down Expand Up @@ -106,12 +106,13 @@ function initDraw() {
showListButton.insertAdjacentHTML("afterend", '<button class="btn btn-outline-primary" type="button" data-bs-toggle="offcanvas" data-bs-target="#offcanvasDraw" aria-controls="offcanvasDraw">Menu</button>')
showListButton.parentElement.appendChild(drawBackButton)
showListButton.remove()

drawButton.remove()

// Opens draw menu
wrapper.classList.remove('listHidden')
bsOffcanvasDraw.show()

window.render = render
window.renderHighlight = renderHighlight
window.renderBackground = renderBackground
window.updateHovering = updateHovering

Expand All @@ -125,13 +126,18 @@ function initDraw() {

let highlightUncharted = highlightUnchartedEl.checked

renderBackground(atlas)
window.updateAtlas = updateAtlas

updateAtlas()

document.addEventListener('timeupdate', () => {
updateAtlas()
})

applyView()

container.style.cursor = "crosshair"

render(path)

container.addEventListener("mousedown", e => {
lastPos = [
e.clientX,
Expand Down Expand Up @@ -168,7 +174,7 @@ function initDraw() {
const coords = getCanvasCoords(e.clientX, e.clientY)

path.push(coords)
render(path)
renderHighlight(path)

undoHistory = []
redoButton.disabled = true
Expand All @@ -185,13 +191,13 @@ function initDraw() {
container.addEventListener("mousemove", e => {
if (!dragging && drawing && path.length > 0) {
const coords = getCanvasCoords(e.clientX, e.clientY)
render([...path, coords])
renderHighlight([...path, coords])
}
})

container.addEventListener("mouseout", function () {
if (!dragging && drawing && path.length > 0) {
render(path)
renderHighlight(path)
}
})

Expand Down Expand Up @@ -228,19 +234,19 @@ function initDraw() {
undoButton.addEventListener("click", e => {
undo()
const coords = getCanvasCoords(e.clientX, e.clientY)
render([...path, coords])
renderHighlight([...path, coords])
})

redoButton.addEventListener("click", e => {
redo()
const coords = getCanvasCoords(e.clientX, e.clientY)
render([...path, coords])
renderHighlight([...path, coords])
})

resetButton.addEventListener("click", e => {
reset()
const coords = getCanvasCoords(e.clientX, e.clientY)
render([...path, coords])
renderHighlight([...path, coords])
})

resetButton.addEventListener("blur", function () {
Expand Down Expand Up @@ -269,7 +275,7 @@ function initDraw() {

highlightUnchartedEl.addEventListener("click", function () {
highlightUncharted = this.checked
render(path)
renderHighlight(path)
})

function generateExportObject() {
Expand Down Expand Up @@ -385,13 +391,11 @@ function initDraw() {
}
githubPostButton.href = githubPostUrl

console.log(githubPostUrl)

exportModal.show()
}

function preview() {
let infoElement = createInfoBlock(generateExportObject(), true)
let infoElement = createInfoBlock(generateExportObject(), 2)
objectsContainer.replaceChildren()
objectsContainer.appendChild(infoElement)
closeObjectsListButton.classList.remove("d-none")
Expand Down Expand Up @@ -488,17 +492,17 @@ function initDraw() {
closeObjectsListButton.classList.add("d-none")
}

function renderBackground() {
function renderBackground(atlas) {

backgroundContext.clearRect(0, 0, highlightCanvas.width, highlightCanvas.height)

backgroundContext.fillStyle = "rgba(0, 0, 0, 1)"
//backgroundContext.fillRect(0, 0, canvas.width, canvas.height)
//backgroundContext.fillRect(0, 0, canvas.width, renderBackgroundcanvas.height)

for (let i = 0; i < atlas.length; i++) {

const path = atlas[i].path
for (const entry of Object.values(atlas)) {

const path = entry.path

backgroundContext.beginPath()

if (path[0]) {
Expand All @@ -515,7 +519,7 @@ function initDraw() {
}
}

function render(path) {
function renderHighlight(path) {

if (!Array.isArray(path)) return

Expand Down Expand Up @@ -558,10 +562,16 @@ function initDraw() {
updateCoordsDisplay(e)
}

function updateAtlas() {
;[atlas, atlasOrder] = filterAtlas(atlasAll)
;[atlasDisplay, atlasOrder] = generateAtlasDisplay(atlas, atlasOrder, currentPeriod, currentVariation)
renderBackground(atlasDisplay)
renderHighlight(atlasDisplay)
}

const getEntry = id => {
if (!id) return
const entries = atlasAll.filter(entry => entry.id.toString() === id.toString())
if (entries.length === 1) return entries[0]
return atlasAll[id]
}

function addFieldButton(inputButton, inputGroup, array, index, name) {
Expand Down Expand Up @@ -795,7 +805,7 @@ function initDraw() {

} else {
document.getElementById("offcanvasDrawLabel").textContent = "New Entry"
pathWithPeriods.push([formatPeriod(currentPeriod, currentPeriod, currentVariation), []])
pathWithPeriods.push([formatPeriod(currentPeriod, null, currentVariation), []])

// Builds multi-input list
addWebsiteFields("", 0, [0])
Expand All @@ -816,19 +826,19 @@ function initDraw() {
)

document.addEventListener('timeupdate', () => {
renderBackground(atlas)
renderBackground(atlasDisplay)
updatePeriodGroups()
})

periodsAdd.addEventListener('click', () => {
pathWithPeriods.push([formatPeriod(currentPeriod, currentPeriod, currentVariation), []])
pathWithPeriods.push([formatPeriod(currentPeriod, null, currentVariation), []])
initPeriodGroups()
})

drawBackButton.href = "./" + formatHash(entry?.id)

document.addEventListener('timeupdate', event => {
drawBackButton.href = "./" + formatHash(entry?.id, event.detail.period, event.detail.period, event.detail.variation)
drawBackButton.href = "./" + formatHash(entry?.id, event.detail.period, event.detail.variation)
})

}
Expand Down Expand Up @@ -1229,7 +1239,7 @@ function updatePeriodGroups() {
function updatePath(newPath, newUndoHistory) {
path = newPath || path
if (path.length > 3) center = calculateCenter(path)
render(path)
renderHighlight(path)
undoButton.disabled = path.length === 0; // Maybe make it undo the cancel action in the future
undoHistory = newUndoHistory || []
redoButton.disabled = (!undoHistory.length)
Expand Down
35 changes: 24 additions & 11 deletions web/_js/main/infoblock.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ function createInfoListItem(name, value) {
return entryInfoListElement
}

function createInfoBlock(entry, isPreview) {
// mode 0 = normal
// mode 1 = entry list but none on atlas
// mode 2 = preview
function createInfoBlock(entry, mode = 0) {
const element = document.createElement("div")
element.className = "card mb-2 overflow-hidden shadow"

Expand All @@ -40,21 +43,31 @@ function createInfoBlock(entry, isPreview) {

const linkElement = document.createElement("a")
linkElement.className = "text-decoration-none d-flex justify-content-between text-body"
if (isPreview) linkElement.href = "#"
else {
linkElement.href = formatHash(entry.id, null, null, null, false, false, false)
linkElement.addEventListener('click', e => {

let nearestPeriod = currentPeriod
let nearestVariation = currentVariation
if (!atlasDisplay[entry.id]) {
[nearestPeriod, nearestVariation] = getNearestPeriod(entry, currentPeriod, currentVariation)
}

if (mode === 2) {
linkElement.href = "#"
} else {
const hash = formatHash(entry.id, nearestPeriod, nearestVariation, false, false, false)
linkElement.href = hash
if (mode === 0) linkElement.addEventListener('click', e => {
e.preventDefault()
location.hash = formatHash(entry.id, null, null, null, false, false, false)
location.hash = hash
window.dispatchEvent(new HashChangeEvent("hashchange"))
})
}

const linkNameElement = document.createElement("span")
linkNameElement.className = "flex-grow-1 text-break"
linkNameElement.textContent = entry.name
headerElement.appendChild(linkElement)
linkElement.appendChild(linkNameElement)
linkElement.insertAdjacentHTML("beforeend", '<i class="bi bi-link-45deg align-self-center link-primary" aria-hidden="true"></i>')
linkElement.insertAdjacentHTML("beforeend", '<i class="bi bi-link-45deg align-self-center link-primary" aria-hidden="true" title="Copy direct link"></i>')
element.appendChild(headerElement)

const bodyElement = document.createElement("div")
Expand Down Expand Up @@ -92,7 +105,7 @@ function createInfoBlock(entry, isPreview) {
}

// Entry data submitted to preview does not include center or path
if (!isPreview) {
if (mode === 0) {
const [x, y] = entry?.center
listElement.appendChild(createInfoListItem("Position: ", `${Math.floor(x)}, ${Math.floor(y)}`))

Expand Down Expand Up @@ -170,11 +183,11 @@ function createInfoBlock(entry, isPreview) {
element.appendChild(idElementContainer)

// Adds edit button only if element is not deleted
if (!isPreview && (!entry.diff || entry.diff !== "delete")) {
if (mode < 2 && (!entry.diff || entry.diff !== "delete")) {
const editElement = document.createElement("a")
editElement.textContent = "Edit"
editElement.innerHTML = '<i class="bi bi-pencil-fill" aria-hidden="true"></i> Edit'
editElement.className = "btn btn-sm btn-outline-primary"
editElement.href = "./?mode=draw&id=" + entry.id + formatHash(false)
editElement.href = "./?mode=draw&id=" + entry.id + formatHash(false, nearestPeriod, nearestVariation, false, false, false)
editElement.title = "Edit " + entry.name
idElementContainer.appendChild(editElement)
}
Expand Down
Loading
Loading