AdminEditors.vue 7.16 KB
Newer Older
1 2 3 4 5 6
<template lang='pug'>
q-page.admin-flags
  .row.q-pa-md.items-center
    .col-auto
      img.admin-icon.animated.fadeInLeft(src='/_assets/icons/fluent-cashbook.svg')
    .col.q-pl-md
7 8
      .text-h5.text-primary.animated.fadeInLeft {{ t('admin.editors.title') }}
      .text-subtitle1.text-grey.animated.fadeInLeft.wait-p2s {{ t('admin.editors.subtitle') }}
9 10 11 12 13
    .col-auto
      q-btn.q-mr-sm.acrylic-btn(
        icon='las la-question-circle'
        flat
        color='grey'
14
        :aria-label='t(`common.actions.viewDocs`)'
15
        :href='siteStore.docsBase + `/admin/editors`'
16 17 18
        target='_blank'
        type='a'
        )
19
        q-tooltip {{ t(`common.actions.viewDocs`) }}
20
      q-btn.q-mr-sm.acrylic-btn(
21
        icon='las la-redo-alt'
22 23
        flat
        color='secondary'
24
        :loading='state.loading > 0'
25
        :aria-label='t(`common.actions.refresh`)'
26
        @click='refresh'
27
        )
28
        q-tooltip {{ t(`common.actions.refresh`) }}
29 30
      q-btn(
        unelevated
31
        icon='mdi-check'
32
        :label='t(`common.actions.apply`)'
33 34
        color='secondary'
        @click='save'
35
        :disabled='state.loading > 0'
36 37 38
      )
  q-separator(inset)
  .q-pa-md.q-gutter-md
39
    q-card
40
      q-list(separator)
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
        template(v-for='editor of editors', :key='editor.id')
          q-item(v-if='flagsStore.experimental || !editor.isDisabled')
            blueprint-icon(:icon='editor.icon')
            q-item-section
              q-item-label: strong {{t(`admin.editors.` + editor.id + `Name`)}}
              q-item-label(caption)
                span {{t(`admin.editors.` + editor.id + `Description`)}}
              q-item-label(caption, v-if='editor.useRendering')
                em.text-purple {{ t('admin.editors.useRenderingPipeline') }}
            template(v-if='editor.hasConfig')
              q-item-section(
                side
                )
                q-btn(
                  icon='las la-cog'
                  :label='t(`admin.editors.configuration`)'
                  :color='$q.dark.isActive ? `blue-grey-3` : `blue-grey-8`'
                  outline
                  no-caps
                  padding='xs md'
                  @click='openConfig(editor.id)'
                )
              q-separator.q-ml-md(vertical)
            q-item-section(side)
              q-toggle.q-pr-sm(
                v-model='state.config[editor.id]'
                :color='editor.isDisabled ? `grey` : `primary`'
                checked-icon='las la-check'
                unchecked-icon='las la-times'
                :label='t(`admin.sites.isActive`)'
                :aria-label='t(`admin.sites.isActive`)'
                :disabled='editor.isDisabled'
                )
74 75
</template>

76
<script setup>
77
import { useMeta, useQuasar } from 'quasar'
78
import { useI18n } from 'vue-i18n'
79 80 81
import { defineAsyncComponent, onMounted, reactive, watch } from 'vue'
import gql from 'graphql-tag'
import { cloneDeep } from 'lodash-es'
82

83 84
import { useAdminStore } from 'src/stores/admin'
import { useFlagsStore } from 'src/stores/flags'
85 86
import { useSiteStore } from 'src/stores/site'

87 88 89 90
// QUASAR

const $q = useQuasar()

91 92
// STORES

93 94
const adminStore = useAdminStore()
const flagsStore = useFlagsStore()
95 96 97
const siteStore = useSiteStore()

// I18N
98 99 100 101 102 103 104 105 106

const { t } = useI18n()

// META

useMeta({
  title: t('admin.editors.title')
})

