@salesforcedevs/docs-components 1.17.2 → 1.17.6-hover-edit

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,257 @@
1
+ import { LightningElement, api, track } from "lwc";
2
+
3
+ export default class TextSelectionSearch extends LightningElement {
4
+ @api searchApiUrl: string = "";
5
+ @api repoUrl: string = "";
6
+ @api placeholder: string = "Search for...";
7
+ @api popoverPosition: "top" | "bottom" = "top";
8
+ @api maxResults: number = 10;
9
+
10
+ @track isVisible: boolean = false;
11
+ @track isHidden: boolean = false;
12
+ @track searchQuery: string = "";
13
+ @track isLoading: boolean = false;
14
+ @track isSuccess: boolean = false;
15
+ @track errorMessage: string = "";
16
+ @track popoverStyle: string = "";
17
+
18
+ private selectedText: string = "";
19
+ private selectionStart: number = 0;
20
+ private selectionEnd: number = 0;
21
+ private initialPopoverStyle: string = ""; // Store initial position
22
+
23
+ connectedCallback() {
24
+ this.setupTextSelectionListener();
25
+ }
26
+
27
+ disconnectedCallback() {
28
+ this.removeTextSelectionListener();
29
+ }
30
+
31
+ // Setup text selection listener
32
+ setupTextSelectionListener() {
33
+ console.log('TextSelectionSearch: Setting up text selection listeners');
34
+ document.addEventListener("mouseup", (event) => this.handleTextSelection(event));
35
+ document.addEventListener("keyup", (event) => this.handleTextSelection(event));
36
+ }
37
+
38
+ removeTextSelectionListener() {
39
+ console.log('TextSelectionSearch: Removing text selection listeners');
40
+ document.removeEventListener("mouseup", (event) => this.handleTextSelection(event));
41
+ document.removeEventListener("keyup", (event) => this.handleTextSelection(event));
42
+ }
43
+
44
+ // Handle text selection
45
+ handleTextSelection(event?: Event) {
46
+ // If popover is visible, completely ignore all text selection events
47
+ if (this.isVisible) {
48
+ console.log('TextSelectionSearch: Popover is visible, ignoring text selection event');
49
+ return;
50
+ }
51
+
52
+ console.log('TextSelectionSearch: Text selection event triggered');
53
+
54
+ // Check if the event target is inside our popover
55
+ if (event && event.target) {
56
+ const target = event.target as Element;
57
+ const popover = this.template.querySelector('.text-selection-search-popover');
58
+ if (popover && popover.contains(target)) {
59
+ console.log('TextSelectionSearch: Click inside popover, ignoring selection event');
60
+ return;
61
+ }
62
+ }
63
+
64
+ const selection = window.getSelection();
65
+
66
+ if (!selection || selection.toString().trim() === "") {
67
+ // Only hide popover if it's currently visible and we're not clicking inside it
68
+ if (this.isVisible) {
69
+ console.log('TextSelectionSearch: No text selected, but popover is visible - keeping it open');
70
+ return;
71
+ }
72
+ console.log('TextSelectionSearch: No text selected, hiding popover');
73
+ this.hidePopover();
74
+ return;
75
+ }
76
+
77
+ this.selectedText = selection.toString().trim();
78
+ console.log('TextSelectionSearch: Selected text:', this.selectedText);
79
+
80
+ if (this.selectedText.length > 0) {
81
+ this.showPopover();
82
+ }
83
+ }
84
+
85
+ // Show popover at selection position
86
+ showPopover() {
87
+ console.log('TextSelectionSearch: Showing popover');
88
+ const selection = window.getSelection();
89
+ if (!selection || selection.rangeCount === 0) {
90
+ console.log('TextSelectionSearch: No selection range found');
91
+ return;
92
+ }
93
+
94
+ const range = selection.getRangeAt(0);
95
+ const rect = range.getBoundingClientRect();
96
+
97
+ // Calculate popover position with more conservative positioning
98
+ const popoverTop = this.popoverPosition === "top"
99
+ ? Math.max(10, rect.top - 80) // Ensure it doesn't go off-screen
100
+ : Math.min(window.innerHeight - 200, rect.bottom + 20);
101
+
102
+ const popoverLeft = Math.max(10, Math.min(window.innerWidth - 350, rect.left + (rect.width / 2) - 175));
103
+
104
+ this.popoverStyle = `position: fixed; top: ${popoverTop}px; left: ${popoverLeft}px; z-index: 9999; width: 350px;`;
105
+ this.initialPopoverStyle = this.popoverStyle; // Store the initial position
106
+ console.log('TextSelectionSearch: Popover style:', this.popoverStyle);
107
+
108
+ this.isVisible = true;
109
+ this.isHidden = false;
110
+ this.searchQuery = this.selectedText;
111
+ this.errorMessage = "";
112
+ this.isSuccess = false;
113
+
114
+ console.log('TextSelectionSearch: Popover should now be visible');
115
+ }
116
+
117
+ // Hide popover
118
+ hidePopover() {
119
+ console.log('TextSelectionSearch: Hiding popover');
120
+ this.isHidden = true;
121
+
122
+ // Use setTimeout to ensure the hidden class is applied before hiding
123
+ setTimeout(() => {
124
+ this.isVisible = false;
125
+ this.searchQuery = "";
126
+ this.errorMessage = "";
127
+ this.isSuccess = false;
128
+ this.popoverStyle = ""; // Reset the positioning style
129
+ this.initialPopoverStyle = ""; // Reset the initial position
130
+
131
+ // Clear any text selection to prevent immediate re-showing
132
+ const selection = window.getSelection();
133
+ if (selection) {
134
+ selection.removeAllRanges();
135
+ }
136
+ }, 100);
137
+ }
138
+
139
+ // Handle popover click to prevent closing
140
+ handlePopoverClick(event: Event) {
141
+ event.stopPropagation();
142
+ }
143
+
144
+ // Handle input click to prevent closing
145
+ handleInputClick(event: Event) {
146
+ event.stopPropagation();
147
+ }
148
+
149
+ // Handle search input change
150
+ handleSearchInputChange(event: Event) {
151
+ const target = event.target as HTMLInputElement;
152
+ this.searchQuery = target.value;
153
+ }
154
+
155
+ // Handle search submission
156
+ async handleSearchSubmit(event: Event) {
157
+ event.preventDefault();
158
+
159
+ if (!this.searchQuery.trim()) {
160
+ this.errorMessage = "Please enter edited text";
161
+ return;
162
+ }
163
+
164
+ if (!this.repoUrl.trim()) {
165
+ this.errorMessage = "Repository URL not configured";
166
+ return;
167
+ }
168
+
169
+ if (!this.searchApiUrl) {
170
+ this.errorMessage = "Search API URL not configured";
171
+ return;
172
+ }
173
+
174
+ await this.performSearch();
175
+ }
176
+
177
+ // Perform search API call
178
+ async performSearch() {
179
+ this.isLoading = true;
180
+ this.errorMessage = "";
181
+ this.isSuccess = false;
182
+
183
+ try {
184
+ const response = await fetch(this.searchApiUrl, {
185
+ method: "POST",
186
+ headers: {
187
+ "Content-Type": "application/json"
188
+ },
189
+ body: JSON.stringify({
190
+ repoUrl: this.repoUrl,
191
+ selectedText: this.selectedText,
192
+ editedText: this.searchQuery
193
+ })
194
+ });
195
+
196
+ if (response.ok) {
197
+ this.isSuccess = true;
198
+
199
+ // Dispatch success event
200
+ this.dispatchEvent(
201
+ new CustomEvent("textedited", {
202
+ detail: {
203
+ repoUrl: this.repoUrl,
204
+ selectedText: this.selectedText,
205
+ editedText: this.searchQuery,
206
+ success: true
207
+ }
208
+ })
209
+ );
210
+
211
+ // Auto-hide popover after success
212
+ setTimeout(() => {
213
+ this.hidePopover();
214
+ }, 2000);
215
+ } else {
216
+ throw new Error(`Edit failed: ${response.status}`);
217
+ }
218
+ } catch (error) {
219
+ console.error("Edit failed:", error);
220
+ this.errorMessage = "Edit failed. Please try again.";
221
+ } finally {
222
+ this.isLoading = false;
223
+ }
224
+ }
225
+
226
+ // Handle escape key
227
+ handleKeyDown(event: KeyboardEvent) {
228
+ if (event.key === "Escape") {
229
+ this.hidePopover();
230
+ }
231
+ }
232
+
233
+ // Getters for UI state
234
+ get hasError() {
235
+ return this.errorMessage.length > 0;
236
+ }
237
+
238
+ get searchButtonDisabled() {
239
+ return this.isLoading || !this.searchQuery.trim();
240
+ }
241
+
242
+ renderedCallback() {
243
+ if (this.isVisible && this.initialPopoverStyle) {
244
+ const popover = this.template.querySelector('.text-selection-search-popover') as HTMLElement;
245
+ if (popover) {
246
+ // Always use the initial position, never recalculate
247
+ popover.style.cssText = this.initialPopoverStyle;
248
+ }
249
+ } else if (!this.isVisible) {
250
+ // Clear the style when popover is hidden
251
+ const popover = this.template.querySelector('.text-selection-search-popover') as HTMLElement;
252
+ if (popover) {
253
+ popover.style.cssText = '';
254
+ }
255
+ }
256
+ }
257
+ }
@@ -1,6 +1,6 @@
1
1
  <template>
