pengx17
2024-06-05 09:33:18 +00:00
parent 928e133655
commit fa4e4c738a
12 changed files with 206 additions and 108 deletions

View File

@@ -2,6 +2,7 @@ import {
createReactComponentFromLit,
useConfirmModal,
useLitPortalFactory,
usePromptModal,
} from '@affine/component';
import { useJournalInfoHelper } from '@affine/core/hooks/use-journal';
import { PeekViewService } from '@affine/core/modules/peek-view';
@@ -80,11 +81,12 @@ const usePatchSpecs = (page: Doc, specs: BlockSpec[]) => {
}, [page.collection]);
const confirmModal = useConfirmModal();
const openPromptModal = usePromptModal();
const patchedSpecs = useMemo(() => {
let patched = patchReferenceRenderer(specs, reactToLit, referenceRenderer);
patched = patchNotificationService(
patchReferenceRenderer(patched, reactToLit, referenceRenderer),
confirmModal
{ ...confirmModal, prompt: openPromptModal }
);
if (!page.readonly && runtimeConfig.enablePeekView) {
patched = patchPeekViewService(patched, peekViewService);
@@ -92,6 +94,7 @@ const usePatchSpecs = (page: Doc, specs: BlockSpec[]) => {
return patched;
}, [
confirmModal,
openPromptModal,
page.readonly,
peekViewService,
reactToLit,

View File

@@ -5,6 +5,7 @@ import {
toast,
type ToastOptions,
type useConfirmModal,
type usePromptModal,
} from '@affine/component';
import type { PeekViewService } from '@affine/core/modules/peek-view';
import type { ActivePeekView } from '@affine/core/modules/peek-view/entities/peek-view';
@@ -114,7 +115,13 @@ export function patchReferenceRenderer(
export function patchNotificationService(
specs: BlockSpec[],
{ closeConfirmModal, openConfirmModal }: ReturnType<typeof useConfirmModal>
{
closeConfirmModal,
openConfirmModal,
prompt,
}: ReturnType<typeof useConfirmModal> & {
prompt: ReturnType<typeof usePromptModal>;
}
) {
const rootSpec = specs.find(
spec => spec.schema.model.flavour === 'affine:page'
@@ -126,22 +133,10 @@ export function patchNotificationService(
patchSpecService(rootSpec, service => {
service.notificationService = {
confirm: async ({
title,
message,
confirmText,
cancelText,
abort,
}: {
title: string;
message: string | TemplateResult;
confirmText: string;
cancelText: string;
abort?: AbortSignal;
}) => {
confirm: async ({ title, message, confirmText, cancelText, abort }) => {
return new Promise<boolean>(resolve => {
openConfirmModal({
title,
title: toReactNode(title),
description: toReactNode(message),
confirmButtonOptions: {
children: confirmText,
@@ -161,6 +156,23 @@ export function patchNotificationService(
});
});
},
prompt: async ({
title,
message,
confirmText,
placeholder,
cancelText,
abort,
}) => {
return prompt({
message: toReactNode(message),
title: toReactNode(title),
confirmText,
cancelText,
placeholder,
abort,
});
},
toast: (message: string, options: ToastOptions) => {
return toast(message, options);
},
@@ -181,6 +193,12 @@ export function patchNotificationService(
{
title: toReactNode(notification.title),
message: toReactNode(notification.message),
action: notification.action?.onClick
? {
label: toReactNode(notification.action?.label),
onClick: notification.action.onClick,
}
: undefined,
},
{
duration: notification.duration || 0,