-
Notifications
You must be signed in to change notification settings - Fork 2
custom component for path input #77
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
Closed
Closed
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
2c2a21c
custom component for path input
kamil-orwat-vmltech 76be575
code cleanup and small fixes
kamil-orwat-vmltech ac3baf5
code cleanup and small fixes
kamil-orwat-vmltech 4e72bb8
cleanup and optimization
kamil-orwat-vmltech 3a9fcea
fixed margin and height
kamil-orwat-vmltech b4ad281
allowing users to enter rootPath = '/'
kamil-orwat-vmltech 4ae1e63
Fix proposition for unknown userId
kamil-orwat-vmltech 5b5c658
Additional check in input
kamil-orwat-vmltech 5c8c435
Merge remote-tracking branch 'origin/main' into path-input
krystian-panek-vmltech 4ed689d
Merge branch 'path-input' of https://github.com/wttech/acm into path-…
kamil-orwat-vmltech e5abd53
displaying selected value
kamil-orwat-vmltech File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,5 +8,6 @@ public enum ArgumentType { | |
STRING, | ||
TEXT, | ||
SELECT, | ||
MULTISELECT | ||
MULTISELECT, | ||
PATH | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
core/src/main/java/com/vml/es/aem/acm/core/code/arg/PathArgument.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.vml.es.aem.acm.core.code.arg; | ||
|
||
import com.vml.es.aem.acm.core.code.Argument; | ||
import com.vml.es.aem.acm.core.code.ArgumentType; | ||
|
||
public class PathArgument extends Argument<String> { | ||
|
||
private String rootPath; | ||
|
||
public PathArgument(String name) { | ||
super(name, ArgumentType.PATH); | ||
} | ||
|
||
public String getRootPath() { | ||
return rootPath; | ||
} | ||
|
||
public void setRootPath(String rootPath) { | ||
this.rootPath = rootPath; | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
...tent/src/main/content/jcr_root/conf/acm/settings/snippet/available/core/argument/path.yml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
group: Argument | ||
name: argument_path | ||
content: | | ||
args.path("${1:name}") { label = "${2:label}"; value = ${3:value}; rootPath = "${4:rootPath}" } | ||
documentation: | | ||
An argument that allows the user to select a path.<br> | ||
The path can be relative to the current resource or absolute. | ||
<br><br> | ||
<b>Example:</b> | ||
<pre> | ||
args.path("path") { label = "Path"; value = "/content"; rootPath = "/content" } | ||
</pre> | ||
<br> | ||
<b>Note the fact that both rootpath and value can't end with '/'</b> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
.label { | ||
color: var(--spectrum-gray-700); | ||
padding-top: var(--spectrum-global-dimension-size-50); | ||
padding-bottom: var(--spectrum-global-dimension-size-65); | ||
font-size: var(--spectrum-global-dimension-font-size-75); | ||
font-weight: var(--spectrum-global-font-weight-regular); | ||
line-height: var(--spectrum-global-font-line-height-small); | ||
font-family: var(--spectrum-global-font-family-base), sans-serif; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,197 @@ | ||
import { SpectrumTreeViewProps, Text, TreeView, TreeViewItem, TreeViewItemContent } from '@adobe/react-spectrum'; | ||
import { Selection } from '@react-types/shared/src/selection'; | ||
import React, {forwardRef, Ref, useCallback, useEffect, useState} from 'react'; | ||
import { apiRequest } from '../utils/api.ts'; | ||
import { AssistCodeOutput, AssistCodeOutputSuggestion } from '../utils/api.types.ts'; | ||
import styles from './PathInput.module.css'; | ||
import codeArgumentStyles from './CodeArgumentInput.module.css'; | ||
|
||
interface Node { | ||
name: string; | ||
children: Node[] | null; | ||
} | ||
|
||
interface IPathInput extends SpectrumTreeViewProps<string> { | ||
rootPath: string; | ||
onChange: (paths: string) => void; | ||
errorMessage: string | undefined; | ||
isInvalid: boolean; | ||
label: string; | ||
value: string; | ||
} | ||
|
||
export const PathInput = forwardRef(function PathInput({ rootPath = "", onChange, label, value, ...props }: IPathInput, ref: Ref<HTMLDivElement>) { | ||
const { getPathCompletion } = useRequestPathCompletion(); | ||
const [loadedPaths, setLoadedPaths] = useState<Set<string>>(new Set()); | ||
const [selected, setSelected] = useState<Set<string>>(new Set()); | ||
const [expanded, setExpanded] = useState<Set<string>>(new Set()); | ||
const [items, setItems] = useState<Node>({ | ||
name: rootPath == "/" ? "" : rootPath, | ||
children: null, | ||
}); | ||
|
||
const onSelectionChange = useCallback((keys: Selection) => { | ||
const newSelection = new Set([...keys].map((key) => key.toString())); | ||
let selectedValue = [...newSelection][0]; | ||
if (selected.has(selectedValue)) { | ||
// @ts-expect-error: Allow assigning null to selectedValue to clear the selection | ||
selectedValue = null; | ||
} | ||
setTimeout(() => { | ||
// Offload the onChange call to avoid blocking the UI | ||
setSelected(new Set([selectedValue])); | ||
onChange(selectedValue); | ||
}, 0) | ||
}, []); | ||
|
||
const translateSuggestion = (suggestion: AssistCodeOutputSuggestion): Node => { | ||
const name = suggestion.it.split('/').at(-1); | ||
return { name: name ? name : suggestion.it, children: null }; | ||
}; | ||
|
||
const findAndUpdateNodeFromPath = (path: string, newChildren: Node[]) => { | ||
const updateTree = (node: Node, pathParts: string[]): Node => { | ||
if (pathParts.length === 0) { | ||
return { ...node, children: newChildren.length > 0 ? newChildren : [] }; | ||
} | ||
const [currentPart, ...remainingParts] = pathParts; | ||
const children = node.children || []; | ||
let child = children.find((child) => child.name === currentPart); | ||
if (!child) { | ||
child = { name: currentPart, children: [] }; | ||
children.push(child); | ||
} | ||
|
||
return { | ||
...node, | ||
children: children.map((c) => (c.name === currentPart ? updateTree(c, remainingParts) : c)), | ||
}; | ||
}; | ||
const rootName = items.name; | ||
const normalizedPath = path.startsWith(rootName) ? path.slice(rootName.length) : path; | ||
const parts = normalizedPath.split('/').filter(Boolean); | ||
setItems((prevItems) => updateTree(prevItems, parts)); | ||
}; | ||
|
||
const loadSuggestions = useCallback(async (path: string) => { | ||
await getPathCompletion(path) | ||
.then((data) => data.suggestions.map(translateSuggestion)) | ||
.then((nodes) => { | ||
findAndUpdateNodeFromPath(path, nodes); | ||
}) | ||
.catch(error => { | ||
console.error("Error loading suggestions:", error); | ||
}); | ||
}, []) | ||
|
||
const onExpandedChange = (keys: Selection) => { | ||
const newKeys = new Set([...keys].map((key) => key.toString())); | ||
const differenceToAdd = [...newKeys].filter((key) => !expanded.has(key)); | ||
const differenceToRemove = [...expanded].filter((key) => !newKeys.has(key)); | ||
|
||
const updatedExpanded = new Set(expanded); | ||
differenceToAdd.forEach(async (key) => { | ||
if (!loadedPaths.has(key)) { | ||
setLoadedPaths(prevPaths => new Set(prevPaths).add(key)); | ||
await loadSuggestions(key + '/'); | ||
} | ||
updatedExpanded.add(key); | ||
}); | ||
|
||
differenceToRemove.forEach((key) => { | ||
updatedExpanded.delete(key); | ||
}); | ||
|
||
setExpanded(updatedExpanded); | ||
}; | ||
|
||
useEffect(() => { | ||
if (rootPath == '/') { | ||
rootPath = ""; | ||
} | ||
// Expand the root path when the component mounts | ||
const initializePathInput = async () => { | ||
if (!loadedPaths.has(rootPath)) { | ||
setLoadedPaths(prevPaths => new Set(prevPaths).add(rootPath)); | ||
await loadSuggestions(rootPath); | ||
} | ||
const expandPathToValue = async (path: string) => { | ||
const pathParts = path.split("/").filter(Boolean); | ||
let currentPath = ""; | ||
const tempExpanded = new Set<string>([rootPath]); | ||
const tempLoadedPaths = new Set(loadedPaths); | ||
// Expand each part of the path except for the last oen | ||
for (const part of pathParts.slice(0, -1)) { | ||
currentPath = `${currentPath}/${part}`.replace(/\/+/g, '/'); | ||
if (!tempLoadedPaths.has(currentPath)) { | ||
await loadSuggestions(currentPath); | ||
tempLoadedPaths.add(currentPath); | ||
tempExpanded.add(currentPath); | ||
} | ||
} | ||
|
||
setLoadedPaths(new Set(tempLoadedPaths)); | ||
setExpanded(new Set(tempExpanded)); | ||
|
||
// If value passed doesn't exist in the loaded paths, clear the selection | ||
if (!tempLoadedPaths.has(value)) { | ||
setSelected(new Set<string>()) | ||
onChange(""); | ||
} | ||
}; | ||
await expandPathToValue(value); | ||
setSelected(new Set([value])); | ||
onChange(value) | ||
}; | ||
if (value != null && value.length > 0) { | ||
// Offload the initialization to avoid blocking the UI | ||
setTimeout(async () => { | ||
await initializePathInput() | ||
}, 0) | ||
} | ||
}, [rootPath, loadSuggestions]); | ||
|
||
return ( | ||
<> | ||
<p className={styles.label}>{label}</p> | ||
<div ref={ref} tabIndex={-1} style={{outline: "none", width: "100%", height: "100%"}}> | ||
<TreeView {...props} selectionMode={"single"} width={'100%'} onSelectionChange={onSelectionChange} onExpandedChange={onExpandedChange} defaultSelectedKeys={new Set(value)} selectedKeys={selected} expandedKeys={expanded}> | ||
kamil-orwat-vmltech marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{items && <TreeItem node={items} path={''} />} | ||
</TreeView> | ||
</div> | ||
{props.errorMessage && <p className={codeArgumentStyles.error}>{props.errorMessage}</p>} | ||
{!props.errorMessage && value && value != "/" && <p className={styles.label}>Selected: {value}</p>} | ||
</> | ||
); | ||
}) | ||
|
||
const TreeItem = ({ node, path }: { node: Node; path: string }) => { | ||
return ( | ||
<TreeViewItem id={path + node.name} textValue={node.name.length > 0 ? node.name : "/"}> | ||
<TreeViewItemContent> | ||
<Text>{node.name.length > 0 ? node.name.replace("/", "") : '/'}</Text> | ||
</TreeViewItemContent> | ||
{node.children && node.children.length > 0 ? ( | ||
node.children.map((child: Node) => <TreeItem node={child} path={path + node.name + '/'} key={path + node.name + child.name} />) | ||
) : node.children ? null : ( | ||
<TreeViewItem children={[]} textValue={'empty'} /> | ||
)} | ||
</TreeViewItem> | ||
); | ||
}; | ||
|
||
const useRequestPathCompletion = () => { | ||
async function getPathCompletion(path: string) { | ||
if (!path.endsWith('/')) { | ||
path += '/'; | ||
} | ||
const response = await apiRequest<AssistCodeOutput>({ | ||
method: 'GET', | ||
url: `/apps/acm/api/assist-code.json?type=resource&word=${encodeURIComponent(path)}`, | ||
operation: 'Code assistance', | ||
}); | ||
return response.data.data; | ||
} | ||
|
||
return { getPathCompletion }; | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,7 +31,6 @@ const ExecutionView = () => { | |
const [autoscrollOutput, setAutoscrollOutput] = useState<boolean>(true); | ||
const { execution, setExecution, loading } = useExecutionPolling(executionId, appState.spaSettings.executionPollInterval); | ||
const [selectedTab, handleTabChange] = useNavigationTab('details'); | ||
|
||
if (loading) { | ||
return ( | ||
<Flex flex="1" justifyContent="center" alignItems="center"> | ||
|
@@ -87,7 +86,7 @@ const ExecutionView = () => { | |
<View backgroundColor="gray-50" padding="size-200" borderRadius="medium" borderColor="dark" borderWidth="thin"> | ||
<Flex direction="row" justifyContent="space-between" gap="size-200"> | ||
<LabeledValue label="ID" value={execution.id} flex="1" /> | ||
<LabeledValue label="User" value={execution.userId} flex="1" /> | ||
<LabeledValue label="User" value={execution.userId ? execution.userId : "unknown"} flex="1" /> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It was throwing errors during development, so I added default version. Do we want to keep it? |
||
<Flex justifyContent="end"> | ||
<Field label="Status" flex="1"> | ||
<div> | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.