EditNews.vue 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. <template>
  2. <modal
  3. class="edit-news-modal"
  4. :title="createNews ? 'Create News' : 'Edit 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="createNews"
  46. @clicked="createNews ? create(false) : update(false)"
  47. />
  48. <save-button
  49. ref="saveAndCloseButton"
  50. default-message="Save and close"
  51. @clicked="createNews ? create(true) : update(true)"
  52. />
  53. <div class="right" v-if="createdAt > 0">
  54. <span>
  55. By
  56. <user-link
  57. :user-id="createdBy"
  58. :alt="createdBy"
  59. /> </span
  60. >&nbsp;<span :title="new Date(createdAt)">
  61. {{
  62. formatDistance(createdAt, new Date(), {
  63. addSuffix: true
  64. })
  65. }}
  66. </span>
  67. </div>
  68. </div>
  69. </template>
  70. </modal>
  71. </template>
  72. <script>
  73. import { mapActions, mapGetters } from "vuex";
  74. import { marked } from "marked";
  75. import { sanitize } from "dompurify";
  76. import Toast from "toasters";
  77. import { formatDistance } from "date-fns";
  78. import ws from "@/ws";
  79. import SaveButton from "../SaveButton.vue";
  80. import { mapModalState } from "@/vuex_helpers";
  81. export default {
  82. components: { SaveButton },
  83. props: {
  84. modalUuid: { type: String, default: "" }
  85. },
  86. data() {
  87. return {
  88. markdown:
  89. "# 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",
  90. status: "published",
  91. showToNewUsers: false,
  92. createdBy: null,
  93. createdAt: 0
  94. };
  95. },
  96. computed: {
  97. ...mapModalState("modals/editNews/MODAL_UUID", {
  98. createNews: state => state.createNews,
  99. newsId: state => state.newsId,
  100. sector: state => state.sector
  101. }),
  102. ...mapGetters({ socket: "websockets/getSocket" })
  103. },
  104. mounted() {
  105. marked.use({
  106. renderer: {
  107. table(header, body) {
  108. return `<table class="table">
  109. <thead>${header}</thead>
  110. <tbody>${body}</tbody>
  111. </table>`;
  112. }
  113. }
  114. });
  115. ws.onConnect(this.init);
  116. },
  117. beforeUnmount() {
  118. // Delete the VueX module that was created for this modal, after all other cleanup tasks are performed
  119. this.$store.unregisterModule(["modals", "editNews", this.modalUuid]);
  120. },
  121. methods: {
  122. init() {
  123. if (this.newsId && !this.createNews) {
  124. this.socket.dispatch(`news.getNewsFromId`, this.newsId, res => {
  125. if (res.status === "success") {
  126. const {
  127. markdown,
  128. status,
  129. showToNewUsers,
  130. createdBy,
  131. createdAt
  132. } = res.data.news;
  133. this.markdown = markdown;
  134. this.status = status;
  135. this.showToNewUsers = showToNewUsers;
  136. this.createdBy = createdBy;
  137. this.createdAt = createdAt;
  138. } else {
  139. new Toast("News with that ID not found.");
  140. this.closeModal("editNews");
  141. }
  142. });
  143. }
  144. },
  145. marked,
  146. sanitize,
  147. getTitle() {
  148. let title = "";
  149. const preview = document.getElementById("preview");
  150. // validate existence of h1 for the page title
  151. if (preview.childNodes.length === 0) return "";
  152. if (preview.childNodes[0].tagName !== "H1") {
  153. for (
  154. let node = 0;
  155. node < preview.childNodes.length;
  156. node += 1
  157. ) {
  158. if (preview.childNodes[node].tagName) {
  159. if (preview.childNodes[node].tagName === "H1")
  160. title = preview.childNodes[node].innerText;
  161. break;
  162. }
  163. }
  164. } else title = preview.childNodes[0].innerText;
  165. return title;
  166. },
  167. create(close) {
  168. if (this.markdown === "")
  169. return new Toast("News item cannot be empty.");
  170. const title = this.getTitle();
  171. if (!title)
  172. return new Toast(
  173. "Please provide a title (heading level 1) at the top of the document."
  174. );
  175. return this.socket.dispatch(
  176. "news.create",
  177. {
  178. title,
  179. markdown: this.markdown,
  180. status: this.status,
  181. showToNewUsers: this.showToNewUsers
  182. },
  183. res => {
  184. new Toast(res.message);
  185. if (res.status === "success" && close)
  186. this.closeModal("editNews");
  187. }
  188. );
  189. },
  190. update(close) {
  191. if (this.markdown === "")
  192. return new Toast("News item cannot be empty.");
  193. const title = this.getTitle();
  194. if (!title)
  195. return new Toast(
  196. "Please provide a title (heading level 1) at the top of the document."
  197. );
  198. return this.socket.dispatch(
  199. "news.update",
  200. this.newsId,
  201. {
  202. title,
  203. markdown: this.markdown,
  204. status: this.status,
  205. showToNewUsers: this.showToNewUsers
  206. },
  207. res => {
  208. new Toast(res.message);
  209. if (res.status === "success" && close)
  210. this.closeModal("editNews");
  211. }
  212. );
  213. },
  214. formatDistance,
  215. ...mapActions("modalVisibility", ["closeModal"])
  216. }
  217. };
  218. </script>
  219. <style lang="less">
  220. .edit-news-modal .modal-card .modal-card-foot .right {
  221. column-gap: 5px;
  222. }
  223. </style>
  224. <style lang="less" scoped>
  225. .night-mode {
  226. .edit-news-modal .modal-card .modal-card-body textarea,
  227. .edit-news-modal .modal-card .modal-card-body #preview {
  228. border-color: var(--grey-3);
  229. }
  230. .edit-news-modal .modal-card .modal-card-body textarea {
  231. background-color: var(--dark-grey);
  232. color: var(--white);
  233. }
  234. }
  235. .edit-news-modal .modal-card .modal-card-body {
  236. .left-section,
  237. .right-section {
  238. padding: 10px;
  239. }
  240. textarea {
  241. border: 0;
  242. outline: none;
  243. resize: none;
  244. font-size: 16px;
  245. }
  246. #preview {
  247. word-break: break-all;
  248. overflow: auto;
  249. box-shadow: 0;
  250. }
  251. textarea,
  252. #preview {
  253. padding: 5px;
  254. border: 1px solid var(--light-grey-3) !important;
  255. border-radius: @border-radius;
  256. height: calc(100vh - 280px);
  257. width: 100%;
  258. }
  259. }
  260. .edit-news-modal .modal-card .modal-card-foot {
  261. .control {
  262. margin-bottom: 0 !important;
  263. }
  264. .right {
  265. line-height: 36px;
  266. column-gap: 0;
  267. }
  268. }
  269. </style>