chahuadev's picture
Upload 51 files
f462b1c verified
// ══════════════════════════════════════════════════════════════════════════════
// Mode Manager - Strict Mode Separation System
// แยกโหมดการทำงานแบบเด็ดขาด ไม่ให้ทับซ้อนกัน
// ══════════════════════════════════════════════════════════════════════════════
import { setDisplayMode } from './presentation-config.js';
/**
* Mode Manager Class
* ควบคุมโหมดการทำงานทั้งหมด แยกอย่างเด็ดขาด
*/
export class ModeManager {
constructor() {
this.currentMode = 'intro';
this.modes = {
intro: null,
presentation: null,
editor: null
};
// Track all active systems
this.activeSystems = {
smartLayout: false,
dragDrop: false,
contentEditable: false,
resizeObserver: false,
keyboardNav: false,
safeZoneBorder: false
};
// System references
this.systems = {
smartLayoutControl: null,
dragDropManager: null,
resizeObserver: null,
eventListeners: []
};
console.log('[ModeManager] Initialized');
}
/**
* Switch to a specific mode (EXCLUSIVE - ปิดโหมดอื่นทั้งหมด)
*/
switchMode(newMode) {
if (!['intro', 'presentation', 'editor'].includes(newMode)) {
console.error(`[ModeManager] Invalid mode: ${newMode}`);
return false;
}
if (this.currentMode === newMode) {
console.log(`[ModeManager] Already in ${newMode} mode`);
return true;
}
console.log(`[ModeManager] SWITCHING: ${this.currentMode}${newMode}`);
// 1. SHUTDOWN current mode completely
this.shutdownMode(this.currentMode);
// 2. UPDATE mode
this.currentMode = newMode;
setDisplayMode(newMode);
// 3. ACTIVATE new mode exclusively
this.activateMode(newMode);
// 4. Dispatch event
window.dispatchEvent(new CustomEvent('modeChanged', {
detail: {
mode: newMode,
timestamp: Date.now()
}
}));
console.log(`[ModeManager] Now in ${newMode} mode (ALL OTHER MODES DISABLED)`);
return true;
}
/**
* Shutdown a mode completely (ปิดทุกระบบในโหมดนั้น)
*/
shutdownMode(mode) {
console.log(`[ModeManager] Shutting down ${mode} mode...`);
switch (mode) {
case 'intro':
this.shutdownIntroMode();
break;
case 'presentation':
this.shutdownPresentationMode();
break;
case 'editor':
this.shutdownEditorMode();
break;
}
console.log(`[ModeManager] ${mode} mode shut down completely`);
}
/**
* Activate a mode exclusively (เปิดเฉพาะระบบในโหมดนั้น)
*/
activateMode(mode) {
console.log(`[ModeManager] Activating ${mode} mode...`);
switch (mode) {
case 'intro':
this.activateIntroMode();
break;
case 'presentation':
this.activatePresentationMode();
break;
case 'editor':
this.activateEditorMode();
break;
}
console.log(`[ModeManager] ${mode} mode activated exclusively`);
}
// ═══════════════════════════════════════════════════════════════════════
// INTRO MODE - เฉพาะหน้า Intro
// ═══════════════════════════════════════════════════════════════════════
activateIntroMode() {
// Intro mode: ไม่มีระบบอะไรทำงานเลย เป็นหน้าคงที่
this.disableAllSystems();
// ปิด Smart Layout อย่างเด็ดขาด
if (window.smartLayoutControl) {
window.smartLayoutControl.detach();
}
// ซ่อน UI ของโหมดอื่นทั้งหมด
this.hideAllModeUI();
// แสดงเฉพาะ intro scene
const scenes = document.querySelectorAll('[data-scene]');
scenes.forEach(scene => {
if (scene.dataset.scene === 'intro') {
scene.classList.add('active');
} else {
scene.classList.remove('active');
}
});
console.log('[ModeManager] Intro mode: Static display only (no systems)');
}
shutdownIntroMode() {
// Intro ไม่มีอะไรต้องปิด
console.log('[ModeManager] Intro mode: No systems to shutdown');
}
// ═══════════════════════════════════════════════════════════════════════
// PRESENTATION MODE - สำหรับนำเสนอเท่านั้น
// ═══════════════════════════════════════════════════════════════════════
activatePresentationMode() {
// ปิดทุกระบบก่อน
this.disableAllSystems();
// เปิดเฉพาะ Keyboard Navigation
this.activeSystems.keyboardNav = true;
// เปิด Smart Layout เฉพาะ Presentation Mode
this.activeSystems.smartLayout = true;
// Reattach Smart Layout observers สำหรับ Presentation
if (window.smartLayoutControl) {
this.systems.smartLayoutControl = window.smartLayoutControl;
window.smartLayoutControl.reattach();
}
// ปิด Safe Zone Border
this.activeSystems.safeZoneBorder = false;
// แสดงเฉพาะ UI ของ Presentation Mode
this.showPresentationUI();
console.log('[ModeManager] Presentation mode: Keyboard nav + Smart Layout ENABLED');
}
shutdownPresentationMode() {
// ปิด Keyboard Navigation
this.activeSystems.keyboardNav = false;
// ปิด Smart Layout และ detach observers
this.activeSystems.smartLayout = false;
if (window.smartLayoutControl) {
window.smartLayoutControl.detach();
}
// ✅ ซ่อน UI ของ Presentation Mode
this.hideAllModeUI();
console.log('[ModeManager] Presentation mode: Shut down');
}
// ═══════════════════════════════════════════════════════════════════════
// EDITOR MODE - สำหรับแก้ไข/ลากย้ายเท่านั้น
// ═══════════════════════════════════════════════════════════════════════
activateEditorMode() {
// ปิดทุกระบบก่อน
this.disableAllSystems();
// เปิด Content Editable
this.activeSystems.contentEditable = true;
// เปิด Drag & Drop
this.activeSystems.dragDrop = true;
// เปิด Safe Zone Border (กรอบสีแดง)
this.activeSystems.safeZoneBorder = true;
this.showSafeZoneBorder();
// ปิด Smart Layout อย่างเด็ดขาด (ห้ามทำงานใน Editor Mode)
this.activeSystems.smartLayout = false;
if (window.smartLayoutControl) {
window.smartLayoutControl.detach();
}
// ปิด Resize Observer
this.activeSystems.resizeObserver = false;
// แสดงเฉพาะ UI ของ Editor Mode
this.showEditorUI();
console.log('[ModeManager] Editor mode: Edit/Drag only (Smart Layout DISABLED)');
}
shutdownEditorMode() {
// ปิด Content Editable
this.activeSystems.contentEditable = false;
this.disableContentEditable();
// ปิด Drag & Drop
this.activeSystems.dragDrop = false;
// ปิด Safe Zone Border
this.activeSystems.safeZoneBorder = false;
this.hideSafeZoneBorder();
// ✅ ซ่อน UI ของ Editor Mode
this.hideAllModeUI();
console.log('[ModeManager] Editor mode: Shut down');
}
// ═══════════════════════════════════════════════════════════════════════
// SYSTEM CONTROL - ควบคุมระบบย่อยๆ
// ═══════════════════════════════════════════════════════════════════════
/**
* ปิดทุกระบบ (ไม่ต้อง detach Smart Layout เพราะแต่ละโหมดจัดการเอง)
*/
disableAllSystems() {
// ไม่ detach Smart Layout ที่นี่ ให้แต่ละโหมดจัดการเอง
// ปิด Content Editable
this.disableContentEditable();
// ซ่อน Safe Zone Border
this.hideSafeZoneBorder();
// รีเซ็ต active systems
Object.keys(this.activeSystems).forEach(key => {
this.activeSystems[key] = false;
});
console.log('[ModeManager] All systems disabled (except Smart Layout - handled per mode)');
}
/**
* ปิด Content Editable ทุกอัน
*/
disableContentEditable() {
const editables = document.querySelectorAll('[contenteditable="true"]');
editables.forEach(el => {
el.contentEditable = false;
el.blur();
});
// ลบ class editable
const stage = document.getElementById('stage');
if (stage) {
stage.classList.remove('slide-editable');
}
}
/**
* ซ่อน Safe Zone Border
*/
hideSafeZoneBorder() {
const safeZone = document.querySelector('.safe-zone-overlay');
if (safeZone) {
safeZone.classList.add('hidden');
}
}
/**
* แสดง Safe Zone Border
*/
showSafeZoneBorder() {
const safeZone = document.querySelector('.safe-zone-overlay');
if (safeZone) {
safeZone.classList.remove('hidden');
}
}
/**
* Get current mode
*/
getCurrentMode() {
return this.currentMode;
}
/**
* Check if specific system is active
*/
isSystemActive(systemName) {
return this.activeSystems[systemName] || false;
}
/**
* Get all active systems
*/
getActiveSystems() {
return Object.entries(this.activeSystems)
.filter(([key, value]) => value)
.map(([key]) => key);
}
/**
* Register system reference
*/
registerSystem(name, reference) {
this.systems[name] = reference;
console.log(`[ModeManager] Registered system: ${name}`);
}
// ═══════════════════════════════════════════════════════════════════════
// UI CONTROL - ควบคุม UI elements ของแต่ละโหมด
// ═══════════════════════════════════════════════════════════════════════
/**
* ซ่อน UI ของทุกโหมด
*/
hideAllModeUI() {
// Presentation Mode UI
const navControls = document.getElementById('navControls');
const exitPresentationBtn = document.getElementById('exitPresentationBtn');
const fullscreenBtn = document.getElementById('fullscreenBtn');
const editSlideBtn = document.getElementById('editSlideBtn');
if (navControls) navControls.classList.add('hidden');
if (exitPresentationBtn) exitPresentationBtn.classList.add('hidden');
if (fullscreenBtn) fullscreenBtn.classList.add('hidden');
if (editSlideBtn) editSlideBtn.classList.add('hidden');
// Editor Mode UI
const saveSlideBtn = document.getElementById('saveSlideBtn');
const cancelEditBtn = document.getElementById('cancelEditBtn');
if (saveSlideBtn) saveSlideBtn.classList.add('hidden');
if (cancelEditBtn) cancelEditBtn.classList.add('hidden');
console.log('[ModeManager] All mode UI hidden');
}
/**
* แสดง UI ของ Presentation Mode เท่านั้น
*/
showPresentationUI() {
this.hideAllModeUI();
const navControls = document.getElementById('navControls');
const exitPresentationBtn = document.getElementById('exitPresentationBtn');
const fullscreenBtn = document.getElementById('fullscreenBtn');
const editSlideBtn = document.getElementById('editSlideBtn');
if (navControls) navControls.classList.remove('hidden');
if (exitPresentationBtn) exitPresentationBtn.classList.remove('hidden');
if (fullscreenBtn) fullscreenBtn.classList.remove('hidden');
if (editSlideBtn) editSlideBtn.classList.remove('hidden');
console.log('[ModeManager] Presentation UI shown exclusively');
}
/**
* แสดง UI ของ Editor Mode เท่านั้น
*/
showEditorUI() {
this.hideAllModeUI();
const saveSlideBtn = document.getElementById('saveSlideBtn');
const cancelEditBtn = document.getElementById('cancelEditBtn');
const exitPresentationBtn = document.getElementById('exitPresentationBtn');
if (saveSlideBtn) saveSlideBtn.classList.remove('hidden');
if (cancelEditBtn) cancelEditBtn.classList.remove('hidden');
if (exitPresentationBtn) exitPresentationBtn.classList.remove('hidden');
console.log('[ModeManager] Editor UI shown exclusively');
}
/**
* Force shutdown all (emergency)
*/
emergencyShutdown() {
console.warn('[ModeManager] EMERGENCY SHUTDOWN');
this.disableAllSystems();
// Stop all observers
if (this.systems.resizeObserver) {
this.systems.resizeObserver.disconnect();
}
// Remove all event listeners
this.systems.eventListeners.forEach(({ element, event, handler }) => {
element.removeEventListener(event, handler);
});
this.systems.eventListeners = [];
console.log('[ModeManager] Emergency shutdown complete');
}
}
// Singleton instance
let modeManagerInstance = null;
/**
* Get Mode Manager instance (Singleton)
*/
export function getModeManager() {
if (!modeManagerInstance) {
modeManagerInstance = new ModeManager();
}
return modeManagerInstance;
}
/**
* Quick access functions
*/
export function switchToIntroMode() {
return getModeManager().switchMode('intro');
}
export function switchToPresentationMode() {
return getModeManager().switchMode('presentation');
}
export function switchToEditorMode() {
return getModeManager().switchMode('editor');
}
export default ModeManager;