UNPKG

@nteract/monaco-editor

Version:

A React component for the monaco editor, tailored for nteract

140 lines 5.47 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.DocumentUri = void 0; const editor_api_1 = require("monaco-editor/esm/vs/editor/editor.api"); const monaco = __importStar(require("monaco-editor/esm/vs/editor/editor.api")); /** * Common Uri identifiers */ var Identifiers; (function (Identifiers) { Identifiers["cellUriMarker"] = "cell"; Identifiers["notebookUriMarker"] = "notebook"; })(Identifiers || (Identifiers = {})); /** * Map of language id to file extension. */ const languageToExtensionMap = new Map(); /** * Document Monaco Uri helper to support conversions/detection for notebook and cells. */ class DocumentUri { /** * Checks if uri represents a document. * @param uri Uri to validate. */ static isDocument(uri) { // Remove file extension if it exists and then check if uri path ends with the notebook marker. const path = uri.path.replace(/\.[^\/.]+$/, ""); return !!(path && path.endsWith(`/${Identifiers.notebookUriMarker}`)); } /** * Checks if uri represents a cell. * @param uri Uri to validate. */ static isCell(uri) { // Remove file extension if it exists and then check if uri path ends with the cell marker. const path = uri.path.replace(/\.[^\/.]+$/, ""); return !!(path && path.endsWith(`/${Identifiers.cellUriMarker}`)); } /** * Get document id from document uri. * @param uri Uri of cell. */ static getDocumentIdFromDocumentUri(uri) { if (DocumentUri.isDocument(uri)) { const tokens = uri.path.split("/"); // Document uri path is in the format of "/<document_id>/notebook.<file_extension>" so we check the length // of the split is 3 tokens and grab the 2nd token (document id) which is index 1. if (tokens.length === 3) { return tokens[1]; } } return undefined; } /** * Get document id from cell uri. * @param uri Uri of cell. */ static getDocumentIdFromCellUri(uri) { if (DocumentUri.isCell(uri)) { const tokens = uri.path.split("/"); // Cell uri path is in the format of "/<document_id>/<cell_id>/cell.<file_extension>" so we check the length // of the split is 4 tokens and grab the 2nd token (document id) which is index 1. if (tokens.length === 4) { return tokens[1]; } } return undefined; } /** * Get cell id from cell uri. * @param uri Uri of cell. */ static getCellIdFromCellUri(uri) { if (DocumentUri.isCell(uri)) { const tokens = uri.path.split("/"); // Cell uri path is in the format of "/<document_id>/<cell_id>/cell.<file_extension>" so we check the length // of the split is 4 tokens and grab the 3rd token (cell id) which is index 2. if (tokens.length === 4) { return tokens[2]; } } return undefined; } /** * Create a document uri. * @param id Document id. */ static createDocumentUri(id, languageId) { const fileExtension = DocumentUri.getFileExtension(languageId); return editor_api_1.Uri.file(`${id}/${Identifiers.notebookUriMarker}${fileExtension}`); } /** * Create a cell uri. * @param documentId Document id. * @param cellId Cell id. */ static createCellUri(documentId, cellId, languageId) { const fileExtension = DocumentUri.getFileExtension(languageId); return editor_api_1.Uri.file(`${documentId}/${cellId}/${Identifiers.cellUriMarker}${fileExtension}`); } /** * Get file extension for language. If no extension exist then empty string is returned. * @param languageId Language id. */ static getFileExtension(languageId) { if (!languageToExtensionMap.has(languageId)) { monaco.languages.getLanguages().forEach((metadata) => { languageToExtensionMap.set( // Language id metadata.id, // File extension if it exists. If more than one file extension exists then take the first one // as it's the more important version compared to the rest in the list. metadata.extensions && metadata.extensions.length > 0 ? metadata.extensions[0] : ""); }); } return languageToExtensionMap.get(languageId) || ""; } } exports.DocumentUri = DocumentUri; //# sourceMappingURL=documentUri.js.map