@nocobase/client-v2 2.2.0-alpha.2 → 2.2.0-alpha.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/es/flow/models/blocks/form/value-runtime/runtime.d.ts +9 -0
- package/es/flow/models/blocks/js-block/JSBlock.d.ts +1 -0
- package/es/index.mjs +47 -47
- package/lib/index.js +58 -58
- package/package.json +7 -7
- package/src/components/form/__tests__/TypedVariableInput.test.tsx +2 -0
- package/src/flow/models/base/ActionModelCore.tsx +6 -4
- package/src/flow/models/base/__tests__/ActionModelCore.render.test.tsx +37 -0
- package/src/flow/models/blocks/form/value-runtime/__tests__/runtime.test.ts +87 -0
- package/src/flow/models/blocks/form/value-runtime/runtime.ts +91 -0
- package/src/flow/models/blocks/js-block/JSBlock.tsx +223 -2
- package/src/flow/models/blocks/js-block/__tests__/JSBlockModel.test.tsx +150 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nocobase/client-v2",
|
|
3
|
-
"version": "2.2.0-alpha.
|
|
3
|
+
"version": "2.2.0-alpha.3",
|
|
4
4
|
"license": "Apache-2.0",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"module": "es/index.mjs",
|
|
@@ -27,11 +27,11 @@
|
|
|
27
27
|
"@formily/antd-v5": "1.2.3",
|
|
28
28
|
"@formily/react": "^2.2.27",
|
|
29
29
|
"@formily/shared": "^2.2.27",
|
|
30
|
-
"@nocobase/evaluators": "2.2.0-alpha.
|
|
31
|
-
"@nocobase/flow-engine": "2.2.0-alpha.
|
|
32
|
-
"@nocobase/sdk": "2.2.0-alpha.
|
|
33
|
-
"@nocobase/shared": "2.2.0-alpha.
|
|
34
|
-
"@nocobase/utils": "2.2.0-alpha.
|
|
30
|
+
"@nocobase/evaluators": "2.2.0-alpha.3",
|
|
31
|
+
"@nocobase/flow-engine": "2.2.0-alpha.3",
|
|
32
|
+
"@nocobase/sdk": "2.2.0-alpha.3",
|
|
33
|
+
"@nocobase/shared": "2.2.0-alpha.3",
|
|
34
|
+
"@nocobase/utils": "2.2.0-alpha.3",
|
|
35
35
|
"ahooks": "^3.7.2",
|
|
36
36
|
"antd": "5.24.2",
|
|
37
37
|
"antd-style": "3.7.1",
|
|
@@ -46,5 +46,5 @@
|
|
|
46
46
|
"react-i18next": "^11.15.1",
|
|
47
47
|
"react-router-dom": "^6.30.1"
|
|
48
48
|
},
|
|
49
|
-
"gitHead": "
|
|
49
|
+
"gitHead": "e3ec19361fd7981af9fe2a7f24ad335025cbefcd"
|
|
50
50
|
}
|
|
@@ -64,6 +64,7 @@ describe('TypedVariableInput - constant rendering', () => {
|
|
|
64
64
|
const item = screen.getByText('True');
|
|
65
65
|
expect(item).toBeInTheDocument();
|
|
66
66
|
});
|
|
67
|
+
expect(screen.getByRole('button', { name: 'variable-switcher' }).className).not.toContain('ant-btn-primary');
|
|
67
68
|
});
|
|
68
69
|
|
|
69
70
|
it('renders the Null placeholder when value=null and nullable=true', async () => {
|
|
@@ -77,6 +78,7 @@ describe('TypedVariableInput - constant rendering', () => {
|
|
|
77
78
|
const nullInput = await screen.findByPlaceholderText('<Null>');
|
|
78
79
|
expect(nullInput).toBeInTheDocument();
|
|
79
80
|
expect(nullInput.getAttribute('readonly')).not.toBeNull();
|
|
81
|
+
expect(screen.getByRole('button', { name: 'variable-switcher' }).className).not.toContain('ant-btn-primary');
|
|
80
82
|
});
|
|
81
83
|
|
|
82
84
|
it('defaults undefined to the first constant type', async () => {
|
|
@@ -25,7 +25,7 @@ export function ActionWithoutPermission(props) {
|
|
|
25
25
|
const dataSourcePrefix = `${t(dataSource.displayName || dataSource.key)} > `;
|
|
26
26
|
const collectionPrefix = collection ? `${t(collection.title) || collection.name || collection.tableName} ` : '';
|
|
27
27
|
return `${dataSourcePrefix}${collectionPrefix}`;
|
|
28
|
-
}, []);
|
|
28
|
+
}, [collection, dataSource.displayName, dataSource.key, t]);
|
|
29
29
|
const { actionName } = props?.forbidden || model.forbidden;
|
|
30
30
|
const messageValue = useMemo(() => {
|
|
31
31
|
return t(
|
|
@@ -143,10 +143,11 @@ export class ActionModel<T extends DefaultStructure = DefaultStructure> extends
|
|
|
143
143
|
renderButton() {
|
|
144
144
|
const { iconOnly, ...props } = this.props;
|
|
145
145
|
const icon = this.getIcon() ? <Icon type={this.getIcon() as any} /> : undefined;
|
|
146
|
+
const titleContent = iconOnly && icon ? null : props.children || this.getTitle();
|
|
146
147
|
|
|
147
148
|
return (
|
|
148
149
|
<Button {...props} onClick={this.onClick.bind(this)} icon={icon}>
|
|
149
|
-
{
|
|
150
|
+
{titleContent}
|
|
150
151
|
</Button>
|
|
151
152
|
);
|
|
152
153
|
}
|
|
@@ -162,11 +163,12 @@ export class ActionModel<T extends DefaultStructure = DefaultStructure> extends
|
|
|
162
163
|
renderHiddenInConfig(): React.ReactNode | undefined {
|
|
163
164
|
const { iconOnly, ...props } = this.props;
|
|
164
165
|
const icon = this.getIcon() ? <Icon type={this.getIcon() as any} /> : undefined;
|
|
166
|
+
const titleContent = iconOnly && icon ? null : props.children || this.getTitle();
|
|
165
167
|
if (this.forbidden) {
|
|
166
168
|
return (
|
|
167
169
|
<ActionWithoutPermission>
|
|
168
170
|
<Button {...props} onClick={this.onClick.bind(this)} icon={icon} style={{ opacity: '0.3' }}>
|
|
169
|
-
{
|
|
171
|
+
{titleContent}
|
|
170
172
|
</Button>
|
|
171
173
|
</ActionWithoutPermission>
|
|
172
174
|
);
|
|
@@ -174,7 +176,7 @@ export class ActionModel<T extends DefaultStructure = DefaultStructure> extends
|
|
|
174
176
|
return (
|
|
175
177
|
<Tooltip title={this.context.t('The button is hidden and only visible when the UI Editor is active')}>
|
|
176
178
|
<Button {...props} onClick={this.onClick.bind(this)} icon={icon} style={{ opacity: '0.3' }}>
|
|
177
|
-
{
|
|
179
|
+
{titleContent}
|
|
178
180
|
</Button>
|
|
179
181
|
</Tooltip>
|
|
180
182
|
);
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { render, screen } from '@nocobase/test/client';
|
|
2
|
+
import { FlowEngine, FlowEngineProvider } from '@nocobase/flow-engine';
|
|
3
|
+
import { App, ConfigProvider } from 'antd';
|
|
4
|
+
import React from 'react';
|
|
5
|
+
import { describe, expect, it } from 'vitest';
|
|
6
|
+
import { ActionModel } from '../ActionModel';
|
|
7
|
+
|
|
8
|
+
class NoIconActionModel extends ActionModel {
|
|
9
|
+
defaultProps = {
|
|
10
|
+
type: 'link' as const,
|
|
11
|
+
title: 'Open details',
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
describe('ActionModel rendering', () => {
|
|
16
|
+
it('shows the title when iconOnly is true but no icon is configured', () => {
|
|
17
|
+
const engine = new FlowEngine();
|
|
18
|
+
engine.registerModels({ NoIconActionModel });
|
|
19
|
+
const model = engine.createModel<NoIconActionModel>({
|
|
20
|
+
use: 'NoIconActionModel',
|
|
21
|
+
props: {
|
|
22
|
+
title: 'Open details',
|
|
23
|
+
iconOnly: true,
|
|
24
|
+
},
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
render(
|
|
28
|
+
<FlowEngineProvider engine={engine}>
|
|
29
|
+
<ConfigProvider>
|
|
30
|
+
<App>{model.render()}</App>
|
|
31
|
+
</ConfigProvider>
|
|
32
|
+
</FlowEngineProvider>,
|
|
33
|
+
);
|
|
34
|
+
|
|
35
|
+
expect(screen.getByRole('button', { name: 'Open details' })).toBeInTheDocument();
|
|
36
|
+
});
|
|
37
|
+
});
|
|
@@ -127,6 +127,93 @@ function createFieldContext(runtime: FormValueRuntime) {
|
|
|
127
127
|
}
|
|
128
128
|
|
|
129
129
|
describe('FormValueRuntime (default rules)', () => {
|
|
130
|
+
it('builds draft snapshots from current user-edited values only', async () => {
|
|
131
|
+
const engineEmitter = new EventEmitter();
|
|
132
|
+
const blockEmitter = new EventEmitter();
|
|
133
|
+
const formStub = createFormStub({});
|
|
134
|
+
const dispatchEvent = vi.fn();
|
|
135
|
+
|
|
136
|
+
const blockModel = {
|
|
137
|
+
uid: 'form-user-edited-draft-snapshot',
|
|
138
|
+
flowEngine: { emitter: engineEmitter },
|
|
139
|
+
emitter: blockEmitter,
|
|
140
|
+
dispatchEvent,
|
|
141
|
+
getAclActionName: () => 'create',
|
|
142
|
+
} as ConstructorParameters<typeof FormValueRuntime>[0]['model'];
|
|
143
|
+
|
|
144
|
+
const runtime = new FormValueRuntime({ model: blockModel, getForm: () => formStub as unknown as FormInstance });
|
|
145
|
+
runtime.mount({ sync: true });
|
|
146
|
+
|
|
147
|
+
const blockCtx = createFieldContext(runtime);
|
|
148
|
+
await runtime.setFormValues(blockCtx, [{ path: ['fixed'], value: 'Fixed auto value' }], { source: 'system' });
|
|
149
|
+
await runtime.setFormValues(blockCtx, [{ path: ['defaultTitle'], value: 'Default auto value' }], {
|
|
150
|
+
source: 'default',
|
|
151
|
+
});
|
|
152
|
+
await runtime.setFormValues(blockCtx, [{ path: ['overrideTitle'], value: 'Override auto value' }], {
|
|
153
|
+
source: 'override',
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
expect(runtime.getUserEditedValuePatches()).toEqual([]);
|
|
157
|
+
expect(runtime.getUserEditedValuesSnapshot()).toEqual({});
|
|
158
|
+
|
|
159
|
+
await runtime.setFormValues(
|
|
160
|
+
blockCtx,
|
|
161
|
+
[
|
|
162
|
+
{ path: ['title'], value: 'Manual title' },
|
|
163
|
+
{ path: ['roles', 0, 'roleName'], value: 'Manual role' },
|
|
164
|
+
],
|
|
165
|
+
{ source: 'user' },
|
|
166
|
+
);
|
|
167
|
+
|
|
168
|
+
expect(runtime.getUserEditedValuePatches()).toEqual([
|
|
169
|
+
{ path: ['title'], value: 'Manual title' },
|
|
170
|
+
{ path: ['roles', 0, 'roleName'], value: 'Manual role' },
|
|
171
|
+
]);
|
|
172
|
+
expect(runtime.getUserEditedValuesSnapshot()).toEqual({
|
|
173
|
+
title: 'Manual title',
|
|
174
|
+
roles: [{ roleName: 'Manual role' }],
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
await runtime.setFormValues(blockCtx, [{ path: ['title'], value: 'Fixed title' }], { source: 'system' });
|
|
178
|
+
|
|
179
|
+
expect(runtime.getUserEditedValuesSnapshot()).toEqual({
|
|
180
|
+
roles: [{ roleName: 'Manual role' }],
|
|
181
|
+
});
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
it('omits non-user descendant values from parent user-edited draft patches', async () => {
|
|
185
|
+
const engineEmitter = new EventEmitter();
|
|
186
|
+
const blockEmitter = new EventEmitter();
|
|
187
|
+
const formStub = createFormStub({});
|
|
188
|
+
const dispatchEvent = vi.fn();
|
|
189
|
+
|
|
190
|
+
const blockModel = {
|
|
191
|
+
uid: 'form-user-edited-parent-draft-patch',
|
|
192
|
+
flowEngine: { emitter: engineEmitter },
|
|
193
|
+
emitter: blockEmitter,
|
|
194
|
+
dispatchEvent,
|
|
195
|
+
getAclActionName: () => 'create',
|
|
196
|
+
} as ConstructorParameters<typeof FormValueRuntime>[0]['model'];
|
|
197
|
+
|
|
198
|
+
const runtime = new FormValueRuntime({ model: blockModel, getForm: () => formStub as unknown as FormInstance });
|
|
199
|
+
runtime.mount({ sync: true });
|
|
200
|
+
|
|
201
|
+
const blockCtx = createFieldContext(runtime);
|
|
202
|
+
await runtime.setFormValues(
|
|
203
|
+
blockCtx,
|
|
204
|
+
[{ path: ['roles'], value: [{ roleName: 'Manual role', fixedRoleName: 'Initial fixed role' }] }],
|
|
205
|
+
{ source: 'user' },
|
|
206
|
+
);
|
|
207
|
+
await runtime.setFormValues(blockCtx, [{ path: ['roles', 0, 'fixedRoleName'], value: 'System fixed role' }], {
|
|
208
|
+
source: 'system',
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
expect(runtime.getUserEditedValuePatches()).toEqual([{ path: ['roles'], value: [{ roleName: 'Manual role' }] }]);
|
|
212
|
+
expect(runtime.getUserEditedValuesSnapshot()).toEqual({
|
|
213
|
+
roles: [{ roleName: 'Manual role' }],
|
|
214
|
+
});
|
|
215
|
+
});
|
|
216
|
+
|
|
130
217
|
it('skips object patches when values are unchanged', async () => {
|
|
131
218
|
const engineEmitter = new EventEmitter();
|
|
132
219
|
const blockEmitter = new EventEmitter();
|
|
@@ -38,6 +38,11 @@ type FormBlockModel = FlowModel & {
|
|
|
38
38
|
getAclActionName?: () => string;
|
|
39
39
|
};
|
|
40
40
|
|
|
41
|
+
export type FormValuePatch = {
|
|
42
|
+
path: NamePath;
|
|
43
|
+
value: unknown;
|
|
44
|
+
};
|
|
45
|
+
|
|
41
46
|
export class FormValueRuntime {
|
|
42
47
|
private readonly model: FormBlockModel;
|
|
43
48
|
private readonly getForm: () => FormInstance;
|
|
@@ -142,6 +147,49 @@ export class FormValueRuntime {
|
|
|
142
147
|
return this.getForm().getFieldsValue(true);
|
|
143
148
|
}
|
|
144
149
|
|
|
150
|
+
getUserEditedValuePatches(): FormValuePatch[] {
|
|
151
|
+
const snapshot = this.getFormValuesSnapshot();
|
|
152
|
+
if (!snapshot || typeof snapshot !== 'object') {
|
|
153
|
+
return [];
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
const patches: FormValuePatch[] = [];
|
|
157
|
+
const pathKeys = Array.from(this.userEditedSet).sort(
|
|
158
|
+
(a, b) => pathKeyToNamePath(a).length - pathKeyToNamePath(b).length,
|
|
159
|
+
);
|
|
160
|
+
|
|
161
|
+
for (const pathKey of pathKeys) {
|
|
162
|
+
if (!this.isCurrentUserEditedPath(pathKey)) {
|
|
163
|
+
continue;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
const namePath = pathKeyToNamePath(pathKey);
|
|
167
|
+
if (!namePath.length || !_.has(snapshot, namePath as any)) {
|
|
168
|
+
continue;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
const value = _.get(snapshot, namePath as any);
|
|
172
|
+
if (typeof value === 'undefined') {
|
|
173
|
+
continue;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
patches.push({
|
|
177
|
+
path: namePath,
|
|
178
|
+
value: this.omitNonUserDescendantValues(pathKey, this.toMirrorSnapshot(value)),
|
|
179
|
+
});
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
return patches;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
getUserEditedValuesSnapshot(): Record<string, unknown> {
|
|
186
|
+
const values: Record<string, unknown> = {};
|
|
187
|
+
for (const patch of this.getUserEditedValuePatches()) {
|
|
188
|
+
_.set(values, patch.path as any, patch.value);
|
|
189
|
+
}
|
|
190
|
+
return values;
|
|
191
|
+
}
|
|
192
|
+
|
|
145
193
|
private toMirrorSnapshot(value: any) {
|
|
146
194
|
const raw = isObservable(value) ? toJS(value) : value;
|
|
147
195
|
return _.cloneDeepWith(raw, (item) => {
|
|
@@ -1453,6 +1501,49 @@ export class FormValueRuntime {
|
|
|
1453
1501
|
return null;
|
|
1454
1502
|
}
|
|
1455
1503
|
|
|
1504
|
+
private findLatestWriteMeta(pathKey: string): FormValueWriteMeta | undefined {
|
|
1505
|
+
let latest: FormValueWriteMeta | undefined;
|
|
1506
|
+
const namePath = pathKeyToNamePath(pathKey);
|
|
1507
|
+
const prefix: NamePath = [];
|
|
1508
|
+
|
|
1509
|
+
for (let i = 0; i < namePath.length; i++) {
|
|
1510
|
+
prefix.push(namePath[i]);
|
|
1511
|
+
const meta = this.lastWriteMetaByPathKey.get(namePathToPathKey(prefix as any));
|
|
1512
|
+
if (!meta) {
|
|
1513
|
+
continue;
|
|
1514
|
+
}
|
|
1515
|
+
if (!latest || meta.writeSeq >= latest.writeSeq) {
|
|
1516
|
+
latest = meta;
|
|
1517
|
+
}
|
|
1518
|
+
}
|
|
1519
|
+
|
|
1520
|
+
return latest;
|
|
1521
|
+
}
|
|
1522
|
+
|
|
1523
|
+
private isCurrentUserEditedPath(pathKey: string) {
|
|
1524
|
+
const lastWrite = this.findLatestWriteMeta(pathKey);
|
|
1525
|
+
return !lastWrite || lastWrite.source === 'user';
|
|
1526
|
+
}
|
|
1527
|
+
|
|
1528
|
+
private omitNonUserDescendantValues(pathKey: string, value: unknown) {
|
|
1529
|
+
if (!value || typeof value !== 'object') {
|
|
1530
|
+
return value;
|
|
1531
|
+
}
|
|
1532
|
+
|
|
1533
|
+
const namePath = pathKeyToNamePath(pathKey);
|
|
1534
|
+
for (const childKey of this.lastWriteMetaByPathKey.keys()) {
|
|
1535
|
+
if (!this.isDescendantPathKey(childKey, pathKey)) {
|
|
1536
|
+
continue;
|
|
1537
|
+
}
|
|
1538
|
+
if (this.isCurrentUserEditedPath(childKey)) {
|
|
1539
|
+
continue;
|
|
1540
|
+
}
|
|
1541
|
+
_.unset(value as Record<string, unknown>, pathKeyToNamePath(childKey).slice(namePath.length) as any);
|
|
1542
|
+
}
|
|
1543
|
+
|
|
1544
|
+
return value;
|
|
1545
|
+
}
|
|
1546
|
+
|
|
1456
1547
|
private isDescendantPathKey(candidateKey: string, parentKey: string) {
|
|
1457
1548
|
if (!candidateKey || !parentKey || candidateKey === parentKey) return false;
|
|
1458
1549
|
const candidatePath = pathKeyToNamePath(candidateKey);
|
|
@@ -16,24 +16,238 @@ import { CodeEditor } from '../../../components/code-editor';
|
|
|
16
16
|
|
|
17
17
|
const NAMESPACE = 'client';
|
|
18
18
|
|
|
19
|
+
const getRootElement = (element: HTMLElement | null) => {
|
|
20
|
+
if (!element) return document.documentElement;
|
|
21
|
+
return (
|
|
22
|
+
(element.closest('.nb-block-grid') as HTMLElement | null) ||
|
|
23
|
+
(element.closest('.nb-page-wrapper') as HTMLElement | null) ||
|
|
24
|
+
(element.closest('.nb-page') as HTMLElement | null) ||
|
|
25
|
+
document.documentElement
|
|
26
|
+
);
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
const getOuterHeight = (element?: HTMLElement | null) => {
|
|
30
|
+
if (!element) return 0;
|
|
31
|
+
const rect = element.getBoundingClientRect();
|
|
32
|
+
const style = window.getComputedStyle(element);
|
|
33
|
+
const marginTop = parseFloat(style.marginTop) || 0;
|
|
34
|
+
const marginBottom = parseFloat(style.marginBottom) || 0;
|
|
35
|
+
return rect.height + marginTop + marginBottom;
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
const getPadding = (element: HTMLElement | null) => {
|
|
39
|
+
if (!element || element === document.documentElement) {
|
|
40
|
+
return { top: 0, bottom: 0 };
|
|
41
|
+
}
|
|
42
|
+
const style = window.getComputedStyle(element);
|
|
43
|
+
return {
|
|
44
|
+
top: parseFloat(style.paddingTop) || 0,
|
|
45
|
+
bottom: parseFloat(style.paddingBottom) || 0,
|
|
46
|
+
};
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
const getPageHeader = (root: HTMLElement) => {
|
|
50
|
+
const page = root.closest('.nb-page') as HTMLElement | null;
|
|
51
|
+
if (!page) return null;
|
|
52
|
+
return (
|
|
53
|
+
(page.querySelector('.ant-page-header') as HTMLElement | null) ||
|
|
54
|
+
(page.querySelector('.pageHeaderCss') as HTMLElement | null)
|
|
55
|
+
);
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
const getAddBlockContainer = (root: HTMLElement) => {
|
|
59
|
+
const button = root.querySelector('[data-flow-add-block]') as HTMLElement | null;
|
|
60
|
+
if (!button) return null;
|
|
61
|
+
return (button.parentElement as HTMLElement | null) || button;
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
function getValidPageTop(a: number, b: number) {
|
|
65
|
+
const aValid = a > 0;
|
|
66
|
+
const bValid = b > 0;
|
|
67
|
+
|
|
68
|
+
if (aValid) return a;
|
|
69
|
+
if (bValid) return b;
|
|
70
|
+
return 0;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const usePlainHostHeight = ({
|
|
74
|
+
height,
|
|
75
|
+
heightMode,
|
|
76
|
+
hostRef,
|
|
77
|
+
marginBlock,
|
|
78
|
+
}: {
|
|
79
|
+
height?: number;
|
|
80
|
+
heightMode?: string;
|
|
81
|
+
hostRef: React.RefObject<HTMLDivElement>;
|
|
82
|
+
marginBlock: number;
|
|
83
|
+
}) => {
|
|
84
|
+
const [fullHeight, setFullHeight] = React.useState<number>();
|
|
85
|
+
const updateFullHeight = React.useCallback(() => {
|
|
86
|
+
if (heightMode !== 'fullHeight' || typeof window === 'undefined') {
|
|
87
|
+
setFullHeight((prev) => (prev === undefined ? prev : undefined));
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
const hostEl = hostRef.current;
|
|
91
|
+
if (!hostEl) return;
|
|
92
|
+
const root = getRootElement(hostEl);
|
|
93
|
+
const hostRect = hostEl.getBoundingClientRect();
|
|
94
|
+
const rootRect = root === document.documentElement ? { top: 0 } : root.getBoundingClientRect();
|
|
95
|
+
const padding = getPadding(root);
|
|
96
|
+
const addBlockContainer = getAddBlockContainer(root);
|
|
97
|
+
const pageTop = rootRect.top + padding.top;
|
|
98
|
+
const topOffset = Math.max(0, hostRect.top - pageTop);
|
|
99
|
+
let bottomOffset = padding.bottom + marginBlock;
|
|
100
|
+
if (addBlockContainer) {
|
|
101
|
+
const gapBetween = marginBlock;
|
|
102
|
+
bottomOffset = gapBetween + getOuterHeight(addBlockContainer) + padding.bottom;
|
|
103
|
+
}
|
|
104
|
+
const nextHeight = Math.max(
|
|
105
|
+
0,
|
|
106
|
+
Math.floor(window.innerHeight - getValidPageTop(pageTop, 110) - topOffset - bottomOffset - 1),
|
|
107
|
+
);
|
|
108
|
+
setFullHeight((prev) => (prev === nextHeight ? prev : nextHeight));
|
|
109
|
+
}, [heightMode, hostRef, marginBlock]);
|
|
110
|
+
|
|
111
|
+
React.useLayoutEffect(() => {
|
|
112
|
+
updateFullHeight();
|
|
113
|
+
}, [updateFullHeight]);
|
|
114
|
+
|
|
115
|
+
React.useEffect(() => {
|
|
116
|
+
if (heightMode !== 'fullHeight' || typeof window === 'undefined') return;
|
|
117
|
+
const hostEl = hostRef.current;
|
|
118
|
+
if (!hostEl || typeof ResizeObserver === 'undefined') return;
|
|
119
|
+
const root = getRootElement(hostEl);
|
|
120
|
+
const pageHeader = getPageHeader(root);
|
|
121
|
+
const addBlockContainer = getAddBlockContainer(root);
|
|
122
|
+
const observer = new ResizeObserver(() => updateFullHeight());
|
|
123
|
+
observer.observe(hostEl);
|
|
124
|
+
if (root instanceof HTMLElement) {
|
|
125
|
+
observer.observe(root);
|
|
126
|
+
}
|
|
127
|
+
if (pageHeader) observer.observe(pageHeader);
|
|
128
|
+
if (addBlockContainer) observer.observe(addBlockContainer);
|
|
129
|
+
window.addEventListener('resize', updateFullHeight);
|
|
130
|
+
return () => {
|
|
131
|
+
observer.disconnect();
|
|
132
|
+
window.removeEventListener('resize', updateFullHeight);
|
|
133
|
+
};
|
|
134
|
+
}, [heightMode, hostRef, updateFullHeight]);
|
|
135
|
+
|
|
136
|
+
if (heightMode === 'specifyValue') {
|
|
137
|
+
return height;
|
|
138
|
+
}
|
|
139
|
+
if (heightMode === 'fullHeight') {
|
|
140
|
+
return fullHeight;
|
|
141
|
+
}
|
|
142
|
+
return null;
|
|
143
|
+
};
|
|
144
|
+
|
|
145
|
+
const JSBlockPlainHost = ({
|
|
146
|
+
uid,
|
|
147
|
+
className,
|
|
148
|
+
heightMode,
|
|
149
|
+
height,
|
|
150
|
+
style,
|
|
151
|
+
beforeContent,
|
|
152
|
+
afterContent,
|
|
153
|
+
contentRef,
|
|
154
|
+
marginBlock,
|
|
155
|
+
...rest
|
|
156
|
+
}: React.HTMLAttributes<HTMLDivElement> & {
|
|
157
|
+
uid: string;
|
|
158
|
+
heightMode?: string;
|
|
159
|
+
height?: number;
|
|
160
|
+
beforeContent?: React.ReactNode;
|
|
161
|
+
afterContent?: React.ReactNode;
|
|
162
|
+
contentRef: React.RefObject<HTMLDivElement>;
|
|
163
|
+
marginBlock: number;
|
|
164
|
+
}) => {
|
|
165
|
+
const hostRef = React.useRef<HTMLDivElement | null>(null);
|
|
166
|
+
const resolvedHeight = usePlainHostHeight({ height, heightMode, hostRef, marginBlock });
|
|
167
|
+
|
|
168
|
+
return (
|
|
169
|
+
<div
|
|
170
|
+
{...rest}
|
|
171
|
+
ref={hostRef}
|
|
172
|
+
id={`model-${uid}`}
|
|
173
|
+
className={className}
|
|
174
|
+
style={{
|
|
175
|
+
display: 'flex',
|
|
176
|
+
flexDirection: 'column',
|
|
177
|
+
height: resolvedHeight ?? undefined,
|
|
178
|
+
minHeight: 0,
|
|
179
|
+
overflow: 'auto',
|
|
180
|
+
...(style || {}),
|
|
181
|
+
}}
|
|
182
|
+
>
|
|
183
|
+
{beforeContent}
|
|
184
|
+
<div ref={contentRef} />
|
|
185
|
+
{afterContent}
|
|
186
|
+
</div>
|
|
187
|
+
);
|
|
188
|
+
};
|
|
189
|
+
|
|
19
190
|
export class JSBlockModel extends BlockModel {
|
|
20
191
|
// Avoid double-run on first mount; only rerun after remounts
|
|
21
192
|
private _mountedOnce = false;
|
|
193
|
+
|
|
194
|
+
get showBlockCard() {
|
|
195
|
+
return this.getStepParams('jsSettings', 'showBlockCard')?.showBlockCard !== false;
|
|
196
|
+
}
|
|
197
|
+
|
|
22
198
|
renderComponent(): React.ReactNode {
|
|
23
199
|
return <div ref={this.context.ref} />;
|
|
24
200
|
}
|
|
25
201
|
render() {
|
|
26
202
|
const decoratorProps = this.decoratorProps || {};
|
|
27
|
-
const {
|
|
203
|
+
const {
|
|
204
|
+
className,
|
|
205
|
+
id: _ignoredId,
|
|
206
|
+
title,
|
|
207
|
+
description,
|
|
208
|
+
showCard: _ignoredShowCard,
|
|
209
|
+
heightMode,
|
|
210
|
+
height,
|
|
211
|
+
style,
|
|
212
|
+
beforeContent,
|
|
213
|
+
afterContent,
|
|
214
|
+
...rest
|
|
215
|
+
} = decoratorProps;
|
|
28
216
|
const mergedClassName = ['code-block', className].filter(Boolean).join(' ');
|
|
29
217
|
|
|
218
|
+
if (!this.showBlockCard) {
|
|
219
|
+
return (
|
|
220
|
+
<JSBlockPlainHost
|
|
221
|
+
{...rest}
|
|
222
|
+
uid={this.uid}
|
|
223
|
+
className={mergedClassName}
|
|
224
|
+
heightMode={heightMode}
|
|
225
|
+
height={height}
|
|
226
|
+
style={style}
|
|
227
|
+
beforeContent={beforeContent}
|
|
228
|
+
afterContent={afterContent}
|
|
229
|
+
contentRef={this.context.ref}
|
|
230
|
+
marginBlock={this.context.themeToken?.marginBlock ?? 0}
|
|
231
|
+
/>
|
|
232
|
+
);
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
const cardProps = {
|
|
236
|
+
...rest,
|
|
237
|
+
height,
|
|
238
|
+
style,
|
|
239
|
+
...(beforeContent === undefined ? {} : { beforeContent }),
|
|
240
|
+
...(afterContent === undefined ? {} : { afterContent }),
|
|
241
|
+
};
|
|
242
|
+
|
|
30
243
|
return (
|
|
31
244
|
<BlockItemCard
|
|
32
245
|
id={`model-${this.uid}`}
|
|
33
246
|
className={mergedClassName}
|
|
34
247
|
title={title}
|
|
35
248
|
description={description}
|
|
36
|
-
{
|
|
249
|
+
heightMode={heightMode}
|
|
250
|
+
{...cardProps}
|
|
37
251
|
>
|
|
38
252
|
<div ref={this.context.ref} />
|
|
39
253
|
</BlockItemCard>
|
|
@@ -61,6 +275,13 @@ JSBlockModel.registerFlow({
|
|
|
61
275
|
key: 'jsSettings',
|
|
62
276
|
title: 'JavaScript settings',
|
|
63
277
|
steps: {
|
|
278
|
+
showBlockCard: {
|
|
279
|
+
title: tExpr('Show block card'),
|
|
280
|
+
uiMode: { type: 'switch', key: 'showBlockCard' },
|
|
281
|
+
defaultParams: {
|
|
282
|
+
showBlockCard: true,
|
|
283
|
+
},
|
|
284
|
+
},
|
|
64
285
|
runJs: {
|
|
65
286
|
title: tExpr('Write JavaScript'),
|
|
66
287
|
useRawParams: true,
|