Skip to content

Commit

Permalink
Add command to save a reference to the active element (#298)
Browse files Browse the repository at this point in the history
  • Loading branch information
david-tejada committed May 14, 2024
1 parent 6f75383 commit 9943066
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
10 changes: 10 additions & 0 deletions src/content/actions/references.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { ElementWrapper } from "../../typings/ElementWrapper";
import { showTooltip } from "../hints/showTooltip";
import { getOrCreateWrapper } from "../wrappers/ElementWrapperClass";
import { getHostPattern } from "../../common/utils";
import { getActiveElement } from "../utils/domUtils";
import { getWrapperForElement } from "../wrappers/wrappers";

function getWrapperFromUniqueSelector(selector: string) {
const element = document.querySelector(selector);
Expand Down Expand Up @@ -39,6 +41,14 @@ export async function saveReference(wrapper: ElementWrapper, name: string) {
showTooltip(wrapper, name);
}

export async function saveReferenceForActiveElement(name: string) {
const activeElement = getActiveElement();
if (activeElement) {
const wrapper = getWrapperForElement(activeElement);
if (wrapper) await saveReference(wrapper, name);
}
}

export async function showReferences() {
const { hostReferences } = await getReferences();

Expand Down
10 changes: 9 additions & 1 deletion src/content/actions/runRangoActionWithoutTarget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ import { scrollToPosition, storeScrollPosition } from "./customScrollPositions";
import { blur, focusFirstInput } from "./focus";
import { unhoverAll } from "./hoverElement";
import { navigateToNextPage, navigateToPreviousPage } from "./pagination";
import { removeReference, showReferences } from "./references";
import {
removeReference,
saveReferenceForActiveElement,
showReferences,
} from "./references";
import { refreshHints } from "./refreshHints";
import { runActionOnReference } from "./runActionOnReference";
import { scroll } from "./scroll";
Expand Down Expand Up @@ -161,6 +165,10 @@ export async function runRangoActionWithoutTarget(
isEditable(document.activeElement)
);

case "saveReferenceForActiveElement":
await saveReferenceForActiveElement(request.arg);
break;

case "runActionOnReference":
return runActionOnReference(request.arg, request.arg2);

Expand Down
6 changes: 6 additions & 0 deletions src/typings/RangoAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,11 @@ interface RangoActionSaveReference {
arg: string;
}

interface RangoActionSaveReferenceForActiveElement {
type: "saveReferenceForActiveElement";
arg: string;
}

export interface RangoActionRunActionOnReference {
type: "runActionOnReference";
arg: RangoActionWithTargets["type"];
Expand Down Expand Up @@ -206,6 +211,7 @@ export type RangoActionWithoutTarget =
| RangoActionSetHintWeight
| RangoActionCopyLocationProperty
| RangoActionOpenPageInNewTab
| RangoActionSaveReferenceForActiveElement
| RangoActionRunActionOnReference
| RangoActionRemoveReference
| RangoActionScrollPosition
Expand Down

0 comments on commit 9943066

Please sign in to comment.