Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix open popup algo #399

Merged
merged 2 commits into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 40 additions & 13 deletions src/Routes/Tables/Algorithms/AlgorithmActions.react.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useCallback, useRef } from 'react';
import React, { useCallback, useRef, useState } from 'react';
import PropTypes from 'prop-types';

import {
Expand Down Expand Up @@ -36,6 +36,8 @@ const deleteConfirmAction = action => {
const overlayStyle = { width: `50ch` };

const AlgorithmActions = ({ record }) => {
const [openPopupRun, setOpenPopupRun] = useState(false);
const [openPopupRunDebug, setOpenPopupRunDebug] = useState(false);
const { goTo } = usePath();
const { builds, ...algorithm } = record;
const { name } = algorithm;
Expand All @@ -56,20 +58,20 @@ const AlgorithmActions = ({ record }) => {
// [applyAlgorithm]
// );

const onEdit = useCallback(() => goTo.edit({ nextAlgorithmId: name }), [
goTo,
name,
]);
const onEdit = useCallback(
() => goTo.edit({ nextAlgorithmId: name }),
[goTo, name]
);

const onClickDelete = useCallback(
() => deleteConfirmAction(() => deleteAlgorithm(name)),
[deleteAlgorithm, name]
);

const onRun = useCallback(input => runAlgorithm({ name, input }), [
runAlgorithm,
name,
]);
const onRun = useCallback(
input => runAlgorithm({ name, input }),
[runAlgorithm, name]
);

const onDebug = useCallback(
input => runAlgorithm({ name, input, debug: true }),
Expand All @@ -88,6 +90,24 @@ const AlgorithmActions = ({ record }) => {
e.stopPropagation();
}, []);

const clickOnRunAlg = useCallback(() => {
onRun();
setOpenPopupRun(false);
}, [onRun]);

const handleOpenChange = useCallback(newOpen => {
setOpenPopupRun(newOpen);
}, []);

const clickOnRunDebug = useCallback(() => {
onDebug();
setOpenPopupRunDebug(false);
}, [onDebug]);

const handleOpenChangeDebug = useCallback(newOpen => {
setOpenPopupRunDebug(newOpen);
}, []);

return (
<div
ref={container}
Expand All @@ -102,8 +122,13 @@ const AlgorithmActions = ({ record }) => {
content={popOverContentRun}
getPopupContainer={setPopupContainer}
mouseLeaveDelay={0.3}
mouseEnterDelay={1}>
<Button icon={<PlayCircleOutlined />} onClick={() => onRun()} />
mouseEnterDelay={2}
open={openPopupRun}
onOpenChange={handleOpenChange}>
<Button
icon={<PlayCircleOutlined />}
onClick={() => clickOnRunAlg()}
/>
</Popover>
<Popover
overlayStyle={overlayStyle}
Expand All @@ -112,8 +137,10 @@ const AlgorithmActions = ({ record }) => {
content={popOverContentDebug}
getPopupContainer={setPopupContainer}
mouseLeaveDelay={0.3}
mouseEnterDelay={1}>
<Button icon={<BugOutlined />} onClick={() => onDebug()} />
mouseEnterDelay={2}
open={openPopupRunDebug}
onOpenChange={handleOpenChangeDebug}>
<Button icon={<BugOutlined />} onClick={() => clickOnRunDebug()} />
</Popover>
<Tooltip title="edit algorithm">
<Button icon={<EditOutlined />} onClick={onEdit} />
Expand Down
59 changes: 49 additions & 10 deletions src/components/common/SignBoard.react.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import React, { useRef, useState } from 'react';
import PropTypes from 'prop-types';
import { Form, Button, Space, Popover, Divider, Row, Col, Input } from 'antd';
import _ from 'lodash';
Expand All @@ -23,11 +23,17 @@ const SignBoard = ({
restField,
}) => {
const { TextArea } = Input;
const inputRef = useRef(null);
const [cursorPosition, setCursorPosition] = useState(null);

const styleButtons = {
flexWrap: 'wrap',
};

const setPosition = () => {
setCursorPosition(inputRef.current.input.selectionStart);
};

const getValuesInput = nameKey => {
const fields = form.getFieldsValue();
const currentNameField = ['listKeyValue', ...nameRef, ...nameKey];
Expand All @@ -38,20 +44,49 @@ const SignBoard = ({

const writeCharsToInput = (nameKey, value) => {
const { fields, currentNameField, lastValue } = getValuesInput(nameKey);
_.set(fields, currentNameField, `${lastValue} ${value}`);
const updatedValue =
lastValue.slice(0, cursorPosition) +
value +
lastValue.slice(cursorPosition);

_.set(fields, currentNameField, updatedValue);

// new postion cursor
const newProstionCursor = cursorPosition + value.length;
setCursorPosition(newProstionCursor);

inputRef.current.focus();
setTimeout(() => {
inputRef.current.setSelectionRange(newProstionCursor, newProstionCursor);
}, 2);

form.setFieldsValue(fields);
onChange();
};

const DeleteCharsInInput = nameKey => {
const { fields, currentNameField, lastValue } = getValuesInput(nameKey);
_.set(
fields,
currentNameField,
lastValue.substring(0, lastValue.lastIndexOf(' '))
);
form.setFieldsValue(fields);
onChange();
if (cursorPosition > 0) {
const { fields, currentNameField, lastValue } = getValuesInput(nameKey);

const newProstionCursor = cursorPosition - 1;
setCursorPosition(newProstionCursor);

const updatedValue =
lastValue.slice(0, newProstionCursor) + lastValue.slice(cursorPosition);

inputRef.current.focus();
setTimeout(() => {
inputRef.current.setSelectionRange(
newProstionCursor,
newProstionCursor
);
}, 2);

_.set(fields, currentNameField, updatedValue);

form.setFieldsValue(fields);
onChange();
}
};

const title = nameKey => {
Expand Down Expand Up @@ -110,19 +145,23 @@ const SignBoard = ({
rules={msgRules}
initialValue=""
onChange={onChange}
onClick={setPosition}
key={`inputTextItem${indexKey}`}>
{type === 'text' ? (
<Input
ref={inputRef}
key={`inputText${indexKey}`}
style={{ width }}
placeholder={placeholder}
autoComplete="off"
onKeyUp={setPosition}
/>
) : (
<TextArea
key={`TextArea${indexKey}`}
style={{ width }}
placeholder={placeholder}
onKeyUp={setPosition}
/>
)}
</Form.Item>
Expand Down
Loading