parent
ad14e6d7ee
commit
70f4d52912
@ -1,76 +1,93 @@
|
||||
/* 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
|
||||
*/
|
||||
export const initJson = (widgetJson: any) => {
|
||||
widgetJson['subDomList'] = []
|
||||
widgetJson['activeId'] = ""
|
||||
widgetJson['activeDomName'] = ""
|
||||
// widgetJson['formConfig'] = {}
|
||||
widgetJson['activeConfig']['activeId'] = ""
|
||||
widgetJson['activeConfig']['activeDisplayName'] = ""
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据表单元素 格式化对应的属性
|
||||
* @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 widgetJson w文书结构JSON
|
||||
*/
|
||||
export const addItem = (addItemDataOrigin: any, fatherId = "", widgetJson: any) => {
|
||||
const { addItemData, activeId, activeDomName } = formatAddItem(addItemDataOrigin)
|
||||
if (fatherId) { } else {
|
||||
widgetJson['subDomList'] = [addItemData]
|
||||
export const addItem = (widgetJson: any, addItemTypeName: any) => {
|
||||
const { addItemData, activeId, activeDisplayName } = formatAddItem(addItemTypeName)
|
||||
if (widgetJson['activeConfig']['activeId']) { } else {
|
||||
widgetJson['subDomList'].push(addItemData)
|
||||
}
|
||||
widgetJson['activeId'] = activeId
|
||||
widgetJson['activeDomName'] = activeDomName
|
||||
widgetJson['activeConfig']['activeId'] = activeId
|
||||
widgetJson['activeDisplayName'] = activeDisplayName
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 根据表单元素 格式化对应的属性
|
||||
* @param addItemDataOrigin
|
||||
* 根据id寻找结构
|
||||
* @param subDomList widgetJson['subDomList']
|
||||
* @param returnParent 是否返回父容器节点
|
||||
*/
|
||||
const formatAddItem = (addItemDataOrigin: any) => {
|
||||
const addItemData = JSON.parse(JSON.stringify(addItemDataOrigin));
|
||||
let activeId = '';
|
||||
let activeDomName = '';
|
||||
addItemData['key'] = Date.now().toString().slice(-6);
|
||||
addItemData['id'] = `${addItemData['type']}-${addItemData['key']}`;
|
||||
activeId = addItemData['id']
|
||||
activeDomName = addItemData['displayName']
|
||||
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: ''
|
||||
export const handleFindDomById = (subDomList: any, activeKey: string, returnParent = false) => {
|
||||
let found: IWidget | null = null
|
||||
|
||||
function dfs(nodes: IWidget[], parent: IWidget | null): boolean {
|
||||
for (const node of nodes) {
|
||||
if (node['id'] === activeKey) {
|
||||
found = returnParent ? parent?.category === 'container' ? parent : null : node
|
||||
return true
|
||||
}
|
||||
};
|
||||
addItemData['subDomList'] = [[colItem]]
|
||||
// activeId = colItem['id']
|
||||
// activeDomName = colItem['displayName']
|
||||
}
|
||||
return {
|
||||
addItemData,
|
||||
activeId,
|
||||
activeDomName
|
||||
|
||||
// 针对 subDomList 为二维数组或一维数组都做兼容处理
|
||||
const children = node.subDomList
|
||||
if (Array.isArray(children)) {
|
||||
if (Array.isArray(children[0])) {
|
||||
// 二维数组情况
|
||||
for (const row of children as IWidget[][]) {
|
||||
if (dfs(row, node)) return true
|
||||
}
|
||||
} 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