Skip to content

Commit

Permalink
chore: pretty table
Browse files Browse the repository at this point in the history
  • Loading branch information
siam-ese committed Dec 7, 2024
1 parent 301b0ef commit c62e128
Show file tree
Hide file tree
Showing 27 changed files with 395 additions and 184 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# **univer-clipsheet-core**

**univer-clipsheet-core** is the core implementation of the **Univer Clipsheet** functionality. This package is organized into separate modules for better scalability and maintainability, including `workflow`, `scraper`, `ui`, and `table`. Below, we introduce the key usage of each module and its components.
**univer-clipsheet-core** is the core implementation of the [**Univer Clipsheet**](https://github.com/dream-num/univer-clipsheet) functionality. This package is organized into separate modules for better scalability and maintainability, including `workflow`, `scraper`, `ui`, and `table`. Below, we introduce the key usage of each module and its components.

**English** | [简体中文](./README-zh.md)

Expand Down
2 changes: 1 addition & 1 deletion eslint.config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import antfu from '@antfu/eslint-config';

const baseRules = {
curly: ['error', 'multi-line'],
'curly': ['error', 'multi-line'],
'no-empty-function': ['error'],
'eol-last': ['error', 'always'],
'import/no-self-import': 'error',
Expand Down
3 changes: 2 additions & 1 deletion packages/shared/lib/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export function sendSetStorageMessage(key: string, payload: any) {
});
}

