jam411 3 лет назад
Родитель
Сommit
b320ba43b0

+ 11 - 15
packages/app/src/components/PageAttachment/PageAttachmentList.tsx

@@ -1,11 +1,9 @@
 import React from 'react';
 import React from 'react';
 
 
-
 import { HasObjectId, IAttachment } from '@growi/core';
 import { HasObjectId, IAttachment } from '@growi/core';
 import { Attachment } from '@growi/ui';
 import { Attachment } from '@growi/ui';
 import { useTranslation } from 'next-i18next';
 import { useTranslation } from 'next-i18next';
 
 
-
 type Props = {
 type Props = {
   attachments: (IAttachment & HasObjectId)[],
   attachments: (IAttachment & HasObjectId)[],
   inUse: { [id: string]: boolean },
   inUse: { [id: string]: boolean },
@@ -24,22 +22,20 @@ export const PageAttachmentList = (props: Props): JSX.Element => {
     return <>{t('No_attachments_yet')}</>;
     return <>{t('No_attachments_yet')}</>;
   }
   }
 
 
-  const attachmentList = attachments.map((attachment) => {
-    return (
-      <Attachment
-        key={`page:attachment:${attachment._id}`}
-        attachment={attachment}
-        inUse={inUse[attachment._id] || false}
-        onAttachmentDeleteClicked={onAttachmentDeleteClicked}
-        isUserLoggedIn={isUserLoggedIn}
-      />
-    );
-  });
-
   return (
   return (
     <div>
     <div>
       <ul className="pl-2">
       <ul className="pl-2">
-        {attachmentList}
+        {attachments.map((attachment) => {
+          return (
+            <Attachment
+              key={`page:attachment:${attachment._id}`}
+              attachment={attachment}
+              inUse={inUse[attachment._id] || false}
+              onAttachmentDeleteClicked={onAttachmentDeleteClicked}
+              isUserLoggedIn={isUserLoggedIn}
+            />
+          );
+        })}
       </ul>
       </ul>
     </div>
     </div>
   );
   );

+ 5 - 0
packages/app/src/stores/attachment.tsx

@@ -9,6 +9,8 @@ import useSWR from 'swr';
 import { apiPost } from '~/client/util/apiv1-client';
 import { apiPost } from '~/client/util/apiv1-client';
 import { apiv3Get } from '~/client/util/apiv3-client';
 import { apiv3Get } from '~/client/util/apiv3-client';
 import { IResAttachmentList } from '~/interfaces/attachment';
 import { IResAttachmentList } from '~/interfaces/attachment';
+import { useEditingMarkdown } from '~/stores/context';
+import { useSWRxCurrentPage } from '~/stores/page';
 
 
 type Util = {
 type Util = {
   remove(body: { attachment_id: string }): Promise<void>
   remove(body: { attachment_id: string }): Promise<void>
@@ -21,6 +23,9 @@ type IDataAttachmentList = {
 };
 };
 
 
 export const useSWRxAttachments = (pageId?: Nullable<string>, pageNumber?: number): SWRResponseWithUtils<Util, IDataAttachmentList, Error> => {
 export const useSWRxAttachments = (pageId?: Nullable<string>, pageNumber?: number): SWRResponseWithUtils<Util, IDataAttachmentList, Error> => {
+  const { data: currentPage } = useSWRxCurrentPage();
+  useEditingMarkdown(currentPage?.revision.body);
+
   const shouldFetch = pageId != null && pageNumber != null;
   const shouldFetch = pageId != null && pageNumber != null;
 
 
   const fetcher = useCallback(async(endpoint, pageId, pageNumber) => {
   const fetcher = useCallback(async(endpoint, pageId, pageNumber) => {

+ 0 - 86
packages/ui/src/components/Attachment/Attachment.jsx

@@ -1,86 +0,0 @@
-import React from 'react';
-
-import PropTypes from 'prop-types';
-
-
-import { UserPicture } from '../User/UserPicture';
-
-export class Attachment extends React.Component {
-
-  constructor(props) {
-    super(props);
-
-    this._onAttachmentDeleteClicked = this._onAttachmentDeleteClicked.bind(this);
-  }
-
-  iconNameByFormat(format) {
-    if (format.match(/image\/.+/i)) {
-      return 'icon-picture';
-    }
-
-    return 'icon-doc';
-  }
-
-  _onAttachmentDeleteClicked(event) {
-    if (this.props.onAttachmentDeleteClicked != null) {
-      this.props.onAttachmentDeleteClicked(this.props.attachment);
-    }
-  }
-
-  render() {
-    const attachment = this.props.attachment;
-    const formatIcon = this.iconNameByFormat(attachment.fileFormat);
-
-    let fileInUse = '';
-    if (this.props.inUse) {
-      fileInUse = <span className="attachment-in-use badge badge-pill badge-info">In Use</span>;
-    }
-
-    const fileType = <span className="attachment-filetype badge badge-pill badge-secondary">{attachment.fileFormat}</span>;
-
-    const btnDownload = (this.props.isUserLoggedIn)
-      ? (
-        <a className="attachment-download" href={attachment.downloadPathProxied}>
-          <i className="icon-cloud-download" />
-        </a>
-      )
-      : '';
-
-    const btnTrash = (this.props.isUserLoggedIn)
-      ? (
-        /* eslint-disable-next-line */
-        <a className="text-danger attachment-delete" onClick={this._onAttachmentDeleteClicked}>
-          <i className="icon-trash" />
-        </a>
-      )
-      : '';
-
-    return (
-      <div className="attachment mb-2">
-        <span className="mr-1 attachment-userpicture">
-          <UserPicture user={attachment.creator} size="sm"></UserPicture>
-        </span>
-        <a className="mr-2" href={attachment.filePathProxied} target="_blank" rel="noopener noreferrer">
-          <i className={formatIcon}></i> {attachment.originalName}
-        </a>
-        <span className="mr-2">{fileType}</span>
-        <span className="mr-2">{fileInUse}</span>
-        <span className="mr-2">{btnDownload}</span>
-        <span className="mr-2">{btnTrash}</span>
-      </div>
-    );
-  }
-
-}
-
-Attachment.propTypes = {
-  attachment: PropTypes.object.isRequired,
-  inUse: PropTypes.bool,
-  onAttachmentDeleteClicked: PropTypes.func,
-  isUserLoggedIn: PropTypes.bool,
-};
-
-Attachment.defaultProps = {
-  inUse: false,
-  isUserLoggedIn: false,
-};

+ 5 - 13
packages/ui/src/components/Attachment/Attachment.tsx

@@ -6,7 +6,7 @@ import { UserPicture } from '../User/UserPicture';
 
 
 type AttachmentProps = {
 type AttachmentProps = {
   attachment: IAttachment & HasObjectId,
   attachment: IAttachment & HasObjectId,
-  inUse: { [id: string]: boolean },
+  inUse: boolean,
   onAttachmentDeleteClicked?: (attachment: IAttachment & HasObjectId) => void,
   onAttachmentDeleteClicked?: (attachment: IAttachment & HasObjectId) => void,
   isUserLoggedIn?: boolean,
   isUserLoggedIn?: boolean,
 };
 };
@@ -17,22 +17,13 @@ export const Attachment = (props: AttachmentProps): JSX.Element => {
     attachment, inUse, isUserLoggedIn, onAttachmentDeleteClicked,
     attachment, inUse, isUserLoggedIn, onAttachmentDeleteClicked,
   } = props;
   } = props;
 
 
-  const iconNameByFormat = (format: string) => {
-    return (format.match(/image\/.+/i)) ? 'icon-picture' : 'icon-doc';
-  };
-
   const _onAttachmentDeleteClicked = () => {
   const _onAttachmentDeleteClicked = () => {
     if (onAttachmentDeleteClicked != null) {
     if (onAttachmentDeleteClicked != null) {
       onAttachmentDeleteClicked(attachment);
       onAttachmentDeleteClicked(attachment);
     }
     }
   };
   };
 
 
-  const formatIcon = iconNameByFormat(attachment.fileFormat);
-
-  const fileInUse = (inUse) ? <span className="attachment-in-use badge badge-pill badge-info">In Use</span> : '';
-
-  const fileType = <span className="attachment-filetype badge badge-pill badge-secondary">{attachment.fileFormat}</span>;
-
+  const formatIcon = (attachment.fileFormat.match(/image\/.+/i)) ? 'icon-picture' : 'icon-doc';
   const btnDownload = (isUserLoggedIn)
   const btnDownload = (isUserLoggedIn)
     ? (
     ? (
       <a className="attachment-download" href={attachment.downloadPathProxied}>
       <a className="attachment-download" href={attachment.downloadPathProxied}>
@@ -40,14 +31,15 @@ export const Attachment = (props: AttachmentProps): JSX.Element => {
       </a>
       </a>
     )
     )
     : '';
     : '';
-
   const btnTrash = (isUserLoggedIn)
   const btnTrash = (isUserLoggedIn)
     ? (
     ? (
-      <a className="text-danger attachment-delete" onClick={() => _onAttachmentDeleteClicked()}>
+      <a className="text-danger attachment-delete" onClick={_onAttachmentDeleteClicked}>
         <i className="icon-trash" />
         <i className="icon-trash" />
       </a>
       </a>
     )
     )
     : '';
     : '';
+  const fileType = <span className="attachment-filetype badge badge-pill badge-secondary">{attachment.fileFormat}</span>;
+  const fileInUse = (inUse) ? <span className="attachment-in-use badge badge-pill badge-info">In Use</span> : '';
 
 
   return (
   return (
     <div className="attachment mb-2">
     <div className="attachment mb-2">