Skip to content

Commit 1ecd75a

Browse files
committed
Add revision description editing
1 parent dabdc1e commit 1ecd75a

4 files changed

Lines changed: 77 additions & 45 deletions

File tree

routes/edit.js

Lines changed: 57 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -164,45 +164,60 @@ router.get('/snippet/:ref?/:version?', (req, res, next) => {
164164
let ref = req.params.ref;
165165
let version = req.params.version;
166166

167-
if (ref === undefined || version === undefined) {
168-
req.flash('warning', `Missing ref and/or version number`);
167+
if (ref === undefined) {
168+
req.flash('warning', `Missing ref`);
169169
res.redirect(baseURL + '/view/snippet');
170170
return;
171171
}
172172

173173
Snippet.getCond({ref}).then(doc => {
174-
Version.getVersion(ref, version).then(ver => {
175-
if (doc === null || ver === null) {
176-
res.redirect(baseURL + '/view/snippet');
177-
return;
178-
}
174+
if (doc === null) {
175+
res.redirect(baseURL + '/view/snippet');
176+
return;
177+
}
179178

180-
Snippet.getTags(doc.id).then(tags => {
181-
if (doc.owner !== req.session.user.id) {
182-
res.redirect(baseURL + '/view/snippet');
179+
Snippet.getTags(doc.id).then(tags => {
180+
if (doc.owner !== req.session.user.id) {
181+
res.redirect(baseURL + '/view/snippet');
182+
} else {
183+
if (version !== undefined) {
184+
Version.getVersion(ref, version).then(ver => {
185+
if (ver === null) {
186+
res.redirect(baseURL + '/view/snippet');
187+
}
188+
doc.description = ver.description; // Replace snippet description with revision's description
189+
res.render('edit/snippet', _.merge(defaultVars, {snippet: doc, version: _.merge(ver, {revision: true}), tags, socket: ':' + settings.general.socket, title: `Edit Snippet ${doc.name}`}));
190+
});
183191
} else {
184-
doc.code = fs.readFileSync(`./data/snippets/${doc.id}-${ver.version}.snippet`); // Read snippet src into this...
185-
res.render('edit/snippet', _.merge(defaultVars, {snippet: doc, tags, socket: ':' + settings.general.socket, title: `Edit Snippet ${doc.name}`}));
192+
res.render('edit/snippet', _.merge(defaultVars, {snippet: doc, version: {version: 1, revision: false}, tags, socket: ':' + settings.general.socket, title: `Edit Snippet ${doc.name}`}));
186193
}
187-
});
194+
}
188195
});
189196
});
190197
});
191198

192199
router.post('/snippet/:ref?/:version?', (req, res, next) => {
193-
let ref = req.params.ref;
194-
let version = req.params.version;
200+
let ref = req.params.ref;
201+
let version = req.params.version;
202+
let versionFmt;
203+
if (version !== undefined) {
204+
versionFmt = "/" + version;
205+
props = ['rename', 'description'];
206+
req.body.tags = "";
207+
} else {
208+
props = ['rename', 'tags', 'description'];
209+
}
195210
let description = req.body.description;
196211

197-
if (ref === undefined || version === undefined) {
198-
req.flash('warning', `Missing ref and/or version number`);
199-
res.redirect(`${baseURL}/edit/snippet/${ref}/${version}`);
212+
if (ref === undefined) {
213+
req.flash('warning', `Missing ref`);
214+
res.redirect(`${baseURL}/edit/snippet/${ref}${versionFmt}${versionFmt}`);
200215
return;
201216
}
202217

203-
if (missingProps(req.body, ['rename', 'tags', 'description'])) {
218+
if (missingProps(req.body, props)) {
204219
req.flash('warning', 'Missing expected form properties');
205-
res.redirect(`${baseURL}/edit/snippet/${ref}/${version}`);
220+
res.redirect(`${baseURL}/edit/snippet/${ref}${versionFmt}`);
206221
return;
207222
}
208223

@@ -221,34 +236,46 @@ router.post('/snippet/:ref?/:version?', (req, res, next) => {
221236
if (name === undefined || name === "") name = doc.name;
222237
if (doc.published === 1 && name !== doc.name) {
223238
req.flash('warning', 'Names can\'t be changed for Published Snippets');
224-
res.redirect(`${baseURL}/edit/snippet/${ref}/${version}`);
239+
res.redirect(`${baseURL}/edit/snippet/${ref}${versionFmt}`);
225240

226241
} else if (name.match(/^[a-zA-Z0-9 _\-\.+\[\]\{\}\(\)]{1,32}$/) === null) {
227242
req.flash('warning', 'Only 32 chars max please! Accepted chars: a-Z0-9 _-.+[]{}()');
228-
res.redirect(`${baseURL}/edit/snippet/${ref}/${version}`);
243+
res.redirect(`${baseURL}/edit/snippet/${ref}${versionFmt}`);
229244

230245
} else if (rejects.length > 0 || req.body.tags.length > 255) {
231246
req.flash('warning', 'Snippet tags invalid! 32 chars per tag, accepted chars: a-Z0-9 _-.+[]{}()');
232-
res.redirect(`${baseURL}/edit/snippet/${ref}/${version}`);
247+
res.redirect(`${baseURL}/edit/snippet/${ref}${versionFmt}`);
233248

234249
} else if (description.length > 65535) {
235250
req.flash('warning', 'Description is too large');
236-
res.redirect(`${baseURL}/edit/snippet/${ref}/${version}`);
251+
res.redirect(`${baseURL}/edit/snippet/${ref}${versionFmt}`);
237252

238253
} else {
239254
Snippet.getCond({name: req.body.rename, owner: req.session.user.id}).then(dup => {
240255
if (req.body.rename !== doc.name && dup !== null) {
241256
req.flash('warning', `You already have another snippet named ${req.body.rename}`);
242-
res.redirect(`${baseURL}/edit/snippet/${ref}/${version}`);
257+
res.redirect(`${baseURL}/edit/snippet/${ref}${versionFmt}`);
243258
return;
244259
}
245260

246-
Snippet.update({name, description}, doc.id).then(() => {
247-
Snippet.updateTags(tags, doc.id).then(() => {
248-
req.flash('info', `Snippet ${name} was updated successfully!`);
249-
res.redirect(baseURL + '/view/snippet' + (doc.published === 1 ? "#publish" : ""));
261+
// Revision edit
262+
if (version !== undefined) {
263+
Version.getVersion(ref, version).then(ver => {
264+
Version.update({description}, ver.id).then(() => {
265+
Snippet.modified(doc.id).then(() => {
266+
req.flash('info', `Snippet ${name} was updated successfully!`);
267+
res.redirect(baseURL + '/view/snippet#publish');
268+
});
269+
});
250270
});
251-
});
271+
} else {
272+
Snippet.update({name, description}, doc.id).then(() => {
273+
Snippet.updateTags(tags, doc.id).then(() => {
274+
req.flash('info', `Snippet ${name} was updated successfully!`);
275+
res.redirect(baseURL + '/view/snippet' + (doc.published === 1 ? "#publish" : ""));
276+
});
277+
});
278+
}
252279
});
253280
}
254281
});

src/js/search.js

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ $(() => {
3838
$('#results tbody tr').removeClass('selected');
3939
selected = $(this).data('id');
4040
$(this).addClass('selected');
41-
snippetInfo($(this).data('id'))
41+
snippetInfo($(this).data('id'));
42+
4243
});
4344
});
4445

@@ -63,8 +64,10 @@ $(() => {
6364
Created: <span id="created" class="date" data-date="${snippets[msg.snippetID].created}"></span><br>
6465
Modified: <span id="modified" class="date" data-date="${snippets[msg.snippetID].modified}"></span><br>
6566
</p>
66-
Description<br>
67-
<textarea id='description' rows='7' readonly>${snippets[msg.snippetID].description}</textarea>
67+
Snippet Description<br>
68+
<textarea id='snippetDescription' rows='5' readonly>${snippets[msg.snippetID].description}</textarea>
69+
Revision Description<br>
70+
<textarea id='revisionDescription' rows='5' readonly></textarea>
6871
Usage
6972
<pre><code id='usage' class="javascript">const mySnippet = require('${snippets[msg.snippetID].user}/${snippets[msg.snippetID].name}/${msg.version}');</code></pre>
7073
</div>
@@ -76,18 +79,18 @@ $(() => {
7679

7780
$("select[name='revision']").change(updateRevision);
7881

79-
updateDates();
82+
updateRevision();
8083
});
8184
});
8285

8386
function updateRevision() {
84-
let self = this;
85-
$.when($.ajax(`ajax/snippetLookup/${snippets[selected].ref}/${$(this).val()}`)).then(function(data) {
87+
let self = $("select[name='revision']");
88+
$.when($.ajax(`ajax/snippetLookup/${snippets[selected].ref}/${self.val()}`)).then(function(data) {
8689
console.log(data);
8790
$('#created').data('date', data.created);
8891
$('#modified').data('date', data.modified);
89-
$('#description').val(data.description);
90-
$('#usage').html(`const mySnippet = require('${snippets[selected].user}/${snippets[selected].name}/${$(self).val()}');`);
92+
$('#revisionDescription').val(data.description);
93+
$('#usage').html(`const mySnippet = require('${snippets[selected].user}/${snippets[selected].name}/${self.val()}');`);
9194

9295
$('pre code').each(function(i, block) {
9396
hljs.highlightBlock(block);

views/pages/edit/snippet.ejs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,16 @@
1010
<% include ../../snippets/subnav %>
1111
</div>
1212
<div class="ten columns">
13-
<h3>Edit Snippet <%=snippet.name%></h3>
13+
<h3>Edit Snippet <%=snippet.name%><%= version.revision && snippet.published ? " | revision " + version.version : "" %></h3>
1414
<form action="" method="POST">
1515
<span class='red' id='limits'>Accepted chars: a-zA-Z0-9 _-.+[]{}()</span><br>
1616
Name<br><input type="text" id="limitsInput" name="rename" maxlength='32' value="<%=snippet.name%>" placeholder="<%=snippet.name%>" autofocus required autocomplete="off" <%=snippet.published ? "readonly" : ""%>><br>
17-
<span class='red' id='tagLimits'>32 chars per tag, accepted chars: a-zA-Z0-9 _-.+[]{}()</span><br>
18-
Tags<br><input type="text" id="tagsInput" name="tags" maxlength='255' value="<%=tags%>" placeholder="<%=tags%>" autocomplete="off"><br>
19-
Description<br><textarea rows='15' cols='50' name='description' placeholder='<%=snippet.description%>' maxlength='65535'><%=snippet.description%></textarea><br>
20-
<input type="submit" id="submit" value="Update Snippet">
17+
<% if (!version.revision) { %>
18+
<span class='red' id='tagLimits'>32 chars per tag, accepted chars: a-zA-Z0-9 _-.+[]{}()</span><br>
19+
Tags<br><input type="text" id="tagsInput" name="tags" maxlength='255' value="<%=tags%>" placeholder="<%=tags%>" autocomplete="off"><br>
20+
<% } %>
21+
<%= version.revision ? "Revision" : "" %> Description<br><textarea rows='15' cols='50' name='description' placeholder='<%=snippet.description%>' maxlength='65535'><%=snippet.description%></textarea><br>
22+
<input type="submit" id="submit" value="Update <%= version.revision ? 'Revision' : 'Snippet' %>">
2123
</form>
2224
</div>
2325
</div>

views/pages/view/snippet.ejs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
<td><%=snippet.name%></td>
4747
<td><span class="date" data-date="<%=snippet.created%>"></span></td>
4848
<td><span class="date" data-date="<%=snippet.modified%>"></span></td>
49-
<td><a href="" onClick="snippetDeletePrompt('<%=snippet.ref%>', '<%=snippet.name%>', '<%=snippet.version%>');return false;">Delete</a> | <a href="code/snippet/<%=snippet.ref%>/<%=snippet.version%>">Code Snippet</a> | <a href="edit/snippet/<%=snippet.ref%>/<%=snippet.version%>">Edit</a> | <a href="publish/snippet/<%=snippet.ref%>">Publish Snippet</a></td>
49+
<td><a href="" onClick="snippetDeletePrompt('<%=snippet.ref%>', '<%=snippet.name%>', '<%=snippet.version%>');return false;">Delete</a> | <a href="code/snippet/<%=snippet.ref%>/<%=snippet.version%>">Code Snippet</a> | <a href="edit/snippet/<%=snippet.ref%>">Edit</a> | <a href="publish/snippet/<%=snippet.ref%>">Publish Snippet</a></td>
5050
</tr>
5151
<% });
5252
}
@@ -86,7 +86,7 @@
8686
<% } %>
8787
</td>
8888
<td><a href="code/snippet/<%=snippet.ref%>/<%=snippet.version%>" name='codeSnippet'><%= snippet.latestPublished ? "View" : "Code" %> Snippet</a> | <a href="edit/snippet/<%=snippet.ref%>/<%=snippet.version%>" name='editSnippet'>Edit</a></td>
89-
<td><%- !snippet.latestPublished ? `<a href='' onClick='revisionPublishPrompt("${snippet.ref}", "${snippet.name}", "${snippet.version}"); return false;'>Publish revision ${snippet.version}</a>` : `<a href='code/snippet/${snippet.ref}/newRevision'>New Revision</a>` %> | <a href="edit/snippet/<%=snippet.ref%>/<%=snippet.version%>" name='editSnippet'>Edit</a></td>
89+
<td><%- !snippet.latestPublished ? `<a href='' onClick='revisionPublishPrompt("${snippet.ref}", "${snippet.name}", "${snippet.version}"); return false;'>Publish revision ${snippet.version}</a>` : `<a href='code/snippet/${snippet.ref}/newRevision'>New Revision</a>` %> | <a href="edit/snippet/<%=snippet.ref%>" name='editSnippet'>Edit</a></td>
9090
</tr>
9191
<% });
9292
}

0 commit comments

Comments
 (0)