UNPKG

dockview

Version:

Zero dependency layout manager supporting tabs, grids and splitviews with ReactJS support

110 lines (109 loc) 3.22 kB
import { tryParseJSON } from '../json'; class TransferObject { constructor() { // } } export class PanelTransfer extends TransferObject { constructor(viewId, groupId, panelId) { super(); this.viewId = viewId; this.groupId = groupId; this.panelId = panelId; } } export class PaneTransfer extends TransferObject { constructor(viewId, paneId) { super(); this.viewId = viewId; this.paneId = paneId; } } export const DATA_KEY = 'splitview/transfer'; export const isPanelTransferEvent = (event) => { if (!event.dataTransfer) { return false; } return event.dataTransfer.types.includes(DATA_KEY); }; export var DragType; (function (DragType) { DragType["DOCKVIEW_TAB"] = "dockview_tab"; DragType["EXTERNAL"] = "external_group_drag"; })(DragType || (DragType = {})); /** * Determine whether this data belong to that of an event that was started by * dragging a tab component */ export const isTabDragEvent = (data) => { return data.type === DragType.DOCKVIEW_TAB; }; /** * Determine whether this data belong to that of an event that was started by * a custom drag-enable component */ export const isCustomDragEvent = (data) => { return data.type === DragType.EXTERNAL; }; export const extractData = (event) => { if (!event.dataTransfer) { return null; } const data = tryParseJSON(event.dataTransfer.getData(DATA_KEY)); if (!data) { console.warn(`[dragEvent] ${DATA_KEY} data is missing`); } if (typeof data.type !== 'string') { console.warn(`[dragEvent] invalid type ${data.type}`); } return data; }; /** * A singleton to store transfer data during drag & drop operations that are only valid within the application. */ export class LocalSelectionTransfer { constructor() { // protect against external instantiation } static getInstance() { return LocalSelectionTransfer.INSTANCE; } hasData(proto) { return proto && proto === this.proto; } clearData(proto) { if (this.hasData(proto)) { this.proto = undefined; this.data = undefined; } } getData(proto) { if (this.hasData(proto)) { return this.data; } return undefined; } setData(data, proto) { if (proto) { this.data = data; this.proto = proto; } } } LocalSelectionTransfer.INSTANCE = new LocalSelectionTransfer(); export function getPanelData() { const panelTransfer = LocalSelectionTransfer.getInstance(); const isPanelEvent = panelTransfer.hasData(PanelTransfer.prototype); if (!isPanelEvent) { return undefined; } return panelTransfer.getData(PanelTransfer.prototype)[0]; } export function getPaneData() { const paneTransfer = LocalSelectionTransfer.getInstance(); const isPanelEvent = paneTransfer.hasData(PaneTransfer.prototype); if (!isPanelEvent) { return undefined; } return paneTransfer.getData(PaneTransfer.prototype)[0]; }