mirror of
https://github.com/microsoft/vscode.git
synced 2026-02-26 12:44:05 +00:00
Extract onceDocumentLoaded to function
This commit is contained in:
12
extensions/markdown/preview-src/events.ts
Normal file
12
extensions/markdown/preview-src/events.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the MIT License. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
export function onceDocumentLoaded(f: () => void) {
|
||||
if (document.readyState === 'loading' || document.readyState === 'uninitialized') {
|
||||
document.addEventListener('DOMContentLoaded', f);
|
||||
} else {
|
||||
f();
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,7 @@
|
||||
|
||||
import { getSettings } from './settings';
|
||||
import { postCommand, postMessage } from './messaging';
|
||||
import { onceDocumentLoaded } from './events';
|
||||
|
||||
// From https://remysharp.com/2010/07/21/throttling-function-calls
|
||||
function throttle(fn: (x: any) => any, threshhold: any, scope?: any) {
|
||||
@@ -182,7 +183,7 @@ var scrollDisabled = true;
|
||||
const marker = new ActiveLineMarker();
|
||||
const settings = getSettings();
|
||||
|
||||
function onLoad() {
|
||||
onceDocumentLoaded(() => {
|
||||
if (settings.scrollPreviewWithEditor) {
|
||||
setTimeout(() => {
|
||||
const initialLine = +settings.line;
|
||||
@@ -192,7 +193,7 @@ function onLoad() {
|
||||
}
|
||||
}, 0);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
const onUpdateView = (() => {
|
||||
const doScroll = throttle((line: number) => {
|
||||
@@ -208,14 +209,6 @@ const onUpdateView = (() => {
|
||||
};
|
||||
})();
|
||||
|
||||
|
||||
if (document.readyState === 'loading' || document.readyState === 'uninitialized') {
|
||||
document.addEventListener('DOMContentLoaded', onLoad);
|
||||
} else {
|
||||
onLoad();
|
||||
}
|
||||
|
||||
|
||||
window.addEventListener('resize', () => {
|
||||
scrollDisabled = true;
|
||||
}, true);
|
||||
|
||||
Reference in New Issue
Block a user