Skip to content

Commit 2f669a9

Browse files
committed
feat: adding markdown editor using simplemde
1 parent b27d1e9 commit 2f669a9

File tree

5 files changed

+72
-5
lines changed

5 files changed

+72
-5
lines changed

app/assets/javascripts/simple_discussion/controllers/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import { application } from "./application"
22

33
import DropdownController from "./dropdown_controller"
44
import ReportSpamController from "./report_spam_controller";
5+
import SimplemdeController from "./simplemde_controller";
56

67
application.register("dropdown", DropdownController);
78
application.register("report-spam", ReportSpamController);
9+
application.register("simplemde",SimplemdeController);
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { Controller } from "@hotwired/stimulus"
2+
//import SimpleMDE from "simplemde";
3+
4+
export default class extends Controller {
5+
static targets = ["textarea"]
6+
7+
connect() {
8+
this.initializeEditor();
9+
10+
const previewButton = document.querySelector(".preview")
11+
previewButton.style.width = "80px"
12+
previewButton.style.height = "34px"
13+
}
14+
15+
initializeEditor() {
16+
this.editor = new SimpleMDE({
17+
element: this.textareaTarget,
18+
forceSync: true,
19+
toolbar: [
20+
"bold",
21+
"italic",
22+
"heading",
23+
"|",
24+
"quote",
25+
"unordered-list",
26+
"ordered-list",
27+
"|",
28+
"link",
29+
{
30+
name: "preview",
31+
className: "preview no-disable",
32+
action: function(editor) {
33+
SimpleMDE.togglePreview(editor);
34+
},
35+
title: "Preview",
36+
}
37+
],
38+
spellChecker: false,
39+
});
40+
}
41+
}

app/assets/stylesheets/simple_discussion.scss

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,3 +296,25 @@
296296
.thread-page-container {
297297
padding: 24px;
298298
}
299+
300+
.preview::before {
301+
content: "Preview";
302+
width: 80px;
303+
}
304+
305+
p {
306+
font-size: 18px;
307+
}
308+
309+
blockquote {
310+
border-left: 5px solid #e9ecef;
311+
padding: 10px 20px;
312+
margin: 10px 0;
313+
font-size: 18px;
314+
font-weight: 500;
315+
color: #6c757d;
316+
}
317+
318+
.CodeMirror-line span {
319+
font-size: 18px;
320+
}

app/views/layouts/simple_discussion.html.erb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@
7676

7777
<% parent_layout("application") %>
7878

79+
<link rel="stylesheet" href="https://cdn.jsdelivr.net/simplemde/latest/simplemde.min.css">
80+
<script src="https://cdn.jsdelivr.net/simplemde/latest/simplemde.min.js"></script>
81+
7982
<script type="module">
8083
import { Application, Controller } from "https://unpkg.com/@hotwired/stimulus/dist/stimulus.js"
8184
window.Stimulus = Application.start()
@@ -117,3 +120,4 @@
117120
}
118121
})
119122
</script>
123+
Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<div class="mt-4"></div>
22
<%= form_for [@forum_thread, @forum_post],
33
url: (@forum_post.persisted? ? simple_discussion.forum_thread_forum_post_path(@forum_thread, @forum_post) : simple_discussion.forum_thread_forum_posts_path(@forum_thread)),
4-
html: { data: { behavior: "comment-form" } } do |f| %>
4+
html: { data: { behavior: "comment-form", controller: "simplemde" } } do |f| %>
55

66
<% if @forum_post.errors.any? %>
77
<div class="alert alert-danger" role="alert">
@@ -13,12 +13,10 @@
1313

1414
<div class="d-block position-relative">
1515
<div class="form-group">
16-
<%= f.text_area :body, placeholder: t('add_a_comment'), rows: 8, class: "form-control simplemde", data: { behavior: "comment-body" } %>
16+
<%= f.text_area :body, placeholder: t('add_a_comment'), rows: 8, class: "form-control", data: { behavior: "comment-body", simplemde_target: "textarea"} %>
17+
<%#= link_to "Parsed with Markdown", "https://guides.github.com/features/mastering-markdown/", target: "_blank" %>
1718
</div>
1819
<%= f.button "#{f.object.new_record? ? t('comment') : t('update_comment') }", class: "btn forum-primary-btn", style: "bottom: 10px; right: 10px", data: {disable_with: "<i class='fa fa-spinner fa-spin'></i> #{t('saving_comment')}"} %>
1920
</div>
20-
2121
<%# Describe text formatting options here with a link %>
22-
<%#= link_to "Parsed with Markdown", "https://guides.github.com/features/mastering-markdown/", target: "_blank" %>
23-
2422
<% end %>

0 commit comments

Comments
 (0)