Add Note Sharing feature.
This commit is contained in:
@@ -63,6 +63,10 @@
|
||||
Mind Map
|
||||
</a>
|
||||
{% if note.can_user_edit(g.user) %}
|
||||
<button type="button" class="btn btn-secondary" onclick="showShareModal()">
|
||||
<span class="icon">🔗</span>
|
||||
Share
|
||||
</button>
|
||||
<a href="{{ url_for('notes.edit_note', slug=note.slug) }}" class="btn btn-primary">
|
||||
<span class="icon">✏️</span>
|
||||
Edit
|
||||
@@ -962,4 +966,504 @@ window.onclick = function(event) {
|
||||
{% endif %}
|
||||
</script>
|
||||
|
||||
<!-- Share Modal -->
|
||||
<div id="share-modal" class="modal" style="display: none;">
|
||||
<div class="modal-overlay" onclick="hideShareModal()"></div>
|
||||
<div class="modal-content modal-large">
|
||||
<div class="modal-header">
|
||||
<h3 class="modal-title">Share Note</h3>
|
||||
<button type="button" class="modal-close" onclick="hideShareModal()">×</button>
|
||||
</div>
|
||||
<div class="modal-body">
|
||||
<!-- Create New Share Form -->
|
||||
<div class="share-create-section">
|
||||
<h4>Create New Share Link</h4>
|
||||
<form id="create-share-form" class="modern-form">
|
||||
<div class="form-row">
|
||||
<div class="form-group">
|
||||
<label for="expires_in_days" class="form-label">Expiration</label>
|
||||
<select id="expires_in_days" name="expires_in_days" class="form-control">
|
||||
<option value="">Never expires</option>
|
||||
<option value="1">1 day</option>
|
||||
<option value="7" selected>7 days</option>
|
||||
<option value="30">30 days</option>
|
||||
<option value="90">90 days</option>
|
||||
</select>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label for="max_views" class="form-label">View Limit</label>
|
||||
<input type="number"
|
||||
id="max_views"
|
||||
name="max_views"
|
||||
class="form-control"
|
||||
min="1"
|
||||
placeholder="Unlimited">
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label for="password" class="form-label">Password Protection (Optional)</label>
|
||||
<input type="password"
|
||||
id="password"
|
||||
name="password"
|
||||
class="form-control"
|
||||
placeholder="Leave empty for no password">
|
||||
</div>
|
||||
<button type="submit" class="btn btn-primary">
|
||||
<span class="icon">➕</span>
|
||||
Create Share Link
|
||||
</button>
|
||||
</form>
|
||||
</div>
|
||||
|
||||
<!-- Existing Shares List -->
|
||||
<div class="shares-list-section">
|
||||
<h4>Active Share Links</h4>
|
||||
<div id="shares-list" class="shares-list">
|
||||
<div class="loading">Loading shares...</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<style>
|
||||
/* Share Modal Styles */
|
||||
.modal {
|
||||
display: none;
|
||||
position: fixed;
|
||||
top: 0;
|
||||
left: 0;
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
z-index: 1000;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
}
|
||||
|
||||
.modal-overlay {
|
||||
position: absolute;
|
||||
top: 0;
|
||||
left: 0;
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
background: rgba(0, 0, 0, 0.5);
|
||||
}
|
||||
|
||||
.modal-content {
|
||||
position: relative;
|
||||
background: white;
|
||||
border-radius: 16px;
|
||||
box-shadow: 0 20px 60px rgba(0, 0, 0, 0.15);
|
||||
max-height: 90vh;
|
||||
overflow-y: auto;
|
||||
margin: 2rem;
|
||||
}
|
||||
|
||||
.modal-large {
|
||||
max-width: 800px;
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
.modal-header {
|
||||
padding: 1.5rem;
|
||||
border-bottom: 1px solid #e5e7eb;
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
}
|
||||
|
||||
.modal-title {
|
||||
font-size: 1.5rem;
|
||||
font-weight: 600;
|
||||
margin: 0;
|
||||
color: #1f2937;
|
||||
}
|
||||
|
||||
.modal-close {
|
||||
background: none;
|
||||
border: none;
|
||||
font-size: 1.5rem;
|
||||
width: 2rem;
|
||||
height: 2rem;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
cursor: pointer;
|
||||
color: #6b7280;
|
||||
border-radius: 6px;
|
||||
transition: all 0.2s;
|
||||
}
|
||||
|
||||
.modal-close:hover {
|
||||
background: #f3f4f6;
|
||||
color: #1f2937;
|
||||
}
|
||||
|
||||
.modal-body {
|
||||
padding: 1.5rem;
|
||||
}
|
||||
|
||||
.share-create-section {
|
||||
background: #f8f9fa;
|
||||
border-radius: 12px;
|
||||
padding: 1.5rem;
|
||||
margin-bottom: 2rem;
|
||||
}
|
||||
|
||||
.shares-list-section h4 {
|
||||
margin-bottom: 1rem;
|
||||
}
|
||||
|
||||
.shares-list {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 1rem;
|
||||
}
|
||||
|
||||
.share-item {
|
||||
background: white;
|
||||
border: 1px solid #e5e7eb;
|
||||
border-radius: 12px;
|
||||
padding: 1.5rem;
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: flex-start;
|
||||
transition: all 0.2s;
|
||||
gap: 1rem;
|
||||
}
|
||||
|
||||
.share-item:hover {
|
||||
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
|
||||
}
|
||||
|
||||
.share-item.expired {
|
||||
opacity: 0.6;
|
||||
background: #f8f9fa;
|
||||
}
|
||||
|
||||
.share-info {
|
||||
flex: 1;
|
||||
min-width: 0;
|
||||
}
|
||||
|
||||
.share-url {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 0.5rem;
|
||||
margin-bottom: 0.75rem;
|
||||
}
|
||||
|
||||
.share-url input {
|
||||
flex: 1;
|
||||
padding: 0.5rem;
|
||||
border: 1px solid #e5e7eb;
|
||||
border-radius: 6px;
|
||||
font-family: monospace;
|
||||
font-size: 0.875rem;
|
||||
min-width: 0;
|
||||
}
|
||||
|
||||
.share-meta {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
gap: 1rem;
|
||||
font-size: 0.875rem;
|
||||
color: #6b7280;
|
||||
}
|
||||
|
||||
.share-meta-item {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 0.25rem;
|
||||
}
|
||||
|
||||
.share-actions {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 0.5rem;
|
||||
flex-shrink: 0;
|
||||
}
|
||||
|
||||
.btn-sm {
|
||||
padding: 0.5rem 1rem;
|
||||
font-size: 0.875rem;
|
||||
border-radius: 6px;
|
||||
white-space: nowrap;
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
gap: 0.25rem;
|
||||
}
|
||||
|
||||
.btn-sm.btn-secondary {
|
||||
background: white;
|
||||
color: #667eea;
|
||||
border: 2px solid #e5e7eb;
|
||||
}
|
||||
|
||||
.btn-sm.btn-secondary:hover {
|
||||
border-color: #667eea;
|
||||
transform: translateY(-1px);
|
||||
box-shadow: 0 2px 8px rgba(102, 126, 234, 0.15);
|
||||
}
|
||||
|
||||
.btn-sm.btn-danger {
|
||||
background: white;
|
||||
color: #dc2626;
|
||||
border: 2px solid #fee2e2;
|
||||
}
|
||||
|
||||
.btn-sm.btn-danger:hover {
|
||||
background: #dc2626;
|
||||
color: white;
|
||||
border-color: #dc2626;
|
||||
transform: translateY(-1px);
|
||||
box-shadow: 0 2px 8px rgba(220, 38, 38, 0.2);
|
||||
}
|
||||
|
||||
.copy-feedback {
|
||||
position: fixed;
|
||||
bottom: 2rem;
|
||||
left: 50%;
|
||||
transform: translateX(-50%);
|
||||
background: #10b981;
|
||||
color: white;
|
||||
padding: 0.75rem 1.5rem;
|
||||
border-radius: 8px;
|
||||
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
|
||||
opacity: 0;
|
||||
transition: opacity 0.3s;
|
||||
}
|
||||
|
||||
.copy-feedback.show {
|
||||
opacity: 1;
|
||||
}
|
||||
|
||||
.empty-state {
|
||||
text-align: center;
|
||||
padding: 2rem;
|
||||
color: #6b7280;
|
||||
}
|
||||
|
||||
/* Form styles in modal */
|
||||
.form-row {
|
||||
display: flex;
|
||||
gap: 1rem;
|
||||
margin-bottom: 1rem;
|
||||
}
|
||||
|
||||
.form-row .form-group {
|
||||
flex: 1;
|
||||
}
|
||||
|
||||
@media (max-width: 640px) {
|
||||
.form-row {
|
||||
flex-direction: column;
|
||||
}
|
||||
|
||||
.share-item {
|
||||
flex-direction: column;
|
||||
align-items: stretch;
|
||||
}
|
||||
|
||||
.share-actions {
|
||||
margin-top: 1rem;
|
||||
justify-content: flex-end;
|
||||
}
|
||||
}
|
||||
</style>
|
||||
|
||||
<script>
|
||||
let shareModal = null;
|
||||
|
||||
function showShareModal() {
|
||||
shareModal = document.getElementById('share-modal');
|
||||
shareModal.style.display = 'flex';
|
||||
loadShares();
|
||||
}
|
||||
|
||||
function hideShareModal() {
|
||||
if (shareModal) {
|
||||
shareModal.style.display = 'none';
|
||||
}
|
||||
}
|
||||
|
||||
async function loadShares() {
|
||||
const sharesList = document.getElementById('shares-list');
|
||||
sharesList.innerHTML = '<div class="loading">Loading shares...</div>';
|
||||
|
||||
try {
|
||||
const response = await fetch(`/api/notes/{{ note.slug }}/shares`);
|
||||
const data = await response.json();
|
||||
|
||||
if (data.success) {
|
||||
if (data.shares.length === 0) {
|
||||
sharesList.innerHTML = '<div class="empty-state">No share links created yet.</div>';
|
||||
} else {
|
||||
sharesList.innerHTML = data.shares.map(share => createShareItem(share)).join('');
|
||||
}
|
||||
} else {
|
||||
sharesList.innerHTML = '<div class="error">Failed to load shares.</div>';
|
||||
}
|
||||
} catch (error) {
|
||||
sharesList.innerHTML = '<div class="error">Failed to load shares.</div>';
|
||||
}
|
||||
}
|
||||
|
||||
function createShareItem(share) {
|
||||
const isExpired = share.is_expired;
|
||||
const isLimitReached = share.is_view_limit_reached;
|
||||
const isInvalid = !share.is_valid;
|
||||
|
||||
return `
|
||||
<div class="share-item ${isInvalid ? 'expired' : ''}">
|
||||
<div class="share-info">
|
||||
<div class="share-url">
|
||||
<input type="text"
|
||||
value="${share.url}"
|
||||
readonly
|
||||
id="share-url-${share.id}"
|
||||
${isInvalid ? 'disabled' : ''}>
|
||||
${!isInvalid ? `
|
||||
<button class="btn btn-sm btn-secondary" onclick="copyShareUrl(${share.id})">
|
||||
<span class="icon">📋</span>
|
||||
Copy
|
||||
</button>
|
||||
` : ''}
|
||||
</div>
|
||||
<div class="share-meta">
|
||||
<div class="share-meta-item">
|
||||
<span class="icon">👁️</span>
|
||||
<span>${share.view_count}${share.max_views ? `/${share.max_views}` : ''} views</span>
|
||||
</div>
|
||||
${share.expires_at ? `
|
||||
<div class="share-meta-item">
|
||||
<span class="icon">⏰</span>
|
||||
<span>${isExpired ? 'Expired' : 'Expires'} ${new Date(share.expires_at).toLocaleDateString()}</span>
|
||||
</div>
|
||||
` : ''}
|
||||
${share.has_password ? `
|
||||
<div class="share-meta-item">
|
||||
<span class="icon">🔒</span>
|
||||
<span>Password protected</span>
|
||||
</div>
|
||||
` : ''}
|
||||
<div class="share-meta-item">
|
||||
<span class="icon">👤</span>
|
||||
<span>Created by ${share.created_by}</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="share-actions">
|
||||
<button class="btn btn-sm btn-danger" onclick="deleteShare(${share.id})">
|
||||
<span class="icon">🗑️</span>
|
||||
Delete
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
`;
|
||||
}
|
||||
|
||||
function copyShareUrl(shareId) {
|
||||
const input = document.getElementById(`share-url-${shareId}`);
|
||||
input.select();
|
||||
document.execCommand('copy');
|
||||
|
||||
// Show feedback
|
||||
showCopyFeedback();
|
||||
}
|
||||
|
||||
function showCopyFeedback() {
|
||||
let feedback = document.getElementById('copy-feedback');
|
||||
if (!feedback) {
|
||||
feedback = document.createElement('div');
|
||||
feedback.id = 'copy-feedback';
|
||||
feedback.className = 'copy-feedback';
|
||||
feedback.textContent = 'Link copied to clipboard!';
|
||||
document.body.appendChild(feedback);
|
||||
}
|
||||
|
||||
feedback.classList.add('show');
|
||||
setTimeout(() => {
|
||||
feedback.classList.remove('show');
|
||||
}, 2000);
|
||||
}
|
||||
|
||||
async function deleteShare(shareId) {
|
||||
if (!confirm('Are you sure you want to delete this share link?')) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await fetch(`/api/notes/shares/${shareId}`, {
|
||||
method: 'DELETE'
|
||||
});
|
||||
|
||||
const data = await response.json();
|
||||
|
||||
if (response.ok) {
|
||||
loadShares();
|
||||
} else {
|
||||
alert('Failed to delete share: ' + (data.error || 'Unknown error'));
|
||||
}
|
||||
} catch (error) {
|
||||
alert('Failed to delete share');
|
||||
}
|
||||
}
|
||||
|
||||
// Create share form handler
|
||||
document.getElementById('create-share-form').addEventListener('submit', async (e) => {
|
||||
e.preventDefault();
|
||||
|
||||
const formData = new FormData(e.target);
|
||||
const data = {};
|
||||
|
||||
// Convert form data to object
|
||||
const expiresInDays = formData.get('expires_in_days');
|
||||
if (expiresInDays) {
|
||||
data.expires_in_days = parseInt(expiresInDays);
|
||||
}
|
||||
|
||||
const maxViews = formData.get('max_views');
|
||||
if (maxViews) {
|
||||
data.max_views = parseInt(maxViews);
|
||||
}
|
||||
|
||||
const password = formData.get('password');
|
||||
if (password) {
|
||||
data.password = password;
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await fetch(`/api/notes/{{ note.slug }}/shares`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify(data)
|
||||
});
|
||||
|
||||
const result = await response.json();
|
||||
|
||||
if (result.success) {
|
||||
e.target.reset();
|
||||
loadShares();
|
||||
|
||||
// Auto-copy the new share URL
|
||||
setTimeout(() => {
|
||||
const input = document.getElementById(`share-url-${result.share.id}`);
|
||||
if (input) {
|
||||
input.select();
|
||||
document.execCommand('copy');
|
||||
showCopyFeedback();
|
||||
}
|
||||
}, 100);
|
||||
} else {
|
||||
alert('Failed to create share: ' + (result.error || 'Unknown error'));
|
||||
}
|
||||
} catch (error) {
|
||||
alert('Failed to create share');
|
||||
}
|
||||
});
|
||||
</script>
|
||||
|
||||
{% endblock %}
|
||||
Reference in New Issue
Block a user