git ssb

30+

cel / git-ssb-web



Commit d3c935772de1f3a9100852af7769cc9083245340

Add issue comment markdown preview

Charles Lehner committed on 3/27/2016, 12:26:39 PM
Parent: 53bfcec9abb5e9b039c3f0c5ed66680cc677e14f

Files changed

index.jschanged
static/styles.csschanged
index.jsView
@@ -239,8 +239,22 @@
239239 })
240240 )
241241 }
242242
243+var issueCommentScript = '(' + function () {
244+ var $ = document.getElementById.bind(document)
245+ $('preview-tab-link').onclick = function (e) {
246+ with (new XMLHttpRequest()) {
247+ open('POST', '', true)
248+ onload = function() {
249+ $('preview-tab').innerHTML = responseText
250+ }
251+ send('action=markdown&text=' +
252+ encodeURIComponent($('comment-text').value))
253+ }
254+ }
255+}.toString() + ')()'
256+
243257 var msgTypes = {
244258 'git-repo': true,
245259 'git-update': true,
246260 'issue': true
@@ -369,8 +383,9 @@
369383
370384 case 'comment':
371385 if (!data.id)
372386 return cb(null, serveError(new Error('Missing id'), 400))
387+
373388 // TODO: add ref mentions
374389 var msg = schemas.post(data.text, data.id, data.branch || data.id)
375390 if (data.open != null)
376391 Issues.schemas.opens(msg, data.id)
@@ -390,8 +405,11 @@
390405 if (err) return cb(null, serveError(err))
391406 cb(null, serveRedirect(encodeLink(issue.id)))
392407 })
393408
409+ case 'markdown':
410+ return cb(null, serveMarkdown(data.text))
411+
394412 default:
395413 cb(null, servePlainError(400, 'What are you trying to do?'))
396414 }
397415 })
@@ -465,8 +483,19 @@
465483 msg
466484 ])
467485 }
468486
487+ function serveMarkdown(text) {
488+ var html = markdown(text)
489+ return pull.values([
490+ [200, {
491+ 'Content-Length': Buffer.byteLength(html),
492+ 'Content-Type': 'text/html; charset=utf-8'
493+ }],
494+ html
495+ ])
496+ }
497+
469498 function renderTry(read) {
470499 var ended
471500 return function (end, cb) {
472501 if (ended) return cb(ended)
@@ -1172,10 +1201,10 @@
11721201 return renderRepoPage(repo, '', pull.once(
11731202 '<h3>New Issue</h3>' +
11741203 '<section><form class="new-issue" action="" method="post">' +
11751204 '<input type="hidden" name="action" value="new-issue">' +
1176- '<p><input class="wide-input" name="title" placeholder="Issue Title" size="69" /></p>' +
1177- '<p><textarea class="wide-input" name="text" placeholder="Description" rows="12" cols="69"></textarea></p>' +
1205+ '<p><input class="wide-input" name="title" placeholder="Issue Title" size="77" /></p>' +
1206+ '<p><textarea class="wide-input" name="text" placeholder="Description" rows="12" cols="77"></textarea></p>' +
11781207 '<button type="submit" class="btn">Create</button>' +
11791208 '</form></section>'))
11801209 }
11811210
@@ -1251,18 +1280,29 @@
12511280 }
12521281 })
12531282 ),
12541283 pull.once(isPublic ? '' : '<section><form action="" method="post">' +
1284+ '<input type="radio" class="tab-radio" id="tab1" name="tab" checked="checked"/>' +
1285+ '<input type="radio" class="tab-radio" id="tab2" name="tab"/>' +
1286+ '<div class="tab-links">' +
1287+ '<label for="tab1" id="write-tab-link" class="tab1-link">Write</label>' +
1288+ '<label for="tab2" id="preview-tab-link" class="tab2-link">Preview</label>' +
1289+ '</div>' +
1290+ '<div id="write-tab" class="tab1">' +
12551291 '<input type="hidden" name="action" value="comment">' +
12561292 '<input type="hidden" name="id" value="' + issue.id + '">' +
1257- '<textarea name="text" class="wide-input" rows="6" cols="69"></textarea>' +
1293+ '<textarea id="comment-text" name="text" class="wide-input" rows="4" cols="77"></textarea>' +
1294+ '</div>' +
1295+ '<div class="preview-text tab2" id="preview-tab">' +
1296+ '</div>' +
12581297 (isAuthor ?
12591298 '<input type="submit" class="btn"' +
12601299 ' name="' + (issue.open ? 'close' : 'open') + '"' +
12611300 ' value="' + (issue.open ? 'Close issue' : 'Reopen issue') + '"' +
12621301 '/>' : '') +
12631302 '<input type="submit" class="btn open" value="Comment" />' +
1264- '</form></section>')
1303+ '<script>' + issueCommentScript + '</script>' +
1304+ '</section></form>')
12651305 ]))
12661306 }
12671307
12681308 }
static/styles.cssView
@@ -110,15 +110,19 @@
110110 margin-left: 1ex;
111111 width: 20em;
112112 }
113113
114+label {
115+ cursor: pointer;
116+}
117+
114118 .name-toggle {
115119 font-size: 1.3em;
116- cursor: pointer;
117120 margin-left: .25ex;
118121 }
119122
120-.name-checkbox {
123+.name-checkbox,
124+.tab-radio {
121125 position: absolute;
122126 opacity: 0;
123127 }
124128
@@ -197,9 +201,10 @@
197201
198202 .wide-input {
199203 width: 99%;
200204 display: block;
201- font-size: 1.1em;
205+ font-size: inherit;
206+ line-height: inherit;
202207 }
203208
204209 /* issues */
205210
@@ -234,4 +239,33 @@
234239 overflow: hidden;
235240 white-space: nowrap;
236241 text-overflow: ellipsis;
237242 }
243+
244+.tab-links label {
245+ padding: 1ex;
246+ color: #333;
247+}
248+
249+#tab1:checked ~ .tab-links .tab1-link,
250+#tab2:checked ~ .tab-links .tab2-link {
251+ text-decoration: underline;
252+}
253+
254+#tab1:not(:checked) ~ .tab1,
255+#tab2:not(:checked) ~ .tab2 {
256+ display: none;
257+}
258+
259+.preview-text {
260+ border: 1px solid #c0bdba;
261+ min-height: 6em;
262+ margin: .25ex 0;
263+ padding: .25ex;
264+}
265+
266+.preview-text :first-child {
267+ margin-top: 0;
268+}
269+.preview-text :last-child {
270+ margin-bottom: 0;
271+}

Built with git-ssb-web