fix: fix get document

This commit is contained in:
fantasticit 2022-03-12 12:49:33 +08:00
parent d10d44abf6
commit 877b065291
2 changed files with 28 additions and 71 deletions

View File

@ -1,24 +1,13 @@
import { import { Node, Command, mergeAttributes, wrappingInputRule } from '@tiptap/core';
Node,
Command,
mergeAttributes,
textInputRule,
textblockTypeInputRule,
wrappingInputRule,
} from '@tiptap/core';
import { NodeViewWrapper, NodeViewContent, ReactNodeViewRenderer } from '@tiptap/react'; import { NodeViewWrapper, NodeViewContent, ReactNodeViewRenderer } from '@tiptap/react';
import { useRouter } from 'next/router'; import { useRouter } from 'next/router';
import Link from 'next/link'; import Link from 'next/link';
import { Space, Select, Popover, Tag, Input, Typography } from '@douyinfe/semi-ui'; import { Select } from '@douyinfe/semi-ui';
import { useWikiTocs } from 'data/wiki'; import { useWikiTocs } from 'data/wiki';
import { useDocumentDetail } from 'data/document';
import { DataRender } from 'components/data-render'; import { DataRender } from 'components/data-render';
import { Empty } from 'components/empty';
import { IconDocument } from 'components/icons'; import { IconDocument } from 'components/icons';
import styles from './index.module.scss'; import styles from './index.module.scss';
const { Text } = Typography;
declare module '@tiptap/core' { declare module '@tiptap/core' {
interface Commands { interface Commands {
documentReference: { documentReference: {
@ -92,34 +81,36 @@ const Render = ({ editor, node, updateAttributes }) => {
const { wikiId, documentId, title } = node.attrs; const { wikiId, documentId, title } = node.attrs;
const { data: tocs, loading, error } = useWikiTocs(isShare ? null : wikiIdFromUrl); const { data: tocs, loading, error } = useWikiTocs(isShare ? null : wikiIdFromUrl);
const selectDoc = (str) => {
const [wikiId, documentId, title] = str.split('/');
updateAttributes({ wikiId, documentId, title });
};
return ( return (
<NodeViewWrapper as="div" className={styles.wrap}> <NodeViewWrapper as="div" className={styles.wrap}>
<div> <div>
{isEditable && ( {isEditable && (
<Select <DataRender
placeholder="请选择文档" loading={loading}
defaultValue={JSON.stringify({ error={error}
wikiId, normalContent={() => (
documentId, <Select
title, placeholder="请选择文档"
})} onChange={(v) => selectDoc(v)}
onChange={(v) => updateAttributes(JSON.parse(v as string))} {...(wikiId && documentId ? { value: `${wikiId}/${documentId}/${title}` } : {})}
>
{(tocs || []).map((toc) => (
<Select.Option
key={toc.id}
value={JSON.stringify({
wikiId: toc.wikiId,
documentId: toc.id,
title: toc.title,
})}
> >
{toc.title} {(tocs || []).map((toc) => (
</Select.Option> <Select.Option
))} label={`${toc.id}/${toc.title}`}
</Select> value={`${toc.wikiId}/${toc.id}/${toc.title}`}
>
{toc.title}
</Select.Option>
))}
</Select>
)}
/>
)} )}
<Link <Link
key={documentId} key={documentId}
href={{ href={{
@ -129,44 +120,10 @@ const Render = ({ editor, node, updateAttributes }) => {
> >
<a className={styles.itemWrap} target="_blank"> <a className={styles.itemWrap} target="_blank">
<IconDocument /> <IconDocument />
<span>{title}</span> <span>{title || '请选择文档'}</span>
</a> </a>
</Link> </Link>
</div> </div>
{/* <div>
<Text type="tertiary"></Text>
<DataRender
loading={loading}
error={error}
normalContent={() => {
if (!documents || !documents.length) {
return <Empty message="暂无子文档" />;
}
return (
<div>
{documents.map((doc) => {
return (
<Link
key={doc.id}
href={{
pathname: `${
!isShare ? "" : "/share"
}/wiki/[wikiId]/document/[documentId]`,
query: { wikiId: doc.wikiId, documentId: doc.id },
}}
>
<a className={styles.itemWrap} target="_blank">
<IconDocument />
<span>{doc.title}</span>
</a>
</Link>
);
})}
</div>
);
}}
/>
</div> */}
<NodeViewContent></NodeViewContent> <NodeViewContent></NodeViewContent>
</NodeViewWrapper> </NodeViewWrapper>
); );

View File

@ -95,7 +95,7 @@ export const useWikiHomeDoc = (wikiId) => {
export const useWikiTocs = (wikiId) => { export const useWikiTocs = (wikiId) => {
const { data, error, mutate } = useSWR<Array<IDocument & { createUser: IUser }>>( const { data, error, mutate } = useSWR<Array<IDocument & { createUser: IUser }>>(
`/wiki/tocs/${wikiId}`, `/wiki/tocs/${wikiId}`,
(url) => HttpClient.get(url) (url) => (wikiId ? HttpClient.get(url) : null)
); );
const loading = !data && !error; const loading = !data && !error;