11import React from "react" ;
22
3- import { ContentBlobToggler , ContentBlobTogglerProps , Markdown } from "./.." ;
3+ import { ContentBlobToggler , ContentBlobTogglerProps , InlineText , Markdown , utils } from "./../../index " ;
44
55export interface StringPreviewContentBlobTogglerProps
66 extends Omit < ContentBlobTogglerProps , "previewContent" | "enableToggler" > {
77 /**
8- The preview content will be cut to this length if it is too long.
8+ * The preview content will be cut to this length if it is too long.
99 */
1010 previewMaxLength ?: number ;
1111 /**
12- The content string. If it is smaller than previewMaxLength this will be displayed in full, else fullviewContent will be displayed.
12+ * The content string.
13+ * If it is smaller than `previewMaxLength` this will be displayed in full, else `fullviewContent` will be displayed.
1314 */
1415 content : string ;
15- /** If only the first non-empty line should be shown in the preview. This will in addition also be shortened according to previewMaxLength. */
16- firstNonEmptyLineOnly ?: boolean ;
17- /** If enabled the preview is rendered as markdown. */
16+ /**
17+ * Use only parts of `content` in the preview.
18+ * `firstMarkdownSection` uses the content until the first double line return.
19+ * Currently overwritten by `firstNonEmptyLineOnly`.
20+ */
21+ useOnly ?: "firstNonEmptyLine" | "firstMarkdownSection" ;
22+ /**
23+ * If enabled the preview is rendered as Markdown.
24+ */
1825 renderPreviewAsMarkdown ?: boolean ;
19- /** White-listing of HTML elements that will be rendered when renderPreviewAsMarkdown is enabled. */
26+ /**
27+ * White-listing of HTML elements that will be rendered when renderPreviewAsMarkdown is enabled.
28+ */
2029 allowedHtmlElementsInPreview ?: string [ ] ;
21- /** Allows to add non-string elements at the end of the content if the full description is shown, i.e. no toggler is necessary.
30+ /**
31+ * Allows to add non-string elements at the end of the content if the full description is shown, i.e. no toggler is necessary.
2232 * This allows to add non-string elements to both the full-view content and the pure string content.
2333 */
2434 noTogglerContentSuffix ?: JSX . Element ;
35+ /**
36+ * If only the first non-empty line should be shown in the preview.
37+ * This will in addition also be shortened according to `previewMaxLength`.
38+ * @deprecated (v26) use `useOnly="firstNonEmptyLine"` instead
39+ */
40+ firstNonEmptyLineOnly ?: boolean ;
2541}
2642
27- /** Version of the content toggler for text only content. */
43+ /** Version of the content toggler for text centric content. */
2844export function StringPreviewContentBlobToggler ( {
2945 className = "" ,
3046 previewMaxLength,
@@ -33,21 +49,44 @@ export function StringPreviewContentBlobToggler({
3349 content,
3450 fullviewContent,
3551 startExtended,
36- firstNonEmptyLineOnly ,
52+ useOnly ,
3753 renderPreviewAsMarkdown = false ,
3854 allowedHtmlElementsInPreview,
3955 noTogglerContentSuffix,
56+ firstNonEmptyLineOnly,
4057} : StringPreviewContentBlobTogglerProps ) {
41- const previewMaybeFirstLine = firstNonEmptyLineOnly ? firstNonEmptyLine ( content ) : content ;
42- const previewString = previewMaxLength ? previewMaybeFirstLine . substr ( 0 , previewMaxLength ) : previewMaybeFirstLine ;
43- const enableToggler = previewString !== content ;
58+ // need to test `firstNonEmptyLineOnly` until property is removed
59+ const useOnlyTest : StringPreviewContentBlobTogglerProps [ "useOnly" ] = firstNonEmptyLineOnly
60+ ? "firstNonEmptyLine"
61+ : useOnly ;
62+
63+ let previewString = content ;
64+ switch ( useOnlyTest ) {
65+ case "firstNonEmptyLine" :
66+ previewString = useOnlyPart ( content , regexFirstNonEmptyLine ) ;
67+ break ;
68+ case "firstMarkdownSection" :
69+ previewString = useOnlyPart ( content , regexFirstMarkdownSection ) ;
70+ }
71+
72+ let enableToggler = previewString !== content ;
73+
4474 let previewContent = renderPreviewAsMarkdown ? (
4575 < Markdown key = "markdown-content" allowedElements = { allowedHtmlElementsInPreview } >
4676 { previewString }
4777 </ Markdown >
4878 ) : (
4979 previewString
5080 ) ;
81+
82+ if (
83+ previewMaxLength &&
84+ utils . reduceToText ( previewContent , { decodeHtmlEntities : true } ) . length > previewMaxLength
85+ ) {
86+ previewContent = utils . reduceToText ( previewContent , { decodeHtmlEntities : true } ) . slice ( 0 , previewMaxLength ) ;
87+ enableToggler = true ;
88+ }
89+
5190 if ( ! enableToggler && noTogglerContentSuffix ) {
5291 previewContent = (
5392 < >
@@ -60,7 +99,7 @@ export function StringPreviewContentBlobToggler({
6099 return (
61100 < ContentBlobToggler
62101 className = { className }
63- previewContent = { previewContent }
102+ previewContent = { < InlineText > { previewContent } </ InlineText > }
64103 toggleExtendText = { toggleExtendText }
65104 toggleReduceText = { toggleReduceText }
66105 fullviewContent = { fullviewContent }
@@ -70,17 +109,26 @@ export function StringPreviewContentBlobToggler({
70109 ) ;
71110}
72111
73- const newLineRegex = new RegExp ( "\r|\n" ) ; // eslint-disable-line
112+ const regexFirstNonEmptyLine = new RegExp ( "\r|\n" ) ; // eslint-disable-line
113+ const regexFirstMarkdownSection = new RegExp ( "\r\n\r\n|\n\n" ) ; // eslint-disable-line
74114
75115/**
76116 * Takes the first non-empty line from a preview string.
77117 */
78118function firstNonEmptyLine ( preview : string ) {
119+ return useOnlyPart ( preview , regexFirstNonEmptyLine ) ;
120+ }
121+
122+ /**
123+ * Returns only the first part from a preview string.
124+ * Or the full string as fallback.
125+ */
126+ function useOnlyPart ( preview : string , regexTest : RegExp ) : string {
79127 const previewString = preview . trim ( ) ;
80- const result = newLineRegex . exec ( previewString ) ;
81- return result !== null ? previewString . substr ( 0 , result . index ) : previewString ;
128+ const result = regexTest . exec ( previewString ) ;
129+ return result !== null ? result . input . slice ( 0 , result . index ) : previewString ;
82130}
83131
84132export const stringPreviewContentBlobTogglerUtils = {
85133 firstNonEmptyLine,
86- } ;
134+ } ;
0 commit comments