Skip to content

Commit

Permalink
Feature: new options to include attachments and/or images in markdown…
Browse files Browse the repository at this point in the history
… summary (#69)

Add options for attachments and images in markdown summary
  • Loading branch information
BagToad authored May 11, 2024
1 parent 8e357c9 commit 75917f3
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 5 deletions.
10 changes: 10 additions & 0 deletions src/background/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,8 @@ browser.runtime.onInstalled.addListener((data) => {
optionsGlobal: {
wrapLists: false,
backgroundProcessing: false,
includeAttachments: true,
includeImages: true,
},
});
} else if (reason === "update") {
Expand All @@ -371,6 +373,14 @@ browser.runtime.onInstalled.addListener((data) => {
data.optionsGlobal.backgroundProcessing === undefined
? false
: data.optionsGlobal.backgroundProcessing,
includeAttachments:
data.optionsGlobal.includeAttachments === undefined
? true
: data.optionsGlobal.includeAttachments,
includeImages:
data.optionsGlobal.includeImages === undefined
? true
: data.optionsGlobal.includeImages,
},
});
});
Expand Down
7 changes: 6 additions & 1 deletion src/options/options.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,17 @@
</head>
<body>
<h1>Options Page</h1>
<div id="input-container-attachments" class="input-container-row"></div>
<div class="input-container-column">
<div class="input-container-row">
<label class="input-label" title="How to handle links that are too long to be displayed in the extension.&#13&#13Enabled = wrap long links.&#13Disabled = scroll long links." for="wrap-lists">Wrap long links?</label>
<input type="checkbox" id="wrap-lists">
</div>
<div id="input-container-attachments" class="input-container-row">
<label class="input-label" title="Include attachments in the markdown summary." for="include-attachments">Include Attachments:</label>
<input type="checkbox" id="include-attachments">
<label class="input-label" title="Include images in the markdown summary." for="include-images">Include Images:</label>
<input type="checkbox" id="include-images">
</div>
</div>
<div id="input-container-save-options" class="input-container-row">
<button id="button-save-global-options">Save Options</button>
Expand Down
15 changes: 14 additions & 1 deletion src/options/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -428,14 +428,21 @@ function saveGlobalOptions() {
if (data.optionsGlobal == undefined) {
data.optionsGlobal = {
wrapLists: false,
includeAttachments: false,
includeImages: false,
};
}

data.optionsGlobal.wrapLists =
document.getElementById("wrap-lists").checked;
data.optionsGlobal.includeAttachments = document.getElementById(
"include-attachments"
).checked;
data.optionsGlobal.includeImages =
document.getElementById("include-images").checked;

browser.storage.sync.set({
optionsGlobal: optionsGlobal,
optionsGlobal: data.optionsGlobal,
});
});
}
Expand All @@ -446,10 +453,16 @@ function loadGlobalOptions() {
if (data.optionsGlobal == undefined) {
data.optionsGlobal = {
wrapLists: false,
includeAttachments: false,
includeImages: false,
};
}
document.getElementById("wrap-lists").checked =
data.optionsGlobal.wrapLists;
document.getElementById("include-attachments").checked =
data.optionsGlobal.includeAttachments;
document.getElementById("include-images").checked =
data.optionsGlobal.includeImages;
});
}

Expand Down
53 changes: 50 additions & 3 deletions src/popup/popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function scrollToComment(data) {
}

// Write the configurable summary to the clipboard.
function writeSummaryClipboard() {
async function writeSummaryClipboard() {
let summary = "";
document.querySelectorAll(".list-links").forEach((list) => {
// If "all" summary type, add all to summary.
Expand All @@ -33,6 +33,52 @@ function writeSummaryClipboard() {
summary += "\n";
}
});

// Include attachments and images in the markdown summary if the respective global options are enabled.
// Fallback to enabled if the options are not set or null.
await browser.storage.sync.get("optionsGlobal").then(async (data) => {
const includeAttachments = data.optionsGlobal?.includeAttachments ?? true;
const includeImages = data.optionsGlobal?.includeImages ?? true;

console.log(includeAttachments, includeImages);

if (includeAttachments) {
summary += "### Attachments\n\n";
await browser.storage.local.get("ticketStorage").then(async (data) => {
if (data.ticketStorage && data.ticketStorage.attachments.length > 0) {
const markdownLinks = data.ticketStorage.attachments
.map((attachment) =>
attachment.attachments
.map(
(file) =>
`[${file.file_name}](${file.content_url}) - ${attachment.created_at}`
)
.join("\n")
)
.join("\n\n");
summary += markdownLinks;
}
});
summary += "\n";
}

if (includeImages) {
summary += "### Images\n\n";
await browser.storage.local.get("ticketStorage").then((data) => {
if (data.ticketStorage && data.ticketStorage.images.length > 0) {
const markdownImages = data.ticketStorage.images
.map(
(image) =>
`_${image.fileName}_ - ${image.createdAt}\n\n![${image.fileName}](${image.url})`
)
.join("\n\n");
summary += markdownImages;
}
});
summary += "\n";
}
});

if (summary != "") {
navigator.clipboard.writeText(summary);
document.getElementById("summary-copy").classList.add("hidden");
Expand Down Expand Up @@ -441,7 +487,7 @@ function copyAttachmentsMarkdown() {
attachment.attachments
.map(
(file) =>
`[${file.file_name}](${file.content_url}) - Comment on: ${attachment.created_at}`
`[${file.file_name}](${file.content_url}) - ${attachment.created_at}`
)
.join("\n")
)
Expand Down Expand Up @@ -471,7 +517,8 @@ function copyImagesMarkdown() {
if (data.ticketStorage && data.ticketStorage.images.length > 0) {
const markdownImages = data.ticketStorage.images
.map(
(image) => `_${image.fileName}_\n![${image.fileName}](${image.url})`
(image) =>
`_${image.fileName}_ - ${image.createdAt}\n\n![${image.fileName}](${image.url})`
)
.join("\n\n");
navigator.clipboard.writeText(markdownImages).then(() => {
Expand Down

0 comments on commit 75917f3

Please sign in to comment.