Thanks to visit codestin.com
Credit goes to github.com

Skip to content
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
12 changes: 4 additions & 8 deletions packages/amis-editor-core/src/component/Editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -312,19 +312,15 @@ export default class Editor extends Component<EditorProps> {
// 快捷功能键
@autobind
handleKeyDown(e: KeyboardEvent) {
const manager = this.manager;
const store = manager.store;

// 弹窗模式不处理
if (this.props.isSubEditor) {
if (this.props.isSubEditor || this.props.readonly || store.activeElement) {
// e.defaultPrevented // 或者已经阻止不处理
return;
}

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

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

if (
(e.target as HTMLElement).tagName === 'BODY' &&
(e.key === 'z' || e.key === 'Z') &&
Expand Down
6 changes: 4 additions & 2 deletions packages/amis-editor-core/src/component/Panel/DialogList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@ export default observer(function DialogList({
manager.openSubEditor({
title: '编辑弹窗',
value: modal,
onChange: ({definitions, ...modal}: any, diff: any) => {
onDefinitionsChange: (definitions, originDefinitions, modal) => {
store.addModal(modal, definitions);
return false;
}
});
}, []);
Expand All @@ -58,8 +59,9 @@ export default observer(function DialogList({
...(modal as any),
definitions: modalsToDefinitions(store.modals, {}, modal)
},
onChange: ({definitions, ...modal}: any, diff: any) => {
onDefinitionsChange: (definitions, originDefinitions, modal) => {
store.updateModal(modalId, modal, definitions);
return false;
}
});
}, []);
Expand Down
49 changes: 43 additions & 6 deletions packages/amis-editor-core/src/store/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ import type {DialogSchema} from 'amis/lib/renderers/Dialog';
import type {DrawerSchema} from 'amis/lib/renderers/Drawer';
import getLayoutInstance from '../layout';
import {isAlive} from 'mobx-state-tree';
import {modalsToDefinitions} from '../util';

export interface SchemaHistory {
versionId: number;
Expand All @@ -75,10 +76,17 @@ export interface SchemaHistory {
export type SubEditorContext = {
title: string;
value: any;
onChange: (value: any, diff: any) => void;
onChange?: (value: any, diff: any) => void;
slot?: any;
data?: any;
validate?: (value: any) => void | string | Promise<void | string>;

// 当 definitions 变化的时候,会触发这个回调
onDefinitionsChange?: (
definitions: any,
originDefinitions: any,
value: any
) => any;
canUndo?: boolean;
canRedo?: boolean;

Expand Down Expand Up @@ -921,7 +929,7 @@ export const MainStore = types

get subEditorValue() {
if (self.subEditorContext) {
return self.subEditorContext.slot
let subSchema = self.subEditorContext.slot
? {
...mapObject(self.subEditorContext.slot, function (value: any) {
if (value === '$$') {
Expand All @@ -933,6 +941,15 @@ export const MainStore = types
isSlot: true
}
: self.subEditorContext.value;

if (!subSchema.definitions) {
subSchema = {
...subSchema,
definitions: modalsToDefinitions(this.modals)
};
}

return subSchema;
}

return undefined;
Expand Down Expand Up @@ -2029,9 +2046,10 @@ export const MainStore = types
},

confirmSubEditor([valueRaw]: any) {
const {onChange, slot} = self.subEditorContext!;
let value = valueRaw.schema;
let originValue = valueRaw.__pristine?.schema || value;
const {onChange, slot, onDefinitionsChange} = self.subEditorContext!;
let {definitions, ...value} = valueRaw.schema;
let {definitions: originDefinitions, ...originValue} =
valueRaw.__pristine?.schema || value;

if (slot) {
const slotPath = self.subEditorSlotPath;
Expand All @@ -2050,11 +2068,30 @@ export const MainStore = types
}
}

onChange(
const prevented =
onDefinitionsChange?.(definitions, originDefinitions, value) ===
false;

onChange?.(
value,
onChange.length > 1 ? diff(originValue, value) : undefined
);

if (!prevented) {
// merge definitions
const patches = diff(
originDefinitions,
definitions,
(path, key) => key === '$$id'
);
this.traceableSetSchema(
JSONUpdate(self.schema, self.schema.$$id, {
definitions: patchDiff(originDefinitions, patches)
}),
true
);
}

self.subEditorContext = undefined;
},

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ function DialogActionPanel({
if (originInd) {
const parent = JSONGetParentById(schema, originInd);
if (parent && parent.actionType) {
originActionId = parent.$$id;
originActionId = originInd;
newRefName = currentModal.modal.$$ref;
} else {
// 没找到很可能是在主页面里面的弹窗
Expand Down Expand Up @@ -312,7 +312,7 @@ function DialogActionPanel({
if (originActionId && newRefName) {
schema = JSONUpdate(
schema,
currentModal.value,
originActionId,
JSONPipeIn({
$ref: newRefName
}),
Expand Down Expand Up @@ -579,7 +579,7 @@ function DialogActionPanel({
manager.openSubEditor({
title: '新建弹窗',
value: modal,
onChange: ({definitions, ...modal}: any, diff: any) => {
onDefinitionsChange: (definitions, originDefinitions, modal) => {
// 不能变 $$id 如果有内部有引用,就找不到了
modal = JSONPipeIn({...modal, $$id: modalId});
let arr = modals.concat();
Expand Down Expand Up @@ -609,6 +609,7 @@ function DialogActionPanel({
}
setModals(arr);
onBulkChange({__actionModals: arr});
return false;
}
});
closePopOver?.();
Expand Down Expand Up @@ -640,7 +641,7 @@ function DialogActionPanel({
currentModal.modal
)
},
onChange: ({definitions, ...modal}: any, diff: any) => {
onDefinitionsChange: (definitions, originDefinitions, modal) => {
// 编辑的时候不要修改 $$id
modal = JSONPipeIn({...modal, $$id: currentModal.modal.$$id});
let arr = modals.map(item =>
Expand All @@ -666,6 +667,7 @@ function DialogActionPanel({
arr = mergeDefinitions(arr, definitions, modal);
setModals(arr);
onBulkChange({__actionModals: arr});
return false;
}
});
}, [modals]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,13 @@ const modalDescDetail: (info: any, context: any, props: any) => any = (
...modal,
definitions: modalsToDefinitions(store.modals, {}, modal)
},
onChange: ({definitions, ...modal}: any, diff: any) => {
onDefinitionsChange: (
definitions: any,
originDefinitions: any,
modal: any
) => {
store.updateModal(modalId, modal, definitions);
return false;
}
});
}}
Expand Down
Loading