aboutsummaryrefslogtreecommitdiff
path: root/src/client/apis/google_docs/GoogleApiClientUtils.ts
blob: c1ac352b1f4ea883552f6fc3755e298a2785e22e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/* eslint-disable no-use-before-define */
import { docs_v1 as docsV1 } 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 namespace GoogleApiClientUtils {
    export enum Actions {
        Create = 'create',
        Retrieve = 'retrieve',
        Update = 'update',
    }

    export namespace Docs {
        export type RetrievalResult = Opt<docsV1.Schema$Document>;
        export type UpdateResult = Opt<docsV1.Schema$BatchUpdateDocumentResponse>;

        export interface UpdateOptions {
            documentId: DocumentId;
            requests: docsV1.Schema$Request[];
        }

        export enum WriteMode {
            Insert,
            Replace,
        }

        export type DocumentId = string;
        export type Reference = DocumentId | CreateOptions;
        export interface Content {
            text: string | string[];
            requests: docsV1.Schema$Request[];
        }
        export type IdHandler = (id: DocumentId) => unknown;
        export type CreationResult = Opt<DocumentId>;
        export type ReadLinesResult = Opt<{ title?: string; bodyLines?: string[] }>;
        export type ReadResult = { title: string; body: string };
        export interface ImportResult {
            title: string;
            text: string;
            state: EditorState;
        }

        export interface CreateOptions {
            title?: string; // if excluded, will use a default title annotated with the current date
        }

        export interface RetrieveOptions {
            documentId: DocumentId;
        }

        export interface ReadOptions {
            documentId: DocumentId;
            removeNewlines?: boolean;
        }

        export interface WriteOptions {
            mode: WriteMode;
            content: Content;
            reference: Reference;
            index?: number; // if excluded, will compute the last index of the document and append the content there
        }

        /**
         * 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()})`,
                },
            };
            try {
                const schema: docsV1.Schema$Document = await Networking.PostToServer(path, parameters);
                return schema.documentId === null ? undefined : schema.documentId;
            } catch {
                return undefined;
            }
        };

        export namespace Utils {
            export type ExtractResult = { text: string; paragraphs: DeconstructedParagraph[] };
            export const extractText = (document: docsV1.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 docsV1.Schema$TextRun).content)
                            .join('')
                    )
                    .join('');
                text = text.substring(0, text.length - 1);
                removeNewlines && text.replace(/\n/g, '');
                return { text, paragraphs };
            };

            export type ContentArray = (docsV1.Schema$TextRun | docsV1.Schema$InlineObjectElement)[];
            export type DeconstructedParagraph = { contents: ContentArray; bullet: Opt<number> };
            const extractParagraphs = (document: docsV1.Schema$Document, filterEmpty = true): DeconstructedParagraph[] => {
                const fragments: DeconstructedParagraph[] = [];
                if (document.body && document.body.content) {
                    for (const element of document.body.content) {
                        const runs: ContentArray = [];
                        let bullet: Opt<number>;
                        if (element.paragraph) {
                            if (element.paragraph.elements) {
                                for (const inner of element.paragraph.elements) {
                                    if (inner) {
                                        if (inner.textRun) {
                                            const run = inner.textRun;
                                            (run.content || !filterEmpty) && runs.push(inner.textRun);
                                        } else if (inner.inlineObjectElement) {
                                            runs.push(inner.inlineObjectElement);
                                        }
                                    }
                                }
                            }
                            if (element.paragraph.bullet) {
                                bullet = element.paragraph.bullet.nestingLevel || 0;
                            }
                        }
                        (runs.length || !filterEmpty) && fragments.push({ contents: runs, bullet });
                    }
                }
                return fragments;
            };

            export const endOf = (schema: docsV1.Schema$Document): number | undefined => {
                if (schema.body && schema.body.content) {
                    const paragraphs = schema.body.content.filter(el => el.paragraph);
                    if (paragraphs.length) {
                        const target = paragraphs[paragraphs.length - 1];
                        if (target.paragraph && target.paragraph.elements) {
                            const length = target.paragraph.elements.length;
                            if (length) {
                                const final = target.paragraph.elements[length - 1];
                                return final.endIndex ? final.endIndex - 1 : undefined;
                            }
                        }
                    }
                }
                return undefined;
            };

            export const initialize = async (reference: Reference) => (typeof reference === 'string' ? reference : create(reference));
        }

        export const retrieve = async (options: RetrieveOptions): Promise<RetrievalResult> => {
            const path = `/googleDocs/Documents/${Actions.Retrieve}`;
            try {
                const parameters = { documentId: options.documentId };
                const schema: RetrievalResult = await Networking.PostToServer(path, parameters);
                return schema;
            } catch {
                return undefined;
            }
        };

        export const update = async (options: UpdateOptions): Promise<UpdateResult> => {
            const path = `/googleDocs/Documents/${Actions.Update}`;
            const parameters = {
                documentId: options.documentId,
                requestBody: {
                    requests: options.requests,
                },
            };
            try {
                const replies: UpdateResult = await Networking.PostToServer(path, parameters);
                return replies;
            } catch {
                return undefined;
            }
        };

        export const read = async (options: ReadOptions): Promise<Opt<ReadResult>> =>
            retrieve({ documentId: options.documentId }).then(document => {
                if (document) {
                    const title = document.title!;
                    const body = Utils.extractText(document, options.removeNewlines).text;
                    return { title, body };
                }
                return undefined;
            });

        export const readLines = async (options: ReadOptions): Promise<Opt<ReadLinesResult>> =>
            retrieve({ documentId: options.documentId }).then(document => {
                if (document) {
                    const { title } = document;
                    let bodyLines = Utils.extractText(document).text.split('\n');
                    options.removeNewlines && (bodyLines = bodyLines.filter(line => line.length));
                    return { title: title ?? '', bodyLines };
                }
                return undefined;
            });

        export const setStyle = async (options: UpdateOptions) => {
            const replies = await update({
                documentId: options.documentId,
                requests: options.requests,
            });
            if (replies && 'errors' in replies) {
                console.log('Write operation failed:');
                console.log(replies); //.errors.map((error: any) => error.message));
            }
            return replies;
        };

        export const write = async (options: WriteOptions): Promise<UpdateResult> => {
            const requests: docsV1.Schema$Request[] = [];
            const documentId = await Utils.initialize(options.reference);
            if (!documentId) {
                return undefined;
            }
            let { index } = options;
            const { mode } = options;
            if (!(index && mode === WriteMode.Insert)) {
                const schema = await retrieve({ documentId });
                if (!schema || !(index = Utils.endOf(schema))) {
                    return undefined;
                }
            }
            if (mode === WriteMode.Replace) {
                index > 1 &&
                    requests.push({
                        deleteContentRange: {
                            range: {
                                startIndex: 1,
                                endIndex: index,
                            },
                        },
                    });
                index = 1;
            }
            const { text } = options.content;
            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 = await update({ documentId, requests });
            if (replies && 'errors' in replies) {
                console.log('Write operation failed:');
                console.log(replies); // .errors.map((error: any) => error.message));
            }
            return replies;
        };
    }
}