@n24q02m/workspace 0.0.0-stage → 0.6.0-beta.1
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.
- package/LICENSE +202 -0
- package/NOTICE +46 -0
- package/README.md +342 -2
- package/bin/cli.mjs +144 -0
- package/build/src/auth/account-context.d.ts +3 -0
- package/build/src/auth/account-context.d.ts.map +1 -0
- package/build/src/auth/account-context.js +17 -0
- package/build/src/auth/account-context.js.map +1 -0
- package/build/src/auth/account-context.test.d.ts +2 -0
- package/build/src/auth/account-context.test.d.ts.map +1 -0
- package/build/src/auth/account-context.test.js +30 -0
- package/build/src/auth/account-context.test.js.map +1 -0
- package/build/src/auth/account-store.d.ts +87 -0
- package/build/src/auth/account-store.d.ts.map +1 -0
- package/build/src/auth/account-store.js +187 -0
- package/build/src/auth/account-store.js.map +1 -0
- package/build/src/auth/account-store.test.d.ts +2 -0
- package/build/src/auth/account-store.test.d.ts.map +1 -0
- package/build/src/auth/account-store.test.js +264 -0
- package/build/src/auth/account-store.test.js.map +1 -0
- package/build/src/auth/add-account.d.ts +12 -0
- package/build/src/auth/add-account.d.ts.map +1 -0
- package/build/src/auth/add-account.js +95 -0
- package/build/src/auth/add-account.js.map +1 -0
- package/build/src/auth/add-account.test.d.ts +2 -0
- package/build/src/auth/add-account.test.d.ts.map +1 -0
- package/build/src/auth/add-account.test.js +155 -0
- package/build/src/auth/add-account.test.js.map +1 -0
- package/build/src/auth/cli-auth.d.ts +2 -0
- package/build/src/auth/cli-auth.d.ts.map +1 -0
- package/build/src/auth/cli-auth.js +47 -0
- package/build/src/auth/cli-auth.js.map +1 -0
- package/build/src/auth/cli-auth.test.d.ts +2 -0
- package/build/src/auth/cli-auth.test.d.ts.map +1 -0
- package/build/src/auth/cli-auth.test.js +78 -0
- package/build/src/auth/cli-auth.test.js.map +1 -0
- package/build/src/auth/consent-server.d.ts +43 -0
- package/build/src/auth/consent-server.d.ts.map +1 -0
- package/build/src/auth/consent-server.js +56 -0
- package/build/src/auth/consent-server.js.map +1 -0
- package/build/src/auth/credential-state.d.ts +7 -0
- package/build/src/auth/credential-state.d.ts.map +1 -0
- package/build/src/auth/credential-state.js +64 -0
- package/build/src/auth/credential-state.js.map +1 -0
- package/build/src/auth/credential-state.test.d.ts +2 -0
- package/build/src/auth/credential-state.test.d.ts.map +1 -0
- package/build/src/auth/credential-state.test.js +142 -0
- package/build/src/auth/credential-state.test.js.map +1 -0
- package/build/src/auth/loopback-consent.d.ts +23 -0
- package/build/src/auth/loopback-consent.d.ts.map +1 -0
- package/build/src/auth/loopback-consent.js +145 -0
- package/build/src/auth/loopback-consent.js.map +1 -0
- package/build/src/auth/loopback-consent.test.d.ts +2 -0
- package/build/src/auth/loopback-consent.test.d.ts.map +1 -0
- package/build/src/auth/loopback-consent.test.js +131 -0
- package/build/src/auth/loopback-consent.test.js.map +1 -0
- package/build/src/auth/oauth-setup.d.ts +10 -0
- package/build/src/auth/oauth-setup.d.ts.map +1 -0
- package/build/src/auth/oauth-setup.js +92 -0
- package/build/src/auth/oauth-setup.js.map +1 -0
- package/build/src/auth/oauth-setup.test.d.ts +2 -0
- package/build/src/auth/oauth-setup.test.d.ts.map +1 -0
- package/build/src/auth/oauth-setup.test.js +164 -0
- package/build/src/auth/oauth-setup.test.js.map +1 -0
- package/build/src/auth/subject-context.d.ts +2 -0
- package/build/src/auth/subject-context.d.ts.map +1 -0
- package/build/src/auth/subject-context.js +16 -0
- package/build/src/auth/subject-context.js.map +1 -0
- package/build/src/auth/subject-context.test.d.ts +2 -0
- package/build/src/auth/subject-context.test.d.ts.map +1 -0
- package/build/src/auth/subject-context.test.js +34 -0
- package/build/src/auth/subject-context.test.js.map +1 -0
- package/build/src/auth/workspace-auth.d.ts +47 -0
- package/build/src/auth/workspace-auth.d.ts.map +1 -0
- package/build/src/auth/workspace-auth.js +123 -0
- package/build/src/auth/workspace-auth.js.map +1 -0
- package/build/src/auth/workspace-auth.test.d.ts +2 -0
- package/build/src/auth/workspace-auth.test.d.ts.map +1 -0
- package/build/src/auth/workspace-auth.test.js +242 -0
- package/build/src/auth/workspace-auth.test.js.map +1 -0
- package/build/src/constants.d.ts +6 -0
- package/build/src/constants.d.ts.map +1 -0
- package/build/src/constants.js +11 -0
- package/build/src/constants.js.map +1 -0
- package/build/src/create-server.d.ts +13 -0
- package/build/src/create-server.d.ts.map +1 -0
- package/build/src/create-server.js +34 -0
- package/build/src/create-server.js.map +1 -0
- package/build/src/create-server.test.d.ts +2 -0
- package/build/src/create-server.test.d.ts.map +1 -0
- package/build/src/create-server.test.js +13 -0
- package/build/src/create-server.test.js.map +1 -0
- package/build/src/docs/calendar.md +15 -0
- package/build/src/docs/chat.md +15 -0
- package/build/src/docs/config.md +117 -0
- package/build/src/docs/docs.md +36 -0
- package/build/src/docs/drive.md +15 -0
- package/build/src/docs/forms.md +119 -0
- package/build/src/docs/gmail.md +18 -0
- package/build/src/docs/overview.md +52 -0
- package/build/src/docs/people.md +10 -0
- package/build/src/docs/sheets.md +10 -0
- package/build/src/docs/slides.md +26 -0
- package/build/src/docs/tasks.md +13 -0
- package/build/src/docs/time.md +11 -0
- package/build/src/init-server.d.ts +10 -0
- package/build/src/init-server.d.ts.map +1 -0
- package/build/src/init-server.js +13 -0
- package/build/src/init-server.js.map +1 -0
- package/build/src/init-server.test.d.ts +2 -0
- package/build/src/init-server.test.d.ts.map +1 -0
- package/build/src/init-server.test.js +15 -0
- package/build/src/init-server.test.js.map +1 -0
- package/build/src/main.d.ts +33 -0
- package/build/src/main.d.ts.map +1 -0
- package/build/src/main.js +120 -0
- package/build/src/main.js.map +1 -0
- package/build/src/main.test.d.ts +2 -0
- package/build/src/main.test.d.ts.map +1 -0
- package/build/src/main.test.js +204 -0
- package/build/src/main.test.js.map +1 -0
- package/build/src/services/FormsService.d.ts +89 -0
- package/build/src/services/FormsService.d.ts.map +1 -0
- package/build/src/services/FormsService.js +140 -0
- package/build/src/services/FormsService.js.map +1 -0
- package/build/src/services/FormsService.test.d.ts +2 -0
- package/build/src/services/FormsService.test.d.ts.map +1 -0
- package/build/src/services/FormsService.test.js +173 -0
- package/build/src/services/FormsService.test.js.map +1 -0
- package/build/src/tools/config.d.ts +19 -0
- package/build/src/tools/config.d.ts.map +1 -0
- package/build/src/tools/config.js +100 -0
- package/build/src/tools/config.js.map +1 -0
- package/build/src/tools/config.test.d.ts +2 -0
- package/build/src/tools/config.test.d.ts.map +1 -0
- package/build/src/tools/config.test.js +201 -0
- package/build/src/tools/config.test.js.map +1 -0
- package/build/src/tools/domains/calendar.d.ts +3 -0
- package/build/src/tools/domains/calendar.d.ts.map +1 -0
- package/build/src/tools/domains/calendar.js +14 -0
- package/build/src/tools/domains/calendar.js.map +1 -0
- package/build/src/tools/domains/chat.d.ts +3 -0
- package/build/src/tools/domains/chat.d.ts.map +1 -0
- package/build/src/tools/domains/chat.js +16 -0
- package/build/src/tools/domains/chat.js.map +1 -0
- package/build/src/tools/domains/docs.d.ts +8 -0
- package/build/src/tools/domains/docs.d.ts.map +1 -0
- package/build/src/tools/domains/docs.js +11 -0
- package/build/src/tools/domains/docs.js.map +1 -0
- package/build/src/tools/domains/docs.test.d.ts +2 -0
- package/build/src/tools/domains/docs.test.d.ts.map +1 -0
- package/build/src/tools/domains/docs.test.js +111 -0
- package/build/src/tools/domains/docs.test.js.map +1 -0
- package/build/src/tools/domains/drive.d.ts +3 -0
- package/build/src/tools/domains/drive.d.ts.map +1 -0
- package/build/src/tools/domains/drive.js +14 -0
- package/build/src/tools/domains/drive.js.map +1 -0
- package/build/src/tools/domains/factory.d.ts +24 -0
- package/build/src/tools/domains/factory.d.ts.map +1 -0
- package/build/src/tools/domains/factory.js +43 -0
- package/build/src/tools/domains/factory.js.map +1 -0
- package/build/src/tools/domains/factory.test.d.ts +2 -0
- package/build/src/tools/domains/factory.test.d.ts.map +1 -0
- package/build/src/tools/domains/factory.test.js +48 -0
- package/build/src/tools/domains/factory.test.js.map +1 -0
- package/build/src/tools/domains/forms.d.ts +9 -0
- package/build/src/tools/domains/forms.d.ts.map +1 -0
- package/build/src/tools/domains/forms.js +12 -0
- package/build/src/tools/domains/forms.js.map +1 -0
- package/build/src/tools/domains/forms.test.d.ts +2 -0
- package/build/src/tools/domains/forms.test.d.ts.map +1 -0
- package/build/src/tools/domains/forms.test.js +93 -0
- package/build/src/tools/domains/forms.test.js.map +1 -0
- package/build/src/tools/domains/gmail.d.ts +3 -0
- package/build/src/tools/domains/gmail.d.ts.map +1 -0
- package/build/src/tools/domains/gmail.js +17 -0
- package/build/src/tools/domains/gmail.js.map +1 -0
- package/build/src/tools/domains/index.d.ts +20 -0
- package/build/src/tools/domains/index.d.ts.map +1 -0
- package/build/src/tools/domains/index.js +230 -0
- package/build/src/tools/domains/index.js.map +1 -0
- package/build/src/tools/domains/people.d.ts +3 -0
- package/build/src/tools/domains/people.d.ts.map +1 -0
- package/build/src/tools/domains/people.js +5 -0
- package/build/src/tools/domains/people.js.map +1 -0
- package/build/src/tools/domains/sheets.d.ts +3 -0
- package/build/src/tools/domains/sheets.d.ts.map +1 -0
- package/build/src/tools/domains/sheets.js +5 -0
- package/build/src/tools/domains/sheets.js.map +1 -0
- package/build/src/tools/domains/slides.d.ts +3 -0
- package/build/src/tools/domains/slides.d.ts.map +1 -0
- package/build/src/tools/domains/slides.js +25 -0
- package/build/src/tools/domains/slides.js.map +1 -0
- package/build/src/tools/domains/tasks.d.ts +3 -0
- package/build/src/tools/domains/tasks.d.ts.map +1 -0
- package/build/src/tools/domains/tasks.js +14 -0
- package/build/src/tools/domains/tasks.js.map +1 -0
- package/build/src/tools/domains/time.d.ts +3 -0
- package/build/src/tools/domains/time.d.ts.map +1 -0
- package/build/src/tools/domains/time.js +6 -0
- package/build/src/tools/domains/time.js.map +1 -0
- package/build/src/tools/helpers/errors.d.ts +48 -0
- package/build/src/tools/helpers/errors.d.ts.map +1 -0
- package/build/src/tools/helpers/errors.js +290 -0
- package/build/src/tools/helpers/errors.js.map +1 -0
- package/build/src/tools/helpers/errors.test.d.ts +2 -0
- package/build/src/tools/helpers/errors.test.d.ts.map +1 -0
- package/build/src/tools/helpers/errors.test.js +220 -0
- package/build/src/tools/helpers/errors.test.js.map +1 -0
- package/build/src/tools/registry.d.ts +14 -0
- package/build/src/tools/registry.d.ts.map +1 -0
- package/build/src/tools/registry.js +276 -0
- package/build/src/tools/registry.js.map +1 -0
- package/build/src/tools/registry.test.d.ts +2 -0
- package/build/src/tools/registry.test.d.ts.map +1 -0
- package/build/src/tools/registry.test.js +321 -0
- package/build/src/tools/registry.test.js.map +1 -0
- package/build/src/transports/http.d.ts +7 -0
- package/build/src/transports/http.d.ts.map +1 -0
- package/build/src/transports/http.js +56 -0
- package/build/src/transports/http.js.map +1 -0
- package/build/src/transports/http.test.d.ts +2 -0
- package/build/src/transports/http.test.d.ts.map +1 -0
- package/build/src/transports/http.test.js +76 -0
- package/build/src/transports/http.test.js.map +1 -0
- package/build/src/vendored/auth/AuthManager.d.ts +7 -0
- package/build/src/vendored/auth/AuthManager.d.ts.map +1 -0
- package/build/src/vendored/auth/AuthManager.js +11 -0
- package/build/src/vendored/auth/AuthManager.js.map +1 -0
- package/build/src/vendored/services/CalendarService.d.ts +164 -0
- package/build/src/vendored/services/CalendarService.d.ts.map +1 -0
- package/build/src/vendored/services/CalendarService.js +726 -0
- package/build/src/vendored/services/CalendarService.js.map +1 -0
- package/build/src/vendored/services/CalendarValidation.d.ts +9 -0
- package/build/src/vendored/services/CalendarValidation.d.ts.map +1 -0
- package/build/src/vendored/services/CalendarValidation.js +125 -0
- package/build/src/vendored/services/CalendarValidation.js.map +1 -0
- package/build/src/vendored/services/ChatService.d.ts +90 -0
- package/build/src/vendored/services/ChatService.d.ts.map +1 -0
- package/build/src/vendored/services/ChatService.js +451 -0
- package/build/src/vendored/services/ChatService.js.map +1 -0
- package/build/src/vendored/services/DocsService.d.ts +109 -0
- package/build/src/vendored/services/DocsService.d.ts.map +1 -0
- package/build/src/vendored/services/DocsService.js +663 -0
- package/build/src/vendored/services/DocsService.js.map +1 -0
- package/build/src/vendored/services/DocsService.test.d.ts +2 -0
- package/build/src/vendored/services/DocsService.test.d.ts.map +1 -0
- package/build/src/vendored/services/DocsService.test.js +77 -0
- package/build/src/vendored/services/DocsService.test.js.map +1 -0
- package/build/src/vendored/services/DriveService.d.ts +117 -0
- package/build/src/vendored/services/DriveService.d.ts.map +1 -0
- package/build/src/vendored/services/DriveService.js +527 -0
- package/build/src/vendored/services/DriveService.js.map +1 -0
- package/build/src/vendored/services/DriveService.test.d.ts +2 -0
- package/build/src/vendored/services/DriveService.test.d.ts.map +1 -0
- package/build/src/vendored/services/DriveService.test.js +115 -0
- package/build/src/vendored/services/DriveService.test.js.map +1 -0
- package/build/src/vendored/services/GmailService.d.ts +137 -0
- package/build/src/vendored/services/GmailService.d.ts.map +1 -0
- package/build/src/vendored/services/GmailService.js +681 -0
- package/build/src/vendored/services/GmailService.js.map +1 -0
- package/build/src/vendored/services/PeopleService.d.ts +42 -0
- package/build/src/vendored/services/PeopleService.d.ts.map +1 -0
- package/build/src/vendored/services/PeopleService.js +168 -0
- package/build/src/vendored/services/PeopleService.js.map +1 -0
- package/build/src/vendored/services/SheetsService.d.ts +52 -0
- package/build/src/vendored/services/SheetsService.d.ts.map +1 -0
- package/build/src/vendored/services/SheetsService.js +262 -0
- package/build/src/vendored/services/SheetsService.js.map +1 -0
- package/build/src/vendored/services/SheetsService.test.d.ts +2 -0
- package/build/src/vendored/services/SheetsService.test.d.ts.map +1 -0
- package/build/src/vendored/services/SheetsService.test.js +104 -0
- package/build/src/vendored/services/SheetsService.test.js.map +1 -0
- package/build/src/vendored/services/SlidesService.d.ts +167 -0
- package/build/src/vendored/services/SlidesService.d.ts.map +1 -0
- package/build/src/vendored/services/SlidesService.js +887 -0
- package/build/src/vendored/services/SlidesService.js.map +1 -0
- package/build/src/vendored/services/TasksService.d.ts +97 -0
- package/build/src/vendored/services/TasksService.d.ts.map +1 -0
- package/build/src/vendored/services/TasksService.js +234 -0
- package/build/src/vendored/services/TasksService.js.map +1 -0
- package/build/src/vendored/services/TimeService.d.ts +29 -0
- package/build/src/vendored/services/TimeService.d.ts.map +1 -0
- package/build/src/vendored/services/TimeService.js +71 -0
- package/build/src/vendored/services/TimeService.js.map +1 -0
- package/build/src/vendored/utils/DriveQueryBuilder.d.ts +15 -0
- package/build/src/vendored/utils/DriveQueryBuilder.d.ts.map +1 -0
- package/build/src/vendored/utils/DriveQueryBuilder.js +17 -0
- package/build/src/vendored/utils/DriveQueryBuilder.js.map +1 -0
- package/build/src/vendored/utils/GaxiosConfig.d.ts +9 -0
- package/build/src/vendored/utils/GaxiosConfig.d.ts.map +1 -0
- package/build/src/vendored/utils/GaxiosConfig.js +30 -0
- package/build/src/vendored/utils/GaxiosConfig.js.map +1 -0
- package/build/src/vendored/utils/IdUtils.d.ts +13 -0
- package/build/src/vendored/utils/IdUtils.d.ts.map +1 -0
- package/build/src/vendored/utils/IdUtils.js +29 -0
- package/build/src/vendored/utils/IdUtils.js.map +1 -0
- package/build/src/vendored/utils/MimeHelper.d.ts +64 -0
- package/build/src/vendored/utils/MimeHelper.d.ts.map +1 -0
- package/build/src/vendored/utils/MimeHelper.js +174 -0
- package/build/src/vendored/utils/MimeHelper.js.map +1 -0
- package/build/src/vendored/utils/MimeHelper.test.d.ts +2 -0
- package/build/src/vendored/utils/MimeHelper.test.d.ts.map +1 -0
- package/build/src/vendored/utils/MimeHelper.test.js +47 -0
- package/build/src/vendored/utils/MimeHelper.test.js.map +1 -0
- package/build/src/vendored/utils/constants.d.ts +9 -0
- package/build/src/vendored/utils/constants.d.ts.map +1 -0
- package/build/src/vendored/utils/constants.js +9 -0
- package/build/src/vendored/utils/constants.js.map +1 -0
- package/build/src/vendored/utils/logger.d.ts +3 -0
- package/build/src/vendored/utils/logger.d.ts.map +1 -0
- package/build/src/vendored/utils/logger.js +15 -0
- package/build/src/vendored/utils/logger.js.map +1 -0
- package/build/src/vendored/utils/paths.d.ts +2 -0
- package/build/src/vendored/utils/paths.d.ts.map +1 -0
- package/build/src/vendored/utils/paths.js +19 -0
- package/build/src/vendored/utils/paths.js.map +1 -0
- package/build/src/vendored/utils/validation.d.ts +100 -0
- package/build/src/vendored/utils/validation.d.ts.map +1 -0
- package/build/src/vendored/utils/validation.js +161 -0
- package/build/src/vendored/utils/validation.js.map +1 -0
- package/build/src/version.d.ts +2 -0
- package/build/src/version.d.ts.map +1 -0
- package/build/src/version.js +29 -0
- package/build/src/version.js.map +1 -0
- package/build/src/version.test.d.ts +2 -0
- package/build/src/version.test.d.ts.map +1 -0
- package/build/src/version.test.js +19 -0
- package/build/src/version.test.js.map +1 -0
- package/build/tsconfig.tsbuildinfo +1 -0
- package/package.json +69 -4
|
@@ -0,0 +1,663 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2025 Google LLC
|
|
4
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
5
|
+
*/
|
|
6
|
+
import { google } from 'googleapis';
|
|
7
|
+
import { logToFile } from '../utils/logger';
|
|
8
|
+
import { extractDocId } from '../utils/IdUtils';
|
|
9
|
+
import { gaxiosOptions } from '../utils/GaxiosConfig';
|
|
10
|
+
import { extractDocumentId as validateAndExtractDocId } from '../utils/validation';
|
|
11
|
+
// Field mask for documents.get when reading tab content. Selects only the
|
|
12
|
+
// structural fields we use; broader masks like 'tabs' alone trigger
|
|
13
|
+
// "comment-specific fields" errors when combined with includeTabsContent.
|
|
14
|
+
export const TABS_FIELD_MASK = 'tabs(tabProperties,documentTab(body,headers,footers,footnotes))';
|
|
15
|
+
export const DEFAULT_DOCS_TEXT_LIMIT = 20_000;
|
|
16
|
+
export const PREVIEW_DOCS_TEXT_LIMIT = 2_000;
|
|
17
|
+
export const MAX_DOCS_TEXT_LIMIT = 100_000;
|
|
18
|
+
export function boundDocsText(text, limit) {
|
|
19
|
+
const totalCharacters = text.length;
|
|
20
|
+
if (totalCharacters <= limit)
|
|
21
|
+
return { text, truncated: false, totalCharacters };
|
|
22
|
+
return {
|
|
23
|
+
text: `${text.slice(0, limit)}\n\n[Content truncated: ${totalCharacters - limit} characters omitted. Request a larger limit to continue, up to ${MAX_DOCS_TEXT_LIMIT}.]`,
|
|
24
|
+
truncated: true,
|
|
25
|
+
totalCharacters
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
export class DocsService {
|
|
29
|
+
/**
|
|
30
|
+
* Recursively flattens a tab tree into a single array,
|
|
31
|
+
* so that nested (child) tabs are included alongside top-level ones.
|
|
32
|
+
*/
|
|
33
|
+
_flattenTabs(tabs) {
|
|
34
|
+
return tabs.flatMap((tab) => {
|
|
35
|
+
const children = tab.childTabs ? this._flattenTabs(tab.childTabs) : [];
|
|
36
|
+
return [tab, ...children];
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
constructor(authManager) {
|
|
40
|
+
this.authManager = authManager;
|
|
41
|
+
this.getSuggestions = async ({ documentId }) => {
|
|
42
|
+
logToFile(`[DocsService] Starting getSuggestions for document: ${documentId}`);
|
|
43
|
+
try {
|
|
44
|
+
const id = extractDocId(documentId) || documentId;
|
|
45
|
+
const docs = await this.getDocsClient();
|
|
46
|
+
const res = await docs.documents.get({
|
|
47
|
+
documentId: id,
|
|
48
|
+
suggestionsViewMode: 'SUGGESTIONS_INLINE',
|
|
49
|
+
fields: 'title,body',
|
|
50
|
+
});
|
|
51
|
+
const suggestions = this._extractSuggestions(res.data.body);
|
|
52
|
+
logToFile(`[DocsService] Found ${suggestions.length} suggestions for document: ${id}`);
|
|
53
|
+
return {
|
|
54
|
+
content: [
|
|
55
|
+
{
|
|
56
|
+
type: 'text',
|
|
57
|
+
text: JSON.stringify({ title: res.data.title, suggestions }, null, 2),
|
|
58
|
+
},
|
|
59
|
+
],
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
catch (error) {
|
|
63
|
+
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
64
|
+
logToFile(`[DocsService] Error during docs.getSuggestions: ${errorMessage}`);
|
|
65
|
+
return {
|
|
66
|
+
isError: true,
|
|
67
|
+
content: [
|
|
68
|
+
{
|
|
69
|
+
type: 'text',
|
|
70
|
+
text: JSON.stringify({ error: errorMessage }),
|
|
71
|
+
},
|
|
72
|
+
],
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
this.create = async ({ title, content, }) => {
|
|
77
|
+
logToFile(`[DocsService] Starting create with title: ${title}, content: ${content ? 'true' : 'false'}`);
|
|
78
|
+
try {
|
|
79
|
+
logToFile('[DocsService] Calling docs.documents.create');
|
|
80
|
+
const docs = await this.getDocsClient();
|
|
81
|
+
const doc = await docs.documents.create({
|
|
82
|
+
requestBody: { title },
|
|
83
|
+
});
|
|
84
|
+
logToFile('[DocsService] docs.documents.create finished');
|
|
85
|
+
const documentId = doc.data.documentId;
|
|
86
|
+
const docTitle = doc.data.title;
|
|
87
|
+
// Insert content if provided
|
|
88
|
+
if (content) {
|
|
89
|
+
logToFile('[DocsService] Inserting content into new doc');
|
|
90
|
+
await docs.documents.batchUpdate({
|
|
91
|
+
documentId,
|
|
92
|
+
requestBody: {
|
|
93
|
+
requests: [
|
|
94
|
+
{
|
|
95
|
+
insertText: {
|
|
96
|
+
location: { index: 1 },
|
|
97
|
+
text: content,
|
|
98
|
+
},
|
|
99
|
+
},
|
|
100
|
+
],
|
|
101
|
+
},
|
|
102
|
+
});
|
|
103
|
+
logToFile('[DocsService] Content insertion finished');
|
|
104
|
+
}
|
|
105
|
+
return {
|
|
106
|
+
content: [
|
|
107
|
+
{
|
|
108
|
+
type: 'text',
|
|
109
|
+
text: JSON.stringify({
|
|
110
|
+
documentId,
|
|
111
|
+
title: docTitle,
|
|
112
|
+
}),
|
|
113
|
+
},
|
|
114
|
+
],
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
catch (error) {
|
|
118
|
+
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
119
|
+
logToFile(`Error during docs.create: ${errorMessage}`);
|
|
120
|
+
return {
|
|
121
|
+
content: [
|
|
122
|
+
{
|
|
123
|
+
type: 'text',
|
|
124
|
+
text: JSON.stringify({ error: errorMessage }),
|
|
125
|
+
},
|
|
126
|
+
],
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
};
|
|
130
|
+
this.writeText = async ({ documentId, text, position = 'end', tabId, }) => {
|
|
131
|
+
logToFile(`[DocsService] Starting writeText for document: ${documentId}, position: ${position}, tabId: ${tabId}`);
|
|
132
|
+
try {
|
|
133
|
+
const id = extractDocId(documentId) || documentId;
|
|
134
|
+
const docs = await this.getDocsClient();
|
|
135
|
+
// Optimize: when appending to the main body, omit location to skip
|
|
136
|
+
// an extra documents.get API call — the Docs API auto-appends.
|
|
137
|
+
if (position === 'end' && !tabId) {
|
|
138
|
+
await docs.documents.batchUpdate({
|
|
139
|
+
documentId: id,
|
|
140
|
+
requestBody: {
|
|
141
|
+
requests: [{ insertText: { text } }],
|
|
142
|
+
},
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
else {
|
|
146
|
+
let index;
|
|
147
|
+
if (position === 'beginning') {
|
|
148
|
+
index = 1;
|
|
149
|
+
}
|
|
150
|
+
else if (position === 'end') {
|
|
151
|
+
// Discover the end index by reading the document (required for tabs)
|
|
152
|
+
const res = await docs.documents.get({
|
|
153
|
+
documentId: id,
|
|
154
|
+
fields: TABS_FIELD_MASK,
|
|
155
|
+
includeTabsContent: true,
|
|
156
|
+
});
|
|
157
|
+
const tabs = this._flattenTabs(res.data.tabs || []);
|
|
158
|
+
let content;
|
|
159
|
+
if (tabId) {
|
|
160
|
+
const tab = tabs.find((t) => t.tabProperties?.tabId === tabId);
|
|
161
|
+
if (!tab) {
|
|
162
|
+
throw new Error(`Tab with ID ${tabId} not found.`);
|
|
163
|
+
}
|
|
164
|
+
content = tab.documentTab?.body?.content;
|
|
165
|
+
}
|
|
166
|
+
else if (tabs.length > 0) {
|
|
167
|
+
content = tabs[0].documentTab?.body?.content;
|
|
168
|
+
}
|
|
169
|
+
const lastElement = content?.[content.length - 1];
|
|
170
|
+
const endIndex = lastElement?.endIndex || 1;
|
|
171
|
+
index = Math.max(1, endIndex - 1);
|
|
172
|
+
}
|
|
173
|
+
else {
|
|
174
|
+
// Treat as a numeric index
|
|
175
|
+
index = parseInt(position, 10);
|
|
176
|
+
if (isNaN(index) || index < 1) {
|
|
177
|
+
throw new Error(`Invalid position: "${position}". Use "beginning", "end", or a positive integer index.`);
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
await docs.documents.batchUpdate({
|
|
181
|
+
documentId: id,
|
|
182
|
+
requestBody: {
|
|
183
|
+
requests: [
|
|
184
|
+
{
|
|
185
|
+
insertText: {
|
|
186
|
+
location: {
|
|
187
|
+
index,
|
|
188
|
+
tabId: tabId,
|
|
189
|
+
},
|
|
190
|
+
text,
|
|
191
|
+
},
|
|
192
|
+
},
|
|
193
|
+
],
|
|
194
|
+
},
|
|
195
|
+
});
|
|
196
|
+
}
|
|
197
|
+
logToFile(`[DocsService] Finished writeText for document: ${id}`);
|
|
198
|
+
return {
|
|
199
|
+
content: [
|
|
200
|
+
{
|
|
201
|
+
type: 'text',
|
|
202
|
+
text: `Successfully wrote text to document ${id} at position ${position}`,
|
|
203
|
+
},
|
|
204
|
+
],
|
|
205
|
+
};
|
|
206
|
+
}
|
|
207
|
+
catch (error) {
|
|
208
|
+
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
209
|
+
logToFile(`[DocsService] Error during docs.writeText: ${errorMessage}`);
|
|
210
|
+
return {
|
|
211
|
+
content: [
|
|
212
|
+
{
|
|
213
|
+
type: 'text',
|
|
214
|
+
text: JSON.stringify({ error: errorMessage }),
|
|
215
|
+
},
|
|
216
|
+
],
|
|
217
|
+
};
|
|
218
|
+
}
|
|
219
|
+
};
|
|
220
|
+
this.formatText = async ({ documentId, formats, tabId, }) => {
|
|
221
|
+
logToFile(`[DocsService] Starting formatText for document: ${documentId}, ${formats.length} format(s)`);
|
|
222
|
+
try {
|
|
223
|
+
const id = extractDocId(documentId) || documentId;
|
|
224
|
+
const requests = [];
|
|
225
|
+
for (const format of formats) {
|
|
226
|
+
const range = {
|
|
227
|
+
startIndex: format.startIndex,
|
|
228
|
+
endIndex: format.endIndex,
|
|
229
|
+
tabId: tabId,
|
|
230
|
+
};
|
|
231
|
+
const headingStyle = DocsService.HEADING_STYLES[format.style.toLowerCase()];
|
|
232
|
+
if (headingStyle) {
|
|
233
|
+
requests.push({
|
|
234
|
+
updateParagraphStyle: {
|
|
235
|
+
range,
|
|
236
|
+
paragraphStyle: {
|
|
237
|
+
namedStyleType: headingStyle,
|
|
238
|
+
},
|
|
239
|
+
fields: 'namedStyleType',
|
|
240
|
+
},
|
|
241
|
+
});
|
|
242
|
+
continue;
|
|
243
|
+
}
|
|
244
|
+
const textStyle = DocsService.TEXT_STYLES[format.style.toLowerCase()];
|
|
245
|
+
if (textStyle) {
|
|
246
|
+
requests.push({
|
|
247
|
+
updateTextStyle: {
|
|
248
|
+
range,
|
|
249
|
+
textStyle,
|
|
250
|
+
fields: Object.keys(textStyle).join(','),
|
|
251
|
+
},
|
|
252
|
+
});
|
|
253
|
+
continue;
|
|
254
|
+
}
|
|
255
|
+
if (format.style.toLowerCase() === 'code') {
|
|
256
|
+
requests.push({
|
|
257
|
+
updateTextStyle: {
|
|
258
|
+
range,
|
|
259
|
+
textStyle: {
|
|
260
|
+
weightedFontFamily: {
|
|
261
|
+
fontFamily: 'Courier New',
|
|
262
|
+
},
|
|
263
|
+
},
|
|
264
|
+
fields: 'weightedFontFamily',
|
|
265
|
+
},
|
|
266
|
+
});
|
|
267
|
+
continue;
|
|
268
|
+
}
|
|
269
|
+
if (format.style.toLowerCase() === 'link' && format.url) {
|
|
270
|
+
requests.push({
|
|
271
|
+
updateTextStyle: {
|
|
272
|
+
range,
|
|
273
|
+
textStyle: {
|
|
274
|
+
link: {
|
|
275
|
+
url: format.url,
|
|
276
|
+
},
|
|
277
|
+
},
|
|
278
|
+
fields: 'link',
|
|
279
|
+
},
|
|
280
|
+
});
|
|
281
|
+
continue;
|
|
282
|
+
}
|
|
283
|
+
logToFile(`[DocsService] Unknown format style: ${format.style}`);
|
|
284
|
+
}
|
|
285
|
+
if (requests.length === 0) {
|
|
286
|
+
return {
|
|
287
|
+
content: [
|
|
288
|
+
{
|
|
289
|
+
type: 'text',
|
|
290
|
+
text: 'No valid formatting requests to apply.',
|
|
291
|
+
},
|
|
292
|
+
],
|
|
293
|
+
};
|
|
294
|
+
}
|
|
295
|
+
const docs = await this.getDocsClient();
|
|
296
|
+
await docs.documents.batchUpdate({
|
|
297
|
+
documentId: id,
|
|
298
|
+
requestBody: { requests },
|
|
299
|
+
});
|
|
300
|
+
logToFile(`[DocsService] Finished formatText for document: ${id}, applied ${requests.length} format(s)`);
|
|
301
|
+
return {
|
|
302
|
+
content: [
|
|
303
|
+
{
|
|
304
|
+
type: 'text',
|
|
305
|
+
text: `Successfully applied ${requests.length} format(s) to document ${id}`,
|
|
306
|
+
},
|
|
307
|
+
],
|
|
308
|
+
};
|
|
309
|
+
}
|
|
310
|
+
catch (error) {
|
|
311
|
+
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
312
|
+
logToFile(`[DocsService] Error during docs.formatText: ${errorMessage}`);
|
|
313
|
+
return {
|
|
314
|
+
content: [
|
|
315
|
+
{
|
|
316
|
+
type: 'text',
|
|
317
|
+
text: JSON.stringify({ error: errorMessage }),
|
|
318
|
+
},
|
|
319
|
+
],
|
|
320
|
+
};
|
|
321
|
+
}
|
|
322
|
+
};
|
|
323
|
+
this.getText = async ({ documentId, tabId, limit, preview = false, }) => {
|
|
324
|
+
logToFile(`[DocsService] Starting getText for document: ${documentId}, tabId: ${tabId}`);
|
|
325
|
+
try {
|
|
326
|
+
const textLimit = limit ?? (preview ? PREVIEW_DOCS_TEXT_LIMIT : DEFAULT_DOCS_TEXT_LIMIT);
|
|
327
|
+
if (!Number.isInteger(textLimit) ||
|
|
328
|
+
textLimit < 1 ||
|
|
329
|
+
textLimit > MAX_DOCS_TEXT_LIMIT) {
|
|
330
|
+
throw new Error(`limit must be an integer from 1 to ${MAX_DOCS_TEXT_LIMIT}.`);
|
|
331
|
+
}
|
|
332
|
+
const id = validateAndExtractDocId(documentId);
|
|
333
|
+
const docs = await this.getDocsClient();
|
|
334
|
+
const res = await docs.documents.get({
|
|
335
|
+
documentId: id,
|
|
336
|
+
fields: `title,${TABS_FIELD_MASK}`,
|
|
337
|
+
includeTabsContent: true,
|
|
338
|
+
suggestionsViewMode: 'PREVIEW_WITHOUT_SUGGESTIONS',
|
|
339
|
+
});
|
|
340
|
+
const docTitle = res.data.title;
|
|
341
|
+
const tabs = this._flattenTabs(res.data.tabs || []);
|
|
342
|
+
if (tabId) {
|
|
343
|
+
const tab = tabs.find((t) => t.tabProperties?.tabId === tabId);
|
|
344
|
+
if (!tab)
|
|
345
|
+
throw new Error(`Tab with ID ${tabId} not found.`);
|
|
346
|
+
const content = tab.documentTab?.body?.content;
|
|
347
|
+
if (!content)
|
|
348
|
+
return { content: [{ type: 'text', text: '' }] };
|
|
349
|
+
let text = docTitle ? `Document Title: ${docTitle}\n\n` : '';
|
|
350
|
+
content.forEach((element) => {
|
|
351
|
+
text += this._readStructuralElement(element);
|
|
352
|
+
});
|
|
353
|
+
return {
|
|
354
|
+
content: [{ type: 'text', text: boundDocsText(text, textLimit).text }],
|
|
355
|
+
};
|
|
356
|
+
}
|
|
357
|
+
if (tabs.length === 0)
|
|
358
|
+
return { content: [{ type: 'text', text: '' }] };
|
|
359
|
+
if (tabs.length === 1) {
|
|
360
|
+
const tab = tabs[0];
|
|
361
|
+
let text = docTitle ? `Document Title: ${docTitle}\n\n` : '';
|
|
362
|
+
tab.documentTab?.body?.content?.forEach((element) => {
|
|
363
|
+
text += this._readStructuralElement(element);
|
|
364
|
+
});
|
|
365
|
+
return {
|
|
366
|
+
content: [{ type: 'text', text: boundDocsText(text, textLimit).text }],
|
|
367
|
+
};
|
|
368
|
+
}
|
|
369
|
+
const tabsData = tabs.map((tab, index) => {
|
|
370
|
+
let tabText = '';
|
|
371
|
+
tab.documentTab?.body?.content?.forEach((element) => {
|
|
372
|
+
tabText += this._readStructuralElement(element);
|
|
373
|
+
});
|
|
374
|
+
const bounded = boundDocsText(tabText, textLimit);
|
|
375
|
+
return {
|
|
376
|
+
tabId: tab.tabProperties?.tabId,
|
|
377
|
+
title: tab.tabProperties?.title,
|
|
378
|
+
content: bounded.text,
|
|
379
|
+
truncated: bounded.truncated,
|
|
380
|
+
totalCharacters: bounded.totalCharacters,
|
|
381
|
+
index,
|
|
382
|
+
};
|
|
383
|
+
});
|
|
384
|
+
return {
|
|
385
|
+
content: [
|
|
386
|
+
{
|
|
387
|
+
type: 'text',
|
|
388
|
+
text: JSON.stringify({ title: docTitle, tabs: tabsData }, null, 2),
|
|
389
|
+
},
|
|
390
|
+
],
|
|
391
|
+
};
|
|
392
|
+
}
|
|
393
|
+
catch (error) {
|
|
394
|
+
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
395
|
+
logToFile(`[DocsService] Error during docs.getText: ${errorMessage}`);
|
|
396
|
+
return {
|
|
397
|
+
content: [
|
|
398
|
+
{
|
|
399
|
+
type: 'text',
|
|
400
|
+
text: JSON.stringify({ error: errorMessage }),
|
|
401
|
+
},
|
|
402
|
+
],
|
|
403
|
+
};
|
|
404
|
+
}
|
|
405
|
+
};
|
|
406
|
+
this.replaceText = async ({ documentId, findText, replaceText, tabId, }) => {
|
|
407
|
+
logToFile(`[DocsService] Starting replaceText for document: ${documentId}, tabId: ${tabId}`);
|
|
408
|
+
try {
|
|
409
|
+
const id = extractDocId(documentId) || documentId;
|
|
410
|
+
const docs = await this.getDocsClient();
|
|
411
|
+
// Get the document to find where the text will be replaced
|
|
412
|
+
const docBefore = await docs.documents.get({
|
|
413
|
+
documentId: id,
|
|
414
|
+
fields: TABS_FIELD_MASK,
|
|
415
|
+
includeTabsContent: true,
|
|
416
|
+
});
|
|
417
|
+
const tabs = this._flattenTabs(docBefore.data.tabs || []);
|
|
418
|
+
const requests = [];
|
|
419
|
+
if (tabId) {
|
|
420
|
+
const tab = tabs.find((t) => t.tabProperties?.tabId === tabId);
|
|
421
|
+
if (!tab) {
|
|
422
|
+
throw new Error(`Tab with ID ${tabId} not found.`);
|
|
423
|
+
}
|
|
424
|
+
const content = tab.documentTab?.body?.content;
|
|
425
|
+
const tabRequests = this._generateReplacementRequests(content, tabId, findText, replaceText);
|
|
426
|
+
// PERFORMANCE: Avoid spread operator on potentially large arrays to prevent "Maximum call stack size exceeded" errors
|
|
427
|
+
for (const request of tabRequests) {
|
|
428
|
+
requests.push(request);
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
else {
|
|
432
|
+
for (const tab of tabs) {
|
|
433
|
+
const currentTabId = tab.tabProperties?.tabId;
|
|
434
|
+
const content = tab.documentTab?.body?.content;
|
|
435
|
+
const tabRequests = this._generateReplacementRequests(content, currentTabId, findText, replaceText);
|
|
436
|
+
// PERFORMANCE: Avoid spread operator on potentially large arrays to prevent "Maximum call stack size exceeded" errors
|
|
437
|
+
for (const request of tabRequests) {
|
|
438
|
+
requests.push(request);
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
if (requests.length > 0) {
|
|
443
|
+
await docs.documents.batchUpdate({
|
|
444
|
+
documentId: id,
|
|
445
|
+
requestBody: {
|
|
446
|
+
requests,
|
|
447
|
+
},
|
|
448
|
+
});
|
|
449
|
+
}
|
|
450
|
+
logToFile(`[DocsService] Finished replaceText for document: ${id}`);
|
|
451
|
+
return {
|
|
452
|
+
content: [
|
|
453
|
+
{
|
|
454
|
+
type: 'text',
|
|
455
|
+
text: `Successfully replaced text in document ${id}`,
|
|
456
|
+
},
|
|
457
|
+
],
|
|
458
|
+
};
|
|
459
|
+
}
|
|
460
|
+
catch (error) {
|
|
461
|
+
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
462
|
+
logToFile(`[DocsService] Error during docs.replaceText: ${errorMessage}`);
|
|
463
|
+
return {
|
|
464
|
+
content: [
|
|
465
|
+
{
|
|
466
|
+
type: 'text',
|
|
467
|
+
text: JSON.stringify({ error: errorMessage }),
|
|
468
|
+
},
|
|
469
|
+
],
|
|
470
|
+
};
|
|
471
|
+
}
|
|
472
|
+
};
|
|
473
|
+
}
|
|
474
|
+
async getDocsClient() {
|
|
475
|
+
const auth = await this.authManager.getAuthenticatedClient();
|
|
476
|
+
const options = { ...gaxiosOptions, auth };
|
|
477
|
+
return google.docs({ version: 'v1', ...options });
|
|
478
|
+
}
|
|
479
|
+
_extractSuggestions(body) {
|
|
480
|
+
const suggestions = [];
|
|
481
|
+
if (!body?.content) {
|
|
482
|
+
return suggestions;
|
|
483
|
+
}
|
|
484
|
+
const processElements = (elements) => {
|
|
485
|
+
elements?.forEach((element) => {
|
|
486
|
+
if (element.paragraph) {
|
|
487
|
+
// Handle paragraph-level style suggestions
|
|
488
|
+
if (element.paragraph.suggestedParagraphStyleChanges) {
|
|
489
|
+
for (const [suggestionId, suggestion] of Object.entries(element.paragraph.suggestedParagraphStyleChanges)) {
|
|
490
|
+
suggestions.push({
|
|
491
|
+
type: 'paragraphStyleChange',
|
|
492
|
+
text: this._getParagraphText(element.paragraph),
|
|
493
|
+
suggestionIds: [suggestionId],
|
|
494
|
+
namedStyleType: suggestion?.paragraphStyle?.namedStyleType ?? undefined,
|
|
495
|
+
startIndex: element.startIndex ?? undefined,
|
|
496
|
+
endIndex: element.endIndex ?? undefined,
|
|
497
|
+
});
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
// Handle text-run-level suggestions within the paragraph
|
|
501
|
+
element.paragraph.elements?.forEach((pElement) => {
|
|
502
|
+
if (pElement.textRun) {
|
|
503
|
+
const baseSuggestion = {
|
|
504
|
+
text: pElement.textRun.content || '',
|
|
505
|
+
startIndex: pElement.startIndex ?? undefined,
|
|
506
|
+
endIndex: pElement.endIndex ?? undefined,
|
|
507
|
+
};
|
|
508
|
+
if (pElement.textRun.suggestedInsertionIds) {
|
|
509
|
+
suggestions.push({
|
|
510
|
+
...baseSuggestion,
|
|
511
|
+
type: 'insertion',
|
|
512
|
+
suggestionIds: pElement.textRun.suggestedInsertionIds,
|
|
513
|
+
});
|
|
514
|
+
}
|
|
515
|
+
if (pElement.textRun.suggestedDeletionIds) {
|
|
516
|
+
suggestions.push({
|
|
517
|
+
...baseSuggestion,
|
|
518
|
+
type: 'deletion',
|
|
519
|
+
suggestionIds: pElement.textRun.suggestedDeletionIds,
|
|
520
|
+
});
|
|
521
|
+
}
|
|
522
|
+
if (pElement.textRun.suggestedTextStyleChanges) {
|
|
523
|
+
suggestions.push({
|
|
524
|
+
...baseSuggestion,
|
|
525
|
+
type: 'styleChange',
|
|
526
|
+
suggestionIds: Object.keys(pElement.textRun.suggestedTextStyleChanges),
|
|
527
|
+
textStyle: pElement.textRun.textStyle,
|
|
528
|
+
});
|
|
529
|
+
}
|
|
530
|
+
}
|
|
531
|
+
});
|
|
532
|
+
}
|
|
533
|
+
else if (element.table) {
|
|
534
|
+
element.table.tableRows?.forEach((row) => {
|
|
535
|
+
row.tableCells?.forEach((cell) => {
|
|
536
|
+
processElements(cell.content);
|
|
537
|
+
});
|
|
538
|
+
});
|
|
539
|
+
}
|
|
540
|
+
});
|
|
541
|
+
};
|
|
542
|
+
processElements(body.content);
|
|
543
|
+
return suggestions;
|
|
544
|
+
}
|
|
545
|
+
_getParagraphText(paragraph) {
|
|
546
|
+
if (!paragraph?.elements) {
|
|
547
|
+
return '';
|
|
548
|
+
}
|
|
549
|
+
return paragraph.elements
|
|
550
|
+
.map((pElement) => pElement.textRun?.content || '')
|
|
551
|
+
.join('');
|
|
552
|
+
}
|
|
553
|
+
_readStructuralElement(element) {
|
|
554
|
+
let text = '';
|
|
555
|
+
if (element.paragraph) {
|
|
556
|
+
element.paragraph.elements?.forEach((pElement) => {
|
|
557
|
+
if (pElement.textRun && pElement.textRun.content) {
|
|
558
|
+
text += pElement.textRun.content;
|
|
559
|
+
}
|
|
560
|
+
else if (pElement.person?.personProperties) {
|
|
561
|
+
text += this._renderPersonChip(pElement.person.personProperties);
|
|
562
|
+
}
|
|
563
|
+
else if (pElement.richLink?.richLinkProperties) {
|
|
564
|
+
text += this._renderRichLinkChip(pElement.richLink.richLinkProperties);
|
|
565
|
+
}
|
|
566
|
+
else if (pElement.dateElement?.dateElementProperties) {
|
|
567
|
+
text += this._renderDateChip(pElement.dateElement.dateElementProperties);
|
|
568
|
+
}
|
|
569
|
+
});
|
|
570
|
+
}
|
|
571
|
+
else if (element.table) {
|
|
572
|
+
element.table.tableRows?.forEach((row) => {
|
|
573
|
+
row.tableCells?.forEach((cell) => {
|
|
574
|
+
cell.content?.forEach((cellContent) => {
|
|
575
|
+
text += this._readStructuralElement(cellContent);
|
|
576
|
+
});
|
|
577
|
+
});
|
|
578
|
+
});
|
|
579
|
+
}
|
|
580
|
+
return text;
|
|
581
|
+
}
|
|
582
|
+
_renderPersonChip(props) {
|
|
583
|
+
const { name, email } = props;
|
|
584
|
+
if (email) {
|
|
585
|
+
return `[${name || email}](mailto:${email})`;
|
|
586
|
+
}
|
|
587
|
+
return name || '';
|
|
588
|
+
}
|
|
589
|
+
_renderRichLinkChip(props) {
|
|
590
|
+
const { title, uri } = props;
|
|
591
|
+
if (uri) {
|
|
592
|
+
return `[${title || uri}](${uri})`;
|
|
593
|
+
}
|
|
594
|
+
return title || '';
|
|
595
|
+
}
|
|
596
|
+
_renderDateChip(props) {
|
|
597
|
+
const { displayText, timestamp } = props;
|
|
598
|
+
return displayText || timestamp || '';
|
|
599
|
+
}
|
|
600
|
+
_generateReplacementRequests(content, tabId, findText, newText) {
|
|
601
|
+
const requests = [];
|
|
602
|
+
const documentText = this._getFullDocumentText(content);
|
|
603
|
+
const occurrences = [];
|
|
604
|
+
let searchIndex = 0;
|
|
605
|
+
while ((searchIndex = documentText.indexOf(findText, searchIndex)) !== -1) {
|
|
606
|
+
occurrences.push(searchIndex + 1);
|
|
607
|
+
searchIndex += findText.length;
|
|
608
|
+
}
|
|
609
|
+
const lengthDiff = newText.length - findText.length;
|
|
610
|
+
let cumulativeOffset = 0;
|
|
611
|
+
for (let i = 0; i < occurrences.length; i++) {
|
|
612
|
+
const occurrence = occurrences[i];
|
|
613
|
+
const adjustedPosition = occurrence + cumulativeOffset;
|
|
614
|
+
// Delete old text
|
|
615
|
+
requests.push({
|
|
616
|
+
deleteContentRange: {
|
|
617
|
+
range: {
|
|
618
|
+
tabId: tabId,
|
|
619
|
+
startIndex: adjustedPosition,
|
|
620
|
+
endIndex: adjustedPosition + findText.length,
|
|
621
|
+
},
|
|
622
|
+
},
|
|
623
|
+
});
|
|
624
|
+
// Insert new text
|
|
625
|
+
requests.push({
|
|
626
|
+
insertText: {
|
|
627
|
+
location: {
|
|
628
|
+
tabId: tabId,
|
|
629
|
+
index: adjustedPosition,
|
|
630
|
+
},
|
|
631
|
+
text: newText,
|
|
632
|
+
},
|
|
633
|
+
});
|
|
634
|
+
cumulativeOffset += lengthDiff;
|
|
635
|
+
}
|
|
636
|
+
return requests;
|
|
637
|
+
}
|
|
638
|
+
_getFullDocumentText(content) {
|
|
639
|
+
let text = '';
|
|
640
|
+
if (content) {
|
|
641
|
+
content.forEach((element) => {
|
|
642
|
+
text += this._readStructuralElement(element);
|
|
643
|
+
});
|
|
644
|
+
}
|
|
645
|
+
return text;
|
|
646
|
+
}
|
|
647
|
+
}
|
|
648
|
+
DocsService.HEADING_STYLES = {
|
|
649
|
+
heading1: 'HEADING_1',
|
|
650
|
+
heading2: 'HEADING_2',
|
|
651
|
+
heading3: 'HEADING_3',
|
|
652
|
+
heading4: 'HEADING_4',
|
|
653
|
+
heading5: 'HEADING_5',
|
|
654
|
+
heading6: 'HEADING_6',
|
|
655
|
+
normalText: 'NORMAL_TEXT',
|
|
656
|
+
};
|
|
657
|
+
DocsService.TEXT_STYLES = {
|
|
658
|
+
bold: { bold: true },
|
|
659
|
+
italic: { italic: true },
|
|
660
|
+
underline: { underline: true },
|
|
661
|
+
strikethrough: { strikethrough: true },
|
|
662
|
+
};
|
|
663
|
+
//# sourceMappingURL=DocsService.js.map
|