2
2
  <doc-content-layout
3
- lwc:if={displayContent}
3
+ lwc:if={loaded}
4
4
  lwc:ref="docContentLayout"
5
5
  coveo-organization-id={coveoOrganizationId}
6
6
  coveo-public-access-token={coveoPublicAccessToken}
@@ -49,12 +49,4 @@
49
49
  onnavclick={handleNavClick}
50
50
  ></doc-content>
51
51
  </doc-content-layout>
52
- <div lwc:if={display404}>
53
- <dx-error
54
- image="https://a.sfdcstatic.com/developer-website/images/404.svg"
55
- code="404"
56
- header="Beep boop. That did not compute."
57
- subtitle="The document you're looking for doesn't seem to exist."
58
- ></dx-error>
59
- </div>
60
52
  </template>
@@ -11,8 +11,7 @@ import {
11
11
  SiderbarFooter,
12
12
  HistoryState,
13
13
  PageReference,
14
- TocMap,
15
- ContentData
14
+ TocMap
16
15
  } from "./types";
17
16
  import { SearchSyncer } from "docUtils/searchSyncer";
18
17
  import { LightningElementWithState } from "dxBaseElements/lightningElementWithState";
@@ -73,8 +72,7 @@ export default class DocXmlContent extends LightningElementWithState<{
73
72
  private contentProvider?: FetchContent;
74
73
  private docContent = "";
75
74
  private language?: DocLanguage | null = null;
76
- private displayContent = false;
77
- private display404 = false;
75
+ private loaded = false;
78
76
  private _pageHeader?: Header;
79
77
  private pdfUrl = "";
80
78
  private tocMap: TocMap = {};
@@ -187,13 +185,7 @@ export default class DocXmlContent extends LightningElementWithState<{
187
185
  this.apiDomain,
188
186
  this.allLanguages
189
187
  );
190
- this.fetchDocument().then((content: any) => {
191
- if (content) {
192
- this.displayContent = true;
193
- } else {
194
- this.display404 = true;
195
- }
196
- });
188
+ this.fetchDocument().then(() => (this.loaded = true));
197
189
  window.addEventListener("popstate", this.handlePopState);
198
190
 
199
191
  this.searchSyncer.init();
@@ -471,7 +463,7 @@ export default class DocXmlContent extends LightningElementWithState<{
471
463
  );
472
464
  }
