parent
ad14e6d7ee
commit
70f4d52912
@ -1,76 +1,93 @@
|
|||||||
/* eslint-disable @typescript-eslint/no-explicit-any */
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
||||||
// import melonjson from "./melon.json";
|
import { containers, basicFields } from "@/utils/widgetsConfig";
|
||||||
|
import type { IWidget } from '@/types/widgetsConfigInterface'
|
||||||
/**
|
/**
|
||||||
* 初始化和重置文书结构
|
* 初始化和重置文书结构
|
||||||
* @param widgetJson
|
* @param widgetJson
|
||||||
*/
|
*/
|
||||||
export const initJson = (widgetJson: any) => {
|
export const initJson = (widgetJson: any) => {
|
||||||
widgetJson['subDomList'] = []
|
widgetJson['subDomList'] = []
|
||||||
widgetJson['activeId'] = ""
|
widgetJson['activeConfig']['activeId'] = ""
|
||||||
widgetJson['activeDomName'] = ""
|
widgetJson['activeConfig']['activeDisplayName'] = ""
|
||||||
// widgetJson['formConfig'] = {}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据表单元素 格式化对应的属性
|
||||||
|
* @param addItemDataOrigin
|
||||||
|
*/
|
||||||
|
export const formatAddItem = (addItemTypeName: string) => {
|
||||||
|
let addItemData = null;
|
||||||
|
let activeId = ""
|
||||||
|
let activeDisplayName = ""
|
||||||
|
const allWidgetList = [...containers, ...basicFields];
|
||||||
|
const targetItem = allWidgetList.filter(item => {
|
||||||
|
return item['type'] == addItemTypeName
|
||||||
|
})
|
||||||
|
if (targetItem['length']) {
|
||||||
|
addItemData = structuredClone(targetItem[0]);
|
||||||
|
addItemData['key'] = `${Date.now().toString().slice(-6)}${Math.floor(Math.random() * 900) + 100}`;
|
||||||
|
addItemData['id'] = `${addItemData['type']}-${addItemData['key']}`
|
||||||
|
addItemData['subDomList'] = [];
|
||||||
|
addItemData['options']['name'] = addItemData['id']
|
||||||
|
activeId = addItemData['id']
|
||||||
|
activeDisplayName = addItemData['displayName']
|
||||||
|
}
|
||||||
|
return {
|
||||||
|
addItemData,
|
||||||
|
activeId,
|
||||||
|
activeDisplayName
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 添加新的子元素
|
* 添加新的子元素
|
||||||
* @param addItemDataOrigin 新元素
|
* @param widgetJson 文书结构JSON
|
||||||
|
* @param addItemTypeName 新元素类型
|
||||||
* @param fatherId 父容器id
|
* @param fatherId 父容器id
|
||||||
* @param widgetJson w文书结构JSON
|
|
||||||
*/
|
*/
|
||||||
export const addItem = (addItemDataOrigin: any, fatherId = "", widgetJson: any) => {
|
export const addItem = (widgetJson: any, addItemTypeName: any) => {
|
||||||
const { addItemData, activeId, activeDomName } = formatAddItem(addItemDataOrigin)
|
const { addItemData, activeId, activeDisplayName } = formatAddItem(addItemTypeName)
|
||||||
if (fatherId) { } else {
|
if (widgetJson['activeConfig']['activeId']) { } else {
|
||||||
widgetJson['subDomList'] = [addItemData]
|
widgetJson['subDomList'].push(addItemData)
|
||||||
}
|
}
|
||||||
widgetJson['activeId'] = activeId
|
widgetJson['activeConfig']['activeId'] = activeId
|
||||||
widgetJson['activeDomName'] = activeDomName
|
widgetJson['activeDisplayName'] = activeDisplayName
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 根据表单元素 格式化对应的属性
|
* 根据id寻找结构
|
||||||
* @param addItemDataOrigin
|
* @param subDomList widgetJson['subDomList']
|
||||||
|
* @param returnParent 是否返回父容器节点
|
||||||
*/
|
*/
|
||||||
const formatAddItem = (addItemDataOrigin: any) => {
|
export const handleFindDomById = (subDomList: any, activeKey: string, returnParent = false) => {
|
||||||
const addItemData = JSON.parse(JSON.stringify(addItemDataOrigin));
|
let found: IWidget | null = null
|
||||||
let activeId = '';
|
|
||||||
let activeDomName = '';
|
function dfs(nodes: IWidget[], parent: IWidget | null): boolean {
|
||||||
addItemData['key'] = Date.now().toString().slice(-6);
|
for (const node of nodes) {
|
||||||
addItemData['id'] = `${addItemData['type']}-${addItemData['key']}`;
|
if (node['id'] === activeKey) {
|
||||||
activeId = addItemData['id']
|
found = returnParent ? parent?.category === 'container' ? parent : null : node
|
||||||
activeDomName = addItemData['displayName']
|
return true
|
||||||
addItemData['options'] = {
|
|
||||||
name: `table-${addItemData['key']}`,
|
|
||||||
hidden: false,
|
|
||||||
customClass: ''
|
|
||||||
};
|
|
||||||
// 针对表格 需要初始化子单元格
|
|
||||||
if (addItemData['type'] == 'table') {
|
|
||||||
const key = addItemData['key'].slice(-1) + addItemData['key'].slice(0, -1);
|
|
||||||
// 子单元格
|
|
||||||
const colItem = {
|
|
||||||
key: key,
|
|
||||||
id: `table-cell-${key}`,
|
|
||||||
category: "container",
|
|
||||||
type: "table-cell",
|
|
||||||
displayName: "子单元格",
|
|
||||||
icon: "Monitor",
|
|
||||||
merged: false,
|
|
||||||
subDomList: [],
|
|
||||||
options: {
|
|
||||||
name: `table-cell-${key}`,
|
|
||||||
hidden: false,
|
|
||||||
colspan: 1,
|
|
||||||
rowspan: 1,
|
|
||||||
customClass: ''
|
|
||||||
}
|
}
|
||||||
};
|
|
||||||
addItemData['subDomList'] = [[colItem]]
|
// 针对 subDomList 为二维数组或一维数组都做兼容处理
|
||||||
// activeId = colItem['id']
|
const children = node.subDomList
|
||||||
// activeDomName = colItem['displayName']
|
if (Array.isArray(children)) {
|
||||||
}
|
if (Array.isArray(children[0])) {
|
||||||
return {
|
// 二维数组情况
|
||||||
addItemData,
|
for (const row of children as IWidget[][]) {
|
||||||
activeId,
|
if (dfs(row, node)) return true
|
||||||
activeDomName
|
}
|
||||||
|
} else {
|
||||||
|
// 一维数组情况
|
||||||
|
if (dfs(children as IWidget[], node)) return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
dfs(subDomList, null)
|
||||||
|
return found
|
||||||
}
|
}
|
@ -0,0 +1,83 @@
|
|||||||
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
||||||
|
import { formatAddItem, handleFindDomById } from "@/utils/index";
|
||||||
|
import type { IWidget, IWidgetJson } from '@/types/widgetsConfigInterface'
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 新增表格 默认会自动生成一个单元格
|
||||||
|
* @param widgetJson
|
||||||
|
* @param addItemTypeName
|
||||||
|
* @param fatherId
|
||||||
|
*/
|
||||||
|
export const addTable = (widgetJson: IWidgetJson, addItemTypeName: any) => {
|
||||||
|
const tableCol = formatAddItem('table-cell');
|
||||||
|
const { addItemData, activeId, activeDisplayName } = formatAddItem(addItemTypeName);
|
||||||
|
if (addItemData && tableCol['addItemData']) {
|
||||||
|
addItemData['subDomList'] = [[tableCol['addItemData']]];
|
||||||
|
}
|
||||||
|
if (widgetJson['activeConfig']['activeId']) {
|
||||||
|
if (widgetJson['activeConfig']['activeId'].startsWith('table-cell')) {
|
||||||
|
// 单元格里面直接加
|
||||||
|
const tableJson = handleFindDomById(widgetJson['subDomList'], widgetJson['activeConfig']['activeId'], false);
|
||||||
|
if (tableJson && addItemData) {
|
||||||
|
(tableJson['subDomList'] as IWidget[]).push(addItemData)
|
||||||
|
}
|
||||||
|
} else if (widgetJson['activeConfig']['activeId'].startsWith('table')) {
|
||||||
|
// 表格后面加
|
||||||
|
if (addItemData) {
|
||||||
|
widgetJson['subDomList'].push(addItemData)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// 寻找上一层单元格加
|
||||||
|
const tableJson = handleFindDomById(widgetJson['subDomList'], widgetJson['activeConfig']['activeId'], true);
|
||||||
|
if (tableJson && addItemData) {
|
||||||
|
(tableJson['subDomList'] as IWidget[]).push(addItemData)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (addItemData) {
|
||||||
|
widgetJson['subDomList'].push(addItemData)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
widgetJson['activeConfig']['activeId'] = activeId
|
||||||
|
widgetJson['activeConfig']['activeDisplayName'] = activeDisplayName
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 新增表格行 以第一列的个数为准
|
||||||
|
* @param widgetJson
|
||||||
|
* @param addItemTypeName
|
||||||
|
* @param fatherId
|
||||||
|
*/
|
||||||
|
export const addTableRow = (widgetJson: IWidgetJson, addItemTypeName = 'table-cell') => {
|
||||||
|
const tableJson = handleFindDomById(widgetJson['subDomList'], widgetJson['activeConfig']['activeId'], false);
|
||||||
|
if (tableJson) {
|
||||||
|
const newRow: IWidget[] = []
|
||||||
|
for (let index = 0; index < tableJson['subDomList'][0]['length']; index++) {
|
||||||
|
const { addItemData } = formatAddItem(addItemTypeName);
|
||||||
|
if (addItemData) {
|
||||||
|
newRow.push(addItemData);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
(tableJson['subDomList'] as IWidget[][]).push(newRow)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 新增表格列 遍历每一行增加
|
||||||
|
* @param widgetJson
|
||||||
|
* @param addItemTypeName
|
||||||
|
* @param fatherId
|
||||||
|
*/
|
||||||
|
export const addTableCol = (widgetJson: IWidgetJson, addItemTypeName = 'table-cell') => {
|
||||||
|
const tableJson = handleFindDomById(widgetJson['subDomList'], widgetJson['activeConfig']['activeId'], false);
|
||||||
|
if (tableJson && tableJson['subDomList']) {
|
||||||
|
for (let index = 0; index < tableJson['subDomList']['length']; index++) {
|
||||||
|
const { addItemData } = formatAddItem(addItemTypeName);
|
||||||
|
if (addItemData) {
|
||||||
|
const row: IWidget[] = tableJson['subDomList'][index];
|
||||||
|
row.push(addItemData);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue