UNPKG

atom-nuclide

Version:

A unified developer experience for web and mobile development, built as a suite of features on top of Atom to provide hackability and the support of an active community.

265 lines (233 loc) 9.14 kB
Object.defineProperty(exports, '__esModule', { value: true }); /* * Copyright (c) 2015-present, Facebook, Inc. * All rights reserved. * * This source code is licensed under the license found in the LICENSE file in * the root directory of this source tree. */ var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ('value' in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })(); function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } } function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; } var _atom2; function _atom() { return _atom2 = require('atom'); } var _reactForAtom2; function _reactForAtom() { return _reactForAtom2 = require('react-for-atom'); } var _rxjsBundlesRxUmdMinJs2; function _rxjsBundlesRxUmdMinJs() { return _rxjsBundlesRxUmdMinJs2 = _interopRequireDefault(require('rxjs/bundles/Rx.umd.min.js')); } var _assert2; function _assert() { return _assert2 = _interopRequireDefault(require('assert')); } var _DatatipComponent2; function _DatatipComponent() { return _DatatipComponent2 = require('./DatatipComponent'); } var LINE_END_MARGIN = 20; var _mouseMove$ = undefined; function documentMouseMove$() { if (_mouseMove$ == null) { _mouseMove$ = (_rxjsBundlesRxUmdMinJs2 || _rxjsBundlesRxUmdMinJs()).default.Observable.fromEvent(document, 'mousemove'); } return _mouseMove$; } var _mouseUp$ = undefined; function documentMouseUp$() { if (_mouseUp$ == null) { _mouseUp$ = (_rxjsBundlesRxUmdMinJs2 || _rxjsBundlesRxUmdMinJs()).default.Observable.fromEvent(document, 'mouseup'); } return _mouseUp$; } var PinnedDatatip = (function () { function PinnedDatatip(datatip, editor, onDispose) { var _this = this; _classCallCheck(this, PinnedDatatip); var component = datatip.component; var range = datatip.range; this._subscriptions = new (_atom2 || _atom()).CompositeDisposable(); this._subscriptions.add(new (_atom2 || _atom()).Disposable(function () { return onDispose(_this); })); this._range = range; this._component = component; this._editor = editor; this._marker = null; this._rangeDecoration = null; this._hostElement = document.createElement('div'); this._hostElement.className = 'nuclide-datatip-overlay'; this._boundDispose = this.dispose.bind(this); this._boundHandleMouseDown = this.handleMouseDown.bind(this); this._boundHandleMouseEnter = this.handleMouseEnter.bind(this); this._boundHandleMouseLeave = this.handleMouseLeave.bind(this); this._boundHandleCapturedClick = this.handleCapturedClick.bind(this); this._hostElement.addEventListener('mouseenter', this._boundHandleMouseEnter); this._hostElement.addEventListener('mouseleave', this._boundHandleMouseLeave); this._subscriptions.add(new (_atom2 || _atom()).Disposable(function () { _this._hostElement.removeEventListener('mouseenter', _this._boundHandleMouseEnter); _this._hostElement.removeEventListener('mouseleave', _this._boundHandleMouseLeave); })); this._mouseUpTimeout = null; this._offset = { x: 0, y: 0 }; this._isDragging = false; this._dragOrigin = null; this._isHovering = false; this.render(); } _createClass(PinnedDatatip, [{ key: 'handleMouseEnter', value: function handleMouseEnter(event) { this._isHovering = true; this.render(); } }, { key: 'handleMouseLeave', value: function handleMouseLeave(event) { this._isHovering = false; this.render(); } }, { key: 'handleGlobalMouseMove', value: function handleGlobalMouseMove(event) { var evt = event; var _dragOrigin = this._dragOrigin; (0, (_assert2 || _assert()).default)(_dragOrigin); this._isDragging = true; this._offset = { x: evt.clientX - _dragOrigin.x, y: evt.clientY - _dragOrigin.y }; this.render(); } }, { key: 'handleGlobalMouseUp', value: function handleGlobalMouseUp() { var _this2 = this; // If the datatip was moved, push the effects of mouseUp to the next tick, // in order to allow cancelation of captured events (e.g. clicks on child components). this._mouseUpTimeout = setTimeout(function () { _this2._isDragging = false; _this2._dragOrigin = null; _this2._mouseUpTimeout = null; _this2._ensureMouseSubscriptionDisposed(); _this2.render(); }, 0); } }, { key: '_ensureMouseSubscriptionDisposed', value: function _ensureMouseSubscriptionDisposed() { if (this._mouseSubscription != null) { this._mouseSubscription.unsubscribe(); this._mouseSubscription = null; } } }, { key: 'handleMouseDown', value: function handleMouseDown(event) { var _this3 = this; var evt = event; this._dragOrigin = { x: evt.clientX - this._offset.x, y: evt.clientY - this._offset.y }; this._ensureMouseSubscriptionDisposed(); this._mouseSubscription = documentMouseMove$().takeUntil(documentMouseUp$()).subscribe(function (e) { _this3.handleGlobalMouseMove(e); }, function (error) {}, function () { _this3.handleGlobalMouseUp(); }); } }, { key: 'handleCapturedClick', value: function handleCapturedClick(event) { if (this._isDragging) { event.stopPropagation(); } } // Ensure positioning of the Datatip at the end of the current line. }, { key: '_updateHostElementPosition', value: function _updateHostElementPosition() { var _editor = this._editor; var _range = this._range; var _hostElement = this._hostElement; var _offset = this._offset; var charWidth = _editor.getDefaultCharWidth(); var lineLength = _editor.getBuffer().getLines()[_range.start.row].length; _hostElement.style.display = 'block'; _hostElement.style.top = -_editor.getLineHeightInPixels() + _offset.y + 'px'; _hostElement.style.left = (lineLength - _range.end.column) * charWidth + LINE_END_MARGIN + _offset.x + 'px'; } }, { key: 'render', value: function render() { var _editor = this._editor; var _range = this._range; var ProvidedComponent = this._component; var _hostElement = this._hostElement; var _isDragging = this._isDragging; var _isHovering = this._isHovering; this._updateHostElementPosition(); (_reactForAtom2 || _reactForAtom()).ReactDOM.render((_reactForAtom2 || _reactForAtom()).React.createElement( (_DatatipComponent2 || _DatatipComponent()).DatatipComponent, { action: (_DatatipComponent2 || _DatatipComponent()).DATATIP_ACTIONS.CLOSE, actionTitle: 'Close this datatip', className: _isDragging ? 'nuclide-datatip-dragging' : '', onActionClick: this._boundDispose, onMouseDown: this._boundHandleMouseDown, onClickCapture: this._boundHandleCapturedClick }, (_reactForAtom2 || _reactForAtom()).React.createElement(ProvidedComponent, null) ), _hostElement); var rangeClassname = 'nuclide-datatip-highlight-region'; if (_isHovering) { rangeClassname += ' nuclide-datatip-highlight-region-active'; } if (this._marker == null) { var marker = _editor.markBufferRange(_range, { invalidate: 'never' }); this._marker = marker; _editor.decorateMarker(marker, { type: 'overlay', position: 'head', item: this._hostElement }); this._rangeDecoration = _editor.decorateMarker(marker, { type: 'highlight', 'class': rangeClassname }); } else { // `this._rangeDecoration` is guaranteed to exist iff `this._marker` exists. (0, (_assert2 || _assert()).default)(this._rangeDecoration); this._rangeDecoration.setProperties({ type: 'highlight', 'class': rangeClassname }); } } }, { key: 'dispose', value: function dispose() { if (this._mouseUpTimeout != null) { clearTimeout(this._mouseUpTimeout); } if (this._marker != null) { this._marker.destroy(); } if (this._mouseSubscription != null) { this._mouseSubscription.unsubscribe(); } (_reactForAtom2 || _reactForAtom()).ReactDOM.unmountComponentAtNode(this._hostElement); this._hostElement.remove(); this._subscriptions.dispose(); } }]); return PinnedDatatip; })(); exports.PinnedDatatip = PinnedDatatip;