-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bd75ec9
commit 6cdbc26
Showing
10 changed files
with
103 additions
and
302 deletions.
There are no files selected for viewing
This file contains 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,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"); | ||
} | ||
}; |
This file contains 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 |
---|---|---|
@@ -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"; |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains 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,8 @@ | ||
import { State } from "./state"; | ||
|
||
export const getSelectedText = (state: State): string => { | ||
if (!state.currentSelection) { | ||
return ""; | ||
} | ||
return state.currentSelection.toString(); | ||
}; |
This file contains 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,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; | ||
} | ||
}); | ||
} | ||
} |
Oops, something went wrong.