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
fix(ui): wrong popup direction for RectPopup (#6071)
Kennan Chan5d6c3b5ff61a
diff --git a/examples/src/sheets/custom/custom-range-popup.tsx b/examples/src/sheets/custom/custom-range-popup.tsxnew file mode 100644index 0000000000..f1693190b7--- /dev/null+++ b/examples/src/sheets/custom/custom-range-popup.tsx@@ -0,0 +1,97 @@+/**+ * 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 { Univer } from '@univerjs/core';+import type { FUniver } from '@univerjs/core/facade';+import type { RectPopupDirection } from '@univerjs/ui';+import { LifecycleStages } from '@univerjs/core';++const DIRECTIONS: RectPopupDirection[] = [+ 'left',+ 'left-center',+ 'left-bottom',+ 'left-top',+ 'right',+ 'right-center',+ 'right-bottom',+ 'right-top',+ 'top',+ 'top-center',+ 'top-left',+ 'top-right',+ 'bottom',+ 'bottom-center',+ 'bottom-left',+ 'bottom-right',+ 'vertical',+ 'vertical-left',+ 'vertical-right',+ 'vertical-center',+ 'horizontal',+ 'horizontal-top',+ 'horizontal-bottom',+ 'horizontal-center',+];++const OFFSETS: Partial<Record<RectPopupDirection, [number, number]>> = {+ 'vertical-left': [80, 0],+ left: [55, 0],+ right: [350, 0],+ top: [0, 20],+ bottom: [0, 20],+ vertical: [0, 40],+ horizontal: [170, 0],+ 'vertical-right': [250, 0],+ 'vertical-center': [220, 0],+ 'horizontal-top': [65, 0],+ 'horizontal-center': [90, 0],+ 'horizontal-bottom': [90, 0],+};++const RANGE = 'C40:F45';++function createPopupComponent(direction: RectPopupDirection) {+ return () => {+ return (<div style={{ color: 'red', border: '1px solid red' }}>{direction}</div>);+ };+}++function generateComponentKey(direction: RectPopupDirection) {+ return `PopupComponent${direction.split('-').map((segment) => `${segment.charAt(0).toUpperCase()}${segment.slice(1)}`).join('')}`;+}++export function customRangePopups(univer: Univer, univerAPI: FUniver) {+ const workbook = univerAPI.getWorkbook('workbook-01');+ const worksheet = workbook?.getSheetBySheetId('sheet-0011');+ const range = worksheet?.getRange(RANGE);+ if (range) {+ DIRECTIONS.forEach((direction) => {+ univerAPI.registerComponent(generateComponentKey(direction), createPopupComponent(direction));+ });+ univerAPI.addEvent(univerAPI.Event.LifeCycleChanged, (params) => {+ if (params.stage === LifecycleStages.Rendered) {+ DIRECTIONS.forEach((direction) => {+ range.attachRangePopup({+ componentKey: generateComponentKey(direction),+ direction,+ offset: OFFSETS[direction] ?? [0, 0],+ showOnSelectionMoving: true,+ });+ });+ }+ });+ }+}diff --git a/examples/src/sheets/main.ts b/examples/src/sheets/main.tsindex ff299715da..aef99405d4 100644--- a/examples/src/sheets/main.ts+++ b/examples/src/sheets/main.ts@@ -50,6 +50,7 @@ import { UniverSheetsZenEditorPlugin } from '@univerjs/sheets-zen-editor';import { UniverUIPlugin } from '@univerjs/ui';import { UniverVue3AdapterPlugin } from '@univerjs/ui-adapter-vue3';import { UniverWebComponentAdapterPlugin } from '@univerjs/ui-adapter-web-component';+import { customRangePopups } from './custom/custom-range-popup';import { customRegisterEvent } from './custom/custom-register-event';import { UniverSheetsCustomShortcutPlugin } from './custom/custom-shortcut';import ImportCSVButtonPlugin from './custom/import-csv-button';@@ -190,6 +191,7 @@ function createNewInstance() {window.univerAPI = FUniver.newAPI(univer);customRegisterEvent(univer, window.univerAPI!);+ customRangePopups(univer, window.univerAPI!);}createNewInstance();diff --git a/packages/ui/src/views/components/popup/RectPopup.tsx b/packages/ui/src/views/components/popup/RectPopup.tsxindex 9516c27395..e510f8d5c7 100644--- a/packages/ui/src/views/components/popup/RectPopup.tsx+++ b/packages/ui/src/views/components/popup/RectPopup.tsx@@ -129,9 +129,9 @@ function calcPopupPosition(layout: IPopupLayoutInfo): { top: number; left: numbehorizontalStyle = (Math.max(startX + offsetX, minLeft) + width) > containerWidth? { left: Math.max(Math.min(maxLeft, endX - width - offsetX), minLeft) }: { left: Math.max(minLeft, Math.min(startX + offsetX, maxLeft)) };- } else if (direction.includes('left')) {- horizontalStyle = { left: Math.max(endX - width, minLeft) };} else if (direction.includes('right')) {+ horizontalStyle = { left: Math.max(endX - width, minLeft) };+ } else if (direction.includes('left')) {horizontalStyle = { left: Math.min(startX, maxLeft) };} else {// If the popup element exceed the visible area. We should "push" it back.