aboutsummaryrefslogtreecommitdiff
path: root/src/client/apis/google_docs/GoogleApiClientUtils.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/client/apis/google_docs/GoogleApiClientUtils.ts')
-rw-r--r--src/client/apis/google_docs/GoogleApiClientUtils.ts189
1 files changed, 118 insertions, 71 deletions
diff --git a/src/client/apis/google_docs/GoogleApiClientUtils.ts b/src/client/apis/google_docs/GoogleApiClientUtils.ts
index 821c52270..798886def 100644
--- a/src/client/apis/google_docs/GoogleApiClientUtils.ts
+++ b/src/client/apis/google_docs/GoogleApiClientUtils.ts
@@ -1,4 +1,4 @@
-import { docs_v1 } from "googleapis";
+import { docs_v1, slides_v1 } from "googleapis";
import { PostToServer } from "../../../Utils";
import { RouteStore } from "../../../server/RouteStore";
import { Opt } from "../../../new_fields/Doc";
@@ -9,50 +9,84 @@ export const Pushes = "googleDocsPushCount";
export namespace GoogleApiClientUtils {
- export namespace Docs {
+ export enum Service {
+ Documents = "Documents",
+ Slides = "Slides"
+ }
- export enum Actions {
- Create = "create",
- Retrieve = "retrieve",
- Update = "update"
- }
+ export enum Actions {
+ Create = "create",
+ Retrieve = "retrieve",
+ Update = "update"
+ }
- export enum WriteMode {
- Insert,
- Replace
- }
+ export enum WriteMode {
+ Insert,
+ Replace
+ }
- export type DocumentId = string;
- export type Reference = DocumentId | CreateOptions;
- export type TextContent = string | string[];
- export type IdHandler = (id: DocumentId) => any;
+ export type Identifier = string;
+ export type Reference = Identifier | CreateOptions;
+ export type TextContent = string | string[];
+ export type IdHandler = (id: Identifier) => any;
+ export type CreationResult = Opt<Identifier>;
+ export type ReadLinesResult = Opt<{ title?: string, bodyLines?: string[] }>;
+ export type ReadResult = { title?: string, body?: string };
- export type CreationResult = Opt<DocumentId>;
- export type RetrievalResult = Opt<docs_v1.Schema$Document>;
- export type UpdateResult = Opt<docs_v1.Schema$BatchUpdateDocumentResponse>;
- export type ReadLinesResult = Opt<{ title?: string, bodyLines?: string[] }>;
- export type ReadResult = { title?: string, body?: string };
+ export interface CreateOptions {
+ service: Service;
+ title?: string; // if excluded, will use a default title annotated with the current date
+ }
- export interface CreateOptions {
- handler: IdHandler; // callback to process the documentId of the newly created Google Doc
- title?: string; // if excluded, will use a default title annotated with the current date
- }
+ export interface RetrieveOptions {
+ service: Service;
+ identifier: Identifier;
+ }
- export interface RetrieveOptions {
- documentId: DocumentId;
- }
+ export interface ReadOptions {
+ identifier: Identifier;
+ removeNewlines?: boolean;
+ }
- export type ReadOptions = RetrieveOptions & { removeNewlines?: boolean };
+ export interface WriteOptions {
+ mode: WriteMode;
+ content: TextContent;
+ reference: Reference;
+ index?: number; // if excluded, will compute the last index of the document and append the content there
+ }
- export interface WriteOptions {
- mode: WriteMode;
- content: TextContent;
- 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 = `${RouteStore.googleDocs}/${options.service}/${Actions.Create}`;
+ const parameters = {
+ requestBody: {
+ title: options.title || `Dash Export (${new Date().toDateString()})`
+ }
+ };
+ try {
+ const schema: any = await PostToServer(path, parameters);
+ let key = ["document", "presentation"].find(prefix => `${prefix}Id` in schema) + "Id";
+ return schema[key];
+ } catch {
+ return undefined;
}
+ };
+
+ export namespace Docs {
+
+ export type RetrievalResult = Opt<docs_v1.Schema$Document | slides_v1.Schema$Presentation>;
+ export type UpdateResult = Opt<docs_v1.Schema$BatchUpdateDocumentResponse>;
export interface UpdateOptions {
- documentId: DocumentId;
+ documentId: Identifier;
requests: docs_v1.Schema$Request[];
}
@@ -96,46 +130,27 @@ 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.
- */
- export const create = async (options: CreateOptions): Promise<CreationResult> => {
- const path = RouteStore.googleDocs + Actions.Create;
- const parameters = {
- requestBody: {
- title: options.title || `Dash Export (${new Date().toDateString()})`
- }
- };
- try {
- const schema: docs_v1.Schema$Document = await PostToServer(path, parameters);
- const generatedId = schema.documentId;
- if (generatedId) {
- options.handler(generatedId);
- return generatedId;
- }
- } catch {
- return undefined;
- }
- };
+ const KeyMapping = new Map<Service, string>([
+ [Service.Documents, "documentId"],
+ [Service.Slides, "presentationId"]
+ ]);
export const retrieve = async (options: RetrieveOptions): Promise<RetrievalResult> => {
- const path = RouteStore.googleDocs + Actions.Retrieve;
+ const path = `${RouteStore.googleDocs}/${options.service}/${Actions.Retrieve}`;
try {
- const schema: RetrievalResult = await PostToServer(path, options);
- return schema;
+ let parameters: any = {}, key: string | undefined;
+ if ((key = KeyMapping.get(options.service))) {
+ parameters[key] = options.identifier;
+ const schema: RetrievalResult = await PostToServer(path, parameters);
+ return schema;
+ }
} catch {
return undefined;
}
};
export const update = async (options: UpdateOptions): Promise<UpdateResult> => {
- const path = RouteStore.googleDocs + Actions.Update;
+ const path = `${RouteStore.googleDocs}/${Service.Documents}/${Actions.Update}`;
const parameters = {
documentId: options.documentId,
requestBody: {
@@ -151,7 +166,7 @@ export namespace GoogleApiClientUtils {
};
export const read = async (options: ReadOptions): Promise<ReadResult> => {
- return retrieve(options).then(document => {
+ return retrieve({ ...options, service: Service.Documents }).then(document => {
let result: ReadResult = {};
if (document) {
let title = document.title;
@@ -163,7 +178,7 @@ export namespace GoogleApiClientUtils {
};
export const readLines = async (options: ReadOptions): Promise<ReadLinesResult> => {
- return retrieve(options).then(document => {
+ return retrieve({ ...options, service: Service.Documents }).then(document => {
let result: ReadLinesResult = {};
if (document) {
let title = document.title;
@@ -177,14 +192,14 @@ export namespace GoogleApiClientUtils {
export const write = async (options: WriteOptions): Promise<UpdateResult> => {
const requests: docs_v1.Schema$Request[] = [];
- const documentId = await Utils.initialize(options.reference);
- if (!documentId) {
+ const identifier = await Utils.initialize(options.reference);
+ if (!identifier) {
return undefined;
}
let index = options.index;
const mode = options.mode;
if (!(index && mode === WriteMode.Insert)) {
- let schema = await retrieve({ documentId });
+ let schema = await retrieve({ identifier, service: Service.Documents });
if (!schema || !(index = Utils.endOf(schema))) {
return undefined;
}
@@ -210,7 +225,7 @@ export namespace GoogleApiClientUtils {
if (!requests.length) {
return undefined;
}
- let replies: any = await update({ documentId, requests });
+ let replies: any = await update({ documentId: identifier, requests });
let errors = "errors";
if (errors in replies) {
console.log("Write operation failed:");
@@ -221,4 +236,36 @@ export namespace GoogleApiClientUtils {
}
+ export namespace Slides {
+
+ export namespace Utils {
+
+ export const extractTextBoxes = (slides: slides_v1.Schema$Page[]) => {
+ slides.map(slide => {
+ let elements = slide.pageElements;
+ if (elements) {
+ let textboxes: slides_v1.Schema$TextContent[] = [];
+ for (let element of elements) {
+ if (element && element.shape && element.shape.shapeType === "TEXT_BOX" && element.shape.text) {
+ textboxes.push(element.shape.text);
+ }
+ }
+ textboxes.map(text => {
+ if (text.textElements) {
+ text.textElements.map(element => {
+
+ });
+ }
+ if (text.lists) {
+
+ }
+ });
+ }
+ });
+ };
+
+ }
+
+ }
+
} \ No newline at end of file