{title && (
{compile(title)}
- {template && 引用模板: {template?.name || '未命名'}}
+ {template && 引用模板: {templateName || '未命名'}}
)}
diff --git a/packages/client/src/schema-settings/SchemaSettings.tsx b/packages/client/src/schema-settings/SchemaSettings.tsx
index cc5dd51b4..bdb5a5196 100644
--- a/packages/client/src/schema-settings/SchemaSettings.tsx
+++ b/packages/client/src/schema-settings/SchemaSettings.tsx
@@ -67,9 +67,9 @@ interface SchemaSettingsProviderProps {
export const SchemaSettingsProvider: React.FC
= (props) => {
const { children, fieldSchema, ...others } = props;
- const { getTemplateBySchemaId } = useSchemaTemplateManager();
+ const { getTemplateBySchema } = useSchemaTemplateManager();
const { name } = useCollection();
- const template = getTemplateBySchemaId(fieldSchema['x-uid']);
+ const template = getTemplateBySchema(fieldSchema);
return (
{children}
@@ -181,6 +181,147 @@ SchemaSettings.Template = (props) => {
);
};
+const findGridSchema = (fieldSchema) => {
+ return fieldSchema.reduceProperties((buf, s) => {
+ if (s['x-component'] === 'FormV2') {
+ const f = s.reduceProperties((buf, s) => {
+ if (s['x-component'] === 'Grid') {
+ return s;
+ }
+ return buf;
+ }, null);
+ if (f) {
+ return f;
+ }
+ }
+ return buf;
+ }, null);
+};
+
+const findBlockTemplateSchema = (fieldSchema) => {
+ return fieldSchema.reduceProperties((buf, s) => {
+ if (s['x-component'] === 'FormV2') {
+ const f = s.reduceProperties((buf, s) => {
+ if (s['x-component'] === 'BlockTemplate') {
+ return s;
+ }
+ return buf;
+ }, null);
+ if (f) {
+ return f;
+ }
+ }
+ return buf;
+ }, null);
+};
+
+SchemaSettings.FormItemTemplate = (props) => {
+ const { insertAdjacentPosition = 'afterBegin', componentName, collectionName } = props;
+ const { t } = useTranslation();
+ const { dn, setVisible, template, fieldSchema } = useSchemaSettings();
+ const api = useAPIClient();
+ const { saveAsTemplate, copyTemplateSchema } = useSchemaTemplateManager();
+ if (!collectionName) {
+ return null;
+ }
+ if (template) {
+ return (
+ {
+ const schema = await copyTemplateSchema(template);
+ const templateSchema = findBlockTemplateSchema(fieldSchema);
+ const sdn = createDesignable({
+ api,
+ refresh: dn.refresh.bind(dn),
+ current: templateSchema.parent,
+ });
+ sdn.loadAPIClientEvents();
+ sdn.removeWithoutEmit(templateSchema);
+ sdn.insertAdjacent(insertAdjacentPosition, schema, {
+ async onSuccess() {
+ await api.request({
+ url: `/uiSchemas:remove/${templateSchema['x-uid']}`,
+ });
+ },
+ });
+ fieldSchema['x-template-key'] = null;
+ await api.request({
+ url: `uiSchemas:patch`,
+ method: 'post',
+ data: {
+ 'x-uid': fieldSchema['x-uid'],
+ 'x-template-key': null,
+ },
+ });
+ dn.refresh();
+ }}
+ >
+ {t('Convert reference to duplicate')}
+
+ );
+ }
+ return (
+ {
+ setVisible(false);
+ const gridSchema = findGridSchema(fieldSchema);
+ console.log('gridSchema', gridSchema);
+ const values = await FormDialog('Save as template', () => {
+ return (
+
+
+
+ );
+ }).open({});
+ const sdn = createDesignable({
+ api,
+ refresh: dn.refresh.bind(dn),
+ current: gridSchema.parent,
+ });
+ sdn.loadAPIClientEvents();
+ const { key } = await saveAsTemplate({
+ collectionName,
+ componentName,
+ name: values.name,
+ uid: gridSchema['x-uid'],
+ });
+ sdn.removeWithoutEmit(gridSchema);
+ sdn.insertAdjacent(insertAdjacentPosition, {
+ type: 'void',
+ 'x-component': 'BlockTemplate',
+ 'x-component-props': {
+ templateId: key,
+ },
+ });
+ fieldSchema['x-template-key'] = key;
+ await api.request({
+ url: `uiSchemas:patch`,
+ method: 'post',
+ data: {
+ 'x-uid': fieldSchema['x-uid'],
+ 'x-template-key': key,
+ },
+ });
+ }}
+ >
+ {t('Save as template')}
+
+ );
+};
+
SchemaSettings.Item = (props) => {
let { eventKey } = props;
return (
diff --git a/packages/client/src/schema-templates/BlockTemplate.tsx b/packages/client/src/schema-templates/BlockTemplate.tsx
index cbf154932..c98e67355 100644
--- a/packages/client/src/schema-templates/BlockTemplate.tsx
+++ b/packages/client/src/schema-templates/BlockTemplate.tsx
@@ -19,7 +19,7 @@ export const BlockTemplate = observer((props: any) => {
return (
-
+
);
diff --git a/packages/client/src/schema-templates/SchemaTemplateManagerProvider.tsx b/packages/client/src/schema-templates/SchemaTemplateManagerProvider.tsx
index 0cf7772d0..6b4c990af 100644
--- a/packages/client/src/schema-templates/SchemaTemplateManagerProvider.tsx
+++ b/packages/client/src/schema-templates/SchemaTemplateManagerProvider.tsx
@@ -46,10 +46,12 @@ const regenerateUid = (s: ISchema) => {
};
export const useSchemaTemplate = () => {
- const { getTemplateBySchemaId } = useSchemaTemplateManager();
+ const { getTemplateBySchema, templates } = useSchemaTemplateManager();
const fieldSchema = useFieldSchema();
const schemaId = fieldSchema['x-uid'];
- return useMemo(() => getTemplateBySchemaId(schemaId), [schemaId]);
+ const templateKey = fieldSchema['x-template-key'];
+ console.log('templateKey', { schemaId, templateKey })
+ return useMemo(() => getTemplateBySchema(fieldSchema), [schemaId, templateKey]);
};
export const useSchemaTemplateManager = () => {
@@ -98,7 +100,20 @@ export const useSchemaTemplateManager = () => {
await refresh();
return { key };
},
+ getTemplateBySchema(schema) {
+ const templateKey = schema['x-template-key'];
+ if (templateKey) {
+ return templates?.find((template) => template.key === templateKey);
+ }
+ const schemaId = schema['x-uid'];
+ if (schemaId) {
+ return templates?.find((template) => template.uid === schemaId);
+ }
+ },
getTemplateBySchemaId(schemaId) {
+ if (!schemaId) {
+ return null;
+ }
return templates?.find((template) => template.uid === schemaId);
},
getTemplateById(key) {