From 6cef58a8ea973985848994a5de4342bf159bf686 Mon Sep 17 00:00:00 2001 From: Lanius Trolling Date: Tue, 18 Jul 2023 13:37:00 -0400 Subject: [PATCH] Two fixes: redirect target indicators are now more accurate in placing, and images no longer change the location if clicked on before the page is done loading --- .idea/kotlinc.xml | 2 +- build.gradle.kts | 32 ++++++------- .../kotlin/info/mechyrdia/lore/parser_tags.kt | 29 ++++++----- .../kotlin/info/mechyrdia/lore/parser_toc.kt | 12 ++--- src/main/resources/static/init.js | 48 +++++++++++-------- 5 files changed, 66 insertions(+), 57 deletions(-) diff --git a/.idea/kotlinc.xml b/.idea/kotlinc.xml index 0fc3113..9a55c2d 100644 --- a/.idea/kotlinc.xml +++ b/.idea/kotlinc.xml @@ -1,6 +1,6 @@ - \ No newline at end of file diff --git a/build.gradle.kts b/build.gradle.kts index 60b001c..88c681e 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -24,8 +24,8 @@ buildscript { plugins { java - kotlin("jvm") version "1.8.10" - kotlin("plugin.serialization") version "1.8.10" + kotlin("jvm") version "1.8.22" + kotlin("plugin.serialization") version "1.8.22" id("com.github.johnrengelman.shadow") version "7.1.2" application } @@ -38,23 +38,23 @@ repositories { } dependencies { - implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core-jvm:1.6.4") - implementation("org.jetbrains.kotlinx:kotlinx-coroutines-jdk8:1.6.4") - implementation("org.jetbrains.kotlinx:kotlinx-coroutines-reactive:1.6.4") - implementation("org.jetbrains.kotlinx:kotlinx-serialization-core-jvm:1.5.0") - implementation("org.jetbrains.kotlinx:kotlinx-serialization-json-jvm:1.5.0") + implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core-jvm:1.7.2") + implementation("org.jetbrains.kotlinx:kotlinx-coroutines-jdk8:1.7.2") + implementation("org.jetbrains.kotlinx:kotlinx-coroutines-reactive:1.7.2") + implementation("org.jetbrains.kotlinx:kotlinx-serialization-core-jvm:1.5.1") + implementation("org.jetbrains.kotlinx:kotlinx-serialization-json-jvm:1.5.1") - implementation("io.ktor:ktor-server-core-jvm:2.3.1") - implementation("io.ktor:ktor-server-netty-jvm:2.3.1") + implementation("io.ktor:ktor-server-core-jvm:2.3.2") + implementation("io.ktor:ktor-server-netty-jvm:2.3.2") - implementation("io.ktor:ktor-server-call-id:2.3.1") - implementation("io.ktor:ktor-server-call-logging:2.3.1") - implementation("io.ktor:ktor-server-forwarded-header:2.3.1") - implementation("io.ktor:ktor-server-html-builder:2.3.1") - implementation("io.ktor:ktor-server-sessions-jvm:2.3.1") - implementation("io.ktor:ktor-server-status-pages:2.3.1") + implementation("io.ktor:ktor-server-call-id:2.3.2") + implementation("io.ktor:ktor-server-call-logging:2.3.2") + implementation("io.ktor:ktor-server-forwarded-header:2.3.2") + implementation("io.ktor:ktor-server-html-builder:2.3.2") + implementation("io.ktor:ktor-server-sessions-jvm:2.3.2") + implementation("io.ktor:ktor-server-status-pages:2.3.2") - implementation("org.jetbrains.kotlinx:kotlinx-html-jvm:0.8.1") + implementation("org.jetbrains.kotlinx:kotlinx-html-jvm:0.9.0") implementation("com.samskivert:jmustache:1.15") implementation("org.apache.groovy:groovy-jsr223:4.0.10") diff --git a/src/main/kotlin/info/mechyrdia/lore/parser_tags.kt b/src/main/kotlin/info/mechyrdia/lore/parser_tags.kt index 8aaef54..4b61fb6 100644 --- a/src/main/kotlin/info/mechyrdia/lore/parser_tags.kt +++ b/src/main/kotlin/info/mechyrdia/lore/parser_tags.kt @@ -86,37 +86,37 @@ enum class TextParserFormattingTag(val type: TextParserTagType) { ), H1( TextParserTagType.Indirect { _, content, _ -> - "

$content

" + "

$content

" } ), H2( TextParserTagType.Indirect { _, content, _ -> val anchor = headerContentToAnchor(content) - "

$content

" + "

$content

" } ), H3( TextParserTagType.Indirect { _, content, _ -> val anchor = headerContentToAnchor(content) - "

$content

" + "

$content

" } ), H4( TextParserTagType.Indirect { _, content, _ -> val anchor = headerContentToAnchor(content) - "

$content

" + "

$content

" } ), H5( TextParserTagType.Indirect { _, content, _ -> val anchor = headerContentToAnchor(content) - "
$content
" + "
$content
" } ), H6( TextParserTagType.Indirect { _, content, _ -> val anchor = headerContentToAnchor(content) - "
$content
" + "
$content
" } ), ALIGN( @@ -147,15 +147,13 @@ enum class TextParserFormattingTag(val type: TextParserTagType) { val imageUrl = sanitizeLink(content) val (width, height) = getSizeParam(tagParam) - val sizeAttrs = getImageSizeAttributes(width, height) - if (imageUrl.endsWith(".svg")) { - val imageFile = File(Configuration.CurrentConfiguration.assetDir, "images/$imageUrl") - val imageSvg = imageFile.readText().replace("" + if (imageUrl.endsWith(".svg")) + File(Configuration.CurrentConfiguration.assetDir, "images/$imageUrl") + .readText() + .replace("window.appendImageThumb('/assets/images/$imageUrl', '${getImageSizeStyleValue(width, height)}');" } ), MODEL( @@ -373,7 +371,8 @@ fun getSizeParam(tagParam: String?): Pair = tagParam?.let { resoluti } ?: (null to null) fun getTableSizeAttributes(width: Int?, height: Int?) = (width?.let { " colspan=\"$it\"" } ?: "") + (height?.let { " rowspan=\"$it\"" } ?: "") -fun getImageSizeAttributes(width: Int?, height: Int?) = " style=\"" + (width?.let { "width: calc(var(--media-size-unit) * $it);" } ?: "") + (height?.let { "height: calc(var(--media-size-unit) * $it);" } ?: "") + "\"" +fun getImageSizeStyleValue(width: Int?, height: Int?) = (width?.let { "width: calc(var(--media-size-unit) * $it);" } ?: "") + (height?.let { "height: calc(var(--media-size-unit) * $it);" } ?: "") +fun getImageSizeAttributes(width: Int?, height: Int?) = " style=\"${getImageSizeStyleValue(width, height)}\"" val NON_ANCHOR_CHAR = Regex("[^a-zA-Z\\d\\-]") val INSIDE_TAG_TEXT = Regex("\\[.*?]") diff --git a/src/main/kotlin/info/mechyrdia/lore/parser_toc.kt b/src/main/kotlin/info/mechyrdia/lore/parser_toc.kt index e36d3e6..c1e837c 100644 --- a/src/main/kotlin/info/mechyrdia/lore/parser_toc.kt +++ b/src/main/kotlin/info/mechyrdia/lore/parser_toc.kt @@ -36,37 +36,37 @@ class TableOfContentsBuilder { enum class TextParserToCBuilderTag(val type: TextParserTagType) { H1( - TextParserTagType.Indirect { _, content, builder -> + TextParserTagType.Indirect { _, content, builder -> builder.add(headerContentToLabel(content), 0, headerContentToAnchor(content)) "[h1]$content[/h1]" } ), H2( - TextParserTagType.Indirect { _, content, builder -> + TextParserTagType.Indirect { _, content, builder -> builder.add(headerContentToLabel(content), 1, headerContentToAnchor(content)) "[h2]$content[/h2]" } ), H3( - TextParserTagType.Indirect { _, content, builder -> + TextParserTagType.Indirect { _, content, builder -> builder.add(headerContentToLabel(content), 2, headerContentToAnchor(content)) "[h3]$content[/h3]" } ), H4( - TextParserTagType.Indirect { _, content, builder -> + TextParserTagType.Indirect { _, content, builder -> builder.add(headerContentToLabel(content), 3, headerContentToAnchor(content)) "[h4]$content[/h4]" } ), H5( - TextParserTagType.Indirect { _, content, builder -> + TextParserTagType.Indirect { _, content, builder -> builder.add(headerContentToLabel(content), 4, headerContentToAnchor(content)) "[h5]$content[/h5]" } ), H6( - TextParserTagType.Indirect { _, content, builder -> + TextParserTagType.Indirect { _, content, builder -> builder.add(headerContentToLabel(content), 5, headerContentToAnchor(content)) "[h6]$content[/h6]" } diff --git a/src/main/resources/static/init.js b/src/main/resources/static/init.js index 1f4890b..e73d7bf 100644 --- a/src/main/resources/static/init.js +++ b/src/main/resources/static/init.js @@ -78,26 +78,33 @@ } }); + window.appendImageThumb = function (src, sizeStyle) { + // Image previewing (1) + const imgElement = document.createElement("img"); + imgElement.src = src; + imgElement.setAttribute("title", "Click to view full size"); + imgElement.setAttribute("style", sizeStyle); + + imgElement.onclick = e => { + e.preventDefault(); + + const thumbView = document.getElementById("thumb-view"); + const thumbViewImg = thumbView.getElementsByTagName("img")[0]; + thumbViewImg.src = e.currentTarget.src; + thumbView.classList.add("visible"); + } + + document.currentScript.after(imgElement); + }; + window.addEventListener("load", function () { - // Image previewing + // Image previewing (2) document.getElementById("thumb-view").addEventListener("click", e => { e.preventDefault(); e.currentTarget.classList.remove("visible"); e.currentTarget.getElementsByTagName("img")[0].src = ""; }); - - const thumbs = document.querySelectorAll("a.thumb"); - for (const thumb of thumbs) { - thumb.onclick = e => { - e.preventDefault(); - - const thumbView = document.getElementById("thumb-view"); - const thumbViewImg = thumbView.getElementsByTagName("img")[0]; - thumbViewImg.src = e.currentTarget.getAttribute("href"); - thumbView.classList.add("visible"); - }; - } }); window.addEventListener("load", function () { @@ -327,15 +334,18 @@ pElement.append(aElement); document.currentScript.after(pElement); } else { - document.cookie = "REDIRECTED_FROM=" + window.location.pathname + "; SameSite=Lax; Secure"; + window.localStorage.setItem("redirectedFrom", window.location.pathname); window.location = redirectTo; } }; - window.checkRedirectTarget = function () { - const redirectSourceValues = document.cookie.split(';').filter(value => value.trim().startsWith("REDIRECTED_FROM=")); - if (redirectSourceValues.length > 0) { - const redirectSource = new URL(redirectSourceValues[0].trim().substring("REDIRECTED_FROM=".length), window.location.origin); + window.checkRedirectTarget = function (anchorHash) { + const redirectTargetValue = window.location.hash; + if (redirectTargetValue !== anchorHash) return; + + const redirectSourceValue = window.localStorage.getItem("redirectedFrom"); + if (redirectSourceValue != null) { + const redirectSource = new URL(redirectSourceValue, window.location.origin); if (redirectSource.search.length > 0) { redirectSource.search += "&redirect=no"; } else { @@ -356,6 +366,6 @@ document.currentScript.after(pElement); } - document.cookie = "REDIRECTED_FROM=; Expires=Thu, 01 Jan 1970 00:00:00 GMT; SameSite=Lax; Secure"; + window.localStorage.removeItem("redirectedFrom"); }; })(); -- 2.25.1