EditNews.vue 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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 UserIdToUsername from "@/components/UserIdToUsername.vue";
  80. import ws from "@/ws";
  81. import SaveButton from "../SaveButton.vue";
  82. import Modal from "../Modal.vue";
  83. export default {
  84. components: { Modal, SaveButton, UserIdToUsername },
  85. props: {
  86. newsId: { type: String, default: "" },
  87. sector: { type: String, default: "admin" }
  88. },
  89. data() {
  90. return {
  91. markdown:
  92. "# 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",
  93. status: "published",
  94. showToNewUsers: false,
  95. createdBy: null,
  96. createdAt: 0
  97. };
  98. },
  99. computed: {
  100. ...mapState("modals/editNews", { news: state => state.news }),
  101. ...mapGetters({ socket: "websockets/getSocket" })
  102. },
  103. mounted() {
  104. marked.use({
  105. renderer: {
  106. table(header, body) {
  107. return `<table class="table">
  108. <thead>${header}</thead>
  109. <tbody>${body}</tbody>
  110. </table>`;
  111. }
  112. }
  113. });
  114. ws.onConnect(this.init);
  115. },
  116. methods: {
  117. init() {
  118. if (this.newsId) {
  119. this.socket.dispatch(`news.getNewsFromId`, this.newsId, res => {
  120. if (res.status === "success") {
  121. const {
  122. markdown,
  123. status,
  124. showToNewUsers,
  125. createdBy,
  126. createdAt
  127. } = res.data.news;
  128. this.markdown = markdown;
  129. this.status = status;
  130. this.showToNewUsers = showToNewUsers;
  131. this.createdBy = createdBy;
  132. this.createdAt = createdAt;
  133. } else {
  134. new Toast("News with that ID not found.");
  135. this.closeModal("editNews");
  136. }
  137. });
  138. }
  139. },
  140. marked,
  141. sanitize,
  142. getTitle() {
  143. let title = "";
  144. const preview = document.getElementById("preview");
  145. // validate existence of h1 for the page title
  146. if (preview.childNodes.length === 0) return "";
  147. if (preview.childNodes[0].tagName !== "H1") {
  148. for (
  149. let node = 0;
  150. node < preview.childNodes.length;
  151. node += 1
  152. ) {
  153. if (preview.childNodes[node].tagName) {
  154. if (preview.childNodes[node].tagName === "H1")
  155. title = preview.childNodes[node].innerText;
  156. break;
  157. }
  158. }
  159. } else title = preview.childNodes[0].innerText;
  160. return title;
  161. },
  162. create(close) {
  163. if (this.markdown === "")
  164. return new Toast("News item cannot be empty.");
  165. const title = this.getTitle();
  166. if (!title)
  167. return new Toast(
  168. "Please provide a title (heading level 1) at the top of the document."
  169. );
  170. return this.socket.dispatch(
  171. "news.create",
  172. {
  173. title,
  174. markdown: this.markdown,
  175. status: this.status,
  176. showToNewUsers: this.showToNewUsers
  177. },
  178. res => {
  179. new Toast(res.message);
  180. if (res.status === "success" && close)
  181. this.closeModal("editNews");
  182. }
  183. );
  184. },
  185. update(close) {
  186. if (this.markdown === "")
  187. return new Toast("News item cannot be empty.");
  188. const title = this.getTitle();
  189. if (!title)
  190. return new Toast(
  191. "Please provide a title (heading level 1) at the top of the document."
  192. );
  193. return this.socket.dispatch(
  194. "news.update",
  195. this.newsId,
  196. {
  197. title,
  198. markdown: this.markdown,
  199. status: this.status,
  200. showToNewUsers: this.showToNewUsers
  201. },
  202. res => {
  203. new Toast(res.message);
  204. if (res.status === "success" && close)
  205. this.closeModal("editNews");
  206. }
  207. );
  208. },
  209. formatDistance,
  210. ...mapActions("modalVisibility", ["closeModal"]),
  211. ...mapActions("modals/editNews", [
  212. "editNews",
  213. "addChange",
  214. "removeChange"
  215. ])
  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>