diff options
author | bobzel <zzzman@gmail.com> | 2024-05-08 23:16:54 -0400 |
---|---|---|
committer | bobzel <zzzman@gmail.com> | 2024-05-08 23:16:54 -0400 |
commit | d40efe81ab30485266b7b686dfb35c531e75a568 (patch) | |
tree | f12c9b151e1c6b35f4afa3962595386ce596e37c /src | |
parent | b858bd3cad81da41e63b9f8e807e41421ca4aa34 (diff) |
fixed text selection on web pages. cleaned up gptSummarize to work on any text.
Diffstat (limited to 'src')
-rw-r--r-- | src/ClientUtils.ts | 2 | ||||
-rw-r--r-- | src/client/views/MarqueeAnnotator.tsx | 1 | ||||
-rw-r--r-- | src/client/views/nodes/DocumentView.tsx | 9 | ||||
-rw-r--r-- | src/client/views/nodes/formattedText/FormattedTextBox.tsx | 3 | ||||
-rw-r--r-- | src/client/views/pdf/AnchorMenu.tsx | 15 |
5 files changed, 17 insertions, 13 deletions
diff --git a/src/ClientUtils.ts b/src/ClientUtils.ts index dbbba896f..d03ae1486 100644 --- a/src/ClientUtils.ts +++ b/src/ClientUtils.ts @@ -518,7 +518,7 @@ export function simulateMouseClick(element: Element | null | undefined, x: numbe export function getWordAtPoint(elem: any, x: number, y: number): string | undefined { if (elem.tagName === 'INPUT') return 'input'; if (elem.tagName === 'TEXTAREA') return 'textarea'; - if (elem.nodeType === elem.TEXT_NODE) { + if (elem.nodeType === elem.TEXT_NODE || elem.textContent) { const range = elem.ownerDocument.createRange(); range.selectNodeContents(elem); let currentPos = 0; diff --git a/src/client/views/MarqueeAnnotator.tsx b/src/client/views/MarqueeAnnotator.tsx index a917a7d57..c18ac6738 100644 --- a/src/client/views/MarqueeAnnotator.tsx +++ b/src/client/views/MarqueeAnnotator.tsx @@ -258,6 +258,7 @@ export class MarqueeAnnotator extends ObservableReactComponent<MarqueeAnnotatorP }; @action onEnd = (x: number, y: number) => { + AnchorMenu.Instance.setSelectedText(''); const marquees = this.props.marqueeContainer.getElementsByClassName('marqueeAnnotator-dragBox'); const marqueeStyle = (Array.from(marquees).lastElement() as HTMLDivElement)?.style; if (!this.isEmpty && marqueeStyle) { diff --git a/src/client/views/nodes/DocumentView.tsx b/src/client/views/nodes/DocumentView.tsx index 8df28a770..ada3de355 100644 --- a/src/client/views/nodes/DocumentView.tsx +++ b/src/client/views/nodes/DocumentView.tsx @@ -981,6 +981,9 @@ export class DocumentViewInternal extends DocComponent<FieldViewProps & Document @observer export class DocumentView extends DocComponent<DocumentViewProps>() { public static ROOT_DIV = 'documentView-effectsWrapper'; + /** + * Opens a new Tab for the doc in the specified location (or in the lightbox) + */ public static addSplit: (Doc: Doc, where: OpenWhereMod) => void; // Lightbox public static _lightboxDoc: () => Doc | undefined; @@ -1012,7 +1015,13 @@ export class DocumentView extends DocComponent<DocumentViewProps>() { public static DeselectAll: (except?: Doc) => void | undefined; public static DeselectView: (dv: DocumentView | undefined) => void | undefined; public static SelectView: (dv: DocumentView | undefined, extendSelection: boolean) => void | undefined; + /** + * returns a list of all currently selected DocumentViews + */ public static Selected: () => DocumentView[]; + /** + * returns a list of all currently selected Docs + */ public static SelectedDocs: () => Doc[]; public static SelectSchemaDoc: (doc: Doc, deselectAllFirst?: boolean) => void; public static SelectedSchemaDoc: () => Opt<Doc>; diff --git a/src/client/views/nodes/formattedText/FormattedTextBox.tsx b/src/client/views/nodes/formattedText/FormattedTextBox.tsx index e354aedb7..624721d7c 100644 --- a/src/client/views/nodes/formattedText/FormattedTextBox.tsx +++ b/src/client/views/nodes/formattedText/FormattedTextBox.tsx @@ -240,7 +240,6 @@ export class FormattedTextBox extends ViewBoxAnnotatableComponent<FormattedTextB @action setupAnchorMenu = () => { AnchorMenu.Instance.Status = 'marquee'; - AnchorMenu.Instance.OnClick = () => { !this.layoutDoc.layout_showSidebar && this.toggleSidebar(); setTimeout(() => this._sidebarRef.current?.anchorMenuClick(this.makeLinkAnchor(undefined, OpenWhere.addRight, undefined, 'Anchored Selection', true))); // give time for sidebarRef to be created @@ -292,6 +291,8 @@ export class FormattedTextBox extends ViewBoxAnnotatableComponent<FormattedTextB DragManager.StartAnchorAnnoDrag([ele], new DragManager.AnchorAnnoDragData(this.DocumentView?.()!, () => this.getAnchor(true), targetCreator), e.pageX, e.pageY); }); + + AnchorMenu.Instance.setSelectedText(window.getSelection()?.toString() ?? ''); const coordsB = this._editorView!.coordsAtPos(this._editorView!.state.selection.to); this._props.rootSelected?.() && AnchorMenu.Instance.jumpTo(coordsB.left, coordsB.bottom); let ele: Opt<HTMLDivElement>; diff --git a/src/client/views/pdf/AnchorMenu.tsx b/src/client/views/pdf/AnchorMenu.tsx index a837969aa..495ea59f0 100644 --- a/src/client/views/pdf/AnchorMenu.tsx +++ b/src/client/views/pdf/AnchorMenu.tsx @@ -36,10 +36,10 @@ export class AnchorMenu extends AntimodeMenu<AntimodeMenuProps> { @observable public Status: 'marquee' | 'annotation' | '' = ''; // GPT additions - @observable private selectedText: string = ''; + @observable private _selectedText: string = ''; @action public setSelectedText = (txt: string) => { - this.selectedText = txt; + this._selectedText = txt.trim(); }; public onMakeAnchor: () => Opt<Doc> = () => undefined; // Method to get anchor from text search @@ -76,7 +76,7 @@ export class AnchorMenu extends AntimodeMenu<AntimodeMenuProps> { * @param e pointer down event */ gptSummarize = async () => { - GPTPopup.Instance?.setSelectedText(this.selectedText); + GPTPopup.Instance?.setSelectedText(this._selectedText); GPTPopup.Instance.generateSummary(); }; @@ -140,13 +140,6 @@ export class AnchorMenu extends AntimodeMenu<AntimodeMenuProps> { this.highlightColor = ClientUtils.colorString(col); }; - /** - * Returns whether the selected text can be summarized. The goal is to have - * all selected text available to summarize but its only supported for pdf and web ATM. - * @returns Whether the GPT icon for summarization should appear - */ - canSummarize = () => DocumentView.SelectedDocs().some(doc => [DocumentType.PDF, DocumentType.WEB].includes(doc.type as any)); - render() { const buttons = this.Status === 'marquee' ? ( @@ -161,7 +154,7 @@ export class AnchorMenu extends AntimodeMenu<AntimodeMenuProps> { /> </div> {/* GPT Summarize icon only shows up when text is highlighted, not on marquee selection */} - {AnchorMenu.Instance.StartCropDrag === unimplementedFunction && this.canSummarize() && ( + {this._selectedText && ( <IconButton tooltip="Summarize with AI" // onPointerDown={this.gptSummarize} |