-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBlogBuilder.html
More file actions
423 lines (362 loc) · 17.3 KB
/
BlogBuilder.html
File metadata and controls
423 lines (362 loc) · 17.3 KB
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Pro JSON Blog Builder</title>
<meta name="viewport" content="width=device-width,initial-scale=1">
<meta name="theme-color" content="#181c24"/>
<!-- TinyMCE -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/tinymce/6.8.3/tinymce.min.js"></script>
<!-- Google Icons & Font -->
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Material+Icons">
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap" rel="stylesheet">
<style>
:root {
--bg: #fafbfc; --fg: #121417; --bar: #eff2f6; --input: #fff;
--border: #e1e6ea; --primary: #2563eb; --primary-fade: #e0e7ff;
--radius: 10px; --shadow: 0 4px 20px rgba(0,0,0,.05);
--danger: #ef4444;
}
[data-theme="dark"] {
--bg: #0f172a; --fg: #f8fafc; --bar: #1e293b; --input: #0f172a;
--border: #334155; --primary: #3b82f6; --primary-fade: #1e3a8a;
--shadow: 0 4px 20px rgba(0,0,0,.3);
}
* { box-sizing: border-box; }
body { font-family: 'Inter', sans-serif; background: var(--bg); color: var(--fg); margin: 0; padding: 0; min-height: 100vh; }
#app { max-width: 1100px; margin: 20px auto; background: var(--bar); border-radius: var(--radius); box-shadow: var(--shadow); overflow: hidden; border: 1px solid var(--border); }
/* --- Top Meta Bar --- */
.meta-bar { display: flex; flex-wrap: wrap; gap: 15px; padding: 20px; background: var(--bar); border-bottom: 1px solid var(--border); }
.meta-group { display: flex; flex-direction: column; gap: 5px; flex: 1; min-width: 150px; }
.meta-group label { font-size: 0.85rem; font-weight: 600; color: var(--fg); opacity: 0.8; }
.meta-group input, .meta-group select {
padding: 8px 12px; border-radius: 6px; border: 1px solid var(--border);
background: var(--input); color: var(--fg); font-family: inherit; font-size: 0.95rem; outline: none;
}
.meta-group input:focus, .meta-group select:focus { border-color: var(--primary); }
.full-width { flex-basis: 100%; }
/* --- Custom Checkbox Dropdown --- */
.multi-select { position: relative; }
.select-box { padding: 8px 12px; border-radius: 6px; border: 1px solid var(--border); background: var(--input); color: var(--fg); font-size: 0.95rem; cursor: pointer; user-select: none; }
.select-box:hover { border-color: var(--primary); }
.dropdown-list { position: absolute; top: 100%; left: 0; right: 0; background: var(--input); border: 1px solid var(--border); border-radius: 6px; z-index: 10; max-height: 180px; overflow-y: auto; display: none; box-shadow: var(--shadow); margin-top: 5px; }
.dropdown-list.show { display: block; }
.dropdown-list label { display: flex; align-items: center; padding: 8px 12px; gap: 8px; cursor: pointer; font-size: 0.9rem; font-weight: normal; margin: 0; opacity: 1; }
.dropdown-list label:hover { background: var(--primary-fade); }
/* --- Tabs --- */
.tabs { display: flex; background: var(--bar); border-bottom: 1px solid var(--border); padding: 0 10px; align-items: center; }
.tab-btn {
background: none; border: none; padding: 15px 20px; color: var(--fg); cursor: pointer;
font-weight: 600; font-size: 0.95rem; border-bottom: 3px solid transparent; opacity: 0.7;
}
.tab-btn.active { border-bottom-color: var(--primary); opacity: 1; color: var(--primary); }
.actions { margin-left: auto; display: flex; gap: 10px; padding-right: 10px; }
.btn { padding: 6px 12px; border-radius: 6px; font-weight: 600; font-size: 0.85rem; cursor: pointer; border: 1px solid var(--border); background: var(--input); color: var(--fg); }
.btn-primary { background: var(--primary); color: white; border-color: var(--primary); }
.btn-danger { color: var(--danger); }
/* --- Panes --- */
.pane { display: none; background: var(--input); min-height: 500px; }
.pane.active { display: block; }
/* JSON Pane */
#jsonPane { padding: 20px; font-family: monospace; white-space: pre-wrap; font-size: 0.9rem; overflow-x: auto; color: var(--fg); }
/* --- Preview Render Styles --- */
#previewPane { padding: 40px 20px; background: var(--bg); display: none; justify-content: center; overflow-y: auto; max-height: 70vh; }
#previewPane.active { display: flex; }
.clog-container { max-width: 768px; width: 100%; background: var(--input); border-radius: 12px; overflow: hidden; box-shadow: var(--shadow); border: 1px solid var(--border); }
.banner-image { width: 100%; height: 350px; object-fit: cover; display: block; background: #cbd5e1; }
.clog-content { padding: 40px; }
.clog-title { font-size: 2.5rem; line-height: 1.2; margin: 0 0 20px 0; font-weight: 700; color: var(--fg); }
.clog-meta { display: flex; flex-direction: column; gap: 15px; margin-bottom: 30px; padding-bottom: 20px; border-bottom: 1px solid var(--border); }
.clog-meta-sub { display: flex; gap: 20px; color: var(--fg); opacity: 0.7; font-size: 0.9rem; }
.clog-meta-item span:first-child { font-weight: 600; margin-right: 5px; }
.clog-tags { display: flex; gap: 8px; flex-wrap: wrap; }
.clog-tags span { background: var(--primary-fade); color: var(--primary); padding: 4px 10px; border-radius: 20px; font-size: 0.8rem; font-weight: 600; }
.clog-tags .cat-badge { background: #334155; color: #fff; }
.clog-body { font-size: 1.1rem; line-height: 1.8; color: var(--fg); }
.clog-body p { margin-bottom: 1.5em; }
.clog-body h2 { margin: 2em 0 1em 0; font-size: 1.8rem; }
.clog-body img { max-width: 100%; border-radius: 8px; margin: 1em 0; }
.clog-body pre { background: #1e293b; color: #fff; padding: 15px; border-radius: 8px; overflow-x: auto; }
.clog-body blockquote { border-left: 4px solid var(--primary); padding-left: 15px; margin-left: 0; font-style: italic; opacity: 0.8; }
</style>
</head>
<body>
<div id="app">
<!-- META DATA INPUTS -->
<div class="meta-bar">
<div class="meta-group">
<label>Blog Title</label>
<input type="text" id="title" placeholder="Mastering Python...">
</div>
<div class="meta-group">
<label>Blog ID (Auto PascalCase)</label>
<input type="text" id="blog-id" placeholder="MasteringPython">
</div>
<!-- Updated Categories Dropdown with Checkboxes -->
<div class="meta-group">
<label>Categories</label>
<div class="multi-select" id="categories-dropdown">
<div class="select-box" id="cat-select-box" onclick="toggleDropdown('categories-list')">Select Categories...</div>
<div class="dropdown-list" id="categories-list">
<!-- Populated by JS -->
</div>
</div>
</div>
<div class="meta-group">
<label>Tags (Sub-cats, comma separated)</label>
<input type="text" id="tags" placeholder="Python, Code, Web">
</div>
<div class="meta-group" style="flex: 0.5;">
<label>Status</label>
<select id="status">
<option value="Un">Unpublished (Un)</option>
<option value="Pub">Published (Pub)</option>
<option value="Not">Not Ready (Not)</option>
</select>
</div>
<div class="meta-group">
<label>Author</label>
<input type="text" id="author" placeholder="John Doe">
</div>
<div class="meta-group full-width">
<label>Banner Image URL</label>
<input type="url" id="img" placeholder="https://images.unsplash.com/...">
</div>
<div class="meta-group full-width">
<label>Short Description (Desc)</label>
<input type="text" id="desc" placeholder="A brief summary of the post...">
</div>
</div>
<!-- TABS & ACTIONS -->
<div class="tabs">
<button class="tab-btn active" onclick="switchTab('editor')">Write (TinyMCE)</button>
<button class="tab-btn" onclick="switchTab('preview')">Preview</button>
<button class="tab-btn" onclick="switchTab('json')">JSON Output</button>
<div class="actions">
<button class="btn" onclick="toggleTheme()">🌓 Theme</button>
<button class="btn btn-danger" onclick="clearAll()">Clear</button>
<button class="btn btn-primary" onclick="copyJSON()">Copy JSON</button>
</div>
</div>
<!-- PANES -->
<!-- 1. Editor Pane -->
<div id="editorPane" class="pane active">
<textarea id="tinymce-editor"></textarea>
</div>
<!-- 2. Preview Pane -->
<div id="previewPane" class="pane">
<article class="clog-container">
<img src="" alt="Banner" id="prev-img" class="banner-image" onerror="this.src='https://via.placeholder.com/800x400?text=No+Image+Provided'"/>
<div class="clog-content">
<h1 class="clog-title" id="prev-title">Blog Post Title</h1>
<div class="clog-meta">
<div class="clog-meta-sub">
<div class="clog-meta-item">
<span>Date:</span> <time id="prev-date"></time>
</div>
<div class="clog-meta-item">
<span>Author:</span> <span class="clog-author" id="prev-author"></span>
</div>
</div>
<div class="clog-meta-item clog-tags" id="prev-tags"></div>
</div>
<!-- The TinyMCE HTML Content injects exactly here -->
<div class="clog-body" id="prev-body"></div>
</div>
</article>
</div>
<!-- 3. JSON Pane -->
<div id="jsonPane" class="pane"></div>
</div>
<script>
const STORAGE_KEY = 'blog_draft_data';
// State Management
const inputs = {
title: document.getElementById('title'),
id: document.getElementById('blog-id'),
tags: document.getElementById('tags'),
status: document.getElementById('status'),
author: document.getElementById('author'),
img: document.getElementById('img'),
desc: document.getElementById('desc')
};
// --- Category Checkbox Logic ---
const AVAILABLE_CATEGORIES =["Technology", "Programming", "Web Development", "Artificial Intelligence", "Lifestyle", "Business", "Design", "Tutorials", "Finance"];
const categoryList = document.getElementById('categories-list');
const catSelectBox = document.getElementById('cat-select-box');
// Populate categories list
AVAILABLE_CATEGORIES.forEach(cat => {
const lbl = document.createElement('label');
lbl.innerHTML = `<input type="checkbox" value="${cat}" class="cat-checkbox"> ${cat}`;
categoryList.appendChild(lbl);
});
function toggleDropdown(id) {
document.getElementById(id).classList.toggle('show');
}
// Close dropdown on outside click
document.addEventListener('click', (e) => {
if (!e.target.closest('.multi-select')) {
document.getElementById('categories-list').classList.remove('show');
}
});
// Handle Category check changes
document.addEventListener('change', (e) => {
if (e.target.classList.contains('cat-checkbox')) {
updateCategoryText();
saveDraft();
}
});
function updateCategoryText() {
const checked = getSelectedCategories();
if (checked.length === 0) {
catSelectBox.innerText = "Select Categories...";
} else {
catSelectBox.innerText = checked.length > 2 ? `${checked.length} selected` : checked.join(', ');
}
}
function getSelectedCategories() {
return Array.from(document.querySelectorAll('.cat-checkbox:checked')).map(cb => cb.value);
}
// --- 1. Initialize TinyMCE Editor ---
tinymce.init({
selector: '#tinymce-editor',
height: 600,
menubar: false,
plugins:[
'advlist', 'autolink', 'lists', 'link', 'image', 'charmap', 'preview',
'anchor', 'searchreplace', 'visualblocks', 'code', 'fullscreen',
'insertdatetime', 'media', 'table', 'help', 'wordcount', 'codesample'
],
toolbar: 'undo redo | blocks | ' +
'bold italic forecolor | alignleft aligncenter ' +
'alignright alignjustify | bullist numlist outdent indent | ' +
'link image codesample table blockquote | removeformat | code fullscreen',
content_style: 'body { font-family:Inter,sans-serif; font-size:16px; line-height:1.6; color:#333; } p {margin-bottom: 1em;}',
skin: window.matchMedia('(prefers-color-scheme: dark)').matches ? 'oxide-dark' : 'oxide',
content_css: window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'default',
// Auto-save logic triggers when editor changes
setup: function(editor) {
editor.on('Change KeyUp', function() {
saveDraft();
});
editor.on('init', function() {
loadDraft(); // Load draft only after editor is ready
});
}
});
// --- 2. Generate JSON Object (Matches exactly requested keys) ---
function getOutputJSON() {
return {
"Title": inputs.title.value.trim(),
"Desc": inputs.desc.value.trim(),
"Author": inputs.author.value.trim() || "Admin",
"Id": inputs.id.value.trim(),
"Tags": inputs.tags.value.split(',').map(t => t.trim()).filter(t => t),
"Categories": getSelectedCategories(),
"Date": getFormattedDate(),
"Img": inputs.img.value.trim(),
"Status": inputs.status.value, // Will be "Un", "Pub", or "Not"
"Data": tinymce.get('tinymce-editor') ? tinymce.get('tinymce-editor').getContent() : ""
};
}
// --- 3. Update Preview Pane ---
function updatePreview() {
const data = getOutputJSON();
document.getElementById('prev-img').src = data.Img;
document.getElementById('prev-title').innerText = data.Title || "Untitled Post";
document.getElementById('prev-date').innerText = data.Date;
document.getElementById('prev-author').innerText = data.Author;
// Mix Categories and Tags visually in preview
const categoriesHtml = data.Categories.map(c => `<span class="cat-badge">${c}</span>`).join('');
const tagsHtml = data.Tags.map(t => `<span>#${t}</span>`).join('');
document.getElementById('prev-tags').innerHTML = categoriesHtml + tagsHtml;
// Inject Editor HTML Directly
document.getElementById('prev-body').innerHTML = data.Data || "<p style='color:gray'>Content goes here...</p>";
}
// --- 4. Update JSON display ---
function updateJSONPane() {
document.getElementById('jsonPane').innerText = JSON.stringify(getOutputJSON(), null, 2);
}
// --- 5. Tab Switching Logic ---
function switchTab(tab) {
document.querySelectorAll('.tab-btn').forEach(b => b.classList.remove('active'));
document.querySelectorAll('.pane').forEach(p => p.classList.remove('active'));
event.target.classList.add('active');
document.getElementById(tab + 'Pane').classList.add('active');
if (tab === 'preview') updatePreview();
if (tab === 'json') updateJSONPane();
}
// --- 6. Local Storage / AutoSave ---
function saveDraft() {
const draft = getOutputJSON();
localStorage.setItem(STORAGE_KEY, JSON.stringify(draft));
}
function loadDraft() {
try {
const saved = JSON.parse(localStorage.getItem(STORAGE_KEY));
if (!saved) return;
inputs.title.value = saved.Title || '';
inputs.id.value = saved.Id || '';
inputs.tags.value = (saved.Tags ||[]).join(', ');
inputs.status.value = saved.Status || 'Un';
inputs.author.value = saved.Author || '';
inputs.img.value = saved.Img || '';
inputs.desc.value = saved.Desc || '';
// Load selected categories
const savedCats = saved.Categories ||[];
document.querySelectorAll('.cat-checkbox').forEach(cb => {
cb.checked = savedCats.includes(cb.value);
});
updateCategoryText();
if (saved.Data && tinymce.get('tinymce-editor')) {
tinymce.get('tinymce-editor').setContent(saved.Data);
}
} catch (e) { console.error("Error loading draft", e); }
}
// Input Listeners to trigger autosave
Object.values(inputs).forEach(input => {
input.addEventListener('input', saveDraft);
});
// Auto-generate ID from Title (PascalCase format logic)
inputs.title.addEventListener('blur', function() {
if (!inputs.id.value && this.value) {
// Remove symbols and convert "My First Blog" to "MyFirstBlog"
inputs.id.value = this.value.match(/[a-zA-Z0-9]+/g)?.map(w => w.charAt(0).toUpperCase() + w.slice(1).toLowerCase()).join('') || '';
saveDraft();
}
});
// --- Helpers ---
function getFormattedDate() {
const d = new Date();
const pad = (n) => (n < 10 ? '0' : '') + n;
return `${pad(d.getDate())}-${pad(d.getMonth() + 1)}-${d.getFullYear()}`;
}
function copyJSON() {
navigator.clipboard.writeText(JSON.stringify(getOutputJSON(), null, 2));
const btn = document.querySelector('.btn-primary');
btn.innerText = "Copied!";
setTimeout(() => btn.innerText = "Copy JSON", 1500);
}
function clearAll() {
if (confirm("Are you sure you want to clear all data?")) {
Object.values(inputs).forEach(input => input.value = '');
inputs.status.value = 'Un';
document.querySelectorAll('.cat-checkbox').forEach(cb => cb.checked = false);
updateCategoryText();
if (tinymce.get('tinymce-editor')) tinymce.get('tinymce-editor').setContent('');
localStorage.removeItem(STORAGE_KEY);
switchTab('editor');
}
}
function toggleTheme() {
const html = document.documentElement;
const isDark = html.getAttribute('data-theme') === 'dark';
html.setAttribute('data-theme', isDark ? 'light' : 'dark');
}
// Set initial theme based on system preference
if (window.matchMedia('(prefers-color-scheme: dark)').matches) {
document.documentElement.setAttribute('data-theme', 'dark');
}
</script>
</body>
</html>