debian-mirror-gitlab/app/assets/javascripts/ide/utils.js

140 lines
3.7 KiB
JavaScript
Raw Normal View History

2020-04-08 14:13:33 +05:30
import { languages } from 'monaco-editor';
2020-11-24 15:15:51 +05:30
import { flatten, isString } from 'lodash';
2020-10-24 23:57:45 +05:30
import { SIDE_LEFT, SIDE_RIGHT } from './constants';
2020-04-08 14:13:33 +05:30
const toLowerCase = x => x.toLowerCase();
const monacoLanguages = languages.getLanguages();
const monacoExtensions = new Set(
flatten(monacoLanguages.map(lang => lang.extensions?.map(toLowerCase) || [])),
);
const monacoMimetypes = new Set(
flatten(monacoLanguages.map(lang => lang.mimetypes?.map(toLowerCase) || [])),
);
const monacoFilenames = new Set(
flatten(monacoLanguages.map(lang => lang.filenames?.map(toLowerCase) || [])),
);
const KNOWN_TYPES = [
{
isText: false,
isMatch(mimeType) {
return mimeType.toLowerCase().includes('image/');
},
},
{
isText: true,
isMatch(mimeType) {
return mimeType.toLowerCase().includes('text/');
},
},
{
isText: true,
isMatch(mimeType, fileName) {
const fileExtension = fileName.includes('.') ? `.${fileName.split('.').pop()}` : '';
return (
monacoExtensions.has(fileExtension.toLowerCase()) ||
monacoMimetypes.has(mimeType.toLowerCase()) ||
monacoFilenames.has(fileName.toLowerCase())
);
},
},
];
2020-11-24 15:15:51 +05:30
export function isTextFile({ name, content, mimeType = '' }) {
const knownType = KNOWN_TYPES.find(type => type.isMatch(mimeType, name));
2020-04-08 14:13:33 +05:30
if (knownType) return knownType.isText;
// does the string contain ascii characters only (ranges from space to tilde, tabs and new lines)
const asciiRegex = /^[ -~\t\n\r]+$/;
2020-11-24 15:15:51 +05:30
2020-04-08 14:13:33 +05:30
// for unknown types, determine the type by evaluating the file contents
2020-11-24 15:15:51 +05:30
return isString(content) && (content === '' || asciiRegex.test(content));
2020-04-08 14:13:33 +05:30
}
2018-11-18 11:00:15 +05:30
export const createPathWithExt = p => {
const ext = p.lastIndexOf('.') >= 0 ? p.substring(p.lastIndexOf('.') + 1) : '';
return `${p.substring(1, p.lastIndexOf('.') + 1 || p.length)}${ext || '.js'}`;
};
2020-05-24 23:13:21 +05:30
export const trimPathComponents = path =>
path
.split('/')
.map(s => s.trim())
.join('/');
export function registerLanguages(def, ...defs) {
2020-07-28 23:09:34 +05:30
defs.forEach(lang => registerLanguages(lang));
2020-05-24 23:13:21 +05:30
const languageId = def.id;
languages.register(def);
languages.setMonarchTokensProvider(languageId, def.language);
languages.setLanguageConfiguration(languageId, def.conf);
}
2020-06-23 00:09:42 +05:30
2020-11-24 15:15:51 +05:30
export function registerSchema(schema) {
const defaults = [languages.json.jsonDefaults, languages.yaml.yamlDefaults];
defaults.forEach(d =>
d.setDiagnosticsOptions({
validate: true,
enableSchemaRequest: true,
hover: true,
completion: true,
schemas: [schema],
}),
);
2020-07-28 23:09:34 +05:30
}
2020-06-23 00:09:42 +05:30
export const otherSide = side => (side === SIDE_RIGHT ? SIDE_LEFT : SIDE_RIGHT);
export function trimTrailingWhitespace(content) {
return content.replace(/[^\S\r\n]+$/gm, '');
}
export function insertFinalNewline(content, eol = '\n') {
return content.slice(-eol.length) !== eol ? `${content}${eol}` : content;
}
export function getPathParents(path, maxDepth = Infinity) {
const pathComponents = path.split('/');
const paths = [];
let depth = 0;
while (pathComponents.length && depth < maxDepth) {
pathComponents.pop();
let parentPath = pathComponents.join('/');
if (parentPath.startsWith('/')) parentPath = parentPath.slice(1);
if (parentPath) paths.push(parentPath);
depth += 1;
}
return paths;
}
export function getPathParent(path) {
return getPathParents(path, 1)[0];
}
/**
* Takes a file object and returns a data uri of its contents.
*
* @param {File} file
*/
export function readFileAsDataURL(file) {
return new Promise(resolve => {
const reader = new FileReader();
reader.addEventListener('load', e => resolve(e.target.result), { once: true });
reader.readAsDataURL(file);
});
}
export function getFileEOL(content = '') {
return content.includes('\r\n') ? 'CRLF' : 'LF';
}