Skip to content

Commit b6cdde6

Browse files
author
Lewis Youl
committed
feat: add mime types and extensions to language + add ability to download raw code file
1 parent 709fbf5 commit b6cdde6

File tree

15 files changed

+101
-20
lines changed

15 files changed

+101
-20
lines changed

app/helpers/application_helper.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def new_snippet_folder_id
4444
end
4545

4646
def color_for_like_status(snippet)
47-
snippet.liked_by?(current_user) ? 'text-red-500' : 'text-gray-500'
47+
snippet.liked_by?(current_user) ? 'text-red-500' : 'text-gray-500 group-hover:text-red-500'
4848
end
4949

5050
def color_class_for_notification_icon(icon)

app/javascript/controllers/counts_controller.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,23 @@ export default class extends Controller {
88

99
increment(event) {
1010
this.countValue += 1
11-
this.countTarget.textContent = this.countValue
11+
12+
this.renderValueAndMargin()
1213
}
1314

1415
decrement(event) {
1516
this.countValue -= 1
16-
this.countTarget.textContent = this.countValue
17+
18+
this.renderValueAndMargin()
19+
}
20+
21+
renderValueAndMargin() {
22+
if (this.countValue > 0) {
23+
this.countTarget.textContent = this.countValue
24+
this.countTarget.classList.add('ml-1')
25+
} else {
26+
this.countTarget.classList.remove('ml-1')
27+
this.countTarget.textContent = ''
28+
}
1729
}
1830
}

app/javascript/controllers/picture_controller.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ import axios from 'axios'
44

