Skip to content

Commit eed4ae2

Browse files
committed
Enable description preview in task edit view
Rails 7 では `javascript_pack_tag` が使えないため,インラインスクリプトで記述するように変更した.
1 parent ebc1fe8 commit eed4ae2

1 file changed

Lines changed: 15 additions & 1 deletion

File tree

app/views/tasks/_form.html.erb

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@
105105
<input id="preview" type="radio" name="tab_item">
106106
<label class="tab_item" for="preview">Preview</label>
107107
<div class="tab_content" id="write_content">
108-
<%= form.text_area :description, class: "border-2 w-full", rows: 10, id: "write-area", placeholder: "内容を入力してください" %>
108+
<%= form.text_area :description, class: "border-2 w-full", rows: 10, id: "write-area", placeholder: "タスクの説明を入力してください" %>
109109
</div>
110110
<div class="tab_content" id="preview_content">
111111
<div style="height: 300px" class="overflow-y-scroll">
@@ -119,5 +119,19 @@
119119
<%= form.submit "保存", class: "btn btn-danger" %>
120120
</div>
121121

122+
<script>
123+
document.getElementById('preview').onchange=function(){
124+
const text = document.getElementById("write-area").value
125+
const preview = document.getElementById("preview-area")
126+
const obj = {text: text};
127+
const method = "POST";
128+
const body = JSON.stringify(obj);
129+
const headers = {
130+
'Accept': 'application/json',
131+
'Content-Type': 'application/json'
132+
};
133+
fetch("/documents/api_markdown", {method, headers, body}).then((res)=> res.json()).then((obj) => {preview.innerHTML=obj.text}).catch(console.error);}
134+
</script>
135+
122136
<%= javascript_include_tag 'prevent_transition_in_editing' %>
123137
<% end %>

0 commit comments

Comments
 (0)