Skip to content

Commit

Permalink
1090 edit labels (#1091)
Browse files Browse the repository at this point in the history
* Remove not used function

* Refactor part of the workflow store and add basic node details panel

* Refactor the way we handle editedWorkflow

* Update the changelog
  • Loading branch information
andergmartins authored Dec 20, 2024
1 parent 6fca600 commit a673cb1
Show file tree
Hide file tree
Showing 12 changed files with 499 additions and 242 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- Add the option to change manually enabled workflows in the bulk edit screen (Issue #942).
- Add the "Save as current status" shortcut to the workflow editor (CTRL/CMD + S) (Issue #1084).
- Add new display settings to customize the shortcode output (Issue #203).
- Add new step setting field to customize the step label in the workflow editor (Issue #1090).

### Changed

Expand Down
407 changes: 265 additions & 142 deletions assets/js/workflowEditor.js

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions assets/jsx/workflow-editor/components/flow-editor/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ export const FlowEditor = (props) => {
removePlaceholderNodes,
setDraggingFromHandle,
setIsConnectingNodes,
unselectAll,
} = useDispatch(workflowStore);

const {
Expand Down Expand Up @@ -274,10 +275,12 @@ export const FlowEditor = (props) => {
);

const onNodesDelete = useCallback(() => {
unselectAll();
updateFlowInEditedWorkflow();
});

const onEdgesDelete = useCallback(() => {
unselectAll();
updateFlowInEditedWorkflow();
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,15 @@ export const KeyboardShortcuts = () => {
const { registerShortcut } = useDispatch(shortcutStore);

const {
isSaving,
isEditedWorkflowSaveable,
} = useSelect(
(select) => {
const {
isSavingWorkflow,
isAutosavingWorkflow,
isEditedWorkflowSaveable,
} = select(workflowStore);

const isSaving = isSavingWorkflow() || isAutosavingWorkflow();

return {
isSaving,
isEditedWorkflowSaveable,
};
},
[]
Expand Down Expand Up @@ -179,7 +176,7 @@ export const KeyboardShortcuts = () => {
useShortcut(SHORTCUT_SAVE_AS_CURRENT_STATUS, (e) => {
e.preventDefault();

if (isSaving) {
if (! isEditedWorkflowSaveable) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import ObjectGroupIcon from "../icons/object-group";
import LinesLeaningIcon from "../icons/lines-leaning";
import NodeDevInfoPanel from "../node-dev-info-panel";
import useScrollToTop from "../scrolled-to-top";
import NodeDetailsPanel from "./node-details-panel";

export const NodeInspector = () => {
const {
Expand Down Expand Up @@ -145,6 +146,8 @@ export const NodeInspector = () => {
<>
<NodeInspectorCard node={selectedNode} />

<NodeDetailsPanel node={selectedNode} />

{nodeHasSettings && (
<NodeSettingsPanel node={selectedNode} />
)}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { PanelRow, TextControl } from "@wordpress/components";
import { __ } from "@wordpress/i18n";
import PersistentPanelBody from "../persistent-panel-body";
import { useSelect, useDispatch } from "@wordpress/data";
import { store as workflowStore } from '../workflow-store';
import { store as editorStore } from '../editor-store';
import { useCallback } from "@wordpress/element";
import Text from "../data-fields/text";

export const NodeDetailsPanel = ({ node }) => {
const {
getNodeTypeByName,
} = useSelect((select) => {
return {
getNodeTypeByName: select(editorStore).getNodeTypeByName,
};
});

const {
updateNode,
} = useDispatch(workflowStore);

const nodeType = getNodeTypeByName(node.data.name);

const onChangeLabel = useCallback((name, value) => {
const newNode = {
id: node.id,
data: {
label: value,
},
};

updateNode(newNode);
}, [node, updateNode]);

return (
<PersistentPanelBody title={__("Step Details", "post-expirator")}>
<PanelRow>
<Text
name="label"
label={__("Step Label", "post-expirator")}
defaultValue={node.data.label || ''}
settings={{
placeholder: nodeType.label,
}}
onChange={onChangeLabel}
description={__("This is the label that will be displayed in the workflow editor.", "post-expirator")}
/>
</PanelRow>
</PersistentPanelBody>
);
};

export default NodeDetailsPanel;
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@ export const NodeSettingsPanel = ({ node }) => {
} = useDispatch(workflowStore);

const onChangeSetting = (fieldName, value) => {
if (! node.data?.settings) {
node.data.settings = {};
}

node.data.settings = {
...node.data.settings,
[fieldName]: value
}
const newNode = {
id: node.id,
data: {
settings: {
[fieldName]: value,
},
},
};

updateNode(node);
updateNode(newNode);
};

let nodeSettings = node.data?.settings;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export const GenericNode = memo(({ id, data, isConnectable, selected, nodeTypeIc
};
}

const nodeLabel = nodeType.label || data.label || __('Node', 'post-expirator');
const nodeLabel = data.label || nodeType.label || __('Node', 'post-expirator');
const nodeClassName = nodeType?.className || 'react-flow__node-genericNode';

let targetHandles = null;
Expand Down
62 changes: 31 additions & 31 deletions assets/jsx/workflow-editor/components/workflow-store/actions.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,27 +57,28 @@ function addWorkflowIdToUrl(workflowId) {
window.history.pushState({}, '', `?page=future_workflow_editor&workflow=${parseInt(workflowId)}`);
}

// FIXME: This is not working as expected. The state we get from the store is not updated if the
// inspector is open or was not closed after the changes.
export function* saveAsDraft({ screenshot } = {}) {
yield {type: 'SAVE_AS_DRAFT_START'};

try {
const wasNewWorkflow = yield select(STORE_NAME).isNewWorkflow();
const editedWorkflow = yield select(STORE_NAME).getWorkflow();

yield dispatch(STORE_NAME).setEditedWorkflowAttribute('status', 'draft');

const editedWorkflow = yield select(STORE_NAME).getEditedWorkflow();

if (screenshot) {
editedWorkflow.screenshot = screenshot;
}
const workflowToSave = {
...editedWorkflow,
status: 'draft',
...(screenshot ? { screenshot } : {})
};

const newWorkflow = yield apiFetch({
path: `${apiUrl}/workflows/${parseInt(editedWorkflow.id)}`,
method: 'PUT',
headers: {
'X-WP-Nonce': nonce,
},
body: JSON.stringify(editedWorkflow),
body: JSON.stringify(workflowToSave),
});

// Add the workflow id to the url, keeping current state in the history
Expand All @@ -104,7 +105,7 @@ export function* saveAsDraft({ screenshot } = {}) {
}

export function* saveAsCurrentStatus({ screenshot } = {}) {
const editedWorkflow = yield select(STORE_NAME).getEditedWorkflow();
const editedWorkflow = yield select(STORE_NAME).getWorkflow();

if (editedWorkflow.status === 'auto-draft') {
yield saveAsDraft({ screenshot });
Expand All @@ -115,19 +116,20 @@ export function* saveAsCurrentStatus({ screenshot } = {}) {

try {
const wasNewWorkflow = yield select(STORE_NAME).isNewWorkflow();
const editedWorkflow = yield select(STORE_NAME).getEditedWorkflow();
const editedWorkflow = yield select(STORE_NAME).getWorkflow();

if (screenshot) {
editedWorkflow.screenshot = screenshot;
}
const workflowToSave = {
...editedWorkflow,
...(screenshot ? { screenshot } : {}),
};

const newWorkflow = yield apiFetch({
path: `${apiUrl}/workflows/${parseInt(editedWorkflow.id)}`,
method: 'PUT',
headers: {
'X-WP-Nonce': nonce,
},
body: JSON.stringify(editedWorkflow),
body: JSON.stringify(workflowToSave),
});

// Add the workflow id to the url, keeping current state in the history
Expand Down Expand Up @@ -158,22 +160,21 @@ export function* publishWorkflow({ screenshot } = {}) {

try {
const wasNewWorkflow = yield select(STORE_NAME).isNewWorkflow();
const editedWorkflow = yield select(STORE_NAME).getWorkflow();

yield dispatch(STORE_NAME).setEditedWorkflowAttribute('status', 'publish');

const editedWorkflow = yield select(STORE_NAME).getEditedWorkflow();

if (screenshot) {
editedWorkflow.screenshot = screenshot;
}
const workflowToSave = {
...editedWorkflow,
status: 'publish',
...(screenshot ? { screenshot } : {}),
};

const newWorkflow = yield apiFetch({
path: `${apiUrl}/workflows/${parseInt(editedWorkflow.id)}`,
method: 'PUT',
headers: {
'X-WP-Nonce': nonce,
},
body: JSON.stringify(editedWorkflow),
body: JSON.stringify(workflowToSave),
});

// Add the workflow id to the url, keeping current state in the history
Expand Down Expand Up @@ -204,22 +205,21 @@ export function* switchToDraft({ screenshot } = {}) {

try {
const wasNewWorkflow = yield select(STORE_NAME).isNewWorkflow();
const editedWorkflow = yield select(STORE_NAME).getWorkflow();

yield dispatch(STORE_NAME).setEditedWorkflowAttribute('status', 'draft');

const editedWorkflow = yield select(STORE_NAME).getEditedWorkflow();

if (screenshot) {
editedWorkflow.screenshot = screenshot;
}
const workflowToSave = {
...editedWorkflow,
status: 'draft',
...(screenshot ? { screenshot } : {}),
};

const newWorkflow = yield apiFetch({
path: `${apiUrl}/workflows/${parseInt(editedWorkflow.id)}`,
method: 'PUT',
headers: {
'X-WP-Nonce': nonce,
},
body: JSON.stringify(editedWorkflow),
body: JSON.stringify(workflowToSave),
});

// Add the workflow id to the url, keeping current state in the history
Expand Down Expand Up @@ -314,7 +314,7 @@ export const setEditedWorkflowAttribute = (key, value) => {
export function* deleteWorkflow () {
yield {type: 'DELETE_WORKFLOW_START'};

const editedWorkflow = yield select(STORE_NAME).getEditedWorkflow();
const editedWorkflow = yield select(STORE_NAME).getWorkflow();

try {
const newWorkflow = yield apiFetch({
Expand Down
Loading

0 comments on commit a673cb1

Please sign in to comment.