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

DEV: Fix invalid markup #6

Merged
merged 3 commits into from Nov 5, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
36 changes: 21 additions & 15 deletions javascripts/discourse/initializers/initialize-for-pdf-preview.js
Expand Up @@ -11,7 +11,7 @@ export default {
if (Mobile.mobileView) return;

try {
const previewMode = settings.preview_mode;
const previewModeSetting = settings.preview_mode;
const newTabIcon = () => {
const template = document.createElement("template");
template.innerHTML = iconHTML("external-link-alt", {
Expand All @@ -31,16 +31,16 @@ export default {
return iframe;
};

const setUpPreviewType = (pdf) => {
if (previewMode === "Inline") {
const setUpPreviewType = (pdf, renderMode) => {
if (renderMode === "Inline") {
const preview = createPreviewElement();
pdf.classList.add("pdf-attachment");
pdf.append(preview);
pdf.after(preview);

return preview;
}

if (previewMode === "New Tab") {
if (renderMode === "New Tab") {
pdf.classList.add("new-tab-pdf");
pdf.prepend(newTabIcon());
}
Expand All @@ -60,21 +60,27 @@ export default {
fileSize.nodeValue = "";
}

// ignore the pdf if its description starts with white-space
const startsWithWhitespace = /^\s+/;
const fileName = pdf.innerText;

if (startsWithWhitespace.test(fileName)) {
pdf.innerText = pdf.innerText.trim();
return;
}
// open the pdf in a new tab if either the global setting is
// "New Tab" or if the pdf description starts with a whitespace
// otherwise, render the preview in the inline in the post
const renderMode =
previewModeSetting === "New Tab" ||
startsWithWhitespace.test(fileName)
? "New Tab"
: "Inline";

// we don't need the space anymore.
pdf.innerText = pdf.innerText.trim();

// handle preview type
const preview = setUpPreviewType(pdf);
const preview = setUpPreviewType(pdf, renderMode);

// the pdf is set to Content-Disposition: attachment; filename="filename.jpg"
// one the server. this means we can't just use the href as the
// data/src for the pdf preview elements.
// on the server. this means we can't just use the href as the
// src for the pdf preview elements.
const httpRequest = new XMLHttpRequest();
httpRequest.open("GET", pdf.href);
httpRequest.responseType = "blob";
Expand All @@ -85,11 +91,11 @@ export default {
if (httpRequest.status === 200) {
const src = URL.createObjectURL(httpRequest.response);

if (previewMode === "Inline") {
if (renderMode === "Inline") {
preview.src = src;
}

if (previewMode === "New Tab") {
if (renderMode === "New Tab") {
pdf.addEventListener("click", (event) => {
event.preventDefault();
window.open(src);
Expand Down