-
Notifications
You must be signed in to change notification settings - Fork 71
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
practice_memo.vueをhtmlとjsでの実装に変更 #7442
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,132 @@ | ||
import Vue from 'vue' | ||
import PracticeMemo from 'practice_memo.vue' | ||
import CSRF from 'csrf' | ||
import TextareaInitializer from 'textarea-initializer' | ||
import MarkdownInitializer from 'markdown-initializer' | ||
import { toast } from 'toast_react' | ||
|
||
document.addEventListener('DOMContentLoaded', () => { | ||
const selector = '#js-practice-memo' | ||
const practiceMemo = document.querySelector(selector) | ||
const practiceMemo = document.querySelector('.practice-memo') | ||
if (practiceMemo) { | ||
const practiceId = practiceMemo.getAttribute('data-practice-id') | ||
new Vue({ | ||
render: (h) => | ||
h(PracticeMemo, { | ||
props: { | ||
practiceId: practiceId | ||
} | ||
}) | ||
}).$mount(selector) | ||
TextareaInitializer.initialize('.a-markdown-input__textarea') | ||
const markdownInitializer = new MarkdownInitializer() | ||
const practiceId = practiceMemo.dataset.practice_id | ||
let savedMemo = '' | ||
|
||
const memoDisplay = practiceMemo.querySelector('.memo-display') | ||
const memoEditor = practiceMemo.querySelector('.memo-editor') | ||
const memoDisplayContent = memoDisplay.querySelector('.a-long-text') | ||
const memoEditorPreview = memoEditor.querySelector( | ||
'.a-markdown-input__preview' | ||
) | ||
const editorTextarea = memoEditor.querySelector( | ||
'.a-markdown-input__textarea' | ||
) | ||
|
||
fetch(`/api/practices/${practiceId}.json`, { | ||
method: 'GET', | ||
headers: { | ||
'X-Requested-With': 'XMLHttpRequest' | ||
}, | ||
credentials: 'same-origin', | ||
redirect: 'manual' | ||
}) | ||
.then((response) => { | ||
return response.json() | ||
}) | ||
.then((json) => { | ||
if (json.memo) { | ||
savedMemo = json.memo | ||
editorTextarea.value = savedMemo | ||
switchMemoDisplay(memoDisplay, savedMemo) | ||
memoDisplayContent.innerHTML = markdownInitializer.render(savedMemo) | ||
memoEditorPreview.innerHTML = markdownInitializer.render(savedMemo) | ||
} | ||
}) | ||
.catch((error) => { | ||
console.warn(error) | ||
}) | ||
|
||
const editButton = memoDisplay.querySelector('.card-main-actions__action') | ||
const modalElements = [memoDisplay, memoEditor] | ||
editButton.addEventListener('click', () => | ||
toggleClass(modalElements, 'is-hidden') | ||
) | ||
|
||
const saveButton = memoEditor.querySelector('.is-primary') | ||
saveButton.addEventListener('click', () => { | ||
toggleClass(modalElements, 'is-hidden') | ||
savedMemo = editorTextarea.value | ||
updateMemo(savedMemo, practiceId) | ||
memoDisplayContent.innerHTML = markdownInitializer.render(savedMemo) | ||
switchMemoDisplay(memoDisplay, savedMemo) | ||
}) | ||
|
||
const cancelButton = memoEditor.querySelector('.is-secondary') | ||
cancelButton.addEventListener('click', () => { | ||
toggleClass(modalElements, 'is-hidden') | ||
editorTextarea.value = savedMemo | ||
memoEditorPreview.innerHTML = markdownInitializer.render(savedMemo) | ||
}) | ||
|
||
editorTextarea.addEventListener('change', () => { | ||
memoEditorPreview.innerHTML = markdownInitializer.render( | ||
editorTextarea.value | ||
) | ||
}) | ||
|
||
const editorTab = memoEditor.querySelector('.editor-tab') | ||
const editorTabContent = memoEditor.querySelector('.is-editor') | ||
const previewTab = memoEditor.querySelector('.preview-tab') | ||
const previewTabContent = memoEditor.querySelector('.is-preview') | ||
|
||
const tabElements = [ | ||
editorTab, | ||
editorTabContent, | ||
previewTab, | ||
previewTabContent | ||
] | ||
editorTab.addEventListener('click', () => | ||
toggleClass(tabElements, 'is-active') | ||
) | ||
previewTab.addEventListener('click', () => | ||
toggleClass(tabElements, 'is-active') | ||
) | ||
} | ||
}) | ||
|
||
function updateMemo(memo, practiceId) { | ||
const params = { | ||
practice: { | ||
memo: memo | ||
} | ||
} | ||
fetch(`/api/practices/${practiceId}`, { | ||
method: 'PUT', | ||
headers: { | ||
'Content-Type': 'application/json; charset=utf-8', | ||
'X-Requested-With': 'XMLHttpRequest', | ||
'X-CSRF-Token': CSRF.getToken() | ||
}, | ||
credentials: 'same-origin', | ||
redirect: 'manual', | ||
body: JSON.stringify(params) | ||
}) | ||
.then(() => { | ||
toast('保存しました!') | ||
}) | ||
.catch((error) => { | ||
console.warn(error) | ||
}) | ||
} | ||
|
||
function switchMemoDisplay(memoDisplay, memo) { | ||
const memoContent = memoDisplay.querySelector('.any-memo') | ||
const noMemoMessage = memoDisplay.querySelector('.no-memo') | ||
memoContent.classList.toggle('is-hidden', memo === '') | ||
noMemoMessage.classList.toggle('is-hidden', memo !== '') | ||
} | ||
|
||
function toggleClass(elements, className) { | ||
elements.forEach((element) => { | ||
element.classList.toggle(className) | ||
}) | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
.a-card.practice-memo data-practice_id=practice_id | ||
This conversation was marked as resolved.
Show resolved
Hide resolved
|
||
.memo-display | ||
.card-body | ||
.card__description.any-memo.is-hidden | ||
.a-long-text.is-md | ||
.card-list.no-memo | ||
.card-list__message | ||
.container | ||
.o-empty-message | ||
.o-empty-message__icon | ||
i.fa-regular.fa-sad-tear | ||
.o-empty-message__text | ||
| プラクティスメモはまだありません。 | ||
hr.a-border-tint | ||
footer.card-footer | ||
.card-main-actions | ||
ul.card-main-actions__items | ||
li.card-main-actions__item | ||
button.card-main-actions__action.a-button.is-sm.is-secondary.is-block | ||
i.fa-solid.fa-pen | ||
| 編集 | ||
.memo-editor.is-hidden | ||
.form-tabs.js-tabs | ||
.form-tabs__tab.js-tabs__tab.editor-tab.is-active | ||
| メモ | ||
.form-tabs__tab.js-tabs__tab.preview-tab | ||
| プレビュー | ||
.card-body | ||
.card__description | ||
.a-markdown-input.js-markdown-parent | ||
.a-markdown-input__inner.is-editor.js-tabs__content.is-active | ||
textarea.a-text-input.a-markdown-input__textarea id='js-practice-memo' data-preview='#practice-memo-preview' name='practice[memo]' | ||
.a-markdown-input__inner.is-preview.js-tabs__content | ||
.a-long-text.a-markdown-input__preview | ||
hr.a-border-tint | ||
.card-footer | ||
.card-main-actions | ||
.card-main-actions__items | ||
.card-main-actions__item | ||
button.a-button.is-sm.is-primary.is-block | ||
| 保存する | ||
.card-main-actions__item | ||
button.a-button.is-sm.is-secondary.is-block | ||
| キャンセル |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
実際にはhiddenかactiveかを切り替えてるのみなので、以下でもいいかなと思いました。ただこちらは好みなので、無視でも大丈夫です!(というかコメントすべきところが思いつかなかっただけです。)