aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorSam Wilkins <samwilkins333@gmail.com>2019-08-13 02:06:32 -0400
committerSam Wilkins <samwilkins333@gmail.com>2019-08-13 02:06:32 -0400
commit278cabf67a77edb3dd3bd9bb392550eeb08ab910 (patch)
tree313d676ba868641fb42980eb127e4ec879d0ea70 /src
parentd271205a94bcb2ebc524b70f8a3ff98398e69252 (diff)
more util functions and clean up
Diffstat (limited to 'src')
-rw-r--r--src/client/apis/google_docs/GoogleApiClientUtils.ts71
-rw-r--r--src/client/views/MainView.tsx24
-rw-r--r--src/server/index.ts1
3 files changed, 78 insertions, 18 deletions
diff --git a/src/client/apis/google_docs/GoogleApiClientUtils.ts b/src/client/apis/google_docs/GoogleApiClientUtils.ts
index 42e621f6b..71e5e1073 100644
--- a/src/client/apis/google_docs/GoogleApiClientUtils.ts
+++ b/src/client/apis/google_docs/GoogleApiClientUtils.ts
@@ -1,6 +1,7 @@
import { docs_v1 } from "googleapis";
import { PostToServer } from "../../../Utils";
import { RouteStore } from "../../../server/RouteStore";
+import { Opt } from "../../../new_fields/Doc";
export namespace GoogleApiClientUtils {
@@ -11,12 +12,30 @@ export namespace GoogleApiClientUtils {
Retrieve = "retrieve"
}
- export namespace Helpers {
+ export namespace Utils {
export const fromRgb = (red: number, green: number, blue: number) => {
return { color: { rgbColor: { red, green, blue } } };
};
+ export const extractText = (document: docs_v1.Schema$Document, removeNewlines = false) => {
+ let fragments: string[] = [];
+ if (document.body && document.body.content) {
+ for (let element of document.body.content) {
+ if (element.paragraph && element.paragraph.elements) {
+ for (let inner of element.paragraph.elements) {
+ if (inner && inner.textRun) {
+ let fragment = inner.textRun.content;
+ fragment && fragments.push(fragment);
+ }
+ }
+ }
+ }
+ }
+ let text = fragments.join("");
+ return removeNewlines ? text.ReplaceAll("\n", "") : text;
+ };
+
}
export const ExampleDocumentSchema = {
@@ -24,20 +43,45 @@ export namespace GoogleApiClientUtils {
body: {
content: [
{
+ endIndex: 1,
+ sectionBreak: {
+ sectionStyle: {
+ columnSeparatorStyle: "NONE",
+ contentDirection: "LEFT_TO_RIGHT"
+ }
+ }
+ },
+ {
paragraph: {
elements: [
{
textRun: {
- content: "And this is its bold, blue text!!!",
+ content: "And this is its bold, blue text!!!\n",
textStyle: {
bold: true,
- backgroundColor: Helpers.fromRgb(0, 0, 1)
+ backgroundColor: Utils.fromRgb(0, 0, 1)
}
}
}
]
}
- }
+ },
+ {
+ paragraph: {
+ elements: [
+ {
+ textRun: {
+ content: "And this is its bold, blue text!!!\n",
+ textStyle: {
+ bold: true,
+ backgroundColor: Utils.fromRgb(0, 0, 1)
+ }
+ }
+ }
+ ]
+ }
+ },
+
] as docs_v1.Schema$StructuralElement[]
}
} as docs_v1.Schema$Document;
@@ -66,20 +110,27 @@ export namespace GoogleApiClientUtils {
}
};
- let path = RouteStore.googleDocs + Actions.Retrieve;
- export const Retrieve = async (documentId: string): Promise<any> => {
+ export const Read = async (documentId: string, removeNewlines = false): Promise<Opt<string>> => {
+ return Retrieve(documentId).then(schema => {
+ return schema ? Utils.extractText(schema, removeNewlines) : undefined;
+ });
+ };
+
+ export const Retrieve = async (documentId: string): Promise<Opt<docs_v1.Schema$Document>> => {
+ let path = RouteStore.googleDocs + Actions.Retrieve;
let parameters = { documentId };
- let documentContents: any;
+ let schema: Opt<docs_v1.Schema$Document>;
try {
- documentContents = await PostToServer(path, parameters);
+ schema = await PostToServer(path, parameters);
} catch (e) {
console.error(e);
- documentContents = undefined;
+ schema = undefined;
} finally {
- return documentContents;
+ return schema;
}
};
+
}
} \ No newline at end of file
diff --git a/src/client/views/MainView.tsx b/src/client/views/MainView.tsx
index 0a987de4a..3a5285a66 100644
--- a/src/client/views/MainView.tsx
+++ b/src/client/views/MainView.tsx
@@ -87,6 +87,11 @@ export class MainView extends React.Component {
if (!("presentationView" in doc)) {
doc.presentationView = new List<Doc>([Docs.Create.TreeDocument([], { title: "Presentation" })]);
}
+ if (!("googleDocId" in doc)) {
+ GoogleApiClientUtils.Docs.Create().then(id => {
+ id && (doc.googleDocId = id);
+ });
+ }
CurrentUserUtils.UserDocument.activeWorkspace = doc;
}
}
@@ -132,8 +137,6 @@ export class MainView extends React.Component {
window.removeEventListener("keydown", KeyManager.Instance.handle);
window.addEventListener("keydown", KeyManager.Instance.handle);
- GoogleApiClientUtils.Docs.Create().then(id => console.log(id));
-
reaction(() => {
let workspaces = CurrentUserUtils.UserDocument.workspaces;
let recent = CurrentUserUtils.UserDocument.recentlyClosed;
@@ -151,6 +154,18 @@ export class MainView extends React.Component {
}, { fireImmediately: true });
}
+ componentDidMount() {
+ reaction(() => this.mainContainer, () => {
+ let main = this.mainContainer, googleDocId;
+ if (main && (googleDocId = StrCast(main.googleDocId))) {
+ GoogleApiClientUtils.Docs.Read(googleDocId, true).then(text => {
+ text && Utils.CopyText(text);
+ console.log(text);
+ });
+ }
+ });
+ }
+
componentWillUnMount() {
window.removeEventListener("keydown", KeyManager.Instance.handle);
window.removeEventListener("pointerdown", this.globalPointerDown);
@@ -573,11 +588,6 @@ export class MainView extends React.Component {
render() {
return (
<div id="main-div">
- <input style={{ position: "absolute", zIndex: 100000 }} onKeyPress={e => {
- if (e.which === 13) {
- GoogleApiClientUtils.Docs.Retrieve(e.currentTarget.value.trim()).then((res: any) => console.log(res));
- }
- }} />
{this.dictationOverlay}
<DocumentDecorations />
{this.mainContent}
diff --git a/src/server/index.ts b/src/server/index.ts
index 9986e62d6..4ccb61681 100644
--- a/src/server/index.ts
+++ b/src/server/index.ts
@@ -791,7 +791,6 @@ const token = path.join(__dirname, "./credentials/google_docs_token.json");
app.post(RouteStore.googleDocs + ":action", (req, res) => {
let parameters = req.body;
- console.log(parameters, req.params.action);
GoogleApiServerUtils.Docs.GetEndpoint({ credentials, token }).then(endpoint => {
let results: Promise<any> | undefined;