<commit_before>/// <reference types="monaco-editor"/>
/// <reference path="loop-protect.d.ts"/>
/// <reference path="file.ts"/>
/// <reference path="utils.ts"/>

require.config({ paths: { 'vs': 'node_modules/monaco-editor/min/vs' } });

var _editor: monaco.editor.IStandaloneCodeEditor;
var _currentProject: Project;
var _currentFile: SourceFile | null = null;
var _currentHtml: SourceFile;

class extraLibs {

	private static mapExtraLibs: {[name:string]:monaco.IDisposable} = {};

	public static dispose()
	{
		//console.log("DISPOSE LIBS", name);
		for (var libName in this.mapExtraLibs)
			this.mapExtraLibs[libName].dispose();
		this.mapExtraLibs = {};	
	}

	public static add(name:string, content:string)
	{
		if (this.mapExtraLibs[name])
			return;
		//console.log("ADD EXTRA LIB", name);
		var disposable = monaco.languages.typescript.javascriptDefaults.addExtraLib(content, name);
		this.mapExtraLibs[name] = disposable;
	}

	public static remove(name:string)
	{
		var lib = this.mapExtraLibs[name];
		if (lib) {
			lib.dispose();
			delete this.mapExtraLibs[name];
		}		
	}
}


var loadCompletePromise: Promise<any>;

function loadProject(project: Project) {
	console.log()

	const fileContainer = document.getElementById("fileContainer")!;
	fileContainer.innerHTML = "";

	const mapFileElements: { [name: string]: Element } = {};
	let defaultFile: SourceFile | null = null;

	function getNodeElement(node?: SourceFolder): Element {

		if (!node)
			return fileContainer;

		let elt = mapFileElements[node.path];
		if (elt)
			return elt;

		const parentElement = getNodeElement(node.parent);

		const li = parentElement.appendChild(document.createElement("li"));
		li.className = "sourceNode";

		const anchor = li.appendChild(document.createElement("a"));
		anchor.href = "#";

		const icon = anchor.appendChild(document.createElement("i"));
		icon.className = "icon fa fa-" + node.icon;

		if (node instanceof SourceFile) {
			click(anchor, () => {
				loadFile(node);
				return false;
			});
			elt = anchor;

			if (node.path === "index.html")
				defaultFile = node;
		}
		else {
			const children = elt = li.appendChild(document.createElement("ul"));
			children.className = "sourceFolder";

			const icon2 = anchor.appendChild(document.createElement("i"));
			icon2.className = "icon fa fa-folder-open-o";

			click(anchor, () => {
				li.classList.toggle("open");
				return false;
			});
		}

		const text = anchor.appendChild(document.createElement("span"));
		text.textContent = node.name;

		node.element = anchor;

		mapFileElements[node.path] = elt;
		return elt;
	}

	if (_currentProject)
		_currentProject.dispose();

	closeFile();
		
	extraLibs.dispose();
	project.items.forEach(item => getNodeElement(item));
	_currentProject = project;

	if (defaultFile) {
		loadCompletePromise.then(() => {
			_currentHtml = defaultFile!;
			_currentHtml.used = true;
			loadPreview();
		})
	}
}

function closeFile() {

	if (!_currentFile)
		return;

	_currentFile.selected = false;

	const model = _editor.getModel();
	if (model)
	{
		if (model != _currentFile.model)
			console.log("model has changed");<commit_msg>load default file<commit_after>/// <reference types="monaco-editor"/>
/// <reference path="loop-protect.d.ts"/>
/// <reference path="file.ts"/>
/// <reference path="utils.ts"/>

require.config({ paths: { 'vs': 'node_modules/monaco-editor/min/vs' } });

var _editor: monaco.editor.IStandaloneCodeEditor;
var _currentProject: Project;
var _currentFile: SourceFile | null = null;
var _currentHtml: SourceFile;

class extraLibs {

	private static mapExtraLibs: {[name:string]:monaco.IDisposable} = {};

	public static dispose()
	{
		//console.log("DISPOSE LIBS", name);
		for (var libName in this.mapExtraLibs)
			this.mapExtraLibs[libName].dispose();
		this.mapExtraLibs = {};	
	}

	public static add(name:string, content:string)
	{
		if (this.mapExtraLibs[name])
			return;
		//console.log("ADD EXTRA LIB", name);
		var disposable = monaco.languages.typescript.javascriptDefaults.addExtraLib(content, name);
		this.mapExtraLibs[name] = disposable;
	}

	public static remove(name:string)
	{
		var lib = this.mapExtraLibs[name];
		if (lib) {
			lib.dispose();
			delete this.mapExtraLibs[name];
		}		
	}
}


var loadCompletePromise: Promise<any>;

function loadProject(project: Project) {
	console.log()

	const fileContainer = document.getElementById("fileContainer")!;
	fileContainer.innerHTML = "";

	const mapFileElements: { [name: string]: Element } = {};
	let defaultFile: SourceFile | null = null;

	function getNodeElement(node?: SourceFolder): Element {

		if (!node)
			return fileContainer;

		let elt = mapFileElements[node.path];
		if (elt)
			return elt;

		const parentElement = getNodeElement(node.parent);

		const li = parentElement.appendChild(document.createElement("li"));
		li.className = "sourceNode";

		const anchor = li.appendChild(document.createElement("a"));
		anchor.href = "#";

		const icon = anchor.appendChild(document.createElement("i"));
		icon.className = "icon fa fa-" + node.icon;

		if (node instanceof SourceFile) {
			click(anchor, () => {
				loadFile(node);
				return false;
			});
			elt = anchor;

			if (node.path === "index.html")
				defaultFile = node;
		}
		else {
			const children = elt = li.appendChild(document.createElement("ul"));
			children.className = "sourceFolder";

			const icon2 = anchor.appendChild(document.createElement("i"));
			icon2.className = "icon fa fa-folder-open-o";

			click(anchor, () => {
				li.classList.toggle("open");
				return false;
			});
		}

		const text = anchor.appendChild(document.createElement("span"));
		text.textContent = node.name;

		node.element = anchor;

		mapFileElements[node.path] = elt;
		return elt;
	}

	if (_currentProject)
		_currentProject.dispose();

	closeFile();
		
	extraLibs.dispose();
	project.items.forEach(item => getNodeElement(item));
	_currentProject = project;

	if (defaultFile) {
		loadCompletePromise.then(() => {
			_currentHtml = defaultFile!;
			loadFile(_currentHtml);
			_currentHtml.used = true;
			loadPreview();
		})
	}
}

function closeFile() {

	if (!_currentFile)
		return;

	_currentFile.selected = false;

	const model = _editor.getModel();
	if (model)
	{
		if (model != _currentFile.model)
			console.log("model has changed");
{'fraction_non_alphanumeric': 0.150023813303699, 'fraction_numerical': 0.0006350214319733291, 'mean_word_length': 4.185185185185185, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2584773', 'n_tokens_mistral': 2263, 'n_tokens_neox': 2008, 'n_words': 420}