Build your own AI office with Univer Office SDK. Bring editing, calculation, collaboration, and file processing into your product for people and AI agents. Built on an extensible open-source core.
TypeScript boardcollaborationdocdocxexceloffce365officepdfpptpptxpresentationsdksheetslidesspreadsheetuniverwordwordprocessorworkspacexlsx
feat(design): add Gallery (#5164)
白熱d5858c540a0b
diff --git a/packages-experimental/uniui/src/plugin.ts b/packages-experimental/uniui/src/plugin.tsindex 7a49fee973..7f62906f39 100644--- a/packages-experimental/uniui/src/plugin.ts+++ b/packages-experimental/uniui/src/plugin.ts@@ -27,6 +27,7 @@ import {DesktopBeforeCloseService,DesktopConfirmService,DesktopDialogService,+ DesktopGalleryService,DesktopGlobalZoneService,DesktopLayoutService,DesktopLocalFileService,@@ -43,6 +44,7 @@ import {IConfirmService,IContextMenuService,IDialogService,+ IGalleryService,IGlobalZoneService,ILayoutService,ILeftSidebarService,@@ -112,6 +114,7 @@ export class UniverUniUIPlugin extends Plugin {[IContextMenuService, { useClass: ContextMenuService }],[IClipboardInterfaceService, { useClass: BrowserClipboardService, lazy: true }],[INotificationService, { useClass: DesktopNotificationService, lazy: true }],+ [IGalleryService, { useClass: DesktopGalleryService, lazy: true }],[IDialogService, { useClass: DesktopDialogService, lazy: true }],[IConfirmService, { useClass: DesktopConfirmService, lazy: true }],[ISidebarService, { useClass: DesktopSidebarService, lazy: true }],diff --git a/packages/design/src/components/gallery/Gallery.stories.tsx b/packages/design/src/components/gallery/Gallery.stories.tsxnew file mode 100644index 0000000000..d6969577cf--- /dev/null+++ b/packages/design/src/components/gallery/Gallery.stories.tsx@@ -0,0 +1,48 @@+/**+ * Copyright 2023-present DreamNum Co., Ltd.+ *+ * 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 type { Meta } from '@storybook/react';+import { useState } from 'react';+import { Button } from '../button/Button';+import { Gallery } from './Gallery';++const meta: Meta<typeof Gallery> = {+ title: 'Components / Gallery',+ component: Gallery,+ parameters: {+ layout: 'centered',+ },+ tags: ['autodocs'],+};++export default meta;++export const GalleryBasic = {+ render() {+ const [open, setOpen] = useState(false);++ const images = ['https://github.com/dream-num.png', 'https://github.com/awesome-univer.png', 'https://github.com/dream-num.png'];++ return (+ <>+ <div>+ <Button onClick={() => setOpen(true)}>Open Gallery</Button>+ <Gallery open={open} onOpenChange={setOpen} images={images} />+ </div>+ </>+ );+ },+};diff --git a/packages/design/src/components/gallery/Gallery.tsx b/packages/design/src/components/gallery/Gallery.tsxnew file mode 100644index 0000000000..3aac6e9a75--- /dev/null+++ b/packages/design/src/components/gallery/Gallery.tsx@@ -0,0 +1,149 @@+/**+ * Copyright 2023-present DreamNum Co., Ltd.+ *+ * 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 { OneToOneSingle, ZoomInSingle, ZoomOutSingle } from '@univerjs/icons';+import { useEffect, useMemo, useState } from 'react';+import { createPortal } from 'react-dom';+import { clsx } from '../../helper/clsx';+import { Pager } from '../pager/Pager';++export interface IGalleryProps {+ className?: string;+ images: string[];+ open?: boolean;+ onOpenChange?: (open: boolean) => void;+}++export function Gallery(props: IGalleryProps) {+ const { className, images, open, onOpenChange } = props;+ const [isVisible, setIsVisible] = useState(false);++ const [activeImageIndex, setActiveImageIndex] = useState(0);+ const [zoomLevel, setZoomLevel] = useState(1);++ const activeImage = useMemo(() => images[activeImageIndex], [activeImageIndex, images]);++ useEffect(() => {+ if (open) {+ setIsVisible(true);+ } else {+ const timer = setTimeout(() => {+ setIsVisible(false);+ }, 150);+ return () => clearTimeout(timer);+ }+ }, [open]);++ if (!open && !isVisible) return null;++ function handleToggleZoom(ratio: number | 'reset') {+ if (ratio === 'reset') {+ setZoomLevel(1);+ return;+ }++ const newZoomLevel = zoomLevel + ratio;+ if (newZoomLevel < 0.5) return;+ if (newZoomLevel > 2) return;+ setZoomLevel(newZoomLevel);+ }++ return createPortal(+ <div+ data-u-comp="gallery"+ className={clsx(+ `+ univer-absolute univer-inset-0 univer-z-[1080] univer-flex univer-size-full univer-select-none+ univer-items-center univer-justify-center+ `,+ {+ 'univer-animate-in univer-fade-in': open,+ 'univer-animate-out univer-fade-out': !open,+ },+ className+ )}+ >+ <div+ className="univer-absolute univer-inset-0 univer-size-full univer-bg-gray-900 univer-opacity-80"+ onClick={() => onOpenChange?.(false)}+ />++ {/* Content */}+ <div className="univer-relative">+ <img+ className={`+ univer-max-h-[40vh] univer-max-w-[60vw] univer-transition-transform univer-duration-300+ univer-ease-out+ `}+ style={{+ transform: `scale(${zoomLevel})`,+ }}+ src={activeImage}+ alt="gallery"+ draggable={false}+ />+ </div>++ {/* Toolbar */}+ <footer+ className={`+ univer-absolute univer-bottom-6 univer-left-1/2 univer-flex -univer-translate-x-1/2+ univer-items-center univer-gap-3 univer-rounded-full univer-bg-gray-800 univer-px-6 univer-py-3+ univer-text-gray-400+ `}+ >+ <Pager+ className={`+ !univer-text-gray-400+ [&_[data-u-comp=pager-left-arrow]:hover]:!univer-bg-gray-600+ [&_[data-u-comp=pager-right-arrow]:hover]:!univer-bg-gray-600+ `}+ value={activeImageIndex + 1}+ total={images.length}+ onChange={(value) => setActiveImageIndex(value - 1)}+ />+ <a+ className={`+ univer-flex univer-cursor-pointer univer-items-center univer-justify-center+ hover:univer-text-white+ `}+ onClick={() => handleToggleZoom(0.25)}+ >+ <ZoomInSingle />+ </a>+ <a+ className={`+ univer-flex univer-cursor-pointer univer-items-center univer-justify-center+ hover:univer-text-white+ `}+ onClick={() => handleToggleZoom(-0.25)}+ >+ <ZoomOutSingle />+ </a>+ <a+ className={`+ univer-flex univer-cursor-pointer univer-items-center univer-justify-center+ hover:univer-text-white+ `}+ onClick={() => handleToggleZoom('reset')}+ >+ <OneToOneSingle />+ </a>+ </footer>+ </div>,+ document.body+ );+}diff --git a/packages/design/src/components/pager/Pager.tsx b/packages/design/src/components/pager/Pager.tsxindex f3dc1c208d..867f7e0f92 100644--- a/packages/design/src/components/pager/Pager.tsx+++ b/packages/design/src/components/pager/Pager.tsx@@ -16,8 +16,11 @@import { MoreLeftSingle, MoreRightSingle } from '@univerjs/icons';import { useMemo } from 'react';+import { clsx } from '../../helper/clsx';export interface IPagerProps {+ className?: string;+text?: string;value: number;@@ -28,7 +31,8 @@ export interface IPagerProps {}export function Pager(props: IPagerProps) {- const { value: current = 0, total: count = 0, loop, text: propText } = props;+ const { className, value: current = 0, total: count = 0, loop, text: propText } = props;+const text = useMemo(() => propText ?? `${current}/${count}`, [current, count, propText]);const hasValue = count > 0;@@ -53,13 +57,21 @@ export function Pager(props: IPagerProps) {};return (- <div className="univer-flex univer-flex-shrink-0 univer-items-center univer-text-[13px] univer-text-gray-700">+ <div+ data-u-comp="pager"+ className={clsx(`+ univer-flex univer-flex-shrink-0 univer-items-center univer-text-[13px] univer-text-gray-700+ dark:univer-text-gray-400+ `, className)}+ >{hasValue? (<><div+ data-u-comp="pager-left-arrow"className={`univer-inline-flex univer-size-4 univer-cursor-pointer univer-items-center univer-rounded+ dark:hover:univer-bg-gray-600hover:univer-bg-gray-50`}role="button"@@ -69,8 +81,10 @@ export function Pager(props: IPagerProps) {</div><div className="univer-mx-1">{text}</div><div+ data-u-comp="pager-right-arrow"className={`univer-inline-flex univer-size-4 univer-cursor-pointer univer-items-center univer-rounded+ dark:hover:univer-bg-gray-600hover:univer-bg-gray-50`}role="button"diff --git a/packages/design/src/index.ts b/packages/design/src/index.tsindex 49aeff4805..fdcbe0dd44 100644--- a/packages/design/src/index.ts+++ b/packages/design/src/index.ts@@ -45,6 +45,7 @@ export { Separator } from './components/separator/Separator';export { type IKBDProps, KBD } from './components/kbd/Kbd';export { type IToasterProps, toast, Toaster } from './components/toaster/Toaster';export { Accordion, type IAccordionProps } from './components/accordion/Accordion';+export { Gallery, type IGalleryProps } from './components/gallery/Gallery';export { DatePanel, DatePicker, type IDatePanelProps, type IDatePickerProps } from './components/date-picker';export { DropdownLegacy, type IDropdownLegacyProps } from './components/dropdown-legacy';diff --git a/packages/drawing-ui/src/controllers/drawing-ui.controller.ts b/packages/drawing-ui/src/controllers/drawing-ui.controller.tsindex f2d141b87c..f61e6ef0ad 100644--- a/packages/drawing-ui/src/controllers/drawing-ui.controller.ts+++ b/packages/drawing-ui/src/controllers/drawing-ui.controller.ts@@ -21,8 +21,6 @@ import { AutoImageCropOperation, CloseImageCropOperation, OpenImageCropOperationimport { ImageResetSizeOperation } from '../commands/operations/image-reset-size.operation';import { COMPONENT_IMAGE_POPUP_MENU } from '../views/image-popup-menu/component-name';import { ImagePopupMenu } from '../views/image-popup-menu/ImagePopupMenu';-import { COMPONENT_IMAGE_VIEWER } from '../views/image-viewer/component-name';-import { ImageViewer } from '../views/image-viewer/ImageViewer';export class DrawingUIController extends Disposable {constructor(@@ -37,7 +35,6 @@ export class DrawingUIController extends Disposable {private _initCustomComponents(): void {const componentManager = this._componentManager;this.disposeWithMe(componentManager.register(COMPONENT_IMAGE_POPUP_MENU, ImagePopupMenu));- this.disposeWithMe(componentManager.register(COMPONENT_IMAGE_VIEWER, ImageViewer));}private _initCommands() {diff --git a/packages/drawing-ui/src/services/drawing-render.service.ts b/packages/drawing-ui/src/services/drawing-render.service.tsindex e6545eb071..01625fb15b 100644--- a/packages/drawing-ui/src/services/drawing-render.service.ts+++ b/packages/drawing-ui/src/services/drawing-render.service.ts@@ -20,17 +20,16 @@ import type { IImageProps, IRectProps, Scene } from '@univerjs/engine-render';import { DrawingTypeEnum } from '@univerjs/core';import { getDrawingShapeKeyByDrawingSearch, IDrawingManagerService, IImageIoService, ImageSourceType } from '@univerjs/drawing';import { DRAWING_OBJECT_LAYER_INDEX, Image, Rect } from '@univerjs/engine-render';-import { IDialogService } from '@univerjs/ui';+import { IGalleryService } from '@univerjs/ui';import { insertGroupObject } from '../controllers/utils';-import { COMPONENT_IMAGE_VIEWER } from '../views/image-viewer/component-name';-const IMAGE_VIEWER_DROPDOWN_PADDING = 50;+// const IMAGE_VIEWER_DROPDOWN_PADDING = 50;export class DrawingRenderService {constructor(@IDrawingManagerService private readonly _drawingManagerService: IDrawingManagerService,@IImageIoService private readonly _imageIoService: IImageIoService,- @IDialogService private readonly _dialogService: IDialogService+ @IGalleryService private readonly _galleryService: IGalleryService) { }// eslint-disable-next-line max-lines-per-function@@ -211,30 +210,20 @@ export class DrawingRenderService {}previewImage(key: string, src: string, width: number, height: number) {- const dialogId = `${key}-viewer-dialog`;-- const screenWidth = window.innerWidth - IMAGE_VIEWER_DROPDOWN_PADDING;- const screenHeight = window.innerHeight - IMAGE_VIEWER_DROPDOWN_PADDING;-- const adjustSize = this._adjustImageSize(width, height, screenWidth, screenHeight);- const dialog = this._dialogService.open({- width: Math.max(adjustSize.width, 200),- id: dialogId,- children: {- label: {- name: COMPONENT_IMAGE_VIEWER,- props: {- src,- width: adjustSize.width,- height: adjustSize.height,- },- },- },- destroyOnClose: true,- closable: false,- onClose: () => {- this._dialogService.close(dialogId);- dialog.dispose();+ // const dialogId = `${key}-viewer-dialog`;++ // const screenWidth = window.innerWidth - IMAGE_VIEWER_DROPDOWN_PADDING;+ // const screenHeight = window.innerHeight - IMAGE_VIEWER_DROPDOWN_PADDING;++ // const adjustSize = this._adjustImageSize(width, height, screenWidth, screenHeight);+ this._galleryService.open({+ images: [src],+ // width: adjustSize.width,+ // height: adjustSize.height,+ onOpenChange: (open) => {+ if (!open) {+ this._galleryService.close();+ }},});}diff --git a/packages/ui/src/index.ts b/packages/ui/src/index.tsindex 1db998577b..197c9e01bc 100644--- a/packages/ui/src/index.ts+++ b/packages/ui/src/index.ts@@ -63,6 +63,8 @@ export { DesktopConfirmService } from './services/confirm/desktop-confirm.servicexport { ContextMenuService, type IContextMenuHandler, IContextMenuService } from './services/contextmenu/contextmenu.service';export { DesktopDialogService } from './services/dialog/desktop-dialog.service';export { IDialogService } from './services/dialog/dialog.service';+export { IGalleryService } from './services/gallery/gallery.service';+export { DesktopGalleryService } from './services/gallery/desktop-gallery.service';export { CanvasFloatDomService, type IFloatDom, type IFloatDomLayout } from './services/dom/canvas-dom-layer.service';export { DesktopGlobalZoneService } from './services/global-zone/desktop-global-zone.service';export { IGlobalZoneService } from './services/global-zone/global-zone.service';diff --git a/packages/ui/src/mobile-ui-plugin.ts b/packages/ui/src/mobile-ui-plugin.tsindex 1f51b704b3..d2b762fcb7 100644--- a/packages/ui/src/mobile-ui-plugin.ts+++ b/packages/ui/src/mobile-ui-plugin.ts@@ -32,6 +32,8 @@ import { ContextMenuService, IContextMenuService } from './services/contextmenu/import { DesktopDialogService } from './services/dialog/desktop-dialog.service';import { IDialogService } from './services/dialog/dialog.service';import { CanvasFloatDomService } from './services/dom/canvas-dom-layer.service';+import { DesktopGalleryService } from './services/gallery/desktop-gallery.service';+import { IGalleryService } from './services/gallery/gallery.service';import { DesktopGlobalZoneService } from './services/global-zone/desktop-global-zone.service';import { IGlobalZoneService } from './services/global-zone/global-zone.service';import { DesktopLayoutService, ILayoutService } from './services/layout/layout.service';@@ -82,6 +84,7 @@ export class UniverMobileUIPlugin extends Plugin {[IContextMenuService, { useClass: ContextMenuService }],[IClipboardInterfaceService, { useClass: BrowserClipboardService, lazy: true }],[INotificationService, { useClass: DesktopNotificationService, lazy: true }],+ [IGalleryService, { useClass: DesktopGalleryService, lazy: true }],[IDialogService, { useClass: DesktopDialogService, lazy: true }],[IConfirmService, { useClass: DesktopConfirmService, lazy: true }],[ISidebarService, { useClass: DesktopSidebarService, lazy: true }],diff --git a/packages/ui/src/services/gallery/desktop-gallery.service.ts b/packages/ui/src/services/gallery/desktop-gallery.service.tsnew file mode 100644index 0000000000..3fa2510a21--- /dev/null+++ b/packages/ui/src/services/gallery/desktop-gallery.service.ts@@ -0,0 +1,65 @@+/**+ * Copyright 2023-present DreamNum Co., Ltd.+ *+ * 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 type { IDisposable } from '@univerjs/core';+import type { IGalleryProps } from '@univerjs/design';+import type { IGalleryService } from './gallery.service';+import { Disposable, Inject, Injector, toDisposable } from '@univerjs/core';+import { Subject } from 'rxjs';+import { connectInjector } from '../../utils/di';+import { GalleryPart } from '../../views/components/gallery-part/GalleryPart';+import { BuiltInUIPart, IUIPartsService } from '../parts/parts.service';++export class DesktopGalleryService extends Disposable implements IGalleryService {+ constructor(+ @Inject(Injector) protected readonly _injector: Injector,+ @IUIPartsService protected readonly _uiPartsService: IUIPartsService+ ) {+ super();++ this._initUIPart();+ }++ gallery$ = new Subject<IGalleryProps>();++ override dispose(): void {+ super.dispose();+ }++ open(option: IGalleryProps): IDisposable {+ this.gallery$.next({+ open: true,+ ...option,+ });++ return toDisposable(() => {+ this.gallery$.complete();+ });+ }++ close() {+ this.gallery$.next({+ open: false,+ images: [],+ });+ }++ protected _initUIPart(): void {+ this.disposeWithMe(+ this._uiPartsService.registerComponent(BuiltInUIPart.GLOBAL, () => connectInjector(GalleryPart, this._injector))+ );+ }+}diff --git a/packages/drawing-ui/src/views/image-viewer/ImageViewer.tsx b/packages/ui/src/services/gallery/gallery.service.tssimilarity index 52%rename from packages/drawing-ui/src/views/image-viewer/ImageViewer.tsxrename to packages/ui/src/services/gallery/gallery.service.tsindex 9be4550516..297a4c161c 100644--- a/packages/drawing-ui/src/views/image-viewer/ImageViewer.tsx+++ b/packages/ui/src/services/gallery/gallery.service.ts@@ -14,27 +14,14 @@* limitations under the License.*/-import type { Nullable } from '@univerjs/core';-import React from 'react';+import type { IDisposable } from '@univerjs/core';+import type { IGalleryProps } from '@univerjs/design';+import type { Subject } from 'rxjs';+import { createIdentifier } from '@univerjs/core';-export interface IImageViewerProps {- src: Nullable<string>;+export const IGalleryService = createIdentifier<IGalleryService>('univer.ui.gallery-service');+export interface IGalleryService {+ gallery$: Subject<IGalleryProps>;+ open(params: IGalleryProps): IDisposable;+ close(): void;}--export const ImageViewer: React.FC<IImageViewerProps> = (props: IImageViewerProps) => {- const { src } = props;-- if (!src) {- return null;- }-- return (- <div className="univer-py-2">- <img- className="univer-relative univer-block univer-size-full"- src={src}- alt="Univer Image Viewer"- />- </div>- );-};diff --git a/packages/ui/src/ui-plugin.ts b/packages/ui/src/ui-plugin.tsindex d07a761d06..a4038ed852 100644--- a/packages/ui/src/ui-plugin.ts+++ b/packages/ui/src/ui-plugin.ts@@ -34,6 +34,8 @@ import { ContextMenuService, IContextMenuService } from './services/contextmenu/import { DesktopDialogService } from './services/dialog/desktop-dialog.service';import { IDialogService } from './services/dialog/dialog.service';import { CanvasFloatDomService } from './services/dom/canvas-dom-layer.service';+import { DesktopGalleryService } from './services/gallery/desktop-gallery.service';+import { IGalleryService } from './services/gallery/gallery.service';import { DesktopGlobalZoneService } from './services/global-zone/desktop-global-zone.service';import { IGlobalZoneService } from './services/global-zone/global-zone.service';import { DesktopLayoutService, ILayoutService } from './services/layout/layout.service';@@ -107,6 +109,7 @@ export class UniverUIPlugin extends Plugin {[IContextMenuService, { useClass: ContextMenuService }],[IClipboardInterfaceService, { useClass: BrowserClipboardService, lazy: true }],[INotificationService, { useClass: DesktopNotificationService, lazy: true }],+ [IGalleryService, { useClass: DesktopGalleryService, lazy: true }],[IDialogService, { useClass: DesktopDialogService, lazy: true }],[IConfirmService, { useClass: DesktopConfirmService, lazy: true }],[ISidebarService, { useClass: DesktopSidebarService, lazy: true }],diff --git a/packages/drawing-ui/src/views/image-viewer/component-name.ts b/packages/ui/src/views/components/gallery-part/GalleryPart.tsxsimilarity index 58%rename from packages/drawing-ui/src/views/image-viewer/component-name.tsrename to packages/ui/src/views/components/gallery-part/GalleryPart.tsxindex 155bd20446..4e75b0e051 100644--- a/packages/drawing-ui/src/views/image-viewer/component-name.ts+++ b/packages/ui/src/views/components/gallery-part/GalleryPart.tsx@@ -14,4 +14,17 @@* limitations under the License.*/-export const COMPONENT_IMAGE_VIEWER = 'COMPONENT_IMAGE_VIEWER';+import { Gallery } from '@univerjs/design';+import { IGalleryService } from '../../../services/gallery/gallery.service';+import { useDependency, useObservable } from '../../../utils/di';++export function GalleryPart() {+ const galleryService = useDependency(IGalleryService);++ const gallery = useObservable(galleryService.gallery$);+ if (!gallery) {+ return null;+ }++ return <Gallery {...gallery} />;+}