Skip to content

Commit 31efb27

Browse files
committed
feature/Add checkbox controller and public/private toggle
1 parent d5671a7 commit 31efb27

File tree

7 files changed

+61
-17
lines changed

7 files changed

+61
-17
lines changed

app/assets/stylesheets/helpers.scss

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
.inline-flex { display: inline-flex; }
77
.flex-column { flex-direction: column; }
88
.v-center { align-items: center; }
9+
.v-bottom { align-items: baseline; }
910
.h-center { justify-content: center; }
1011
.flex-end { justify-content: flex-end; }
1112

app/assets/stylesheets/snippets.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@
141141

142142
.create-snippet--options-wrapper {
143143
display: flex;
144-
justify-content: flex-end;
144+
justify-content: space-between;
145145
margin-top: 16px;
146146
}
147147

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { Controller } from 'stimulus';
2+
3+
export default class extends Controller {
4+
static targets = ['text', 'icon', 'value']
5+
static values = {
6+
checkedText: String,
7+
uncheckedText: String,
8+
checkedIcon: String,
9+
uncheckedIcon: String
10+
}
11+
12+
toggle() {
13+
this.toggleText();
14+
this.toggleIcon();
15+
}
16+
17+
toggleText() {
18+
console.log()
19+
}
20+
21+
toggleIcon() {
22+
if (this.isChecked) {
23+
this.textTarget.innerHTML = this.uncheckedTextValue
24+
this.iconTarget.setAttribute('src', this.uncheckedIconValue)
25+
this.valueTarget.value = false
26+
} else {
27+
this.textTarget.innerHTML = this.checkedTextValue
28+
this.iconTarget.setAttribute('src', this.checkedIconValue)
29+
this.valueTarget.value = true
30+
}
31+
}
32+
33+
get isChecked() {
34+
return this.currentText === this.checkedTextValue
35+
}
36+
37+
get currentText() {
38+
return this.textTarget.innerHTML
39+
}
40+
}

app/javascript/controllers/highlight_controller.js

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,9 @@ import { Controller } from 'stimulus';
22
import marked from 'marked';
33

44
export default class extends Controller {
5-
static targets = ["code", "languages", "highlightedBody", "description"];
5+
static targets = ["code", "highlightedBody", "description"];
66

77
connect() {
8-
this.addLanguagesToSelect()
9-
108
marked.setOptions({
119
highlight: function(code, lang) {
1210
if (lang && lang !== '') {
@@ -24,22 +22,11 @@ export default class extends Controller {
2422
this.descriptionTarget.innerHTML = event.target.value
2523
}
2624

27-
// TODO: Pass in the selected language!!!
2825
highlight(event) {
2926
const rawCode = event.currentTarget.value
3027
const highlightedCode = marked(rawCode)
3128

3229
this.codeTarget.innerHTML = highlightedCode
3330
this.highlightedBodyTarget.value = highlightedCode
3431
}
35-
36-
addLanguagesToSelect() {
37-
hljs.listLanguages().forEach(language => {
38-
const option = document.createElement('option')
39-
option.text = language
40-
option.value = language
41-
42-
this.languagesTarget.add(option)
43-
})
44-
}
4532
}

app/views/modals/snippets/new.html.erb

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,22 @@
3030
</div>
3131

3232
<div class="create-snippet--options-wrapper">
33-
<%= f.select :folder_id, options_from_collection_for_select(@folders, :id, :name, params[:folder_id]), prompt: 'Select folder...' %>
34-
<%= f.submit 'SAVE', class: "margin-left button--cta-new" %>
33+
<div class="flex v-center" style="cursor: pointer;"
34+
data-controller="checkbox"
35+
data-action="click->checkbox#toggle"
36+
data-checkbox-checked-text-value="Public"
37+
data-checkbox-checked-icon-value="icons/lock-open.svg"
38+
data-checkbox-unchecked-text-value="Private"
39+
data-checkbox-unchecked-icon-value="icons/lock-closed.svg"
40+
>
41+
<span data-checkbox-target="text">Public</span>
42+
<img data-checkbox-target="icon" src="icons/lock-open.svg" width="20">
43+
<%= f.hidden_field :public, value: true, data: { checkbox_target: 'value' } %>
44+
</div>
45+
<div class="flex">
46+
<%= f.select :folder_id, options_from_collection_for_select(@folders, :id, :name, params[:folder_id]), prompt: 'Select folder...' %>
47+
<%= f.submit 'SAVE', class: "margin-left button--cta-new" %>
48+
</div>
3549
</div>
3650
<% end %>
3751

public/icons/lock-closed.svg

Lines changed: 1 addition & 0 deletions
Loading

public/icons/lock-open.svg

Lines changed: 1 addition & 0 deletions
Loading

0 commit comments

Comments
 (0)