spotnotes.app/refactor.py

99 lines
4.3 KiB
Python
Raw Normal View History

import os
import re
base_dir = r'z:\source\forgejo\spotnotes.app\static\assets\js'
index_path = os.path.join(base_dir, 'index.js')
with open(index_path, 'r', encoding='utf-8') as f:
content = f.read()
# Remove the blocks that were moved to map.js and sidebar.js
# initMap
content = re.sub(r'function initMap\(\).*?^}', '', content, flags=re.MULTILINE | re.DOTALL)
# initSidebar
content = re.sub(r'function initSidebar\(\).*?^}', '', content, flags=re.MULTILINE | re.DOTALL)
# renderMapMarkers
content = re.sub(r'function renderMapMarkers\(\).*?^}', '', content, flags=re.MULTILINE | re.DOTALL)
# stopTracking
content = re.sub(r'function stopTracking\(\).*?^}', '', content, flags=re.MULTILINE | re.DOTALL)
# updateTrackingUI
content = re.sub(r'function updateTrackingUI\(\).*?^}', '', content, flags=re.MULTILINE | re.DOTALL)
# renderNotepadList
content = re.sub(r'function renderNotepadList\(pads, containerId, isFavorite\).*?^}', '', content, flags=re.MULTILINE | re.DOTALL)
# setupPublicToggle
content = re.sub(r'function setupPublicToggle\(\).*?^}', '', content, flags=re.MULTILINE | re.DOTALL)
# toggleSection
content = re.sub(r'function toggleSection\(event\).*?^}', '', content, flags=re.MULTILINE | re.DOTALL)
# Now, wrap the whole thing or replace globals
# It's easier to rewrite index.js using a structured output or targeted regexes.
# Let's replace the variable declarations:
globals_to_replace = [
'map', 'activeNotes', 'userLocation', 'activePadName', 'activePadId',
'activePadIsPublic', 'activePadOwnerId', 'activePadCanWrite', 'currentUser',
'isTracking', 'userMarker', 'notifiedNoteIds', 'isLoggedIn', 'currentMarkers', 'topZIndex'
]
# Remove the var declarations
for var in globals_to_replace:
content = re.sub(r'let\s+' + var + r'\s*=[^;]+;\n?', '', content)
# Prepend window.SpotNotes. to usages
for var in globals_to_replace:
# Use negative lookbehind/lookahead to avoid matching substring or properties if any
content = re.sub(r'(?<!\w|\.)' + var + r'(?!\w)', f'window.SpotNotes.{var}', content)
# Some function names
functions_to_namespace = [
'loadNotepads', 'loadNotepadsOfType', 'loadNotesForPad', 'loadSingleNote',
'loadAllMyNotes', 'toggleFavorite', 'shareNotepad', 'shareNote',
'deleteNotepad', 'deleteNote', 'reportNote', 'openShareSettingsModal',
'removeCollaborator', 'displaySearchResults', 'escapeHtml'
]
for func in functions_to_namespace:
# replace function definition
content = re.sub(r'async function ' + func + r'\(', f'window.SpotNotes.{func} = async function(', content)
content = re.sub(r'function ' + func + r'\(', f'window.SpotNotes.{func} = function(', content)
content = re.sub(r'window\.' + func + r'\s*=\s*async function\(', f'window.SpotNotes.{func} = async function(', content)
content = re.sub(r'window\.' + func + r'\s*=\s*function\(', f'window.SpotNotes.{func} = function(', content)
# replace usages
content = re.sub(r'(?<!\w|\.)' + func + r'\(', f'window.SpotNotes.{func}(', content)
# also replace usages as callbacks, e.g. .addEventListener('click', loadNotepads)
content = re.sub(r'(?<!\w|\.)' + func + r'(?!\w|\()', f'window.SpotNotes.{func}', content)
# Also fix the initMap / initSidebar / setupPublicToggle usages
for func in ['initMap', 'initSidebar', 'renderMapMarkers', 'stopTracking', 'updateTrackingUI', 'renderNotepadList', 'setupPublicToggle', 'toggleSection']:
content = re.sub(r'(?<!\w|\.)' + func + r'\(', f'window.SpotNotes.{func}(', content)
content = re.sub(r'(?<!\w|\.)' + func + r'(?!\w|\()', f'window.SpotNotes.{func}', content)
# Add initialization block for state
init_block = """window.SpotNotes = window.SpotNotes || {};
// Globals
window.SpotNotes.map = null;
window.SpotNotes.activeNotes = [];
window.SpotNotes.userLocation = null;
window.SpotNotes.activePadName = "";
window.SpotNotes.activePadId = null;
window.SpotNotes.activePadIsPublic = false;
window.SpotNotes.activePadOwnerId = null;
window.SpotNotes.activePadCanWrite = false;
window.SpotNotes.currentUser = null;
window.SpotNotes.isTracking = false;
window.SpotNotes.userMarker = null;
window.SpotNotes.notifiedNoteIds = new Set();
window.SpotNotes.isLoggedIn = false;
window.SpotNotes.currentMarkers = [];
window.SpotNotes.topZIndex = 1000000;
"""
with open(index_path, 'w', encoding='utf-8') as f:
f.write(init_block + content)
print('Refactored index.js')