refactor
This commit is contained in:
106
features/add-field-label/main.js
Normal file
106
features/add-field-label/main.js
Normal file
@@ -0,0 +1,106 @@
|
||||
/**
|
||||
* 添加字段信息功能模块
|
||||
* 负责获取表单配置并添加字段信息标签
|
||||
*/
|
||||
|
||||
import { generateFields } from './fields.js';
|
||||
import { getGuestSpaceId, getAppId, isInDetailPage, isInAdminFormPage } from '../../utils/kintone-utils.js';
|
||||
import { FieldLabelProcessor } from '../../page/detail/field-label-processor.js';
|
||||
import { AdminFieldLabelProcessor } from '../../page/admin/form/admin-field-label-processor.js';
|
||||
import { KintoneRestAPIClient } from '@kintone/rest-api-client';
|
||||
import { PAGE_TYPES } from '../../utils/constants.js';
|
||||
|
||||
/**
|
||||
* 从 Kintone API 获取表单数据
|
||||
* @param {number} appId - 应用 ID
|
||||
* @param {boolean} isPreview - 是否为预览模式
|
||||
* @returns {Promise<Object>} 表单字段和布局数据
|
||||
*/
|
||||
const fetchFormData = async (appId, isPreview) => {
|
||||
try {
|
||||
// 初始化 Kintone REST API 客户端
|
||||
const client = new KintoneRestAPIClient({
|
||||
guestSpaceId: getGuestSpaceId(), // 访客空间 ID,支持在访客空间中工作
|
||||
});
|
||||
|
||||
const [formFieldsResult, layoutResult] = await Promise.all([
|
||||
client.app.getFormFields({
|
||||
app: appId,
|
||||
preview: isPreview,
|
||||
}),
|
||||
client.app.getFormLayout({
|
||||
app: appId,
|
||||
preview: isPreview,
|
||||
}),
|
||||
]);
|
||||
|
||||
if (!formFieldsResult?.properties) {
|
||||
throw new Error('Failed to retrieve form field data');
|
||||
}
|
||||
|
||||
if (!layoutResult?.layout) {
|
||||
throw new Error('Failed to retrieve form layout data');
|
||||
}
|
||||
|
||||
return {
|
||||
formFields: formFieldsResult.properties, // 表单字段属性
|
||||
layout: layoutResult.layout, // 表单布局配置
|
||||
};
|
||||
} catch (error) {
|
||||
console.error('Failed to fetch form data: ', error);
|
||||
throw new Error(`Unable to retrieve form configuration: ${error.message}`);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* 主函数:为 Kintone 表单添加字段信息标签
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
export const addFieldLabel = async () => {
|
||||
try {
|
||||
const appId = getAppId();
|
||||
if (!appId || typeof appId !== 'number') {
|
||||
return;
|
||||
}
|
||||
|
||||
// 创建字段标签处理器实例
|
||||
const labelProcessor = getFieldLabelProcessor(appId);
|
||||
if (!labelProcessor) {
|
||||
console.warn('Unable to create field label processor')
|
||||
return;
|
||||
}
|
||||
|
||||
// 从 API 获取表单配置数据
|
||||
const { formFields, layout } = await fetchFormData(appId, labelProcessor.isPreview());
|
||||
|
||||
// 处理字段并生成标签数据
|
||||
const { fields: fieldsWithLabels, spacers: spacerElements } =
|
||||
generateFields(formFields, layout);
|
||||
|
||||
console.log(`Processed ${fieldsWithLabels.length} fields and ${spacerElements.length} spacer elements`);
|
||||
|
||||
// 字段元素
|
||||
labelProcessor.processFieldLabels(fieldsWithLabels);
|
||||
// 间距元素
|
||||
labelProcessor.processSpacerLabels(spacerElements);
|
||||
|
||||
} catch (error) {
|
||||
console.error('Failed to add field information: ', error);
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
|
||||
const getFieldLabelProcessor = (appId) => {
|
||||
if (isInDetailPage()) {
|
||||
return new FieldLabelProcessor({
|
||||
appId,
|
||||
pageType: PAGE_TYPES.DETAIL // 当前专注于详情页面
|
||||
});
|
||||
}
|
||||
if (isInAdminFormPage()) {
|
||||
return new AdminFieldLabelProcessor({
|
||||
appId,
|
||||
pageType: PAGE_TYPES.ADMIN // admin 表单页面
|
||||
});
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user