article.jsp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
  2. <%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
  3. <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
  4. <%@ taglib uri="http://www.springframework.org/tags" prefix="spring" %>
  5. <%@ taglib uri = "http://java.sun.com/jsp/jstl/functions" prefix = "fn" %>
  6. <%@ page language="java" contentType="text/html;charset=UTF-8" pageEncoding="UTF-8" %>
  7. <jsp:include page="/WEB-INF/jsp/include/head.jsp"></jsp:include>
  8. <script>
  9. const tagSet = new Set();
  10. let editForm = null;
  11. let tagInputArea = null;
  12. let tagList = null;
  13. window.onload = () => {
  14. editForm = window.document.querySelector('#editor-form');
  15. tagInputArea = window.document.querySelector('#tag-input-area');
  16. tagList = window.document.querySelector('.tag-list');
  17. const article = "${article}";
  18. if (article !== "") {
  19. editForm['article-title'].value = "${article.title}";
  20. editForm['subtitle'].value = "${article.subtitle}";
  21. editForm['article-body'].value = "${article.content}";
  22. const strTags = "${article.tags}";
  23. strTags.split(',').forEach(tag => {
  24. addTag(tag);
  25. });
  26. }
  27. }
  28. function enterKeys(){
  29. const target = event.target;
  30. if (event.keyCode === 13) {
  31. event.preventDefault();
  32. const inputValue = tagInputArea.value.trim();
  33. if (inputValue !== '') {
  34. addTag(tagInputArea.value.trim());
  35. }
  36. }
  37. }
  38. const addTag = (tag) => {
  39. if (!tagSet.has(tag)) {
  40. const spanTag = window.document.createElement('span');
  41. spanTag.classList.add('tag');
  42. spanTag.textContent = tag;
  43. const btnDelete = window.document.createElement('i');
  44. btnDelete.classList.add('fas');
  45. btnDelete.classList.add('fa-times');
  46. btnDelete.onclick = (event) => {
  47. tagList.removeChild(spanTag); // 엘리먼트 제거
  48. tagSet.delete(tag); // 태그 집합에서 태그 제거
  49. }
  50. spanTag.append(btnDelete);
  51. console.log(spanTag);
  52. tagList.append(spanTag);
  53. tagSet.add(tag);
  54. tagInputArea.value = "";
  55. }
  56. }
  57. const editFormSubmit = () => {
  58. if (validateForm()) {
  59. const json = {};
  60. json['title'] = editForm['article-title'].value;
  61. json['subtitle'] = editForm['subtitle'].value;
  62. json['content'] = editForm['article-body'].value;
  63. json['tags'] = Array.from(tagSet).join(',');
  64. json['created'] = new Date();
  65. const options = {
  66. body: JSON.stringify(json),
  67. method: "post",
  68. headers: {
  69. "Content-Type": "application/json"
  70. }
  71. };
  72. fetch("/article", options)
  73. .then(response => {
  74. if (response.status === 201) {
  75. return response.json();
  76. }
  77. })
  78. .then(json => {
  79. location.href = "/article/" + json.id;
  80. })
  81. }
  82. }
  83. const validateForm = () => {
  84. const articleTitle = editForm['article-title'].value;
  85. if(articleTitle === null || articleTitle === ""){
  86. alert("제목을 입력해주세요.");
  87. return false;
  88. }
  89. const subtitle = editForm['subtitle'].value;
  90. if(subtitle === null || subtitle === ""){
  91. alert("부제목을 입력해주세요.");
  92. return false;
  93. }
  94. const articleBody = editForm['article-body'].value;
  95. if(articleBody === null || articleBody === ""){
  96. alert("내용을 입력해주세요.");
  97. return false;
  98. }
  99. return true;
  100. }
  101. </script>
  102. </head>
  103. <body>
  104. <jsp:include page="/WEB-INF/jsp/include/header.jsp"></jsp:include>
  105. <!-- Editor -->
  106. <div class="container main editor-page">
  107. <div class="row">
  108. <div class="col-10">
  109. <form
  110. name="editorForm"
  111. id="editor-form"
  112. class="form-group"
  113. method="post"
  114. action="/article"
  115. onsubmit="return false"
  116. >
  117. <!-- onsubmit="return validateForm()" -->
  118. <div class="form-data">
  119. <input
  120. type="text"
  121. class="form-control"
  122. name="article-title"
  123. placeholder ="Article Title"
  124. autocomplete="off"
  125. >
  126. </div>
  127. <div class="form-data">
  128. <input
  129. type="text"
  130. class="form-control form-control-sm"
  131. name="subtitle"
  132. placeholder ="What's this article about?"
  133. autocomplete="off"
  134. >
  135. </div>
  136. <div class="form-data">
  137. <textarea
  138. class="form-control form-control-sm"
  139. rows="8"
  140. name="article-body"
  141. placeholder="Write your article (in markdown)"
  142. ></textarea>
  143. </div>
  144. <div class="form-data">
  145. <input
  146. id="tag-input-area"
  147. type="text"
  148. class="form-control form-control-sm tag-form"
  149. name="tags"
  150. placeholder="Enter tags"
  151. autocomplete="off"
  152. onkeydown="enterKey()"
  153. >
  154. <div class="tag-list">
  155. </div>
  156. </div>
  157. <button class="btn-signUp" onclick="editFormSubmit()">Publish Article</button>
  158. </form>
  159. </div>
  160. </div>
  161. </div>
  162. </body>
  163. </html>