Schema 优化

This commit is contained in:
2betop 2020-09-09 13:56:33 +08:00
parent 7b6d4943de
commit 4341bebf91
13 changed files with 341 additions and 59 deletions

View File

@ -6,7 +6,7 @@ import fs = require('fs');
import path = require('path');
import tsj = require('ts-json-schema-generator');
import mkdirp = require('mkdirp');
import {DiagnosticError} from 'ts-json-schema-generator';
import {DiagnosticError, UnknownNodeError} from 'ts-json-schema-generator';
/**
*
@ -47,6 +47,18 @@ main().catch(e => {
console.log(diagnostic.messageText);
console.log('\n');
});
return;
} else if (e instanceof UnknownNodeError) {
let node = e.getNode();
const sourceFile = node.getSourceFile();
const position = sourceFile.getLineAndCharacterOfPosition(node.pos);
console.log(
`\x1b[36m${sourceFile.fileName}:${position.line + 1}:${
position.character + 1
}\x1b[0m - \x1b[31m类型不支持转 JSON Schema\x1b[0m\n`
);
return;
}

View File

@ -11,6 +11,10 @@ import {CardsSchema} from './renderers/Cards';
import {FormSchema} from './renderers/Form';
import {CarouselSchema} from './renderers/Carousel';
import {ChartSchema} from './renderers/Chart';
import {CollapseSchema} from './renderers/Collapse';
import {ColorSchema} from './renderers/Color';
import {ContainerSchema} from './renderers/Container';
import {CRUDSchema} from './renderers/CRUD';
// 每加个类型,这补充一下。
export type SchemaType =
@ -29,7 +33,11 @@ export type SchemaType =
| 'card'
| 'cards'
| 'carousel'
| 'chart';
| 'chart'
| 'collapse'
| 'color'
| 'container'
| 'crud';
export type SchemaObject =
| PageSchema
@ -44,9 +52,16 @@ export type SchemaObject =
| CardSchema
| CardsSchema
| CarouselSchema
| ChartSchema;
| ChartSchema
| CollapseSchema
| ColorSchema
| ContainerSchema
| CRUDSchema;
export type SchemaCollection = SchemaObject | Array<SchemaObject> | SchemaTpl;
export type SchemaCollection =
| SchemaObject
| SchemaTpl
| Array<SchemaObject | SchemaTpl>;
/**
* `data.xxx > 5`

View File

@ -12,6 +12,7 @@ import {LocaleProps, localeable} from '../locale';
import Html from './Html';
import {PlainObject} from '../types';
import {render as renderSchema} from '../factory';
import {SchemaObject} from '../Schema';
export interface AlertProps extends ThemeProps, LocaleProps {
container?: any;

View File

@ -51,6 +51,7 @@ import {
LocaleContext,
LocaleProps
} from './locale';
import {SchemaCollection, SchemaObject, SchemaTpl} from './Schema';
export interface TestFunc {
(
@ -139,7 +140,7 @@ export interface RendererConfig extends RendererBasicConfig {
}
export interface RenderSchemaFilter {
(schema: Schema, renderer: RendererConfig, props?: object): SchemaNode;
(schema: Schema, renderer: RendererConfig, props?: object): SchemaObject;
}
export interface RootRenderProps {
@ -593,7 +594,7 @@ class SchemaRenderer extends React.Component<SchemaRendererProps, any> {
const theme = this.props.env.theme;
if (Array.isArray(schema)) {
return renderChildren($path, schema, rest) as JSX.Element;
return renderChildren($path, schema as any, rest) as JSX.Element;
} else if (schema.children) {
return React.isValidElement(schema.children)
? schema.children
@ -971,7 +972,7 @@ let stores: {
[propName: string]: IRendererStore;
} = {};
export function render(
schema: SchemaNode,
schema: Schema,
props: RootRenderProps = {},
options: RenderOptions = {},
pathPrefix: string = ''

View File

@ -1,7 +1,14 @@
import React from 'react';
import PropTypes from 'prop-types';
import {Renderer, RendererProps} from '../factory';
import {SchemaNode, Schema, Action, Api, ApiObject} from '../types';
import {
SchemaNode,
Schema,
Action,
Api,
ApiObject,
PlainObject
} from '../types';
import {CRUDStore, ICRUDStore} from '../store/crud';
import {
createObject,
@ -30,53 +37,211 @@ import findIndex from 'lodash/findIndex';
import Html from '../components/Html';
import {Spinner} from '../components';
import {Icon} from '../components/icons';
import {
BaseSchema,
SchemaApi,
SchemaClassName,
SchemaExpression,
SchemaName,
SchemaObject,
SchemaTokenizeableString
} from '../Schema';
import {ActionSchema} from './Action';
import {CardsSchema} from './Cards';
export interface CRUDProps extends RendererProps {
api?: Api;
filter?: Schema;
store: ICRUDStore;
defaultParams: object;
syncLocation?: boolean;
primaryField?: string;
export interface CRUDCommonSchema extends BaseSchema {
/**
* CRUD
*/
type: 'crud';
/**
*
*/
mode?: 'table' | 'grid' | 'cards' | /* grid 的别名*/ 'list';
toolbarInline?: boolean;
toolbar?: SchemaNode; // 不推荐,但是还是要兼容老用法。
headerToolbar?: SchemaNode;
footerToolbar?: SchemaNode;
bulkActions?: Array<Action>;
itemActions?: Array<Action>;
orderField?: string;
saveOrderApi?: Api;
quickSaveApi?: Api;
quickSaveItemApi?: Api;
/**
* API
*/
api?: SchemaApi;
/**
*
*/
bulkActions?: Array<ActionSchema>;
/**
*
*/
itemActions?: Array<ActionSchema>;
/**
* {\"perPage\": 24}
*/
defaultParams?: PlainObject;
/**
*
*/
draggable?: boolean;
/**
*
*/
draggableOn?: SchemaExpression;
name?: SchemaName;
/**
*
*/
filter?: any; // todo
/**
*
* @deprecated api sendOn
*/
initFetch?: boolean;
perPageAvailable?: Array<number | string>;
messages: {
/**
*
* @deprecated api sendOn
*/
initFetchOn?: SchemaExpression;
/**
* DOM className
*/
innerClassName?: SchemaClassName;
/**
*
*/
interval?: number;
/**
*
*/
orderField?: string;
/**
*
* @default page
*/
pageField?: string;
/**
*
* @default perPage
*/
perPageField?: string;
/**
* API
*/
quickSaveApi?: SchemaApi;
/**
* 使 API
*/
quickSaveItemApi?: SchemaApi;
/**
* api
*/
saveOrderApi?: SchemaApi;
/**
* ,true
* @default true
*/
syncLocation?: boolean;
/**
*
*/
headerToolbar?: Array<SchemaObject>; // todo
/**
*
*/
footerToolbar?: Array<SchemaObject>; // todo
/**
* [10, 20, 50, 100]
*/
perPageAvailable?: Array<number>;
messages?: {
fetchFailed?: string;
fetchSuccess?: string;
saveFailed?: string;
saveSuccess?: string;
};
pickerMode?: boolean; // 选择模式,用做表单中的选择操作
pageField?: string;
perPageField?: string;
/**
*
*/
hideQuickSaveBtn?: boolean;
autoJumpToTopOnPagerChange?: boolean; // 是否自动跳顶部,当切分页的时候。
interval?: number;
/**
*
*/
autoJumpToTopOnPagerChange?: boolean;
/**
*
*/
silentPolling?: boolean;
stopAutoRefreshWhen?: string;
stopAutoRefreshWhen?: SchemaExpression;
stopAutoRefreshWhenModalIsOpen?: boolean;
filterTogglable?: boolean;
filterDefaultVisible?: boolean;
/**
*
*/
syncResponse2Query?: boolean;
/**
*
*/
keepItemSelectionOnPageChange?: boolean;
/**
*
*/
loadDataOnce?: boolean;
loadDataOnceFetchOnFilter?: boolean; // 在开启loadDataOnce时filter时是否去重新请求api
source?: string;
/**
* loadDataOnce时filter时是否去重新请求api
*/
loadDataOnceFetchOnFilter?: boolean;
/**
*
*/
source?: SchemaTokenizeableString;
}
export type CRUDCardsSchema = CRUDCommonSchema & {
mode: 'cards';
} & Omit<CardsSchema, 'type'>;
/**
* CRUD
* https://baidu.gitee.io/amis/docs/components/crud
*/
export type CRUDSchema = CRUDCardsSchema; // todo
export interface CRUDProps extends RendererProps, CRUDCommonSchema {
store: ICRUDStore;
pickerMode?: boolean; // 选择模式,用做表单中的选择操作
}
export default class CRUD extends React.Component<CRUDProps, any> {
static propsList: Array<string> = [
static propsList: Array<keyof CRUDProps> = [
'bulkActions',
'itemActions',
'mode',
@ -1569,7 +1734,12 @@ export default class CRUD extends React.Component<CRUDProps, any> {
}
}
return this.renderToolbar(headerToolbar, 0, childProps, toolbarRenderer);
return this.renderToolbar(
headerToolbar || [],
0,
childProps,
toolbarRenderer
);
}
renderFooterToolbar(

View File

@ -18,6 +18,7 @@ import {
SchemaClassName,
SchemaCollection,
SchemaExpression,
SchemaObject,
SchemaTpl,
SchemaUrlPath
} from '../Schema';

View File

@ -21,7 +21,7 @@ import {
SchemaCollection,
SchemaExpression,
SchemaTpl,
TokenizeableString
SchemaTokenizeableString
} from '../Schema';
import {CardSchema} from './Card';
@ -75,7 +75,7 @@ export interface CardsSchema extends BaseSchema {
*
* @default ${items}
*/
source?: TokenizeableString;
source?: SchemaTokenizeableString;
/**
*

View File

@ -98,7 +98,7 @@ export interface CarouselState {
nextAnimation: string;
}
const defaultSchema = {
const defaultSchema: SchemaObject = {
type: 'tpl',
tpl: `
<% if (data.hasOwnProperty('image')) { %>

View File

@ -1,15 +1,63 @@
import React from 'react';
import {Renderer, RendererProps} from '../factory';
import {Collapse as BasicCollapse} from '../components/Collapse';
import {
BaseSchema,
SchemaClassName,
SchemaCollection,
SchemaTpl
} from '../Schema';
export interface CollapseProps extends RendererProps {
title?: string; // 标题
/**
* Collapse
* https://baidu.gitee.io/amis/docs/components/collapse
*/
export interface CollapseSchema extends BaseSchema {
/**
*
*/
type: 'collapse';
/**
*
*/
body: SchemaCollection;
/**
* Body className
*/
bodyClassName?: SchemaClassName;
/**
*
*/
collapsable?: boolean;
/**
*
*/
collapsed?: boolean;
/**
* CSS
*/
headingClassName?: string;
/**
*
*/
title?: SchemaTpl;
/**
*
*/
size?: 'xs' | 'sm' | 'md' | 'lg' | 'base';
}
export interface CollapseProps extends RendererProps, CollapseSchema {
wrapperComponent?: any;
headingComponent?: any;
collapsed?: boolean;
bodyClassName?: string;
headingClassName?: string;
// 内容口子
children?: JSX.Element | ((props?: any) => JSX.Element);
}

View File

@ -3,14 +3,32 @@
*/
import React from 'react';
import {Renderer, RendererProps} from '../factory';
import {BaseSchema} from '../Schema';
import {resolveVariableAndFilter} from '../utils/tpl-builtin';
export interface ColorProps extends RendererProps {
className: string;
defaultColor: string;
showValue: boolean;
/**
* Color
* https://baidu.gitee.io/amis/docs/components/color
*/
export interface ColorSchema extends BaseSchema {
/**
*
*/
type: 'color';
/**
*
*/
defaultColor?: string;
/**
*
*/
showValue?: boolean;
}
export interface ColorProps extends RendererProps, ColorSchema {}
export class ColorField extends React.Component<ColorProps, object> {
static defaultProps = {
className: '',

View File

@ -1,11 +1,31 @@
import React from 'react';
import {Renderer, RendererProps} from '../factory';
import {BaseSchema, SchemaClassName, SchemaCollection} from '../Schema';
import {SchemaNode} from '../types';
export interface ContainerProps extends RendererProps {
body?: SchemaNode;
/**
* Container
* https://baidu.gitee.io/amis/docs/components/container
*/
export interface ContainerSchema extends BaseSchema {
/**
* container
*/
type: 'container';
/**
*
*/
body: SchemaCollection;
/**
* body
*/
bodyClassName?: SchemaClassName;
}
export interface ContainerProps extends RendererProps, ContainerSchema {
children?: (props: any) => React.ReactNode;
className?: string;
}
export default class Container<T> extends React.Component<

View File

@ -923,18 +923,14 @@ export default class Form extends React.Component<FormProps, object> {
region: string = '',
otherProps: Partial<FormProps> = {}
): React.ReactNode {
return this.renderControls(
schema.controls as SchemaNode,
region,
otherProps
);
return this.renderControls(schema.controls!, region, otherProps);
// return schema.tabs ? this.renderTabs(schema.tabs, schema, region)
// : schema.fieldSet ? this.renderFiledSet(schema.fieldSet, schema, region) : this.renderControls(schema.controls as SchemaNode, schema, region);
}
renderControls(
controls: SchemaNode,
controls: Array<any>,
region: string,
otherProps: Partial<FormProps> = {}
): React.ReactNode {

View File

@ -66,7 +66,7 @@ export interface Button {
className?: string;
}
export type SchemaNode = number | string | Schema | SchemaArray;
export type SchemaNode = Schema | string | Array<Schema | string>;
export interface SchemaArray extends Array<SchemaNode> {}
export interface Definitions {
[propName: string]: SchemaNode;