Update file language on renaming.
This commit is contained in:
		@@ -1,17 +1,18 @@
 | 
			
		||||
import { getFileExtention } from '../../utils/helpers';
 | 
			
		||||
import state, { IFile } from '../index';
 | 
			
		||||
 | 
			
		||||
const languageMapping = {
 | 
			
		||||
const languageMapping: Record<string, string | undefined> = {
 | 
			
		||||
  'ts': 'typescript',
 | 
			
		||||
  'js': 'javascript',
 | 
			
		||||
  'md': 'markdown',
 | 
			
		||||
  'c': 'c',
 | 
			
		||||
  'h': 'c',
 | 
			
		||||
  'other': ''
 | 
			
		||||
} /* Initializes empty file to global state */
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export const createNewFile = (name: string) => {
 | 
			
		||||
  const tempName = name.split('.');
 | 
			
		||||
  const fileExt = tempName[tempName.length - 1] || 'other';
 | 
			
		||||
  const emptyFile: IFile = { name, language: languageMapping[fileExt as 'ts' | 'js' | 'md' | 'c' | 'h' | 'other'], content: "" };
 | 
			
		||||
  const ext = getFileExtention(name) || ''
 | 
			
		||||
 | 
			
		||||
  const emptyFile: IFile = { name, language: languageMapping[ext] || '', content: '' };
 | 
			
		||||
  state.files.push(emptyFile);
 | 
			
		||||
  state.active = state.files.length - 1;
 | 
			
		||||
};
 | 
			
		||||
@@ -20,5 +21,8 @@ export const renameFile = (oldName: string, nwName: string) => {
 | 
			
		||||
  const file = state.files.find(file => file.name === oldName)
 | 
			
		||||
  if (!file) throw Error(`No file exists with name ${oldName}`)
 | 
			
		||||
 | 
			
		||||
  const ext = getFileExtention(nwName) || ''
 | 
			
		||||
  const language = languageMapping[ext] || ''
 | 
			
		||||
  file.name = nwName
 | 
			
		||||
  file.language = language
 | 
			
		||||
};
 | 
			
		||||
		Reference in New Issue
	
	Block a user