diff --git a/extensions/markdown/preview-src/csp.ts b/extensions/markdown/preview-src/csp.ts index 007e7e30fbb80e1c97a751416590e9a81dbd3a82..a418fad2ab7f9e8aafe934e757699c8dd5c49629 100644 --- a/extensions/markdown/preview-src/csp.ts +++ b/extensions/markdown/preview-src/csp.ts @@ -4,6 +4,7 @@ *--------------------------------------------------------------------------------------------*/ import { getSettings } from './settings'; +import { postCommand } from './messaging'; const strings = JSON.parse(document.getElementById('vscode-markdown-preview-data').getAttribute('data-strings')); const settings = getSettings(); @@ -24,14 +25,7 @@ const showCspWarning = () => { notification.setAttribute('role', 'button'); notification.setAttribute('aria-label', strings.cspAlertMessageLabel); notification.onclick = () => { - window.parent.postMessage({ - type: 'command', - source: settings.source, - body: { - command: 'markdown.showPreviewSecuritySelector', - args: [settings.source] - } - }, '*'); + postCommand('markdown.showPreviewSecuritySelector', [settings.source]); }; document.body.appendChild(notification); }; diff --git a/extensions/markdown/preview-src/index.ts b/extensions/markdown/preview-src/index.ts index 1311315897d297f833cd771b24ec52a0b416a366..e37a40a86a64b77187ace500ca778cf62e9a7562 100644 --- a/extensions/markdown/preview-src/index.ts +++ b/extensions/markdown/preview-src/index.ts @@ -4,7 +4,7 @@ *--------------------------------------------------------------------------------------------*/ import { getSettings } from './settings'; - +import { postCommand, postMessage } from './messaging'; // From https://remysharp.com/2010/07/21/throttling-function-calls function throttle(fn: (x: any) => any, threshhold: any, scope?: any) { @@ -37,24 +37,6 @@ function clampLine(line: number) { return clamp(0, settings.lineCount - 1, line); } -/** - * Post a message to the markdown extension - */ -function postMessage(type: string, body: object) { - window.parent.postMessage({ - type, - source: settings.source, - body - }, '*'); -} - -/** - * Post a command to be executed to the markdown extension - */ -function postCommand(command: string, args: any[]) { - postMessage('command', { command, args }); -} - interface CodeLineElement { element: HTMLElement; diff --git a/extensions/markdown/preview-src/loading.ts b/extensions/markdown/preview-src/loading.ts index ae4c01f68e48dec4d8699029e2956afd20e0fac3..757eba7b8e631cdfb575c6fc2aa19d0cdf6601ba 100644 --- a/extensions/markdown/preview-src/loading.ts +++ b/extensions/markdown/preview-src/loading.ts @@ -3,6 +3,7 @@ * Licensed under the MIT License. See License.txt in the project root for license information. *--------------------------------------------------------------------------------------------*/ import { getSettings } from './settings'; +import { postCommand } from './messaging'; const unloadedStyles: string[] = []; @@ -25,12 +26,5 @@ window.addEventListener('load', () => { if (!unloadedStyles.length) { return; } - window.parent.postMessage({ - type: 'command', - source: settings.source, - body: { - command: '_markdown.onPreviewStyleLoadError', - args: [unloadedStyles] - } - }, '*'); + postCommand('_markdown.onPreviewStyleLoadError', [unloadedStyles]); }); \ No newline at end of file diff --git a/extensions/markdown/preview-src/messaging.ts b/extensions/markdown/preview-src/messaging.ts new file mode 100644 index 0000000000000000000000000000000000000000..10571ff28db6147e4211ccfd30a2d511638fd114 --- /dev/null +++ b/extensions/markdown/preview-src/messaging.ts @@ -0,0 +1,24 @@ +/*--------------------------------------------------------------------------------------------- + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. See License.txt in the project root for license information. + *--------------------------------------------------------------------------------------------*/ + +import { getSettings } from './settings'; + +/** + * Post a message to the markdown extension + */ +export function postMessage(type: string, body: object) { + window.parent.postMessage({ + type, + source: getSettings().source, + body + }, '*'); +} + +/** + * Post a command to be executed to the markdown extension + */ +export function postCommand(command: string, args: any[]) { + postMessage('command', { command, args }); +}