rc-new-window
Advanced tools
Comparing version 0.1.5 to 0.1.6
@@ -1,2 +0,2 @@ | ||
import * as Bowser from 'bowser'; | ||
import Bowser from 'bowser'; | ||
const browser = Bowser.getParser(window.navigator.userAgent); | ||
@@ -3,0 +3,0 @@ export function gerWindowBorder() { |
# History | ||
---- | ||
## 0.1.6 / 2021-02-23 | ||
- fix es module | ||
## 0.1.3 / 2020-12-18 | ||
@@ -5,0 +9,0 @@ |
"use strict"; | ||
var __createBinding = void 0 && (void 0).__createBinding || (Object.create ? function (o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
Object.defineProperty(o, k2, { | ||
enumerable: true, | ||
get: function get() { | ||
return m[k]; | ||
} | ||
}); | ||
} : function (o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
}); | ||
var __setModuleDefault = void 0 && (void 0).__setModuleDefault || (Object.create ? function (o, v) { | ||
Object.defineProperty(o, "default", { | ||
enumerable: true, | ||
value: v | ||
}); | ||
} : function (o, v) { | ||
o["default"] = v; | ||
}); | ||
var __importStar = void 0 && (void 0).__importStar || function (mod) { | ||
if (mod && mod.__esModule) return mod; | ||
var result = {}; | ||
if (mod != null) for (var k in mod) { | ||
if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); | ||
} | ||
__setModuleDefault(result, mod); | ||
return result; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.popupWindowBorder = exports.popupSupported = exports.isSafari = exports.gerWindowBorder = void 0; | ||
var Bowser = __importStar(require("bowser")); | ||
var browser = Bowser.getParser(window.navigator.userAgent); | ||
const bowser_1 = __importDefault(require("bowser")); | ||
const browser = bowser_1.default.getParser(window.navigator.userAgent); | ||
function gerWindowBorder() { | ||
switch (browser.getOSName(true)) { | ||
case 'windows': | ||
{ | ||
var result; | ||
switch (browser.getBrowserName(true)) { | ||
case 'firefox': | ||
result = [68, 8, 8]; | ||
break; | ||
case 'microsoft edge': | ||
result = [62, 8, 8]; | ||
break; | ||
//case 'chrome': | ||
default: | ||
result = [60, 8, 8]; | ||
switch (browser.getOSName(true)) { | ||
case 'windows': { | ||
let result; | ||
switch (browser.getBrowserName(true)) { | ||
case 'firefox': | ||
result = [68, 8, 8]; | ||
break; | ||
case 'microsoft edge': | ||
result = [62, 8, 8]; | ||
break; | ||
//case 'chrome': | ||
default: | ||
result = [60, 8, 8]; | ||
} | ||
if (window.devicePixelRatio > 1) { | ||
result[0] -= 2; | ||
result[1] -= 1; | ||
result[2] -= 1; | ||
} | ||
return result; | ||
} | ||
if (window.devicePixelRatio > 1) { | ||
result[0] -= 2; | ||
result[1] -= 1; | ||
result[2] -= 1; | ||
case 'macos': { | ||
switch (browser.getBrowserName(true)) { | ||
case 'safari': | ||
return [22, 0, 0]; | ||
case 'firefox': | ||
return [59, 0, 0]; | ||
//case 'chrome': | ||
default: | ||
return [51, 0, 0]; | ||
} | ||
} | ||
return result; | ||
} | ||
case 'macos': | ||
{ | ||
switch (browser.getBrowserName(true)) { | ||
case 'safari': | ||
return [22, 0, 0]; | ||
case 'firefox': | ||
return [59, 0, 0]; | ||
//case 'chrome': | ||
default: | ||
return [51, 0, 0]; | ||
} | ||
} | ||
} | ||
return [60, 8, 8]; | ||
} | ||
return [60, 8, 8]; | ||
} | ||
exports.gerWindowBorder = gerWindowBorder; | ||
exports.isSafari = browser.getBrowserName(true) === 'safari'; | ||
exports.popupSupported = browser.getPlatformType() === 'desktop'; | ||
exports.popupWindowBorder = gerWindowBorder(); | ||
exports.popupWindowBorder = gerWindowBorder(); |
426
lib/index.js
"use strict"; | ||
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); | ||
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray")); | ||
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck")); | ||
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass")); | ||
var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits")); | ||
var _createSuper2 = _interopRequireDefault(require("@babel/runtime/helpers/createSuper")); | ||
var __importDefault = void 0 && (void 0).__importDefault || function (mod) { | ||
return mod && mod.__esModule ? mod : { | ||
"default": mod | ||
}; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
var react_1 = __importDefault(require("react")); | ||
var react_dom_1 = __importDefault(require("react-dom")); | ||
var debounce_1 = __importDefault(require("lodash/debounce")); | ||
var BrowserPopupWindow_1 = require("./BrowserPopupWindow"); | ||
var onNewWindowResize = debounce_1.default(function () { | ||
// add/remove element on main document, force it to dispatch resize observer event on the popup window | ||
var div = document.createElement('div'); | ||
document.body.append(div); | ||
div.remove(); // TODO update resize event | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const react_1 = __importDefault(require("react")); | ||
const react_dom_1 = __importDefault(require("react-dom")); | ||
const debounce_1 = __importDefault(require("lodash/debounce")); | ||
const BrowserPopupWindow_1 = require("./BrowserPopupWindow"); | ||
const onNewWindowResize = debounce_1.default(() => { | ||
// add/remove element on main document, force it to dispatch resize observer event on the popup window | ||
let div = document.createElement('div'); | ||
document.body.append(div); | ||
div.remove(); | ||
// TODO update resize event | ||
}, 200); | ||
@@ -43,158 +21,103 @@ /** | ||
*/ | ||
var NewWindow = /*#__PURE__*/function (_react_1$default$Pure) { | ||
(0, _inherits2.default)(NewWindow, _react_1$default$Pure); | ||
var _super = (0, _createSuper2.default)(NewWindow); | ||
/** | ||
* The NewWindow function constructor. | ||
* @param {Object} props | ||
*/ | ||
function NewWindow(props) { | ||
var _this; | ||
(0, _classCallCheck2.default)(this, NewWindow); | ||
_this = _super.call(this, props); | ||
_this.released = false; | ||
_this.container = document.createElement('div'); | ||
_this.state = { | ||
mounted: false | ||
}; | ||
_this.onMainWindowUnload = function () { | ||
if (_this.window) { | ||
_this.window.close(); | ||
} | ||
}; | ||
class NewWindow extends react_1.default.PureComponent { | ||
/** | ||
* Release the new window and anything that was bound to it. | ||
* The NewWindow function constructor. | ||
* @param {Object} props | ||
*/ | ||
_this.release = function (event) { | ||
// This method can be called once. | ||
if (_this.released) { | ||
return; | ||
} | ||
_this.released = true; | ||
window.removeEventListener('beforeunload', _this.onMainWindowUnload); | ||
_this.window.addEventListener('beforeunload', _this.release); | ||
if (event) { | ||
// Call any function bound to the `onUnload` prop. | ||
var onClose = _this.props.onClose; | ||
if (typeof onClose === 'function') { | ||
onClose(); | ||
} | ||
} | ||
}; | ||
return _this; | ||
} | ||
/** | ||
* Render the NewWindow component. | ||
*/ | ||
(0, _createClass2.default)(NewWindow, [{ | ||
key: "render", | ||
value: function render() { | ||
if (!this.state.mounted) return null; | ||
return react_dom_1.default.createPortal(this.props.children, this.container); | ||
constructor(props) { | ||
super(props); | ||
this.released = false; | ||
this.container = document.createElement('div'); | ||
this.state = { mounted: false }; | ||
this.onMainWindowUnload = () => { | ||
if (this.window) { | ||
this.window.close(); | ||
} | ||
}; | ||
/** | ||
* Release the new window and anything that was bound to it. | ||
*/ | ||
this.release = (event) => { | ||
// This method can be called once. | ||
if (this.released) { | ||
return; | ||
} | ||
this.released = true; | ||
window.removeEventListener('beforeunload', this.onMainWindowUnload); | ||
this.window.addEventListener('beforeunload', this.release); | ||
if (event) { | ||
// Call any function bound to the `onUnload` prop. | ||
const { onClose } = this.props; | ||
if (typeof onClose === 'function') { | ||
onClose(); | ||
} | ||
} | ||
}; | ||
} | ||
}, { | ||
key: "componentDidMount", | ||
value: function componentDidMount() { | ||
this.openChild(); | ||
this.setState({ | ||
mounted: true | ||
}); | ||
/** | ||
* Render the NewWindow component. | ||
*/ | ||
render() { | ||
if (!this.state.mounted) | ||
return null; | ||
return react_dom_1.default.createPortal(this.props.children, this.container); | ||
} | ||
componentDidMount() { | ||
this.openChild(); | ||
this.setState({ mounted: true }); | ||
} | ||
/** | ||
* Create the new window when NewWindow component mount. | ||
*/ | ||
}, { | ||
key: "openChild", | ||
value: function openChild() { | ||
var _this2 = this; | ||
var _this$props = this.props, | ||
url = _this$props.url, | ||
title = _this$props.title, | ||
name = _this$props.name, | ||
width = _this$props.width, | ||
height = _this$props.height, | ||
initPopupInnerRect = _this$props.initPopupInnerRect, | ||
initPopupOuterRect = _this$props.initPopupOuterRect, | ||
onBlock = _this$props.onBlock, | ||
onOpen = _this$props.onOpen; | ||
var features = { | ||
width: width, | ||
height: height | ||
}; | ||
if (initPopupOuterRect) { | ||
features = initPopupOuterRect(); | ||
var _BrowserPopupWindow_ = (0, _slicedToArray2.default)(BrowserPopupWindow_1.popupWindowBorder, 3), | ||
topBorder = _BrowserPopupWindow_[0], | ||
sideBorder = _BrowserPopupWindow_[1], | ||
bottomBorder = _BrowserPopupWindow_[2]; | ||
if (!BrowserPopupWindow_1.isSafari) { | ||
features.width -= sideBorder * 2; | ||
features.height -= topBorder + bottomBorder; | ||
openChild() { | ||
const { url, title, name, width, height, initPopupInnerRect, initPopupOuterRect, onBlock, onOpen, } = this.props; | ||
let features = { width, height }; | ||
if (initPopupOuterRect) { | ||
features = initPopupOuterRect(); | ||
const [topBorder, sideBorder, bottomBorder] = BrowserPopupWindow_1.popupWindowBorder; | ||
if (!BrowserPopupWindow_1.isSafari) { | ||
features.width -= sideBorder * 2; | ||
features.height -= topBorder + bottomBorder; | ||
} | ||
} | ||
} else if (initPopupInnerRect) { | ||
features = initPopupInnerRect(); | ||
var _BrowserPopupWindow_2 = (0, _slicedToArray2.default)(BrowserPopupWindow_1.popupWindowBorder, 2), | ||
_topBorder = _BrowserPopupWindow_2[0], | ||
_sideBorder = _BrowserPopupWindow_2[1]; | ||
features.left -= _sideBorder; | ||
features.top -= _topBorder; | ||
if (BrowserPopupWindow_1.isSafari) { | ||
features.height += _topBorder; | ||
else if (initPopupInnerRect) { | ||
features = initPopupInnerRect(); | ||
const [topBorder, sideBorder] = BrowserPopupWindow_1.popupWindowBorder; | ||
features.left -= sideBorder; | ||
features.top -= topBorder; | ||
if (BrowserPopupWindow_1.isSafari) { | ||
features.height += topBorder; | ||
} | ||
} | ||
} else { | ||
features.left = window.top.outerWidth / 2 + window.top.screenX - width / 2; | ||
features.top = window.top.outerHeight / 2 + window.top.screenY - height / 2; | ||
} // Open a new window. | ||
this.window = window.open(url, name, toWindowFeatures(features)); // Check if the new window was successfully opened. | ||
if (this.window) { | ||
window.addEventListener('beforeunload', this.onMainWindowUnload); | ||
this.window.addEventListener('resize', onNewWindowResize); | ||
this.window.document.title = title || document.title; | ||
this.window.document.body.appendChild(this.container); // If specified, copy styles from parent window's document. | ||
if (this.props.copyStyles) { | ||
setTimeout(function () { | ||
return copyStyles(document, _this2.window.document); | ||
}, 0); | ||
else { | ||
features.left = window.top.outerWidth / 2 + window.top.screenX - width / 2; | ||
features.top = window.top.outerHeight / 2 + window.top.screenY - height / 2; | ||
} | ||
if (typeof onOpen === 'function') { | ||
onOpen(this.window); | ||
} // Release anything bound to this component before the new window unload. | ||
this.window.addEventListener('beforeunload', this.release); | ||
} else { | ||
// Handle error on opening of new window. | ||
if (typeof onBlock === 'function') { | ||
onBlock(); | ||
} else { | ||
console.warn('A new window could not be opened. Maybe it was blocked.'); | ||
// Open a new window. | ||
this.window = window.open(url, name, toWindowFeatures(features)); | ||
// Check if the new window was successfully opened. | ||
if (this.window) { | ||
window.addEventListener('beforeunload', this.onMainWindowUnload); | ||
this.window.addEventListener('resize', onNewWindowResize); | ||
this.window.document.title = title || document.title; | ||
this.window.document.body.appendChild(this.container); | ||
// If specified, copy styles from parent window's document. | ||
if (this.props.copyStyles) { | ||
setTimeout(() => copyStyles(document, this.window.document), 0); | ||
} | ||
if (typeof onOpen === 'function') { | ||
onOpen(this.window); | ||
} | ||
// Release anything bound to this component before the new window unload. | ||
this.window.addEventListener('beforeunload', this.release); | ||
} | ||
} | ||
else { | ||
// Handle error on opening of new window. | ||
if (typeof onBlock === 'function') { | ||
onBlock(); | ||
} | ||
else { | ||
console.warn('A new window could not be opened. Maybe it was blocked.'); | ||
} | ||
} | ||
} | ||
@@ -204,15 +127,9 @@ /** | ||
*/ | ||
}, { | ||
key: "componentWillUnmount", | ||
value: function componentWillUnmount() { | ||
if (this.window) { | ||
this.release(); | ||
this.window.close(); | ||
} | ||
componentWillUnmount() { | ||
if (this.window) { | ||
this.release(); | ||
this.window.close(); | ||
} | ||
} | ||
}]); | ||
return NewWindow; | ||
}(react_1.default.PureComponent); | ||
} | ||
NewWindow.supported = BrowserPopupWindow_1.popupSupported; | ||
@@ -222,9 +139,8 @@ /** | ||
*/ | ||
NewWindow.defaultProps = { | ||
url: '', | ||
name: '', | ||
width: 640, | ||
height: 480, | ||
copyStyles: true | ||
url: '', | ||
name: '', | ||
width: 640, | ||
height: 480, | ||
copyStyles: true, | ||
}; | ||
@@ -235,3 +151,2 @@ /** | ||
*/ | ||
/** | ||
@@ -243,45 +158,45 @@ * Copy styles from a source document to a target. | ||
*/ | ||
function copyStyles(source, target) { | ||
Array.from(source.styleSheets).forEach(function (styleSheet) { | ||
// For <style> elements | ||
var rules; | ||
if (styleSheet.href) { | ||
// for <link> elements loading CSS from a URL | ||
var newLinkEl = source.createElement('link'); | ||
newLinkEl.rel = 'stylesheet'; | ||
newLinkEl.href = styleSheet.href; | ||
target.head.appendChild(newLinkEl); | ||
} else { | ||
try { | ||
rules = styleSheet.cssRules; | ||
} catch (err) {// can't access crossdomain rules | ||
} | ||
if (rules) { | ||
var newStyleEl = source.createElement('style'); // Write the text of each rule into the body of the style element | ||
Array.from(styleSheet.cssRules).forEach(function (cssRule) { | ||
var cssText = cssRule.cssText, | ||
type = cssRule.type; | ||
var returnText = cssText; // Check if the cssRule type is CSSImportRule (3) or CSSFontFaceRule (5) to handle local imports on a about:blank page | ||
// '/custom.css' turns to 'http://my-site.com/custom.css' | ||
if ([3, 5].includes(type)) { | ||
returnText = cssText.split('url(').map(function (line) { | ||
if (line[1] === '/') { | ||
return "".concat(line.slice(0, 1)).concat(window.location.origin).concat(line.slice(1)); | ||
} | ||
return line; | ||
}).join('url('); | ||
} | ||
newStyleEl.appendChild(source.createTextNode(returnText)); | ||
}); | ||
target.head.appendChild(newStyleEl); | ||
} | ||
} | ||
}); | ||
Array.from(source.styleSheets).forEach(styleSheet => { | ||
// For <style> elements | ||
let rules; | ||
if (styleSheet.href) { | ||
// for <link> elements loading CSS from a URL | ||
const newLinkEl = source.createElement('link'); | ||
newLinkEl.rel = 'stylesheet'; | ||
newLinkEl.href = styleSheet.href; | ||
target.head.appendChild(newLinkEl); | ||
} | ||
else { | ||
try { | ||
rules = styleSheet.cssRules; | ||
} | ||
catch (err) { | ||
// can't access crossdomain rules | ||
} | ||
if (rules) { | ||
const newStyleEl = source.createElement('style'); | ||
// Write the text of each rule into the body of the style element | ||
Array.from(styleSheet.cssRules).forEach(cssRule => { | ||
const { cssText, type } = cssRule; | ||
let returnText = cssText; | ||
// Check if the cssRule type is CSSImportRule (3) or CSSFontFaceRule (5) to handle local imports on a about:blank page | ||
// '/custom.css' turns to 'http://my-site.com/custom.css' | ||
if ([3, 5].includes(type)) { | ||
returnText = cssText | ||
.split('url(') | ||
.map(line => { | ||
if (line[1] === '/') { | ||
return `${line.slice(0, 1)}${window.location.origin}${line.slice(1)}`; | ||
} | ||
return line; | ||
}) | ||
.join('url('); | ||
} | ||
newStyleEl.appendChild(source.createTextNode(returnText)); | ||
}); | ||
target.head.appendChild(newStyleEl); | ||
} | ||
} | ||
}); | ||
} | ||
@@ -294,16 +209,15 @@ /** | ||
*/ | ||
function toWindowFeatures(obj) { | ||
return Object.keys(obj).reduce(function (features, name) { | ||
var value = obj[name]; | ||
if (typeof value === 'boolean') { | ||
features.push("".concat(name, "=").concat(value ? 'yes' : 'no')); | ||
} else { | ||
features.push("".concat(name, "=").concat(value)); | ||
} | ||
return features; | ||
}, []).join(','); | ||
return Object.keys(obj) | ||
.reduce((features, name) => { | ||
const value = obj[name]; | ||
if (typeof value === 'boolean') { | ||
features.push(`${name}=${value ? 'yes' : 'no'}`); | ||
} | ||
else { | ||
features.push(`${name}=${value}`); | ||
} | ||
return features; | ||
}, []) | ||
.join(','); | ||
} | ||
@@ -314,4 +228,2 @@ /** | ||
*/ | ||
exports.default = NewWindow; | ||
exports.default = NewWindow; |
"use strict"; | ||
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); | ||
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck")); | ||
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass")); | ||
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray")); | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.mapWindowToElement = exports.mapElementToScreenRect = exports.estimateWindowBorder = exports.estimateBrowserZoom = void 0; | ||
var BrowserPopupWindow_1 = require("./BrowserPopupWindow"); | ||
const BrowserPopupWindow_1 = require("./BrowserPopupWindow"); | ||
function estimateBrowserZoom(_window) { | ||
// one of them might be off by a lot due to developer console or other browser plugin | ||
var _BrowserPopupWindow_ = BrowserPopupWindow_1.gerWindowBorder(), | ||
_BrowserPopupWindow_2 = (0, _slicedToArray2.default)(_BrowserPopupWindow_, 3), | ||
topBorder = _BrowserPopupWindow_2[0], | ||
sideBorder = _BrowserPopupWindow_2[1], | ||
bottomBorder = _BrowserPopupWindow_2[2]; | ||
if (_window.outerWidth === _window.screen.availWidth) { | ||
sideBorder = 0; | ||
bottomBorder = 0; | ||
} | ||
var xRatio = (_window.outerWidth - sideBorder * 2) / _window.innerWidth; | ||
var yRatio = (_window.outerHeight - topBorder - bottomBorder) / _window.innerHeight; | ||
var zoomRatio = Math.min(yRatio, xRatio); | ||
if (zoomRatio > 1.8) { | ||
zoomRatio = Math.round(zoomRatio); | ||
} else if (zoomRatio > 0.73) { | ||
zoomRatio = Math.round(zoomRatio * 20) / 20; | ||
} else { | ||
zoomRatio = 2 / Math.round(2 / zoomRatio); | ||
} | ||
console.log("zoom ".concat(zoomRatio)); | ||
return zoomRatio; | ||
// one of them might be off by a lot due to developer console or other browser plugin | ||
let [topBorder, sideBorder, bottomBorder] = BrowserPopupWindow_1.gerWindowBorder(); | ||
if (_window.outerWidth === _window.screen.availWidth) { | ||
sideBorder = 0; | ||
bottomBorder = 0; | ||
} | ||
let xRatio = (_window.outerWidth - sideBorder * 2) / _window.innerWidth; | ||
let yRatio = (_window.outerHeight - topBorder - bottomBorder) / _window.innerHeight; | ||
let zoomRatio = Math.min(yRatio, xRatio); | ||
if (zoomRatio > 1.8) { | ||
zoomRatio = Math.round(zoomRatio); | ||
} | ||
else if (zoomRatio > 0.73) { | ||
zoomRatio = Math.round(zoomRatio * 20) / 20; | ||
} | ||
else { | ||
zoomRatio = 2 / Math.round(2 / zoomRatio); | ||
} | ||
console.log(`zoom ${zoomRatio}`); | ||
return zoomRatio; | ||
} | ||
exports.estimateBrowserZoom = estimateBrowserZoom; | ||
function estimateWindowBorder(_window) { | ||
var addBorder = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false; | ||
var zoom = _window ? estimateBrowserZoom(_window) : 1; | ||
var xBorder = _window.outerWidth - _window.innerWidth * zoom >> 1; | ||
var yBorder = Math.round(_window.outerHeight - _window.innerHeight * zoom); | ||
if (xBorder > 32) { | ||
// probably because of debugger console, assume it's in the right side | ||
xBorder = 8; | ||
} else { | ||
yBorder -= xBorder; | ||
} | ||
return [xBorder, yBorder, zoom]; | ||
function estimateWindowBorder(_window, addBorder = false) { | ||
let zoom = _window ? estimateBrowserZoom(_window) : 1; | ||
let xBorder = (_window.outerWidth - _window.innerWidth * zoom) >> 1; | ||
let yBorder = Math.round(_window.outerHeight - _window.innerHeight * zoom); | ||
if (xBorder > 32) { | ||
// probably because of debugger console, assume it's in the right side | ||
xBorder = 8; | ||
} | ||
else { | ||
yBorder -= xBorder; | ||
} | ||
return [xBorder, yBorder, zoom]; | ||
} | ||
exports.estimateWindowBorder = estimateWindowBorder; | ||
var MapRect2D = /*#__PURE__*/function () { | ||
function MapRect2D() { | ||
(0, _classCallCheck2.default)(this, MapRect2D); | ||
} | ||
(0, _createClass2.default)(MapRect2D, [{ | ||
key: "init", | ||
value: function init(x1, y1, w1, h1, x2, y2, w2, h2) { | ||
this.scaleX = w2 / w1; | ||
this.scaleY = h2 / h1; | ||
this.offsetX = x2 - x1 * this.scaleX; | ||
this.offsetY = y2 - y1 * this.scaleY; | ||
class MapRect2D { | ||
init(x1, y1, w1, h1, x2, y2, w2, h2) { | ||
this.scaleX = w2 / w1; | ||
this.scaleY = h2 / h1; | ||
this.offsetX = x2 - x1 * this.scaleX; | ||
this.offsetY = y2 - y1 * this.scaleY; | ||
} | ||
}, { | ||
key: "map", | ||
value: function map(pt) { | ||
return { | ||
x: pt.x * this.scaleX + this.offsetX, | ||
y: pt.y * this.scaleY + this.offsetY | ||
}; | ||
map(pt) { | ||
return { x: pt.x * this.scaleX + this.offsetX, y: pt.y * this.scaleY + this.offsetY }; | ||
} | ||
}, { | ||
key: "revertMap", | ||
value: function revertMap(pt) { | ||
return { | ||
x: (pt.x - this.offsetX) / this.scaleX, | ||
y: (pt.y - this.offsetY) / this.scaleY | ||
}; | ||
revertMap(pt) { | ||
return { x: (pt.x - this.offsetX) / this.scaleX, y: (pt.y - this.offsetY) / this.scaleY }; | ||
} | ||
}]); | ||
return MapRect2D; | ||
}(); | ||
} | ||
function mapElementToScreenRect(element, rect) { | ||
if (!element) { | ||
return null; | ||
} | ||
var clientRect = element.getBoundingClientRect(); | ||
var mapRect = new MapRect2D(); | ||
mapRect.init(0, 0, element.offsetWidth, element.offsetHeight, clientRect.x, clientRect.y, clientRect.width, clientRect.height); | ||
var mappedRect; | ||
if (rect) { | ||
var _mapRect$map = mapRect.map({ | ||
x: rect.left, | ||
y: rect.top | ||
}), | ||
x = _mapRect$map.x, | ||
y = _mapRect$map.y; | ||
var _mapRect$map2 = mapRect.map({ | ||
x: rect.left + rect.width, | ||
y: rect.top + rect.height | ||
}), | ||
x2 = _mapRect$map2.x, | ||
y2 = _mapRect$map2.y; | ||
mappedRect = { | ||
left: x, | ||
top: y, | ||
width: x2 - x, | ||
height: y2 - y | ||
}; | ||
} else { | ||
mappedRect = { | ||
left: clientRect.left, | ||
top: clientRect.top, | ||
width: clientRect.width, | ||
height: clientRect.height | ||
}; | ||
} | ||
var _document = element.ownerDocument; | ||
var _window = _document.defaultView; | ||
if (!_window) { | ||
return clientRect; | ||
} // recursively get rect if it's an iframe | ||
if (_window.frameElement) { | ||
return mapElementToScreenRect(_window.frameElement, mappedRect); | ||
} | ||
var _estimateWindowBorder = estimateWindowBorder(_window), | ||
_estimateWindowBorder2 = (0, _slicedToArray2.default)(_estimateWindowBorder, 3), | ||
xBorder = _estimateWindowBorder2[0], | ||
yBorder = _estimateWindowBorder2[1], | ||
zoom = _estimateWindowBorder2[2]; | ||
if (zoom !== 1) { | ||
mappedRect.left *= zoom; | ||
mappedRect.top *= zoom; | ||
mappedRect.width *= zoom; | ||
mappedRect.height *= zoom; | ||
} | ||
mappedRect.left += _window.screenX + xBorder; | ||
mappedRect.top += _window.screenY + yBorder; | ||
return mappedRect; | ||
if (!element) { | ||
return null; | ||
} | ||
let clientRect = element.getBoundingClientRect(); | ||
let mapRect = new MapRect2D(); | ||
mapRect.init(0, 0, element.offsetWidth, element.offsetHeight, clientRect.x, clientRect.y, clientRect.width, clientRect.height); | ||
let mappedRect; | ||
if (rect) { | ||
let { x, y } = mapRect.map({ x: rect.left, y: rect.top }); | ||
let { x: x2, y: y2 } = mapRect.map({ x: rect.left + rect.width, y: rect.top + rect.height }); | ||
mappedRect = { left: x, top: y, width: x2 - x, height: y2 - y }; | ||
} | ||
else { | ||
mappedRect = { | ||
left: clientRect.left, | ||
top: clientRect.top, | ||
width: clientRect.width, | ||
height: clientRect.height, | ||
}; | ||
} | ||
let _document = element.ownerDocument; | ||
let _window = _document.defaultView; | ||
if (!_window) { | ||
return clientRect; | ||
} | ||
// recursively get rect if it's an iframe | ||
if (_window.frameElement) { | ||
return mapElementToScreenRect(_window.frameElement, mappedRect); | ||
} | ||
let [xBorder, yBorder, zoom] = estimateWindowBorder(_window); | ||
if (zoom !== 1) { | ||
mappedRect.left *= zoom; | ||
mappedRect.top *= zoom; | ||
mappedRect.width *= zoom; | ||
mappedRect.height *= zoom; | ||
} | ||
mappedRect.left += _window.screenX + xBorder; | ||
mappedRect.top += _window.screenY + yBorder; | ||
return mappedRect; | ||
} | ||
exports.mapElementToScreenRect = mapElementToScreenRect; | ||
function mapWindowToElement(targetElement, fromWindow, fromRect) { | ||
var removeBorder = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : true; | ||
if (!targetElement) { | ||
return null; | ||
} | ||
if (fromWindow) { | ||
fromRect = { | ||
left: fromWindow.screenX, | ||
top: fromWindow.screenY, | ||
width: fromWindow.outerWidth, | ||
height: fromWindow.outerHeight | ||
}; | ||
if (removeBorder) { | ||
var _BrowserPopupWindow_3 = (0, _slicedToArray2.default)(BrowserPopupWindow_1.popupWindowBorder, 3), | ||
topBorder = _BrowserPopupWindow_3[0], | ||
sideBorder = _BrowserPopupWindow_3[1], | ||
bottomBorder = _BrowserPopupWindow_3[2]; | ||
fromRect.left += sideBorder; | ||
fromRect.top += topBorder; | ||
fromRect.width -= sideBorder * 2; | ||
fromRect.height -= topBorder + bottomBorder; | ||
function mapWindowToElement(targetElement, fromWindow, fromRect, removeBorder = true) { | ||
if (!targetElement) { | ||
return null; | ||
} | ||
} else if (!fromRect) { | ||
return null; | ||
} | ||
var _document = targetElement.ownerDocument; | ||
var _window = _document.defaultView; | ||
if (!_window) { | ||
return fromRect; | ||
} // recursively get rect if it's an iframe | ||
if (_window.frameElement) { | ||
fromRect = mapWindowToElement(_window.frameElement, null, fromRect); | ||
} else { | ||
var _estimateWindowBorder3 = estimateWindowBorder(_window), | ||
_estimateWindowBorder4 = (0, _slicedToArray2.default)(_estimateWindowBorder3, 3), | ||
xBorder = _estimateWindowBorder4[0], | ||
yBorder = _estimateWindowBorder4[1], | ||
zoom = _estimateWindowBorder4[2]; | ||
fromRect.left -= _window.screenX + xBorder; | ||
fromRect.top -= _window.screenY + yBorder; | ||
if (zoom !== 1) { | ||
fromRect.left /= zoom; | ||
fromRect.top /= zoom; | ||
fromRect.width /= zoom; | ||
fromRect.height /= zoom; | ||
if (fromWindow) { | ||
fromRect = { | ||
left: fromWindow.screenX, | ||
top: fromWindow.screenY, | ||
width: fromWindow.outerWidth, | ||
height: fromWindow.outerHeight, | ||
}; | ||
if (removeBorder) { | ||
const [topBorder, sideBorder, bottomBorder] = BrowserPopupWindow_1.popupWindowBorder; | ||
fromRect.left += sideBorder; | ||
fromRect.top += topBorder; | ||
fromRect.width -= sideBorder * 2; | ||
fromRect.height -= topBorder + bottomBorder; | ||
} | ||
} | ||
} | ||
var clientRect = targetElement.getBoundingClientRect(); | ||
var mapRect = new MapRect2D(); | ||
mapRect.init(0, 0, targetElement.offsetWidth, targetElement.offsetHeight, clientRect.x, clientRect.y, clientRect.width, clientRect.height); | ||
var mappedRect; | ||
var _mapRect$revertMap = mapRect.revertMap({ | ||
x: fromRect.left, | ||
y: fromRect.top | ||
}), | ||
x = _mapRect$revertMap.x, | ||
y = _mapRect$revertMap.y; | ||
var _mapRect$revertMap2 = mapRect.revertMap({ | ||
x: fromRect.left + fromRect.width, | ||
y: fromRect.top + fromRect.height | ||
}), | ||
x2 = _mapRect$revertMap2.x, | ||
y2 = _mapRect$revertMap2.y; | ||
mappedRect = { | ||
left: x, | ||
top: y, | ||
width: x2 - x, | ||
height: y2 - y | ||
}; | ||
return mappedRect; | ||
else if (!fromRect) { | ||
return null; | ||
} | ||
let _document = targetElement.ownerDocument; | ||
let _window = _document.defaultView; | ||
if (!_window) { | ||
return fromRect; | ||
} | ||
// recursively get rect if it's an iframe | ||
if (_window.frameElement) { | ||
fromRect = mapWindowToElement(_window.frameElement, null, fromRect); | ||
} | ||
else { | ||
let [xBorder, yBorder, zoom] = estimateWindowBorder(_window); | ||
fromRect.left -= _window.screenX + xBorder; | ||
fromRect.top -= _window.screenY + yBorder; | ||
if (zoom !== 1) { | ||
fromRect.left /= zoom; | ||
fromRect.top /= zoom; | ||
fromRect.width /= zoom; | ||
fromRect.height /= zoom; | ||
} | ||
} | ||
let clientRect = targetElement.getBoundingClientRect(); | ||
let mapRect = new MapRect2D(); | ||
mapRect.init(0, 0, targetElement.offsetWidth, targetElement.offsetHeight, clientRect.x, clientRect.y, clientRect.width, clientRect.height); | ||
let mappedRect; | ||
let { x, y } = mapRect.revertMap({ x: fromRect.left, y: fromRect.top }); | ||
let { x: x2, y: y2 } = mapRect.revertMap({ | ||
x: fromRect.left + fromRect.width, | ||
y: fromRect.top + fromRect.height, | ||
}); | ||
mappedRect = { left: x, top: y, width: x2 - x, height: y2 - y }; | ||
return mappedRect; | ||
} | ||
exports.mapWindowToElement = mapWindowToElement; | ||
exports.mapWindowToElement = mapWindowToElement; |
{ | ||
"name": "rc-new-window", | ||
"version": "0.1.5", | ||
"version": "0.1.6", | ||
"description": "popup new browser window with react", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
39044
989