diff --git a/.changeset/silver-badgers-heal.md b/.changeset/silver-badgers-heal.md new file mode 100644 index 0000000000..b1f239c3dc --- /dev/null +++ b/.changeset/silver-badgers-heal.md @@ -0,0 +1,35 @@ +--- +'@backstage/plugin-techdocs-module-addons-contrib': patch +'@backstage/plugin-techdocs': patch +--- + +Create a TechDocs `` addon that allows users to open images in a light-box on documentation pages, they can navigate between images if there are several on one page. + +Here's an example on how to use it in a Backstage app: + +```diff +import { + DefaultTechDocsHome, + TechDocsIndexPage, + TechDocsReaderPage, +} from '@backstage/plugin-techdocs'; +import { TechDocsAddons } from '@backstage/plugin-techdocs-react/alpha'; ++import { LightBox } from '@backstage/plugin-techdocs-module-addons-contrib'; + +const AppRoutes = () => { + + // other plugin routes + }> + + + } + > + ++ + + + ; +}; +``` diff --git a/docs/features/techdocs/addons.md b/docs/features/techdocs/addons.md index fe1058b2ca..60999f599c 100644 --- a/docs/features/techdocs/addons.md +++ b/docs/features/techdocs/addons.md @@ -125,6 +125,7 @@ discover available Addons, we've compiled a list of them here: | [``](https://backstage.io/docs/reference/plugin-techdocs-module-addons-contrib.expandablenavigation) | `@backstage/plugin-techdocs-module-addons-contrib` | Allows TechDocs users to expand or collapse the entire TechDocs main navigation, and keeps the user's preferred state between documentation sites. | | [``](https://backstage.io/docs/reference/plugin-techdocs-module-addons-contrib.reportissue) | `@backstage/plugin-techdocs-module-addons-contrib` | Allows TechDocs users to select a portion of text on a TechDocs page and open an issue against the repository that contains the documentation, populating the issue description with the selected text according to a configurable template. | | [``](https://backstage.io/docs/reference/plugin-techdocs-module-addons-contrib.textsize) | `@backstage/plugin-techdocs-module-addons-contrib` | This TechDocs addon allows users to customize text size on documentation pages, they can select how much they want to increase or decrease the font size via slider or buttons. The default value for font size is 100% and this setting is kept in the browser's local storage whenever it is changed. | +| [``](https://backstage.io/docs/reference/plugin-techdocs-module-addons-contrib.lightbox) | `@backstage/plugin-techdocs-module-addons-contrib` | This TechDocs addon allows users to open images in a light-box on documentation pages, they can navigate between images if there are several on one page. The image size of the light-box image is the same as the image size on the document page. | Got an Addon to contribute? Feel free to add a row above! diff --git a/packages/app/src/App.tsx b/packages/app/src/App.tsx index 8984a69259..f789328104 100644 --- a/packages/app/src/App.tsx +++ b/packages/app/src/App.tsx @@ -78,6 +78,7 @@ import { ExpandableNavigation, ReportIssue, TextSize, + LightBox, } from '@backstage/plugin-techdocs-module-addons-contrib'; import { SettingsLayout, @@ -203,6 +204,7 @@ const routes = ( + + ); diff --git a/plugins/techdocs-module-addons-contrib/api-report.md b/plugins/techdocs-module-addons-contrib/api-report.md index 68fad9121d..c242bb02f2 100644 --- a/plugins/techdocs-module-addons-contrib/api-report.md +++ b/plugins/techdocs-module-addons-contrib/api-report.md @@ -10,6 +10,9 @@ import { BackstagePlugin } from '@backstage/core-plugin-api'; // @public export const ExpandableNavigation: () => JSX.Element | null; +// @public +export const LightBox: () => JSX.Element | null; + // @public export const ReportIssue: (props: ReportIssueProps) => JSX.Element | null; diff --git a/plugins/techdocs-module-addons-contrib/package.json b/plugins/techdocs-module-addons-contrib/package.json index 1b92e15c16..cd72b1a548 100644 --- a/plugins/techdocs-module-addons-contrib/package.json +++ b/plugins/techdocs-module-addons-contrib/package.json @@ -44,6 +44,7 @@ "@material-ui/lab": "4.0.0-alpha.57", "@react-hookz/web": "^20.0.0", "git-url-parse": "^13.0.0", + "photoswipe": "^5.3.5", "react-use": "^17.2.4" }, "peerDependencies": { diff --git a/plugins/techdocs-module-addons-contrib/src/LigthBox/LightBox.test.tsx b/plugins/techdocs-module-addons-contrib/src/LigthBox/LightBox.test.tsx new file mode 100644 index 0000000000..b63aed8777 --- /dev/null +++ b/plugins/techdocs-module-addons-contrib/src/LigthBox/LightBox.test.tsx @@ -0,0 +1,50 @@ +/* + * Copyright 2022 The Backstage Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { TechDocsAddonTester } from '@backstage/plugin-techdocs-addons-test-utils'; + +import React from 'react'; + +import { LightBox } from '../plugin'; + +describe('LightBox', () => { + it('renders without exploding', async () => { + const { getByText } = await TechDocsAddonTester.buildAddonsInTechDocs([ + , + ]) + .withDom(TEST_CONTENT) + .renderWithEffects(); + + expect(getByText('TEST_CONTENT')).toBeInTheDocument(); + }); + + it('Add onclick event to images', async () => { + const { getByTestId } = await TechDocsAddonTester.buildAddonsInTechDocs([ + , + ]) + .withDom( + dog, + ) + .renderWithEffects(); + + expect(getByTestId('fixture').onclick).not.toBeUndefined(); + expect(getByTestId('fixture').onclick).toEqual(expect.any(Function)); + }); +}); diff --git a/plugins/techdocs-module-addons-contrib/src/LigthBox/LightBox.tsx b/plugins/techdocs-module-addons-contrib/src/LigthBox/LightBox.tsx new file mode 100644 index 0000000000..b74c2f9659 --- /dev/null +++ b/plugins/techdocs-module-addons-contrib/src/LigthBox/LightBox.tsx @@ -0,0 +1,71 @@ +/* + * Copyright 2022 The Backstage Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { useEffect } from 'react'; +import { useShadowRootElements } from '@backstage/plugin-techdocs-react'; +// @ts-ignore +import PhotoSwipeLightbox, { DataSource } from 'photoswipe/lightbox'; +import PhotoSwipe from 'photoswipe'; +import 'photoswipe/style.css'; +import './lightbox.css'; + +export const LightBoxAddon = () => { + const images = useShadowRootElements(['img']); + + useEffect(() => { + let dataSourceImages: DataSource | null = null; + + let lightbox = new PhotoSwipeLightbox({ + pswpModule: PhotoSwipe, + wheelToZoom: true, + arrowPrevSVG: + '', + arrowNextSVG: + '', + closeSVG: + '', + zoomSVG: + '', + }); + + images.forEach((image, index) => { + image.onclick = () => { + if (dataSourceImages === null) { + dataSourceImages = images.map(dataSourceImage => { + return { + element: dataSourceImage, + src: dataSourceImage.src, + msrc: dataSourceImage.src, + alt: dataSourceImage.alt, + width: dataSourceImage.clientWidth, + height: dataSourceImage.clientHeight, + }; + }); + } + lightbox.loadAndOpen(index, dataSourceImages); + return false; + }; + }); + lightbox.init(); + + return () => { + lightbox.destroy(); + lightbox = null; + }; + }, [images]); + + return null; +}; diff --git a/plugins/techdocs-module-addons-contrib/src/LigthBox/index.ts b/plugins/techdocs-module-addons-contrib/src/LigthBox/index.ts new file mode 100644 index 0000000000..144359abb7 --- /dev/null +++ b/plugins/techdocs-module-addons-contrib/src/LigthBox/index.ts @@ -0,0 +1,16 @@ +/* + * Copyright 2022 The Backstage Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +export * from './LightBox'; diff --git a/plugins/techdocs-module-addons-contrib/src/LigthBox/lightbox.css b/plugins/techdocs-module-addons-contrib/src/LigthBox/lightbox.css new file mode 100644 index 0000000000..63170792f1 --- /dev/null +++ b/plugins/techdocs-module-addons-contrib/src/LigthBox/lightbox.css @@ -0,0 +1,7 @@ +.pswp { + --pswp-bg: #f8f8f8; +} +.pswp__counter { + color: rgba(0, 0, 0, 0.87); + text-shadow: none; +} diff --git a/plugins/techdocs-module-addons-contrib/src/index.ts b/plugins/techdocs-module-addons-contrib/src/index.ts index 528afe5c0d..bad7701486 100644 --- a/plugins/techdocs-module-addons-contrib/src/index.ts +++ b/plugins/techdocs-module-addons-contrib/src/index.ts @@ -25,6 +25,7 @@ export { ExpandableNavigation, ReportIssue, TextSize, + LightBox, } from './plugin'; export type { ReportIssueProps, diff --git a/plugins/techdocs-module-addons-contrib/src/plugin.ts b/plugins/techdocs-module-addons-contrib/src/plugin.ts index 6136f176d0..c4023a72af 100644 --- a/plugins/techdocs-module-addons-contrib/src/plugin.ts +++ b/plugins/techdocs-module-addons-contrib/src/plugin.ts @@ -22,6 +22,7 @@ import { import { ExpandableNavigationAddon } from './ExpandableNavigation'; import { ReportIssueAddon, ReportIssueProps } from './ReportIssue'; import { TextSizeAddon } from './TextSize'; +import { LightBoxAddon } from './LigthBox'; /** * The TechDocs addons contrib plugin @@ -203,3 +204,49 @@ export const TextSize = techdocsModuleAddonsContribPlugin.provide( component: TextSizeAddon, }), ); + +/** + * This TechDocs addon allows users to open images in a lightbox on documentation pages, they can navigate between images if there are several on one page. + * + * @remarks + * The image size of the lightbox image is the same as the image size on the document page. + * + * @example + * Here's a simple example: + * ``` + * import { + * DefaultTechDocsHome, + * TechDocsIndexPage, + * TechDocsReaderPage, + * } from '@backstage/plugin-techdocs'; + * import { TechDocsAddons } from '@backstage/plugin-techdocs-react'; + * import { LightBox } from '@backstage/plugin-techdocs-module-addons-contrib'; + * + * + * const AppRoutes = () => { + * + * // other plugin routes + * }> + * + * + * } + * > + * + * + * + * + * ; + * }; + * ``` + * + * @public + */ +export const LightBox = techdocsModuleAddonsContribPlugin.provide( + createTechDocsAddonExtension({ + name: 'LightBox', + location: TechDocsAddonLocations.Content, + component: LightBoxAddon, + }), +); diff --git a/plugins/techdocs/src/reader/components/TechDocsReaderPage/TechDocsReaderPage.tsx b/plugins/techdocs/src/reader/components/TechDocsReaderPage/TechDocsReaderPage.tsx index ffe95e0b7e..ea7e4ed77b 100644 --- a/plugins/techdocs/src/reader/components/TechDocsReaderPage/TechDocsReaderPage.tsx +++ b/plugins/techdocs/src/reader/components/TechDocsReaderPage/TechDocsReaderPage.tsx @@ -102,6 +102,7 @@ CONFIGURATION 4: and provided content in + diff --git a/yarn.lock b/yarn.lock index f042b70bf5..4a15b8cf09 100644 --- a/yarn.lock +++ b/yarn.lock @@ -8225,6 +8225,7 @@ __metadata: cross-fetch: ^3.1.5 git-url-parse: ^13.0.0 msw: ^0.49.0 + photoswipe: ^5.3.5 react-use: ^17.2.4 peerDependencies: react: ^16.13.1 || ^17.0.0 @@ -31430,6 +31431,13 @@ __metadata: languageName: node linkType: hard +"photoswipe@npm:^5.3.5": + version: 5.3.5 + resolution: "photoswipe@npm:5.3.5" + checksum: bd3eafebffc114210b8e5403746b48a9bfe436cca05a65f8d5efd6eacea785a51f0f124d7326c8f651d754d76124c9768a920383843c4a619da582a86e535b27 + languageName: node + linkType: hard + "picocolors@npm:^1.0.0": version: 1.0.0 resolution: "picocolors@npm:1.0.0"