dockview-core
Version:
Zero dependency layout manager supporting tabs, groups, grids and splitviews for vanilla TypeScript
179 lines (178 loc) • 9.43 kB
JavaScript
import { LocalSelectionTransfer, PanelTransfer, } from '../../../dnd/dataTransfer';
import { html5Backend, pointerBackend, } from '../../../dnd/backend';
import { Emitter } from '../../../events';
import { CompositeDisposable, Disposable, MutableDisposable, } from '../../../lifecycle';
import { toggleClass } from '../../../dom';
import { resolveDndCapabilities } from '../../dndCapabilities';
// Floating-group redock via touch: require a deliberate long press so the
// "move the float around" gesture doesn't double-trigger the redock ghost.
// Infinity pressTolerance disables the pre-arm flick override; any motion
// during the wait is treated as drag-the-float, not redock intent.
const FLOATING_REDOCK_INITIATION_DELAY_MS = 500;
/**
* The drag-source half of a group drag handle: html5 + pointer drag sources
* that publish a group-level `PanelTransfer`, the "Multiple Panels (N)" ghost,
* and the floating-group disambiguation that keeps the redock gesture from
* firing alongside the overlay's move-the-float gesture.
*
* Shared by the tab-bar void container and the dedicated floating title bar so
* both grab handles redock identically.
*/
export class GroupDragSource extends CompositeDisposable {
// Resolved lazily so a retargetable handle (the floating title bar) always
// drags the window's *current* anchor group, not the one captured here.
get group() {
return this.groupAccessor();
}
constructor(options) {
var _a, _b, _c;
super();
this.panelTransfer = LocalSelectionTransfer.getInstance();
this._onDragStart = new Emitter();
this.onDragStart = this._onDragStart.event;
this._element = options.element;
this.accessor = options.accessor;
const group = options.group;
this.groupAccessor = typeof group === 'function' ? group : () => group;
this.isFloatingMoveHandle =
(_a = options.isFloatingMoveHandle) !== null && _a !== void 0 ? _a : (() => true);
const caps = resolveDndCapabilities(this.accessor.options);
this._element.draggable = caps.html5;
toggleClass(this._element, 'dv-draggable', caps.html5 || caps.pointer);
this.addDisposables(this._onDragStart);
const buildMultiPanelsGhost = () => {
const ghostEl = document.createElement('div');
const style = window.getComputedStyle(this._element);
const bgColor = style.getPropertyValue('--dv-activegroup-visiblepanel-tab-background-color');
const color = style.getPropertyValue('--dv-activegroup-visiblepanel-tab-color');
ghostEl.style.backgroundColor = bgColor;
ghostEl.style.color = color;
ghostEl.style.padding = '2px 8px';
ghostEl.style.height = '24px';
ghostEl.style.fontSize = '11px';
ghostEl.style.lineHeight = '20px';
ghostEl.style.borderRadius = '12px';
ghostEl.style.whiteSpace = 'nowrap';
ghostEl.style.boxSizing = 'border-box';
// HTML5 setDragImage snapshots the element as appended to the
// document; a default block-level div would stretch to the
// body's width and render as a viewport-wide bar.
ghostEl.style.display = 'inline-block';
ghostEl.textContent = `Multiple Panels (${this.group.size})`;
return ghostEl;
};
const buildGhostSpec = () => {
var _a, _b;
// The custom-ghost resolution (createGroupDragGhostComponent) is
// owned by the AdvancedDnD module; core keeps the default chip and
// falls back to it when no custom ghost is produced (incl. when
// the module is absent).
const customGhost = (_b = (_a = this.accessor).buildGroupDragGhost) === null || _b === void 0 ? void 0 : _b.call(_a, this.group);
if (customGhost) {
return customGhost;
}
return {
element: buildMultiPanelsGhost(),
offsetX: 30,
offsetY: -10,
};
};
const sharedDragOptions = {
getData: () => {
this.panelTransfer.setData([new PanelTransfer(this.accessor.id, this.group.id, null)], PanelTransfer.prototype);
return {
dispose: () => {
this.panelTransfer.clearData(PanelTransfer.prototype);
},
};
},
createGhost: buildGhostSpec,
onDragStart: (event) => {
this._onDragStart.fire(event);
},
};
this.html5DragSource = html5Backend.createDragSource(this._element, Object.assign(Object.assign({}, sharedDragOptions), { disabled: !caps.html5, isCancelled: (event) => {
// HTML5: when this element is the floating window's move
// handle, redock needs shift+drag (otherwise click-and-drag
// conflicts with moving the float). A non-move-handle (e.g. the
// void container when a title bar moves the float) redocks with
// a plain drag, like a group in the main grid.
if (this.group.api.location.type === 'floating' &&
this.isFloatingMoveHandle() &&
!event.shiftKey) {
return true;
}
if (this.group.api.location.type === 'edge' &&
this.group.size === 0) {
return true;
}
return false;
} }));
// Only the move handle needs the touch disambiguation; other handles
// redock with the normal grid press behaviour.
const isFloating = () => {
var _a, _b, _c;
return ((_c = (_b = (_a = this.group) === null || _a === void 0 ? void 0 : _a.api) === null || _b === void 0 ? void 0 : _b.location) === null || _c === void 0 ? void 0 : _c.type) === 'floating' &&
this.isFloatingMoveHandle();
};
this.pointerDragSource = pointerBackend.createDragSource(this._element, Object.assign(Object.assign({}, sharedDragOptions), { disabled: !caps.pointer, touchOnly: !caps.pointerHandlesMouse,
// Floating groups share this element with the overlay's
// move-the-float drag. Without a longer hold + tolerance
// override, both gestures commit simultaneously and the
// user sees the float follow their finger *and* a ghost.
touchInitiationDelay: () => isFloating() ? FLOATING_REDOCK_INITIATION_DELAY_MS : 250, pressTolerance: () => (isFloating() ? Infinity : 8), isCancelled: () => {
if (!resolveDndCapabilities(this.accessor.options).pointer) {
return true;
}
// Pointer: long-press IS the deliberate gesture, so
// floating groups don't need the shift gate.
if (this.group.api.location.type === 'edge' &&
this.group.size === 0) {
return true;
}
return false;
}, onDragStart: (event) => {
var _a;
// Redock just committed — abort any in-flight overlay
// move so the float stops following the finger while
// the ghost takes over.
(_a = this.getFloatingOverlay()) === null || _a === void 0 ? void 0 : _a.cancelPendingDrag();
this._onDragStart.fire(event);
} }));
// Mirror direction: once the overlay's move-the-float gesture has
// actually moved something, cancel the pending redock arm so the
// ghost doesn't appear mid-drag if the user holds past 500ms.
const overlayMoveSub = new MutableDisposable();
const refreshOverlayMoveSub = () => {
const overlay = this.getFloatingOverlay();
overlayMoveSub.value = overlay
? overlay.onDidStartMoving(() => {
this.pointerDragSource.cancelPending();
})
: Disposable.NONE;
};
refreshOverlayMoveSub();
this.addDisposables(overlayMoveSub);
const locationChange = (_c = (_b = this.group) === null || _b === void 0 ? void 0 : _b.api) === null || _c === void 0 ? void 0 : _c.onDidLocationChange;
if (locationChange) {
this.addDisposables(locationChange(refreshOverlayMoveSub));
}
this.addDisposables(this.html5DragSource, this.pointerDragSource);
}
updateDragAndDropState() {
const caps = resolveDndCapabilities(this.accessor.options);
this._element.draggable = caps.html5;
toggleClass(this._element, 'dv-draggable', caps.html5 || caps.pointer);
this.html5DragSource.setDisabled(!caps.html5);
this.pointerDragSource.setDisabled(!caps.pointer);
this.pointerDragSource.setTouchOnly(!caps.pointerHandlesMouse);
}
getFloatingOverlay() {
var _a, _b;
if (!this.group) {
return undefined;
}
return (_b = (_a = this.accessor.floatingGroups) === null || _a === void 0 ? void 0 : _a.find((fg) => fg.group === this.group)) === null || _b === void 0 ? void 0 : _b.overlay;
}
}
export { FLOATING_REDOCK_INITIATION_DELAY_MS };