Skip to content

Commit

Permalink
feat: refactor codebase
Browse files Browse the repository at this point in the history
  • Loading branch information
Neet-Nestor committed Oct 18, 2024
1 parent bd75ec9 commit 6cdbc26
Show file tree
Hide file tree
Showing 10 changed files with 103 additions and 302 deletions.
48 changes: 48 additions & 0 deletions src/action.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { State } from "./state";

export const replaceSelectedText = (
state: State,
parameters: { newText: string | string[] },
): void => {
const { newText } = parameters;
const selection = state.currentSelection;
if (!newText || !selection) {
return;
}
if (selection.rangeCount > 0) {
const range = selection.getRangeAt(0);
range.deleteContents();
if (Array.isArray(newText)) {
const fragment = document.createDocumentFragment();
newText.forEach((text) =>
fragment.appendChild(document.createTextNode(text)),
);
range.insertNode(fragment);
} else {
range.insertNode(document.createTextNode(newText));
}
selection.removeAllRanges();
}
};

export const appendTextToDocument = (
state: State,
parameters: { text: string },
): void => {
if (window.location.hostname.includes("overleaf.com")) {
const { text } = parameters;
const editorElement = document.querySelector(".cm-content");
if (editorElement) {
const textNode = document.createTextNode(text);
editorElement.appendChild(textNode);

// Scroll to bottom
const scroller = document.querySelector(".cm-scroller");
if (scroller) {
scroller.scrollTo({ top: scroller.scrollHeight, behavior: "smooth" });
}
}
} else {
throw new Error("Not Implemented");
}
};
39 changes: 4 additions & 35 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,4 @@
import * as Overleaf from "./page/overleaf";
import { ToolName } from "./tool";
export { getToolInfo, getToolsInfo, allTools } from "./tool";

const PAGE_HANDLER_MAP: Record<string, typeof Overleaf.PageHandler> = {
"www.overleaf.com": Overleaf.PageHandler,
};

const PAGE_TOOLS_MAP: Record<string, ToolName[]> = {
"www.overleaf.com": Overleaf.availableTools,
};

export const isPageSupported = (url: string) => {
return Object.keys(PAGE_HANDLER_MAP).includes(url);
};

export const isCurrentPageSupported = () => {
return Object.keys(PAGE_HANDLER_MAP).includes(window.location.hostname);
};

export const initHandler = () => {
if (isCurrentPageSupported()) {
return new PAGE_HANDLER_MAP[window.location.hostname]();
}
console.error("[Web Agent Interface] No tools found for the current page");
};

export const getAvailableTools = () => {
if (isCurrentPageSupported()) {
return PAGE_TOOLS_MAP[window.location.hostname];
}
console.error("[Web Agent Interface] No tools found for the current page");
};

export * as Overleaf from "./page/overleaf";
export { tool, toolName, ToolName } from "./tool";
export { State } from "./state";
export * as retriever from "./retriever";
export * as action from "./action";
48 changes: 0 additions & 48 deletions src/page/googleCalendar/commands/run_view_events.ts

This file was deleted.

102 changes: 0 additions & 102 deletions src/page/googleCalendar/googleCalendar.ts

This file was deleted.

7 changes: 0 additions & 7 deletions src/page/googleCalendar/googleCalendar_base.ts

This file was deleted.

3 changes: 0 additions & 3 deletions src/page/interface.ts

This file was deleted.

93 changes: 0 additions & 93 deletions src/page/overleaf.ts

This file was deleted.

8 changes: 8 additions & 0 deletions src/retriever.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { State } from "./state";

export const getSelectedText = (state: State): string => {
if (!state.currentSelection) {
return "";
}
return state.currentSelection.toString();
};
17 changes: 17 additions & 0 deletions src/state.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export class State {
public currentSelection: Selection | undefined;

constructor() {
document.addEventListener("selectionchange", (): void => {
const selection = window.getSelection();

if (
selection &&
typeof selection.rangeCount !== "undefined" &&
selection.rangeCount > 0
) {
this.currentSelection = selection;
}
});
}
}
Loading

0 comments on commit 6cdbc26

Please sign in to comment.