mirror of
https://github.com/ION606/static-site-hosting.git
synced 2026-05-14 22:16:54 +00:00
added better editing
This commit is contained in:
+110
-42
@@ -11,54 +11,74 @@ function formatOption(option) {
|
||||
}
|
||||
|
||||
|
||||
function getAceMode(language) {
|
||||
let aceMode = "ace/mode/html"; // default fallback
|
||||
if (language === "css") {
|
||||
aceMode = "ace/mode/css";
|
||||
} else if (language === "javascript") {
|
||||
aceMode = "ace/mode/javascript";
|
||||
} else if (language === "htmlmixed") {
|
||||
aceMode = "ace/mode/html";
|
||||
}
|
||||
return aceMode;
|
||||
}
|
||||
|
||||
document.addEventListener("DOMContentLoaded", () => {
|
||||
const storedTheme = localStorage.getItem('editortheme');
|
||||
|
||||
// for each textarea (one per file) replace with Ace Editor
|
||||
const editors = Array.from(document.querySelectorAll('textarea')).map((textarea) => {
|
||||
const language = textarea.dataset.language,
|
||||
themeStyle = document.querySelector('#theme-style'),
|
||||
isDark = themeStyle.href.includes('dark-styles.css'),
|
||||
aceTheme = isDark ? "ace/theme/twilight" : "ace/theme/chrome",
|
||||
editorContainer = document.createElement('div');
|
||||
// Initialize Ace editor instance on the container
|
||||
const editor = ace.edit("editor-container");
|
||||
editor.setTheme("ace/theme/chrome");
|
||||
|
||||
editorContainer.style.width = "100%";
|
||||
editorContainer.style.height = "400px";
|
||||
// Default mode as html mixed. It will be updated based on file type.
|
||||
editor.session.setMode("ace/mode/html");
|
||||
|
||||
// insert the container after the textarea
|
||||
textarea.parentNode.insertBefore(editorContainer, textarea.nextSibling);
|
||||
// hide the original textarea
|
||||
textarea.style.display = "none";
|
||||
// To track the current file (index)
|
||||
var currentFileIndex = null;
|
||||
|
||||
const editor = ace.edit(editorContainer);
|
||||
editor.setTheme(storedTheme || aceTheme);
|
||||
// Returns Ace mode based on file extension
|
||||
function getAceMode(filename) {
|
||||
if (filename.endsWith('.css')) return "ace/mode/css";
|
||||
if (filename.endsWith('.js')) return "ace/mode/javascript";
|
||||
return "ace/mode/html";
|
||||
}
|
||||
|
||||
let aceMode = "ace/mode/html"; // default fallback
|
||||
if (language === "css") {
|
||||
aceMode = "ace/mode/css";
|
||||
} else if (language === "javascript") {
|
||||
aceMode = "ace/mode/javascript";
|
||||
} else if (language === "htmlmixed") {
|
||||
aceMode = "ace/mode/html";
|
||||
// When a file card is clicked then load its content into the editor
|
||||
document.querySelectorAll('#file-list .file-card').forEach((card) => {
|
||||
card.addEventListener('click', () => {
|
||||
// Save changes of currently open file
|
||||
if (currentFileIndex !== null) document.getElementById(`textarea-${currentFileIndex}`).value = editor.getValue();
|
||||
currentFileIndex = card.getAttribute("data-index");
|
||||
|
||||
// Get filename and load content from corresponding hidden textarea
|
||||
const filename = card.getAttribute("data-file");
|
||||
editor.setValue(document.getElementById(`textarea-${currentFileIndex}`).value);
|
||||
editor.session.setMode(getAceMode(filename));
|
||||
|
||||
// Show the editor container
|
||||
document.getElementById("editor-container").classList.add("visible");
|
||||
});
|
||||
});
|
||||
|
||||
// Update the corresponding hidden textarea whenever the editor value changes
|
||||
editor.session.on("change", function (e) {
|
||||
if (currentFileIndex !== null) {
|
||||
const ta = document.getElementById("textarea-" + currentFileIndex);
|
||||
ta.value = editor.getValue();
|
||||
}
|
||||
editor.session.setMode(aceMode);
|
||||
});
|
||||
|
||||
// set initial content from the textarea and add options
|
||||
editor.session.setValue(textarea.value.trim());
|
||||
editor.setOptions({
|
||||
fontSize: "14px",
|
||||
tabSize: 4,
|
||||
useSoftTabs: true,
|
||||
wrap: true,
|
||||
showPrintMargin: false
|
||||
// Initialize Select2 for the theme selector
|
||||
$(document).ready(function () {
|
||||
$('#theme-selector').select2({
|
||||
templateResult: formatOption,
|
||||
placeholder: 'Select a theme',
|
||||
allowClear: true
|
||||
}).on('change', function () {
|
||||
const selectedTheme = $('#theme-selector').val();
|
||||
localStorage.setItem('editortheme', selectedTheme);
|
||||
editor.setTheme(selectedTheme);
|
||||
});
|
||||
|
||||
// update the hidden textarea whenever the Ace content changes
|
||||
editor.session.on('change', () => {
|
||||
textarea.value = editor.getValue();
|
||||
});
|
||||
|
||||
return editor;
|
||||
});
|
||||
|
||||
// Retrieve the list of available themes
|
||||
@@ -66,7 +86,7 @@ document.addEventListener("DOMContentLoaded", () => {
|
||||
themes = ThemeList.themes,
|
||||
themeSelector = document.querySelector('#theme-selector');
|
||||
|
||||
themes.forEach(function (theme) {
|
||||
themes.forEach((theme) => {
|
||||
var option = document.createElement('option');
|
||||
option.value = theme.theme;
|
||||
option.textContent = theme.caption;
|
||||
@@ -81,7 +101,55 @@ document.addEventListener("DOMContentLoaded", () => {
|
||||
placeholder: 'Select a theme',
|
||||
allowClear: true
|
||||
}).on('change', function (_) {
|
||||
localStorage.setItem('editortheme', themeSelector.value)
|
||||
editors.forEach(editor => editor.setTheme(themeSelector.value));
|
||||
const selectedTheme = $('#theme-selector').val();
|
||||
localStorage.setItem('editortheme', selectedTheme);
|
||||
editor.setTheme(selectedTheme);
|
||||
});
|
||||
});
|
||||
|
||||
// Add event listener for Ctrl+S to save the current file
|
||||
//TODO: save this
|
||||
const editEl = document.querySelector('#editor-container');
|
||||
|
||||
document.addEventListener('keydown', (e) => {
|
||||
if (e.ctrlKey && e.key === 's') {
|
||||
e.preventDefault();
|
||||
if (currentFileIndex !== null) {
|
||||
document.getElementById("textarea-" + currentFileIndex).value = editor.getValue();
|
||||
document.querySelector("form").submit();
|
||||
}
|
||||
}
|
||||
else if (editEl.contains(e.target)) {
|
||||
document.getElementById("textarea-" + currentFileIndex).value = editor.getValue();
|
||||
}
|
||||
});
|
||||
|
||||
// form submit intercept
|
||||
document.querySelector("form").addEventListener("submit", async (e) => {
|
||||
e.preventDefault();
|
||||
|
||||
const formData = new FormData();
|
||||
document.querySelectorAll("[id^='textarea-']").forEach((textarea) => {
|
||||
if (textarea.dataset.edited === "true") {
|
||||
formData.append(textarea.name, textarea.value);
|
||||
delete textarea.dataset.edited;
|
||||
}
|
||||
});
|
||||
|
||||
await fetch(window.location.href, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"X-Requested-With": "XMLHttpRequest"
|
||||
},
|
||||
body: formData
|
||||
})
|
||||
.then((response) => response.json())
|
||||
.then((data) => {
|
||||
if (data.success) {
|
||||
alert("Site updated successfully!"); // Or display a flash message
|
||||
}
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error("Error:", error);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user