import {iTableNode} from "../utils/table.utils"; import {tPlc} from "../../plc/utils/plc.utils"; import {globalComputed, reactive, toRaw} from "@peryl/react-compose"; import {PlainObject} from "@peryl/utils/event"; import {LoadingText} from "../../../../utils/LoadingText"; import {AsyncFormatter} from "../../../AsyncFormatter"; import {Formatter} from "../../../Formatter"; import {renderBodyCell} from "../../plc/utils/plc.render"; import {iEffects} from "@peryl/utils/createEffects"; import {delay} from "@peryl/utils/delay"; /** * 计算cell渲染所需要的内容 * @author 韦胜健 * @date 2022/12/5 20:37 */ export function useCellValue( { optionGetter, formEdit, effects, }: { optionGetter: () => { node: iTableNode, plc: tPlc }, formEdit: boolean, effects: iEffects, } ) { /** * 当前渲染的行数据 * @author 韦胜健 * @date 2022.12.3 10:50 */ const row = globalComputed(() => { const { node } = optionGetter(); return node.state.edit ? node.state.editRow : node.data; }); effects.push(() => row.effect.stop()); /** * 当前单元格的目标数据 * @author 韦胜健 * @date 2022.12.3 10:50 */ const sourceValue = globalComputed(() => { const { plc } = optionGetter(); return !!plc.props.field ? row.value[plc.props.field] : null; }); effects.push(() => sourceValue.effect.stop()); /** * 异步格式化的状态 * @author 韦胜健 * @date 2022.12.3 10:51 */ const asyncState = reactive({ asyncValue: null as null | { /*格式化时的行数据*/ row: PlainObject, /*格式化的原始值*/ sourceValue: any, /*格式化得到的目标值*/ targetValue: any, /*是否处于加载状态*/ loading: boolean, } }); effects.push(() => delay().then(() => {asyncState.asyncValue = null;})); const targetValue = globalComputed(() => { const { plc, node } = optionGetter(); const { formatter, asyncFormatter, formatterOptions } = plc.props; const cellRenderScope = { plc: plc, node: node, row: row.value }; if (!!asyncFormatter) { /*异步过滤器*/ if (!!asyncState.asyncValue && toRaw(asyncState.asyncValue.sourceValue) === toRaw(sourceValue.value) && toRaw(asyncState.asyncValue.row) === toRaw(row.value)) { /*已经加载过值*/ // console.log(asyncState.asyncValue.loading, LoadingText.value); return asyncState.asyncValue.loading ? LoadingText.value : asyncState.asyncValue.targetValue; } else { const asyncFormatterFunction = typeof asyncFormatter === "function" ? asyncFormatter : AsyncFormatter[asyncFormatter]; if (!asyncFormatterFunction) { console.error(`无法识别格式化函数名称:${asyncFormatter}`); } else { /*没有加载过值,重新加载这个值*/ asyncState.asyncValue = { sourceValue: sourceValue.value, row: row.value, targetValue: null, loading: true, }; (async () => { try { const sv = sourceValue.value; const sr = row.value; const value = await asyncFormatterFunction(sourceValue.value, { ...cellRenderScope, ...formatterOptions }); if (sv === sourceValue.value && sr === row.value) { asyncState.asyncValue = { sourceValue: sourceValue.value, row: row.value, targetValue: value, loading: false, }; } } catch (e) { /*异步格式化失败*/ console.error(e); if (!!asyncState.asyncValue) { asyncState.asyncValue.loading = false; asyncState.asyncValue.targetValue = 'error'; } } })(); return LoadingText.value; } } } else if (formatter) { /*同步过滤器*/ const formatterFunction = typeof formatter === "function" ? formatter : Formatter[formatter]; if (!formatterFunction) { console.error(`无法识别格式化函数名称:${formatter}`); } else { try { return formatterFunction(sourceValue.value, { ...cellRenderScope, ...formatterOptions }); } catch (e) { console.error(e); return 'error'; } } } else { /*没有过滤器*/ return sourceValue.value; } }); effects.push(() => targetValue.effect.stop()); const bodyCell = globalComputed(() => { const { node, plc } = optionGetter(); const bc = renderBodyCell({ node, plc, formEdit, targetValue: targetValue.value }); return () => bc; }); effects.push(() => bodyCell.effect.stop()); return { bodyCell, targetValue }; }