UNPKG

8.03 kBJavaScriptView Raw
1"use strict";
2// *****************************************************************************
3// Copyright (C) 2017 TypeFox and others.
4//
5// This program and the accompanying materials are made available under the
6// terms of the Eclipse Public License v. 2.0 which is available at
7// http://www.eclipse.org/legal/epl-2.0.
8//
9// This Source Code may also be made available under the following Secondary
10// Licenses when the conditions for such availability set forth in the Eclipse
11// Public License v. 2.0 are satisfied: GNU General Public License, version 2
12// with the GNU Classpath Exception which is available at
13// https://www.gnu.org/software/classpath/license.html.
14//
15// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
16// *****************************************************************************
17/*---------------------------------------------------------------------------------------------
18 * Copyright (c) Microsoft Corporation. All rights reserved.
19 * Licensed under the MIT License. See License.txt in the project root for license information.
20 *--------------------------------------------------------------------------------------------*/
21Object.defineProperty(exports, "__esModule", { value: true });
22exports.measureTextHeight = exports.measureTextWidth = exports.preventNavigation = exports.parseCssTime = exports.parseCssMagnitude = exports.animationFrame = exports.isBasicWasmSupported = exports.isNative = exports.isIPad = exports.isSafari = exports.isChrome = exports.isWebKit = exports.isFirefox = exports.isOpera = exports.isEdgeOrIE = exports.isEdge = exports.isIE = void 0;
23const userAgent = typeof navigator !== 'undefined' ? navigator.userAgent : '';
24exports.isIE = (userAgent.indexOf('Trident') >= 0);
25exports.isEdge = (userAgent.indexOf('Edge/') >= 0);
26exports.isEdgeOrIE = exports.isIE || exports.isEdge;
27exports.isOpera = (userAgent.indexOf('Opera') >= 0);
28exports.isFirefox = (userAgent.indexOf('Firefox') >= 0);
29exports.isWebKit = (userAgent.indexOf('AppleWebKit') >= 0);
30exports.isChrome = (userAgent.indexOf('Chrome') >= 0);
31exports.isSafari = (userAgent.indexOf('Chrome') === -1) && (userAgent.indexOf('Safari') >= 0);
32exports.isIPad = (userAgent.indexOf('iPad') >= 0);
33// eslint-disable-next-line @typescript-eslint/no-explicit-any
34exports.isNative = typeof window.process !== 'undefined';
35// eslint-disable-next-line @typescript-eslint/no-explicit-any
36exports.isBasicWasmSupported = typeof window.WebAssembly !== 'undefined';
37/**
38 * Resolves after the next animation frame if no parameter is given,
39 * or after the given number of animation frames.
40 */
41function animationFrame(n = 1) {
42 return new Promise(resolve => {
43 function frameFunc() {
44 if (n <= 0) {
45 resolve();
46 }
47 else {
48 n--;
49 requestAnimationFrame(frameFunc);
50 }
51 }
52 frameFunc();
53 });
54}
55exports.animationFrame = animationFrame;
56function parseCssMagnitude(value, defaultValue) {
57 if (value) {
58 let parsed;
59 if (value.endsWith('px')) {
60 parsed = parseFloat(value.substring(0, value.length - 2));
61 }
62 else {
63 parsed = parseFloat(value);
64 }
65 if (!isNaN(parsed)) {
66 return parsed;
67 }
68 }
69 return defaultValue;
70}
71exports.parseCssMagnitude = parseCssMagnitude;
72function parseCssTime(time, defaultValue) {
73 if (time) {
74 let parsed;
75 if (time.endsWith('ms')) {
76 parsed = parseFloat(time.substring(0, time.length - 2));
77 }
78 else if (time.endsWith('s')) {
79 parsed = parseFloat(time.substring(0, time.length - 1)) * 1000;
80 }
81 else {
82 parsed = parseFloat(time);
83 }
84 if (!isNaN(parsed)) {
85 return parsed;
86 }
87 }
88 return defaultValue;
89}
90exports.parseCssTime = parseCssTime;
91function getMonacoEditorScroll(elem) {
92 const linesContent = elem.querySelector('.lines-content');
93 const viewLines = elem.querySelector('.view-lines');
94 // eslint-disable-next-line no-null/no-null
95 if (linesContent === null || viewLines === null) {
96 return undefined;
97 }
98 const linesContentStyle = linesContent.style;
99 const elemStyle = elem.style;
100 const viewLinesStyle = viewLines.style;
101 return {
102 left: -parseCssMagnitude(linesContentStyle.left, 0),
103 top: -parseCssMagnitude(linesContentStyle.top, 0),
104 maxLeft: parseCssMagnitude(viewLinesStyle.width, 0) - parseCssMagnitude(elemStyle.width, 0),
105 maxTop: parseCssMagnitude(viewLinesStyle.height, 0) - parseCssMagnitude(elemStyle.height, 0)
106 };
107}
108/**
109 * Prevent browser back/forward navigation of a mouse wheel event.
110 */
111function preventNavigation(event) {
112 const { currentTarget, deltaX, deltaY } = event;
113 const absDeltaX = Math.abs(deltaX);
114 const absDeltaY = Math.abs(deltaY);
115 let elem = event.target;
116 while (elem && elem !== currentTarget) {
117 let scroll;
118 if (elem.classList.contains('monaco-scrollable-element')) {
119 scroll = getMonacoEditorScroll(elem);
120 }
121 else {
122 scroll = {
123 left: elem.scrollLeft,
124 top: elem.scrollTop,
125 maxLeft: elem.scrollWidth - elem.clientWidth,
126 maxTop: elem.scrollHeight - elem.clientHeight
127 };
128 }
129 if (scroll) {
130 const scrollH = scroll.maxLeft > 0 && (deltaX < 0 && scroll.left > 0 || deltaX > 0 && scroll.left < scroll.maxLeft);
131 const scrollV = scroll.maxTop > 0 && (deltaY < 0 && scroll.top > 0 || deltaY > 0 && scroll.top < scroll.maxTop);
132 if (scrollH && scrollV || scrollH && absDeltaX > absDeltaY || scrollV && absDeltaY > absDeltaX) {
133 // The event is consumed by the scrollable child element
134 return;
135 }
136 }
137 elem = elem.parentElement;
138 }
139 event.preventDefault();
140 event.stopPropagation();
141}
142exports.preventNavigation = preventNavigation;
143function measureTextWidth(text, style) {
144 const measureElement = getMeasurementElement(style);
145 text = Array.isArray(text) ? text : [text];
146 let width = 0;
147 for (const item of text) {
148 measureElement.textContent = item;
149 width = Math.max(measureElement.getBoundingClientRect().width, width);
150 }
151 return width;
152}
153exports.measureTextWidth = measureTextWidth;
154function measureTextHeight(text, style) {
155 const measureElement = getMeasurementElement(style);
156 text = Array.isArray(text) ? text : [text];
157 let height = 0;
158 for (const item of text) {
159 measureElement.textContent = item;
160 height = Math.max(measureElement.getBoundingClientRect().height, height);
161 }
162 return height;
163}
164exports.measureTextHeight = measureTextHeight;
165const defaultStyle = document.createElement('div').style;
166defaultStyle.fontFamily = 'var(--theia-ui-font-family)';
167defaultStyle.fontSize = 'var(--theia-ui-font-size1)';
168defaultStyle.visibility = 'hidden';
169function getMeasurementElement(style) {
170 let measureElement = document.getElementById('measure');
171 if (!measureElement) {
172 measureElement = document.createElement('span');
173 measureElement.id = 'measure';
174 measureElement.style.fontFamily = defaultStyle.fontFamily;
175 measureElement.style.fontSize = defaultStyle.fontSize;
176 measureElement.style.visibility = defaultStyle.visibility;
177 document.body.appendChild(measureElement);
178 }
179 const measureStyle = measureElement.style;
180 // Reset styling first
181 for (let i = 0; i < measureStyle.length; i++) {
182 const property = measureStyle[i];
183 measureStyle.setProperty(property, defaultStyle.getPropertyValue(property));
184 }
185 // Apply new styling
186 if (style) {
187 for (const [key, value] of Object.entries(style)) {
188 measureStyle.setProperty(key, value);
189 }
190 }
191 return measureElement;
192}
193//# sourceMappingURL=browser.js.map
\No newline at end of file