|
32 | 32 | border-radius: 5px;
|
33 | 33 | font-size: 16px;
|
34 | 34 | transition: background-color 0.3s;
|
| 35 | + margin-right: 5px; |
35 | 36 | }
|
36 | 37 | .button:hover {
|
37 | 38 | background-color: #45a049;
|
38 | 39 | }
|
39 |
| - .form-container { |
40 |
| - display: none; |
41 |
| - margin-top: 20px; |
42 |
| - background-color: #ffffff; |
43 |
| - border: 1px solid #ccc; |
44 |
| - border-radius: 8px; |
45 |
| - padding: 20px; |
46 |
| - box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1); |
47 |
| - } |
48 |
| - .form-container input, |
49 |
| - .form-container textarea { |
| 40 | + .edit-area { |
50 | 41 | width: 100%;
|
51 |
| - padding: 10px; |
| 42 | + height: 100px; |
52 | 43 | margin-top: 10px;
|
| 44 | + padding: 10px; |
| 45 | + font-size: 16px; |
53 | 46 | border: 1px solid #ccc;
|
54 | 47 | border-radius: 5px;
|
55 |
| - font-size: 16px; |
56 |
| - } |
57 |
| - .form-container textarea { |
58 |
| - height: 150px; |
59 | 48 | }
|
60 | 49 | </style>
|
61 | 50 | </head>
|
62 | 51 | <body>
|
63 | 52 | <h1>Global Wiki</h1>
|
64 | 53 | <div id="wiki"></div>
|
65 | 54 |
|
66 |
| - <button class="button" onclick="toggleForm()">Create New Wiki Page</button> |
| 55 | + <button class="button" onclick="showCreateForm()">Create New Wiki Page</button> |
67 | 56 |
|
68 |
| - <div class="form-container" id="form-container"> |
| 57 | + <div class="form-container" id="form-container" style="display:none;"> |
69 | 58 | <h2>Create New Page</h2>
|
70 |
| - <input id="new-title" placeholder="Page Title" /><br> |
71 |
| - <textarea id="new-content" placeholder="Page Content"></textarea><br> |
72 |
| - <button class="button" onclick="addPage()">Add Page</button> |
| 59 | + <input id="page-title" placeholder="Page Title" /><br> |
| 60 | + <textarea id="page-content" placeholder="Page Content"></textarea><br> |
| 61 | + <button class="button" onclick="savePage()">Save Page</button> |
| 62 | + <button class="button" style="background-color: #ccc;" onclick="closeForm()">Cancel</button> |
73 | 63 | </div>
|
74 | 64 |
|
75 | 65 | <script>
|
76 | 66 | const JSON_BIN_READ_URL = 'https://api.jsonbin.io/v3/b/67c73db1e41b4d34e4a0bfb1/latest';
|
77 | 67 | const JSON_BIN_WRITE_URL = 'https://api.jsonbin.io/v3/b/67c73db1e41b4d34e4a0bfb1';
|
78 | 68 |
|
79 |
| - function toggleForm() { |
80 |
| - const formContainer = document.getElementById('form-container'); |
81 |
| - formContainer.style.display = formContainer.style.display === 'none' || formContainer.style.display === '' ? 'block' : 'none'; |
82 |
| - } |
| 69 | + let pages = []; |
83 | 70 |
|
84 | 71 | async function fetchWiki() {
|
85 | 72 | try {
|
86 | 73 | const res = await fetch(JSON_BIN_READ_URL);
|
87 | 74 | const data = await res.json();
|
88 |
| - const pages = data.record; |
89 |
| - document.getElementById('wiki').innerHTML = pages.map((page, index) => ` |
90 |
| - <div class="page"> |
91 |
| - <h3>${page.title}</h3> |
92 |
| - <p>${page.content}</p> |
93 |
| - <button class="button" onclick="editPage(${index})">Edit</button> |
94 |
| - <button class="button" onclick="deletePage(${index})">Delete</button> |
95 |
| - </div> |
96 |
| - `).join(''); |
| 75 | + pages = data.record; |
| 76 | + renderWiki(); |
97 | 77 | } catch (error) {
|
98 | 78 | console.error('Error fetching wiki:', error);
|
99 | 79 | }
|
100 | 80 | }
|
101 | 81 |
|
102 |
| - async function updateWiki(pages) { |
103 |
| - try { |
104 |
| - const res = await fetch(JSON_BIN_WRITE_URL, { |
105 |
| - method: 'PUT', |
106 |
| - headers: { 'Content-Type': 'application/json' }, |
107 |
| - body: JSON.stringify({ record: pages }) |
108 |
| - }); |
109 |
| - fetchWiki(); |
110 |
| - } catch (error) { |
111 |
| - console.error('Error updating wiki:', error); |
112 |
| - } |
| 82 | + function renderWiki() { |
| 83 | + document.getElementById('wiki').innerHTML = pages.map((page, index) => ` |
| 84 | + <div class="page" id="page-${index}"> |
| 85 | + <h3>${page.title}</h3> |
| 86 | + <p id="content-${index}">${page.content}</p> |
| 87 | + <button class="button" onclick="showEditArea(${index})">Edit</button> |
| 88 | + <button class="button" style="background-color: #e74c3c;" onclick="deletePage(${index})">Delete</button> |
| 89 | + </div> |
| 90 | + `).join(''); |
| 91 | + } |
| 92 | + |
| 93 | + function showCreateForm() { |
| 94 | + document.getElementById('form-container').style.display = 'block'; |
| 95 | + document.getElementById('page-title').value = ''; |
| 96 | + document.getElementById('page-content').value = ''; |
113 | 97 | }
|
114 | 98 |
|
115 |
| - async function addPage() { |
116 |
| - const title = document.getElementById('new-title').value; |
117 |
| - const content = document.getElementById('new-content').value; |
| 99 | + function closeForm() { |
| 100 | + document.getElementById('form-container').style.display = 'none'; |
| 101 | + } |
| 102 | + |
| 103 | + async function savePage() { |
| 104 | + const title = document.getElementById('page-title').value; |
| 105 | + const content = document.getElementById('page-content').value; |
| 106 | + |
118 | 107 | if (!title || !content) {
|
119 | 108 | alert('Title and content cannot be empty.');
|
120 | 109 | return;
|
121 | 110 | }
|
122 |
| - try { |
123 |
| - const res = await fetch(JSON_BIN_READ_URL); |
124 |
| - const data = await res.json(); |
125 |
| - const pages = data.record; |
126 |
| - pages.push({ title, content }); |
127 |
| - updateWiki(pages); |
128 |
| - } catch (error) { |
129 |
| - console.error('Error adding page:', error); |
| 111 | + |
| 112 | + pages.push({ title, content }); |
| 113 | + await updateWiki(); |
| 114 | + closeForm(); |
| 115 | + } |
| 116 | + |
| 117 | + function showEditArea(index) { |
| 118 | + const contentElement = document.getElementById(`content-${index}`); |
| 119 | + const pageElement = document.getElementById(`page-${index}`); |
| 120 | + |
| 121 | + // Only create an edit area if one isn't already there |
| 122 | + if (!document.getElementById(`edit-area-${index}`)) { |
| 123 | + const currentContent = pages[index].content; |
| 124 | + |
| 125 | + const editArea = document.createElement('textarea'); |
| 126 | + editArea.id = `edit-area-${index}`; |
| 127 | + editArea.className = 'edit-area'; |
| 128 | + editArea.value = currentContent; |
| 129 | + |
| 130 | + const saveButton = document.createElement('button'); |
| 131 | + saveButton.className = 'button'; |
| 132 | + saveButton.innerText = 'Save Changes'; |
| 133 | + saveButton.onclick = () => saveEdit(index); |
| 134 | + |
| 135 | + const cancelButton = document.createElement('button'); |
| 136 | + cancelButton.className = 'button'; |
| 137 | + cancelButton.style.backgroundColor = '#ccc'; |
| 138 | + cancelButton.innerText = 'Cancel'; |
| 139 | + cancelButton.onclick = () => renderWiki(); // Rerender to remove edit area |
| 140 | + |
| 141 | + contentElement.style.display = 'none'; // Hide the original content |
| 142 | + pageElement.appendChild(editArea); |
| 143 | + pageElement.appendChild(saveButton); |
| 144 | + pageElement.appendChild(cancelButton); |
130 | 145 | }
|
131 | 146 | }
|
132 | 147 |
|
133 |
| - async function editPage(index) { |
134 |
| - const newContent = prompt('Edit page content:'); |
135 |
| - if (newContent !== null) { |
136 |
| - try { |
137 |
| - const res = await fetch(JSON_BIN_READ_URL); |
138 |
| - const data = await res.json(); |
139 |
| - const pages = data.record; |
140 |
| - pages[index].content = newContent; |
141 |
| - updateWiki(pages); |
142 |
| - } catch (error) { |
143 |
| - console.error('Error editing page:', error); |
144 |
| - } |
| 148 | + async function saveEdit(index) { |
| 149 | + const newContent = document.getElementById(`edit-area-${index}`).value; |
| 150 | + if (newContent.trim() === '') { |
| 151 | + alert('Content cannot be empty.'); |
| 152 | + return; |
145 | 153 | }
|
| 154 | + |
| 155 | + pages[index].content = newContent; |
| 156 | + await updateWiki(); |
146 | 157 | }
|
147 | 158 |
|
148 | 159 | async function deletePage(index) {
|
149 | 160 | if (confirm('Are you sure you want to delete this page?')) {
|
150 |
| - try { |
151 |
| - const res = await fetch(JSON_BIN_READ_URL); |
152 |
| - const data = await res.json(); |
153 |
| - const pages = data.record; |
154 |
| - pages.splice(index, 1); |
155 |
| - updateWiki(pages); |
156 |
| - } catch (error) { |
157 |
| - console.error('Error deleting page:', error); |
158 |
| - } |
| 161 | + pages.splice(index, 1); |
| 162 | + await updateWiki(); |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + async function updateWiki() { |
| 167 | + try { |
| 168 | + await fetch(JSON_BIN_WRITE_URL, { |
| 169 | + method: 'PUT', |
| 170 | + headers: { 'Content-Type': 'application/json' }, |
| 171 | + body: JSON.stringify({ record: pages }) |
| 172 | + }); |
| 173 | + fetchWiki(); |
| 174 | + } catch (error) { |
| 175 | + console.error('Error updating wiki:', error); |
159 | 176 | }
|
160 | 177 | }
|
161 | 178 |
|
|
0 commit comments