mirror of
https://github.com/letscontrolit/ESPEasy.git
synced 2026-07-27 19:57:38 +00:00
ColorCode Additions
- Minor correction in the "commandlist“. - Added Android enable confirmation. - Auto format and correct the words in the "commandlist“.
This commit is contained in:
+165
-30
@@ -13,10 +13,10 @@ var commonCommands = ["AccessInfo", "Background", "Build", "ClearAccessBlock", "
|
||||
"WiFiKey2", "WiFiMode", "WiFiScan", "WiFiSSID", "WiFiSSID2", "WiFiSTAMode",
|
||||
"Event", "AsyncEvent",
|
||||
"GPIO", "GPIOToggle", "LongPulse", "LongPulse_mS", "Monitor", "Pulse", "PWM", "Servo", "Status", "Tone", "RTTTL", "UnMonitor",
|
||||
"Provision", "Provision,Config", "Provision,Security", "Provision,Notification", "Provision,Provision", "Provision,Rules,", "Provision,CustomCdnUrl", "Provision,Firmware,", ];
|
||||
"Provision", "Provision,Config", "Provision,Security", "Provision,Notification", "Provision,Provision", "Provision,Rules,", "Provision,CustomCdnUrl", "Provision,Firmware,"];
|
||||
var commonEvents = ["Clock#Time", "Login#Failed", "MQTT#Connected", "MQTT#Disconnected", "MQTTimport#Connected", "MQTTimport#Disconnected", "Rules#Timer", "System#Boot",
|
||||
"System#BootMode", "System#Sleep", "System#Wake", "TaskExit#", "TaskInit#", "ThingspeakReply", "Time#Initialized", "Time#Set", "WiFi#APmodeDisabled", "WiFi#APmodeEnabled",
|
||||
"WiFi#ChangedAccesspoint", "WiFi#ChangedWiFichannel", "WiFi#Connected", "WiFi#Disconnected", "ProvisionFirmware",];
|
||||
"WiFi#ChangedAccesspoint", "WiFi#ChangedWiFichannel", "WiFi#Connected", "WiFi#Disconnected"];
|
||||
var commonPlugins = [
|
||||
//P003
|
||||
"ResetPulseCounter", "SetPulseCounterTotal", "LogPulseStatistic",
|
||||
@@ -200,37 +200,172 @@ var EXTRAWORDS = commonAtoms.concat(commonPlugins, commonKeywords, commonCommand
|
||||
|
||||
var rEdit;
|
||||
function initCM() {
|
||||
CodeMirror.commands.autocomplete = function (cm) { cm.showHint({ hint: CodeMirror.hint.anyword }); }
|
||||
rEdit = CodeMirror.fromTextArea(document.getElementById('rules'), {
|
||||
tabSize: 2, indentWithTabs: false, lineNumbers: true, autoCloseBrackets: true,
|
||||
extraKeys: {
|
||||
'Ctrl-Space': 'autocomplete',
|
||||
Tab: (cm) => {
|
||||
if (cm.getMode().name === 'null') {
|
||||
cm.execCommand('insertTab');
|
||||
} else {
|
||||
if (cm.somethingSelected()) {
|
||||
cm.execCommand('indentMore');
|
||||
} else {
|
||||
cm.execCommand('insertSoftTab');
|
||||
}
|
||||
}
|
||||
},
|
||||
'Shift-Tab': (cm) => cm.execCommand('indentLess'),
|
||||
var confirmR = true
|
||||
var android = /Android/.test(navigator.userAgent);
|
||||
if (android) {
|
||||
if (confirm("Do you want to enable colored rules? (There are some issues with the standard Android Keyboard causing it to fail!)")) {
|
||||
confirmR = true
|
||||
} else {
|
||||
confirmR = false
|
||||
}
|
||||
});
|
||||
rEdit.on('change', function () { rEdit.save() });
|
||||
//hinting on input
|
||||
rEdit.on("inputRead", function (cm, event) {
|
||||
var letters = /[\w%,.]/; //characters for activation
|
||||
var cur = cm.getCursor();
|
||||
var token = cm.getTokenAt(cur);
|
||||
if (letters.test(event.text) && token.type != "comment") {
|
||||
cm.showHint({ completeSingle: false });
|
||||
};
|
||||
});
|
||||
}
|
||||
if (confirmR) {
|
||||
CodeMirror.commands.autocomplete = function (cm) { cm.showHint({ hint: CodeMirror.hint.anyword }); }
|
||||
rEdit = CodeMirror.fromTextArea(document.getElementById('rules'), {
|
||||
tabSize: 2, indentWithTabs: false, lineNumbers: true, autoCloseBrackets: true,
|
||||
extraKeys: {
|
||||
'Ctrl-Space': 'autocomplete',
|
||||
Tab: (cm) => {
|
||||
if (cm.getMode().name === 'null') {
|
||||
cm.execCommand('insertTab');
|
||||
} else {
|
||||
if (cm.somethingSelected()) {
|
||||
cm.execCommand('indentMore');
|
||||
} else {
|
||||
cm.execCommand('insertSoftTab');
|
||||
}
|
||||
}
|
||||
},
|
||||
'Shift-Tab': (cm) => cm.execCommand('indentLess'),
|
||||
}
|
||||
});
|
||||
rEdit.on('change', function () { rEdit.save() });
|
||||
//hinting on input
|
||||
rEdit.on("inputRead", function (cm, event) {
|
||||
var letters = /[\w%,.]/; //characters for activation
|
||||
var cur = cm.getCursor();
|
||||
var token = cm.getTokenAt(cur);
|
||||
if (letters.test(event.text) && token.type != "comment") {
|
||||
cm.showHint({ completeSingle: false });
|
||||
};
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------- add formatting option
|
||||
document.addEventListener('keydown', function (e) {
|
||||
// Ctrl + Shift + F to format
|
||||
if (e.ctrlKey && e.shiftKey && e.key.toLowerCase() === 'f') {
|
||||
e.preventDefault();
|
||||
console.log('Formatting...');
|
||||
initalAutocorrection();
|
||||
|
||||
const textarea = document.getElementById('rules');
|
||||
textarea.value = formatLogic(textarea.value);
|
||||
|
||||
// Clean up previous CodeMirror instances (if any)
|
||||
document.querySelectorAll('div.cm-s-default').forEach(el => el.remove());
|
||||
initCM();
|
||||
}
|
||||
});
|
||||
|
||||
function initalAutocorrection() {
|
||||
const textarea = document.getElementById("rules");
|
||||
let text = textarea.value;
|
||||
|
||||
for (const word of EXTRAWORDS) {
|
||||
if (word === "Do") {
|
||||
const pattern = /(^|\s)(do)(\s*)(\/\/.*)?$/gmi;
|
||||
text = text.replace(pattern, (match, p1, p2, p3, p4) => {
|
||||
return `${p1}Do${p3}${p4 ?? ""}`;
|
||||
});
|
||||
} else {
|
||||
const pattern = new RegExp(`^\\s*\\b${word}\\b`, "gmi");
|
||||
text = text.replace(pattern, (match) => {
|
||||
// Replace the matched word with its corrected casing
|
||||
return match.replace(new RegExp(word, 'i'), word);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
textarea.value = text;
|
||||
}
|
||||
|
||||
function formatLogic(text) {
|
||||
const INDENT = ' '; // 2 spaces
|
||||
const lines = text.split('\n').map(line => line.trim()); // remove all existing indentation
|
||||
|
||||
let indentLevel = 0;
|
||||
const result = [];
|
||||
const stack = [];
|
||||
|
||||
// ignoring case
|
||||
function startsWithKeyword(line, keywords) {
|
||||
line = line.toLowerCase();
|
||||
return keywords.some(k => line.startsWith(k));
|
||||
}
|
||||
|
||||
// We define these keywords:
|
||||
const ON_START = 'on'; // Start of main block
|
||||
const ON_END = 'endon'; // End of main block
|
||||
const IF_START = 'if'; // If block start
|
||||
const ELSE = 'else'; // Else block
|
||||
const IF_END = 'endif'; // Endif
|
||||
|
||||
for (let i = 0; i < lines.length; i++) {
|
||||
const line = lines[i];
|
||||
const low = line.toLowerCase();
|
||||
|
||||
// On ... Endon blocks
|
||||
if (startsWithKeyword(low, [ON_START])) {
|
||||
// main block - no indent for this line
|
||||
result.push(line);
|
||||
indentLevel = 1; // indent inside On ... Endon
|
||||
stack.push(ON_START);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (startsWithKeyword(low, [ON_END])) {
|
||||
// ending main block - decrease indent before printing
|
||||
indentLevel = Math.max(indentLevel - 1, 0);
|
||||
result.push(INDENT.repeat(indentLevel) + line);
|
||||
stack.pop();
|
||||
continue;
|
||||
}
|
||||
|
||||
// If inside main block:
|
||||
if (stack.includes(ON_START)) {
|
||||
// Handle If, Else, Endif blocks inside main block
|
||||
|
||||
if (startsWithKeyword(low, [IF_START])) {
|
||||
// If starts: indent current line, push stack, increase indent
|
||||
result.push(INDENT.repeat(indentLevel) + line);
|
||||
stack.push(IF_START);
|
||||
indentLevel++;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (startsWithKeyword(low, [ELSE])) {
|
||||
// Else: decrease indent (close previous If block), print Else, increase indent again
|
||||
indentLevel = Math.max(indentLevel - 1, 0);
|
||||
result.push(INDENT.repeat(indentLevel) + line);
|
||||
indentLevel++;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (startsWithKeyword(low, [IF_END])) {
|
||||
// Endif: decrease indent, print line, pop stack
|
||||
indentLevel = Math.max(indentLevel - 1, 0);
|
||||
result.push(INDENT.repeat(indentLevel) + line);
|
||||
// pop only if top of stack is If
|
||||
if (stack[stack.length - 1] === IF_START) {
|
||||
stack.pop();
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
// Normal line inside main block or If blocks, indent with current level
|
||||
result.push(INDENT.repeat(indentLevel) + line);
|
||||
continue;
|
||||
}
|
||||
|
||||
// Lines outside any On ... Endon block, print as is (or trim)
|
||||
result.push(line);
|
||||
}
|
||||
|
||||
return result.join('\n');
|
||||
}
|
||||
//--------------------------------------------------------------------------------- end of formatting option
|
||||
|
||||
(function (mod) {
|
||||
if (typeof exports == "object" && typeof module == "object") // CommonJS
|
||||
|
||||
Vendored
+1
-1
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user