Skip to content

Commit

Permalink
fix: 移动端编辑器mobile 字段保存失败 (#11072)
Browse files Browse the repository at this point in the history
* feat:编辑器移动端支持单独编辑

* fix: ts报错

* fix: 移动端编辑器mobile 字段保存失败

---------

Co-authored-by: zhangxulong <zhangxulong@baidu.com>
Co-authored-by: qkiroc <30946345+qkiroc@users.noreply.github.com>
  • Loading branch information
3 people authored Oct 17, 2024
1 parent eaf5217 commit 69905fc
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import React from 'react';
import {PanelProps} from '../../plugin';
import {autobind} from '../../util';
import {autobind, DiffChange} from '../../util';
import AMisCodeEditor from './AMisCodeEditor';
import type {BaseSchema} from 'amis';

export default class CodeEditorPanel extends React.Component<PanelProps> {
@autobind
Expand All @@ -23,10 +24,10 @@ export default class CodeEditorPanel extends React.Component<PanelProps> {
}

@autobind
onChange(...rest: any) {
onChange(value: BaseSchema, diff?: Array<DiffChange>) {
const {store} = this.props;
store.patchCodeEdit(true);
this.props.onChange(rest);
this.props.onChange(value, diff);
store.patchCodeEdit(false);
}

Expand Down
35 changes: 26 additions & 9 deletions packages/amis-editor-core/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -934,6 +934,24 @@ export function patchDiff(left: any, changes: Array<DiffChange> | undefined) {
);
}

/**
* 添加移动端单独编辑标识
*/
function addMobileAttribute(path: Array<string>) {
const editorStore = (window as any)?.editorStore;

if (
editorStore.isMobileAloneEdit &&
editorStore.isMobile &&
!editorStore.isCodeEditing &&
!path.slice(-1)?.[0]?.startsWith?.('__') &&
path[0] !== 'body' &&
path[0] !== 'mobile'
) {
path.unshift('mobile');
}
}

/**
* 因为左侧是个不可变动的对象,所以先 copy 了对应的属性,再传给 DeepDiff.applyChange
*/
Expand All @@ -947,6 +965,11 @@ function applyChange(target: any, source: any, change: DiffChange) {
path.pop();
}

// pc 响应式页面,纯h5页面不处理
if (change.kind === 'E' || change.kind === 'N') {
addMobileAttribute(path);
}

path.reduce(
({target, source}, key) => {
const nextSource = source[key];
Expand All @@ -969,15 +992,9 @@ function applyChange(target: any, source: any, change: DiffChange) {
source
}
);
const editorStore = (window as any)?.editorStore;
// pc 响应式页面,纯h5页面不处理
if (
editorStore.isMobileAloneEdit &&
editorStore.isMobile &&
!editorStore.isCodeEditing
) {
change.path.unshift('mobile');
}

// 一定要要再添加一遍
addMobileAttribute(change.path);
DeepDiff.applyChange(target, source, change);
}

Expand Down

0 comments on commit 69905fc

Please sign in to comment.