Преглед изворни кода

refs 120355: complete RefImg

Futa Arai пре 3 година
родитељ
комит
5f672290be

+ 1 - 3
packages/remark-attachment-refs/src/client/components/AttachmentList.tsx

@@ -14,7 +14,6 @@ type Props = {
   isLoading: boolean
   isLoading: boolean
   error?: Error
   error?: Error
   attachments: IAttachmentHasId[]
   attachments: IAttachmentHasId[]
-  isExtractImg?: boolean
 };
 };
 
 
 export const AttachmentList = ({
 export const AttachmentList = ({
@@ -22,7 +21,6 @@ export const AttachmentList = ({
   isLoading,
   isLoading,
   error,
   error,
   attachments,
   attachments,
-  isExtractImg = false,
 }: Props): JSX.Element => {
 }: Props): JSX.Element => {
   const renderNoAttachmentsMessage = () => {
   const renderNoAttachmentsMessage = () => {
     let message;
     let message;
@@ -67,7 +65,7 @@ export const AttachmentList = ({
       return renderNoAttachmentsMessage();
       return renderNoAttachmentsMessage();
     }
     }
 
 
-    return (isExtractImg)
+    return (refsContext.isExtractImage)
       ? <ExtractedAttachments attachments={attachments} refsContext={refsContext} />
       ? <ExtractedAttachments attachments={attachments} refsContext={refsContext} />
       : attachments.map((attachment) => {
       : attachments.map((attachment) => {
         return <AttachmentLink key={attachment._id} attachment={attachment} inUse={false} />;
         return <AttachmentLink key={attachment._id} attachment={attachment} inUse={false} />;

+ 7 - 7
packages/remark-attachment-refs/src/client/components/ExtractedAttachments.tsx

@@ -38,8 +38,8 @@ export const ExtractedAttachments = React.memo(({
 
 
     const width = options?.width;
     const width = options?.width;
     const height = options?.height;
     const height = options?.height;
-    const maxWidth = options?.['max-width'];
-    const maxHeight = options?.['max-height'];
+    const maxWidth = options?.maxWidth;
+    const maxHeight = options?.maxHeight;
     const display = options?.display || 'block';
     const display = options?.display || 'block';
 
 
     const containerStyles = {
     const containerStyles = {
@@ -61,8 +61,8 @@ export const ExtractedAttachments = React.memo(({
   const getClassesAndStylesForGrid = useCallback(() => {
   const getClassesAndStylesForGrid = useCallback(() => {
     const { options } = refsContext;
     const { options } = refsContext;
 
 
-    const maxWidth = options?.['max-width'];
-    const maxHeight = options?.['max-height'];
+    const maxWidth = options?.maxWidth;
+    const maxHeight = options?.maxHeight;
 
 
     const containerStyles = {
     const containerStyles = {
       width: refsContext.getOptGridWidth(),
       width: refsContext.getOptGridWidth(),
@@ -110,7 +110,7 @@ export const ExtractedAttachments = React.memo(({
 
 
     // carousel settings
     // carousel settings
     let onClick;
     let onClick;
-    if (options?.['no-carousel'] == null) {
+    if (options?.noCarousel == null) {
       // pointer cursor
       // pointer cursor
       Object.assign(containerStyles, { cursor: 'pointer' });
       Object.assign(containerStyles, { cursor: 'pointer' });
       // set click handler
       // set click handler
@@ -128,7 +128,7 @@ export const ExtractedAttachments = React.memo(({
 
 
   const renderCarousel = useCallback(() => {
   const renderCarousel = useCallback(() => {
     const { options } = refsContext;
     const { options } = refsContext;
-    const withCarousel = options?.['no-carousel'] == null;
+    const withCarousel = options?.noCarousel == null;
 
 
     const images = getAttachmentsFilteredByFormat()
     const images = getAttachmentsFilteredByFormat()
       .map((attachment) => {
       .map((attachment) => {
@@ -159,7 +159,7 @@ export const ExtractedAttachments = React.memo(({
 
 
   const { options } = refsContext;
   const { options } = refsContext;
   const grid = options?.grid;
   const grid = options?.grid;
-  const gridGap = options?.['grid-gap'];
+  const gridGap = options?.gridGap;
 
 
   const styles = {};
   const styles = {};
 
 

+ 16 - 6
packages/remark-attachment-refs/src/client/components/RefImg.tsx

@@ -7,22 +7,33 @@ import { RefsContext } from './util/refs-context';
 
 
 
 
 type Props = {
 type Props = {
-  fileNameOrId: string,
-  pagePath: string,
-  isImmutable?: boolean,
+  fileNameOrId: string
+  pagePath: string
+  width?: string
+  height?: string
+  maxWidth?: string
+  maxHeight?: string
+  alt?: string
+
+  isImmutable?: boolean
 };
 };
 
 
 const RefImgSubstance = React.memo(({
 const RefImgSubstance = React.memo(({
   fileNameOrId,
   fileNameOrId,
   pagePath,
   pagePath,
+  width,
+  height,
+  maxWidth,
+  maxHeight,
+  alt,
   isImmutable,
   isImmutable,
 }: Props): JSX.Element => {
 }: Props): JSX.Element => {
   const refsContext = useMemo(() => {
   const refsContext = useMemo(() => {
     const options = {
     const options = {
-      fileNameOrId, pagePath,
+      fileNameOrId, pagePath, width, height, maxWidth, maxHeight, alt,
     };
     };
     return new RefsContext('refimg', options);
     return new RefsContext('refimg', options);
-  }, [fileNameOrId, pagePath]);
+  }, [fileNameOrId, pagePath, width, height, maxWidth, maxHeight, alt]);
 
 
   const { data, error, isLoading } = useSWRxRef(pagePath, fileNameOrId, isImmutable);
   const { data, error, isLoading } = useSWRxRef(pagePath, fileNameOrId, isImmutable);
   const attachments = data != null ? [data] : [];
   const attachments = data != null ? [data] : [];
@@ -32,7 +43,6 @@ const RefImgSubstance = React.memo(({
     isLoading={isLoading}
     isLoading={isLoading}
     error={error}
     error={error}
     attachments={attachments}
     attachments={attachments}
-    isExtractImg
   />;
   />;
 });
 });
 
 

+ 4 - 0
packages/remark-attachment-refs/src/client/components/util/refs-context.ts

@@ -57,6 +57,10 @@ export class RefsContext {
     return this.tag === 'ref' || this.tag === 'refimg';
     return this.tag === 'ref' || this.tag === 'refimg';
   }
   }
 
 
+  get isExtractImage(): boolean {
+    return this.tag === 'refimg' || this.tag === 'refsimg';
+  }
+
   getOptGrid(): string | undefined {
   getOptGrid(): string | undefined {
     return GRID_AVAILABLE_OPTIONS_LIST.find(item => item === this.options?.grid);
     return GRID_AVAILABLE_OPTIONS_LIST.find(item => item === this.options?.grid);
   }
   }

+ 9 - 4
packages/remark-attachment-refs/src/client/services/renderer/refs.ts

@@ -8,11 +8,11 @@ import { visit } from 'unist-util-visit';
 
 
 const REF_NAME_PATTERN = new RegExp(/refimg|ref/);
 const REF_NAME_PATTERN = new RegExp(/refimg|ref/);
 const REFS_NAME_PATTERN = new RegExp(/refsimg|refs/);
 const REFS_NAME_PATTERN = new RegExp(/refsimg|refs/);
-const REF_SUPPORTED_ATTRIBUTES = ['file', 'id', 'page', 'fileNameOrId', 'pagePath'];
-const REF_IMG_SUPPORTED_ATTRIBUTES = ['file', 'id', 'page', 'width', 'height', 'max-width', 'max-height', 'alt', 'fileNameOrId', 'pagePath'];
-const REFS_SUPPORTED_ATTRIBUTES = ['page', 'prefix', 'depth', 'regexp', 'pagePath'];
+const REF_SUPPORTED_ATTRIBUTES = ['fileNameOrId', 'pagePath'];
+const REF_IMG_SUPPORTED_ATTRIBUTES = ['fileNameOrId', 'pagePath', 'width', 'height', 'maxWidth', 'maxHeight', 'alt'];
+const REFS_SUPPORTED_ATTRIBUTES = ['pagePath', 'prefix', 'depth', 'regexp'];
 const REFS_IMG_SUPPORTED_ATTRIBUTES = [
 const REFS_IMG_SUPPORTED_ATTRIBUTES = [
-  'page', 'prefix', 'depth', 'regexp', 'width', 'height', 'max-width', 'max-height', 'display', 'grid', 'grid-gap', 'no-carousel', 'pagePath',
+  'pagePath', 'prefix', 'depth', 'regexp', 'width', 'height', 'maxWidth', 'maxHeight', 'display', 'grid', 'gridGap', 'noCarousel',
 ];
 ];
 
 
 const { hasHeadingSlash } = pathUtils;
 const { hasHeadingSlash } = pathUtils;
@@ -62,6 +62,11 @@ export const remarkPlugin: Plugin = function() {
           return;
           return;
         }
         }
 
 
+        attributes.maxWidth = attributes['max-width'];
+        attributes.maxHeight = attributes['max-height'];
+        attributes.gridGap = attributes['grid-gap'];
+        attributes.noCarousel = attributes['no-carousel'];
+
         data.hName = node.name;
         data.hName = node.name;
         data.hProperties = attributes;
         data.hProperties = attributes;
       }
       }