aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/client/views/nodes/FormattedTextBox.tsx13
-rw-r--r--src/new_fields/RichTextField.ts50
2 files changed, 48 insertions, 15 deletions
diff --git a/src/client/views/nodes/FormattedTextBox.tsx b/src/client/views/nodes/FormattedTextBox.tsx
index 46aed9b2d..5ba2aa0cf 100644
--- a/src/client/views/nodes/FormattedTextBox.tsx
+++ b/src/client/views/nodes/FormattedTextBox.tsx
@@ -12,7 +12,7 @@ import { EditorView } from "prosemirror-view";
import { Doc, Opt, DocListCast } from "../../../new_fields/Doc";
import { Id, Copy } from '../../../new_fields/FieldSymbols';
import { List } from '../../../new_fields/List';
-import { RichTextField } from "../../../new_fields/RichTextField";
+import { RichTextField, ToPlainText, FromPlainText } from "../../../new_fields/RichTextField";
import { createSchema, listSpec, makeInterface } from "../../../new_fields/Schema";
import { BoolCast, Cast, NumCast, StrCast, DateCast } from "../../../new_fields/Types";
import { DocServer } from "../../DocServer";
@@ -296,8 +296,13 @@ export class FormattedTextBox extends DocComponent<(FieldViewProps & FormattedTe
let dataDoc = Doc.GetProto(this.props.Document);
let documentId = StrCast(dataDoc[googleDocKey]);
if (documentId) {
- let contents = await GoogleApiClientUtils.Docs.read({ documentId });
- contents ? console.log(contents) : delete dataDoc[googleDocKey];
+ let exportState = await GoogleApiClientUtils.Docs.read({ documentId });
+ if (exportState) {
+ let data = Cast(dataDoc.data, RichTextField);
+ data && data[FromPlainText](exportState);
+ } else {
+ delete dataDoc[googleDocKey];
+ }
}
}
@@ -693,7 +698,7 @@ export class FormattedTextBox extends DocComponent<(FieldViewProps & FormattedTe
title: StrCast(dataDoc.title),
handler: id => dataDoc[googleDocKey] = id
},
- content: data.plainText()
+ content: data[ToPlainText]()
});
}
diff --git a/src/new_fields/RichTextField.ts b/src/new_fields/RichTextField.ts
index 4f782816c..9d8a1cecb 100644
--- a/src/new_fields/RichTextField.ts
+++ b/src/new_fields/RichTextField.ts
@@ -4,12 +4,14 @@ import { Deserializable } from "../client/util/SerializationHelper";
import { Copy, ToScriptString } from "./FieldSymbols";
import { scriptingGlobal } from "../client/util/Scripting";
+export const ToPlainText = Symbol("PlainText");
+export const FromPlainText = Symbol("PlainText");
+
@scriptingGlobal
@Deserializable("RichTextField")
export class RichTextField extends ObjectField {
@serializable(true)
- readonly Data: string;
- private Extractor = /,\"text\":\"([^\}]*)\"\}/g;
+ Data: string;
constructor(data: string) {
super();
@@ -24,15 +26,41 @@ export class RichTextField extends ObjectField {
return `new RichTextField("${this.Data}")`;
}
- plainText = () => {
- let contents = "";
- let matches: RegExpExecArray | null;
- let considering = this.Data;
- while ((matches = this.Extractor.exec(considering)) !== null) {
- contents += matches[1];
- considering = considering.substring(matches.index + matches[0].length);
- this.Extractor.lastIndex = 0;
+ [ToPlainText]() {
+ let content = JSON.parse(this.Data).doc.content;
+ let paragraphs = content.filter((item: any) => item.type === "paragraph");
+ let output = "";
+ for (let i = 0; i < paragraphs.length; i++) {
+ let paragraph = paragraphs[i];
+ if (paragraph.content) {
+ output += paragraph.content.map((block: any) => block.text).join("");
+ } else {
+ output += i > 0 && paragraphs[i - 1].content ? "\n\n" : "\n";
+ }
}
- return contents.ReplaceAll("\\", "");
+ return output;
+ }
+
+ [FromPlainText](plainText: string) {
+ let elements = plainText.split("\n");
+ let parsed = JSON.parse(this.Data);
+ parsed.doc.content = elements.map(text => {
+ let paragraph: any = { type: "paragraph" };
+ if (text.length) {
+ paragraph.content = [{
+ type: "text",
+ marks: [],
+ text
+ }];
+ }
+ return paragraph;
+ });
+ parsed.selection = {
+ type: "text",
+ anchor: 0,
+ head: 0
+ };
+ this.Data = JSON.stringify(parsed);
}
+
} \ No newline at end of file