// Storage Editor - Mark Module (mark.js)
// Handles adding smart markers to code selections
(function() {
// Wrap selection with smart marker
function wrapSelectionWithSmartMarker(editor, markerName) {
if (!editor) {
console.error('❌ No editor instance');
return;
}
const selected = editor.getSelectedText();
if (!selected) {
alert("⚠️ Select some text first!");
return;
}
const range = editor.getSelectionRange();
// Get language detection from core module
let subLang = 'text';
let commentStyle = { open: '//', close: '' };
if (window.StorageEditor && typeof StorageEditor.detectSubLanguage === 'function') {
subLang = StorageEditor.detectSubLanguage(editor);
}
if (window.StorageEditor && typeof StorageEditor.getCommentStyleFor === 'function') {
commentStyle = StorageEditor.getCommentStyleFor(subLang);
}
const { open, close } = commentStyle;
let wrapped;
if (close) {
wrapped = `${open}${markerName}<${close}\n${selected}\n${open}${markerName}>${close}`;
} else {
wrapped = `${open}${markerName}<\n${selected}\n${open}${markerName}>`;
}
const wasReadOnly = editor.getReadOnly();
editor.setReadOnly(false);
editor.session.replace(range, wrapped);
editor.setReadOnly(wasReadOnly);
if (typeof showToast === "function") {
showToast(`✅ Marked: ${markerName} (${subLang})`, "success");
}
console.log(`✅ Added marker: ${markerName} in ${subLang} context`);
}
// Prompt user for marker name and add it
function addMarker(editor) {
if (!editor) {
console.error('❌ No editor instance provided');
alert('⚠️ No editor instance');
return;
}
const selected = editor.getSelectedText();
if (!selected) {
alert("⚠️ Select some text first!");
return;
}
const markerName = prompt("Enter marker name (e.g., SCOPE, TARGET, KEEP, NOTE):");
if (markerName && markerName.trim()) {
wrapSelectionWithSmartMarker(editor, markerName.trim().toUpperCase());
}
}
// Quick marker shortcuts
function addQuickMarker(editor, markerName) {
if (!editor) {
console.error('❌ No editor instance provided');
return;
}
wrapSelectionWithSmartMarker(editor, markerName);
}
// Export API
window.StorageEditorMark = {
addMarker,
addQuickMarker,
wrapSelectionWithSmartMarker,
// Convenience methods for common markers
markScope: (editor) => addQuickMarker(editor, 'SCOPE'),
markTarget: (editor) => addQuickMarker(editor, 'TARGET'),
markKeep: (editor) => addQuickMarker(editor, 'KEEP'),
markNote: (editor) => addQuickMarker(editor, 'NOTE'),
markTodo: (editor) => addQuickMarker(editor, 'TODO'),
markFix: (editor) => addQuickMarker(editor, 'FIX')
};
console.log('✅ Storage Editor Mark module loaded');
})();