55
export default class extends Controller {
66
static targets = ['code']
7+
static values = {
8+
code: String,
9+
extension: String,
10+
mimeType: String
11+
}
712

813
initialize() {
914
const csrfToken = document.querySelector("meta[name=csrf-token]").content
@@ -30,6 +35,27 @@ export default class extends Controller {
3035
.catch(console.error)
3136
}
3237

38+
downloadRaw() {
39+
const fileName = `snippet.${this.extensionValue}`
40+
const blob = new Blob([this.codeValue], { type: this.mimeTypeValue });
41+
let anchorElement = document.createElement('a');
42+
43+
anchorElement.download = fileName;
44+
anchorElement.href = URL.createObjectURL(blob);
45+
anchorElement.dataset.downloadurl = [
46+
this.mimeTypeValue,
47+
anchorElement.download,
48+
anchorElement.href
49+
].join(':');
50+
anchorElement.style.display = "none";
51+
52+
document.body.appendChild(anchorElement);
53+
anchorElement.click();
54+
document.body.removeChild(anchorElement);
55+
56+
setTimeout(function() { URL.revokeObjectURL(anchorElement.href); }, 1500);
57+
}
58+
3359
tweetImage(blob) {
3460
let formData = new FormData();
3561
formData.append('fname', 'tweet.png');

app/models/language.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
11
class Language < ApplicationRecord
22
validates_uniqueness_of :name
3+
4+
def mime_type
5+
mime_types.first
6+
end
7+
8+
def extension
9+
extensions.first
10+
end
311
end

app/tasks/update_languages_task.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class UpdateLanguagesTask
2+
def call
3+
LANGUAGES.each do |lang|
4+
language = Language.find_or_initialize_by(name: lang[:name])
5+
6+
language.update!(
7+
mime_types: lang[:mime] ? [lang[:mime]] : lang[:mimes],
8+
extensions: lang[:ext] || ['txt']
9+
)
10+
end
11+
end
12+
end
Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1-
<h3 class="text-center mb-4">Capture Image</h3>
1+
<h3 class="text-center mb-4">Share Snippet</h3>
22

33
<h5 class="mb-2"><%= @snippet.description %></h5>
4-
<div data-controller="picture">
4+
<div
5+
data-controller="picture"
6+
data-picture-code-value="<%= @snippet.body %>"
7+
data-picture-extension-value="<%= @snippet.language.extension %>"
8+
data-picture-mime-type-value="<%= @snippet.language.mime_type %>"
9+
>
510
<div data-picture-target="code" class="bg-cyan px-16 py-20">
611
<%= render partial: 'shared/codemirror', locals: { code: @snippet.body, mode_id: @snippet.language_id, read_only: false, capture: true } %>
712
</div>
@@ -12,9 +17,14 @@
1217
<span class="font-normal">Tweet</span>
1318
<% end %>
1419

15-
<%= button_tag class: 'button--cta-tertiary', data: { action: 'click->picture#download' } do %>
20+
<%= button_tag class: 'button--cta-tertiary mr-2', data: { action: 'click->picture#download' } do %>
1621
<%= render partial: 'shared/icons/download', locals: { classes: 'h-5 w-5 mr-1' } %>
17-
<span class="font-normal">Download</span>
22+
<span class="font-normal">PNG</span>
23+
<% end %>
24+
25+
<%= button_tag class: 'button--cta-tertiary', data: { action: 'click->picture#downloadRaw' } do %>
26+
<%= render partial: 'shared/icons/download_document', locals: { classes: 'h-5 w-5 mr-1' } %>
27+
<span class="font-normal">Raw</span>
1828
<% end %>
1929
</div>
2030
</div>

app/views/shared/_comment_button.html.erb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
<%= link_to snippet_path(snippet, anchor: 'new-comment'), class: "no-underline flex items-center cursor-pointer transition" do %>
2-
<%= render partial: 'shared/icons/chat', locals: { width: 5, height: 5, color: 'text-gray-500' } %>
1+
<%= link_to snippet_path(snippet, anchor: 'new-comment'), class: "no-underline flex items-center cursor-pointer transition group border border-gray-300 rounded-md px-2 py-1 hover:border-purple-500" do %>
2+
<%= render partial: 'shared/icons/chat', locals: { width: 5, height: 5, color: 'text-gray-500 group-hover:text-purple-500' } %>
33
<span
44
data-controller='counts'
55
data-counts-count-value="<%= snippet.comments_count %>"
66
data-action='comment-created@window->counts#increment comment-deleted@window->counts#decrement'
77
data-counts-target="count"
8-
class="inline-block text-sm text-gray-500 ml-1 -mb-0.5"
8+
class="inline-block text-sm text-gray-500 -mb-0.5 <%= snippet.comments? ? 'ml-1' : '' %>"
99
>
1010
<% if snippet.comments? %>
1111
<%= snippet.comments_count %>

app/views/shared/_like_button.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<% end %>
1212
>
1313
<div
14-
class="flex h-full items-center mr-2"
14+
class="flex h-full items-center mr-2 group border border-gray-300 rounded-md px-2 py-1 hover:border-red-500"
1515
data-action="click->like#toggle"
1616
>
1717
<%= render partial: 'shared/icons/heart', locals: { width: 5, height: 5, color: color_for_like_status(snippet) } %>
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
<%= button_tag(
2-
class: "ml-2 no-underline flex items-center cursor-pointer transition",
2+
title: 'Share or download this snippet',
3+
class: "group ml-2 no-underline flex items-center cursor-pointer transition border border-gray-300 rounded-md px-2 py-1 hover:border-cyan",
34
data: {
45
controller: 'modal',
56
action: 'modal#present',
6-
modal_url_value: picture_modals_snippet_path(snippet: snippet)
7+
modal_url_value: picture_modals_snippet_path(snippet)
78
}
89
) do %>
9-
<%= render partial: 'shared/icons/camera', locals: { classes: 'w-5 h-5 text-gray-500' } %>
10+
<%= render partial: 'shared/icons/share', locals: { classes: 'w-5 h-5 text-gray-500 group-hover:text-cyan' } %>
1011
<% end %>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<svg class="<%= classes %>" fill="currentColor" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg">
2+
<path fill-rule="evenodd" d="M6 2a2 2 0 00-2 2v12a2 2 0 002 2h8a2 2 0 002-2V7.414A2 2 0 0015.414 6L12 2.586A2 2 0 0010.586 2H6zm5 6a1 1 0 10-2 0v3.586l-1.293-1.293a1 1 0 10-1.414 1.414l3 3a1 1 0 001.414 0l3-3a1 1 0 00-1.414-1.414L11 11.586V8z" clip-rule="evenodd"></path>
3+
</svg>

0 commit comments

Comments
 (0)