|
@@ -4,55 +4,37 @@
|
|
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring" %>
|
|
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring" %>
|
|
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
|
|
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
|
|
<%@ page language="java" contentType="text/html;charset=UTF-8" pageEncoding="UTF-8" %>
|
|
<%@ page language="java" contentType="text/html;charset=UTF-8" pageEncoding="UTF-8" %>
|
|
|
|
+<% pageContext.setAttribute("LF", "\n" ); %>
|
|
|
|
+
|
|
|
|
|
|
<jsp:include page="/WEB-INF/jsp/include/head.jsp"></jsp:include>
|
|
<jsp:include page="/WEB-INF/jsp/include/head.jsp"></jsp:include>
|
|
<script>
|
|
<script>
|
|
-
|
|
|
|
const tagSet = new Set();
|
|
const tagSet = new Set();
|
|
let editForm = null;
|
|
let editForm = null;
|
|
let tagInputArea = null;
|
|
let tagInputArea = null;
|
|
let tagList = null;
|
|
let tagList = null;
|
|
- let isEditPage = false;
|
|
|
|
-
|
|
|
|
- // 아티클 수정 페이지로 왔을 때 사용되는 객체
|
|
|
|
- const originArticle = {};
|
|
|
|
|
|
|
|
window.onload = () => {
|
|
window.onload = () => {
|
|
editForm = window.document.querySelector('#editor-form');
|
|
editForm = window.document.querySelector('#editor-form');
|
|
tagInputArea = window.document.querySelector('#tag-input-area');
|
|
tagInputArea = window.document.querySelector('#tag-input-area');
|
|
tagList = window.document.querySelector('.tag-list');
|
|
tagList = window.document.querySelector('.tag-list');
|
|
|
|
|
|
- const article = "${article}";
|
|
|
|
-
|
|
|
|
- if (article !== "") {
|
|
|
|
- isEditPage = true;
|
|
|
|
- originArticle['id'] = parseInt("${article.id}");
|
|
|
|
- originArticle['title'] = "${article.title}";
|
|
|
|
- originArticle['subtitle'] = "${article.subtitle}";
|
|
|
|
- originArticle['content'] = "${article.content}";
|
|
|
|
- originArticle['tags'] = "${article.tags}";
|
|
|
|
- originArticle['created'] = "${article.created}";
|
|
|
|
- originArticle['writerId'] = parseInt("${article.writerId}");
|
|
|
|
-
|
|
|
|
- editForm['article-title'].value = "${article.title}";
|
|
|
|
- editForm['subtitle'].value = "${article.subtitle}";
|
|
|
|
- editForm['article-body'].value = "${article.content}";
|
|
|
|
- // const strTags = "${article.tags}";
|
|
|
|
|
|
+ // 게시물 수정 페이지일 경우
|
|
|
|
+ if ("${editPage}" === "true") {
|
|
"${article.tags}".split(',').forEach(tag => {
|
|
"${article.tags}".split(',').forEach(tag => {
|
|
if (tag !== '') {
|
|
if (tag !== '') {
|
|
addTag(tag);
|
|
addTag(tag);
|
|
}
|
|
}
|
|
});
|
|
});
|
|
-
|
|
|
|
- console.log(originArticle);
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- function enterKey(){
|
|
|
|
|
|
+ // 엔터키 입력시 태그 생성
|
|
|
|
+ function enterKey() {
|
|
const target = event.target;
|
|
const target = event.target;
|
|
|
|
|
|
if (event.keyCode === 13) {
|
|
if (event.keyCode === 13) {
|
|
- event.preventDefault();
|
|
|
|
|
|
+ event.preventDefault(); // 새로고침 막아줌
|
|
const inputValue = tagInputArea.value.trim();
|
|
const inputValue = tagInputArea.value.trim();
|
|
if (inputValue !== '') {
|
|
if (inputValue !== '') {
|
|
addTag(tagInputArea.value.trim());
|
|
addTag(tagInputArea.value.trim());
|
|
@@ -60,6 +42,7 @@
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ // 태그 표시
|
|
const addTag = (tag) => {
|
|
const addTag = (tag) => {
|
|
if (!tagSet.has(tag)) {
|
|
if (!tagSet.has(tag)) {
|
|
const spanTag = window.document.createElement('span');
|
|
const spanTag = window.document.createElement('span');
|
|
@@ -76,8 +59,6 @@
|
|
|
|
|
|
spanTag.append(btnDelete);
|
|
spanTag.append(btnDelete);
|
|
|
|
|
|
- console.log(spanTag);
|
|
|
|
-
|
|
|
|
tagList.append(spanTag);
|
|
tagList.append(spanTag);
|
|
tagSet.add(tag);
|
|
tagSet.add(tag);
|
|
tagInputArea.value = "";
|
|
tagInputArea.value = "";
|
|
@@ -86,63 +67,58 @@
|
|
|
|
|
|
const submitForm = () => {
|
|
const submitForm = () => {
|
|
if (!validateForm()) {
|
|
if (!validateForm()) {
|
|
- return ;
|
|
|
|
|
|
+ return;
|
|
}
|
|
}
|
|
-
|
|
|
|
const json = {};
|
|
const json = {};
|
|
|
|
|
|
- if (isEditPage) {
|
|
|
|
- // 게시물 수정
|
|
|
|
- let isEdit = false;
|
|
|
|
- if (editForm['article-title'].value !== originArticle.title) {
|
|
|
|
|
|
+ // 게시글 수정페이지일 경우
|
|
|
|
+ if ("${editPage}" === "true") {
|
|
|
|
+
|
|
|
|
+ if (editForm['article-title'].value !== "${article.title}") {
|
|
json['title'] = editForm['article-title'].value;
|
|
json['title'] = editForm['article-title'].value;
|
|
- isEdit = true;
|
|
|
|
}
|
|
}
|
|
- if (editForm['subtitle'].value !== originArticle.subtitle) {
|
|
|
|
- json['subtitle'] = editForm['subtitle'].value;
|
|
|
|
- isEdit = true;
|
|
|
|
|
|
+
|
|
|
|
+ if (editForm['subtitle'].value !== "${article.subtitle}") {
|
|
|
|
+ json['subtitle'] = editForm['subtitle'].value;
|
|
}
|
|
}
|
|
- if (editForm['article-body'].value !== originArticle.content) {
|
|
|
|
|
|
+
|
|
|
|
+ if (editForm['article-body'].value.replace(/\n/g, '<br>') !== "${fn:replace(article.content, LF, '<br>')}") {
|
|
json['content'] = editForm['article-body'].value;
|
|
json['content'] = editForm['article-body'].value;
|
|
- isEdit = true;
|
|
|
|
}
|
|
}
|
|
- if (Array.from(tagSet).join(',') !== originArticle.tags) {
|
|
|
|
|
|
+
|
|
|
|
+ if (Array.from(tagSet).join(',') !== "${article.tags}") {
|
|
json['tags'] = Array.from(tagSet).join(',');
|
|
json['tags'] = Array.from(tagSet).join(',');
|
|
- isEdit = true;
|
|
|
|
}
|
|
}
|
|
|
|
|
|
- if (isEdit) {
|
|
|
|
- // json['id'] = originArticle.id; // 게시물 아이디는 url에 포함
|
|
|
|
- json['writerId'] = originArticle.writerId;
|
|
|
|
- // json['writerEmail'] = originArticle.writerEmail;
|
|
|
|
|
|
+ // 게시글 수정
|
|
|
|
+ json['writerId'] = "${ssId}";
|
|
|
|
|
|
- const options = {
|
|
|
|
- body: JSON.stringify(json),
|
|
|
|
- method: 'PATCH',
|
|
|
|
- headers: {
|
|
|
|
- "Content-Type": "application/json"
|
|
|
|
- }
|
|
|
|
- };
|
|
|
|
-
|
|
|
|
- fetch('/article/' + originArticle.id, options)
|
|
|
|
- .then(response => {
|
|
|
|
- if (response.status === 200) {
|
|
|
|
- return response.json();
|
|
|
|
- }
|
|
|
|
- })
|
|
|
|
- .then(json => {
|
|
|
|
- location.href = "/article/" + json.id;
|
|
|
|
- });
|
|
|
|
- }
|
|
|
|
|
|
+ const options = {
|
|
|
|
+ body: JSON.stringify(json),
|
|
|
|
+ method: 'PATCH',
|
|
|
|
+ headers: {
|
|
|
|
+ "Content-Type": "application/json"
|
|
|
|
+ }
|
|
|
|
+ };
|
|
|
|
|
|
|
|
+ fetch('/article/${article.id}', options)
|
|
|
|
+ .then(response => {
|
|
|
|
+ if (response.status === 200) {
|
|
|
|
+ console.log(response);
|
|
|
|
+ return response.json();
|
|
|
|
+ }
|
|
|
|
+ })
|
|
|
|
+ .then(json => {
|
|
|
|
+ location.href = "/article/" + json.id;
|
|
|
|
+ });
|
|
} else {
|
|
} else {
|
|
- // 첫 게시물 생성
|
|
|
|
|
|
+ // 첫 게시글 생성
|
|
json['title'] = editForm['article-title'].value;
|
|
json['title'] = editForm['article-title'].value;
|
|
json['subtitle'] = editForm['subtitle'].value;
|
|
json['subtitle'] = editForm['subtitle'].value;
|
|
json['content'] = editForm['article-body'].value;
|
|
json['content'] = editForm['article-body'].value;
|
|
json['tags'] = Array.from(tagSet).join(',');
|
|
json['tags'] = Array.from(tagSet).join(',');
|
|
json['created'] = new Date();
|
|
json['created'] = new Date();
|
|
-
|
|
|
|
|
|
+
|
|
const options = {
|
|
const options = {
|
|
body: JSON.stringify(json),
|
|
body: JSON.stringify(json),
|
|
method: "POST",
|
|
method: "POST",
|
|
@@ -150,7 +126,7 @@
|
|
"Content-Type": "application/json"
|
|
"Content-Type": "application/json"
|
|
}
|
|
}
|
|
};
|
|
};
|
|
-
|
|
|
|
|
|
+
|
|
fetch("/article", options)
|
|
fetch("/article", options)
|
|
.then(response => {
|
|
.then(response => {
|
|
if (response.status === 201) {
|
|
if (response.status === 201) {
|
|
@@ -163,6 +139,7 @@
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ // 공백 체크
|
|
const validateForm = () => {
|
|
const validateForm = () => {
|
|
const articleTitle = editForm['article-title'].value;
|
|
const articleTitle = editForm['article-title'].value;
|
|
if (articleTitle === null || articleTitle === "") {
|
|
if (articleTitle === null || articleTitle === "") {
|
|
@@ -197,26 +174,31 @@
|
|
<!-- onsubmit="return validateForm()" -->
|
|
<!-- onsubmit="return validateForm()" -->
|
|
<div class="form-data">
|
|
<div class="form-data">
|
|
<input type="text" class="form-control" name="article-title"
|
|
<input type="text" class="form-control" name="article-title"
|
|
- placeholder="Article Title" autocomplete="off">
|
|
|
|
|
|
+ placeholder="Article Title" autocomplete="off"
|
|
|
|
+ value="<c:if test="${editPage eq true}"><c:out value="${article.subtitle}" />
|
|
|
|
+ </c:if>">
|
|
</div>
|
|
</div>
|
|
<div class="form-data">
|
|
<div class="form-data">
|
|
- <input type="text" class="form-control form-control-sm" name="subtitle"
|
|
|
|
- placeholder="What's this article about?" autocomplete="off">
|
|
|
|
|
|
+ <input type="text" class="form-control form-control-sm"
|
|
|
|
+ name="subtitle" placeholder="What's this article about?"
|
|
|
|
+ autocomplete="off" value="<c:if test="${editPage eq true}"><c:out value="${article.subtitle}"/></c:if>">
|
|
</div>
|
|
</div>
|
|
<div class="form-data">
|
|
<div class="form-data">
|
|
<textarea class="form-control form-control-sm" rows="8"
|
|
<textarea class="form-control form-control-sm" rows="8"
|
|
name="article-body"
|
|
name="article-body"
|
|
- placeholder="Write your article (in markdown)"></textarea>
|
|
|
|
|
|
+ placeholder="Write your article (in markdown)"><c:if test="${editPage eq true}"><c:out value="${article.content}"/></c:if></textarea>
|
|
</div>
|
|
</div>
|
|
<div class="form-data">
|
|
<div class="form-data">
|
|
<input id="tag-input-area" type="text"
|
|
<input id="tag-input-area" type="text"
|
|
class="form-control form-control-sm tag-form" name="tags"
|
|
class="form-control form-control-sm tag-form" name="tags"
|
|
- placeholder="Enter tags" autocomplete="off" onkeydown="enterKey()">
|
|
|
|
|
|
+ placeholder="Enter tags" autocomplete="off"
|
|
|
|
+ onkeydown="enterKey()">
|
|
<div class="tag-list">
|
|
<div class="tag-list">
|
|
|
|
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
- <button class="btn-signUp" onclick="submitForm()">Publish Article</button>
|
|
|
|
|
|
+ <button class="btn-signUp" onclick="submitForm()">Publish
|
|
|
|
+ Article</button>
|
|
</form>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|