123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426 |
- import { defineStore } from 'pinia'
- import gql from 'graphql-tag'
- import { cloneDeep, initial, last, pick, transform } from 'lodash-es'
- import { DateTime } from 'luxon'
- import { useSiteStore } from './site'
- import { useEditorStore } from './editor'
- const pagePropsFragment = gql`
- fragment PageRead on Page {
- allowComments
- allowContributions
- allowRatings
- contentType
- createdAt
- description
- editor
- icon
- id
- isBrowsable
- locale
- password
- path
- publishEndDate
- publishStartDate
- publishState
- relations {
- id
- position
- label
- caption
- icon
- target
- }
- render
- scriptJsLoad
- scriptJsUnload
- scriptCss
- showSidebar
- showTags
- showToc
- tags {
- tag
- title
- }
- title
- toc
- tocDepth {
- min
- max
- }
- updatedAt
- }
- `
- const gqlQueries = {
- pageById: gql`
- query loadPage (
- $id: UUID!
- ) {
- pageById(
- id: $id
- ) {
- ...PageRead
- }
- }
- ${pagePropsFragment}
- `,
- pageByPath: gql`
- query loadPage (
- $siteId: UUID!
- $path: String!
- ) {
- pageByPath(
- siteId: $siteId
- path: $path
- ) {
- ...PageRead
- }
- }
- ${pagePropsFragment}
- `,
- pageByIdWithContent: gql`
- query loadPageWithContent (
- $id: UUID!
- ) {
- pageById(
- id: $id
- ) {
- ...PageRead,
- content
- }
- }
- ${pagePropsFragment}
- `,
- pageByPathWithContent: gql`
- query loadPageWithContent (
- $siteId: UUID!
- $path: String!
- ) {
- pageByPath(
- siteId: $siteId
- path: $path
- ) {
- ...PageRead,
- content
- }
- }
- ${pagePropsFragment}
- `
- }
- export const usePageStore = defineStore('page', {
- state: () => ({
- allowComments: false,
- allowContributions: true,
- allowRatings: true,
- authorId: 0,
- authorName: '',
- commentsCount: 0,
- content: '',
- createdAt: '',
- description: '',
- editor: '',
- icon: 'las la-file-alt',
- id: '',
- isBrowsable: true,
- locale: 'en',
- password: '',
- path: '',
- publishEndDate: '',
- publishStartDate: '',
- publishState: '',
- relations: [],
- render: '',
- scriptJsLoad: '',
- scriptJsUnload: '',
- scriptCss: '',
- showSidebar: true,
- showTags: true,
- showToc: true,
- tags: [],
- title: '',
- toc: [],
- tocDepth: {
- min: 1,
- max: 2
- },
- updatedAt: ''
- }),
- getters: {
- breadcrumbs: (state) => {
- const siteStore = useSiteStore()
- const pathPrefix = siteStore.useLocales ? `/${state.locale}` : ''
- return transform(state.path.split('/'), (result, value, key) => {
- result.push({
- id: key,
- title: value,
- icon: 'las la-file-alt',
- locale: 'en',
- path: (last(result)?.path || pathPrefix) + `/${value}`
- })
- }, [])
- },
- folderPath: (state) => {
- return initial(state.path.split('/')).join('/')
- }
- },
- actions: {
- /**
- * PAGE - LOAD
- */
- async pageLoad ({ path, id, withContent = false }) {
- const editorStore = useEditorStore()
- const siteStore = useSiteStore()
- try {
- let query
- if (withContent) {
- query = id ? gqlQueries.pageByIdWithContent : gqlQueries.pageByPathWithContent
- } else {
- query = id ? gqlQueries.pageById : gqlQueries.pageByPath
- }
- const resp = await APOLLO_CLIENT.query({
- query,
- variables: id ? { id } : { siteId: siteStore.id, path },
- fetchPolicy: 'network-only'
- })
- const pageData = cloneDeep((id ? resp?.data?.pageById : resp?.data?.pageByPath) ?? {})
- if (!pageData?.id) {
- throw new Error('ERR_PAGE_NOT_FOUND')
- }
- // Update page store
- this.$patch({
- ...pageData,
- relations: pageData.relations.map(r => pick(r, ['id', 'position', 'label', 'caption', 'icon', 'target'])),
- tocDepth: pick(pageData.tocDepth, ['min', 'max'])
- })
- // Update editor state timestamps
- const curDate = DateTime.utc()
- editorStore.$patch({
- lastChangeTimestamp: curDate,
- lastSaveTimestamp: curDate
- })
- } catch (err) {
- console.warn(err)
- throw err
- }
- },
- /**
- * PAGE - CREATE
- */
- pageCreate ({ editor, locale, path, title = '', description = '', content = '' }) {
- const editorStore = useEditorStore()
- // if (['markdown', 'api'].includes(editor)) {
- // commit('site/SET_SHOW_SIDE_NAV', false, { root: true })
- // } else {
- // commit('site/SET_SHOW_SIDE_NAV', true, { root: true })
- // }
- // if (['markdown', 'channel', 'api'].includes(editor)) {
- // commit('site/SET_SHOW_SIDEBAR', false, { root: true })
- // } else {
- // commit('site/SET_SHOW_SIDEBAR', true, { root: true })
- // }
- // -> Page Data
- this.id = 0
- this.locale = locale || this.locale
- if (path || path === '') {
- this.path = path
- } else {
- this.path = this.path.length < 2 ? 'new-page' : `${this.path}/new-page`
- }
- this.title = title ?? ''
- this.description = description ?? ''
- this.icon = 'las la-file-alt'
- this.publishState = 'published'
- this.relations = []
- this.tags = []
- this.content = content ?? ''
- this.render = ''
- // -> View Mode
- this.mode = 'edit'
- // -> Editor Mode
- editorStore.$patch({
- isActive: true,
- editor,
- mode: 'create'
- })
- },
- /**
- * PAGE SAVE
- */
- async pageSave () {
- const editorStore = useEditorStore()
- const siteStore = useSiteStore()
- try {
- if (editorStore.mode === 'create') {
- const resp = await APOLLO_CLIENT.mutate({
- mutation: gql`
- mutation createPage (
- $allowComments: Boolean
- $allowContributions: Boolean
- $allowRatings: Boolean
- $content: String!
- $description: String!
- $editor: String!
- $icon: String
- $isBrowsable: Boolean
- $locale: String!
- $path: String!
- $publishState: PagePublishState!
- $publishEndDate: Date
- $publishStartDate: Date
- $relations: [PageRelationInput!]
- $scriptCss: String
- $scriptJsLoad: String
- $scriptJsUnload: String
- $showSidebar: Boolean
- $showTags: Boolean
- $showToc: Boolean
- $siteId: UUID!
- $tags: [String!]
- $title: String!
- $tocDepth: PageTocDepthInput
- ) {
- createPage (
- allowComments: $allowComments
- allowContributions: $allowContributions
- allowRatings: $allowRatings
- content: $content
- description: $description
- editor: $editor
- icon: $icon
- isBrowsable: $isBrowsable
- locale: $locale
- path: $path
- publishState: $publishState
- publishEndDate: $publishEndDate
- publishStartDate: $publishStartDate
- relations: $relations
- scriptCss: $scriptCss
- scriptJsLoad: $scriptJsLoad
- scriptJsUnload: $scriptJsUnload
- showSidebar: $showSidebar
- showTags: $showTags
- showToc: $showToc
- siteId: $siteId
- tags: $tags
- title: $title
- tocDepth: $tocDepth
- ) {
- operation {
- succeeded
- message
- }
- }
- }
- `,
- variables: {
- ...pick(this, [
- 'allowComments',
- 'allowContributions',
- 'allowRatings',
- 'content',
- 'description',
- 'icon',
- 'isBrowsable',
- 'locale',
- 'password',
- 'path',
- 'publishEndDate',
- 'publishStartDate',
- 'publishState',
- 'relations',
- 'scriptJsLoad',
- 'scriptJsUnload',
- 'scriptCss',
- 'showSidebar',
- 'showTags',
- 'showToc',
- 'tags',
- 'title',
- 'tocDepth'
- ]),
- editor: editorStore.editor,
- siteId: siteStore.id
- }
- })
- const result = resp?.data?.createPage?.operation ?? {}
- if (!result.succeeded) {
- throw new Error(result.message)
- }
- this.id = resp.data.createPage.page.id
- this.editor = editorStore.editor
- } else {
- const resp = await APOLLO_CLIENT.mutate({
- mutation: gql`
- mutation savePage (
- $id: UUID!
- $patch: PageUpdateInput!
- ) {
- updatePage (
- id: $id
- patch: $patch
- ) {
- operation {
- succeeded
- message
- }
- }
- }
- `,
- variables: {
- id: this.id,
- patch: pick(this, [
- 'allowComments',
- 'allowContributions',
- 'allowRatings',
- 'content',
- 'description',
- 'icon',
- 'isBrowsable',
- 'locale',
- 'password',
- 'path',
- 'publishEndDate',
- 'publishStartDate',
- 'publishState',
- 'relations',
- 'scriptJsLoad',
- 'scriptJsUnload',
- 'scriptCss',
- 'showSidebar',
- 'showTags',
- 'showToc',
- 'tags',
- 'title',
- 'tocDepth'
- ])
- }
- })
- const result = resp?.data?.updatePage?.operation ?? {}
- if (!result.succeeded) {
- throw new Error(result.message)
- }
- }
- // Update editor state timestamps
- const curDate = DateTime.utc()
- editorStore.$patch({
- lastChangeTimestamp: curDate,
- lastSaveTimestamp: curDate
- })
- } catch (err) {
- console.warn(err)
- throw err
- }
- },
- generateToc () {
- }
- }
- })
|