mirror of
https://github.com/microsoft/vscode.git
synced 2025-12-24 12:19:20 +00:00
Rename markdown to markdown-language-features
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the MIT License. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
import { getElementsForSourceLine } from './scroll-sync';
|
||||
|
||||
export class ActiveLineMarker {
|
||||
private _current: any;
|
||||
|
||||
onDidChangeTextEditorSelection(line: number) {
|
||||
const { previous } = getElementsForSourceLine(line);
|
||||
this._update(previous && previous.element);
|
||||
}
|
||||
|
||||
_update(before: HTMLElement | undefined) {
|
||||
this._unmarkActiveElement(this._current);
|
||||
this._markActiveElement(before);
|
||||
this._current = before;
|
||||
}
|
||||
|
||||
_unmarkActiveElement(element: HTMLElement | undefined) {
|
||||
if (!element) {
|
||||
return;
|
||||
}
|
||||
element.className = element.className.replace(/\bcode-active-line\b/g, '');
|
||||
}
|
||||
|
||||
_markActiveElement(element: HTMLElement | undefined) {
|
||||
if (!element) {
|
||||
return;
|
||||
}
|
||||
element.className += ' code-active-line';
|
||||
}
|
||||
}
|
||||
49
extensions/markdown-language-features/preview-src/csp.ts
Normal file
49
extensions/markdown-language-features/preview-src/csp.ts
Normal file
@@ -0,0 +1,49 @@
|
||||
/*---------------------------------------------------------------------------------------------
|
||||
* 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';
|
||||
import { getStrings } from './strings';
|
||||
import { postCommand } from './messaging';
|
||||
|
||||
/**
|
||||
* Shows an alert when there is a content security policy violation.
|
||||
*/
|
||||
export class CspAlerter {
|
||||
private didShow = false;
|
||||
|
||||
constructor() {
|
||||
document.addEventListener('securitypolicyviolation', () => {
|
||||
this.showCspWarning();
|
||||
});
|
||||
|
||||
window.addEventListener('message', (event) => {
|
||||
if (event && event.data && event.data.name === 'vscode-did-block-svg') {
|
||||
this.showCspWarning();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private showCspWarning() {
|
||||
const strings = getStrings();
|
||||
const settings = getSettings();
|
||||
|
||||
if (this.didShow || settings.disableSecurityWarnings) {
|
||||
return;
|
||||
}
|
||||
this.didShow = true;
|
||||
|
||||
const notification = document.createElement('a');
|
||||
notification.innerText = strings.cspAlertMessageText;
|
||||
notification.setAttribute('id', 'code-csp-warning');
|
||||
notification.setAttribute('title', strings.cspAlertMessageTitle);
|
||||
|
||||
notification.setAttribute('role', 'button');
|
||||
notification.setAttribute('aria-label', strings.cspAlertMessageLabel);
|
||||
notification.onclick = () => {
|
||||
postCommand('markdown.showPreviewSecuritySelector', [settings.source]);
|
||||
};
|
||||
document.body.appendChild(notification);
|
||||
}
|
||||
}
|
||||
12
extensions/markdown-language-features/preview-src/events.ts
Normal file
12
extensions/markdown-language-features/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();
|
||||
}
|
||||
}
|
||||
117
extensions/markdown-language-features/preview-src/index.ts
Normal file
117
extensions/markdown-language-features/preview-src/index.ts
Normal file
@@ -0,0 +1,117 @@
|
||||
/*---------------------------------------------------------------------------------------------
|
||||
* 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';
|
||||
import { postCommand, postMessage } from './messaging';
|
||||
import { onceDocumentLoaded } from './events';
|
||||
import { getEditorLineNumberForPageOffset, scrollToRevealSourceLine } from './scroll-sync';
|
||||
import { ActiveLineMarker } from './activeLineMarker';
|
||||
import throttle = require('lodash.throttle');
|
||||
|
||||
var scrollDisabled = true;
|
||||
const marker = new ActiveLineMarker();
|
||||
const settings = getSettings();
|
||||
|
||||
onceDocumentLoaded(() => {
|
||||
if (settings.scrollPreviewWithEditor) {
|
||||
setTimeout(() => {
|
||||
const initialLine = +settings.line;
|
||||
if (!isNaN(initialLine)) {
|
||||
scrollDisabled = true;
|
||||
scrollToRevealSourceLine(initialLine);
|
||||
}
|
||||
}, 0);
|
||||
}
|
||||
});
|
||||
|
||||
const onUpdateView = (() => {
|
||||
const doScroll = throttle((line: number) => {
|
||||
scrollDisabled = true;
|
||||
scrollToRevealSourceLine(line);
|
||||
}, 50);
|
||||
|
||||
return (line: number, settings: any) => {
|
||||
if (!isNaN(line)) {
|
||||
settings.line = line;
|
||||
doScroll(line);
|
||||
}
|
||||
};
|
||||
})();
|
||||
|
||||
window.addEventListener('resize', () => {
|
||||
scrollDisabled = true;
|
||||
}, true);
|
||||
|
||||
window.addEventListener('message', event => {
|
||||
if (event.data.source !== settings.source) {
|
||||
return;
|
||||
}
|
||||
|
||||
switch (event.data.type) {
|
||||
case 'onDidChangeTextEditorSelection':
|
||||
marker.onDidChangeTextEditorSelection(event.data.line);
|
||||
break;
|
||||
|
||||
case 'updateView':
|
||||
onUpdateView(event.data.line, settings);
|
||||
break;
|
||||
}
|
||||
}, false);
|
||||
|
||||
document.addEventListener('dblclick', event => {
|
||||
if (!settings.doubleClickToSwitchToEditor) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Ignore clicks on links
|
||||
for (let node = event.target as HTMLElement; node; node = node.parentNode as HTMLElement) {
|
||||
if (node.tagName === 'A') {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
const offset = event.pageY;
|
||||
const line = getEditorLineNumberForPageOffset(offset);
|
||||
if (typeof line === 'number' && !isNaN(line)) {
|
||||
postMessage('didClick', { line });
|
||||
}
|
||||
});
|
||||
|
||||
document.addEventListener('click', event => {
|
||||
if (!event) {
|
||||
return;
|
||||
}
|
||||
|
||||
let node: any = event.target;
|
||||
while (node) {
|
||||
if (node.tagName && node.tagName === 'A' && node.href) {
|
||||
if (node.getAttribute('href').startsWith('#')) {
|
||||
break;
|
||||
}
|
||||
if (node.href.startsWith('file://') || node.href.startsWith('vscode-resource:')) {
|
||||
const [path, fragment] = node.href.replace(/^(file:\/\/|vscode-resource:)/i, '').split('#');
|
||||
postCommand('_markdown.openDocumentLink', [{ path, fragment }]);
|
||||
event.preventDefault();
|
||||
event.stopPropagation();
|
||||
break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
node = node.parentNode;
|
||||
}
|
||||
}, true);
|
||||
|
||||
if (settings.scrollEditorWithPreview) {
|
||||
window.addEventListener('scroll', throttle(() => {
|
||||
if (scrollDisabled) {
|
||||
scrollDisabled = false;
|
||||
} else {
|
||||
const line = getEditorLineNumberForPageOffset(window.scrollY);
|
||||
if (typeof line === 'number' && !isNaN(line)) {
|
||||
postMessage('revealLine', { line });
|
||||
}
|
||||
}
|
||||
}, 50));
|
||||
}
|
||||
31
extensions/markdown-language-features/preview-src/loading.ts
Normal file
31
extensions/markdown-language-features/preview-src/loading.ts
Normal file
@@ -0,0 +1,31 @@
|
||||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the MIT License. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
import { postCommand } from './messaging';
|
||||
|
||||
export class StyleLoadingMonitor {
|
||||
private unloadedStyles: string[] = [];
|
||||
|
||||
constructor() {
|
||||
const onStyleLoadError = (event: any) => {
|
||||
const source = event.target.dataset.source;
|
||||
this.unloadedStyles.push(source);
|
||||
};
|
||||
|
||||
window.addEventListener('DOMContentLoaded', () => {
|
||||
for (const link of document.getElementsByClassName('code-user-style') as HTMLCollectionOf<HTMLElement>) {
|
||||
if (link.dataset.source) {
|
||||
link.onerror = onStyleLoadError;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
window.addEventListener('load', () => {
|
||||
if (!this.unloadedStyles.length) {
|
||||
return;
|
||||
}
|
||||
postCommand('_markdown.onPreviewStyleLoadError', [this.unloadedStyles]);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -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 });
|
||||
}
|
||||
13
extensions/markdown-language-features/preview-src/pre.ts
Normal file
13
extensions/markdown-language-features/preview-src/pre.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the MIT License. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
import { CspAlerter } from './csp';
|
||||
import { StyleLoadingMonitor } from './loading';
|
||||
|
||||
// tslint:disable-next-line:no-unused-expression
|
||||
new CspAlerter();
|
||||
|
||||
// tslint:disable-next-line:no-unused-expression
|
||||
new StyleLoadingMonitor();
|
||||
127
extensions/markdown-language-features/preview-src/scroll-sync.ts
Normal file
127
extensions/markdown-language-features/preview-src/scroll-sync.ts
Normal file
@@ -0,0 +1,127 @@
|
||||
/*---------------------------------------------------------------------------------------------
|
||||
* 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';
|
||||
|
||||
|
||||
function clamp(min: number, max: number, value: number) {
|
||||
return Math.min(max, Math.max(min, value));
|
||||
}
|
||||
|
||||
function clampLine(line: number) {
|
||||
return clamp(0, getSettings().lineCount - 1, line);
|
||||
}
|
||||
|
||||
|
||||
export interface CodeLineElement {
|
||||
element: HTMLElement;
|
||||
line: number;
|
||||
}
|
||||
|
||||
const getCodeLineElements = (() => {
|
||||
let elements: CodeLineElement[];
|
||||
return () => {
|
||||
if (!elements) {
|
||||
elements = Array.prototype.map.call(
|
||||
document.getElementsByClassName('code-line'),
|
||||
(element: any) => {
|
||||
const line = +element.getAttribute('data-line');
|
||||
return { element, line };
|
||||
})
|
||||
.filter((x: any) => !isNaN(x.line));
|
||||
}
|
||||
return elements;
|
||||
};
|
||||
})();
|
||||
|
||||
/**
|
||||
* Find the html elements that map to a specific target line in the editor.
|
||||
*
|
||||
* If an exact match, returns a single element. If the line is between elements,
|
||||
* returns the element prior to and the element after the given line.
|
||||
*/
|
||||
export function getElementsForSourceLine(targetLine: number): { previous: CodeLineElement; next?: CodeLineElement; } {
|
||||
const lineNumber = Math.floor(targetLine);
|
||||
const lines = getCodeLineElements();
|
||||
let previous = lines[0] || null;
|
||||
for (const entry of lines) {
|
||||
if (entry.line === lineNumber) {
|
||||
return { previous: entry, next: undefined };
|
||||
}
|
||||
else if (entry.line > lineNumber) {
|
||||
return { previous, next: entry };
|
||||
}
|
||||
previous = entry;
|
||||
}
|
||||
return { previous };
|
||||
}
|
||||
|
||||
/**
|
||||
* Find the html elements that are at a specific pixel offset on the page.
|
||||
*/
|
||||
export function getLineElementsAtPageOffset(offset: number): { previous: CodeLineElement; next?: CodeLineElement; } {
|
||||
const lines = getCodeLineElements();
|
||||
const position = offset - window.scrollY;
|
||||
let lo = -1;
|
||||
let hi = lines.length - 1;
|
||||
while (lo + 1 < hi) {
|
||||
const mid = Math.floor((lo + hi) / 2);
|
||||
const bounds = lines[mid].element.getBoundingClientRect();
|
||||
if (bounds.top + bounds.height >= position) {
|
||||
hi = mid;
|
||||
}
|
||||
else {
|
||||
lo = mid;
|
||||
}
|
||||
}
|
||||
const hiElement = lines[hi];
|
||||
const hiBounds = hiElement.element.getBoundingClientRect();
|
||||
if (hi >= 1 && hiBounds.top > position) {
|
||||
const loElement = lines[lo];
|
||||
return { previous: loElement, next: hiElement };
|
||||
}
|
||||
return { previous: hiElement };
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempt to reveal the element for a source line in the editor.
|
||||
*/
|
||||
export function scrollToRevealSourceLine(line: number) {
|
||||
const { previous, next } = getElementsForSourceLine(line);
|
||||
if (previous && getSettings().scrollPreviewWithEditor) {
|
||||
let scrollTo = 0;
|
||||
const rect = previous.element.getBoundingClientRect();
|
||||
const previousTop = rect.top;
|
||||
if (next && next.line !== previous.line) {
|
||||
// Between two elements. Go to percentage offset between them.
|
||||
const betweenProgress = (line - previous.line) / (next.line - previous.line);
|
||||
const elementOffset = next.element.getBoundingClientRect().top - previousTop;
|
||||
scrollTo = previousTop + betweenProgress * elementOffset;
|
||||
}
|
||||
else {
|
||||
scrollTo = previousTop;
|
||||
}
|
||||
window.scroll(0, Math.max(1, window.scrollY + scrollTo));
|
||||
}
|
||||
}
|
||||
|
||||
export function getEditorLineNumberForPageOffset(offset: number) {
|
||||
const { previous, next } = getLineElementsAtPageOffset(offset);
|
||||
if (previous) {
|
||||
const previousBounds = previous.element.getBoundingClientRect();
|
||||
const offsetFromPrevious = (offset - window.scrollY - previousBounds.top);
|
||||
if (next) {
|
||||
const progressBetweenElements = offsetFromPrevious / (next.element.getBoundingClientRect().top - previousBounds.top);
|
||||
const line = previous.line + progressBetweenElements * (next.line - previous.line);
|
||||
return clampLine(line);
|
||||
}
|
||||
else {
|
||||
const progressWithinElement = offsetFromPrevious / (previousBounds.height);
|
||||
const line = previous.line + progressWithinElement;
|
||||
return clampLine(line);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the MIT License. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
export interface PreviewSettings {
|
||||
source: string;
|
||||
line: number;
|
||||
lineCount: number;
|
||||
scrollPreviewWithEditor?: boolean;
|
||||
scrollEditorWithPreview: boolean;
|
||||
disableSecurityWarnings: boolean;
|
||||
doubleClickToSwitchToEditor: boolean;
|
||||
}
|
||||
|
||||
let cachedSettings: PreviewSettings | undefined = undefined;
|
||||
|
||||
export function getSettings(): PreviewSettings {
|
||||
if (cachedSettings) {
|
||||
return cachedSettings;
|
||||
}
|
||||
|
||||
const element = document.getElementById('vscode-markdown-preview-data');
|
||||
if (element) {
|
||||
const data = element.getAttribute('data-settings');
|
||||
if (data) {
|
||||
cachedSettings = JSON.parse(data);
|
||||
return cachedSettings!;
|
||||
}
|
||||
}
|
||||
|
||||
throw new Error('Could not load settings');
|
||||
}
|
||||
15
extensions/markdown-language-features/preview-src/strings.ts
Normal file
15
extensions/markdown-language-features/preview-src/strings.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the MIT License. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
export function getStrings(): { [key: string]: string } {
|
||||
const store = document.getElementById('vscode-markdown-preview-data');
|
||||
if (store) {
|
||||
const data = store.getAttribute('data-strings');
|
||||
if (data) {
|
||||
return JSON.parse(data);
|
||||
}
|
||||
}
|
||||
throw new Error('Could not load strings');
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"outDir": "./dist/",
|
||||
"module": "commonjs",
|
||||
"target": "es6",
|
||||
"jsx": "react",
|
||||
"sourceMap": true,
|
||||
"strict": true,
|
||||
"noImplicitAny": true,
|
||||
"noUnusedLocals": true
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user