123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286 |
- <script setup lang="ts">
- import { defineAsyncComponent, ref, computed, onMounted } from "vue";
- import { marked } from "marked";
- import DOMPurify from "dompurify";
- import Toast from "toasters";
- import { formatDistance } from "date-fns";
- import { useModalsStore } from "@/stores/modals";
- import { useForm } from "@/composables/useForm";
- import { useEvents } from "@/composables/useEvents";
- import { useModels } from "@/composables/useModels";
- import { useWebsocketStore } from "@/stores/websocket";
- import { useModelStore } from "@/stores/model";
- const Modal = defineAsyncComponent(() => import("@/components/Modal.vue"));
- const SaveButton = defineAsyncComponent(
- () => import("@/components/SaveButton.vue")
- );
- const props = defineProps({
- modalUuid: { type: String, required: true },
- createNews: { type: Boolean, default: false },
- newsId: { type: String, default: null },
- sector: { type: String, default: "admin" }
- });
- const { runJob } = useWebsocketStore();
- const { findById } = useModelStore();
- const { onReady } = useEvents();
- const { closeCurrentModal } = useModalsStore();
- const { registerModel, onDeleted } = useModels();
- const createdBy = ref();
- const createdAt = ref(0);
- const dataLoaded = ref(false);
- const canShow = computed(() => {
- if (props.newsId) return dataLoaded.value;
- return true;
- });
- const getTitle = () => {
- let title = "";
- const preview = document.getElementById("preview");
- // validate existence of h1 for the page title
- if (!preview || preview.childNodes.length === 0) return "";
- if (preview.childNodes[0].nodeName !== "H1") {
- for (let node = 0; node < preview.childNodes.length; node += 1) {
- if (preview.childNodes[node].nodeName) {
- if (preview.childNodes[node].nodeName === "H1")
- title = preview.childNodes[node].textContent || "";
- break;
- }
- }
- } else title = preview.childNodes[0].textContent || "";
- return title;
- };
- const { inputs, save, setModelValues } = useForm(
- {
- markdown: {
- value: "# 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",
- validate: (value: string) => {
- if (value === "") return "News item cannot be empty.";
- if (!getTitle())
- return "Please provide a title (heading level 1) at the top of the document.";
- return true;
- }
- },
- status: "published",
- showToNewUsers: false
- },
- ({ status, messages, values }, resolve, reject) => {
- if (status === "success") {
- const query = {
- title: getTitle(),
- markdown: values.markdown,
- status: values.status,
- showToNewUsers: values.showToNewUsers
- };
- const method = props.createNews
- ? runJob(`data.news.create`, { query })
- : runJob(`data.news.updateById`, { _id: props.newsId, query });
- method.then(resolve).catch(reject);
- } else {
- if (status === "unchanged") new Toast(messages.unchanged);
- else if (status === "error")
- Object.values(messages).forEach(message => {
- new Toast({ content: message, timeout: 8000 });
- });
- resolve();
- }
- },
- {
- modalUuid: props.modalUuid
- }
- );
- onMounted(async () => {
- marked.use({
- renderer: {
- table(header, body) {
- return `<table class="table">
- <thead>${header}</thead>
- <tbody>${body}</tbody>
- </table>`;
- }
- }
- });
- await onReady(async () => {
- if (props.newsId && !props.createNews) {
- const data = await findById("news", props.newsId).catch(() => {
- new Toast("News with that ID not found.");
- closeCurrentModal();
- });
- if (!data) return;
- const model = await registerModel(data, { news: "createdBy" });
- setModelValues(model, ["markdown", "status", "showToNewUsers"]);
- createdBy.value = model.createdBy;
- createdAt.value = model.createdAt;
- dataLoaded.value = true;
- await onDeleted("news", ({ oldDoc }) => {
- if (oldDoc._id !== props.newsId) return;
- new Toast("News item has been deleted.");
- closeCurrentModal();
- });
- }
- });
- });
- </script>
- <template>
- <modal
- class="edit-news-modal"
- :title="createNews ? 'Create News' : 'Edit News'"
- :size="'wide'"
- :split="true"
- >
- <template #body>
- <div class="left-section" v-show="canShow">
- <p><strong>Markdown</strong></p>
- <textarea v-model="inputs['markdown'].value"></textarea>
- </div>
- <div class="right-section" v-show="canShow">
- <p><strong>Preview</strong></p>
- <div
- class="news-item"
- id="preview"
- v-html="
- DOMPurify.sanitize(marked(inputs['markdown'].value))
- "
- ></div>
- </div>
- </template>
- <template #footer>
- <div>
- <p class="control select">
- <select v-model="inputs['status'].value">
- <option value="draft">Draft</option>
- <option value="published" selected>Publish</option>
- </select>
- </p>
- <p class="is-expanded checkbox-control">
- <label class="switch">
- <input
- type="checkbox"
- id="show-to-new-users"
- v-model="inputs['showToNewUsers'].value"
- />
- <span class="slider round"></span>
- </label>
- <label for="show-to-new-users">
- <p>Show to new users</p>
- </label>
- </p>
- <save-button
- ref="saveButton"
- v-if="createNews"
- @clicked="save()"
- />
- <save-button
- ref="saveAndCloseButton"
- default-message="Save and close"
- @clicked="save(closeCurrentModal)"
- />
- <div class="right" v-if="createdAt > 0">
- <span>
- By
- <router-link
- :to="{ path: `/u/${createdBy.username}` }"
- :title="createdBy._id"
- >
- {{ createdBy.name }}
- </router-link></span
- > <span :title="new Date(createdAt).toString()">
- {{
- formatDistance(createdAt, new Date(), {
- addSuffix: true
- })
- }}
- </span>
- </div>
- </div>
- </template>
- </modal>
- </template>
- <style lang="less">
- .edit-news-modal .modal-card .modal-card-foot .right {
- column-gap: 5px;
- }
- </style>
- <style lang="less" scoped>
- .night-mode {
- .edit-news-modal .modal-card .modal-card-body textarea,
- .edit-news-modal .modal-card .modal-card-body #preview {
- border-color: var(--grey-3);
- }
- .edit-news-modal .modal-card .modal-card-body textarea {
- background-color: var(--dark-grey);
- color: var(--white);
- }
- }
- .edit-news-modal .modal-card .modal-card-body {
- .left-section,
- .right-section {
- padding: 10px;
- }
- textarea {
- border: 0;
- outline: none;
- resize: none;
- font-size: 16px;
- }
- #preview {
- word-break: break-all;
- overflow: auto;
- box-shadow: 0;
- }
- textarea,
- #preview {
- padding: 5px;
- border: 1px solid var(--light-grey-3) !important;
- border-radius: @border-radius;
- height: calc(100vh - 280px);
- width: 100%;
- }
- }
- .edit-news-modal .modal-card .modal-card-foot {
- .control {
- margin-bottom: 0 !important;
- }
- .right {
- line-height: 36px;
- column-gap: 0;
- }
- }
- </style>
|