File size: 6,932 Bytes
85bc0da
 
88dde93
85bc0da
 
 
88dde93
 
 
 
85bc0da
 
 
88dde93
 
 
85bc0da
 
 
88dde93
 
85bc0da
88dde93
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85bc0da
 
 
 
 
88dde93
85bc0da
 
 
88dde93
85bc0da
 
 
88dde93
 
 
 
 
 
85bc0da
 
88dde93
 
 
 
 
 
 
85bc0da
88dde93
 
 
 
 
 
 
85bc0da
88dde93
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85bc0da
88dde93
 
 
 
85bc0da
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<?php
require_once 'user-manager.php';
require_once 'storage.php';

// Initialize user manager and require login
$userManager = new UserManager();
if (!$userManager->isLoggedIn()) {
    header('Location: index.html');
    exit;
}

$currentUser = $userManager->getCurrentUser();

// Initialize storage manager for GitHub file access
$storageManager = new StorageManager();

// Get the editor HTML
$html = file_get_contents('editor.html');

// Get user files from GitHub storage
$files = '';
$userFiles = [];

try {
    $githubFiles = $storageManager->listFiles();
    
    // Add user files from GitHub
    foreach ($githubFiles as $file) {
        if (in_array($file['name'], array('new-page-blank-template.html', 'editor.html'))) continue;
        
        $pathInfo = pathinfo($file['name']);
        $filename = $pathInfo['filename'];
        $folder = 'My Files (GitHub)';
        $url = $file['path'];
        $name = $filename;
        $title = ucfirst($name);
        
        $files .= '<li class="file" data-url="' . htmlspecialchars($url) . '" data-folder="' . htmlspecialchars($folder) . '">';
        $files .= '<label class="folder-name">' . htmlspecialchars($folder) . '</label>';
        $files .= '<div class="file-name"><span>' . htmlspecialchars($title) . '</span> <div class="file-actions">';
        $files .= '<button class="btn btn-sm btn-light load-file-btn" data-file="' . htmlspecialchars($url) . '">Load</button>';
        $files .= '</div></div></li>';
        
        $userFiles[] = [
            'filename' => $url,
            'title' => $title,
            'folder' => $folder
        ];
    }
} catch (Exception $e) {
    error_log("Editor: Error loading GitHub files: " . $e->getMessage());
}

// Fallback: check local user files as backup
$localUserDir = "user-files/$currentUser";
if (is_dir($localUserDir)) {
    $localFiles = glob("$localUserDir/*.html");
    
    foreach ($localFiles as $file) {
        if (in_array(basename($file), array('new-page-blank-template.html', 'editor.html'))) continue;
        $pathInfo = pathinfo($file);
        $filename = $pathInfo['filename'];
        $folder = 'Local Files';
        $url = str_replace(__DIR__ . DIRECTORY_SEPARATOR, '', $file);
        $url = str_replace('\\', '/', $url);
        $name = $filename;
        $title = ucfirst($name);
        
        $files .= '<li class="file" data-url="' . htmlspecialchars($url) . '" data-folder="' . htmlspecialchars($folder) . '">';
        $files .= '<label class="folder-name">' . htmlspecialchars($folder) . '</label>';
        $files .= '<div class="file-name"><span>' . htmlspecialchars($title) . '</span> <div class="file-actions">';
        $files .= '<button class="btn btn-sm btn-light load-file-btn" data-file="' . htmlspecialchars($url) . '">Load</button>';
        $files .= '</div></div></li>';
        
        $userFiles[] = [
            'filename' => $url,
            'title' => $title,
            'folder' => $folder
        ];
    }
}

// Also include demo files for reference
$demoFiles = array_merge(glob('demo/*/*.html'), glob('demo/*.html'));

foreach ($demoFiles as $file) {
    if (in_array(basename($file), array('new-page-blank-template.html', 'editor.html'))) continue;
    $pathInfo = pathinfo($file);
    $filename = $pathInfo['filename'];
    $folder = ucfirst(basename(dirname($file)));
    $url = $file;
    $name = $filename;
    $title = ucfirst($name);
    
    $files .= '<li class="file" data-url="' . htmlspecialchars($url) . '" data-folder="' . htmlspecialchars($folder) . '">';
    $files .= '<label class="folder-name">' . htmlspecialchars($folder) . ' (Demo)</label>';
    $files .= '<div class="file-name"><span>' . htmlspecialchars($title) . '</span> <div class="file-actions">';
    $files .= '<button class="btn btn-sm btn-light load-file-btn" data-file="' . htmlspecialchars($url) . '">Load</button>';
    $files .= '</div></div></li>';
}

// Add user info and file list to the HTML
$userInfo = '<div class="user-info alert alert-info">';
$userInfo .= '<strong>当前用户:</strong> ' . htmlspecialchars($currentUser);
$userInfo .= '<br><strong>存储:</strong> ' . StorageConfig::getStorageType();
$userInfo .= '<br><strong>GitHub文件:</strong> ' . count($githubFiles ?? []) . ' 个';
if (isset($localFiles)) {
    $userInfo .= '<br><strong>本地缓存:</strong> ' . count($localFiles) . ' 个';
}
$userInfo .= '</div>';

// Inject JavaScript for file management
$fileManagementScript = '
<script>
window.vvvebUserFiles = ' . json_encode($userFiles) . ';
window.vvvebCurrentUser = "' . htmlspecialchars($currentUser) . '";

// Enhanced file loading with GitHub support
function loadFileFromGitHub(filename) {
    console.log("Loading file from GitHub:", filename);
    
    fetch("save.php?action=loadFile&file=" + encodeURIComponent(filename))
        .then(response => response.json())
        .then(data => {
            if (data.success) {
                Vvveb.Builder.loadHtml(data.content);
                console.log("File loaded successfully from GitHub");
                
                // Update current file reference
                window.currentFile = filename;
                
                // Update page title if available
                const titleMatch = data.content.match(/<title>(.*?)<\/title>/i);
                if (titleMatch) {
                    const titleInput = document.querySelector("input[name=title]");
                    if (titleInput) titleInput.value = titleMatch[1];
                }
            } else {
                alert("Error loading file: " + (data.message || "Unknown error"));
            }
        })
        .catch(error => {
            console.error("Error loading file:", error);
            alert("Error loading file from GitHub");
        });
}

// Override the existing file loading
document.addEventListener("DOMContentLoaded", function() {
    // Add enhanced file loading for GitHub files
    document.querySelectorAll(".load-file-btn").forEach(btn => {
        btn.addEventListener("click", function() {
            const filename = this.getAttribute("data-file");
            loadFileFromGitHub(filename);
        });
    });
    
    // Add refresh button for file list
    const refreshBtn = document.createElement("button");
    refreshBtn.className = "btn btn-sm btn-secondary";
    refreshBtn.innerHTML = "🔄 刷新文件列表";
    refreshBtn.onclick = function() {
        location.reload();
    };
    
    const fileManager = document.querySelector("#filemanager .file-manager-header");
    if (fileManager) {
        fileManager.appendChild(refreshBtn);
    }
});
</script>';

// Insert user info and files into the HTML
$html = str_replace('<!--files-li-->', $files, $html);
$html = str_replace('</head>', $fileManagementScript . '</head>', $html);
$html = str_replace('<div id="filemanager">', '<div id="filemanager">' . $userInfo, $html);

echo $html;
?>