473
465
 
474
- async fetchDocument(): Promise<string> {
466
+ async fetchDocument(): Promise<void> {
475
467
  this.setState({
476
468
  isFetchingDocument: true
477
469
  });
@@ -485,7 +477,7 @@ export default class DocXmlContent extends LightningElementWithState<{
485
477
  this.setState({
486
478
  isFetchingDocument: false
487
479
  });
488
- return "";
480
+ return;
489
481
  }
490
482
 
491
483
  this.docTitle = data.docTitle;
@@ -517,11 +509,11 @@ export default class DocXmlContent extends LightningElementWithState<{
517
509
  data.id
518
510
  ) {
519
511
  try {
520
- const moreData = await this.fetchContent();
512
+ await this.fetchContent();
521
513
  this.setState({
522
514
  isFetchingDocument: false
523
515
  });
524
- return moreData.content;
516
+ return;
525
517
  } catch (error) {
526
518
  this.pageReference.contentDocumentId = `${data.id}.htm`;
527
519
  this.pageReference.hash = "";
@@ -535,10 +527,9 @@ export default class DocXmlContent extends LightningElementWithState<{
535
527
  this.setState({
536
528
  isFetchingDocument: false
537
529
  });
538
- return data.content;
539
530
  }
540
531
 
541
- async fetchContent(): Promise<ContentData> {
532
+ async fetchContent(): Promise<void> {
542
533
  this.setState({
543
534
  isFetchingContent: true
544
535
  });
@@ -562,7 +553,6 @@ export default class DocXmlContent extends LightningElementWithState<{
562
553
  this.setState({
563
554
  isFetchingContent: false
564
555
  });
565
- return data;
566
556
  }
567
557
 
568
558
  updateHeaderAndSidebarFooter(): void {
package/LICENSE DELETED
@@ -1,12 +0,0 @@
1
- Copyright (c) 2020, Salesforce.com, Inc.
2
- All rights reserved.
3
-
4
- Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
5
-
6
- * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
7
-
8
- * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
9
-
10
- * Neither the name of Salesforce.com nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
11
-
12
- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.