From 53dd96c6866f20ea1b41475e28d598a1881d6e58 Mon Sep 17 00:00:00 2001
From: Pierre Ranwez <pierre.ranwez.etu@univ-lille.fr>
Date: Mon, 14 Feb 2022 11:35:34 +0100
Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat:=20midi=20learn=20on=20focused?=
 =?UTF-8?q?,=20cleaning=20useless=20code?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 main.js | 245 +++++++++++++++++++++++---------------------------------
 1 file changed, 100 insertions(+), 145 deletions(-)

diff --git a/main.js b/main.js
index 6f8f126..6d25ebb 100644
--- a/main.js
+++ b/main.js
@@ -137,105 +137,109 @@ function analyseAll() {
 		var all = [];
 		var parser = new CSSParser();
 		var sheet = parser.parse(result.css, false, false);
-		sheet.cssRules.forEach(element => {
-			element.declarations.forEach(css => {
-				var templates = [];
-				cssValue = css.valueText;
-				count = css.valueText.split("[").length;
-				if (count > 1) {
-					for (let index = 0; index < count - 1; index++) {
-						template = cssValue.substring(
-							cssValue.indexOf("[") + 1,
-							cssValue.indexOf("]")
-						);
-						cssValue = cssValue.replace("[" + template + "]", "");
-						var parameters = JSON.parse(result.parameters);
-						parameters.forEach(p => {
-							templateSplited = template.split(' ');
-							if (p[1] == templateSplited[0]) {
-								if (p[4] != "") {
-									p[4].split(' ').forEach(midiE => {
-										var obj = {
-											type: '',
-											name: templateSplited[0],
-											templateFull: template,
-											eventType: '',
-											eventName: '',
-											options: '',
-											array: templateSplited.length > 1 ? (templateSplited[1].includes(';') ? templateSplited[1].split(';') : '') : '',
-											min: templateSplited.length > 1 ? (templateSplited[1].includes(':') ? templateSplited[1].split(':')[0] : '') : '',
-											max: templateSplited.length > 1 ? (templateSplited[1].includes(':') ? templateSplited[1].split(':')[1] : '') : '',
+		if (sheet.cssRules.length > 0) {
+			sheet.cssRules.forEach(element => {
+				if (element.declarations.length > 0) {
+					element.declarations.forEach(css => {
+						var templates = [];
+						cssValue = css.valueText;
+						count = css.valueText.split("[").length;
+						if (count > 1) {
+							for (let index = 0; index < count - 1; index++) {
+								template = cssValue.substring(
+									cssValue.indexOf("[") + 1,
+									cssValue.indexOf("]")
+								);
+								cssValue = cssValue.replace("[" + template + "]", "");
+								var parameters = JSON.parse(result.parameters);
+								parameters.forEach(p => {
+									templateSplited = template.split(' ');
+									if (p[1] == templateSplited[0]) {
+										if (p[4] != "") {
+											p[4].split(' ').forEach(midiE => {
+												var obj = {
+													type: '',
+													name: templateSplited[0],
+													templateFull: template,
+													eventType: '',
+													eventName: '',
+													options: '',
+													array: templateSplited.length > 1 ? (templateSplited[1].includes(';') ? templateSplited[1].split(';') : '') : '',
+													min: templateSplited.length > 1 ? (templateSplited[1].includes(':') ? templateSplited[1].split(':')[0] : '') : '',
+													max: templateSplited.length > 1 ? (templateSplited[1].includes(':') ? templateSplited[1].split(':')[1] : '') : '',
+												}
+												obj.type = 'midi';
+												if (midiE.includes(':')) {
+													obj.eventType = midiE.split(':')[0];
+													obj.eventName = midiE.split(':')[1];
+												}
+												else {
+													obj.eventType = midiE;
+													obj.eventName = '';
+												}
+												obj.options = p[3];
+												obj.array = ((obj.array == '') ? (p[2].includes(';') ? p[2].split(';') : '') : obj.array);
+												obj.min = ((obj.min == '') ? (p[2].includes(':') ? p[2].split(':')[0] : 0) : obj.min);
+												obj.max = ((obj.max == '') ? (p[2].includes(':') ? p[2].split(':')[1] : 1) : obj.max);
+												templates.push(obj);
+											});
 										}
-										obj.type = 'midi';
-										if (midiE.includes(':')) {
-											obj.eventType = midiE.split(':')[0];
-											obj.eventName = midiE.split(':')[1];
+										if (p[5] != "") {
+											p[5].split(' ').forEach(audioE => {
+												var obj = {
+													type: '',
+													name: templateSplited[0],
+													templateFull: template,
+													eventType: '',
+													eventName: '',
+													options: '',
+													array: templateSplited.length > 1 ? (templateSplited[1].includes(';') ? templateSplited[1].split(';') : '') : '',
+													min: templateSplited.length > 1 ? (templateSplited[1].includes(':') ? templateSplited[1].split(':')[0] : '') : '',
+													max: templateSplited.length > 1 ? (templateSplited[1].includes(':') ? templateSplited[1].split(':')[1] : '') : '',
+												}
+												obj.type = 'audio';
+												if (audioE.includes(':')) {
+													obj.eventType = audioE.split(':')[0];
+													obj.eventName = audioE.split(':')[1];
+												}
+												else {
+													obj.eventType = audioE;
+													obj.eventName = '';
+												}
+												obj.options = p[3];
+												obj.array = ((obj.array == '') ? (p[2].includes(';') ? p[2].split(';') : '') : obj.array);
+												obj.min = ((obj.min == '') ? (p[2].includes(':') ? p[2].split(':')[0] : 0) : obj.min);
+												obj.max = ((obj.max == '') ? (p[2].includes(':') ? p[2].split(':')[1] : 1) : obj.max);
+												templates.push(obj);
+											});
 										}
-										else {
-											obj.eventType = midiE;
-											obj.eventName = '';
-										}
-										obj.options = p[3];
-										obj.array = ((obj.array == '') ? (p[2].includes(';') ? p[2].split(';') : '') : obj.array);
-										obj.min = ((obj.min == '') ? (p[2].includes(':') ? p[2].split(':')[0] : 0) : obj.min);
-										obj.max = ((obj.max == '') ? (p[2].includes(':') ? p[2].split(':')[1] : 1) : obj.max);
-										templates.push(obj);
-									});
-								}
-								if (p[5] != "") {
-									p[5].split(' ').forEach(audioE => {
-										var obj = {
-											type: '',
-											name: templateSplited[0],
-											templateFull: template,
-											eventType: '',
-											eventName: '',
-											options: '',
-											array: templateSplited.length > 1 ? (templateSplited[1].includes(';') ? templateSplited[1].split(';') : '') : '',
-											min: templateSplited.length > 1 ? (templateSplited[1].includes(':') ? templateSplited[1].split(':')[0] : '') : '',
-											max: templateSplited.length > 1 ? (templateSplited[1].includes(':') ? templateSplited[1].split(':')[1] : '') : '',
-										}
-										obj.type = 'audio';
-										if (audioE.includes(':')) {
-											obj.eventType = audioE.split(':')[0];
-											obj.eventName = audioE.split(':')[1];
-										}
-										else {
-											obj.eventType = audioE;
-											obj.eventName = '';
-										}
-										obj.options = p[3];
-										obj.array = ((obj.array == '') ? (p[2].includes(';') ? p[2].split(';') : '') : obj.array);
-										obj.min = ((obj.min == '') ? (p[2].includes(':') ? p[2].split(':')[0] : 0) : obj.min);
-										obj.max = ((obj.max == '') ? (p[2].includes(':') ? p[2].split(':')[1] : 1) : obj.max);
-										templates.push(obj);
-									});
-								}
+									}
+								});
+
 							}
+						} else {
+							templates.push({
+								type: 'pure',
+								name: '',
+								templateFull: '',
+								eventType: '',
+								eventName: '',
+								options: '',
+								array: '',
+								min: '',
+								max: '',
+							});
+						}
+						all.push({
+							element: element.mSelectorText,
+							property: css.property,
+							value: css.valueText,
+							templates: templates
 						});
-
-					}
-				} else {
-					templates.push({
-						type: 'pure',
-						name: '',
-						templateFull: '',
-						eventType: '',
-						eventName: '',
-						options: '',
-						array: '',
-						min: '',
-						max: '',
 					});
 				}
-				all.push({
-					element: element.mSelectorText,
-					property: css.property,
-					value: css.valueText,
-					templates: templates
-				});
 			});
-		});
+		}
 		chrome.storage.sync.set({ all: all });
 	});
 }
@@ -305,8 +309,10 @@ function onMessage({ type, data }) {
 		case 'midiEvent': {
 			if (dataMidi != data) {
 				$('#midiEvent').text(data);
-				if (editor.isFocused()) {
-					editor.session.insert(editor.getCursorPosition(), data)
+				var focusedTd = $('[contenteditable]:focus');
+				var focusedCol = $(focusedTd).index('#parameters tr:eq(' + $(focusedTd).parent().index('#parameters tr') + ') td');
+				if (focusedTd.length > 0 && focusedCol == 4) {
+					focused.text(focused.text() + ' ' + data);
 				}
 				dataMidi = data;
 			}
@@ -326,61 +332,11 @@ function onMessage({ type, data }) {
 	}
 }
 
-
-
-
-$(document).keydown(function (event) {
-	// If Control or Command key is pressed and the S key is pressed
-	// run save function. 83 is the key code for S.
-	if ((event.ctrlKey || event.metaKey) && event.which == 83) {
-		// Save Function
-		event.preventDefault();
-		updateCss();
-		return false;
-	}
-}
-);
-$(document).keydown(function (event) {
-	// If Control or Command key is pressed and the S key is pressed
-	// run save function. 83 is the key code for S.
-	if ((event.ctrlKey || event.metaKey) && event.which == 79) {
-		// Save Function
-		event.preventDefault();
-		activateButton = !activateButton;
-		chrome.storage.sync.set({ activate: activateButton }, function () {
-			if (activateButton) {
-				$('#onOff').text('Désactiver');
-				var API = chrome || browser;
-				API.tabs.query({ active: true, currentWindow: true }, function (tabs) {
-					if (tabs[0] && tabs[0].favIconUrl) {
-						chrome.storage.sync.set({ activateFav: tabs[0].favIconUrl });
-					}
-				});
-			} else {
-				$('#onOff').text('Activer');
-			}
-			chrome.tabs.query({ active: true, currentWindow: true },
-				function (tabs) {
-					chrome.tabs.sendMessage(tabs[0].id, { type: 'start', data: activateButton });
-				}
-			);
-		});
-		return false;
-	}
-}
-);
-
 $('#onOff').on('click', function () {
 	activateButton = !activateButton;
 	chrome.storage.sync.set({ activate: activateButton }, function () {
 		if (activateButton) {
 			$('#onOff').addClass('active');
-			var API = chrome || browser;
-			API.tabs.query({ active: true, currentWindow: true }, function (tabs) {
-				if (tabs[0] && tabs[0].favIconUrl) {
-					chrome.storage.sync.set({ activateFav: tabs[0].favIconUrl });
-				}
-			});
 		} else {
 			$('#onOff').removeClass('active');
 		}
@@ -479,7 +435,6 @@ $(document).on('click', '.icon-tabler-minus', function () {
 
 
 
-
 // Wait messages from content script
 chrome.runtime.onMessage.addListener(onMessage);
 // Indicate to Background that the popup is ready
-- 
GitLab