Skip to content

Commit

Permalink
Merge pull request #120 from yennanliu/blog-dev-009-add-blog-font-style
Browse files Browse the repository at this point in the history
blog-dev-009-add-blog-font-style : Add blog font style feature
  • Loading branch information
yennanliu authored Nov 22, 2023
2 parents a25d00d + b863ea2 commit 9224699
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,11 @@ public String createPost(CreatePost request, Model model, Principal principal){
post.setDateTime(LocalDateTime.now());
post.setSynopsis(PostUtil.getSynopsis(request.getContent()));
post.setAuthorId(author.getId());

post.setFontSize(request.getFontSize());
post.setFontStyle(request.getFontStyle());
post.setFontColor(request.getFontColor());

post.setDateTime(LocalDateTime.now());
log.info(">>> request = " + request + " post = " + post + " author = " + author);
log.info(">>>> create post end ...");
Expand Down Expand Up @@ -187,7 +192,6 @@ public String getMyPost(
model.addAttribute("posts", posts);
// get current user login via spring security
model.addAttribute("user", principal.getName());
//Arrays.stream(pageInfo.getNavigatepageNums()).forEach(System.out::println);
model.addAttribute("user", principal.getName());
return "post/my_post";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,15 @@ public class Post implements Serializable {
@Column
private Integer authorId;

@Column
private String fontSize;

@Column
private String fontStyle;

@Column
private String fontColor;

@Column
@Convert(converter = DateTimeUtil.class)
private LocalDateTime dateTime;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,8 @@ public class CreatePost {
private String content;
private String synopsis;
private String author;
private String fontSize;
private String fontStyle;
private String fontColor;
private LocalDateTime dateTime;
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public void logout(HttpServletRequest request,
Authentication authentication) {

// 登出時在console印出的訊息
System.out.println(">>> going to logoug ... MyLogoutHandler.logout()");
System.out.println(">>> going to logout ... MyLogoutHandler.logout()");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
post.setContent(htmlContent);
post.setSynopsis(getSynopsisFromHtmlContent(htmlContent));
post.setDateTime(LocalDateTime.now());

// TODO : avoid hardcode below
post.setFontSize("12px");
post.setFontStyle("Ariel");
post.setFontColor("black");

postRepository.save(post);
}catch (Exception e){
System.out.println(">>> create post failed : " + e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,28 @@ public InMemoryUserDetailsManager(UserDetails user1, UserDetails user2, UserDeta

@Bean
public InMemoryUserDetailsManager userDetailsService() {

UserDetails user1 = User.withUsername("user1")
.password(passwordEncoder().encode("user1Pass"))
.roles("USER")
.build();

UserDetails user2 = User.withUsername("user2")
.password(passwordEncoder().encode("user2Pass"))
.roles("USER")
.build();

UserDetails admin = User.withUsername("admin")
.password(passwordEncoder().encode("adminPass"))
.roles("ADMIN")
.build();

return new InMemoryUserDetailsManager(user1, user2, admin);
}

@Bean
public PasswordEncoder passwordEncoder() {

return new BCryptPasswordEncoder();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@

<!-- public void insertPost(Post post); -->
<insert id="insertPost">
INSERT INTO posts(`title`,`content`,`synopsis`,`author_id`, `date_time`)
values(#{Post.title}, #{Post.content}, #{Post.synopsis}, #{Post.authorId}, #{Post.dateTime})
INSERT INTO posts(`title`,`content`,`synopsis`,`author_id`, `font_size`, `font_style`, `font_color`, `date_time`)
values(#{Post.title}, #{Post.content}, #{Post.synopsis}, #{Post.authorId}, #{Post.fontSize}, #{Post.fontStyle}, #{Post.fontColor}, #{Post.dateTime})
</insert>

<!-- public void updatePost(Post post); -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,14 @@ <h1 align="center">Create A New Post</h1>
<option value="Times New Roman, serif">Times New Roman</option>
<option value="Courier New, monospace">Courier New</option>
</select>

<label for="font-color">Font Color:</label>
<input type="color" id="font-color" onchange="changeStyle()">
</div>

<input type="hidden" th:value="20px" name="selectedFontSize" id="selectedFontSize" th:field="*{FontSize}" />
<input type="hidden" th:value="Ariel" name="selectedFontStyle" id="selectedFontStyle" th:field="*{FontStyle}" />
<input type="hidden" th:value="red" name="selectedFontColor" id="selectedFontColor" th:field="*{FontColor}" />

<textarea id="content" name="courseMemo" th:field="*{content}" oninput="changeStyle()">blog content</textarea>
<p><input type="submit" value="Submit" /> <input type="reset" value="Reset" /></p>
</form>
Expand All @@ -53,9 +57,17 @@ <h1 align="center">Create A New Post</h1>
var fontStyle = document.getElementById('font-style').value;
var fontColor = document.getElementById('font-color').value;

console.log(">>> fontSize = " + fontSize);

content.style.fontSize = fontSize;
content.style.fontFamily = fontStyle;
content.style.color = fontColor;

document.getElementById('selectedFontSize').value = fontSize;
document.getElementById('selectedFontStyle').value = fontStyle;
document.getElementById('selectedFontColor').value = fontColor;

//console.log(">>> content = " + JSON.stringify(content));
}
</script>
</body>
Expand Down
2 changes: 1 addition & 1 deletion springBootBlog/src/main/resources/templates/post/post.html
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
<h1 th:utext="${post.title}">Title</h1>
<p class="card-subtitle mb-2"
th:text="${#temporals.monthName(post.dateTime) + ' ' + #temporals.day(post.dateTime) + ', ' + #temporals.year(post.dateTime)}">LocalDateTime</p>
<p th:utext="${post.content}">Content</p>
<p th:utext="${post.content}" style="font-size: ${post.fontSize}; font-family: ${post.fontStyle}; color: ${post.fontColor};">Content</p>
</article>
</div>
<!-- create comment -->
Expand Down

0 comments on commit 9224699

Please sign in to comment.