UNPKG

8.11 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 common_1 = require("../common");
24const userAgent = typeof navigator !== 'undefined' ? navigator.userAgent : '';
25exports.isIE = (userAgent.indexOf('Trident') >= 0);
26exports.isEdge = (userAgent.indexOf('Edge/') >= 0);
27exports.isEdgeOrIE = exports.isIE || exports.isEdge;
28exports.isOpera = (userAgent.indexOf('Opera') >= 0);
29exports.isFirefox = (userAgent.indexOf('Firefox') >= 0);
30exports.isWebKit = (userAgent.indexOf('AppleWebKit') >= 0);
31exports.isChrome = (userAgent.indexOf('Chrome') >= 0);
32exports.isSafari = (userAgent.indexOf('Chrome') === -1) && (userAgent.indexOf('Safari') >= 0);
33exports.isIPad = (userAgent.indexOf('iPad') >= 0);
34// eslint-disable-next-line @typescript-eslint/no-explicit-any
35/**
36 * @deprecated us Environment.electron.is
37 */
38exports.isNative = common_1.environment.electron.is();
39// eslint-disable-next-line @typescript-eslint/no-explicit-any
40exports.isBasicWasmSupported = typeof window.WebAssembly !== 'undefined';
41/**
42 * Resolves after the next animation frame if no parameter is given,
43 * or after the given number of animation frames.
44 */
45function animationFrame(n = 1) {
46 return new Promise(resolve => {
47 function frameFunc() {
48 if (n <= 0) {
49 resolve();
50 }
51 else {
52 n--;
53 requestAnimationFrame(frameFunc);
54 }
55 }
56 frameFunc();
57 });
58}
59exports.animationFrame = animationFrame;
60function parseCssMagnitude(value, defaultValue) {
61 if (value) {
62 let parsed;
63 if (value.endsWith('px')) {
64 parsed = parseFloat(value.substring(0, value.length - 2));
65 }
66 else {
67 parsed = parseFloat(value);
68 }
69 if (!isNaN(parsed)) {
70 return parsed;
71 }
72 }
73 return defaultValue;
74}
75exports.parseCssMagnitude = parseCssMagnitude;
76function parseCssTime(time, defaultValue) {
77 if (time) {
78 let parsed;
79 if (time.endsWith('ms')) {
80 parsed = parseFloat(time.substring(0, time.length - 2));
81 }
82 else if (time.endsWith('s')) {
83 parsed = parseFloat(time.substring(0, time.length - 1)) * 1000;
84 }
85 else {
86 parsed = parseFloat(time);
87 }
88 if (!isNaN(parsed)) {
89 return parsed;
90 }
91 }
92 return defaultValue;
93}
94exports.parseCssTime = parseCssTime;
95function getMonacoEditorScroll(elem) {
96 const linesContent = elem.querySelector('.lines-content');
97 const viewLines = elem.querySelector('.view-lines');
98 // eslint-disable-next-line no-null/no-null
99 if (linesContent === null || viewLines === null) {
100 return undefined;
101 }
102 const linesContentStyle = linesContent.style;
103 const elemStyle = elem.style;
104 const viewLinesStyle = viewLines.style;
105 return {
106 left: -parseCssMagnitude(linesContentStyle.left, 0),
107 top: -parseCssMagnitude(linesContentStyle.top, 0),
108 maxLeft: parseCssMagnitude(viewLinesStyle.width, 0) - parseCssMagnitude(elemStyle.width, 0),
109 maxTop: parseCssMagnitude(viewLinesStyle.height, 0) - parseCssMagnitude(elemStyle.height, 0)
110 };
111}
112/**
113 * Prevent browser back/forward navigation of a mouse wheel event.
114 */
115function preventNavigation(event) {
116 const { currentTarget, deltaX, deltaY } = event;
117 const absDeltaX = Math.abs(deltaX);
118 const absDeltaY = Math.abs(deltaY);
119 let elem = event.target;
120 while (elem && elem !== currentTarget) {
121 let scroll;
122 if (elem.classList.contains('monaco-scrollable-element')) {
123 scroll = getMonacoEditorScroll(elem);
124 }
125 else {
126 scroll = {
127 left: elem.scrollLeft,
128 top: elem.scrollTop,
129 maxLeft: elem.scrollWidth - elem.clientWidth,
130 maxTop: elem.scrollHeight - elem.clientHeight
131 };
132 }
133 if (scroll) {
134 const scrollH = scroll.maxLeft > 0 && (deltaX < 0 && scroll.left > 0 || deltaX > 0 && scroll.left < scroll.maxLeft);
135 const scrollV = scroll.maxTop > 0 && (deltaY < 0 && scroll.top > 0 || deltaY > 0 && scroll.top < scroll.maxTop);
136 if (scrollH && scrollV || scrollH && absDeltaX > absDeltaY || scrollV && absDeltaY > absDeltaX) {
137 // The event is consumed by the scrollable child element
138 return;
139 }
140 }
141 elem = elem.parentElement;
142 }
143 event.preventDefault();
144 event.stopPropagation();
145}
146exports.preventNavigation = preventNavigation;
147function measureTextWidth(text, style) {
148 const measureElement = getMeasurementElement(style);
149 text = Array.isArray(text) ? text : [text];
150 let width = 0;
151 for (const item of text) {
152 measureElement.textContent = item;
153 width = Math.max(measureElement.getBoundingClientRect().width, width);
154 }
155 return width;
156}
157exports.measureTextWidth = measureTextWidth;
158function measureTextHeight(text, style) {
159 const measureElement = getMeasurementElement(style);
160 text = Array.isArray(text) ? text : [text];
161 let height = 0;
162 for (const item of text) {
163 measureElement.textContent = item;
164 height = Math.max(measureElement.getBoundingClientRect().height, height);
165 }
166 return height;
167}
168exports.measureTextHeight = measureTextHeight;
169const defaultStyle = document.createElement('div').style;
170defaultStyle.fontFamily = 'var(--theia-ui-font-family)';
171defaultStyle.fontSize = 'var(--theia-ui-font-size1)';
172defaultStyle.visibility = 'hidden';
173function getMeasurementElement(style) {
174 let measureElement = document.getElementById('measure');
175 if (!measureElement) {
176 measureElement = document.createElement('span');
177 measureElement.id = 'measure';
178 measureElement.style.fontFamily = defaultStyle.fontFamily;
179 measureElement.style.fontSize = defaultStyle.fontSize;
180 measureElement.style.visibility = defaultStyle.visibility;
181 document.body.appendChild(measureElement);
182 }
183 const measureStyle = measureElement.style;
184 // Reset styling first
185 for (let i = 0; i < measureStyle.length; i++) {
186 const property = measureStyle[i];
187 measureStyle.setProperty(property, defaultStyle.getPropertyValue(property));
188 }
189 // Apply new styling
190 if (style) {
191 for (const [key, value] of Object.entries(style)) {
192 measureStyle.setProperty(key, value);
193 }
194 }
195 return measureElement;
196}
197//# sourceMappingURL=browser.js.map
\No newline at end of file