From 56c04df75aa8b9d8140edd274fafa5b1dd99d32f Mon Sep 17 00:00:00 2001 From: huanhuanwa <44698191+huanhuanwa@users.noreply.github.com> Date: Wed, 14 Aug 2024 10:35:36 +0800 Subject: [PATCH] fix(core): set textManage key to board #WIK-16312 (#960) --- .changeset/empty-cups-guess.md | 5 +++ .../draw/src/generators/text.generator.ts | 32 +++++++++++-------- packages/draw/src/plugins/with-table.ts | 4 +-- packages/draw/src/table.component.ts | 2 +- packages/draw/src/utils/geometry.ts | 2 +- packages/draw/src/utils/table.ts | 10 +++--- 6 files changed, 32 insertions(+), 23 deletions(-) create mode 100644 .changeset/empty-cups-guess.md diff --git a/.changeset/empty-cups-guess.md b/.changeset/empty-cups-guess.md new file mode 100644 index 000000000..f63978f3b --- /dev/null +++ b/.changeset/empty-cups-guess.md @@ -0,0 +1,5 @@ +--- +'@plait/draw': patch +--- + +set textManage key to board diff --git a/packages/draw/src/generators/text.generator.ts b/packages/draw/src/generators/text.generator.ts index f247ce3cf..c0b76aa61 100644 --- a/packages/draw/src/generators/text.generator.ts +++ b/packages/draw/src/generators/text.generator.ts @@ -25,18 +25,22 @@ export interface TextGeneratorOptions { getMaxWidth?: () => number; } -export const KEY_TO_TEXT_MANAGE: Map = new Map(); +export const KEY_TO_TEXT_MANAGE: WeakMap = new WeakMap(); -export const setTextManage = (key: string, textManage: TextManage) => { - return KEY_TO_TEXT_MANAGE.set(key, textManage); +export const setTextManage = (board: PlaitBoard, key: string, textManage: TextManage) => { + const textManages = KEY_TO_TEXT_MANAGE.get(board)!; + return KEY_TO_TEXT_MANAGE.set(board, { ...textManages, [key]: textManage }); }; -export const getTextManage = (key: string) => { - return KEY_TO_TEXT_MANAGE.get(key); +export const getTextManage = (board: PlaitBoard, key: string): TextManage => { + const textManages = KEY_TO_TEXT_MANAGE.get(board)!; + return textManages[key]; }; -export const deleteTextManage = (key: string) => { - return KEY_TO_TEXT_MANAGE.delete(key); +export const deleteTextManage = (board: PlaitBoard, key: string) => { + const textManages = KEY_TO_TEXT_MANAGE.get(board)!; + delete textManages[key]; + KEY_TO_TEXT_MANAGE.set(board, textManages); }; export class TextGenerator { @@ -66,7 +70,7 @@ export class TextGenerator { .textPlugins; this.textManages = this.texts.map(text => { const textManage = this.createTextManage(text, textPlugins); - setTextManage(getTextKey(this.element, text), textManage); + setTextManage(this.board, getTextKey(this.element, text), textManage); return textManage; }); ELEMENT_TO_TEXT_MANAGES.set(this.element, this.textManages); @@ -75,7 +79,7 @@ export class TextGenerator { draw(elementG: SVGElement) { const centerPoint = RectangleClient.getCenterPoint(this.board.getRectangle(this.element)!); this.texts.forEach(drawShapeText => { - const textManage = getTextManage(getTextKey(this.element, drawShapeText)); + const textManage = getTextManage(this.board, getTextKey(this.element, drawShapeText)); if (drawShapeText.text && textManage) { textManage.draw(drawShapeText.text); elementG.append(textManage.g); @@ -95,21 +99,21 @@ export class TextGenerator { }); if (removedTexts.length) { removedTexts.forEach(item => { - const textManage = getTextManage(item.key); + const textManage = getTextManage(this.board, item.key); const index = this.textManages.findIndex(value => value === textManage); if (index > -1 && item.text && item.textHeight) { this.textManages.splice(index, 1); } textManage?.destroy(); - deleteTextManage(item.key); + deleteTextManage(this.board, item.key); }); } currentDrawShapeTexts.forEach(drawShapeText => { if (drawShapeText.text) { - let textManage = getTextManage(getTextKey(this.element, drawShapeText)); + let textManage = getTextManage(this.board, getTextKey(this.element, drawShapeText)); if (!textManage) { textManage = this.createTextManage(drawShapeText, textPlugins); - setTextManage(drawShapeText.key, textManage); + setTextManage(this.board, drawShapeText.key, textManage); textManage.draw(drawShapeText.text); elementG.append(textManage.g); this.textManages.push(textManage); @@ -160,7 +164,7 @@ export class TextGenerator { this.textManages = []; ELEMENT_TO_TEXT_MANAGES.delete(this.element); this.texts.forEach(item => { - deleteTextManage(item.key); + deleteTextManage(this.board, item.key); }); } } diff --git a/packages/draw/src/plugins/with-table.ts b/packages/draw/src/plugins/with-table.ts index eaffcc553..26160e02c 100644 --- a/packages/draw/src/plugins/with-table.ts +++ b/packages/draw/src/plugins/with-table.ts @@ -84,7 +84,7 @@ export const withTable = (board: PlaitBoard) => { cell = cells.find(item => item.text && item.textHeight); } if (cell) { - editCell(cell); + editCell(board, cell); return; } } @@ -100,7 +100,7 @@ export const withTable = (board: PlaitBoard) => { if (hitElement && PlaitDrawElement.isElementByTable(hitElement)) { const hitCell = getHitCell(tableBoard, hitElement, point); if (hitCell && hitCell.text && hitCell.textHeight) { - editCell(hitCell); + editCell(board, hitCell); return; } } diff --git a/packages/draw/src/table.component.ts b/packages/draw/src/table.component.ts index 65a6ed0a7..9d2960d10 100644 --- a/packages/draw/src/table.component.ts +++ b/packages/draw/src/table.component.ts @@ -88,7 +88,7 @@ export class TableComponent extends CommonElementFlavour { if (PlaitTableElement.isVerticalText(item)) { - const textManage = getTextManage(item.id); + const textManage = getTextManage(this.board, item.id); if (textManage) { const engine = getEngine(TableSymbols.table); const rectangle = engine.getTextRectangle!(this.element, { key: item.id, board: this.board }); diff --git a/packages/draw/src/utils/geometry.ts b/packages/draw/src/utils/geometry.ts index 7de98360e..6a53441cc 100644 --- a/packages/draw/src/utils/geometry.ts +++ b/packages/draw/src/utils/geometry.ts @@ -360,7 +360,7 @@ export const createDefaultGeometry = (board: PlaitBoard, points: [Point, Point], }; export const editText = (board: PlaitBoard, element: PlaitGeometry, text?: PlaitDrawShapeText) => { - const textManage = text ? getTextManage(`${element.id}-${text.key}`)! : getFirstTextManage(element); + const textManage = text ? getTextManage(board, `${element.id}-${text.key}`)! : getFirstTextManage(element); if (textManage) { textManage.edit(() => { // delay to avoid blinking diff --git a/packages/draw/src/utils/table.ts b/packages/draw/src/utils/table.ts index bfdb44d0f..f7bd4013b 100644 --- a/packages/draw/src/utils/table.ts +++ b/packages/draw/src/utils/table.ts @@ -107,13 +107,13 @@ export function getHitCell(board: PlaitTableBoard, element: PlaitBaseTable, poin return null; } -export function editCell(cell: PlaitTableCell) { - const textManage = getTextManageByCell(cell); +export function editCell(board: PlaitBoard, cell: PlaitTableCell) { + const textManage = getTextManageByCell(board, cell); textManage && textManage.edit(); } -export function getTextManageByCell(cell: PlaitTableCell) { - return getTextManage(cell.id); +export function getTextManageByCell(board: PlaitBoard, cell: PlaitTableCell) { + return getTextManage(board, cell.id); } export const updateColumns = (table: PlaitBaseTable, columnId: string, width: number, offset: number) => { @@ -186,7 +186,7 @@ export const getSelectedTableCellsEditor = (board: PlaitBoard): BaseEditor[] | u const elements = getSelectedTableElements(board); const selectedCells = getSelectedCells(elements[0]); const selectedCellsEditor = selectedCells?.map(cell => { - const textManage = getTextManageByCell(cell); + const textManage = getTextManageByCell(board, cell); return textManage?.editor; }); if (selectedCellsEditor?.length) {