fix(plugin-workflow): fix node form values when closed (#2978)

* fix(plugin-workflow): fix node form values when closed

* fix(plugin-workflow): fix manual schema config refresh
This commit is contained in:
Junyi 2023-11-06 23:50:25 +08:00 committed by GitHub
parent 10a1f65284
commit 2747e7640f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 228 additions and 198 deletions

View File

@ -2,7 +2,7 @@ import { css, cx } from '@emotion/css';
import { useForm } from '@formily/react'; import { useForm } from '@formily/react';
import { Input } from 'antd'; import { Input } from 'antd';
import { cloneDeep } from 'lodash'; import { cloneDeep } from 'lodash';
import React, { useEffect, useMemo, useRef, useState } from 'react'; import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react';
import sanitizeHTML from 'sanitize-html'; import sanitizeHTML from 'sanitize-html';
import { error } from '@nocobase/utils/client'; import { error } from '@nocobase/utils/client';
@ -228,6 +228,7 @@ export function TextArea(props) {
} }
const sel = window.getSelection?.(); const sel = window.getSelection?.();
if (sel) { if (sel) {
try {
const children = Array.from(current.childNodes) as HTMLElement[]; const children = Array.from(current.childNodes) as HTMLElement[];
if (children.length) { if (children.length) {
if (range[0] === -1) { if (range[0] === -1) {
@ -248,6 +249,9 @@ export function TextArea(props) {
nextRange.collapse(true); nextRange.collapse(true);
sel.removeAllRanges(); sel.removeAllRanges();
sel.addRange(nextRange); sel.addRange(nextRange);
} catch (ex) {
// console.error(ex);
}
} }
} else { } else {
const { lastChild } = current; const { lastChild } = current;
@ -262,7 +266,8 @@ export function TextArea(props) {
} }
}, [html]); }, [html]);
function onInsert(paths: string[]) { const onInsert = useCallback(
function (paths: string[]) {
const variable: string[] = paths.filter((key) => Boolean(key.trim())); const variable: string[] = paths.filter((key) => Boolean(key.trim()));
const { current } = inputRef; const { current } = inputRef;
if (!current || !variable) { if (!current || !variable) {
@ -279,22 +284,27 @@ export function TextArea(props) {
setChanged(true); setChanged(true);
setRange(getCurrentRange(current)); setRange(getCurrentRange(current));
onChange(getValue(current)); onChange(getValue(current));
} },
[keyLabelMap, onChange, range],
);
function onInput({ currentTarget }) { const onInput = useCallback(
function ({ currentTarget }) {
if (ime) { if (ime) {
return; return;
} }
setChanged(true); setChanged(true);
setRange(getCurrentRange(currentTarget)); setRange(getCurrentRange(currentTarget));
onChange(getValue(currentTarget)); onChange(getValue(currentTarget));
} },
[ime, onChange],
);
function onBlur({ currentTarget }) { const onBlur = useCallback(function ({ currentTarget }) {
setRange(getCurrentRange(currentTarget)); setRange(getCurrentRange(currentTarget));
} }, []);
function onKeyDown(ev) { const onKeyDown = useCallback(function (ev) {
if (ev.key === 'Enter') { if (ev.key === 'Enter') {
ev.preventDefault(); ev.preventDefault();
} }
@ -305,9 +315,10 @@ export function TextArea(props) {
// if (ev.key === 'Alt') { // if (ev.key === 'Alt') {
// console.debug(getCurrentRange(ev.currentTarget)); // console.debug(getCurrentRange(ev.currentTarget));
// } // }
} }, []);
function onPaste(ev) { const onPaste = useCallback(
function (ev) {
ev.preventDefault(); ev.preventDefault();
const input = ev.clipboardData.getData('text/html') || ev.clipboardData.getData('text'); const input = ev.clipboardData.getData('text/html') || ev.clipboardData.getData('text');
const sanitizedHTML = sanitizeHTML(input, { const sanitizedHTML = sanitizeHTML(input, {
@ -335,7 +346,9 @@ export function TextArea(props) {
pasteHTML(ev.currentTarget, sanitizedHTML); pasteHTML(ev.currentTarget, sanitizedHTML);
setRange(getCurrentRange(ev.currentTarget)); setRange(getCurrentRange(ev.currentTarget));
onChange(getValue(ev.currentTarget)); onChange(getValue(ev.currentTarget));
} },
[onChange],
);
const disabled = props.disabled || form.disabled; const disabled = props.disabled || form.disabled;

View File

@ -1,4 +1,4 @@
import React, { useCallback, useContext, useMemo, useState } from 'react'; import React, { useCallback, useContext, useEffect, useMemo, useState } from 'react';
import { DeleteOutlined } from '@ant-design/icons'; import { DeleteOutlined } from '@ant-design/icons';
import { cloneDeep } from 'lodash'; import { cloneDeep } from 'lodash';
import { createForm } from '@formily/core'; import { createForm } from '@formily/core';
@ -8,6 +8,7 @@ import { useTranslation } from 'react-i18next';
import { import {
ActionContextProvider, ActionContextProvider,
FormProvider,
SchemaComponent, SchemaComponent,
SchemaInitializerItemOptions, SchemaInitializerItemOptions,
css, css,
@ -273,6 +274,10 @@ export function JobButton() {
); );
} }
function useNodeFormProps() {
return { form: useForm() };
}
export function NodeDefaultView(props) { export function NodeDefaultView(props) {
const { data, children } = props; const { data, children } = props;
const compile = useCompile(); const compile = useCompile();
@ -292,15 +297,14 @@ export function NodeDefaultView(props) {
const values = cloneDeep(data.config); const values = cloneDeep(data.config);
return createForm({ return createForm({
initialValues: values, initialValues: values,
values,
disabled: workflow.executed, disabled: workflow.executed,
}); });
}, [data, workflow]); }, [data, workflow]);
const resetForm = useCallback( const resetForm = useCallback(
(changed) => { (editing) => {
setFormValueChanged(changed); setEditingConfig(editing);
if (!changed) { if (!editing) {
form.reset(); form.reset();
} }
}, },
@ -366,13 +370,17 @@ export function NodeDefaultView(props) {
<ActionContextProvider <ActionContextProvider
value={{ value={{
visible: editingConfig, visible: editingConfig,
setVisible: setEditingConfig, setVisible: resetForm,
formValueChanged, formValueChanged,
setFormValueChanged: resetForm, setFormValueChanged,
}} }}
> >
<FormProvider form={form}>
<SchemaComponent <SchemaComponent
scope={instruction.scope} scope={{
...instruction.scope,
useNodeFormProps,
}}
components={instruction.components} components={instruction.components}
schema={{ schema={{
type: 'void', type: 'void',
@ -418,7 +426,8 @@ export function NodeDefaultView(props) {
), ),
'x-decorator': 'FormV2', 'x-decorator': 'FormV2',
'x-decorator-props': { 'x-decorator-props': {
form, // form,
useProps: '{{ useNodeFormProps }}',
}, },
'x-component': 'Action.Drawer', 'x-component': 'Action.Drawer',
properties: { properties: {
@ -483,6 +492,7 @@ export function NodeDefaultView(props) {
}, },
}} }}
/> />
</FormProvider>
</ActionContextProvider> </ActionContextProvider>
</div> </div>
{children} {children}

View File

@ -1,8 +1,8 @@
import React, { useCallback, useContext, useEffect, useMemo, useState } from 'react';
import { FormLayout } from '@formily/antd-v5'; import { FormLayout } from '@formily/antd-v5';
import { createForm } from '@formily/core'; import { createForm } from '@formily/core';
import { FormProvider, ISchema, Schema, useFieldSchema, useForm } from '@formily/react'; import { FormProvider, ISchema, Schema, useFieldSchema, useForm } from '@formily/react';
import { Alert, Button, Modal, Space } from 'antd'; import { Alert, Button, Modal, Space } from 'antd';
import React, { useContext, useEffect, useMemo, useState } from 'react';
import { useTranslation } from 'react-i18next'; import { useTranslation } from 'react-i18next';
import { import {
@ -450,13 +450,9 @@ export function SchemaConfig({ value, onChange }) {
[], [],
); );
return ( const refresh = useCallback(
<SchemaComponentContext.Provider function refresh() {
value={{ // ctx.refresh?.();
...ctx,
designable: !workflow.executed,
refresh() {
ctx.refresh?.();
const { tabs } = lodash.get(schema.toJSON(), 'properties.drawer.properties') as { tabs: ISchema }; const { tabs } = lodash.get(schema.toJSON(), 'properties.drawer.properties') as { tabs: ISchema };
const forms = Array.from(manualFormTypes.getValues()).reduce( const forms = Array.from(manualFormTypes.getValues()).reduce(
(result, item: ManualFormType) => Object.assign(result, item.config.parseFormOptions(tabs)), (result, item: ManualFormType) => Object.assign(result, item.config.parseFormOptions(tabs)),
@ -466,6 +462,15 @@ export function SchemaConfig({ value, onChange }) {
onChange(tabs.properties); onChange(tabs.properties);
}, },
[form, onChange, schema],
);
return (
<SchemaComponentContext.Provider
value={{
...ctx,
designable: !workflow.executed,
refresh,
}} }}
> >
<SchemaInitializerProvider <SchemaInitializerProvider
@ -520,7 +525,9 @@ export function SchemaConfigButton(props) {
<Button type="primary" onClick={() => setVisible(true)}> <Button type="primary" onClick={() => setVisible(true)}>
{workflow.executed ? lang('View user interface') : lang('Configure user interface')} {workflow.executed ? lang('View user interface') : lang('Configure user interface')}
</Button> </Button>
<ActionContextProvider value={{ visible, setVisible }}>{props.children}</ActionContextProvider> <ActionContextProvider value={{ visible, setVisible, formValueChanged: false }}>
{props.children}
</ActionContextProvider>
</> </>
); );
} }