diff options
Diffstat (limited to 'src/client/apis/google_docs/GoogleApiClientUtils.ts')
-rw-r--r-- | src/client/apis/google_docs/GoogleApiClientUtils.ts | 119 |
1 files changed, 61 insertions, 58 deletions
diff --git a/src/client/apis/google_docs/GoogleApiClientUtils.ts b/src/client/apis/google_docs/GoogleApiClientUtils.ts index c8f381cc0..3c2f923ea 100644 --- a/src/client/apis/google_docs/GoogleApiClientUtils.ts +++ b/src/client/apis/google_docs/GoogleApiClientUtils.ts @@ -1,22 +1,20 @@ -import { docs_v1 } from "googleapis"; -import { Opt } from "../../../fields/Doc"; -import { isArray } from "util"; -import { EditorState } from "prosemirror-state"; -import { Networking } from "../../Network"; +import { docs_v1 } from 'googleapis'; +import { isArray } from 'util'; +import { EditorState } from 'prosemirror-state'; +import { Opt } from '../../../fields/Doc'; +import { Networking } from '../../Network'; -export const Pulls = "googleDocsPullCount"; -export const Pushes = "googleDocsPushCount"; +export const Pulls = 'googleDocsPullCount'; +export const Pushes = 'googleDocsPushCount'; export namespace GoogleApiClientUtils { - export enum Actions { - Create = "create", - Retrieve = "retrieve", - Update = "update" + Create = 'create', + Retrieve = 'retrieve', + Update = 'update', } export namespace Docs { - export type RetrievalResult = Opt<docs_v1.Schema$Document>; export type UpdateResult = Opt<docs_v1.Schema$BatchUpdateDocumentResponse>; @@ -27,7 +25,7 @@ export namespace GoogleApiClientUtils { export enum WriteMode { Insert, - Replace + Replace, } export type DocumentId = string; @@ -38,8 +36,8 @@ export namespace GoogleApiClientUtils { } export type IdHandler = (id: DocumentId) => any; export type CreationResult = Opt<DocumentId>; - export type ReadLinesResult = Opt<{ title?: string, bodyLines?: string[] }>; - export type ReadResult = { title: string, body: string }; + export type ReadLinesResult = Opt<{ title?: string; bodyLines?: string[] }>; + export type ReadResult = { title: string; body: string }; export interface ImportResult { title: string; text: string; @@ -67,20 +65,20 @@ export namespace GoogleApiClientUtils { } /** - * After following the authentication routine, which connects this API call to the current signed in account - * and grants the appropriate permissions, this function programmatically creates an arbitrary Google Doc which - * should appear in the user's Google Doc library instantaneously. - * - * @param options the title to assign to the new document, and the information necessary - * to store the new documentId returned from the creation process - * @returns the documentId of the newly generated document, or undefined if the creation process fails. - */ + * After following the authentication routine, which connects this API call to the current signed in account + * and grants the appropriate permissions, this function programmatically creates an arbitrary Google Doc which + * should appear in the user's Google Doc library instantaneously. + * + * @param options the title to assign to the new document, and the information necessary + * to store the new documentId returned from the creation process + * @returns the documentId of the newly generated document, or undefined if the creation process fails. + */ export const create = async (options: CreateOptions): Promise<CreationResult> => { const path = `/googleDocs/Documents/${Actions.Create}`; const parameters = { requestBody: { - title: options.title || `Dash Export (${new Date().toDateString()})` - } + title: options.title || `Dash Export (${new Date().toDateString()})`, + }, }; try { const schema: docs_v1.Schema$Document = await Networking.PostToServer(path, parameters); @@ -91,18 +89,24 @@ export namespace GoogleApiClientUtils { }; export namespace Utils { - - export type ExtractResult = { text: string, paragraphs: DeconstructedParagraph[] }; + export type ExtractResult = { text: string; paragraphs: DeconstructedParagraph[] }; export const extractText = (document: docs_v1.Schema$Document, removeNewlines = false): ExtractResult => { const paragraphs = extractParagraphs(document); - let text = paragraphs.map(paragraph => paragraph.contents.filter(content => !("inlineObjectId" in content)).map(run => (run as docs_v1.Schema$TextRun).content).join("")).join(""); + let text = paragraphs + .map(paragraph => + paragraph.contents + .filter(content => !('inlineObjectId' in content)) + .map(run => (run as docs_v1.Schema$TextRun).content) + .join('') + ) + .join(''); text = text.substring(0, text.length - 1); - removeNewlines && text.replace(/\n/g, ""); + removeNewlines && text.replace(/\n/g, ''); return { text, paragraphs }; }; export type ContentArray = (docs_v1.Schema$TextRun | docs_v1.Schema$InlineObjectElement)[]; - export type DeconstructedParagraph = { contents: ContentArray, bullet: Opt<number> }; + export type DeconstructedParagraph = { contents: ContentArray; bullet: Opt<number> }; const extractParagraphs = (document: docs_v1.Schema$Document, filterEmpty = true): DeconstructedParagraph[] => { const fragments: DeconstructedParagraph[] = []; if (document.body && document.body.content) { @@ -148,8 +152,7 @@ export namespace GoogleApiClientUtils { } }; - export const initialize = async (reference: Reference) => typeof reference === "string" ? reference : create(reference); - + export const initialize = async (reference: Reference) => (typeof reference === 'string' ? reference : create(reference)); } export const retrieve = async (options: RetrieveOptions): Promise<RetrievalResult> => { @@ -168,8 +171,8 @@ export namespace GoogleApiClientUtils { const parameters = { documentId: options.documentId, requestBody: { - requests: options.requests - } + requests: options.requests, + }, }; try { const replies: UpdateResult = await Networking.PostToServer(path, parameters); @@ -193,9 +196,9 @@ export namespace GoogleApiClientUtils { return retrieve({ documentId: options.documentId }).then(document => { if (document) { const title = document.title; - let bodyLines = Utils.extractText(document).text.split("\n"); + let bodyLines = Utils.extractText(document).text.split('\n'); options.removeNewlines && (bodyLines = bodyLines.filter(line => line.length)); - return { title: title ?? "", bodyLines }; + return { title: title ?? '', bodyLines }; } }); }; @@ -203,10 +206,10 @@ export namespace GoogleApiClientUtils { export const setStyle = async (options: UpdateOptions) => { const replies: any = await update({ documentId: options.documentId, - requests: options.requests + requests: options.requests, }); - if ("errors" in replies) { - console.log("Write operation failed:"); + if ('errors' in replies) { + console.log('Write operation failed:'); console.log(replies.errors.map((error: any) => error.message)); } return replies; @@ -227,35 +230,35 @@ export namespace GoogleApiClientUtils { } } if (mode === WriteMode.Replace) { - index > 1 && requests.push({ - deleteContentRange: { - range: { - startIndex: 1, - endIndex: index - } - } - }); + index > 1 && + requests.push({ + deleteContentRange: { + range: { + startIndex: 1, + endIndex: index, + }, + }, + }); index = 1; } const text = options.content.text; - text.length && requests.push({ - insertText: { - text: isArray(text) ? text.join("\n") : text, - location: { index } - } - }); + text.length && + requests.push({ + insertText: { + text: isArray(text) ? text.join('\n') : text, + location: { index }, + }, + }); if (!requests.length) { return undefined; } requests.push(...options.content.requests); const replies: any = await update({ documentId, requests }); - if ("errors" in replies) { - console.log("Write operation failed:"); + if ('errors' in replies) { + console.log('Write operation failed:'); console.log(replies.errors.map((error: any) => error.message)); } return replies; }; - } - -}
\ No newline at end of file +} |