Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): set textManage key to board #WIK-16312 #960

Merged
merged 1 commit into from
Aug 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/empty-cups-guess.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@plait/draw': patch
---

set textManage key to board
32 changes: 18 additions & 14 deletions packages/draw/src/generators/text.generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,22 @@ export interface TextGeneratorOptions<T> {
getMaxWidth?: () => number;
}

export const KEY_TO_TEXT_MANAGE: Map<string, TextManage> = new Map();
export const KEY_TO_TEXT_MANAGE: WeakMap<PlaitBoard, { [key: string]: TextManage }> = 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<T extends PlaitElement = PlaitGeometry> {
Expand Down Expand Up @@ -66,7 +70,7 @@ export class TextGenerator<T extends PlaitElement = PlaitGeometry> {
.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);
Expand All @@ -75,7 +79,7 @@ export class TextGenerator<T extends PlaitElement = PlaitGeometry> {
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);
Expand All @@ -95,21 +99,21 @@ export class TextGenerator<T extends PlaitElement = PlaitGeometry> {
});
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);
Expand Down Expand Up @@ -160,7 +164,7 @@ export class TextGenerator<T extends PlaitElement = PlaitGeometry> {
this.textManages = [];
ELEMENT_TO_TEXT_MANAGES.delete(this.element);
this.texts.forEach(item => {
deleteTextManage(item.key);
deleteTextManage(this.board, item.key);
});
}
}
4 changes: 2 additions & 2 deletions packages/draw/src/plugins/with-table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand All @@ -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;
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/draw/src/table.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export class TableComponent<T extends PlaitTable> extends CommonElementFlavour<T
const table = (this.board as PlaitTableBoard).buildTable(this.element);
table.cells.forEach(item => {
if (PlaitTableElement.isVerticalText(item)) {
const textManage = getTextManage(item.id);
const textManage = getTextManage(this.board, item.id);
if (textManage) {
const engine = getEngine<PlaitTable>(TableSymbols.table);
const rectangle = engine.getTextRectangle!(this.element, { key: item.id, board: this.board });
Expand Down
2 changes: 1 addition & 1 deletion packages/draw/src/utils/geometry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions packages/draw/src/utils/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -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) {
Expand Down
Loading