import { $, DATA_ELEMENT, DATA_TRANSIENT_ATTRIBUTES, EDITABLE, UI, transformCustomTags, DATA_CONTENTEDITABLE_KEY, } from '@aomao/engine'; import { TableValue, TableMenu, TemplateInterface, TableInterface, } from '../types'; const TABLE_WRAPPER_CLASS_NAME = 'table-wrapper'; const TABLE_OVERFLOW_CLASS_NAME = 'table-overflow'; const TABLE_CLASS_NAME = 'data-table'; const COLS_HEADER_CLASS_NAME = 'table-cols-header'; const COLS_HEADER_ITEM_CLASS_NAME = 'table-cols-header-item'; const COLS_HEADER_TRIGGER_CLASS_NAME = 'cols-trigger'; const COLS_ADDITION_HEADER_CLASS_NAME = 'cols-addition-header'; const ROWS_HEADER_CLASS_NAME = 'table-rows-header'; const ROWS_HEADER_ITEM_CLASS_NAME = 'table-rows-header-item'; const ROWS_HEADER_TRIGGER_CLASS_NAME = 'rows-trigger'; const HEADER_CLASS_NAME = 'table-header'; const MENUBAR_CLASS_NAME = 'table-menubar'; const MENUBAR_ITEM_CLASS_NAME = 'table-menubar-item'; const MENUBAR_ITEM_INPUT_CALSS_NAME = 'table-menubar-item-input'; const VIEWPORT = 'table-viewport'; const VIEWPORT_READER = 'data-table-reader'; const PLACEHOLDER_CLASS_NAME = 'table-placeholder'; const MULTI_ADDITION_CLASS_NAME = 'multi-addition'; const TABLE_HIGHLIGHT = 'table-highlight'; const ROW_DELETE_BUTTON_CLASS_NAME = 'table-row-delete-button'; const COL_DELETE_BUTTON_CLASS_NAME = 'table-col-delete-button'; const ROW_ADD_BUTTON_CLASS_NAME = 'table-row-add-button'; const ROW_ADD_BUTTON_SPLIT_CLASS_NAME = 'table-row-add-split-button'; const COL_ADD_BUTTON_CLASS_NAME = 'table-col-add-button'; const COL_ADD_BUTTON_SPLIT_CLASS_NAME = 'table-col-add-split-button'; const TABLE_TD_CONTENT_CLASS_NAME = 'table-main-content'; const TABLE_TD_BG_CLASS_NAME = 'table-main-bg'; class Template implements TemplateInterface { static readonly TABLE_WRAPPER_CLASS = `.${TABLE_WRAPPER_CLASS_NAME}`; static readonly TABLE_OVERFLOW_CLASS = `.${TABLE_OVERFLOW_CLASS_NAME}`; static readonly TABLE_CLASS = `.${TABLE_CLASS_NAME}`; static readonly COLS_HEADER_CLASS = `.${COLS_HEADER_CLASS_NAME}`; static readonly COLS_HEADER_ITEM_CLASS = `.${COLS_HEADER_ITEM_CLASS_NAME}`; static readonly COLS_HEADER_TRIGGER_CLASS = `.${COLS_HEADER_TRIGGER_CLASS_NAME}`; static readonly COLS_ADDITION_HEADER_CLASS = `.${COLS_ADDITION_HEADER_CLASS_NAME}`; static readonly ROWS_HEADER_CLASS = `.${ROWS_HEADER_CLASS_NAME}`; static readonly ROWS_HEADER_ITEM_CLASS = `.${ROWS_HEADER_ITEM_CLASS_NAME}`; static readonly ROWS_HEADER_TRIGGER_CLASS = `.${ROWS_HEADER_TRIGGER_CLASS_NAME}`; static readonly HEADER_CLASS = `.${HEADER_CLASS_NAME}`; static readonly MENUBAR_CLASS = `.${MENUBAR_CLASS_NAME}`; static readonly MENUBAR_ITEM_CLASS = `.${MENUBAR_ITEM_CLASS_NAME}`; static readonly MENUBAR_ITEM_INPUT_CALSS = `.${MENUBAR_ITEM_INPUT_CALSS_NAME}`; static readonly VIEWPORT = `.${VIEWPORT}`; static readonly VIEWPORT_READER = `.${VIEWPORT_READER}`; static readonly PLACEHOLDER_CLASS = `.${PLACEHOLDER_CLASS_NAME}`; static readonly MULTI_ADDITION_CLASS = `.${MULTI_ADDITION_CLASS_NAME}`; static readonly TABLE_HIGHLIGHT_CLASS = `.${TABLE_HIGHLIGHT}`; static readonly ROW_DELETE_BUTTON_CLASS = `.${ROW_DELETE_BUTTON_CLASS_NAME}`; static readonly COL_DELETE_BUTTON_CLASS = `.${COL_DELETE_BUTTON_CLASS_NAME}`; static readonly ROW_ADD_BUTTON_CLASS = `.${ROW_ADD_BUTTON_CLASS_NAME}`; static readonly COL_ADD_BUTTON_CLASS = `.${COL_ADD_BUTTON_CLASS_NAME}`; static readonly ROW_ADD_BUTTON_SPLIT_CLASS = `.${ROW_ADD_BUTTON_SPLIT_CLASS_NAME}`; static readonly COL_ADD_BUTTON_SPLIT_CLASS = `.${COL_ADD_BUTTON_SPLIT_CLASS_NAME}`; static readonly TABLE_TD_CONTENT_CLASS = `.${TABLE_TD_CONTENT_CLASS_NAME}`; static readonly TABLE_TD_BG_CLASS = `.${TABLE_TD_BG_CLASS_NAME}`; static readonly CellBG = `
`; isReadonly: boolean = false; static EmptyCell(readonly: boolean = false) { return `


${Template.CellBG}`; } getEmptyCell() { return Template.EmptyCell(this.isReadonly); } private table: TableInterface; constructor(table: TableInterface) { this.table = table; } renderRowsHeader(rows: number) { return ( `
` + `
` + `
` + `
`.repeat(rows) + `
` ); } renderColsHeader(cols: number) { return ( `
` + `
` + `
` + `
`.repeat(cols) + `
` ); } /** * 用于Card渲染 * @param {object} value 参数 * @param {number} value.rows 行数 * @param {number} value.cols 列数 * @param {string} value.html html 字符串 * @return {string} 返回 html 字符串 */ htmlEdit( { rows, cols, html, noBorder, overflow }: TableValue, menus: TableMenu, ): string { cols = cols === -Infinity ? 1 : cols; rows = rows === -Infinity ? 1 : rows; cols = cols === Infinity ? 10 : cols; rows = rows === Infinity ? 10 : rows; const tds = `${this.getEmptyCell()}`.repeat( cols, ); const trs = `${tds}`.repeat(rows); const col = ``.repeat(cols); const colgroup = `${col}`; const tableHighlight = `
`; let tableHeader = `
`; const placeholder = `
`; let menuBar = menus.map((menu) => { if (menu.split) { return '
'; } let menuContent = menu.text; switch (menu.action) { case 'insertColLeft': case 'insertColRight': case 'insertRowUp': case 'insertRowDown': menuContent = menuContent?.replace( '$data', ``, ) || ''; break; } return `
${menuContent}
`; }); menuBar = [ `
${menuBar.join( '', )}
`, ]; if (html) { const hasColGroup = html.indexOf(' -1; html = transformCustomTags(html) || html; if (!hasColGroup) { html = html?.replace(/^(]+>)/, function (match) { return match + colgroup; }); } const normalTable = this.table.helper.normalize($(html)); const trs = normalTable.find('tr'); rows = trs.length; html = normalTable.get()!.outerHTML; } const table = html || `${colgroup}${trs}
`; return `
${tableHeader}
${this.renderColsHeader( cols, )}${table}${placeholder}${tableHighlight}
${this.renderRowsHeader( rows, )}${menuBar}
`; } htmlView({ html, noBorder, overflow }: TableValue) { html = transformCustomTags(html ?? ''); const table = $(html); table.find('td').attributes(DATA_ELEMENT, EDITABLE); return `
${table.get()?.outerHTML}
`; } } export default Template;