diff options
Diffstat (limited to 'app/assets/javascripts/ide/utils.js')
-rw-r--r-- | app/assets/javascripts/ide/utils.js | 61 |
1 files changed, 50 insertions, 11 deletions
diff --git a/app/assets/javascripts/ide/utils.js b/app/assets/javascripts/ide/utils.js index 1ea2b199237..c28a2bd9f1d 100644 --- a/app/assets/javascripts/ide/utils.js +++ b/app/assets/javascripts/ide/utils.js @@ -1,4 +1,4 @@ -import { commitItemIconMap } from './constants'; +import { SIDE_LEFT, SIDE_RIGHT } from './constants'; import { languages } from 'monaco-editor'; import { flatten } from 'lodash'; @@ -53,16 +53,6 @@ export function isTextFile(content, mimeType, fileName) { return asciiRegex.test(content); } -export const getCommitIconMap = file => { - if (file.deleted) { - return commitItemIconMap.deleted; - } else if (file.tempFile && !file.prevPath) { - return commitItemIconMap.addition; - } - - return commitItemIconMap.modified; -}; - export const createPathWithExt = p => { const ext = p.lastIndexOf('.') >= 0 ? p.substring(p.lastIndexOf('.') + 1) : ''; @@ -84,3 +74,52 @@ export function registerLanguages(def, ...defs) { languages.setMonarchTokensProvider(languageId, def.language); languages.setLanguageConfiguration(languageId, def.conf); } + +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'; +} |