Просмотр исходного кода

disable delete button if shared page

ryoji-s 2 лет назад
Родитель
Сommit
12647c7c1d

+ 3 - 3
apps/app/src/client/services/renderer/renderer.tsx

@@ -65,7 +65,7 @@ export const generateViewOptions = (
     drawio.remarkPlugin,
     mermaid.remarkPlugin,
     xsvToTable.remarkPlugin,
-    attachment.remarkPlugin,
+    [attachment.remarkPlugin, { isSharedPage: config.isSharedPage }],
     lsxGrowiDirective.remarkPlugin,
     refsGrowiDirective.remarkPlugin,
     [slides.remarkPlugin, { isEnabledMarp: config.isEnabledMarp }],
@@ -178,7 +178,7 @@ export const generateSimpleViewOptions = (
     drawio.remarkPlugin,
     mermaid.remarkPlugin,
     xsvToTable.remarkPlugin,
-    attachment.remarkPlugin,
+    [attachment.remarkPlugin, { isSharedPage: config.isSharedPage }],
     lsxGrowiDirective.remarkPlugin,
     refsGrowiDirective.remarkPlugin,
   );
@@ -259,7 +259,7 @@ export const generatePreviewOptions = (config: RendererConfig, pagePath: string)
     drawio.remarkPlugin,
     mermaid.remarkPlugin,
     xsvToTable.remarkPlugin,
-    attachment.remarkPlugin,
+    [attachment.remarkPlugin, { isSharedPage: config.isSharedPage }],
     lsxGrowiDirective.remarkPlugin,
     refsGrowiDirective.remarkPlugin,
     [slides.remarkPlugin, { isEnabledMarp: config.isEnabledMarp }],

+ 12 - 6
apps/app/src/components/ReactMarkdownComponents/RichAttachment.tsx

@@ -9,11 +9,15 @@ import { useDeleteAttachmentModal } from '~/stores/modal';
 
 import styles from './RichAttachment.module.scss';
 
-export const RichAttachment: React.FC<{
+type RichAttachmentProps = {
   attachmentId: string,
   url: string,
-  attachmentName: string
-}> = React.memo(({ attachmentId, url, attachmentName }) => {
+  attachmentName: string,
+  isSharedPage?: boolean,
+}
+
+export const RichAttachment = React.memo((props: RichAttachmentProps) => {
+  const { attachmentId, attachmentName, isSharedPage } = props;
   const { t } = useTranslation();
   const { data: attachment, remove } = useSWRxAttachment(attachmentId);
   const { open: openDeleteAttachmentModal } = useDeleteAttachmentModal();
@@ -66,9 +70,11 @@ export const RichAttachment: React.FC<{
               <a className="ml-2 attachment-download" href={downloadPathProxied}>
                 <i className="icon-cloud-download" />
               </a>
-              <a className="ml-2 text-danger attachment-delete" onClick={onClickTrashButtonHandler}>
-                <i className="icon-trash" />
-              </a>
+              {isSharedPage ?? (
+                <a className="ml-2 text-danger attachment-delete" onClick={onClickTrashButtonHandler}>
+                  <i className="icon-trash" />
+                </a>
+              )}
             </div>
             <div className="d-flex align-items-center">
               <UserPicture user={creator} size="sm" />

+ 9 - 4
apps/app/src/services/renderer/remark-plugins/attachment.ts

@@ -5,7 +5,7 @@ import { Plugin } from 'unified';
 import { Node } from 'unist';
 import { visit } from 'unist-util-visit';
 
-const SUPPORTED_ATTRIBUTES = ['attachmentId', 'url', 'attachmentName'];
+const SUPPORTED_ATTRIBUTES = ['attachmentId', 'url', 'attachmentName', 'isSharedPage'];
 
 const isAttachmentLink = (url: string): boolean => {
   // https://regex101.com/r/9qZhiK/1
@@ -13,7 +13,7 @@ const isAttachmentLink = (url: string): boolean => {
   return attachmentUrlFormat.test(url);
 };
 
-const rewriteNode = (node: Node) => {
+const rewriteNode = (node: Node, isSharedPage?: boolean) => {
   const attachmentId = path.basename(node.url as string);
   const data = node.data ?? (node.data = {});
   data.hName = 'attachment';
@@ -21,15 +21,20 @@ const rewriteNode = (node: Node) => {
     attachmentId,
     url: node.url,
     attachmentName: (node.children as any)[0]?.value,
+    isSharedPage,
   };
 };
 
-export const remarkPlugin: Plugin = () => {
+type AttachmentPluginParams = {
+  isSharedPage?: boolean,
+}
+
+export const remarkPlugin: Plugin<[AttachmentPluginParams]> = (options) => {
   return (tree) => {
     visit(tree, (node) => {
       if (node.type === 'link') {
         if (isAttachmentLink(node.url as string)) {
-          rewriteNode(node);
+          rewriteNode(node, options.isSharedPage);
         }
       }
     });