mirror of
https://github.com/zotero/zotero.git
synced 2026-11-16 09:41:38 +00:00
Handle item and collection drags over the tab bar (#6035)
Holding a drag over a tab for a moment selects it, so that whatever is being dragged can be dropped into that tab's content (e.g., items or collections onto a collection or child items onto a parent item), including in another window. Items, collections, and saved searches dropped onto the tab bar open in new tabs: attachments and their parent items in the reader, notes in note tabs, and collections and saved searches in library tabs.
This commit is contained in:
parent
917da3bd19
commit
687098b60d
3 changed files with 244 additions and 9 deletions
|
|
@ -132,6 +132,8 @@ const DRAG_THUMBNAIL_OFFSET = -16;
|
|||
const DROP_INDICATOR_INSET = 4;
|
||||
// How far a dragged tab's pointer moves before the tab follows it
|
||||
const DRAG_MOVE_THRESHOLD = 8;
|
||||
// How long a drag rests on a tab before the tab is selected
|
||||
const SPRING_LOAD_DELAY = 750;
|
||||
|
||||
const TabBar = forwardRef(function (props, ref) {
|
||||
const [tabs, setTabs] = useState([]);
|
||||
|
|
@ -156,6 +158,8 @@ const TabBar = forwardRef(function (props, ref) {
|
|||
const tearOffOffsetRef = useRef();
|
||||
// Whether the dragged tab was dropped on this window's own tab bar
|
||||
const droppedInOwnWindowRef = useRef(false);
|
||||
// The tab a drag is resting on, and the timer that selects it
|
||||
const springLoadRef = useRef({ id: null, timer: null });
|
||||
const tabsInnerContainerRef = useRef();
|
||||
const tabsRef = useRef();
|
||||
const spacerRef = useRef();
|
||||
|
|
@ -462,16 +466,61 @@ const TabBar = forwardRef(function (props, ref) {
|
|||
return index === -1 ? tabNodes.length : index;
|
||||
}
|
||||
|
||||
const handleTabBarDragOver = useCallback((event) => {
|
||||
event.preventDefault();
|
||||
event.dataTransfer.dropEffect = 'move';
|
||||
// A tab from another window is only inserted when dropped, so show where it would go
|
||||
if (!dragIDRef.current) {
|
||||
if (event.dataTransfer.types.includes('zotero/tab')) {
|
||||
setDropIndex(getDropIndex(event.clientX));
|
||||
}
|
||||
function cancelSpringLoad() {
|
||||
clearTimeout(springLoadRef.current.timer);
|
||||
springLoadRef.current = { id: null, timer: null };
|
||||
}
|
||||
|
||||
// Select a tab that a drag of something other than a tab rests on, so that it can be dropped
|
||||
// in the tab's content
|
||||
const handleSpringLoadDragOver = useCallback((event) => {
|
||||
if (dragIDRef.current || event.dataTransfer.types.includes('zotero/tab')) {
|
||||
return;
|
||||
}
|
||||
let tabNode = event.target.closest('.tab');
|
||||
let id = tabNode?.dataset.id || null;
|
||||
if (id === springLoadRef.current.id) {
|
||||
return;
|
||||
}
|
||||
cancelSpringLoad();
|
||||
if (!id || tabNode.classList.contains('selected')) {
|
||||
return;
|
||||
}
|
||||
let timer = setTimeout(() => {
|
||||
springLoadRef.current.timer = null;
|
||||
props.onTabSelect(id);
|
||||
}, SPRING_LOAD_DELAY);
|
||||
springLoadRef.current = { id, timer };
|
||||
}, [props.onTabSelect]);
|
||||
|
||||
const handleSpringLoadDragLeave = useCallback((event) => {
|
||||
if (tabsInnerContainerRef.current.contains(event.relatedTarget)) {
|
||||
return;
|
||||
}
|
||||
cancelSpringLoad();
|
||||
}, []);
|
||||
|
||||
const handleTabBarDragOver = useCallback((event) => {
|
||||
let { dataTransfer } = event;
|
||||
// A tab from another window, or objects to open in new tabs, are only taken when
|
||||
// dropped, so show where they would go
|
||||
if (!dragIDRef.current) {
|
||||
let isTab = dataTransfer.types.includes('zotero/tab');
|
||||
if (!isTab && !props.canOpenDrop(dataTransfer)) {
|
||||
return;
|
||||
}
|
||||
event.preventDefault();
|
||||
let effect = isTab || dataTransfer.effectAllowed == 'move' ? 'move' : 'copy';
|
||||
// See LibraryTree#setDropEffect()
|
||||
if (!isTab && (Zotero.isWin || Zotero.isLinux)) {
|
||||
dataTransfer.effectAllowed = effect;
|
||||
}
|
||||
dataTransfer.dropEffect = effect;
|
||||
setDropIndex(getDropIndex(event.clientX));
|
||||
return;
|
||||
}
|
||||
event.preventDefault();
|
||||
dataTransfer.dropEffect = 'move';
|
||||
setDragImageShown(event.dataTransfer, false);
|
||||
// Start moving the tab once the pointer passes the drag threshold
|
||||
if (!dragMovedRef.current) {
|
||||
|
|
@ -552,6 +601,14 @@ const TabBar = forwardRef(function (props, ref) {
|
|||
|
||||
const handleTabBarDrop = useCallback((event) => {
|
||||
setDropIndex(null);
|
||||
cancelSpringLoad();
|
||||
if (!event.dataTransfer.types.includes('zotero/tab')) {
|
||||
if (props.canOpenDrop(event.dataTransfer)) {
|
||||
event.preventDefault();
|
||||
props.onOpenDrop(event.dataTransfer, getDropIndex(event.clientX));
|
||||
}
|
||||
return;
|
||||
}
|
||||
let payload;
|
||||
try {
|
||||
payload = JSON.parse(event.dataTransfer.getData('zotero/tab'));
|
||||
|
|
@ -570,7 +627,7 @@ const TabBar = forwardRef(function (props, ref) {
|
|||
}
|
||||
event.preventDefault();
|
||||
props.onTabDropFromWindow(payload.windowID, payload.id, getDropIndex(event.clientX));
|
||||
}, [props.onTabDropFromWindow, props.windowID]);
|
||||
}, [props.onTabDropFromWindow, props.canOpenDrop, props.onOpenDrop, props.windowID]);
|
||||
|
||||
const handleTabClose = useCallback((event, id) => {
|
||||
// When a tab is being closed, lock the widths of tabs and scroll position,
|
||||
|
|
@ -639,6 +696,8 @@ const TabBar = forwardRef(function (props, ref) {
|
|||
className="tab-bar-inner-container"
|
||||
onWheel={handleWheel}
|
||||
onMouseLeave={handleTabBarMouseLeave}
|
||||
onDragOver={handleSpringLoadDragOver}
|
||||
onDragLeave={handleSpringLoadDragLeave}
|
||||
>
|
||||
<div className="pinned-tabs">
|
||||
<div
|
||||
|
|
@ -725,6 +784,8 @@ TabBar.propTypes = {
|
|||
onLoad: PropTypes.func.isRequired,
|
||||
onTabMove: PropTypes.func.isRequired,
|
||||
onTabDropFromWindow: PropTypes.func.isRequired,
|
||||
canOpenDrop: PropTypes.func.isRequired,
|
||||
onOpenDrop: PropTypes.func.isRequired,
|
||||
onDrawDragThumbnail: PropTypes.func.isRequired,
|
||||
onTabTearOff: PropTypes.func.isRequired,
|
||||
onRefocus: PropTypes.func.isRequired,
|
||||
|
|
|
|||
|
|
@ -743,6 +743,8 @@ var Zotero_Tabs = new function () {
|
|||
onTabSelect={this.select.bind(this)}
|
||||
onTabMove={this.move.bind(this)}
|
||||
onTabDropFromWindow={this._onTabDropFromWindow.bind(this)}
|
||||
canOpenDrop={this._canOpenDrop.bind(this)}
|
||||
onOpenDrop={this._onOpenDrop.bind(this)}
|
||||
onDrawDragThumbnail={this._drawThumbnail.bind(this)}
|
||||
onTabTearOff={this._onTabTearOff.bind(this)}
|
||||
onTabClose={id => this.canClose(id) && this.close(id)}
|
||||
|
|
@ -1348,6 +1350,83 @@ var Zotero_Tabs = new function () {
|
|||
.catch(e => Zotero.logError(e));
|
||||
};
|
||||
|
||||
/**
|
||||
* Whether a drag holds items, collections, or saved searches, which open in new tabs when
|
||||
* dropped on the tab bar
|
||||
*
|
||||
* @param {DataTransfer} dataTransfer
|
||||
* @return {Boolean}
|
||||
*/
|
||||
this._canOpenDrop = function (dataTransfer) {
|
||||
return ['zotero/item', 'zotero/collection', 'zotero/search']
|
||||
.some(type => dataTransfer.types.includes(type));
|
||||
};
|
||||
|
||||
/**
|
||||
* Open items, collections, or saved searches dropped on the tab bar in new tabs, selecting
|
||||
* the first
|
||||
*
|
||||
* @param {DataTransfer} dataTransfer
|
||||
* @param {Integer} index
|
||||
*/
|
||||
this._onOpenDrop = function (dataTransfer, index) {
|
||||
let { dataType, data: ids } = Zotero.DragDrop.getDataFromDataTransfer(dataTransfer);
|
||||
this._openDroppedObjects(dataType, ids, index)
|
||||
.catch(e => Zotero.logError(e));
|
||||
};
|
||||
|
||||
this._openDroppedObjects = async function (dataType, ids, index) {
|
||||
let select = true;
|
||||
if (dataType == 'zotero/collection' || dataType == 'zotero/search') {
|
||||
let objectsClass = dataType == 'zotero/collection' ? Zotero.Collections : Zotero.Searches;
|
||||
for (let object of objectsClass.get(ids)) {
|
||||
this.addLibraryTab({
|
||||
title: object.name,
|
||||
icon: dataType == 'zotero/collection' ? 'collection' : 'search',
|
||||
state: { collections: [object.treeViewID], focusedCollection: object.treeViewID },
|
||||
index: index++,
|
||||
select
|
||||
});
|
||||
select = false;
|
||||
}
|
||||
return;
|
||||
}
|
||||
for (let item of Zotero.Items.get(ids)) {
|
||||
let options = { tabIndex: index++, openInBackground: !select, window };
|
||||
select = false;
|
||||
if (item.isNote()) {
|
||||
await Zotero.Notes.open(item.id, null, options);
|
||||
continue;
|
||||
}
|
||||
let location = null;
|
||||
let attachment = item;
|
||||
if (item.isAnnotation()) {
|
||||
location = { annotationID: item.key };
|
||||
attachment = item.parentItem;
|
||||
}
|
||||
else if (item.isRegularItem()) {
|
||||
attachment = await item.getBestAttachment();
|
||||
}
|
||||
if (attachment?.attachmentReaderType) {
|
||||
await Zotero.Reader.open(attachment.id, location, options);
|
||||
continue;
|
||||
}
|
||||
// An item with nothing to open in a tab is shown in its library
|
||||
let library = Zotero.Libraries.get(item.libraryID);
|
||||
this.addLibraryTab({
|
||||
title: library.name,
|
||||
icon: library.libraryType == 'group' ? 'library-group' : 'library',
|
||||
state: {
|
||||
collections: [library.treeViewID],
|
||||
focusedCollection: library.treeViewID,
|
||||
items: [item.treeViewID]
|
||||
},
|
||||
index: options.tabIndex,
|
||||
select: !options.openInBackground
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Select a tab
|
||||
*
|
||||
|
|
|
|||
|
|
@ -981,6 +981,101 @@ describe("Zotero_Tabs", function() {
|
|||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* Drag items, collections, or saved searches over another window's tab bar and drop them
|
||||
* at its end
|
||||
*
|
||||
* @param {String} type
|
||||
* @param {Integer[]} ids
|
||||
*/
|
||||
function dropOnTabBar(type, ids) {
|
||||
let tabBar = win2.document.querySelector('#tab-bar-container .tabs-wrapper .tabs');
|
||||
let { right, top } = tabBar.getBoundingClientRect();
|
||||
let dataTransfer = new win2.DataTransfer();
|
||||
dataTransfer.setData(type, ids.join(','));
|
||||
for (let eventType of ['dragover', 'drop']) {
|
||||
tabBar.dispatchEvent(new win2.DragEvent(eventType, {
|
||||
bubbles: true, cancelable: true, dataTransfer, clientX: right - 5, clientY: top + 5
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
||||
it("should select a tab that a drag rests on", async function () {
|
||||
let targetID = win2.Zotero_Tabs.selectedID;
|
||||
let id = win2.Zotero_Tabs.addLibraryTab();
|
||||
try {
|
||||
await waitForCallback(
|
||||
() => win2.document.querySelector(`#tab-bar-container .tab.selected[data-id="${id}"]`),
|
||||
50,
|
||||
20
|
||||
);
|
||||
let tabNode = win2.document.querySelector(`#tab-bar-container .tab[data-id="${targetID}"]`);
|
||||
let dataTransfer = new win2.DataTransfer();
|
||||
dataTransfer.setData('zotero/item', String(item.id));
|
||||
tabNode.dispatchEvent(
|
||||
new win2.DragEvent('dragover', { bubbles: true, cancelable: true, dataTransfer })
|
||||
);
|
||||
assert.equal(win2.Zotero_Tabs.selectedID, id);
|
||||
await waitForCallback(() => win2.Zotero_Tabs.selectedID == targetID, 100, 20);
|
||||
}
|
||||
finally {
|
||||
win2.Zotero_Tabs.close(id);
|
||||
}
|
||||
});
|
||||
|
||||
it("should open a collection dropped on the tab bar in a new tab", async function () {
|
||||
let count = win2.Zotero_Tabs._tabs.length;
|
||||
dropOnTabBar('zotero/collection', [collection.id]);
|
||||
let newID = win2.Zotero_Tabs.selectedID;
|
||||
try {
|
||||
assert.equal(win2.Zotero_Tabs._tabs.length, count + 1);
|
||||
assert.equal(win2.Zotero_Tabs._getTab(newID).tabIndex, count);
|
||||
await waitForCallback(() => win2.ZoteroPane.getLibraryView(newID)?.loaded, 50, 20);
|
||||
let view = win2.ZoteroPane.getLibraryView(newID);
|
||||
await view.waitForLibraryTabState();
|
||||
assert.deepEqual(view.getCollectionTreeRows().map(row => row.id), [collection.treeViewID]);
|
||||
}
|
||||
finally {
|
||||
win2.Zotero_Tabs.close(newID);
|
||||
}
|
||||
});
|
||||
|
||||
it("should open an item dropped on the tab bar in a reader tab", async function () {
|
||||
let parentItem = await createDataObject('item');
|
||||
let attachment = await importPDFAttachment(parentItem);
|
||||
dropOnTabBar('zotero/item', [parentItem.id]);
|
||||
let reader;
|
||||
await waitForCallback(
|
||||
() => (reader = Zotero.Reader.getByTabID(win2.Zotero_Tabs.selectedID)),
|
||||
50,
|
||||
20
|
||||
);
|
||||
try {
|
||||
assert.equal(reader.itemID, attachment.id);
|
||||
assert.equal(reader._window, win2);
|
||||
}
|
||||
finally {
|
||||
reader.close();
|
||||
}
|
||||
});
|
||||
|
||||
it("should show an item with nothing to open dropped on the tab bar in a library tab", async function () {
|
||||
let newItem = await createDataObject('item');
|
||||
let count = win2.Zotero_Tabs._tabs.length;
|
||||
dropOnTabBar('zotero/item', [newItem.id]);
|
||||
await waitForCallback(() => win2.Zotero_Tabs._tabs.length > count, 50, 20);
|
||||
let newID = win2.Zotero_Tabs.selectedID;
|
||||
try {
|
||||
await waitForCallback(() => win2.ZoteroPane.getLibraryView(newID)?.loaded, 50, 20);
|
||||
let view = win2.ZoteroPane.getLibraryView(newID);
|
||||
await view.waitForLibraryTabState();
|
||||
assert.sameMembers(view.itemsView.getSelectedItems(true), [newItem.id]);
|
||||
}
|
||||
finally {
|
||||
win2.Zotero_Tabs.close(newID);
|
||||
}
|
||||
});
|
||||
|
||||
it("should move a reader tab to another window", async function () {
|
||||
let attachment = await importPDFAttachment(otherItem);
|
||||
let reader = await Zotero.Reader.open(attachment.id, null, { window: win });
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue