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

Optimise content model table fetching for Table Edit Plugins #2656

Merged
merged 3 commits into from
May 24, 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
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { createElement } from '../../../pluginUtils/CreateElement/createElement';
import { DragAndDropHelper } from '../../../pluginUtils/DragAndDrop/DragAndDropHelper';
import { getCMTableFromTable } from '../utils/getTableFromContentModel';
import type { TableEditFeature } from './TableEditFeature';
import {
isElementOfType,
normalizeRect,
getFirstSelectedTable,
MIN_ALLOWED_TABLE_CELL_WIDTH,
normalizeTable,
} from 'roosterjs-content-model-dom';
Expand Down Expand Up @@ -107,24 +107,8 @@ function onDragStart(context: DragAndDropContext, event: MouseEvent): DragAndDro

const { editor, table } = context;

// Get current selection
const selection = editor.getDOMSelection();

// Select first cell of the table
editor.setDOMSelection({
type: 'table',
firstColumn: 0,
firstRow: 0,
lastColumn: 0,
lastRow: 0,
table: table,
});

// Get the table content model
const cmTable = getFirstSelectedTable(editor.getContentModelCopy('disconnected'))[0];

// Restore selection
editor.setDOMSelection(selection);
// Get Table block in content model
const cmTable = getCMTableFromTable(editor, table);

if (rect && cmTable) {
onStart();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { createElement } from '../../../pluginUtils/CreateElement/createElement';
import { DragAndDropHelper } from '../../../pluginUtils/DragAndDrop/DragAndDropHelper';
import { formatInsertPointWithContentModel } from 'roosterjs-content-model-api';
import { getCMTableFromTable } from '../utils/getTableFromContentModel';
import type { TableEditFeature } from './TableEditFeature';
import type { OnTableEditorCreatedCallback } from '../../OnTableEditorCreatedCallback';
import type { DragAndDropHandler } from '../../../pluginUtils/DragAndDrop/DragAndDropHandler';
Expand Down Expand Up @@ -234,24 +235,11 @@ export function onDragStart(context: TableMoverContext): TableMoverInitValue {
tableRect.style.left = `${trect.left}px`;
div.parentNode?.appendChild(tableRect);

// Get current selection
// Get drag start selection
const initialSelection = editor.getDOMSelection();

// Select first cell of the table
editor.setDOMSelection({
type: 'table',
firstColumn: 0,
firstRow: 0,
lastColumn: 0,
lastRow: 0,
table: table,
});

// Get the table content model
const [cmTable] = getFirstSelectedTable(editor.getContentModelCopy('disconnected'));

// Restore selection
editor.setDOMSelection(initialSelection);
// Get Table block in content model
const cmTable = getCMTableFromTable(editor, table);

return {
cmTable,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
import { createElement } from '../../../pluginUtils/CreateElement/createElement';
import { DragAndDropHelper } from '../../../pluginUtils/DragAndDrop/DragAndDropHelper';
import { getCMTableFromTable } from '../utils/getTableFromContentModel';
import { isNodeOfType, normalizeRect, normalizeTable } from 'roosterjs-content-model-dom';
import type { TableEditFeature } from './TableEditFeature';
import type { OnTableEditorCreatedCallback } from '../../OnTableEditorCreatedCallback';
import {
getFirstSelectedTable,
isNodeOfType,
normalizeRect,
normalizeTable,
} from 'roosterjs-content-model-dom';
import type { ContentModelTable, IEditor, Rect } from 'roosterjs-content-model-types';
import type { DragAndDropHandler } from '../../../pluginUtils/DragAndDrop/DragAndDropHandler';

Expand Down Expand Up @@ -129,24 +125,8 @@ function onDragStart(context: DragAndDropContext, event: MouseEvent) {

const { editor, table } = context;

// Get current selection
const selection = editor.getDOMSelection();

// Select first cell of the table
editor.setDOMSelection({
type: 'table',
firstColumn: 0,
firstRow: 0,
lastColumn: 0,
lastRow: 0,
table: table,
});

// Get the table content model
const cmTable = getFirstSelectedTable(editor.getContentModelCopy('disconnected'))[0];

// Restore selection
editor.setDOMSelection(selection);
// Get Table block in content model
const cmTable = getCMTableFromTable(editor, table);

// Save original widths and heights
const heights: number[] = [];
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { getFirstSelectedTable } from 'roosterjs-content-model-dom';
import type { ContentModelTable, IEditor } from 'roosterjs-content-model-types';

/**
* @internal
* Get ContentModelTable from a table element if it is present in the content model
*/
export function getCMTableFromTable(editor: IEditor, table: HTMLTableElement) {
let cmTable: ContentModelTable | undefined;

editor.formatContentModel(
model => {
[cmTable] = getFirstSelectedTable(model);
return false;
},
{
selectionOverride: {
type: 'table',
firstColumn: 0,
firstRow: 0,
lastColumn: 0,
lastRow: 0,
table: table,
},
}
);

return cmTable;
}
Loading