Skip to content

Commit

Permalink
feat(client): add onContentUpdated hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
pengzhanbo committed Dec 6, 2024
1 parent 82379c0 commit a2f17b1
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
9 changes: 7 additions & 2 deletions packages/client/src/components/Content.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { computed, defineAsyncComponent, defineComponent, h } from 'vue'
import { usePageComponent } from '../composables/index.js'
import { runCallbacks, usePageComponent } from '../composables/index.js'
import { resolveRoute } from '../router/index.js'

/**
Expand All @@ -26,6 +26,11 @@ export const Content = defineComponent({
)
})

return () => h(ContentComponent.value)
return () =>
h(ContentComponent.value, {
onVnodeMounted: runCallbacks,
onVnodeUpdated: runCallbacks,
onVnodeBeforeUnmount: runCallbacks,
})
},
})
21 changes: 21 additions & 0 deletions packages/client/src/composables/contentUpdated.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { onUnmounted } from 'vue'

let contentUpdatedCallbacks: (() => unknown)[] = []

/**
* Register callback that is called every time the markdown content is updated
* in the DOM.
*/
export const onContentUpdated = (fn: () => unknown): void => {
contentUpdatedCallbacks.push(fn)
onUnmounted(() => {
contentUpdatedCallbacks = contentUpdatedCallbacks.filter((f) => f !== fn)
})
}

/**
* Call all registered callbacks
*/
export const runCallbacks = (): void => {
contentUpdatedCallbacks.forEach((fn) => fn())
}
1 change: 1 addition & 0 deletions packages/client/src/composables/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './clientData.js'
export * from './clientDataUtils.js'
export * from './updateHead.js'
export * from './contentUpdated.js'

0 comments on commit a2f17b1

Please sign in to comment.