fix: improve code

This commit is contained in:
chenos 2025-06-24 10:32:14 +08:00
parent 32488e898e
commit 443252a643
3 changed files with 44 additions and 18 deletions

View File

@ -85,7 +85,7 @@ export class APIResource<TData = any> extends FlowResource<TData> {
url: this.getURL(),
...this.getRefreshRequestOptions(),
});
this.setData(data?.data);
this.setData(data);
}
protected getRefreshRequestOptions() {

View File

@ -7,40 +7,54 @@
* For more information, please refer to: https://www.nocobase.com/agreement.
*/
import React, { useRef, useEffect } from 'react';
import { connect, mapProps } from '@formily/react';
import React, { useEffect, useRef } from 'react';
// CodeMirror imports
import { EditorView, basicSetup } from 'codemirror';
import { javascript } from '@codemirror/lang-javascript';
import { oneDark } from '@codemirror/theme-one-dark';
import { EditorState } from '@codemirror/state';
import { autocompletion, CompletionContext, CompletionResult } from '@codemirror/autocomplete';
import { javascript } from '@codemirror/lang-javascript';
import { lintGutter } from '@codemirror/lint';
import { EditorState } from '@codemirror/state';
import { oneDark } from '@codemirror/theme-one-dark';
import { basicSetup, EditorView } from 'codemirror';
import { createJavaScriptLinter } from './linter';
// 自定义自动补全函数
const createCustomCompletion = () => {
const contextVariables = [
{
label: 'getModelById',
type: 'function',
info: 'Get a model instance by its UID',
detail: '(uid: string) => FlowModel | null',
boost: 101,
},
{
label: 'element',
type: 'variable',
info: 'The DOM element to render into',
detail: 'HTMLElement',
boost: 99,
boost: 100,
},
{
label: 'ctx',
type: 'variable',
info: 'Flow context object',
detail: 'FlowContext',
boost: 98,
boost: 99,
},
{
label: 'model',
type: 'variable',
info: 'Current model instance',
detail: 'FlowModel',
boost: 98,
},
{
label: 'resource',
type: 'variable',
info: 'Current resource instance',
detail: 'APIResource',
boost: 97,
},
{

View File

@ -9,7 +9,7 @@
import { BlockModel } from '@nocobase/client';
import { APIResource } from '@nocobase/flow-engine';
import { Card, Spin } from 'antd';
import { Card, Skeleton, Spin } from 'antd';
import React, { createRef } from 'react';
import { CodeEditor } from './CodeEditor';
@ -30,13 +30,9 @@ export class LowcodeBlockFlowModel extends BlockModel {
return (
<Card>
{loading && (
<div style={{ textAlign: 'center', padding: '20px' }}>
<Spin />
<div style={{ marginTop: '8px' }}>Loading lowcode component...</div>
</div>
)}
<div ref={this.ref} style={{ width: '100%' }} />
<Spin spinning={loading} tip="Loading lowcode component...">
<div ref={this.ref} style={{ width: '100%' }} />
</Spin>
</Card>
);
}
@ -98,6 +94,9 @@ LowcodeBlockFlowModel.registerFlow({
steps: {
setMainResource: {
handler(ctx) {
if (ctx.model.resource) {
return;
}
ctx.model.resource = new APIResource();
ctx.model.resource.setAPIClient(ctx.globals.api);
},
@ -199,17 +198,30 @@ element.innerHTML = \`
});
};
const getModelById = (uid: string) => {
return ctx.globals.flowEngine.getModel(uid);
};
// Create a safe execution context for the code (as async function)
// Wrap user code in an async function
const wrappedCode = `
return (async function(element, ctx, model, requirejs, requireAsync, loadCSS) {
return (async function(element, ctx, model, resource, requirejs, requireAsync, loadCSS, getModelById) {
${params.code}
}).apply(this, arguments);
`;
const executionFunction = new Function(wrappedCode);
// Execute the code
await executionFunction(element, ctx, ctx.model, requirejs, requireAsync, loadCSS);
await executionFunction(
element,
ctx,
ctx.model,
ctx.model.resource,
requirejs,
requireAsync,
loadCSS,
getModelById,
);
ctx.model.setProps('loading', false);
} catch (error: any) {