Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 扩展编辑器功能 #10834

Merged
merged 4 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions packages/amis-editor-core/scss/editor.scss
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
min-width: 980px;
overflow: hidden;
user-select: none;
position: relative;

// 覆盖amis默认top值,避免导致未垂直居中
.ae-Editor-toolbar svg.icon {
Expand Down Expand Up @@ -125,6 +126,18 @@
border: 1px dashed rgb(206, 208, 211);
}

.subEditor-container {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
& .subEditor-dialog {
height: 100%;
margin: 0;
}
}

// 弹窗编辑器面板
.subEditor-dialog {
overflow: hidden;
Expand Down
13 changes: 11 additions & 2 deletions packages/amis-editor-core/src/component/Editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ export interface EditorProps extends PluginEventListener {
getHostNodeDataSchema?: () => Promise<any>;

getAvaiableContextFields?: (node: EditorNodeType) => Promise<any>;
readonly?: boolean;
}

export default class Editor extends Component<EditorProps> {
Expand Down Expand Up @@ -275,6 +276,10 @@ export default class Editor extends Component<EditorProps> {
return;
}

if (this.props.readonly) {
return;
}

const manager = this.manager;
const store = manager.store;

Expand Down Expand Up @@ -573,7 +578,8 @@ export default class Editor extends Component<EditorProps> {
previewProps,
autoFocus,
isSubEditor,
amisEnv
amisEnv,
readonly
} = this.props;

return (
Expand All @@ -588,7 +594,7 @@ export default class Editor extends Component<EditorProps> {
)}
>
<div className="ae-Editor-inner" onContextMenu={this.handleContextMenu}>
{!preview && (
{!preview && !readonly && (
<LeftPanels
store={this.store}
manager={this.manager}
Expand Down Expand Up @@ -618,6 +624,7 @@ export default class Editor extends Component<EditorProps> {
amisEnv={amisEnv}
autoFocus={autoFocus}
toolbarContainer={this.getToolbarContainer}
readonly={readonly}
></Preview>
</div>

Expand All @@ -628,6 +635,7 @@ export default class Editor extends Component<EditorProps> {
theme={theme}
appLocale={appLocale}
amisEnv={amisEnv}
readonly={readonly}
/>
)}

Expand All @@ -639,6 +647,7 @@ export default class Editor extends Component<EditorProps> {
manager={this.manager}
theme={theme}
amisEnv={amisEnv}
readonly={readonly}
/>
<ScaffoldModal
store={this.store}
Expand Down
6 changes: 4 additions & 2 deletions packages/amis-editor-core/src/component/HighlightBox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export interface HighlightBoxProps {
onSwitch?: (id: string) => void;
manager: EditorManager;
children?: React.ReactNode;
readonly?: boolean;
}

export default observer(function ({
Expand All @@ -30,7 +31,8 @@ export default observer(function ({
node,
toolbarContainer,
onSwitch,
manager
manager,
readonly
}: HighlightBoxProps) {
const handleWResizerMouseDown = React.useCallback(
(e: MouseEvent) => startResize(e, 'horizontal'),
Expand Down Expand Up @@ -250,7 +252,7 @@ export default observer(function ({
draggable={!!curFreeContainerId || isDraggableContainer}
onDragStart={handleDragStart}
>
{isActive ? (
{isActive && !readonly ? (
<div
className={`ae-Editor-toolbarPopover ${
isRightElem ? 'is-right-elem' : ''
Expand Down
30 changes: 27 additions & 3 deletions packages/amis-editor-core/src/component/Panel/RightPanels.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {observer} from 'mobx-react';
import React from 'react';
import {Tab, Tabs} from 'amis';
import {Tab, Tabs, toast} from 'amis';
import cx from 'classnames';
import {EditorManager} from '../../manager';
import {EditorStoreType} from '../../store/editor';
Expand All @@ -16,6 +16,7 @@ interface RightPanelsProps {
theme?: string;
appLocale?: string;
amisEnv?: any;
readonly?: boolean;
}

interface RightPanelsStates {
Expand Down Expand Up @@ -62,6 +63,29 @@ export class RightPanels extends React.Component<
return findDOMNode(this) as HTMLElement;
}

@autobind
handlePanelChangeValue(
...arg: Parameters<typeof this.props.manager.panelChangeValue>
) {
const {manager, readonly} = this.props;

if (readonly) {
const diff = arg[1];
if (
!diff?.find((item: any) =>
item.path.find(
(p: string) => !p.startsWith('__') && p !== 'pullRefresh'
)
)
) {
return;
}
toast.error('不支持编辑');
} else {
manager.panelChangeValue(...arg);
}
}

render() {
const {store, manager, theme} = this.props;
const {isOpenStatus, isFixedStatus} = this.state;
Expand All @@ -77,7 +101,7 @@ export class RightPanels extends React.Component<
path: node?.path,
node: node,
value: store.value,
onChange: manager.panelChangeValue,
onChange: this.handlePanelChangeValue,
store: store,
manager: manager,
popOverContainer: this.getPopOverContainer
Expand All @@ -90,7 +114,7 @@ export class RightPanels extends React.Component<
info={node?.info}
path={node?.path}
value={store.value}
onChange={manager.panelChangeValue}
onChange={this.handlePanelChangeValue}
store={store}
manager={manager}
popOverContainer={this.getPopOverContainer}
Expand Down
4 changes: 4 additions & 0 deletions packages/amis-editor-core/src/component/Preview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ export interface PreviewProps {
autoFocus?: boolean;

toolbarContainer?: () => any;

readonly?: boolean;
}

export interface PreviewState {
Expand Down Expand Up @@ -604,9 +606,11 @@ export default class Preview extends Component<PreviewProps> {
toolbarContainer={toolbarContainer}
onSwitch={this.handleNavSwitch}
manager={manager}
readonly={this.props.readonly}
>
{node.childRegions.map(region =>
!node.memberImmutable(region.region) &&
!this.props.readonly &&
store.isRegionActive(region.id, region.region) ? (
<RegionHighlightBox
manager={manager}
Expand Down
13 changes: 10 additions & 3 deletions packages/amis-editor-core/src/component/SubEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export interface SubEditorProps {
manager: EditorManager;
theme?: string;
amisEnv?: RenderOptions;
readonly?: boolean;
}

@observer
Expand Down Expand Up @@ -97,7 +98,7 @@ export class SubEditor extends React.Component<SubEditorProps> {
}

buildSchema() {
const {store, manager, amisEnv} = this.props;
const {store, manager, amisEnv, readonly} = this.props;
const subEditorContext = store.subEditorContext;
const config = manager.config;
let superEditorData: any = store.superEditorData;
Expand All @@ -118,6 +119,7 @@ export class SubEditor extends React.Component<SubEditorProps> {
? {
type: 'form',
mode: 'normal',
wrapWithPanel: false,
wrapperComponent: 'div',
onValidate: async (value: any) => {
const result = await store.subEditorContext?.validate?.(value);
Expand Down Expand Up @@ -190,6 +192,7 @@ export class SubEditor extends React.Component<SubEditorProps> {
getAvaiableContextFields={node =>
manager.getAvailableContextFields(node)
}
readonly={readonly}
/>
)
}
Expand Down Expand Up @@ -244,10 +247,14 @@ export class SubEditor extends React.Component<SubEditorProps> {
}

render() {
const {store, theme, manager} = this.props;
const {store, theme, manager, readonly} = this.props;
if (!store.subEditorContext) {
return null;
}
return render(
{
type: 'dialog',
type: readonly ? 'container' : 'dialog',
className: readonly ? 'subEditor-container' : 'subEditor-dialog',
...this.buildSchema()
},

Expand Down
6 changes: 4 additions & 2 deletions packages/amis-editor-core/src/component/base/SchemaForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ export function SchemaFrom({
return schema;
}, [body, controls, submitOnChange]);

const [init, setInit] = React.useState(true);

const themeConfig = React.useMemo(() => getThemeConfig(), []);
const submitSubscribers = React.useRef<Array<Function>>([]);
const subscribeSubmit = React.useCallback(
Expand Down Expand Up @@ -147,10 +149,10 @@ export function SchemaFrom({
newValue = pipeOut ? await pipeOut(newValue, value) : newValue;
const diffValue = diff(value, newValue);
// 没有变化时不触发onChange
if (!diffValue) {
if (!diffValue || init) {
setInit(false);
return;
}

onChange(newValue, diffValue, (schema, value, id, diff) => {
return submitSubscribers.current.reduce((schema, fn) => {
return fn(schema, value, id, diff);
Expand Down
10 changes: 10 additions & 0 deletions packages/amis-editor-core/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1029,6 +1029,9 @@ export abstract class BasePlugin implements PluginInterface {

static scene = ['global'];

name?: string;
rendererName?: string;

/**
* 如果配置里面有 rendererName 自动返回渲染器信息。
* @param renderer
Expand Down Expand Up @@ -1279,6 +1282,13 @@ export abstract class BasePlugin implements PluginInterface {
originalValue: node.schema.value // 记录原始值,循环引用检测需要
} as any;
}

getKeyAndName() {
return {
key: this.rendererName,
name: this.name
};
}
}

/**
Expand Down
Loading
Loading