mirror of
https://github.com/microsoft/vscode.git
synced 2025-12-24 20:26:08 +00:00
Small cleanup follow up on #236145
- Don't send content as json - Reuse existing load helper
This commit is contained in:
@@ -7,7 +7,7 @@ import { ActiveLineMarker } from './activeLineMarker';
|
||||
import { onceDocumentLoaded } from './events';
|
||||
import { createPosterForVsCode } from './messaging';
|
||||
import { getEditorLineNumberForPageOffset, scrollToRevealSourceLine, getLineElementForFragment } from './scroll-sync';
|
||||
import { SettingsManager, getData } from './settings';
|
||||
import { SettingsManager, getData, getRawData } from './settings';
|
||||
import throttle = require('lodash.throttle');
|
||||
import morphdom from 'morphdom';
|
||||
import type { ToWebviewMessage } from '../types/previewMessaging';
|
||||
@@ -61,8 +61,16 @@ function doAfterImagesLoaded(cb: () => void) {
|
||||
}
|
||||
|
||||
onceDocumentLoaded(() => {
|
||||
const scrollProgress = state.scrollProgress;
|
||||
// Load initial html
|
||||
const htmlParser = new DOMParser();
|
||||
const markDownHtml = htmlParser.parseFromString(
|
||||
getRawData('data-initial-md-content'),
|
||||
'text/html'
|
||||
);
|
||||
document.body.appendChild(markDownHtml.body);
|
||||
|
||||
// Restore
|
||||
const scrollProgress = state.scrollProgress;
|
||||
addImageContexts();
|
||||
if (typeof scrollProgress === 'number' && !settings.settings.fragment) {
|
||||
doAfterImagesLoaded(() => {
|
||||
@@ -353,15 +361,6 @@ document.addEventListener('click', event => {
|
||||
}
|
||||
}, true);
|
||||
|
||||
window.addEventListener('load', () => {
|
||||
const htmlParser = new DOMParser();
|
||||
const markDownHtml = htmlParser.parseFromString(
|
||||
decodeURIComponent(getData('data-md-content')),
|
||||
'text/html'
|
||||
);
|
||||
document.body.appendChild(markDownHtml.body);
|
||||
});
|
||||
|
||||
window.addEventListener('scroll', throttle(() => {
|
||||
updateScrollProgress();
|
||||
|
||||
|
||||
@@ -16,18 +16,22 @@ export interface PreviewSettings {
|
||||
readonly webviewResourceRoot: string;
|
||||
}
|
||||
|
||||
export function getData<T = {}>(key: string): T {
|
||||
export function getRawData(key: string): string {
|
||||
const element = document.getElementById('vscode-markdown-preview-data');
|
||||
if (element) {
|
||||
const data = element.getAttribute(key);
|
||||
if (data) {
|
||||
return JSON.parse(data);
|
||||
return data;
|
||||
}
|
||||
}
|
||||
|
||||
throw new Error(`Could not load data for ${key}`);
|
||||
}
|
||||
|
||||
export function getData<T = {}>(key: string): T {
|
||||
return JSON.parse(getRawData(key));
|
||||
}
|
||||
|
||||
export class SettingsManager {
|
||||
private _settings: PreviewSettings = getData('data-settings');
|
||||
|
||||
|
||||
@@ -99,7 +99,7 @@ export class MdDocumentRenderer {
|
||||
data-settings="${escapeAttribute(JSON.stringify(initialData))}"
|
||||
data-strings="${escapeAttribute(JSON.stringify(previewStrings))}"
|
||||
data-state="${escapeAttribute(JSON.stringify(state || {}))}"
|
||||
data-md-content="${escapeAttribute(JSON.stringify(encodeURIComponent(body.html)))}">
|
||||
data-initial-md-content="${escapeAttribute(body.html)}">
|
||||
<script src="${this._extensionResourcePath(resourceProvider, 'pre.js')}" nonce="${nonce}"></script>
|
||||
${this._getStyles(resourceProvider, sourceUri, config, imageInfo)}
|
||||
<base href="${resourceProvider.asWebviewUri(markdownDocument.uri)}">
|
||||
|
||||
Reference in New Issue
Block a user