107 108 109 110 111 112 113 114 115 116 117 118
const state = reactive({
  loading: 0,
  config: {
    api: false,
    asciidoc: false,
    blog: false,
    channel: false,
    markdown: false,
    redirect: true,
    wysiwyg: false
  }
})
119 120
const editors = reactive([
  {
121 122 123 124
    id: 'api',
    icon: 'api',
    isDisabled: true,
    useRendering: false
125
  },
126
  {
127 128 129 130
    id: 'asciidoc',
    icon: 'asciidoc',
    hasConfig: true,
    useRendering: true
131 132 133 134
  },
  {
    id: 'blog',
    icon: 'typewriter-with-paper',
135 136
    isDisabled: true,
    useRendering: true
137 138
  },
  {
139 140 141 142 143 144 145 146 147 148
    id: 'channel',
    icon: 'chat',
    isDisabled: true,
    useRendering: false
  },
  {
    id: 'markdown',
    icon: 'markdown',
    hasConfig: true,
    useRendering: true
149 150 151 152
  },
  {
    id: 'redirect',
    icon: 'advance',
153 154 155 156 157 158 159
    isDisabled: true,
    useRendering: false
  },
  {
    id: 'wysiwyg',
    icon: 'google-presentation',
    useRendering: true
160
  }
161 162
])

163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
// WATCHERS

watch(() => adminStore.currentSiteId, (newValue) => {
  $q.loading.show()
  load()
})

// METHODS

async function load () {
  state.loading++
  try {
    const resp = await APOLLO_CLIENT.query({
      query: gql`
        query getEditorsState (
          $siteId: UUID!
        ) {
        siteById (
          id: $siteId
        ) {
          id
          editors {
            asciidoc {
              isActive
            }
            markdown {
              isActive
            }
            wysiwyg  {
              isActive
            }
          }
        }
      }`,
      variables: {
        siteId: adminStore.currentSiteId
      },
      fetchPolicy: 'network-only'
    })
    const data = cloneDeep(resp?.data?.siteById?.editors)
    state.config.asciidoc = data?.asciidoc?.isActive ?? false
    state.config.markdown = data?.markdown?.isActive ?? false
    state.config.wysiwyg = data?.wysiwyg?.isActive ?? false
  } catch (err) {
    $q.notify({
      type: 'negative',
      message: 'Failed to fetch editors state.'
    })
  }
  $q.loading.hide()
  state.loading--
}

216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
async function save () {
  state.loading++
  try {
    const respRaw = await APOLLO_CLIENT.mutate({
      mutation: gql`
        mutation saveEditorState (
          $id: UUID!
          $patch: SiteUpdateInput!
          ) {
          updateSite (
            id: $id,
            patch: $patch
            ) {
            operation {
              succeeded
              slug
              message
            }
          }
        }
      `,
      variables: {
        id: adminStore.currentSiteId,
        patch: {
          editors: {
            asciidoc: { isActive: state.config.asciidoc },
            markdown: { isActive: state.config.markdown },
            wysiwyg: { isActive: state.config.wysiwyg }
          }
        }
      }
    })
    if (respRaw?.data?.updateSite?.operation?.succeeded) {
249 250 251 252 253 254 255 256 257
      if (adminStore.currentSiteId === siteStore.id) {
        siteStore.$patch({
          editors: {
            asciidoc: state.config.asciidoc,
            markdown: state.config.markdown,
            wysiwyg: state.config.wysiwyg
          }
        })
      }
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
      $q.notify({
        type: 'positive',
        message: t('admin.editors.saveSuccess')
      })
    } else {
      throw new Error(respRaw?.data?.updateSite?.operation?.message || 'An unexpected error occured.')
    }
  } catch (err) {
    $q.notify({
      type: 'negative',
      message: 'Failed to save site editors config',
      caption: err.message
    })
  }
  state.loading--
}
274 275 276 277 278 279 280 281

async function refresh () {
  await load()
}

function openConfig (editorId) {
  switch (editorId) {
    case 'markdown': {
282 283 284
      adminStore.$patch({
        overlayOpts: { },
        overlay: 'EditorMarkdownConfig'
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
      })
      break
    }
    default: {
      $q.notify({
        type: 'negative',
        message: 'Invalid Editor Config Call'
      })
    }
  }
}

// MOUNTED

onMounted(async () => {
  $q.loading.show()
  if (adminStore.currentSiteId) {
    await load()
  }
})
305 306 307 308 309
</script>

<style lang='scss'>

</style>