|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| import { DragDropManager } from './drag-drop-manager.js';
|
| import { PersistentStorage } from '../../shared/persistent-storage.js';
|
| import { ContentFingerprint } from '../../shared/content-fingerprint.js';
|
| import { setDisplayMode, getDisplayMode } from './presentation-config.js';
|
| import {
|
| switchToEditorMode,
|
| switchToPresentationMode,
|
| getModeManager
|
| } from './mode-manager.js';
|
|
|
| |
| |
| |
|
|
| export class SlideEditor {
|
| constructor() {
|
| this.isEditMode = false;
|
| this.isDragMode = false;
|
| this.currentSlideIndex = -1;
|
| this.currentFilePath = null;
|
| this.slideOverrides = new Map();
|
| this.originalContent = null;
|
| this.hasUnsavedChanges = false;
|
|
|
|
|
| this.editBtn = document.getElementById('editSlideBtn');
|
| this.saveBtn = document.getElementById('saveSlideBtn');
|
| this.cancelBtn = document.getElementById('cancelEditBtn');
|
| this.stage = document.getElementById('stage');
|
|
|
|
|
| this.positionControls = document.getElementById('positionControls');
|
| this.toggleSafeZoneBtn = document.getElementById('toggleSafeZoneBtn');
|
| this.toggleSnapGridBtn = document.getElementById('toggleSnapGridBtn');
|
| this.resetPositionsBtn = document.getElementById('resetPositionsBtn');
|
| this.exportSettingsBtn = document.getElementById('exportSettingsBtn');
|
| this.positionIndicator = document.getElementById('positionIndicator');
|
|
|
|
|
| this.dragDropManager = new DragDropManager();
|
| this.persistentStorage = new PersistentStorage();
|
|
|
| this.init();
|
| }
|
|
|
| |
| |
|
|
| init() {
|
| if (!this.editBtn || !this.saveBtn || !this.cancelBtn) {
|
| console.warn('[SlideEditor] Required buttons not found');
|
| return;
|
| }
|
|
|
|
|
| this.editBtn.addEventListener('click', () => this.toggleEditMode());
|
| this.saveBtn.addEventListener('click', () => this.saveChanges());
|
| this.cancelBtn.addEventListener('click', () => this.cancelEdit());
|
|
|
|
|
| if (this.toggleSafeZoneBtn) {
|
| this.toggleSafeZoneBtn.addEventListener('click', () => this.toggleSafeZone());
|
| }
|
| if (this.toggleSnapGridBtn) {
|
| this.toggleSnapGridBtn.addEventListener('click', () => this.toggleSnapGrid());
|
| }
|
| if (this.resetPositionsBtn) {
|
| this.resetPositionsBtn.addEventListener('click', () => this.resetAllPositions());
|
| }
|
| if (this.exportSettingsBtn) {
|
| this.exportSettingsBtn.addEventListener('click', () => this.exportAllSettings());
|
| }
|
|
|
|
|
| document.addEventListener('keydown', (e) => {
|
| if (!this.isEditMode) return;
|
|
|
|
|
| if ((e.ctrlKey || e.metaKey) && e.key === 's') {
|
| e.preventDefault();
|
| this.saveChanges();
|
| }
|
|
|
|
|
| if (e.key === 'Escape') {
|
| e.preventDefault();
|
| this.cancelEdit();
|
| }
|
| });
|
|
|
| console.log('[SlideEditor] Initialized');
|
| }
|
|
|
| |
| |
|
|
| showEditButton() {
|
| if (this.editBtn) {
|
| this.editBtn.classList.remove('hidden');
|
| }
|
| }
|
|
|
| |
| |
|
|
| hideEditButton() {
|
| if (this.editBtn) {
|
| this.editBtn.classList.add('hidden');
|
| }
|
| this.exitEditMode();
|
| }
|
|
|
| |
| |
|
|
| setCurrentSlide(index) {
|
| this.currentSlideIndex = index;
|
| }
|
|
|
| |
| |
|
|
| toggleEditMode() {
|
| if (this.isEditMode) {
|
| this.exitEditMode();
|
| } else {
|
| this.enterEditMode();
|
| }
|
| }
|
|
|
| |
| |
|
|
| |
| |
|
|
| enterEditMode() {
|
| if (this.currentSlideIndex < 0) {
|
| console.warn('[SlideEditor] No slide selected');
|
| return;
|
| }
|
|
|
|
|
| switchToEditorMode();
|
|
|
| this.isEditMode = true;
|
| this.hasUnsavedChanges = false;
|
|
|
|
|
| const activeScene = this.stage.querySelector('.scene.active');
|
| if (!activeScene) {
|
| console.warn('[SlideEditor] No active scene found');
|
| return;
|
| }
|
|
|
| this.originalContent = activeScene.cloneNode(true);
|
|
|
|
|
|
|
|
|
| this.makeContentEditable(activeScene);
|
|
|
|
|
| this.stage.classList.add('slide-editable');
|
| this.editBtn.classList.add('active');
|
| this.showEditIndicator();
|
|
|
|
|
|
|
| console.log(`[SlideEditor] Edit mode entered for slide ${this.currentSlideIndex}`);
|
| }
|
|
|
| |
| |
|
|
| async exitEditMode() {
|
| if (!this.isEditMode) return;
|
|
|
|
|
| if (this.hasUnsavedChanges) {
|
| const result = await this.showExitConfirmation('Edit Mode', 'You have unsaved changes.');
|
|
|
| if (result === 'cancel') {
|
|
|
| return;
|
| } else if (result === 'save') {
|
|
|
| await this.saveCurrentSlide();
|
| }
|
|
|
| }
|
|
|
| this.isEditMode = false;
|
|
|
|
|
| switchToPresentationMode();
|
|
|
|
|
| const activeScene = this.stage.querySelector('.scene.active');
|
| if (activeScene) {
|
| this.makeContentNonEditable(activeScene);
|
| }
|
|
|
|
|
| this.stage.classList.remove('slide-editable');
|
| this.editBtn.classList.remove('active');
|
| this.hideEditIndicator();
|
| this.hideSaveButtons();
|
|
|
|
|
|
|
| this.originalContent = null;
|
| this.hasUnsavedChanges = false;
|
|
|
| console.log('[SlideEditor] Edit mode exited');
|
| }
|
|
|
| |
| |
| |
| |
| |
|
|
| async showExitConfirmation(modeName, message = '') {
|
| return new Promise((resolve) => {
|
| const dialog = document.createElement('div');
|
| dialog.className = 'exit-confirmation-dialog';
|
| dialog.innerHTML = `
|
| <div class="exit-confirmation-overlay"></div>
|
| <div class="exit-confirmation-content">
|
| <div class="exit-confirmation-header">
|
| <h3>Exit ${modeName}?</h3>
|
| </div>
|
| <div class="exit-confirmation-body">
|
| <p class="warning-text">${message}</p>
|
| <p>Do you want to save your changes?</p>
|
| </div>
|
| <div class="exit-confirmation-footer">
|
| <button class="btn-exit-save" data-action="save">
|
| Save & Exit
|
| </button>
|
| <button class="btn-exit-discard" data-action="discard">
|
| Discard Changes
|
| </button>
|
| <button class="btn-exit-cancel" data-action="cancel">
|
| Cancel
|
| </button>
|
| </div>
|
| </div>
|
| `;
|
|
|
| document.body.appendChild(dialog);
|
|
|
|
|
| dialog.querySelectorAll('button').forEach(btn => {
|
| btn.addEventListener('click', () => {
|
| const action = btn.dataset.action;
|
| document.body.removeChild(dialog);
|
| resolve(action);
|
| });
|
| });
|
|
|
|
|
| const escHandler = (e) => {
|
| if (e.key === 'Escape') {
|
| document.body.removeChild(dialog);
|
| document.removeEventListener('keydown', escHandler);
|
| resolve('cancel');
|
| }
|
| };
|
| document.addEventListener('keydown', escHandler);
|
| });
|
| }
|
|
|
| |
| |
|
|
| makeContentEditable(scene) {
|
|
|
| const editableElements = scene.querySelectorAll('h1, h2, h3, h4, h5, h6, p, li, blockquote');
|
|
|
| editableElements.forEach((el, index) => {
|
| el.setAttribute('contenteditable', 'true');
|
| el.setAttribute('data-editable-id', `edit-${index}`);
|
| el.setAttribute('data-placeholder', 'Click to edit...');
|
|
|
|
|
| el.addEventListener('input', () => this.onContentChange());
|
| el.addEventListener('blur', () => this.onContentChange());
|
| });
|
| }
|
|
|
| |
| |
|
|
| makeContentNonEditable(scene) {
|
| const editableElements = scene.querySelectorAll('[contenteditable="true"]');
|
|
|
| editableElements.forEach(el => {
|
| el.removeAttribute('contenteditable');
|
| el.removeAttribute('data-editable-id');
|
| el.removeAttribute('data-placeholder');
|
| });
|
| }
|
|
|
| |
| |
|
|
| onContentChange() {
|
| if (!this.hasUnsavedChanges) {
|
| this.hasUnsavedChanges = true;
|
| this.showSaveButtons();
|
| }
|
| }
|
|
|
| |
| |
|
|
| saveChanges() {
|
| if (!this.isEditMode || this.currentSlideIndex < 0) return;
|
|
|
| const activeScene = this.stage.querySelector('.scene.active');
|
| if (!activeScene) return;
|
|
|
|
|
| const editedContent = this.extractEditedContent(activeScene);
|
|
|
|
|
| this.slideOverrides.set(this.currentSlideIndex, {
|
| timestamp: Date.now(),
|
| content: editedContent,
|
| html: activeScene.innerHTML
|
| });
|
|
|
| console.log(`[SlideEditor] Saved changes for slide ${this.currentSlideIndex}`, editedContent);
|
|
|
|
|
| this.exitEditMode();
|
|
|
|
|
| this.showNotification('✓ Slide saved', 'success');
|
| }
|
|
|
| |
| |
|
|
| cancelEdit() {
|
| if (!this.isEditMode || !this.originalContent) return;
|
|
|
| const activeScene = this.stage.querySelector('.scene.active');
|
| if (activeScene && this.originalContent) {
|
|
|
| activeScene.parentNode.replaceChild(this.originalContent.cloneNode(true), activeScene);
|
|
|
|
|
| const restoredScene = this.stage.querySelector('.scene[data-scene]');
|
| if (restoredScene) {
|
| restoredScene.classList.add('active');
|
| }
|
| }
|
|
|
| this.exitEditMode();
|
| this.showNotification('✗ Changes cancelled', 'warning');
|
| }
|
|
|
| |
| |
|
|
| extractEditedContent(scene) {
|
| const content = {};
|
|
|
|
|
| const editables = scene.querySelectorAll('[data-editable-id]');
|
| editables.forEach(el => {
|
| const id = el.getAttribute('data-editable-id');
|
| content[id] = {
|
| tag: el.tagName.toLowerCase(),
|
| text: el.textContent.trim(),
|
| html: el.innerHTML
|
| };
|
| });
|
|
|
| return content;
|
| }
|
|
|
| |
| |
|
|
| applyOverrides(slideIndex, scene) {
|
| if (!this.slideOverrides.has(slideIndex)) return false;
|
|
|
| const override = this.slideOverrides.get(slideIndex);
|
|
|
|
|
| if (override.html) {
|
| scene.innerHTML = override.html;
|
| console.log(`[SlideEditor] Applied overrides to slide ${slideIndex}`);
|
| return true;
|
| }
|
|
|
| return false;
|
| }
|
|
|
| |
| |
|
|
| hasOverrides(slideIndex) {
|
| return this.slideOverrides.has(slideIndex);
|
| }
|
|
|
| |
| |
|
|
| getAllOverrides() {
|
| return Array.from(this.slideOverrides.entries()).map(([index, data]) => ({
|
| slideIndex: index,
|
| ...data
|
| }));
|
| }
|
|
|
| |
| |
|
|
| clearOverride(slideIndex) {
|
| this.slideOverrides.delete(slideIndex);
|
| }
|
|
|
| |
| |
|
|
| clearAllOverrides() {
|
| this.slideOverrides.clear();
|
| }
|
|
|
| |
| |
|
|
| exportOverrides() {
|
| const data = this.getAllOverrides();
|
| return JSON.stringify(data, null, 2);
|
| }
|
|
|
| |
| |
|
|
| importOverrides(jsonString) {
|
| try {
|
| const data = JSON.parse(jsonString);
|
| data.forEach(item => {
|
| this.slideOverrides.set(item.slideIndex, {
|
| timestamp: item.timestamp,
|
| content: item.content,
|
| html: item.html
|
| });
|
| });
|
| console.log(`[SlideEditor] Imported ${data.length} overrides`);
|
| return true;
|
| } catch (error) {
|
| console.error('[SlideEditor] Failed to import overrides:', error);
|
| return false;
|
| }
|
| }
|
|
|
| |
| |
|
|
| showEditIndicator() {
|
| let indicator = document.getElementById('editModeIndicator');
|
| if (!indicator) {
|
| indicator = document.createElement('div');
|
| indicator.id = 'editModeIndicator';
|
| indicator.className = 'edit-mode-indicator';
|
| indicator.textContent = '✎ Edit Mode';
|
| this.stage.appendChild(indicator);
|
| }
|
| }
|
|
|
| |
| |
|
|
| hideEditIndicator() {
|
| const indicator = document.getElementById('editModeIndicator');
|
| if (indicator) {
|
| indicator.remove();
|
| }
|
| }
|
|
|
| |
| |
|
|
| showSaveButtons() {
|
| if (this.saveBtn) this.saveBtn.classList.remove('hidden');
|
| if (this.cancelBtn) this.cancelBtn.classList.remove('hidden');
|
| }
|
|
|
| |
| |
|
|
| hideSaveButtons() {
|
| if (this.saveBtn) this.saveBtn.classList.add('hidden');
|
| if (this.cancelBtn) this.cancelBtn.classList.add('hidden');
|
| }
|
|
|
| |
| |
|
|
| showNotification(message, type = 'info') {
|
|
|
| const notification = document.createElement('div');
|
| notification.className = `slide-edit-notification notification-${type}`;
|
| notification.textContent = message;
|
| notification.style.cssText = `
|
| position: fixed;
|
| top: 5rem;
|
| left: 50%;
|
| transform: translateX(-50%);
|
| z-index: 1000;
|
| background: ${type === 'success' ? 'rgba(16, 185, 129, 0.95)' :
|
| type === 'warning' ? 'rgba(245, 158, 11, 0.95)' :
|
| 'rgba(59, 130, 246, 0.95)'};
|
| color: white;
|
| padding: 0.75rem 1.5rem;
|
| border-radius: 0.5rem;
|
| font-weight: 600;
|
| box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
|
| animation: slideDown 0.3s ease-out;
|
| `;
|
|
|
| document.body.appendChild(notification);
|
|
|
|
|
| setTimeout(() => {
|
| notification.style.animation = 'slideUp 0.3s ease-out';
|
| setTimeout(() => notification.remove(), 300);
|
| }, 3000);
|
| }
|
|
|
| |
| |
|
|
| reset() {
|
| this.exitEditMode();
|
| this.exitDragMode();
|
| this.currentSlideIndex = -1;
|
| this.hideEditButton();
|
| this.hidePositionControls();
|
| }
|
|
|
|
|
|
|
|
|
|
|
| |
| |
|
|
| |
| |
|
|
| enterDragMode() {
|
| const activeScene = this.stage?.querySelector('.scene.active');
|
| if (!activeScene) return;
|
|
|
|
|
| switchToEditorMode();
|
|
|
| this.isDragMode = true;
|
|
|
|
|
|
|
| this.dragDropManager.enableDragging(activeScene);
|
| this.showPositionControls();
|
|
|
|
|
| if (this.editBtn) {
|
| this.editBtn.style.background = 'rgba(139, 92, 246, 0.9)';
|
| }
|
|
|
| this.showNotification('⊕ Drag Mode - ลากเพื่อย้ายตำแหน่งบรรทัด', 'info');
|
| }
|
|
|
| |
| |
|
|
| exitDragMode() {
|
| const activeScene = this.stage?.querySelector('.scene.active');
|
| if (activeScene) {
|
| this.dragDropManager.disableDragging(activeScene);
|
| }
|
|
|
|
|
| switchToPresentationMode();
|
|
|
| this.isDragMode = false;
|
| this.hidePositionControls();
|
|
|
|
|
|
|
|
|
| if (this.editBtn) {
|
| this.editBtn.style.background = '';
|
| }
|
| }
|
|
|
| |
| |
|
|
| toggleDragMode() {
|
| if (this.isDragMode) {
|
| this.exitDragMode();
|
| } else {
|
|
|
| if (this.isEditMode) {
|
| this.exitEditMode();
|
| }
|
| this.enterDragMode();
|
| }
|
| }
|
|
|
| |
| |
|
|
| showPositionControls() {
|
| if (this.positionControls) {
|
| this.positionControls.classList.remove('hidden');
|
| }
|
| }
|
|
|
| |
| |
|
|
| hidePositionControls() {
|
| if (this.positionControls) {
|
| this.positionControls.classList.add('hidden');
|
| }
|
| }
|
|
|
| |
| |
|
|
| toggleSafeZone() {
|
| const isActive = this.dragDropManager.showSafeZone;
|
| this.dragDropManager.toggleSafeZone(!isActive);
|
|
|
| if (this.toggleSafeZoneBtn) {
|
| if (!isActive) {
|
| this.toggleSafeZoneBtn.classList.add('active');
|
| this.showNotification('✓ Safe Zone เปิด', 'success');
|
| } else {
|
| this.toggleSafeZoneBtn.classList.remove('active');
|
| this.showNotification('✗ Safe Zone ปิด', 'info');
|
| }
|
| }
|
| }
|
|
|
| |
| |
|
|
| toggleSnapGrid() {
|
| const isActive = this.dragDropManager.snapToGrid;
|
| this.dragDropManager.toggleSnapToGrid(!isActive);
|
|
|
| if (this.toggleSnapGridBtn) {
|
| if (!isActive) {
|
| this.toggleSnapGridBtn.classList.add('active');
|
| this.showNotification('✓ Snap to Grid เปิด', 'success');
|
| } else {
|
| this.toggleSnapGridBtn.classList.remove('active');
|
| this.showNotification('✗ Snap to Grid ปิด', 'info');
|
| }
|
| }
|
| }
|
|
|
| |
| |
|
|
| resetAllPositions() {
|
| if (!confirm('รีเซ็ตตำแหน่งและการแก้ไขทั้งหมดของสไลด์นี้?\n(การตั้งค่าจะถูกลบถาวร)')) {
|
| return;
|
| }
|
|
|
| const activeScene = this.stage?.querySelector('.scene.active');
|
| if (!activeScene) return;
|
|
|
|
|
| const elements = activeScene.querySelectorAll('.draggable-content');
|
| elements.forEach(element => {
|
| this.dragDropManager.resetPosition(element);
|
| });
|
|
|
|
|
| if (this.currentSlideIndex >= 0) {
|
| this.slideOverrides.delete(this.currentSlideIndex);
|
| }
|
|
|
|
|
| if (this.currentFilePath && this.currentSlideIndex >= 0) {
|
| this.persistentStorage.clearSlide(this.currentFilePath, this.currentSlideIndex);
|
| }
|
|
|
|
|
| this.dragDropManager.clearAllPositions();
|
|
|
| this.showNotification('✓ รีเซ็ตทั้งหมดเรียบร้อย', 'success');
|
| }
|
|
|
| |
| |
|
|
| exportAllSettings() {
|
| if (!this.currentFilePath) {
|
| this.showNotification('⚠ กรุณาเปิดไฟล์ก่อน', 'warning');
|
| return;
|
| }
|
|
|
| const totalSlides = this.getTotalSlides();
|
| this.persistentStorage.downloadJSON(this.currentFilePath, totalSlides);
|
| this.showNotification('✓ ส่งออกการตั้งค่าเรียบร้อย', 'success');
|
| }
|
|
|
| |
| |
|
|
| getTotalSlides() {
|
|
|
| return 10;
|
| }
|
|
|
| |
| |
|
|
| setCurrentFile(filePath) {
|
| this.currentFilePath = filePath;
|
| this.persistentStorage.setCurrentFile(filePath);
|
| }
|
|
|
| |
| |
|
|
| applyPositions(slideIndex, scene) {
|
| if (!this.currentFilePath) return false;
|
|
|
| const positions = this.persistentStorage.loadPositions(this.currentFilePath, slideIndex);
|
| if (!positions) return false;
|
|
|
| this.dragDropManager.loadPositions(positions);
|
| return true;
|
| }
|
|
|
| |
| |
|
|
| savePositions() {
|
| if (!this.currentFilePath || this.currentSlideIndex < 0) return;
|
|
|
| const positions = this.dragDropManager.exportPositions();
|
| this.persistentStorage.savePositions(
|
| this.currentFilePath,
|
| this.currentSlideIndex,
|
| positions
|
| );
|
| }
|
|
|
|
|
|
|
|
|
|
|
| |
| |
|
|
| createFingerprintMap(scene) {
|
| return ContentFingerprint.mapSlideElements(scene);
|
| }
|
|
|
| |
| |
|
|
| matchSettingsToContent(scene, savedSettings) {
|
| const elementMap = this.createFingerprintMap(scene);
|
| const matched = [];
|
|
|
| elementMap.forEach(({ element, fingerprint }) => {
|
| const setting = ContentFingerprint.match(fingerprint, savedSettings);
|
| if (setting) {
|
| matched.push({
|
| element,
|
| setting,
|
| confidence: setting.matchConfidence
|
| });
|
| }
|
| });
|
|
|
| return matched;
|
| }
|
| }
|
|
|
|
|
| const style = document.createElement('style');
|
| style.textContent = `
|
| @keyframes slideDown {
|
| from {
|
| opacity: 0;
|
| transform: translate(-50%, -20px);
|
| }
|
| to {
|
| opacity: 1;
|
| transform: translate(-50%, 0);
|
| }
|
| }
|
|
|
| @keyframes slideUp {
|
| from {
|
| opacity: 1;
|
| transform: translate(-50%, 0);
|
| }
|
| to {
|
| opacity: 0;
|
| transform: translate(-50%, -20px);
|
| }
|
| }
|
| `;
|
| document.head.appendChild(style);
|
|
|
|
|