feat: made editor use MIME types

This commit is contained in:
ThinLiquid 2024-01-25 08:30:59 +00:00
parent 0c2ffbbb9f
commit cc3583bfdc
No known key found for this signature in database
GPG key ID: 17538DC3DF6A7387
2 changed files with 70 additions and 22 deletions

View file

@ -12,26 +12,6 @@ interface EditorConfig {
path: string path: string
} }
const fileLanguageMap: {
[key: string]: string
} = {
c: 'clike',
cpp: 'clike',
java: 'clike',
cs: 'clike',
ts: 'typescript',
js: 'javascript',
mjs: 'javascript',
cjs: 'javascript',
jsx: 'jsx',
tsx: 'tsx',
html: 'html',
md: 'markdown',
css: 'css',
xml: 'xml',
py: 'python'
}
const Editor: Process = { const Editor: Process = {
config: { config: {
name: 'Editor', name: 'Editor',
@ -40,6 +20,8 @@ const Editor: Process = {
targetVer: '1.0.0-indev.0' targetVer: '1.0.0-indev.0'
}, },
run: async (process) => { run: async (process) => {
const MIMETypes = await process.loadLibrary('lib/MIMETypes')
if (Object.keys(process.data).length > 0) { if (Object.keys(process.data).length > 0) {
const win = await process.loadLibrary('lib/WindowManager').then((wm: any) => { const win = await process.loadLibrary('lib/WindowManager').then((wm: any) => {
return wm.createWindow({ return wm.createWindow({
@ -153,8 +135,44 @@ const Editor: Process = {
} }
}) })
const fileExtension = data.path.split('.').pop()?.toLowerCase() as string const fileExtension = (data.path.split('.').pop() as string).toLowerCase()
const language = fileLanguageMap[fileExtension] ?? 'text' console.log('owo ' + fileExtension, MIMETypes)
const mime = fileExtension in MIMETypes ? MIMETypes[fileExtension].type : 'text/plain'
let language = 'text'
switch (mime) {
case 'text/markdown':
language = 'markdown'
break
case 'text/css':
language = 'css'
break
case 'text/html':
language = 'html'
break
case 'text/javascript':
language = 'javascript'
break
case 'text/jsx':
language = 'jsx'
break
case 'application/x-flow-theme':
case 'application/json':
language = 'clike'
break
case 'text/typescript':
language = 'typescript'
break
case 'text/tsx':
language = 'tsx'
break
case 'application/python':
language = 'python'
break
default:
language = 'text'
break
}
const value = Buffer.from(await fs.readFile(data.path)).toString() const value = Buffer.from(await fs.readFile(data.path)).toString()
const editor = fullEditor( const editor = fullEditor(

View file

@ -44,6 +44,30 @@ const MIMETypes: Library = {
opensWith: ['apps/ImageViewer'], opensWith: ['apps/ImageViewer'],
icon: 'image' icon: 'image'
}, },
cjs: {
type: 'application/javascript',
description: 'CommonJS Module',
opensWith: ['apps/Editor'],
icon: 'code'
},
htm: {
type: 'text/html',
description: 'HTML Document',
opensWith: ['apps/Editor'],
icon: 'code'
},
html: {
type: 'text/html',
description: 'HTML Document',
opensWith: ['apps/Editor'],
icon: 'code'
},
js: {
type: 'text/javascript',
description: 'JavaScript File',
opensWith: ['apps/Editor'],
icon: 'code'
},
lnk: { lnk: {
type: 'application/x-ms-shortcut', type: 'application/x-ms-shortcut',
description: 'Windows Shortcut', description: 'Windows Shortcut',
@ -56,6 +80,12 @@ const MIMETypes: Library = {
opensWith: ['apps/Editor'], opensWith: ['apps/Editor'],
icon: 'markdown' icon: 'markdown'
}, },
mjs: {
type: 'text/javascript',
description: 'JavaScript Module',
opensWith: ['apps/Editor'],
icon: 'code'
},
mp4: { mp4: {
type: 'video/mp4', type: 'video/mp4',
description: 'MP4 Video', description: 'MP4 Video',