vendor: amis 6.13.0 base (fork root)
64
packages/amis-editor-core/src/compat.ts
Normal file
@@ -0,0 +1,64 @@
|
||||
/**
|
||||
* 兼容 1.x 里面的注册渲染器的方法。
|
||||
*/
|
||||
import {EditorManager, registerEditorPlugin} from './manager';
|
||||
import {BasePlugin} from './plugin';
|
||||
|
||||
/**
|
||||
* 旧版本注册渲染器编辑器的方法。
|
||||
*
|
||||
* @deprecated
|
||||
* @param rendererName
|
||||
* @param config
|
||||
*/
|
||||
export function RendererEditor(
|
||||
rendererName: string,
|
||||
config: {
|
||||
name: string;
|
||||
description?: string;
|
||||
type?: string;
|
||||
previewSchema?: any;
|
||||
scaffold?: any;
|
||||
}
|
||||
) {
|
||||
return function (Klass: new (manager: EditorManager) => BasicEditor) {
|
||||
registerEditorPlugin(
|
||||
class ComposedPlugin extends Klass {
|
||||
rendererName: string;
|
||||
name: string;
|
||||
description?: string;
|
||||
scaffold?: any;
|
||||
previewSchema?: any;
|
||||
panelTitle?: string;
|
||||
panelControls?: Array<any>;
|
||||
|
||||
constructor(manager: EditorManager) {
|
||||
super(manager);
|
||||
|
||||
this.rendererName = rendererName;
|
||||
this.name = this.tipName || config.name;
|
||||
this.description = config.description;
|
||||
this.scaffold = config.scaffold || {type: config.type};
|
||||
this.previewSchema = config.previewSchema || this.scaffold;
|
||||
|
||||
if (this.settingsSchema) {
|
||||
this.panelTitle = this.settingsSchema.title;
|
||||
this.panelControls = this.settingsSchema.body;
|
||||
}
|
||||
}
|
||||
}
|
||||
);
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 为了兼容旧版的注册方法
|
||||
* @deprecated
|
||||
*/
|
||||
export class BasicEditor extends BasePlugin {
|
||||
tipName?: string;
|
||||
settingsSchema?: {
|
||||
title: string;
|
||||
body: Array<any>;
|
||||
};
|
||||
}
|
||||
45
packages/amis-editor-core/src/component/AsyncLayer.tsx
Normal file
@@ -0,0 +1,45 @@
|
||||
/**
|
||||
* @file AsyncLayer.tsx
|
||||
* @desc 异步加载层
|
||||
*/
|
||||
import React from 'react';
|
||||
import {Spinner} from 'amis';
|
||||
|
||||
export interface AsyncLayerOptions {
|
||||
fallback?: React.ReactNode;
|
||||
}
|
||||
|
||||
export const makeAsyncLayer = (
|
||||
schemaBuilderFn: () => Promise<any>,
|
||||
options?: AsyncLayerOptions
|
||||
) => {
|
||||
const {fallback} = options || {};
|
||||
const LazyComponent = React.lazy(async () => {
|
||||
const schemaFormRender = await schemaBuilderFn();
|
||||
|
||||
return {
|
||||
default: (...props: any[]) => <>{schemaFormRender(...props)}</>
|
||||
};
|
||||
});
|
||||
|
||||
return (props: any) => (
|
||||
<React.Suspense
|
||||
fallback={
|
||||
fallback && React.isValidElement(fallback) ? (
|
||||
fallback
|
||||
) : (
|
||||
<Spinner
|
||||
show
|
||||
overlay
|
||||
size="sm"
|
||||
tip="配置面板加载中"
|
||||
tipPlacement="bottom"
|
||||
className="flex"
|
||||
/>
|
||||
)
|
||||
}
|
||||
>
|
||||
<LazyComponent {...props} />
|
||||
</React.Suspense>
|
||||
);
|
||||
};
|
||||
273
packages/amis-editor-core/src/component/Breadcrumb.tsx
Normal file
@@ -0,0 +1,273 @@
|
||||
import React from 'react';
|
||||
import {reaction} from 'mobx';
|
||||
import {Icon, resizeSensor} from 'amis';
|
||||
import {EditorStoreType} from '../store/editor';
|
||||
import {EditorManager} from '../manager';
|
||||
import {observer} from 'mobx-react';
|
||||
import {EditorNodeType} from '../store/node';
|
||||
import {autobind} from '../util';
|
||||
|
||||
export interface BreadcrumbProps {
|
||||
store: EditorStoreType;
|
||||
manager: EditorManager;
|
||||
}
|
||||
|
||||
interface BreadcrumbStates {
|
||||
showLeftScrollBtn: boolean;
|
||||
showRightScrollBtn: boolean;
|
||||
}
|
||||
|
||||
@observer
|
||||
export default class Breadcrumb extends React.Component<
|
||||
BreadcrumbProps,
|
||||
BreadcrumbStates
|
||||
> {
|
||||
readonly breadcrumbRef = React.createRef<HTMLDivElement>();
|
||||
readonly bcnContentRef = React.createRef<HTMLDivElement>();
|
||||
|
||||
currentBreadcrumb: HTMLElement; // 记录当前面包屑元素
|
||||
unReaction: () => void;
|
||||
unSensor?: () => void;
|
||||
|
||||
constructor(props: any) {
|
||||
super(props);
|
||||
|
||||
this.state = {
|
||||
showLeftScrollBtn: false,
|
||||
showRightScrollBtn: false
|
||||
};
|
||||
|
||||
this.unReaction = reaction(
|
||||
() => this.props.store.bcn,
|
||||
() => {
|
||||
// 内容变动时,自动触发一次计算
|
||||
this.refreshHandleScroll(true);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
componentDidMount() {
|
||||
const scrollElem = this.getCurBreadcrumb();
|
||||
const breadcrumbContainer = this.getBreadcrumbContainer();
|
||||
|
||||
if (!scrollElem || !breadcrumbContainer) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.unSensor = resizeSensor(breadcrumbContainer, () => {
|
||||
this.refreshHandleScroll();
|
||||
});
|
||||
}
|
||||
|
||||
componentWillUnmount() {
|
||||
this.unReaction();
|
||||
if (this.unSensor) {
|
||||
this.unSensor();
|
||||
delete this.unSensor;
|
||||
}
|
||||
}
|
||||
|
||||
@autobind
|
||||
refreshHandleScroll(resetScroll?: boolean) {
|
||||
setTimeout(() => {
|
||||
this.HandleScroll(resetScroll);
|
||||
}, 0);
|
||||
}
|
||||
|
||||
@autobind
|
||||
getCurBreadcrumb() {
|
||||
return this.bcnContentRef.current;
|
||||
}
|
||||
|
||||
@autobind
|
||||
getBreadcrumbContainer() {
|
||||
return this.breadcrumbRef.current;
|
||||
}
|
||||
|
||||
@autobind
|
||||
getScrollLeft(): string {
|
||||
const curBreadcrumb = this.getCurBreadcrumb();
|
||||
return curBreadcrumb ? curBreadcrumb.style.left : '0';
|
||||
}
|
||||
|
||||
@autobind
|
||||
toNumber(pxStr: string): number {
|
||||
let curScrollLeft = 0;
|
||||
if (!pxStr) {
|
||||
return curScrollLeft;
|
||||
}
|
||||
return Number.parseInt(pxStr);
|
||||
}
|
||||
|
||||
@autobind
|
||||
HandleScroll(resetScroll?: boolean) {
|
||||
const scrollElem = this.getCurBreadcrumb();
|
||||
const scrollContainer = this.getBreadcrumbContainer();
|
||||
|
||||
if (!scrollElem || !scrollContainer) {
|
||||
return;
|
||||
}
|
||||
|
||||
const scrollLeft = this.toNumber(this.getScrollLeft());
|
||||
const maxScrollLeft = scrollElem.offsetWidth - scrollContainer.offsetWidth;
|
||||
|
||||
if (resetScroll) {
|
||||
scrollElem.style.left = '0';
|
||||
}
|
||||
|
||||
this.setState({
|
||||
showLeftScrollBtn: scrollLeft < 0,
|
||||
showRightScrollBtn: scrollLeft > -maxScrollLeft
|
||||
});
|
||||
}
|
||||
|
||||
@autobind
|
||||
handleScrollToLeft() {
|
||||
// 向左滚动:看左侧内容,left + 50, 滚动内容需要右移
|
||||
const scrollElem = this.getCurBreadcrumb();
|
||||
|
||||
if (!scrollElem) {
|
||||
return;
|
||||
}
|
||||
|
||||
const scrollLeft = this.toNumber(this.getScrollLeft());
|
||||
|
||||
if (scrollLeft >= -50 && scrollLeft < 0) {
|
||||
scrollElem.style.left = '0';
|
||||
this.refreshHandleScroll();
|
||||
} else if (scrollLeft < -50) {
|
||||
scrollElem.style.left = `${scrollLeft + 50}px`;
|
||||
this.refreshHandleScroll();
|
||||
}
|
||||
}
|
||||
|
||||
@autobind
|
||||
handleScrollToRight() {
|
||||
// 向左滚动:看左侧内容,left - 50, 滚动内容需要左移
|
||||
const scrollElem = this.getCurBreadcrumb();
|
||||
const scrollContainer = this.getBreadcrumbContainer();
|
||||
|
||||
if (!scrollElem || !scrollContainer) {
|
||||
return;
|
||||
}
|
||||
|
||||
const scrollLeft = this.toNumber(this.getScrollLeft());
|
||||
const maxScrollLeft =
|
||||
scrollElem.offsetWidth - scrollContainer.offsetWidth + 32;
|
||||
|
||||
if (scrollLeft - 50 > -maxScrollLeft) {
|
||||
scrollElem.style.left = `${scrollLeft - 50}px`;
|
||||
} else {
|
||||
scrollElem.style.left = `-${maxScrollLeft}px`;
|
||||
}
|
||||
this.refreshHandleScroll();
|
||||
}
|
||||
|
||||
@autobind
|
||||
handleClick(e: React.MouseEvent<HTMLAnchorElement>) {
|
||||
const dom = e.currentTarget;
|
||||
const id = dom.getAttribute('data-node-id')!;
|
||||
const region = dom.getAttribute('data-node-region')!;
|
||||
const store = this.props.store;
|
||||
const manager = this.props.manager;
|
||||
const node = store.getNodeById(id);
|
||||
|
||||
if (node?.info?.editable === false) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (region) {
|
||||
// 点击容器类型自动弹出「组件插入面板」有点干扰用户操作
|
||||
// manager.showInsertPanel(region, id);
|
||||
/** 特殊区域允许点击事件 */
|
||||
store.setActiveId(id, region);
|
||||
} else {
|
||||
store.setActiveId(id);
|
||||
}
|
||||
}
|
||||
|
||||
@autobind
|
||||
handleMouseEnter(e: React.MouseEvent<HTMLAnchorElement>) {
|
||||
const dom = e.currentTarget;
|
||||
const id = dom.getAttribute('data-node-id')!;
|
||||
const region = dom.getAttribute('data-node-region')!;
|
||||
const store = this.props.store;
|
||||
|
||||
store.setHoverId(id, region);
|
||||
}
|
||||
|
||||
render() {
|
||||
const {store} = this.props;
|
||||
const {showLeftScrollBtn, showRightScrollBtn} = this.state;
|
||||
const bcn = store.bcn;
|
||||
|
||||
return (
|
||||
<div className="ae-Breadcrumb" ref={this.breadcrumbRef}>
|
||||
{showLeftScrollBtn && (
|
||||
<div
|
||||
className="ae-Breadcrumb-scrollLeft-btn"
|
||||
onClick={this.handleScrollToLeft}
|
||||
>
|
||||
<Icon icon="editor-double-arrow" className="icon" />
|
||||
</div>
|
||||
)}
|
||||
<div className="ae-Breadcrumb-content" ref={this.bcnContentRef}>
|
||||
{bcn.length ? (
|
||||
<ul>
|
||||
{bcn.map((item, index) => {
|
||||
const nearby: Array<EditorNodeType> = (
|
||||
item.parent as EditorNodeType
|
||||
)?.uniqueChildren;
|
||||
const region =
|
||||
item.region ||
|
||||
item.childRegions.find((i: any) => i.region)?.region;
|
||||
return (
|
||||
<li key={index}>
|
||||
<span
|
||||
data-node-id={item.id}
|
||||
data-node-region={region}
|
||||
onClick={this.handleClick}
|
||||
onMouseEnter={this.handleMouseEnter}
|
||||
>
|
||||
{item.label}
|
||||
</span>
|
||||
{nearby?.length > 1 ? (
|
||||
<ul className="hoverShowScrollBar">
|
||||
{nearby.map(child => (
|
||||
<li key={`${child.id}-${child.region}`}>
|
||||
<span
|
||||
data-node-id={child.id}
|
||||
data-node-region={child.region}
|
||||
onClick={this.handleClick}
|
||||
onMouseEnter={this.handleMouseEnter}
|
||||
className={
|
||||
child.id === item.id &&
|
||||
child.region === item.region
|
||||
? 'is-active'
|
||||
: ''
|
||||
}
|
||||
>
|
||||
{child.label}
|
||||
</span>
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
) : null}
|
||||
</li>
|
||||
);
|
||||
})}
|
||||
</ul>
|
||||
) : null}
|
||||
</div>
|
||||
{showRightScrollBtn && (
|
||||
<div
|
||||
className="ae-Breadcrumb-scrollRight-btn"
|
||||
onClick={this.handleScrollToRight}
|
||||
>
|
||||
<Icon icon="editor-double-arrow" className="icon" />
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
}
|
||||
1055
packages/amis-editor-core/src/component/ClassNameControl.tsx
Normal file
@@ -0,0 +1,79 @@
|
||||
import {findDOMNode} from 'react-dom';
|
||||
import {JSONPipeOut} from '../util';
|
||||
import React from 'react';
|
||||
import {NodeWrapper} from './NodeWrapper';
|
||||
|
||||
export class CommonConfigWrapper extends NodeWrapper {
|
||||
// 销毁的时候要把加的 aeEditor-common-config 干掉
|
||||
componentWillUnmount() {
|
||||
const root = findDOMNode(this) as HTMLElement;
|
||||
|
||||
if (!root) {
|
||||
return;
|
||||
}
|
||||
|
||||
const info = this.props.$$editor;
|
||||
let dom = info.wrapperResolve ? info.wrapperResolve(root) : root;
|
||||
|
||||
(Array.isArray(dom) ? dom : dom ? [dom] : []).forEach(dom => {
|
||||
dom.classList.remove('ae-Editor-common-config');
|
||||
});
|
||||
}
|
||||
|
||||
markDom(id: string) {
|
||||
const root = findDOMNode(this) as HTMLElement;
|
||||
|
||||
if (!root || !id) {
|
||||
return;
|
||||
}
|
||||
|
||||
const info = this.props.$$editor;
|
||||
|
||||
let dom = info.wrapperResolve ? info.wrapperResolve(root) : root;
|
||||
const schema = this.props.$$commonSchema;
|
||||
schema &&
|
||||
(Array.isArray(dom) ? dom : dom ? [dom] : []).forEach(dom => {
|
||||
dom.setAttribute('data-editor-id', id);
|
||||
dom.classList.add('ae-Editor-common-config');
|
||||
});
|
||||
}
|
||||
|
||||
render() {
|
||||
// store 都有可能有循环引用,不能调用 JSONPipeOut
|
||||
let {$$editor, $$node, $schema, store, ...rest} = this.props;
|
||||
const renderer = $$editor.renderer;
|
||||
|
||||
rest = JSONPipeOut(rest, false);
|
||||
|
||||
if ($$editor.filterProps) {
|
||||
rest = $$editor.filterProps.call($$editor.plugin, rest, $$node);
|
||||
}
|
||||
|
||||
if ($$editor.renderRenderer) {
|
||||
return $$editor.renderRenderer.call(
|
||||
$$editor.plugin,
|
||||
{
|
||||
...rest,
|
||||
store,
|
||||
$schema,
|
||||
$$editor,
|
||||
...$$editor.wrapperProps,
|
||||
ref: this.refFn
|
||||
},
|
||||
$$editor
|
||||
);
|
||||
}
|
||||
const Component = renderer.component!;
|
||||
|
||||
return (
|
||||
<Component
|
||||
{...rest}
|
||||
store={store}
|
||||
$schema={$schema}
|
||||
$$editor={$$editor}
|
||||
{...$$editor.wrapperProps}
|
||||
ref={this.refFn}
|
||||
/>
|
||||
);
|
||||
}
|
||||
}
|
||||
135
packages/amis-editor-core/src/component/ContainerWrapper.tsx
Normal file
@@ -0,0 +1,135 @@
|
||||
import {NodeWrapper, NodeWrapperProps} from './NodeWrapper';
|
||||
import React from 'react';
|
||||
import {observer} from 'mobx-react';
|
||||
import {autobind} from '../util';
|
||||
import type {Schema} from 'amis';
|
||||
import find from 'lodash/find';
|
||||
import {RegionWrapper} from './RegionWrapper';
|
||||
|
||||
export interface ContainerWrapperProps extends NodeWrapperProps {}
|
||||
|
||||
@observer
|
||||
export class ContainerWrapper extends React.Component<ContainerWrapperProps> {
|
||||
ref: any;
|
||||
getWrappedInstance() {
|
||||
return this.ref;
|
||||
}
|
||||
|
||||
@autobind
|
||||
refFn(ref: any) {
|
||||
this.ref = ref;
|
||||
}
|
||||
|
||||
/*
|
||||
由于基本上容器渲染器都是通过 this.props.render('region', subScheme) 来渲染孩子节点的。
|
||||
所以 ContainerWrapper 只要修改下发的 render 即可完成包裹。
|
||||
*/
|
||||
@autobind
|
||||
renderChild(region: string, node: Schema, props: any) {
|
||||
const {render, $$editor, $$node, $schema} = this.props;
|
||||
|
||||
if (
|
||||
$$editor.regions?.find(item => item.key === region)?.hiddenOn?.($schema)
|
||||
) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const child = render(region, node, props);
|
||||
|
||||
if ($$node?.memberImmutable(region)) {
|
||||
return child;
|
||||
}
|
||||
|
||||
const config = find(
|
||||
$$editor.regions,
|
||||
item => item.key === region && !item.matchRegion && !item.renderMethod
|
||||
);
|
||||
|
||||
if (config) {
|
||||
const Region = config.wrapper || RegionWrapper;
|
||||
|
||||
return (
|
||||
<Region
|
||||
key={props?.key}
|
||||
preferTag={config.preferTag}
|
||||
name={config.key}
|
||||
label={config.label}
|
||||
placeholder={config.placeholder}
|
||||
regionConfig={config}
|
||||
editorStore={$$editor.plugin.manager.store}
|
||||
wrapperResolve={config.wrapperResolve}
|
||||
manager={$$editor.plugin.manager}
|
||||
children={child}
|
||||
rendererName={$$editor.renderer.name}
|
||||
$$editor={$$editor}
|
||||
node={$$node}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
return child;
|
||||
}
|
||||
|
||||
render() {
|
||||
const {$$editor, $$node, ...rest} = this.props;
|
||||
const props: any = {};
|
||||
const editorStore = $$editor.plugin.manager.store;
|
||||
|
||||
if (
|
||||
$$editor.id &&
|
||||
(editorStore.isActive($$editor.id) ||
|
||||
editorStore.dropId === $$editor.id) &&
|
||||
Array.isArray($$editor.regions)
|
||||
) {
|
||||
$$editor.regions.forEach(({key, optional}) => {
|
||||
if (optional) {
|
||||
return;
|
||||
} else if ($$node?.memberImmutable(key)) {
|
||||
return;
|
||||
}
|
||||
|
||||
let defaultRegion: any[] = [];
|
||||
// /**
|
||||
// * form表单的按钮组特殊处理
|
||||
// * 原因:确保编辑态也显示默认的提交按钮
|
||||
// */
|
||||
// if (
|
||||
// key === 'actions' &&
|
||||
// (typeof rest.submitText === 'undefined' || rest.submitText)
|
||||
// ) {
|
||||
// defaultRegion = [
|
||||
// {
|
||||
// type: 'submit',
|
||||
// label: rest.submitText || '提交',
|
||||
// primary: true
|
||||
// }
|
||||
// ];
|
||||
// }
|
||||
|
||||
let region = Array.isArray(rest[key])
|
||||
? rest[key]
|
||||
: rest[key]
|
||||
? [rest[key]]
|
||||
: defaultRegion;
|
||||
|
||||
if (!region.length) {
|
||||
region = region.concat();
|
||||
region.push({children: () => null});
|
||||
}
|
||||
|
||||
props[key] = region;
|
||||
});
|
||||
}
|
||||
|
||||
return (
|
||||
<NodeWrapper
|
||||
{...rest}
|
||||
{...props}
|
||||
$$editor={$$editor}
|
||||
$$node={$$node}
|
||||
render={this.renderChild}
|
||||
ref={this.refFn}
|
||||
/>
|
||||
);
|
||||
}
|
||||
}
|
||||
739
packages/amis-editor-core/src/component/Editor.tsx
Normal file
@@ -0,0 +1,739 @@
|
||||
import React, {Component} from 'react';
|
||||
import cx from 'classnames';
|
||||
import Preview from './Preview';
|
||||
import {autobind} from '../util';
|
||||
import {MainStore, EditorStoreType} from '../store/editor';
|
||||
import {EditorManager, EditorManagerConfig, PluginClass} from '../manager';
|
||||
import {reaction} from 'mobx';
|
||||
import {RenderOptions, closeContextMenus, toast} from 'amis';
|
||||
import {
|
||||
PluginEventListener,
|
||||
RendererPluginAction,
|
||||
IGlobalEvent
|
||||
} from '../plugin';
|
||||
import {reGenerateID} from '../util';
|
||||
import {SubEditor} from './SubEditor';
|
||||
import Breadcrumb from './Breadcrumb';
|
||||
import {destroy, isAlive} from 'mobx-state-tree';
|
||||
import {ScaffoldModal} from './ScaffoldModal';
|
||||
import {PopOverForm} from './PopOverForm';
|
||||
import {ModalForm} from './ModalForm';
|
||||
import {ContextMenuPanel} from './Panel/ContextMenuPanel';
|
||||
import {LeftPanels} from './Panel/LeftPanels';
|
||||
import {RightPanels} from './Panel/RightPanels';
|
||||
import type {SchemaObject} from 'amis';
|
||||
import type {VariableGroup, VariableOptions} from '../variable';
|
||||
import type {EditorNodeType} from '../store/node';
|
||||
import {MobileDevTool} from 'amis-ui';
|
||||
import {LeftPanelsProps} from './Panel/LeftPanels';
|
||||
import {RightPanelsProps} from './Panel/RightPanels';
|
||||
|
||||
export interface EditorProps extends PluginEventListener {
|
||||
value: SchemaObject;
|
||||
onChange: (value: SchemaObject) => void;
|
||||
preview?: boolean;
|
||||
isMobile?: boolean;
|
||||
isSubEditor?: boolean;
|
||||
autoFocus?: boolean;
|
||||
className?: string;
|
||||
$schemaUrl?: string;
|
||||
schemas?: Array<any>;
|
||||
theme?: string;
|
||||
/** 工具栏模式 */
|
||||
toolbarMode?: 'default' | 'mini';
|
||||
/** 是否需要弹框 */
|
||||
noDialog?: boolean;
|
||||
/** 应用语言类型 */
|
||||
appLocale?: string;
|
||||
/** 是否开启多语言 */
|
||||
i18nEnabled?: boolean;
|
||||
showCustomRenderersPanel?: boolean;
|
||||
amisDocHost?: string;
|
||||
superEditorData?: any;
|
||||
withSuperDataSchema?: boolean;
|
||||
/** 当前 Editor 为 SubEditor 时触发的宿主节点 */
|
||||
hostManager?: EditorManager;
|
||||
hostNode?: EditorNodeType;
|
||||
dataBindingChange?: (
|
||||
value: string,
|
||||
data: any,
|
||||
manager?: EditorManager
|
||||
) => void;
|
||||
|
||||
/**
|
||||
* Preview 预览前可以修改配置。
|
||||
* 比如把api地址替换成 proxy 地址。
|
||||
*/
|
||||
schemaFilter?: (schema: any, isPreview?: boolean) => any;
|
||||
amisEnv?: RenderOptions;
|
||||
|
||||
/**
|
||||
* 上下文数据,设置后,面板和脚手架表单里面可以取到这些值。
|
||||
*/
|
||||
ctx?: any;
|
||||
data?: any;
|
||||
|
||||
/**
|
||||
* 是否禁用内置插件
|
||||
*/
|
||||
disableBultinPlugin?: boolean;
|
||||
|
||||
/**
|
||||
* 插件场景
|
||||
*/
|
||||
scene?: string;
|
||||
|
||||
disablePluginList?:
|
||||
| Array<string>
|
||||
| ((id: string, plugin: PluginClass) => boolean);
|
||||
|
||||
plugins?: Array<
|
||||
| PluginClass
|
||||
| [PluginClass, Record<string, any> | (() => Record<string, any>)]
|
||||
>;
|
||||
|
||||
/**
|
||||
* 传给预览器的其他属性
|
||||
*/
|
||||
previewProps?: any;
|
||||
|
||||
isHiddenProps?: (key: string) => boolean;
|
||||
|
||||
/**
|
||||
* 事件动作面板相关配置
|
||||
*/
|
||||
actionOptions?: {
|
||||
// 是否开启老动作配置入口
|
||||
showOldEntry?: boolean;
|
||||
/**
|
||||
* 通用动作集(事件动作面板左侧动作树)
|
||||
*/
|
||||
actionTreeGetter?: (
|
||||
actionTree: RendererPluginAction[]
|
||||
) => RendererPluginAction[];
|
||||
/**
|
||||
* 自定义动作配置
|
||||
*/
|
||||
customActionGetter?: (manager: EditorManager) => {
|
||||
[propName: string]: RendererPluginAction;
|
||||
};
|
||||
|
||||
globalEventGetter?: (manager: EditorManager) => IGlobalEvent[];
|
||||
};
|
||||
|
||||
/** 上下文变量 */
|
||||
variables?: VariableGroup[];
|
||||
/** 变量配置 */
|
||||
variableOptions?: VariableOptions;
|
||||
|
||||
onUndo?: () => void; // 用于触发外部 undo 事件
|
||||
onRedo?: () => void; // 用于触发外部 redo 事件
|
||||
onSave?: () => void; // 用于触发外部 save 事件
|
||||
onPreview?: (preview: boolean) => void; // 用于触发外部 预览 事件
|
||||
|
||||
/** 打开公式编辑器之前触发的事件 */
|
||||
onFormulaEditorOpen?: (
|
||||
node: EditorNodeType,
|
||||
manager: EditorManager,
|
||||
ctx: Record<string, any>,
|
||||
host?: {
|
||||
node?: EditorNodeType;
|
||||
manager?: EditorManager;
|
||||
}
|
||||
) => Promise<void | boolean>;
|
||||
|
||||
getHostNodeDataSchema?: () => Promise<any>;
|
||||
|
||||
getAvaiableContextFields?: (node: EditorNodeType) => Promise<any>;
|
||||
readonly?: boolean;
|
||||
|
||||
onEditorMount?: (manager: EditorManager) => void;
|
||||
onEditorUnmount?: (manager: EditorManager) => void;
|
||||
|
||||
children?: React.ReactNode | ((manager: EditorManager) => React.ReactNode);
|
||||
|
||||
LeftPanelsComponent?: React.ComponentType<LeftPanelsProps>;
|
||||
RightPanelsComponent?: React.ComponentType<RightPanelsProps>;
|
||||
|
||||
/**
|
||||
* 富文本编辑器配置, 用于内联编辑
|
||||
*/
|
||||
richTextOptions?: any;
|
||||
richTextToken?: string;
|
||||
}
|
||||
|
||||
export default class Editor extends Component<EditorProps> {
|
||||
readonly store: EditorStoreType;
|
||||
readonly manager: EditorManager;
|
||||
readonly mainRef = React.createRef<HTMLDivElement>();
|
||||
readonly mainPreviewRef = React.createRef<HTMLDivElement>();
|
||||
readonly mainPreviewBodyRef = React.createRef<any>();
|
||||
toDispose: Array<Function> = [];
|
||||
lastResult: any;
|
||||
curCopySchemaData: any; // 用于记录当前复制的元素
|
||||
|
||||
static defaultProps = {
|
||||
autoFocus: true
|
||||
};
|
||||
isInternalChange: boolean = false;
|
||||
|
||||
constructor(props: EditorProps) {
|
||||
super(props);
|
||||
|
||||
const {
|
||||
value,
|
||||
isSubEditor = false,
|
||||
onChange,
|
||||
showCustomRenderersPanel,
|
||||
superEditorData,
|
||||
hostManager,
|
||||
onEditorMount,
|
||||
...rest
|
||||
} = props;
|
||||
|
||||
const config: EditorManagerConfig = {
|
||||
...rest
|
||||
};
|
||||
this.store = MainStore.create(
|
||||
{
|
||||
isMobile: props.isMobile,
|
||||
theme: props.theme,
|
||||
toolbarMode: props.toolbarMode || 'default',
|
||||
noDialog: props.noDialog,
|
||||
isSubEditor,
|
||||
amisDocHost: props.amisDocHost,
|
||||
superEditorData,
|
||||
appLocale: props.appLocale,
|
||||
appCorpusData: props?.amisEnv?.replaceText,
|
||||
i18nEnabled: props?.i18nEnabled ?? false
|
||||
},
|
||||
config
|
||||
);
|
||||
this.store.setCtx(props.ctx);
|
||||
this.store.setSchema(value);
|
||||
if (showCustomRenderersPanel !== undefined) {
|
||||
this.store.setShowCustomRenderersPanel(showCustomRenderersPanel);
|
||||
}
|
||||
|
||||
this.manager = new EditorManager(config, this.store, hostManager);
|
||||
|
||||
this.store.setGlobalEvents(
|
||||
config.actionOptions?.globalEventGetter?.(this.manager) || []
|
||||
);
|
||||
|
||||
// 子编辑器不再重新设置 editorStore
|
||||
if (!(props.isSubEditor && (window as any).editorStore)) {
|
||||
(window as any).editorStore = this.store;
|
||||
}
|
||||
|
||||
// 添加快捷键事件
|
||||
document.addEventListener('keydown', this.handleKeyDown);
|
||||
|
||||
window.addEventListener('message', this.handleMessage, false);
|
||||
|
||||
this.toDispose.push(
|
||||
reaction(
|
||||
() => this.store.schemaRaw,
|
||||
(raw: any) => {
|
||||
this.lastResult = raw;
|
||||
|
||||
if (this.isInternalChange) {
|
||||
return;
|
||||
}
|
||||
props.onChange(raw);
|
||||
}
|
||||
)
|
||||
);
|
||||
this.toDispose.push(
|
||||
this.manager.on('preview2editor', () => this.manager.rebuild())
|
||||
);
|
||||
|
||||
onEditorMount?.(this.manager);
|
||||
}
|
||||
|
||||
componentDidMount() {
|
||||
const store = this.manager.store;
|
||||
if (this.props.isSubEditor) {
|
||||
// 等待子编辑器动画结束重新获取高亮组件位置
|
||||
setTimeout(() => {
|
||||
store.calculateHighlightBox(store.highlightNodes.map(node => node.id));
|
||||
}, 500);
|
||||
} else {
|
||||
this.manager.trigger('init', {
|
||||
data: this.manager
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
componentDidUpdate(prevProps: EditorProps) {
|
||||
const props = this.props;
|
||||
|
||||
if (props.value !== prevProps.value && props.value !== this.lastResult) {
|
||||
this.isInternalChange = true;
|
||||
this.store.setSchema(props.value);
|
||||
this.isInternalChange = false;
|
||||
}
|
||||
|
||||
if (props.isMobile !== prevProps.isMobile) {
|
||||
this.store.setIsMobile(props.isMobile);
|
||||
}
|
||||
|
||||
if (props.ctx !== prevProps.ctx) {
|
||||
this.store.setCtx(props.ctx);
|
||||
}
|
||||
|
||||
if (props.appLocale !== prevProps.appLocale) {
|
||||
this.store.setAppLocale(props.appLocale);
|
||||
}
|
||||
|
||||
if (props?.amisEnv?.replaceText !== prevProps?.amisEnv?.replaceText) {
|
||||
this.store.setAppCorpusData(props?.amisEnv?.replaceText);
|
||||
}
|
||||
if (
|
||||
props.actionOptions?.globalEventGetter?.(this.manager) !==
|
||||
prevProps.actionOptions?.globalEventGetter?.(this.manager)
|
||||
) {
|
||||
this.store.setGlobalEvents(
|
||||
props.actionOptions?.globalEventGetter?.(this.manager) || []
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
componentWillUnmount() {
|
||||
this.props.onEditorUnmount?.(this.manager);
|
||||
document.removeEventListener('keydown', this.handleKeyDown);
|
||||
window.removeEventListener('message', this.handleMessage);
|
||||
this.toDispose.forEach(fn => fn());
|
||||
this.toDispose = [];
|
||||
this.manager.dispose();
|
||||
setTimeout(() => destroy(this.store), 4);
|
||||
}
|
||||
|
||||
// 快捷功能键
|
||||
@autobind
|
||||
handleKeyDown(e: KeyboardEvent) {
|
||||
const manager = this.manager;
|
||||
const store = manager.store;
|
||||
|
||||
// 弹窗模式不处理
|
||||
if (this.props.isSubEditor || this.props.readonly || store.activeElement) {
|
||||
// e.defaultPrevented // 或者已经阻止不处理
|
||||
return;
|
||||
}
|
||||
|
||||
if (
|
||||
(e.target as HTMLElement).tagName === 'BODY' &&
|
||||
(e.key === 'z' || e.key === 'Z') &&
|
||||
(e.metaKey || e.ctrlKey) &&
|
||||
e.shiftKey
|
||||
) {
|
||||
e.preventDefault();
|
||||
this.redo(); // 重做
|
||||
return;
|
||||
} else if (
|
||||
(e.target as HTMLElement).tagName === 'BODY' &&
|
||||
(e.key === 'z' || e.key === 'Z') &&
|
||||
(e.metaKey || e.ctrlKey)
|
||||
) {
|
||||
e.preventDefault();
|
||||
this.undo(); // 撤销
|
||||
return;
|
||||
} else if (
|
||||
(e.target as HTMLElement).tagName === 'BODY' &&
|
||||
(e.key === 's' || e.key === 'S') &&
|
||||
(e.metaKey || e.ctrlKey)
|
||||
) {
|
||||
e.preventDefault();
|
||||
this.save(); // 保存
|
||||
return;
|
||||
} else if (
|
||||
(e.target as HTMLElement).tagName === 'BODY' &&
|
||||
(e.key === 'c' || e.key === 'C') &&
|
||||
(e.metaKey || e.ctrlKey)
|
||||
) {
|
||||
// e.preventDefault(); // 注释掉阻止默认行为的方法,允许用户copy设计器中的文字
|
||||
this.copy(); // 复制
|
||||
return;
|
||||
} else if (
|
||||
(e.target as HTMLElement).tagName === 'BODY' &&
|
||||
(e.key === 'v' || e.key === 'V') &&
|
||||
(e.metaKey || e.ctrlKey)
|
||||
) {
|
||||
e.preventDefault();
|
||||
if (this.curCopySchemaData) {
|
||||
this.paste(); // 粘贴
|
||||
}
|
||||
return;
|
||||
} else if (
|
||||
(e.target as HTMLElement).tagName === 'BODY' &&
|
||||
e.key === 'x' &&
|
||||
(e.metaKey || e.ctrlKey)
|
||||
) {
|
||||
e.preventDefault();
|
||||
// 剪切
|
||||
if (this.store.activeId) {
|
||||
const node = store.getNodeById(this.store.activeId);
|
||||
if (node && store.activeRegion) {
|
||||
toast.warning('区域节点不允许剪切。');
|
||||
} else if (store.isRootSchema(this.store.activeId)) {
|
||||
toast.warning('根节点不允许剪切。');
|
||||
} else if (node && node.moveable) {
|
||||
this.copy(); // 复制
|
||||
this.manager.del(this.store.activeId); // 删除
|
||||
} else {
|
||||
toast.warning('当前元素不允许剪切。');
|
||||
}
|
||||
}
|
||||
} else if (
|
||||
(e.target as HTMLElement).tagName === 'BODY' &&
|
||||
e.key === 'p' &&
|
||||
(e.metaKey || e.ctrlKey)
|
||||
) {
|
||||
// 预览
|
||||
e.preventDefault(); // 避免触发系统默认的事件(打印快捷键)
|
||||
this.preview();
|
||||
} else if (
|
||||
(e.target as HTMLElement).tagName === 'BODY' &&
|
||||
e.key === 'ArrowUp' &&
|
||||
(e.metaKey || e.ctrlKey)
|
||||
) {
|
||||
e.preventDefault();
|
||||
// 向上移动
|
||||
if (this.store.activeId) {
|
||||
const node = store.getNodeById(this.store.activeId);
|
||||
if (node && node.canMoveUp) {
|
||||
this.manager.moveUp();
|
||||
} else {
|
||||
toast.warning('当前元素不能向上移动');
|
||||
}
|
||||
}
|
||||
} else if (
|
||||
(e.target as HTMLElement).tagName === 'BODY' &&
|
||||
e.key === 'ArrowDown' &&
|
||||
(e.metaKey || e.ctrlKey)
|
||||
) {
|
||||
e.preventDefault();
|
||||
// 向下移动
|
||||
if (this.store.activeId) {
|
||||
const node = store.getNodeById(this.store.activeId);
|
||||
if (node && node.canMoveDown) {
|
||||
this.manager.moveDown();
|
||||
} else {
|
||||
toast.warning('当前元素不能向下移动');
|
||||
}
|
||||
}
|
||||
} else if (
|
||||
(e.target as HTMLElement).tagName === 'BODY' &&
|
||||
(e.key === 'Backspace' || e.key === 'Delete')
|
||||
) {
|
||||
e.preventDefault();
|
||||
// 删除快捷键
|
||||
if (this.store.activeId) {
|
||||
const node = store.getNodeById(this.store.activeId);
|
||||
if (
|
||||
node &&
|
||||
store.activeRegion &&
|
||||
node.info?.regions &&
|
||||
node.info.regions.length > 1
|
||||
) {
|
||||
toast.warning('区域节点不可以直接删除。');
|
||||
} else if (store.isRootSchema(this.store.activeId)) {
|
||||
toast.warning('根节点不允许删除。');
|
||||
} else if (node && (node.removable || node.removable === undefined)) {
|
||||
this.manager.del(this.store.activeId);
|
||||
} else {
|
||||
toast.warning('当前元素不允许删除。');
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
@autobind
|
||||
handleMessage(event: any) {
|
||||
if (!event.data) {
|
||||
return;
|
||||
}
|
||||
// 增加插件动态添加事件响应机制
|
||||
if (
|
||||
event.data.type === 'amis-widget-register-event' &&
|
||||
event.data.editorPluginName
|
||||
) {
|
||||
console.info(
|
||||
'[amis-editor]响应动态添加插件事件:',
|
||||
event.data.editorPluginName
|
||||
);
|
||||
this.manager.dynamicAddPlugin(event.data.editorPluginName);
|
||||
}
|
||||
}
|
||||
|
||||
// 右键菜单
|
||||
@autobind
|
||||
async handleContextMenu(e: React.MouseEvent<HTMLElement>) {
|
||||
// inline edit 模式不要右键
|
||||
if (this.store.activeElement) {
|
||||
return;
|
||||
}
|
||||
|
||||
e.persist();
|
||||
await closeContextMenus();
|
||||
let targetId: string = '';
|
||||
let region = '';
|
||||
|
||||
// 如果在某个已点选的控件下面,则让当前选中的渲染器呼出右键菜单
|
||||
if (this.store.activeId) {
|
||||
targetId = (e.target as HTMLElement)
|
||||
.closest(`[data-editor-id="${this.store.activeId}"]`)
|
||||
?.getAttribute('data-editor-id')!;
|
||||
} else if (this.store.selections.length) {
|
||||
targetId = (e.target as HTMLElement)
|
||||
.closest(
|
||||
this.store.selections
|
||||
.map(item => `[data-editor-id="${item}"]`)
|
||||
.join(',')
|
||||
)
|
||||
?.getAttribute('data-editor-id')!;
|
||||
}
|
||||
|
||||
// 没找到就近找
|
||||
if (!targetId) {
|
||||
const dom = (e.target as HTMLElement).closest(
|
||||
'[data-editor-id], [data-hlbox-id]'
|
||||
);
|
||||
targetId =
|
||||
dom?.getAttribute('data-editor-id') ||
|
||||
dom?.getAttribute('data-hlbox-id') ||
|
||||
'';
|
||||
}
|
||||
|
||||
// 没找到看看是不是在大纲中的右键
|
||||
if (!targetId) {
|
||||
const node = (e.target as HTMLElement).closest(
|
||||
'[data-node-id]'
|
||||
) as HTMLElement;
|
||||
targetId = node?.getAttribute('data-node-id')!;
|
||||
|
||||
if (!targetId) {
|
||||
return;
|
||||
}
|
||||
|
||||
region = node.getAttribute('data-node-region')!;
|
||||
}
|
||||
|
||||
e.preventDefault();
|
||||
e.stopPropagation();
|
||||
const manager = this.manager;
|
||||
let offsetX = 0;
|
||||
let offsetY = 0;
|
||||
|
||||
// 说明是 iframe 里面
|
||||
if ((e.target as HTMLElement).ownerDocument !== document) {
|
||||
const rect = manager.store.getIframe()!.getBoundingClientRect();
|
||||
offsetX = rect.left;
|
||||
offsetY = rect.top;
|
||||
}
|
||||
|
||||
manager.openContextMenu(targetId, region, {
|
||||
x: window.scrollX + e.clientX + offsetX,
|
||||
y: window.scrollY + e.clientY + offsetY,
|
||||
clientX: e.clientX,
|
||||
clientY: e.clientY,
|
||||
target: e.target as HTMLElement
|
||||
});
|
||||
}
|
||||
|
||||
canUndo() {
|
||||
return this.store.canUndo;
|
||||
}
|
||||
|
||||
canRedo() {
|
||||
return this.store.canRedo;
|
||||
}
|
||||
|
||||
undo() {
|
||||
if (this.props.onUndo) {
|
||||
this.props.onUndo(); // 优先使用外部撤销方法
|
||||
} else {
|
||||
this.store.undo();
|
||||
}
|
||||
}
|
||||
|
||||
redo() {
|
||||
if (this.props.onRedo) {
|
||||
this.props.onRedo(); // 优先使用外部重做方法
|
||||
} else {
|
||||
this.store.redo();
|
||||
}
|
||||
}
|
||||
|
||||
// 默认数据变动都会触发前端暂存,此处主要用于执行外部保存方法
|
||||
save() {
|
||||
if (this.props.onSave) {
|
||||
this.props.onSave();
|
||||
}
|
||||
}
|
||||
|
||||
preview() {
|
||||
if (this.props.onPreview) {
|
||||
this.props.onPreview(!this.props.preview);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 复制的内容以变量的形式存放
|
||||
* 备注1: 系统的复制&粘贴需要开启https服务才有效,所有这里改用内存形式实现
|
||||
* 备注2: 此方法不鞥实现跨页面复制&粘贴能力
|
||||
* 备注3: 后续需要支持下跨页面跨浏览器复制&粘贴能力
|
||||
*/
|
||||
copy() {
|
||||
if (this.store.activeId) {
|
||||
this.curCopySchemaData = this.store.getSchema(this.store.activeId);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 粘贴上一次复制的内容
|
||||
*/
|
||||
paste() {
|
||||
if (this.store.activeId && this.curCopySchemaData) {
|
||||
if (!this.curCopySchemaData) {
|
||||
// 考虑复制的元素被删除的情况
|
||||
return;
|
||||
}
|
||||
const curSimpleSchema = this.store.getSimpleSchema(
|
||||
this.curCopySchemaData
|
||||
);
|
||||
if (this.store.activeId === this.curCopySchemaData.$$id) {
|
||||
// 复制和粘贴是同一个元素,则直接追加到当前元素后面
|
||||
this.manager.appendSiblingSchema(reGenerateID(curSimpleSchema), false);
|
||||
} else {
|
||||
this.manager.addElem(reGenerateID(curSimpleSchema), false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@autobind
|
||||
getToolbarContainer() {
|
||||
return this.mainRef.current;
|
||||
}
|
||||
|
||||
render() {
|
||||
const {
|
||||
preview,
|
||||
isMobile,
|
||||
className,
|
||||
theme,
|
||||
appLocale,
|
||||
data,
|
||||
previewProps,
|
||||
autoFocus,
|
||||
isSubEditor,
|
||||
amisEnv,
|
||||
readonly,
|
||||
children,
|
||||
LeftPanelsComponent,
|
||||
RightPanelsComponent
|
||||
} = this.props;
|
||||
const FinalLeftPanels = LeftPanelsComponent ?? LeftPanels;
|
||||
const FinalRightPanels = RightPanelsComponent ?? RightPanels;
|
||||
|
||||
return (
|
||||
<div
|
||||
ref={this.mainRef}
|
||||
className={cx(
|
||||
'ae-Editor',
|
||||
{
|
||||
preview: preview
|
||||
},
|
||||
className
|
||||
)}
|
||||
>
|
||||
<div
|
||||
className={cx(
|
||||
'ae-Editor-inner',
|
||||
isMobile && 'ae-Editor-inner--mobile'
|
||||
)}
|
||||
onContextMenu={this.handleContextMenu}
|
||||
>
|
||||
{!preview && !readonly && (
|
||||
<FinalLeftPanels
|
||||
store={this.store}
|
||||
manager={this.manager}
|
||||
theme={theme}
|
||||
/>
|
||||
)}
|
||||
|
||||
<div className="ae-Main" ref={this.mainPreviewRef}>
|
||||
{!preview && (
|
||||
<div className="ae-Header">
|
||||
<Breadcrumb store={this.store} manager={this.manager} />
|
||||
<div
|
||||
id="aeHeaderRightContainer"
|
||||
className="ae-Header-Right-Container"
|
||||
></div>
|
||||
</div>
|
||||
)}
|
||||
{isMobile && (
|
||||
<MobileDevTool
|
||||
container={this.mainPreviewRef.current}
|
||||
previewBody={
|
||||
this.mainPreviewBodyRef.current?.currentDom?.current
|
||||
}
|
||||
onChangeScale={scale => {
|
||||
if (scale >= 0) {
|
||||
this.store.setScale(scale / 100);
|
||||
}
|
||||
}}
|
||||
/>
|
||||
)}
|
||||
<Preview
|
||||
{...previewProps}
|
||||
editable={!preview}
|
||||
isMobile={isMobile}
|
||||
store={this.store}
|
||||
manager={this.manager}
|
||||
theme={theme}
|
||||
appLocale={appLocale}
|
||||
data={data}
|
||||
amisEnv={amisEnv}
|
||||
autoFocus={autoFocus}
|
||||
toolbarContainer={this.getToolbarContainer}
|
||||
readonly={readonly}
|
||||
ref={this.mainPreviewBodyRef}
|
||||
></Preview>
|
||||
</div>
|
||||
|
||||
{!preview && (
|
||||
<FinalRightPanels
|
||||
store={this.store}
|
||||
manager={this.manager}
|
||||
theme={theme}
|
||||
appLocale={appLocale}
|
||||
amisEnv={amisEnv}
|
||||
readonly={readonly}
|
||||
/>
|
||||
)}
|
||||
|
||||
{!preview && <ContextMenuPanel store={this.store} />}
|
||||
|
||||
{typeof children === 'function' ? children(this.manager) : children}
|
||||
</div>
|
||||
|
||||
<SubEditor
|
||||
store={this.store}
|
||||
manager={this.manager}
|
||||
theme={theme}
|
||||
amisEnv={amisEnv}
|
||||
readonly={readonly}
|
||||
/>
|
||||
<ScaffoldModal
|
||||
store={this.store}
|
||||
manager={this.manager}
|
||||
theme={theme}
|
||||
/>
|
||||
<PopOverForm store={this.store} manager={this.manager} theme={theme} />
|
||||
<ModalForm store={this.store} manager={this.manager} theme={theme} />
|
||||
</div>
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
import {findDOMNode} from 'react-dom';
|
||||
import {JSONPipeOut} from '../util';
|
||||
import React from 'react';
|
||||
import {NodeWrapper} from './NodeWrapper';
|
||||
|
||||
export class FormConfigWrapper extends NodeWrapper {
|
||||
// 销毁的时候要把加的 aeEditor-form-config 干掉
|
||||
componentWillUnmount() {
|
||||
const root = findDOMNode(this) as HTMLElement;
|
||||
|
||||
if (!root) {
|
||||
return;
|
||||
}
|
||||
|
||||
const info = this.props.$$editor;
|
||||
let dom = info.wrapperResolve ? info.wrapperResolve(root) : root;
|
||||
|
||||
(Array.isArray(dom) ? dom : dom ? [dom] : []).forEach(dom => {
|
||||
dom.classList.remove('ae-Editor-form-config');
|
||||
});
|
||||
}
|
||||
|
||||
markDom(id: string) {
|
||||
const root = findDOMNode(this) as HTMLElement;
|
||||
|
||||
if (!root || !id) {
|
||||
return;
|
||||
}
|
||||
|
||||
const info = this.props.$$editor;
|
||||
|
||||
let dom = info.wrapperResolve ? info.wrapperResolve(root) : root;
|
||||
const schema = this.props.$$formSchema;
|
||||
schema &&
|
||||
(Array.isArray(dom) ? dom : dom ? [dom] : []).forEach(dom => {
|
||||
dom.setAttribute('data-editor-id', id);
|
||||
dom.classList.add('ae-Editor-form-config');
|
||||
});
|
||||
}
|
||||
|
||||
render() {
|
||||
// store 都有可能有循环引用,不能调用 JSONPipeOut
|
||||
let {$$editor, $$node, $schema, store, ...rest} = this.props;
|
||||
const renderer = $$editor.renderer;
|
||||
|
||||
rest = JSONPipeOut(rest, false);
|
||||
|
||||
if ($$editor.filterProps) {
|
||||
rest = $$editor.filterProps.call($$editor.plugin, rest, $$node);
|
||||
}
|
||||
|
||||
if ($$editor.renderRenderer) {
|
||||
return $$editor.renderRenderer.call(
|
||||
$$editor.plugin,
|
||||
{
|
||||
...rest,
|
||||
store,
|
||||
$schema,
|
||||
$$editor,
|
||||
...$$editor.wrapperProps,
|
||||
ref: this.refFn
|
||||
},
|
||||
$$editor
|
||||
);
|
||||
}
|
||||
const Component = renderer.component!;
|
||||
|
||||
return (
|
||||
<Component
|
||||
{...rest}
|
||||
store={store}
|
||||
$schema={$schema}
|
||||
$$editor={$$editor}
|
||||
{...$$editor.wrapperProps}
|
||||
ref={this.refFn}
|
||||
/>
|
||||
);
|
||||
}
|
||||
}
|
||||
406
packages/amis-editor-core/src/component/HighlightBox.tsx
Normal file
@@ -0,0 +1,406 @@
|
||||
import React from 'react';
|
||||
import cx from 'classnames';
|
||||
import {EditorStoreType} from '../store/editor';
|
||||
import {observer} from 'mobx-react';
|
||||
import {EditorNodeType} from '../store/node';
|
||||
import {Icon} from 'amis';
|
||||
import {autobind, noop} from '../util';
|
||||
import {PluginEvent, ResizeMoveEventContext} from '../plugin';
|
||||
import {EditorManager} from '../manager';
|
||||
import {isAlive} from 'mobx-state-tree';
|
||||
|
||||
export interface HighlightBoxProps {
|
||||
store: EditorStoreType;
|
||||
node: EditorNodeType;
|
||||
id: string;
|
||||
className?: string;
|
||||
title: string;
|
||||
toolbarContainer?: () => any;
|
||||
onSwitch?: (id: string) => void;
|
||||
manager: EditorManager;
|
||||
children?: React.ReactNode;
|
||||
readonly?: boolean;
|
||||
}
|
||||
|
||||
export default observer(function ({
|
||||
className,
|
||||
store,
|
||||
id,
|
||||
title,
|
||||
children,
|
||||
node,
|
||||
toolbarContainer,
|
||||
onSwitch,
|
||||
manager,
|
||||
readonly
|
||||
}: HighlightBoxProps) {
|
||||
const handleWResizerMouseDown = React.useCallback(
|
||||
(e: MouseEvent) => startResize(e, 'horizontal'),
|
||||
[]
|
||||
);
|
||||
|
||||
const handleHResizerMouseDown = React.useCallback(
|
||||
(e: MouseEvent) => startResize(e, 'vertical'),
|
||||
[]
|
||||
);
|
||||
|
||||
const handleResizerMouseDown = React.useCallback(
|
||||
(e: MouseEvent) => startResize(e, 'both'),
|
||||
[]
|
||||
);
|
||||
|
||||
const startResize = React.useCallback(
|
||||
(e: MouseEvent, direction: 'horizontal' | 'vertical' | 'both') => {
|
||||
const isLeftButton =
|
||||
(e.button === 1 && window.event !== null) || e.button === 0;
|
||||
if (!isLeftButton || e.defaultPrevented) return;
|
||||
|
||||
e.preventDefault();
|
||||
if (!node) {
|
||||
return;
|
||||
}
|
||||
|
||||
const target = document.querySelector(`[data-editor-id="${id}"]`);
|
||||
|
||||
if (!target) {
|
||||
return;
|
||||
}
|
||||
manager.disableHover = true;
|
||||
|
||||
const event = manager[
|
||||
direction === 'both'
|
||||
? 'onSizeChangeStart'
|
||||
: direction === 'vertical'
|
||||
? 'onHeightChangeStart'
|
||||
: 'onWidthChangeStart'
|
||||
](e, {
|
||||
dom: target as HTMLElement,
|
||||
node: node,
|
||||
store: store,
|
||||
resizer:
|
||||
direction === 'both'
|
||||
? resizerDom.current!
|
||||
: direction === 'vertical'
|
||||
? hResizerDom.current!
|
||||
: wResizerDom.current!
|
||||
}) as PluginEvent<
|
||||
ResizeMoveEventContext,
|
||||
{
|
||||
onMove(e: MouseEvent): void;
|
||||
onEnd(e: MouseEvent): void;
|
||||
}
|
||||
>;
|
||||
|
||||
const pluginOnMove = event.data?.onMove;
|
||||
const pluginonEnd = event.data?.onEnd;
|
||||
|
||||
if (!pluginOnMove && !pluginonEnd) {
|
||||
return;
|
||||
}
|
||||
mainRef.current?.setAttribute('data-resizing', '');
|
||||
|
||||
const onMove = (e: MouseEvent) => {
|
||||
e.preventDefault();
|
||||
pluginOnMove?.(e);
|
||||
};
|
||||
|
||||
const onUp = (e: MouseEvent) => {
|
||||
e.preventDefault();
|
||||
manager.disableHover = false;
|
||||
mainRef.current?.removeAttribute('data-resizing');
|
||||
window.removeEventListener('mousemove', onMove);
|
||||
window.removeEventListener('mouseup', onUp);
|
||||
document.body.style.cursor = 'default';
|
||||
|
||||
// 阻止 click 事件触发。
|
||||
let captureClick = (e: MouseEvent) => {
|
||||
window.removeEventListener('click', captureClick, true);
|
||||
e.preventDefault();
|
||||
e.stopPropagation();
|
||||
};
|
||||
window.addEventListener('click', captureClick, true);
|
||||
setTimeout(
|
||||
() => window.removeEventListener('click', captureClick, true),
|
||||
350
|
||||
);
|
||||
|
||||
pluginonEnd?.(e);
|
||||
};
|
||||
|
||||
window.addEventListener('mousemove', onMove);
|
||||
window.addEventListener('mouseup', onUp);
|
||||
document.body.style.cursor =
|
||||
direction === 'both'
|
||||
? 'nwse-resize'
|
||||
: direction === 'vertical'
|
||||
? 'ns-resize'
|
||||
: 'ew-resize';
|
||||
},
|
||||
[]
|
||||
);
|
||||
const wResizerDom = React.useRef<HTMLElement>();
|
||||
const wResizerRef = React.useCallback((ref: HTMLElement) => {
|
||||
if (ref) {
|
||||
ref.addEventListener('mousedown', handleWResizerMouseDown);
|
||||
} else {
|
||||
wResizerDom.current?.removeEventListener(
|
||||
'mousedown',
|
||||
handleWResizerMouseDown
|
||||
);
|
||||
}
|
||||
|
||||
wResizerDom.current = ref;
|
||||
}, []);
|
||||
|
||||
const hResizerDom = React.useRef<HTMLElement>();
|
||||
const hResizerRef = React.useCallback((ref: HTMLElement) => {
|
||||
if (ref) {
|
||||
ref.addEventListener('mousedown', handleHResizerMouseDown);
|
||||
} else {
|
||||
hResizerDom.current?.removeEventListener(
|
||||
'mousedown',
|
||||
handleHResizerMouseDown
|
||||
);
|
||||
}
|
||||
|
||||
hResizerDom.current = ref;
|
||||
}, []);
|
||||
|
||||
const resizerDom = React.useRef<HTMLElement>();
|
||||
const resizerRef = React.useCallback((ref: HTMLElement) => {
|
||||
if (ref) {
|
||||
ref.addEventListener('mousedown', handleResizerMouseDown);
|
||||
} else {
|
||||
resizerDom.current?.removeEventListener(
|
||||
'mousedown',
|
||||
handleResizerMouseDown
|
||||
);
|
||||
}
|
||||
|
||||
resizerDom.current = ref;
|
||||
}, []);
|
||||
|
||||
const handleMouseEnter = React.useCallback(() => {
|
||||
if (manager.disableHover) {
|
||||
return;
|
||||
}
|
||||
|
||||
store.setHoverId(id);
|
||||
}, []);
|
||||
|
||||
const handleDragStart = React.useCallback((e: React.DragEvent) => {
|
||||
if (manager.disableHover) {
|
||||
return;
|
||||
}
|
||||
|
||||
manager.startDrag(id, e);
|
||||
}, []);
|
||||
|
||||
const mainRef = React.createRef<HTMLDivElement>();
|
||||
const toolbars = store.sortedToolbars;
|
||||
const secondaryToolbars = store.sortedSecondaryToolbars;
|
||||
const specialToolbars = store.sortedSpecialToolbars;
|
||||
const isActive = store.isActive(id);
|
||||
const curFreeContainerId = store.parentIsFreeContainer();
|
||||
const isHover =
|
||||
store.isHoved(id) ||
|
||||
store.dropId === id ||
|
||||
store.insertOrigId === id ||
|
||||
curFreeContainerId === id;
|
||||
const isDraggableContainer = store.draggableContainer(id);
|
||||
|
||||
// todo 干掉这个逻辑
|
||||
// 获取当前高亮画布宽度
|
||||
const aePreviewOffsetWidth = document.getElementById(
|
||||
'aePreviewHighlightBox'
|
||||
)!.offsetWidth;
|
||||
|
||||
if (!isAlive(node)) {
|
||||
return <div />;
|
||||
}
|
||||
|
||||
// 判断是否在最右侧(考虑组件头部工具栏被遮挡的问题)
|
||||
const isRightElem = aePreviewOffsetWidth - node.x < 176; // 跳过icode代码检查
|
||||
const isSelected =
|
||||
(isActive && !store.activeElement) || ~store.selections.indexOf(id);
|
||||
|
||||
React.useLayoutEffect(() => {
|
||||
if (!node.draggable || !isSelected || !isAlive(node)) {
|
||||
return;
|
||||
}
|
||||
const dom = node.getTarget() as HTMLElement;
|
||||
const targets = Array.isArray(dom) ? dom : dom ? [dom] : [];
|
||||
targets.forEach(item => {
|
||||
item.setAttribute('draggable', 'true');
|
||||
item.addEventListener('dragstart', handleDragStart as any);
|
||||
});
|
||||
return () => {
|
||||
targets.forEach(item => {
|
||||
item.removeAttribute('draggable');
|
||||
item.removeEventListener('dragstart', handleDragStart as any);
|
||||
});
|
||||
};
|
||||
}, [node.draggable, isSelected]);
|
||||
|
||||
/* bca-disable */
|
||||
return (
|
||||
<div
|
||||
className={cx(
|
||||
'ae-Editor-hlbox',
|
||||
{
|
||||
shake: id === store.insertOrigId,
|
||||
focused: store.activeElement && isActive,
|
||||
selected: isSelected,
|
||||
hover: isHover,
|
||||
regionOn: node.childRegions.some(region =>
|
||||
store.isRegionHighlighted(region.id, region.region)
|
||||
),
|
||||
isFreeContainerElem: !!curFreeContainerId || isDraggableContainer
|
||||
},
|
||||
className
|
||||
)}
|
||||
data-hlbox-id={id}
|
||||
style={{
|
||||
display:
|
||||
node.w && node.h && !node.info.plugin.notHighlight ? 'block' : 'none',
|
||||
top: node.y,
|
||||
left: node.x,
|
||||
width: node.w,
|
||||
height: node.h
|
||||
}}
|
||||
ref={mainRef}
|
||||
onMouseEnter={handleMouseEnter}
|
||||
draggable={node.draggable}
|
||||
onDragStart={handleDragStart}
|
||||
>
|
||||
{isActive && !store.activeElement && !readonly ? (
|
||||
<div
|
||||
className={`ae-Editor-toolbarPopover ${
|
||||
isRightElem ? 'is-right-elem' : ''
|
||||
}`}
|
||||
onClick={e => e.stopPropagation()}
|
||||
>
|
||||
<div className="ae-Editor-nav">
|
||||
{node.host ? (
|
||||
<div
|
||||
className="ae-Editor-tip parent"
|
||||
onClick={() => onSwitch?.(node.host.id)}
|
||||
>
|
||||
{node.host.label}
|
||||
</div>
|
||||
) : null}
|
||||
|
||||
<div key="tip" className="ae-Editor-tip current">
|
||||
{title}
|
||||
</div>
|
||||
|
||||
{node.firstChild ? (
|
||||
<div
|
||||
className="ae-Editor-tip child"
|
||||
onClick={() => onSwitch?.(node.firstChild.id)}
|
||||
>
|
||||
{node.firstChild.label}
|
||||
</div>
|
||||
) : null}
|
||||
</div>
|
||||
|
||||
<div
|
||||
className={cx('ae-Editor-toolbar', {
|
||||
invisible: toolbars.length === 0
|
||||
})}
|
||||
key="toolbar"
|
||||
>
|
||||
{toolbars.map(item => (
|
||||
<button
|
||||
key={item.id}
|
||||
type="button"
|
||||
draggable={item.draggable}
|
||||
onDragStart={item.onDragStart}
|
||||
data-id={item.id}
|
||||
data-tooltip={item.tooltip || undefined}
|
||||
data-position={item.placement || 'top'}
|
||||
onClick={item.onClick}
|
||||
>
|
||||
{item.iconSvg ? (
|
||||
<Icon className="icon" icon={item.iconSvg} />
|
||||
) : ~item.icon!.indexOf('<') ? (
|
||||
<span dangerouslySetInnerHTML={{__html: item.icon!}} />
|
||||
) : (
|
||||
<i className={item.icon} />
|
||||
)}
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
) : null}
|
||||
|
||||
{isActive && secondaryToolbars.length ? (
|
||||
<div className="ae-Editor-toolbar sencondary" key="sencondary-toolbar">
|
||||
{secondaryToolbars.map(item => (
|
||||
<button
|
||||
key={item.id}
|
||||
type="button"
|
||||
className={item.className}
|
||||
data-id={item.id}
|
||||
data-tooltip={item.tooltip || undefined}
|
||||
data-position={item.placement || 'top'}
|
||||
onClick={item.onClick}
|
||||
>
|
||||
{item.iconSvg ? (
|
||||
<Icon className="icon" icon={item.iconSvg} />
|
||||
) : ~item.icon!.indexOf('<') ? (
|
||||
<span dangerouslySetInnerHTML={{__html: item.icon!}} />
|
||||
) : (
|
||||
<i className={item.icon} />
|
||||
)}
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
) : null}
|
||||
|
||||
{isActive && specialToolbars.length ? (
|
||||
<div className="ae-Editor-toolbar special" key="special-toolbar">
|
||||
{specialToolbars.map(item => (
|
||||
<button
|
||||
key={item.id}
|
||||
type="button"
|
||||
className={item.className}
|
||||
data-id={item.id}
|
||||
data-tooltip={item.tooltip || undefined}
|
||||
data-position={item.placement || 'top'}
|
||||
onClick={item.onClick}
|
||||
>
|
||||
{item.iconSvg ? (
|
||||
<Icon className="icon" icon={item.iconSvg} />
|
||||
) : ~item.icon!.indexOf('<') ? (
|
||||
<span dangerouslySetInnerHTML={{__html: item.icon!}} />
|
||||
) : (
|
||||
<i className={item.icon} />
|
||||
)}
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
) : null}
|
||||
|
||||
{children}
|
||||
|
||||
{node.widthMutable ? (
|
||||
<>
|
||||
<span className="ae-border-WResizer" ref={wResizerRef}></span>
|
||||
<span className="ae-WResizer" ref={wResizerRef}></span>
|
||||
</>
|
||||
) : null}
|
||||
|
||||
{node.heightMutable ? (
|
||||
<>
|
||||
<span className="ae-border-HResizer" ref={hResizerRef}></span>
|
||||
<span className="ae-HResizer" ref={hResizerRef}></span>
|
||||
</>
|
||||
) : null}
|
||||
|
||||
{node.widthMutable && node.heightMutable ? (
|
||||
<span className="ae-Resizer" ref={resizerRef}></span>
|
||||
) : null}
|
||||
</div>
|
||||
);
|
||||
});
|
||||
358
packages/amis-editor-core/src/component/IFramePreview.tsx
Normal file
@@ -0,0 +1,358 @@
|
||||
import {observer} from 'mobx-react';
|
||||
import {EditorStoreType} from '../store/editor';
|
||||
import React, {memo} from 'react';
|
||||
import {EditorManager} from '../manager';
|
||||
import Frame, {useFrame} from 'react-frame-component';
|
||||
import {SchemaRenderer} from './SchemaRenderer';
|
||||
import {
|
||||
autobind,
|
||||
closeContextMenus,
|
||||
findTree,
|
||||
render,
|
||||
resizeSensor
|
||||
} from 'amis';
|
||||
import {isAlive} from 'mobx-state-tree';
|
||||
|
||||
/**
|
||||
* 这个用了 observer,所以能最小程度的刷新,数据不变按理是不会刷新的。
|
||||
*/
|
||||
export interface IFramePreviewProps {
|
||||
editable?: boolean;
|
||||
autoFocus?: boolean;
|
||||
store: EditorStoreType;
|
||||
env: any;
|
||||
data?: any;
|
||||
manager: EditorManager;
|
||||
/** 应用语言类型 */
|
||||
appLocale?: string;
|
||||
}
|
||||
@observer
|
||||
export default class IFramePreview extends React.Component<IFramePreviewProps> {
|
||||
initialContent: string = '';
|
||||
dialogMountRef: React.RefObject<HTMLDivElement> = React.createRef();
|
||||
iframeRef: HTMLIFrameElement;
|
||||
constructor(props: IFramePreviewProps) {
|
||||
super(props);
|
||||
|
||||
const styles = [].slice
|
||||
.call(document.querySelectorAll('link[rel="stylesheet"], style'))
|
||||
.map((el: any) => {
|
||||
return el.outerHTML;
|
||||
});
|
||||
styles.push(`<style>
|
||||
html, body, .ae-IFramePreview, .ae-IFramePreview > .frame-content, .ae-PageWrapper {
|
||||
position: relative;
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
}
|
||||
html::-webkit-scrollbar,
|
||||
body::-webkit-scrollbar {
|
||||
display: none;
|
||||
}
|
||||
</style>`);
|
||||
|
||||
this.initialContent = `<!DOCTYPE html><html><head>${styles.join(
|
||||
''
|
||||
)}</head><body><div class="ae-IFramePreview AMISCSSWrapper"></div></body></html>`;
|
||||
}
|
||||
|
||||
componentDidMount() {
|
||||
if (this.props.autoFocus) {
|
||||
// 一般弹框动画差不多 350ms
|
||||
// 延时 350ms,在弹框中展示编辑器效果要好点。
|
||||
const store = this.props.manager.store;
|
||||
setTimeout(() => {
|
||||
if (isAlive(store)) {
|
||||
const first = findTree(
|
||||
store.outline,
|
||||
item => !item.isRegion && item.clickable
|
||||
);
|
||||
|
||||
first && store.setActiveId(first.id);
|
||||
}
|
||||
}, 350);
|
||||
} else {
|
||||
this.props.manager.buildRenderersAndPanels();
|
||||
}
|
||||
}
|
||||
|
||||
@autobind
|
||||
iframeRefFunc(iframe: any) {
|
||||
const store = this.props.store;
|
||||
this.iframeRef = iframe;
|
||||
isAlive(store) && store.setIframe(iframe);
|
||||
}
|
||||
|
||||
@autobind
|
||||
getModalContainer() {
|
||||
const store = this.props.store;
|
||||
return store.getDoc().body;
|
||||
}
|
||||
|
||||
@autobind
|
||||
isMobile() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@autobind
|
||||
getDialogMountRef() {
|
||||
return this.dialogMountRef.current;
|
||||
}
|
||||
|
||||
@autobind
|
||||
iframeContentDidMount() {
|
||||
const body = this.iframeRef.contentWindow?.document.body;
|
||||
body?.classList.add('ae-PreviewIFrameBody');
|
||||
}
|
||||
|
||||
render() {
|
||||
const {editable, store, appLocale, autoFocus, env, data, manager, ...rest} =
|
||||
this.props;
|
||||
|
||||
return (
|
||||
<Frame
|
||||
className={'ae-PreviewIFrame'}
|
||||
initialContent={this.initialContent}
|
||||
ref={this.iframeRefFunc}
|
||||
contentDidMount={this.iframeContentDidMount}
|
||||
>
|
||||
<InnerComponent store={store} editable={editable} manager={manager} />
|
||||
<div ref={this.dialogMountRef} className="ae-PageWrapper">
|
||||
{render(
|
||||
editable ? store.filteredSchema : store.filteredSchemaForPreview,
|
||||
{
|
||||
globalVars: store.globalVariables,
|
||||
...rest,
|
||||
key: editable ? 'edit-mode' : 'preview-mode',
|
||||
theme: env.theme,
|
||||
data: data,
|
||||
context: store.ctx,
|
||||
locale: appLocale,
|
||||
editorDialogMountNode: this.getDialogMountRef
|
||||
},
|
||||
{
|
||||
...env,
|
||||
session: `${env.session}-${
|
||||
editable ? 'edit' : 'preview'
|
||||
}-iframe-preview`,
|
||||
SchemaRenderer: editable ? SchemaRenderer : undefined,
|
||||
useMobileUI: true,
|
||||
isMobile: this.isMobile,
|
||||
getModalContainer: this.getModalContainer
|
||||
}
|
||||
)}
|
||||
<InnerSvgSpirit />
|
||||
</div>
|
||||
</Frame>
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
function InnerComponent({
|
||||
store,
|
||||
editable,
|
||||
manager
|
||||
}: {
|
||||
store: EditorStoreType;
|
||||
editable?: boolean;
|
||||
manager: EditorManager;
|
||||
}) {
|
||||
// Hook returns iframe's window and document instances from Frame context
|
||||
const {document: doc, window: win} = useFrame();
|
||||
const editableRef = React.useRef(editable);
|
||||
|
||||
const handleMouseLeave = React.useCallback(() => {
|
||||
store.setHoverId('');
|
||||
}, []);
|
||||
|
||||
const handleMouseMove = React.useCallback((e: MouseEvent) => {
|
||||
const dom = e.target as HTMLElement;
|
||||
const target = dom.closest(`[data-editor-id]`);
|
||||
|
||||
if (target) {
|
||||
store.setHoverId(target.getAttribute('data-editor-id')!);
|
||||
}
|
||||
}, []);
|
||||
|
||||
const handleBodyClick = React.useCallback(() => {
|
||||
closeContextMenus();
|
||||
}, []);
|
||||
|
||||
const handleClick = React.useCallback((e: MouseEvent) => {
|
||||
const target = (e.target as HTMLElement).closest(`[data-editor-id]`);
|
||||
closeContextMenus();
|
||||
|
||||
if (e.defaultPrevented) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (store.activeElement) {
|
||||
// 禁用内部的点击事件
|
||||
e.preventDefault();
|
||||
return;
|
||||
}
|
||||
|
||||
if (target) {
|
||||
store.setActiveId(target.getAttribute('data-editor-id')!);
|
||||
}
|
||||
|
||||
if (editableRef.current) {
|
||||
// 让渲染器不可点,只能点击选中。
|
||||
const event = manager.trigger('prevent-click', {
|
||||
data: e
|
||||
});
|
||||
|
||||
if (!event.prevented && !event.stoped) {
|
||||
e.preventDefault();
|
||||
e.stopPropagation();
|
||||
}
|
||||
}
|
||||
}, []);
|
||||
|
||||
const handleDBClick = React.useCallback((e: MouseEvent) => {
|
||||
const target = e.target as HTMLElement;
|
||||
const hostElem = target.closest(`[data-editor-id]`) as HTMLElement;
|
||||
if (hostElem) {
|
||||
const node = store.getNodeById(hostElem.getAttribute('data-editor-id')!);
|
||||
if (!node) {
|
||||
return;
|
||||
}
|
||||
|
||||
const rendererInfo = node.info;
|
||||
|
||||
// 需要支持 :scope > xxx 语法,所以才这么写
|
||||
let inlineElem: HTMLElement | undefined | null = null;
|
||||
const inlineSetting = (rendererInfo.inlineEditableElements || []).find(
|
||||
elem => {
|
||||
inlineElem = (
|
||||
[].slice.call(
|
||||
hostElem.querySelectorAll(elem.match)
|
||||
) as Array<HTMLElement>
|
||||
).find(dom => dom.contains(target));
|
||||
return !!inlineElem;
|
||||
}
|
||||
)!;
|
||||
|
||||
// 如果命中了支持内联编辑的元素,则开始内联编辑
|
||||
if (inlineElem && inlineSetting) {
|
||||
manager.startInlineEdit(node, inlineElem, inlineSetting, e);
|
||||
}
|
||||
}
|
||||
}, []);
|
||||
|
||||
const handeMouseOver = React.useCallback((e: MouseEvent) => {
|
||||
if (editableRef.current) {
|
||||
e.preventDefault();
|
||||
e.stopPropagation();
|
||||
}
|
||||
}, []);
|
||||
|
||||
// 禁用内部的提交事件
|
||||
const handleSubmit = React.useCallback((e: Event) => {
|
||||
e.preventDefault();
|
||||
e.stopPropagation();
|
||||
}, []);
|
||||
|
||||
const handleDragEnter = React.useCallback((e: DragEvent) => {
|
||||
if (!editable) {
|
||||
return;
|
||||
}
|
||||
e.stopPropagation();
|
||||
manager.dnd.dragEnter(e);
|
||||
}, []);
|
||||
|
||||
const handleDragLeave = React.useCallback((e: DragEvent) => {
|
||||
if (!editable) {
|
||||
return;
|
||||
}
|
||||
e.stopPropagation();
|
||||
manager.dnd.dragLeave(e);
|
||||
}, []);
|
||||
|
||||
const handleDragOver = React.useCallback((e: DragEvent) => {
|
||||
if (!editable) {
|
||||
return;
|
||||
}
|
||||
e.stopPropagation();
|
||||
manager.dnd.dragOver(e);
|
||||
}, []);
|
||||
|
||||
const handleDrop = React.useCallback((e: DragEvent) => {
|
||||
if (!editable) {
|
||||
return;
|
||||
}
|
||||
e.stopPropagation();
|
||||
manager.dnd.drop(e);
|
||||
}, []);
|
||||
|
||||
React.useEffect(() => {
|
||||
store.setDoc(doc);
|
||||
const layer = doc?.querySelector('.frame-content') as HTMLElement;
|
||||
|
||||
doc!.addEventListener('click', handleBodyClick);
|
||||
layer!.addEventListener('mouseleave', handleMouseLeave);
|
||||
layer!.addEventListener('mousemove', handleMouseMove);
|
||||
layer!.addEventListener('click', handleClick, true);
|
||||
layer!.addEventListener('dblclick', handleDBClick);
|
||||
layer!.addEventListener('mouseover', handeMouseOver);
|
||||
layer!.addEventListener('submit', handleSubmit);
|
||||
layer!.addEventListener('dragenter', handleDragEnter);
|
||||
layer!.addEventListener('dragleave', handleDragLeave);
|
||||
layer!.addEventListener('dragover', handleDragOver);
|
||||
layer!.addEventListener('drop', handleDrop);
|
||||
|
||||
const widgetsLayer = store.getLayer();
|
||||
const handleScroll = () => {
|
||||
widgetsLayer?.classList.add('is-scrolling');
|
||||
store.calculateHighlightBox(store.highlightNodes.map(item => item.id));
|
||||
};
|
||||
const handleScrollEnd = () => {
|
||||
widgetsLayer?.classList.remove('is-scrolling');
|
||||
};
|
||||
|
||||
win?.addEventListener('scroll', handleScroll, true);
|
||||
win?.addEventListener('scrollend', handleScrollEnd, true);
|
||||
|
||||
return () => {
|
||||
win?.removeEventListener('scroll', handleScroll, true);
|
||||
win?.removeEventListener('scrollend', handleScrollEnd, true);
|
||||
doc!.removeEventListener('click', handleBodyClick);
|
||||
layer!.removeEventListener('mouseleave', handleMouseLeave);
|
||||
layer!.removeEventListener('mousemove', handleMouseMove);
|
||||
layer!.removeEventListener('click', handleClick);
|
||||
layer!.removeEventListener('mouseover', handeMouseOver);
|
||||
layer!.removeEventListener('dblclick', handleDBClick);
|
||||
layer!.removeEventListener('submit', handleSubmit);
|
||||
layer!.removeEventListener('dragenter', handleDragEnter);
|
||||
layer!.removeEventListener('dragleave', handleDragLeave);
|
||||
layer!.removeEventListener('dragover', handleDragOver);
|
||||
layer!.removeEventListener('drop', handleDrop);
|
||||
store.setDoc(document);
|
||||
};
|
||||
}, [doc]);
|
||||
|
||||
React.useEffect(() => {
|
||||
doc
|
||||
?.querySelector('body>div:first-child')
|
||||
?.classList.toggle('is-edting', editable);
|
||||
editableRef.current = editable;
|
||||
}, [editable]);
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
const InnerSvgSpirit = memo(() => {
|
||||
// @ts-ignore 这里取的是平台的变量
|
||||
let spiriteIcons = window.spiriteIcons;
|
||||
if (spiriteIcons) {
|
||||
return (
|
||||
<div
|
||||
id="amis-icon-manage-mount-node"
|
||||
style={{display: 'none'}}
|
||||
dangerouslySetInnerHTML={{__html: spiriteIcons}}
|
||||
></div>
|
||||
);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
});
|
||||
70
packages/amis-editor-core/src/component/MiniEditor.tsx
Normal file
@@ -0,0 +1,70 @@
|
||||
import React from 'react';
|
||||
import Editor, {EditorProps} from './Editor';
|
||||
import cx from 'classnames';
|
||||
import Preview from './Preview';
|
||||
import {SubEditor} from './SubEditor';
|
||||
import {ScaffoldModal} from './ScaffoldModal';
|
||||
import {autobind} from '../util';
|
||||
import {BaseEventContext, BasicPanelItem, PluginEvent} from '../plugin';
|
||||
|
||||
export default class MiniEditor extends Editor {
|
||||
constructor(props: EditorProps) {
|
||||
super(props);
|
||||
this.manager.on('build-panels', this.buildPanels);
|
||||
}
|
||||
|
||||
componentWillUnmount() {
|
||||
this.manager.off('build-panels', this.buildPanels);
|
||||
}
|
||||
|
||||
@autobind
|
||||
buildPanels(event: PluginEvent<BaseEventContext>) {
|
||||
const panels: Array<BasicPanelItem> = event.context.data!;
|
||||
|
||||
// todo, 以后干脆不要收集。
|
||||
// 因为 MiniEditor 不显示面板,所以把搜集的所有面板都干掉。
|
||||
if (Array.isArray(panels)) {
|
||||
panels.splice(0, panels.length);
|
||||
}
|
||||
}
|
||||
|
||||
render() {
|
||||
const {preview, className, theme, data, isMobile, autoFocus, previewProps} =
|
||||
this.props;
|
||||
|
||||
return (
|
||||
<div
|
||||
className={cx(
|
||||
'ae-Editor',
|
||||
{
|
||||
preview: preview
|
||||
},
|
||||
className
|
||||
)}
|
||||
>
|
||||
<div className="ae-Editor-inner" onContextMenu={this.handleContextMenu}>
|
||||
<div className="ae-Main">
|
||||
<Preview
|
||||
{...previewProps}
|
||||
isMobile={isMobile}
|
||||
editable={!preview}
|
||||
store={this.store}
|
||||
manager={this.manager}
|
||||
theme={theme}
|
||||
data={data}
|
||||
autoFocus={autoFocus}
|
||||
appLocale={this.props.appLocale}
|
||||
></Preview>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<SubEditor store={this.store} manager={this.manager} theme={theme} />
|
||||
<ScaffoldModal
|
||||
store={this.store}
|
||||
manager={this.manager}
|
||||
theme={theme}
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
}
|
||||
161
packages/amis-editor-core/src/component/ModalForm.tsx
Normal file
@@ -0,0 +1,161 @@
|
||||
import React from 'react';
|
||||
import {EditorManager} from '../manager';
|
||||
import {EditorStoreType} from '../store/editor';
|
||||
import {
|
||||
render,
|
||||
Modal,
|
||||
getTheme,
|
||||
Icon,
|
||||
Spinner,
|
||||
Button,
|
||||
Overlay,
|
||||
PopOver,
|
||||
Drawer
|
||||
} from 'amis';
|
||||
import {observer} from 'mobx-react';
|
||||
import {diff} from '../util';
|
||||
import {autobind, createObject} from 'amis-core';
|
||||
|
||||
export interface ModalFormProps {
|
||||
store: EditorStoreType;
|
||||
manager: EditorManager;
|
||||
theme?: string;
|
||||
}
|
||||
|
||||
@observer
|
||||
export class ModalForm extends React.Component<ModalFormProps> {
|
||||
@autobind
|
||||
async handleConfirmClick() {
|
||||
const form = this.amisScope?.getComponents()[0];
|
||||
|
||||
if (!form) {
|
||||
return;
|
||||
}
|
||||
const {store} = this.props;
|
||||
const modalFormContext = store.modalForm!;
|
||||
|
||||
try {
|
||||
store.markModalFormBuzy(true);
|
||||
const newValue = await form.doAction(
|
||||
{
|
||||
type: 'submit'
|
||||
},
|
||||
form.props.data,
|
||||
true
|
||||
);
|
||||
|
||||
modalFormContext.callback?.(
|
||||
newValue,
|
||||
diff(modalFormContext.value, newValue)
|
||||
);
|
||||
store.closeModalForm();
|
||||
} catch (e) {
|
||||
console.error(e.stack);
|
||||
store.setModalFormError(e.message);
|
||||
}
|
||||
|
||||
store.markModalFormBuzy(false);
|
||||
}
|
||||
|
||||
amisScope: any;
|
||||
@autobind
|
||||
scopeRef(scoped: any) {
|
||||
this.amisScope = scoped;
|
||||
}
|
||||
|
||||
buildSchema() {
|
||||
const {store} = this.props;
|
||||
const modalFormContext = store.modalForm!;
|
||||
|
||||
return {
|
||||
type: 'form',
|
||||
wrapWithPanel: false,
|
||||
mode: 'normal',
|
||||
wrapperComponent: 'div',
|
||||
initApi: modalFormContext.initApi,
|
||||
api: modalFormContext.api,
|
||||
body: modalFormContext.body,
|
||||
submitOnChange: false,
|
||||
autoFocus: true
|
||||
};
|
||||
}
|
||||
|
||||
render() {
|
||||
const {store, theme, manager} = this.props;
|
||||
const modalFormContext = store.modalForm;
|
||||
const modalMode = store.modalMode || 'dialog';
|
||||
const contents = modalFormContext
|
||||
? render(
|
||||
this.buildSchema(),
|
||||
{
|
||||
data: createObject(store.ctx, modalFormContext?.value),
|
||||
manager,
|
||||
scopeRef: this.scopeRef
|
||||
},
|
||||
{
|
||||
...manager.env,
|
||||
session: 'modal-form',
|
||||
theme: theme
|
||||
}
|
||||
)
|
||||
: null;
|
||||
|
||||
return modalMode === 'drawer' ? (
|
||||
<Drawer
|
||||
position={(modalFormContext?.postion as any) || 'left'}
|
||||
size={modalFormContext?.size || 'md'}
|
||||
theme={theme}
|
||||
show={!!modalFormContext}
|
||||
onHide={store.closeModalForm}
|
||||
>
|
||||
<div className="cxd-Drawer-header">{modalFormContext?.title}</div>
|
||||
<div className="cxd-Drawer-body">{contents}</div>
|
||||
<div className="cxd-Drawer-footer">
|
||||
<div className="cxd-Drawer-info">
|
||||
{store.modalFormError ? (
|
||||
<div className="cxd-Drawer-error">{store.modalFormError}</div>
|
||||
) : null}
|
||||
</div>
|
||||
<Button
|
||||
disabled={store.modalFormBuzy}
|
||||
level="primary"
|
||||
onClick={this.handleConfirmClick}
|
||||
>
|
||||
确认
|
||||
</Button>
|
||||
<Button onClick={store.closeModalForm}>取消</Button>
|
||||
</div>
|
||||
</Drawer>
|
||||
) : (
|
||||
<Modal
|
||||
theme={theme}
|
||||
size={modalFormContext?.size || 'md'}
|
||||
show={!!modalFormContext}
|
||||
onHide={store.closeModalForm}
|
||||
closeOnEsc={false}
|
||||
>
|
||||
{modalFormContext?.title ? (
|
||||
<Modal.Header onClose={store.closeModalForm}>
|
||||
{modalFormContext.title}
|
||||
</Modal.Header>
|
||||
) : null}
|
||||
<Modal.Body>{contents}</Modal.Body>
|
||||
<Modal.Footer>
|
||||
<div className="cxd-Dialog-info">
|
||||
{store.modalFormError ? (
|
||||
<div className="cxd-Dialog-error">{store.modalFormError}</div>
|
||||
) : null}
|
||||
</div>
|
||||
<Button
|
||||
disabled={store.modalFormBuzy}
|
||||
level="primary"
|
||||
onClick={this.handleConfirmClick}
|
||||
>
|
||||
确认
|
||||
</Button>
|
||||
<Button onClick={store.closeModalForm}>取消</Button>
|
||||
</Modal.Footer>
|
||||
</Modal>
|
||||
);
|
||||
}
|
||||
}
|
||||
137
packages/amis-editor-core/src/component/NodeWrapper.tsx
Normal file
@@ -0,0 +1,137 @@
|
||||
import {RendererProps, isObject} from 'amis-core';
|
||||
import {observer} from 'mobx-react';
|
||||
import {isAlive} from 'mobx-state-tree';
|
||||
import React from 'react';
|
||||
import {findDOMNode} from 'react-dom';
|
||||
import merge from 'lodash/merge';
|
||||
import omit from 'lodash/omit';
|
||||
import {RendererInfo} from '../plugin';
|
||||
import {EditorNodeType} from '../store/node';
|
||||
import {autobind, isEmpty} from '../util';
|
||||
import {filter} from 'amis-core';
|
||||
|
||||
export interface NodeWrapperProps extends RendererProps {
|
||||
$$editor: RendererInfo; // 当前节点信息(info)
|
||||
$$node?: EditorNodeType; // 虚拟dom节点信息
|
||||
}
|
||||
|
||||
@observer
|
||||
export class NodeWrapper extends React.Component<NodeWrapperProps> {
|
||||
/** 合并 Mock 配置时应该忽略的属性 */
|
||||
omitMockProps = ['id', '$$id', 'enable', 'maxDisplayRows'];
|
||||
|
||||
componentDidMount() {
|
||||
this.markDom(this.props.$$editor.id);
|
||||
|
||||
// 稍微等会,因为有可能别的 node 还没更新完成。
|
||||
const node = this.props.$$node;
|
||||
node &&
|
||||
requestAnimationFrame(() => {
|
||||
() => isAlive(node) && node.calculateHighlightBox();
|
||||
});
|
||||
}
|
||||
|
||||
componentDidUpdate(prevProps: NodeWrapperProps) {
|
||||
this.markDom(this.props.$$editor.id);
|
||||
}
|
||||
|
||||
ref: any;
|
||||
getWrappedInstance() {
|
||||
return this.ref;
|
||||
}
|
||||
|
||||
@autobind
|
||||
refFn(ref: any) {
|
||||
this.ref = ref;
|
||||
}
|
||||
|
||||
/**
|
||||
* 弄点标记
|
||||
*/
|
||||
markDom(id: string) {
|
||||
const root = findDOMNode(this) as HTMLElement;
|
||||
|
||||
if (!root || !id) {
|
||||
return;
|
||||
}
|
||||
|
||||
const info = this.props.$$editor;
|
||||
let visible =
|
||||
this.props.$$visible !== false && this.props.$$hidden !== true;
|
||||
|
||||
if (visible) {
|
||||
const schema = this.props.$schema;
|
||||
const data = this.props.data;
|
||||
const id = filter(schema.id, data);
|
||||
const name = filter(schema.name, data);
|
||||
const states = (this.props.statusStore as any).raw.visibleState as any;
|
||||
|
||||
if ((states[id] ?? states[name]) === false) {
|
||||
visible = false;
|
||||
}
|
||||
}
|
||||
|
||||
let dom = info.wrapperResolve ? info.wrapperResolve(root) : root;
|
||||
(Array.isArray(dom) ? dom : dom ? [dom] : []).forEach(dom => {
|
||||
dom.setAttribute('data-editor-id', id);
|
||||
dom.setAttribute('name', this.props.id);
|
||||
dom.setAttribute('data-visible', visible ? '' : 'false');
|
||||
dom.setAttribute('data-hide-text', visible ? '' : '<隐藏状态>');
|
||||
|
||||
if (info.regions) {
|
||||
dom.setAttribute('data-container', '');
|
||||
} else {
|
||||
dom.removeAttribute('data-container');
|
||||
}
|
||||
});
|
||||
info.plugin?.markDom?.(dom, this.props);
|
||||
}
|
||||
|
||||
render() {
|
||||
// store 可能有循环引用,不能调用 JSONPipeOut
|
||||
let {$$editor, $$node, store, ...rest} = this.props;
|
||||
const renderer = $$editor.renderer;
|
||||
|
||||
if ($$editor.filterProps) {
|
||||
rest = $$editor.filterProps.call($$editor.plugin, rest, $$node);
|
||||
}
|
||||
|
||||
const mockProps = omit(rest?.editorSetting?.mock, this.omitMockProps);
|
||||
|
||||
// 自动合并假数据
|
||||
if (isObject(mockProps) && !isEmpty(mockProps)) {
|
||||
rest = merge(rest, mockProps);
|
||||
}
|
||||
|
||||
if ($$editor.renderRenderer) {
|
||||
return $$editor.renderRenderer.call(
|
||||
$$editor.plugin,
|
||||
{
|
||||
...rest,
|
||||
store,
|
||||
...$$node?.state,
|
||||
$$editor,
|
||||
...$$editor.wrapperProps,
|
||||
ref: this.refFn
|
||||
},
|
||||
$$editor
|
||||
);
|
||||
}
|
||||
const Component = renderer.component!;
|
||||
|
||||
const supportRef =
|
||||
Component.prototype?.isReactComponent ||
|
||||
(Component as any).$$typeof === Symbol.for('react.forward_ref');
|
||||
|
||||
return (
|
||||
<Component
|
||||
{...rest}
|
||||
store={store}
|
||||
{...$$node?.state}
|
||||
$$editor={$$editor}
|
||||
{...$$editor.wrapperProps}
|
||||
ref={supportRef ? this.refFn : undefined}
|
||||
/>
|
||||
);
|
||||
}
|
||||
}
|
||||
343
packages/amis-editor-core/src/component/Panel/AMisCodeEditor.tsx
Normal file
@@ -0,0 +1,343 @@
|
||||
import React from 'react';
|
||||
import {Editor} from 'amis-ui';
|
||||
import {
|
||||
isObjectShallowModified,
|
||||
guid,
|
||||
diff,
|
||||
filterSchemaForConfig
|
||||
} from '../../util';
|
||||
import cx from 'classnames';
|
||||
import {prompt, toast} from 'amis';
|
||||
import debounce from 'lodash/debounce';
|
||||
import isArray from 'lodash/isArray';
|
||||
import findIndex from 'lodash/findIndex';
|
||||
import {parse, stringify} from 'json-ast-comments';
|
||||
import isPlainObject from 'lodash/isPlainObject';
|
||||
|
||||
const internalSchema = /^\/schemas\/(.*).json$/;
|
||||
|
||||
async function buildSchema(
|
||||
schemaUrl: string,
|
||||
definition: string,
|
||||
fileUri: string,
|
||||
origin: any
|
||||
) {
|
||||
const schemas: Array<{
|
||||
uri: string;
|
||||
fileMatch?: Array<any>;
|
||||
schema: any;
|
||||
}> = Array.isArray(origin) ? origin.concat() : [];
|
||||
|
||||
// 开发环境直接读取本地 schema.json 文件。
|
||||
if (process.env.NODE_ENV !== 'production') {
|
||||
schemas.some(item => item.uri === schemaUrl) ||
|
||||
schemas.push({
|
||||
uri: schemaUrl,
|
||||
// @ts-ignore
|
||||
schema: await import('amis/schema.json').then(item => item.default)
|
||||
});
|
||||
}
|
||||
|
||||
if (internalSchema.test(definition)) {
|
||||
const rawName = RegExp.$1;
|
||||
const uri = `${schemaUrl.replace(
|
||||
/^(\w+\:\/\/[^\/]+)\/.*$/,
|
||||
'$1'
|
||||
)}/schemas/${rawName}.json`;
|
||||
|
||||
// 如果存在,先删掉
|
||||
const idx = findIndex(schemas, item => item.fileMatch?.[0] === fileUri);
|
||||
if (~idx) {
|
||||
schemas.splice(idx, 1);
|
||||
}
|
||||
|
||||
schemas.push({
|
||||
uri,
|
||||
fileMatch: [fileUri],
|
||||
schema: {
|
||||
$schema: 'http://json-schema.org/draft-07/schema#',
|
||||
$ref: `${schemaUrl}#/definitions/${rawName}`
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
return schemas;
|
||||
}
|
||||
|
||||
const codeErrorWarning = debounce(e => {
|
||||
toast.warning(`代码有误,错误的地方是\n ${e.toString().split('\n')[1]}`);
|
||||
}, 3000);
|
||||
|
||||
export interface AMisCodeEditorProps {
|
||||
value: any;
|
||||
onChange: (value: any, diff: any) => void;
|
||||
onPaste?: (e: any) => void;
|
||||
disabled?: boolean;
|
||||
$schemaUrl?: string;
|
||||
$schema?: string;
|
||||
className?: string;
|
||||
theme?: string;
|
||||
}
|
||||
|
||||
export default class AMisCodeEditor extends React.Component<AMisCodeEditorProps> {
|
||||
state = {
|
||||
wrongSchema: '',
|
||||
value: this.props.value,
|
||||
contents: this.obj2str(this.props.value, this.props)
|
||||
};
|
||||
lastResult: any;
|
||||
toDispose: Array<() => void> = [];
|
||||
editor: any;
|
||||
monaco: any;
|
||||
model: any;
|
||||
decorations: any;
|
||||
uri = `isuda://schema/${guid()}.json`;
|
||||
|
||||
componentDidUpdate(prevProps: AMisCodeEditorProps) {
|
||||
const props = this.props;
|
||||
|
||||
if (prevProps.$schema !== props.$schema && this.monaco) {
|
||||
this.changeJsonOptions(props);
|
||||
}
|
||||
|
||||
if (
|
||||
isObjectShallowModified(props.value, prevProps.value) &&
|
||||
isObjectShallowModified(props.value, this.lastResult)
|
||||
) {
|
||||
this.lastResult = null;
|
||||
|
||||
this.setState({
|
||||
value: props.value,
|
||||
contents: this.obj2str(props.value, props)
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
obj2str(value: any, props: AMisCodeEditorProps) {
|
||||
// 隐藏公共配置
|
||||
value = filterSchemaForConfig(value);
|
||||
|
||||
if (!isArray(value)) {
|
||||
value = {
|
||||
type: value?.type,
|
||||
...value
|
||||
};
|
||||
}
|
||||
|
||||
if (isArray(value)) {
|
||||
return stringify(value);
|
||||
} else if (!value.type && props.$schema?.match(/PageSchema/i)) {
|
||||
value.type = 'page';
|
||||
} else if (!value.type) {
|
||||
delete value.type;
|
||||
}
|
||||
|
||||
delete value.$schema;
|
||||
|
||||
return stringify(value);
|
||||
}
|
||||
|
||||
str2obj(str: string) {
|
||||
try {
|
||||
if (str === '') {
|
||||
return {};
|
||||
}
|
||||
const curObj = parse(str);
|
||||
if (codeErrorWarning) {
|
||||
// 此次代码转换成功后,立即取消上一次的错误提示(避免显示上一次的错误提示)
|
||||
codeErrorWarning.cancel();
|
||||
}
|
||||
return curObj;
|
||||
} catch (e) {
|
||||
codeErrorWarning(e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
emitChange = debounce(
|
||||
() => {
|
||||
const {onChange, value} = this.props;
|
||||
let ret: any = this.str2obj(this.state.contents);
|
||||
|
||||
if (!ret || (!isPlainObject(ret) && !isArray(ret))) {
|
||||
this.setState({
|
||||
wrongSchema: this.state.contents
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
this.setState({
|
||||
wrongSchema: ''
|
||||
});
|
||||
|
||||
delete ret.$schema;
|
||||
|
||||
// 补齐公共配置项目
|
||||
ret = filterSchemaForConfig(ret, this.props.value);
|
||||
const diffResult = diff(this.lastResult || value, ret);
|
||||
this.lastResult = ret;
|
||||
|
||||
onChange(ret, diffResult);
|
||||
},
|
||||
250,
|
||||
{
|
||||
trailing: true,
|
||||
leading: false
|
||||
}
|
||||
);
|
||||
|
||||
async changeJsonOptions(props: AMisCodeEditorProps = this.props) {
|
||||
const monaco = this.monaco;
|
||||
let schemaUrl =
|
||||
props.$schemaUrl ||
|
||||
`${window.location.protocol}//${window.location.host}/schema.json`;
|
||||
|
||||
if (schemaUrl.indexOf('/') === 0) {
|
||||
schemaUrl = `${window.location.protocol}//${window.location.host}${schemaUrl}`;
|
||||
}
|
||||
|
||||
const schemas = await buildSchema(
|
||||
schemaUrl,
|
||||
props.$schema!,
|
||||
monaco.Uri.parse(this.uri).toString(),
|
||||
monaco.languages.json?.jsonDefaults.diagnosticsOptions.schemas
|
||||
);
|
||||
|
||||
monaco.languages.json.jsonDefaults.setDiagnosticsOptions({
|
||||
schemas: schemas,
|
||||
validate: true,
|
||||
enableSchemaRequest: true,
|
||||
allowComments: true
|
||||
});
|
||||
}
|
||||
|
||||
editorFactory = (
|
||||
containerElement: HTMLElement,
|
||||
monaco: any,
|
||||
options: any
|
||||
) => {
|
||||
const modelUri = monaco.Uri.parse(this.uri);
|
||||
this.model = monaco.editor.createModel(
|
||||
this.state.contents,
|
||||
'json',
|
||||
modelUri
|
||||
);
|
||||
return monaco.editor.create(containerElement, {
|
||||
autoIndent: true,
|
||||
formatOnType: true,
|
||||
formatOnPaste: true,
|
||||
selectOnLineNumbers: true,
|
||||
scrollBeyondLastLine: false,
|
||||
folding: true,
|
||||
scrollbar: {alwaysConsumeMouseWheel: false}, // 弹窗编辑中的编辑器有时会无法滚动
|
||||
minimap: {
|
||||
enabled: false
|
||||
},
|
||||
...options,
|
||||
model: this.model
|
||||
});
|
||||
};
|
||||
|
||||
editorDidMount = (editor: any, monaco: any) => {
|
||||
this.editor = editor;
|
||||
this.monaco = monaco;
|
||||
|
||||
this.changeJsonOptions(this.props);
|
||||
this.props.onPaste &&
|
||||
this.toDispose.push(this.editor.onDidPaste(this.props.onPaste).dispose);
|
||||
};
|
||||
|
||||
editorWillUnmount = (editor: any, monaco: any) => {
|
||||
this.toDispose.forEach(fn => fn());
|
||||
this.toDispose = [];
|
||||
};
|
||||
|
||||
handleChange = (value: string) => {
|
||||
this.setState(
|
||||
{
|
||||
contents: value
|
||||
},
|
||||
this.emitChange
|
||||
);
|
||||
};
|
||||
|
||||
handleBlur = async () => {
|
||||
const {wrongSchema, value} = this.state;
|
||||
if (!wrongSchema) {
|
||||
return;
|
||||
}
|
||||
const result = await prompt(
|
||||
[
|
||||
{
|
||||
className: 'w-full',
|
||||
type: 'tpl',
|
||||
label: false,
|
||||
tpl: '当前有部分已更改数据因为格式不正确尚未保存,您确认要丢弃这部分更改吗?'
|
||||
},
|
||||
{
|
||||
type: 'switch',
|
||||
label: false,
|
||||
option: '查看更改',
|
||||
name: 'diff',
|
||||
value: false
|
||||
},
|
||||
{
|
||||
visibleOn: 'this.diff',
|
||||
label: false,
|
||||
type: 'diff-editor',
|
||||
allowFullscreen: true,
|
||||
disabled: true,
|
||||
name: 'newValue',
|
||||
size: 'xxl',
|
||||
language: 'json',
|
||||
diffValue: '${oldValue}'
|
||||
}
|
||||
],
|
||||
{
|
||||
oldValue: value,
|
||||
newValue: wrongSchema
|
||||
},
|
||||
'请确认'
|
||||
);
|
||||
if (result) {
|
||||
this.setState({wrongSchema: '', contents: stringify(value)});
|
||||
} else {
|
||||
this.editor.focus();
|
||||
}
|
||||
};
|
||||
|
||||
render() {
|
||||
let {disabled, className, theme} = this.props;
|
||||
|
||||
return (
|
||||
// @ts-ignore
|
||||
<Editor
|
||||
className={cx('amis-code-editor', className)}
|
||||
value={this.state.contents}
|
||||
onChange={this.handleChange}
|
||||
onBlur={this.handleBlur}
|
||||
language="json"
|
||||
theme={theme}
|
||||
editorFactory={this.editorFactory}
|
||||
editorDidMount={this.editorDidMount}
|
||||
editorWillUnmount={this.editorWillUnmount}
|
||||
options={{
|
||||
automaticLayout: true,
|
||||
lineNumbers: 'off',
|
||||
glyphMargin: false,
|
||||
tabSize: 2,
|
||||
wordWrap: 'on',
|
||||
lineDecorationsWidth: 0,
|
||||
lineNumbersMinChars: 0,
|
||||
selectOnLineNumbers: true,
|
||||
scrollBeyondLastLine: false,
|
||||
folding: true,
|
||||
minimap: {
|
||||
enabled: false
|
||||
},
|
||||
readOnly: disabled
|
||||
}}
|
||||
/>
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
import {observer} from 'mobx-react';
|
||||
import React from 'react';
|
||||
import {Tab, Tabs} from 'amis';
|
||||
import RenderersPanel from './RenderersPanel';
|
||||
import {PanelProps} from '../../plugin';
|
||||
import {autobind} from '../../util';
|
||||
|
||||
type PanelStates = {
|
||||
toggleCollapseFolderStatus: boolean;
|
||||
};
|
||||
|
||||
@observer
|
||||
export class AvailableRenderersPanel extends React.Component<
|
||||
PanelProps,
|
||||
PanelStates
|
||||
> {
|
||||
@autobind
|
||||
handleSelect(key: string) {
|
||||
if (key) {
|
||||
this.props.store.changeRenderersTabsKey(key);
|
||||
}
|
||||
}
|
||||
|
||||
render() {
|
||||
const {store, manager, children} = this.props;
|
||||
const renderersTabsKey = store.renderersTabsKey || 'base-renderers';
|
||||
const curTheme = store.theme;
|
||||
const customRenderersByOrder = store.customRenderersByOrder || [];
|
||||
const groupedSubRenderers = store.groupedSubRenderers || {};
|
||||
const groupedCustomRenderers = store.groupedCustomRenderers || {}; // 自定义组件
|
||||
|
||||
return (
|
||||
<div className="ae-RendererPanel">
|
||||
<div className="panel-header">组件</div>
|
||||
<div className="ae-RendererPanel-content">
|
||||
{typeof children === 'function' ? children(this.props) : children}
|
||||
{store.showCustomRenderersPanel &&
|
||||
customRenderersByOrder.length > 0 && (
|
||||
<Tabs
|
||||
theme={curTheme}
|
||||
tabsMode={'line'} // tiled
|
||||
className="ae-RendererList-tabs"
|
||||
linksClassName="ae-RendererList-tabs-header"
|
||||
contentClassName="ae-RendererList-tabs-content"
|
||||
activeKey={renderersTabsKey}
|
||||
onSelect={this.handleSelect}
|
||||
>
|
||||
<Tab
|
||||
key={'base-renderers'}
|
||||
eventKey={'base-renderers'}
|
||||
title={'系统组件'}
|
||||
className={`ae-RendererList-tabs-panel base-renderers`}
|
||||
mountOnEnter={true}
|
||||
unmountOnExit={false}
|
||||
>
|
||||
<RenderersPanel
|
||||
groupedRenderers={groupedSubRenderers}
|
||||
store={store}
|
||||
manager={manager}
|
||||
searchRendererType={'renderer'}
|
||||
/>
|
||||
</Tab>
|
||||
<Tab
|
||||
key={'custom-renderers'}
|
||||
eventKey={'custom-renderers'}
|
||||
title={'自定义组件'}
|
||||
className={`ae-RendererList-tabs-panel custom-renderers`}
|
||||
mountOnEnter={true}
|
||||
unmountOnExit={false}
|
||||
>
|
||||
<RenderersPanel
|
||||
groupedRenderers={groupedCustomRenderers}
|
||||
store={store}
|
||||
manager={manager}
|
||||
searchRendererType={'custom-renderer'}
|
||||
/>
|
||||
</Tab>
|
||||
</Tabs>
|
||||
)}
|
||||
{(!store.showCustomRenderersPanel ||
|
||||
Object.keys(groupedCustomRenderers).length < 1) && (
|
||||
<RenderersPanel
|
||||
className={'only-base-component'}
|
||||
groupedRenderers={groupedSubRenderers}
|
||||
store={store}
|
||||
manager={manager}
|
||||
searchRendererType={'renderer'}
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
import React from 'react';
|
||||
import {PanelProps} from '../../plugin';
|
||||
import {autobind} from '../../util';
|
||||
import AMisCodeEditor from './AMisCodeEditor';
|
||||
|
||||
export default class CodeEditorPanel extends React.Component<PanelProps> {
|
||||
@autobind
|
||||
handleCodePaste(e: any) {
|
||||
setTimeout(() => {
|
||||
this.props.manager.patchSchema(true);
|
||||
|
||||
// 检测是否整体粘贴组件,如果是的话对指定类型组件存在重复ID的重新生成
|
||||
if (
|
||||
e?.languageId === 'json' &&
|
||||
e.range?.startColumn === 1 &&
|
||||
e.range?.startLineNumber === 1 &&
|
||||
e.range?.endColumn === 2 &&
|
||||
e.range?.endLineNumber > 1
|
||||
) {
|
||||
this.props.manager.reGenerateNodeDuplicateID(['布局容器', '展示']);
|
||||
}
|
||||
}, 500);
|
||||
}
|
||||
|
||||
render() {
|
||||
const {onChange, manager, store} = this.props;
|
||||
|
||||
return (
|
||||
<div className="ae-CodePanel">
|
||||
<div className="panel-header">源码</div>
|
||||
<div className="ae-CodePanel-content">
|
||||
<AMisCodeEditor
|
||||
value={store.valueWithoutHiddenProps}
|
||||
onChange={onChange}
|
||||
$schema={store.jsonSchemaUri}
|
||||
$schemaUrl={manager.config.$schemaUrl}
|
||||
onPaste={this.handleCodePaste}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
import {observer} from 'mobx-react';
|
||||
import React from 'react';
|
||||
import {Icon} from 'amis';
|
||||
import {EditorStoreType} from '../../store/editor';
|
||||
|
||||
export interface ContextMenuPanelProps {
|
||||
store: EditorStoreType;
|
||||
}
|
||||
|
||||
@observer
|
||||
export class ContextMenuPanel extends React.Component<ContextMenuPanelProps> {
|
||||
render() {
|
||||
const store = this.props.store;
|
||||
const contextMenuPanel = store.contextMenuPanel;
|
||||
const menus =
|
||||
contextMenuPanel && contextMenuPanel.menus ? contextMenuPanel.menus : [];
|
||||
|
||||
return menus && menus.length > 0 ? (
|
||||
<div className="context-menu-setting">
|
||||
<div className="setting-header">批量操作</div>
|
||||
<div className="setting-body">
|
||||
{menus.map(menu => {
|
||||
return (
|
||||
menu.label && (
|
||||
<div
|
||||
key={menu.label}
|
||||
className={`setting-item ${menu.disabled ? 'disabled' : ''}`}
|
||||
onClick={!menu.disabled ? menu.onSelect : null}
|
||||
>
|
||||
<div className="icon-box">
|
||||
<Icon icon={menu.icon} className="menu-icon" />
|
||||
</div>
|
||||
<div className="setting-info">{menu.label}</div>
|
||||
</div>
|
||||
)
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
</div>
|
||||
) : null;
|
||||
}
|
||||
}
|
||||
155
packages/amis-editor-core/src/component/Panel/DialogList.tsx
Normal file
@@ -0,0 +1,155 @@
|
||||
import {ClassNamesFn} from 'amis-core';
|
||||
import {observer} from 'mobx-react';
|
||||
import React from 'react';
|
||||
import {EditorStoreType} from '../../store/editor';
|
||||
import {
|
||||
JSONGetById,
|
||||
modalsToDefinitions,
|
||||
reGenerateID,
|
||||
translateSchema
|
||||
} from '../../util';
|
||||
import {Button, Icon, ListMenu, PopOverContainer, confirm} from 'amis';
|
||||
import {EditorManager} from '../../manager';
|
||||
import cloneDeep from 'lodash/cloneDeep';
|
||||
|
||||
export interface DialogListProps {
|
||||
classnames: ClassNamesFn;
|
||||
store: EditorStoreType;
|
||||
manager: EditorManager;
|
||||
}
|
||||
|
||||
export default observer(function DialogList({
|
||||
classnames: cx,
|
||||
store,
|
||||
manager
|
||||
}: DialogListProps) {
|
||||
const modals = store.modals.filter(item => !item.disabled);
|
||||
|
||||
const handleAddDialog = React.useCallback(() => {
|
||||
const modal = {
|
||||
type: 'dialog',
|
||||
title: '未命名弹窗',
|
||||
definitions: modalsToDefinitions(store.modals),
|
||||
body: [
|
||||
{
|
||||
type: 'tpl',
|
||||
tpl: '弹窗内容'
|
||||
}
|
||||
]
|
||||
};
|
||||
|
||||
manager.openSubEditor({
|
||||
title: '编辑弹窗',
|
||||
value: modal,
|
||||
onDefinitionsChange: (definitions, originDefinitions, modal) => {
|
||||
store.addModal(modal, definitions);
|
||||
return false;
|
||||
}
|
||||
});
|
||||
}, []);
|
||||
|
||||
const handleEditDialog = React.useCallback((event: React.UIEvent<any>) => {
|
||||
const index = parseInt(event.currentTarget.getAttribute('data-index')!, 10);
|
||||
const modal = store.modals[index];
|
||||
const modalId = modal.$$id!;
|
||||
manager.openSubEditor({
|
||||
title: '编辑弹窗',
|
||||
value: {
|
||||
type: 'dialog',
|
||||
...(modal as any),
|
||||
definitions: modalsToDefinitions(store.modals, {}, modal)
|
||||
},
|
||||
onDefinitionsChange: (definitions, originDefinitions, modal) => {
|
||||
store.updateModal(modalId, modal, definitions);
|
||||
return false;
|
||||
}
|
||||
});
|
||||
}, []);
|
||||
|
||||
const handleDelDialog = React.useCallback(
|
||||
async (event: React.UIEvent<any>) => {
|
||||
event.stopPropagation();
|
||||
event.preventDefault();
|
||||
|
||||
const index = parseInt(
|
||||
event.currentTarget
|
||||
.closest('[data-index]')!
|
||||
.getAttribute('data-index')!,
|
||||
10
|
||||
);
|
||||
const dialog = store.modals[index];
|
||||
const refsCount = store.countModalActionRefs(dialog.$$id!);
|
||||
|
||||
const confirmed = await confirm(
|
||||
refsCount
|
||||
? `当前弹窗已关联 ${refsCount} 个事件,删除后,所配置的事件动作将一起被删除。`
|
||||
: '',
|
||||
`确认删除弹窗「${dialog.editorSetting?.displayName || dialog.title}」?`
|
||||
);
|
||||
|
||||
if (confirmed) {
|
||||
store.removeModal(dialog.$$id!);
|
||||
}
|
||||
},
|
||||
[]
|
||||
);
|
||||
|
||||
const handleCopyDialog = React.useCallback((event: React.UIEvent<any>) => {
|
||||
event.stopPropagation();
|
||||
event.preventDefault();
|
||||
|
||||
const index = parseInt(
|
||||
event.currentTarget.closest('[data-index]')!.getAttribute('data-index')!,
|
||||
10
|
||||
);
|
||||
let dialog = cloneDeep(store.modals[index]);
|
||||
dialog = reGenerateID(dialog);
|
||||
|
||||
store.addModal({
|
||||
...dialog,
|
||||
title: `${dialog.title} - 复制`,
|
||||
editorSetting: {
|
||||
...dialog.editorSetting,
|
||||
displayName: dialog.editorSetting?.displayName
|
||||
? `${dialog.editorSetting?.displayName} - 复制`
|
||||
: ''
|
||||
}
|
||||
});
|
||||
}, []);
|
||||
|
||||
return (
|
||||
<div className={cx('ae-DialogList-wrap', 'hoverShowScrollBar')}>
|
||||
<Button size="sm" level="enhance" block onClick={handleAddDialog}>
|
||||
新增弹窗
|
||||
</Button>
|
||||
{modals.length ? (
|
||||
<ul className="ae-DialogList">
|
||||
{modals.map((modal, index) => (
|
||||
<li
|
||||
className="ae-DialogList-item"
|
||||
data-index={index}
|
||||
key={modal.$$id || index}
|
||||
onClick={handleEditDialog}
|
||||
>
|
||||
<span>
|
||||
{`${
|
||||
modal.editorSetting?.displayName ||
|
||||
modal.title ||
|
||||
'未命名弹窗'
|
||||
}`}
|
||||
</span>
|
||||
<a onClick={handleCopyDialog} className="ae-DialogList-iconBtn">
|
||||
<Icon className="icon" icon="copy" />
|
||||
</a>
|
||||
<a onClick={handleDelDialog} className="ae-DialogList-iconBtn">
|
||||
<Icon className="icon" icon="trash" />
|
||||
</a>
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
) : (
|
||||
<div className="ae-DialogList-placeholder">暂无弹窗</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,54 @@
|
||||
import {observer} from 'mobx-react';
|
||||
import React from 'react';
|
||||
import {EditorManager} from '../../manager';
|
||||
import {EditorStoreType} from '../../store/editor';
|
||||
import {Drawer} from 'amis';
|
||||
import {autobind} from '../../util';
|
||||
import {findDOMNode} from 'react-dom';
|
||||
import {EditorNodeType} from '../../store/node';
|
||||
|
||||
export interface PanelsProps {
|
||||
store: EditorStoreType;
|
||||
manager: EditorManager;
|
||||
node: EditorNodeType | undefined;
|
||||
panelItem: any;
|
||||
theme?: string;
|
||||
}
|
||||
|
||||
@observer
|
||||
export class DrawerPanel extends React.Component<PanelsProps> {
|
||||
@autobind
|
||||
getPopOverContainer() {
|
||||
return findDOMNode(this) as HTMLElement;
|
||||
}
|
||||
|
||||
render() {
|
||||
const {store, manager, node, panelItem, theme} = this.props;
|
||||
const id = store.activeId;
|
||||
|
||||
return (
|
||||
<Drawer
|
||||
position="left"
|
||||
size="md"
|
||||
theme={theme}
|
||||
show={!!panelItem}
|
||||
onHide={store.closeInsertPanel}
|
||||
className="ae-InsertPanel-drawer"
|
||||
>
|
||||
{panelItem && panelItem.component ? (
|
||||
<panelItem.component
|
||||
key={panelItem.key}
|
||||
id={id}
|
||||
info={node?.info}
|
||||
path={node?.path}
|
||||
value={store.value}
|
||||
onChange={manager.panelChangeValue}
|
||||
store={store}
|
||||
manager={manager}
|
||||
popOverContainer={this.getPopOverContainer}
|
||||
/>
|
||||
) : null}
|
||||
</Drawer>
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
import {observer} from 'mobx-react';
|
||||
import React from 'react';
|
||||
import {EditorManager} from '../../manager';
|
||||
import {EditorStoreType} from '../../store/editor';
|
||||
import {Drawer} from 'amis';
|
||||
import {autobind} from '../../util';
|
||||
import {findDOMNode} from 'react-dom';
|
||||
import {EditorNodeType} from '../../store/node';
|
||||
|
||||
export interface PanelsProps {
|
||||
store: EditorStoreType;
|
||||
manager: EditorManager;
|
||||
node: EditorNodeType | undefined;
|
||||
panelItem: any;
|
||||
theme?: string;
|
||||
}
|
||||
|
||||
@observer
|
||||
export class DrawerRendererPanel extends React.Component<PanelsProps> {
|
||||
@autobind
|
||||
getPopOverContainer() {
|
||||
return findDOMNode(this) as HTMLElement;
|
||||
}
|
||||
|
||||
render() {
|
||||
const {store, manager, node, panelItem, theme} = this.props;
|
||||
const id = store.activeId;
|
||||
const showInsertRenderer = store.showInsertRenderer;
|
||||
|
||||
return (
|
||||
<Drawer
|
||||
position="left"
|
||||
size="md"
|
||||
theme={theme}
|
||||
show={showInsertRenderer}
|
||||
onHide={store.closeInsertRendererPanel}
|
||||
className="ae-InsertRendererPanel-drawer"
|
||||
bodyClassName="ae-drawer-renderers"
|
||||
>
|
||||
{panelItem.title && (
|
||||
<div className="ae-drawer-title">{panelItem.title}</div>
|
||||
)}
|
||||
{panelItem && panelItem.component ? (
|
||||
<panelItem.component
|
||||
key={panelItem.key}
|
||||
id={id}
|
||||
info={node?.info}
|
||||
path={node?.path}
|
||||
value={store.value}
|
||||
onChange={manager.panelChangeValue}
|
||||
store={store}
|
||||
manager={manager}
|
||||
popOverContainer={this.getPopOverContainer}
|
||||
/>
|
||||
) : null}
|
||||
</Drawer>
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,182 @@
|
||||
import {Button, Html, Icon, InputBox, render} from 'amis';
|
||||
import find from 'lodash/find';
|
||||
import {observer} from 'mobx-react';
|
||||
import React from 'react';
|
||||
import {PanelProps, SubRendererInfo} from '../../plugin';
|
||||
import {autobind} from '../../util';
|
||||
import {RendererThumb} from '../RendererThumb';
|
||||
|
||||
@observer
|
||||
export class InsertSubRendererPanel extends React.Component<PanelProps> {
|
||||
@autobind
|
||||
handleLeftClick(e: React.MouseEvent) {
|
||||
const tag = e.currentTarget.getAttribute('data-value')!;
|
||||
const store = this.props.store;
|
||||
|
||||
store.setInsertTag(tag);
|
||||
}
|
||||
|
||||
@autobind
|
||||
handleClick(e: React.MouseEvent) {
|
||||
const id = e.currentTarget.getAttribute('data-value')!;
|
||||
const store = this.props.store;
|
||||
store.setInsertSelected(id);
|
||||
}
|
||||
|
||||
@autobind
|
||||
hadnlDBClick(e: React.MouseEvent) {
|
||||
const id = e.currentTarget.getAttribute('data-value')!;
|
||||
const store = this.props.store;
|
||||
store.setInsertSelected(id);
|
||||
const manager = this.props.manager;
|
||||
store.insertMode === 'replace' ? manager.replace() : manager.insert();
|
||||
}
|
||||
|
||||
@autobind
|
||||
handleInsert() {
|
||||
const manager = this.props.manager;
|
||||
|
||||
manager.insert();
|
||||
}
|
||||
|
||||
@autobind
|
||||
handleReplace() {
|
||||
const manager = this.props.manager;
|
||||
|
||||
manager.replace();
|
||||
}
|
||||
|
||||
@autobind
|
||||
handleCancel() {
|
||||
const store = this.props.store;
|
||||
store.closeInsertPanel();
|
||||
}
|
||||
|
||||
render() {
|
||||
const store = this.props.store;
|
||||
const manager = this.props.manager;
|
||||
const info = store.getNodeById(store.insertId)?.info!;
|
||||
const regionLabel = find(
|
||||
info.regions,
|
||||
region => region.key === store.insertRegion
|
||||
)?.label;
|
||||
const grouped = store.groupedInsertRenderers;
|
||||
const keys = Object.keys(grouped);
|
||||
const insertTag = store.insertTag || '全部';
|
||||
const list = grouped[insertTag] || grouped['全部'];
|
||||
|
||||
return (
|
||||
<div className="ae-InsertPanel">
|
||||
{store.insertMode === 'replace' ? (
|
||||
<div className="ae-InsertPanel-title">更改组件类型</div>
|
||||
) : (
|
||||
<div className="ae-InsertPanel-title">
|
||||
<span>选中组件插入到</span>
|
||||
<code>
|
||||
{info.name} > {regionLabel}
|
||||
</code>
|
||||
</div>
|
||||
)}
|
||||
|
||||
<div className="m-b-xs">
|
||||
<InputBox
|
||||
className="editor-InputSearch"
|
||||
value={store.insertRenderersKeywords}
|
||||
onChange={store.changeInsertRenderersKeywords}
|
||||
placeholder={'输入关键字可过滤组件'}
|
||||
clearable={false}
|
||||
>
|
||||
{store.insertRenderersKeywords ? (
|
||||
<a onClick={store.resetInsertRenderersKeywords}>
|
||||
<Icon icon="close" className="icon" />
|
||||
</a>
|
||||
) : (
|
||||
<Icon icon="search" className="icon" />
|
||||
)}
|
||||
</InputBox>
|
||||
</div>
|
||||
|
||||
<div className="ae-RenderersPicker-list">
|
||||
<ul>
|
||||
{keys.map(key => (
|
||||
<li key={key} className={insertTag === key ? 'is-active' : ''}>
|
||||
<a data-value={key} onClick={this.handleLeftClick}>
|
||||
{key}
|
||||
</a>
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
<div className="ae-RenderersPicker-content">
|
||||
{Array.isArray(list) && list.length ? (
|
||||
<ul>
|
||||
{list.map(item => (
|
||||
<li
|
||||
key={item.id}
|
||||
className={
|
||||
store.insertSelected === item.id ? 'is-active' : ''
|
||||
}
|
||||
data-value={item.id}
|
||||
onClick={this.handleClick}
|
||||
onDoubleClick={this.hadnlDBClick}
|
||||
>
|
||||
<RendererThumb
|
||||
theme={manager.env.theme}
|
||||
env={manager.env}
|
||||
schema={
|
||||
item.previewSchema || {
|
||||
type: 'tpl',
|
||||
tpl: '无法预览'
|
||||
}
|
||||
}
|
||||
/>
|
||||
|
||||
<div className="ae-RenderersPicker-info">
|
||||
<h4>{item.name}</h4>
|
||||
<div>
|
||||
<Html html={item.description} />
|
||||
{item.docLink ? (
|
||||
<a
|
||||
target="_blank"
|
||||
href={store.amisDocHost + item.docLink}
|
||||
>
|
||||
<span>详情</span>
|
||||
</a>
|
||||
) : null}
|
||||
</div>
|
||||
</div>
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
) : (
|
||||
<div>没有可用组件,也许你该切换容器试试。</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
<div className="ae-InsertPanel-footer">
|
||||
{store.insertMode === 'replace' ? (
|
||||
<Button
|
||||
className="action-btn action-btn-primary"
|
||||
onClick={this.handleReplace}
|
||||
disabled={!store.insertSelected}
|
||||
level="primary"
|
||||
>
|
||||
替换
|
||||
</Button>
|
||||
) : (
|
||||
<Button
|
||||
className="action-btn action-btn-primary"
|
||||
onClick={this.handleInsert}
|
||||
disabled={!store.insertSelected}
|
||||
level="primary"
|
||||
>
|
||||
插入
|
||||
</Button>
|
||||
)}
|
||||
<Button className="action-btn" onClick={this.handleCancel}>
|
||||
取消
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
}
|
||||
206
packages/amis-editor-core/src/component/Panel/LeftPanels.tsx
Normal file
@@ -0,0 +1,206 @@
|
||||
import {observer} from 'mobx-react';
|
||||
import React from 'react';
|
||||
import {Icon} from 'amis';
|
||||
import cx from 'classnames';
|
||||
import {EditorManager} from '../../manager';
|
||||
import {EditorStoreType} from '../../store/editor';
|
||||
import {Tab, Tabs} from 'amis';
|
||||
import {autobind} from '../../util';
|
||||
import {findDOMNode} from 'react-dom';
|
||||
import find from 'lodash/find';
|
||||
import {PanelItem} from '../../plugin';
|
||||
import {DrawerPanel} from './DrawerPanel';
|
||||
import {DrawerRendererPanel} from './DrawerRendererPanel';
|
||||
|
||||
export interface LeftPanelsProps {
|
||||
store: EditorStoreType;
|
||||
manager: EditorManager;
|
||||
theme?: string;
|
||||
}
|
||||
|
||||
interface LeftPanelsStates {
|
||||
isFixedStatus: boolean;
|
||||
}
|
||||
|
||||
@observer
|
||||
export class LeftPanels extends React.Component<
|
||||
LeftPanelsProps,
|
||||
LeftPanelsStates
|
||||
> {
|
||||
constructor(props: any) {
|
||||
super(props);
|
||||
|
||||
this.state = {
|
||||
isFixedStatus: false // 默认非fixed模式
|
||||
};
|
||||
}
|
||||
|
||||
@autobind
|
||||
handleHidden() {
|
||||
const {changeLeftPanelOpenStatus, leftPanelOpenStatus, changeLeftPanelKey} =
|
||||
this.props.store;
|
||||
const nextStatus = !leftPanelOpenStatus;
|
||||
changeLeftPanelOpenStatus(nextStatus);
|
||||
if (nextStatus) {
|
||||
// 展开则默认显示大纲面板
|
||||
changeLeftPanelKey('outline');
|
||||
} else {
|
||||
// 折叠时则不显示任何tab面板
|
||||
changeLeftPanelKey('none');
|
||||
}
|
||||
}
|
||||
|
||||
@autobind
|
||||
handleFixed() {
|
||||
this.setState({
|
||||
isFixedStatus: !this.state.isFixedStatus
|
||||
});
|
||||
}
|
||||
|
||||
@autobind
|
||||
handleSelect(key: string) {
|
||||
const {changeLeftPanelOpenStatus, changeLeftPanelKey} = this.props.store;
|
||||
if (key) {
|
||||
// 先展开再显示指定面板内容
|
||||
changeLeftPanelOpenStatus(true);
|
||||
changeLeftPanelKey(key);
|
||||
}
|
||||
}
|
||||
|
||||
@autobind
|
||||
getPopOverContainer() {
|
||||
return findDOMNode(this) as HTMLElement;
|
||||
}
|
||||
|
||||
render() {
|
||||
const {store, manager, theme} = this.props;
|
||||
const {isFixedStatus} = this.state;
|
||||
const leftPanelOpenStatus = store.leftPanelOpenStatus;
|
||||
const panels = store.getLeftPanels();
|
||||
const id = store.activeId;
|
||||
const node = store.getNodeById(id);
|
||||
const panelKey = store.getLeftPanelKey();
|
||||
const insertPanel =
|
||||
store.insertId &&
|
||||
store.insertRegion &&
|
||||
find(panels, p => p.key === 'insert');
|
||||
|
||||
const insertRendererPanel = find(panels, p => p.key === 'insertRenderer');
|
||||
|
||||
const renderPanel = (panel: PanelItem) => {
|
||||
return panel.render ? (
|
||||
panel.render({
|
||||
id,
|
||||
info: node?.info,
|
||||
path: node?.path,
|
||||
node: node,
|
||||
value: store.value,
|
||||
onChange: manager.panelChangeValue,
|
||||
store: store,
|
||||
manager: manager,
|
||||
popOverContainer: this.getPopOverContainer
|
||||
})
|
||||
) : panel.component ? (
|
||||
<panel.component
|
||||
node={node}
|
||||
key={panel.key}
|
||||
id={id}
|
||||
info={node?.info}
|
||||
path={node?.path}
|
||||
value={store.value}
|
||||
onChange={manager.panelChangeValue}
|
||||
store={store}
|
||||
manager={manager}
|
||||
popOverContainer={this.getPopOverContainer}
|
||||
/>
|
||||
) : null;
|
||||
};
|
||||
|
||||
return (
|
||||
<>
|
||||
{panels.length > 0 && (
|
||||
<div
|
||||
className={cx(
|
||||
'editor-left-panel width-draggable',
|
||||
leftPanelOpenStatus ? '' : 'hidden-status',
|
||||
isFixedStatus ? 'fixed-status' : ''
|
||||
)}
|
||||
>
|
||||
<div
|
||||
className={`editor-panel-btn`}
|
||||
editor-tooltip={isFixedStatus ? '关闭悬浮模式' : '开启悬浮模式'}
|
||||
tooltip-position="right"
|
||||
>
|
||||
<Icon
|
||||
icon={isFixedStatus ? 'editor-fixed' : 'editor-no-fixed'}
|
||||
className="panel-btn"
|
||||
onClick={this.handleFixed}
|
||||
/>
|
||||
</div>
|
||||
<Tabs
|
||||
className="editorPanel-tabs"
|
||||
linksClassName="editorPanel-tabs-header"
|
||||
contentClassName="editorPanel-tabs-content"
|
||||
theme={theme}
|
||||
activeKey={panelKey}
|
||||
onSelect={this.handleSelect}
|
||||
tabsMode="sidebar"
|
||||
sidePosition="left"
|
||||
>
|
||||
{panels.map(panel => {
|
||||
return panel.key !== 'insert' &&
|
||||
panel.key !== 'insertRenderer' ? (
|
||||
<Tab
|
||||
key={panel.key}
|
||||
eventKey={panel.key}
|
||||
title={
|
||||
panel.title ?? (
|
||||
<span
|
||||
className="editor-tab-icon editor-tab-s-icon"
|
||||
editor-tooltip={panel.tooltip}
|
||||
>
|
||||
{panel.icon}
|
||||
</span>
|
||||
)
|
||||
}
|
||||
// icon={panel.icon}
|
||||
className={`editorPanel-tabs-pane ae-Editor-${panel.key}Pane`}
|
||||
mountOnEnter={true}
|
||||
unmountOnExit={false}
|
||||
>
|
||||
{renderPanel(panel)}
|
||||
</Tab>
|
||||
) : null;
|
||||
})}
|
||||
</Tabs>
|
||||
<div
|
||||
className={cx(
|
||||
'left-panel-arrow',
|
||||
leftPanelOpenStatus ? '' : 'hidden-status'
|
||||
)}
|
||||
onClick={this.handleHidden}
|
||||
></div>
|
||||
</div>
|
||||
)}
|
||||
{isFixedStatus && (
|
||||
<div className="editor-left-panel-fixed-placeholder"></div>
|
||||
)}
|
||||
<DrawerPanel
|
||||
store={store}
|
||||
manager={manager}
|
||||
node={node}
|
||||
panelItem={insertPanel}
|
||||
theme={theme}
|
||||
/>
|
||||
{/* 插入组件面板(复用现有组件面板)*/}
|
||||
<DrawerRendererPanel
|
||||
store={store}
|
||||
manager={manager}
|
||||
node={node}
|
||||
panelItem={insertRendererPanel}
|
||||
theme={theme}
|
||||
/>
|
||||
</>
|
||||
);
|
||||
}
|
||||
}
|
||||
438
packages/amis-editor-core/src/component/Panel/Outline.tsx
Normal file
@@ -0,0 +1,438 @@
|
||||
import {observer} from 'mobx-react';
|
||||
import React from 'react';
|
||||
import {PanelProps} from '../../plugin';
|
||||
import cx from 'classnames';
|
||||
import {autobind, translateSchema} from '../../util';
|
||||
import {Icon, InputBox, Tab, Tabs} from 'amis';
|
||||
import {EditorNodeType} from '../../store/node';
|
||||
import {isAlive} from 'mobx-state-tree';
|
||||
import type {Schema} from 'amis';
|
||||
import DialogList from './DialogList';
|
||||
|
||||
@observer
|
||||
export class OutlinePanel extends React.Component<PanelProps> {
|
||||
state = {
|
||||
curSearchElemKey: '' // 用于记录用户当前输入的关键字
|
||||
};
|
||||
|
||||
dragEnterCount = 0;
|
||||
|
||||
@autobind
|
||||
handleClick(e: React.MouseEvent<HTMLAnchorElement>) {
|
||||
const store = this.props.store;
|
||||
const dom = e.currentTarget;
|
||||
const id = dom.getAttribute('data-node-id')!;
|
||||
const region = dom.getAttribute('data-node-region')!;
|
||||
const manager = this.props.manager;
|
||||
|
||||
if (region) {
|
||||
// 点击容器类型自动弹出「组件插入面板」有点干扰用户操作
|
||||
// manager.showInsertPanel(region, id);
|
||||
/** 特殊区域允许点击事件 */
|
||||
if (store.activeId === id && store.activeRegion === region) {
|
||||
// 重复点击则取消区域选中
|
||||
store.setActiveId(id);
|
||||
} else {
|
||||
store.setActiveId(id, region);
|
||||
}
|
||||
} else {
|
||||
store.setActiveId(id);
|
||||
}
|
||||
}
|
||||
|
||||
@autobind
|
||||
handleDialogNodeClick(
|
||||
e: React.MouseEvent<HTMLAnchorElement>,
|
||||
option: Schema
|
||||
) {
|
||||
// const store = this.props.store;
|
||||
// store.setPreviewDialogId(option.$$id);
|
||||
}
|
||||
|
||||
@autobind
|
||||
handleEnter(e: React.MouseEvent) {
|
||||
const dom = e.currentTarget;
|
||||
const id = dom.getAttribute('data-node-id')!;
|
||||
const region = dom.getAttribute('data-node-region')!;
|
||||
const store = this.props.store;
|
||||
|
||||
store.setHoverId(id, region);
|
||||
}
|
||||
|
||||
@autobind
|
||||
handleTabChange(key: string) {
|
||||
const store = this.props.store;
|
||||
if (key && isAlive(store)) {
|
||||
store.changeOutlineTabsKey(key);
|
||||
// if (key === 'component-outline') {
|
||||
// store.setPreviewDialogId();
|
||||
// }
|
||||
}
|
||||
}
|
||||
|
||||
@autobind
|
||||
handleDragStart(e: React.DragEvent) {
|
||||
const id = e.currentTarget!.getAttribute('data-node-id')!;
|
||||
|
||||
if (!id) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.props.manager.startDrag(id, e);
|
||||
}
|
||||
|
||||
timer: any;
|
||||
|
||||
@autobind
|
||||
handleDragEnter(e: React.DragEvent) {
|
||||
// this.dragEnterCount++;
|
||||
const target = e.target as HTMLElement;
|
||||
const dom = target.closest(`[data-node-id][data-node-region]`);
|
||||
|
||||
if (!dom) {
|
||||
return;
|
||||
}
|
||||
|
||||
e.preventDefault();
|
||||
|
||||
// 避免切换太快
|
||||
clearTimeout(this.timer);
|
||||
this.timer = setTimeout(() => {
|
||||
const manager = this.props.manager;
|
||||
const id = dom.getAttribute('data-node-id')!;
|
||||
const region = dom.getAttribute('data-node-region')!; // 大纲树中的容器节点
|
||||
|
||||
id &&
|
||||
region &&
|
||||
manager.dnd.switchToRegion(e.nativeEvent, id, region, true);
|
||||
}, 100);
|
||||
}
|
||||
|
||||
// @autobind
|
||||
// handleDragLeave() {
|
||||
// this.dragEnterCount--;
|
||||
// }
|
||||
|
||||
@autobind
|
||||
handleDragOver(e: React.DragEvent) {
|
||||
// const target = e.target as HTMLElement;
|
||||
// const dom = target.closest(`[data-node-id][data-node-region]`);
|
||||
|
||||
// if (!dom) {
|
||||
// return;
|
||||
// }
|
||||
|
||||
// const manager = this.props.manager;
|
||||
// const id = dom.getAttribute('data-node-id')!;
|
||||
// const region = dom.getAttribute('data-node-region')!; // 大纲树中的容器节点
|
||||
|
||||
// 这个得有,否则无法触发 drop 事件
|
||||
e.preventDefault();
|
||||
// id && region && manager.dnd.switchToRegion(e.nativeEvent, id, region);
|
||||
}
|
||||
|
||||
@autobind
|
||||
handleDrop(e: React.DragEvent) {
|
||||
const manager = this.props.manager;
|
||||
manager.dnd.drop(e.nativeEvent);
|
||||
}
|
||||
|
||||
@autobind
|
||||
handleSearchElemKeyChange(searchVal: string) {
|
||||
this.setState({
|
||||
curSearchElemKey: searchVal
|
||||
});
|
||||
}
|
||||
|
||||
@autobind
|
||||
clearSearchElemKey() {
|
||||
this.setState({
|
||||
curSearchElemKey: ''
|
||||
});
|
||||
}
|
||||
|
||||
@autobind
|
||||
renderTitleByKeyword(rendererTitle: string, curSearchTitle: string) {
|
||||
if (curSearchTitle && ~rendererTitle.indexOf(curSearchTitle)) {
|
||||
const keywordStartIndex = rendererTitle.indexOf(curSearchTitle);
|
||||
const keywordEndIndex = keywordStartIndex + curSearchTitle.length;
|
||||
return (
|
||||
<span>
|
||||
{rendererTitle.substring(0, keywordStartIndex)}
|
||||
<span className="has-keywords">{curSearchTitle}</span>
|
||||
{rendererTitle.substring(keywordEndIndex)}
|
||||
</span>
|
||||
);
|
||||
} else {
|
||||
return rendererTitle;
|
||||
}
|
||||
}
|
||||
|
||||
renderItem(
|
||||
option: EditorNodeType,
|
||||
index: number,
|
||||
type?: 'dialog' | 'dialogView',
|
||||
indent = 0
|
||||
) {
|
||||
const store = this.props.store;
|
||||
const {curSearchElemKey} = this.state;
|
||||
|
||||
const children = /*!store.dragging && */ (
|
||||
option.singleRegion
|
||||
? option.uniqueChildren[0]!.uniqueChildren
|
||||
: option.uniqueChildren
|
||||
) as Array<EditorNodeType>;
|
||||
|
||||
const hasChildren = children.length;
|
||||
|
||||
// if (store.dragging && !option.isRegion && !option.children.length) {
|
||||
// return null;
|
||||
// }
|
||||
|
||||
return (
|
||||
<li
|
||||
style={{'--indent': indent} as any}
|
||||
className={cx('ae-Outline-node', {
|
||||
'is-folded': option.folded,
|
||||
'is-active':
|
||||
(store.activeId === option.id && !option.region) ||
|
||||
(option.isRegion &&
|
||||
store.dropId === option.id &&
|
||||
store.dropRegion === option.region) ||
|
||||
(option.isRegion &&
|
||||
store.activeId === option.id &&
|
||||
store.activeRegion === option.region),
|
||||
'is-region': option.isRegion,
|
||||
'is-hover': !option.isRegion && store.isContextOn(option.id),
|
||||
'has-children': hasChildren
|
||||
// 'is-dragging':
|
||||
// store.dragId === option.id && store.dragType === 'schema'
|
||||
})}
|
||||
key={index}
|
||||
>
|
||||
<a
|
||||
onClick={this.handleClick}
|
||||
onMouseEnter={this.handleEnter}
|
||||
data-node-id={option.id}
|
||||
data-node-region={option.region}
|
||||
data-node-common-config={option.schema?.$$commonSchema}
|
||||
data-node-form-config={option.schema?.$$formSchema}
|
||||
draggable={option.draggable}
|
||||
onDragStart={this.handleDragStart}
|
||||
>
|
||||
{hasChildren ? (
|
||||
<span
|
||||
onClick={option.toggleFold}
|
||||
className={cx('ae-Outline-expander ae-Outline-node-icon', {
|
||||
'is-folded': option.folded
|
||||
})}
|
||||
data-node-id={option.id}
|
||||
data-node-region={option.region}
|
||||
>
|
||||
<Icon icon="down-arrow" />
|
||||
</span>
|
||||
) : (
|
||||
<span className={cx('ae-Outline-textIndent')}></span>
|
||||
)}
|
||||
<span className="ae-Outline-node-text">
|
||||
{option.isCommonConfig
|
||||
? `${option.label}-[公共配置]`
|
||||
: option.isFormConfig
|
||||
? `${option.label}-[表单配置]`
|
||||
: this.renderTitleByKeyword(
|
||||
this.getDialogNodeLabel(option, type),
|
||||
curSearchElemKey
|
||||
)}
|
||||
</span>
|
||||
</a>
|
||||
{hasChildren ? (
|
||||
<ul className="ae-Outline-sublist">
|
||||
{children.map((option, index) =>
|
||||
this.renderItem(option, index, type, indent + 1)
|
||||
)}
|
||||
</ul>
|
||||
) : null}
|
||||
</li>
|
||||
);
|
||||
}
|
||||
|
||||
renderDropItem(option: EditorNodeType, index: number) {
|
||||
const children = option.uniqueChildren as Array<EditorNodeType>;
|
||||
const store = this.props.store;
|
||||
|
||||
// 当前是当前正拖拽的元素,则不显示
|
||||
if (store.dragId && option.id === store.dragId) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const doms: any = children
|
||||
.map((option, index) => this.renderDropItem(option, index))
|
||||
.filter(item => item);
|
||||
|
||||
if (!doms.length && !option.isRegion) {
|
||||
return null;
|
||||
} else if (option.isRegion) {
|
||||
return (
|
||||
<div
|
||||
className={cx('ae-Outline-dropNode', {
|
||||
'is-active':
|
||||
store.dropId === option.id && store.dropRegion === option.region
|
||||
})}
|
||||
data-node-id={option.id}
|
||||
data-node-region={option.region}
|
||||
key={`${option.id}-${index}`}
|
||||
>
|
||||
<a>
|
||||
{option.host.singleRegion
|
||||
? option.host.label
|
||||
: `(${option.host.label})${option.label}`}
|
||||
</a>
|
||||
{doms}
|
||||
</div>
|
||||
);
|
||||
} else {
|
||||
return doms;
|
||||
}
|
||||
}
|
||||
|
||||
getDialogNodeLabel(option: EditorNodeType, type?: 'dialog' | 'dialogView') {
|
||||
if (!type) {
|
||||
return option.label;
|
||||
} else {
|
||||
return this.getDialogLabel(option, true, 'dialogTitle');
|
||||
}
|
||||
}
|
||||
|
||||
getDialogLabel(
|
||||
option: any,
|
||||
isNode: boolean,
|
||||
title: 'title' | 'dialogTitle' = 'title'
|
||||
) {
|
||||
let rendererTitle = '';
|
||||
if (isNode) {
|
||||
rendererTitle = option.label;
|
||||
}
|
||||
if (option.type === 'dialog' || option.type === 'drawer') {
|
||||
if (!isNode || (isNode && !option.region)) {
|
||||
const titleStr = translateSchema(option[title]);
|
||||
if (option.type === 'drawer') {
|
||||
rendererTitle = `${titleStr || '抽屉式弹窗'}(抽屉式弹窗)`;
|
||||
} else {
|
||||
if (option.dialogType === 'confirm') {
|
||||
rendererTitle = `${titleStr || '确认对话框'}(确认对话框)`;
|
||||
} else {
|
||||
rendererTitle = `${titleStr || '弹窗'}(弹窗)`;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return rendererTitle;
|
||||
}
|
||||
|
||||
renderDialogItem(option: any, index: number) {
|
||||
// const store = this.props.store;
|
||||
// const children = store.root.children;
|
||||
// const isSelectedDialog = option.$$id === store.previewDialogId;
|
||||
|
||||
const dialogLabel = this.getDialogLabel(option, false);
|
||||
|
||||
return (
|
||||
<li className={cx('ae-Outline-node')} key={index}>
|
||||
<a onClick={e => this.handleDialogNodeClick(e, option)}>
|
||||
<span className="ae-Outline-node-text">
|
||||
{this.renderTitleByKeyword(
|
||||
dialogLabel,
|
||||
this.state.curSearchElemKey
|
||||
)}
|
||||
</span>
|
||||
</a>
|
||||
</li>
|
||||
);
|
||||
}
|
||||
|
||||
render() {
|
||||
const {curSearchElemKey} = this.state;
|
||||
const {store, manager} = this.props;
|
||||
const outlineTabsKey = store.outlineTabsKey || 'component-outline';
|
||||
const options = store.outline;
|
||||
|
||||
return (
|
||||
<div className="ae-Outline-panel">
|
||||
<div className="panel-header">视图结构</div>
|
||||
<Tabs
|
||||
className="ae-outline-tabs"
|
||||
linksClassName="ae-outline-tabs-header"
|
||||
contentClassName="ae-outline-tabs-content"
|
||||
tabsMode="line"
|
||||
onSelect={this.handleTabChange}
|
||||
activeKey={outlineTabsKey}
|
||||
>
|
||||
<Tab
|
||||
className={'ae-outline-tabs-panel'}
|
||||
key={'component-outline'}
|
||||
eventKey={'component-outline'}
|
||||
title={'组件大纲'}
|
||||
>
|
||||
<InputBox
|
||||
className="editor-InputSearch"
|
||||
value={curSearchElemKey}
|
||||
onChange={this.handleSearchElemKeyChange}
|
||||
placeholder={'查询页面元素'}
|
||||
clearable={false}
|
||||
>
|
||||
{curSearchElemKey ? (
|
||||
<a onClick={this.clearSearchElemKey}>
|
||||
<Icon icon="close" className="icon" />
|
||||
</a>
|
||||
) : (
|
||||
<Icon icon="editor-search" className="icon" />
|
||||
)}
|
||||
</InputBox>
|
||||
<hr className="margin-top" />
|
||||
<div
|
||||
className={cx('ae-Outline', 'hoverShowScrollBar', {
|
||||
'ae-Outline--draging': store.dragging
|
||||
})}
|
||||
onDragOver={this.handleDragOver}
|
||||
onDragEnter={this.handleDragEnter}
|
||||
// onDragLeave={this.handleDragLeave}
|
||||
onDrop={this.handleDrop}
|
||||
>
|
||||
{store.dragging ? (
|
||||
<div className="ae-Outline-tip">可拖入以下区域固定拖入容器</div>
|
||||
) : null}
|
||||
|
||||
{options.length ? (
|
||||
store.dragging ? (
|
||||
<div className="ae-Outline-drop">
|
||||
{options.map((option, index) =>
|
||||
this.renderDropItem(option, index)
|
||||
)}
|
||||
</div>
|
||||
) : (
|
||||
<ul className="ae-Outline-list">
|
||||
{options.map((option, index) =>
|
||||
this.renderItem(option, index)
|
||||
)}
|
||||
</ul>
|
||||
)
|
||||
) : (
|
||||
<div>加载中,请稍等...</div>
|
||||
)}
|
||||
</div>
|
||||
</Tab>
|
||||
{store.isSubEditor || store.noDialog ? null : (
|
||||
<Tab
|
||||
className={'ae-outline-tabs-panel'}
|
||||
key={'dialog-outline'}
|
||||
eventKey={'dialog-outline'}
|
||||
title={'弹窗列表'}
|
||||
>
|
||||
<DialogList manager={manager} store={store} classnames={cx} />
|
||||
</Tab>
|
||||
)}
|
||||
</Tabs>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
}
|
||||
273
packages/amis-editor-core/src/component/Panel/RenderersPanel.tsx
Normal file
@@ -0,0 +1,273 @@
|
||||
import {Html, render, TestIdBuilder, TooltipWrapper} from 'amis';
|
||||
import {observer} from 'mobx-react';
|
||||
import React from 'react';
|
||||
import cx from 'classnames';
|
||||
import {Icon} from '../../icons/index';
|
||||
import SearchRendererPanel from '../base/SearchRendererPanel';
|
||||
import SearchCustomRendererPanel from '../base/SearchCustomRendererPanel';
|
||||
import {autobind, noop, isHasPluginIcon} from '../../util';
|
||||
import {EditorStoreType} from '../../store/editor';
|
||||
import {EditorManager} from '../../manager';
|
||||
import {SubRendererInfo} from '../../plugin';
|
||||
|
||||
type PanelProps = {
|
||||
store: EditorStoreType;
|
||||
manager: EditorManager;
|
||||
groupedRenderers: {
|
||||
[propName: string]: Array<SubRendererInfo>;
|
||||
};
|
||||
searchRendererType: string;
|
||||
className?: string;
|
||||
testIdBuilder?: TestIdBuilder;
|
||||
};
|
||||
|
||||
type PanelStates = {
|
||||
toggleCollapseFolderStatus: boolean;
|
||||
};
|
||||
|
||||
@observer
|
||||
export default class RenderersPanel extends React.Component<
|
||||
PanelProps,
|
||||
PanelStates
|
||||
> {
|
||||
state = {
|
||||
toggleCollapseFolderStatus: false // 用于触发重新渲染组件面板的
|
||||
};
|
||||
|
||||
// 用于记录组件分类面板的折叠状态
|
||||
curCollapseFolded: {
|
||||
[propName: string]: boolean;
|
||||
} = {};
|
||||
|
||||
// 暂未使用
|
||||
@autobind
|
||||
handleRegionFilterClick(e: React.MouseEvent) {
|
||||
let region = e.currentTarget.getAttribute('data-value')!;
|
||||
|
||||
const {store, manager} = this.props;
|
||||
region = region === store.subRendererRegion ? '' : region;
|
||||
manager.switchToRegion(region);
|
||||
}
|
||||
|
||||
@autobind
|
||||
handleDragStart(e: React.DragEvent, label: string) {
|
||||
const current = e.currentTarget;
|
||||
const id = current.getAttribute('data-id')!;
|
||||
e.dataTransfer.setData(`dnd-dom/[data-id="${id}"]`, '');
|
||||
|
||||
// 将鼠标位置调整到最左侧,以免拖入的时候挡住容器信息引来混淆
|
||||
e.dataTransfer!.setDragImage(e.target as any, 0, 0);
|
||||
|
||||
// 增加默认拖拽过程中元素
|
||||
e.dataTransfer!.effectAllowed = 'copy';
|
||||
}
|
||||
|
||||
// 组件点选使用
|
||||
@autobind
|
||||
handleClick(e: React.MouseEvent) {
|
||||
const id = e.currentTarget.getAttribute('data-dnd-id')!;
|
||||
this.props.manager.addElem(id);
|
||||
}
|
||||
|
||||
// 改变折叠状态
|
||||
@autobind
|
||||
changeCollapseFoldStatus(tagKey: string, event: any) {
|
||||
this.curCollapseFolded[tagKey] = !this.curCollapseFolded[tagKey];
|
||||
this.setState({
|
||||
toggleCollapseFolderStatus: !this.state.toggleCollapseFolderStatus
|
||||
});
|
||||
event.preventDefault();
|
||||
event.stopPropagation();
|
||||
}
|
||||
|
||||
renderThumb(schema: any) {
|
||||
const manager = this.props.manager;
|
||||
return schema ? (
|
||||
render(
|
||||
schema,
|
||||
{
|
||||
onAction: noop
|
||||
},
|
||||
manager.env
|
||||
)
|
||||
) : (
|
||||
<p>没有预览图</p>
|
||||
);
|
||||
}
|
||||
|
||||
render() {
|
||||
const {store, searchRendererType, className, testIdBuilder} = this.props;
|
||||
const grouped = this.props.groupedRenderers || {};
|
||||
const keys = Object.keys(grouped);
|
||||
|
||||
return (
|
||||
<div className={cx('ae-RendererList', className)}>
|
||||
{searchRendererType === 'renderer' && (
|
||||
<SearchRendererPanel store={store} />
|
||||
)}
|
||||
{searchRendererType === 'custom-renderer' && (
|
||||
<SearchCustomRendererPanel store={store} />
|
||||
)}
|
||||
<hr className="margin-top" />
|
||||
|
||||
{/* {node.childRegions.length ? (
|
||||
<div className="ae-RegionFilter">
|
||||
区域:
|
||||
{node.childRegions.map(region => (
|
||||
<div
|
||||
className={
|
||||
store.subRendererRegion === region.region ? 'is-active' : ''
|
||||
}
|
||||
data-value={region.region}
|
||||
onClick={this.handleRegionFilterClick}
|
||||
key={region.region}
|
||||
>
|
||||
{region.label}
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
) : null} */}
|
||||
|
||||
{/*<div className="ae-RendererList-tip">
|
||||
请选择以下组件拖入「{node?.label}」中
|
||||
</div>*/}
|
||||
|
||||
<div className="ae-RendererList-groupWrap hoverShowScrollBar">
|
||||
{keys.length ? (
|
||||
keys.map((tag, index) => {
|
||||
const items = grouped[tag];
|
||||
|
||||
if (!items || !items.length) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return (
|
||||
<React.Fragment key={tag}>
|
||||
<div
|
||||
key={`${tag}-head`}
|
||||
className={'ae-RendererList-head collapse-header'}
|
||||
onClick={(event: any) => {
|
||||
this.changeCollapseFoldStatus(tag, event);
|
||||
}}
|
||||
>
|
||||
{tag}
|
||||
<div
|
||||
className={cx('expander-icon', {
|
||||
'is-folded': !!this.curCollapseFolded[tag]
|
||||
})}
|
||||
title={
|
||||
!!this.curCollapseFolded[tag] ? '点击展开' : '点击折叠'
|
||||
}
|
||||
>
|
||||
<Icon icon="right-arrow-bold" />
|
||||
</div>
|
||||
</div>
|
||||
<div
|
||||
key={`${tag}-content`}
|
||||
className={cx('ae-RendererList-group collapse-content', {
|
||||
'is-folded': !!this.curCollapseFolded[tag]
|
||||
})}
|
||||
>
|
||||
{items.map((item: any) => {
|
||||
const key = `${index}_${item.id}`;
|
||||
const usePluginIcon = isHasPluginIcon(item);
|
||||
const testid = `editor-renderer-${item.plugin.rendererName}`;
|
||||
|
||||
return (
|
||||
<div
|
||||
key={key}
|
||||
className="ae-RendererList-item"
|
||||
draggable
|
||||
data-id={key}
|
||||
data-dnd-type="subrenderer"
|
||||
data-dnd-id={item.id}
|
||||
data-dnd-data={JSON.stringify(
|
||||
item.scaffold || {type: item.type}
|
||||
)}
|
||||
onDragStart={(e: React.DragEvent) =>
|
||||
this.handleDragStart(e, item.name)
|
||||
}
|
||||
{...testIdBuilder?.getChild(testid).getTestId()}
|
||||
>
|
||||
<div
|
||||
className="icon-box"
|
||||
data-dnd-id={item.id}
|
||||
title={`点击添加「${item.name}」`}
|
||||
onClick={this.handleClick}
|
||||
>
|
||||
{usePluginIcon && <Icon icon={item.pluginIcon} />}
|
||||
{!usePluginIcon && (
|
||||
<i
|
||||
className={cx(
|
||||
'fa-fw',
|
||||
item.icon || 'fa fa-circle-thin'
|
||||
)}
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
<div
|
||||
className="ae-RendererInfo"
|
||||
data-dnd-id={item.id}
|
||||
onClick={this.handleClick}
|
||||
>
|
||||
{item.name}
|
||||
</div>
|
||||
<TooltipWrapper
|
||||
tooltipClassName="ae-RendererThumb"
|
||||
trigger="hover"
|
||||
rootClose={true}
|
||||
placement="right"
|
||||
tooltip={{
|
||||
offset: [10, 0], // x轴偏移,避免遮挡边框
|
||||
children: () => (
|
||||
<div>
|
||||
<div className="ae-Renderer-title">
|
||||
{item.name}
|
||||
</div>
|
||||
{item.description || item.docLink ? (
|
||||
<div className="ae-Renderer-info">
|
||||
<Html
|
||||
html={
|
||||
item.description
|
||||
? item.description
|
||||
: ''
|
||||
}
|
||||
/>
|
||||
{item.docLink && (
|
||||
<a
|
||||
target="_blank"
|
||||
href={
|
||||
store.amisDocHost + item.docLink
|
||||
}
|
||||
>
|
||||
详情
|
||||
</a>
|
||||
)}
|
||||
</div>
|
||||
) : null}
|
||||
<div className="ae-Renderer-preview">
|
||||
{this.renderThumb(item.previewSchema)}
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}}
|
||||
>
|
||||
<div className="ae-RendererIcon">
|
||||
<Icon icon="editor-help" className="icon" />
|
||||
</div>
|
||||
</TooltipWrapper>
|
||||
</div>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
</React.Fragment>
|
||||
);
|
||||
})
|
||||
) : (
|
||||
<span>没有找到可用组件,您可以换个关键字继续查找。</span>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
}
|
||||
195
packages/amis-editor-core/src/component/Panel/RightPanels.tsx
Normal file
@@ -0,0 +1,195 @@
|
||||
import {observer} from 'mobx-react';
|
||||
import React from 'react';
|
||||
import {Tab, Tabs, toast} from 'amis';
|
||||
import cx from 'classnames';
|
||||
import {EditorManager} from '../../manager';
|
||||
import {EditorStoreType} from '../../store/editor';
|
||||
import {Icon} from '../../icons/index';
|
||||
import {autobind, isHasPluginIcon} from '../../util';
|
||||
import {findDOMNode} from 'react-dom';
|
||||
import {PanelItem} from '../../plugin';
|
||||
import {WidthDraggableBtn} from '../base/WidthDraggableBtn';
|
||||
|
||||
export interface RightPanelsProps {
|
||||
store: EditorStoreType;
|
||||
manager: EditorManager;
|
||||
theme?: string;
|
||||
appLocale?: string;
|
||||
amisEnv?: any;
|
||||
readonly?: boolean;
|
||||
}
|
||||
|
||||
interface RightPanelsStates {
|
||||
isOpenStatus: boolean;
|
||||
isFixedStatus: boolean;
|
||||
}
|
||||
|
||||
@observer
|
||||
export class RightPanels extends React.Component<
|
||||
RightPanelsProps,
|
||||
RightPanelsStates
|
||||
> {
|
||||
constructor(props: any) {
|
||||
super(props);
|
||||
|
||||
this.state = {
|
||||
isOpenStatus: true, // 默认展开
|
||||
isFixedStatus: false // 默认非fixed模式
|
||||
};
|
||||
}
|
||||
|
||||
@autobind
|
||||
handleFixed() {
|
||||
this.setState({
|
||||
isFixedStatus: !this.state.isFixedStatus
|
||||
});
|
||||
}
|
||||
|
||||
@autobind
|
||||
handleSelect(key: string) {
|
||||
const store = this.props.store;
|
||||
store.changePanelKey(key);
|
||||
}
|
||||
|
||||
@autobind
|
||||
handleHidden() {
|
||||
this.setState({
|
||||
isOpenStatus: !this.state.isOpenStatus
|
||||
});
|
||||
}
|
||||
|
||||
@autobind
|
||||
getPopOverContainer() {
|
||||
return findDOMNode(this) as HTMLElement;
|
||||
}
|
||||
|
||||
@autobind
|
||||
handlePanelChangeValue(
|
||||
...arg: Parameters<typeof this.props.manager.panelChangeValue>
|
||||
) {
|
||||
const {manager} = this.props;
|
||||
|
||||
manager.panelChangeValue(...arg);
|
||||
}
|
||||
|
||||
render() {
|
||||
const {store, manager, theme, readonly} = this.props;
|
||||
const {isOpenStatus, isFixedStatus} = this.state;
|
||||
const panels = store.getPanels();
|
||||
const id = store.activeId;
|
||||
const node = store.getNodeById(id);
|
||||
const panelKey = store.getPanelKey();
|
||||
const renderPanel = (panel: PanelItem) => {
|
||||
return panel.render ? (
|
||||
panel.render({
|
||||
id,
|
||||
info: node?.info,
|
||||
path: node?.path,
|
||||
node: node,
|
||||
value: store.value,
|
||||
onChange: this.handlePanelChangeValue,
|
||||
store: store,
|
||||
manager: manager,
|
||||
popOverContainer: this.getPopOverContainer,
|
||||
readonly
|
||||
})
|
||||
) : panel.component ? (
|
||||
<panel.component
|
||||
node={node}
|
||||
key={panel.key}
|
||||
id={id}
|
||||
info={node?.info}
|
||||
path={node?.path}
|
||||
value={store.value}
|
||||
onChange={this.handlePanelChangeValue}
|
||||
store={store}
|
||||
manager={manager}
|
||||
popOverContainer={this.getPopOverContainer}
|
||||
readonly={readonly}
|
||||
/>
|
||||
) : null;
|
||||
};
|
||||
|
||||
return panels.length > 0 ? (
|
||||
<div
|
||||
className={cx(
|
||||
'editor-right-panel width-draggable',
|
||||
'AMISCSSWrapper',
|
||||
panels.length > 1 ? 'mul-tabs-panel' : '',
|
||||
isOpenStatus ? '' : 'hidden-status',
|
||||
isFixedStatus ? 'fixed-status' : ''
|
||||
)}
|
||||
>
|
||||
<div
|
||||
className={`editor-panel-btn`}
|
||||
editor-tooltip={isFixedStatus ? '关闭悬浮模式' : '开启悬浮模式'}
|
||||
tooltip-position="left"
|
||||
>
|
||||
<Icon
|
||||
icon={isFixedStatus ? 'editor-fixed' : 'editor-no-fixed'}
|
||||
className="panel-btn"
|
||||
onClick={this.handleFixed}
|
||||
/>
|
||||
</div>
|
||||
<div className="editorPanel-inner">
|
||||
{panels.length === 1 ? (
|
||||
renderPanel(panels[0])
|
||||
) : (
|
||||
<Tabs
|
||||
theme={theme}
|
||||
tabsMode="sidebar"
|
||||
sidePosition="right"
|
||||
className="editorPanel-tabs"
|
||||
linksClassName="editorPanel-tabs-header"
|
||||
contentClassName="editorPanel-tabs-content"
|
||||
activeKey={panelKey}
|
||||
onSelect={this.handleSelect}
|
||||
>
|
||||
{panels.map(panel => {
|
||||
const usePluginIcon = isHasPluginIcon(panel);
|
||||
return (
|
||||
<Tab
|
||||
key={panel.key}
|
||||
eventKey={panel.key}
|
||||
// title={panel.title}
|
||||
// icon={panel.icon}
|
||||
title={
|
||||
<span
|
||||
className="editor-tab-icon"
|
||||
editor-tooltip={panel.title}
|
||||
tooltip-position="left"
|
||||
>
|
||||
{usePluginIcon && (
|
||||
<Icon
|
||||
icon={panel.pluginIcon}
|
||||
className="pluginIcon"
|
||||
/>
|
||||
)}
|
||||
{!usePluginIcon && (
|
||||
<Icon icon={panel.icon} className="fa" />
|
||||
)}
|
||||
</span>
|
||||
}
|
||||
className={`editorPanel-tabs-pane ae-Editor-${panel.key}Pane`}
|
||||
mountOnEnter={true}
|
||||
unmountOnExit={false}
|
||||
>
|
||||
{renderPanel(panel)}
|
||||
</Tab>
|
||||
);
|
||||
})}
|
||||
</Tabs>
|
||||
)}
|
||||
</div>
|
||||
<WidthDraggableBtn isLeftDragIcon={true} />
|
||||
<div
|
||||
className={cx(
|
||||
'right-panel-arrow',
|
||||
isOpenStatus ? '' : 'hidden-status'
|
||||
)}
|
||||
onClick={this.handleHidden}
|
||||
></div>
|
||||
</div>
|
||||
) : null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
/**
|
||||
* 显示可以作为 target 的名称列表
|
||||
*/
|
||||
import {observer} from 'mobx-react';
|
||||
import cx from 'classnames';
|
||||
import React from 'react';
|
||||
import {PanelProps} from '../../plugin';
|
||||
import {autobind} from '../../util';
|
||||
|
||||
@observer
|
||||
export class TargetNamePanel extends React.Component<PanelProps> {
|
||||
@autobind
|
||||
handleClick(e: React.MouseEvent<HTMLElement>) {
|
||||
const {store, manager} = this.props;
|
||||
const editorId = e.currentTarget.getAttribute('data-targetname-id');
|
||||
store.setActiveId(editorId!);
|
||||
}
|
||||
|
||||
@autobind
|
||||
handleEnter(e: React.MouseEvent<HTMLElement>) {
|
||||
const {store, manager} = this.props;
|
||||
const editorId = e.currentTarget.getAttribute('data-targetname-id');
|
||||
store.setHoverId(editorId!);
|
||||
}
|
||||
|
||||
render() {
|
||||
const {store, manager} = this.props;
|
||||
const targetNames = this.props.store.targetNames;
|
||||
|
||||
return (
|
||||
<div className="ae-TargetName">
|
||||
<span>当表单、列表等组件有名字时会出现在这里方便选择</span>
|
||||
<ul className="ae-TargetName-list">
|
||||
{targetNames.map(targetName => {
|
||||
const editorId = targetName.editorId;
|
||||
return (
|
||||
<li
|
||||
className={cx('ae-TargetName-node', {
|
||||
'is-active': store.activeId === editorId,
|
||||
'is-hover': store.isHoved(editorId)
|
||||
})}
|
||||
data-targetname-id={editorId}
|
||||
onMouseEnter={this.handleEnter}
|
||||
onClick={this.handleClick}
|
||||
key={editorId}
|
||||
>
|
||||
<span className="label label-info pull-right">
|
||||
{targetName.type}
|
||||
</span>
|
||||
<a>{targetName.name}</a>
|
||||
</li>
|
||||
);
|
||||
})}
|
||||
</ul>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
}
|
||||
89
packages/amis-editor-core/src/component/PopOverForm.tsx
Normal file
@@ -0,0 +1,89 @@
|
||||
import React from 'react';
|
||||
import {EditorManager} from '../manager';
|
||||
import {EditorStoreType} from '../store/editor';
|
||||
import {
|
||||
render,
|
||||
Modal,
|
||||
getTheme,
|
||||
Icon,
|
||||
Spinner,
|
||||
Button,
|
||||
Overlay,
|
||||
PopOver
|
||||
} from 'amis';
|
||||
import {observer} from 'mobx-react';
|
||||
import {diff} from '../util';
|
||||
import {createObject} from 'amis-core';
|
||||
|
||||
export interface PopOverFormProps {
|
||||
store: EditorStoreType;
|
||||
manager: EditorManager;
|
||||
theme?: string;
|
||||
}
|
||||
|
||||
@observer
|
||||
export class PopOverForm extends React.Component<PopOverFormProps> {
|
||||
overlay = React.createRef<any>();
|
||||
buildSchema() {
|
||||
const {store} = this.props;
|
||||
const popOverFormContext = store.popOverForm!;
|
||||
|
||||
return {
|
||||
type: 'form',
|
||||
wrapWithPanel: false,
|
||||
mode: 'normal',
|
||||
wrapperComponent: 'div',
|
||||
body: popOverFormContext.body,
|
||||
submitOnChange: true,
|
||||
autoFocus: true,
|
||||
onFinished: (newValue: any) => {
|
||||
popOverFormContext.callback?.(
|
||||
newValue,
|
||||
diff(popOverFormContext.value, newValue)
|
||||
);
|
||||
|
||||
setTimeout(() => this.overlay.current?.updatePosition(), 200);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
render() {
|
||||
const {store, theme, manager} = this.props;
|
||||
const popOverFormContext = store.popOverForm;
|
||||
|
||||
return (
|
||||
<Overlay
|
||||
target={popOverFormContext?.target}
|
||||
placement={
|
||||
'left-bottom-left-top left-top-left-bottom right-bottom-right-top right-top-right-bottom center'
|
||||
}
|
||||
show={!!popOverFormContext}
|
||||
ref={this.overlay}
|
||||
>
|
||||
<PopOver
|
||||
overlay
|
||||
className="ae-Editor-popOverForm"
|
||||
onHide={store.closePopOverForm}
|
||||
>
|
||||
{popOverFormContext ? (
|
||||
render(
|
||||
this.buildSchema(),
|
||||
{
|
||||
data: createObject(store.ctx, popOverFormContext?.value),
|
||||
node: popOverFormContext?.node,
|
||||
manager
|
||||
},
|
||||
{
|
||||
...manager.env,
|
||||
session: 'popover-form',
|
||||
theme: theme
|
||||
}
|
||||
)
|
||||
) : (
|
||||
<p>Loading...</p>
|
||||
)}
|
||||
</PopOver>
|
||||
</Overlay>
|
||||
);
|
||||
}
|
||||
}
|
||||
874
packages/amis-editor-core/src/component/Preview.tsx
Normal file
@@ -0,0 +1,874 @@
|
||||
import {
|
||||
render,
|
||||
toast,
|
||||
resolveRenderer,
|
||||
Modal,
|
||||
Icon,
|
||||
resizeSensor,
|
||||
Spinner
|
||||
} from 'amis';
|
||||
import React, {Component} from 'react';
|
||||
import cx from 'classnames';
|
||||
import {autobind, guid, noop, reactionWithOldValue} from '../util';
|
||||
import {clearStoresCache, RenderOptions} from 'amis-core';
|
||||
import type {Schema} from 'amis';
|
||||
import {EditorStoreType} from '../store/editor';
|
||||
import {observer} from 'mobx-react';
|
||||
import {EditorManager} from '../manager';
|
||||
import HighlightBox from './HighlightBox';
|
||||
import RegionHighlightBox from './RegionHLBox';
|
||||
import {ErrorRenderer} from './base/ErrorRenderer';
|
||||
import {isAlive} from 'mobx-state-tree';
|
||||
import {findTree} from 'amis-core';
|
||||
import BackTop from './base/BackTop';
|
||||
import {reaction} from 'mobx';
|
||||
import type {RendererConfig} from 'amis-core';
|
||||
import IFramePreview from './IFramePreview';
|
||||
import {SchemaRenderer} from './SchemaRenderer';
|
||||
|
||||
export interface PreviewProps {
|
||||
// isEditorEnabled?: (
|
||||
// info: any,
|
||||
// path: string,
|
||||
// renderer: any,
|
||||
// schema: any
|
||||
// ) => boolean;
|
||||
|
||||
theme?: string;
|
||||
/** 应用语言类型 */
|
||||
appLocale?: string;
|
||||
amisEnv?: any;
|
||||
className?: string;
|
||||
editable?: boolean;
|
||||
isMobile?: boolean;
|
||||
store: EditorStoreType;
|
||||
manager: EditorManager;
|
||||
data?: any;
|
||||
autoFocus?: boolean;
|
||||
|
||||
toolbarContainer?: () => any;
|
||||
|
||||
readonly?: boolean;
|
||||
ref?: any;
|
||||
}
|
||||
|
||||
export interface PreviewState {
|
||||
ready?: boolean;
|
||||
}
|
||||
|
||||
@observer
|
||||
export default class Preview extends Component<PreviewProps> {
|
||||
currentDom = React.createRef<HTMLDivElement>();
|
||||
dialogReaction: any;
|
||||
env: RenderOptions = {
|
||||
...this.props.manager.env,
|
||||
notify: (type, msg, conf) => {
|
||||
if (this.props.editable) {
|
||||
console.warn('[Notify]', type, msg);
|
||||
return;
|
||||
}
|
||||
|
||||
toast[type]
|
||||
? toast[type](msg, conf || (type === 'error' ? '系统错误' : '系统消息'))
|
||||
: console.warn('[Notify]', type, msg);
|
||||
},
|
||||
theme: this.props.theme,
|
||||
session: `preview-${this.props.manager.id}`,
|
||||
rendererResolver: this.rendererResolver.bind(this)
|
||||
};
|
||||
|
||||
doingSelection = false;
|
||||
|
||||
componentDidMount() {
|
||||
const currentDom = this.currentDom.current!;
|
||||
currentDom.addEventListener('mouseleave', this.handleMouseLeave);
|
||||
currentDom.addEventListener('mousemove', this.handleMouseMove);
|
||||
currentDom.addEventListener('click', this.handleClick, true);
|
||||
currentDom.addEventListener('dblclick', this.handleDBClick, true);
|
||||
currentDom.addEventListener('mouseover', this.handeMouseOver);
|
||||
currentDom.addEventListener('mousedown', this.handeMouseDown);
|
||||
currentDom.addEventListener('submit', this.handleSubmit);
|
||||
this.props.manager.on('after-update', this.handlePreviewViewChange);
|
||||
}
|
||||
|
||||
componentWillUnmount() {
|
||||
if (this.currentDom.current) {
|
||||
const currentDom = this.currentDom.current!;
|
||||
currentDom.removeEventListener('mouseleave', this.handleMouseLeave);
|
||||
currentDom.removeEventListener('mousemove', this.handleMouseMove);
|
||||
currentDom.removeEventListener('click', this.handleClick, true);
|
||||
currentDom.removeEventListener('dblclick', this.handleDBClick, true);
|
||||
currentDom.removeEventListener('mouseover', this.handeMouseOver);
|
||||
currentDom.removeEventListener('mousedown', this.handeMouseDown);
|
||||
currentDom.removeEventListener('submit', this.handleSubmit);
|
||||
this.props.manager.off('after-update', this.handlePreviewViewChange);
|
||||
this.dialogReaction?.();
|
||||
}
|
||||
|
||||
setTimeout(() => clearStoresCache([this.env.session!]), 500);
|
||||
}
|
||||
|
||||
unSensor?: () => void;
|
||||
layer?: HTMLDivElement;
|
||||
scrollLayer?: HTMLDivElement;
|
||||
|
||||
@autobind
|
||||
handleLayerScroll(e: WheelEvent) {
|
||||
e.preventDefault();
|
||||
e.stopPropagation();
|
||||
|
||||
const store = this.props.store;
|
||||
const iframe = store.getIframe();
|
||||
|
||||
if (iframe) {
|
||||
iframe.contentWindow?.scrollTo({
|
||||
top: iframe.contentWindow?.scrollY + e.deltaY,
|
||||
behavior: 'smooth'
|
||||
});
|
||||
this.handlePreviewViewChange();
|
||||
} else {
|
||||
this.scrollLayer?.scrollTo({
|
||||
top: this.scrollLayer.scrollTop + e.deltaY,
|
||||
behavior: 'smooth'
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@autobind
|
||||
contentsRef(ref: HTMLDivElement | null) {
|
||||
if (ref) {
|
||||
this.layer = ref
|
||||
.closest('.ae-Preview')!
|
||||
.querySelector('.ae-Preview-widgets') as HTMLDivElement;
|
||||
|
||||
this.unSensor = resizeSensor(ref, this.handlePreviewViewChange);
|
||||
if (this.props.isMobile) {
|
||||
ref = ref.firstChild as HTMLDivElement;
|
||||
}
|
||||
|
||||
this.scrollLayer = ref as HTMLDivElement;
|
||||
this.scrollLayer.removeEventListener(
|
||||
'scroll',
|
||||
this.handlePreviewViewChange
|
||||
);
|
||||
this.scrollLayer.addEventListener('scroll', this.handlePreviewViewChange);
|
||||
this.props.store.setLayer(this.layer);
|
||||
|
||||
this.layer.addEventListener('wheel', this.handleLayerScroll, true);
|
||||
} else {
|
||||
this.layer?.removeEventListener('wheel', this.handleLayerScroll, true);
|
||||
this.scrollLayer?.removeEventListener(
|
||||
'scroll',
|
||||
this.handlePreviewViewChange
|
||||
);
|
||||
delete this.scrollLayer;
|
||||
delete this.layer;
|
||||
this.unSensor?.();
|
||||
delete this.unSensor;
|
||||
}
|
||||
}
|
||||
|
||||
// 优化这块
|
||||
readonly unReaction: () => void = reactionWithOldValue(
|
||||
() => [this.getHighlightNodes(), this.props.store.activeId],
|
||||
([ids]: [Array<string>], oldValue: [Array<string>]) => {
|
||||
const store = this.props.store;
|
||||
// requestAnimationFrame(() => {
|
||||
// this.calculateHighlightBox(ids);
|
||||
// });
|
||||
store.activeHighlightNodes(ids);
|
||||
let oldIds = oldValue?.[0];
|
||||
|
||||
if (Array.isArray(oldIds)) {
|
||||
oldIds = oldIds.filter(id => !~ids.indexOf(id));
|
||||
store.deActiveHighlightNodes(oldIds);
|
||||
// store.resetHighlightBox(oldIds);
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
@autobind
|
||||
handlePreviewViewChange() {
|
||||
if (this.layer && this.scrollLayer) {
|
||||
requestAnimationFrame(() => {
|
||||
if (!this.layer) {
|
||||
return;
|
||||
}
|
||||
this.layer!.style.cssText += `transform: translate(0, -${
|
||||
this.scrollLayer!.scrollTop
|
||||
}px);`;
|
||||
});
|
||||
}
|
||||
|
||||
requestAnimationFrame(() =>
|
||||
this.calculateHighlightBox(this.getHighlightNodes())
|
||||
);
|
||||
}
|
||||
|
||||
getHighlightNodes() {
|
||||
const store = this.props.store;
|
||||
return store.highlightNodes.map(item => item.id);
|
||||
}
|
||||
|
||||
@autobind
|
||||
calculateHighlightBox(ids: Array<string>) {
|
||||
const store = this.props.store;
|
||||
this.layer && store.calculateHighlightBox(ids);
|
||||
}
|
||||
|
||||
@autobind
|
||||
handeMouseDown(e: MouseEvent) {
|
||||
const isLeftButton =
|
||||
(e.button === 1 && window.event !== null) || e.button === 0;
|
||||
if (!this.props.editable || !isLeftButton || e.defaultPrevented) return;
|
||||
|
||||
// todo highlight 改成可拖拽后,框选逻辑异常了,需要修复
|
||||
const store = this.props.store;
|
||||
if (
|
||||
e.defaultPrevented ||
|
||||
(e.target as HTMLElement)?.closest('[draggable]') ||
|
||||
store.activeElement
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
const dom = this.layer as HTMLElement;
|
||||
if (!dom) {
|
||||
return;
|
||||
}
|
||||
|
||||
let cursor: HTMLDivElement | null = null;
|
||||
const rect = dom.getBoundingClientRect();
|
||||
const startX = e.pageX;
|
||||
const startY = e.pageY;
|
||||
const x = startX - rect.left;
|
||||
const y = startY - rect.top;
|
||||
|
||||
let onMove = (e: MouseEvent) => {
|
||||
if (!cursor) {
|
||||
cursor = document.createElement('div');
|
||||
cursor.classList.add('ae-Editor-selectionCursor');
|
||||
dom.appendChild(cursor);
|
||||
}
|
||||
|
||||
this.doingSelection = true;
|
||||
const w = e.pageX - startX;
|
||||
const h = e.pageY - startY;
|
||||
cursor.style.cssText = `left: ${x + Math.min(w, 0)}px; top: ${
|
||||
y + Math.min(h, 0)
|
||||
}px; width: ${Math.abs(w)}px; height: ${Math.abs(h)}px;`;
|
||||
};
|
||||
let onUp = (e: MouseEvent) => {
|
||||
this.doingSelection = false;
|
||||
window.removeEventListener('mousemove', onMove);
|
||||
window.removeEventListener('mouseup', onUp);
|
||||
cursor && dom.removeChild(cursor);
|
||||
|
||||
const w = e.pageX - startX;
|
||||
const h = e.pageY - startY;
|
||||
const rect = {
|
||||
x: x + Math.min(w, 0),
|
||||
y: y + Math.min(h, 0),
|
||||
w: Math.abs(w),
|
||||
h: Math.abs(h)
|
||||
};
|
||||
|
||||
if (Math.hypot(w, h) < 10) {
|
||||
return;
|
||||
}
|
||||
|
||||
// 阻止 click 事件触发。
|
||||
let captureClick = (e: MouseEvent) => {
|
||||
window.removeEventListener('click', captureClick, true);
|
||||
e.preventDefault();
|
||||
e.stopPropagation();
|
||||
};
|
||||
window.addEventListener('click', captureClick, true);
|
||||
setTimeout(
|
||||
() => window.removeEventListener('click', captureClick, true),
|
||||
350
|
||||
);
|
||||
|
||||
this.doSelection(rect);
|
||||
};
|
||||
window.addEventListener('mousemove', onMove);
|
||||
window.addEventListener('mouseup', onUp);
|
||||
}
|
||||
|
||||
/** 拖拽多选 */
|
||||
doSelection(rect: {x: number; y: number; w: number; h: number}) {
|
||||
const layer = this.layer;
|
||||
const dom = this.currentDom.current;
|
||||
if (!layer || !dom) {
|
||||
return;
|
||||
}
|
||||
|
||||
const selections: Array<HTMLElement> = [];
|
||||
const layerRect = layer.getBoundingClientRect();
|
||||
const frameRect = {
|
||||
left: rect.x + layerRect.left,
|
||||
top: rect.y + layerRect.top,
|
||||
width: rect.w,
|
||||
height: rect.h,
|
||||
right: rect.x + layerRect.left + rect.w,
|
||||
bottom: rect.y + layerRect.top + rect.h
|
||||
};
|
||||
const nodes = dom.querySelectorAll(`[data-editor-id]`);
|
||||
[].slice.apply(nodes).forEach((node: HTMLElement) => {
|
||||
if (selections.some(container => container.contains(node))) {
|
||||
return;
|
||||
}
|
||||
|
||||
const nodeRect = node.getBoundingClientRect();
|
||||
|
||||
// 完全包含
|
||||
if (
|
||||
frameRect.left <= nodeRect.left &&
|
||||
frameRect.top <= nodeRect.top &&
|
||||
frameRect.right >= nodeRect.right &&
|
||||
frameRect.bottom >= nodeRect.bottom
|
||||
) {
|
||||
~selections.indexOf(node) || selections.push(node);
|
||||
}
|
||||
});
|
||||
const ids = selections
|
||||
.map(item => item.getAttribute('data-editor-id')!)
|
||||
.filter((id: string, idx, list) => list.indexOf(id) === idx);
|
||||
ids.length && this.props.manager.setSelection(ids);
|
||||
}
|
||||
|
||||
@autobind
|
||||
handleClick(e: MouseEvent) {
|
||||
const store = this.props.store;
|
||||
|
||||
// 处于编辑态时,不响应点击事件
|
||||
if (store.activeElement) {
|
||||
// 同时阻止渲染器里面的 click 事件
|
||||
e.preventDefault();
|
||||
return;
|
||||
}
|
||||
|
||||
if (
|
||||
(e.target as HTMLElement).closest(
|
||||
'.ae-editor-action-btn,.ae-Editor-toolbarPopover '
|
||||
)
|
||||
) {
|
||||
// 设计器内容区中允许点击的元素,比如:回到顶部功能按钮。
|
||||
// 或者高亮区的操作按钮
|
||||
return;
|
||||
}
|
||||
|
||||
if (e.defaultPrevented) {
|
||||
e.stopPropagation();
|
||||
return;
|
||||
}
|
||||
|
||||
const target = (e.target as HTMLElement).closest(
|
||||
`[data-editor-id],[data-hlbox-id]`
|
||||
);
|
||||
|
||||
if (target?.matches('[data-editor-id]')) {
|
||||
const curActiveId = target.getAttribute('data-editor-id');
|
||||
|
||||
e.metaKey
|
||||
? this.props.manager.toggleSelection(curActiveId!)
|
||||
: store.setActiveId(curActiveId!);
|
||||
} else if (target?.matches('[data-hlbox-id]')) {
|
||||
const id = target.getAttribute('data-hlbox-id')!;
|
||||
const {x, y} = this.getElementPoint(e);
|
||||
let elements = store.getDoc().elementsFromPoint(x, y);
|
||||
|
||||
let node = elements.find(
|
||||
(ele: Element) =>
|
||||
ele.hasAttribute('data-editor-id') &&
|
||||
ele.getAttribute('data-editor-id') !== id &&
|
||||
!ele.querySelector(`[data-editor-id="${id}"]`)
|
||||
);
|
||||
if (node) {
|
||||
const nodeId = node.getAttribute('data-editor-id')!;
|
||||
// 如果已经进入了内联模式
|
||||
// 不要再切选中了
|
||||
setTimeout(() => {
|
||||
store.activeElement || store.setActiveId(nodeId);
|
||||
}, 350);
|
||||
}
|
||||
}
|
||||
|
||||
if (!this.layer?.contains(e.target as HTMLElement) && this.props.editable) {
|
||||
// 让渲染器不可点,只能点击选中。
|
||||
const event = this.props.manager.trigger('prevent-click', {
|
||||
data: e
|
||||
});
|
||||
|
||||
if (!event.prevented && !event.stoped) {
|
||||
e.preventDefault();
|
||||
e.stopPropagation();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@autobind
|
||||
handleDBClick(e: MouseEvent) {
|
||||
const store = this.props.store;
|
||||
let target = e.target as HTMLElement;
|
||||
let hostElem = target.closest(`[data-editor-id]`) as HTMLElement;
|
||||
|
||||
if (!hostElem) {
|
||||
const hlbox = target.closest(`[data-hlbox-id]`) as HTMLElement;
|
||||
// 自由容器里面的高亮区域是可以点击的
|
||||
// 当点击来自高亮区域时,需要根据位置计算出组件上点击的元素
|
||||
if (hlbox) {
|
||||
const {x, y} = this.getElementPoint(e);
|
||||
const elements = store.getDoc().elementsFromPoint(x, y);
|
||||
target = elements.find((ele: Element) => {
|
||||
hostElem = ele.closest(
|
||||
`[data-editor-id="${hlbox.getAttribute('data-hlbox-id')}"]`
|
||||
) as HTMLElement;
|
||||
return hostElem;
|
||||
}) as HTMLElement;
|
||||
}
|
||||
}
|
||||
|
||||
if (hostElem) {
|
||||
const node = store.getNodeById(hostElem.getAttribute('data-editor-id')!);
|
||||
if (!node) {
|
||||
return;
|
||||
}
|
||||
e.preventDefault();
|
||||
const rendererInfo = node.info;
|
||||
|
||||
// 需要支持 :scope > xxx 语法,所以才这么写
|
||||
let inlineElem: HTMLElement | undefined | null = null;
|
||||
const inlineSetting = (rendererInfo.inlineEditableElements || []).find(
|
||||
elem => {
|
||||
inlineElem = (
|
||||
[].slice.call(
|
||||
hostElem.querySelectorAll(elem.match)
|
||||
) as Array<HTMLElement>
|
||||
).find(dom => dom.contains(target));
|
||||
return !!inlineElem;
|
||||
}
|
||||
)!;
|
||||
|
||||
// 如果命中了支持内联编辑的元素,则开始内联编辑
|
||||
if (inlineElem && inlineSetting) {
|
||||
const manager = this.props.manager;
|
||||
manager.startInlineEdit(node, inlineElem, inlineSetting, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@autobind
|
||||
handleNavSwitch(id: string) {
|
||||
const store = this.props.store;
|
||||
store.setActiveId(id);
|
||||
}
|
||||
|
||||
@autobind
|
||||
handleMouseMove(e: MouseEvent) {
|
||||
if (this.doingSelection || this.props.manager.disableHover) {
|
||||
return;
|
||||
}
|
||||
|
||||
const store = this.props.store;
|
||||
const dom = e.target as HTMLElement;
|
||||
|
||||
if ((e.target as HTMLElement).closest('.ignore-hover-elem')) {
|
||||
// 设计器内容区中忽略hover的元素,比如:region头部标签。
|
||||
return;
|
||||
}
|
||||
|
||||
const target = dom.closest(`[data-editor-id],[data-hlbox-id]`);
|
||||
|
||||
if (target?.matches('[data-editor-id]')) {
|
||||
const curHoverId = target.getAttribute('data-editor-id');
|
||||
store.setHoverId(curHoverId!);
|
||||
} else if (target?.matches('[data-hlbox-id]')) {
|
||||
const {x, y} = this.getElementPoint(e);
|
||||
const elements = store.getDoc().elementsFromPoint(x, y);
|
||||
let hostElem: HTMLElement | null = null;
|
||||
for (const ele of elements) {
|
||||
hostElem = ele.closest(`[data-editor-id]`) as HTMLElement;
|
||||
if (hostElem) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (hostElem) {
|
||||
// 判断当前是否在子区域
|
||||
const curHoverId = hostElem.getAttribute('data-editor-id');
|
||||
store.setHoverId(curHoverId!);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
getElementPoint(e: MouseEvent) {
|
||||
const store = this.props.store;
|
||||
let x = e.clientX;
|
||||
let y = e.clientY;
|
||||
|
||||
const iframe = store.getIframe();
|
||||
|
||||
// 计算鼠标位置在页面中的实际位置,如果iframe存在,需要考虑iframe偏移量以及iframe的缩放比例
|
||||
if (iframe) {
|
||||
const preview: HTMLElement = (store.getLayer() as HTMLElement)
|
||||
.previousSibling?.firstChild as HTMLElement;
|
||||
const previewRect = preview.getBoundingClientRect();
|
||||
|
||||
x -= previewRect.left;
|
||||
y -= previewRect.top;
|
||||
// 如果有缩放比例,重新计算位置
|
||||
const scale = store.getScale();
|
||||
if (scale >= 0) {
|
||||
x = x / scale;
|
||||
y = y / scale;
|
||||
}
|
||||
}
|
||||
|
||||
return {x, y};
|
||||
}
|
||||
|
||||
@autobind
|
||||
handleMouseLeave() {
|
||||
const store = this.props.store;
|
||||
store.setHoverId('');
|
||||
}
|
||||
|
||||
@autobind
|
||||
handeMouseOver(e: MouseEvent) {
|
||||
if (this.props.editable) {
|
||||
e.preventDefault();
|
||||
e.stopPropagation();
|
||||
}
|
||||
}
|
||||
|
||||
// 禁用内部的提交事件
|
||||
handleSubmit(e: Event) {
|
||||
e.preventDefault();
|
||||
e.stopPropagation();
|
||||
}
|
||||
|
||||
@autobind
|
||||
handleDragEnter(e: React.DragEvent) {
|
||||
if (!this.props.editable) {
|
||||
// 非编辑态下不监听拖拽事件
|
||||
return;
|
||||
}
|
||||
const manager = this.props.manager;
|
||||
manager.dnd.dragEnter(e.nativeEvent);
|
||||
}
|
||||
|
||||
@autobind
|
||||
handleDragLeave(e: React.DragEvent) {
|
||||
if (!this.props.editable) {
|
||||
return;
|
||||
}
|
||||
const manager = this.props.manager;
|
||||
manager.dnd.dragLeave(e.nativeEvent);
|
||||
}
|
||||
|
||||
@autobind
|
||||
handleDragOver(e: React.DragEvent) {
|
||||
if (!this.props.editable) {
|
||||
return;
|
||||
}
|
||||
const manager = this.props.manager;
|
||||
manager.dnd.dragOver(e.nativeEvent);
|
||||
}
|
||||
|
||||
@autobind
|
||||
handleDrop(e: React.DragEvent) {
|
||||
if (!this.props.editable) {
|
||||
return;
|
||||
}
|
||||
const manager = this.props.manager;
|
||||
manager.dnd.drop(e.nativeEvent);
|
||||
}
|
||||
|
||||
@autobind
|
||||
handleWidgetsDragEnter(e: React.DragEvent) {
|
||||
const target = e.target as HTMLElement;
|
||||
const dom = target.closest(`[data-node-id][data-node-region].region-tip`);
|
||||
|
||||
if (!dom) {
|
||||
return;
|
||||
}
|
||||
|
||||
e.preventDefault();
|
||||
const manager = this.props.manager;
|
||||
const id = dom.getAttribute('data-node-id')!;
|
||||
const region = dom.getAttribute('data-node-region')!; // 大纲树中的容器节点
|
||||
|
||||
id && region && manager.dnd.switchToRegion(e.nativeEvent, id, region);
|
||||
}
|
||||
|
||||
@autobind
|
||||
getCurrentTarget() {
|
||||
const isMobile = this.props.isMobile;
|
||||
if (isMobile) {
|
||||
return this.currentDom.current!.querySelector(
|
||||
'.ae-Preview-inner'
|
||||
) as HTMLDivElement;
|
||||
} else {
|
||||
return this.currentDom.current!.querySelector(
|
||||
'.ae-Preview-body'
|
||||
) as HTMLDivElement;
|
||||
}
|
||||
}
|
||||
|
||||
rendererResolver(path: string, schema: Schema, props: any) {
|
||||
const {editable, manager} = this.props;
|
||||
|
||||
let renderer: RendererConfig = resolveRenderer(path, schema)!;
|
||||
if (editable === false) {
|
||||
return renderer;
|
||||
}
|
||||
|
||||
renderer = renderer || {
|
||||
name: 'error',
|
||||
test: () => true,
|
||||
component: ErrorRenderer
|
||||
};
|
||||
|
||||
let info = manager.getEditorInfo(renderer!, path, schema);
|
||||
|
||||
info &&
|
||||
(renderer = {
|
||||
...renderer,
|
||||
component: manager.makeWrapper(info, renderer)
|
||||
});
|
||||
|
||||
return renderer;
|
||||
}
|
||||
|
||||
render() {
|
||||
const {
|
||||
className,
|
||||
editable,
|
||||
store,
|
||||
manager,
|
||||
amisEnv,
|
||||
theme,
|
||||
isMobile,
|
||||
autoFocus,
|
||||
toolbarContainer,
|
||||
appLocale,
|
||||
...rest
|
||||
} = this.props;
|
||||
|
||||
const env = {
|
||||
...this.env,
|
||||
...amisEnv
|
||||
};
|
||||
|
||||
return (
|
||||
<div
|
||||
id="editor-preview-body"
|
||||
onDragEnter={this.handleDragEnter}
|
||||
onDragLeave={this.handleDragLeave}
|
||||
onDragOver={this.handleDragOver}
|
||||
onDrop={this.handleDrop}
|
||||
className={cx(
|
||||
'ae-Preview',
|
||||
'AMISCSSWrapper',
|
||||
className,
|
||||
isMobile ? 'is-mobile-body' : 'is-pc-body'
|
||||
)}
|
||||
ref={this.currentDom}
|
||||
>
|
||||
<div className={cx('ae-Preview-outter')}>
|
||||
<div
|
||||
key={
|
||||
/* contentsLayer 逻辑不一样需要更新一下 */ isMobile
|
||||
? 'mobile-body'
|
||||
: 'pc-body'
|
||||
}
|
||||
className={cx(
|
||||
'ae-Preview-body',
|
||||
className,
|
||||
editable ? 'is-edting' : '',
|
||||
isMobile ? 'is-mobile' : 'is-pc hoverShowScrollBar'
|
||||
)}
|
||||
ref={this.contentsRef}
|
||||
>
|
||||
<div className="ae-Preview-inner">
|
||||
{!store.ready ? (
|
||||
<div className="ae-Preview-loading">
|
||||
<Spinner overlay size="lg" />
|
||||
</div>
|
||||
) : isMobile ? (
|
||||
<IFramePreview
|
||||
{...rest}
|
||||
key="mobile"
|
||||
editable={editable}
|
||||
store={store}
|
||||
env={env}
|
||||
manager={manager}
|
||||
autoFocus={autoFocus}
|
||||
appLocale={appLocale}
|
||||
></IFramePreview>
|
||||
) : (
|
||||
<SmartPreview
|
||||
{...rest}
|
||||
editable={editable}
|
||||
autoFocus={autoFocus}
|
||||
store={store}
|
||||
env={env}
|
||||
manager={manager}
|
||||
key="pc"
|
||||
appLocale={appLocale}
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
{this.currentDom.current && (
|
||||
<BackTop
|
||||
key={isMobile ? 'mobile-back-up' : 'pc-back-up'}
|
||||
className="ae-editor-action-btn"
|
||||
target={this.getCurrentTarget.bind(this)}
|
||||
onClick={(e: any) => {
|
||||
console.log(e);
|
||||
}}
|
||||
>
|
||||
<Icon icon="back-up" className="back-top-icon" />
|
||||
</BackTop>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div
|
||||
onDragEnter={this.handleWidgetsDragEnter}
|
||||
className="ae-Preview-widgets"
|
||||
id="aePreviewHighlightBox"
|
||||
>
|
||||
{store.highlightNodes.map(node => (
|
||||
<HighlightBox
|
||||
node={node}
|
||||
key={node.id}
|
||||
store={store}
|
||||
id={node.id}
|
||||
title={node.label}
|
||||
toolbarContainer={toolbarContainer}
|
||||
onSwitch={this.handleNavSwitch}
|
||||
manager={manager}
|
||||
readonly={this.props.readonly}
|
||||
>
|
||||
{node.childRegions.map(region =>
|
||||
!node.memberImmutable(region.region) &&
|
||||
!store.activeElement &&
|
||||
!this.props.readonly &&
|
||||
store.isRegionActive(region.id, region.region) ? (
|
||||
<RegionHighlightBox
|
||||
manager={manager}
|
||||
key={region.region}
|
||||
store={store}
|
||||
node={region}
|
||||
id={region.id}
|
||||
name={region.region}
|
||||
title={region.label}
|
||||
preferTag={region.preferTag}
|
||||
isOnlyChildRegion={node.childRegions.length === 1}
|
||||
/>
|
||||
) : null
|
||||
)}
|
||||
</HighlightBox>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 这个用了 observer,所以能最小程度的刷新,数据不变按理是不会刷新的。
|
||||
*/
|
||||
export interface SmartPreviewProps {
|
||||
editable?: boolean;
|
||||
autoFocus?: boolean;
|
||||
store: EditorStoreType;
|
||||
env: any;
|
||||
data?: any;
|
||||
manager: EditorManager;
|
||||
/** 应用语言类型 */
|
||||
appLocale?: string;
|
||||
}
|
||||
@observer
|
||||
class SmartPreview extends React.Component<SmartPreviewProps> {
|
||||
dialogMountRef: React.RefObject<HTMLDivElement> = React.createRef();
|
||||
|
||||
componentDidMount() {
|
||||
const store = this.props.store;
|
||||
|
||||
if (this.props.autoFocus) {
|
||||
// 一般弹框动画差不多 350ms
|
||||
// 延时 350ms,在弹框中展示编辑器效果要好点。
|
||||
setTimeout(() => {
|
||||
if (isAlive(store)) {
|
||||
const first = findTree(
|
||||
store.outline,
|
||||
item => !item.isRegion && item.clickable
|
||||
);
|
||||
if (first && isAlive(store)) {
|
||||
let region = first.childRegions.find(
|
||||
(item: any) => item.region === 'body'
|
||||
)?.region;
|
||||
region =
|
||||
region ?? first.childRegions.find((i: any) => i.region)?.region;
|
||||
store.setActiveId(first.id, region);
|
||||
}
|
||||
}
|
||||
}, 350);
|
||||
} else {
|
||||
this.props.manager.buildRenderersAndPanels();
|
||||
}
|
||||
}
|
||||
|
||||
componentDidUpdate(prevProps: SmartPreviewProps) {
|
||||
const props = this.props;
|
||||
|
||||
if (props.editable !== prevProps.editable) {
|
||||
if (props.editable) {
|
||||
// 当预览状态切换到设计状态
|
||||
this.props.manager.trigger('preview2editor', {
|
||||
data: this.props.manager
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@autobind
|
||||
getDialogMountRef() {
|
||||
return this.dialogMountRef.current;
|
||||
}
|
||||
|
||||
render() {
|
||||
const {editable, store, appLocale, autoFocus, env, data, manager, ...rest} =
|
||||
this.props;
|
||||
|
||||
return (
|
||||
// 弹窗挂载节点
|
||||
<div ref={this.dialogMountRef} className="ae-PageWrapper">
|
||||
{render(
|
||||
editable ? store.filteredSchema : store.filteredSchemaForPreview,
|
||||
{
|
||||
globalVars: store.globalVariables,
|
||||
...rest,
|
||||
key: editable ? 'edit-mode' : 'preview-mode',
|
||||
theme: env.theme,
|
||||
data: data,
|
||||
context: store.ctx,
|
||||
locale: appLocale,
|
||||
editorDialogMountNode: this.getDialogMountRef
|
||||
},
|
||||
{
|
||||
...env,
|
||||
session: `${env.session}-${
|
||||
editable ? 'edit' : 'preview'
|
||||
}-smart-preview`,
|
||||
SchemaRenderer: editable ? SchemaRenderer : undefined,
|
||||
enableAMISDebug: !editable
|
||||
}
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
}
|
||||
91
packages/amis-editor-core/src/component/RegionHLBox.tsx
Normal file
@@ -0,0 +1,91 @@
|
||||
import React from 'react';
|
||||
import cx from 'classnames';
|
||||
import {Icon} from 'amis';
|
||||
import {EditorStoreType} from '../store/editor';
|
||||
import {observer} from 'mobx-react';
|
||||
import {EditorManager} from '../manager';
|
||||
import {EditorNodeType} from '../store/node';
|
||||
import {autobind} from '../util';
|
||||
import {isAlive} from 'mobx-state-tree';
|
||||
|
||||
export const AddBTNSvg = `<svg viewBox="0 0 12 12">
|
||||
<g stroke="none" stroke-width="1" fill="none" fill-rule="evenodd">
|
||||
<g id="plus" fill="currentColor" fill-rule="nonzero">
|
||||
<polygon points="6.6 6.6 6.6 12 5.4 12 5.4 6.6 0 6.6 0 5.4 5.4 5.4 5.4 0 6.6 0 6.6 5.4 12 5.4 12 6.6"></polygon>
|
||||
</g>
|
||||
</g>
|
||||
</svg>`;
|
||||
|
||||
export interface HighlightBoxProps {
|
||||
store: EditorStoreType;
|
||||
node: EditorNodeType;
|
||||
name: string;
|
||||
id: string;
|
||||
className?: string;
|
||||
title: string;
|
||||
preferTag?: string;
|
||||
manager: EditorManager;
|
||||
isOnlyChildRegion: boolean;
|
||||
}
|
||||
|
||||
// 改成 sfc 函数组件 可以消除一个警告 但是不知道为什么
|
||||
// 可以消除 Can't perform a React state update on an unmounted component
|
||||
export default observer(function (props: HighlightBoxProps) {
|
||||
const {manager, store, id, name, title, node, isOnlyChildRegion} = props;
|
||||
const handleClick = React.useCallback(() => {
|
||||
manager.emptyRegion(id, name);
|
||||
}, [id, name, manager]);
|
||||
|
||||
let isHiglight = store.isRegionHighlighted(id, name);
|
||||
let isDragEnter = store.isRegionDragEnter(id, name);
|
||||
const host = store.getNodeById(id)!;
|
||||
const dx = node.x - host.x;
|
||||
const dy = node.y - host.y;
|
||||
|
||||
return (
|
||||
<div
|
||||
data-renderer={node.host.info.renderer.name}
|
||||
data-region={name}
|
||||
className={cx(
|
||||
'ae-Editor-rhlbox',
|
||||
isDragEnter ? 'is-dragenter' : '',
|
||||
store.planDropId === id ? 'region-can-be-drop' : '',
|
||||
// !isOnlyChildRegion && isHiglightHover ? 'region-hover' : '',
|
||||
isOnlyChildRegion || isHiglight ? 'is-highlight' : ''
|
||||
// dx < 87 && dy < 21 && node.x < 190 ? 'region-label-within' : ''
|
||||
)}
|
||||
style={{
|
||||
width: node.w,
|
||||
height: node.h,
|
||||
borderWidth: `${Math.max(0, dy)}px ${Math.max(
|
||||
0,
|
||||
host.w - dx - node.w
|
||||
)}px ${Math.max(0, host.h - dy - node.h)}px ${Math.max(0, dx)}px`
|
||||
}}
|
||||
>
|
||||
<div
|
||||
data-node-id={id}
|
||||
data-node-region={name}
|
||||
className={`region-tip ${
|
||||
isOnlyChildRegion ? 'is-only-child-region' : ''
|
||||
} ignore-hover-elem`}
|
||||
>
|
||||
<span className="region-text">{title}</span>
|
||||
{store.dragging ? null : <span className="margin-space">|</span>}
|
||||
|
||||
{store.dragging ? null : (
|
||||
<button
|
||||
type="button"
|
||||
className="clear-icon-btn"
|
||||
title={''}
|
||||
data-tooltip={'点击清空当前区域'}
|
||||
data-position={'bottom'}
|
||||
onClick={handleClick}
|
||||
>
|
||||
<Icon icon="clear-btn" />
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
});
|
||||
128
packages/amis-editor-core/src/component/RegionWrapper.tsx
Normal file
@@ -0,0 +1,128 @@
|
||||
import {isAlive} from 'mobx-state-tree';
|
||||
import React from 'react';
|
||||
import {findDOMNode} from 'react-dom';
|
||||
import {EditorManager} from '../manager';
|
||||
import {RegionConfig, RendererInfo} from '../plugin';
|
||||
import {needFillPlaceholder} from '../util';
|
||||
import {EditorStoreType} from '../store/editor';
|
||||
import {EditorNodeContext, EditorNodeType} from '../store/node';
|
||||
|
||||
export interface RegionWrapperProps {
|
||||
name: string;
|
||||
label: string;
|
||||
placeholder?: string | JSX.Element;
|
||||
preferTag?: string;
|
||||
wrapperResolve?: (dom: HTMLElement) => HTMLElement;
|
||||
editorStore: EditorStoreType;
|
||||
manager: EditorManager;
|
||||
rendererName?: string;
|
||||
regionConfig: RegionConfig;
|
||||
node?: EditorNodeType; // 虚拟dom节点信息
|
||||
$$editor?: RendererInfo; // 当前节点信息(info)
|
||||
children?: React.ReactNode;
|
||||
}
|
||||
|
||||
/**
|
||||
* 1.DOM标记 添加 data-region、data-region-host 和 data-renderer 属性
|
||||
* 2.构建 Node store 节点。
|
||||
*/
|
||||
export class RegionWrapper extends React.Component<RegionWrapperProps> {
|
||||
static contextType = EditorNodeContext;
|
||||
parentNode: EditorNodeType;
|
||||
editorNode: EditorNodeType;
|
||||
|
||||
UNSAFE_componentWillMount() {
|
||||
this.parentNode = (this.context as any)!;
|
||||
|
||||
/**
|
||||
* 当前parent为空时尝试通过节点id获取当前上下文
|
||||
* 备注:非react容器类自定义组件需要
|
||||
*/
|
||||
const {$$editor, manager} = this.props;
|
||||
if (!this.parentNode && $$editor && $$editor.id) {
|
||||
const curContext = manager.store.getNodeById($$editor.id);
|
||||
if (curContext) {
|
||||
this.parentNode = curContext;
|
||||
}
|
||||
}
|
||||
|
||||
if (!this.parentNode) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.editorNode = this.parentNode.addChild({
|
||||
id: this.parentNode.id,
|
||||
type: this.parentNode.type,
|
||||
label: this.props.label,
|
||||
path: `${this.parentNode.path}/${this.props.name}`,
|
||||
region: this.props.name, // regions中的key值
|
||||
regionInfo: this.props.regionConfig,
|
||||
preferTag: this.props.preferTag
|
||||
});
|
||||
}
|
||||
|
||||
componentDidMount() {
|
||||
if (this.editorNode && isAlive(this.editorNode)) {
|
||||
this.editorNode &&
|
||||
this.markDom(
|
||||
this.editorNode.id,
|
||||
this.props.name,
|
||||
this.props.rendererName
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
componentDidUpdate(prevProps: RegionWrapperProps) {
|
||||
this.editorNode &&
|
||||
this.markDom(
|
||||
this.editorNode.id,
|
||||
this.props.name,
|
||||
this.props.rendererName
|
||||
);
|
||||
}
|
||||
|
||||
componentWillUnmount() {
|
||||
if (this.editorNode && isAlive(this.editorNode) && this.parentNode) {
|
||||
this.parentNode.removeChild(this.editorNode);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 弄点标记
|
||||
*/
|
||||
markDom(id: string, region: string, rendererName?: string) {
|
||||
const dom = findDOMNode(this) as HTMLElement;
|
||||
|
||||
if (!dom) {
|
||||
return;
|
||||
}
|
||||
const wrapperResolve = this.props.wrapperResolve;
|
||||
const wrapper = wrapperResolve ? wrapperResolve(dom) : dom.parentElement!;
|
||||
|
||||
wrapper.setAttribute('data-region', region);
|
||||
wrapper.setAttribute('data-region-host', id);
|
||||
rendererName && wrapper.setAttribute('data-renderer', rendererName);
|
||||
}
|
||||
|
||||
render() {
|
||||
const isLayoutItem =
|
||||
this.props.rendererName === 'wrapper' ||
|
||||
this.props.rendererName === 'container';
|
||||
let isNeedFillPlaceholder = false;
|
||||
if (needFillPlaceholder(this.props)) {
|
||||
isNeedFillPlaceholder = true;
|
||||
}
|
||||
return (
|
||||
<EditorNodeContext.Provider value={this.editorNode}>
|
||||
{this.props.children}
|
||||
<span
|
||||
className={`ae-Region-placeholder ${
|
||||
isLayoutItem ? 'layout-content' : ''
|
||||
} ${isNeedFillPlaceholder ? 'fill-placeholder' : ''}`}
|
||||
>
|
||||
{this.props.placeholder || this.props.label}
|
||||
</span>
|
||||
</EditorNodeContext.Provider>
|
||||
);
|
||||
}
|
||||
}
|
||||
109
packages/amis-editor-core/src/component/RendererThumb.tsx
Normal file
@@ -0,0 +1,109 @@
|
||||
import type {Schema} from 'amis-core';
|
||||
import {LazyComponent} from 'amis-core';
|
||||
import React from 'react';
|
||||
import {resizeSensor, render, Icon} from 'amis';
|
||||
|
||||
interface ThumbProps {
|
||||
schema: Schema;
|
||||
theme?: string;
|
||||
env: any;
|
||||
}
|
||||
interface ThumbStates {
|
||||
scale: Boolean;
|
||||
}
|
||||
|
||||
export class RendererThumb extends React.Component<ThumbProps, ThumbStates> {
|
||||
ref: HTMLDivElement;
|
||||
unSensor: Function;
|
||||
|
||||
constructor(props: ThumbProps) {
|
||||
super(props);
|
||||
this.state = {
|
||||
scale: true
|
||||
};
|
||||
this.rootRef = this.rootRef.bind(this);
|
||||
this.syncHeight = this.syncHeight.bind(this);
|
||||
this.handleClick = this.handleClick.bind(this);
|
||||
}
|
||||
|
||||
env = {
|
||||
session: 'preview',
|
||||
...this.props.env
|
||||
};
|
||||
|
||||
componentWillUnmount() {
|
||||
this.unSensor && this.unSensor();
|
||||
}
|
||||
|
||||
rootRef(ref: HTMLDivElement) {
|
||||
this.ref = ref;
|
||||
|
||||
if (ref) {
|
||||
this.syncHeight();
|
||||
this.unSensor = resizeSensor(
|
||||
ref.firstChild?.firstChild as HTMLElement,
|
||||
this.syncHeight
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
syncHeight() {
|
||||
if (!this.ref) {
|
||||
return;
|
||||
}
|
||||
const scale = this.state.scale;
|
||||
const child = this.ref.firstChild as HTMLElement;
|
||||
this.ref.style.cssText = `height: ${
|
||||
child.scrollHeight / (scale ? 2 : 1)
|
||||
}px;`;
|
||||
}
|
||||
|
||||
handleClick(e: React.MouseEvent) {
|
||||
e.preventDefault();
|
||||
this.setState({
|
||||
scale: !this.state.scale
|
||||
});
|
||||
}
|
||||
|
||||
render() {
|
||||
const {schema, theme} = this.props;
|
||||
|
||||
return (
|
||||
<LazyComponent
|
||||
unMountOnHidden={false}
|
||||
schema={schema}
|
||||
component={({schema}: {schema: Schema; [key: string]: any}) => (
|
||||
<div
|
||||
className={`ae-RenderersPicker-thumb ${
|
||||
this.state.scale ? 'is-scaled' : ''
|
||||
}`}
|
||||
>
|
||||
<div className="ae-Editor-rendererThumbWrap">
|
||||
<div
|
||||
className="ae-Editor-rendererThumbIcon"
|
||||
onClick={this.handleClick}
|
||||
>
|
||||
<Icon icon={this.state.scale ? 'zoom-in' : 'zoom-out'} />
|
||||
</div>
|
||||
<div ref={this.rootRef} className={`ae-Editor-rendererThumb`}>
|
||||
<div className="ae-Editor-rendererThumbInner">
|
||||
{render(
|
||||
{
|
||||
...schema,
|
||||
mode:
|
||||
schema.mode === 'horizontal' ? 'normal' : schema.mode
|
||||
},
|
||||
{
|
||||
theme
|
||||
},
|
||||
this.env
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
/>
|
||||
);
|
||||
}
|
||||
}
|
||||
319
packages/amis-editor-core/src/component/ScaffoldModal.tsx
Normal file
@@ -0,0 +1,319 @@
|
||||
import React from 'react';
|
||||
import {EditorManager} from '../manager';
|
||||
import {EditorStoreType} from '../store/editor';
|
||||
import {render, Modal, getTheme, Icon, Spinner, Button} from 'amis';
|
||||
import {observer} from 'mobx-react';
|
||||
import {autobind, isObject} from '../util';
|
||||
|
||||
export interface SubEditorProps {
|
||||
store: EditorStoreType;
|
||||
manager: EditorManager;
|
||||
theme?: string;
|
||||
}
|
||||
|
||||
@observer
|
||||
export class ScaffoldModal extends React.Component<SubEditorProps> {
|
||||
@autobind
|
||||
async handleConfirm([values]: any) {
|
||||
const store = this.props.store;
|
||||
const pipeOutFunc = store.scaffoldForm?.pipeOut;
|
||||
|
||||
values = {
|
||||
...store.scaffoldForm?.value,
|
||||
...values
|
||||
};
|
||||
|
||||
if (pipeOutFunc && typeof pipeOutFunc === 'function') {
|
||||
const mapped = await pipeOutFunc(values);
|
||||
|
||||
values = {
|
||||
...mapped
|
||||
};
|
||||
}
|
||||
|
||||
store.scaffoldForm?.callback(values);
|
||||
store.closeScaffoldForm();
|
||||
store.scaffoldForm?.stepsBody && store.setScaffoldStep(0);
|
||||
}
|
||||
|
||||
buildSchema() {
|
||||
const {store} = this.props;
|
||||
const scaffoldFormContext = store.scaffoldForm!;
|
||||
|
||||
let body = scaffoldFormContext.controls ?? scaffoldFormContext.body;
|
||||
if (scaffoldFormContext.stepsBody) {
|
||||
body = [
|
||||
{
|
||||
type: 'steps',
|
||||
name: '__step',
|
||||
className: 'ae-Steps',
|
||||
steps: body.map((step, index) => ({
|
||||
title: step.title,
|
||||
value: index,
|
||||
iconClassName: 'ae-Steps-Icon'
|
||||
}))
|
||||
},
|
||||
...body.map((step, index) => ({
|
||||
type: 'container',
|
||||
visibleOn: `__step === ${index}`,
|
||||
body: step.body
|
||||
}))
|
||||
];
|
||||
}
|
||||
|
||||
let layout: object;
|
||||
if (isObject(scaffoldFormContext.mode)) {
|
||||
layout = scaffoldFormContext.mode as object;
|
||||
} else {
|
||||
layout = {
|
||||
mode: scaffoldFormContext.mode || 'normal'
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
type: 'form',
|
||||
wrapWithPanel: false,
|
||||
initApi: scaffoldFormContext.initApi,
|
||||
api: scaffoldFormContext.api,
|
||||
...layout,
|
||||
wrapperComponent: 'div',
|
||||
[scaffoldFormContext.controls ? 'controls' : 'body']: body
|
||||
};
|
||||
// const {store} = this.props;
|
||||
// const scaffoldFormContext = store.scaffoldForm;
|
||||
|
||||
// return {
|
||||
// show: !!scaffoldFormContext,
|
||||
// size: scaffoldFormContext?.size || 'md',
|
||||
// title: scaffoldFormContext?.title,
|
||||
// onClose: store.closeScaffoldForm,
|
||||
// onConfirm: this.handleConfirm,
|
||||
// data: createObject(store.ctx, scaffoldFormContext?.value),
|
||||
// body: scaffoldFormContext
|
||||
// ? {
|
||||
// type: 'form',
|
||||
// initApi: scaffoldFormContext.initApi,
|
||||
// api: scaffoldFormContext.api,
|
||||
// mode: scaffoldFormContext.mode || 'normal',
|
||||
// wrapperComponent: 'div',
|
||||
// onValidate: scaffoldFormContext.validate,
|
||||
// [scaffoldFormContext.controls ? 'controls' : 'body']:
|
||||
// scaffoldFormContext.controls ?? scaffoldFormContext.body
|
||||
// }
|
||||
// : {
|
||||
// type: 'tpl',
|
||||
// tpl: 'Loading...'
|
||||
// },
|
||||
// actions: [
|
||||
// [
|
||||
// {
|
||||
// type: 'submit',
|
||||
// label: '确认',
|
||||
// level: 'primary'
|
||||
// },
|
||||
// {
|
||||
// type: 'button',
|
||||
// label: '取消',
|
||||
// actionType: 'close'
|
||||
// }
|
||||
// ]
|
||||
// ],
|
||||
// closeOnEsc: false,
|
||||
// bodyClassName: 'ae-Dialog'
|
||||
// // lazyRender: true
|
||||
// };
|
||||
}
|
||||
|
||||
amisScope: any;
|
||||
|
||||
@autobind
|
||||
scopeRef(scoped: any) {
|
||||
this.amisScope = scoped;
|
||||
}
|
||||
|
||||
@autobind
|
||||
goToNextStep() {
|
||||
const store = this.props.store;
|
||||
const form = this.amisScope?.getComponents()[0].props.store;
|
||||
const step = store.scaffoldFormStep + 1;
|
||||
form.setValueByName('__step', step);
|
||||
/** 切换步骤导致schema重新渲染,Form数据域中的数据会丢失 */
|
||||
store.updateScaffoldData(form?.data, true);
|
||||
|
||||
// 控制按钮
|
||||
store.setScaffoldStep(step);
|
||||
// 标记是否手动操作过
|
||||
store.setScaffoldStepManipulated(true);
|
||||
}
|
||||
|
||||
@autobind
|
||||
goToPrevStep() {
|
||||
// 不能更新props的data,控制amis不重新渲染,否则数据会重新初始化
|
||||
const store = this.props.store;
|
||||
const form = this.amisScope?.getComponents()[0].props.store;
|
||||
const step = store.scaffoldFormStep - 1;
|
||||
form.setValueByName('__step', step);
|
||||
store.updateScaffoldData(form?.data, true);
|
||||
|
||||
// 控制按钮
|
||||
store.setScaffoldStep(step);
|
||||
}
|
||||
|
||||
@autobind
|
||||
async handleConfirmClick() {
|
||||
const form = this.amisScope?.getComponents()[0];
|
||||
|
||||
if (!form) {
|
||||
return;
|
||||
}
|
||||
const {store} = this.props;
|
||||
|
||||
try {
|
||||
store.setScaffoldBuzy(true);
|
||||
|
||||
const values = await form.doAction(
|
||||
{
|
||||
type: 'submit'
|
||||
},
|
||||
form.props.data,
|
||||
true
|
||||
);
|
||||
|
||||
await this.handleConfirm([values]);
|
||||
} catch (e) {
|
||||
console.log(e.stack);
|
||||
store.setScaffoldError(e.message);
|
||||
}
|
||||
|
||||
store.setScaffoldBuzy(false);
|
||||
store.setScaffoldStep(0);
|
||||
}
|
||||
|
||||
@autobind
|
||||
handleCancelClick() {
|
||||
this.props.store.closeScaffoldForm();
|
||||
this.props.store.setScaffoldStep(0);
|
||||
}
|
||||
|
||||
render() {
|
||||
const {store, theme, manager} = this.props;
|
||||
const scaffoldFormContext = store.scaffoldForm;
|
||||
const cx = getTheme(theme || 'cxd').classnames;
|
||||
const isStepBody = !!scaffoldFormContext?.stepsBody;
|
||||
const canSkip = !!scaffoldFormContext?.canSkip;
|
||||
const isLastStep =
|
||||
isStepBody &&
|
||||
store.scaffoldFormStep === scaffoldFormContext!.body.length - 1;
|
||||
const isFirstStep = isStepBody && store.scaffoldFormStep === 0;
|
||||
|
||||
return (
|
||||
<Modal
|
||||
theme={theme}
|
||||
size={scaffoldFormContext?.size || 'md'}
|
||||
contentClassName={scaffoldFormContext?.className}
|
||||
show={!!scaffoldFormContext}
|
||||
onHide={this.handleCancelClick}
|
||||
className="ae-scaffoldForm-Modal :AMISCSSWrapper"
|
||||
closeOnEsc={!store.scaffoldFormBuzy}
|
||||
>
|
||||
<div className={cx('Modal-header')}>
|
||||
{!store.scaffoldFormBuzy ? (
|
||||
<a
|
||||
data-position="left"
|
||||
onClick={this.handleCancelClick}
|
||||
className={cx('Modal-close')}
|
||||
>
|
||||
<Icon icon="close" className="icon" />
|
||||
</a>
|
||||
) : null}
|
||||
<div className={cx('Modal-title')}>{scaffoldFormContext?.title}</div>
|
||||
</div>
|
||||
<div className={cx('Modal-body')}>
|
||||
{scaffoldFormContext ? (
|
||||
render(
|
||||
this.buildSchema(),
|
||||
{
|
||||
data: store.scaffoldData,
|
||||
onValidate: scaffoldFormContext.validate,
|
||||
scopeRef: this.scopeRef,
|
||||
manager
|
||||
},
|
||||
{
|
||||
...manager.env,
|
||||
session: 'scaffold-dialog',
|
||||
theme: theme
|
||||
}
|
||||
)
|
||||
) : (
|
||||
<p>Loading...</p>
|
||||
)}
|
||||
</div>
|
||||
|
||||
<div className={cx('Modal-footer')}>
|
||||
{store.scaffoldFormBuzy || store.scaffoldError ? (
|
||||
<div className={cx('Dialog-info')} key="info">
|
||||
<Spinner size="sm" key="info" show={store.scaffoldFormBuzy} />
|
||||
{store.scaffoldError ? (
|
||||
<span className={cx('Dialog-error')}>
|
||||
{store.scaffoldError}
|
||||
</span>
|
||||
) : null}
|
||||
</div>
|
||||
) : null}
|
||||
{isStepBody && canSkip && isFirstStep && (
|
||||
<Button
|
||||
onClick={this.handleConfirmClick}
|
||||
disabled={store.scaffoldFormBuzy}
|
||||
>
|
||||
跳过向导
|
||||
</Button>
|
||||
)}
|
||||
{isStepBody && !isFirstStep && (
|
||||
<Button
|
||||
level="primary"
|
||||
onClick={this.goToPrevStep}
|
||||
disabled={store.scaffoldFormBuzy}
|
||||
>
|
||||
上一步
|
||||
</Button>
|
||||
)}
|
||||
{isStepBody && !isLastStep && (
|
||||
<Button
|
||||
level="primary"
|
||||
onClick={this.goToNextStep}
|
||||
disabled={store.scaffoldFormBuzy}
|
||||
>
|
||||
下一步
|
||||
</Button>
|
||||
)}
|
||||
{(!isStepBody || isLastStep) && (
|
||||
<Button
|
||||
level="primary"
|
||||
onClick={this.handleConfirmClick}
|
||||
disabled={store.scaffoldFormBuzy}
|
||||
>
|
||||
确认
|
||||
</Button>
|
||||
)}
|
||||
<Button onClick={this.handleCancelClick}>取消</Button>
|
||||
</div>
|
||||
</Modal>
|
||||
);
|
||||
}
|
||||
|
||||
// _render() {
|
||||
// const {store, theme, manager} = this.props;
|
||||
|
||||
// return render(
|
||||
// {
|
||||
// type: 'dialog'
|
||||
// },
|
||||
// this.buildSchema(),
|
||||
// {
|
||||
// ...manager.env,
|
||||
// seesion: 'scaffold-dialog',
|
||||
// theme: theme
|
||||
// }
|
||||
// );
|
||||
// }
|
||||
}
|
||||
28
packages/amis-editor-core/src/component/SchemaRenderer.tsx
Normal file
@@ -0,0 +1,28 @@
|
||||
import React from 'react';
|
||||
import {SchemaRenderer as CoreSchemaRenderer} from 'amis-core';
|
||||
|
||||
// 编辑态时用这个来代替默认的组件渲染
|
||||
export const SchemaRenderer = React.forwardRef((props: any, ref: any) => {
|
||||
const statusStore = props.statusStore;
|
||||
const proxyStatusStore = React.useMemo(() => {
|
||||
return statusStore.isEditorProxy
|
||||
? statusStore
|
||||
: new Proxy(statusStore, {
|
||||
get(target, prop, receiver) {
|
||||
// 始终是可见的, 否则没法编辑了
|
||||
if (prop === 'visibleState') {
|
||||
return {};
|
||||
} else if (prop === 'isEditorProxy') {
|
||||
return true;
|
||||
} else if (prop === 'raw') {
|
||||
return target;
|
||||
}
|
||||
return Reflect.get(target, prop, receiver);
|
||||
}
|
||||
});
|
||||
}, [statusStore]);
|
||||
|
||||
return (
|
||||
<CoreSchemaRenderer {...props} statusStore={proxyStatusStore} ref={ref} />
|
||||
);
|
||||
});
|
||||
280
packages/amis-editor-core/src/component/SubEditor.tsx
Normal file
@@ -0,0 +1,280 @@
|
||||
import React from 'react';
|
||||
import {EditorManager} from '../manager';
|
||||
import {EditorStoreType} from '../store/editor';
|
||||
import {render} from 'amis';
|
||||
import {createObject, RenderOptions} from 'amis-core';
|
||||
|
||||
import {observer} from 'mobx-react';
|
||||
import Editor from './Editor';
|
||||
import {
|
||||
BuildPanelEventContext,
|
||||
PluginEvent,
|
||||
RendererInfoResolveEventContext
|
||||
} from '../plugin';
|
||||
import {autobind} from '../util';
|
||||
import omit from 'lodash/omit';
|
||||
|
||||
export interface SubEditorProps {
|
||||
store: EditorStoreType;
|
||||
manager: EditorManager;
|
||||
theme?: string;
|
||||
amisEnv?: RenderOptions;
|
||||
readonly?: boolean;
|
||||
}
|
||||
|
||||
@observer
|
||||
export class SubEditor extends React.Component<SubEditorProps> {
|
||||
@autobind
|
||||
afterResolveEditorInfo(event: PluginEvent<RendererInfoResolveEventContext>) {
|
||||
const store = this.props.store;
|
||||
const context = event.context;
|
||||
const slot = store.subEditorContext?.slot;
|
||||
|
||||
if (!slot) {
|
||||
if (
|
||||
context.data &&
|
||||
!context.schemaPath &&
|
||||
store.subEditorContext?.memberImmutable
|
||||
) {
|
||||
context.data.memberImmutable = store.subEditorContext?.memberImmutable;
|
||||
}
|
||||
return;
|
||||
}
|
||||
const slotPath = store.subEditorSlotPath;
|
||||
|
||||
if (!~context.schemaPath.indexOf(slotPath) && context.data) {
|
||||
context.data.editable = false;
|
||||
context.data.memberImmutable = !Array.isArray(
|
||||
store.subEditorContext?.value
|
||||
);
|
||||
|
||||
if (!context.data.memberImmutable) {
|
||||
context.data.name = '容器';
|
||||
}
|
||||
} else if (context.schemaPath === slotPath && context.data) {
|
||||
if (!Array.isArray(store.subEditorContext?.value)) {
|
||||
context.data.movable = false;
|
||||
context.data.removable = false;
|
||||
}
|
||||
|
||||
context.data.typeMutable = store.subEditorContext?.typeMutable;
|
||||
}
|
||||
}
|
||||
|
||||
@autobind
|
||||
handleBuildPanels(event: PluginEvent<BuildPanelEventContext>) {
|
||||
const store = this.props.store;
|
||||
const slot = store.subEditorContext?.slot;
|
||||
|
||||
if (!slot) {
|
||||
return;
|
||||
}
|
||||
// const slotPath = store.subEditorSlotPath;
|
||||
const context = event.context;
|
||||
|
||||
// 成员节点固定时,不展示组件面板
|
||||
if (!!context.info.memberImmutable) {
|
||||
const panels = context.data.concat();
|
||||
context.data.splice(0, context.data.length);
|
||||
const renderersPanel = panels.filter(r => r.key !== 'renderers');
|
||||
renderersPanel && context.data.push(...renderersPanel);
|
||||
// 默认选中大纲
|
||||
context.changeLeftPanelKey = 'outline';
|
||||
}
|
||||
|
||||
/*
|
||||
// 备注: 当前逻辑有点问题(context.schemaPath 基本上都不会含 slotPath),先注释掉。
|
||||
if (!~context.schemaPath.indexOf(slotPath)) {
|
||||
const panels = context.data.concat();
|
||||
context.data.splice(0, context.data.length);
|
||||
|
||||
// 如果是slot外面,一个面板都不给。
|
||||
if (!context.info.memberImmutable) {
|
||||
const renderersPanel = panels.find(r => r.key === 'renderers');
|
||||
renderersPanel && context.data.push(renderersPanel);
|
||||
}
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
buildSchema() {
|
||||
const {store, manager, amisEnv, readonly} = this.props;
|
||||
const subEditorContext = store.subEditorContext;
|
||||
const config = manager.config;
|
||||
let superEditorData: any = store.superEditorData;
|
||||
if (!!subEditorContext) {
|
||||
superEditorData = createObject(
|
||||
store.superEditorData,
|
||||
subEditorContext?.data
|
||||
);
|
||||
}
|
||||
const variables: any = manager.config?.variables || [];
|
||||
|
||||
return {
|
||||
size: 'full',
|
||||
title: store.subEditorContext?.title,
|
||||
onClose: store.closeSubEditor,
|
||||
onConfirm: store.confirmSubEditor,
|
||||
body: store.subEditorContext
|
||||
? {
|
||||
type: 'form',
|
||||
mode: 'normal',
|
||||
wrapWithPanel: false,
|
||||
wrapperComponent: 'div',
|
||||
onValidate: async (value: any) => {
|
||||
const result = await store.subEditorContext?.validate?.(value);
|
||||
if (result) {
|
||||
return {
|
||||
schema: result
|
||||
};
|
||||
}
|
||||
return;
|
||||
},
|
||||
onChange: store.subEditorOnChange,
|
||||
body: [
|
||||
{
|
||||
name: 'schema',
|
||||
asFormItem: true,
|
||||
children: ({
|
||||
value,
|
||||
onChange
|
||||
}: {
|
||||
value: any;
|
||||
onChange: (value: any) => void;
|
||||
}) => (
|
||||
<Editor
|
||||
autoFocus
|
||||
value={value}
|
||||
ref={store.subEditorRef}
|
||||
onChange={onChange}
|
||||
data={store.subEditorContext?.data}
|
||||
hostManager={manager}
|
||||
hostNode={store.subEditorContext?.hostNode}
|
||||
superEditorData={superEditorData}
|
||||
schemaFilter={manager.config.schemaFilter}
|
||||
theme={manager.env.theme}
|
||||
afterResolveEditorInfo={this.afterResolveEditorInfo}
|
||||
onBuildPanels={this.handleBuildPanels}
|
||||
isMobile={store.isMobile}
|
||||
isSubEditor={true}
|
||||
ctx={store.ctx}
|
||||
schemas={manager.config?.schemas}
|
||||
variables={variables}
|
||||
amisEnv={amisEnv || config.amisEnv}
|
||||
appLocale={config.appLocale}
|
||||
i18nEnabled={config.i18nEnabled}
|
||||
plugins={config.plugins}
|
||||
actionOptions={config.actionOptions}
|
||||
showCustomRenderersPanel={
|
||||
store.showCustomRenderersPanel ?? true
|
||||
}
|
||||
isHiddenProps={config.isHiddenProps}
|
||||
$schemaUrl={config.$schemaUrl}
|
||||
onFormulaEditorOpen={async (
|
||||
node,
|
||||
subEditormanager,
|
||||
data
|
||||
) => {
|
||||
const fn = manager?.config?.onFormulaEditorOpen;
|
||||
|
||||
if (fn && typeof fn === 'function') {
|
||||
return fn(node, subEditormanager, data, {
|
||||
node: subEditorContext?.hostNode,
|
||||
manager: manager
|
||||
});
|
||||
}
|
||||
return;
|
||||
}}
|
||||
getHostNodeDataSchema={async () => {
|
||||
await manager.getContextSchemas(manager.store.activeId);
|
||||
return manager.dataSchema;
|
||||
}}
|
||||
getAvaiableContextFields={node =>
|
||||
manager.getAvailableContextFields(node)
|
||||
}
|
||||
readonly={readonly}
|
||||
/>
|
||||
)
|
||||
},
|
||||
readonly && {
|
||||
type: 'button',
|
||||
label: '返回编辑器',
|
||||
className: 'subEditor-container-back',
|
||||
onClick: () => store.closeSubEditor()
|
||||
}
|
||||
].filter(Boolean)
|
||||
}
|
||||
: {
|
||||
type: 'tpl',
|
||||
tpl: 'Loading...'
|
||||
},
|
||||
actions: [
|
||||
[
|
||||
{
|
||||
children: subEditorContext ? (
|
||||
<div className="ae-DialogToolbar">
|
||||
<button
|
||||
type="button"
|
||||
data-tooltip="撤销"
|
||||
data-position="top"
|
||||
disabled={!subEditorContext.canUndo}
|
||||
onClick={store.undoSubEditor}
|
||||
>
|
||||
<i className="fa fa-undo" />
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
data-tooltip="重做"
|
||||
data-position="top"
|
||||
disabled={!subEditorContext.canRedo}
|
||||
onClick={store.redoSubEditor}
|
||||
>
|
||||
<i className="fa fa-rotate-right" />
|
||||
</button>
|
||||
</div>
|
||||
) : null
|
||||
},
|
||||
{
|
||||
type: 'submit',
|
||||
label: '保存',
|
||||
level: 'primary'
|
||||
},
|
||||
{
|
||||
type: 'button',
|
||||
label: '取消',
|
||||
actionType: 'close'
|
||||
}
|
||||
]
|
||||
],
|
||||
closeOnEsc: false,
|
||||
bodyClassName: 'ae-dialog subEditor-dialog'
|
||||
// lazyRender: true
|
||||
};
|
||||
}
|
||||
|
||||
render() {
|
||||
const {store, theme, manager, readonly} = this.props;
|
||||
if (!store.subEditorContext) {
|
||||
return null;
|
||||
}
|
||||
return render(
|
||||
{
|
||||
type: readonly ? 'container' : 'dialog',
|
||||
className: readonly ? 'subEditor-container' : 'subEditor-dialog',
|
||||
...this.buildSchema()
|
||||
},
|
||||
|
||||
{
|
||||
show: !!store.subEditorContext,
|
||||
data: {
|
||||
schema: store.subEditorValue
|
||||
}
|
||||
},
|
||||
{
|
||||
...omit(manager.env, 'replaceText'),
|
||||
session: 'editor-dialog',
|
||||
theme: theme
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
76
packages/amis-editor-core/src/component/VRenderer.tsx
Normal file
@@ -0,0 +1,76 @@
|
||||
/**
|
||||
* @file 虚拟的渲染器,也就是说其实并不存在渲染器,而是弄个假的用来编辑。
|
||||
*/
|
||||
import {isAlive} from 'mobx-state-tree';
|
||||
import React from 'react';
|
||||
import {findDOMNode} from 'react-dom';
|
||||
import {RendererInfo} from '../plugin';
|
||||
import {EditorNodeContext, EditorNodeType} from '../store/node';
|
||||
|
||||
export interface VRendererProps extends RendererInfo {
|
||||
path: string;
|
||||
data?: any;
|
||||
widthMutable?: boolean;
|
||||
children?: React.ReactNode;
|
||||
}
|
||||
|
||||
export class VRenderer extends React.Component<VRendererProps> {
|
||||
static contextType = EditorNodeContext;
|
||||
editorNode: EditorNodeType;
|
||||
|
||||
UNSAFE_componentWillMount() {
|
||||
const {data, path, widthMutable, ...info} = this.props;
|
||||
const parent: EditorNodeType = this.context as any;
|
||||
this.editorNode = parent.addChild({
|
||||
id: info.id,
|
||||
type: info.type,
|
||||
label: info.name,
|
||||
path: this.props.path,
|
||||
schemaPath: info.schemaPath,
|
||||
info: info,
|
||||
getData: () => this.props.data,
|
||||
widthMutable,
|
||||
memberIndex: info.memberIndex
|
||||
});
|
||||
}
|
||||
|
||||
componentDidMount() {
|
||||
this.markDom(this.editorNode.id);
|
||||
}
|
||||
|
||||
componentDidUpdate() {
|
||||
this.markDom(this.editorNode.id);
|
||||
}
|
||||
|
||||
componentWillUnmount() {
|
||||
if (this.editorNode && isAlive(this.editorNode)) {
|
||||
const parent: EditorNodeType = this.context as any;
|
||||
parent.removeChild(this.editorNode);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 弄点标记
|
||||
*/
|
||||
markDom(id: string) {
|
||||
const root = findDOMNode(this) as HTMLElement;
|
||||
|
||||
if (!root) {
|
||||
return;
|
||||
}
|
||||
|
||||
const info = this.editorNode.info!;
|
||||
let dom = info.wrapperResolve ? info.wrapperResolve(root) : root;
|
||||
(Array.isArray(dom) ? dom : dom ? [dom] : []).forEach(dom =>
|
||||
dom.setAttribute('data-editor-id', id)
|
||||
);
|
||||
}
|
||||
|
||||
render() {
|
||||
return (
|
||||
<EditorNodeContext.Provider value={this.editorNode}>
|
||||
{this.props.children}
|
||||
</EditorNodeContext.Provider>
|
||||
);
|
||||
}
|
||||
}
|
||||
152
packages/amis-editor-core/src/component/base/BackTop.tsx
Normal file
@@ -0,0 +1,152 @@
|
||||
import React from 'react';
|
||||
import cx from 'classnames';
|
||||
import omit from 'lodash/omit';
|
||||
import {autobind} from '../../util';
|
||||
|
||||
export interface BackTopProps {
|
||||
visibilityHeight?: number;
|
||||
onClick?: React.MouseEventHandler<HTMLElement>;
|
||||
target: () => HTMLElement | Window | Document;
|
||||
children?: React.ReactNode;
|
||||
className?: string;
|
||||
visible?: boolean;
|
||||
}
|
||||
|
||||
export interface BackTopStates {
|
||||
visible: boolean;
|
||||
}
|
||||
|
||||
function isWindow(obj: any) {
|
||||
return obj !== null && obj !== undefined && obj === obj.window;
|
||||
}
|
||||
|
||||
function getScroll(
|
||||
target: HTMLElement | Window | Document | undefined,
|
||||
top: boolean
|
||||
): number {
|
||||
if (typeof window === 'undefined') {
|
||||
return 0;
|
||||
}
|
||||
const method = top ? 'scrollTop' : 'scrollLeft';
|
||||
let result = 0;
|
||||
const _isWindow = isWindow(target);
|
||||
if (_isWindow) {
|
||||
result = (target as Window)[top ? 'pageYOffset' : 'pageXOffset'];
|
||||
} else if (target instanceof Document) {
|
||||
result = target.documentElement[method];
|
||||
} else if (target) {
|
||||
result = (target as HTMLElement)[method];
|
||||
}
|
||||
if (target && !_isWindow && typeof result !== 'number') {
|
||||
result = ((target as HTMLElement).ownerDocument || (target as Document))
|
||||
.documentElement?.[method];
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
export default class BackTop extends React.Component<
|
||||
BackTopProps,
|
||||
BackTopStates
|
||||
> {
|
||||
ref = React.createRef<HTMLDivElement>();
|
||||
scrollElem: any = null;
|
||||
|
||||
constructor(props: any) {
|
||||
super(props);
|
||||
|
||||
this.state = {
|
||||
visible: props.visible !== undefined ? props.visible : false
|
||||
};
|
||||
}
|
||||
|
||||
componentDidMount() {
|
||||
this.bindScrollEvent();
|
||||
}
|
||||
|
||||
componentWillUnmount() {
|
||||
if (this.scrollElem) {
|
||||
this.scrollElem.removeEventListener('scroll', this.handleScroll);
|
||||
}
|
||||
}
|
||||
|
||||
@autobind
|
||||
getDefaultTarget() {
|
||||
return this.ref.current && this.ref.current.ownerDocument
|
||||
? this.ref.current.ownerDocument
|
||||
: window;
|
||||
}
|
||||
|
||||
@autobind
|
||||
handleScroll(e: React.UIEvent<HTMLElement> | {target: any}) {
|
||||
const visibilityHeight = this.props.visibilityHeight || 400;
|
||||
const scrollTop = getScroll(e.target, true);
|
||||
this.setState({
|
||||
visible: scrollTop > visibilityHeight!
|
||||
});
|
||||
}
|
||||
|
||||
@autobind
|
||||
bindScrollEvent() {
|
||||
const {target} = this.props;
|
||||
this.scrollElem = target() || this.getDefaultTarget();
|
||||
|
||||
if (!this.scrollElem) {
|
||||
return;
|
||||
}
|
||||
this.scrollElem.addEventListener('scroll', this.handleScroll);
|
||||
|
||||
this.handleScroll({
|
||||
target: this.scrollElem
|
||||
});
|
||||
}
|
||||
|
||||
@autobind
|
||||
scrollToTop(e: React.MouseEvent<HTMLDivElement>) {
|
||||
const {onClick} = this.props;
|
||||
|
||||
if (!this.scrollElem) {
|
||||
return;
|
||||
}
|
||||
this.scrollElem.scrollTo({
|
||||
top: 0,
|
||||
behavior: 'smooth'
|
||||
});
|
||||
if (typeof onClick === 'function') {
|
||||
onClick(e);
|
||||
}
|
||||
}
|
||||
|
||||
render() {
|
||||
const {children, className = ''} = this.props;
|
||||
const classString = cx(
|
||||
'editor-backTop',
|
||||
className,
|
||||
this.state.visible ? '' : 'hidden'
|
||||
);
|
||||
|
||||
const divProps = omit(this.props, [
|
||||
'className',
|
||||
'children',
|
||||
'visibilityHeight',
|
||||
'target',
|
||||
'visible'
|
||||
]);
|
||||
|
||||
const defaultElement = (
|
||||
<div className="backTop-content">
|
||||
<div className="backTop-icon">UP</div>
|
||||
</div>
|
||||
);
|
||||
|
||||
return (
|
||||
<div
|
||||
{...divProps}
|
||||
className={classString}
|
||||
onClick={this.scrollToTop}
|
||||
ref={this.ref}
|
||||
>
|
||||
{children || defaultElement}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
import React from 'react';
|
||||
|
||||
export class ErrorRenderer extends React.Component<any> {
|
||||
render() {
|
||||
return <div className="ae-ErrorRenderer">类型错误,无法渲染</div>;
|
||||
}
|
||||
}
|
||||
237
packages/amis-editor-core/src/component/base/SchemaForm.tsx
Normal file
@@ -0,0 +1,237 @@
|
||||
import React from 'react';
|
||||
import {EditorNodeType} from '../../store/node';
|
||||
import {EditorManager} from '../../manager';
|
||||
import {diff, getThemeConfig} from '../../util';
|
||||
import pick from 'lodash/pick';
|
||||
import {
|
||||
createObjectFromChain,
|
||||
createObject,
|
||||
extractObjectChain,
|
||||
IFormStore,
|
||||
render,
|
||||
toast
|
||||
} from 'amis';
|
||||
import omit from 'lodash/omit';
|
||||
import cx from 'classnames';
|
||||
|
||||
export const SchemaForm = React.forwardRef(
|
||||
(
|
||||
{
|
||||
propKey,
|
||||
body,
|
||||
definitions,
|
||||
controls,
|
||||
onChange,
|
||||
value,
|
||||
env,
|
||||
api,
|
||||
popOverContainer,
|
||||
submitOnChange,
|
||||
node,
|
||||
manager,
|
||||
justify,
|
||||
ctx,
|
||||
pipeIn,
|
||||
pipeOut,
|
||||
readonly,
|
||||
disabled,
|
||||
appendSubmitBtn,
|
||||
...rest
|
||||
}: {
|
||||
propKey?: string;
|
||||
env: any;
|
||||
body?: Array<any>;
|
||||
/**
|
||||
* @deprecated 用 body 代替
|
||||
*/
|
||||
controls?: Array<any>;
|
||||
definitions?: any;
|
||||
value: any;
|
||||
api?: any;
|
||||
onChange: (
|
||||
value: any,
|
||||
diff: any,
|
||||
filter: (schema: any, value: any, id: string, diff?: any) => any
|
||||
) => void;
|
||||
popOverContainer?: () => HTMLElement | void;
|
||||
submitOnChange?: boolean;
|
||||
node?: EditorNodeType;
|
||||
manager: EditorManager;
|
||||
panelById?: string;
|
||||
justify?: boolean;
|
||||
ctx?: any;
|
||||
pipeIn?: (value: any) => any;
|
||||
pipeOut?: (value: any, oldValue: any) => any;
|
||||
readonly?: boolean;
|
||||
disabled?: boolean;
|
||||
appendSubmitBtn?: boolean;
|
||||
inheritData?: boolean;
|
||||
},
|
||||
ref: any
|
||||
) => {
|
||||
const schema = React.useMemo(() => {
|
||||
let containerKey = 'body';
|
||||
|
||||
if (Array.isArray(controls)) {
|
||||
body = controls;
|
||||
containerKey = 'controls';
|
||||
}
|
||||
|
||||
body = Array.isArray(body) ? body.concat() : [];
|
||||
|
||||
if (submitOnChange === false && appendSubmitBtn !== false) {
|
||||
body.push({
|
||||
type: 'submit',
|
||||
label: '保存',
|
||||
level: 'primary',
|
||||
block: true,
|
||||
className: 'ae-Settings-actions'
|
||||
});
|
||||
}
|
||||
const schema: any = {
|
||||
key: propKey,
|
||||
mode: 'normal',
|
||||
title: '',
|
||||
wrapWithPanel: false,
|
||||
messages: {
|
||||
validateFailed: ''
|
||||
},
|
||||
...pick(rest, [
|
||||
'mode',
|
||||
'title',
|
||||
'wrapWithPanel',
|
||||
'messages',
|
||||
'horizontal',
|
||||
'inheritData'
|
||||
]),
|
||||
$schema: undefined,
|
||||
definitions,
|
||||
[containerKey]: body,
|
||||
className: cx(
|
||||
'config-form-content',
|
||||
'ae-Settings-content',
|
||||
'hoverShowScrollBar',
|
||||
submitOnChange === false ? 'with-actions' : ''
|
||||
),
|
||||
wrapperComponent: 'div',
|
||||
name: 'schemaform',
|
||||
type: 'form',
|
||||
api,
|
||||
submitOnChange: submitOnChange !== false
|
||||
};
|
||||
|
||||
if (justify) {
|
||||
schema.mode = 'horizontal';
|
||||
schema.horizontal = {
|
||||
left: 4,
|
||||
right: 8,
|
||||
justify: true
|
||||
};
|
||||
}
|
||||
return schema;
|
||||
}, [body, controls, submitOnChange]);
|
||||
|
||||
const themeConfig = React.useMemo(() => getThemeConfig(), []);
|
||||
const submitSubscribers = React.useRef<Array<Function>>([]);
|
||||
const subscribeSubmit = React.useCallback(
|
||||
(
|
||||
fn: (schema: any, value: any, id: string, diff?: any) => any,
|
||||
once = false
|
||||
) => {
|
||||
let raw = fn;
|
||||
const unsubscribe = () => {
|
||||
submitSubscribers.current = submitSubscribers.current.filter(
|
||||
item => ((item as any).__raw ?? item) !== raw
|
||||
);
|
||||
};
|
||||
|
||||
if (once) {
|
||||
fn = (schema: any, value: any, id: string, diff?: any) => {
|
||||
const ret = raw(schema, value, id, diff);
|
||||
unsubscribe();
|
||||
return ret;
|
||||
};
|
||||
(fn as any).__raw = raw;
|
||||
}
|
||||
submitSubscribers.current.push(fn);
|
||||
return unsubscribe;
|
||||
},
|
||||
[]
|
||||
);
|
||||
|
||||
const [init, setInit] = React.useState(true);
|
||||
|
||||
const data = React.useMemo(() => {
|
||||
value = value || {};
|
||||
const finalValue = pipeIn ? pipeIn(value) : value;
|
||||
|
||||
return createObjectFromChain([
|
||||
ctx,
|
||||
themeConfig,
|
||||
...extractObjectChain(finalValue)
|
||||
]);
|
||||
}, [value, themeConfig, ctx]);
|
||||
|
||||
const scopedRef = React.useRef<any>(null);
|
||||
const scopedRefSetter = React.useCallback((ref: any) => {
|
||||
scopedRef.current = ref;
|
||||
}, []);
|
||||
|
||||
React.useImperativeHandle(ref, () => {
|
||||
return {
|
||||
submit: () => {
|
||||
const form = scopedRef.current?.getComponentByName('schemaform');
|
||||
if (!form) {
|
||||
return false;
|
||||
}
|
||||
return form.doAction!({type: 'submit'}, form.props.data, true);
|
||||
},
|
||||
flush: () => {
|
||||
const form = scopedRef.current?.getComponentByName('schemaform');
|
||||
if (!form) {
|
||||
return false;
|
||||
}
|
||||
return form.flush();
|
||||
}
|
||||
};
|
||||
});
|
||||
|
||||
return render(
|
||||
schema,
|
||||
{
|
||||
onFinished: async (newValue: any, action: any, store: IFormStore) => {
|
||||
newValue = pipeOut ? await pipeOut(newValue, value) : newValue;
|
||||
const diffValue = diff(value, newValue);
|
||||
// 没有变化时不触发onChange
|
||||
if (!diffValue) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (readonly && !init) {
|
||||
toast.error('不支持修改');
|
||||
store.setPristine(value);
|
||||
store.reset();
|
||||
return;
|
||||
}
|
||||
setInit(false);
|
||||
onChange(newValue, diffValue, (schema, value, id, diff) => {
|
||||
return submitSubscribers.current.reduce((schema, fn) => {
|
||||
return fn(schema, value, id, diff);
|
||||
}, schema);
|
||||
});
|
||||
},
|
||||
disabled,
|
||||
data: data,
|
||||
node: node,
|
||||
manager: manager,
|
||||
popOverContainer,
|
||||
subscribeSchemaSubmit: subscribeSubmit,
|
||||
scopeRef: scopedRefSetter
|
||||
},
|
||||
{
|
||||
...omit(env, 'replaceText')
|
||||
// theme: 'cxd' // 右侧属性配置面板固定使用cxd主题展示
|
||||
}
|
||||
);
|
||||
}
|
||||
);
|
||||
@@ -0,0 +1,54 @@
|
||||
import React from 'react';
|
||||
import {observer} from 'mobx-react';
|
||||
import SearchPanel from './SearchPanel';
|
||||
import {EditorStoreType} from '../../store/editor';
|
||||
import {SubRendererInfo} from '../../plugin';
|
||||
|
||||
interface SearchCustomRendererProps {
|
||||
store: EditorStoreType;
|
||||
}
|
||||
|
||||
interface SearchCustomRendererStates {
|
||||
customRenderersByOrder: Array<SubRendererInfo>; // 仅获取一次
|
||||
defaultKeyword: string;
|
||||
}
|
||||
|
||||
@observer
|
||||
export default class SearchCustomRendererPanel extends React.Component<
|
||||
SearchCustomRendererProps,
|
||||
SearchCustomRendererStates
|
||||
> {
|
||||
localStorageKey = 'amis-editor-custom-renderer-search-history';
|
||||
|
||||
constructor(props: any) {
|
||||
super(props);
|
||||
let customRenderersByOrder = props.store.customRenderersByOrder;
|
||||
// 剔除隐藏的组件
|
||||
customRenderersByOrder = customRenderersByOrder.filter(
|
||||
(item: SubRendererInfo) => !item.disabledRendererPlugin
|
||||
);
|
||||
const {customRenderersKeywords, customRenderersTag} = props.store;
|
||||
this.state = {
|
||||
customRenderersByOrder: customRenderersByOrder,
|
||||
defaultKeyword: customRenderersKeywords || customRenderersTag || ''
|
||||
};
|
||||
}
|
||||
|
||||
render() {
|
||||
const {customRenderersByOrder, defaultKeyword} = this.state;
|
||||
const {changeCustomRenderersKeywords, changeCustomRenderersTag} =
|
||||
this.props.store;
|
||||
|
||||
return customRenderersByOrder && customRenderersByOrder.length > 0 ? (
|
||||
<SearchPanel
|
||||
allResult={customRenderersByOrder}
|
||||
externalKeyword={defaultKeyword}
|
||||
immediateChange
|
||||
closeAutoComplete
|
||||
searchPanelUUID={this.localStorageKey}
|
||||
onChange={changeCustomRenderersKeywords}
|
||||
onTagChange={changeCustomRenderersTag}
|
||||
/>
|
||||
) : null;
|
||||
}
|
||||
}
|
||||
695
packages/amis-editor-core/src/component/base/SearchPanel.tsx
Normal file
@@ -0,0 +1,695 @@
|
||||
import React from 'react';
|
||||
import {observer} from 'mobx-react';
|
||||
import {Icon, InputBox, resolveVariable} from 'amis';
|
||||
import cx from 'classnames';
|
||||
import {autobind, stringRegExp} from '../../util';
|
||||
import isString from 'lodash/isString';
|
||||
import {matchSorter} from 'match-sorter';
|
||||
|
||||
/**
|
||||
* 通用搜索功能组件,附带以下功能:
|
||||
* 1、搜索历史:会自动记录用户搜索过的关键字,方便用户下次使用;
|
||||
* 2、自动提示补全:autoComplete 设置为 true 后,每次输入关键字后会展示当前所有含有关键字的信息(并以分类的形式展示);
|
||||
* 3、搜索分类:取决于当前搜索数据结构是否带tag,如果不带tag则不展示搜索分类;
|
||||
*/
|
||||
interface SearchProps {
|
||||
allResult: Array<any>; // 当前所有可用搜索数据
|
||||
searchPanelUUID: string; // 搜索面板ID,需要确保唯一,前端web存储搜索记录需要
|
||||
closeAutoComplete?: boolean; // 是否关闭 autoComplete,默认开启
|
||||
externalKeyword?: string; // 外部记录的搜索关键字:主要用于外部主动触发搜索操作
|
||||
tagKey?: string; // 根据搜索结果中的那个字段进行分类,默认为 ‘tags’
|
||||
onChange: (keyword: string) => void; // 搜索关键字变动时执行
|
||||
onTagChange?: (tag: string) => void; // 点击搜索分类时执行
|
||||
immediateChange?: boolean; // 是否开启即时反馈,数值变动立即触发onChange,默认为false
|
||||
}
|
||||
|
||||
interface SearchStates {
|
||||
resultTags: Array<string>; // 存储当前所有分类,不随搜索关键字变化
|
||||
resultByTag: {
|
||||
// 按tag分类存放allResult中的数据: 不随搜索关键字变化
|
||||
[propName: string]: Array<any>;
|
||||
};
|
||||
curKeyword: string; // 当前搜索关键字
|
||||
searchResult: Array<any>; // 存储匹配当前搜索关键字的结果数据: 随搜索关键字变化
|
||||
searchResultByTag: {
|
||||
// 用于存储含有当前搜索关键字的列表(按tag分类存放): 随搜索关键字变化
|
||||
[propName: string]: Array<any>;
|
||||
};
|
||||
visible: boolean; // 用于控制是否显示搜索下拉面板
|
||||
curKeywordSearchHistory: string[]; // 历史搜索记录
|
||||
toggleTagFolderStatus: boolean;
|
||||
}
|
||||
@observer
|
||||
export default class SearchPanel extends React.Component<
|
||||
SearchProps,
|
||||
SearchStates
|
||||
> {
|
||||
ref = React.createRef<HTMLDivElement>();
|
||||
curInputBox: any;
|
||||
localStorageKey = 'amis-editor-search-panel';
|
||||
// 用于搜索联动面板(autoComplete)中的分类展示折叠状态
|
||||
curTagFolded: {
|
||||
[propName: string]: boolean;
|
||||
} = {};
|
||||
lastSearchTag: string; // 用于记录上一次的tag
|
||||
|
||||
constructor(props: any) {
|
||||
super(props);
|
||||
|
||||
if (props.searchPanelUUID) {
|
||||
this.localStorageKey = props.searchPanelUUID;
|
||||
}
|
||||
|
||||
let curResultTags: Array<string> = [];
|
||||
let curResultByTag: {
|
||||
[propName: string]: Array<any>;
|
||||
} = {};
|
||||
if (props.allResult && props.allResult.length > 0) {
|
||||
// 获取分类信息
|
||||
const curResultTagsObj = this.getResultTags(props.allResult);
|
||||
curResultTags = curResultTagsObj.curResultTags;
|
||||
curResultByTag = curResultTagsObj.curResultByTag;
|
||||
}
|
||||
|
||||
this.state = {
|
||||
resultTags: curResultTags,
|
||||
resultByTag: curResultByTag,
|
||||
curKeyword: props.externalKeyword || '',
|
||||
searchResult: [],
|
||||
searchResultByTag: {},
|
||||
visible: false,
|
||||
curKeywordSearchHistory: this.getSearchHistory(),
|
||||
toggleTagFolderStatus: true
|
||||
};
|
||||
}
|
||||
|
||||
componentDidMount() {
|
||||
if (this.ref.current?.childNodes[0]?.childNodes[0]) {
|
||||
this.curInputBox = this.ref.current.childNodes[0].childNodes[0];
|
||||
this.curInputBox.addEventListener('keyup', this.bindEnterEvent);
|
||||
}
|
||||
}
|
||||
|
||||
componentWillUnmount() {
|
||||
if (this.curInputBox) {
|
||||
this.curInputBox.removeEventListener('keyup', this.bindEnterEvent);
|
||||
}
|
||||
}
|
||||
|
||||
UNSAFE_componentWillReceiveProps(nextProps: any) {
|
||||
const {externalKeyword, allResult} = nextProps;
|
||||
// 当externalKeyword在外部发现变动,则将其值同步给curKeyword,并执行一次搜索
|
||||
if (externalKeyword !== this.state.curKeyword) {
|
||||
this.setState(
|
||||
{
|
||||
curKeyword: externalKeyword
|
||||
},
|
||||
() => {
|
||||
this.groupedResultByKeyword(externalKeyword);
|
||||
}
|
||||
);
|
||||
}
|
||||
// 外部搜索数据变更时
|
||||
if (allResult !== this.props.allResult) {
|
||||
let curResultTags: string[] = [];
|
||||
let curResultByTag: {
|
||||
[propName: string]: any[];
|
||||
} = {};
|
||||
if (allResult && allResult.length > 0) {
|
||||
// 获取分类信息
|
||||
const curResultTagsObj = this.getResultTags(allResult);
|
||||
curResultTags = curResultTagsObj.curResultTags;
|
||||
curResultByTag = curResultTagsObj.curResultByTag;
|
||||
}
|
||||
this.setState({
|
||||
resultTags: curResultTags,
|
||||
resultByTag: curResultByTag
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@autobind
|
||||
getSearchHistory() {
|
||||
let searchHistory = [];
|
||||
if (window.localStorage) {
|
||||
const historyDataStr = window.localStorage.getItem(this.localStorageKey);
|
||||
if (historyDataStr) {
|
||||
const historyData = JSON.parse(historyDataStr);
|
||||
if (historyData && Array.isArray(historyData)) {
|
||||
searchHistory = historyData;
|
||||
}
|
||||
}
|
||||
}
|
||||
return searchHistory;
|
||||
}
|
||||
|
||||
/**
|
||||
* 从搜索数据中获取分类信息,并按分类存放搜索数据,方便后续通过分类直接获取搜索数据
|
||||
*/
|
||||
getResultTags(allResult: Array<any>) {
|
||||
let curResultTags: Array<string> = [];
|
||||
let curResultByTag: {
|
||||
[propName: string]: Array<any>;
|
||||
} = {};
|
||||
const curTagKey = this.props.tagKey || 'tags';
|
||||
|
||||
allResult.forEach(item => {
|
||||
if (!isString(item) && item[curTagKey]) {
|
||||
const tags = Array.isArray(item[curTagKey])
|
||||
? item[curTagKey].concat()
|
||||
: item[curTagKey]
|
||||
? [item[curTagKey]]
|
||||
: ['其他'];
|
||||
tags.forEach((tag: string) => {
|
||||
if (curResultTags.indexOf(tag) < 0) {
|
||||
curResultTags.push(tag);
|
||||
}
|
||||
if (curResultByTag[tag]) {
|
||||
curResultByTag[tag].push(item);
|
||||
} else {
|
||||
curResultByTag[tag] = [];
|
||||
curResultByTag[tag].push(item);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
return {
|
||||
curResultTags,
|
||||
curResultByTag
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据关键字过滤数据,按分组存放
|
||||
*/
|
||||
groupedResultByKeyword(keywords: string = '') {
|
||||
const {allResult} = this.props;
|
||||
let curSearchResult: any[] = [];
|
||||
let curSearchResultByTag: {
|
||||
[propName: string]: any[];
|
||||
} = {};
|
||||
const curKeyword = keywords ? keywords : this.state.curKeyword;
|
||||
const curTagKey = this.props.tagKey || 'tags';
|
||||
const regular = curKeyword
|
||||
? new RegExp(stringRegExp(curKeyword), 'i')
|
||||
: null;
|
||||
|
||||
if (allResult.length && isString(allResult[0])) {
|
||||
matchSorter(allResult, keywords).forEach(item => {
|
||||
// 兼容字符串类型
|
||||
curSearchResult.push(item);
|
||||
});
|
||||
} else {
|
||||
const searchMap = new Map<string, any>();
|
||||
matchSorter(allResult, keywords, {
|
||||
keys: ['name', 'description', 'scaffold.type', 'searchKeywords']
|
||||
}).forEach(item => {
|
||||
searchMap.set(item.id, item);
|
||||
});
|
||||
|
||||
allResult.forEach(item => {
|
||||
if (searchMap.has(item.id)) {
|
||||
if (item[curTagKey]) {
|
||||
const tags = Array.isArray(item[curTagKey])
|
||||
? item[curTagKey].concat()
|
||||
: item[curTagKey]
|
||||
? [item[curTagKey]]
|
||||
: ['其他'];
|
||||
tags.forEach((tag: string) => {
|
||||
curSearchResultByTag[tag] = curSearchResultByTag[tag] || [];
|
||||
curSearchResultByTag[tag].push(item);
|
||||
});
|
||||
} else {
|
||||
curSearchResult.push(item);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// 更新当前搜索结果数据(备注: 附带重置功能)
|
||||
this.setState({
|
||||
searchResult: curSearchResult,
|
||||
searchResultByTag: curSearchResultByTag
|
||||
});
|
||||
}
|
||||
|
||||
@autobind
|
||||
bindFocusEvent() {
|
||||
this.setState({
|
||||
visible: true
|
||||
});
|
||||
}
|
||||
|
||||
@autobind
|
||||
bindBlurEvent() {
|
||||
const {curKeyword} = this.state;
|
||||
this.setState(
|
||||
{
|
||||
visible: false
|
||||
},
|
||||
() => {
|
||||
if (curKeyword) {
|
||||
this.addSearchHistory(curKeyword);
|
||||
}
|
||||
this.props.onChange(curKeyword);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
@autobind
|
||||
updateCurKeyword(keywords: string) {
|
||||
let curKeyword = keywords;
|
||||
curKeyword = curKeyword ? curKeyword.trim() : curKeyword;
|
||||
this.setState(
|
||||
{
|
||||
curKeyword: curKeyword
|
||||
},
|
||||
() => {
|
||||
this.groupedResultByKeyword(curKeyword);
|
||||
if (this.props.immediateChange) {
|
||||
this.props.onChange(curKeyword);
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
// 改变折叠状态
|
||||
@autobind
|
||||
changeTagFoldStatus(tagKey: string, event: any) {
|
||||
event.preventDefault();
|
||||
event.stopPropagation();
|
||||
this.curInputBox.focus(); // 让输入框获取焦点
|
||||
this.curTagFolded[tagKey] = !this.curTagFolded[tagKey];
|
||||
this.setState({
|
||||
toggleTagFolderStatus: !this.state.toggleTagFolderStatus
|
||||
});
|
||||
}
|
||||
|
||||
// 快捷键事件
|
||||
@autobind
|
||||
bindEnterEvent(event: any) {
|
||||
event.preventDefault();
|
||||
const {curKeyword} = this.state;
|
||||
if (event?.keyCode === 13) {
|
||||
// Enter 快捷键
|
||||
this.setState(
|
||||
{
|
||||
visible: false
|
||||
},
|
||||
() => {
|
||||
// enter执行搜索后记录当前查询关键字
|
||||
this.addSearchHistory(curKeyword);
|
||||
this.props.onChange(curKeyword);
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/** 删除搜索关键字 */
|
||||
@autobind
|
||||
bindClearActionEvent() {
|
||||
this.setState(
|
||||
{
|
||||
curKeyword: '',
|
||||
searchResult: [],
|
||||
searchResultByTag: {}
|
||||
},
|
||||
() => {
|
||||
this.props.onChange('');
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
/** 组件分类tag点击事件 */
|
||||
@autobind
|
||||
bindTagClickEvent(tag: string) {
|
||||
const searchResult = this.state.resultByTag[tag];
|
||||
this.setState(
|
||||
{
|
||||
visible: false,
|
||||
curKeyword: tag,
|
||||
searchResult: searchResult,
|
||||
searchResultByTag: {
|
||||
[tag]: searchResult
|
||||
}
|
||||
},
|
||||
() => {
|
||||
this.props.onTagChange && this.props.onTagChange(tag);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
/** 添加搜索历史 */
|
||||
@autobind
|
||||
addSearchHistory(newKeywords: string) {
|
||||
const {curKeywordSearchHistory} = this.state;
|
||||
// 判断是否有相同的搜索记录
|
||||
if (curKeywordSearchHistory.indexOf(newKeywords) > -1) {
|
||||
return;
|
||||
}
|
||||
// 最多存储10条搜索记录
|
||||
if (curKeywordSearchHistory.length === 10) {
|
||||
curKeywordSearchHistory.shift();
|
||||
curKeywordSearchHistory.push(newKeywords);
|
||||
} else {
|
||||
curKeywordSearchHistory.push(newKeywords);
|
||||
}
|
||||
this.updateSearchHistory();
|
||||
}
|
||||
|
||||
/** 搜索历史/点击 */
|
||||
@autobind
|
||||
clickKeywordEvent(keywords: string) {
|
||||
this.setState(
|
||||
{
|
||||
visible: false,
|
||||
curKeyword: keywords
|
||||
},
|
||||
() => {
|
||||
this.groupedResultByKeyword(keywords);
|
||||
this.props.onChange(keywords);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
@autobind
|
||||
deleteSearchHistory(event: any, newKeywords: string) {
|
||||
event.preventDefault();
|
||||
event.stopPropagation();
|
||||
this.curInputBox.focus(); // 让输入框获取焦点
|
||||
const {curKeywordSearchHistory} = this.state;
|
||||
const deleteKeywordIndex = curKeywordSearchHistory.indexOf(newKeywords);
|
||||
curKeywordSearchHistory.splice(deleteKeywordIndex, 1);
|
||||
this.updateSearchHistory();
|
||||
}
|
||||
|
||||
@autobind
|
||||
clearSearchHistory(event: any) {
|
||||
event.preventDefault();
|
||||
event.stopPropagation();
|
||||
this.curInputBox.focus(); // 让输入框获取焦点
|
||||
const {curKeywordSearchHistory} = this.state;
|
||||
if (curKeywordSearchHistory && curKeywordSearchHistory.length > 0) {
|
||||
this.setState(
|
||||
{
|
||||
curKeywordSearchHistory: []
|
||||
},
|
||||
() => {
|
||||
this.updateSearchHistory();
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/** 将搜索记录保存到localStorage */
|
||||
@autobind
|
||||
updateSearchHistory() {
|
||||
if (window.localStorage) {
|
||||
const {curKeywordSearchHistory} = this.state;
|
||||
window.localStorage.setItem(
|
||||
this.localStorageKey,
|
||||
JSON.stringify(curKeywordSearchHistory)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/** 显示搜索关键字 */
|
||||
@autobind
|
||||
renderNameByKeyword(rendererName: string, curKeyword: string) {
|
||||
if (curKeyword && ~rendererName.indexOf(curKeyword)) {
|
||||
const keywordStartIndex = rendererName.indexOf(curKeyword);
|
||||
const keywordEndIndex = keywordStartIndex + curKeyword.length;
|
||||
return (
|
||||
<span>
|
||||
{rendererName.substring(0, keywordStartIndex)}
|
||||
<span className="is-keyword">{curKeyword}</span>
|
||||
{rendererName.substring(keywordEndIndex)}
|
||||
</span>
|
||||
);
|
||||
} else {
|
||||
return rendererName;
|
||||
}
|
||||
}
|
||||
|
||||
/** 判断搜索展示内容是否有滚动(交互优化) */
|
||||
@autobind
|
||||
resultIsHasScroll(
|
||||
searchSubRenderers: {
|
||||
[propName: string]: Array<any>;
|
||||
},
|
||||
maxShowLine: number
|
||||
) {
|
||||
let curShowLine = 0;
|
||||
const curSearchKwTag = searchSubRenderers
|
||||
? Object.keys(searchSubRenderers)
|
||||
: [];
|
||||
curSearchKwTag.map((tag: string) => {
|
||||
if (!!this.curTagFolded[tag]) {
|
||||
curShowLine += 1;
|
||||
} else {
|
||||
curShowLine += searchSubRenderers[tag].length + 1;
|
||||
}
|
||||
});
|
||||
return curShowLine > maxShowLine; // isHasScroll
|
||||
}
|
||||
|
||||
render() {
|
||||
const {allResult, closeAutoComplete, immediateChange} = this.props;
|
||||
const {resultTags, curKeyword, searchResult, searchResultByTag, visible} =
|
||||
this.state;
|
||||
const searchResultTags = searchResultByTag
|
||||
? Object.keys(searchResultByTag)
|
||||
: [];
|
||||
const curKeywordSearchHistory = [
|
||||
...this.state.curKeywordSearchHistory
|
||||
].reverse();
|
||||
let isShowSearchPanel = false;
|
||||
if (visible && allResult && allResult.length > 0) {
|
||||
isShowSearchPanel = true;
|
||||
}
|
||||
// 关闭 autoComplete 时不需要展示搜索列表
|
||||
if (closeAutoComplete && curKeyword) {
|
||||
isShowSearchPanel = false;
|
||||
}
|
||||
|
||||
// 关闭 autoComplete 时,当历史记录和搜索分类都为空时也不展示
|
||||
if (
|
||||
closeAutoComplete &&
|
||||
(!resultTags || (resultTags && resultTags.length === 0)) &&
|
||||
(!curKeywordSearchHistory ||
|
||||
(curKeywordSearchHistory && curKeywordSearchHistory.length === 0))
|
||||
) {
|
||||
isShowSearchPanel = false;
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="editor-InputSearch-panel" ref={this.ref}>
|
||||
<InputBox
|
||||
className="editor-InputSearch"
|
||||
value={curKeyword}
|
||||
onChange={this.updateCurKeyword}
|
||||
placeholder={'输入关键字查询组件'}
|
||||
clearable={false}
|
||||
onFocus={this.bindFocusEvent}
|
||||
onBlur={this.bindBlurEvent}
|
||||
>
|
||||
<>
|
||||
{immediateChange &&
|
||||
(curKeyword ? (
|
||||
<Icon
|
||||
icon="search-clear"
|
||||
className="icon delete-btn-icon"
|
||||
onClick={this.bindClearActionEvent}
|
||||
/>
|
||||
) : (
|
||||
<Icon
|
||||
icon="editor-search"
|
||||
className="icon"
|
||||
onClick={this.bindBlurEvent}
|
||||
/>
|
||||
))}
|
||||
{!immediateChange && (
|
||||
<>
|
||||
{curKeyword && (
|
||||
<Icon
|
||||
icon="search-clear"
|
||||
className="icon delete-btn-icon margin-right"
|
||||
onClick={this.bindClearActionEvent}
|
||||
/>
|
||||
)}
|
||||
<Icon
|
||||
icon="editor-search"
|
||||
className="icon"
|
||||
onClick={this.bindBlurEvent}
|
||||
/>
|
||||
</>
|
||||
)}
|
||||
</>
|
||||
</InputBox>
|
||||
<div
|
||||
className={`editor-InputSearch-content ${
|
||||
isShowSearchPanel ? '' : 'hidden-status'
|
||||
}`}
|
||||
>
|
||||
{!curKeyword &&
|
||||
curKeywordSearchHistory &&
|
||||
curKeywordSearchHistory.length > 0 && (
|
||||
<div
|
||||
className={`search-history ${
|
||||
resultTags && resultTags.length > 0 ? 'has-border-bottom' : ''
|
||||
}`}
|
||||
>
|
||||
<div className="header">
|
||||
<div className="header-title">搜索历史</div>
|
||||
<div
|
||||
className="header-clear-icon"
|
||||
onClick={(event: any) => this.clearSearchHistory(event)}
|
||||
>
|
||||
清空
|
||||
</div>
|
||||
</div>
|
||||
<div
|
||||
className={`history-cont ${
|
||||
curKeywordSearchHistory.length > 6 ? 'hasScrollBtn' : ''
|
||||
}`}
|
||||
>
|
||||
{curKeywordSearchHistory.map((keyword, index) => (
|
||||
<div
|
||||
className="history-item"
|
||||
key={`${keyword}-${index}`}
|
||||
onClick={() => this.clickKeywordEvent(keyword)}
|
||||
>
|
||||
<div className="history-keyword">{keyword}</div>
|
||||
<div className="delete-icon" title="点击删除这条搜索记录">
|
||||
<Icon
|
||||
icon="close"
|
||||
onClick={(event: any) =>
|
||||
this.deleteSearchHistory(event, keyword)
|
||||
}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
{curKeyword &&
|
||||
searchResult.length === 0 &&
|
||||
searchResultTags.length === 0 && (
|
||||
<div className={`search-result-list`}>
|
||||
<div className="search-result-placeholder">
|
||||
搜索结果为空,您可以换个关键字继续查找。
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
{curKeyword && searchResult.length > 0 && (
|
||||
<div
|
||||
className={`search-result-list ${
|
||||
searchResult.length > 6 ? 'hasScrollBtn' : ''
|
||||
}`}
|
||||
>
|
||||
{searchResult.length > 1 && (
|
||||
<div className="subRenderers-list only-one-tag">
|
||||
{searchResult.map((item2, itemIndex2) => (
|
||||
<div
|
||||
className="subRenderers-item"
|
||||
key={`subRenderers-only-one-tag-${itemIndex2}`}
|
||||
onClick={() => this.clickKeywordEvent(item2.name)}
|
||||
>
|
||||
{item2.name || item2.type}
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
{curKeyword && searchResultTags.length > 0 && (
|
||||
<div
|
||||
className={`search-result-list ${
|
||||
this.resultIsHasScroll(searchResultByTag, 6)
|
||||
? 'hasScrollBtn'
|
||||
: ''
|
||||
}`}
|
||||
>
|
||||
{searchResultTags.length > 1 &&
|
||||
searchResultTags.map((cTag, index) => (
|
||||
<div
|
||||
className="multiple-subRenderers-list"
|
||||
key={`${cTag}-subRenderers-list`}
|
||||
>
|
||||
<div
|
||||
className={cx('subRenderers-header', {
|
||||
'is-folded': !!this.curTagFolded[cTag]
|
||||
})}
|
||||
title={
|
||||
!!this.curTagFolded[cTag] ? '点击展开' : '点击折叠'
|
||||
}
|
||||
onClick={(event: any) => {
|
||||
this.changeTagFoldStatus(cTag, event);
|
||||
}}
|
||||
>
|
||||
{cTag}
|
||||
<Icon icon="right-arrow-bold" />
|
||||
</div>
|
||||
<div
|
||||
className={cx('subRenderers-list', {
|
||||
'is-folded': !!this.curTagFolded[cTag]
|
||||
})}
|
||||
>
|
||||
{searchResultByTag[cTag] &&
|
||||
searchResultByTag[cTag].map((item, itemIndex) => (
|
||||
<div
|
||||
className="subRenderers-item"
|
||||
key={itemIndex}
|
||||
onClick={() => this.clickKeywordEvent(item.name)}
|
||||
>
|
||||
{this.renderNameByKeyword(item.name, curKeyword)}
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
{searchResultTags.length === 1 &&
|
||||
searchResultTags.map((cTag2, index2) => (
|
||||
<div
|
||||
className="subRenderers-list only-one-tag"
|
||||
key={`subRenderers-tag-${cTag2}`}
|
||||
>
|
||||
{searchResultByTag[cTag2] &&
|
||||
searchResultByTag[cTag2].map((item2, itemIndex2) => (
|
||||
<div
|
||||
className="subRenderers-item"
|
||||
key={`subRenderers-only-one-tag-${itemIndex2}`}
|
||||
onClick={() => this.clickKeywordEvent(item2.name)}
|
||||
>
|
||||
{item2.name || item2.type}
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
{!curKeyword && resultTags && resultTags.length > 0 && (
|
||||
<div className="tag-list">
|
||||
<div className="header">组件分类</div>
|
||||
<div className="tag-list-cont">
|
||||
{resultTags.length
|
||||
? resultTags.map((tag, index) => (
|
||||
<div
|
||||
className="tag-item"
|
||||
key={`${tag}-${index}`}
|
||||
onClick={() => this.bindTagClickEvent(tag)}
|
||||
>
|
||||
{tag}
|
||||
</div>
|
||||
))
|
||||
: null}
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
import React from 'react';
|
||||
import {observer} from 'mobx-react';
|
||||
import SearchPanel from './SearchPanel';
|
||||
import {EditorStoreType} from '../../store/editor';
|
||||
import {SubRendererInfo} from '../../plugin';
|
||||
|
||||
interface SearchRendererProps {
|
||||
store: EditorStoreType;
|
||||
}
|
||||
|
||||
interface SearchRendererStates {
|
||||
subRenderersByOrder: Array<SubRendererInfo>; // 仅获取一次
|
||||
defaultKeyword: string;
|
||||
}
|
||||
|
||||
@observer
|
||||
export default class SearchRendererPanel extends React.Component<
|
||||
SearchRendererProps,
|
||||
SearchRendererStates
|
||||
> {
|
||||
localStorageKey = 'amis-editor-renderer-search-history';
|
||||
lastSubRenderersTag: string; // 用于记录上一次的tag
|
||||
|
||||
constructor(props: any) {
|
||||
super(props);
|
||||
let subRenderersByOrder = props.store.subRenderersByOrder;
|
||||
// 剔除隐藏的组件
|
||||
subRenderersByOrder = subRenderersByOrder.filter(
|
||||
(item: SubRendererInfo) => !item.disabledRendererPlugin
|
||||
);
|
||||
const {subRenderersKeywords, subRenderersTag} = props.store;
|
||||
this.state = {
|
||||
subRenderersByOrder: subRenderersByOrder,
|
||||
defaultKeyword: subRenderersKeywords || subRenderersTag || ''
|
||||
};
|
||||
this.lastSubRenderersTag = props.store.subRenderersTag;
|
||||
|
||||
this.changeSubRenderersTag = this.changeSubRenderersTag.bind(this);
|
||||
}
|
||||
|
||||
UNSAFE_componentWillReceiveProps(nextProps: any) {
|
||||
const subRenderersTag = nextProps.store.subRenderersTag;
|
||||
const subRenderersKeywords = nextProps.store.subRenderersKeywords;
|
||||
/**
|
||||
* 当subRenderersTag在外部发现变动,则将其值同步给curKeyword
|
||||
* 备注:外部重置tag时,也会将subRenderersKeywords重置。
|
||||
*/
|
||||
if (subRenderersTag !== this.lastSubRenderersTag && !subRenderersKeywords) {
|
||||
this.lastSubRenderersTag = subRenderersTag;
|
||||
this.setState({
|
||||
defaultKeyword: subRenderersTag
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
changeSubRenderersTag(curTag: string) {
|
||||
this.lastSubRenderersTag = curTag;
|
||||
this.props.store.changeSubRenderersTag(curTag);
|
||||
}
|
||||
|
||||
render() {
|
||||
const {subRenderersByOrder, defaultKeyword} = this.state;
|
||||
const {changeSubRenderersKeywords} = this.props.store;
|
||||
|
||||
return (
|
||||
<SearchPanel
|
||||
allResult={subRenderersByOrder}
|
||||
externalKeyword={defaultKeyword}
|
||||
searchPanelUUID={this.localStorageKey}
|
||||
onChange={changeSubRenderersKeywords}
|
||||
onTagChange={this.changeSubRenderersTag}
|
||||
immediateChange={true}
|
||||
closeAutoComplete={true}
|
||||
/>
|
||||
);
|
||||
}
|
||||
}
|
||||
181
packages/amis-editor-core/src/component/base/ShortcutKey.tsx
Normal file
@@ -0,0 +1,181 @@
|
||||
import React from 'react';
|
||||
import {Modal, Icon} from 'amis';
|
||||
import cx from 'classnames';
|
||||
import {autobind} from '../../util';
|
||||
|
||||
export interface ShortcutKeyProps {
|
||||
title?: string;
|
||||
size?: string;
|
||||
closeOnEsc?: boolean;
|
||||
closeOnOutside?: boolean;
|
||||
ShortcutKeyList?: Array<ShortcutKeyItem>;
|
||||
}
|
||||
|
||||
interface ShortcutKeyItem {
|
||||
title: string;
|
||||
letters: Array<string>;
|
||||
tooltip?: string;
|
||||
}
|
||||
|
||||
export interface ShortcutKeyStates {
|
||||
visible: boolean;
|
||||
}
|
||||
|
||||
const ShortcutKeyList = [
|
||||
{
|
||||
title: '复制',
|
||||
letters: ['⌘', 'c'],
|
||||
tooltip: '复制当前选中元素'
|
||||
},
|
||||
{
|
||||
title: '粘贴',
|
||||
letters: ['⌘', 'v'],
|
||||
tooltip: '将复制的元素插入到当前选中节点'
|
||||
},
|
||||
{
|
||||
title: '剪切',
|
||||
letters: ['⌘', 'x'],
|
||||
tooltip: '剪切当前选中元素'
|
||||
},
|
||||
{
|
||||
title: '撤销',
|
||||
letters: ['⌘', 'z'],
|
||||
tooltip: '恢复上一次撤销的操作'
|
||||
},
|
||||
{
|
||||
title: '重做',
|
||||
letters: ['⌘', 'Shift', 'z'],
|
||||
tooltip: '恢复上一次撤销的操作'
|
||||
},
|
||||
{
|
||||
title: '保存',
|
||||
letters: ['⌘', 's'],
|
||||
tooltip: '保存当前所有操作'
|
||||
},
|
||||
{
|
||||
title: '预览/编辑',
|
||||
letters: ['⌘', 'p'],
|
||||
tooltip: '开启/关闭预览模式'
|
||||
},
|
||||
{
|
||||
title: '删除',
|
||||
letters: ['Delete'],
|
||||
tooltip: '删除当前节点'
|
||||
},
|
||||
{
|
||||
title: '删除',
|
||||
letters: ['Backspace'],
|
||||
tooltip: '删除当前节点'
|
||||
},
|
||||
{
|
||||
title: '向上移动',
|
||||
letters: ['⌘', '↑'],
|
||||
tooltip: '向上移动当前节点'
|
||||
},
|
||||
{
|
||||
title: '向下移动',
|
||||
letters: ['⌘', '↓'],
|
||||
tooltip: '向下移动当前节点'
|
||||
}
|
||||
];
|
||||
|
||||
export default class ShortcutKey extends React.Component<
|
||||
ShortcutKeyProps,
|
||||
ShortcutKeyStates
|
||||
> {
|
||||
constructor(props: any) {
|
||||
super(props);
|
||||
|
||||
this.state = {
|
||||
visible: false
|
||||
};
|
||||
}
|
||||
|
||||
@autobind
|
||||
closeShortcutKeyModal() {
|
||||
this.setState({
|
||||
visible: false
|
||||
});
|
||||
}
|
||||
|
||||
@autobind
|
||||
showShortcutKeyModal() {
|
||||
this.setState({
|
||||
visible: true
|
||||
});
|
||||
}
|
||||
|
||||
render() {
|
||||
const {title, size, closeOnEsc, closeOnOutside} = this.props;
|
||||
const curShortcutKeyList = this.props.ShortcutKeyList || ShortcutKeyList;
|
||||
|
||||
return (
|
||||
<>
|
||||
<div
|
||||
className="shortcut-icon-btn"
|
||||
editor-tooltip="点击查看当前可用快捷键"
|
||||
tooltip-position="bottom"
|
||||
>
|
||||
<Icon icon="editor-shortcut" onClick={this.showShortcutKeyModal} />
|
||||
</div>
|
||||
<Modal
|
||||
size={size || 'xs'}
|
||||
show={this.state.visible}
|
||||
closeOnEsc={closeOnEsc ?? true}
|
||||
closeOnOutside={closeOnOutside ?? true}
|
||||
onHide={this.closeShortcutKeyModal}
|
||||
contentClassName="shortcut-list-modal"
|
||||
>
|
||||
<div className="shortcut-modal-header">
|
||||
<div className="shortcut-modal-title">
|
||||
{title || '当前可用快捷键'}
|
||||
</div>
|
||||
<Icon
|
||||
icon="close"
|
||||
className="shortcut-modal-icon"
|
||||
onClick={this.closeShortcutKeyModal}
|
||||
/>
|
||||
</div>
|
||||
<div className="shortcut-modal-body">
|
||||
{curShortcutKeyList && curShortcutKeyList.length > 0 && (
|
||||
<div className="shortcut-list">
|
||||
{curShortcutKeyList.map(
|
||||
(shortcutKey: ShortcutKeyItem, index: number) => {
|
||||
return (
|
||||
<div className="shortcut-item" key={index}>
|
||||
{shortcutKey.tooltip && (
|
||||
<div
|
||||
className="shortcut-title"
|
||||
editor-tooltip={shortcutKey.tooltip}
|
||||
>
|
||||
{shortcutKey.title}
|
||||
</div>
|
||||
)}
|
||||
{!shortcutKey.tooltip && (
|
||||
<div className="shortcut-title">
|
||||
{shortcutKey.title}
|
||||
</div>
|
||||
)}
|
||||
<div className="shortcut-letters">
|
||||
{shortcutKey.letters.map((letter: string) => {
|
||||
return (
|
||||
<div className="shortcut-letter" key={letter}>
|
||||
{letter}
|
||||
</div>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
{!curShortcutKeyList ||
|
||||
(curShortcutKeyList.length === 0 && <span>暂无快捷键</span>)}
|
||||
</div>
|
||||
</Modal>
|
||||
</>
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
import React from 'react';
|
||||
import cx from 'classnames';
|
||||
|
||||
interface WidthDraggableProps {
|
||||
isLeftDragIcon?: boolean;
|
||||
className?: any;
|
||||
}
|
||||
|
||||
export class WidthDraggableBtn extends React.Component<WidthDraggableProps> {
|
||||
startX: number;
|
||||
startWidth: number;
|
||||
dragWrap: HTMLElement;
|
||||
|
||||
constructor(props: any) {
|
||||
super(props);
|
||||
this.handleResizeMouseDown = this.handleResizeMouseDown.bind(this);
|
||||
this.handleResizeMouseMove = this.handleResizeMouseMove.bind(this);
|
||||
this.handleResizeMouseUp = this.handleResizeMouseUp.bind(this);
|
||||
}
|
||||
|
||||
handleResizeMouseDown(e: React.MouseEvent) {
|
||||
// todo 可能 ie 不正确
|
||||
let isRightMB = e.nativeEvent.which == 3;
|
||||
|
||||
if (isRightMB) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.dragWrap = e.currentTarget.parentElement as HTMLElement;
|
||||
document.addEventListener('mousemove', this.handleResizeMouseMove);
|
||||
document.addEventListener('mouseup', this.handleResizeMouseUp);
|
||||
this.startX = e.clientX;
|
||||
this.startWidth = this.dragWrap.offsetWidth;
|
||||
// 拖拽过程中会选中文本,导致卡顿,所以阻止下默认行为
|
||||
e.preventDefault();
|
||||
}
|
||||
|
||||
handleResizeMouseMove(e: MouseEvent) {
|
||||
const {isLeftDragIcon} = this.props;
|
||||
const dx = e.clientX - this.startX;
|
||||
this.dragWrap.style.cssText += `width: ${Math.max(
|
||||
isLeftDragIcon ? this.startWidth - dx : this.startWidth + dx,
|
||||
260
|
||||
)}px`;
|
||||
}
|
||||
|
||||
handleResizeMouseUp() {
|
||||
document.removeEventListener('mousemove', this.handleResizeMouseMove);
|
||||
document.removeEventListener('mouseup', this.handleResizeMouseUp);
|
||||
}
|
||||
|
||||
render() {
|
||||
const {isLeftDragIcon, className} = this.props;
|
||||
return (
|
||||
<div
|
||||
onMouseDown={this.handleResizeMouseDown}
|
||||
className={cx(
|
||||
'width-draggable-icon',
|
||||
className,
|
||||
isLeftDragIcon ? 'leftBtn' : ''
|
||||
)}
|
||||
></div>
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
import React from 'react';
|
||||
import {WidthDraggableBtn} from './WidthDraggableBtn';
|
||||
|
||||
export default function (
|
||||
NeedWidthDraggableComp: React.ComponentType,
|
||||
isLeftDragIcon?: boolean
|
||||
): React.ComponentType {
|
||||
return class WidthDraggableContainer extends React.Component {
|
||||
render() {
|
||||
return (
|
||||
<>
|
||||
<NeedWidthDraggableComp {...this.props} />
|
||||
<WidthDraggableBtn isLeftDragIcon={isLeftDragIcon} />
|
||||
</>
|
||||
);
|
||||
}
|
||||
};
|
||||
}
|
||||
699
packages/amis-editor-core/src/component/factory.tsx
Normal file
@@ -0,0 +1,699 @@
|
||||
import {render, RendererProps} from 'amis';
|
||||
import {isAlive} from 'mobx-state-tree';
|
||||
import React from 'react';
|
||||
import {NodeWrapper} from './NodeWrapper';
|
||||
import {PanelProps, RegionConfig, RendererInfo} from '../plugin';
|
||||
import groupBy from 'lodash/groupBy';
|
||||
import {RegionWrapper} from './RegionWrapper';
|
||||
import find from 'lodash/find';
|
||||
import {ContainerWrapper} from './ContainerWrapper';
|
||||
import {observer} from 'mobx-react';
|
||||
import {EditorNodeContext, EditorNodeType} from '../store/node';
|
||||
import {EditorManager} from '../manager';
|
||||
import flatten from 'lodash/flatten';
|
||||
import {render as reactRender, unmountComponentAtNode} from 'react-dom';
|
||||
import {autobind, JSONGetById, JSONUpdate, appTranslate} from '../util';
|
||||
import {ErrorBoundary, LazyComponent} from 'amis-core';
|
||||
import {CommonConfigWrapper} from './CommonConfigWrapper';
|
||||
import type {Schema} from 'amis';
|
||||
import type {DataScope} from 'amis-core';
|
||||
import type {RendererConfig} from 'amis-core';
|
||||
import type {SchemaCollection} from 'amis';
|
||||
import {SchemaForm} from './base/SchemaForm';
|
||||
import memoize from 'lodash/memoize';
|
||||
import {FormConfigWrapper} from './FormConfigWrapper';
|
||||
|
||||
// 创建 Node Store 并构建成树
|
||||
export function makeWrapper(
|
||||
manager: EditorManager,
|
||||
info: RendererInfo,
|
||||
rendererConfig: RendererConfig
|
||||
) {
|
||||
type Props = RendererProps & {
|
||||
$$id: string;
|
||||
};
|
||||
const store = manager.store;
|
||||
const renderer = rendererConfig.component!;
|
||||
|
||||
@observer
|
||||
class Wrapper extends React.Component<Props> {
|
||||
static displayName = renderer.displayName;
|
||||
static propsList = ((renderer && renderer.propsList) || []).concat([
|
||||
'$$id'
|
||||
// '$$editor'
|
||||
]);
|
||||
static contextType = EditorNodeContext;
|
||||
editorNode?: EditorNodeType;
|
||||
scopeId?: string;
|
||||
|
||||
UNSAFE_componentWillMount() {
|
||||
const parent: EditorNodeType = (this.context as any) || store.root;
|
||||
if (!info.id) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.editorNode = parent.addChild({
|
||||
id: info.id, // 页面schema中的 $$id
|
||||
type: info.type,
|
||||
label: info.name,
|
||||
isCommonConfig: !!this.props.$$commonSchema,
|
||||
isFormConfig: !!this.props.$$formSchema,
|
||||
path: this.props.$path,
|
||||
schemaPath: info.schemaPath,
|
||||
dialogTitle: info.dialogTitle,
|
||||
dialogType: info.dialogType,
|
||||
info,
|
||||
getData: () => this.props.data
|
||||
});
|
||||
|
||||
this.editorNode!.setRendererConfig(rendererConfig);
|
||||
|
||||
// 查找父数据域,将当前组件数据域追加上去,使其形成父子关系
|
||||
if (
|
||||
(rendererConfig.storeType || info.isListComponent) &&
|
||||
!manager.dataSchema.hasScope(`${info.id}-${info.type}`)
|
||||
) {
|
||||
let from = parent;
|
||||
let closestScope: DataScope | undefined = undefined;
|
||||
while (from && !closestScope) {
|
||||
if (from === store.root) {
|
||||
closestScope = manager.dataSchema.getScope('root');
|
||||
} else if (manager.dataSchema.hasScope(`${from.id}-${from.type}`)) {
|
||||
closestScope = manager.dataSchema.getScope(
|
||||
`${from.id}-${from.type}`
|
||||
);
|
||||
}
|
||||
|
||||
// node.parent 是找不到 root 那层的,所以需要自己加逻辑
|
||||
from = from.parentId === 'root' ? store.root : from.parent;
|
||||
}
|
||||
|
||||
if (closestScope) {
|
||||
manager.dataSchema.switchTo(closestScope.id);
|
||||
} else {
|
||||
throw new Error('程序错误');
|
||||
}
|
||||
|
||||
this.scopeId = `${info.id}-${info.type}`;
|
||||
manager.dataSchema.addScope([], this.scopeId);
|
||||
if (info.name) {
|
||||
const nodeSchema = manager.store.getNodeById(info.id)?.schema;
|
||||
const tag = appTranslate(nodeSchema?.title ?? nodeSchema?.name);
|
||||
manager.dataSchema.current.tag = `${info.name}${
|
||||
tag && typeof tag === 'string' ? ` : ${tag}` : ''
|
||||
}`;
|
||||
manager.dataSchema.current.group = '组件上下文';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
componentDidUpdate(prevProps: Props) {
|
||||
const props = this.props;
|
||||
|
||||
if (
|
||||
this.editorNode &&
|
||||
props.$$commonSchema !== prevProps.$$commonSchema
|
||||
) {
|
||||
this.editorNode.updateIsCommonConfig(!!this.props.$$commonSchema);
|
||||
}
|
||||
|
||||
if (this.editorNode && props.$$formSchema !== prevProps.$$formSchema) {
|
||||
this.editorNode.updateIsFormConfig(!!this.props.$$formSchema);
|
||||
}
|
||||
}
|
||||
|
||||
componentWillUnmount() {
|
||||
if (this.editorNode && isAlive(this.editorNode)) {
|
||||
const parent: EditorNodeType = (this.context as any) || store.root;
|
||||
parent.removeChild(this.editorNode);
|
||||
}
|
||||
|
||||
if (this.scopeId) {
|
||||
manager.dataSchema.removeScope(this.scopeId);
|
||||
}
|
||||
}
|
||||
|
||||
@autobind
|
||||
wrapperRef(raw: any) {
|
||||
let ref = raw;
|
||||
while (ref?.getWrappedInstance) {
|
||||
ref = ref.getWrappedInstance();
|
||||
}
|
||||
|
||||
if (ref && !ref.props) {
|
||||
Object.defineProperty(ref, 'props', {
|
||||
get: () => this.props
|
||||
});
|
||||
} else if (!ref && raw) {
|
||||
ref = {};
|
||||
Object.defineProperty(ref, 'props', {
|
||||
get: () => this.props
|
||||
});
|
||||
}
|
||||
|
||||
if (this.editorNode && isAlive(this.editorNode)) {
|
||||
this.editorNode.setComponent(ref);
|
||||
|
||||
info.plugin?.componentRef?.(this.editorNode, ref);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 主要目的是渲染器下发的 $$editor 指向当前层,而不是 store 层。
|
||||
*/
|
||||
@autobind
|
||||
renderChild(region: string, node: Schema, props: any) {
|
||||
const {render} = this.props; // render: amis渲染器
|
||||
|
||||
// $$id 变化,渲染器最好也变化
|
||||
if (node?.$$id) {
|
||||
props = props || {}; // props 可能为 undefined
|
||||
props.key = `${props.key}-${node.$$id}`;
|
||||
}
|
||||
|
||||
return render(region, node, {...props, $$editor: info});
|
||||
}
|
||||
|
||||
render() {
|
||||
const {$$id, ...rest} = this.props;
|
||||
|
||||
/*
|
||||
* 根据渲染器信息决定时 NodeWrapper 包裹还是 ContainerWrapper 包裹。
|
||||
* NodeWrapper 主要完成 dom 节点的标记(即:添加 data-editor-id 属性)。
|
||||
* 同时如果插件中设置了自定义渲染,则会调用自定义渲染相关逻辑。
|
||||
* ContainerWrapper 本身依然会包一层 NodeWrapper, 除此之外,主要完成对区域的包裹,和 dom 标记。
|
||||
* 会不会包裹区域跟编辑器信息中配置不配置 regions 有关,配置了才会包裹。
|
||||
* **并不是每个容器节点都是这样的。比如 amis 中的 Table 渲染器。**
|
||||
*/
|
||||
const Wrapper = /*info.wrapper || (*/ this.props.$$commonSchema
|
||||
? CommonConfigWrapper
|
||||
: this.props.$$formSchema
|
||||
? FormConfigWrapper
|
||||
: info.regions
|
||||
? ContainerWrapper
|
||||
: NodeWrapper; /*)*/
|
||||
return (
|
||||
<EditorNodeContext.Provider
|
||||
value={this.editorNode || (this.context as any)}
|
||||
>
|
||||
<ErrorBoundary
|
||||
customErrorMsg={`拦截到${info.type}渲染错误`}
|
||||
fallback={() => {
|
||||
return (
|
||||
<div className="renderer-error-boundary">
|
||||
{info?.type}
|
||||
渲染发生错误,详细错误信息请查看控制台输出。
|
||||
</div>
|
||||
);
|
||||
}}
|
||||
>
|
||||
{info.useLazyRender ? (
|
||||
<LazyComponent placeholder={<span />}>
|
||||
<Wrapper
|
||||
{...rest}
|
||||
render={this.renderChild}
|
||||
$$editor={info}
|
||||
$$node={this.editorNode}
|
||||
ref={this.wrapperRef}
|
||||
/>
|
||||
</LazyComponent>
|
||||
) : (
|
||||
<Wrapper
|
||||
{...rest}
|
||||
render={this.renderChild}
|
||||
$$editor={info}
|
||||
$$node={this.editorNode}
|
||||
ref={this.wrapperRef}
|
||||
/>
|
||||
)}
|
||||
</ErrorBoundary>
|
||||
</EditorNodeContext.Provider>
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return Wrapper as any;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将之前选择的弹窗和本次现有弹窗schema替换为$ref引用
|
||||
* @param schema
|
||||
* @param dialogId 选择弹窗的id
|
||||
* @param dialogRefsName
|
||||
*/
|
||||
function replaceDialogtoRef(
|
||||
schema: Schema,
|
||||
dialogId: string,
|
||||
dialogRefsName: string
|
||||
) {
|
||||
let replacedSchema = schema;
|
||||
const dialog = JSONGetById(schema, dialogId);
|
||||
if (dialog) {
|
||||
replacedSchema = JSONUpdate(schema, dialogId, {$ref: dialogRefsName}, true);
|
||||
}
|
||||
return replacedSchema;
|
||||
}
|
||||
|
||||
export function makeSchemaFormRender(
|
||||
manager: EditorManager,
|
||||
schema: {
|
||||
body?: SchemaCollection;
|
||||
controls?: Array<any>;
|
||||
definitions?: any;
|
||||
api?: any;
|
||||
submitOnChange?: boolean;
|
||||
justify?: boolean;
|
||||
panelById?: string;
|
||||
formKey?: string;
|
||||
pipeIn?: (value: any) => any;
|
||||
pipeOut?: (value: any) => any;
|
||||
}
|
||||
) {
|
||||
const env = {...manager.env, session: 'schema-form'};
|
||||
const filterBody = memoize(body =>
|
||||
body ? flatten(Array.isArray(body) ? body : [body]) : undefined
|
||||
);
|
||||
|
||||
return ({
|
||||
value,
|
||||
onChange,
|
||||
popOverContainer,
|
||||
id,
|
||||
store,
|
||||
node,
|
||||
readonly
|
||||
}: PanelProps) => {
|
||||
const ctx = {...manager.store.ctx};
|
||||
|
||||
if (schema?.panelById && schema?.panelById !== node?.id) {
|
||||
// 用于过滤掉异常的渲染
|
||||
return <></>;
|
||||
}
|
||||
|
||||
if (id) {
|
||||
Object.defineProperty(ctx, '__props__', {
|
||||
get: () => {
|
||||
const node = store.getNodeById(id);
|
||||
return node?.getComponent()?.props || {};
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// 每一层的面板数据不要共用
|
||||
const curFormKey = `${id}-${node?.type}${schema.formKey ? '-' : ''}${
|
||||
schema.formKey ? schema.formKey : ''
|
||||
}`;
|
||||
|
||||
const body = filterBody(schema.body);
|
||||
const controls = filterBody(schema.controls);
|
||||
|
||||
return (
|
||||
<SchemaForm
|
||||
key={curFormKey}
|
||||
propKey={curFormKey}
|
||||
api={schema.api}
|
||||
definitions={schema.definitions}
|
||||
body={body}
|
||||
controls={controls}
|
||||
value={value}
|
||||
ctx={ctx}
|
||||
pipeIn={schema.pipeIn}
|
||||
pipeOut={schema.pipeOut}
|
||||
submitOnChange={schema.submitOnChange}
|
||||
onChange={onChange}
|
||||
env={env}
|
||||
popOverContainer={popOverContainer}
|
||||
node={node}
|
||||
manager={manager}
|
||||
justify={schema.justify}
|
||||
readonly={readonly}
|
||||
/>
|
||||
);
|
||||
};
|
||||
}
|
||||
|
||||
export function hackIn(
|
||||
renderer: RendererConfig,
|
||||
regions?: Array<RegionConfig>,
|
||||
overrides?: any
|
||||
) {
|
||||
let rawComponent: any = renderer.Renderer;
|
||||
while (rawComponent.ComposedComponent) {
|
||||
rawComponent = rawComponent.ComposedComponent;
|
||||
}
|
||||
|
||||
const prototype: any = rawComponent.prototype;
|
||||
|
||||
if (Array.isArray(regions)) {
|
||||
const grouped = groupBy(regions, item => item.renderMethod);
|
||||
|
||||
// 主要是 render 方法没办法覆盖,只能指定其他 render 方法。
|
||||
// 所以才需要配置 renderMethod 指定覆盖其他方法。
|
||||
Object.keys(grouped).forEach(key => {
|
||||
// 不要重复 hack, 或者目标方法不存在。
|
||||
if (prototype[`__${key}`] || !prototype[key]) {
|
||||
return;
|
||||
}
|
||||
|
||||
const regions = grouped[key];
|
||||
const customRenderCreator = regions[0]?.renderMethodOverride;
|
||||
|
||||
prototype[`__${key}`] = prototype[key];
|
||||
prototype[key] = (function (origin, fn) {
|
||||
if (typeof origin !== 'function') {
|
||||
return origin;
|
||||
}
|
||||
|
||||
return function (this: any) {
|
||||
const prev = this.super;
|
||||
this.super = origin.bind(this);
|
||||
const result = fn.apply(this, arguments);
|
||||
this.super = prev;
|
||||
return result;
|
||||
};
|
||||
})(
|
||||
prototype[`__${key}`],
|
||||
customRenderCreator?.(regions.concat(), insertRegion) ||
|
||||
function (this: any, ...args: any[]) {
|
||||
const info: RendererInfo = this.props.$$editor;
|
||||
const dom = this.super.apply(this, args);
|
||||
|
||||
if (
|
||||
info &&
|
||||
(!this.props.$$commonSchema || !this.props.$$formSchema) &&
|
||||
Array.isArray(info.regions) &&
|
||||
regions.every(region =>
|
||||
find(info.regions!, c => c.key === region.key)
|
||||
)
|
||||
) {
|
||||
const regionsCopy = regions.map(r => {
|
||||
const i = find(
|
||||
info.regions,
|
||||
c =>
|
||||
c.key === r.key &&
|
||||
(!r.rendererName || r.rendererName === c.rendererName)
|
||||
);
|
||||
|
||||
if (i) {
|
||||
return {
|
||||
...r,
|
||||
label: i.label,
|
||||
preferTag: i.preferTag
|
||||
};
|
||||
}
|
||||
|
||||
return r;
|
||||
});
|
||||
|
||||
return insertRegion(
|
||||
this,
|
||||
dom,
|
||||
regionsCopy,
|
||||
info,
|
||||
info.plugin.manager
|
||||
);
|
||||
}
|
||||
|
||||
return dom;
|
||||
}
|
||||
);
|
||||
});
|
||||
} else if (overrides) {
|
||||
Object.keys(overrides).forEach(key => {
|
||||
// 不要重复 hack
|
||||
if (prototype[`__${key}`] || typeof prototype[key] !== 'function') {
|
||||
return;
|
||||
}
|
||||
|
||||
prototype[`__${key}`] = prototype[key];
|
||||
prototype[key] = (function (origin, fn) {
|
||||
if (typeof origin !== 'function') {
|
||||
return origin;
|
||||
}
|
||||
|
||||
return function (this: any) {
|
||||
const prev = this.super;
|
||||
this.super = origin.bind(this);
|
||||
const result = fn.apply(this, arguments);
|
||||
this.super = prev;
|
||||
return result;
|
||||
};
|
||||
})(prototype[`__${key}`], overrides[key]);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
function getMatchedRegion(
|
||||
component: JSX.Element,
|
||||
dom: JSX.Element,
|
||||
regions: Array<RegionConfig>
|
||||
): [RegionConfig | undefined, number] {
|
||||
let index = -1;
|
||||
let resolved: RegionConfig | undefined = undefined;
|
||||
|
||||
regions.some((item, i) => {
|
||||
/*
|
||||
正常直接在覆盖的方法外面包裹一次 Region 即可,但是有时候其实并不是正则的容器,
|
||||
有时候可能时返回的 JSX.Element 里面的 Element。
|
||||
所以有时候还得配置 matchRegion 这样的属性,来告诉 hack 逻辑应该包裹到哪块。
|
||||
还有个 insertPosition 也是相关的配置。
|
||||
*/
|
||||
if (item.matchRegion!(dom, component)) {
|
||||
index = i;
|
||||
resolved = item;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
});
|
||||
|
||||
return [resolved, index];
|
||||
}
|
||||
|
||||
function insertRegion(
|
||||
component: JSX.Element,
|
||||
dom: JSX.Element,
|
||||
regions: Array<RegionConfig>,
|
||||
info: RendererInfo,
|
||||
manager: EditorManager
|
||||
): JSX.Element {
|
||||
// 如果没有开启这些区域的内容修改功能,则跳过插入逻辑
|
||||
if (info.memberImmutable === true) {
|
||||
return dom;
|
||||
} else if (
|
||||
Array.isArray(info.memberImmutable) &&
|
||||
regions.every(reg =>
|
||||
(info.memberImmutable as Array<string>).includes(reg.key)
|
||||
)
|
||||
) {
|
||||
return dom;
|
||||
}
|
||||
|
||||
const rootRegion = find(regions, r => !r.matchRegion);
|
||||
|
||||
if (rootRegion) {
|
||||
const Region = rootRegion.wrapper || RegionWrapper;
|
||||
|
||||
if (rootRegion.insertPosition === 'inner' && React.isValidElement(dom)) {
|
||||
return React.cloneElement(dom as any, {
|
||||
children: (
|
||||
<Region
|
||||
key={rootRegion.key}
|
||||
preferTag={rootRegion.preferTag}
|
||||
name={rootRegion.key}
|
||||
label={rootRegion.label}
|
||||
placeholder={rootRegion.placeholder}
|
||||
regionConfig={rootRegion}
|
||||
editorStore={manager.store}
|
||||
manager={manager}
|
||||
children={(dom as any).props.children}
|
||||
wrapperResolve={rootRegion.wrapperResolve}
|
||||
rendererName={info.renderer.name}
|
||||
/>
|
||||
)
|
||||
});
|
||||
} else {
|
||||
return (
|
||||
<Region
|
||||
key={rootRegion.key}
|
||||
preferTag={rootRegion.preferTag}
|
||||
name={rootRegion.key}
|
||||
label={rootRegion.label}
|
||||
placeholder={rootRegion.placeholder}
|
||||
regionConfig={rootRegion}
|
||||
editorStore={manager.store}
|
||||
manager={manager}
|
||||
children={dom}
|
||||
wrapperResolve={rootRegion.wrapperResolve}
|
||||
rendererName={info.renderer.name}
|
||||
/>
|
||||
);
|
||||
}
|
||||
} else if (regions.length) {
|
||||
const [resolved, index] = getMatchedRegion(component, dom, regions);
|
||||
|
||||
if (resolved) {
|
||||
const Region = resolved.wrapper || RegionWrapper;
|
||||
regions.splice(index, 1);
|
||||
|
||||
if (resolved.insertPosition === 'outter') {
|
||||
return (
|
||||
<Region
|
||||
key={resolved.key}
|
||||
preferTag={resolved.preferTag}
|
||||
name={resolved.key}
|
||||
label={resolved.label}
|
||||
placeholder={resolved.placeholder}
|
||||
regionConfig={resolved}
|
||||
editorStore={manager.store}
|
||||
manager={manager}
|
||||
children={dom}
|
||||
wrapperResolve={resolved.wrapperResolve}
|
||||
/>
|
||||
);
|
||||
} else if (React.isValidElement(dom)) {
|
||||
const children = (dom.props as any).children;
|
||||
|
||||
return React.cloneElement(dom, {
|
||||
children: (
|
||||
<Region
|
||||
key={resolved.key}
|
||||
preferTag={resolved.preferTag}
|
||||
name={resolved.key}
|
||||
label={resolved.label}
|
||||
placeholder={resolved.placeholder}
|
||||
regionConfig={resolved}
|
||||
editorStore={manager.store}
|
||||
manager={manager}
|
||||
children={children}
|
||||
wrapperResolve={resolved.wrapperResolve}
|
||||
/>
|
||||
)
|
||||
} as any);
|
||||
}
|
||||
} else if (React.isValidElement(dom) && (dom.props as any).children) {
|
||||
let children: any = (dom.props as any).children;
|
||||
|
||||
if (Array.isArray(children)) {
|
||||
children = children.map(child =>
|
||||
insertRegion(component, child, regions, info, manager)
|
||||
);
|
||||
} else {
|
||||
children = insertRegion(
|
||||
component,
|
||||
children,
|
||||
regions,
|
||||
info,
|
||||
manager
|
||||
) as any;
|
||||
}
|
||||
|
||||
return React.cloneElement(dom, {
|
||||
children
|
||||
} as any);
|
||||
}
|
||||
}
|
||||
|
||||
return dom;
|
||||
}
|
||||
|
||||
export function mapReactElement(
|
||||
dom: JSX.Element,
|
||||
iterator: (dom: JSX.Element, index?: number) => JSX.Element,
|
||||
index?: number
|
||||
) {
|
||||
if (!React.isValidElement(dom)) {
|
||||
return dom;
|
||||
}
|
||||
|
||||
let mapped = iterator(dom, index);
|
||||
|
||||
// 如果完全一样,说明没找到要替换的。
|
||||
if (mapped === dom && (dom.props as any).children) {
|
||||
const children = (dom.props as any).children;
|
||||
if (Array.isArray(children)) {
|
||||
const childMapped: Array<any> = [];
|
||||
let modified = false;
|
||||
children.forEach((child: any, index: number) => {
|
||||
let mapped = mapReactElement(child, iterator, index);
|
||||
|
||||
if (mapped !== child) {
|
||||
modified = true;
|
||||
if (React.isValidElement(mapped) && !mapped.key) {
|
||||
mapped = React.cloneElement(mapped, {key: index});
|
||||
}
|
||||
}
|
||||
|
||||
childMapped.push(mapped);
|
||||
});
|
||||
|
||||
if (modified) {
|
||||
mapped = React.cloneElement(mapped, {
|
||||
children: childMapped
|
||||
});
|
||||
}
|
||||
} else {
|
||||
const childMapped = mapReactElement(children, iterator, index);
|
||||
if (childMapped !== children) {
|
||||
mapped = React.cloneElement(mapped, {
|
||||
children: childMapped
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return mapped;
|
||||
}
|
||||
|
||||
const thumbHost = document.createElement('div');
|
||||
export function renderThumbToGhost(
|
||||
ghost: HTMLElement,
|
||||
region: EditorNodeType,
|
||||
schema: any,
|
||||
manager: EditorManager
|
||||
) {
|
||||
// bca-disable-next-line
|
||||
ghost.innerHTML = '';
|
||||
let path = '';
|
||||
const host = region.host!;
|
||||
const component = host.getComponent()!;
|
||||
const isForm = component?.renderControl && region.region === 'body';
|
||||
|
||||
try {
|
||||
reactRender(
|
||||
render(
|
||||
{
|
||||
children: ({render}: any) => {
|
||||
return isForm
|
||||
? render('', {
|
||||
type: 'form',
|
||||
wrapWithPanel: false,
|
||||
mode: component.props.mode,
|
||||
body: [schema]
|
||||
})
|
||||
: render(region.region, schema);
|
||||
}
|
||||
} as any,
|
||||
{},
|
||||
{
|
||||
...manager.env,
|
||||
theme: component?.props.theme || manager.env.theme,
|
||||
session: 'ghost-thumb'
|
||||
},
|
||||
path
|
||||
),
|
||||
thumbHost
|
||||
);
|
||||
} catch (e) {}
|
||||
|
||||
/* bca-disable */
|
||||
const html =
|
||||
thumbHost.innerHTML ||
|
||||
'<div class="wrapper-sm b-a b-light m-b-sm">拖入占位</div>';
|
||||
// bca-disable-line
|
||||
ghost.innerHTML = html;
|
||||
/* bca-enable */
|
||||
|
||||
unmountComponentAtNode(thumbHost);
|
||||
// bca-disable-next-line
|
||||
thumbHost.innerHTML = '';
|
||||
}
|
||||
130
packages/amis-editor-core/src/deepSplice.ts
Normal file
@@ -0,0 +1,130 @@
|
||||
import isPlainObject from 'lodash/isPlainObject';
|
||||
|
||||
/**
|
||||
* 深度 splice 数组,同时返回新的对象,按需拷贝,没有副作用
|
||||
*
|
||||
* 第二个参数可以是一个函数,函数的返回值不为 false 时,表示找到了目标,函数的返回值为 number 时,表示偏移量
|
||||
*
|
||||
* @param target
|
||||
* @param path
|
||||
* @param numberToDelete
|
||||
* @param items
|
||||
* @returns
|
||||
*/
|
||||
export function deepSplice(
|
||||
target: any,
|
||||
path:
|
||||
| string
|
||||
| ((value: any, index: number | string, stack: Array<any>) => any),
|
||||
numberToDelete: number,
|
||||
provider?: ((origin: any) => any) | any,
|
||||
...items: any[]
|
||||
) {
|
||||
if (typeof path === 'function') {
|
||||
path = getPathByFunction(target, path);
|
||||
}
|
||||
|
||||
if (!path) {
|
||||
return target;
|
||||
}
|
||||
|
||||
const paths = (path as string).split('.');
|
||||
const last = paths.pop()!;
|
||||
let host = target;
|
||||
const stack: Array<{
|
||||
host: any;
|
||||
key: string | number | undefined;
|
||||
}> = [];
|
||||
for (let i = 0; i < paths.length; i++) {
|
||||
stack.unshift({
|
||||
key: paths[i]!,
|
||||
host: host
|
||||
});
|
||||
host = host[paths[i]];
|
||||
}
|
||||
|
||||
if (!Array.isArray(host)) {
|
||||
throw new Error('deepSplice: target is not an array');
|
||||
}
|
||||
host = host.concat();
|
||||
|
||||
if (provider && typeof provider === 'function') {
|
||||
host.splice.apply(
|
||||
host,
|
||||
[last, numberToDelete].concat(provider(host[last]))
|
||||
);
|
||||
} else {
|
||||
host.splice.apply(host, [last, numberToDelete, provider].concat(items));
|
||||
}
|
||||
|
||||
return stack.reduce((prefix, {host, key}) => {
|
||||
host = Array.isArray(host) ? host.concat() : {...host};
|
||||
host[key!] = prefix;
|
||||
|
||||
return host;
|
||||
}, host);
|
||||
}
|
||||
|
||||
function getPathByFunction(
|
||||
target: any,
|
||||
fn: (value: any, index: number | string, stack: Array<any>) => any
|
||||
): string {
|
||||
let paths: Array<string> = [];
|
||||
let resolved: boolean = false;
|
||||
let stack: Array<any> = [
|
||||
{
|
||||
path: '',
|
||||
key: '',
|
||||
stack: [],
|
||||
data: target
|
||||
}
|
||||
];
|
||||
|
||||
while (stack.length) {
|
||||
let cur = stack.shift()!;
|
||||
let data = cur.data;
|
||||
let path = cur.path;
|
||||
let nodeStack = cur.stack;
|
||||
let key = cur.key;
|
||||
|
||||
const result = fn(data, key, nodeStack);
|
||||
|
||||
if (result) {
|
||||
resolved = true;
|
||||
paths = path.split('.').filter((item: any) => item);
|
||||
|
||||
// apply offset
|
||||
if (typeof result === 'number') {
|
||||
paths[paths.length - 1] = (
|
||||
parseInt(paths[paths.length - 1], 10) + result
|
||||
).toString();
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
if (Array.isArray(data)) {
|
||||
// 注意 Object.keys 返回的 key 是字符串,所以这里要转换一下
|
||||
data.forEach((prop, key) => {
|
||||
stack.push({
|
||||
path: `${path}.${key}`,
|
||||
key: key,
|
||||
stack: [data].concat(nodeStack),
|
||||
data: prop
|
||||
});
|
||||
});
|
||||
} else if (isPlainObject(data)) {
|
||||
Object.keys(data).forEach(key => {
|
||||
let prop = data[key];
|
||||
stack.push({
|
||||
path: `${path}.${key}`,
|
||||
key: key,
|
||||
stack: [data].concat(nodeStack),
|
||||
data: prop
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return resolved ? paths.join('.') : '';
|
||||
}
|
||||
90
packages/amis-editor-core/src/dnd/autoScroll.ts
Normal file
@@ -0,0 +1,90 @@
|
||||
/**
|
||||
* @file 拖拽过程中自动滚动容器
|
||||
*/
|
||||
import {getScrollParent} from 'amis-core';
|
||||
|
||||
export interface AutoScrollOptions {
|
||||
/** 滚动容器,默认为 document.documentElement */
|
||||
container?: HTMLElement;
|
||||
}
|
||||
|
||||
export function getScrollableParent(
|
||||
element: HTMLElement,
|
||||
iframe?: HTMLElement
|
||||
): HTMLElement {
|
||||
if (iframe) {
|
||||
// 如果是移动端编辑,它的滚动是在 iframe 外面的。
|
||||
return getScrollParent(iframe) as HTMLElement;
|
||||
}
|
||||
|
||||
const scrollableParent = getScrollParent(element);
|
||||
return scrollableParent as HTMLElement;
|
||||
}
|
||||
|
||||
export class AutoScroll {
|
||||
options: AutoScrollOptions;
|
||||
|
||||
time: number;
|
||||
|
||||
constructor(options: AutoScrollOptions = {}) {
|
||||
this.options = {
|
||||
container: document.documentElement,
|
||||
...options
|
||||
};
|
||||
this.time = Date.now();
|
||||
}
|
||||
|
||||
checkScroll(e: MouseEvent | DragEvent) {
|
||||
const {container} = this.options;
|
||||
if (!container) return;
|
||||
|
||||
const rect = container.getBoundingClientRect();
|
||||
const thresholdX = Math.max(30, rect.width * 0.1);
|
||||
const thresholdY = Math.max(30, rect.height * 0.1);
|
||||
let mouseX = e.pageX;
|
||||
let mouseY = e.pageY;
|
||||
|
||||
// 如果拖拽发生在 iframe 中,则需要计算 iframe 的偏移
|
||||
if ((e.target as HTMLElement).ownerDocument !== container.ownerDocument) {
|
||||
const iframe = (e.target as HTMLElement).ownerDocument.defaultView
|
||||
?.frameElement;
|
||||
const iframeRect = iframe?.getBoundingClientRect();
|
||||
mouseX += iframeRect?.left ?? 0;
|
||||
mouseY += iframeRect?.top ?? 0;
|
||||
}
|
||||
|
||||
if (mouseX < rect.left + thresholdX && container.scrollLeft > 0) {
|
||||
// 计算距离边界的比例,越近比例越大
|
||||
const distanceRatio = 1 - (mouseX - rect.left) / thresholdX;
|
||||
// 根据比例计算滚动距离,最小5,最大30
|
||||
const scrollAmount = Math.max(5, Math.round(30 * distanceRatio));
|
||||
container.scrollLeft -= scrollAmount;
|
||||
} else if (
|
||||
mouseX > rect.right - thresholdX &&
|
||||
container.scrollLeft < container.scrollWidth - container.clientWidth
|
||||
) {
|
||||
// 计算距离边界的比例,越近比例越大
|
||||
const distanceRatio = 1 - (rect.right - mouseX) / thresholdX;
|
||||
// 根据比例计算滚动距离,最小5,最大30
|
||||
const scrollAmount = Math.max(5, Math.round(30 * distanceRatio));
|
||||
container.scrollLeft += scrollAmount;
|
||||
}
|
||||
|
||||
if (mouseY < rect.top + thresholdY && container.scrollTop > 0) {
|
||||
// 计算距离边界的比例,越近比例越大
|
||||
const distanceRatio = 1 - (mouseY - rect.top) / thresholdY;
|
||||
// 根据比例计算滚动距离,最小5,最大30
|
||||
const scrollAmount = Math.max(5, Math.round(30 * distanceRatio));
|
||||
container.scrollTop -= scrollAmount;
|
||||
} else if (
|
||||
mouseY > rect.bottom - thresholdY &&
|
||||
container.scrollTop < container.scrollHeight - container.clientHeight
|
||||
) {
|
||||
// 计算距离边界的比例,越近比例越大
|
||||
const distanceRatio = 1 - (rect.bottom - mouseY) / thresholdY;
|
||||
// 根据比例计算滚动距离,最小5,最大30
|
||||
const scrollAmount = Math.max(5, Math.round(30 * distanceRatio));
|
||||
container.scrollTop += scrollAmount;
|
||||
}
|
||||
}
|
||||
}
|
||||
382
packages/amis-editor-core/src/dnd/default.ts
Normal file
@@ -0,0 +1,382 @@
|
||||
/**
|
||||
* 默认的拖拽模式实现。
|
||||
*/
|
||||
import {animation} from 'amis';
|
||||
import findIndex from 'lodash/findIndex';
|
||||
import {EditorDNDManager} from './index';
|
||||
import {renderThumbToGhost} from '../component/factory';
|
||||
import {EditorNodeType} from '../store/node';
|
||||
import {translateSchema} from '../util';
|
||||
import {DNDModeInterface} from './interface';
|
||||
import {AutoScroll, getScrollableParent} from './autoScroll';
|
||||
function getRelativeParent(element: HTMLElement): HTMLElement | null {
|
||||
let parent = element;
|
||||
const win = parent.ownerDocument.defaultView || window;
|
||||
|
||||
while (parent) {
|
||||
const style = win.getComputedStyle(parent);
|
||||
if (style.position === 'relative') {
|
||||
return parent;
|
||||
}
|
||||
parent = parent.parentElement as HTMLElement;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
export class DefaultDNDMode implements DNDModeInterface {
|
||||
readonly dndContainer: HTMLElement; // 记录当前拖拽区域
|
||||
readonly relativeContainer: HTMLElement;
|
||||
dropOn?: string;
|
||||
dropPosition?: 'top' | 'bottom' | 'left' | 'right' | 'center' | 'middle';
|
||||
|
||||
autoScroll?: AutoScroll;
|
||||
|
||||
constructor(readonly dnd: EditorDNDManager, readonly region: EditorNodeType) {
|
||||
// 初始化时,默认将元素所在区域设置为当前拖拽区域
|
||||
this.dndContainer = this.dnd.store
|
||||
.getDoc()
|
||||
.querySelector(
|
||||
`[data-region="${region.region}"][data-region-host="${region.id}"]`
|
||||
) as HTMLElement;
|
||||
|
||||
// 获取相对定位的父级元素
|
||||
this.relativeContainer =
|
||||
getRelativeParent(this.dndContainer) || this.dndContainer;
|
||||
|
||||
const scrollableParent = getScrollableParent(
|
||||
this.dndContainer,
|
||||
this.dnd.store.getIframe()
|
||||
);
|
||||
if (scrollableParent) {
|
||||
this.autoScroll = new AutoScroll({
|
||||
container: scrollableParent
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
layoutInfo: LayoutInfo | null = null;
|
||||
|
||||
/**
|
||||
* 记录上次交换时的鼠标位置。
|
||||
*/
|
||||
exchangeX: number = 0;
|
||||
exchangeY: number = 0;
|
||||
|
||||
/**
|
||||
* 首次拖入,把 ghost 插入进来。让用户有个直观感受。
|
||||
* @param e
|
||||
* @param ghost
|
||||
*/
|
||||
enter(e: DragEvent, ghost: HTMLElement) {
|
||||
ghost.innerHTML = '';
|
||||
ghost.classList.add('use-indicator');
|
||||
|
||||
const layoutInfo = new LayoutDetector(this.dndContainer).detect();
|
||||
this.layoutInfo = layoutInfo;
|
||||
|
||||
ghost.style.cssText =
|
||||
'width: var(--ae-DragGhost-size); height: var(--ae-DragGhost-size);';
|
||||
this.dndContainer.appendChild(ghost);
|
||||
this.over(e, ghost);
|
||||
}
|
||||
|
||||
/**
|
||||
* 拖出去了,就移除 ghost
|
||||
* @param e
|
||||
* @param ghost
|
||||
*/
|
||||
leave(e: DragEvent, ghost: HTMLElement) {
|
||||
this.layoutInfo = null;
|
||||
ghost.style.cssText = '';
|
||||
ghost.classList.remove('use-indicator');
|
||||
this.dndContainer.removeChild(ghost);
|
||||
}
|
||||
|
||||
over(e: DragEvent, ghost: HTMLElement) {
|
||||
const target = this.getTarget(e);
|
||||
const wrapper = this.dndContainer;
|
||||
|
||||
this.autoScroll?.checkScroll(e);
|
||||
|
||||
if (target) {
|
||||
const dropPosition = this.detectDropPosition(e, target);
|
||||
this.updateIndicator(
|
||||
ghost,
|
||||
|
||||
target,
|
||||
this.reductionPosition(dropPosition, this.layoutInfo?.isHorizontal)
|
||||
);
|
||||
} else {
|
||||
// 拖入了某个区域上
|
||||
const children = this.getChildren(wrapper);
|
||||
|
||||
if (!children.length && !this.dropOn) {
|
||||
const placeholder = wrapper.querySelector(
|
||||
'.ae-Region-placeholder'
|
||||
) as HTMLElement;
|
||||
|
||||
this.updateIndicator(
|
||||
ghost,
|
||||
placeholder?.offsetWidth ? placeholder : wrapper,
|
||||
this.layoutInfo?.isHorizontal ? 'center' : 'middle'
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
const lastChild = children[children.length - 1];
|
||||
const firstChild = children[0];
|
||||
|
||||
const dropPositionOfLast = this.detectDropPosition(e, lastChild);
|
||||
|
||||
if (dropPositionOfLast === 'right' || dropPositionOfLast === 'bottom') {
|
||||
this.updateIndicator(
|
||||
ghost,
|
||||
|
||||
lastChild,
|
||||
this.reductionPosition('right', this.layoutInfo?.isHorizontal)
|
||||
);
|
||||
} else {
|
||||
this.updateIndicator(
|
||||
ghost,
|
||||
|
||||
firstChild,
|
||||
this.reductionPosition('left', this.layoutInfo?.isHorizontal)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回个相对位置,如果没有数据会插入到结尾。
|
||||
*/
|
||||
getDropBeforeId() {
|
||||
if (
|
||||
!this.dropOn ||
|
||||
this.dropPosition === 'center' ||
|
||||
this.dropPosition === 'middle'
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (this.dropPosition === 'top' || this.dropPosition === 'left') {
|
||||
return this.dropOn;
|
||||
}
|
||||
|
||||
const children = this.getChildren(this.dndContainer);
|
||||
const idx = children.findIndex(
|
||||
item => item.getAttribute('data-editor-id') === this.dropOn
|
||||
);
|
||||
if (idx !== -1 && children[idx + 1]) {
|
||||
return children[idx + 1].getAttribute('data-editor-id')!;
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当时拖动到了哪个节点上面。
|
||||
*/
|
||||
getTarget(e: DragEvent) {
|
||||
let target = (e.target as HTMLElement).closest(
|
||||
'[data-editor-id]:not(.ae-is-draging)'
|
||||
) as HTMLElement;
|
||||
|
||||
while (target) {
|
||||
const region = target.parentElement?.closest('[data-region]');
|
||||
|
||||
if (region === this.dndContainer) {
|
||||
const renderer = target.getAttribute('data-renderer');
|
||||
if (renderer === 'grid') {
|
||||
// grid 组件中的分栏的子栏也可以拖入 选中分栏组件同级组件拖动时有问题 兼容一下
|
||||
return target.parentElement;
|
||||
} else {
|
||||
return target;
|
||||
}
|
||||
}
|
||||
|
||||
target =
|
||||
(target.parentElement?.closest(
|
||||
'[data-editor-id]:not(.ae-is-draging)'
|
||||
) as HTMLElement) || null;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
// 获取直接孩子,剔除掉间接孩子
|
||||
getChildren(region: HTMLElement): Array<HTMLElement> {
|
||||
const indirectChildren = Array.from(
|
||||
region.querySelectorAll(':scope [data-editor-id] [data-editor-id]')
|
||||
);
|
||||
return Array.from(
|
||||
region.querySelectorAll('[data-editor-id]:not(.ae-is-draging)')
|
||||
).filter(item => !indirectChildren.includes(item)) as any;
|
||||
}
|
||||
|
||||
detectDropPosition(
|
||||
event: DragEvent,
|
||||
dropTarget: HTMLElement
|
||||
): 'top' | 'bottom' | 'left' | 'right' {
|
||||
const rect = dropTarget.getBoundingClientRect();
|
||||
const x = event.clientX - rect.left;
|
||||
const y = event.clientY - rect.top;
|
||||
|
||||
// 计算相对位置(百分比)
|
||||
const relativeX = x / rect.width;
|
||||
const relativeY = y / rect.height;
|
||||
|
||||
// 判断位置(上、下、左、右)
|
||||
if (relativeY < 0.25) return 'top';
|
||||
if (relativeY > 0.75) return 'bottom';
|
||||
if (relativeX < 0.5) return 'left';
|
||||
return 'right';
|
||||
}
|
||||
|
||||
reductionPosition(
|
||||
position: 'top' | 'bottom' | 'left' | 'right',
|
||||
isHorizontal?: boolean
|
||||
) {
|
||||
const left = position === 'left' || position === 'top';
|
||||
return isHorizontal ? (left ? 'left' : 'right') : left ? 'top' : 'bottom';
|
||||
}
|
||||
|
||||
updateIndicator(
|
||||
ghost: HTMLElement,
|
||||
target: HTMLElement,
|
||||
dropPosition: 'top' | 'bottom' | 'left' | 'right' | 'center' | 'middle'
|
||||
) {
|
||||
// const containerRect = container.getBoundingClientRect();
|
||||
const targetRect = target.getBoundingClientRect();
|
||||
const frameRect = this.relativeContainer.getBoundingClientRect();
|
||||
|
||||
ghost.style.cssText += `width: ${
|
||||
dropPosition === 'left' ||
|
||||
dropPosition === 'right' ||
|
||||
dropPosition === 'center'
|
||||
? 'var(--ae-DragGhost-size);'
|
||||
: targetRect.width
|
||||
}px; height: ${
|
||||
dropPosition === 'top' ||
|
||||
dropPosition === 'bottom' ||
|
||||
dropPosition === 'middle'
|
||||
? 'var(--ae-DragGhost-size);'
|
||||
: targetRect.height
|
||||
}px; left: ${
|
||||
dropPosition === 'center'
|
||||
? targetRect.left + targetRect.width / 2 - frameRect.left
|
||||
: dropPosition === 'right'
|
||||
? targetRect.right - frameRect.left
|
||||
: targetRect.left - frameRect.left
|
||||
}px; top: ${
|
||||
dropPosition === 'middle'
|
||||
? targetRect.top + targetRect.height / 2 - frameRect.top
|
||||
: dropPosition === 'bottom'
|
||||
? targetRect.bottom - frameRect.top
|
||||
: targetRect.top - frameRect.top
|
||||
}px; `;
|
||||
|
||||
this.dropOn = target.getAttribute('data-editor-id')!;
|
||||
this.dropPosition = dropPosition;
|
||||
}
|
||||
|
||||
/**
|
||||
* 销毁
|
||||
*/
|
||||
dispose() {
|
||||
delete this.autoScroll;
|
||||
delete this.dropOn;
|
||||
delete this.dropPosition;
|
||||
}
|
||||
}
|
||||
|
||||
interface LayoutInfo {
|
||||
type: 'flex' | 'grid' | 'block';
|
||||
isHorizontal?: boolean;
|
||||
isWrapped?: boolean;
|
||||
hasColumns?: boolean;
|
||||
}
|
||||
|
||||
class LayoutDetector {
|
||||
private container: HTMLElement;
|
||||
|
||||
constructor(container: HTMLElement) {
|
||||
this.container = container;
|
||||
}
|
||||
|
||||
detectByStyle(): LayoutInfo {
|
||||
const win = this.container.ownerDocument.defaultView || window;
|
||||
const style = win.getComputedStyle(this.container);
|
||||
const display = style.display;
|
||||
|
||||
switch (display) {
|
||||
case 'flex':
|
||||
case 'inline-flex':
|
||||
return this.detectFlexAlignment(style);
|
||||
case 'grid':
|
||||
case 'inline-grid':
|
||||
return this.detectGridAlignment(style);
|
||||
default:
|
||||
return this.detectBlockAlignment();
|
||||
}
|
||||
}
|
||||
|
||||
detectFlexAlignment(style: CSSStyleDeclaration): LayoutInfo {
|
||||
const direction = style.flexDirection;
|
||||
const wrap = style.flexWrap;
|
||||
|
||||
return {
|
||||
type: 'flex',
|
||||
isHorizontal: direction.includes('row'),
|
||||
isWrapped: wrap !== 'nowrap'
|
||||
};
|
||||
}
|
||||
|
||||
detectGridAlignment(style: CSSStyleDeclaration): LayoutInfo {
|
||||
const autoFlow = style.gridAutoFlow;
|
||||
const templateColumns = style.gridTemplateColumns;
|
||||
|
||||
return {
|
||||
type: 'grid',
|
||||
isHorizontal: autoFlow.includes('column'),
|
||||
hasColumns: templateColumns !== 'none'
|
||||
};
|
||||
}
|
||||
|
||||
detectBlockAlignment(): LayoutInfo {
|
||||
const children = Array.from(this.container.children).filter(item =>
|
||||
item.matches('[data-editor-id]')
|
||||
);
|
||||
if (!children.length) {
|
||||
return {type: 'block', isHorizontal: false};
|
||||
} else if (children.length === 1) {
|
||||
const win = this.container.ownerDocument.defaultView || window;
|
||||
const style = win.getComputedStyle(this.container);
|
||||
const rect = this.container.getBoundingClientRect();
|
||||
const childRect = children[0].getBoundingClientRect();
|
||||
const paddingWidth =
|
||||
parseFloat(style.paddingLeft) + parseFloat(style.paddingRight);
|
||||
const paddingHeight =
|
||||
parseFloat(style.paddingTop) + parseFloat(style.paddingBottom);
|
||||
|
||||
return {
|
||||
type: 'block',
|
||||
isHorizontal:
|
||||
childRect.height / (rect.height - paddingHeight) >
|
||||
childRect.width / (rect.width - paddingWidth)
|
||||
};
|
||||
}
|
||||
|
||||
const rect1 = children[0].getBoundingClientRect();
|
||||
const rect2 = children[1].getBoundingClientRect();
|
||||
|
||||
return {
|
||||
type: 'block',
|
||||
isHorizontal:
|
||||
Math.abs(rect2.left - rect1.left) > Math.abs(rect2.top - rect1.top)
|
||||
};
|
||||
}
|
||||
|
||||
detect(): LayoutInfo {
|
||||
return this.detectByStyle();
|
||||
}
|
||||
}
|
||||
299
packages/amis-editor-core/src/dnd/flex.ts
Normal file
@@ -0,0 +1,299 @@
|
||||
import {isMobile} from 'amis-core';
|
||||
/**
|
||||
* 支持上下左右拖拽模式
|
||||
*/
|
||||
import findIndex from 'lodash/findIndex';
|
||||
import {EditorDNDManager} from '.';
|
||||
import {renderThumbToGhost} from '../component/factory';
|
||||
import {EditorNodeType} from '../store/node';
|
||||
import {translateSchema} from '../util';
|
||||
import {DNDModeInterface} from './interface';
|
||||
import findLastIndex from 'lodash/findLastIndex';
|
||||
import find from 'lodash/find';
|
||||
import {AutoScroll, getScrollableParent} from './autoScroll';
|
||||
|
||||
const className = 'PushHighlight';
|
||||
|
||||
export class FlexDNDMode implements DNDModeInterface {
|
||||
readonly dndContainer: HTMLElement; // 记录当前拖拽区域
|
||||
dropBeforeId?: string;
|
||||
position?: 'top' | 'bottom' | 'left' | 'right';
|
||||
maxRolLength = 4;
|
||||
dragNode?: any;
|
||||
dragId: string;
|
||||
store: any;
|
||||
autoScroll?: AutoScroll;
|
||||
constructor(
|
||||
readonly dnd: EditorDNDManager,
|
||||
readonly region: EditorNodeType,
|
||||
config: any
|
||||
) {
|
||||
// 初始化时,默认将元素所在区域设置为当前拖拽区域
|
||||
this.dndContainer = this.dnd.store
|
||||
.getDoc()
|
||||
.querySelector(
|
||||
`[data-region="${region.region}"][data-region-host="${region.id}"]`
|
||||
) as HTMLElement;
|
||||
this.maxRolLength = config.regionNode.maxRolLength || 4;
|
||||
const scrollableParent = getScrollableParent(
|
||||
this.dndContainer,
|
||||
this.dnd.store.getIframe()
|
||||
);
|
||||
if (scrollableParent) {
|
||||
this.autoScroll = new AutoScroll({
|
||||
container: scrollableParent
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 首次拖入,把 ghost 插入进来。让用户有个直观感受。
|
||||
* @param e
|
||||
* @param ghost
|
||||
*/
|
||||
enter(e: DragEvent, ghost: HTMLElement) {
|
||||
const dragEl = this.dnd.dragElement;
|
||||
const list = Array.isArray(this.region.schema) ? this.region.schema : [];
|
||||
const manager = this.dnd.manager;
|
||||
this.store = manager.store;
|
||||
// 如果区域里面没有元素,ghost就渲染为真实的表单元素
|
||||
if (list.length === 0) {
|
||||
if (dragEl && dragEl.closest('[data-region]') === this.dndContainer) {
|
||||
const child = this.getChild(this.dndContainer, dragEl);
|
||||
this.dndContainer.insertBefore(ghost, child);
|
||||
let innerHTML = dragEl.outerHTML
|
||||
.replace('ae-is-draging', '')
|
||||
.replace(/\bdata\-editor\-id=(?:'.+?'|".+?")/g, '');
|
||||
ghost.innerHTML = innerHTML;
|
||||
} else {
|
||||
renderThumbToGhost(
|
||||
ghost,
|
||||
this.region,
|
||||
translateSchema(this.store.dragSchema),
|
||||
manager
|
||||
);
|
||||
this.dndContainer.appendChild(ghost);
|
||||
}
|
||||
} else {
|
||||
ghost.innerHTML = '';
|
||||
// 直接插入 ghost,over的时候再去调整样式
|
||||
this.dndContainer.appendChild(ghost);
|
||||
}
|
||||
this.dragId = this.store.dragId;
|
||||
this.dragNode =
|
||||
find(list, (item: any) => item.$$id === this.dragId) ||
|
||||
this.store.dragSchema;
|
||||
}
|
||||
|
||||
/**
|
||||
* 拖出去了,就移除 ghost
|
||||
* @param e
|
||||
* @param ghost
|
||||
*/
|
||||
leave(e: DragEvent, ghost: HTMLElement) {
|
||||
this.dndContainer.removeChild(ghost);
|
||||
this.clearGhostStyle(ghost);
|
||||
}
|
||||
|
||||
over(e: DragEvent, ghost: HTMLElement) {
|
||||
this.autoScroll?.checkScroll(e);
|
||||
|
||||
const {isMobile} = this.store;
|
||||
const colTarget = (e.target as HTMLElement).closest('[role="flex-col"]');
|
||||
const wrapper = this.dndContainer;
|
||||
const elemSchema = this.region.schema;
|
||||
const {x: wx, y: wy} = wrapper.getBoundingClientRect();
|
||||
const list: Array<any> = Array.isArray(elemSchema) ? elemSchema : [];
|
||||
this.clearGhostStyle(ghost);
|
||||
|
||||
if (colTarget && list.length) {
|
||||
const {width, height, x, y} = colTarget.getBoundingClientRect();
|
||||
const cx = e.clientX;
|
||||
const cy = e.clientY;
|
||||
const w = width / 8;
|
||||
const h = height / 2;
|
||||
|
||||
const target = this.getTarget(colTarget);
|
||||
const targetId = target.getAttribute('data-editor-id')!;
|
||||
const targetIndex = findIndex(
|
||||
list,
|
||||
(item: any) => item.$$id === targetId
|
||||
);
|
||||
const targetRow = list[targetIndex].row;
|
||||
const targetRowLen = list.filter(
|
||||
(item: any) => item.row === targetRow
|
||||
).length;
|
||||
// 是否可以插入到左右
|
||||
const canRL =
|
||||
this.dragId !== targetId && // 拖拽和目标不能是同一个元素,才能插入到左右
|
||||
this.dragNode?.$$dragMode !== 'hv' &&
|
||||
list[targetIndex]?.$$dragMode !== 'hv' && // 如果拖拽元素和目标元素的拖拽模式不能是垂直,才能插入到左右
|
||||
(targetRowLen < this.maxRolLength ||
|
||||
this.dragNode?.row === targetRow) && // 如果当前行的元素个数小于最大行长度,或者拖拽的元素就在当前行,才能插入到当前行
|
||||
!isMobile; // 移动端不支持左右拖拽
|
||||
|
||||
if (cx < x + w && canRL) {
|
||||
ghost.classList.add(`ae-${className}-left`);
|
||||
ghost.style.left = x - wx + 'px';
|
||||
ghost.style.top = y - wy + 'px';
|
||||
ghost.style.height = height + 'px';
|
||||
this.dropBeforeId = targetId;
|
||||
this.position = 'left';
|
||||
} else if (cx > x + 7 * w && canRL) {
|
||||
ghost.classList.add(`ae-${className}-right`);
|
||||
ghost.style.left = x - wx + width + 'px';
|
||||
ghost.style.top = y - wy + 'px';
|
||||
ghost.style.height = height + 'px';
|
||||
this.dropBeforeId =
|
||||
list[
|
||||
list[targetIndex + 1]?.$$id === this.dragId
|
||||
? targetIndex + 2
|
||||
: targetIndex + 1
|
||||
]?.$$id;
|
||||
this.position = 'right';
|
||||
} else if (cy < y + h) {
|
||||
// 移动端,独占一行的元素不能插入到一行的中间
|
||||
if (
|
||||
this.store.isMobile &&
|
||||
(this.dragNode?.$$dragMode !== 'hv' ||
|
||||
list[targetIndex]?.$$dragMode !== 'hv') &&
|
||||
list[targetIndex].row === list[targetIndex - 1]?.row
|
||||
) {
|
||||
delete this.position;
|
||||
delete this.dropBeforeId;
|
||||
return;
|
||||
}
|
||||
|
||||
ghost.classList.add(`ae-${className}-top`);
|
||||
ghost.style.width = '100%';
|
||||
ghost.style.top = y - wy + 'px';
|
||||
|
||||
if (this.store.isMobile) {
|
||||
this.dropBeforeId = targetId;
|
||||
} else {
|
||||
const beforeIndex = findIndex(
|
||||
list,
|
||||
(item: any) => item.row === targetRow
|
||||
);
|
||||
const index =
|
||||
list[beforeIndex]?.$$id === this.dragId
|
||||
? beforeIndex + 1
|
||||
: beforeIndex;
|
||||
this.dropBeforeId = list[index]?.$$id;
|
||||
}
|
||||
this.position = 'top';
|
||||
} else {
|
||||
// 移动端,独占一行的元素不能插入到一行的中间
|
||||
if (
|
||||
this.store.isMobile &&
|
||||
(this.dragNode?.$$dragMode !== 'hv' ||
|
||||
list[targetIndex]?.$$dragMode !== 'hv') &&
|
||||
list[targetIndex].row === list[targetIndex + 1]?.row
|
||||
) {
|
||||
delete this.position;
|
||||
delete this.dropBeforeId;
|
||||
return;
|
||||
}
|
||||
ghost.classList.add(`ae-${className}-bottom`);
|
||||
ghost.style.width = '100%';
|
||||
ghost.style.top = y - wy + height + 'px';
|
||||
if (this.store.isMobile) {
|
||||
this.dropBeforeId = list[targetIndex + 1]?.$$id;
|
||||
} else {
|
||||
const lastIndex = findLastIndex(
|
||||
list,
|
||||
(item: any) => item.row === targetRow
|
||||
);
|
||||
const index =
|
||||
list[lastIndex + 1]?.$$id === this.dragId
|
||||
? lastIndex + 2
|
||||
: lastIndex + 1;
|
||||
this.dropBeforeId = list[index]?.$$id;
|
||||
}
|
||||
this.position = 'bottom';
|
||||
}
|
||||
} else {
|
||||
this.dropBeforeId = undefined;
|
||||
if (list.length) {
|
||||
const rows = wrapper.querySelectorAll('[role="flex-row"]');
|
||||
const lastRow = rows[rows.length - 1];
|
||||
const {y, height} = lastRow.getBoundingClientRect();
|
||||
ghost.classList.add(`ae-${className}-bottom`);
|
||||
ghost.style.width = '100%';
|
||||
ghost.style.top = y - wy + height + 'px';
|
||||
}
|
||||
this.position = 'bottom';
|
||||
}
|
||||
}
|
||||
|
||||
clearGhostStyle(ghost: HTMLElement) {
|
||||
// 清除ghost的样式
|
||||
ghost.style.left = '';
|
||||
ghost.style.top = '';
|
||||
ghost.style.right = '';
|
||||
ghost.style.bottom = '';
|
||||
ghost.style.width = '';
|
||||
ghost.style.height = '';
|
||||
ghost.classList.remove(`ae-${className}-left`);
|
||||
ghost.classList.remove(`ae-${className}-right`);
|
||||
ghost.classList.remove(`ae-${className}-top`);
|
||||
ghost.classList.remove(`ae-${className}-bottom`);
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回个相对位置,如果没有数据会插入到结尾。
|
||||
*/
|
||||
getDropBeforeId() {
|
||||
return this.dropBeforeId;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当时拖动到了哪个节点上面。
|
||||
*/
|
||||
getTarget(col: Element | null) {
|
||||
let target = col?.querySelector('[data-editor-id]') as HTMLElement;
|
||||
return target;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取区域的直接孩子,因为有时候会在孩子的孩子里面。
|
||||
* 但是插入 ghost 的相对位置,insertBefore 只能是当前孩子。
|
||||
* @param dom
|
||||
* @param descend
|
||||
*/
|
||||
getChild(dom: HTMLElement, descend: HTMLElement) {
|
||||
let child = descend;
|
||||
|
||||
while (child) {
|
||||
if (child.parentElement === dom) {
|
||||
break;
|
||||
}
|
||||
|
||||
child = child.parentElement!;
|
||||
}
|
||||
|
||||
return child;
|
||||
}
|
||||
|
||||
/**
|
||||
* 销毁
|
||||
*/
|
||||
dispose() {
|
||||
delete this.autoScroll;
|
||||
delete this.dropBeforeId;
|
||||
delete this.position;
|
||||
}
|
||||
|
||||
getDropPosition() {
|
||||
return this.position;
|
||||
}
|
||||
|
||||
// 是否中断 drop 事件
|
||||
interruptionDrop() {
|
||||
// 如果没有 dropBeforeId 和 position,说明没有拖拽到任何元素上,中断 drop 事件
|
||||
if (!this.dropBeforeId && !this.position) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
702
packages/amis-editor-core/src/dnd/index.ts
Normal file
@@ -0,0 +1,702 @@
|
||||
/**
|
||||
* @file 拖拽相关逻辑
|
||||
*/
|
||||
import find from 'lodash/find';
|
||||
import {isAlive} from 'mobx-state-tree';
|
||||
import {toast} from 'amis';
|
||||
import debounce from 'lodash/debounce';
|
||||
import {EditorManager} from '../manager';
|
||||
import {DragEventContext, SubRendererInfo} from '../plugin';
|
||||
import {EditorStoreType} from '../store/editor';
|
||||
import {EditorNodeType} from '../store/node';
|
||||
import {
|
||||
JSONGetById,
|
||||
autobind,
|
||||
reactionWithOldValue,
|
||||
unitFormula
|
||||
} from '../util';
|
||||
import {DefaultDNDMode} from './default';
|
||||
import {DNDModeInterface} from './interface';
|
||||
import {PositionHDNDMode} from './position-h';
|
||||
import {FlexDNDMode} from './flex';
|
||||
|
||||
const toastWarning = debounce(msg => {
|
||||
toast.warning(msg);
|
||||
}, 500);
|
||||
|
||||
export class EditorDNDManager {
|
||||
toDispose: Array<() => void> = [];
|
||||
|
||||
/**
|
||||
* 这个会随着 region 的切换,每次动态创建。
|
||||
*/
|
||||
dndMode?: DNDModeInterface;
|
||||
|
||||
/**
|
||||
* 拖拽对象 dom。
|
||||
*/
|
||||
readonly dragGhost: HTMLElement;
|
||||
|
||||
/**
|
||||
* 统计dragEnter 的次数,因为这个方法会调用很多次,
|
||||
* 有时候只想第一次进来的时候处理逻辑
|
||||
*/
|
||||
dragEnterCount = 0;
|
||||
|
||||
/**
|
||||
* 当前被拖动的渲染器元素。注意这里不一定是 e.target。
|
||||
*/
|
||||
dragElement?: HTMLElement;
|
||||
|
||||
/**
|
||||
* 拖拽跟随元素
|
||||
*/
|
||||
dragImage?: HTMLElement;
|
||||
|
||||
/**
|
||||
* 是否锁定自动切换
|
||||
*/
|
||||
lockAutoSwitch = false;
|
||||
|
||||
/**
|
||||
* 记录上次鼠标位置信息,协助拖拽计算的。
|
||||
*/
|
||||
lastX: number = 0;
|
||||
lastY: number = 0;
|
||||
curDragId: string;
|
||||
startX: number = 0;
|
||||
startY: number = 0;
|
||||
|
||||
constructor(
|
||||
readonly manager: EditorManager,
|
||||
readonly store: EditorStoreType
|
||||
) {
|
||||
this.toDispose.push(
|
||||
reactionWithOldValue(
|
||||
() => (store.dragType === 'schema' ? store.dragId : ''),
|
||||
this.updateDragElements
|
||||
),
|
||||
|
||||
/**
|
||||
* 自动给拖入的区域添加 is-dragenter 之类的 css。
|
||||
*/
|
||||
reactionWithOldValue(
|
||||
() => ({id: store.dropId, region: store.dropRegion}),
|
||||
this.updateDropRegion
|
||||
)
|
||||
|
||||
// /**
|
||||
// * 自动给即将激活的区域添加高亮。
|
||||
// */
|
||||
// reactionWithOldValue(
|
||||
// () => ({id: store.planDropId, region: store.planDropRegion}),
|
||||
// this.updatePlanDropRegion
|
||||
// )
|
||||
);
|
||||
|
||||
this.dragGhost = document.createElement('div');
|
||||
this.dragGhost.classList.add('ae-DragGhost');
|
||||
this.dragGhost.classList.add('is-ghost');
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建拖拽跟随元素,如果用默认太大了,而且有时候不统一。
|
||||
* @param id
|
||||
* @param node
|
||||
*/
|
||||
createDragImage(id: string, node?: EditorNodeType) {
|
||||
const dragImage = document.createElement('div');
|
||||
dragImage.classList.add('ae-DragImage');
|
||||
// bca-disable-next-line
|
||||
dragImage.innerHTML = `<span>${node?.label || id}</span>`;
|
||||
document.body.appendChild(dragImage);
|
||||
// dragImage.style.cssText += `width: ${node.w}px; height: ${node.h}px;`;
|
||||
this.dragImage = dragImage;
|
||||
return dragImage;
|
||||
}
|
||||
|
||||
/**
|
||||
* 销毁拖拽跟随元素
|
||||
*/
|
||||
disposeDragImage() {
|
||||
const dragImage = this.dragImage;
|
||||
dragImage?.parentElement?.removeChild(dragImage);
|
||||
delete this.dragImage;
|
||||
}
|
||||
|
||||
/**
|
||||
* 切换到目标区域。
|
||||
* @param e
|
||||
* @param id
|
||||
* @param region
|
||||
*/
|
||||
switchToRegion(
|
||||
e: DragEvent,
|
||||
id: string,
|
||||
region: string,
|
||||
lockAutoSwitch = false
|
||||
): boolean {
|
||||
// 如果锁定了,将不会自动切换容器了
|
||||
// 拖完后释放
|
||||
this.lockAutoSwitch = lockAutoSwitch || this.lockAutoSwitch;
|
||||
const store = this.store;
|
||||
if (
|
||||
!id ||
|
||||
!region ||
|
||||
(store.dropId === id && store.dropRegion === region)
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
const node = store.getNodeById(id, region)!;
|
||||
const config = node.regionInfo!;
|
||||
|
||||
// 获取当前拖拽的元素schema数据
|
||||
const json = store.dragSchema;
|
||||
|
||||
if (
|
||||
config?.accept?.(
|
||||
json,
|
||||
node,
|
||||
store.dragId ? store.getNodeById(store.dragId) : undefined
|
||||
) === false ||
|
||||
node.host?.memberImmutable(region)
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const context: DragEventContext = {
|
||||
mode: store.dragMode as any,
|
||||
sourceType: store.dragType as any,
|
||||
sourceId: store.dragId,
|
||||
data: store.dragSchema,
|
||||
targetId: id,
|
||||
targetRegion: region
|
||||
};
|
||||
|
||||
const event = this.manager.trigger('dnd-accept', context);
|
||||
if (event.prevented) {
|
||||
return false;
|
||||
}
|
||||
|
||||
this.dndMode?.leave(e, this.dragGhost);
|
||||
this.dndMode?.dispose();
|
||||
|
||||
store.setDropId(id, region);
|
||||
this.makeDNDModeInstance(node);
|
||||
this.dndMode?.enter(e, this.dragGhost!);
|
||||
store.calculateHighlightBox([id]);
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据区域的配置,创建拖拽模式实例。
|
||||
* 比如 table 列区域的拖拽就是放根线表示拖入的位置。
|
||||
* @param region
|
||||
*/
|
||||
makeDNDModeInstance(region: EditorNodeType) {
|
||||
if (!region || !isAlive(region)) {
|
||||
return this.dndMode || null;
|
||||
}
|
||||
const mode = region.regionInfo?.dndMode;
|
||||
const regionNode = JSONGetById(this.store.schema, region.id);
|
||||
let Klass: new (
|
||||
dnd: EditorDNDManager,
|
||||
region: EditorNodeType,
|
||||
config: any
|
||||
) => DNDModeInterface = DefaultDNDMode; // todo 根据配置自动实例化不同的。
|
||||
|
||||
if (mode === 'position-h') {
|
||||
Klass = PositionHDNDMode;
|
||||
}
|
||||
if (typeof mode === 'function') {
|
||||
if (mode(regionNode) === 'flex') {
|
||||
Klass = FlexDNDMode;
|
||||
}
|
||||
}
|
||||
|
||||
this.dndMode = new Klass(this, region, {regionNode});
|
||||
return this.dndMode;
|
||||
}
|
||||
|
||||
/**
|
||||
* 拖拽入口,一个是拖拽按钮 toolbar,一个是 outline 里面的导航项
|
||||
* @param id
|
||||
* @param e
|
||||
*/
|
||||
startDrag(id: string, e: DragEvent) {
|
||||
const node = this.store.getNodeById(id)!;
|
||||
// 获取要拖拽的元素dom节点
|
||||
const dom = this.store.getDoc().querySelector(`[data-editor-id="${id}"]`);
|
||||
if (!node || !dom) {
|
||||
e.preventDefault();
|
||||
return;
|
||||
}
|
||||
e.target?.addEventListener('dragend', this.dragEnd);
|
||||
|
||||
this.lastX = e.clientX;
|
||||
this.lastY = e.clientY;
|
||||
|
||||
if (this.manager.draggableContainer(node.id)) {
|
||||
this.curDragId = id;
|
||||
this.startX = e.clientX;
|
||||
this.startY = e.clientY;
|
||||
return;
|
||||
}
|
||||
|
||||
this.dragElement = dom as HTMLElement;
|
||||
// const rect = dom.getBoundingClientRect();
|
||||
e.dataTransfer!.effectAllowed = 'move';
|
||||
e.dataTransfer!.setDragImage(
|
||||
this.createDragImage(id, node),
|
||||
0, //e.clientX - rect.left,
|
||||
0
|
||||
);
|
||||
e.dataTransfer!.setData(`dnd/ae-node-${id}`.toLowerCase(), ``);
|
||||
|
||||
setTimeout(() => {
|
||||
this.store.setDragId(id);
|
||||
const region = node.parent;
|
||||
// 直接设置成当前节点所在容器。
|
||||
this.switchToRegion(e, region.id, region.region);
|
||||
}, 4);
|
||||
}
|
||||
|
||||
/**
|
||||
* 有东西拖进来了。目前只支持内部面边里面的可用组件,后续还会支持其他类型。
|
||||
* @param e
|
||||
*/
|
||||
@autobind
|
||||
dragEnter(e: DragEvent) {
|
||||
const store = this.store;
|
||||
this.dragEnterCount++;
|
||||
const activeId = store.activeId;
|
||||
|
||||
if (activeId) {
|
||||
const curNode = store.getNodeById(activeId);
|
||||
if (!curNode) {
|
||||
toastWarning('请先选择一个元素作为插入的位置。');
|
||||
return;
|
||||
}
|
||||
/*
|
||||
if (curNode?.schema?.type === 'flex') {
|
||||
toastWarning('布局容器组件不支持拖拽插入子元素。');
|
||||
return;
|
||||
}
|
||||
*/
|
||||
} else {
|
||||
toastWarning('请先选择一个元素作为插入的位置。');
|
||||
return;
|
||||
}
|
||||
|
||||
if (store.dragId || this.dragEnterCount !== 1) {
|
||||
return;
|
||||
}
|
||||
|
||||
const types = e.dataTransfer!.types;
|
||||
if (types.length > 0) {
|
||||
for (let i = types.length - 1; i >= 0; i--) {
|
||||
if (/^dnd-dom\/(.*)$/.test(types[i])) {
|
||||
const selector = RegExp.$1;
|
||||
const dom = document.querySelector(selector);
|
||||
if (dom) {
|
||||
dom.addEventListener('dragend', this.dragEnd);
|
||||
const id = dom.getAttribute('data-dnd-id')!;
|
||||
const type = dom.getAttribute('data-dnd-type')!;
|
||||
const dataRaw = dom.getAttribute('data-dnd-data');
|
||||
const schema = dataRaw
|
||||
? JSON.parse(dataRaw)
|
||||
: {
|
||||
type: 'tpl',
|
||||
tpl: 'Unknown'
|
||||
};
|
||||
store.setDragId(id, 'copy', type, schema);
|
||||
const containerId = store.activeContainerId;
|
||||
|
||||
// 如果当前选中了某个组件,则默认让其第一个区域处于拖入状态。
|
||||
if (containerId) {
|
||||
const node = store.getNodeById(containerId);
|
||||
if (node?.childRegions.length) {
|
||||
let slotIndex = 0;
|
||||
node.childRegions.forEach((regionItem: any, index: number) => {
|
||||
// 优先使用body作为插入子元素的位置
|
||||
if (regionItem.region) {
|
||||
slotIndex = index;
|
||||
}
|
||||
});
|
||||
this.switchToRegion(
|
||||
e,
|
||||
node.id,
|
||||
node.childRegions[slotIndex].region
|
||||
);
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (this.curDragId && this.manager.draggableContainer(this.curDragId)) {
|
||||
// 特殊布局元素拖拽位置时,不需要 switchToRegion
|
||||
// 判断父级容器是否自由容器
|
||||
const curNode = store.getNodeById(activeId);
|
||||
if (curNode) {
|
||||
const parentNode = curNode.parentId
|
||||
? store.getNodeById(curNode.parentId)
|
||||
: undefined;
|
||||
if (parentNode?.schema?.isFreeContainer) {
|
||||
store.setDropId(curNode.parentId, 'body');
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 拖入后的每一次移动,除了切换区域的逻辑外都丢给 DNDMode 那个去处理了。
|
||||
* @param e
|
||||
*/
|
||||
@autobind
|
||||
dragOver(e: DragEvent) {
|
||||
const store = this.store;
|
||||
const target = e.target as HTMLElement;
|
||||
e.preventDefault();
|
||||
|
||||
const dx = e.clientX - this.lastX;
|
||||
const dy = e.clientY - this.lastY;
|
||||
const d = Math.hypot(dx, dy);
|
||||
|
||||
// 自由容器里面的拖拽分支
|
||||
if (this.curDragId && this.manager.draggableContainer(this.curDragId)) {
|
||||
// 特殊布局元素拖拽位置
|
||||
const doc = store.getDoc();
|
||||
const parentDoc = parent?.window.document;
|
||||
|
||||
// 实时调整高亮区域坐标值
|
||||
let dragHlBoxItem = doc.querySelector(
|
||||
`[data-hlbox-id='${this.curDragId}']`
|
||||
) as HTMLElement;
|
||||
|
||||
if (store.isMobile && !dragHlBoxItem && parentDoc) {
|
||||
dragHlBoxItem = parentDoc.querySelector(
|
||||
`[data-hlbox-id='${this.curDragId}']`
|
||||
) as HTMLElement;
|
||||
}
|
||||
|
||||
if (dragHlBoxItem) {
|
||||
const hlBoxInset = dragHlBoxItem.style.inset || 'auto';
|
||||
const hlBoxInsetArr = hlBoxInset.split(' ');
|
||||
const hlBInset = {
|
||||
top: dragHlBoxItem.style.top || hlBoxInsetArr[0] || 'auto',
|
||||
right: dragHlBoxItem.style.right || hlBoxInsetArr[1] || 'auto',
|
||||
bottom:
|
||||
dragHlBoxItem.style.bottom ||
|
||||
hlBoxInsetArr[2] ||
|
||||
hlBoxInsetArr[0] ||
|
||||
'auto',
|
||||
left:
|
||||
dragHlBoxItem.style.left ||
|
||||
hlBoxInsetArr[3] ||
|
||||
hlBoxInsetArr[1] ||
|
||||
'auto'
|
||||
};
|
||||
dragHlBoxItem.style.inset = `${
|
||||
hlBInset.top !== 'auto' ? unitFormula(hlBInset.top, dy) : 'auto'
|
||||
} ${
|
||||
hlBInset.right !== 'auto' ? unitFormula(hlBInset.right, -dx) : 'auto'
|
||||
} ${
|
||||
hlBInset.bottom !== 'auto'
|
||||
? unitFormula(hlBInset.bottom, -dy)
|
||||
: 'auto'
|
||||
} ${
|
||||
hlBInset.left !== 'auto' ? unitFormula(hlBInset.left, dx) : 'auto'
|
||||
}`;
|
||||
}
|
||||
|
||||
// 实时调整被拖拽元素的坐标值
|
||||
const dragContainerItem = doc.querySelector(
|
||||
`[data-editor-id='${this.curDragId}']`
|
||||
) as HTMLElement;
|
||||
|
||||
if (dragContainerItem) {
|
||||
const curInset = dragContainerItem.style.inset || 'auto';
|
||||
const insetArr = curInset.split(' ');
|
||||
const inset = {
|
||||
top: insetArr[0] || 'auto',
|
||||
right: insetArr[1] || 'auto',
|
||||
bottom: insetArr[2] || insetArr[0] || 'auto',
|
||||
left: insetArr[3] || insetArr[1] || 'auto'
|
||||
};
|
||||
dragContainerItem.style.inset = `${
|
||||
inset.top !== 'auto' ? unitFormula(inset.top, dy) : 'auto'
|
||||
} ${inset.right !== 'auto' ? unitFormula(inset.right, -dx) : 'auto'} ${
|
||||
inset.bottom !== 'auto' ? unitFormula(inset.bottom, -dy) : 'auto'
|
||||
} ${inset.left !== 'auto' ? unitFormula(inset.left, dx) : 'auto'}`;
|
||||
}
|
||||
this.lastX = e.clientX;
|
||||
this.lastY = e.clientY;
|
||||
return;
|
||||
}
|
||||
|
||||
if (!store.dropId || !target || d < 5) {
|
||||
return;
|
||||
}
|
||||
|
||||
const curRegion = target.closest(
|
||||
`[data-region][data-region-host]`
|
||||
) as HTMLElement;
|
||||
let hostId = curRegion?.getAttribute('data-region-host');
|
||||
let region = curRegion?.getAttribute('data-region');
|
||||
|
||||
let parentRegion: HTMLElement | null = null;
|
||||
const containerElem: HTMLElement | null = target.closest(
|
||||
'[data-editor-id][data-container]'
|
||||
) as HTMLElement;
|
||||
|
||||
if (
|
||||
curRegion &&
|
||||
containerElem &&
|
||||
curRegion.contains(containerElem) &&
|
||||
containerElem.querySelector(`[data-region-host="${store.dropId}"]`)
|
||||
) {
|
||||
// 如果拖拽所在的组件,包含当前dropId , 且这次是打算切到外层去,则不允许
|
||||
// 就是拖拽还发生在当前组件,不要把拖入容器切到上层去
|
||||
// 比如你打算将 form 的按钮拖到内容区,但是离开了按钮区时因为有一段时间不在任何form 的任何区域
|
||||
// 里面,这个时候一下跑到 form 所在的区域去了,这个时候就不应该切换。
|
||||
hostId = '';
|
||||
region = '';
|
||||
} else if (
|
||||
(parentRegion = curRegion?.parentElement?.closest(
|
||||
'[data-region][data-region-host]'
|
||||
) as HTMLElement) &&
|
||||
containerElem &&
|
||||
this.isInEdgeRegion(e, containerElem)
|
||||
) {
|
||||
// 如果当前区域是边缘区域,且有父级区域,则切换到父级区域
|
||||
hostId = parentRegion.getAttribute('data-region-host');
|
||||
region = parentRegion.getAttribute('data-region');
|
||||
}
|
||||
|
||||
this.lastX = e.clientX;
|
||||
this.lastY = e.clientY;
|
||||
|
||||
if (
|
||||
!this.lockAutoSwitch &&
|
||||
region &&
|
||||
hostId &&
|
||||
(store.dropId !== hostId || region !== store.dropRegion)
|
||||
) {
|
||||
this.switchToRegion(e, hostId, region);
|
||||
}
|
||||
|
||||
store.setPlanDropId('', '');
|
||||
this.dndMode?.over(e, this.dragGhost!);
|
||||
}
|
||||
|
||||
/**
|
||||
* 拖拽释放的逻辑。
|
||||
* @param e
|
||||
*/
|
||||
@autobind
|
||||
async drop(e: DragEvent) {
|
||||
const store = this.store;
|
||||
if (this.curDragId && this.manager.draggableContainer(this.curDragId)) {
|
||||
// 特殊布局元素拖拽位置后更新schema-style数据
|
||||
const dx = e.clientX - this.startX;
|
||||
const dy = e.clientY - this.startY;
|
||||
this.manager.updateContainerStyleByDrag(this.curDragId, dx, dy);
|
||||
// 重置拖拽ID,避免影响其他拖拽元素
|
||||
this.curDragId = '';
|
||||
this.store.setDropId('');
|
||||
return;
|
||||
}
|
||||
|
||||
if (!store.dropId) {
|
||||
// 没有拖拽接受容器,则直接跳过
|
||||
return;
|
||||
}
|
||||
|
||||
const beforeId = this.dndMode?.getDropBeforeId();
|
||||
const position = this.dndMode?.getDropPosition?.();
|
||||
|
||||
// 如果中断 drop 事件,则直接返回
|
||||
if (this.dndMode?.interruptionDrop?.()) {
|
||||
return;
|
||||
}
|
||||
if (store.dragMode === 'move') {
|
||||
this.manager.move(
|
||||
store.dropId,
|
||||
store.dropRegion,
|
||||
store.dragId,
|
||||
beforeId,
|
||||
{position}
|
||||
);
|
||||
} else if (store.dragMode === 'copy') {
|
||||
let schema = store.dragSchema;
|
||||
const dropId = store.dropId;
|
||||
const dropRegion = store.dropRegion;
|
||||
let subRenderer: SubRendererInfo | undefined = undefined;
|
||||
|
||||
if (store.dragType === 'subrenderer') {
|
||||
subRenderer = find(store.subRenderers, r => r.id === store.dragId);
|
||||
if (subRenderer?.scaffoldForm) {
|
||||
schema = await this.manager.scaffold(
|
||||
subRenderer.scaffoldForm,
|
||||
schema
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
this.manager.addChild(
|
||||
dropId,
|
||||
dropRegion,
|
||||
schema,
|
||||
beforeId,
|
||||
subRenderer,
|
||||
{
|
||||
id: store.dragId,
|
||||
type: store.dragType,
|
||||
data: store.dragSchema,
|
||||
position: position
|
||||
},
|
||||
false
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 拖拽离开时调用。
|
||||
* @param e
|
||||
*/
|
||||
@autobind
|
||||
dragLeave(e: DragEvent) {
|
||||
this.dragEnterCount--;
|
||||
}
|
||||
|
||||
/**
|
||||
* 拖拽结束了。
|
||||
* @param e
|
||||
*/
|
||||
@autobind
|
||||
dragEnd(e: DragEvent) {
|
||||
e.target?.removeEventListener('dragend', this.dragEnd);
|
||||
|
||||
this.dndMode?.leave(e, this.dragGhost!);
|
||||
delete this.dndMode;
|
||||
// bca-disable-next-line
|
||||
this.dragGhost.innerHTML = '';
|
||||
this.store.setDragId('');
|
||||
this.store.setDropId('');
|
||||
this.store.setPlanDropId('', '');
|
||||
this.disposeDragImage();
|
||||
this.dragEnterCount = 0;
|
||||
this.lockAutoSwitch = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 自动给正在拖拽的元素加 is-draging 之类的 css。
|
||||
*/
|
||||
@autobind
|
||||
updateDragElements(dragId: string) {
|
||||
if (dragId && this.manager.draggableContainer(dragId)) {
|
||||
return;
|
||||
}
|
||||
if (dragId) {
|
||||
[].slice
|
||||
.call(
|
||||
this.store.getDoc().querySelectorAll(`[data-editor-id="${dragId}"]`)
|
||||
)
|
||||
.forEach((elem: HTMLElement) => elem.classList.add('ae-is-draging'));
|
||||
} else {
|
||||
[].slice
|
||||
.call(this.store.getDoc().querySelectorAll(`.ae-is-draging`))
|
||||
.forEach((elem: HTMLElement) => elem.classList.remove('ae-is-draging'));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 自动给拖入的区域添加 is-dragenter 之类的 css。
|
||||
*/
|
||||
@autobind
|
||||
updateDropRegion(
|
||||
value: {id: string; region: string},
|
||||
oldValue?: {id: string; region: string}
|
||||
) {
|
||||
if (
|
||||
this.store.dragId &&
|
||||
this.manager.draggableContainer(this.store.dragId)
|
||||
) {
|
||||
return;
|
||||
}
|
||||
if (oldValue?.id && oldValue.region) {
|
||||
this.store
|
||||
.getDoc()
|
||||
.querySelector(
|
||||
`[data-region="${oldValue.region}"][data-region-host="${oldValue.id}"]`
|
||||
)
|
||||
?.classList.remove('is-dragenter');
|
||||
}
|
||||
|
||||
if (value.id && value.region) {
|
||||
this.store
|
||||
.getDoc()
|
||||
.querySelector(
|
||||
`[data-region="${value.region}"][data-region-host="${value.id}"]`
|
||||
)
|
||||
?.classList.add('is-dragenter');
|
||||
}
|
||||
}
|
||||
|
||||
isInEdgeRegion(e: DragEvent, target: HTMLElement) {
|
||||
const rect = target.getBoundingClientRect();
|
||||
const x = e.clientX - rect.left;
|
||||
const y = e.clientY - rect.top;
|
||||
const w = rect.width;
|
||||
const h = rect.height;
|
||||
const xEdge = 5;
|
||||
const yEdge = 5;
|
||||
return x < xEdge || y < yEdge || x > w - xEdge || y > h - yEdge;
|
||||
}
|
||||
|
||||
// /**
|
||||
// * 自动给即将激活的区域添加高亮。
|
||||
// */
|
||||
// @autobind
|
||||
// updatePlanDropRegion(
|
||||
// value: {id: string; region: string},
|
||||
// oldValue?: {id: string; region: string}
|
||||
// ) {
|
||||
// if (
|
||||
// this.store.dragId &&
|
||||
// this.manager.draggableContainer(this.store.dragId)
|
||||
// ) {
|
||||
// return;
|
||||
// }
|
||||
// if (oldValue?.id && oldValue.region) {
|
||||
// this.store
|
||||
// .getDoc()
|
||||
// .querySelector(
|
||||
// `[data-region="${oldValue.region}"][data-region-host="${oldValue.id}"]`
|
||||
// )
|
||||
// ?.classList.remove('is-entering');
|
||||
// }
|
||||
|
||||
// if (value.id && value.region) {
|
||||
// this.store
|
||||
// .getDoc()
|
||||
// .querySelector(
|
||||
// `[data-region="${value.region}"][data-region-host="${value.id}"]`
|
||||
// )
|
||||
// ?.classList.add('is-entering');
|
||||
// }
|
||||
// }
|
||||
|
||||
/**
|
||||
* 销毁函数。
|
||||
*/
|
||||
dispose() {
|
||||
this.disposeDragImage();
|
||||
this.toDispose.forEach(fn => fn());
|
||||
this.toDispose = [];
|
||||
}
|
||||
}
|
||||
29
packages/amis-editor-core/src/dnd/interface.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
import {
|
||||
DeleteEventContext,
|
||||
InsertEventContext,
|
||||
MoveEventContext
|
||||
} from '../plugin';
|
||||
import {EditorNodeType} from '../store/node';
|
||||
import {EditorDNDManager} from './index';
|
||||
|
||||
/**
|
||||
* 每种拖拽模式都需要实现这些约定。
|
||||
*/
|
||||
export interface DNDModeInterface {
|
||||
readonly dnd: EditorDNDManager;
|
||||
readonly region: EditorNodeType;
|
||||
|
||||
enter: (e: DragEvent, ghost: HTMLElement) => void;
|
||||
|
||||
leave: (e: DragEvent, ghost: HTMLElement) => void;
|
||||
|
||||
over: (e: DragEvent, ghost: HTMLElement) => void;
|
||||
|
||||
getDropBeforeId: () => string | undefined;
|
||||
|
||||
dispose: () => void;
|
||||
|
||||
getDropPosition?: () => 'top' | 'bottom' | 'left' | 'right' | undefined;
|
||||
|
||||
interruptionDrop?: () => boolean; // 是否中断 drop 事件
|
||||
}
|
||||
153
packages/amis-editor-core/src/dnd/position-h.ts
Normal file
@@ -0,0 +1,153 @@
|
||||
/**
|
||||
* @file 一种更简单的拖拽模式,直接在某个位置显示条线来告知用户拖拽即将拖拽到哪。
|
||||
* 比如 Table 里面的列位置的移动,如果真要移动dom,那可能要命。
|
||||
*/
|
||||
import findIndex from 'lodash/findIndex';
|
||||
import {DNDModeInterface} from './interface';
|
||||
import {EditorNodeType} from '../store/node';
|
||||
import {EditorDNDManager} from './index';
|
||||
import {AutoScroll, getScrollableParent} from './autoScroll';
|
||||
|
||||
export class PositionHDNDMode implements DNDModeInterface {
|
||||
readonly dndContainer: HTMLElement; // 记录当前拖拽区域
|
||||
dropBeforeId?: string;
|
||||
autoScroll?: AutoScroll;
|
||||
|
||||
constructor(readonly dnd: EditorDNDManager, readonly region: EditorNodeType) {
|
||||
// 初始化时,默认将元素所在区域设置为当前拖拽区域
|
||||
this.dndContainer = this.dnd.store
|
||||
.getDoc()
|
||||
.querySelector(
|
||||
`[data-region="${region.region}"][data-region-host="${region.id}"]`
|
||||
) as HTMLElement;
|
||||
const scrollableParent = getScrollableParent(
|
||||
this.dndContainer,
|
||||
this.dnd.store.getIframe()
|
||||
);
|
||||
if (scrollableParent) {
|
||||
this.autoScroll = new AutoScroll({
|
||||
container: scrollableParent
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
enter(e: DragEvent, ghost: HTMLElement) {
|
||||
ghost.innerHTML = '';
|
||||
ghost.classList.add('use-position');
|
||||
ghost.classList.add('is-horizontal');
|
||||
|
||||
const dragEl = this.dnd.dragElement;
|
||||
const regionRect = this.dndContainer.getBoundingClientRect();
|
||||
|
||||
const list: Array<any> = Array.isArray(this.region.schema)
|
||||
? this.region.schema
|
||||
: [];
|
||||
|
||||
// 如果就在移动的元素所在的区域,那么把 ghost 放到原来的位置。
|
||||
if (dragEl && dragEl.closest(`[data-region]`) === this.dndContainer) {
|
||||
const id = dragEl.getAttribute('data-editor-id')!;
|
||||
const idx = findIndex(list, (item: any) => item.$$id === id);
|
||||
if (~idx && list[idx + 1]) {
|
||||
this.dropBeforeId = list[idx + 1].$$id;
|
||||
}
|
||||
|
||||
if (dragEl.nextElementSibling) {
|
||||
const rect = dragEl.nextElementSibling.getBoundingClientRect();
|
||||
ghost.style.cssText += `top: 0; left: ${rect.x - regionRect.x}px;`;
|
||||
} else {
|
||||
ghost.style.cssText += `top: 0; left: 100%;`;
|
||||
}
|
||||
} else {
|
||||
ghost.style.cssText += `top: 0; left: -999999%;`;
|
||||
}
|
||||
this.dndContainer.appendChild(ghost);
|
||||
}
|
||||
|
||||
leave(e: DragEvent, ghost: HTMLElement) {
|
||||
ghost.style.cssText = '';
|
||||
ghost.classList.remove('use-position');
|
||||
ghost.classList.remove('is-horizontal');
|
||||
this.dndContainer.removeChild(ghost);
|
||||
}
|
||||
|
||||
over(e: DragEvent, ghost: HTMLElement) {
|
||||
this.autoScroll?.checkScroll(e);
|
||||
|
||||
let target = this.getTarget(e);
|
||||
if (!target) {
|
||||
return;
|
||||
}
|
||||
|
||||
// 如果是列特殊处理。
|
||||
if (this.dndContainer?.getAttribute('data-renderer') === 'table') {
|
||||
const col = target.parentElement?.closest(
|
||||
'th[data-editor-id], td[data-editor-id]'
|
||||
);
|
||||
if (col && this.dndContainer.contains(col)) {
|
||||
target = col as HTMLElement;
|
||||
}
|
||||
}
|
||||
|
||||
const regionRect = this.dndContainer.getBoundingClientRect();
|
||||
|
||||
const rect = target.getBoundingClientRect();
|
||||
const leading = (e.clientX - rect.left) / rect.width < 0.5;
|
||||
|
||||
if (leading) {
|
||||
ghost.style.cssText += `left: ${rect.x - regionRect.x}px;`;
|
||||
this.dropBeforeId = target.getAttribute('data-editor-id')!;
|
||||
} else if (
|
||||
target.nextElementSibling &&
|
||||
target.nextElementSibling.hasAttribute('data-editor-id')
|
||||
) {
|
||||
ghost.style.cssText += `top: 0; left: ${rect.right - regionRect.x}px;`;
|
||||
this.dropBeforeId =
|
||||
target.nextElementSibling.getAttribute('data-editor-id')!;
|
||||
} else {
|
||||
ghost.style.cssText += `top: 0; left: ${rect.right - regionRect.x}px;`;
|
||||
delete this.dropBeforeId;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当时拖动到了哪个节点上面。
|
||||
*/
|
||||
getTarget(e: DragEvent) {
|
||||
let target = (e.target as HTMLElement).closest(
|
||||
'[data-editor-id]:not(.ae-is-draging)'
|
||||
) as HTMLElement;
|
||||
|
||||
while (target) {
|
||||
const region = target.parentElement?.closest('[data-region]');
|
||||
|
||||
if (region === this.dndContainer) {
|
||||
const renderer = target.getAttribute('data-renderer');
|
||||
if (renderer === 'grid') {
|
||||
// grid 组件中的分栏的子栏也可以拖入 选中分栏组件同级组件拖动时有问题 兼容一下
|
||||
return target.parentElement;
|
||||
} else {
|
||||
return target;
|
||||
}
|
||||
}
|
||||
|
||||
target =
|
||||
(target.parentElement?.closest(
|
||||
'[data-editor-id]:not(.ae-is-draging)'
|
||||
) as HTMLElement) || null;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
getDropBeforeId() {
|
||||
return this.dropBeforeId;
|
||||
}
|
||||
|
||||
/**
|
||||
* 销毁
|
||||
*/
|
||||
dispose() {
|
||||
delete this.dropBeforeId;
|
||||
delete this.autoScroll;
|
||||
}
|
||||
}
|
||||
57
packages/amis-editor-core/src/env.ts
Normal file
@@ -0,0 +1,57 @@
|
||||
/**
|
||||
* 传给 amis 渲染器的默认 env
|
||||
*/
|
||||
import {attachmentAdpator, RenderOptions} from 'amis-core';
|
||||
import axios from 'axios';
|
||||
import {alert, confirm, toast} from 'amis';
|
||||
|
||||
export const env: RenderOptions = {
|
||||
updateLocation: () => {},
|
||||
jumpTo: () => {
|
||||
toast.info('温馨提示:预览模式下禁止跳转');
|
||||
},
|
||||
fetcher: async (api: any) => {
|
||||
let {url, method, data, config, headers} = api;
|
||||
config = config || {};
|
||||
config.url = url;
|
||||
config.withCredentials = true;
|
||||
|
||||
if (config.cancelExecutor) {
|
||||
config.cancelToken = new axios.CancelToken(config.cancelExecutor);
|
||||
}
|
||||
|
||||
config.headers = headers
|
||||
? {...config.headers, ...headers}
|
||||
: config.headers ?? {};
|
||||
config.method = method;
|
||||
config.data = data;
|
||||
|
||||
if (method === 'get' && data) {
|
||||
config.params = data;
|
||||
} else if (data && data instanceof FormData) {
|
||||
// config.headers['Content-Type'] = 'multipart/form-data';
|
||||
} else if (
|
||||
data &&
|
||||
typeof data !== 'string' &&
|
||||
!(data instanceof Blob) &&
|
||||
!(data instanceof ArrayBuffer)
|
||||
) {
|
||||
data = JSON.stringify(data);
|
||||
config.headers['Content-Type'] = 'application/json';
|
||||
}
|
||||
|
||||
let response = await axios(config);
|
||||
response = await attachmentAdpator(response, (msg: string) => msg, api);
|
||||
return response;
|
||||
},
|
||||
isCancel: (value: any) => (axios as any).isCancel(value),
|
||||
alert,
|
||||
confirm,
|
||||
notify: (type, msg) => {
|
||||
toast[type]
|
||||
? toast[type](msg, type === 'error' ? '系统错误' : '系统消息')
|
||||
: console.warn('[Notify]', type, msg);
|
||||
},
|
||||
/* 强制隐藏组件内部的报错信息,会覆盖组件内部属性 */
|
||||
forceSilenceInsideError: false
|
||||
};
|
||||
1
packages/amis-editor-core/src/icons/api.svg
Normal file
@@ -0,0 +1 @@
|
||||
<svg class="icon" transform="translate(0 -2)" viewBox="0 0 1024 1024" xmlns="http://www.w3.org/2000/svg"><path d="M534.1 207.332c39.1-39.1 90.3-58.6 141.5-58.6 39.2 0 78.4 11.4 112.1 34.3l76.1-76.1c1.6-1.5 3.6-2.3 5.7-2.3s4.1.7 5.7 2.3l42.4 42.4c3.1 3.1 3.1 8.2 0 11.4l-76.1 76.1c52.7 77.8 44.6 184.5-24.3 253.5l-101.9 101.9c-1.6 1.5-3.7 2.3-5.7 2.3-2.1 0-4.1-.7-5.7-2.3l-271.7-271.7c-3.1-3.1-3.1-8.2 0-11.3zm-225.6 225.5c1.6-1.5 3.7-2.3 5.7-2.3 1.156 0 2.282.212 3.327.67l-.031.034 276.02 276.02c1.065 2.848.46 6.2-1.816 8.476l-101.9 101.9c-39.1 39.1-90.3 58.6-141.5 58.6-39.2 0-78.4-11.4-112.1-34.3l-76.1 76.1c-1.6 1.5-3.6 2.3-5.7 2.3s-4.1-.7-5.7-2.3l-42.4-42.4c-3.1-3.1-3.1-8.2 0-11.3l76.1-76.1c-52.7-77.8-44.6-184.5 24.3-253.5zm273.7-177.4-59.4 59.4 186.8 186.8 59.4-59.4c24.9-25 38.7-58.1 38.7-93.4 0-35.3-13.8-68.5-38.7-93.4-25-25-58.1-38.7-93.4-38.7-35.3 0-68.5 13.8-93.4 38.7zm-268 268-59.4 59.4c-25 25-38.7 58.1-38.7 93.4 0 35.3 13.8 68.5 38.7 93.4 25 25 58.1 38.7 93.4 38.7 35.3 0 68.5-13.8 93.4-38.7l59.4-59.4-186.8-186.8z" fill="#1C6EFF"/><path d="M567.811 548.615a8 8 0 0 1 11.314 0l33.941 33.941a8 8 0 0 1 0 11.314l-66.468 66.468-45.255-45.255 66.468-66.468zM429.22 410.022a8 8 0 0 1 11.313 0l33.941 33.942a8 8 0 0 1 0 11.313l-66.468 66.468-45.255-45.255 66.469-66.468z" fill="#44D7B6"/></svg>
|
||||
|
After Width: | Height: | Size: 1.3 KiB |
1
packages/amis-editor-core/src/icons/arrow-to-bottom.svg
Normal file
@@ -0,0 +1 @@
|
||||
<svg viewBox="0 0 1024 1024" xmlns="http://www.w3.org/2000/svg" width="16" height="16"><path d="M810.667 853.333H213.333a42.667 42.667 0 0 0 0 85.334h597.334a42.667 42.667 0 0 0 0-85.334zm-328.96-97.706a42.667 42.667 0 0 0 14.08 8.96 40.107 40.107 0 0 0 32.426 0 42.667 42.667 0 0 0 14.08-8.96L712.96 584.96a42.667 42.667 0 0 0-60.587-60.587l-97.706 98.134V128a42.667 42.667 0 0 0-85.334 0v494.507l-97.706-98.134a42.667 42.667 0 1 0-60.587 60.587z"/></svg>
|
||||
|
After Width: | Height: | Size: 457 B |
1
packages/amis-editor-core/src/icons/arrow-to-right.svg
Normal file
@@ -0,0 +1 @@
|
||||
<svg viewBox="0 0 1024 1024" xmlns="http://www.w3.org/2000/svg"><path d="M755.627 481.707 584.96 311.04a42.667 42.667 0 1 0-60.587 60.587l98.134 97.706H128a42.667 42.667 0 0 0 0 85.334h494.507l-98.134 97.706a42.667 42.667 0 0 0 0 60.587 42.667 42.667 0 0 0 60.587 0l170.667-170.667a42.667 42.667 0 0 0 8.96-14.08 42.667 42.667 0 0 0 0-32.426 42.667 42.667 0 0 0-8.96-14.08zM896 170.667a42.667 42.667 0 0 0-42.667 42.666v597.334a42.667 42.667 0 0 0 85.334 0V213.333A42.667 42.667 0 0 0 896 170.667z"/></svg>
|
||||
|
After Width: | Height: | Size: 507 B |
1
packages/amis-editor-core/src/icons/cancel-icon.svg
Normal file
@@ -0,0 +1 @@
|
||||
<svg width="24" height="24" xmlns="http://www.w3.org/2000/svg"><g fill-rule="evenodd"><path d="M9 10.333v-3h12V4H9V1h13a2 2 0 0 1 2 2v5.333a2 2 0 0 1-1.85 1.995l-.15.005H9ZM2 1h7v3H0V3a2 2 0 0 1 2-2Z"/><path d="M2 1h1v9.333H2a2 2 0 0 1-2-2V3a2 2 0 0 1 2-2Zm0 12.667h1V23H2a2 2 0 0 1-2-2v-5.333a2 2 0 0 1 2-2ZM6 1h3v9.333H6zm0 12.667h3V23H6zm15 0h1a2 2 0 0 1 2 2V21a2 2 0 0 1-2 2h-1v-9.333Z"/><path d="M0 7.333h9v3H2a2 2 0 0 1-2-2v-1Zm9 6.334h13a2 2 0 0 1 2 2v1H9v-3Zm-7 0h7v3H0v-1a2 2 0 0 1 2-2ZM9 20h15v1a2 2 0 0 1-2 2H9v-3Zm-9 0h9v3H2a2 2 0 0 1-2-2v-1Z"/></g></svg>
|
||||
|
After Width: | Height: | Size: 568 B |
1
packages/amis-editor-core/src/icons/clear.svg
Normal file
@@ -0,0 +1 @@
|
||||
<svg class="icon" viewBox="0 0 1024 1024" xmlns="http://www.w3.org/2000/svg" width="48" height="48"><path d="M452.225 146.9 111.637 487.488a33.223 33.223 0 0 1-47.132 0c-13.073-13.073-12.9-34.059 0-47.132L405.093 99.768a33.223 33.223 0 0 1 47.132 0 33.223 33.223 0 0 1 0 47.132zm472.007 471.835L583.644 959.323a33.223 33.223 0 0 1-47.132 0c-13.073-13.073-12.901-34.06 0-47.132L877.1 571.603a33.223 33.223 0 0 1 47.132 0c13.073 13.073 12.9 34.23 0 47.132zm-473.039 2.064L348.157 723.835a33.223 33.223 0 0 1-47.132 0c-13.073-13.073-12.901-34.058 0-47.131L404.06 573.667a33.223 33.223 0 0 1 47.132 0c13.073 13.073 13.073 34.23 0 47.132zm-152.749-95.812-74.654 74.654a33.223 33.223 0 0 1-47.132 0c-13.073-13.073-12.9-34.059 0-47.132l74.655-74.654a33.223 33.223 0 0 1 47.131 0c12.902 13.073 12.902 34.231 0 47.132zM545.285 772l-74.654 74.653a33.223 33.223 0 0 1-47.132 0c-13.073-13.073-12.901-34.058 0-47.131l74.654-74.655a33.223 33.223 0 0 1 47.132 0 33.223 33.223 0 0 1 0 47.132z"/><path d="M536.34 959.323 64.505 487.66a33.223 33.223 0 0 1 0-47.132c13.073-13.073 34.06-12.9 47.132 0l471.835 471.835a33.223 33.223 0 0 1 0 47.132c-13.073 12.9-34.059 12.9-47.132-.172zM732.264 763.57 260.43 291.737a33.223 33.223 0 0 1 0-47.132c13.073-13.073 34.06-12.901 47.132 0L779.224 716.44a33.223 33.223 0 0 1 0 47.132c-12.901 13.073-34.059 12.9-46.96 0zm180.099-544.08L804.682 111.637a33.223 33.223 0 0 1 0-47.132c13.073-13.073 34.059-12.9 47.132 0l107.68 107.853a33.223 33.223 0 0 1 0 47.132c-13.072 13.073-34.23 13.073-47.131 0zM587.256 328.719 405.437 146.9a33.223 33.223 0 0 1 0-47.132c13.073-13.073 34.06-12.9 47.132 0L634.56 281.76a33.223 33.223 0 0 1 0 47.132c-13.073 12.901-34.23 12.901-47.304-.172zM877.1 618.563l-181.819-181.82a33.223 33.223 0 0 1 0-47.131c13.073-13.073 34.059-12.901 47.132 0l181.99 181.99a33.223 33.223 0 0 1 0 47.133c-13.072 12.9-34.23 12.9-47.303-.172z"/><path d="M959.323 219.49 742.24 436.572a33.223 33.223 0 0 1-47.132 0c-13.073-13.073-12.901-34.059 0-47.132L912.19 172.358a33.223 33.223 0 0 1 47.132 0c13.073 12.901 13.073 34.059 0 47.132zM851.642 111.637 634.388 328.72a33.223 33.223 0 0 1-47.132 0c-13.073-13.073-12.9-34.059 0-47.132L804.51 64.505a33.223 33.223 0 0 1 47.132 0c13.073 13.073 12.9 34.06 0 47.132z"/></svg>
|
||||
|
After Width: | Height: | Size: 2.2 KiB |
@@ -0,0 +1 @@
|
||||
<svg width="16" height="16" xmlns="http://www.w3.org/2000/svg"><path fill="#83868C" d="M8 11 4 6h8z"/></svg>
|
||||
|
After Width: | Height: | Size: 109 B |
1
packages/amis-editor-core/src/icons/copy-btn.svg
Normal file
@@ -0,0 +1 @@
|
||||
<svg width="15" height="15" xmlns="http://www.w3.org/2000/svg"><path d="M11 1.6a2.4 2.4 0 0 1 2.394 2.236L13.4 4v5a2.4 2.4 0 0 1-2.236 2.394L11 11.4l-.633.001A2.4 2.4 0 0 1 8 13.4H4A2.4 2.4 0 0 1 1.6 11V6A2.4 2.4 0 0 1 4 3.6h.632a2.392 2.392 0 0 1 2.195-1.994L6.99 1.6H11ZM8 4.4H4A1.6 1.6 0 0 0 2.4 6v5A1.6 1.6 0 0 0 4 12.6h4A1.6 1.6 0 0 0 9.6 11V6A1.6 1.6 0 0 0 8 4.4Zm-2.2 5.7c.11 0 .2.09.2.2v.4a.2.2 0 0 1-.2.2H3.717a.2.2 0 0 1-.2-.2v-.4c0-.11.09-.2.2-.2H5.8ZM11 2.4H6.99c-.743 0-1.368.51-1.542 1.2H8A2.4 2.4 0 0 1 10.4 6l-.001 4.6H11a1.6 1.6 0 0 0 1.593-1.454L12.6 9V4a1.6 1.6 0 0 0-1.454-1.593L11 2.4ZM8.3 8.1c.11 0 .2.09.2.2v.4a.2.2 0 0 1-.2.2H3.717a.2.2 0 0 1-.2-.2v-.4c0-.11.09-.2.2-.2H8.3Zm0-2c.11 0 .2.09.2.2v.4a.2.2 0 0 1-.2.2H3.717a.2.2 0 0 1-.2-.2v-.4c0-.11.09-.2.2-.2H8.3Z" fill="#FFF"/></svg>
|
||||
|
After Width: | Height: | Size: 808 B |
1
packages/amis-editor-core/src/icons/copy-icon.svg
Normal file
@@ -0,0 +1 @@
|
||||
<svg width="24" height="23" xmlns="http://www.w3.org/2000/svg"><g fill-rule="evenodd"><rect x="1.5" y="7.5" width="15" height="14" rx="2"/><path d="M6 2a2 2 0 0 1 2-2h14a2 2 0 0 1 2 2v14a2 2 0 0 1-2 2h-4v-2.6h3v-12H9V6H6V2Z" fill-rule="nonzero"/></g></svg>
|
||||
|
After Width: | Height: | Size: 257 B |
1
packages/amis-editor-core/src/icons/delete-bold.svg
Normal file
@@ -0,0 +1 @@
|
||||
<svg width="13" height="14" xmlns="http://www.w3.org/2000/svg"><path d="M12.833 2.499V3.5H11.33l.001 10h-9v-10H.832V2.499h12ZM10.331 3.5h-7v9h7v-9Zm-4.5 2.999v4h-1v-4h1Zm3 0v4h-1v-4h1ZM8.832.5v.999h-4V.5h4Z" fill="currentColor" fill-rule="evenodd"/></svg>
|
||||
|
After Width: | Height: | Size: 256 B |
1
packages/amis-editor-core/src/icons/delete-btn.svg
Normal file
@@ -0,0 +1 @@
|
||||
<svg viewBox="0 0 14 14" xmlns="http://www.w3.org/2000/svg"><path d="M9.25 1.1a.4.4 0 0 1 .392.32l.008.08v1.153h3.15c.11 0 .2.09.2.2v.4a.2.2 0 0 1-.2.2h-1.65v7.044a2.4 2.4 0 0 1-2.236 2.395l-.164.005h-3.5a2.4 2.4 0 0 1-2.394-2.235l-.006-.165-.001-7.044H1.2a.2.2 0 0 1-.2-.2v-.4c0-.11.09-.2.2-.2h3.149L4.35 1.5a.4.4 0 0 1 .32-.392l.08-.008h4.5Zm1.1 2.353H3.649l.001 7.044a1.6 1.6 0 0 0 1.454 1.594l.146.006h3.5a1.6 1.6 0 0 0 1.593-1.454l.007-.146V3.453ZM4.816 4.997l.4.006a.2.2 0 0 1 .198.202l-.065 5.1a.2.2 0 0 1-.202.198l-.4-.006a.2.2 0 0 1-.198-.202l.065-5.1a.2.2 0 0 1 .202-.198Zm2 0 .4.006a.2.2 0 0 1 .198.202l-.065 5.1a.2.2 0 0 1-.202.198l-.4-.006a.2.2 0 0 1-.198-.202l.065-5.1a.2.2 0 0 1 .202-.198Zm2 0 .4.006a.2.2 0 0 1 .198.202l-.065 5.1a.2.2 0 0 1-.202.198l-.4-.006a.2.2 0 0 1-.198-.202l.065-5.1a.2.2 0 0 1 .202-.198ZM8.85 1.9h-3.7v.753h3.7V1.9Z"/></svg>
|
||||
|
After Width: | Height: | Size: 864 B |
1
packages/amis-editor-core/src/icons/delete-easy.svg
Normal file
@@ -0,0 +1 @@
|
||||
<svg width="17" height="16" xmlns="http://www.w3.org/2000/svg"><path d="M14.333 3.499V4.5H12.83l.001 10h-9v-10H2.332V3.499h12ZM11.831 4.5h-7v9h7v-9Zm-4.5 2.999v4h-1v-4h1Zm3 0v4h-1v-4h1Zm.001-5.999v.999h-4V1.5h4Z" fill="currentColor" fill-rule="evenodd"/></svg>
|
||||
|
After Width: | Height: | Size: 261 B |
1
packages/amis-editor-core/src/icons/delete-icon.svg
Normal file
@@ -0,0 +1 @@
|
||||
<svg width="24" height="23" xmlns="http://www.w3.org/2000/svg"><g fill-rule="evenodd"><path opacity=".66" d="M11 9.5v9H8v-9zm5 0v9h-3v-9z"/><path d="M16 0a2 2 0 0 1 2 2v3h4.5a1.5 1.5 0 0 1 0 3H21v13a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V8H1.5a1.5 1.5 0 0 1 0-3H6V2a2 2 0 0 1 2-2h8Zm2 8H6v12h12V8Zm-3-5H9v2h6V3Z" fill-rule="nonzero" opacity=".6"/></g></svg>
|
||||
|
After Width: | Height: | Size: 349 B |
1
packages/amis-editor-core/src/icons/dialog.svg
Normal file
@@ -0,0 +1 @@
|
||||
<svg class="icon" viewBox="0 0 1155 1024" xmlns="http://www.w3.org/2000/svg" width="225.586" height="200"><path d="M1076.512 315.077H840.204V78.769A78.77 78.77 0 0 0 761.435 0H78.77A78.77 78.77 0 0 0 0 78.77v420.1a78.77 78.77 0 0 0 78.77 78.77h14.939l-9.295 23.368a78.77 78.77 0 0 0 73.124 107.913 76.879 76.879 0 0 0 38.23-9.977l119.309-66.166v181.169a78.77 78.77 0 0 0 78.769 78.769h347.188l218.453 121.305a76.879 76.879 0 0 0 38.256 9.977 78.77 78.77 0 0 0 73.15-107.914l-9.295-23.368h14.914a78.77 78.77 0 0 0 78.769-78.77V393.847a78.77 78.77 0 0 0-78.77-78.77zm-761.435 78.769v148.873l-157.539 87.434 52.513-131.282H78.77V78.77h682.666v236.308h-367.59a78.77 78.77 0 0 0-78.768 78.769zm761.435 420.102H945.23l52.513 131.282-236.308-131.282h-367.59V393.846h682.667v420.102zm-577.64-262.564h472.614a39.385 39.385 0 0 0 0-78.77H498.871a39.385 39.385 0 0 0 0 78.77zm0 157.538h472.614a39.385 39.385 0 0 0 0-78.769H498.871a39.385 39.385 0 0 0 0 78.77z"/></svg>
|
||||
|
After Width: | Height: | Size: 958 B |
1
packages/amis-editor-core/src/icons/display-block.svg
Normal file
@@ -0,0 +1 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 22 14"><path opacity=".15" d="M19 3v8H3V3h16m1-1H2v10h18V2z"/><path d="M19.8 1H2.2A1.2 1.2 0 0 0 1 2.2v9.6A1.2 1.2 0 0 0 2.2 13h17.6a1.2 1.2 0 0 0 1.2-1.2V2.2A1.2 1.2 0 0 0 19.8 1zm.2 11H2V2h18v10z"/><path opacity=".35" d="M3 3h16v8H3z"/></svg>
|
||||
|
After Width: | Height: | Size: 321 B |
1
packages/amis-editor-core/src/icons/display-flex.svg
Normal file
@@ -0,0 +1 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 23 14"><path d="M21 2v10H2V2h19zm1-1H1v12h21V1z" opacity=".15"/><path d="M1.2 0A1.2 1.2 0 0 0 0 1.2v11.6A1.2 1.2 0 0 0 1.2 14h20.6a1.2 1.2 0 0 0 1.2-1.2V1.2A1.2 1.2 0 0 0 21.8 0H1.2zM22 13H1V1h21v12z"/><path opacity=".4" d="M3 3h5v8H3z"/><path d="M4 4v6h3V4H4zm4-1v8H3V3h5z"/><path opacity=".4" d="M9 3h5v8H9z"/><path d="M10 4v6h3V4h-3zm4-1v8H9V3h5z"/><path opacity=".4" d="M15 3h5v8h-5z"/><path d="M16 4v6h3V4h-3zm4-1v8h-5V3h5z"/></svg>
|
||||
|
After Width: | Height: | Size: 514 B |
@@ -0,0 +1 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 28 14"><path d="M25 3v8H3V3h22m1-1H2v10h24V2z" opacity=".15"/><path opacity=".35" d="M8.418 9.8H7.66L7.25 11h1.58l-.232-.676-.18-.525zM3 11h.567l2.916-7.94h3.15L11 6.78V3H3v8zm15.12-1.002a3.302 3.302 0 0 1-.48-.586c-.173.343-.42.653-.73.888.002 0 .004-.002.005-.003l-.008.006.003-.003a3.145 3.145 0 0 1-1.092.55c-.4.11-.825.15-1.303.15h6.017a4.416 4.416 0 0 1-1.12-.215 3.51 3.51 0 0 1-1.292-.787zM14.237 3c.49 0 .853.014 1.167.06.335.05.656.16.953.314h.003l.004.003c.365.19.694.492.898.866.13.234.197.49.24.747.16-.298.343-.58.578-.82v-.002c.358-.366.8-.643 1.287-.826a4.373 4.373 0 0 1 1.557-.272 5.67 5.67 0 0 1 1.442.18l.01.002.01.003c.205.06.4.126.59.208.15.064.283.125.407.185l.562.274v3.016h-1.54l-.28-.24a7.018 7.018 0 0 0-.233-.188 2.703 2.703 0 0 0-.26-.175 1.594 1.594 0 0 0-.277-.125h-.006l-.2-.03a.772.772 0 0 0-.26.042l-.16.102a.63.63 0 0 0-.13.22 1.486 1.486 0 0 0-.075.516c0 .266.04.436.074.513a.59.59 0 0 0 .13.212l.15.09h.003a.868.868 0 0 0 .265.045l.236-.037h.008a1.04 1.04 0 0 0 .25-.113l.007-.004.01-.005.24-.16a6.41 6.41 0 0 0 .216-.17l.285-.254h1.536v3.017l-.59.266c-.15.066-.292.13-.422.184a4.19 4.19 0 0 1-.53.19 5.918 5.918 0 0 1-.636.15c-.076.012-.16.02-.24.026H25V3H14.237z"/><path d="M25.8 1H2.2A1.2 1.2 0 0 0 1 2.2v9.6A1.2 1.2 0 0 0 2.2 13h23.6a1.2 1.2 0 0 0 1.2-1.2V2.2A1.2 1.2 0 0 0 25.8 1zm.2 11H2V2h24v10zm-7.186-2.72c.258.248.567.435.927.56s.76.19 1.18.19c.29 0 .51-.014.68-.042.167-.028.34-.07.53-.123.143-.04.28-.09.41-.146l.412-.19V8.16h-.154c-.07.06-.157.134-.265.22a3.337 3.337 0 0 1-.35.237 2.11 2.11 0 0 1-.485.22 1.798 1.798 0 0 1-1.1-.01 1.403 1.403 0 0 1-.523-.3c-.15-.14-.28-.33-.38-.57a2.36 2.36 0 0 1-.152-.9c0-.347.047-.64.14-.88s.216-.433.367-.58c.157-.15.33-.26.517-.325a1.786 1.786 0 0 1 1.116-.014c.164.056.316.125.457.206a4.868 4.868 0 0 1 .644.466h.17V4.54a13.03 13.03 0 0 0-.367-.168 3.667 3.667 0 0 0-1.005-.27 4.736 4.736 0 0 0-.65-.04c-.436 0-.837.07-1.205.207s-.675.333-.924.59c-.26.262-.457.58-.59.95S18 6.6 18 7.055c0 .488.07.918.213 1.29s.343.683.6.933zm-3.264.607c.272-.076.52-.2.75-.38.195-.15.35-.338.46-.567.113-.23.17-.488.17-.778 0-.4-.11-.722-.332-.967-.22-.245-.52-.406-.896-.484v-.03a1.46 1.46 0 0 0 .623-.51 1.36 1.36 0 0 0 .22-.77c0-.25-.052-.47-.16-.665a1.15 1.15 0 0 0-.49-.465 2.065 2.065 0 0 0-.64-.215A7.67 7.67 0 0 0 14.237 4H12v6h2.515c.418 0 .763-.037 1.035-.113zM13.478 5.1h.14c.29 0 .517.002.678.006s.296.027.404.07c.12.05.206.123.253.223a.69.69 0 0 1 .072.29.86.86 0 0 1-.062.34c-.042.1-.13.18-.263.24a1.05 1.05 0 0 1-.39.08c-.166.007-.366.01-.6.01h-.233V5.1zm.108 3.8h-.108V7.405h.324c.222 0 .434 0 .638.004.203 0 .363.02.48.05.17.05.29.13.36.23s.106.25.106.44c0 .144-.03.272-.087.384s-.17.2-.32.27a1.257 1.257 0 0 1-.5.104c-.18.003-.48.004-.9.004zm-6.64-.1h2.187l.41 1.2h1.574L8.935 4.06H7.182L5 10h1.535l.412-1.2zM8.04 5.58l.727 2.13H7.313l.727-2.13z"/></svg>
|
||||
|
After Width: | Height: | Size: 2.9 KiB |
1
packages/amis-editor-core/src/icons/display-inline.svg
Normal file
@@ -0,0 +1 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 20 8"><path d="M14.814 6.25c.258.248.567.435.927.56s.76.19 1.18.19c.29 0 .51-.014.68-.042.167-.028.34-.07.53-.123.143-.04.28-.09.41-.146l.41-.19V5.13h-.153c-.07.06-.157.134-.265.22a3.337 3.337 0 0 1-.35.237 2.11 2.11 0 0 1-.484.22 1.798 1.798 0 0 1-1.1-.01 1.403 1.403 0 0 1-.523-.3 1.566 1.566 0 0 1-.38-.57c-.1-.24-.152-.54-.152-.9 0-.346.048-.638.14-.878s.217-.437.368-.58c.16-.155.33-.26.52-.33a1.786 1.786 0 0 1 1.117-.013c.165.055.317.124.458.205a4.385 4.385 0 0 1 .643.47h.17V1.52c-.107-.05-.23-.107-.37-.168a3.613 3.613 0 0 0-1.003-.27 4.736 4.736 0 0 0-.65-.04c-.436 0-.837.07-1.205.207s-.674.33-.923.59c-.26.26-.458.58-.59.95S14 3.57 14 4.02c0 .488.07.918.213 1.29s.343.683.6.933zm-3.264.608c.272-.075.52-.2.75-.377a1.5 1.5 0 0 0 .46-.56 1.72 1.72 0 0 0 .17-.77c0-.4-.11-.72-.332-.96-.22-.244-.52-.404-.896-.48v-.04a1.46 1.46 0 0 0 .623-.516c.147-.22.22-.474.22-.763 0-.25-.052-.47-.16-.66a1.152 1.152 0 0 0-.49-.46 2.06 2.06 0 0 0-.64-.212A7.67 7.67 0 0 0 10.237 1H8v5.97h2.515c.418 0 .763-.037 1.035-.112zM9.478 2.095h.14c.29 0 .517.002.678.006.162.01.296.03.404.07a.46.46 0 0 1 .253.23.69.69 0 0 1 .072.298.85.85 0 0 1-.062.34.492.492 0 0 1-.263.24 1.052 1.052 0 0 1-.39.08c-.166.006-.366.01-.6.01h-.232v-1.27zm.108 3.78h-.108V4.388h.324c.222 0 .434 0 .638.004a1.8 1.8 0 0 1 .48.056c.17.05.29.127.36.23.07.104.106.25.106.44 0 .144-.03.272-.087.383s-.17.2-.32.27-.32.1-.5.11c-.18.004-.48.004-.9.004zm-6.64-.106h2.187l.41 1.2h1.574L4.935 1.03H3.182L1 6.97h1.535l.412-1.2zM4.04 2.55l.727 2.13H3.313l.727-2.13z"/></svg>
|
||||
|
After Width: | Height: | Size: 1.6 KiB |
1
packages/amis-editor-core/src/icons/down-arrow.svg
Normal file
@@ -0,0 +1 @@
|
||||
<svg width="24" height="24" viewBox="0 0 16 16" xmlns="http://www.w3.org/2000/svg"><path stroke="currentColor" stroke-linejoin="round" d="m2.35 5.497 5.657 5.657 5.657-5.657" fill="none"/></svg>
|
||||
|
After Width: | Height: | Size: 195 B |
1
packages/amis-editor-core/src/icons/drag-btn.svg
Normal file
@@ -0,0 +1 @@
|
||||
<svg width="16" height="16" xmlns="http://www.w3.org/2000/svg"><g fill="#FFF"><path d="m10.263 4.52-.283.283a.2.2 0 0 1-.283 0L8 3.105 6.303 4.803a.2.2 0 0 1-.283 0l-.283-.283a.2.2 0 0 1 0-.283l1.98-1.98a.4.4 0 0 1 .503-.051l.063.051 1.98 1.98a.2.2 0 0 1 0 .283Zm-4.526 6.953.283-.283a.2.2 0 0 1 .283 0L8 12.888l1.697-1.698a.2.2 0 0 1 .283 0l.283.283a.2.2 0 0 1 0 .283l-1.98 1.98a.4.4 0 0 1-.503.05l-.063-.05-1.98-1.98a.2.2 0 0 1 0-.283Zm5.743-1.21-.283-.283a.2.2 0 0 1 0-.283L12.895 8l-1.698-1.697a.2.2 0 0 1 0-.283l.283-.283a.2.2 0 0 1 .283 0l1.98 1.98a.4.4 0 0 1 .051.503l-.051.063-1.98 1.98a.2.2 0 0 1-.283 0ZM4.546 5.737l.282.283a.2.2 0 0 1 0 .283L3.131 8l1.697 1.697a.2.2 0 0 1 0 .283l-.282.283a.2.2 0 0 1-.283 0l-1.98-1.98a.4.4 0 0 1-.051-.503l.05-.063 1.98-1.98a.2.2 0 0 1 .284 0Z"/><path d="M8.4 3v4.7h4.7v.8H8.4V13h-.8V8.5H3.1v-.8l4.5-.001V3h.8Z"/></g></svg>
|
||||
|
After Width: | Height: | Size: 869 B |
1
packages/amis-editor-core/src/icons/drag-icon.svg
Normal file
@@ -0,0 +1 @@
|
||||
<svg width="7" height="12" xmlns="http://www.w3.org/2000/svg"><path d="M5.167 10a1 1 0 1 1 0 2 1 1 0 0 1 0-2Zm0-5a1 1 0 1 1 0 2 1 1 0 0 1 0-2Zm0-5a1 1 0 1 1 0 2 1 1 0 0 1 0-2Zm-4 10a1 1 0 1 1 0 2 1 1 0 0 1 0-2Zm0-5a1 1 0 1 1 0 2 1 1 0 0 1 0-2Zm0-5a1 1 0 1 1 0 2 1 1 0 0 1 0-2Z" fill="#84868C" fill-rule="evenodd"/></svg>
|
||||
|
After Width: | Height: | Size: 321 B |
1
packages/amis-editor-core/src/icons/edit.svg
Normal file
@@ -0,0 +1 @@
|
||||
<svg width="24" height="24" viewBox="0 0 16 16" xmlns="http://www.w3.org/2000/svg"><path d="m11.536 3.05 1.414 1.414-7.778 7.779-1.395.019-.02-1.434zM9.414 4.464l2.122 2.122M1.5 14h13" fill="none" stroke="currentColor" stroke-linejoin="round"/></svg>
|
||||
|
After Width: | Height: | Size: 251 B |
1
packages/amis-editor-core/src/icons/event-setting.svg
Normal file
@@ -0,0 +1 @@
|
||||
<svg width="16" height="16" xmlns="http://www.w3.org/2000/svg"><g fill="none" fill-rule="evenodd"><path d="M0 0h16v16H0z"/><path d="M7.356 14H2.333c-.552 0-1-.504-1-1.125v-9.75c0-.621.448-1.125 1-1.125h11.334c.552 0 1 .504 1 1.125v4.522" stroke="#151B26" stroke-linecap="round" stroke-linejoin="round"/><path d="M1.333 3a1 1 0 0 1 1-1h11.334a1 1 0 0 1 1 1v3H1.333V3Z" stroke="#151B26"/><path d="M3.333 3.333a.667.667 0 1 1 0 1.334.667.667 0 0 1 0-1.334Zm2 0a.667.667 0 1 1 0 1.334.667.667 0 0 1 0-1.334Z" fill="#151B26" fill-rule="nonzero"/><g stroke="#151B26" stroke-linejoin="round"><path d="m13.192 14.017 1.5-2.5-1.5-2.5h-3l-1.5 2.5 1.5 2.5z"/><path d="M11.692 12.42a.912.912 0 0 0 .92-.903.912.912 0 0 0-.92-.903.912.912 0 0 0-.921.903c0 .498.412.903.92.903Z"/></g></g></svg>
|
||||
|
After Width: | Height: | Size: 781 B |
1
packages/amis-editor-core/src/icons/fixed.svg
Normal file
@@ -0,0 +1 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" fill="currentColor" width="16" height="16" viewBox="0 0 1024 1024" data-spm-anchor-id="a1z1go8z.25060599.0.i11.71e369e4cItcn5"><path d="m750.848 28.928 245.248 242.944a66.048 66.048 0 1 1-93.184 93.184l-25.6-19.456-249.6 353.792 78.336 78.336a66.048 66.048 0 0 1-93.184 92.672l-460.8-464.64a66.048 66.048 0 0 1 93.184-93.184l76.8 78.336 354.048-249.856-18.176-18.944a66.048 66.048 0 1 1 93.184-93.184zM380.672 732.416l-91.904-90.88c-74.24 89.6-191.488 219.904-212.736 247.04a419.84 419.84 0 0 0-70.656 128 419.84 419.84 0 0 0 128-70.144c27.136-21.248 157.44-138.496 246.528-214.016z" data-spm-anchor-id="a1z1go8z.25060599.0.i9.71e369e4cItcn5"/></svg>
|
||||
|
After Width: | Height: | Size: 691 B |
1
packages/amis-editor-core/src/icons/hammer.svg
Normal file
@@ -0,0 +1 @@
|
||||
<svg class="icon" viewBox="0 0 1024 1024" xmlns="http://www.w3.org/2000/svg" width="16" height="16"><path d="m947.9 761.4-264-263.9c-16.3-16.3-43.1-16.3-59.4 0l-19.8 19.8-161-161L708.1 91.9H428L303.6 216.2l-12.3-12.3h-59.4v59.4l12.3 12.3L63.9 456l140 140 180.3-180.3 161.1 161.1-19.8 19.8c-16.3 16.3-16.3 43.1 0 59.4l263.9 263.9c16.3 16.3 43.1 16.3 59.4 0l99-99c16.4-16.4 16.4-43.1.1-59.5z"/></svg>
|
||||
|
After Width: | Height: | Size: 399 B |
108
packages/amis-editor-core/src/icons/index.tsx
Normal file
@@ -0,0 +1,108 @@
|
||||
import {registerIcon, Icon} from 'amis';
|
||||
import ArrowToRight from './arrow-to-right.svg';
|
||||
import LeftArrowToleft from './left-arrow-to-left.svg';
|
||||
import TopArrowToTop from './top-arrow-to-top.svg';
|
||||
import ArrowToBottom from './arrow-to-bottom.svg';
|
||||
import CollapseOpen from './collapse-open-icon.svg';
|
||||
import DisplayInline from './display-inline.svg';
|
||||
import DisplayBlock from './display-block.svg';
|
||||
import DisplayInlineBlock from './display-inline-block.svg';
|
||||
import DisplayFlex from './display-flex.svg';
|
||||
import Harmmer from './hammer.svg';
|
||||
import Dialog from './dialog.svg';
|
||||
import Setting from './setting.svg';
|
||||
import PickerIcon from './picker-icon.svg';
|
||||
|
||||
registerIcon('arrow-to-right', ArrowToRight);
|
||||
registerIcon('left-arrow-to-left', LeftArrowToleft);
|
||||
registerIcon('top-arrow-to-top', TopArrowToTop);
|
||||
registerIcon('arrow-to-bottom', ArrowToBottom);
|
||||
registerIcon('collapse-open', CollapseOpen);
|
||||
registerIcon('harmmer', Harmmer);
|
||||
registerIcon('dialog', Dialog);
|
||||
registerIcon('setting', Setting);
|
||||
registerIcon('picker-icon', PickerIcon);
|
||||
|
||||
// 「页面设计器改版」设计侧提供的icon(组件头部工具栏icon)
|
||||
import CopyBtn from './copy-btn.svg';
|
||||
import MoreBtn from './more-btn.svg';
|
||||
import DeleteBtn from './delete-btn.svg';
|
||||
import DragBtn from './drag-btn.svg';
|
||||
import UpBtn from './up-btn.svg';
|
||||
import ClearBtn from './clear.svg';
|
||||
|
||||
import MergeIcon from './merge-icon.svg';
|
||||
import CancelIcon from './cancel-icon.svg';
|
||||
import CopyIcon from './copy-icon.svg';
|
||||
import DeleteIcon from './delete-icon.svg';
|
||||
import FixedIcon from './fixed.svg';
|
||||
import NoFixedIcon from './no-fixed.svg';
|
||||
|
||||
import Edit from './edit.svg';
|
||||
import Plus from './plus.svg';
|
||||
import UpArrow from './up-arrow.svg';
|
||||
import DownArrowR from './down-arrow.svg';
|
||||
import DeleteEasy from './delete-easy.svg';
|
||||
import DeleteBold from './delete-bold.svg';
|
||||
import DragIcon from './drag-icon.svg';
|
||||
import EventSetting from './event-setting.svg';
|
||||
|
||||
registerIcon('edit-full-btn', Edit);
|
||||
registerIcon('event-setting', EventSetting);
|
||||
registerIcon('add-btn', Plus);
|
||||
registerIcon('close-btn', UpArrow);
|
||||
registerIcon('open-btn-r', DownArrowR);
|
||||
registerIcon('delete-easy-btn', DeleteEasy);
|
||||
registerIcon('delete-bold-btn', DeleteBold);
|
||||
registerIcon('drag-six-circle-btn', DragIcon);
|
||||
|
||||
// 3.0 升级 相关icon
|
||||
import BackUpBtn from './v3/back-up.svg';
|
||||
import DownArrow from './v3/down-arrow.svg';
|
||||
import EditorSearch from './v3/search.svg';
|
||||
import EditorHelp from './v3/editor-help.svg';
|
||||
import EditorFixed from './v3/fixed.svg';
|
||||
import EditorNoFixed from './v3/no-fixed.svg';
|
||||
import EditorRenderer from './v3/renderer.svg';
|
||||
import EditorOutline from './v3/outline.svg';
|
||||
import EditorCode from './v3/code.svg';
|
||||
import EditorCommonConfig from './v3/common-config.svg';
|
||||
import EditorDoubleArrow from './v3/right-double-arrow.svg';
|
||||
import SearchClear from './v3/search-clear.svg';
|
||||
import Shortcut from './v3/shortcut.svg';
|
||||
|
||||
registerIcon('drag-btn', DragBtn);
|
||||
registerIcon('more-btn', MoreBtn);
|
||||
registerIcon('copy-btn', CopyBtn);
|
||||
registerIcon('delete-btn', DeleteBtn);
|
||||
registerIcon('up-btn', UpBtn);
|
||||
registerIcon('clear-btn', ClearBtn);
|
||||
registerIcon('open-btn', DownArrow);
|
||||
|
||||
registerIcon('merge-icon', MergeIcon);
|
||||
registerIcon('cancel-icon', CancelIcon);
|
||||
registerIcon('copy-icon', CopyIcon);
|
||||
registerIcon('delete-icon', DeleteIcon);
|
||||
registerIcon('fixed-icon', FixedIcon);
|
||||
registerIcon('no-fixed-icon', NoFixedIcon);
|
||||
registerIcon('editor-shortcut', Shortcut);
|
||||
|
||||
// 外观-显示类型
|
||||
registerIcon('display-inline', DisplayInline);
|
||||
registerIcon('display-block', DisplayBlock);
|
||||
registerIcon('display-inline-block', DisplayInlineBlock);
|
||||
registerIcon('display-flex', DisplayFlex);
|
||||
registerIcon('back-up', BackUpBtn);
|
||||
registerIcon('down-arrow', DownArrow);
|
||||
registerIcon('editor-search', EditorSearch);
|
||||
registerIcon('editor-help', EditorHelp);
|
||||
registerIcon('editor-fixed', EditorFixed);
|
||||
registerIcon('editor-no-fixed', EditorNoFixed);
|
||||
registerIcon('editor-renderer', EditorRenderer);
|
||||
registerIcon('editor-outline', EditorOutline);
|
||||
registerIcon('editor-code', EditorCode);
|
||||
registerIcon('editor-common-config', EditorCommonConfig);
|
||||
registerIcon('editor-double-arrow', EditorDoubleArrow);
|
||||
registerIcon('search-clear', SearchClear);
|
||||
|
||||
export {registerIcon, Icon};
|
||||
@@ -0,0 +1 @@
|
||||
<svg viewBox="0 0 1024 1024" xmlns="http://www.w3.org/2000/svg" width="16" height="16"><path d="M896 469.333H401.493l98.134-97.706a42.667 42.667 0 1 0-60.587-60.587L268.373 481.707a42.667 42.667 0 0 0-8.96 14.08 42.667 42.667 0 0 0 0 32.426 42.667 42.667 0 0 0 8.96 14.08L439.04 712.96a42.667 42.667 0 0 0 60.587 0 42.667 42.667 0 0 0 0-60.587l-98.134-97.706H896a42.667 42.667 0 0 0 0-85.334zM128 128a42.667 42.667 0 0 0-42.667 42.667v682.666a42.667 42.667 0 0 0 85.334 0V170.667A42.667 42.667 0 0 0 128 128z"/></svg>
|
||||
|
After Width: | Height: | Size: 518 B |
1
packages/amis-editor-core/src/icons/merge-icon.svg
Normal file
@@ -0,0 +1 @@
|
||||
<svg width="24" height="24" xmlns="http://www.w3.org/2000/svg"><g fill-rule="evenodd"><rect x="1" y="1" width="22" height="3" rx="1.5"/><rect x="1" y="7" width="22" height="3" rx="1.5"/><rect x="1" y="20" width="22" height="3" rx="1.5"/><path d="m12.376 17.07 2.398-2.74a.5.5 0 0 0-.376-.83H9.602a.5.5 0 0 0-.376.83l2.398 2.74a.5.5 0 0 0 .752 0Z"/></g></svg>
|
||||
|
After Width: | Height: | Size: 359 B |
1
packages/amis-editor-core/src/icons/more-btn.svg
Normal file
@@ -0,0 +1 @@
|
||||
<svg width="14" height="14" xmlns="http://www.w3.org/2000/svg"><g fill="none" fill-rule="evenodd"><path d="M0 0h14v14H0z"/><circle fill="#FFF" cx="2.8" cy="7" r="1"/><circle fill="#FFF" cx="11.2" cy="7" r="1"/><circle fill="#FFF" cx="7" cy="7" r="1"/></g></svg>
|
||||
|
After Width: | Height: | Size: 262 B |
1
packages/amis-editor-core/src/icons/no-fixed.svg
Normal file
@@ -0,0 +1 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" fill="currentColor" width="16" height="16" viewBox="0 0 1024 1024" data-spm-anchor-id="a1z1go8z.25060599.0.i10.71e369e4cItcn5"><path d="M160.256 816.64C116.224 872.448 102.4 921.6 102.4 921.6s49.152-13.824 104.96-57.856c22.016-17.408 128-112.64 200.704-174.08l-73.728-73.728c-61.44 72.704-157.184 178.688-174.08 200.704zm488.448-607.232-206.336 145.92 226.304 226.304 145.92-206.336 15.872 15.872c20.992 20.992 54.784 20.992 75.776 0s20.992-54.784 0-75.776l-197.12-197.12c-20.992-20.992-54.784-20.992-75.776 0-20.992 20.992-20.992 54.784 0 75.776l15.36 15.36zm-400.896 125.44c-9.728 2.048-18.944 6.656-26.624 14.336-20.992 20.992-20.992 54.784 0 75.776L599.04 802.816c20.992 20.992 54.784 20.992 75.776 0 7.68-7.68 12.288-16.896 14.336-26.624L247.808 334.848z" data-spm-anchor-id="a1z1go8z.25060599.0.i12.71e369e4cItcn5"/><path d="M840.704 879.104c-9.728 0-19.456-3.584-27.136-11.264l-657.92-657.408c-14.848-14.848-14.848-39.424 0-54.272 14.848-14.848 39.424-14.848 54.272 0l657.92 657.92c14.848 14.848 14.848 39.424 0 54.272-7.168 7.168-16.896 10.752-27.136 10.752z" data-spm-anchor-id="a1z1go8z.25060599.0.i13.71e369e4cItcn5"/></svg>
|
||||
|
After Width: | Height: | Size: 1.1 KiB |
1
packages/amis-editor-core/src/icons/picker-icon.svg
Normal file
@@ -0,0 +1 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg"><g fill="none" fill-rule="evenodd" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round"><path d="M14.5 14.5h-13v-13h13v1.998"/><path d="m12.775 4.943 1.768 1.768-1.766 1.765M13.85 6.71H8m1.694 2.525-1.768 1.768 1.766 1.766m-1.074-1.767 5.882-.001"/></g></svg>
|
||||
|
After Width: | Height: | Size: 316 B |
1
packages/amis-editor-core/src/icons/plus.svg
Normal file
@@ -0,0 +1 @@
|
||||
<svg width="16" height="16" xmlns="http://www.w3.org/2000/svg"><path d="M8.5 2v5.5H14v1H8.5V14h-1V8.5H2v-1h5.5V2h1Z" fill="currentColor" fill-rule="evenodd"/></svg>
|
||||
|
After Width: | Height: | Size: 165 B |
1
packages/amis-editor-core/src/icons/position-bottom.svg
Normal file
@@ -0,0 +1 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="none"><path d="M11 7.23 8.23 10m0 0L5.463 7.23M8.23 10V3M14 12.7H2" stroke="currentColor" stroke-width="1.4"/></svg>
|
||||
|
After Width: | Height: | Size: 186 B |
1
packages/amis-editor-core/src/icons/position-left.svg
Normal file
@@ -0,0 +1 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="none"><path d="M8.77 11 6 8.23m0 0 2.77-2.768M6 8.23h7M3.3 14V2" stroke="currentColor" stroke-width="1.4"/></svg>
|
||||
|
After Width: | Height: | Size: 183 B |
1
packages/amis-editor-core/src/icons/position-right.svg
Normal file
@@ -0,0 +1 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="none"><path d="M7.23 5 10 7.77m0 0-2.77 2.769M10 7.769H3M12.7 2v12" stroke="currentColor" stroke-width="1.4"/></svg>
|
||||
|
After Width: | Height: | Size: 186 B |
1
packages/amis-editor-core/src/icons/position-top.svg
Normal file
@@ -0,0 +1 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="none"><path d="M8.77 11 6 8.23m0 0 2.77-2.768M6 8.23h7M3.3 14V2" stroke="currentColor" stroke-width="1.4"/></svg>
|
||||
|
After Width: | Height: | Size: 183 B |
1
packages/amis-editor-core/src/icons/setting.svg
Normal file
@@ -0,0 +1 @@
|
||||
<svg width="16" height="16" xmlns="http://www.w3.org/2000/svg"><g fill="none" fill-rule="evenodd"><path d="M0 0h16v16H0z"/><path d="M11.5 2 15 8.063l-3.5 6.062h-7L1 8.063 4.5 2h7Zm-.578 1H5.077L2.155 8.064l2.922 5.06h5.845l2.924-5.06L10.922 3ZM8 6.064a2 2 0 1 1 0 4 2 2 0 0 1 0-4Zm0 1a1.001 1.001 0 0 0 0 2 1.001 1.001 0 0 0 0-2Z" fill="#84868C" fill-rule="nonzero"/></g></svg>
|
||||
|
After Width: | Height: | Size: 378 B |
1
packages/amis-editor-core/src/icons/top-arrow-to-top.svg
Normal file
@@ -0,0 +1 @@
|
||||
<svg viewBox="0 0 1024 1024" xmlns="http://www.w3.org/2000/svg" width="16" height="16"><path d="M542.293 268.373a42.667 42.667 0 0 0-14.08-8.96 42.667 42.667 0 0 0-32.426 0 42.667 42.667 0 0 0-14.08 8.96L311.04 439.04a42.667 42.667 0 1 0 60.587 60.587l97.706-98.134V896a42.667 42.667 0 0 0 85.334 0V401.493l97.706 98.134a42.667 42.667 0 0 0 60.587 0 42.667 42.667 0 0 0 0-60.587zm268.374-183.04H213.333a42.667 42.667 0 0 0 0 85.334h597.334a42.667 42.667 0 0 0 0-85.334z"/></svg>
|
||||
|
After Width: | Height: | Size: 479 B |
1
packages/amis-editor-core/src/icons/up-arrow.svg
Normal file
@@ -0,0 +1 @@
|
||||
<svg width="24" height="24" viewBox="0 0 16 16" xmlns="http://www.w3.org/2000/svg"><path stroke="currentColor" stroke-linejoin="round" d="M13.664 10.507 8.007 4.85 2.35 10.507" fill="none"/></svg>
|
||||
|
After Width: | Height: | Size: 197 B |
1
packages/amis-editor-core/src/icons/up-btn.svg
Normal file
@@ -0,0 +1 @@
|
||||
<svg width="45" height="45" viewBox="0 0 28 29" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"><defs><radialGradient cx="69.986%" cy="19.319%" fx="69.986%" fy="19.319%" r="49.554%" gradientTransform="scale(-1 -.71033) rotate(-39.868 -.641 1.89)" id="e"><stop stop-color="#FFF" offset="0%"/><stop stop-color="#FFF" stop-opacity="0" offset="100%"/></radialGradient><radialGradient cx="77.918%" cy="13.589%" fx="77.918%" fy="13.589%" r="45.882%" id="j"><stop stop-color="#FFF" offset="0%"/><stop stop-color="#FFF" stop-opacity="0" offset="100%"/></radialGradient><linearGradient x1="50%" y1="0%" x2="50%" y2="100%" id="a"><stop stop-color="#0752E9" offset="0%"/><stop stop-color="#2468F2" offset="100%"/></linearGradient><linearGradient x1="50%" y1="0%" x2="50%" y2="100%" id="g"><stop stop-color="#0752E9" offset="0%"/><stop stop-color="#2468F2" offset="100%"/></linearGradient><filter x="-75%" y="-75%" width="250%" height="250%" filterUnits="objectBoundingBox" id="c"><feGaussianBlur stdDeviation="7" in="SourceGraphic"/></filter><filter x="-100%" y="-100%" width="300%" height="300%" filterUnits="objectBoundingBox" id="h"><feGaussianBlur stdDeviation="2" in="SourceGraphic"/></filter><circle id="f" cx="3" cy="2.5" r="2.5"/><path d="m8 0 .335.276a13.981 13.981 0 0 1 5.075 11.687c1.592 1.198 2.59 3.019 2.59 5.35a7.97 7.97 0 0 1-1.93 5.212l-.72-2.16a3 3 0 0 0-2.846-2.051H5.496a3 3 0 0 0-2.846 2.051l-.72 2.16A7.969 7.969 0 0 1 0 17.314c0-2.332.998-4.153 2.59-5.352A13.977 13.977 0 0 1 7.665.276L8 0Z" id="b"/></defs><g fill="none" fill-rule="evenodd"><path d="M14 5c7.732 0 14 6.268 14 14 0 3.728-1.457 7.116-3.834 9.625A6.967 6.967 0 0 1 20 30a6.976 6.976 0 0 1-2.799-.582 10.339 10.339 0 0 1-2.974 3.889l-.113.09-.114.088a10.34 10.34 0 0 1-3.201-4.068A6.963 6.963 0 0 1 8 30c-1.56 0-3.002-.51-4.166-1.374A13.954 13.954 0 0 1 0 19C0 11.268 6.268 5 14 5Z" fill="url(#a)" transform="translate(0 -5)"/><g transform="translate(6 1.5)"><mask id="d" fill="#fff"><use xlink:href="#b"/></mask><use fill="#F2F2FF" xlink:href="#b"/><circle fill="#2468F2" filter="url(#c)" mask="url(#d)" cx="19" cy="-2" r="14"/><path d="m8 0 .335.276a13.981 13.981 0 0 1 5.075 11.687c1.592 1.198 2.59 3.019 2.59 5.35a7.97 7.97 0 0 1-1.93 5.212l-.72-2.16a3 3 0 0 0-2.662-2.046l-.184-.005H5.496a3 3 0 0 0-2.783 1.879l-.063.172-.72 2.16A7.969 7.969 0 0 1 0 17.314c0-2.332.998-4.153 2.59-5.352A13.977 13.977 0 0 1 7.497.416L8 0Zm0 .647-.179.15a13.472 13.472 0 0 0-4.75 10.762l.018.372.017.268-.215.162C1.354 13.52.5 15.226.5 17.314c0 1.39.38 2.72 1.077 3.874l.178.28.425-1.275.069-.187a3.5 3.5 0 0 1 3.043-2.186l.204-.006h5.024l.198.006a3.5 3.5 0 0 1 3.04 2.205l.067.182.42 1.261.012-.017a7.459 7.459 0 0 0 1.236-3.818l.007-.32c0-1.992-.778-3.638-2.185-4.79l-.205-.16-.215-.163.016-.268A13.478 13.478 0 0 0 8.303.904L8 .647Z" fill-opacity=".7" fill="url(#e)" mask="url(#d)"/></g><g transform="translate(11 6.814)"><mask id="i" fill="#fff"><use xlink:href="#f"/></mask><use fill="url(#g)" xlink:href="#f"/><path fill="#2468F2" filter="url(#h)" mask="url(#i)" d="M3-3h6v6H3z"/></g><path d="M8 11.314a2.5 2.5 0 1 1 0 5 2.5 2.5 0 0 1 0-5Zm0 .5a2 2 0 1 0 0 4 2 2 0 0 0 0-4Z" fill-opacity=".7" fill="url(#j)" transform="translate(6 -4.5)"/></g></svg>
|
||||
|
After Width: | Height: | Size: 3.2 KiB |
1
packages/amis-editor-core/src/icons/v3/back-up.svg
Normal file
@@ -0,0 +1 @@
|
||||
<svg width="32" height="32" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"><defs><linearGradient x1="100%" y1="101.089%" x2="0%" y2="-14.125%" id="c"><stop stop-color="#FFF" offset="0%"/><stop stop-color="#FFF8F8" offset="46.533%"/><stop stop-color="#DDFEEB" offset="100%"/></linearGradient><linearGradient x1="100%" y1="51.269%" x2="0%" y2="48.407%" id="f"><stop stop-color="#FFF" offset="0%"/><stop stop-color="#FFF8F8" offset="46.533%"/><stop stop-color="#DDFEEB" offset="100%"/></linearGradient><filter x="-24.3%" y="-19.2%" width="148.6%" height="153.6%" filterUnits="objectBoundingBox" id="a"><feOffset dy="1" in="SourceAlpha" result="shadowOffsetOuter1"/><feGaussianBlur stdDeviation="1" in="shadowOffsetOuter1" result="shadowBlurOuter1"/><feColorMatrix values="0 0 0 0 0.179423161 0 0 0 0 0.342133642 0 0 0 0 0.637515745 0 0 0 0.650991587 0" in="shadowBlurOuter1"/></filter><filter x="-25%" y="-125%" width="150%" height="450%" filterUnits="objectBoundingBox" id="d"><feOffset dy="1" in="SourceAlpha" result="shadowOffsetOuter1"/><feGaussianBlur stdDeviation="1" in="shadowOffsetOuter1" result="shadowBlurOuter1"/><feColorMatrix values="0 0 0 0 0.179423161 0 0 0 0 0.342133642 0 0 0 0 0.637515745 0 0 0 0.650991587 0" in="shadowBlurOuter1"/></filter><rect id="e" x="0" y="0" width="14" height="2" rx="1"/><path d="M15.962 12.004c.157.01.312.056.452.139l.101.07.095.082L23 18.322a1 1 0 0 1-1.32 1.498l-.094-.083-4.61-4.315.001 8.788c0 .466-.447.843-1 .843-.512 0-.935-.325-.993-.745l-.007-.098v-8.866l-4.372 4.393a1 1 0 0 1-1.497-1.32l.083-.095 6.004-6.027a.996.996 0 0 1 .648-.29h.119Z" id="b"/></defs><g fill="none" fill-rule="evenodd"><rect fill="#2468F2" width="32" height="32" rx="16"/><g fill-rule="nonzero"><use fill="#000" filter="url(#a)" xlink:href="#b"/><use fill="url(#c)" xlink:href="#b"/></g><g transform="translate(9 7.702)" fill-rule="nonzero"><use fill="#000" filter="url(#d)" xlink:href="#e"/><use fill="url(#f)" xlink:href="#e"/></g></g></svg>
|
||||
|
After Width: | Height: | Size: 2.0 KiB |
8
packages/amis-editor-core/src/icons/v3/code.svg
Normal file
@@ -0,0 +1,8 @@
|
||||
<svg viewBox="0 0 6 6" xmlns="http://www.w3.org/2000/svg">
|
||||
<g fill="none" fill-rule="evenodd">
|
||||
<path d="M.2.6h5.6v4.8H.2z"/>
|
||||
<path stroke="currentColor" stroke-width=".5" stroke-linejoin="round" d="M.5.917h5v4.166h-5z"/>
|
||||
<path stroke="currentColor" stroke-width=".5" stroke-linecap="round" d="M3.57 1.947 2.449 4.042"/>
|
||||
<path d="m4.214 2.411.59.59-.589.588M1.861 2.411l-.59.59.589.588" stroke="currentColor" stroke-width=".5" stroke-linecap="round" stroke-linejoin="round"/>
|
||||
</g>
|
||||
</svg>
|
||||
|
After Width: | Height: | Size: 527 B |
1
packages/amis-editor-core/src/icons/v3/common-config.svg
Normal file
@@ -0,0 +1 @@
|
||||
<svg width="16" height="16" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg"><g fill="none" fill-rule="evenodd"><path d="M0 0h20v20H0z"/><path stroke="currentColor" stroke-width="1.25" stroke-linejoin="round" d="m10 1.25 7.578 4.375v8.75L10 18.75l-7.578-4.375v-8.75z"/><path stroke="currentColor" stroke-width="1.25" stroke-linecap="round" d="m5.824 7.826 3.955 2.418m4.275-2.418-4.275 2.418m0 4.601v-4.601"/></g></svg>
|
||||
|
After Width: | Height: | Size: 426 B |
1
packages/amis-editor-core/src/icons/v3/down-arrow.svg
Normal file
@@ -0,0 +1 @@
|
||||
<svg viewBox="0 0 16 16" fill="currentColor" xmlns="http://www.w3.org/2000/svg"><path d="m12.233 5-4.24 4.24L3.753 5l-.713.713 4.95 4.95.003-.003.003.003 4.95-4.95z"/></svg>
|
||||
|
After Width: | Height: | Size: 174 B |
1
packages/amis-editor-core/src/icons/v3/drag.svg
Normal file
@@ -0,0 +1 @@
|
||||
<svg width="20" height="18" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"><defs><path id="a" d="M0 0h20v18H0z"/><path d="M11 1H9v7H5v2h4v7h2v-7h4V8h-4V1ZM1 9l4-3v6L1 9Zm18 0-4 3V6l4 3Z" id="c"/></defs><g fill="none" fill-rule="evenodd"><path d="M11 1H9v7H5v2h4v7h2v-7h4V8h-4V1ZM1 9l4-3v6L1 9Zm18 0-4 3V6l4 3Z" fill="#363B3E"/><mask id="b" fill="#fff"><use xlink:href="#a"/></mask><path d="M11 1h1V0h-1v1ZM9 1V0H8v1h1Zm0 7v1h1V8H9ZM5 8V7H4v1h1Zm0 2H4v1h1v-1Zm4 0h1V9H9v1Zm0 7H8v1h1v-1Zm2 0v1h1v-1h-1Zm0-7V9h-1v1h1Zm4 0v1h1v-1h-1Zm0-2h1V7h-1v1Zm-4 0h-1v1h1V8ZM5 6h1V4L4.4 5.2 5 6Zm0 6-.6.8L6 14v-2H5Zm10 0h-1v2l1.6-1.2-.6-.8Zm0-6 .6-.8L14 4v2h1Zm-4-6H9v2h2V0ZM8 1v7h2V1H8Zm1 6H5v2h4V7ZM4 8v2h2V8H4Zm1 3h4V9H5v2Zm3-1v7h2v-7H8Zm1 8h2v-2H9v2Zm3-1v-7h-2v7h2Zm-1-6h4V9h-4v2Zm5-1V8h-2v2h2Zm-1-3h-4v2h4V7Zm-3 1V1h-2v7h2ZM1.6 9.8l4-3-1.2-1.6-4 3 1.2 1.6ZM4 6v6h2V6H4Zm1.6 5.2-4-3L.4 9.8l4 3 1.2-1.6Zm12.8-3-4 3 1.2 1.6 4-3-1.2-1.6ZM16 12V6h-2v6h2Zm-1.6-5.2 4 3 1.2-1.6-4-3-1.2 1.6Z" fill-opacity=".8" fill="#FFF" fill-rule="nonzero" mask="url(#b)"/><use fill="#151B26" xlink:href="#c"/></g></svg>
|
||||
|
After Width: | Height: | Size: 1.1 KiB |
1
packages/amis-editor-core/src/icons/v3/editor-help.svg
Normal file
@@ -0,0 +1 @@
|
||||
<svg viewBox="0 0 16 16" xmlns="http://www.w3.org/2000/svg"><path d="m7.976 1 .241.003A7 7 0 1 1 7.977 1Zm.004 1-.225.005A6 6 0 1 0 7.98 2Zm.03 8.8a.6.6 0 1 1 .004 1.2.6.6 0 0 1-.005-1.2Zm-.024-6.787c1.3-.004 2.368 1.003 2.455 2.258l.007.18c.01.33-.07.742-.325 1.15-.258.414-.656.734-1.2.922l-.272.085c-.12.049-.144.159-.146.744v.407l-1 .003v-.296c.007-1.197.136-1.59.83-1.81l.12-.034c.406-.11.663-.3.82-.55.111-.18.16-.366.171-.51l.002-.102a1.453 1.453 0 0 0-2.9-.13l-.007.14-1 .002a2.453 2.453 0 0 1 2.445-2.46Z"/></svg>
|
||||
|
After Width: | Height: | Size: 523 B |
1
packages/amis-editor-core/src/icons/v3/fixed.svg
Normal file
@@ -0,0 +1 @@
|
||||
<svg width="16" height="16" xmlns="http://www.w3.org/2000/svg"><g fill="none" fill-rule="evenodd"><path d="M0 0h16v16H0z"/><path d="m14.333 5.928-4.26-4.26a.572.572 0 0 0-.813 0L6.475 4.455A5.698 5.698 0 0 0 2.27 5.67a.574.574 0 0 0-.047.854l3.142 3.142-3.724 3.72a.273.273 0 0 0-.08.17l-.059.643a.276.276 0 0 0 .275.301c.009 0 .017 0 .026-.002l.643-.059a.273.273 0 0 0 .17-.079l3.724-3.724 3.14 3.141a.572.572 0 0 0 .855-.046 5.711 5.711 0 0 0 1.214-4.209l2.785-2.785a.573.573 0 0 0 0-.81Zm-3.667 2.718-.424.423.066.595a4.494 4.494 0 0 1-.526 2.661L3.678 6.217a4.471 4.471 0 0 1 2.66-.526l.595.066.424-.424 2.311-2.312 3.311 3.311-2.313 2.314Zm0 0-.424.423.066.595a4.494 4.494 0 0 1-.526 2.661L3.678 6.217a4.471 4.471 0 0 1 2.66-.526l.595.066.424-.424 2.311-2.312 3.311 3.311-2.313 2.314Z" fill="currentColor" fill-rule="nonzero"/></g></svg>
|
||||
|
After Width: | Height: | Size: 843 B |
1
packages/amis-editor-core/src/icons/v3/h5-preview.svg
Normal file
@@ -0,0 +1 @@
|
||||
<svg viewBox="0 0 16 16" xmlns="http://www.w3.org/2000/svg"><g fill="none"><path d="M0 0h16v16H0z"/><path d="M13 1.5a.5.5 0 0 1 .5.5v12a.5.5 0 0 1-.5.5H3a.5.5 0 0 1-.5-.5V2a.5.5 0 0 1 .5-.5Zm-.5 1h-9v11h9v-11ZM9 11.66a.5.5 0 0 1 .09.992L9 12.66H7a.5.5 0 0 1-.09-.992L7 11.66h2Z" fill="currentColor"/></g></svg>
|
||||
|
After Width: | Height: | Size: 311 B |