EditNews.vue 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. <template>
  2. <modal
  3. class="edit-news-modal"
  4. :title="newsId ? 'Edit News' : 'Create News'"
  5. >
  6. <template #body>
  7. <div id="markdown-editor-and-preview">
  8. <div class="column">
  9. <p><strong>Markdown</strong></p>
  10. <textarea v-model="markdown"></textarea>
  11. </div>
  12. <div class="column">
  13. <p><strong>Preview</strong></p>
  14. <div
  15. class="news-item"
  16. id="preview"
  17. v-html="sanitize(marked(markdown))"
  18. ></div>
  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. <save-button
  31. ref="saveButton"
  32. v-if="newsId"
  33. @clicked="newsId ? update(false) : create(false)"
  34. />
  35. <save-button
  36. ref="saveAndCloseButton"
  37. default-message="Save and close"
  38. @clicked="newsId ? update(true) : create(true)"
  39. />
  40. <div class="right" v-if="createdAt > 0">
  41. <span>
  42. By
  43. <user-id-to-username
  44. :user-id="createdBy"
  45. :alt="createdBy"
  46. :link="true"
  47. /> </span
  48. >&nbsp;<span :title="new Date(createdAt)">
  49. {{
  50. formatDistance(createdAt, new Date(), {
  51. addSuffix: true
  52. })
  53. }}
  54. </span>
  55. </div>
  56. </div>
  57. </template>
  58. </modal>
  59. </template>
  60. <script>
  61. import { mapActions, mapGetters, mapState } from "vuex";
  62. import marked from "marked";
  63. import { sanitize } from "dompurify";
  64. import Toast from "toasters";
  65. import { formatDistance } from "date-fns";
  66. import UserIdToUsername from "@/components/UserIdToUsername.vue";
  67. import ws from "@/ws";
  68. import SaveButton from "../SaveButton.vue";
  69. import Modal from "../Modal.vue";
  70. export default {
  71. components: { Modal, SaveButton, UserIdToUsername },
  72. props: {
  73. newsId: { type: String, default: "" },
  74. sector: { type: String, default: "admin" }
  75. },
  76. data() {
  77. return {
  78. markdown:
  79. "# 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",
  80. status: "published",
  81. createdBy: null,
  82. createdAt: 0
  83. };
  84. },
  85. computed: {
  86. ...mapState("modals/editNews", { news: state => state.news }),
  87. ...mapGetters({ socket: "websockets/getSocket" })
  88. },
  89. mounted() {
  90. marked.use({
  91. renderer: {
  92. table(header, body) {
  93. return `<table class="table is-striped">
  94. <thead>${header}</thead>
  95. <tbody>${body}</tbody>
  96. </table>`;
  97. }
  98. }
  99. });
  100. ws.onConnect(this.init);
  101. },
  102. methods: {
  103. init() {
  104. if (this.newsId) {
  105. this.socket.dispatch(`news.getNewsFromId`, this.newsId, res => {
  106. if (res.status === "success") {
  107. const { markdown, status, createdBy, createdAt } =
  108. res.data.news;
  109. this.markdown = markdown;
  110. this.status = status;
  111. this.createdBy = createdBy;
  112. this.createdAt = createdAt;
  113. } else {
  114. new Toast("News with that ID not found.");
  115. this.closeModal("editNews");
  116. }
  117. });
  118. }
  119. },
  120. marked,
  121. sanitize,
  122. getTitle() {
  123. let title = "";
  124. const preview = document.getElementById("preview");
  125. // validate existence of h1 for the page title
  126. if (preview.childNodes.length === 0) return "";
  127. if (preview.childNodes[0].tagName !== "H1") {
  128. for (
  129. let node = 0;
  130. node < preview.childNodes.length;
  131. node += 1
  132. ) {
  133. if (preview.childNodes[node].tagName) {
  134. if (preview.childNodes[node].tagName === "H1")
  135. title = preview.childNodes[node].innerText;
  136. break;
  137. }
  138. }
  139. } else title = preview.childNodes[0].innerText;
  140. return title;
  141. },
  142. create(close) {
  143. if (this.markdown === "")
  144. return new Toast("News item cannot be empty.");
  145. const title = this.getTitle();
  146. if (!title)
  147. return new Toast(
  148. "Please provide a title (heading level 1) at the top of the document."
  149. );
  150. return this.socket.dispatch(
  151. "news.create",
  152. {
  153. title,
  154. markdown: this.markdown,
  155. status: this.status
  156. },
  157. res => {
  158. new Toast(res.message);
  159. if (res.status === "success" && close)
  160. this.closeModal("editNews");
  161. }
  162. );
  163. },
  164. update(close) {
  165. if (this.markdown === "")
  166. return new Toast("News item cannot be empty.");
  167. const title = this.getTitle();
  168. if (!title)
  169. return new Toast(
  170. "Please provide a title (heading level 1) at the top of the document."
  171. );
  172. return this.socket.dispatch(
  173. "news.update",
  174. this.newsId,
  175. {
  176. title,
  177. markdown: this.markdown,
  178. status: this.status
  179. },
  180. res => {
  181. new Toast(res.message);
  182. if (res.status === "success" && close)
  183. this.closeModal("editNews");
  184. }
  185. );
  186. },
  187. formatDistance,
  188. ...mapActions("modalVisibility", ["closeModal"]),
  189. ...mapActions("modals/editNews", [
  190. "editNews",
  191. "addChange",
  192. "removeChange"
  193. ])
  194. }
  195. };
  196. </script>
  197. <style lang="scss">
  198. .edit-news-modal {
  199. .modal-card {
  200. width: 1300px;
  201. .modal-card-foot .right {
  202. margin: auto 0 auto auto !important;
  203. span:not(:last-child) {
  204. margin-right: 0 !important;
  205. }
  206. }
  207. }
  208. }
  209. </style>
  210. <style lang="scss" scoped>
  211. .night-mode {
  212. #markdown-editor-and-preview textarea,
  213. #markdown-editor-and-preview #preview {
  214. border-color: #fff;
  215. }
  216. }
  217. #markdown-editor-and-preview {
  218. display: flex;
  219. flex-wrap: wrap;
  220. .column {
  221. display: flex;
  222. flex-direction: column;
  223. width: 350px;
  224. flex-grow: 1;
  225. flex-basis: initial;
  226. }
  227. textarea {
  228. border: 0;
  229. outline: none;
  230. resize: none;
  231. margin-right: 5px;
  232. font-size: 16px;
  233. }
  234. #preview {
  235. word-break: break-all;
  236. overflow: auto;
  237. box-shadow: none;
  238. }
  239. textarea,
  240. #preview {
  241. padding: 5px;
  242. border: 1px solid var(--light-grey-3) !important;
  243. border-radius: 3px;
  244. height: 700px;
  245. }
  246. }
  247. </style>