mirror of
https://gitee.com/nocobase/nocobase.git
synced 2025-07-02 11:12:20 +08:00
fix: improve code
This commit is contained in:
parent
32488e898e
commit
443252a643
@ -85,7 +85,7 @@ export class APIResource<TData = any> extends FlowResource<TData> {
|
|||||||
url: this.getURL(),
|
url: this.getURL(),
|
||||||
...this.getRefreshRequestOptions(),
|
...this.getRefreshRequestOptions(),
|
||||||
});
|
});
|
||||||
this.setData(data?.data);
|
this.setData(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected getRefreshRequestOptions() {
|
protected getRefreshRequestOptions() {
|
||||||
|
@ -7,40 +7,54 @@
|
|||||||
* For more information, please refer to: https://www.nocobase.com/agreement.
|
* For more information, please refer to: https://www.nocobase.com/agreement.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import React, { useRef, useEffect } from 'react';
|
|
||||||
import { connect, mapProps } from '@formily/react';
|
import { connect, mapProps } from '@formily/react';
|
||||||
|
import React, { useEffect, useRef } from 'react';
|
||||||
|
|
||||||
// CodeMirror imports
|
// 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 { autocompletion, CompletionContext, CompletionResult } from '@codemirror/autocomplete';
|
||||||
|
import { javascript } from '@codemirror/lang-javascript';
|
||||||
import { lintGutter } from '@codemirror/lint';
|
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';
|
import { createJavaScriptLinter } from './linter';
|
||||||
|
|
||||||
// 自定义自动补全函数
|
// 自定义自动补全函数
|
||||||
const createCustomCompletion = () => {
|
const createCustomCompletion = () => {
|
||||||
const contextVariables = [
|
const contextVariables = [
|
||||||
|
{
|
||||||
|
label: 'getModelById',
|
||||||
|
type: 'function',
|
||||||
|
info: 'Get a model instance by its UID',
|
||||||
|
detail: '(uid: string) => FlowModel | null',
|
||||||
|
boost: 101,
|
||||||
|
},
|
||||||
{
|
{
|
||||||
label: 'element',
|
label: 'element',
|
||||||
type: 'variable',
|
type: 'variable',
|
||||||
info: 'The DOM element to render into',
|
info: 'The DOM element to render into',
|
||||||
detail: 'HTMLElement',
|
detail: 'HTMLElement',
|
||||||
boost: 99,
|
boost: 100,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
label: 'ctx',
|
label: 'ctx',
|
||||||
type: 'variable',
|
type: 'variable',
|
||||||
info: 'Flow context object',
|
info: 'Flow context object',
|
||||||
detail: 'FlowContext',
|
detail: 'FlowContext',
|
||||||
boost: 98,
|
boost: 99,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
label: 'model',
|
label: 'model',
|
||||||
type: 'variable',
|
type: 'variable',
|
||||||
info: 'Current model instance',
|
info: 'Current model instance',
|
||||||
detail: 'FlowModel',
|
detail: 'FlowModel',
|
||||||
|
boost: 98,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
label: 'resource',
|
||||||
|
type: 'variable',
|
||||||
|
info: 'Current resource instance',
|
||||||
|
detail: 'APIResource',
|
||||||
boost: 97,
|
boost: 97,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -9,7 +9,7 @@
|
|||||||
|
|
||||||
import { BlockModel } from '@nocobase/client';
|
import { BlockModel } from '@nocobase/client';
|
||||||
import { APIResource } from '@nocobase/flow-engine';
|
import { APIResource } from '@nocobase/flow-engine';
|
||||||
import { Card, Spin } from 'antd';
|
import { Card, Skeleton, Spin } from 'antd';
|
||||||
import React, { createRef } from 'react';
|
import React, { createRef } from 'react';
|
||||||
import { CodeEditor } from './CodeEditor';
|
import { CodeEditor } from './CodeEditor';
|
||||||
|
|
||||||
@ -30,13 +30,9 @@ export class LowcodeBlockFlowModel extends BlockModel {
|
|||||||
|
|
||||||
return (
|
return (
|
||||||
<Card>
|
<Card>
|
||||||
{loading && (
|
<Spin spinning={loading} tip="Loading lowcode component...">
|
||||||
<div style={{ textAlign: 'center', padding: '20px' }}>
|
|
||||||
<Spin />
|
|
||||||
<div style={{ marginTop: '8px' }}>Loading lowcode component...</div>
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
<div ref={this.ref} style={{ width: '100%' }} />
|
<div ref={this.ref} style={{ width: '100%' }} />
|
||||||
|
</Spin>
|
||||||
</Card>
|
</Card>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ -98,6 +94,9 @@ LowcodeBlockFlowModel.registerFlow({
|
|||||||
steps: {
|
steps: {
|
||||||
setMainResource: {
|
setMainResource: {
|
||||||
handler(ctx) {
|
handler(ctx) {
|
||||||
|
if (ctx.model.resource) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
ctx.model.resource = new APIResource();
|
ctx.model.resource = new APIResource();
|
||||||
ctx.model.resource.setAPIClient(ctx.globals.api);
|
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)
|
// Create a safe execution context for the code (as async function)
|
||||||
// Wrap user code in an async function
|
// Wrap user code in an async function
|
||||||
const wrappedCode = `
|
const wrappedCode = `
|
||||||
return (async function(element, ctx, model, requirejs, requireAsync, loadCSS) {
|
return (async function(element, ctx, model, resource, requirejs, requireAsync, loadCSS, getModelById) {
|
||||||
${params.code}
|
${params.code}
|
||||||
}).apply(this, arguments);
|
}).apply(this, arguments);
|
||||||
`;
|
`;
|
||||||
const executionFunction = new Function(wrappedCode);
|
const executionFunction = new Function(wrappedCode);
|
||||||
|
|
||||||
// Execute the code
|
// 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);
|
ctx.model.setProps('loading', false);
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user