Speakr is a personal, self-hosted web application designed for transcribing audio recordings
Python
Add drag-and-drop tag reordering with desktop and mobile support
Murtaza Nasire1c60741b0cc
diff --git a/src/api/recordings.py b/src/api/recordings.pyindex 27744fb6..66a1553b 100644--- a/src/api/recordings.py+++ b/src/api/recordings.py@@ -3100,6 +3100,52 @@ def remove_tag_from_recording(recording_id, tag_id):return jsonify({'error': 'An unexpected error occurred.'}), 500+@recordings_bp.route('/api/recordings/<int:recording_id>/tags/reorder', methods=['PUT'])+@login_required+def reorder_recording_tags(recording_id):+ """Reorder tags on a recording. Updates the order field for each RecordingTag."""+ try:+ recording = db.session.get(Recording, recording_id)+ if not recording:+ return jsonify({'error': 'Recording not found'}), 404++ # Check if user has edit access to this recording+ if not has_recording_access(recording, current_user, require_edit=True):+ return jsonify({'error': 'You do not have permission to modify this recording'}), 403++ data = request.get_json()+ if not data or 'tag_ids' not in data:+ return jsonify({'error': 'Missing tag_ids in request body'}), 400++ tag_ids = data.get('tag_ids', [])+ if not isinstance(tag_ids, list):+ return jsonify({'error': 'tag_ids must be a list'}), 400++ # Update order for each tag+ for order, tag_id in enumerate(tag_ids, 1):+ recording_tag = RecordingTag.query.filter_by(+ recording_id=recording_id,+ tag_id=tag_id+ ).first()+ if recording_tag:+ recording_tag.order = order++ db.session.commit()++ # Return updated recording+ recording_dict = recording.to_dict(viewer_user=current_user)+ enrich_recording_dict_with_user_status(recording_dict, recording, current_user)+ return jsonify({+ 'success': True,+ 'recording': recording_dict+ })++ except Exception as e:+ db.session.rollback()+ current_app.logger.error(f"Error reordering tags on recording {recording_id}: {e}", exc_info=True)+ return jsonify({'error': 'An unexpected error occurred.'}), 500++# --- Auto-deletion and Chunks Processing ---@recordings_bp.route('/api/recordings/<int:recording_id>/toggle_deletion_exempt', methods=['POST'])diff --git a/static/js/modules/composables/tags.js b/static/js/modules/composables/tags.jsindex c8cb16f9..6e4a7b1d 100644--- a/static/js/modules/composables/tags.js+++ b/static/js/modules/composables/tags.js@@ -3,7 +3,7 @@* Handles tag operations for recordings*/-const { computed } = Vue;+const { computed, ref } = Vue;export function useTags({recordings,@@ -17,6 +17,10 @@ export function useTags({}) {// State (using passed refs from parent)+ // --- Tag Drag-and-Drop State for Edit Modal ---+ const modalDraggedTagIndex = ref(null);+ const modalDragOverTagIndex = ref(null);+// Computedconst getRecordingTags = (recording) => {if (!recording || !recording.tags) return [];@@ -159,6 +163,114 @@ export function useTags({}};+ // --- Modal Tag Reordering ---++ const reorderModalTags = async (fromIndex, toIndex) => {+ if (!editingRecording.value || !editingRecording.value.tags) return;++ // Reorder locally first for immediate visual feedback+ const tags = [...editingRecording.value.tags];+ const [removed] = tags.splice(fromIndex, 1);+ tags.splice(toIndex, 0, removed);+ editingRecording.value.tags = tags;++ // Update in recordings list+ const recordingInList = recordings.value.find(r => r.id === editingRecording.value.id);+ if (recordingInList && recordingInList !== editingRecording.value) {+ recordingInList.tags = [...tags];+ }++ // Update selectedRecording if it matches+ if (selectedRecording.value && selectedRecording.value.id === editingRecording.value.id) {+ selectedRecording.value.tags = [...tags];+ }++ // Persist to backend+ try {+ const csrfToken = document.querySelector('meta[name="csrf-token"]')?.getAttribute('content');+ const tagIds = tags.map(t => t.id);++ const response = await fetch(`/api/recordings/${editingRecording.value.id}/tags/reorder`, {+ method: 'PUT',+ headers: {+ 'Content-Type': 'application/json',+ 'X-CSRFToken': csrfToken+ },+ body: JSON.stringify({ tag_ids: tagIds })+ });++ if (!response.ok) {+ const errorData = await response.json();+ throw new Error(errorData.error || 'Failed to reorder tags');+ }++ showToast('Tags reordered', 'fa-arrows-alt', 1500, 'success');++ } catch (error) {+ console.error('Error reordering tags:', error);+ setGlobalError(`Failed to save tag order: ${error.message}`);+ }+ };++ // === MOUSE DRAG HANDLERS (Modal) ===+ const handleModalTagDragStart = (index, event) => {+ modalDraggedTagIndex.value = index;+ event.dataTransfer.effectAllowed = 'move';+ event.dataTransfer.setData('text/plain', index.toString());+ };++ const handleModalTagDragOver = (index, event) => {+ event.preventDefault();+ event.dataTransfer.dropEffect = 'move';+ modalDragOverTagIndex.value = index;+ };++ const handleModalTagDrop = (targetIndex, event) => {+ event.preventDefault();+ if (modalDraggedTagIndex.value !== null && modalDraggedTagIndex.value !== targetIndex) {+ reorderModalTags(modalDraggedTagIndex.value, targetIndex);+ }+ modalDraggedTagIndex.value = null;+ modalDragOverTagIndex.value = null;+ };++ const handleModalTagDragEnd = () => {+ modalDraggedTagIndex.value = null;+ modalDragOverTagIndex.value = null;+ };++ // === TOUCH HANDLERS (Modal - Mobile) ===+ let modalTouchStartIndex = null;++ const handleModalTagTouchStart = (index, event) => {+ modalTouchStartIndex = index;+ modalDraggedTagIndex.value = index;+ };++ const handleModalTagTouchMove = (event) => {+ if (modalTouchStartIndex === null) return;+ event.preventDefault();++ const touch = event.touches[0];+ const elementBelow = document.elementFromPoint(touch.clientX, touch.clientY);+ const tagElement = elementBelow?.closest('[data-modal-tag-index]');++ if (tagElement) {+ const targetIndex = parseInt(tagElement.dataset.modalTagIndex);+ modalDragOverTagIndex.value = targetIndex;+ }+ };++ const handleModalTagTouchEnd = () => {+ if (modalTouchStartIndex !== null && modalDragOverTagIndex.value !== null &&+ modalTouchStartIndex !== modalDragOverTagIndex.value) {+ reorderModalTags(modalTouchStartIndex, modalDragOverTagIndex.value);+ }+ modalTouchStartIndex = null;+ modalDraggedTagIndex.value = null;+ modalDragOverTagIndex.value = null;+ };+return {// ComputedfilteredAvailableTagsForModal,@@ -169,6 +281,17 @@ export function useTags({editRecordingTags,closeEditTagsModal,addTagToRecording,- removeTagFromRecording+ removeTagFromRecording,++ // Modal Tag Drag-and-Drop+ modalDraggedTagIndex,+ modalDragOverTagIndex,+ handleModalTagDragStart,+ handleModalTagDragOver,+ handleModalTagDrop,+ handleModalTagDragEnd,+ handleModalTagTouchStart,+ handleModalTagTouchMove,+ handleModalTagTouchEnd};}diff --git a/static/js/modules/composables/upload.js b/static/js/modules/composables/upload.jsindex 57f99f0b..f6c9a7c6 100644--- a/static/js/modules/composables/upload.js+++ b/static/js/modules/composables/upload.js@@ -38,7 +38,7 @@ export function useUpload(state, utils) {dragover, availableTags, uploadTagSearchFilter} = state;- const { computed, nextTick } = Vue;+ const { computed, nextTick, ref } = Vue;const { setGlobalError, showToast, formatFileSize, onChatComplete } = utils;@@ -49,6 +49,78 @@ export function useUpload(state, utils) {).filter(Boolean);});+ // --- Tag Drag-and-Drop State ---+ const draggedTagIndex = ref(null);+ const dragOverTagIndex = ref(null);++ // Reorder selectedTagIds array+ const reorderSelectedTags = (fromIndex, toIndex) => {+ const tagIds = [...selectedTagIds.value];+ const [removed] = tagIds.splice(fromIndex, 1);+ tagIds.splice(toIndex, 0, removed);+ selectedTagIds.value = tagIds;+ applyTagDefaults(); // Re-apply defaults since first tag may have changed+ };++ // === MOUSE DRAG HANDLERS ===+ const handleTagDragStart = (index, event) => {+ draggedTagIndex.value = index;+ event.dataTransfer.effectAllowed = 'move';+ event.dataTransfer.setData('text/plain', index.toString());+ };++ const handleTagDragOver = (index, event) => {+ event.preventDefault();+ event.dataTransfer.dropEffect = 'move';+ dragOverTagIndex.value = index;+ };++ const handleTagDrop = (targetIndex, event) => {+ event.preventDefault();+ if (draggedTagIndex.value !== null && draggedTagIndex.value !== targetIndex) {+ reorderSelectedTags(draggedTagIndex.value, targetIndex);+ }+ draggedTagIndex.value = null;+ dragOverTagIndex.value = null;+ };++ const handleTagDragEnd = () => {+ draggedTagIndex.value = null;+ dragOverTagIndex.value = null;+ };++ // === TOUCH HANDLERS (Mobile) ===+ let touchStartIndex = null;++ const handleTagTouchStart = (index, event) => {+ touchStartIndex = index;+ draggedTagIndex.value = index;+ };++ const handleTagTouchMove = (event) => {+ if (touchStartIndex === null) return;+ event.preventDefault();++ const touch = event.touches[0];+ const elementBelow = document.elementFromPoint(touch.clientX, touch.clientY);+ const tagElement = elementBelow?.closest('[data-tag-index]');++ if (tagElement) {+ const targetIndex = parseInt(tagElement.dataset.tagIndex);+ dragOverTagIndex.value = targetIndex;+ }+ };++ const handleTagTouchEnd = () => {+ if (touchStartIndex !== null && dragOverTagIndex.value !== null &&+ touchStartIndex !== dragOverTagIndex.value) {+ reorderSelectedTags(touchStartIndex, dragOverTagIndex.value);+ }+ touchStartIndex = null;+ draggedTagIndex.value = null;+ dragOverTagIndex.value = null;+ };+// Handle drag eventsconst handleDragOver = (e) => {e.preventDefault();@@ -671,6 +743,16 @@ export function useUpload(state, utils) {addTagToSelection,removeTagFromSelection,applyTagDefaults,- filteredAvailableTagsForUpload+ filteredAvailableTagsForUpload,+ // Tag drag-and-drop+ draggedTagIndex,+ dragOverTagIndex,+ handleTagDragStart,+ handleTagDragOver,+ handleTagDrop,+ handleTagDragEnd,+ handleTagTouchStart,+ handleTagTouchMove,+ handleTagTouchEnd};}diff --git a/templates/components/recording-view.html b/templates/components/recording-view.htmlindex ac223c8d..ec33f8c4 100644--- a/templates/components/recording-view.html+++ b/templates/components/recording-view.html@@ -102,25 +102,42 @@Select Tags (optional, in priority order)</label>- <!-- Selected Tags Display - Compact -->+ <!-- Selected Tags Display - Compact with Drag Reorder --><div v-if="selectedTags.length > 0" class="mb-2">- <div class="p-1.5 bg-[var(--bg-tertiary)] rounded-md border border-[var(--border-secondary)] max-h-16 overflow-y-auto">+ <div class="p-1.5 bg-[var(--bg-tertiary)] rounded-md border border-[var(--border-secondary)] max-h-16 overflow-y-auto"+ @touchmove="handleTagTouchMove"><div class="flex flex-wrap gap-1"><span v-for="(tag, index) in selectedTags" :key="tag.id"- class="inline-flex items-center px-1.5 py-0.5 rounded-full text-[11px] font-medium cursor-help"+ :data-tag-index="index"+ draggable="true"+ @dragstart="handleTagDragStart(index, $event)"+ @dragover="handleTagDragOver(index, $event)"+ @drop="handleTagDrop(index, $event)"+ @dragend="handleTagDragEnd"+ @touchstart="handleTagTouchStart(index, $event)"+ @touchend="handleTagTouchEnd"+ :class="[+ 'inline-flex items-center px-1.5 py-0.5 rounded-full text-[11px] font-medium transition-all duration-150',+ draggedTagIndex === index ? 'opacity-50 cursor-grabbing' : 'cursor-grab',+ dragOverTagIndex === index && draggedTagIndex !== index ? 'ring-2 ring-[var(--ring-focus)] ring-offset-1' : ''+ ]":style="{ backgroundColor: tag.color, color: getContrastTextColor(tag.color) }"- :title="tag.group_id ? ('Group: ' + tag.group_name) : tag.name">+ :title="(tag.group_id ? ('Group: ' + tag.group_name) : tag.name) + ' (drag to reorder)'"><span class="opacity-75 mr-0.5 text-[9px]">${index + 1}.</span><i v-if="tag.group_id" class="fas fa-users mr-1 text-[9px]"></i><span v-if="tag.group_id" class="opacity-75">${tag.group_name}: </span><span>${tag.name}</span>- <button @click="removeTagFromSelection(tag.id)"+ <button @click.stop="removeTagFromSelection(tag.id)"class="ml-1 hover:opacity-100 opacity-70"><i class="fas fa-times" style="font-size: 9px;"></i></button></span></div></div>+ <p class="text-[10px] text-[var(--text-muted)] mt-0.5">+ <i class="fas fa-grip-vertical mr-0.5" style="font-size: 9px;"></i>+ Drag to reorder • First tag's defaults applied+ </p></div><!-- Tag Selection Container - Compact -->diff --git a/templates/components/upload-view.html b/templates/components/upload-view.htmlindex b4f6a9a4..d408e62b 100644--- a/templates/components/upload-view.html+++ b/templates/components/upload-view.html@@ -121,19 +121,32 @@${ t('tags.title') }</label>- <!-- Selected Tags Display - Compact -->+ <!-- Selected Tags Display - Compact with Drag Reorder --><div v-if="selectedTags.length > 0" class="mb-2">- <div class="p-1.5 bg-[var(--bg-tertiary)] rounded-md border border-[var(--border-secondary)] max-h-16 overflow-y-auto">+ <div class="p-1.5 bg-[var(--bg-tertiary)] rounded-md border border-[var(--border-secondary)] max-h-16 overflow-y-auto"+ @touchmove="handleTagTouchMove"><div class="flex flex-wrap gap-1"><span v-for="(tag, index) in selectedTags" :key="tag.id"- class="inline-flex items-center px-1.5 py-0.5 rounded-full text-[11px] font-medium cursor-help"+ :data-tag-index="index"+ draggable="true"+ @dragstart="handleTagDragStart(index, $event)"+ @dragover="handleTagDragOver(index, $event)"+ @drop="handleTagDrop(index, $event)"+ @dragend="handleTagDragEnd"+ @touchstart="handleTagTouchStart(index, $event)"+ @touchend="handleTagTouchEnd"+ :class="[+ 'inline-flex items-center px-1.5 py-0.5 rounded-full text-[11px] font-medium transition-all duration-150',+ draggedTagIndex === index ? 'opacity-50 cursor-grabbing' : 'cursor-grab',+ dragOverTagIndex === index && draggedTagIndex !== index ? 'ring-2 ring-[var(--ring-focus)] ring-offset-1' : ''+ ]":style="{ backgroundColor: tag.color, color: getContrastTextColor(tag.color) }"- :title="tag.group_id ? ('Group: ' + tag.group_name) : tag.name">+ :title="(tag.group_id ? ('Group: ' + tag.group_name) : tag.name) + ' (drag to reorder)'"><span class="opacity-75 mr-0.5 text-[9px]">${index + 1}.</span><i v-if="tag.group_id" class="fas fa-users mr-1 text-[9px]"></i><span v-if="tag.group_id" class="opacity-75">${tag.group_name}: </span><span>${tag.name}</span>- <button @click="removeTagFromSelection(tag.id)"+ <button @click.stop="removeTagFromSelection(tag.id)"class="ml-1 hover:opacity-100 opacity-70"><i class="fas fa-times" style="font-size: 9px;"></i></button>@@ -141,8 +154,8 @@</div></div><p class="text-[10px] text-[var(--text-muted)] mt-0.5">- <i class="fas fa-info-circle mr-0.5" style="font-size: 9px;"></i>- Tags ordered by priority+ <i class="fas fa-grip-vertical mr-0.5" style="font-size: 9px;"></i>+ Drag to reorder • First tag's defaults applied</p></div>diff --git a/templates/modals/edit-tags-modal.html b/templates/modals/edit-tags-modal.htmlindex baa84c7c..b359596d 100644--- a/templates/modals/edit-tags-modal.html+++ b/templates/modals/edit-tags-modal.html@@ -10,22 +10,41 @@</div></div><div class="p-6 overflow-y-auto flex-1">- <!-- Current Tags -->+ <!-- Current Tags with Drag Reorder --><div class="mb-4"><label class="block text-sm font-medium text-[var(--text-secondary)] mb-2" v-text="t('tags.currentTags')"></label>- <div v-if="selectedRecording && getRecordingTags(selectedRecording).length > 0" class="flex flex-wrap gap-2">- <span v-for="tag in getRecordingTags(selectedRecording)" :key="tag.id"- class="inline-flex items-center px-3 py-1 rounded-full text-sm font-medium cursor-help"+ <div v-if="editingRecording && editingRecording.tags && editingRecording.tags.length > 0"+ class="flex flex-wrap gap-2"+ @touchmove="handleModalTagTouchMove">+ <span v-for="(tag, index) in editingRecording.tags" :key="tag.id"+ :data-modal-tag-index="index"+ draggable="true"+ @dragstart="handleModalTagDragStart(index, $event)"+ @dragover="handleModalTagDragOver(index, $event)"+ @drop="handleModalTagDrop(index, $event)"+ @dragend="handleModalTagDragEnd"+ @touchstart="handleModalTagTouchStart(index, $event)"+ @touchend="handleModalTagTouchEnd"+ :class="[+ 'inline-flex items-center px-3 py-1 rounded-full text-sm font-medium transition-all duration-150',+ modalDraggedTagIndex === index ? 'opacity-50 cursor-grabbing' : 'cursor-grab',+ modalDragOverTagIndex === index && modalDraggedTagIndex !== index ? 'ring-2 ring-[var(--ring-focus)] ring-offset-1' : ''+ ]":style="{ backgroundColor: tag.color, color: getContrastTextColor(tag.color) }"- :title="tag.group_id ? ('Group: ' + tag.group_name) : tag.name">+ :title="(tag.group_id ? ('Group: ' + tag.group_name) : tag.name) + ' (drag to reorder)'">+ <span class="opacity-75 mr-1 text-xs">${index + 1}.</span><i v-if="tag.group_id" class="fas fa-users mr-1.5 text-xs"></i><span v-if="tag.group_id" class="opacity-75">${ tag.group_name }: </span>${ tag.name }- <button @click="removeTagFromRecording(tag.id)" class="ml-2 hover:opacity-80">+ <button @click.stop="removeTagFromRecording(tag.id)" class="ml-2 hover:opacity-80"><i class="fas fa-times text-xs"></i></button></span></div><p v-else class="text-sm text-[var(--text-muted)] italic" v-text="t('tags.noTags')"></p>+ <p v-if="editingRecording && editingRecording.tags && editingRecording.tags.length > 1" class="text-xs text-[var(--text-muted)] mt-2">+ <i class="fas fa-grip-vertical mr-1" style="font-size: 10px;"></i>+ Drag to reorder • First tag's defaults are applied+ </p></div><!-- Add Tags -->