From 0a68f769f4675a4afa0b41675979507a5049ba13 Mon Sep 17 00:00:00 2001 From: Matt Bierner Date: Mon, 5 Mar 2018 10:59:35 -0800 Subject: [PATCH] Move common command logic to own file --- extensions/markdown/preview-src/csp.ts | 10 ++------ extensions/markdown/preview-src/index.ts | 20 +--------------- extensions/markdown/preview-src/loading.ts | 10 ++------ extensions/markdown/preview-src/messaging.ts | 24 ++++++++++++++++++++ 4 files changed, 29 insertions(+), 35 deletions(-) create mode 100644 extensions/markdown/preview-src/messaging.ts diff --git a/extensions/markdown/preview-src/csp.ts b/extensions/markdown/preview-src/csp.ts index 007e7e30fbb..a418fad2ab7 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 1311315897d..e37a40a86a6 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 ae4c01f68e4..757eba7b8e6 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 00000000000..10571ff28db --- /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 }); +} -- GitLab