Files
mainsail/src/plugins/helpers.ts
T
Stefan Dej 1a68218267 feat: confirm before closing the browser tab (#723)
when the editor is open and file is changed
2022-03-20 01:45:06 +01:00

271 lines
8.1 KiB
TypeScript

import { FileStateFile } from '@/store/files/types'
import { PrinterStateMacroParams } from '@/store/printer/types'
import {
mdiArrowCollapseVertical,
mdiCodeTags,
mdiConsoleLine,
mdiDipSwitch,
mdiEngine,
mdiGamepad,
mdiInformation,
mdiPrinter3d,
mdiThermometerLines,
mdiWebcam,
} from '@mdi/js'
export const findDirectory = (folder: FileStateFile[], dirArray: string[]): FileStateFile[] | null => {
if (folder !== undefined && folder !== null && dirArray.length) {
const parent = folder?.find((element: FileStateFile) => element.isDirectory && element.filename === dirArray[0])
if (parent) {
dirArray.shift()
if (parent.childrens && dirArray.length) return findDirectory(parent.childrens, dirArray)
else if (parent.childrens) return parent.childrens
}
return folder
}
return null
}
export const caseInsensitiveSort = (values: any[], orderType: string): any[] => {
return values.sort((a, b) => {
const stringA = a[orderType].toLowerCase()
const stringB = b[orderType].toLowerCase()
if (stringA < stringB) return -1
if (stringA > stringB) return 1
return 0
})
}
export const capitalize = (str: string): string => {
return str.charAt(0).toUpperCase() + str.slice(1)
}
export const convertPanelnameToIcon = (name: string): string => {
if (name.startsWith('macrogroup_')) return mdiCodeTags
switch (name) {
case 'webcam':
return mdiWebcam
case 'zoffset':
return mdiArrowCollapseVertical
case 'control':
return mdiGamepad
case 'macros':
return mdiCodeTags
case 'printsettings':
return mdiPrinter3d
case 'miscellaneous':
return mdiDipSwitch
case 'tools':
return mdiThermometerLines
case 'miniconsole':
return mdiConsoleLine
case 'machine-settings':
return mdiEngine
default:
return mdiInformation
}
}
export const camelize = (str: string): string => {
return str
.replace(/_/g, ' ')
.replace(/(?:^\w|[A-Z]|\b\w)/g, (word, index) => {
return index === 0 ? word.toLowerCase() : word.toUpperCase()
})
.replace(/\s+/g, '')
}
export function formatConsoleMessage(message: string): string {
message = message.replace(/!! /g, '')
message = message.replace(/\/\/ /g, '')
message = message.replace('\n// ', '<br>')
message = message.replace(/\r\n|\r|\n/g, '<br>')
//message = message.replaceAll('<br />', '<br>')
//return message.split('<br>');
return message
}
export const convertName = (name: string): string => {
let output = ''
name = name.replace(/_/g, ' ')
name.split(' ').forEach((split) => {
output += ' ' + split.charAt(0).toUpperCase() + split.slice(1)
})
output = output.slice(1)
return output
}
export const formatFilesize = (fileSizeInBytes: number): string => {
let i = -1
const byteUnits = [' kB', ' MB', ' GB', ' TB', ' PB', ' EB', ' ZB', ' YB']
do {
fileSizeInBytes = fileSizeInBytes / 1024
i++
} while (fileSizeInBytes > 1024)
return Math.max(fileSizeInBytes, 0.1).toFixed(1) + byteUnits[i]
}
export const formatDate = (date: number): string => {
const tmp2 = new Date(date)
return tmp2.toLocaleString().replace(',', '')
}
export const formatFrequency = (frequency: number): string => {
let i = -1
const units = [' kHz', ' MHz', ' GHz']
do {
frequency = frequency / 1000
i++
} while (frequency > 1000)
return Math.max(frequency, 0.1).toFixed() + units[i]
}
export const formatPrintTime = (totalSeconds: number): string => {
if (totalSeconds) {
let output = ''
const days = Math.floor(totalSeconds / (3600 * 24))
if (days) {
totalSeconds %= 3600 * 24
output += days + 'd'
}
const hours = Math.floor(totalSeconds / 3600)
totalSeconds %= 3600
if (hours) output += ' ' + hours + 'h'
const minutes = Math.floor(totalSeconds / 60)
if (minutes) output += ' ' + minutes + 'm'
const seconds = totalSeconds % 60
if (seconds) output += ' ' + seconds.toFixed(0) + 's'
return output
}
return '--'
}
export const sortFiles = (items: FileStateFile[] | null, sortBy: string[], sortDesc: boolean[]): FileStateFile[] => {
const sortBySingle = sortBy.length ? sortBy[0] : 'filename'
const sortDescSingle = sortDesc[0]
if (items !== null) {
// Sort by index
items.sort(function (a: any, b: any) {
if (a[sortBySingle] === b[sortBySingle]) return 0
if (a[sortBySingle] === null || a[sortBySingle] === undefined) return -1
if (b[sortBySingle] === null || b[sortBySingle] === undefined) return 1
if (a[sortBySingle].constructor === String && b[sortBySingle].constructor === String) {
return a[sortBySingle].localeCompare(b[sortBySingle], undefined, { sensivity: 'base' })
}
if (a[sortBySingle] instanceof Array && b[sortBySingle] instanceof Array) {
const reducedA = a[sortBySingle].length ? a.filament.reduce((a: any, b: any) => a + b) : 0
const reducedB = b[sortBySingle].length ? b.filament.reduce((a: any, b: any) => a + b) : 0
return reducedA - reducedB
}
return a[sortBySingle] - b[sortBySingle]
})
// Deal with descending order
if (sortDescSingle) items.reverse()
// Then make sure directories come first
items.sort((a: any, b: any) => (a.isDirectory === b.isDirectory ? 0 : a.isDirectory ? -1 : 1))
}
return items ?? []
}
export function strLongestEqual(a: string, b: string): string {
const l = Math.min(a?.length ?? Number.MAX_VALUE, b?.length ?? Number.MAX_VALUE)
let i = 0
while (i < l && (a.charCodeAt(i) ^ b.charCodeAt(i)) === 0) {
i += 1
}
return a.substr(0, i)
}
export function reverseString(str: string): string {
return str === '' ? '' : reverseString(str.substr(1)) + str.charAt(0)
}
export function formatTime(date: Date): string {
let hours: string | number = date.getHours()
if (hours < 10) hours = '0' + hours.toString()
let minutes: string | number = date.getMinutes()
if (minutes < 10) minutes = '0' + minutes.toString()
let seconds: string | number = date.getSeconds()
if (seconds < 10) seconds = '0' + seconds.toString()
return hours + ':' + minutes + ':' + seconds
}
export function getMacroParams(macro: { gcode: string }): PrinterStateMacroParams {
const paramRegex =
/{%?.*?params\.([A-Za-z_0-9]+)(?:\|(int|string|double))?(?:\|default\('?"?(.*?)"?'?\))?(?:\|(int|string))?.*?%?}/
let params = paramRegex.exec(macro.gcode)
let currentMatch = macro.gcode
let ret: PrinterStateMacroParams = null
while (params) {
if (ret === null) {
ret = {}
}
const name = params[1]
const t: 'int' | 'string' | 'double' | null = (params[2] ?? params[4] ?? null) as
| 'int'
| 'string'
| 'double'
| null
const def = params[3] ?? null
ret[`${name}`] = {
type: t,
default: def,
}
currentMatch = currentMatch.replace(params[0], '')
params = paramRegex.exec(currentMatch)
}
const paramInRegex = /{%?.*?if.*?'([A-Za-z_0-9]+)' (?:not )?in params.*?%?}/
params = paramInRegex.exec(macro.gcode)
currentMatch = macro.gcode
while (params) {
if (ret === null) {
ret = {}
}
const name = params[1]
if (!(`${name}` in ret)) {
ret[`${name}`] = {
type: null,
default: null,
}
}
currentMatch = currentMatch.replace(params[0], '')
params = paramInRegex.exec(currentMatch)
}
return ret
}
export function windowBeforeUnloadFunction(e: BeforeUnloadEvent) {
e.preventDefault()
e.returnValue = ''
}