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

feature-9993-added #10100

Closed
wants to merge 2 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
10 changes: 9 additions & 1 deletion openlibrary/i18n/messages.pot
Original file line number Diff line number Diff line change
Expand Up @@ -4320,13 +4320,21 @@ msgid "Upload"
msgstr ""

#: covers/add.html
msgid "Or, use the the cover from Internet Archive"
msgid "Or, paste an image from your clipboard"
msgstr ""

#: covers/add.html
msgid "No Image Pasted"
msgstr ""

#: covers/add.html
msgid "Uploading..."
msgstr ""

#: covers/add.html
msgid "Or, use the the cover from Internet Archive"
msgstr ""

#: covers/author_photo.html
msgid "Pull up a larger author photo"
msgstr ""
Expand Down
77 changes: 77 additions & 0 deletions openlibrary/plugins/openlibrary/js/covers.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ function add_iframe(selector, src) {
// covers/manage.html and covers/add.html
export function initCoversAddManage() {
$('.ol-cover-form').on('submit', function () {
console.log('submitting form');
const loadingIndicator = document.querySelector('.loadingIndicator');
const formDivs = document.querySelectorAll('.ol-cover-form, .imageIntro');

Expand Down Expand Up @@ -112,3 +113,79 @@ export function initCoversSaved() {
parent.$(cover_selector).attr('src', cover_url);
}
}

// This function will be triggered when the user clicks the "Paste" button
export async function pasteImage() {
console.log('Pasting image...');
let formData = null;
try {
const clipboardItems = await navigator.clipboard.read();
for (const item of clipboardItems) {
if (item.types.includes('image/png')) {
const blob = await item.getType('image/png');
const image = document.getElementById('image');
image.src = URL.createObjectURL(blob);
image.style.display = 'block';

// Update the global formData with the new image blob
formData = new FormData();
formData.append('file', blob, 'pasted-image.png');

// Automatically fill in the hidden file input with the FormData
const fileInput = document.getElementById('hiddenFileInput');
const file = new File([blob], 'pasted-image.png', { type: 'image/png' });
const dataTransfer = new DataTransfer();
dataTransfer.items.add(file);
fileInput.files = dataTransfer.files; // This sets the file input with the image

// Show the upload button and update its text
const uploadButton = document.getElementById('uploadButton2');
console.log(uploadButton);
uploadButton.style.display = 'inline';
uploadButton.innerText = 'Use this image';

// Update the paste button text
document.getElementById('pasteButton').innerText = 'Change Image';
console.log(document.getElementById('pasteButton').innerText);

return formData;
} else {
throw new Error('Clipboard does not contain PNG image data.');
}
}
} catch (error) {
console.error(error.message);
}
}

export function initPasteForm(formData) {
document.getElementById('uploadButton2').addEventListener('click', function(event) {
event.preventDefault(); // Prevent the default form submission

const form = document.getElementById('clipboardForm');
if (formData) {
// Show the loading indicator
const loadingIndicator = document.querySelector('.loadingIndicator');
const formDivs = document.querySelectorAll('.ol-cover-form, .imageIntro');

if (loadingIndicator) {
loadingIndicator.classList.remove('hidden');
formDivs.forEach(div => div.classList.add('hidden'));
}

// Submit the form
form.submit();

// Hide the loading indicator after a delay to simulate form submission time
setTimeout(() => {
if (loadingIndicator) {
loadingIndicator.classList.add('hidden');
formDivs.forEach(div => div.classList.remove('hidden'));
}
initCoversSaved(); // Trigger the initCoversSaved function
}, 3000); // Delay Added
} else {
alert('No image data to upload.');
}
});
}
13 changes: 13 additions & 0 deletions openlibrary/plugins/openlibrary/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,19 @@ jQuery(function () {
});
}

const coverForm = document.querySelector('.ol-cover-form--clipboard');
if (coverForm) {
import(/* webpackChunkName: "covers" */ './covers')
.then(module => {
if (coverForm) {
document.getElementById('pasteButton').addEventListener('click', async () => {
const formData = await module.pasteImage();
module.initPasteForm(formData);
});
}
});
}

if (document.getElementById('addbook')) {
import(/* webpackChunkName: "add-book" */ './add-book')
.then(module => module.initAddBookImport());
Expand Down
18 changes: 17 additions & 1 deletion openlibrary/templates/covers/add.html
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,26 @@
</div>
<div class="input">
<input type="file" name="file" id="coverFile" value="" accept=".jpg, .jpeg, .gif, .png" required/>
<button type="submit" class="cta-btn cta-btn--vanilla">$_("Upload")</button>
<button id='uploadButton1' type="submit" class="cta-btn cta-btn--vanilla">$_("Upload")</button>
</div>
</form>

<form id="clipboardForm" class="ol-cover-form ol-cover-form--clipboard" method="post" enctype="multipart/form-data" action="$action">
<div class="label">
<label for="coverClipboard">$_("Or, paste an image from your clipboard")</label>
</div>
<div class="input">
<button type="button" onclick="pasteImage()" class="cta-btn cta-btn--vanilla" id="pasteButton">$_("No Image Pasted")</button>
</div>
<!-- Hidden file input that will be populated with the pasted image -->
<input type="file" name="file" id="hiddenFileInput" style="display: none;" />
<button id="uploadButton2" type="submit" class="cta-btn cta-btn--vanilla" style="display: none;">$_("Upload")</button>
</form>

<div id="imageContainer" style="text-align: center; margin-top: 20px;">
<img id="image" style="max-width: 100%; max-height: 400px; display: none; margin: 0 auto;" />
</div>
$:macros.LoadingIndicator(_("Uploading..."))
$#<form class="ol-cover-form ol-cover-form--url" method="post" enctype="multipart/form-data" action="$action">
$# <div class="label">
$# <label id="imageWeb" for="imageUrl">$_("Or, paste in the image URL if it's on the web")</label>
Expand Down
Loading