PromucFlow_constructor/app/client/src/utils/editorContextUtils.ts
Hetu Nandu b4adfcf8e2
feat: [Context Switching]: Change focus target and fix cursor position (#17794)
Co-authored-by: 
rahulramesha <rahul@appsmith.com>
2022-11-15 11:50:18 +05:30

72 lines
1.8 KiB
TypeScript

/**
* Append PageId to path and return the key
* @param path
* @param currentPageId
* @returns
*/
export function generatePropertyKey(
path: string | undefined,
currentPageId: string,
) {
if (!path) return;
return `Page[${currentPageId}].${path}`;
}
/**
* This method returns boolean if the propertyControl is focused.
* @param domElement
* @returns
*/
export function shouldFocusOnPropertyControl(
domElement?: HTMLDivElement | null,
) {
const isCurrentFocusOnInput =
["input", "textarea"].indexOf(
document.activeElement?.tagName?.toLowerCase() || "",
) >= 0;
let isCurrentFocusOnProperty = false;
if (domElement) {
isCurrentFocusOnProperty = domElement.contains(document.activeElement);
}
return !(isCurrentFocusOnInput || isCurrentFocusOnProperty);
}
/**
* Returns a focusable field of PropertyControl.
* @param element
* @returns
*/
export function getPropertyControlFocusElement(
element: HTMLDivElement | null,
): HTMLElement | undefined {
if (element?.children) {
const [, propertyInputElement] = element.children;
if (propertyInputElement) {
const uiInputElement = propertyInputElement.querySelector(
'button:not([tabindex="-1"]), input, [tabindex]:not([tabindex="-1"])',
) as HTMLElement | undefined;
if (uiInputElement) {
return uiInputElement;
}
const codeEditorInputElement = propertyInputElement.getElementsByClassName(
"CodeEditorTarget",
)[0] as HTMLElement | undefined;
if (codeEditorInputElement) {
return codeEditorInputElement;
}
const lazyCodeEditorInputElement = propertyInputElement.getElementsByClassName(
"LazyCodeEditor",
)[0] as HTMLElement | undefined;
if (lazyCodeEditorInputElement) {
return lazyCodeEditorInputElement;
}
}
}
}