Skip to content

Commit de1c184

Browse files
committed
better ux
1 parent 69839db commit de1c184

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

assets/js/bloecks.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,7 @@ var BLOECKS = (function($) {
275275
initDragDrop();
276276
checkForMessages();
277277
initCopyPasteHandlers();
278+
// Button states will be updated after loadMultiClipboardFromServer() completes
278279
});
279280

280281
// Check for BLOECKS messages and show as toasts
@@ -489,6 +490,7 @@ var BLOECKS = (function($) {
489490
checkForMessages();
490491
initCopyPasteHandlers();
491492
checkForScrollTarget(); // Add this call!
493+
// Button states will be updated via loadMultiClipboardFromServer()
492494
}, 100);
493495
}
494496
});
@@ -502,6 +504,7 @@ var BLOECKS = (function($) {
502504
checkForMessages();
503505
initCopyPasteHandlers();
504506
checkForScrollTarget(); // Add this call!
507+
// Button states will be updated via loadMultiClipboardFromServer()
505508
}, 150);
506509
}
507510
});
@@ -524,6 +527,7 @@ var BLOECKS = (function($) {
524527
checkForMessages();
525528
initCopyPasteHandlers();
526529
checkForScrollTarget();
530+
// Button states will be updated via loadMultiClipboardFromServer()
527531
}, 100);
528532
}
529533
});
@@ -651,6 +655,11 @@ var BLOECKS = (function($) {
651655
container: '#rex-js-page-main-content',
652656
fragment: '#rex-js-page-main-content',
653657
push: false // Important: don't push to history
658+
}).done(function() {
659+
// After PJAX reload, restore button states based on clipboard
660+
setTimeout(function() {
661+
updateAllButtonStates();
662+
}, 100);
654663
});
655664
}, 800); // Shorter wait time
656665
} else {
@@ -706,13 +715,42 @@ var BLOECKS = (function($) {
706715
}
707716

708717
updatePasteButtons();
718+
// Update button states after adding
719+
updateAllButtonStates();
709720
}
710721

711722
function removeFromMultiClipboard(sliceId) {
712723
multiClipboard = multiClipboard.filter(function(item) {
713724
return item.source_slice_id !== sliceId;
714725
});
715726
updatePasteButtons();
727+
// Update button states after removal
728+
updateAllButtonStates();
729+
}
730+
731+
// Update all copy/cut button states based on current clipboard
732+
function updateAllButtonStates() {
733+
// Reset all buttons first
734+
$('.bloecks-copy, .bloecks-cut').each(function() {
735+
var $btn = $(this);
736+
$btn.removeClass('btn-success')
737+
.removeClass('active')
738+
.removeAttr('data-bloecks-cutncopy-iscopied');
739+
});
740+
741+
// Mark buttons for items in clipboard
742+
multiClipboard.forEach(function(item) {
743+
var sliceId = item.source_slice_id;
744+
var action = item.action;
745+
var selector = '.bloecks-' + action + '[data-slice-id="' + sliceId + '"]';
746+
747+
$(selector).each(function() {
748+
var $btn = $(this);
749+
$btn.addClass('btn-success')
750+
.addClass('active')
751+
.attr('data-bloecks-cutncopy-iscopied', 'true');
752+
});
753+
});
716754
}
717755

718756
function clearMultiClipboard() {
@@ -1051,6 +1089,8 @@ var BLOECKS = (function($) {
10511089
// Re-check multi-clipboard config after navigation
10521090
if (typeof BLOECKS_MULTI_CLIPBOARD !== 'undefined' && BLOECKS_MULTI_CLIPBOARD) {
10531091
setMultiClipboardEnabled(true);
1092+
// Update button states after PJAX reload
1093+
updateAllButtonStates();
10541094
loadMultiClipboardFromServer();
10551095
}
10561096
}, 100);
@@ -1083,6 +1123,8 @@ var BLOECKS = (function($) {
10831123
}
10841124

10851125
updatePasteButtons();
1126+
// Update button states after loading from server
1127+
updateAllButtonStates();
10861128
}
10871129
},
10881130
error: function() {
@@ -1112,6 +1154,7 @@ var BLOECKS = (function($) {
11121154
removeFromMultiClipboard: removeFromMultiClipboard,
11131155
clearMultiClipboard: clearMultiClipboard,
11141156
loadMultiClipboardFromServer: loadMultiClipboardFromServer,
1157+
updateAllButtonStates: updateAllButtonStates,
11151158
version: '2.5.0'
11161159
};
11171160

@@ -1126,6 +1169,9 @@ $(document).ready(function() {
11261169
if (typeof BLOECKS_MULTI_CLIPBOARD !== 'undefined' && BLOECKS_MULTI_CLIPBOARD) {
11271170
BLOECKS.setMultiClipboardEnabled(true);
11281171
}
1172+
1173+
// Load clipboard status from server (which will call updateAllButtonStates)
1174+
BLOECKS.loadMultiClipboardFromServer();
11291175
});
11301176

11311177
// Re-initialize after PJAX navigation
@@ -1140,6 +1186,7 @@ $(document).on('pjax:complete pjax:end rex:ready', function() {
11401186
}
11411187

11421188
// Load current clipboard status to sync frontend with backend
1189+
// This will also call updateAllButtonStates() in its success callback
11431190
BLOECKS.loadMultiClipboardFromServer();
11441191

11451192
// Check for scroll target after PJAX navigation (important for paste operations)
@@ -1165,6 +1212,10 @@ if (typeof MutationObserver !== 'undefined') {
11651212
if (shouldReinit) {
11661213
setTimeout(function() {
11671214
BLOECKS.initCopyPasteHandlers();
1215+
// Update button states when new buttons are added
1216+
if (typeof BLOECKS.updateAllButtonStates === 'function') {
1217+
BLOECKS.updateAllButtonStates();
1218+
}
11681219
}, 50);
11691220
}
11701221
});

0 commit comments

Comments
 (0)