export function requestDataSource<T >(key: string, params: unknown, filter: (message: PushDataSourceMessage<T>) => boolean) {
export function requestDataSource<T, P = Record<string, any>>(key: string, params: P, filter: (message: PushDataSourceMessage<T>) => boolean) {
const getMsg: GetDataSourceMessage<string> = {
type: ClipsheetMessageTypeEnum.GetDataSource,
payload: {
Expand All @@ -99,3 +99,4 @@ export function requestDataSource<T >(key: string, params: unknown, filter: (mes
return promisifyMessage<PushDataSourceMessage<T>>((msg) => msg.type === ClipsheetMessageTypeEnum.PushDataSource && msg.payload.key === key && filter(msg))
.then((msg) => msg.payload.value);
}

26 changes: 16 additions & 10 deletions packages/table/lib/parser/html-table-extraction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* Copyright 2023-present DreamNum Inc.
*
*/
import { ObservableValue } from '@univer-clipsheet-core/shared';
import type { IInitialSheet, ISheet_Row, ISheet_Row_Cell } from './misc';
import { convertRelativeToAbsolute, generateSelector, getElementCompareContent, getElementText, Initial_Sheet_Type_Num, isEmptyCell, replaceControlChars, safeQueryHelper, Sheet_Cell_Type_Enum } from './misc';

Expand Down Expand Up @@ -209,7 +210,9 @@ interface ILazyLoadTableElements {
}

export class LazyLoadTableElements {
private _listeners = new Set<() => void>();
private _onRowsUpdated$ = new ObservableValue<ISheet_Row[]>([]);
private _onChange$ = new ObservableValue<void>(undefined);
// private _listeners = new Set<() => void>();
private _lazyLoadTableElements: ILazyLoadTableElements[] = [];
private _observers: MutationObserver[] = [];
private _existingRows: Map<string, Node> = new Map();
Expand All @@ -218,6 +221,10 @@ export class LazyLoadTableElements {
this._init();

this._scrollListener();

this._onRowsUpdated$.subscribe(() => {
this._onChange$.next();
});
}

get rows() {
Expand All @@ -233,7 +240,6 @@ export class LazyLoadTableElements {
const rows = queryTableScopeRows(table).filter((node) => this._tableRowFilter(node));
if (rows.length) {
this._addRowsToItem(item, rows);
this._notifyChange();
}
}

Expand Down Expand Up @@ -279,6 +285,8 @@ export class LazyLoadTableElements {
element.sheet.density = element.sheet.cellCount / element.sheet.rows.length;

element.isAdded = true;

this._onRowsUpdated$.next(sheet.rows);
}

private _scrollListener() {
Expand All @@ -293,7 +301,6 @@ export class LazyLoadTableElements {
const rows = queryTableScopeRows(element.table).filter((node) => this._tableRowFilter(node));
if (rows.length > 0) {
this._addRowsToItem(element, rows);
this._notifyChange();
}
});
timer = null;
Expand All @@ -306,7 +313,6 @@ export class LazyLoadTableElements {
const rows = Array.from(mutation.addedNodes).filter((node) => this._tableRowFilter(node as Element)) as HTMLTableRowElement[];

this._addRowsToItem(element, rows);
this._notifyChange();

timeoutReviewTable(element);
});
Expand All @@ -321,14 +327,12 @@ export class LazyLoadTableElements {
});
}

private _notifyChange() {
this._listeners.forEach((listener) => listener());
onRowsUpdated(listener: (rows: ISheet_Row[]) => void) {
return this._onRowsUpdated$.subscribe(listener);
}

onChange(listener: () => void) {
this._listeners.add(listener);

return () => this._listeners.delete(listener);
return this._onChange$.subscribe(listener);
}

private _disposeWithMe(observer: MutationObserver) {
Expand All @@ -343,7 +347,9 @@ export class LazyLoadTableElements {
});

this._observers = [];
this._listeners.clear();

this._onChange$.dispose();
this._onRowsUpdated$.dispose();

this._existingRows.clear();
}
Expand Down
129 changes: 88 additions & 41 deletions packages/table/lib/parser/misc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,47 @@ export function isValidUrl(urlStr: string) {
}
}

export function analyzeRowsToSheetRows(analyzeRows: ITableElementAnalyzeRowData[], onCell?: (cell: ISheet_Row_Cell) => void) {
return analyzeRows.map((rowData) => {
const row: ISheet_Row = {
cells: [],
};

Object.keys(rowData).sort().forEach((cellKey) => {
const cellData = rowData[cellKey];

const { text, src, href } = cellData;

let cell: ISheet_Row_Cell;
if (href != null && href.length > 0 && isValidUrl(href)) {
cell = {
type: Sheet_Cell_Type_Enum.URL,
text,
url: href,
};
} else if (src != null && src.length > 0 && isValidUrl(src)) {
cell = {
type: Sheet_Cell_Type_Enum.IMAGE,
text,
url: src,
};
} else {
cell = {
type: Sheet_Cell_Type_Enum.TEXT,
text,
url: '',
};
}

onCell?.(cell);

row.cells.push(cell);
});

return row;
});
}

export function toResultTable(mergeTableElements: ITableElementAnalyzeRowData[][], selectors: string[], title = 'TableApproximation', type = Initial_Sheet_Type_Num.APPROXIMATION_TABLE) {
const resultSheets: IInitialSheet[] = [];

Expand All @@ -117,49 +158,55 @@ export function toResultTable(mergeTableElements: ITableElementAnalyzeRowData[][
sheet.cellCount = cellCount;

let valueCellCount = 0;
tableData.forEach((rowData) => {
const row: ISheet_Row = {
cells: [],
};
Object.keys(rowData).sort().forEach((cellKey) => {
// if (!sheet.columnName.includes(cellKey)) {
// sheet.columnName.push(cellKey);
// }

const cellData = rowData[cellKey];

const { text, src, href } = cellData;

let cell: ISheet_Row_Cell;
if (href != null && href.length > 0 && isValidUrl(href)) {
cell = {
type: Sheet_Cell_Type_Enum.URL,
text,
url: href,
};
} else if (src != null && src.length > 0 && isValidUrl(src)) {
cell = {
type: Sheet_Cell_Type_Enum.IMAGE,
text,
url: src,
};
} else {
cell = {
type: Sheet_Cell_Type_Enum.TEXT,
text,
url: '',
};
}

if (!isEmptyCell(cell)) {
valueCellCount++;
}

row.cells.push(cell);
});

sheet.rows.push(row);
sheet.rows = analyzeRowsToSheetRows(tableData, (cell) => {
if (!isEmptyCell(cell)) {
valueCellCount++;
}
});
// tableData.forEach((rowData) => {
// const row: ISheet_Row = {
// cells: [],
// };
// Object.keys(rowData).sort().forEach((cellKey) => {
// // if (!sheet.columnName.includes(cellKey)) {
// // sheet.columnName.push(cellKey);
// // }

// const cellData = rowData[cellKey];

// const { text, src, href } = cellData;

// let cell: ISheet_Row_Cell;
// if (href != null && href.length > 0 && isValidUrl(href)) {
// cell = {
// type: Sheet_Cell_Type_Enum.URL,
// text,
// url: href,
// };
// } else if (src != null && src.length > 0 && isValidUrl(src)) {
// cell = {
// type: Sheet_Cell_Type_Enum.IMAGE,
// text,
// url: src,
// };
// } else {
// cell = {
// type: Sheet_Cell_Type_Enum.TEXT,
// text,
// url: '',
// };
// }

// if (!isEmptyCell(cell)) {
// valueCellCount++;
// }

// row.cells.push(cell);
// });

// sheet.rows.push(row);
// });

sheet.density = valueCellCount / cellCount;

Expand Down
35 changes: 22 additions & 13 deletions packages/table/lib/parser/table-approximation-extraction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
*
*/

import { ObservableValue } from '@univer-clipsheet-core/shared';
import { ajaxJsonToTable } from './ajax-json-to-table';
import type { IInitialSheet, ITableElementAnalyzeRowData, UnknownJson } from './misc';
import { deleteRepeatColumn, escapeSpecialCharacters, generateSelector, generateUniqueSelector, getCellData, getElementCompareContent, getElementsAtDepth, getElementText, getImageInAttributes, safeQueryHelper, toResultTable } from './misc';
import type { IInitialSheet, ISheet_Row, ITableElementAnalyzeRowData, UnknownJson } from './misc';
import { analyzeRowsToSheetRows, deleteRepeatColumn, escapeSpecialCharacters, generateSelector, generateUniqueSelector, getCellData, getElementCompareContent, getElementsAtDepth, getElementText, getImageInAttributes, safeQueryHelper, toResultTable } from './misc';

const MIN_ELEMENT_SIZE_RATIO_LIMIT = 0.01;

Expand Down Expand Up @@ -298,7 +299,8 @@ interface ILazyLoadElements {
}

export class LazyLoadElements {
private _listeners = new Set<() => void>();
private _onRowsUpdated$ = new ObservableValue<ISheet_Row[]>([]);
private _onChange$ = new ObservableValue<void>(undefined);
private _cloneTables: ILazyLoadElements[] = [];
private _observers: MutationObserver[] = [];
private _existingTextRows: Map<string, Node> = new Map();
Expand All @@ -308,6 +310,10 @@ export class LazyLoadElements {
this._init();

this._scrollListener();

this._onRowsUpdated$.subscribe(() => {
this._onChange$.next();
});
}

get rows() {
Expand Down Expand Up @@ -354,7 +360,9 @@ export class LazyLoadElements {
});
}

this._notifyChange();
setTimeout(() => {
this._onChange$.next();
});
}

private _disposeWithMe(observer: MutationObserver) {
Expand Down Expand Up @@ -409,17 +417,19 @@ export class LazyLoadElements {
return node instanceof Element;
}) as Element[];

cloneTable.rowData.push(...(new TableElementAnalyze({
const tableElementAnalyze = new TableElementAnalyze({
element: tableElement,
children: childElements,
fitClasses: cloneTable.table.fitClasses,
area: cloneTable.table.area,
textContent: cloneTable.table.textContent,
weightedScore: cloneTable.table.weightedScore,
selectorString: cloneTable.table.selectorString,
}, config).tableData));
}, config);

this._notifyChange();
cloneTable.rowData = cloneTable.rowData.concat(tableElementAnalyze.tableData);

this._onRowsUpdated$.next(analyzeRowsToSheetRows(tableElementAnalyze.tableData));
}

private _scrollListener() {
Expand All @@ -434,14 +444,12 @@ export class LazyLoadElements {
});
}

private _notifyChange() {
this._listeners.forEach((listener) => listener());
onRowsUpdated(listener: (rows: ISheet_Row[]) => void) {
return this._onRowsUpdated$.subscribe(listener);
}

onChange(listener: () => void) {
this._listeners.add(listener);

return () => this._listeners.delete(listener);
return this._onChange$.subscribe(listener);
}

dispose() {
Expand All @@ -451,7 +459,8 @@ export class LazyLoadElements {
obs.disconnect();
});
this._observers = [];
this._listeners.clear();
this._onChange$.dispose();
this._onRowsUpdated$.dispose();
this._existingRows.clear();
this._existingTextRows.clear();
}
Expand Down
Loading

0 comments on commit c62e128

Please sign in to comment.