+
-
@@ -109,9 +88,9 @@ const handleDelDom = () => {
-
+
|
@@ -140,4 +119,4 @@ const handleDelDom = () => {
-
\ No newline at end of file
+
diff --git a/src/types/widgetsConfigInterface.ts b/src/types/widgetsConfigInterface.ts
index 2bf724c..4b1a329 100644
--- a/src/types/widgetsConfigInterface.ts
+++ b/src/types/widgetsConfigInterface.ts
@@ -37,7 +37,7 @@ export interface IWidget {
// 属性
options: IOption
// 子结构
- subDomList?: [IWidget] | [[IWidget]]
+ subDomList?: IWidget[] | IWidget[][]
// 单元格是否被合并
merged?: boolean
// X轴坐标
@@ -49,8 +49,12 @@ export interface IWidget {
export interface IWidgetJson {
// 页面结构
subDomList: IWidget[]
- // 当前选中元素ID
- activeId: string
- // 当前选中元素名称
- activeDomName: string
+ // 选中元素配置
+ activeConfig: {
+ // 当前选中元素Id
+ activeId: string
+ // 当前选中元素名称
+ activeDisplayName: string
+ }
+
}
diff --git a/src/utils/index.ts b/src/utils/index.ts
index 15fd3ca..bbd3b47 100644
--- a/src/utils/index.ts
+++ b/src/utils/index.ts
@@ -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
+}
\ No newline at end of file
diff --git a/src/utils/table.ts b/src/utils/table.ts
new file mode 100644
index 0000000..00374b7
--- /dev/null
+++ b/src/utils/table.ts
@@ -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);
+ }
+ }
+ }
+}
\ No newline at end of file
diff --git a/src/utils/widgetsConfig.ts b/src/utils/widgetsConfig.ts
index ea9f704..7f1dc20 100644
--- a/src/utils/widgetsConfig.ts
+++ b/src/utils/widgetsConfig.ts
@@ -6,14 +6,25 @@ export const containers: IWidget[] = [
type: 'table',
displayName: '表格',
icon: 'Monitor',
- options: { name: '' }
+ options: {
+ name: '',
+ hidden: false,
+ customClass: ""
+ }
},
{
category: 'container',
type: 'table-cell',
displayName: '子单元格',
icon: 'Monitor',
- options: { name: '' }
+ merged: false,
+ options: {
+ name: '',
+ hidden: false,
+ colspan: 1,
+ rowspan: 1,
+ customClass: ""
+ }
}
]
diff --git a/src/views/Home/HomeView.vue b/src/views/Home/HomeView.vue
index 8013afa..31c2f70 100644
--- a/src/views/Home/HomeView.vue
+++ b/src/views/Home/HomeView.vue
@@ -1,13 +1,17 @@
diff --git a/src/views/Home/subCom/LeftSide.vue b/src/views/Home/subCom/LeftSide.vue
index 757d8a1..de2488b 100644
--- a/src/views/Home/subCom/LeftSide.vue
+++ b/src/views/Home/subCom/LeftSide.vue
@@ -1,7 +1,8 @@
@@ -35,7 +45,7 @@ const widgetJson = inject('widgetJson')
-
+
{{ item['displayName'] }}