Skip to content

Commit

Permalink
feat: add hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
yy-wow committed Oct 10, 2024
1 parent f44fe8e commit c8bc905
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 2 deletions.
32 changes: 30 additions & 2 deletions packages/toolbars/preview/src/Main.vue
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
<script>
import { previewPage, previewBlock } from '@opentiny/tiny-engine-common/js/preview'
import { useBlock, useCanvas, useLayout, useNotify } from '@opentiny/tiny-engine-meta-register'
import { getMergeMeta } from '@opentiny/tiny-engine-meta-register'
import { getMergeMeta, getOptions } from '@opentiny/tiny-engine-meta-register'
import meta from '../meta'
import { ToolbarBase } from '@opentiny/tiny-engine-common'
export default {
Expand All @@ -30,7 +31,26 @@ export default {
const { isBlock, getCurrentPage, canvasApi } = useCanvas()
const { getCurrentBlock } = useBlock()
const preview = () => {
const preview = async () => {
const { beforePreview, previewMethod, afterPreview } = getOptions(meta.id)
const printer = console
try {
if (typeof beforePreview === 'function') {
await beforePreview()
}
if (typeof previewMethod === 'function') {
const stop = await previewMethod()
if (stop) {
return
}
}
} catch (error) {
printer.log(error)
}
if (useLayout().isEmptyPage()) {
useNotify({
type: 'warning',
Expand Down Expand Up @@ -59,6 +79,14 @@ export default {
params.pageInfo.name = page?.name
previewPage(params)
}
if (typeof afterPreview === 'function') {
try {
await afterPreview()
} catch (error) {
printer.log('Error in afterPreview:', error)
}
}
}
return {
Expand Down
1 change: 1 addition & 0 deletions packages/toolbars/save/src/Main.vue
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ import { useCanvas } from '@opentiny/tiny-engine-meta-register'
import { ToolbarBase } from '@opentiny/tiny-engine-common'
import { openCommon, saveCommon } from './js/index'
import { isLoading } from './js/index'
export const api = {
saveCommon,
openCommon
Expand Down
29 changes: 29 additions & 0 deletions packages/toolbars/save/src/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@ import {
useLayout,
useNotify,
usePage,
getOptions,
getMetaApi,
META_APP
} from '@opentiny/tiny-engine-meta-register'
import { constants } from '@opentiny/tiny-engine-utils'
import { handlePageUpdate } from '@opentiny/tiny-engine-common/js/http'
import meta from '../../meta'

const { PAGE_STATUS } = constants
const state = reactive({
Expand Down Expand Up @@ -95,6 +97,25 @@ export const openCommon = async () => {
return
}

const { beforeSave, saveMethod, saved } = getOptions(meta.id)
const printer = console

try {
if (typeof beforeSave === 'function') {
await beforeSave()
}

if (typeof saveMethod === 'function') {
const stop = await saveMethod()

if (stop) {
return
}
}
} catch (error) {
printer.log(error)
}

const pageStatus = useLayout().layoutState?.pageStatus
const curPageState = pageStatus?.state
const pageInfo = pageStatus?.data
Expand Down Expand Up @@ -138,5 +159,13 @@ export const openCommon = async () => {

saveCommon(state.code).finally(() => {
state.disabled = false

if (typeof saved === 'function') {
try {
saved()
} catch (error) {
printer.log(error)
}
}
})
}

0 comments on commit c8bc905

Please sign in to comment.