EditNews.vue 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. <template>
  2. <modal
  3. class="edit-news-modal"
  4. :title="newsId ? 'Edit News' : 'Create News'"
  5. :size="'wide'"
  6. :split="true"
  7. >
  8. <template #body>
  9. <div class="left-section">
  10. <p><strong>Markdown</strong></p>
  11. <textarea v-model="markdown"></textarea>
  12. </div>
  13. <div class="right-section">
  14. <p><strong>Preview</strong></p>
  15. <div
  16. class="news-item"
  17. id="preview"
  18. v-html="sanitize(marked(markdown))"
  19. ></div>
  20. </div>
  21. </template>
  22. <template #footer>
  23. <div>
  24. <p class="control select">
  25. <select v-model="status">
  26. <option value="draft">Draft</option>
  27. <option value="published" selected>Publish</option>
  28. </select>
  29. </p>
  30. <p class="is-expanded checkbox-control">
  31. <label class="switch">
  32. <input
  33. type="checkbox"
  34. id="show-to-new-users"
  35. v-model="showToNewUsers"
  36. />
  37. <span class="slider round"></span>
  38. </label>
  39. <label for="show-to-new-users">
  40. <p>Show to new users</p>
  41. </label>
  42. </p>
  43. <save-button
  44. ref="saveButton"
  45. v-if="newsId"
  46. @clicked="newsId ? update(false) : create(false)"
  47. />
  48. <save-button
  49. ref="saveAndCloseButton"
  50. default-message="Save and close"
  51. @clicked="newsId ? update(true) : create(true)"
  52. />
  53. <div class="right" v-if="createdAt > 0">
  54. <span>
  55. By
  56. <user-id-to-username
  57. :user-id="createdBy"
  58. :alt="createdBy"
  59. :link="true"
  60. /> </span
  61. >&nbsp;<span :title="new Date(createdAt)">
  62. {{
  63. formatDistance(createdAt, new Date(), {
  64. addSuffix: true
  65. })
  66. }}
  67. </span>
  68. </div>
  69. </div>
  70. </template>
  71. </modal>
  72. </template>
  73. <script>
  74. import { mapActions, mapGetters, mapState } from "vuex";
  75. import { marked } from "marked";
  76. import { sanitize } from "dompurify";
  77. import Toast from "toasters";
  78. import { formatDistance } from "date-fns";
  79. import ws from "@/ws";
  80. import SaveButton from "../SaveButton.vue";
  81. export default {
  82. components: { SaveButton },
  83. props: {
  84. newsId: { type: String, default: "" },
  85. sector: { type: String, default: "admin" }
  86. },
  87. data() {
  88. return {
  89. markdown:
  90. "# Header\n## Sub-Header\n- **So**\n- _Many_\n- ~Points~\n\nOther things you want to say and [link](https://example.com).\n\n### Sub-Sub-Header\n> Oh look, a quote!\n\n`lil code`\n\n```\nbig code\n```\n",
  91. status: "published",
  92. showToNewUsers: false,
  93. createdBy: null,
  94. createdAt: 0
  95. };
  96. },
  97. computed: {
  98. ...mapState("modals/editNews", { news: state => state.news }),
  99. ...mapGetters({ socket: "websockets/getSocket" })
  100. },
  101. mounted() {
  102. marked.use({
  103. renderer: {
  104. table(header, body) {
  105. return `<table class="table">
  106. <thead>${header}</thead>
  107. <tbody>${body}</tbody>
  108. </table>`;
  109. }
  110. }
  111. });
  112. ws.onConnect(this.init);
  113. },
  114. methods: {
  115. init() {
  116. if (this.newsId) {
  117. this.socket.dispatch(`news.getNewsFromId`, this.newsId, res => {
  118. if (res.status === "success") {
  119. const {
  120. markdown,
  121. status,
  122. showToNewUsers,
  123. createdBy,
  124. createdAt
  125. } = res.data.news;
  126. this.markdown = markdown;
  127. this.status = status;
  128. this.showToNewUsers = showToNewUsers;
  129. this.createdBy = createdBy;
  130. this.createdAt = createdAt;
  131. } else {
  132. new Toast("News with that ID not found.");
  133. this.closeModal("editNews");
  134. }
  135. });
  136. }
  137. },
  138. marked,
  139. sanitize,
  140. getTitle() {
  141. let title = "";
  142. const preview = document.getElementById("preview");
  143. // validate existence of h1 for the page title
  144. if (preview.childNodes.length === 0) return "";
  145. if (preview.childNodes[0].tagName !== "H1") {
  146. for (
  147. let node = 0;
  148. node < preview.childNodes.length;
  149. node += 1
  150. ) {
  151. if (preview.childNodes[node].tagName) {
  152. if (preview.childNodes[node].tagName === "H1")
  153. title = preview.childNodes[node].innerText;
  154. break;
  155. }
  156. }
  157. } else title = preview.childNodes[0].innerText;
  158. return title;
  159. },
  160. create(close) {
  161. if (this.markdown === "")
  162. return new Toast("News item cannot be empty.");
  163. const title = this.getTitle();
  164. if (!title)
  165. return new Toast(
  166. "Please provide a title (heading level 1) at the top of the document."
  167. );
  168. return this.socket.dispatch(
  169. "news.create",
  170. {
  171. title,
  172. markdown: this.markdown,
  173. status: this.status,
  174. showToNewUsers: this.showToNewUsers
  175. },
  176. res => {
  177. new Toast(res.message);
  178. if (res.status === "success" && close)
  179. this.closeModal("editNews");
  180. }
  181. );
  182. },
  183. update(close) {
  184. if (this.markdown === "")
  185. return new Toast("News item cannot be empty.");
  186. const title = this.getTitle();
  187. if (!title)
  188. return new Toast(
  189. "Please provide a title (heading level 1) at the top of the document."
  190. );
  191. return this.socket.dispatch(
  192. "news.update",
  193. this.newsId,
  194. {
  195. title,
  196. markdown: this.markdown,
  197. status: this.status,
  198. showToNewUsers: this.showToNewUsers
  199. },
  200. res => {
  201. new Toast(res.message);
  202. if (res.status === "success" && close)
  203. this.closeModal("editNews");
  204. }
  205. );
  206. },
  207. formatDistance,
  208. ...mapActions("modalVisibility", ["closeModal"]),
  209. ...mapActions("modals/editNews", [
  210. "editNews",
  211. "addChange",
  212. "removeChange"
  213. ])
  214. }
  215. };
  216. </script>
  217. <style lang="less">
  218. .edit-news-modal .modal-card .modal-card-foot .right {
  219. column-gap: 5px;
  220. }
  221. </style>
  222. <style lang="less" scoped>
  223. .night-mode {
  224. .edit-news-modal .modal-card .modal-card-body textarea,
  225. .edit-news-modal .modal-card .modal-card-body #preview {
  226. border-color: var(--grey-3);
  227. }
  228. .edit-news-modal .modal-card .modal-card-body textarea {
  229. background-color: var(--dark-grey);
  230. color: var(--white);
  231. }
  232. }
  233. .edit-news-modal .modal-card .modal-card-body {
  234. .left-section,
  235. .right-section {
  236. padding: 10px;
  237. }
  238. textarea {
  239. border: 0;
  240. outline: none;
  241. resize: none;
  242. font-size: 16px;
  243. }
  244. #preview {
  245. word-break: break-all;
  246. overflow: auto;
  247. box-shadow: 0;
  248. }
  249. textarea,
  250. #preview {
  251. padding: 5px;
  252. border: 1px solid var(--light-grey-3) !important;
  253. border-radius: @border-radius;
  254. height: calc(100vh - 280px);
  255. width: 100%;
  256. }
  257. }
  258. .edit-news-modal .modal-card .modal-card-foot {
  259. .control {
  260. margin-bottom: 0 !important;
  261. }
  262. .right {
  263. line-height: 36px;
  264. column-gap: 0;
  265. }
  266. }
  267. </style>